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,
253 * We've found a matching block without downloading it.
254 * Encrypt it and pass it to our "receive" function as
255 * if we had received it from the network.
257 * @param dc download in question
258 * @param chk request this relates to
259 * @param sm request details
260 * @param block plaintext data matching request
261 * @param len number of bytes in block
262 * @param depth depth of the block
263 * @param do_store should we still store the block on disk?
264 * @return GNUNET_OK on success
267 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
268 const struct ContentHashKey *chk,
269 struct DownloadRequest *sm,
275 struct ProcessResultClosure prc;
277 struct GNUNET_CRYPTO_AesSessionKey sk;
278 struct GNUNET_CRYPTO_AesInitializationVector iv;
279 GNUNET_HashCode query;
281 GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
282 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len,
288 return GNUNET_SYSERR;
290 GNUNET_CRYPTO_hash (enc, len, &query);
291 if (0 != memcmp (&query,
293 sizeof (GNUNET_HashCode)))
296 return GNUNET_SYSERR;
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Matching block already present, no need for download!\n");
302 /* already got it! */
306 prc.type = (dc->treedepth == depth)
307 ? GNUNET_BLOCK_TYPE_DBLOCK
308 : GNUNET_BLOCK_TYPE_IBLOCK;
309 prc.query = chk->query;
310 prc.do_store = do_store;
311 process_result_with_request (&prc,
319 * Closure for match_full_data.
321 struct MatchDataContext
324 * CHK we are looking for.
326 const struct ContentHashKey *chk;
329 * Download we're processing.
331 struct GNUNET_FS_DownloadContext *dc;
336 struct DownloadRequest *sm;
339 * Overall offset in the file.
344 * Desired length of the block.
349 * Flag set to GNUNET_YES on success.
355 * Type of a function that libextractor calls for each
356 * meta data item found.
358 * @param cls closure (user-defined)
359 * @param plugin_name name of the plugin that produced this value;
360 * special values can be used (i.e. '<zlib>' for zlib being
361 * used in the main libextractor library and yielding
363 * @param type libextractor-type describing the meta data
364 * @param format basic format information about data
365 * @param data_mime_type mime-type of data (not of the original file);
366 * can be NULL (if mime-type is not known)
367 * @param data actual meta-data found
368 * @param data_len number of bytes in data
369 * @return 0 to continue extracting, 1 to abort
372 match_full_data (void *cls,
373 const char *plugin_name,
374 enum EXTRACTOR_MetaType type,
375 enum EXTRACTOR_MetaFormat format,
376 const char *data_mime_type,
380 struct MatchDataContext *mdc = cls;
383 if (type == EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
385 if ( (mdc->offset > data_len) ||
386 (mdc->offset + mdc->len > data_len) )
388 GNUNET_CRYPTO_hash (&data[mdc->offset],
391 if (0 != memcmp (&key,
393 sizeof (GNUNET_HashCode)))
400 encrypt_existing_match (mdc->dc,
411 mdc->done = GNUNET_YES;
419 * Schedule the download of the specified block in the tree.
421 * @param dc overall download this block belongs to
422 * @param chk content-hash-key of the block
423 * @param offset offset of the block in the file
424 * (for IBlocks, the offset is the lowest
425 * offset of any DBlock in the subtree under
427 * @param depth depth of the block, 0 is the root of the tree
430 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
431 const struct ContentHashKey *chk,
435 struct DownloadRequest *sm;
439 char block[DBLOCK_SIZE];
441 struct MatchDataContext mdc;
442 struct GNUNET_DISK_FileHandle *fh;
444 total = GNUNET_ntohll (dc->uri->data.chk.file_length);
445 len = GNUNET_FS_tree_calculate_block_size (total,
449 off = compute_disk_offset (total,
453 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
457 sm->is_pending = GNUNET_YES;
458 sm->next = dc->pending;
460 GNUNET_CONTAINER_multihashmap_put (dc->active,
463 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
464 if ( (dc->tried_full_data == GNUNET_NO) &&
472 mdc.done = GNUNET_NO;
473 GNUNET_CONTAINER_meta_data_iterate (dc->meta,
476 if (mdc.done == GNUNET_YES)
478 dc->tried_full_data = GNUNET_YES;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Scheduling download at offset %llu and depth %u for `%s'\n",
483 (unsigned long long) offset,
485 GNUNET_h2s (&chk->query));
488 if ( (dc->old_file_size > off) &&
489 (dc->filename != NULL) )
490 fh = GNUNET_DISK_file_open (dc->filename,
491 GNUNET_DISK_OPEN_READ,
492 GNUNET_DISK_PERM_NONE);
495 GNUNET_DISK_file_seek (fh,
497 GNUNET_DISK_SEEK_SET) ) &&
499 GNUNET_DISK_file_read (fh,
503 GNUNET_CRYPTO_hash (block, len, &key);
504 if ( (0 == memcmp (&key,
506 sizeof (GNUNET_HashCode))) &&
508 encrypt_existing_match (dc,
516 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
521 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
522 if (depth < dc->treedepth)
524 // FIXME: try if we could
525 // reconstitute this IBLOCK
526 // from the existing blocks on disk (can wait)
527 // (read block(s), encode, compare with
528 // query; if matches, simply return)
531 if ( (dc->th == NULL) &&
532 (dc->client != NULL) )
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Asking for transmission to FS service\n");
538 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
539 sizeof (struct SearchMessage),
540 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
542 &transmit_download_request,
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Transmission request not issued (%p %p)\n",
561 * Suggest a filename based on given metadata.
563 * @param md given meta data
564 * @return NULL if meta data is useless for suggesting a filename
567 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
569 static const char *mimeMap[][2] = {
570 {"application/bz2", ".bz2"},
571 {"application/gnunet-directory", ".gnd"},
572 {"application/java", ".class"},
573 {"application/msword", ".doc"},
574 {"application/ogg", ".ogg"},
575 {"application/pdf", ".pdf"},
576 {"application/pgp-keys", ".key"},
577 {"application/pgp-signature", ".pgp"},
578 {"application/postscript", ".ps"},
579 {"application/rar", ".rar"},
580 {"application/rtf", ".rtf"},
581 {"application/xml", ".xml"},
582 {"application/x-debian-package", ".deb"},
583 {"application/x-dvi", ".dvi"},
584 {"applixation/x-flac", ".flac"},
585 {"applixation/x-gzip", ".gz"},
586 {"application/x-java-archive", ".jar"},
587 {"application/x-java-vm", ".class"},
588 {"application/x-python-code", ".pyc"},
589 {"application/x-redhat-package-manager", ".rpm"},
590 {"application/x-rpm", ".rpm"},
591 {"application/x-tar", ".tar"},
592 {"application/x-tex-pk", ".pk"},
593 {"application/x-texinfo", ".texinfo"},
594 {"application/x-xcf", ".xcf"},
595 {"application/x-xfig", ".xfig"},
596 {"application/zip", ".zip"},
598 {"audio/midi", ".midi"},
599 {"audio/mpeg", ".mp3"},
600 {"audio/real", ".rm"},
601 {"audio/x-wav", ".wav"},
603 {"image/gif", ".gif"},
604 {"image/jpeg", ".jpg"},
605 {"image/pcx", ".pcx"},
606 {"image/png", ".png"},
607 {"image/tiff", ".tiff"},
608 {"image/x-ms-bmp", ".bmp"},
609 {"image/x-xpixmap", ".xpm"},
611 {"text/css", ".css"},
612 {"text/html", ".html"},
613 {"text/plain", ".txt"},
614 {"text/rtf", ".rtf"},
615 {"text/x-c++hdr", ".h++"},
616 {"text/x-c++src", ".c++"},
617 {"text/x-chdr", ".h"},
618 {"text/x-csrc", ".c"},
619 {"text/x-java", ".java"},
620 {"text/x-moc", ".moc"},
621 {"text/x-pascal", ".pas"},
622 {"text/x-perl", ".pl"},
623 {"text/x-python", ".py"},
624 {"text/x-tex", ".tex"},
626 {"video/avi", ".avi"},
627 {"video/mpeg", ".mpeg"},
628 {"video/quicktime", ".qt"},
629 {"video/real", ".rm"},
630 {"video/x-msvideo", ".avi"},
639 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
640 EXTRACTOR_METATYPE_FILENAME);
644 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
645 EXTRACTOR_METATYPE_MIMETYPE);
649 while ( (mimeMap[i][0] != NULL) &&
650 (0 != strcmp (mime, mimeMap[i][0])))
652 if (mimeMap[i][1] == NULL)
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
654 GNUNET_ERROR_TYPE_BULK,
655 _("Did not find mime type `%s' in extension list.\n"),
661 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
662 EXTRACTOR_METATYPE_TITLE,
663 EXTRACTOR_METATYPE_BOOK_TITLE,
664 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
665 EXTRACTOR_METATYPE_PACKAGE_NAME,
666 EXTRACTOR_METATYPE_URL,
667 EXTRACTOR_METATYPE_URI,
668 EXTRACTOR_METATYPE_DESCRIPTION,
669 EXTRACTOR_METATYPE_ISRC,
670 EXTRACTOR_METATYPE_JOURNAL_NAME,
671 EXTRACTOR_METATYPE_AUTHOR_NAME,
672 EXTRACTOR_METATYPE_SUBJECT,
673 EXTRACTOR_METATYPE_ALBUM,
674 EXTRACTOR_METATYPE_ARTIST,
675 EXTRACTOR_METATYPE_KEYWORDS,
676 EXTRACTOR_METATYPE_COMMENT,
677 EXTRACTOR_METATYPE_UNKNOWN,
679 if ( (base == NULL) &&
683 return GNUNET_strdup (ext);
686 GNUNET_asprintf (&ret,
696 * We've lost our connection with the FS service.
697 * Re-establish it and re-transmit all of our
700 * @param dc download context that is having trouble
703 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
707 * We found an entry in a directory. Check if the respective child
708 * already exists and if not create the respective child download.
710 * @param cls the parent download
711 * @param filename name of the file in the directory
712 * @param uri URI of the file (CHK or LOC)
713 * @param meta meta data of the file
714 * @param length number of bytes in data
715 * @param data contents of the file (or NULL if they were not inlined)
718 trigger_recursive_download (void *cls,
719 const char *filename,
720 const struct GNUNET_FS_Uri *uri,
721 const struct GNUNET_CONTAINER_MetaData *meta,
727 * We're done downloading a directory. Open the file and
728 * trigger all of the (remaining) child downloads.
730 * @param dc context of download that just completed
733 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
738 struct GNUNET_DISK_FileHandle *h;
739 struct GNUNET_DISK_MapHandle *m;
741 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
742 size = (size_t) size64;
743 if (size64 != (uint64_t) size)
745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
746 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
749 if (dc->filename != NULL)
751 h = GNUNET_DISK_file_open (dc->filename,
752 GNUNET_DISK_OPEN_READ,
753 GNUNET_DISK_PERM_NONE);
757 GNUNET_assert (dc->temp_filename != NULL);
758 h = GNUNET_DISK_file_open (dc->temp_filename,
759 GNUNET_DISK_OPEN_READ,
760 GNUNET_DISK_PERM_NONE);
764 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768 _("Directory too large for system address space\n"));
772 GNUNET_FS_directory_list_contents (size,
775 &trigger_recursive_download,
777 GNUNET_DISK_file_unmap (m);
779 GNUNET_DISK_file_close (h);
780 if (dc->filename == NULL)
782 if (0 != UNLINK (dc->temp_filename))
783 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
786 GNUNET_free (dc->temp_filename);
787 dc->temp_filename = NULL;
793 * Check if all child-downloads have completed and
794 * if so, signal completion (and possibly recurse to
798 check_completed (struct GNUNET_FS_DownloadContext *dc)
800 struct GNUNET_FS_ProgressInfo pi;
801 struct GNUNET_FS_DownloadContext *pos;
803 pos = dc->child_head;
806 if ( (pos->emsg == NULL) &&
807 (pos->completed < pos->length) )
808 return; /* not done yet */
809 if ( (pos->child_head != NULL) &&
810 (pos->has_finished != GNUNET_YES) )
811 return; /* not transitively done yet */
814 dc->has_finished = GNUNET_YES;
815 GNUNET_FS_download_sync_ (dc);
816 /* signal completion */
817 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
818 GNUNET_FS_download_make_status_ (&pi, dc);
819 if (dc->parent != NULL)
820 check_completed (dc->parent);
825 * We found an entry in a directory. Check if the respective child
826 * already exists and if not create the respective child download.
828 * @param cls the parent download
829 * @param filename name of the file in the directory
830 * @param uri URI of the file (CHK or LOC)
831 * @param meta meta data of the file
832 * @param length number of bytes in data
833 * @param data contents of the file (or NULL if they were not inlined)
836 trigger_recursive_download (void *cls,
837 const char *filename,
838 const struct GNUNET_FS_Uri *uri,
839 const struct GNUNET_CONTAINER_MetaData *meta,
843 struct GNUNET_FS_DownloadContext *dc = cls;
844 struct GNUNET_FS_DownloadContext *cpos;
845 struct GNUNET_DISK_FileHandle *fh;
847 const char *real_name;
856 return; /* entry for the directory itself */
857 cpos = dc->child_head;
860 if ( (GNUNET_FS_uri_test_equal (uri,
862 ( (filename != NULL) &&
863 (0 == strcmp (cpos->filename,
869 return; /* already exists */
871 if (NULL == filename)
873 fn = GNUNET_FS_meta_data_suggest_filename (meta);
876 us = GNUNET_FS_uri_to_string (uri);
877 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_PREFIX
878 GNUNET_FS_URI_CHK_INFIX)]);
881 else if (fn[0] == '.')
884 us = GNUNET_FS_uri_to_string (uri);
885 GNUNET_asprintf (&fn,
887 &us[strlen (GNUNET_FS_URI_PREFIX
888 GNUNET_FS_URI_CHK_INFIX)], ext);
892 /* change '\' to '/' (this should have happened
893 during insertion, but malicious peers may
894 not have done this) */
895 while (NULL != (pos = strstr (fn, "\\")))
897 /* remove '../' everywhere (again, well-behaved
898 peers don't do this, but don't trust that
899 we did not get something nasty) */
900 while (NULL != (pos = strstr (fn, "../")))
908 if (dc->filename == NULL)
914 dn = GNUNET_strdup (dc->filename);
915 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
917 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
918 GNUNET_FS_DIRECTORY_EXT)) );
919 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
921 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
922 GNUNET_FS_DIRECTORY_EXT)) )
923 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
924 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
925 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
927 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
928 GNUNET_FS_DIRECTORY_EXT)) ) )
930 GNUNET_asprintf (&full_name,
935 GNUNET_FS_DIRECTORY_EXT);
939 GNUNET_asprintf (&full_name,
947 if ( (full_name != NULL) &&
949 GNUNET_DISK_directory_create_for_file (full_name)) )
951 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
952 _("Failed to create directory for recursive download of `%s'\n"),
954 GNUNET_free (full_name);
955 GNUNET_free_non_null (fn);
960 if ( (data != NULL) &&
961 (GNUNET_FS_uri_chk_get_file_size (uri) == length) )
963 if (full_name == NULL)
965 temp_name = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
966 real_name = temp_name;
970 real_name = full_name;
972 /* write to disk, then trigger normal download which will instantly progress to completion */
973 fh = GNUNET_DISK_file_open (real_name,
974 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
975 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
978 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
981 GNUNET_free (full_name);
982 GNUNET_free_non_null (fn);
986 GNUNET_DISK_file_write (fh,
990 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
994 GNUNET_DISK_file_close (fh);
996 GNUNET_FS_download_start (dc->h,
999 full_name, temp_name,
1001 GNUNET_FS_uri_chk_get_file_size (uri),
1006 GNUNET_free_non_null (full_name);
1007 GNUNET_free_non_null (temp_name);
1008 GNUNET_free_non_null (fn);
1013 * Free entries in the map.
1015 * @param cls unused (NULL)
1017 * @param entry entry of type "struct DownloadRequest" which is freed
1021 free_entry (void *cls,
1022 const GNUNET_HashCode *key,
1025 GNUNET_free (entry);
1031 * Iterator over entries in the pending requests in the 'active' map for the
1032 * reply that we just got.
1034 * @param cls closure (our 'struct ProcessResultClosure')
1035 * @param key query for the given value / request
1036 * @param value value in the hash map (a 'struct DownloadRequest')
1037 * @return GNUNET_YES (we should continue to iterate); unless serious error
1040 process_result_with_request (void *cls,
1041 const GNUNET_HashCode * key,
1044 struct ProcessResultClosure *prc = cls;
1045 struct DownloadRequest *sm = value;
1046 struct DownloadRequest *ppos;
1047 struct DownloadRequest *pprev;
1048 struct GNUNET_DISK_FileHandle *fh;
1049 struct GNUNET_FS_DownloadContext *dc = prc->dc;
1050 struct GNUNET_CRYPTO_AesSessionKey skey;
1051 struct GNUNET_CRYPTO_AesInitializationVector iv;
1053 struct GNUNET_FS_ProgressInfo pi;
1058 struct ContentHashKey *chk;
1061 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
1065 if (prc->size != bs)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
1073 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
1076 GNUNET_assert (GNUNET_YES ==
1077 GNUNET_CONTAINER_multihashmap_remove (dc->active,
1080 /* if this request is on the pending list, remove it! */
1083 while (ppos != NULL)
1088 dc->pending = ppos->next;
1090 pprev->next = ppos->next;
1096 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
1097 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data,
1104 dc->emsg = GNUNET_strdup ("internal error decrypting content");
1107 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
1112 if ( ( GNUNET_YES == prc->do_store) &&
1113 ( (dc->filename != NULL) ||
1114 (is_recursive_download (dc)) ) &&
1115 ( (sm->depth == dc->treedepth) ||
1116 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
1118 fh = GNUNET_DISK_file_open (dc->filename != NULL
1120 : dc->temp_filename,
1121 GNUNET_DISK_OPEN_READWRITE |
1122 GNUNET_DISK_OPEN_CREATE,
1123 GNUNET_DISK_PERM_USER_READ |
1124 GNUNET_DISK_PERM_USER_WRITE |
1125 GNUNET_DISK_PERM_GROUP_READ |
1126 GNUNET_DISK_PERM_OTHER_READ);
1128 if ( (NULL == fh) &&
1129 (GNUNET_YES == prc->do_store) &&
1130 ( (dc->filename != NULL) ||
1131 (is_recursive_download (dc)) ) &&
1132 ( (sm->depth == dc->treedepth) ||
1133 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
1135 GNUNET_asprintf (&dc->emsg,
1136 _("Download failed: could not open file `%s': %s\n"),
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Saving decrypted block to disk at offset %llu\n",
1146 (unsigned long long) off);
1149 GNUNET_DISK_file_seek (fh,
1151 GNUNET_DISK_SEEK_SET) ) )
1153 GNUNET_asprintf (&dc->emsg,
1154 _("Failed to seek to offset %llu in file `%s': %s\n"),
1155 (unsigned long long) off,
1161 GNUNET_DISK_file_write (fh,
1165 GNUNET_asprintf (&dc->emsg,
1166 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1167 (unsigned int) prc->size,
1168 (unsigned long long) off,
1173 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1176 if (sm->depth == dc->treedepth)
1179 if (sm->offset < dc->offset)
1181 /* starting offset begins in the middle of pt,
1182 do not count first bytes as progress */
1183 GNUNET_assert (app > (dc->offset - sm->offset));
1184 app -= (dc->offset - sm->offset);
1186 if (sm->offset + prc->size > dc->offset + dc->length)
1188 /* end of block is after relevant range,
1189 do not count last bytes as progress */
1190 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
1191 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
1193 dc->completed += app;
1195 /* do recursive download if option is set and either meta data
1196 says it is a directory or if no meta data is given AND filename
1197 ends in '.gnd' (top-level case) */
1198 if (is_recursive_download (dc))
1199 GNUNET_FS_directory_list_contents (prc->size,
1202 &trigger_recursive_download,
1206 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1207 pi.value.download.specifics.progress.data = pt;
1208 pi.value.download.specifics.progress.offset = sm->offset;
1209 pi.value.download.specifics.progress.data_len = prc->size;
1210 pi.value.download.specifics.progress.depth = sm->depth;
1211 GNUNET_FS_download_make_status_ (&pi, dc);
1212 GNUNET_assert (dc->completed <= dc->length);
1213 if (dc->completed == dc->length)
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Download completed, truncating file to desired length %llu\n",
1218 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1220 /* truncate file to size (since we store IBlocks at the end) */
1221 if (dc->filename != NULL)
1223 if (0 != truncate (dc->filename,
1224 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1225 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1229 if (dc->job_queue != NULL)
1231 GNUNET_FS_dequeue_ (dc->job_queue);
1232 dc->job_queue = NULL;
1234 if (is_recursive_download (dc))
1235 full_recursive_download (dc);
1236 if (dc->child_head == NULL)
1238 /* signal completion */
1239 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
1240 GNUNET_FS_download_make_status_ (&pi, dc);
1241 if (dc->parent != NULL)
1242 check_completed (dc->parent);
1244 GNUNET_assert (sm->depth == dc->treedepth);
1246 if (sm->depth == dc->treedepth)
1248 GNUNET_FS_download_sync_ (dc);
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1256 (unsigned long long) sm->offset);
1258 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1259 chk = (struct ContentHashKey*) pt;
1260 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1262 off = compute_dblock_offset (sm->offset,
1266 if ( (off + DBLOCK_SIZE >= dc->offset) &&
1267 (off < dc->offset + dc->length) )
1268 schedule_block_download (dc,
1274 GNUNET_FS_download_sync_ (dc);
1279 GNUNET_DISK_file_close (fh);
1280 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1281 pi.value.download.specifics.error.message = dc->emsg;
1282 GNUNET_FS_download_make_status_ (&pi, dc);
1283 /* abort all pending requests */
1286 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1289 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1290 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1296 GNUNET_FS_download_sync_ (dc);
1302 * Process a download result.
1304 * @param dc our download context
1305 * @param type type of the result
1306 * @param data the (encrypted) response
1307 * @param size size of data
1310 process_result (struct GNUNET_FS_DownloadContext *dc,
1311 enum GNUNET_BLOCK_Type type,
1315 struct ProcessResultClosure prc;
1321 prc.do_store = GNUNET_YES;
1322 GNUNET_CRYPTO_hash (data, size, &prc.query);
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Received result for query `%s' from `%s'-service\n",
1326 GNUNET_h2s (&prc.query),
1329 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1331 &process_result_with_request,
1337 * Type of a function to call when we receive a message
1340 * @param cls closure
1341 * @param msg message received, NULL on timeout or fatal error
1344 receive_results (void *cls,
1345 const struct GNUNET_MessageHeader * msg)
1347 struct GNUNET_FS_DownloadContext *dc = cls;
1348 const struct PutMessage *cm;
1351 if ( (NULL == msg) ||
1352 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1353 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1355 GNUNET_break (msg == NULL);
1359 msize = ntohs(msg->size);
1360 cm = (const struct PutMessage*) msg;
1364 msize - sizeof (struct PutMessage));
1365 if (dc->client == NULL)
1366 return; /* fatal error */
1367 /* continue receiving */
1368 GNUNET_CLIENT_receive (dc->client,
1371 GNUNET_TIME_UNIT_FOREVER_REL);
1377 * We're ready to transmit a search request to the
1378 * file-sharing service. Do it. If there is
1379 * more than one request pending, try to send
1380 * multiple or request another transmission.
1382 * @param cls closure
1383 * @param size number of bytes available in buf
1384 * @param buf where the callee should write the message
1385 * @return number of bytes written to buf
1388 transmit_download_request (void *cls,
1392 struct GNUNET_FS_DownloadContext *dc = cls;
1394 struct SearchMessage *sm;
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "Transmitting download request failed, trying to reconnect\n");
1406 GNUNET_assert (size >= sizeof (struct SearchMessage));
1409 while ( (dc->pending != NULL) &&
1410 (size >= msize + sizeof (struct SearchMessage)) )
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Transmitting download request for `%s' to `%s'-service\n",
1415 GNUNET_h2s (&dc->pending->chk.query),
1418 memset (sm, 0, sizeof (struct SearchMessage));
1419 sm->header.size = htons (sizeof (struct SearchMessage));
1420 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1421 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1422 sm->options = htonl (1);
1424 sm->options = htonl (0);
1425 if (dc->pending->depth == dc->treedepth)
1426 sm->type = htonl (GNUNET_BLOCK_TYPE_DBLOCK);
1428 sm->type = htonl (GNUNET_BLOCK_TYPE_IBLOCK);
1429 sm->anonymity_level = htonl (dc->anonymity);
1430 sm->target = dc->target.hashPubKey;
1431 sm->query = dc->pending->chk.query;
1432 dc->pending->is_pending = GNUNET_NO;
1433 dc->pending = dc->pending->next;
1434 msize += sizeof (struct SearchMessage);
1437 if (dc->pending != NULL)
1438 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1439 sizeof (struct SearchMessage),
1440 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1442 &transmit_download_request,
1449 * Reconnect to the FS service and transmit our queries NOW.
1451 * @param cls our download context
1455 do_reconnect (void *cls,
1456 const struct GNUNET_SCHEDULER_TaskContext *tc)
1458 struct GNUNET_FS_DownloadContext *dc = cls;
1459 struct GNUNET_CLIENT_Connection *client;
1461 dc->task = GNUNET_SCHEDULER_NO_TASK;
1462 client = GNUNET_CLIENT_connect (dc->h->sched,
1467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1468 "Connecting to `%s'-service failed, will try again.\n",
1473 dc->client = client;
1474 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1475 sizeof (struct SearchMessage),
1476 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1478 &transmit_download_request,
1480 GNUNET_CLIENT_receive (client,
1483 GNUNET_TIME_UNIT_FOREVER_REL);
1488 * Add entries that are not yet pending back to the pending list.
1490 * @param cls our download context
1492 * @param entry entry of type "struct DownloadRequest"
1496 retry_entry (void *cls,
1497 const GNUNET_HashCode *key,
1500 struct GNUNET_FS_DownloadContext *dc = cls;
1501 struct DownloadRequest *dr = entry;
1503 if (! dr->is_pending)
1505 dr->next = dc->pending;
1506 dr->is_pending = GNUNET_YES;
1507 dc->pending = entry;
1514 * We've lost our connection with the FS service.
1515 * Re-establish it and re-transmit all of our
1518 * @param dc download context that is having trouble
1521 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1524 if (NULL != dc->client)
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "Moving all requests back to pending list\n");
1532 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1535 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1538 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Will try to reconnect in 1s\n");
1546 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1547 GNUNET_TIME_UNIT_SECONDS,
1555 * We're allowed to ask the FS service for our blocks. Start the download.
1557 * @param cls the 'struct GNUNET_FS_DownloadContext'
1558 * @param client handle to use for communcation with FS (we must destroy it!)
1561 activate_fs_download (void *cls,
1562 struct GNUNET_CLIENT_Connection *client)
1564 struct GNUNET_FS_DownloadContext *dc = cls;
1565 struct GNUNET_FS_ProgressInfo pi;
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Download activated\n");
1571 GNUNET_assert (NULL != client);
1572 GNUNET_assert (dc->client == NULL);
1573 GNUNET_assert (dc->th == NULL);
1574 dc->client = client;
1575 GNUNET_CLIENT_receive (client,
1578 GNUNET_TIME_UNIT_FOREVER_REL);
1579 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1580 GNUNET_FS_download_make_status_ (&pi, dc);
1581 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Asking for transmission to FS service\n");
1588 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1589 sizeof (struct SearchMessage),
1590 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1592 &transmit_download_request,
1594 GNUNET_assert (dc->th != NULL);
1599 * We must stop to ask the FS service for our blocks. Pause the download.
1601 * @param cls the 'struct GNUNET_FS_DownloadContext'
1604 deactivate_fs_download (void *cls)
1606 struct GNUNET_FS_DownloadContext *dc = cls;
1607 struct GNUNET_FS_ProgressInfo pi;
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Download deactivated\n");
1615 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1618 if (NULL != dc->client)
1620 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1623 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1624 GNUNET_FS_download_make_status_ (&pi, dc);
1629 * Create SUSPEND event for the given download operation
1630 * and then clean up our state (without stop signal).
1632 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1635 GNUNET_FS_download_signal_suspend_ (void *cls)
1637 struct GNUNET_FS_DownloadContext *dc = cls;
1638 struct GNUNET_FS_ProgressInfo pi;
1640 if (dc->top != NULL)
1641 GNUNET_FS_end_top (dc->h, dc->top);
1642 while (NULL != dc->child_head)
1643 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1644 if (dc->search != NULL)
1646 dc->search->download = NULL;
1649 if (dc->job_queue != NULL)
1651 GNUNET_FS_dequeue_ (dc->job_queue);
1652 dc->job_queue = NULL;
1654 if (dc->parent != NULL)
1655 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1656 dc->parent->child_tail,
1658 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1659 GNUNET_FS_download_make_status_ (&pi, dc);
1660 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1661 GNUNET_SCHEDULER_cancel (dc->h->sched,
1663 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1666 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1667 GNUNET_free_non_null (dc->filename);
1668 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1669 GNUNET_FS_uri_destroy (dc->uri);
1670 GNUNET_free_non_null (dc->temp_filename);
1671 GNUNET_free_non_null (dc->serialization);
1677 * Download parts of a file. Note that this will store
1678 * the blocks at the respective offset in the given file. Also, the
1679 * download is still using the blocking of the underlying FS
1680 * encoding. As a result, the download may *write* outside of the
1681 * given boundaries (if offset and length do not match the 32k FS
1682 * block boundaries). <p>
1684 * This function should be used to focus a download towards a
1685 * particular portion of the file (optimization), not to strictly
1686 * limit the download to exactly those bytes.
1688 * @param h handle to the file sharing subsystem
1689 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1690 * @param meta known metadata for the file (can be NULL)
1691 * @param filename where to store the file, maybe NULL (then no file is
1692 * created on disk and data must be grabbed from the callbacks)
1693 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1694 * can be NULL (in which case we will pick a name if needed); the temporary file
1695 * may already exist, in which case we will try to use the data that is there and
1696 * if it is not what is desired, will overwrite it
1697 * @param offset at what offset should we start the download (typically 0)
1698 * @param length how many bytes should be downloaded starting at offset
1699 * @param anonymity anonymity level to use for the download
1700 * @param options various options
1701 * @param cctx initial value for the client context for this download
1702 * @param parent parent download to associate this download with (use NULL
1703 * for top-level downloads; useful for manually-triggered recursive downloads)
1704 * @return context that can be used to control this download
1706 struct GNUNET_FS_DownloadContext *
1707 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1708 const struct GNUNET_FS_Uri *uri,
1709 const struct GNUNET_CONTAINER_MetaData *meta,
1710 const char *filename,
1711 const char *tempname,
1715 enum GNUNET_FS_DownloadOptions options,
1717 struct GNUNET_FS_DownloadContext *parent)
1719 struct GNUNET_FS_ProgressInfo pi;
1720 struct GNUNET_FS_DownloadContext *dc;
1722 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1723 if ( (offset + length < offset) ||
1724 (offset + length > uri->data.chk.file_length) )
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Starting download `%s' of %llu bytes\n",
1733 (unsigned long long) length);
1735 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1737 dc->parent = parent;
1740 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1744 dc->uri = GNUNET_FS_uri_dup (uri);
1745 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1746 dc->client_info = cctx;
1747 dc->start_time = GNUNET_TIME_absolute_get ();
1748 if (NULL != filename)
1750 dc->filename = GNUNET_strdup (filename);
1751 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1752 GNUNET_DISK_file_size (filename,
1756 if (GNUNET_FS_uri_test_loc (dc->uri))
1757 GNUNET_assert (GNUNET_OK ==
1758 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1760 dc->offset = offset;
1761 dc->length = length;
1762 dc->anonymity = anonymity;
1763 dc->options = options;
1764 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1765 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1766 if ( (filename == NULL) &&
1767 (is_recursive_download (dc) ) )
1769 if (tempname != NULL)
1770 dc->temp_filename = GNUNET_strdup (tempname);
1772 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 "Download tree has depth %u\n",
1782 dc->top = GNUNET_FS_make_top (dc->h,
1783 &GNUNET_FS_download_signal_suspend_,
1786 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1787 pi.value.download.specifics.start.meta = meta;
1788 GNUNET_FS_download_make_status_ (&pi, dc);
1789 schedule_block_download (dc,
1790 &dc->uri->data.chk.chk,
1792 1 /* 0 == CHK, 1 == top */);
1793 GNUNET_FS_download_sync_ (dc);
1794 GNUNET_FS_download_start_downloading_ (dc);
1800 * Download parts of a file based on a search result. The download
1801 * will be associated with the search result (and the association
1802 * will be preserved when serializing/deserializing the state).
1803 * If the search is stopped, the download will not be aborted but
1804 * be 'promoted' to a stand-alone download.
1806 * As with the other download function, this will store
1807 * the blocks at the respective offset in the given file. Also, the
1808 * download is still using the blocking of the underlying FS
1809 * encoding. As a result, the download may *write* outside of the
1810 * given boundaries (if offset and length do not match the 32k FS
1811 * block boundaries). <p>
1813 * The given range can be used to focus a download towards a
1814 * particular portion of the file (optimization), not to strictly
1815 * limit the download to exactly those bytes.
1817 * @param h handle to the file sharing subsystem
1818 * @param sr the search result to use for the download (determines uri and
1819 * meta data and associations)
1820 * @param filename where to store the file, maybe NULL (then no file is
1821 * created on disk and data must be grabbed from the callbacks)
1822 * @param tempname where to store temporary file data, not used if filename is non-NULL;
1823 * can be NULL (in which case we will pick a name if needed); the temporary file
1824 * may already exist, in which case we will try to use the data that is there and
1825 * if it is not what is desired, will overwrite it
1826 * @param offset at what offset should we start the download (typically 0)
1827 * @param length how many bytes should be downloaded starting at offset
1828 * @param anonymity anonymity level to use for the download
1829 * @param options various download options
1830 * @param cctx initial value for the client context for this download
1831 * @return context that can be used to control this download
1833 struct GNUNET_FS_DownloadContext *
1834 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
1835 struct GNUNET_FS_SearchResult *sr,
1836 const char *filename,
1837 const char *tempname,
1841 enum GNUNET_FS_DownloadOptions options,
1844 struct GNUNET_FS_ProgressInfo pi;
1845 struct GNUNET_FS_DownloadContext *dc;
1847 if ( (sr == NULL) ||
1848 (sr->download != NULL) )
1853 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri));
1854 if ( (offset + length < offset) ||
1855 (offset + length > sr->uri->data.chk.file_length) )
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Starting download `%s' of %llu bytes\n",
1864 (unsigned long long) length);
1866 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1870 if (sr->probe_ctx != NULL)
1872 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
1873 sr->probe_ctx = NULL;
1875 dc->uri = GNUNET_FS_uri_dup (sr->uri);
1876 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
1877 dc->client_info = cctx;
1878 dc->start_time = GNUNET_TIME_absolute_get ();
1879 if (NULL != filename)
1881 dc->filename = GNUNET_strdup (filename);
1882 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1883 GNUNET_DISK_file_size (filename,
1887 if (GNUNET_FS_uri_test_loc (dc->uri))
1888 GNUNET_assert (GNUNET_OK ==
1889 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1891 dc->offset = offset;
1892 dc->length = length;
1893 dc->anonymity = anonymity;
1894 dc->options = options;
1895 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1896 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1897 if ( (filename == NULL) &&
1898 (is_recursive_download (dc) ) )
1900 if (tempname != NULL)
1901 dc->temp_filename = GNUNET_strdup (tempname);
1903 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "Download tree has depth %u\n",
1911 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1912 pi.value.download.specifics.start.meta = dc->meta;
1913 GNUNET_FS_download_make_status_ (&pi, dc);
1914 schedule_block_download (dc,
1915 &dc->uri->data.chk.chk,
1917 1 /* 0 == CHK, 1 == top */);
1918 GNUNET_FS_download_sync_ (dc);
1919 GNUNET_FS_download_start_downloading_ (dc);
1925 * Start the downloading process (by entering the queue).
1927 * @param dc our download context
1930 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
1932 GNUNET_assert (dc->job_queue == NULL);
1933 dc->job_queue = GNUNET_FS_queue_ (dc->h,
1934 &activate_fs_download,
1935 &deactivate_fs_download,
1937 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
1942 * Stop a download (aborts if download is incomplete).
1944 * @param dc handle for the download
1945 * @param do_delete delete files of incomplete downloads
1948 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1951 struct GNUNET_FS_ProgressInfo pi;
1954 if (dc->top != NULL)
1955 GNUNET_FS_end_top (dc->h, dc->top);
1956 if (dc->search != NULL)
1958 dc->search->download = NULL;
1961 if (dc->job_queue != NULL)
1963 GNUNET_FS_dequeue_ (dc->job_queue);
1964 dc->job_queue = NULL;
1966 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
1967 while (NULL != dc->child_head)
1968 GNUNET_FS_download_stop (dc->child_head,
1970 if (dc->parent != NULL)
1971 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1972 dc->parent->child_tail,
1974 if (dc->serialization != NULL)
1975 GNUNET_FS_remove_sync_file_ (dc->h,
1976 ( (dc->parent != NULL) || (dc->search != NULL) )
1977 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1978 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
1980 if ( (GNUNET_YES == have_children) &&
1981 (dc->parent == NULL) )
1982 GNUNET_FS_remove_sync_dir_ (dc->h,
1983 (dc->search != NULL)
1984 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1985 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1987 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1988 GNUNET_FS_download_make_status_ (&pi, dc);
1989 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1990 GNUNET_SCHEDULER_cancel (dc->h->sched,
1992 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1995 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1996 if (dc->filename != NULL)
1998 if ( (dc->completed != dc->length) &&
1999 (GNUNET_YES == do_delete) )
2001 if (0 != UNLINK (dc->filename))
2002 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2006 GNUNET_free (dc->filename);
2008 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2009 GNUNET_FS_uri_destroy (dc->uri);
2010 if (NULL != dc->temp_filename)
2012 if (0 != UNLINK (dc->temp_filename))
2013 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2016 GNUNET_free (dc->temp_filename);
2018 GNUNET_free_non_null (dc->serialization);
2022 /* end of fs_download.c */