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 * - different priority for scheduling probe downloads?
27 * - check if iblocks can be computed from existing blocks (can wait, hard)
30 #include "gnunet_constants.h"
31 #include "gnunet_fs_service.h"
35 #define DEBUG_DOWNLOAD GNUNET_NO
38 * Determine if the given download (options and meta data) should cause
39 * use to try to do a recursive download.
42 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
44 return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
45 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
46 ( (dc->meta == NULL) &&
47 ( (NULL == dc->filename) ||
48 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
50 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
51 GNUNET_FS_DIRECTORY_EXT)) ) ) ) );
56 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
57 * only have to truncate the file once we're done).
59 * Given the offset of a block (with respect to the DBLOCKS) and its
60 * depth, return the offset where we would store this block in the
63 * @param fsize overall file size
64 * @param off offset of the block in the file
65 * @param depth depth of the block in the tree
66 * @param treedepth maximum depth of the tree
67 * @return off for DBLOCKS (depth == treedepth),
68 * otherwise an offset past the end
69 * of the file that does not overlap
70 * with the range for any other block
73 compute_disk_offset (uint64_t fsize,
76 unsigned int treedepth)
79 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
80 uint64_t loff; /* where do IBlocks for depth "i" start? */
81 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
83 if (depth == treedepth)
85 /* first IBlocks start at the end of file, rounded up
86 to full DBLOCK_SIZE */
87 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
88 lsize = ( (fsize + DBLOCK_SIZE-1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
89 GNUNET_assert (0 == (off % DBLOCK_SIZE));
90 ioff = (off / DBLOCK_SIZE);
91 for (i=treedepth-1;i>depth;i--)
94 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
95 GNUNET_assert (lsize > 0);
96 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
97 ioff /= CHK_PER_INODE;
99 return loff + ioff * sizeof (struct ContentHashKey);
104 * Given a file of the specified treedepth and a block at the given
105 * offset and depth, calculate the offset for the CHK at the given
108 * @param offset the offset of the first
109 * DBLOCK in the subtree of the
111 * @param depth the depth of the IBLOCK in the tree
112 * @param treedepth overall depth of the tree
113 * @param k which CHK in the IBLOCK are we
115 * @return offset if k=0, otherwise an appropriately
116 * larger value (i.e., if depth = treedepth-1,
117 * the returned value should be offset+DBLOCK_SIZE)
120 compute_dblock_offset (uint64_t offset,
122 unsigned int treedepth,
126 uint64_t lsize; /* what is the size of the sum of all DBlocks
127 that a CHK at depth i corresponds to? */
129 if (depth == treedepth)
132 for (i=treedepth-1;i>depth;i--)
133 lsize *= CHK_PER_INODE;
134 return offset + k * lsize;
139 * Fill in all of the generic fields for a download event and call the
142 * @param pi structure to fill in
143 * @param dc overall download context
146 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
147 struct GNUNET_FS_DownloadContext *dc)
149 pi->value.download.dc = dc;
150 pi->value.download.cctx
152 pi->value.download.pctx
153 = (dc->parent == NULL) ? NULL : dc->parent->client_info;
154 pi->value.download.sctx
155 = (dc->search == NULL) ? NULL : dc->search->client_info;
156 pi->value.download.uri
158 pi->value.download.filename
160 pi->value.download.size
162 pi->value.download.duration
163 = GNUNET_TIME_absolute_get_duration (dc->start_time);
164 pi->value.download.completed
166 pi->value.download.anonymity
168 pi->value.download.eta
169 = GNUNET_TIME_calculate_eta (dc->start_time,
172 pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
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 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len,
341 GNUNET_CRYPTO_hash (enc, len, &query);
342 if (0 == memcmp (&query,
344 sizeof (GNUNET_HashCode)))
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Matching block already present, no need for download!\n");
350 /* already got it! */
354 prc.type = (dc->treedepth == depth)
355 ? GNUNET_BLOCK_TYPE_DBLOCK
356 : GNUNET_BLOCK_TYPE_IBLOCK;
357 prc.query = chk->query;
358 prc.do_store = GNUNET_NO; /* useless */
359 process_result_with_request (&prc,
367 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
373 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
374 if (depth < dc->treedepth)
376 // FIXME: try if we could
377 // reconstitute this IBLOCK
378 // from the existing blocks on disk (can wait)
379 // (read block(s), encode, compare with
380 // query; if matches, simply return)
383 if ( (dc->th == NULL) &&
384 (dc->client != NULL) )
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Asking for transmission to FS service\n");
390 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
391 sizeof (struct SearchMessage),
392 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
394 &transmit_download_request,
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Transmission request not issued (%p %p)\n",
413 * Suggest a filename based on given metadata.
415 * @param md given meta data
416 * @return NULL if meta data is useless for suggesting a filename
419 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
421 static const char *mimeMap[][2] = {
422 {"application/bz2", ".bz2"},
423 {"application/gnunet-directory", ".gnd"},
424 {"application/java", ".class"},
425 {"application/msword", ".doc"},
426 {"application/ogg", ".ogg"},
427 {"application/pdf", ".pdf"},
428 {"application/pgp-keys", ".key"},
429 {"application/pgp-signature", ".pgp"},
430 {"application/postscript", ".ps"},
431 {"application/rar", ".rar"},
432 {"application/rtf", ".rtf"},
433 {"application/xml", ".xml"},
434 {"application/x-debian-package", ".deb"},
435 {"application/x-dvi", ".dvi"},
436 {"applixation/x-flac", ".flac"},
437 {"applixation/x-gzip", ".gz"},
438 {"application/x-java-archive", ".jar"},
439 {"application/x-java-vm", ".class"},
440 {"application/x-python-code", ".pyc"},
441 {"application/x-redhat-package-manager", ".rpm"},
442 {"application/x-rpm", ".rpm"},
443 {"application/x-tar", ".tar"},
444 {"application/x-tex-pk", ".pk"},
445 {"application/x-texinfo", ".texinfo"},
446 {"application/x-xcf", ".xcf"},
447 {"application/x-xfig", ".xfig"},
448 {"application/zip", ".zip"},
450 {"audio/midi", ".midi"},
451 {"audio/mpeg", ".mp3"},
452 {"audio/real", ".rm"},
453 {"audio/x-wav", ".wav"},
455 {"image/gif", ".gif"},
456 {"image/jpeg", ".jpg"},
457 {"image/pcx", ".pcx"},
458 {"image/png", ".png"},
459 {"image/tiff", ".tiff"},
460 {"image/x-ms-bmp", ".bmp"},
461 {"image/x-xpixmap", ".xpm"},
463 {"text/css", ".css"},
464 {"text/html", ".html"},
465 {"text/plain", ".txt"},
466 {"text/rtf", ".rtf"},
467 {"text/x-c++hdr", ".h++"},
468 {"text/x-c++src", ".c++"},
469 {"text/x-chdr", ".h"},
470 {"text/x-csrc", ".c"},
471 {"text/x-java", ".java"},
472 {"text/x-moc", ".moc"},
473 {"text/x-pascal", ".pas"},
474 {"text/x-perl", ".pl"},
475 {"text/x-python", ".py"},
476 {"text/x-tex", ".tex"},
478 {"video/avi", ".avi"},
479 {"video/mpeg", ".mpeg"},
480 {"video/quicktime", ".qt"},
481 {"video/real", ".rm"},
482 {"video/x-msvideo", ".avi"},
491 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
492 EXTRACTOR_METATYPE_FILENAME);
496 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
497 EXTRACTOR_METATYPE_MIMETYPE);
501 while ( (mimeMap[i][0] != NULL) &&
502 (0 != strcmp (mime, mimeMap[i][0])))
504 if (mimeMap[i][1] == NULL)
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
506 GNUNET_ERROR_TYPE_BULK,
507 _("Did not find mime type `%s' in extension list.\n"),
513 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
514 EXTRACTOR_METATYPE_TITLE,
515 EXTRACTOR_METATYPE_BOOK_TITLE,
516 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
517 EXTRACTOR_METATYPE_PACKAGE_NAME,
518 EXTRACTOR_METATYPE_URL,
519 EXTRACTOR_METATYPE_URI,
520 EXTRACTOR_METATYPE_DESCRIPTION,
521 EXTRACTOR_METATYPE_ISRC,
522 EXTRACTOR_METATYPE_JOURNAL_NAME,
523 EXTRACTOR_METATYPE_AUTHOR_NAME,
524 EXTRACTOR_METATYPE_SUBJECT,
525 EXTRACTOR_METATYPE_ALBUM,
526 EXTRACTOR_METATYPE_ARTIST,
527 EXTRACTOR_METATYPE_KEYWORDS,
528 EXTRACTOR_METATYPE_COMMENT,
529 EXTRACTOR_METATYPE_UNKNOWN,
531 if ( (base == NULL) &&
535 return GNUNET_strdup (ext);
538 GNUNET_asprintf (&ret,
548 * We've lost our connection with the FS service.
549 * Re-establish it and re-transmit all of our
552 * @param dc download context that is having trouble
555 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
559 * We found an entry in a directory. Check if the respective child
560 * already exists and if not create the respective child download.
562 * @param cls the parent download
563 * @param filename name of the file in the directory
564 * @param uri URI of the file (CHK or LOC)
565 * @param meta meta data of the file
566 * @param length number of bytes in data
567 * @param data contents of the file (or NULL if they were not inlined)
570 trigger_recursive_download (void *cls,
571 const char *filename,
572 const struct GNUNET_FS_Uri *uri,
573 const struct GNUNET_CONTAINER_MetaData *meta,
579 * We're done downloading a directory. Open the file and
580 * trigger all of the (remaining) child downloads.
582 * @param dc context of download that just completed
585 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
590 struct GNUNET_DISK_FileHandle *h;
591 struct GNUNET_DISK_MapHandle *m;
593 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
594 size = (size_t) size64;
595 if (size64 != (uint64_t) size)
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
601 if (dc->filename != NULL)
603 h = GNUNET_DISK_file_open (dc->filename,
604 GNUNET_DISK_OPEN_READ,
605 GNUNET_DISK_PERM_NONE);
609 GNUNET_assert (dc->temp_filename != NULL);
610 h = GNUNET_DISK_file_open (dc->temp_filename,
611 GNUNET_DISK_OPEN_READ,
612 GNUNET_DISK_PERM_NONE);
616 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
620 _("Directory too large for system address space\n"));
624 GNUNET_FS_directory_list_contents (size,
627 &trigger_recursive_download,
629 GNUNET_DISK_file_unmap (m);
631 GNUNET_DISK_file_close (h);
632 if (dc->filename == NULL)
634 if (0 != UNLINK (dc->temp_filename))
635 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
638 GNUNET_free (dc->temp_filename);
639 dc->temp_filename = NULL;
645 * Check if all child-downloads have completed and
646 * if so, signal completion (and possibly recurse to
650 check_completed (struct GNUNET_FS_DownloadContext *dc)
652 struct GNUNET_FS_ProgressInfo pi;
653 struct GNUNET_FS_DownloadContext *pos;
655 pos = dc->child_head;
658 if ( (pos->emsg == NULL) &&
659 (pos->completed < pos->length) )
660 return; /* not done yet */
661 if ( (pos->child_head != NULL) &&
662 (pos->has_finished != GNUNET_YES) )
663 return; /* not transitively done yet */
666 dc->has_finished = GNUNET_YES;
667 GNUNET_FS_download_sync_ (dc);
668 /* signal completion */
669 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
670 GNUNET_FS_download_make_status_ (&pi, dc);
671 if (dc->parent != NULL)
672 check_completed (dc->parent);
677 * We found an entry in a directory. Check if the respective child
678 * already exists and if not create the respective child download.
680 * @param cls the parent download
681 * @param filename name of the file in the directory
682 * @param uri URI of the file (CHK or LOC)
683 * @param meta meta data of the file
684 * @param length number of bytes in data
685 * @param data contents of the file (or NULL if they were not inlined)
688 trigger_recursive_download (void *cls,
689 const char *filename,
690 const struct GNUNET_FS_Uri *uri,
691 const struct GNUNET_CONTAINER_MetaData *meta,
695 struct GNUNET_FS_DownloadContext *dc = cls;
696 struct GNUNET_FS_DownloadContext *cpos;
697 struct GNUNET_DISK_FileHandle *fh;
699 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);
744 /* change '\' to '/' (this should have happened
745 during insertion, but malicious peers may
746 not have done this) */
747 while (NULL != (pos = strstr (fn, "\\")))
749 /* remove '../' everywhere (again, well-behaved
750 peers don't do this, but don't trust that
751 we did not get something nasty) */
752 while (NULL != (pos = strstr (fn, "../")))
760 if (dc->filename == NULL)
766 dn = GNUNET_strdup (dc->filename);
767 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
769 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
770 GNUNET_FS_DIRECTORY_EXT)) );
771 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
773 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
774 GNUNET_FS_DIRECTORY_EXT)) )
775 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
776 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
777 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
779 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
780 GNUNET_FS_DIRECTORY_EXT)) ) )
782 GNUNET_asprintf (&full_name,
787 GNUNET_FS_DIRECTORY_EXT);
791 GNUNET_asprintf (&full_name,
799 if ( (full_name != NULL) &&
801 GNUNET_DISK_directory_create_for_file (full_name)) )
803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804 _("Failed to create directory for recursive download of `%s'\n"),
806 GNUNET_free (full_name);
807 GNUNET_free_non_null (fn);
812 if ( (data != NULL) &&
813 (GNUNET_FS_uri_chk_get_file_size (uri) == length) )
815 if (full_name == NULL)
817 temp_name = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
818 real_name = temp_name;
822 real_name = full_name;
824 /* write to disk, then trigger normal download which will instantly progress to completion */
825 fh = GNUNET_DISK_file_open (real_name,
826 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
827 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
830 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
833 GNUNET_free (full_name);
834 GNUNET_free_non_null (fn);
838 GNUNET_DISK_file_write (fh,
842 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
846 GNUNET_DISK_file_close (fh);
848 GNUNET_FS_download_start (dc->h,
851 full_name, temp_name,
853 GNUNET_FS_uri_chk_get_file_size (uri),
858 GNUNET_free_non_null (full_name);
859 GNUNET_free_non_null (temp_name);
860 GNUNET_free_non_null (fn);
865 * Free entries in the map.
867 * @param cls unused (NULL)
869 * @param entry entry of type "struct DownloadRequest" which is freed
873 free_entry (void *cls,
874 const GNUNET_HashCode *key,
883 * Iterator over entries in the pending requests in the 'active' map for the
884 * reply that we just got.
886 * @param cls closure (our 'struct ProcessResultClosure')
887 * @param key query for the given value / request
888 * @param value value in the hash map (a 'struct DownloadRequest')
889 * @return GNUNET_YES (we should continue to iterate); unless serious error
892 process_result_with_request (void *cls,
893 const GNUNET_HashCode * key,
896 struct ProcessResultClosure *prc = cls;
897 struct DownloadRequest *sm = value;
898 struct DownloadRequest *ppos;
899 struct DownloadRequest *pprev;
900 struct GNUNET_DISK_FileHandle *fh;
901 struct GNUNET_FS_DownloadContext *dc = prc->dc;
902 struct GNUNET_CRYPTO_AesSessionKey skey;
903 struct GNUNET_CRYPTO_AesInitializationVector iv;
905 struct GNUNET_FS_ProgressInfo pi;
910 struct ContentHashKey *chk;
913 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
925 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
928 GNUNET_assert (GNUNET_YES ==
929 GNUNET_CONTAINER_multihashmap_remove (dc->active,
932 /* if this request is on the pending list, remove it! */
940 dc->pending = ppos->next;
942 pprev->next = ppos->next;
948 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
949 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data,
956 dc->emsg = GNUNET_strdup ("internal error decrypting content");
959 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
964 if ( ( GNUNET_YES == prc->do_store) &&
965 ( (dc->filename != NULL) ||
966 (is_recursive_download (dc)) ) &&
967 ( (sm->depth == dc->treedepth) ||
968 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
970 fh = GNUNET_DISK_file_open (dc->filename != NULL
973 GNUNET_DISK_OPEN_READWRITE |
974 GNUNET_DISK_OPEN_CREATE,
975 GNUNET_DISK_PERM_USER_READ |
976 GNUNET_DISK_PERM_USER_WRITE |
977 GNUNET_DISK_PERM_GROUP_READ |
978 GNUNET_DISK_PERM_OTHER_READ);
981 (GNUNET_YES == prc->do_store) &&
982 ( (dc->filename != NULL) ||
983 (is_recursive_download (dc)) ) &&
984 ( (sm->depth == dc->treedepth) ||
985 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
987 GNUNET_asprintf (&dc->emsg,
988 _("Download failed: could not open file `%s': %s\n"),
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Saving decrypted block to disk at offset %llu\n",
998 (unsigned long long) off);
1001 GNUNET_DISK_file_seek (fh,
1003 GNUNET_DISK_SEEK_SET) ) )
1005 GNUNET_asprintf (&dc->emsg,
1006 _("Failed to seek to offset %llu in file `%s': %s\n"),
1007 (unsigned long long) off,
1013 GNUNET_DISK_file_write (fh,
1017 GNUNET_asprintf (&dc->emsg,
1018 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1019 (unsigned int) prc->size,
1020 (unsigned long long) off,
1025 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1028 if (sm->depth == dc->treedepth)
1031 if (sm->offset < dc->offset)
1033 /* starting offset begins in the middle of pt,
1034 do not count first bytes as progress */
1035 GNUNET_assert (app > (dc->offset - sm->offset));
1036 app -= (dc->offset - sm->offset);
1038 if (sm->offset + prc->size > dc->offset + dc->length)
1040 /* end of block is after relevant range,
1041 do not count last bytes as progress */
1042 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
1043 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
1045 dc->completed += app;
1047 /* do recursive download if option is set and either meta data
1048 says it is a directory or if no meta data is given AND filename
1049 ends in '.gnd' (top-level case) */
1050 if (is_recursive_download (dc))
1051 GNUNET_FS_directory_list_contents (prc->size,
1054 &trigger_recursive_download,
1058 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1059 pi.value.download.specifics.progress.data = pt;
1060 pi.value.download.specifics.progress.offset = sm->offset;
1061 pi.value.download.specifics.progress.data_len = prc->size;
1062 pi.value.download.specifics.progress.depth = sm->depth;
1063 GNUNET_FS_download_make_status_ (&pi, dc);
1064 GNUNET_assert (dc->completed <= dc->length);
1065 if (dc->completed == dc->length)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Download completed, truncating file to desired length %llu\n",
1070 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1072 /* truncate file to size (since we store IBlocks at the end) */
1073 if (dc->filename != NULL)
1075 if (0 != truncate (dc->filename,
1076 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1077 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1081 if (dc->job_queue != NULL)
1083 GNUNET_FS_dequeue_ (dc->job_queue);
1084 dc->job_queue = NULL;
1086 if (is_recursive_download (dc))
1087 full_recursive_download (dc);
1088 if (dc->child_head == NULL)
1090 /* signal completion */
1091 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
1092 GNUNET_FS_download_make_status_ (&pi, dc);
1093 if (dc->parent != NULL)
1094 check_completed (dc->parent);
1096 GNUNET_assert (sm->depth == dc->treedepth);
1098 if (sm->depth == dc->treedepth)
1100 GNUNET_FS_download_sync_ (dc);
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1108 (unsigned long long) sm->offset);
1110 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1111 chk = (struct ContentHashKey*) pt;
1112 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1114 off = compute_dblock_offset (sm->offset,
1118 if ( (off + DBLOCK_SIZE >= dc->offset) &&
1119 (off < dc->offset + dc->length) )
1120 schedule_block_download (dc,
1126 GNUNET_FS_download_sync_ (dc);
1131 GNUNET_DISK_file_close (fh);
1132 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1133 pi.value.download.specifics.error.message = dc->emsg;
1134 GNUNET_FS_download_make_status_ (&pi, dc);
1135 /* abort all pending requests */
1138 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1141 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1142 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1148 GNUNET_FS_download_sync_ (dc);
1154 * Process a download result.
1156 * @param dc our download context
1157 * @param type type of the result
1158 * @param data the (encrypted) response
1159 * @param size size of data
1162 process_result (struct GNUNET_FS_DownloadContext *dc,
1163 enum GNUNET_BLOCK_Type type,
1167 struct ProcessResultClosure prc;
1173 prc.do_store = GNUNET_YES;
1174 GNUNET_CRYPTO_hash (data, size, &prc.query);
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Received result for query `%s' from `%s'-service\n",
1178 GNUNET_h2s (&prc.query),
1181 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1183 &process_result_with_request,
1189 * Type of a function to call when we receive a message
1192 * @param cls closure
1193 * @param msg message received, NULL on timeout or fatal error
1196 receive_results (void *cls,
1197 const struct GNUNET_MessageHeader * msg)
1199 struct GNUNET_FS_DownloadContext *dc = cls;
1200 const struct PutMessage *cm;
1203 if ( (NULL == msg) ||
1204 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1205 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1207 GNUNET_break (msg == NULL);
1211 msize = ntohs(msg->size);
1212 cm = (const struct PutMessage*) msg;
1216 msize - sizeof (struct PutMessage));
1217 if (dc->client == NULL)
1218 return; /* fatal error */
1219 /* continue receiving */
1220 GNUNET_CLIENT_receive (dc->client,
1223 GNUNET_TIME_UNIT_FOREVER_REL);
1229 * We're ready to transmit a search request to the
1230 * file-sharing service. Do it. If there is
1231 * more than one request pending, try to send
1232 * multiple or request another transmission.
1234 * @param cls closure
1235 * @param size number of bytes available in buf
1236 * @param buf where the callee should write the message
1237 * @return number of bytes written to buf
1240 transmit_download_request (void *cls,
1244 struct GNUNET_FS_DownloadContext *dc = cls;
1246 struct SearchMessage *sm;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Transmitting download request failed, trying to reconnect\n");
1258 GNUNET_assert (size >= sizeof (struct SearchMessage));
1261 while ( (dc->pending != NULL) &&
1262 (size >= msize + sizeof (struct SearchMessage)) )
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Transmitting download request for `%s' to `%s'-service\n",
1267 GNUNET_h2s (&dc->pending->chk.query),
1270 memset (sm, 0, sizeof (struct SearchMessage));
1271 sm->header.size = htons (sizeof (struct SearchMessage));
1272 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1273 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1274 sm->options = htonl (1);
1276 sm->options = htonl (0);
1277 if (dc->pending->depth == dc->treedepth)
1278 sm->type = htonl (GNUNET_BLOCK_TYPE_DBLOCK);
1280 sm->type = htonl (GNUNET_BLOCK_TYPE_IBLOCK);
1281 sm->anonymity_level = htonl (dc->anonymity);
1282 sm->target = dc->target.hashPubKey;
1283 sm->query = dc->pending->chk.query;
1284 dc->pending->is_pending = GNUNET_NO;
1285 dc->pending = dc->pending->next;
1286 msize += sizeof (struct SearchMessage);
1289 if (dc->pending != NULL)
1290 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1291 sizeof (struct SearchMessage),
1292 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1294 &transmit_download_request,
1301 * Reconnect to the FS service and transmit our queries NOW.
1303 * @param cls our download context
1307 do_reconnect (void *cls,
1308 const struct GNUNET_SCHEDULER_TaskContext *tc)
1310 struct GNUNET_FS_DownloadContext *dc = cls;
1311 struct GNUNET_CLIENT_Connection *client;
1313 dc->task = GNUNET_SCHEDULER_NO_TASK;
1314 client = GNUNET_CLIENT_connect (dc->h->sched,
1319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1320 "Connecting to `%s'-service failed, will try again.\n",
1325 dc->client = client;
1326 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1327 sizeof (struct SearchMessage),
1328 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1330 &transmit_download_request,
1332 GNUNET_CLIENT_receive (client,
1335 GNUNET_TIME_UNIT_FOREVER_REL);
1340 * Add entries that are not yet pending back to the pending list.
1342 * @param cls our download context
1344 * @param entry entry of type "struct DownloadRequest"
1348 retry_entry (void *cls,
1349 const GNUNET_HashCode *key,
1352 struct GNUNET_FS_DownloadContext *dc = cls;
1353 struct DownloadRequest *dr = entry;
1355 if (! dr->is_pending)
1357 dr->next = dc->pending;
1358 dr->is_pending = GNUNET_YES;
1359 dc->pending = entry;
1366 * We've lost our connection with the FS service.
1367 * Re-establish it and re-transmit all of our
1370 * @param dc download context that is having trouble
1373 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1376 if (NULL != dc->client)
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Moving all requests back to pending list\n");
1384 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1387 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1390 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Will try to reconnect in 1s\n");
1398 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1399 GNUNET_TIME_UNIT_SECONDS,
1407 * We're allowed to ask the FS service for our blocks. Start the download.
1409 * @param cls the 'struct GNUNET_FS_DownloadContext'
1410 * @param client handle to use for communcation with FS (we must destroy it!)
1413 activate_fs_download (void *cls,
1414 struct GNUNET_CLIENT_Connection *client)
1416 struct GNUNET_FS_DownloadContext *dc = cls;
1417 struct GNUNET_FS_ProgressInfo pi;
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Download activated\n");
1423 GNUNET_assert (NULL != client);
1424 GNUNET_assert (dc->client == NULL);
1425 GNUNET_assert (dc->th == NULL);
1426 dc->client = client;
1427 GNUNET_CLIENT_receive (client,
1430 GNUNET_TIME_UNIT_FOREVER_REL);
1431 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1432 GNUNET_FS_download_make_status_ (&pi, dc);
1433 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Asking for transmission to FS service\n");
1440 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1441 sizeof (struct SearchMessage),
1442 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1444 &transmit_download_request,
1446 GNUNET_assert (dc->th != NULL);
1451 * We must stop to ask the FS service for our blocks. Pause the download.
1453 * @param cls the 'struct GNUNET_FS_DownloadContext'
1456 deactivate_fs_download (void *cls)
1458 struct GNUNET_FS_DownloadContext *dc = cls;
1459 struct GNUNET_FS_ProgressInfo pi;
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 "Download deactivated\n");
1467 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1470 if (NULL != dc->client)
1472 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1475 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1476 GNUNET_FS_download_make_status_ (&pi, dc);
1481 * Create SUSPEND event for the given download operation
1482 * and then clean up our state (without stop signal).
1484 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1487 GNUNET_FS_download_signal_suspend_ (void *cls)
1489 struct GNUNET_FS_DownloadContext *dc = cls;
1490 struct GNUNET_FS_ProgressInfo pi;
1492 if (dc->top != NULL)
1493 GNUNET_FS_end_top (dc->h, dc->top);
1494 while (NULL != dc->child_head)
1495 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1496 if (dc->search != NULL)
1498 dc->search->download = NULL;
1501 if (dc->job_queue != NULL)
1503 GNUNET_FS_dequeue_ (dc->job_queue);
1504 dc->job_queue = NULL;
1506 if (dc->parent != NULL)
1507 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1508 dc->parent->child_tail,
1510 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1511 GNUNET_FS_download_make_status_ (&pi, dc);
1512 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1513 GNUNET_SCHEDULER_cancel (dc->h->sched,
1515 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1518 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1519 GNUNET_free_non_null (dc->filename);
1520 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1521 GNUNET_FS_uri_destroy (dc->uri);
1522 GNUNET_free_non_null (dc->temp_filename);
1523 GNUNET_free_non_null (dc->serialization);
1529 * Download parts of a file. Note that this will store
1530 * the blocks at the respective offset in the given file. Also, the
1531 * download is still using the blocking of the underlying FS
1532 * encoding. As a result, the download may *write* outside of the
1533 * given boundaries (if offset and length do not match the 32k FS
1534 * block boundaries). <p>
1536 * This function should be used to focus a download towards a
1537 * particular portion of the file (optimization), not to strictly
1538 * limit the download to exactly those bytes.
1540 * @param h handle to the file sharing subsystem
1541 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1542 * @param meta known metadata for the file (can be NULL)
1543 * @param filename where to store the file, maybe NULL (then no file is
1544 * created on disk and data must be grabbed from the callbacks)
1545 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1546 * can be NULL (in which case we will pick a name if needed); the temporary file
1547 * may already exist, in which case we will try to use the data that is there and
1548 * if it is not what is desired, will overwrite it
1549 * @param offset at what offset should we start the download (typically 0)
1550 * @param length how many bytes should be downloaded starting at offset
1551 * @param anonymity anonymity level to use for the download
1552 * @param options various options
1553 * @param cctx initial value for the client context for this download
1554 * @param parent parent download to associate this download with (use NULL
1555 * for top-level downloads; useful for manually-triggered recursive downloads)
1556 * @return context that can be used to control this download
1558 struct GNUNET_FS_DownloadContext *
1559 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1560 const struct GNUNET_FS_Uri *uri,
1561 const struct GNUNET_CONTAINER_MetaData *meta,
1562 const char *filename,
1563 const char *tempname,
1567 enum GNUNET_FS_DownloadOptions options,
1569 struct GNUNET_FS_DownloadContext *parent)
1571 struct GNUNET_FS_ProgressInfo pi;
1572 struct GNUNET_FS_DownloadContext *dc;
1574 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1575 if ( (offset + length < offset) ||
1576 (offset + length > uri->data.chk.file_length) )
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Starting download `%s' of %llu bytes\n",
1585 (unsigned long long) length);
1587 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1589 dc->parent = parent;
1592 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1596 dc->uri = GNUNET_FS_uri_dup (uri);
1597 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1598 dc->client_info = cctx;
1599 dc->start_time = GNUNET_TIME_absolute_get ();
1600 if (NULL != filename)
1602 dc->filename = GNUNET_strdup (filename);
1603 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1604 GNUNET_DISK_file_size (filename,
1608 if (GNUNET_FS_uri_test_loc (dc->uri))
1609 GNUNET_assert (GNUNET_OK ==
1610 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1612 dc->offset = offset;
1613 dc->length = length;
1614 dc->anonymity = anonymity;
1615 dc->options = options;
1616 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1617 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1618 if ( (filename == NULL) &&
1619 (is_recursive_download (dc) ) )
1621 if (tempname != NULL)
1622 dc->temp_filename = GNUNET_strdup (tempname);
1624 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Download tree has depth %u\n",
1634 dc->top = GNUNET_FS_make_top (dc->h,
1635 &GNUNET_FS_download_signal_suspend_,
1638 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1639 pi.value.download.specifics.start.meta = meta;
1640 GNUNET_FS_download_make_status_ (&pi, dc);
1641 schedule_block_download (dc,
1642 &dc->uri->data.chk.chk,
1644 1 /* 0 == CHK, 1 == top */);
1645 GNUNET_FS_download_sync_ (dc);
1646 GNUNET_FS_download_start_downloading_ (dc);
1652 * Download parts of a file based on a search result. The download
1653 * will be associated with the search result (and the association
1654 * will be preserved when serializing/deserializing the state).
1655 * If the search is stopped, the download will not be aborted but
1656 * be 'promoted' to a stand-alone download.
1658 * As with the other download function, this will store
1659 * the blocks at the respective offset in the given file. Also, the
1660 * download is still using the blocking of the underlying FS
1661 * encoding. As a result, the download may *write* outside of the
1662 * given boundaries (if offset and length do not match the 32k FS
1663 * block boundaries). <p>
1665 * The given range can be used to focus a download towards a
1666 * particular portion of the file (optimization), not to strictly
1667 * limit the download to exactly those bytes.
1669 * @param h handle to the file sharing subsystem
1670 * @param sr the search result to use for the download (determines uri and
1671 * meta data and associations)
1672 * @param filename where to store the file, maybe NULL (then no file is
1673 * created on disk and data must be grabbed from the callbacks)
1674 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1675 * can be NULL (in which case we will pick a name if needed); the temporary file
1676 * may already exist, in which case we will try to use the data that is there and
1677 * if it is not what is desired, will overwrite it
1678 * @param offset at what offset should we start the download (typically 0)
1679 * @param length how many bytes should be downloaded starting at offset
1680 * @param anonymity anonymity level to use for the download
1681 * @param options various download options
1682 * @param cctx initial value for the client context for this download
1683 * @return context that can be used to control this download
1685 struct GNUNET_FS_DownloadContext *
1686 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
1687 struct GNUNET_FS_SearchResult *sr,
1688 const char *filename,
1689 const char *tempname,
1693 enum GNUNET_FS_DownloadOptions options,
1696 struct GNUNET_FS_ProgressInfo pi;
1697 struct GNUNET_FS_DownloadContext *dc;
1699 if ( (sr == NULL) ||
1700 (sr->download != NULL) )
1705 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri));
1706 if ( (offset + length < offset) ||
1707 (offset + length > sr->uri->data.chk.file_length) )
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Starting download `%s' of %llu bytes\n",
1716 (unsigned long long) length);
1718 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1722 if (sr->probe_ctx != NULL)
1724 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1725 sr->probe_ctx = NULL;
1727 dc->uri = GNUNET_FS_uri_dup (sr->uri);
1728 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
1729 dc->client_info = cctx;
1730 dc->start_time = GNUNET_TIME_absolute_get ();
1731 if (NULL != filename)
1733 dc->filename = GNUNET_strdup (filename);
1734 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1735 GNUNET_DISK_file_size (filename,
1739 if (GNUNET_FS_uri_test_loc (dc->uri))
1740 GNUNET_assert (GNUNET_OK ==
1741 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1743 dc->offset = offset;
1744 dc->length = length;
1745 dc->anonymity = anonymity;
1746 dc->options = options;
1747 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1748 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1749 if ( (filename == NULL) &&
1750 (is_recursive_download (dc) ) )
1752 if (tempname != NULL)
1753 dc->temp_filename = GNUNET_strdup (tempname);
1755 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1760 "Download tree has depth %u\n",
1763 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1764 pi.value.download.specifics.start.meta = dc->meta;
1765 GNUNET_FS_download_make_status_ (&pi, dc);
1766 schedule_block_download (dc,
1767 &dc->uri->data.chk.chk,
1769 1 /* 0 == CHK, 1 == top */);
1770 GNUNET_FS_download_sync_ (dc);
1771 GNUNET_FS_download_start_downloading_ (dc);
1777 * Start the downloading process (by entering the queue).
1779 * @param dc our download context
1782 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
1784 GNUNET_assert (dc->job_queue == NULL);
1785 dc->job_queue = GNUNET_FS_queue_ (dc->h,
1786 &activate_fs_download,
1787 &deactivate_fs_download,
1789 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
1794 * Stop a download (aborts if download is incomplete).
1796 * @param dc handle for the download
1797 * @param do_delete delete files of incomplete downloads
1800 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1803 struct GNUNET_FS_ProgressInfo pi;
1806 if (dc->top != NULL)
1807 GNUNET_FS_end_top (dc->h, dc->top);
1808 if (dc->search != NULL)
1810 dc->search->download = NULL;
1813 if (dc->job_queue != NULL)
1815 GNUNET_FS_dequeue_ (dc->job_queue);
1816 dc->job_queue = NULL;
1818 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
1819 while (NULL != dc->child_head)
1820 GNUNET_FS_download_stop (dc->child_head,
1822 if (dc->parent != NULL)
1823 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1824 dc->parent->child_tail,
1826 if (dc->serialization != NULL)
1827 GNUNET_FS_remove_sync_file_ (dc->h,
1828 ( (dc->parent != NULL) || (dc->search != NULL) )
1829 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1830 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
1832 if ( (GNUNET_YES == have_children) &&
1833 (dc->parent == NULL) )
1834 GNUNET_FS_remove_sync_dir_ (dc->h,
1835 (dc->search != NULL)
1836 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1837 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1839 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1840 GNUNET_FS_download_make_status_ (&pi, dc);
1841 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1842 GNUNET_SCHEDULER_cancel (dc->h->sched,
1844 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1847 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1848 if (dc->filename != NULL)
1850 if ( (dc->completed != dc->length) &&
1851 (GNUNET_YES == do_delete) )
1853 if (0 != UNLINK (dc->filename))
1854 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1858 GNUNET_free (dc->filename);
1860 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1861 GNUNET_FS_uri_destroy (dc->uri);
1862 if (NULL != dc->temp_filename)
1864 if (0 != UNLINK (dc->temp_filename))
1865 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1868 GNUNET_free (dc->temp_filename);
1870 GNUNET_free_non_null (dc->serialization);
1874 /* end of fs_download.c */