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 3, 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 pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
174 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
175 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
178 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL,
183 * We're ready to transmit a search request to the
184 * file-sharing service. Do it. If there is
185 * more than one request pending, try to send
186 * multiple or request another transmission.
189 * @param size number of bytes available in buf
190 * @param buf where the callee should write the message
191 * @return number of bytes written to buf
194 transmit_download_request (void *cls,
200 * Closure for iterator processing results.
202 struct ProcessResultClosure
208 GNUNET_HashCode query;
211 * Data found in P2P network.
216 * Our download context.
218 struct GNUNET_FS_DownloadContext *dc;
221 * Number of bytes in data.
228 enum GNUNET_BLOCK_Type type;
231 * Flag to indicate if this block should be stored on disk.
239 * Iterator over entries in the pending requests in the 'active' map for the
240 * reply that we just got.
242 * @param cls closure (our 'struct ProcessResultClosure')
243 * @param key query for the given value / request
244 * @param value value in the hash map (a 'struct DownloadRequest')
245 * @return GNUNET_YES (we should continue to iterate); unless serious error
248 process_result_with_request (void *cls,
249 const GNUNET_HashCode * key,
255 * Schedule the download of the specified block in the tree.
257 * @param dc overall download this block belongs to
258 * @param chk content-hash-key of the block
259 * @param offset offset of the block in the file
260 * (for IBlocks, the offset is the lowest
261 * offset of any DBlock in the subtree under
263 * @param depth depth of the block, 0 is the root of the tree
266 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
267 const struct ContentHashKey *chk,
271 struct DownloadRequest *sm;
275 char block[DBLOCK_SIZE];
277 struct ProcessResultClosure prc;
278 struct GNUNET_DISK_FileHandle *fh;
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Scheduling download at offset %llu and depth %u for `%s'\n",
283 (unsigned long long) offset,
285 GNUNET_h2s (&chk->query));
287 total = GNUNET_ntohll (dc->uri->data.chk.file_length);
288 off = compute_disk_offset (total,
292 len = GNUNET_FS_tree_calculate_block_size (total,
296 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
300 sm->is_pending = GNUNET_YES;
301 sm->next = dc->pending;
303 GNUNET_CONTAINER_multihashmap_put (dc->active,
306 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
308 if ( (dc->old_file_size > off) &&
309 (dc->filename != NULL) )
310 fh = GNUNET_DISK_file_open (dc->filename,
311 GNUNET_DISK_OPEN_READ,
312 GNUNET_DISK_PERM_NONE);
315 GNUNET_DISK_file_seek (fh,
317 GNUNET_DISK_SEEK_SET) ) &&
319 GNUNET_DISK_file_read (fh,
323 GNUNET_CRYPTO_hash (block, len, &key);
324 if (0 == memcmp (&key,
326 sizeof (GNUNET_HashCode)))
329 struct GNUNET_CRYPTO_AesSessionKey sk;
330 struct GNUNET_CRYPTO_AesInitializationVector iv;
331 GNUNET_HashCode query;
333 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
334 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len,
342 GNUNET_CRYPTO_hash (enc, len, &query);
343 if (0 == memcmp (&query,
345 sizeof (GNUNET_HashCode)))
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Matching block already present, no need for download!\n");
351 /* already got it! */
355 prc.type = (dc->treedepth == depth)
356 ? GNUNET_BLOCK_TYPE_DBLOCK
357 : GNUNET_BLOCK_TYPE_IBLOCK;
358 prc.query = chk->query;
359 prc.do_store = GNUNET_NO; /* useless */
360 process_result_with_request (&prc,
368 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
374 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
375 if (depth < dc->treedepth)
377 // FIXME: try if we could
378 // reconstitute this IBLOCK
379 // from the existing blocks on disk (can wait)
380 // (read block(s), encode, compare with
381 // query; if matches, simply return)
384 if ( (dc->th == NULL) &&
385 (dc->client != NULL) )
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Asking for transmission to FS service\n");
391 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
392 sizeof (struct SearchMessage),
393 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
395 &transmit_download_request,
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Transmission request not issued (%p %p)\n",
414 * Suggest a filename based on given metadata.
416 * @param md given meta data
417 * @return NULL if meta data is useless for suggesting a filename
420 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
422 static const char *mimeMap[][2] = {
423 {"application/bz2", ".bz2"},
424 {"application/gnunet-directory", ".gnd"},
425 {"application/java", ".class"},
426 {"application/msword", ".doc"},
427 {"application/ogg", ".ogg"},
428 {"application/pdf", ".pdf"},
429 {"application/pgp-keys", ".key"},
430 {"application/pgp-signature", ".pgp"},
431 {"application/postscript", ".ps"},
432 {"application/rar", ".rar"},
433 {"application/rtf", ".rtf"},
434 {"application/xml", ".xml"},
435 {"application/x-debian-package", ".deb"},
436 {"application/x-dvi", ".dvi"},
437 {"applixation/x-flac", ".flac"},
438 {"applixation/x-gzip", ".gz"},
439 {"application/x-java-archive", ".jar"},
440 {"application/x-java-vm", ".class"},
441 {"application/x-python-code", ".pyc"},
442 {"application/x-redhat-package-manager", ".rpm"},
443 {"application/x-rpm", ".rpm"},
444 {"application/x-tar", ".tar"},
445 {"application/x-tex-pk", ".pk"},
446 {"application/x-texinfo", ".texinfo"},
447 {"application/x-xcf", ".xcf"},
448 {"application/x-xfig", ".xfig"},
449 {"application/zip", ".zip"},
451 {"audio/midi", ".midi"},
452 {"audio/mpeg", ".mp3"},
453 {"audio/real", ".rm"},
454 {"audio/x-wav", ".wav"},
456 {"image/gif", ".gif"},
457 {"image/jpeg", ".jpg"},
458 {"image/pcx", ".pcx"},
459 {"image/png", ".png"},
460 {"image/tiff", ".tiff"},
461 {"image/x-ms-bmp", ".bmp"},
462 {"image/x-xpixmap", ".xpm"},
464 {"text/css", ".css"},
465 {"text/html", ".html"},
466 {"text/plain", ".txt"},
467 {"text/rtf", ".rtf"},
468 {"text/x-c++hdr", ".h++"},
469 {"text/x-c++src", ".c++"},
470 {"text/x-chdr", ".h"},
471 {"text/x-csrc", ".c"},
472 {"text/x-java", ".java"},
473 {"text/x-moc", ".moc"},
474 {"text/x-pascal", ".pas"},
475 {"text/x-perl", ".pl"},
476 {"text/x-python", ".py"},
477 {"text/x-tex", ".tex"},
479 {"video/avi", ".avi"},
480 {"video/mpeg", ".mpeg"},
481 {"video/quicktime", ".qt"},
482 {"video/real", ".rm"},
483 {"video/x-msvideo", ".avi"},
492 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
493 EXTRACTOR_METATYPE_FILENAME);
497 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
498 EXTRACTOR_METATYPE_MIMETYPE);
502 while ( (mimeMap[i][0] != NULL) &&
503 (0 != strcmp (mime, mimeMap[i][0])))
505 if (mimeMap[i][1] == NULL)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
507 GNUNET_ERROR_TYPE_BULK,
508 _("Did not find mime type `%s' in extension list.\n"),
514 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
515 EXTRACTOR_METATYPE_TITLE,
516 EXTRACTOR_METATYPE_BOOK_TITLE,
517 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
518 EXTRACTOR_METATYPE_PACKAGE_NAME,
519 EXTRACTOR_METATYPE_URL,
520 EXTRACTOR_METATYPE_URI,
521 EXTRACTOR_METATYPE_DESCRIPTION,
522 EXTRACTOR_METATYPE_ISRC,
523 EXTRACTOR_METATYPE_JOURNAL_NAME,
524 EXTRACTOR_METATYPE_AUTHOR_NAME,
525 EXTRACTOR_METATYPE_SUBJECT,
526 EXTRACTOR_METATYPE_ALBUM,
527 EXTRACTOR_METATYPE_ARTIST,
528 EXTRACTOR_METATYPE_KEYWORDS,
529 EXTRACTOR_METATYPE_COMMENT,
530 EXTRACTOR_METATYPE_UNKNOWN,
532 if ( (base == NULL) &&
536 return GNUNET_strdup (ext);
539 GNUNET_asprintf (&ret,
549 * We've lost our connection with the FS service.
550 * Re-establish it and re-transmit all of our
553 * @param dc download context that is having trouble
556 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
560 * We found an entry in a directory. Check if the respective child
561 * already exists and if not create the respective child download.
563 * @param cls the parent download
564 * @param filename name of the file in the directory
565 * @param uri URI of the file (CHK or LOC)
566 * @param meta meta data of the file
567 * @param length number of bytes in data
568 * @param data contents of the file (or NULL if they were not inlined)
571 trigger_recursive_download (void *cls,
572 const char *filename,
573 const struct GNUNET_FS_Uri *uri,
574 const struct GNUNET_CONTAINER_MetaData *meta,
580 * We're done downloading a directory. Open the file and
581 * trigger all of the (remaining) child downloads.
583 * @param dc context of download that just completed
586 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
591 struct GNUNET_DISK_FileHandle *h;
592 struct GNUNET_DISK_MapHandle *m;
594 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
595 size = (size_t) size64;
596 if (size64 != (uint64_t) size)
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
602 if (dc->filename != NULL)
604 h = GNUNET_DISK_file_open (dc->filename,
605 GNUNET_DISK_OPEN_READ,
606 GNUNET_DISK_PERM_NONE);
610 GNUNET_assert (dc->temp_filename != NULL);
611 h = GNUNET_DISK_file_open (dc->temp_filename,
612 GNUNET_DISK_OPEN_READ,
613 GNUNET_DISK_PERM_NONE);
617 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
621 _("Directory too large for system address space\n"));
625 GNUNET_FS_directory_list_contents (size,
628 &trigger_recursive_download,
630 GNUNET_DISK_file_unmap (m);
632 GNUNET_DISK_file_close (h);
633 if (dc->filename == NULL)
635 if (0 != UNLINK (dc->temp_filename))
636 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
639 GNUNET_free (dc->temp_filename);
640 dc->temp_filename = NULL;
646 * Check if all child-downloads have completed and
647 * if so, signal completion (and possibly recurse to
651 check_completed (struct GNUNET_FS_DownloadContext *dc)
653 struct GNUNET_FS_ProgressInfo pi;
654 struct GNUNET_FS_DownloadContext *pos;
656 pos = dc->child_head;
659 if ( (pos->emsg == NULL) &&
660 (pos->completed < pos->length) )
661 return; /* not done yet */
662 if ( (pos->child_head != NULL) &&
663 (pos->has_finished != GNUNET_YES) )
664 return; /* not transitively done yet */
667 dc->has_finished = GNUNET_YES;
668 GNUNET_FS_download_sync_ (dc);
669 /* signal completion */
670 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
671 GNUNET_FS_download_make_status_ (&pi, dc);
672 if (dc->parent != NULL)
673 check_completed (dc->parent);
678 * We found an entry in a directory. Check if the respective child
679 * already exists and if not create the respective child download.
681 * @param cls the parent download
682 * @param filename name of the file in the directory
683 * @param uri URI of the file (CHK or LOC)
684 * @param meta meta data of the file
685 * @param length number of bytes in data
686 * @param data contents of the file (or NULL if they were not inlined)
689 trigger_recursive_download (void *cls,
690 const char *filename,
691 const struct GNUNET_FS_Uri *uri,
692 const struct GNUNET_CONTAINER_MetaData *meta,
696 struct GNUNET_FS_DownloadContext *dc = cls;
697 struct GNUNET_FS_DownloadContext *cpos;
698 struct GNUNET_DISK_FileHandle *fh;
700 const char *real_name;
708 return; /* entry for the directory itself */
709 cpos = dc->child_head;
712 if ( (GNUNET_FS_uri_test_equal (uri,
714 ( (filename != NULL) &&
715 (0 == strcmp (cpos->filename,
721 return; /* already exists */
723 if (NULL == filename)
725 fn = GNUNET_FS_meta_data_suggest_filename (meta);
728 us = GNUNET_FS_uri_to_string (uri);
729 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_PREFIX
730 GNUNET_FS_URI_CHK_INFIX)]);
733 else if (fn[0] == '.')
736 us = GNUNET_FS_uri_to_string (uri);
737 GNUNET_asprintf (&fn,
739 &us[strlen (GNUNET_FS_URI_PREFIX
740 GNUNET_FS_URI_CHK_INFIX)], ext);
746 if (dc->filename == NULL)
752 dn = GNUNET_strdup (dc->filename);
753 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
755 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
756 GNUNET_FS_DIRECTORY_EXT)) );
757 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
759 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
760 GNUNET_FS_DIRECTORY_EXT)) )
761 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
762 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
763 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
765 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
766 GNUNET_FS_DIRECTORY_EXT)) ) )
768 GNUNET_asprintf (&full_name,
773 GNUNET_FS_DIRECTORY_EXT);
777 GNUNET_asprintf (&full_name,
785 if ( (full_name != NULL) &&
787 GNUNET_DISK_directory_create_for_file (full_name)) )
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790 _("Failed to create directory for recursive download of `%s'\n"),
792 GNUNET_free (full_name);
793 GNUNET_free_non_null (fn);
798 if ( (data != NULL) &&
799 (GNUNET_FS_uri_chk_get_file_size (uri) == length) )
801 if (full_name == NULL)
803 temp_name = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
804 real_name = temp_name;
808 real_name = full_name;
810 /* write to disk, then trigger normal download which will instantly progress to completion */
811 fh = GNUNET_DISK_file_open (real_name,
812 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
813 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
816 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
819 GNUNET_free (full_name);
820 GNUNET_free_non_null (fn);
824 GNUNET_DISK_file_write (fh,
828 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
832 GNUNET_DISK_file_close (fh);
834 GNUNET_FS_download_start (dc->h,
837 full_name, temp_name,
839 GNUNET_FS_uri_chk_get_file_size (uri),
844 GNUNET_free_non_null (full_name);
845 GNUNET_free_non_null (temp_name);
846 GNUNET_free_non_null (fn);
851 * Free entries in the map.
853 * @param cls unused (NULL)
855 * @param entry entry of type "struct DownloadRequest" which is freed
859 free_entry (void *cls,
860 const GNUNET_HashCode *key,
869 * Iterator over entries in the pending requests in the 'active' map for the
870 * reply that we just got.
872 * @param cls closure (our 'struct ProcessResultClosure')
873 * @param key query for the given value / request
874 * @param value value in the hash map (a 'struct DownloadRequest')
875 * @return GNUNET_YES (we should continue to iterate); unless serious error
878 process_result_with_request (void *cls,
879 const GNUNET_HashCode * key,
882 struct ProcessResultClosure *prc = cls;
883 struct DownloadRequest *sm = value;
884 struct DownloadRequest *ppos;
885 struct DownloadRequest *pprev;
886 struct GNUNET_DISK_FileHandle *fh;
887 struct GNUNET_FS_DownloadContext *dc = prc->dc;
888 struct GNUNET_CRYPTO_AesSessionKey skey;
889 struct GNUNET_CRYPTO_AesInitializationVector iv;
891 struct GNUNET_FS_ProgressInfo pi;
896 struct ContentHashKey *chk;
899 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
911 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
914 GNUNET_assert (GNUNET_YES ==
915 GNUNET_CONTAINER_multihashmap_remove (dc->active,
918 /* if this request is on the pending list, remove it! */
926 dc->pending = ppos->next;
928 pprev->next = ppos->next;
934 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
935 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data,
942 dc->emsg = GNUNET_strdup ("internal error decrypting content");
945 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
950 if ( ( GNUNET_YES == prc->do_store) &&
951 ( (dc->filename != NULL) ||
952 (is_recursive_download (dc)) ) &&
953 ( (sm->depth == dc->treedepth) ||
954 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
956 fh = GNUNET_DISK_file_open (dc->filename != NULL
959 GNUNET_DISK_OPEN_READWRITE |
960 GNUNET_DISK_OPEN_CREATE,
961 GNUNET_DISK_PERM_USER_READ |
962 GNUNET_DISK_PERM_USER_WRITE |
963 GNUNET_DISK_PERM_GROUP_READ |
964 GNUNET_DISK_PERM_OTHER_READ);
967 (GNUNET_YES == prc->do_store) &&
968 ( (dc->filename != NULL) ||
969 (is_recursive_download (dc)) ) &&
970 ( (sm->depth == dc->treedepth) ||
971 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
973 GNUNET_asprintf (&dc->emsg,
974 _("Download failed: could not open file `%s': %s\n"),
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Saving decrypted block to disk at offset %llu\n",
984 (unsigned long long) off);
987 GNUNET_DISK_file_seek (fh,
989 GNUNET_DISK_SEEK_SET) ) )
991 GNUNET_asprintf (&dc->emsg,
992 _("Failed to seek to offset %llu in file `%s': %s\n"),
993 (unsigned long long) off,
999 GNUNET_DISK_file_write (fh,
1003 GNUNET_asprintf (&dc->emsg,
1004 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1005 (unsigned int) prc->size,
1006 (unsigned long long) off,
1011 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1014 if (sm->depth == dc->treedepth)
1017 if (sm->offset < dc->offset)
1019 /* starting offset begins in the middle of pt,
1020 do not count first bytes as progress */
1021 GNUNET_assert (app > (dc->offset - sm->offset));
1022 app -= (dc->offset - sm->offset);
1024 if (sm->offset + prc->size > dc->offset + dc->length)
1026 /* end of block is after relevant range,
1027 do not count last bytes as progress */
1028 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
1029 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
1031 dc->completed += app;
1033 /* do recursive download if option is set and either meta data
1034 says it is a directory or if no meta data is given AND filename
1035 ends in '.gnd' (top-level case) */
1036 if (is_recursive_download (dc))
1037 GNUNET_FS_directory_list_contents (prc->size,
1040 &trigger_recursive_download,
1044 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1045 pi.value.download.specifics.progress.data = pt;
1046 pi.value.download.specifics.progress.offset = sm->offset;
1047 pi.value.download.specifics.progress.data_len = prc->size;
1048 pi.value.download.specifics.progress.depth = sm->depth;
1049 GNUNET_FS_download_make_status_ (&pi, dc);
1050 GNUNET_assert (dc->completed <= dc->length);
1051 if (dc->completed == dc->length)
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Download completed, truncating file to desired length %llu\n",
1056 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1058 /* truncate file to size (since we store IBlocks at the end) */
1059 if (dc->filename != NULL)
1061 if (0 != truncate (dc->filename,
1062 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1063 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1067 if (dc->job_queue != NULL)
1069 GNUNET_FS_dequeue_ (dc->job_queue);
1070 dc->job_queue = NULL;
1072 if (is_recursive_download (dc))
1073 full_recursive_download (dc);
1074 if (dc->child_head == NULL)
1076 /* signal completion */
1077 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
1078 GNUNET_FS_download_make_status_ (&pi, dc);
1079 if (dc->parent != NULL)
1080 check_completed (dc->parent);
1082 GNUNET_assert (sm->depth == dc->treedepth);
1084 if (sm->depth == dc->treedepth)
1086 GNUNET_FS_download_sync_ (dc);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1094 (unsigned long long) sm->offset);
1096 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1097 chk = (struct ContentHashKey*) pt;
1098 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1100 off = compute_dblock_offset (sm->offset,
1104 if ( (off + DBLOCK_SIZE >= dc->offset) &&
1105 (off < dc->offset + dc->length) )
1106 schedule_block_download (dc,
1112 GNUNET_FS_download_sync_ (dc);
1117 GNUNET_DISK_file_close (fh);
1118 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1119 pi.value.download.specifics.error.message = dc->emsg;
1120 GNUNET_FS_download_make_status_ (&pi, dc);
1121 /* abort all pending requests */
1124 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1127 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1128 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1134 GNUNET_FS_download_sync_ (dc);
1140 * Process a download result.
1142 * @param dc our download context
1143 * @param type type of the result
1144 * @param data the (encrypted) response
1145 * @param size size of data
1148 process_result (struct GNUNET_FS_DownloadContext *dc,
1149 enum GNUNET_BLOCK_Type type,
1153 struct ProcessResultClosure prc;
1159 prc.do_store = GNUNET_YES;
1160 GNUNET_CRYPTO_hash (data, size, &prc.query);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Received result for query `%s' from `%s'-service\n",
1164 GNUNET_h2s (&prc.query),
1167 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1169 &process_result_with_request,
1175 * Type of a function to call when we receive a message
1178 * @param cls closure
1179 * @param msg message received, NULL on timeout or fatal error
1182 receive_results (void *cls,
1183 const struct GNUNET_MessageHeader * msg)
1185 struct GNUNET_FS_DownloadContext *dc = cls;
1186 const struct PutMessage *cm;
1189 if ( (NULL == msg) ||
1190 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1191 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1193 GNUNET_break (msg == NULL);
1197 msize = ntohs(msg->size);
1198 cm = (const struct PutMessage*) msg;
1202 msize - sizeof (struct PutMessage));
1203 if (dc->client == NULL)
1204 return; /* fatal error */
1205 /* continue receiving */
1206 GNUNET_CLIENT_receive (dc->client,
1209 GNUNET_TIME_UNIT_FOREVER_REL);
1215 * We're ready to transmit a search request to the
1216 * file-sharing service. Do it. If there is
1217 * more than one request pending, try to send
1218 * multiple or request another transmission.
1220 * @param cls closure
1221 * @param size number of bytes available in buf
1222 * @param buf where the callee should write the message
1223 * @return number of bytes written to buf
1226 transmit_download_request (void *cls,
1230 struct GNUNET_FS_DownloadContext *dc = cls;
1232 struct SearchMessage *sm;
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Transmitting download request failed, trying to reconnect\n");
1244 GNUNET_assert (size >= sizeof (struct SearchMessage));
1247 while ( (dc->pending != NULL) &&
1248 (size >= msize + sizeof (struct SearchMessage)) )
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "Transmitting download request for `%s' to `%s'-service\n",
1253 GNUNET_h2s (&dc->pending->chk.query),
1256 memset (sm, 0, sizeof (struct SearchMessage));
1257 sm->header.size = htons (sizeof (struct SearchMessage));
1258 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1259 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1260 sm->options = htonl (1);
1262 sm->options = htonl (0);
1263 if (dc->pending->depth == dc->treedepth)
1264 sm->type = htonl (GNUNET_BLOCK_TYPE_DBLOCK);
1266 sm->type = htonl (GNUNET_BLOCK_TYPE_IBLOCK);
1267 sm->anonymity_level = htonl (dc->anonymity);
1268 sm->target = dc->target.hashPubKey;
1269 sm->query = dc->pending->chk.query;
1270 dc->pending->is_pending = GNUNET_NO;
1271 dc->pending = dc->pending->next;
1272 msize += sizeof (struct SearchMessage);
1275 if (dc->pending != NULL)
1276 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1277 sizeof (struct SearchMessage),
1278 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1280 &transmit_download_request,
1287 * Reconnect to the FS service and transmit our queries NOW.
1289 * @param cls our download context
1293 do_reconnect (void *cls,
1294 const struct GNUNET_SCHEDULER_TaskContext *tc)
1296 struct GNUNET_FS_DownloadContext *dc = cls;
1297 struct GNUNET_CLIENT_Connection *client;
1299 dc->task = GNUNET_SCHEDULER_NO_TASK;
1300 client = GNUNET_CLIENT_connect (dc->h->sched,
1305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1306 "Connecting to `%s'-service failed, will try again.\n",
1311 dc->client = client;
1312 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1313 sizeof (struct SearchMessage),
1314 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1316 &transmit_download_request,
1318 GNUNET_CLIENT_receive (client,
1321 GNUNET_TIME_UNIT_FOREVER_REL);
1326 * Add entries that are not yet pending back to the pending list.
1328 * @param cls our download context
1330 * @param entry entry of type "struct DownloadRequest"
1334 retry_entry (void *cls,
1335 const GNUNET_HashCode *key,
1338 struct GNUNET_FS_DownloadContext *dc = cls;
1339 struct DownloadRequest *dr = entry;
1341 if (! dr->is_pending)
1343 dr->next = dc->pending;
1344 dr->is_pending = GNUNET_YES;
1345 dc->pending = entry;
1352 * We've lost our connection with the FS service.
1353 * Re-establish it and re-transmit all of our
1356 * @param dc download context that is having trouble
1359 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1362 if (NULL != dc->client)
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Moving all requests back to pending list\n");
1370 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1373 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1376 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Will try to reconnect in 1s\n");
1384 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1385 GNUNET_TIME_UNIT_SECONDS,
1393 * We're allowed to ask the FS service for our blocks. Start the download.
1395 * @param cls the 'struct GNUNET_FS_DownloadContext'
1396 * @param client handle to use for communcation with FS (we must destroy it!)
1399 activate_fs_download (void *cls,
1400 struct GNUNET_CLIENT_Connection *client)
1402 struct GNUNET_FS_DownloadContext *dc = cls;
1403 struct GNUNET_FS_ProgressInfo pi;
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Download activated\n");
1409 GNUNET_assert (NULL != client);
1410 GNUNET_assert (dc->client == NULL);
1411 GNUNET_assert (dc->th == NULL);
1412 dc->client = client;
1413 GNUNET_CLIENT_receive (client,
1416 GNUNET_TIME_UNIT_FOREVER_REL);
1417 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1418 GNUNET_FS_download_make_status_ (&pi, dc);
1419 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Asking for transmission to FS service\n");
1426 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1427 sizeof (struct SearchMessage),
1428 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1430 &transmit_download_request,
1432 GNUNET_assert (dc->th != NULL);
1437 * We must stop to ask the FS service for our blocks. Pause the download.
1439 * @param cls the 'struct GNUNET_FS_DownloadContext'
1442 deactivate_fs_download (void *cls)
1444 struct GNUNET_FS_DownloadContext *dc = cls;
1445 struct GNUNET_FS_ProgressInfo pi;
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "Download deactivated\n");
1453 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1456 if (NULL != dc->client)
1458 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1461 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1462 GNUNET_FS_download_make_status_ (&pi, dc);
1467 * Create SUSPEND event for the given download operation
1468 * and then clean up our state (without stop signal).
1470 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1473 GNUNET_FS_download_signal_suspend_ (void *cls)
1475 struct GNUNET_FS_DownloadContext *dc = cls;
1476 struct GNUNET_FS_ProgressInfo pi;
1478 if (dc->top != NULL)
1479 GNUNET_FS_end_top (dc->h, dc->top);
1480 while (NULL != dc->child_head)
1481 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1482 if (dc->search != NULL)
1484 dc->search->download = NULL;
1487 if (dc->job_queue != NULL)
1489 GNUNET_FS_dequeue_ (dc->job_queue);
1490 dc->job_queue = NULL;
1492 if (dc->parent != NULL)
1493 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1494 dc->parent->child_tail,
1496 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1497 GNUNET_FS_download_make_status_ (&pi, dc);
1498 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1499 GNUNET_SCHEDULER_cancel (dc->h->sched,
1501 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1504 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1505 GNUNET_free_non_null (dc->filename);
1506 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1507 GNUNET_FS_uri_destroy (dc->uri);
1508 GNUNET_free_non_null (dc->temp_filename);
1509 GNUNET_free_non_null (dc->serialization);
1515 * Download parts of a file. Note that this will store
1516 * the blocks at the respective offset in the given file. Also, the
1517 * download is still using the blocking of the underlying FS
1518 * encoding. As a result, the download may *write* outside of the
1519 * given boundaries (if offset and length do not match the 32k FS
1520 * block boundaries). <p>
1522 * This function should be used to focus a download towards a
1523 * particular portion of the file (optimization), not to strictly
1524 * limit the download to exactly those bytes.
1526 * @param h handle to the file sharing subsystem
1527 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1528 * @param meta known metadata for the file (can be NULL)
1529 * @param filename where to store the file, maybe NULL (then no file is
1530 * created on disk and data must be grabbed from the callbacks)
1531 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1532 * can be NULL (in which case we will pick a name if needed); the temporary file
1533 * may already exist, in which case we will try to use the data that is there and
1534 * if it is not what is desired, will overwrite it
1535 * @param offset at what offset should we start the download (typically 0)
1536 * @param length how many bytes should be downloaded starting at offset
1537 * @param anonymity anonymity level to use for the download
1538 * @param options various options
1539 * @param cctx initial value for the client context for this download
1540 * @param parent parent download to associate this download with (use NULL
1541 * for top-level downloads; useful for manually-triggered recursive downloads)
1542 * @return context that can be used to control this download
1544 struct GNUNET_FS_DownloadContext *
1545 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1546 const struct GNUNET_FS_Uri *uri,
1547 const struct GNUNET_CONTAINER_MetaData *meta,
1548 const char *filename,
1549 const char *tempname,
1553 enum GNUNET_FS_DownloadOptions options,
1555 struct GNUNET_FS_DownloadContext *parent)
1557 struct GNUNET_FS_ProgressInfo pi;
1558 struct GNUNET_FS_DownloadContext *dc;
1560 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1561 if ( (offset + length < offset) ||
1562 (offset + length > uri->data.chk.file_length) )
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Starting download `%s' of %llu bytes\n",
1571 (unsigned long long) length);
1573 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1575 dc->parent = parent;
1578 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1582 dc->uri = GNUNET_FS_uri_dup (uri);
1583 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1584 dc->client_info = cctx;
1585 dc->start_time = GNUNET_TIME_absolute_get ();
1586 if (NULL != filename)
1588 dc->filename = GNUNET_strdup (filename);
1589 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1590 GNUNET_DISK_file_size (filename,
1594 if (GNUNET_FS_uri_test_loc (dc->uri))
1595 GNUNET_assert (GNUNET_OK ==
1596 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1598 dc->offset = offset;
1599 dc->length = length;
1600 dc->anonymity = anonymity;
1601 dc->options = options;
1602 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1603 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1604 if ( (filename == NULL) &&
1605 (is_recursive_download (dc) ) )
1607 if (tempname != NULL)
1608 dc->temp_filename = GNUNET_strdup (tempname);
1610 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Download tree has depth %u\n",
1620 dc->top = GNUNET_FS_make_top (dc->h,
1621 &GNUNET_FS_download_signal_suspend_,
1624 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1625 pi.value.download.specifics.start.meta = meta;
1626 GNUNET_FS_download_make_status_ (&pi, dc);
1627 schedule_block_download (dc,
1628 &dc->uri->data.chk.chk,
1630 1 /* 0 == CHK, 1 == top */);
1631 GNUNET_FS_download_sync_ (dc);
1632 GNUNET_FS_download_start_downloading_ (dc);
1638 * Download parts of a file based on a search result. The download
1639 * will be associated with the search result (and the association
1640 * will be preserved when serializing/deserializing the state).
1641 * If the search is stopped, the download will not be aborted but
1642 * be 'promoted' to a stand-alone download.
1644 * As with the other download function, this will store
1645 * the blocks at the respective offset in the given file. Also, the
1646 * download is still using the blocking of the underlying FS
1647 * encoding. As a result, the download may *write* outside of the
1648 * given boundaries (if offset and length do not match the 32k FS
1649 * block boundaries). <p>
1651 * The given range can be used to focus a download towards a
1652 * particular portion of the file (optimization), not to strictly
1653 * limit the download to exactly those bytes.
1655 * @param h handle to the file sharing subsystem
1656 * @param sr the search result to use for the download (determines uri and
1657 * meta data and associations)
1658 * @param filename where to store the file, maybe NULL (then no file is
1659 * created on disk and data must be grabbed from the callbacks)
1660 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1661 * can be NULL (in which case we will pick a name if needed); the temporary file
1662 * may already exist, in which case we will try to use the data that is there and
1663 * if it is not what is desired, will overwrite it
1664 * @param offset at what offset should we start the download (typically 0)
1665 * @param length how many bytes should be downloaded starting at offset
1666 * @param anonymity anonymity level to use for the download
1667 * @param options various download options
1668 * @param cctx initial value for the client context for this download
1669 * @return context that can be used to control this download
1671 struct GNUNET_FS_DownloadContext *
1672 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
1673 struct GNUNET_FS_SearchResult *sr,
1674 const char *filename,
1675 const char *tempname,
1679 enum GNUNET_FS_DownloadOptions options,
1682 struct GNUNET_FS_ProgressInfo pi;
1683 struct GNUNET_FS_DownloadContext *dc;
1685 if ( (sr == NULL) ||
1686 (sr->download != NULL) )
1691 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri));
1692 if ( (offset + length < offset) ||
1693 (offset + length > sr->uri->data.chk.file_length) )
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "Starting download `%s' of %llu bytes\n",
1702 (unsigned long long) length);
1704 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1708 if (sr->probe_ctx != NULL)
1710 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1711 sr->probe_ctx = NULL;
1713 dc->uri = GNUNET_FS_uri_dup (sr->uri);
1714 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
1715 dc->client_info = cctx;
1716 dc->start_time = GNUNET_TIME_absolute_get ();
1717 if (NULL != filename)
1719 dc->filename = GNUNET_strdup (filename);
1720 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1721 GNUNET_DISK_file_size (filename,
1725 if (GNUNET_FS_uri_test_loc (dc->uri))
1726 GNUNET_assert (GNUNET_OK ==
1727 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1729 dc->offset = offset;
1730 dc->length = length;
1731 dc->anonymity = anonymity;
1732 dc->options = options;
1733 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1734 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1735 if ( (filename == NULL) &&
1736 (is_recursive_download (dc) ) )
1738 if (tempname != NULL)
1739 dc->temp_filename = GNUNET_strdup (tempname);
1741 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1746 "Download tree has depth %u\n",
1749 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1750 pi.value.download.specifics.start.meta = dc->meta;
1751 GNUNET_FS_download_make_status_ (&pi, dc);
1752 schedule_block_download (dc,
1753 &dc->uri->data.chk.chk,
1755 1 /* 0 == CHK, 1 == top */);
1756 GNUNET_FS_download_sync_ (dc);
1757 GNUNET_FS_download_start_downloading_ (dc);
1763 * Start the downloading process (by entering the queue).
1765 * @param dc our download context
1768 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
1770 GNUNET_assert (dc->job_queue == NULL);
1771 dc->job_queue = GNUNET_FS_queue_ (dc->h,
1772 &activate_fs_download,
1773 &deactivate_fs_download,
1775 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
1780 * Stop a download (aborts if download is incomplete).
1782 * @param dc handle for the download
1783 * @param do_delete delete files of incomplete downloads
1786 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1789 struct GNUNET_FS_ProgressInfo pi;
1792 if (dc->top != NULL)
1793 GNUNET_FS_end_top (dc->h, dc->top);
1794 if (dc->search != NULL)
1796 dc->search->download = NULL;
1799 if (dc->job_queue != NULL)
1801 GNUNET_FS_dequeue_ (dc->job_queue);
1802 dc->job_queue = NULL;
1804 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
1805 while (NULL != dc->child_head)
1806 GNUNET_FS_download_stop (dc->child_head,
1808 if (dc->parent != NULL)
1809 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1810 dc->parent->child_tail,
1812 if (dc->serialization != NULL)
1813 GNUNET_FS_remove_sync_file_ (dc->h,
1814 ( (dc->parent != NULL) || (dc->search != NULL) )
1815 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1816 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
1818 if ( (GNUNET_YES == have_children) &&
1819 (dc->parent == NULL) )
1820 GNUNET_FS_remove_sync_dir_ (dc->h,
1821 (dc->search != NULL)
1822 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1823 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1825 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1826 GNUNET_FS_download_make_status_ (&pi, dc);
1827 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1828 GNUNET_SCHEDULER_cancel (dc->h->sched,
1830 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1833 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1834 if (dc->filename != NULL)
1836 if ( (dc->completed != dc->length) &&
1837 (GNUNET_YES == do_delete) )
1839 if (0 != UNLINK (dc->filename))
1840 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1844 GNUNET_free (dc->filename);
1846 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1847 GNUNET_FS_uri_destroy (dc->uri);
1848 if (NULL != dc->temp_filename)
1850 if (0 != UNLINK (dc->temp_filename))
1851 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1854 GNUNET_free (dc->temp_filename);
1856 GNUNET_free_non_null (dc->serialization);
1860 /* end of fs_download.c */