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) )
310 fh = GNUNET_DISK_file_open (dc->filename,
311 GNUNET_DISK_OPEN_READ,
312 GNUNET_DISK_PERM_NONE);
316 GNUNET_DISK_file_seek (fh,
318 GNUNET_DISK_SEEK_SET) ) &&
320 GNUNET_DISK_file_read (fh,
324 GNUNET_CRYPTO_hash (block, len, &key);
325 if (0 == memcmp (&key,
327 sizeof (GNUNET_HashCode)))
330 struct GNUNET_CRYPTO_AesSessionKey sk;
331 struct GNUNET_CRYPTO_AesInitializationVector iv;
332 GNUNET_HashCode query;
334 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
335 GNUNET_CRYPTO_aes_encrypt (block, len,
339 GNUNET_CRYPTO_hash (enc, len, &query);
340 if (0 == memcmp (&query,
342 sizeof (GNUNET_HashCode)))
344 /* already got it! */
348 prc.type = (dc->treedepth == depth)
349 ? GNUNET_BLOCK_TYPE_DBLOCK
350 : GNUNET_BLOCK_TYPE_IBLOCK;
351 prc.query = chk->query;
352 prc.do_store = GNUNET_NO; /* useless */
353 process_result_with_request (&prc,
365 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
366 if (depth < dc->treedepth)
368 // FIXME: try if we could
369 // reconstitute this IBLOCK
370 // from the existing blocks on disk (can wait)
371 // (read block(s), encode, compare with
372 // query; if matches, simply return)
375 if ( (dc->th == NULL) &&
376 (dc->client != NULL) )
377 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
378 sizeof (struct SearchMessage),
379 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
381 &transmit_download_request,
388 * Suggest a filename based on given metadata.
390 * @param md given meta data
391 * @return NULL if meta data is useless for suggesting a filename
394 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
396 static const char *mimeMap[][2] = {
397 {"application/bz2", ".bz2"},
398 {"application/gnunet-directory", ".gnd"},
399 {"application/java", ".class"},
400 {"application/msword", ".doc"},
401 {"application/ogg", ".ogg"},
402 {"application/pdf", ".pdf"},
403 {"application/pgp-keys", ".key"},
404 {"application/pgp-signature", ".pgp"},
405 {"application/postscript", ".ps"},
406 {"application/rar", ".rar"},
407 {"application/rtf", ".rtf"},
408 {"application/xml", ".xml"},
409 {"application/x-debian-package", ".deb"},
410 {"application/x-dvi", ".dvi"},
411 {"applixation/x-flac", ".flac"},
412 {"applixation/x-gzip", ".gz"},
413 {"application/x-java-archive", ".jar"},
414 {"application/x-java-vm", ".class"},
415 {"application/x-python-code", ".pyc"},
416 {"application/x-redhat-package-manager", ".rpm"},
417 {"application/x-rpm", ".rpm"},
418 {"application/x-tar", ".tar"},
419 {"application/x-tex-pk", ".pk"},
420 {"application/x-texinfo", ".texinfo"},
421 {"application/x-xcf", ".xcf"},
422 {"application/x-xfig", ".xfig"},
423 {"application/zip", ".zip"},
425 {"audio/midi", ".midi"},
426 {"audio/mpeg", ".mp3"},
427 {"audio/real", ".rm"},
428 {"audio/x-wav", ".wav"},
430 {"image/gif", ".gif"},
431 {"image/jpeg", ".jpg"},
432 {"image/pcx", ".pcx"},
433 {"image/png", ".png"},
434 {"image/tiff", ".tiff"},
435 {"image/x-ms-bmp", ".bmp"},
436 {"image/x-xpixmap", ".xpm"},
438 {"text/css", ".css"},
439 {"text/html", ".html"},
440 {"text/plain", ".txt"},
441 {"text/rtf", ".rtf"},
442 {"text/x-c++hdr", ".h++"},
443 {"text/x-c++src", ".c++"},
444 {"text/x-chdr", ".h"},
445 {"text/x-csrc", ".c"},
446 {"text/x-java", ".java"},
447 {"text/x-moc", ".moc"},
448 {"text/x-pascal", ".pas"},
449 {"text/x-perl", ".pl"},
450 {"text/x-python", ".py"},
451 {"text/x-tex", ".tex"},
453 {"video/avi", ".avi"},
454 {"video/mpeg", ".mpeg"},
455 {"video/quicktime", ".qt"},
456 {"video/real", ".rm"},
457 {"video/x-msvideo", ".avi"},
466 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
467 EXTRACTOR_METATYPE_FILENAME);
471 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
472 EXTRACTOR_METATYPE_MIMETYPE);
476 while ( (mimeMap[i][0] != NULL) &&
477 (0 != strcmp (mime, mimeMap[i][0])))
479 if (mimeMap[i][1] == NULL)
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
481 GNUNET_ERROR_TYPE_BULK,
482 _("Did not find mime type `%s' in extension list.\n"),
488 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
489 EXTRACTOR_METATYPE_TITLE,
490 EXTRACTOR_METATYPE_BOOK_TITLE,
491 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
492 EXTRACTOR_METATYPE_PACKAGE_NAME,
493 EXTRACTOR_METATYPE_URL,
494 EXTRACTOR_METATYPE_URI,
495 EXTRACTOR_METATYPE_DESCRIPTION,
496 EXTRACTOR_METATYPE_ISRC,
497 EXTRACTOR_METATYPE_JOURNAL_NAME,
498 EXTRACTOR_METATYPE_AUTHOR_NAME,
499 EXTRACTOR_METATYPE_SUBJECT,
500 EXTRACTOR_METATYPE_ALBUM,
501 EXTRACTOR_METATYPE_ARTIST,
502 EXTRACTOR_METATYPE_KEYWORDS,
503 EXTRACTOR_METATYPE_COMMENT,
504 EXTRACTOR_METATYPE_UNKNOWN,
506 if ( (base == NULL) &&
510 return GNUNET_strdup (ext);
513 GNUNET_asprintf (&ret,
523 * We've lost our connection with the FS service.
524 * Re-establish it and re-transmit all of our
527 * @param dc download context that is having trouble
530 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
534 * We found an entry in a directory. Check if the respective child
535 * already exists and if not create the respective child download.
537 * @param cls the parent download
538 * @param filename name of the file in the directory
539 * @param uri URI of the file (CHK or LOC)
540 * @param meta meta data of the file
541 * @param length number of bytes in data
542 * @param data contents of the file (or NULL if they were not inlined)
545 trigger_recursive_download (void *cls,
546 const char *filename,
547 const struct GNUNET_FS_Uri *uri,
548 const struct GNUNET_CONTAINER_MetaData *meta,
554 * We're done downloading a directory. Open the file and
555 * trigger all of the (remaining) child downloads.
557 * @param dc context of download that just completed
560 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
565 struct GNUNET_DISK_FileHandle *h;
566 struct GNUNET_DISK_MapHandle *m;
568 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
569 size = (size_t) size64;
570 if (size64 != (uint64_t) size)
572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
576 if (dc->filename != NULL)
578 h = GNUNET_DISK_file_open (dc->filename,
579 GNUNET_DISK_OPEN_READ,
580 GNUNET_DISK_PERM_NONE);
584 GNUNET_assert (dc->temp_filename != NULL);
585 h = GNUNET_DISK_file_open (dc->temp_filename,
586 GNUNET_DISK_OPEN_READ,
587 GNUNET_DISK_PERM_NONE);
591 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595 _("Directory too large for system address space\n"));
599 GNUNET_FS_directory_list_contents (size,
602 &trigger_recursive_download,
604 GNUNET_DISK_file_unmap (m);
606 GNUNET_DISK_file_close (h);
607 if (dc->filename == NULL)
609 if (0 != UNLINK (dc->temp_filename))
610 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
613 GNUNET_free (dc->temp_filename);
614 dc->temp_filename = NULL;
620 * Check if all child-downloads have completed and
621 * if so, signal completion (and possibly recurse to
625 check_completed (struct GNUNET_FS_DownloadContext *dc)
627 struct GNUNET_FS_ProgressInfo pi;
628 struct GNUNET_FS_DownloadContext *pos;
630 pos = dc->child_head;
633 if ( (pos->emsg == NULL) &&
634 (pos->completed < pos->length) )
635 return; /* not done yet */
636 if ( (pos->child_head != NULL) &&
637 (pos->has_finished != GNUNET_YES) )
638 return; /* not transitively done yet */
641 dc->has_finished = GNUNET_YES;
642 GNUNET_FS_download_sync_ (dc);
643 /* signal completion */
644 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
645 GNUNET_FS_download_make_status_ (&pi, dc);
646 if (dc->parent != NULL)
647 check_completed (dc->parent);
652 * We found an entry in a directory. Check if the respective child
653 * already exists and if not create the respective child download.
655 * @param cls the parent download
656 * @param filename name of the file in the directory
657 * @param uri URI of the file (CHK or LOC)
658 * @param meta meta data of the file
659 * @param length number of bytes in data
660 * @param data contents of the file (or NULL if they were not inlined)
663 trigger_recursive_download (void *cls,
664 const char *filename,
665 const struct GNUNET_FS_Uri *uri,
666 const struct GNUNET_CONTAINER_MetaData *meta,
670 struct GNUNET_FS_DownloadContext *dc = cls;
671 struct GNUNET_FS_DownloadContext *cpos;
672 struct GNUNET_DISK_FileHandle *fh;
674 const char *real_name;
682 return; /* entry for the directory itself */
683 cpos = dc->child_head;
686 if ( (GNUNET_FS_uri_test_equal (uri,
688 ( (filename != NULL) &&
689 (0 == strcmp (cpos->filename,
695 return; /* already exists */
697 if (NULL == filename)
699 fn = GNUNET_FS_meta_data_suggest_filename (meta);
702 us = GNUNET_FS_uri_to_string (uri);
703 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_PREFIX
704 GNUNET_FS_URI_CHK_INFIX)]);
707 else if (fn[0] == '.')
710 us = GNUNET_FS_uri_to_string (uri);
711 GNUNET_asprintf (&fn,
713 &us[strlen (GNUNET_FS_URI_PREFIX
714 GNUNET_FS_URI_CHK_INFIX)], ext);
720 if (dc->filename == NULL)
726 dn = GNUNET_strdup (dc->filename);
727 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
729 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
730 GNUNET_FS_DIRECTORY_EXT)) );
731 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
733 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
734 GNUNET_FS_DIRECTORY_EXT)) )
735 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
736 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
737 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
739 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
740 GNUNET_FS_DIRECTORY_EXT)) ) )
742 GNUNET_asprintf (&full_name,
747 GNUNET_FS_DIRECTORY_EXT);
751 GNUNET_asprintf (&full_name,
759 if ( (full_name != NULL) &&
761 GNUNET_DISK_directory_create_for_file (full_name)) )
763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
764 _("Failed to create directory for recursive download of `%s'\n"),
766 GNUNET_free (full_name);
767 GNUNET_free_non_null (fn);
772 if ( (data != NULL) &&
773 (GNUNET_FS_uri_chk_get_file_size (uri) == length) )
775 if (full_name == NULL)
777 temp_name = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
778 real_name = temp_name;
782 real_name = full_name;
784 /* write to disk, then trigger normal download which will instantly progress to completion */
785 fh = GNUNET_DISK_file_open (real_name,
786 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
787 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
790 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
793 GNUNET_free (full_name);
794 GNUNET_free_non_null (fn);
798 GNUNET_DISK_file_write (fh,
802 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
806 GNUNET_DISK_file_close (fh);
808 GNUNET_FS_download_start (dc->h,
811 full_name, temp_name,
813 GNUNET_FS_uri_chk_get_file_size (uri),
818 GNUNET_free_non_null (full_name);
819 GNUNET_free_non_null (temp_name);
820 GNUNET_free_non_null (fn);
825 * Iterator over entries in the pending requests in the 'active' map for the
826 * reply that we just got.
828 * @param cls closure (our 'struct ProcessResultClosure')
829 * @param key query for the given value / request
830 * @param value value in the hash map (a 'struct DownloadRequest')
831 * @return GNUNET_YES (we should continue to iterate); unless serious error
834 process_result_with_request (void *cls,
835 const GNUNET_HashCode * key,
838 struct ProcessResultClosure *prc = cls;
839 struct DownloadRequest *sm = value;
840 struct DownloadRequest *ppos;
841 struct DownloadRequest *pprev;
842 struct GNUNET_DISK_FileHandle *fh;
843 struct GNUNET_FS_DownloadContext *dc = prc->dc;
844 struct GNUNET_CRYPTO_AesSessionKey skey;
845 struct GNUNET_CRYPTO_AesInitializationVector iv;
847 struct GNUNET_FS_ProgressInfo pi;
852 struct ContentHashKey *chk;
855 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
867 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
870 GNUNET_assert (GNUNET_YES ==
871 GNUNET_CONTAINER_multihashmap_remove (dc->active,
874 /* if this request is on the pending list, remove it! */
882 dc->pending = ppos->next;
884 pprev->next = ppos->next;
890 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
891 GNUNET_CRYPTO_aes_decrypt (prc->data,
896 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
901 if ( ( GNUNET_YES == prc->do_store) &&
902 ( (dc->filename != NULL) ||
903 (is_recursive_download (dc)) ) &&
904 ( (sm->depth == dc->treedepth) ||
905 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
907 fh = GNUNET_DISK_file_open (dc->filename != NULL
910 GNUNET_DISK_OPEN_READWRITE |
911 GNUNET_DISK_OPEN_CREATE,
912 GNUNET_DISK_PERM_USER_READ |
913 GNUNET_DISK_PERM_USER_WRITE |
914 GNUNET_DISK_PERM_GROUP_READ |
915 GNUNET_DISK_PERM_OTHER_READ);
918 (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 GNUNET_asprintf (&dc->emsg,
925 _("Download failed: could not open file `%s': %s\n"),
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Saving decrypted block to disk at offset %llu\n",
935 (unsigned long long) off);
938 GNUNET_DISK_file_seek (fh,
940 GNUNET_DISK_SEEK_SET) ) )
942 GNUNET_asprintf (&dc->emsg,
943 _("Failed to seek to offset %llu in file `%s': %s\n"),
944 (unsigned long long) off,
950 GNUNET_DISK_file_write (fh,
954 GNUNET_asprintf (&dc->emsg,
955 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
956 (unsigned int) prc->size,
957 (unsigned long long) off,
962 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
965 if (sm->depth == dc->treedepth)
968 if (sm->offset < dc->offset)
970 /* starting offset begins in the middle of pt,
971 do not count first bytes as progress */
972 GNUNET_assert (app > (dc->offset - sm->offset));
973 app -= (dc->offset - sm->offset);
975 if (sm->offset + prc->size > dc->offset + dc->length)
977 /* end of block is after relevant range,
978 do not count last bytes as progress */
979 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
980 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
982 dc->completed += app;
984 /* do recursive download if option is set and either meta data
985 says it is a directory or if no meta data is given AND filename
986 ends in '.gnd' (top-level case) */
987 if (is_recursive_download (dc))
988 GNUNET_FS_directory_list_contents (prc->size,
991 &trigger_recursive_download,
995 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
996 pi.value.download.specifics.progress.data = pt;
997 pi.value.download.specifics.progress.offset = sm->offset;
998 pi.value.download.specifics.progress.data_len = prc->size;
999 pi.value.download.specifics.progress.depth = sm->depth;
1000 GNUNET_FS_download_make_status_ (&pi, dc);
1001 GNUNET_assert (dc->completed <= dc->length);
1002 if (dc->completed == dc->length)
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Download completed, truncating file to desired length %llu\n",
1007 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1009 /* truncate file to size (since we store IBlocks at the end) */
1010 if (dc->filename != NULL)
1012 if (0 != truncate (dc->filename,
1013 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1014 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1018 if (dc->job_queue != NULL)
1020 GNUNET_FS_dequeue_ (dc->job_queue);
1021 dc->job_queue = NULL;
1023 if (is_recursive_download (dc))
1024 full_recursive_download (dc);
1025 if (dc->child_head == NULL)
1027 /* signal completion */
1028 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
1029 GNUNET_FS_download_make_status_ (&pi, dc);
1030 if (dc->parent != NULL)
1031 check_completed (dc->parent);
1033 GNUNET_assert (sm->depth == dc->treedepth);
1035 if (sm->depth == dc->treedepth)
1037 GNUNET_FS_download_sync_ (dc);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1045 (unsigned long long) sm->offset);
1047 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1048 chk = (struct ContentHashKey*) pt;
1049 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1051 off = compute_dblock_offset (sm->offset,
1055 if ( (off + DBLOCK_SIZE >= dc->offset) &&
1056 (off < dc->offset + dc->length) )
1057 schedule_block_download (dc,
1063 GNUNET_FS_download_sync_ (dc);
1068 GNUNET_DISK_file_close (fh);
1069 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1070 pi.value.download.specifics.error.message = dc->emsg;
1071 GNUNET_FS_download_make_status_ (&pi, dc);
1072 /* abort all pending requests */
1075 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1078 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1079 /* FIXME: clean up dc->active / pending! */
1082 GNUNET_FS_download_sync_ (dc);
1088 * Process a download result.
1090 * @param dc our download context
1091 * @param type type of the result
1092 * @param data the (encrypted) response
1093 * @param size size of data
1096 process_result (struct GNUNET_FS_DownloadContext *dc,
1097 enum GNUNET_BLOCK_Type type,
1101 struct ProcessResultClosure prc;
1107 prc.do_store = GNUNET_YES;
1108 GNUNET_CRYPTO_hash (data, size, &prc.query);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Received result for query `%s' from `%s'-service\n",
1112 GNUNET_h2s (&prc.query),
1115 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1117 &process_result_with_request,
1123 * Type of a function to call when we receive a message
1126 * @param cls closure
1127 * @param msg message received, NULL on timeout or fatal error
1130 receive_results (void *cls,
1131 const struct GNUNET_MessageHeader * msg)
1133 struct GNUNET_FS_DownloadContext *dc = cls;
1134 const struct PutMessage *cm;
1137 if ( (NULL == msg) ||
1138 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1139 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1141 GNUNET_break (msg == NULL);
1145 msize = ntohs(msg->size);
1146 cm = (const struct PutMessage*) msg;
1150 msize - sizeof (struct PutMessage));
1151 if (dc->client == NULL)
1152 return; /* fatal error */
1153 /* continue receiving */
1154 GNUNET_CLIENT_receive (dc->client,
1157 GNUNET_TIME_UNIT_FOREVER_REL);
1163 * We're ready to transmit a search request to the
1164 * file-sharing service. Do it. If there is
1165 * more than one request pending, try to send
1166 * multiple or request another transmission.
1168 * @param cls closure
1169 * @param size number of bytes available in buf
1170 * @param buf where the callee should write the message
1171 * @return number of bytes written to buf
1174 transmit_download_request (void *cls,
1178 struct GNUNET_FS_DownloadContext *dc = cls;
1180 struct SearchMessage *sm;
1188 GNUNET_assert (size >= sizeof (struct SearchMessage));
1191 while ( (dc->pending != NULL) &&
1192 (size > msize + sizeof (struct SearchMessage)) )
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Transmitting download request for `%s' to `%s'-service\n",
1197 GNUNET_h2s (&dc->pending->chk.query),
1200 memset (sm, 0, sizeof (struct SearchMessage));
1201 sm->header.size = htons (sizeof (struct SearchMessage));
1202 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1203 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1204 sm->options = htonl (1);
1206 sm->options = htonl (0);
1207 if (dc->pending->depth == dc->treedepth)
1208 sm->type = htonl (GNUNET_BLOCK_TYPE_DBLOCK);
1210 sm->type = htonl (GNUNET_BLOCK_TYPE_IBLOCK);
1211 sm->anonymity_level = htonl (dc->anonymity);
1212 sm->target = dc->target.hashPubKey;
1213 sm->query = dc->pending->chk.query;
1214 dc->pending->is_pending = GNUNET_NO;
1215 dc->pending = dc->pending->next;
1216 msize += sizeof (struct SearchMessage);
1219 if (dc->pending != NULL)
1220 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1221 sizeof (struct SearchMessage),
1222 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1224 &transmit_download_request,
1231 * Reconnect to the FS service and transmit our queries NOW.
1233 * @param cls our download context
1237 do_reconnect (void *cls,
1238 const struct GNUNET_SCHEDULER_TaskContext *tc)
1240 struct GNUNET_FS_DownloadContext *dc = cls;
1241 struct GNUNET_CLIENT_Connection *client;
1243 dc->task = GNUNET_SCHEDULER_NO_TASK;
1244 client = GNUNET_CLIENT_connect (dc->h->sched,
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250 "Connecting to `%s'-service failed, will try again.\n",
1255 dc->client = client;
1256 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1257 sizeof (struct SearchMessage),
1258 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1260 &transmit_download_request,
1262 GNUNET_CLIENT_receive (client,
1265 GNUNET_TIME_UNIT_FOREVER_REL);
1270 * Add entries that are not yet pending back to the pending list.
1272 * @param cls our download context
1274 * @param entry entry of type "struct DownloadRequest"
1278 retry_entry (void *cls,
1279 const GNUNET_HashCode *key,
1282 struct GNUNET_FS_DownloadContext *dc = cls;
1283 struct DownloadRequest *dr = entry;
1285 if (! dr->is_pending)
1287 dr->next = dc->pending;
1288 dr->is_pending = GNUNET_YES;
1289 dc->pending = entry;
1296 * We've lost our connection with the FS service.
1297 * Re-establish it and re-transmit all of our
1300 * @param dc download context that is having trouble
1303 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1306 if (NULL != dc->client)
1310 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1313 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1316 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1320 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1321 GNUNET_TIME_UNIT_SECONDS,
1329 * We're allowed to ask the FS service for our blocks. Start the download.
1331 * @param cls the 'struct GNUNET_FS_DownloadContext'
1332 * @param client handle to use for communcation with FS (we must destroy it!)
1335 activate_fs_download (void *cls,
1336 struct GNUNET_CLIENT_Connection *client)
1338 struct GNUNET_FS_DownloadContext *dc = cls;
1339 struct GNUNET_FS_ProgressInfo pi;
1341 GNUNET_assert (NULL != client);
1342 dc->client = client;
1343 GNUNET_CLIENT_receive (client,
1346 GNUNET_TIME_UNIT_FOREVER_REL);
1347 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1348 GNUNET_FS_download_make_status_ (&pi, dc);
1349 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1352 if ( (dc->th == NULL) &&
1353 (dc->client != NULL) )
1354 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1355 sizeof (struct SearchMessage),
1356 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1358 &transmit_download_request,
1364 * We must stop to ask the FS service for our blocks. Pause the download.
1366 * @param cls the 'struct GNUNET_FS_DownloadContext'
1369 deactivate_fs_download (void *cls)
1371 struct GNUNET_FS_DownloadContext *dc = cls;
1372 struct GNUNET_FS_ProgressInfo pi;
1376 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1379 if (NULL != dc->client)
1381 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1384 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1385 GNUNET_FS_download_make_status_ (&pi, dc);
1390 * Free entries in the map.
1392 * @param cls unused (NULL)
1394 * @param entry entry of type "struct DownloadRequest" which is freed
1398 free_entry (void *cls,
1399 const GNUNET_HashCode *key,
1402 GNUNET_free (entry);
1408 * Create SUSPEND event for the given download operation
1409 * and then clean up our state (without stop signal).
1411 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1414 GNUNET_FS_download_signal_suspend_ (void *cls)
1416 struct GNUNET_FS_DownloadContext *dc = cls;
1417 struct GNUNET_FS_ProgressInfo pi;
1419 if (dc->top != NULL)
1420 GNUNET_FS_end_top (dc->h, dc->top);
1421 while (NULL != dc->child_head)
1422 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1423 if (dc->search != NULL)
1425 dc->search->download = NULL;
1428 if (dc->job_queue != NULL)
1430 GNUNET_FS_dequeue_ (dc->job_queue);
1431 dc->job_queue = NULL;
1433 if (dc->parent != NULL)
1434 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1435 dc->parent->child_tail,
1437 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1438 GNUNET_FS_download_make_status_ (&pi, dc);
1439 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1440 GNUNET_SCHEDULER_cancel (dc->h->sched,
1442 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1445 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1446 GNUNET_free_non_null (dc->filename);
1447 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1448 GNUNET_FS_uri_destroy (dc->uri);
1449 GNUNET_free_non_null (dc->temp_filename);
1450 GNUNET_free_non_null (dc->serialization);
1456 * Download parts of a file. Note that this will store
1457 * the blocks at the respective offset in the given file. Also, the
1458 * download is still using the blocking of the underlying FS
1459 * encoding. As a result, the download may *write* outside of the
1460 * given boundaries (if offset and length do not match the 32k FS
1461 * block boundaries). <p>
1463 * This function should be used to focus a download towards a
1464 * particular portion of the file (optimization), not to strictly
1465 * limit the download to exactly those bytes.
1467 * @param h handle to the file sharing subsystem
1468 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1469 * @param meta known metadata for the file (can be NULL)
1470 * @param filename where to store the file, maybe NULL (then no file is
1471 * created on disk and data must be grabbed from the callbacks)
1472 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1473 * can be NULL (in which case we will pick a name if needed); the temporary file
1474 * may already exist, in which case we will try to use the data that is there and
1475 * if it is not what is desired, will overwrite it
1476 * @param offset at what offset should we start the download (typically 0)
1477 * @param length how many bytes should be downloaded starting at offset
1478 * @param anonymity anonymity level to use for the download
1479 * @param options various options
1480 * @param cctx initial value for the client context for this download
1481 * @param parent parent download to associate this download with (use NULL
1482 * for top-level downloads; useful for manually-triggered recursive downloads)
1483 * @return context that can be used to control this download
1485 struct GNUNET_FS_DownloadContext *
1486 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1487 const struct GNUNET_FS_Uri *uri,
1488 const struct GNUNET_CONTAINER_MetaData *meta,
1489 const char *filename,
1490 const char *tempname,
1494 enum GNUNET_FS_DownloadOptions options,
1496 struct GNUNET_FS_DownloadContext *parent)
1498 struct GNUNET_FS_ProgressInfo pi;
1499 struct GNUNET_FS_DownloadContext *dc;
1501 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1502 if ( (offset + length < offset) ||
1503 (offset + length > uri->data.chk.file_length) )
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Starting download `%s' of %llu bytes\n",
1512 (unsigned long long) length);
1514 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1516 dc->parent = parent;
1519 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1523 dc->uri = GNUNET_FS_uri_dup (uri);
1524 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1525 dc->client_info = cctx;
1526 dc->start_time = GNUNET_TIME_absolute_get ();
1527 if (NULL != filename)
1529 dc->filename = GNUNET_strdup (filename);
1530 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1531 GNUNET_DISK_file_size (filename,
1535 if (GNUNET_FS_uri_test_loc (dc->uri))
1536 GNUNET_assert (GNUNET_OK ==
1537 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1539 dc->offset = offset;
1540 dc->length = length;
1541 dc->anonymity = anonymity;
1542 dc->options = options;
1543 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1544 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1545 if ( (filename == NULL) &&
1546 (is_recursive_download (dc) ) )
1548 if (tempname != NULL)
1549 dc->temp_filename = GNUNET_strdup (tempname);
1551 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Download tree has depth %u\n",
1559 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1560 pi.value.download.specifics.start.meta = meta;
1561 GNUNET_FS_download_make_status_ (&pi, dc);
1562 schedule_block_download (dc,
1563 &dc->uri->data.chk.chk,
1565 1 /* 0 == CHK, 1 == top */);
1566 GNUNET_FS_download_sync_ (dc);
1567 GNUNET_FS_download_start_downloading_ (dc);
1569 dc->top = GNUNET_FS_make_top (dc->h,
1570 &GNUNET_FS_download_signal_suspend_,
1578 * Download parts of a file based on a search result. The download
1579 * will be associated with the search result (and the association
1580 * will be preserved when serializing/deserializing the state).
1581 * If the search is stopped, the download will not be aborted but
1582 * be 'promoted' to a stand-alone download.
1584 * As with the other download function, this will store
1585 * the blocks at the respective offset in the given file. Also, the
1586 * download is still using the blocking of the underlying FS
1587 * encoding. As a result, the download may *write* outside of the
1588 * given boundaries (if offset and length do not match the 32k FS
1589 * block boundaries). <p>
1591 * The given range can be used to focus a download towards a
1592 * particular portion of the file (optimization), not to strictly
1593 * limit the download to exactly those bytes.
1595 * @param h handle to the file sharing subsystem
1596 * @param sr the search result to use for the download (determines uri and
1597 * meta data and associations)
1598 * @param filename where to store the file, maybe NULL (then no file is
1599 * created on disk and data must be grabbed from the callbacks)
1600 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1601 * can be NULL (in which case we will pick a name if needed); the temporary file
1602 * may already exist, in which case we will try to use the data that is there and
1603 * if it is not what is desired, will overwrite it
1604 * @param offset at what offset should we start the download (typically 0)
1605 * @param length how many bytes should be downloaded starting at offset
1606 * @param anonymity anonymity level to use for the download
1607 * @param options various download options
1608 * @param cctx initial value for the client context for this download
1609 * @return context that can be used to control this download
1611 struct GNUNET_FS_DownloadContext *
1612 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
1613 struct GNUNET_FS_SearchResult *sr,
1614 const char *filename,
1615 const char *tempname,
1619 enum GNUNET_FS_DownloadOptions options,
1622 struct GNUNET_FS_ProgressInfo pi;
1623 struct GNUNET_FS_DownloadContext *dc;
1625 if (sr->download != NULL)
1630 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri));
1631 if ( (offset + length < offset) ||
1632 (offset + length > sr->uri->data.chk.file_length) )
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1639 "Starting download `%s' of %llu bytes\n",
1641 (unsigned long long) length);
1643 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1647 if (sr->probe_ctx != NULL)
1649 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1650 sr->probe_ctx = NULL;
1652 dc->uri = GNUNET_FS_uri_dup (sr->uri);
1653 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
1654 dc->client_info = cctx;
1655 dc->start_time = GNUNET_TIME_absolute_get ();
1656 if (NULL != filename)
1658 dc->filename = GNUNET_strdup (filename);
1659 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1660 GNUNET_DISK_file_size (filename,
1664 if (GNUNET_FS_uri_test_loc (dc->uri))
1665 GNUNET_assert (GNUNET_OK ==
1666 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1668 dc->offset = offset;
1669 dc->length = length;
1670 dc->anonymity = anonymity;
1671 dc->options = options;
1672 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1673 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1674 if ( (filename == NULL) &&
1675 (is_recursive_download (dc) ) )
1677 if (tempname != NULL)
1678 dc->temp_filename = GNUNET_strdup (tempname);
1680 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Download tree has depth %u\n",
1688 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1689 pi.value.download.specifics.start.meta = dc->meta;
1690 GNUNET_FS_download_make_status_ (&pi, dc);
1691 schedule_block_download (dc,
1692 &dc->uri->data.chk.chk,
1694 1 /* 0 == CHK, 1 == top */);
1695 GNUNET_FS_download_sync_ (dc);
1696 GNUNET_FS_download_start_downloading_ (dc);
1702 * Start the downloading process (by entering the queue).
1704 * @param dc our download context
1707 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
1709 dc->job_queue = GNUNET_FS_queue_ (dc->h,
1710 &activate_fs_download,
1711 &deactivate_fs_download,
1713 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
1718 * Stop a download (aborts if download is incomplete).
1720 * @param dc handle for the download
1721 * @param do_delete delete files of incomplete downloads
1724 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1727 struct GNUNET_FS_ProgressInfo pi;
1730 if (dc->top != NULL)
1731 GNUNET_FS_end_top (dc->h, dc->top);
1732 if (dc->search != NULL)
1734 dc->search->download = NULL;
1737 if (dc->job_queue != NULL)
1739 GNUNET_FS_dequeue_ (dc->job_queue);
1740 dc->job_queue = NULL;
1742 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
1743 while (NULL != dc->child_head)
1744 GNUNET_FS_download_stop (dc->child_head,
1746 if (dc->parent != NULL)
1747 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1748 dc->parent->child_tail,
1750 if (dc->serialization != NULL)
1751 GNUNET_FS_remove_sync_file_ (dc->h,
1752 ( (dc->parent != NULL) || (dc->search != NULL) )
1753 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1754 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
1756 if (GNUNET_YES == have_children)
1757 GNUNET_FS_remove_sync_dir_ (dc->h,
1758 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1760 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1761 GNUNET_FS_download_make_status_ (&pi, dc);
1762 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1763 GNUNET_SCHEDULER_cancel (dc->h->sched,
1765 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1768 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1769 if (dc->filename != NULL)
1771 if ( (dc->completed != dc->length) &&
1772 (GNUNET_YES == do_delete) )
1774 if (0 != UNLINK (dc->filename))
1775 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1779 GNUNET_free (dc->filename);
1781 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1782 GNUNET_FS_uri_destroy (dc->uri);
1783 if (NULL != dc->temp_filename)
1785 if (0 != UNLINK (dc->temp_filename))
1786 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1789 GNUNET_free (dc->temp_filename);
1791 GNUNET_free_non_null (dc->serialization);
1795 /* end of fs_download.c */