2 This file is part of GNUnet.
3 (C) 2001-2011 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?
29 #include "gnunet_constants.h"
30 #include "gnunet_fs_service.h"
34 #define DEBUG_DOWNLOAD GNUNET_NO
37 * Determine if the given download (options and meta data) should cause
38 * use to try to do a recursive download.
41 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
43 return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
44 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
45 ( (dc->meta == NULL) &&
46 ( (NULL == dc->filename) ||
47 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
49 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
50 GNUNET_FS_DIRECTORY_EXT)) ) ) ) );
55 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
56 * only have to truncate the file once we're done).
58 * Given the offset of a block (with respect to the DBLOCKS) and its
59 * depth, return the offset where we would store this block in the
62 * @param fsize overall file size
63 * @param off offset of the block in the file
64 * @param depth depth of the block in the tree, 0 for DBLOCK
65 * @return off for DBLOCKS (depth == treedepth),
66 * otherwise an offset past the end
67 * of the file that does not overlap
68 * with the range for any other block
71 compute_disk_offset (uint64_t fsize,
76 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
77 uint64_t loff; /* where do IBlocks for depth "i" start? */
78 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
82 /* first IBlocks start at the end of file, rounded up
83 to full DBLOCK_SIZE */
84 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
85 lsize = ( (fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
86 GNUNET_assert (0 == (off % DBLOCK_SIZE));
87 ioff = (off / DBLOCK_SIZE);
91 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
92 GNUNET_assert (lsize > 0);
93 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
94 ioff /= CHK_PER_INODE;
96 return loff + ioff * sizeof (struct ContentHashKey);
101 * Given a block at the given offset and depth, calculate the offset
102 * for the CHK at the given index.
104 * @param offset the offset of the first
105 * DBLOCK in the subtree of the
107 * @param depth the depth of the IBLOCK in the tree, 0 for DBLOCK
108 * @param k which CHK in the IBLOCK are we
110 * @return offset if k=0, otherwise an appropriately
111 * larger value (i.e., if depth = 1,
112 * the returned value should be offset+k*DBLOCK_SIZE)
115 compute_dblock_offset (uint64_t offset,
120 uint64_t lsize; /* what is the size of the sum of all DBlocks
121 that a CHK at depth i corresponds to? */
126 for (i=1;i<depth;i++)
127 lsize *= CHK_PER_INODE;
128 return offset + k * lsize;
133 * Fill in all of the generic fields for a download event and call the
136 * @param pi structure to fill in
137 * @param dc overall download context
140 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
141 struct GNUNET_FS_DownloadContext *dc)
143 pi->value.download.dc = dc;
144 pi->value.download.cctx
146 pi->value.download.pctx
147 = (dc->parent == NULL) ? NULL : dc->parent->client_info;
148 pi->value.download.sctx
149 = (dc->search == NULL) ? NULL : dc->search->client_info;
150 pi->value.download.uri
152 pi->value.download.filename
154 pi->value.download.size
156 pi->value.download.duration
157 = GNUNET_TIME_absolute_get_duration (dc->start_time);
158 pi->value.download.completed
160 pi->value.download.anonymity
162 pi->value.download.eta
163 = GNUNET_TIME_calculate_eta (dc->start_time,
166 pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
167 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
168 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
171 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL,
177 * We're ready to transmit a search request to the
178 * file-sharing service. Do it. If there is
179 * more than one request pending, try to send
180 * multiple or request another transmission.
183 * @param size number of bytes available in buf
184 * @param buf where the callee should write the message
185 * @return number of bytes written to buf
188 transmit_download_request (void *cls,
194 * Closure for iterator processing results.
196 struct ProcessResultClosure
202 GNUNET_HashCode query;
205 * Data found in P2P network.
210 * Our download context.
212 struct GNUNET_FS_DownloadContext *dc;
215 * Number of bytes in data.
222 enum GNUNET_BLOCK_Type type;
225 * Flag to indicate if this block should be stored on disk.
233 * Iterator over entries in the pending requests in the 'active' map for the
234 * reply that we just got.
236 * @param cls closure (our 'struct ProcessResultClosure')
237 * @param key query for the given value / request
238 * @param value value in the hash map (a 'struct DownloadRequest')
239 * @return GNUNET_YES (we should continue to iterate); unless serious error
242 process_result_with_request (void *cls,
243 const GNUNET_HashCode * key,
248 * We've found a matching block without downloading it.
249 * Encrypt it and pass it to our "receive" function as
250 * if we had received it from the network.
252 * @param dc download in question
253 * @param chk request this relates to
254 * @param dr request details
255 * @param block plaintext data matching request
256 * @param len number of bytes in block
257 * @param do_store should we still store the block on disk?
258 * @return GNUNET_OK on success
261 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
262 const struct ContentHashKey *chk,
263 struct DownloadRequest *dr,
268 struct ProcessResultClosure prc;
270 struct GNUNET_CRYPTO_AesSessionKey sk;
271 struct GNUNET_CRYPTO_AesInitializationVector iv;
272 GNUNET_HashCode query;
274 GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
275 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len,
281 return GNUNET_SYSERR;
283 GNUNET_CRYPTO_hash (enc, len, &query);
284 if (0 != memcmp (&query,
286 sizeof (GNUNET_HashCode)))
289 return GNUNET_SYSERR;
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Matching block for `%s' at offset %llu already present, no need for download!\n",
295 (unsigned long long) dr->offset);
297 /* already got it! */
301 prc.type = (0 == dr->depth)
302 ? GNUNET_BLOCK_TYPE_FS_DBLOCK
303 : GNUNET_BLOCK_TYPE_FS_IBLOCK;
304 prc.query = chk->query;
305 prc.do_store = do_store;
306 process_result_with_request (&prc,
314 * We've lost our connection with the FS service.
315 * Re-establish it and re-transmit all of our
318 * @param dc download context that is having trouble
321 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
325 * We found an entry in a directory. Check if the respective child
326 * already exists and if not create the respective child download.
328 * @param cls the parent download
329 * @param filename name of the file in the directory
330 * @param uri URI of the file (CHK or LOC)
331 * @param meta meta data of the file
332 * @param length number of bytes in data
333 * @param data contents of the file (or NULL if they were not inlined)
336 trigger_recursive_download (void *cls,
337 const char *filename,
338 const struct GNUNET_FS_Uri *uri,
339 const struct GNUNET_CONTAINER_MetaData *meta,
345 * We're done downloading a directory. Open the file and
346 * trigger all of the (remaining) child downloads.
348 * @param dc context of download that just completed
351 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
356 struct GNUNET_DISK_FileHandle *h;
357 struct GNUNET_DISK_MapHandle *m;
359 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
360 size = (size_t) size64;
361 if (size64 != (uint64_t) size)
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
367 if (dc->filename != NULL)
369 h = GNUNET_DISK_file_open (dc->filename,
370 GNUNET_DISK_OPEN_READ,
371 GNUNET_DISK_PERM_NONE);
375 GNUNET_assert (dc->temp_filename != NULL);
376 h = GNUNET_DISK_file_open (dc->temp_filename,
377 GNUNET_DISK_OPEN_READ,
378 GNUNET_DISK_PERM_NONE);
382 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386 _("Directory too large for system address space\n"));
390 GNUNET_FS_directory_list_contents (size,
393 &trigger_recursive_download,
395 GNUNET_DISK_file_unmap (m);
397 GNUNET_DISK_file_close (h);
398 if (dc->filename == NULL)
400 if (0 != UNLINK (dc->temp_filename))
401 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
404 GNUNET_free (dc->temp_filename);
405 dc->temp_filename = NULL;
411 * Check if all child-downloads have completed (or trigger them if
412 * necessary) and once we're completely done, signal completion (and
413 * possibly recurse to parent). This function MUST be called when the
414 * download of a file itself is done or when the download of a file is
415 * done and then later a direct child download has completed (and
416 * hence this download may complete itself).
418 * @param dc download to check for completion of children
421 check_completed (struct GNUNET_FS_DownloadContext *dc)
423 struct GNUNET_FS_ProgressInfo pi;
424 struct GNUNET_FS_DownloadContext *pos;
426 /* first, check if we need to download children */
427 if ( (dc->child_head == NULL) &&
428 (is_recursive_download (dc)) )
429 full_recursive_download (dc);
430 /* then, check if children are done already */
431 pos = dc->child_head;
434 if ( (pos->emsg == NULL) &&
435 (pos->completed < pos->length) )
436 return; /* not done yet */
437 if ( (pos->child_head != NULL) &&
438 (pos->has_finished != GNUNET_YES) )
439 return; /* not transitively done yet */
442 /* All of our children are done, so mark this download done */
443 dc->has_finished = GNUNET_YES;
444 if (dc->job_queue != NULL)
446 GNUNET_FS_dequeue_ (dc->job_queue);
447 dc->job_queue = NULL;
449 GNUNET_FS_download_sync_ (dc);
451 /* signal completion */
452 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
453 GNUNET_FS_download_make_status_ (&pi, dc);
455 /* let parent know */
456 if (dc->parent != NULL)
457 check_completed (dc->parent);
462 * We got a block of plaintext data (from the meta data).
463 * Try it for upward reconstruction of the data. On success,
464 * the top-level block will move to state BRS_DOWNLOAD_UP.
466 * @param dr one of our request entries
467 * @param data plaintext data, starting from the beginning of the file
468 * @param data_len number of bytes in data
471 try_match_block (struct GNUNET_FS_DownloadContext *dc,
472 struct DownloadRequest *dr,
476 struct GNUNET_FS_ProgressInfo pi;
478 char enc[DBLOCK_SIZE];
479 struct ContentHashKey chks[CHK_PER_INODE];
480 struct ContentHashKey in_chk;
481 struct GNUNET_CRYPTO_AesSessionKey sk;
482 struct GNUNET_CRYPTO_AesInitializationVector iv;
484 struct DownloadRequest *drc;
485 struct GNUNET_DISK_FileHandle *fh;
492 odata_len = data_len;
493 if (BRS_DOWNLOAD_UP == dr->state)
497 complete = GNUNET_YES;
498 for (i=0;i<dr->num_children;i++)
500 drc = dr->children[i];
504 if (drc->state != BRS_RECONSTRUCT_META_UP)
505 complete = GNUNET_NO;
509 if (GNUNET_YES != complete)
511 data = (const char*) chks;
512 dlen = dr->num_children * sizeof (struct ContentHashKey);
516 if (dr->offset > data_len)
518 dlen = GNUNET_MIN (data_len - dr->offset,
521 GNUNET_CRYPTO_hash (&data[dr->offset],
524 GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
525 if (-1 == GNUNET_CRYPTO_aes_encrypt (&data[dr->offset], dlen,
533 GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
538 dr->state = BRS_RECONSTRUCT_META_UP;
541 if (0 != memcmp (&in_chk,
543 sizeof (struct ContentHashKey)))
545 /* other peer provided bogus meta data */
549 /* write block to disk */
550 fn = dc->filename != NULL
553 fh = GNUNET_DISK_file_open (fn,
554 GNUNET_DISK_OPEN_READWRITE |
555 GNUNET_DISK_OPEN_CREATE |
556 GNUNET_DISK_OPEN_TRUNCATE,
557 GNUNET_DISK_PERM_USER_READ |
558 GNUNET_DISK_PERM_USER_WRITE |
559 GNUNET_DISK_PERM_GROUP_READ |
560 GNUNET_DISK_PERM_OTHER_READ);
563 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
566 GNUNET_asprintf (&dc->emsg,
567 _("Failed to open file `%s' for writing"),
569 GNUNET_DISK_file_close (fh);
570 dr->state = BRS_ERROR;
571 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
572 pi.value.download.specifics.error.message = dc->emsg;
573 GNUNET_FS_download_make_status_ (&pi, dc);
577 GNUNET_DISK_file_write (fh,
581 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
584 GNUNET_asprintf (&dc->emsg,
585 _("Failed to open file `%s' for writing"),
587 GNUNET_DISK_file_close (fh);
588 dr->state = BRS_ERROR;
589 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
590 pi.value.download.specifics.error.message = dc->emsg;
591 GNUNET_FS_download_make_status_ (&pi, dc);
594 GNUNET_DISK_file_close (fh);
596 dr->state = BRS_DOWNLOAD_UP;
597 dc->completed = dc->length;
598 GNUNET_FS_download_sync_ (dc);
599 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
600 pi.value.download.specifics.progress.data = data;
601 pi.value.download.specifics.progress.offset = 0;
602 pi.value.download.specifics.progress.data_len = dlen;
603 pi.value.download.specifics.progress.depth = 0;
604 GNUNET_FS_download_make_status_ (&pi, dc);
605 if (0 != truncate (dc->filename,
606 GNUNET_ntohll (dc->uri->data.chk.file_length)))
607 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
610 check_completed (dc);
613 /* how did we get here? */
621 * Type of a function that libextractor calls for each
622 * meta data item found. If we find full data meta data,
623 * call 'try_match_block' on it.
625 * @param cls our 'struct GNUNET_FS_DownloadContext*'
626 * @param plugin_name name of the plugin that produced this value;
627 * special values can be used (i.e. '<zlib>' for zlib being
628 * used in the main libextractor library and yielding
630 * @param type libextractor-type describing the meta data
631 * @param format basic format information about data
632 * @param data_mime_type mime-type of data (not of the original file);
633 * can be NULL (if mime-type is not known)
634 * @param data actual meta-data found
635 * @param data_len number of bytes in data
636 * @return 0 to continue extracting, 1 to abort
639 match_full_data (void *cls,
640 const char *plugin_name,
641 enum EXTRACTOR_MetaType type,
642 enum EXTRACTOR_MetaFormat format,
643 const char *data_mime_type,
647 struct GNUNET_FS_DownloadContext *dc = cls;
649 if (type != EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Found %u bytes of FD!\n",
654 (unsigned int) data_len);
656 if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
659 return 1; /* bogus meta data */
670 * Set the state of the given download request to
671 * BRS_DOWNLOAD_UP and propagate it up the tree.
673 * @param dr download request that is done
676 propagate_up (struct DownloadRequest *dr)
682 dr->state = BRS_DOWNLOAD_UP;
686 for (i=0;i<dr->num_children;i++)
687 if (dr->children[i]->state != BRS_DOWNLOAD_UP)
690 while (i == dr->num_children);
695 * Try top-down reconstruction. Before, the given request node
696 * must have the state BRS_CHK_SET. Afterwards, more nodes may
697 * have that state or advanced to BRS_DOWNLOAD_DOWN or even
698 * BRS_DOWNLOAD_UP. It is also possible to get BRS_ERROR on the
701 * @param dc overall download this block belongs to
702 * @param dr block to reconstruct
705 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
706 struct DownloadRequest *dr)
709 char block[DBLOCK_SIZE];
714 unsigned int chk_off;
715 struct DownloadRequest *drc;
716 uint64_t child_block_size;
717 const struct ContentHashKey *chks;
720 GNUNET_assert (dc->rfh != NULL);
721 GNUNET_assert (dr->state == BRS_CHK_SET);
722 total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
723 GNUNET_assert (dr->depth < dc->treedepth);
724 len = GNUNET_FS_tree_calculate_block_size (total,
727 GNUNET_assert (len <= DBLOCK_SIZE);
728 off = compute_disk_offset (total,
731 if (dc->old_file_size < off + len)
732 return; /* failure */
734 GNUNET_DISK_file_seek (dc->rfh,
736 GNUNET_DISK_SEEK_SET) )
738 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
741 return; /* failure */
744 GNUNET_DISK_file_read (dc->rfh,
748 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
751 return; /* failure */
753 GNUNET_CRYPTO_hash (block, len, &key);
754 if (0 != memcmp (&key,
756 sizeof (GNUNET_HashCode)))
757 return; /* mismatch */
759 encrypt_existing_match (dc,
766 /* hash matches but encrypted block does not, really bad */
767 dr->state = BRS_ERROR;
769 while (dr->parent != NULL)
772 dr->state = BRS_ERROR;
777 dr->state = BRS_DOWNLOAD_DOWN;
779 /* set CHKs for children */
780 up_done = GNUNET_YES;
781 chks = (const struct ContentHashKey*) block;
782 for (i=0;i<dr->num_children;i++)
784 drc = dr->children[i];
785 GNUNET_assert (drc->offset >= dr->offset);
786 child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
787 GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);
788 chk_off = (drc->offset - dr->offset) / child_block_size;
789 GNUNET_assert (drc->state == BRS_INIT);
790 drc->state = BRS_CHK_SET;
791 drc->chk = chks[chk_off];
792 try_top_down_reconstruction (dc, drc);
793 if (drc->state != BRS_DOWNLOAD_UP)
794 up_done = GNUNET_NO; /* children not all done */
796 if (up_done == GNUNET_YES)
797 propagate_up (dr); /* children all done (or no children...) */
802 * Schedule the download of the specified block in the tree.
804 * @param dc overall download this block belongs to
805 * @param chk content-hash-key of the block
806 * @param offset offset of the block in the file
807 * (for IBlocks, the offset is the lowest
808 * offset of any DBlock in the subtree under
810 * @param depth depth of the block, 0 is the root of the tree
813 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
814 struct DownloadRequest *dr)
823 case BRS_RECONSTRUCT_DOWN:
826 case BRS_RECONSTRUCT_META_UP:
829 case BRS_RECONSTRUCT_UP:
833 /* normal case, start download */
835 case BRS_DOWNLOAD_DOWN:
836 for (i=0;i<dr->num_children;i++)
837 schedule_block_download (dc, dr->children[i]);
839 case BRS_DOWNLOAD_UP:
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Scheduling download at offset %llu and depth %u for `%s'\n",
849 (unsigned long long) dr->offset,
851 GNUNET_h2s (&dr->chk.query));
853 GNUNET_CONTAINER_DLL_insert (dc->pending_head,
856 dr->is_pending = GNUNET_YES;
857 GNUNET_CONTAINER_multihashmap_put (dc->active,
860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
861 if (dc->client == NULL)
862 return; /* download not active */
864 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
865 sizeof (struct SearchMessage),
866 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
868 &transmit_download_request,
873 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
876 * We found an entry in a directory. Check if the respective child
877 * already exists and if not create the respective child download.
879 * @param cls the parent download
880 * @param filename name of the file in the directory
881 * @param uri URI of the file (CHK or LOC)
882 * @param meta meta data of the file
883 * @param length number of bytes in data
884 * @param data contents of the file (or NULL if they were not inlined)
887 trigger_recursive_download (void *cls,
888 const char *filename,
889 const struct GNUNET_FS_Uri *uri,
890 const struct GNUNET_CONTAINER_MetaData *meta,
894 struct GNUNET_FS_DownloadContext *dc = cls;
895 struct GNUNET_FS_DownloadContext *cpos;
906 return; /* entry for the directory itself */
907 cpos = dc->child_head;
910 if ( (GNUNET_FS_uri_test_equal (uri,
912 ( (filename != NULL) &&
913 (0 == strcmp (cpos->filename,
919 return; /* already exists */
921 if (NULL == filename)
923 fn = GNUNET_FS_meta_data_suggest_filename (meta);
926 us = GNUNET_FS_uri_to_string (uri);
927 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_CHK_PREFIX)]);
930 else if (fn[0] == '.')
933 us = GNUNET_FS_uri_to_string (uri);
934 GNUNET_asprintf (&fn,
936 &us[strlen (GNUNET_FS_URI_CHK_PREFIX)], ext);
940 /* change '\' to '/' (this should have happened
941 during insertion, but malicious peers may
942 not have done this) */
943 while (NULL != (pos = strstr (fn, "\\")))
945 /* remove '../' everywhere (again, well-behaved
946 peers don't do this, but don't trust that
947 we did not get something nasty) */
948 while (NULL != (pos = strstr (fn, "../")))
956 if (dc->filename == NULL)
962 dn = GNUNET_strdup (dc->filename);
963 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
965 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
966 GNUNET_FS_DIRECTORY_EXT)) );
967 sfn = GNUNET_strdup (filename);
968 while ( (strlen (sfn) > 0) &&
969 (filename[strlen(sfn)-1] == '/') )
970 sfn[strlen(sfn)-1] = '\0';
971 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
973 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
974 GNUNET_FS_DIRECTORY_EXT)) )
975 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
976 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
977 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
979 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
980 GNUNET_FS_DIRECTORY_EXT)) ) )
982 GNUNET_asprintf (&full_name,
987 GNUNET_FS_DIRECTORY_EXT);
991 GNUNET_asprintf (&full_name,
999 if ( (full_name != NULL) &&
1001 GNUNET_DISK_directory_create_for_file (full_name)) )
1003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1004 _("Failed to create directory for recursive download of `%s'\n"),
1006 GNUNET_free (full_name);
1007 GNUNET_free_non_null (fn);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "Triggering recursive download of size %llu with %u bytes MD\n",
1015 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
1016 (unsigned int) GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
1018 GNUNET_FS_download_start (dc->h,
1021 full_name, temp_name,
1023 GNUNET_FS_uri_chk_get_file_size (uri),
1028 GNUNET_free_non_null (full_name);
1029 GNUNET_free_non_null (temp_name);
1030 GNUNET_free_non_null (fn);
1035 * (recursively) free download request structure
1037 * @param dr request to free
1040 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
1046 for (i=0;i<dr->num_children;i++)
1047 GNUNET_FS_free_download_request_ (dr->children[i]);
1048 GNUNET_free_non_null (dr->children);
1054 * Iterator over entries in the pending requests in the 'active' map for the
1055 * reply that we just got.
1057 * @param cls closure (our 'struct ProcessResultClosure')
1058 * @param key query for the given value / request
1059 * @param value value in the hash map (a 'struct DownloadRequest')
1060 * @return GNUNET_YES (we should continue to iterate); unless serious error
1063 process_result_with_request (void *cls,
1064 const GNUNET_HashCode *key,
1067 struct ProcessResultClosure *prc = cls;
1068 struct DownloadRequest *dr = value;
1069 struct GNUNET_FS_DownloadContext *dc = prc->dc;
1070 struct DownloadRequest *drc;
1071 struct GNUNET_DISK_FileHandle *fh = NULL;
1072 struct GNUNET_CRYPTO_AesSessionKey skey;
1073 struct GNUNET_CRYPTO_AesInitializationVector iv;
1075 struct GNUNET_FS_ProgressInfo pi;
1080 struct ContentHashKey *chk;
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Received block `%s' matching pending request at depth %u and offset %llu/%llu\n",
1087 (unsigned long long) dr->offset,
1088 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1091 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
1094 if (prc->size != bs)
1096 GNUNET_asprintf (&dc->emsg,
1097 _("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)\n"),
1100 (unsigned long long) dr->offset,
1101 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length),
1103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1106 while (dr->parent != NULL)
1108 dr->state = BRS_ERROR;
1111 dr->state = BRS_ERROR;
1115 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active,
1118 if (GNUNET_YES == dr->is_pending)
1120 GNUNET_CONTAINER_DLL_remove (dc->pending_head,
1123 dr->is_pending = GNUNET_NO;
1127 GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key, &skey, &iv);
1128 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data,
1135 dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
1138 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
1142 if ( ( GNUNET_YES == prc->do_store) &&
1143 ( (dc->filename != NULL) ||
1144 (is_recursive_download (dc)) ) &&
1145 ( (dr->depth == dc->treedepth) ||
1146 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
1148 fh = GNUNET_DISK_file_open (dc->filename != NULL
1150 : dc->temp_filename,
1151 GNUNET_DISK_OPEN_READWRITE |
1152 GNUNET_DISK_OPEN_CREATE,
1153 GNUNET_DISK_PERM_USER_READ |
1154 GNUNET_DISK_PERM_USER_WRITE |
1155 GNUNET_DISK_PERM_GROUP_READ |
1156 GNUNET_DISK_PERM_OTHER_READ);
1159 GNUNET_asprintf (&dc->emsg,
1160 _("Download failed: could not open file `%s': %s\n"),
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Saving decrypted block to disk at offset %llu\n",
1168 (unsigned long long) off);
1171 GNUNET_DISK_file_seek (fh,
1173 GNUNET_DISK_SEEK_SET) ) )
1175 GNUNET_asprintf (&dc->emsg,
1176 _("Failed to seek to offset %llu in file `%s': %s\n"),
1177 (unsigned long long) off,
1183 GNUNET_DISK_file_write (fh,
1187 GNUNET_asprintf (&dc->emsg,
1188 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1189 (unsigned int) prc->size,
1190 (unsigned long long) off,
1195 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1201 /* DBLOCK, update progress and try recursion if applicable */
1203 if (dr->offset < dc->offset)
1205 /* starting offset begins in the middle of pt,
1206 do not count first bytes as progress */
1207 GNUNET_assert (app > (dc->offset - dr->offset));
1208 app -= (dc->offset - dr->offset);
1210 if (dr->offset + prc->size > dc->offset + dc->length)
1212 /* end of block is after relevant range,
1213 do not count last bytes as progress */
1214 GNUNET_assert (app > (dr->offset + prc->size) - (dc->offset + dc->length));
1215 app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1217 dc->completed += app;
1219 /* do recursive download if option is set and either meta data
1220 says it is a directory or if no meta data is given AND filename
1221 ends in '.gnd' (top-level case) */
1222 if (is_recursive_download (dc))
1223 GNUNET_FS_directory_list_contents (prc->size,
1226 &trigger_recursive_download,
1230 dr->state = BRS_DOWNLOAD_DOWN;
1231 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1232 pi.value.download.specifics.progress.data = pt;
1233 pi.value.download.specifics.progress.offset = dr->offset;
1234 pi.value.download.specifics.progress.data_len = prc->size;
1235 pi.value.download.specifics.progress.depth = dr->depth;
1236 GNUNET_FS_download_make_status_ (&pi, dc);
1237 GNUNET_assert (dc->completed <= dc->length);
1241 if (dc->completed == dc->length)
1243 /* download completed, signal */
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Download completed, truncating file to desired length %llu\n",
1247 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
1249 /* truncate file to size (since we store IBlocks at the end) */
1250 if (dc->filename != NULL)
1252 if (0 != truncate (dc->filename,
1253 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1254 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1258 GNUNET_assert (dr->depth == 0);
1259 check_completed (dc);
1263 /* bottom of the tree, no child downloads possible, just sync */
1264 GNUNET_FS_download_sync_ (dc);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1272 (unsigned long long) dr->offset);
1274 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
1275 chk = (struct ContentHashKey*) pt;
1276 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
1278 off = compute_dblock_offset (dr->offset,
1281 drc = dr->children[i];
1286 drc->state = BRS_CHK_SET;
1287 schedule_block_download (dc, drc);
1289 case BRS_RECONSTRUCT_DOWN:
1292 case BRS_RECONSTRUCT_META_UP:
1295 case BRS_RECONSTRUCT_UP:
1301 case BRS_DOWNLOAD_DOWN:
1304 case BRS_DOWNLOAD_UP:
1315 GNUNET_FS_download_sync_ (dc);
1320 GNUNET_DISK_file_close (fh);
1321 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1322 pi.value.download.specifics.error.message = dc->emsg;
1323 GNUNET_FS_download_make_status_ (&pi, dc);
1324 /* abort all pending requests */
1327 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1330 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1332 GNUNET_FS_free_download_request_ (dc->top_request);
1333 dc->top_request = NULL;
1334 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1336 dc->pending_head = NULL;
1337 dc->pending_tail = NULL;
1338 GNUNET_FS_download_sync_ (dc);
1344 * Process a download result.
1346 * @param dc our download context
1347 * @param type type of the result
1348 * @param data the (encrypted) response
1349 * @param size size of data
1352 process_result (struct GNUNET_FS_DownloadContext *dc,
1353 enum GNUNET_BLOCK_Type type,
1357 struct ProcessResultClosure prc;
1363 prc.do_store = GNUNET_YES;
1364 GNUNET_CRYPTO_hash (data, size, &prc.query);
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "Received result for query `%s' from `%s'-service\n",
1368 GNUNET_h2s (&prc.query),
1371 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1373 &process_result_with_request,
1379 * Type of a function to call when we receive a message
1382 * @param cls closure
1383 * @param msg message received, NULL on timeout or fatal error
1386 receive_results (void *cls,
1387 const struct GNUNET_MessageHeader * msg)
1389 struct GNUNET_FS_DownloadContext *dc = cls;
1390 const struct PutMessage *cm;
1393 if ( (NULL == msg) ||
1394 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1395 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1397 GNUNET_break (msg == NULL);
1401 msize = ntohs(msg->size);
1402 cm = (const struct PutMessage*) msg;
1406 msize - sizeof (struct PutMessage));
1407 if (dc->client == NULL)
1408 return; /* fatal error */
1409 /* continue receiving */
1410 GNUNET_CLIENT_receive (dc->client,
1413 GNUNET_TIME_UNIT_FOREVER_REL);
1419 * We're ready to transmit a search request to the
1420 * file-sharing service. Do it. If there is
1421 * more than one request pending, try to send
1422 * multiple or request another transmission.
1424 * @param cls closure
1425 * @param size number of bytes available in buf
1426 * @param buf where the callee should write the message
1427 * @return number of bytes written to buf
1430 transmit_download_request (void *cls,
1434 struct GNUNET_FS_DownloadContext *dc = cls;
1436 struct SearchMessage *sm;
1437 struct DownloadRequest *dr;
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1444 "Transmitting download request failed, trying to reconnect\n");
1449 GNUNET_assert (size >= sizeof (struct SearchMessage));
1452 while ( (NULL != (dr = dc->pending_head)) &&
1453 (size >= msize + sizeof (struct SearchMessage)) )
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 "Transmitting download request for `%s' to `%s'-service\n",
1458 GNUNET_h2s (&dr->chk.query),
1461 memset (sm, 0, sizeof (struct SearchMessage));
1462 sm->header.size = htons (sizeof (struct SearchMessage));
1463 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1464 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1465 sm->options = htonl (1);
1467 sm->options = htonl (0);
1469 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
1471 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
1472 sm->anonymity_level = htonl (dc->anonymity);
1473 sm->target = dc->target.hashPubKey;
1474 sm->query = dr->chk.query;
1475 GNUNET_CONTAINER_DLL_remove (dc->pending_head,
1478 dr->is_pending = GNUNET_NO;
1479 msize += sizeof (struct SearchMessage);
1482 if (dc->pending_head != NULL)
1484 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1485 sizeof (struct SearchMessage),
1486 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1488 &transmit_download_request,
1490 GNUNET_assert (dc->th != NULL);
1497 * Reconnect to the FS service and transmit our queries NOW.
1499 * @param cls our download context
1503 do_reconnect (void *cls,
1504 const struct GNUNET_SCHEDULER_TaskContext *tc)
1506 struct GNUNET_FS_DownloadContext *dc = cls;
1507 struct GNUNET_CLIENT_Connection *client;
1509 dc->task = GNUNET_SCHEDULER_NO_TASK;
1510 client = GNUNET_CLIENT_connect ("fs",
1514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1515 "Connecting to `%s'-service failed, will try again.\n",
1520 dc->client = client;
1521 if (dc->pending_head != NULL)
1523 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1524 sizeof (struct SearchMessage),
1525 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1527 &transmit_download_request,
1529 GNUNET_assert (dc->th != NULL);
1531 GNUNET_CLIENT_receive (client,
1534 GNUNET_TIME_UNIT_FOREVER_REL);
1539 * Add entries to the pending list.
1541 * @param cls our download context
1543 * @param entry entry of type "struct DownloadRequest"
1547 retry_entry (void *cls,
1548 const GNUNET_HashCode *key,
1551 struct GNUNET_FS_DownloadContext *dc = cls;
1552 struct DownloadRequest *dr = entry;
1554 GNUNET_CONTAINER_DLL_insert (dc->pending_head,
1557 dr->is_pending = GNUNET_YES;
1563 * We've lost our connection with the FS service.
1564 * Re-establish it and re-transmit all of our
1567 * @param dc download context that is having trouble
1570 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1573 if (NULL != dc->client)
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1577 "Moving all requests back to pending list\n");
1581 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1584 /* full reset of the pending list */
1585 dc->pending_head = NULL;
1586 dc->pending_tail = NULL;
1587 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1590 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595 "Will try to reconnect in 1s\n");
1598 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1605 * We're allowed to ask the FS service for our blocks. Start the download.
1607 * @param cls the 'struct GNUNET_FS_DownloadContext'
1608 * @param client handle to use for communcation with FS (we must destroy it!)
1611 activate_fs_download (void *cls,
1612 struct GNUNET_CLIENT_Connection *client)
1614 struct GNUNET_FS_DownloadContext *dc = cls;
1615 struct GNUNET_FS_ProgressInfo pi;
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Download activated\n");
1621 GNUNET_assert (NULL != client);
1622 GNUNET_assert (dc->client == NULL);
1623 GNUNET_assert (dc->th == NULL);
1624 dc->client = client;
1625 GNUNET_CLIENT_receive (client,
1628 GNUNET_TIME_UNIT_FOREVER_REL);
1629 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1630 GNUNET_FS_download_make_status_ (&pi, dc);
1631 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Asking for transmission to FS service\n");
1638 if (dc->pending_head != NULL)
1640 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1641 sizeof (struct SearchMessage),
1642 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1644 &transmit_download_request,
1646 GNUNET_assert (dc->th != NULL);
1652 * We must stop to ask the FS service for our blocks. Pause the download.
1654 * @param cls the 'struct GNUNET_FS_DownloadContext'
1657 deactivate_fs_download (void *cls)
1659 struct GNUNET_FS_DownloadContext *dc = cls;
1660 struct GNUNET_FS_ProgressInfo pi;
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664 "Download deactivated\n");
1668 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1671 if (NULL != dc->client)
1673 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1676 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1677 GNUNET_FS_download_make_status_ (&pi, dc);
1682 * (recursively) Create a download request structure.
1684 * @param parent parent of the current entry
1685 * @param depth depth of the current entry, 0 are the DBLOCKs,
1686 * top level block is 'dc->treedepth - 1'
1687 * @param dr_offset offset in the original file this block maps to
1688 * (as in, offset of the first byte of the first DBLOCK
1689 * in the subtree rooted in the returned download request tree)
1690 * @param file_start_offset desired starting offset for the download
1691 * in the original file; requesting tree should not contain
1692 * DBLOCKs prior to the file_start_offset
1693 * @param file_length desired number of bytes the user wanted to access
1694 * (from file_start_offset). Resulting tree should not contain
1695 * DBLOCKs after file_start_offset + file_length.
1696 * @return download request tree for the given range of DBLOCKs at
1697 * the specified depth
1699 static struct DownloadRequest *
1700 create_download_request (struct DownloadRequest *parent,
1703 uint64_t file_start_offset,
1704 uint64_t desired_length)
1706 struct DownloadRequest *dr;
1708 unsigned int head_skip;
1709 uint64_t child_block_size;
1711 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1712 dr->parent = parent;
1714 dr->offset = dr_offset;
1717 child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1719 /* calculate how many blocks at this level are not interesting
1720 from the start (rounded down), either because of the requested
1721 file offset or because this IBlock is further along */
1722 if (dr_offset < file_start_offset)
1723 head_skip = file_start_offset / child_block_size;
1725 head_skip = dr_offset / child_block_size;
1727 /* calculate index of last block at this level that is interesting (rounded up) */
1728 dr->num_children = file_start_offset + desired_length / child_block_size;
1729 if (dr->num_children * child_block_size < file_start_offset + desired_length)
1730 dr->num_children++; /* round up */
1732 /* now we can get the total number of children for this block */
1733 dr->num_children -= head_skip;
1734 if (dr->num_children > CHK_PER_INODE)
1735 dr->num_children = CHK_PER_INODE; /* cap at max */
1737 /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1738 GNUNET_assert (dr->num_children > 0);
1740 dr->children = GNUNET_malloc (dr->num_children *
1741 sizeof (struct DownloadRequest *));
1742 for (i=0;i<dr->num_children;i++)
1743 dr->children[i] = create_download_request (dr,
1745 dr_offset + i * child_block_size,
1754 * Continuation after a possible attempt to reconstruct
1755 * the current IBlock from the existing file.
1757 * @param cls the 'struct ReconstructContext'
1758 * @param tc scheduler context
1761 reconstruct_cont (void *cls,
1762 const struct GNUNET_SCHEDULER_TaskContext *tc)
1764 struct GNUNET_FS_DownloadContext *dc = cls;
1766 /* clean up state from tree encoder */
1769 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1772 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1774 GNUNET_SCHEDULER_cancel (dc->task);
1775 dc->task = GNUNET_SCHEDULER_NO_TASK;
1777 if (dc->rfh != NULL)
1779 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1782 /* start "normal" download */
1783 schedule_block_download (dc,
1789 * Task requesting the next block from the tree encoder.
1791 * @param tc task context
1794 get_next_block (void *cls,
1795 const struct GNUNET_SCHEDULER_TaskContext *tc)
1797 struct GNUNET_FS_DownloadContext *dc = cls;
1799 dc->task = GNUNET_SCHEDULER_NO_TASK;
1800 GNUNET_FS_tree_encoder_next (dc->te);
1806 * Function called asking for the current (encoded)
1807 * block to be processed. After processing the
1808 * client should either call "GNUNET_FS_tree_encode_next"
1809 * or (on error) "GNUNET_FS_tree_encode_finish".
1811 * This function checks if the content on disk matches
1812 * the expected content based on the URI.
1814 * @param cls closure
1815 * @param chk content hash key for the block
1816 * @param offset offset of the block
1817 * @param depth depth of the block, 0 for DBLOCK
1818 * @param type type of the block (IBLOCK or DBLOCK)
1819 * @param block the (encrypted) block
1820 * @param block_size size of block (in bytes)
1823 reconstruct_cb (void *cls,
1824 const struct ContentHashKey *chk,
1827 enum GNUNET_BLOCK_Type type,
1829 uint16_t block_size)
1831 struct GNUNET_FS_DownloadContext *dc = cls;
1832 struct GNUNET_FS_ProgressInfo pi;
1833 struct DownloadRequest *dr;
1837 /* find corresponding request entry */
1838 dr = dc->top_request;
1839 while (dr->depth > depth)
1841 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1842 chld = (offset - dr->offset) / blen;
1843 GNUNET_assert (chld < dr->num_children);
1844 dr = dr->children[chld];
1850 case BRS_RECONSTRUCT_DOWN:
1852 case BRS_RECONSTRUCT_META_UP:
1854 case BRS_RECONSTRUCT_UP:
1857 if (0 == memcmp (chk,
1859 sizeof (struct ContentHashKey)))
1861 /* block matches, hence tree below matches;
1862 this request is done! */
1863 dr->state = BRS_DOWNLOAD_UP;
1864 /* calculate how many bytes of payload this block
1866 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1867 /* how many of those bytes are in the requested range? */
1868 blen = GNUNET_MIN (blen,
1869 dc->length + dc->offset - dr->offset);
1870 /* signal progress */
1871 dc->completed += blen;
1872 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1873 pi.value.download.specifics.progress.data = NULL;
1874 pi.value.download.specifics.progress.offset = offset;
1875 pi.value.download.specifics.progress.data_len = 0;
1876 pi.value.download.specifics.progress.depth = 0;
1877 GNUNET_FS_download_make_status_ (&pi, dc);
1883 case BRS_DOWNLOAD_DOWN:
1885 case BRS_DOWNLOAD_UP:
1893 if ( (dr == dc->top_request) &&
1894 (dr->state == BRS_DOWNLOAD_UP) )
1896 check_completed (dc);
1899 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block,
1905 * Function called by the tree encoder to obtain a block of plaintext
1906 * data (for the lowest level of the tree).
1908 * @param cls our 'struct ReconstructContext'
1909 * @param offset identifies which block to get
1910 * @param max (maximum) number of bytes to get; returning
1911 * fewer will also cause errors
1912 * @param buf where to copy the plaintext buffer
1913 * @param emsg location to store an error message (on error)
1914 * @return number of bytes copied to buf, 0 on error
1917 fh_reader (void *cls,
1923 struct GNUNET_FS_DownloadContext *dc = cls;
1924 struct GNUNET_DISK_FileHandle *fh = dc->rfh;
1929 GNUNET_DISK_file_seek (fh,
1931 GNUNET_DISK_SEEK_SET))
1933 *emsg = GNUNET_strdup (strerror (errno));
1936 ret = GNUNET_DISK_file_read (fh, buf, max);
1939 *emsg = GNUNET_strdup (strerror (errno));
1947 * Task that creates the initial (top-level) download
1948 * request for the file.
1950 * @param cls the 'struct GNUNET_FS_DownloadContext'
1951 * @param tc scheduler context
1954 GNUNET_FS_download_start_task_ (void *cls,
1955 const struct GNUNET_SCHEDULER_TaskContext *tc)
1957 struct GNUNET_FS_DownloadContext *dc = cls;
1958 struct GNUNET_FS_ProgressInfo pi;
1959 struct GNUNET_DISK_FileHandle *fh;
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "Start task running...\n");
1965 dc->task = GNUNET_SCHEDULER_NO_TASK;
1966 if (dc->length == 0)
1968 /* no bytes required! */
1969 if (dc->filename != NULL)
1971 fh = GNUNET_DISK_file_open (dc->filename != NULL
1973 : dc->temp_filename,
1974 GNUNET_DISK_OPEN_READWRITE |
1975 GNUNET_DISK_OPEN_CREATE |
1976 ( (0 == GNUNET_FS_uri_chk_get_file_size (dc->uri))
1977 ? GNUNET_DISK_OPEN_TRUNCATE : 0),
1978 GNUNET_DISK_PERM_USER_READ |
1979 GNUNET_DISK_PERM_USER_WRITE |
1980 GNUNET_DISK_PERM_GROUP_READ |
1981 GNUNET_DISK_PERM_OTHER_READ);
1982 GNUNET_DISK_file_close (fh);
1984 GNUNET_FS_download_sync_ (dc);
1985 check_completed (dc);
1988 if (dc->emsg != NULL)
1990 if (dc->top_request == NULL)
1992 dc->top_request = create_download_request (NULL, dc->treedepth - 1, 0,
1993 dc->offset, dc->length);
1994 dc->top_request->state = BRS_CHK_SET;
1995 dc->top_request->chk = (dc->uri->type == chk)
1996 ? dc->uri->data.chk.chk
1997 : dc->uri->data.loc.fi.chk;
1999 GNUNET_FS_download_sync_ (dc);
2000 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
2001 pi.value.download.specifics.start.meta = dc->meta;
2002 GNUNET_FS_download_make_status_ (&pi, dc);
2004 GNUNET_FS_download_start_downloading_ (dc);
2005 /* attempt reconstruction from disk */
2006 if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
2007 dc->rfh = GNUNET_DISK_file_open (dc->filename,
2008 GNUNET_DISK_OPEN_READ,
2009 GNUNET_DISK_PERM_NONE);
2010 if (dc->top_request->state == BRS_CHK_SET)
2012 if (dc->rfh != NULL)
2014 /* first, try top-down */
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "Trying top-down reconstruction for `%s'\n",
2020 try_top_down_reconstruction (dc, dc->top_request);
2021 switch (dc->top_request->state)
2025 case BRS_DOWNLOAD_DOWN:
2026 break; /* normal, some blocks already down */
2027 case BRS_DOWNLOAD_UP:
2028 /* already done entirely, party! */
2029 if (dc->rfh != NULL)
2031 /* avoid hanging on to file handle longer than
2033 GNUNET_DISK_file_close (dc->rfh);
2038 GNUNET_asprintf (&dc->emsg,
2040 GNUNET_FS_download_sync_ (dc);
2041 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
2042 pi.value.download.specifics.error.message = dc->emsg;
2043 GNUNET_FS_download_make_status_ (&pi, dc);
2051 /* attempt reconstruction from meta data */
2052 if ( (GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
2053 (NULL != dc->meta) )
2056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2057 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
2058 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
2059 (unsigned int) GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
2061 GNUNET_CONTAINER_meta_data_iterate (dc->meta,
2064 if (dc->top_request->state == BRS_DOWNLOAD_UP)
2066 if (dc->rfh != NULL)
2068 /* avoid hanging on to file handle longer than
2070 GNUNET_DISK_file_close (dc->rfh);
2073 return; /* finished, status update was already done for us */
2076 if (dc->rfh != NULL)
2078 /* finally, try bottom-up */
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Trying bottom-up reconstruction of file `%s'\n",
2084 dc->te = GNUNET_FS_tree_encoder_create (dc->h,
2091 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block,
2096 /* simple, top-level download */
2097 schedule_block_download (dc,
2100 if (dc->top_request->state == BRS_DOWNLOAD_UP)
2101 check_completed (dc);
2106 * Create SUSPEND event for the given download operation
2107 * and then clean up our state (without stop signal).
2109 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
2112 GNUNET_FS_download_signal_suspend_ (void *cls)
2114 struct GNUNET_FS_DownloadContext *dc = cls;
2115 struct GNUNET_FS_ProgressInfo pi;
2117 if (dc->top != NULL)
2118 GNUNET_FS_end_top (dc->h, dc->top);
2119 while (NULL != dc->child_head)
2120 GNUNET_FS_download_signal_suspend_ (dc->child_head);
2121 if (dc->search != NULL)
2123 dc->search->download = NULL;
2126 if (dc->job_queue != NULL)
2128 GNUNET_FS_dequeue_ (dc->job_queue);
2129 dc->job_queue = NULL;
2131 if (dc->parent != NULL)
2132 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
2133 dc->parent->child_tail,
2135 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2137 GNUNET_SCHEDULER_cancel (dc->task);
2138 dc->task = GNUNET_SCHEDULER_NO_TASK;
2140 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
2141 GNUNET_FS_download_make_status_ (&pi, dc);
2144 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2147 if (dc->rfh != NULL)
2149 GNUNET_DISK_file_close (dc->rfh);
2152 GNUNET_FS_free_download_request_ (dc->top_request);
2153 if (dc->active != NULL)
2155 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2158 GNUNET_free_non_null (dc->filename);
2159 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2160 GNUNET_FS_uri_destroy (dc->uri);
2161 GNUNET_free_non_null (dc->temp_filename);
2162 GNUNET_free_non_null (dc->serialization);
2168 * Download parts of a file. Note that this will store
2169 * the blocks at the respective offset in the given file. Also, the
2170 * download is still using the blocking of the underlying FS
2171 * encoding. As a result, the download may *write* outside of the
2172 * given boundaries (if offset and length do not match the 32k FS
2173 * block boundaries). <p>
2175 * This function should be used to focus a download towards a
2176 * particular portion of the file (optimization), not to strictly
2177 * limit the download to exactly those bytes.
2179 * @param h handle to the file sharing subsystem
2180 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2181 * @param meta known metadata for the file (can be NULL)
2182 * @param filename where to store the file, maybe NULL (then no file is
2183 * created on disk and data must be grabbed from the callbacks)
2184 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2185 * can be NULL (in which case we will pick a name if needed); the temporary file
2186 * may already exist, in which case we will try to use the data that is there and
2187 * if it is not what is desired, will overwrite it
2188 * @param offset at what offset should we start the download (typically 0)
2189 * @param length how many bytes should be downloaded starting at offset
2190 * @param anonymity anonymity level to use for the download
2191 * @param options various options
2192 * @param cctx initial value for the client context for this download
2193 * @param parent parent download to associate this download with (use NULL
2194 * for top-level downloads; useful for manually-triggered recursive downloads)
2195 * @return context that can be used to control this download
2197 struct GNUNET_FS_DownloadContext *
2198 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2199 const struct GNUNET_FS_Uri *uri,
2200 const struct GNUNET_CONTAINER_MetaData *meta,
2201 const char *filename,
2202 const char *tempname,
2206 enum GNUNET_FS_DownloadOptions options,
2208 struct GNUNET_FS_DownloadContext *parent)
2210 struct GNUNET_FS_DownloadContext *dc;
2212 GNUNET_assert (GNUNET_FS_uri_test_chk (uri) ||
2213 GNUNET_FS_uri_test_loc (uri) );
2215 if ( (offset + length < offset) ||
2216 (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)) )
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Starting download `%s' of %llu bytes\n",
2225 (unsigned long long) length);
2227 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
2229 dc->parent = parent;
2232 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2236 dc->uri = GNUNET_FS_uri_dup (uri);
2237 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
2238 dc->client_info = cctx;
2239 dc->start_time = GNUNET_TIME_absolute_get ();
2240 if (NULL != filename)
2242 dc->filename = GNUNET_strdup (filename);
2243 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2244 GNUNET_DISK_file_size (filename,
2248 if (GNUNET_FS_uri_test_loc (dc->uri))
2249 GNUNET_assert (GNUNET_OK ==
2250 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2252 dc->offset = offset;
2253 dc->length = length;
2254 dc->anonymity = anonymity;
2255 dc->options = options;
2256 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2257 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size(dc->uri));
2258 if ( (filename == NULL) &&
2259 (is_recursive_download (dc) ) )
2261 if (tempname != NULL)
2262 dc->temp_filename = GNUNET_strdup (tempname);
2264 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 "Download tree has depth %u\n",
2274 dc->top = GNUNET_FS_make_top (dc->h,
2275 &GNUNET_FS_download_signal_suspend_,
2278 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_,
2285 * Download parts of a file based on a search result. The download
2286 * will be associated with the search result (and the association
2287 * will be preserved when serializing/deserializing the state).
2288 * If the search is stopped, the download will not be aborted but
2289 * be 'promoted' to a stand-alone download.
2291 * As with the other download function, this will store
2292 * the blocks at the respective offset in the given file. Also, the
2293 * download is still using the blocking of the underlying FS
2294 * encoding. As a result, the download may *write* outside of the
2295 * given boundaries (if offset and length do not match the 32k FS
2296 * block boundaries). <p>
2298 * The given range can be used to focus a download towards a
2299 * particular portion of the file (optimization), not to strictly
2300 * limit the download to exactly those bytes.
2302 * @param h handle to the file sharing subsystem
2303 * @param sr the search result to use for the download (determines uri and
2304 * meta data and associations)
2305 * @param filename where to store the file, maybe NULL (then no file is
2306 * created on disk and data must be grabbed from the callbacks)
2307 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2308 * can be NULL (in which case we will pick a name if needed); the temporary file
2309 * may already exist, in which case we will try to use the data that is there and
2310 * if it is not what is desired, will overwrite it
2311 * @param offset at what offset should we start the download (typically 0)
2312 * @param length how many bytes should be downloaded starting at offset
2313 * @param anonymity anonymity level to use for the download
2314 * @param options various download options
2315 * @param cctx initial value for the client context for this download
2316 * @return context that can be used to control this download
2318 struct GNUNET_FS_DownloadContext *
2319 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2320 struct GNUNET_FS_SearchResult *sr,
2321 const char *filename,
2322 const char *tempname,
2326 enum GNUNET_FS_DownloadOptions options,
2329 struct GNUNET_FS_DownloadContext *dc;
2331 if ( (sr == NULL) ||
2332 (sr->download != NULL) )
2337 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri) ||
2338 GNUNET_FS_uri_test_loc (sr->uri) );
2339 if ( (offset + length < offset) ||
2340 (offset + length > sr->uri->data.chk.file_length) )
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "Starting download `%s' of %llu bytes\n",
2349 (unsigned long long) length);
2351 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
2355 if (sr->probe_ctx != NULL)
2357 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2358 sr->probe_ctx = NULL;
2360 dc->uri = GNUNET_FS_uri_dup (sr->uri);
2361 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
2362 dc->client_info = cctx;
2363 dc->start_time = GNUNET_TIME_absolute_get ();
2364 if (NULL != filename)
2366 dc->filename = GNUNET_strdup (filename);
2367 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2368 GNUNET_DISK_file_size (filename,
2372 if (GNUNET_FS_uri_test_loc (dc->uri))
2373 GNUNET_assert (GNUNET_OK ==
2374 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2376 dc->offset = offset;
2377 dc->length = length;
2378 dc->anonymity = anonymity;
2379 dc->options = options;
2380 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2381 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
2382 if ( (filename == NULL) &&
2383 (is_recursive_download (dc) ) )
2385 if (tempname != NULL)
2386 dc->temp_filename = GNUNET_strdup (tempname);
2388 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Download tree has depth %u\n",
2396 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_,
2403 * Start the downloading process (by entering the queue).
2405 * @param dc our download context
2408 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2410 if (dc->completed == dc->length)
2412 GNUNET_assert (dc->job_queue == NULL);
2413 dc->job_queue = GNUNET_FS_queue_ (dc->h,
2414 &activate_fs_download,
2415 &deactivate_fs_download,
2417 (dc->length + DBLOCK_SIZE-1) / DBLOCK_SIZE);
2422 * Stop a download (aborts if download is incomplete).
2424 * @param dc handle for the download
2425 * @param do_delete delete files of incomplete downloads
2428 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
2431 struct GNUNET_FS_ProgressInfo pi;
2434 if (dc->top != NULL)
2435 GNUNET_FS_end_top (dc->h, dc->top);
2438 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2440 GNUNET_SCHEDULER_cancel (dc->task);
2441 dc->task = GNUNET_SCHEDULER_NO_TASK;
2443 if (dc->search != NULL)
2445 dc->search->download = NULL;
2448 if (dc->job_queue != NULL)
2450 GNUNET_FS_dequeue_ (dc->job_queue);
2451 dc->job_queue = NULL;
2455 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2458 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2459 while (NULL != dc->child_head)
2460 GNUNET_FS_download_stop (dc->child_head,
2462 if (dc->parent != NULL)
2463 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
2464 dc->parent->child_tail,
2466 if (dc->serialization != NULL)
2467 GNUNET_FS_remove_sync_file_ (dc->h,
2468 ( (dc->parent != NULL) || (dc->search != NULL) )
2469 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
2470 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD ,
2472 if ( (GNUNET_YES == have_children) &&
2473 (dc->parent == NULL) )
2474 GNUNET_FS_remove_sync_dir_ (dc->h,
2475 (dc->search != NULL)
2476 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
2477 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2479 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2480 GNUNET_FS_download_make_status_ (&pi, dc);
2481 GNUNET_FS_free_download_request_ (dc->top_request);
2482 dc->top_request = NULL;
2483 if (dc->active != NULL)
2485 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2488 if (dc->filename != NULL)
2490 if ( (dc->completed != dc->length) &&
2491 (GNUNET_YES == do_delete) )
2493 if (0 != UNLINK (dc->filename))
2494 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2498 GNUNET_free (dc->filename);
2500 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2501 GNUNET_FS_uri_destroy (dc->uri);
2502 if (NULL != dc->temp_filename)
2504 if (0 != UNLINK (dc->temp_filename))
2505 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2508 GNUNET_free (dc->temp_filename);
2510 GNUNET_free_non_null (dc->serialization);
2514 /* end of fs_download.c */