2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file fs/fs_download.c
22 * @brief download methods
23 * @author Christian Grothoff
26 * - location URI suppport (can wait, easy)
27 * - different priority for scheduling probe downloads?
28 * - check if iblocks can be computed from existing blocks (can wait, hard)
31 #include "gnunet_constants.h"
32 #include "gnunet_fs_service.h"
36 #define DEBUG_DOWNLOAD GNUNET_NO
39 * Determine if the given download (options and meta data) should cause
40 * use to try to do a recursive download.
43 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
45 return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
46 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
47 ( (dc->meta == NULL) &&
48 ( (NULL == dc->filename) ||
49 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
51 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
52 GNUNET_FS_DIRECTORY_EXT)) ) ) ) );
57 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
58 * only have to truncate the file once we're done).
60 * Given the offset of a block (with respect to the DBLOCKS) and its
61 * depth, return the offset where we would store this block in the
64 * @param fsize overall file size
65 * @param off offset of the block in the file
66 * @param depth depth of the block in the tree
67 * @param treedepth maximum depth of the tree
68 * @return off for DBLOCKS (depth == treedepth),
69 * otherwise an offset past the end
70 * of the file that does not overlap
71 * with the range for any other block
74 compute_disk_offset (uint64_t fsize,
77 unsigned int treedepth)
80 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
81 uint64_t loff; /* where do IBlocks for depth "i" start? */
82 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
84 if (depth == treedepth)
86 /* first IBlocks start at the end of file, rounded up
87 to full DBLOCK_SIZE */
88 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
89 lsize = ( (fsize + DBLOCK_SIZE-1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
90 GNUNET_assert (0 == (off % DBLOCK_SIZE));
91 ioff = (off / DBLOCK_SIZE);
92 for (i=treedepth-1;i>depth;i--)
95 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
96 GNUNET_assert (lsize > 0);
97 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
98 ioff /= CHK_PER_INODE;
100 return loff + ioff * sizeof (struct ContentHashKey);
105 * Given a file of the specified treedepth and a block at the given
106 * offset and depth, calculate the offset for the CHK at the given
109 * @param offset the offset of the first
110 * DBLOCK in the subtree of the
112 * @param depth the depth of the IBLOCK in the tree
113 * @param treedepth overall depth of the tree
114 * @param k which CHK in the IBLOCK are we
116 * @return offset if k=0, otherwise an appropriately
117 * larger value (i.e., if depth = treedepth-1,
118 * the returned value should be offset+DBLOCK_SIZE)
121 compute_dblock_offset (uint64_t offset,
123 unsigned int treedepth,
127 uint64_t lsize; /* what is the size of the sum of all DBlocks
128 that a CHK at depth i corresponds to? */
130 if (depth == treedepth)
133 for (i=treedepth-1;i>depth;i--)
134 lsize *= CHK_PER_INODE;
135 return offset + k * lsize;
140 * Fill in all of the generic fields for a download event and call the
143 * @param pi structure to fill in
144 * @param dc overall download context
147 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
148 struct GNUNET_FS_DownloadContext *dc)
150 pi->value.download.dc = dc;
151 pi->value.download.cctx
153 pi->value.download.pctx
154 = (dc->parent == NULL) ? NULL : dc->parent->client_info;
155 pi->value.download.sctx
156 = (dc->search == NULL) ? NULL : dc->search->client_info;
157 pi->value.download.uri
159 pi->value.download.filename
161 pi->value.download.size
163 pi->value.download.duration
164 = GNUNET_TIME_absolute_get_duration (dc->start_time);
165 pi->value.download.completed
167 pi->value.download.anonymity
169 pi->value.download.eta
170 = GNUNET_TIME_calculate_eta (dc->start_time,
173 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
174 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
177 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL,
182 * We're ready to transmit a search request to the
183 * file-sharing service. Do it. If there is
184 * more than one request pending, try to send
185 * multiple or request another transmission.
188 * @param size number of bytes available in buf
189 * @param buf where the callee should write the message
190 * @return number of bytes written to buf
193 transmit_download_request (void *cls,
199 * Closure for iterator processing results.
201 struct ProcessResultClosure
207 GNUNET_HashCode query;
210 * Data found in P2P network.
215 * Our download context.
217 struct GNUNET_FS_DownloadContext *dc;
220 * Number of bytes in data.
227 enum GNUNET_BLOCK_Type type;
230 * Flag to indicate if this block should be stored on disk.
238 * Iterator over entries in the pending requests in the 'active' map for the
239 * reply that we just got.
241 * @param cls closure (our 'struct ProcessResultClosure')
242 * @param key query for the given value / request
243 * @param value value in the hash map (a 'struct DownloadRequest')
244 * @return GNUNET_YES (we should continue to iterate); unless serious error
247 process_result_with_request (void *cls,
248 const GNUNET_HashCode * key,
254 * Schedule the download of the specified block in the tree.
256 * @param dc overall download this block belongs to
257 * @param chk content-hash-key of the block
258 * @param offset offset of the block in the file
259 * (for IBlocks, the offset is the lowest
260 * offset of any DBlock in the subtree under
262 * @param depth depth of the block, 0 is the root of the tree
265 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
266 const struct ContentHashKey *chk,
270 struct DownloadRequest *sm;
274 char block[DBLOCK_SIZE];
276 struct ProcessResultClosure prc;
277 struct GNUNET_DISK_FileHandle *fh;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Scheduling download at offset %llu and depth %u for `%s'\n",
282 (unsigned long long) offset,
284 GNUNET_h2s (&chk->query));
286 total = GNUNET_ntohll (dc->uri->data.chk.file_length);
287 off = compute_disk_offset (total,
291 len = GNUNET_FS_tree_calculate_block_size (total,
295 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
299 sm->is_pending = GNUNET_YES;
300 sm->next = dc->pending;
302 GNUNET_CONTAINER_multihashmap_put (dc->active,
305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
307 if ( (dc->old_file_size > off) &&
308 (dc->filename != NULL) )
309 fh = GNUNET_DISK_file_open (dc->filename,
310 GNUNET_DISK_OPEN_READ,
311 GNUNET_DISK_PERM_NONE);
314 GNUNET_DISK_file_seek (fh,
316 GNUNET_DISK_SEEK_SET) ) &&
318 GNUNET_DISK_file_read (fh,
322 GNUNET_CRYPTO_hash (block, len, &key);
323 if (0 == memcmp (&key,
325 sizeof (GNUNET_HashCode)))
328 struct GNUNET_CRYPTO_AesSessionKey sk;
329 struct GNUNET_CRYPTO_AesInitializationVector iv;
330 GNUNET_HashCode query;
332 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
333 GNUNET_CRYPTO_aes_encrypt (block, len,
337 GNUNET_CRYPTO_hash (enc, len, &query);
338 if (0 == memcmp (&query,
340 sizeof (GNUNET_HashCode)))
342 /* already got it! */
346 prc.type = (dc->treedepth == depth)
347 ? GNUNET_BLOCK_TYPE_DBLOCK
348 : GNUNET_BLOCK_TYPE_IBLOCK;
349 prc.query = chk->query;
350 prc.do_store = GNUNET_NO; /* useless */
351 process_result_with_request (&prc,
359 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
364 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
365 if (depth < dc->treedepth)
367 // FIXME: try if we could
368 // reconstitute this IBLOCK
369 // from the existing blocks on disk (can wait)
370 // (read block(s), encode, compare with
371 // query; if matches, simply return)
374 if ( (dc->th == NULL) &&
375 (dc->client != NULL) )
376 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
377 sizeof (struct SearchMessage),
378 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
380 &transmit_download_request,
387 * Suggest a filename based on given metadata.
389 * @param md given meta data
390 * @return NULL if meta data is useless for suggesting a filename
393 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
395 static const char *mimeMap[][2] = {
396 {"application/bz2", ".bz2"},
397 {"application/gnunet-directory", ".gnd"},
398 {"application/java", ".class"},
399 {"application/msword", ".doc"},
400 {"application/ogg", ".ogg"},
401 {"application/pdf", ".pdf"},
402 {"application/pgp-keys", ".key"},
403 {"application/pgp-signature", ".pgp"},
404 {"application/postscript", ".ps"},
405 {"application/rar", ".rar"},
406 {"application/rtf", ".rtf"},
407 {"application/xml", ".xml"},
408 {"application/x-debian-package", ".deb"},
409 {"application/x-dvi", ".dvi"},
410 {"applixation/x-flac", ".flac"},
411 {"applixation/x-gzip", ".gz"},
412 {"application/x-java-archive", ".jar"},
413 {"application/x-java-vm", ".class"},
414 {"application/x-python-code", ".pyc"},
415 {"application/x-redhat-package-manager", ".rpm"},
416 {"application/x-rpm", ".rpm"},
417 {"application/x-tar", ".tar"},
418 {"application/x-tex-pk", ".pk"},
419 {"application/x-texinfo", ".texinfo"},
420 {"application/x-xcf", ".xcf"},
421 {"application/x-xfig", ".xfig"},
422 {"application/zip", ".zip"},
424 {"audio/midi", ".midi"},
425 {"audio/mpeg", ".mp3"},
426 {"audio/real", ".rm"},
427 {"audio/x-wav", ".wav"},
429 {"image/gif", ".gif"},
430 {"image/jpeg", ".jpg"},
431 {"image/pcx", ".pcx"},
432 {"image/png", ".png"},
433 {"image/tiff", ".tiff"},
434 {"image/x-ms-bmp", ".bmp"},
435 {"image/x-xpixmap", ".xpm"},
437 {"text/css", ".css"},
438 {"text/html", ".html"},
439 {"text/plain", ".txt"},
440 {"text/rtf", ".rtf"},
441 {"text/x-c++hdr", ".h++"},
442 {"text/x-c++src", ".c++"},
443 {"text/x-chdr", ".h"},
444 {"text/x-csrc", ".c"},
445 {"text/x-java", ".java"},
446 {"text/x-moc", ".moc"},
447 {"text/x-pascal", ".pas"},
448 {"text/x-perl", ".pl"},
449 {"text/x-python", ".py"},
450 {"text/x-tex", ".tex"},
452 {"video/avi", ".avi"},
453 {"video/mpeg", ".mpeg"},
454 {"video/quicktime", ".qt"},
455 {"video/real", ".rm"},
456 {"video/x-msvideo", ".avi"},
465 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
466 EXTRACTOR_METATYPE_FILENAME);
470 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
471 EXTRACTOR_METATYPE_MIMETYPE);
475 while ( (mimeMap[i][0] != NULL) &&
476 (0 != strcmp (mime, mimeMap[i][0])))
478 if (mimeMap[i][1] == NULL)
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
480 GNUNET_ERROR_TYPE_BULK,
481 _("Did not find mime type `%s' in extension list.\n"),
487 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
488 EXTRACTOR_METATYPE_TITLE,
489 EXTRACTOR_METATYPE_BOOK_TITLE,
490 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
491 EXTRACTOR_METATYPE_PACKAGE_NAME,
492 EXTRACTOR_METATYPE_URL,
493 EXTRACTOR_METATYPE_URI,
494 EXTRACTOR_METATYPE_DESCRIPTION,
495 EXTRACTOR_METATYPE_ISRC,
496 EXTRACTOR_METATYPE_JOURNAL_NAME,
497 EXTRACTOR_METATYPE_AUTHOR_NAME,
498 EXTRACTOR_METATYPE_SUBJECT,
499 EXTRACTOR_METATYPE_ALBUM,
500 EXTRACTOR_METATYPE_ARTIST,
501 EXTRACTOR_METATYPE_KEYWORDS,
502 EXTRACTOR_METATYPE_COMMENT,
503 EXTRACTOR_METATYPE_UNKNOWN,
505 if ( (base == NULL) &&
509 return GNUNET_strdup (ext);
512 GNUNET_asprintf (&ret,
522 * We've lost our connection with the FS service.
523 * Re-establish it and re-transmit all of our
526 * @param dc download context that is having trouble
529 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
533 * We found an entry in a directory. Check if the respective child
534 * already exists and if not create the respective child download.
536 * @param cls the parent download
537 * @param filename name of the file in the directory
538 * @param uri URI of the file (CHK or LOC)
539 * @param meta meta data of the file
540 * @param length number of bytes in data
541 * @param data contents of the file (or NULL if they were not inlined)
544 trigger_recursive_download (void *cls,
545 const char *filename,
546 const struct GNUNET_FS_Uri *uri,
547 const struct GNUNET_CONTAINER_MetaData *meta,
553 * We're done downloading a directory. Open the file and
554 * trigger all of the (remaining) child downloads.
556 * @param dc context of download that just completed
559 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
564 struct GNUNET_DISK_FileHandle *h;
565 struct GNUNET_DISK_MapHandle *m;
567 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
568 size = (size_t) size64;
569 if (size64 != (uint64_t) size)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
575 if (dc->filename != NULL)
577 h = GNUNET_DISK_file_open (dc->filename,
578 GNUNET_DISK_OPEN_READ,
579 GNUNET_DISK_PERM_NONE);
583 GNUNET_assert (dc->temp_filename != NULL);
584 h = GNUNET_DISK_file_open (dc->temp_filename,
585 GNUNET_DISK_OPEN_READ,
586 GNUNET_DISK_PERM_NONE);
590 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 _("Directory too large for system address space\n"));
598 GNUNET_FS_directory_list_contents (size,
601 &trigger_recursive_download,
603 GNUNET_DISK_file_unmap (m);
605 GNUNET_DISK_file_close (h);
606 if (dc->filename == NULL)
608 if (0 != UNLINK (dc->temp_filename))
609 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
612 GNUNET_free (dc->temp_filename);
613 dc->temp_filename = NULL;
619 * Check if all child-downloads have completed and
620 * if so, signal completion (and possibly recurse to
624 check_completed (struct GNUNET_FS_DownloadContext *dc)
626 struct GNUNET_FS_ProgressInfo pi;
627 struct GNUNET_FS_DownloadContext *pos;
629 pos = dc->child_head;
632 if ( (pos->emsg == NULL) &&
633 (pos->completed < pos->length) )
634 return; /* not done yet */
635 if ( (pos->child_head != NULL) &&
636 (pos->has_finished != GNUNET_YES) )
637 return; /* not transitively done yet */
640 dc->has_finished = GNUNET_YES;
641 GNUNET_FS_download_sync_ (dc);
642 /* signal completion */
643 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
644 GNUNET_FS_download_make_status_ (&pi, dc);
645 if (dc->parent != NULL)
646 check_completed (dc->parent);
651 * We found an entry in a directory. Check if the respective child
652 * already exists and if not create the respective child download.
654 * @param cls the parent download
655 * @param filename name of the file in the directory
656 * @param uri URI of the file (CHK or LOC)
657 * @param meta meta data of the file
658 * @param length number of bytes in data
659 * @param data contents of the file (or NULL if they were not inlined)
662 trigger_recursive_download (void *cls,
663 const char *filename,
664 const struct GNUNET_FS_Uri *uri,
665 const struct GNUNET_CONTAINER_MetaData *meta,
669 struct GNUNET_FS_DownloadContext *dc = cls;
670 struct GNUNET_FS_DownloadContext *cpos;
671 struct GNUNET_DISK_FileHandle *fh;
673 const char *real_name;
681 return; /* entry for the directory itself */
682 cpos = dc->child_head;
685 if ( (GNUNET_FS_uri_test_equal (uri,
687 ( (filename != NULL) &&
688 (0 == strcmp (cpos->filename,
694 return; /* already exists */
696 if (NULL == filename)
698 fn = GNUNET_FS_meta_data_suggest_filename (meta);
701 us = GNUNET_FS_uri_to_string (uri);
702 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_PREFIX
703 GNUNET_FS_URI_CHK_INFIX)]);
706 else if (fn[0] == '.')
709 us = GNUNET_FS_uri_to_string (uri);
710 GNUNET_asprintf (&fn,
712 &us[strlen (GNUNET_FS_URI_PREFIX
713 GNUNET_FS_URI_CHK_INFIX)], ext);
719 if (dc->filename == NULL)
725 dn = GNUNET_strdup (dc->filename);
726 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
728 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
729 GNUNET_FS_DIRECTORY_EXT)) );
730 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
732 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
733 GNUNET_FS_DIRECTORY_EXT)) )
734 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
735 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
736 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
738 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
739 GNUNET_FS_DIRECTORY_EXT)) ) )
741 GNUNET_asprintf (&full_name,
746 GNUNET_FS_DIRECTORY_EXT);
750 GNUNET_asprintf (&full_name,
758 if ( (full_name != NULL) &&
760 GNUNET_DISK_directory_create_for_file (full_name)) )
762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
763 _("Failed to create directory for recursive download of `%s'\n"),
765 GNUNET_free (full_name);
766 GNUNET_free_non_null (fn);
771 if ( (data != NULL) &&
772 (GNUNET_FS_uri_chk_get_file_size (uri) == length) )
774 if (full_name == NULL)
776 temp_name = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
777 real_name = temp_name;
781 real_name = full_name;
783 /* write to disk, then trigger normal download which will instantly progress to completion */
784 fh = GNUNET_DISK_file_open (real_name,
785 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
786 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
789 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
792 GNUNET_free (full_name);
793 GNUNET_free_non_null (fn);
797 GNUNET_DISK_file_write (fh,
801 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
805 GNUNET_DISK_file_close (fh);
807 GNUNET_FS_download_start (dc->h,
810 full_name, temp_name,
812 GNUNET_FS_uri_chk_get_file_size (uri),
817 GNUNET_free_non_null (full_name);
818 GNUNET_free_non_null (temp_name);
819 GNUNET_free_non_null (fn);
824 * Free entries in the map.
826 * @param cls unused (NULL)
828 * @param entry entry of type "struct DownloadRequest" which is freed
832 free_entry (void *cls,
833 const GNUNET_HashCode *key,
842 * Iterator over entries in the pending requests in the 'active' map for the
843 * reply that we just got.
845 * @param cls closure (our 'struct ProcessResultClosure')
846 * @param key query for the given value / request
847 * @param value value in the hash map (a 'struct DownloadRequest')
848 * @return GNUNET_YES (we should continue to iterate); unless serious error
851 process_result_with_request (void *cls,
852 const GNUNET_HashCode * key,
855 struct ProcessResultClosure *prc = cls;
856 struct DownloadRequest *sm = value;
857 struct DownloadRequest *ppos;
858 struct DownloadRequest *pprev;
859 struct GNUNET_DISK_FileHandle *fh;
860 struct GNUNET_FS_DownloadContext *dc = prc->dc;
861 struct GNUNET_CRYPTO_AesSessionKey skey;
862 struct GNUNET_CRYPTO_AesInitializationVector iv;
864 struct GNUNET_FS_ProgressInfo pi;
869 struct ContentHashKey *chk;
872 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
884 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
887 GNUNET_assert (GNUNET_YES ==
888 GNUNET_CONTAINER_multihashmap_remove (dc->active,
891 /* if this request is on the pending list, remove it! */
899 dc->pending = ppos->next;
901 pprev->next = ppos->next;
907 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
908 GNUNET_CRYPTO_aes_decrypt (prc->data,
913 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
918 if ( ( GNUNET_YES == prc->do_store) &&
919 ( (dc->filename != NULL) ||
920 (is_recursive_download (dc)) ) &&
921 ( (sm->depth == dc->treedepth) ||
922 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
924 fh = GNUNET_DISK_file_open (dc->filename != NULL
927 GNUNET_DISK_OPEN_READWRITE |
928 GNUNET_DISK_OPEN_CREATE,
929 GNUNET_DISK_PERM_USER_READ |
930 GNUNET_DISK_PERM_USER_WRITE |
931 GNUNET_DISK_PERM_GROUP_READ |
932 GNUNET_DISK_PERM_OTHER_READ);
935 (GNUNET_YES == prc->do_store) &&
936 ( (dc->filename != NULL) ||
937 (is_recursive_download (dc)) ) &&
938 ( (sm->depth == dc->treedepth) ||
939 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
941 GNUNET_asprintf (&dc->emsg,
942 _("Download failed: could not open file `%s': %s\n"),
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "Saving decrypted block to disk at offset %llu\n",
952 (unsigned long long) off);
955 GNUNET_DISK_file_seek (fh,
957 GNUNET_DISK_SEEK_SET) ) )
959 GNUNET_asprintf (&dc->emsg,
960 _("Failed to seek to offset %llu in file `%s': %s\n"),
961 (unsigned long long) off,
967 GNUNET_DISK_file_write (fh,
971 GNUNET_asprintf (&dc->emsg,
972 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
973 (unsigned int) prc->size,
974 (unsigned long long) off,
979 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
982 if (sm->depth == dc->treedepth)
985 if (sm->offset < dc->offset)
987 /* starting offset begins in the middle of pt,
988 do not count first bytes as progress */
989 GNUNET_assert (app > (dc->offset - sm->offset));
990 app -= (dc->offset - sm->offset);
992 if (sm->offset + prc->size > dc->offset + dc->length)
994 /* end of block is after relevant range,
995 do not count last bytes as progress */
996 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
997 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
999 dc->completed += app;
1001 /* do recursive download if option is set and either meta data
1002 says it is a directory or if no meta data is given AND filename
1003 ends in '.gnd' (top-level case) */
1004 if (is_recursive_download (dc))
1005 GNUNET_FS_directory_list_contents (prc->size,
1008 &trigger_recursive_download,
1012 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1013 pi.value.download.specifics.progress.data = pt;
1014 pi.value.download.specifics.progress.offset = sm->offset;
1015 pi.value.download.specifics.progress.data_len = prc->size;
1016 pi.value.download.specifics.progress.depth = sm->depth;
1017 GNUNET_FS_download_make_status_ (&pi, dc);
1018 GNUNET_assert (dc->completed <= dc->length);
1019 if (dc->completed == dc->length)
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "Download completed, truncating file to desired length %llu\n",
1024 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1026 /* truncate file to size (since we store IBlocks at the end) */
1027 if (dc->filename != NULL)
1029 if (0 != truncate (dc->filename,
1030 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1031 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1035 if (dc->job_queue != NULL)
1037 GNUNET_FS_dequeue_ (dc->job_queue);
1038 dc->job_queue = NULL;
1040 if (is_recursive_download (dc))
1041 full_recursive_download (dc);
1042 if (dc->child_head == NULL)
1044 /* signal completion */
1045 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
1046 GNUNET_FS_download_make_status_ (&pi, dc);
1047 if (dc->parent != NULL)
1048 check_completed (dc->parent);
1050 GNUNET_assert (sm->depth == dc->treedepth);
1052 if (sm->depth == dc->treedepth)
1054 GNUNET_FS_download_sync_ (dc);
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1062 (unsigned long long) sm->offset);
1064 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1065 chk = (struct ContentHashKey*) pt;
1066 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1068 off = compute_dblock_offset (sm->offset,
1072 if ( (off + DBLOCK_SIZE >= dc->offset) &&
1073 (off < dc->offset + dc->length) )
1074 schedule_block_download (dc,
1080 GNUNET_FS_download_sync_ (dc);
1085 GNUNET_DISK_file_close (fh);
1086 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1087 pi.value.download.specifics.error.message = dc->emsg;
1088 GNUNET_FS_download_make_status_ (&pi, dc);
1089 /* abort all pending requests */
1092 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1095 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1096 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1102 GNUNET_FS_download_sync_ (dc);
1108 * Process a download result.
1110 * @param dc our download context
1111 * @param type type of the result
1112 * @param data the (encrypted) response
1113 * @param size size of data
1116 process_result (struct GNUNET_FS_DownloadContext *dc,
1117 enum GNUNET_BLOCK_Type type,
1121 struct ProcessResultClosure prc;
1127 prc.do_store = GNUNET_YES;
1128 GNUNET_CRYPTO_hash (data, size, &prc.query);
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Received result for query `%s' from `%s'-service\n",
1132 GNUNET_h2s (&prc.query),
1135 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1137 &process_result_with_request,
1143 * Type of a function to call when we receive a message
1146 * @param cls closure
1147 * @param msg message received, NULL on timeout or fatal error
1150 receive_results (void *cls,
1151 const struct GNUNET_MessageHeader * msg)
1153 struct GNUNET_FS_DownloadContext *dc = cls;
1154 const struct PutMessage *cm;
1157 if ( (NULL == msg) ||
1158 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1159 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1161 GNUNET_break (msg == NULL);
1165 msize = ntohs(msg->size);
1166 cm = (const struct PutMessage*) msg;
1170 msize - sizeof (struct PutMessage));
1171 if (dc->client == NULL)
1172 return; /* fatal error */
1173 /* continue receiving */
1174 GNUNET_CLIENT_receive (dc->client,
1177 GNUNET_TIME_UNIT_FOREVER_REL);
1183 * We're ready to transmit a search request to the
1184 * file-sharing service. Do it. If there is
1185 * more than one request pending, try to send
1186 * multiple or request another transmission.
1188 * @param cls closure
1189 * @param size number of bytes available in buf
1190 * @param buf where the callee should write the message
1191 * @return number of bytes written to buf
1194 transmit_download_request (void *cls,
1198 struct GNUNET_FS_DownloadContext *dc = cls;
1200 struct SearchMessage *sm;
1208 GNUNET_assert (size >= sizeof (struct SearchMessage));
1211 while ( (dc->pending != NULL) &&
1212 (size > msize + sizeof (struct SearchMessage)) )
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Transmitting download request for `%s' to `%s'-service\n",
1217 GNUNET_h2s (&dc->pending->chk.query),
1220 memset (sm, 0, sizeof (struct SearchMessage));
1221 sm->header.size = htons (sizeof (struct SearchMessage));
1222 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1223 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1224 sm->options = htonl (1);
1226 sm->options = htonl (0);
1227 if (dc->pending->depth == dc->treedepth)
1228 sm->type = htonl (GNUNET_BLOCK_TYPE_DBLOCK);
1230 sm->type = htonl (GNUNET_BLOCK_TYPE_IBLOCK);
1231 sm->anonymity_level = htonl (dc->anonymity);
1232 sm->target = dc->target.hashPubKey;
1233 sm->query = dc->pending->chk.query;
1234 dc->pending->is_pending = GNUNET_NO;
1235 dc->pending = dc->pending->next;
1236 msize += sizeof (struct SearchMessage);
1239 if (dc->pending != NULL)
1240 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1241 sizeof (struct SearchMessage),
1242 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1244 &transmit_download_request,
1251 * Reconnect to the FS service and transmit our queries NOW.
1253 * @param cls our download context
1257 do_reconnect (void *cls,
1258 const struct GNUNET_SCHEDULER_TaskContext *tc)
1260 struct GNUNET_FS_DownloadContext *dc = cls;
1261 struct GNUNET_CLIENT_Connection *client;
1263 dc->task = GNUNET_SCHEDULER_NO_TASK;
1264 client = GNUNET_CLIENT_connect (dc->h->sched,
1269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1270 "Connecting to `%s'-service failed, will try again.\n",
1275 dc->client = client;
1276 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1277 sizeof (struct SearchMessage),
1278 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1280 &transmit_download_request,
1282 GNUNET_CLIENT_receive (client,
1285 GNUNET_TIME_UNIT_FOREVER_REL);
1290 * Add entries that are not yet pending back to the pending list.
1292 * @param cls our download context
1294 * @param entry entry of type "struct DownloadRequest"
1298 retry_entry (void *cls,
1299 const GNUNET_HashCode *key,
1302 struct GNUNET_FS_DownloadContext *dc = cls;
1303 struct DownloadRequest *dr = entry;
1305 if (! dr->is_pending)
1307 dr->next = dc->pending;
1308 dr->is_pending = GNUNET_YES;
1309 dc->pending = entry;
1316 * We've lost our connection with the FS service.
1317 * Re-establish it and re-transmit all of our
1320 * @param dc download context that is having trouble
1323 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1326 if (NULL != dc->client)
1330 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1333 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1336 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1340 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1341 GNUNET_TIME_UNIT_SECONDS,
1349 * We're allowed to ask the FS service for our blocks. Start the download.
1351 * @param cls the 'struct GNUNET_FS_DownloadContext'
1352 * @param client handle to use for communcation with FS (we must destroy it!)
1355 activate_fs_download (void *cls,
1356 struct GNUNET_CLIENT_Connection *client)
1358 struct GNUNET_FS_DownloadContext *dc = cls;
1359 struct GNUNET_FS_ProgressInfo pi;
1361 GNUNET_assert (NULL != client);
1362 GNUNET_assert (dc->client == NULL);
1363 GNUNET_assert (dc->th == NULL);
1364 dc->client = client;
1365 GNUNET_CLIENT_receive (client,
1368 GNUNET_TIME_UNIT_FOREVER_REL);
1369 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1370 GNUNET_FS_download_make_status_ (&pi, dc);
1371 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1374 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1375 sizeof (struct SearchMessage),
1376 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1378 &transmit_download_request,
1384 * We must stop to ask the FS service for our blocks. Pause the download.
1386 * @param cls the 'struct GNUNET_FS_DownloadContext'
1389 deactivate_fs_download (void *cls)
1391 struct GNUNET_FS_DownloadContext *dc = cls;
1392 struct GNUNET_FS_ProgressInfo pi;
1396 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1399 if (NULL != dc->client)
1401 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1404 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1405 GNUNET_FS_download_make_status_ (&pi, dc);
1410 * Create SUSPEND event for the given download operation
1411 * and then clean up our state (without stop signal).
1413 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1416 GNUNET_FS_download_signal_suspend_ (void *cls)
1418 struct GNUNET_FS_DownloadContext *dc = cls;
1419 struct GNUNET_FS_ProgressInfo pi;
1421 if (dc->top != NULL)
1422 GNUNET_FS_end_top (dc->h, dc->top);
1423 while (NULL != dc->child_head)
1424 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1425 if (dc->search != NULL)
1427 dc->search->download = NULL;
1430 if (dc->job_queue != NULL)
1432 GNUNET_FS_dequeue_ (dc->job_queue);
1433 dc->job_queue = NULL;
1435 if (dc->parent != NULL)
1436 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1437 dc->parent->child_tail,
1439 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1440 GNUNET_FS_download_make_status_ (&pi, dc);
1441 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1442 GNUNET_SCHEDULER_cancel (dc->h->sched,
1444 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1447 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1448 GNUNET_free_non_null (dc->filename);
1449 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1450 GNUNET_FS_uri_destroy (dc->uri);
1451 GNUNET_free_non_null (dc->temp_filename);
1452 GNUNET_free_non_null (dc->serialization);
1458 * Download parts of a file. Note that this will store
1459 * the blocks at the respective offset in the given file. Also, the
1460 * download is still using the blocking of the underlying FS
1461 * encoding. As a result, the download may *write* outside of the
1462 * given boundaries (if offset and length do not match the 32k FS
1463 * block boundaries). <p>
1465 * This function should be used to focus a download towards a
1466 * particular portion of the file (optimization), not to strictly
1467 * limit the download to exactly those bytes.
1469 * @param h handle to the file sharing subsystem
1470 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1471 * @param meta known metadata for the file (can be NULL)
1472 * @param filename where to store the file, maybe NULL (then no file is
1473 * created on disk and data must be grabbed from the callbacks)
1474 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1475 * can be NULL (in which case we will pick a name if needed); the temporary file
1476 * may already exist, in which case we will try to use the data that is there and
1477 * if it is not what is desired, will overwrite it
1478 * @param offset at what offset should we start the download (typically 0)
1479 * @param length how many bytes should be downloaded starting at offset
1480 * @param anonymity anonymity level to use for the download
1481 * @param options various options
1482 * @param cctx initial value for the client context for this download
1483 * @param parent parent download to associate this download with (use NULL
1484 * for top-level downloads; useful for manually-triggered recursive downloads)
1485 * @return context that can be used to control this download
1487 struct GNUNET_FS_DownloadContext *
1488 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1489 const struct GNUNET_FS_Uri *uri,
1490 const struct GNUNET_CONTAINER_MetaData *meta,
1491 const char *filename,
1492 const char *tempname,
1496 enum GNUNET_FS_DownloadOptions options,
1498 struct GNUNET_FS_DownloadContext *parent)
1500 struct GNUNET_FS_ProgressInfo pi;
1501 struct GNUNET_FS_DownloadContext *dc;
1503 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1504 if ( (offset + length < offset) ||
1505 (offset + length > uri->data.chk.file_length) )
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Starting download `%s' of %llu bytes\n",
1514 (unsigned long long) length);
1516 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1518 dc->parent = parent;
1521 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1525 dc->uri = GNUNET_FS_uri_dup (uri);
1526 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1527 dc->client_info = cctx;
1528 dc->start_time = GNUNET_TIME_absolute_get ();
1529 if (NULL != filename)
1531 dc->filename = GNUNET_strdup (filename);
1532 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1533 GNUNET_DISK_file_size (filename,
1537 if (GNUNET_FS_uri_test_loc (dc->uri))
1538 GNUNET_assert (GNUNET_OK ==
1539 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1541 dc->offset = offset;
1542 dc->length = length;
1543 dc->anonymity = anonymity;
1544 dc->options = options;
1545 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1546 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1547 if ( (filename == NULL) &&
1548 (is_recursive_download (dc) ) )
1550 if (tempname != NULL)
1551 dc->temp_filename = GNUNET_strdup (tempname);
1553 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "Download tree has depth %u\n",
1563 dc->top = GNUNET_FS_make_top (dc->h,
1564 &GNUNET_FS_download_signal_suspend_,
1567 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1568 pi.value.download.specifics.start.meta = meta;
1569 GNUNET_FS_download_make_status_ (&pi, dc);
1570 schedule_block_download (dc,
1571 &dc->uri->data.chk.chk,
1573 1 /* 0 == CHK, 1 == top */);
1574 GNUNET_FS_download_sync_ (dc);
1575 GNUNET_FS_download_start_downloading_ (dc);
1581 * Download parts of a file based on a search result. The download
1582 * will be associated with the search result (and the association
1583 * will be preserved when serializing/deserializing the state).
1584 * If the search is stopped, the download will not be aborted but
1585 * be 'promoted' to a stand-alone download.
1587 * As with the other download function, this will store
1588 * the blocks at the respective offset in the given file. Also, the
1589 * download is still using the blocking of the underlying FS
1590 * encoding. As a result, the download may *write* outside of the
1591 * given boundaries (if offset and length do not match the 32k FS
1592 * block boundaries). <p>
1594 * The given range can be used to focus a download towards a
1595 * particular portion of the file (optimization), not to strictly
1596 * limit the download to exactly those bytes.
1598 * @param h handle to the file sharing subsystem
1599 * @param sr the search result to use for the download (determines uri and
1600 * meta data and associations)
1601 * @param filename where to store the file, maybe NULL (then no file is
1602 * created on disk and data must be grabbed from the callbacks)
1603 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1604 * can be NULL (in which case we will pick a name if needed); the temporary file
1605 * may already exist, in which case we will try to use the data that is there and
1606 * if it is not what is desired, will overwrite it
1607 * @param offset at what offset should we start the download (typically 0)
1608 * @param length how many bytes should be downloaded starting at offset
1609 * @param anonymity anonymity level to use for the download
1610 * @param options various download options
1611 * @param cctx initial value for the client context for this download
1612 * @return context that can be used to control this download
1614 struct GNUNET_FS_DownloadContext *
1615 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
1616 struct GNUNET_FS_SearchResult *sr,
1617 const char *filename,
1618 const char *tempname,
1622 enum GNUNET_FS_DownloadOptions options,
1625 struct GNUNET_FS_ProgressInfo pi;
1626 struct GNUNET_FS_DownloadContext *dc;
1628 if (sr->download != NULL)
1633 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri));
1634 if ( (offset + length < offset) ||
1635 (offset + length > sr->uri->data.chk.file_length) )
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "Starting download `%s' of %llu bytes\n",
1644 (unsigned long long) length);
1646 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1650 if (sr->probe_ctx != NULL)
1652 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1653 sr->probe_ctx = NULL;
1655 dc->uri = GNUNET_FS_uri_dup (sr->uri);
1656 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
1657 dc->client_info = cctx;
1658 dc->start_time = GNUNET_TIME_absolute_get ();
1659 if (NULL != filename)
1661 dc->filename = GNUNET_strdup (filename);
1662 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1663 GNUNET_DISK_file_size (filename,
1667 if (GNUNET_FS_uri_test_loc (dc->uri))
1668 GNUNET_assert (GNUNET_OK ==
1669 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1671 dc->offset = offset;
1672 dc->length = length;
1673 dc->anonymity = anonymity;
1674 dc->options = options;
1675 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1676 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1677 if ( (filename == NULL) &&
1678 (is_recursive_download (dc) ) )
1680 if (tempname != NULL)
1681 dc->temp_filename = GNUNET_strdup (tempname);
1683 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688 "Download tree has depth %u\n",
1691 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1692 pi.value.download.specifics.start.meta = dc->meta;
1693 GNUNET_FS_download_make_status_ (&pi, dc);
1694 schedule_block_download (dc,
1695 &dc->uri->data.chk.chk,
1697 1 /* 0 == CHK, 1 == top */);
1698 GNUNET_FS_download_sync_ (dc);
1699 GNUNET_FS_download_start_downloading_ (dc);
1705 * Start the downloading process (by entering the queue).
1707 * @param dc our download context
1710 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
1712 GNUNET_assert (dc->job_queue == NULL);
1713 dc->job_queue = GNUNET_FS_queue_ (dc->h,
1714 &activate_fs_download,
1715 &deactivate_fs_download,
1717 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
1722 * Stop a download (aborts if download is incomplete).
1724 * @param dc handle for the download
1725 * @param do_delete delete files of incomplete downloads
1728 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1731 struct GNUNET_FS_ProgressInfo pi;
1734 if (dc->top != NULL)
1735 GNUNET_FS_end_top (dc->h, dc->top);
1736 if (dc->search != NULL)
1738 dc->search->download = NULL;
1741 if (dc->job_queue != NULL)
1743 GNUNET_FS_dequeue_ (dc->job_queue);
1744 dc->job_queue = NULL;
1746 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
1747 while (NULL != dc->child_head)
1748 GNUNET_FS_download_stop (dc->child_head,
1750 if (dc->parent != NULL)
1751 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1752 dc->parent->child_tail,
1754 if (dc->serialization != NULL)
1755 GNUNET_FS_remove_sync_file_ (dc->h,
1756 ( (dc->parent != NULL) || (dc->search != NULL) )
1757 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1758 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
1760 if ( (GNUNET_YES == have_children) &&
1761 (dc->parent == NULL) )
1762 GNUNET_FS_remove_sync_dir_ (dc->h,
1763 (dc->search != NULL)
1764 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1765 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1767 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1768 GNUNET_FS_download_make_status_ (&pi, dc);
1769 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1770 GNUNET_SCHEDULER_cancel (dc->h->sched,
1772 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1775 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1776 if (dc->filename != NULL)
1778 if ( (dc->completed != dc->length) &&
1779 (GNUNET_YES == do_delete) )
1781 if (0 != UNLINK (dc->filename))
1782 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1786 GNUNET_free (dc->filename);
1788 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1789 GNUNET_FS_uri_destroy (dc->uri);
1790 if (NULL != dc->temp_filename)
1792 if (0 != UNLINK (dc->temp_filename))
1793 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1796 GNUNET_free (dc->temp_filename);
1798 GNUNET_free_non_null (dc->serialization);
1802 /* end of fs_download.c */