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_EXTRA_LOGGING
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) -
50 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, 0 for DBLOCK
66 * @return off for DBLOCKS (depth == treedepth),
67 * otherwise an offset past the end
68 * of the file that does not overlap
69 * with the range for any other block
72 compute_disk_offset (uint64_t fsize, uint64_t off, unsigned int depth)
75 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
76 uint64_t loff; /* where do IBlocks for depth "i" start? */
77 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
81 /* first IBlocks start at the end of file, rounded up
82 * to full DBLOCK_SIZE */
83 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
85 ((fsize + DBLOCK_SIZE -
86 1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
87 GNUNET_assert (0 == (off % DBLOCK_SIZE));
88 ioff = (off / DBLOCK_SIZE);
89 for (i = 1; i < depth; i++)
92 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
93 GNUNET_assert (lsize > 0);
94 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
95 ioff /= CHK_PER_INODE;
97 return loff + ioff * sizeof (struct ContentHashKey);
102 * Fill in all of the generic fields for a download event and call the
105 * @param pi structure to fill in
106 * @param dc overall download context
109 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
110 struct GNUNET_FS_DownloadContext *dc)
112 pi->value.download.dc = dc;
113 pi->value.download.cctx = dc->client_info;
114 pi->value.download.pctx =
115 (dc->parent == NULL) ? NULL : dc->parent->client_info;
116 pi->value.download.sctx =
117 (dc->search == NULL) ? NULL : dc->search->client_info;
118 pi->value.download.uri = dc->uri;
119 pi->value.download.filename = dc->filename;
120 pi->value.download.size = dc->length;
121 /* FIXME: Fix duration calculation to account for pauses */
122 pi->value.download.duration =
123 GNUNET_TIME_absolute_get_duration (dc->start_time);
124 pi->value.download.completed = dc->completed;
125 pi->value.download.anonymity = dc->anonymity;
126 pi->value.download.eta =
127 GNUNET_TIME_calculate_eta (dc->start_time, dc->completed, dc->length);
128 pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
129 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
130 dc->client_info = dc->h->upcb (dc->h->upcb_cls, pi);
132 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL, pi);
137 * We're ready to transmit a search request to the
138 * file-sharing service. Do it. If there is
139 * more than one request pending, try to send
140 * multiple or request another transmission.
143 * @param size number of bytes available in buf
144 * @param buf where the callee should write the message
145 * @return number of bytes written to buf
148 transmit_download_request (void *cls, size_t size, void *buf);
152 * Closure for iterator processing results.
154 struct ProcessResultClosure
160 GNUNET_HashCode query;
163 * Data found in P2P network.
168 * Our download context.
170 struct GNUNET_FS_DownloadContext *dc;
173 * Number of bytes in data.
180 enum GNUNET_BLOCK_Type type;
183 * Flag to indicate if this block should be stored on disk.
187 struct GNUNET_TIME_Absolute last_transmission;
193 * Iterator over entries in the pending requests in the 'active' map for the
194 * reply that we just got.
196 * @param cls closure (our 'struct ProcessResultClosure')
197 * @param key query for the given value / request
198 * @param value value in the hash map (a 'struct DownloadRequest')
199 * @return GNUNET_YES (we should continue to iterate); unless serious error
202 process_result_with_request (void *cls, const GNUNET_HashCode * key,
207 * We've found a matching block without downloading it.
208 * Encrypt it and pass it to our "receive" function as
209 * if we had received it from the network.
211 * @param dc download in question
212 * @param chk request this relates to
213 * @param dr request details
214 * @param block plaintext data matching request
215 * @param len number of bytes in block
216 * @param do_store should we still store the block on disk?
217 * @return GNUNET_OK on success
220 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
221 const struct ContentHashKey *chk,
222 struct DownloadRequest *dr, const char *block,
223 size_t len, int do_store)
225 struct ProcessResultClosure prc;
227 struct GNUNET_CRYPTO_AesSessionKey sk;
228 struct GNUNET_CRYPTO_AesInitializationVector iv;
229 GNUNET_HashCode query;
231 GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
232 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len, &sk, &iv, enc))
235 return GNUNET_SYSERR;
237 GNUNET_CRYPTO_hash (enc, len, &query);
238 if (0 != memcmp (&query, &chk->query, sizeof (GNUNET_HashCode)))
241 return GNUNET_SYSERR;
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Matching block for `%s' at offset %llu already present, no need for download!\n",
246 dc->filename, (unsigned long long) dr->offset);
248 /* already got it! */
254 dr->depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK : GNUNET_BLOCK_TYPE_FS_IBLOCK;
255 prc.query = chk->query;
256 prc.do_store = do_store;
257 prc.last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
258 process_result_with_request (&prc, &chk->key, dr);
264 * We've lost our connection with the FS service.
265 * Re-establish it and re-transmit all of our
268 * @param dc download context that is having trouble
271 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
275 * We found an entry in a directory. Check if the respective child
276 * already exists and if not create the respective child download.
278 * @param cls the parent download
279 * @param filename name of the file in the directory
280 * @param uri URI of the file (CHK or LOC)
281 * @param meta meta data of the file
282 * @param length number of bytes in data
283 * @param data contents of the file (or NULL if they were not inlined)
286 trigger_recursive_download (void *cls, const char *filename,
287 const struct GNUNET_FS_Uri *uri,
288 const struct GNUNET_CONTAINER_MetaData *meta,
289 size_t length, const void *data);
293 * We're done downloading a directory. Open the file and
294 * trigger all of the (remaining) child downloads.
296 * @param dc context of download that just completed
299 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
304 struct GNUNET_DISK_FileHandle *h;
305 struct GNUNET_DISK_MapHandle *m;
307 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
308 size = (size_t) size64;
309 if (size64 != (uint64_t) size)
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 ("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
316 if (dc->filename != NULL)
318 h = GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
319 GNUNET_DISK_PERM_NONE);
323 GNUNET_assert (dc->temp_filename != NULL);
324 h = GNUNET_DISK_file_open (dc->temp_filename, GNUNET_DISK_OPEN_READ,
325 GNUNET_DISK_PERM_NONE);
329 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333 _("Directory too large for system address space\n"));
337 GNUNET_FS_directory_list_contents (size, data, 0,
338 &trigger_recursive_download, dc);
339 GNUNET_DISK_file_unmap (m);
341 GNUNET_DISK_file_close (h);
342 if (dc->filename == NULL)
344 if (0 != UNLINK (dc->temp_filename))
345 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
347 GNUNET_free (dc->temp_filename);
348 dc->temp_filename = NULL;
354 * Check if all child-downloads have completed (or trigger them if
355 * necessary) and once we're completely done, signal completion (and
356 * possibly recurse to parent). This function MUST be called when the
357 * download of a file itself is done or when the download of a file is
358 * done and then later a direct child download has completed (and
359 * hence this download may complete itself).
361 * @param dc download to check for completion of children
364 check_completed (struct GNUNET_FS_DownloadContext *dc)
366 struct GNUNET_FS_ProgressInfo pi;
367 struct GNUNET_FS_DownloadContext *pos;
369 /* first, check if we need to download children */
370 if ((dc->child_head == NULL) && (is_recursive_download (dc)))
371 full_recursive_download (dc);
372 /* then, check if children are done already */
373 pos = dc->child_head;
376 if ((pos->emsg == NULL) && (pos->completed < pos->length))
377 return; /* not done yet */
378 if ((pos->child_head != NULL) && (pos->has_finished != GNUNET_YES))
379 return; /* not transitively done yet */
382 /* All of our children are done, so mark this download done */
383 dc->has_finished = GNUNET_YES;
384 if (dc->job_queue != NULL)
386 GNUNET_FS_dequeue_ (dc->job_queue);
387 dc->job_queue = NULL;
389 GNUNET_FS_download_sync_ (dc);
391 /* signal completion */
392 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
393 GNUNET_FS_download_make_status_ (&pi, dc);
395 /* let parent know */
396 if (dc->parent != NULL)
397 check_completed (dc->parent);
402 * We got a block of plaintext data (from the meta data).
403 * Try it for upward reconstruction of the data. On success,
404 * the top-level block will move to state BRS_DOWNLOAD_UP.
406 * @param dc context for the download
407 * @param dr download request to match against
408 * @param data plaintext data, starting from the beginning of the file
409 * @param data_len number of bytes in data
412 try_match_block (struct GNUNET_FS_DownloadContext *dc,
413 struct DownloadRequest *dr, const char *data, size_t data_len)
415 struct GNUNET_FS_ProgressInfo pi;
417 char enc[DBLOCK_SIZE];
418 struct ContentHashKey chks[CHK_PER_INODE];
419 struct ContentHashKey in_chk;
420 struct GNUNET_CRYPTO_AesSessionKey sk;
421 struct GNUNET_CRYPTO_AesInitializationVector iv;
423 struct DownloadRequest *drc;
424 struct GNUNET_DISK_FileHandle *fh;
431 odata_len = data_len;
432 if (BRS_DOWNLOAD_UP == dr->state)
436 complete = GNUNET_YES;
437 for (i = 0; i < dr->num_children; i++)
439 drc = dr->children[i];
440 try_match_block (dc, drc, data, data_len);
441 if (drc->state != BRS_RECONSTRUCT_META_UP)
442 complete = GNUNET_NO;
446 if (GNUNET_YES != complete)
448 data = (const char *) chks;
449 dlen = dr->num_children * sizeof (struct ContentHashKey);
453 if (dr->offset > data_len)
455 dlen = GNUNET_MIN (data_len - dr->offset, DBLOCK_SIZE);
457 GNUNET_CRYPTO_hash (&data[dr->offset], dlen, &in_chk.key);
458 GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
459 if (-1 == GNUNET_CRYPTO_aes_encrypt (&data[dr->offset], dlen, &sk, &iv, enc))
464 GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
469 dr->state = BRS_RECONSTRUCT_META_UP;
472 if (0 != memcmp (&in_chk, &dr->chk, sizeof (struct ContentHashKey)))
474 /* other peer provided bogus meta data */
478 /* write block to disk */
479 fn = dc->filename != NULL ? dc->filename : dc->temp_filename;
480 fh = GNUNET_DISK_file_open (fn,
481 GNUNET_DISK_OPEN_READWRITE |
482 GNUNET_DISK_OPEN_CREATE |
483 GNUNET_DISK_OPEN_TRUNCATE,
484 GNUNET_DISK_PERM_USER_READ |
485 GNUNET_DISK_PERM_USER_WRITE |
486 GNUNET_DISK_PERM_GROUP_READ |
487 GNUNET_DISK_PERM_OTHER_READ);
490 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
491 GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
493 GNUNET_DISK_file_close (fh);
494 dr->state = BRS_ERROR;
495 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
496 pi.value.download.specifics.error.message = dc->emsg;
497 GNUNET_FS_download_make_status_ (&pi, dc);
500 if (data_len != GNUNET_DISK_file_write (fh, odata, odata_len))
502 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
503 GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
505 GNUNET_DISK_file_close (fh);
506 dr->state = BRS_ERROR;
507 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
508 pi.value.download.specifics.error.message = dc->emsg;
509 GNUNET_FS_download_make_status_ (&pi, dc);
512 GNUNET_DISK_file_close (fh);
514 dr->state = BRS_DOWNLOAD_UP;
515 dc->completed = dc->length;
516 GNUNET_FS_download_sync_ (dc);
517 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
518 pi.value.download.specifics.progress.data = data;
519 pi.value.download.specifics.progress.offset = 0;
520 pi.value.download.specifics.progress.data_len = dlen;
521 pi.value.download.specifics.progress.depth = 0;
522 pi.value.download.specifics.progress.trust_offered = 0;
523 GNUNET_FS_download_make_status_ (&pi, dc);
524 if ((NULL != dc->filename) &&
526 truncate (dc->filename,
527 GNUNET_ntohll (dc->uri->data.chk.file_length))))
528 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
530 check_completed (dc);
533 /* how did we get here? */
541 * Type of a function that libextractor calls for each
542 * meta data item found. If we find full data meta data,
543 * call 'try_match_block' on it.
545 * @param cls our 'struct GNUNET_FS_DownloadContext*'
546 * @param plugin_name name of the plugin that produced this value;
547 * special values can be used (i.e. '<zlib>' for zlib being
548 * used in the main libextractor library and yielding
550 * @param type libextractor-type describing the meta data
551 * @param format basic format information about data
552 * @param data_mime_type mime-type of data (not of the original file);
553 * can be NULL (if mime-type is not known)
554 * @param data actual meta-data found
555 * @param data_len number of bytes in data
556 * @return 0 to continue extracting, 1 to abort
559 match_full_data (void *cls, const char *plugin_name,
560 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
561 const char *data_mime_type, const char *data, size_t data_len)
563 struct GNUNET_FS_DownloadContext *dc = cls;
565 if (type != EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u bytes of FD!\n",
569 (unsigned int) data_len);
571 if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
574 return 1; /* bogus meta data */
576 try_match_block (dc, dc->top_request, data, data_len);
582 * Set the state of the given download request to
583 * BRS_DOWNLOAD_UP and propagate it up the tree.
585 * @param dr download request that is done
588 propagate_up (struct DownloadRequest *dr)
594 dr->state = BRS_DOWNLOAD_UP;
598 for (i = 0; i < dr->num_children; i++)
599 if (dr->children[i]->state != BRS_DOWNLOAD_UP)
602 while (i == dr->num_children);
607 * Try top-down reconstruction. Before, the given request node
608 * must have the state BRS_CHK_SET. Afterwards, more nodes may
609 * have that state or advanced to BRS_DOWNLOAD_DOWN or even
610 * BRS_DOWNLOAD_UP. It is also possible to get BRS_ERROR on the
613 * @param dc overall download this block belongs to
614 * @param dr block to reconstruct
617 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
618 struct DownloadRequest *dr)
621 char block[DBLOCK_SIZE];
626 unsigned int chk_off;
627 struct DownloadRequest *drc;
628 uint64_t child_block_size;
629 const struct ContentHashKey *chks;
632 GNUNET_assert (dc->rfh != NULL);
633 GNUNET_assert (dr->state == BRS_CHK_SET);
634 total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
635 GNUNET_assert (dr->depth < dc->treedepth);
636 len = GNUNET_FS_tree_calculate_block_size (total, dr->offset, dr->depth);
637 GNUNET_assert (len <= DBLOCK_SIZE);
638 off = compute_disk_offset (total, dr->offset, dr->depth);
639 if (dc->old_file_size < off + len)
640 return; /* failure */
641 if (off != GNUNET_DISK_file_seek (dc->rfh, off, GNUNET_DISK_SEEK_SET))
643 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "seek", dc->filename);
644 return; /* failure */
646 if (len != GNUNET_DISK_file_read (dc->rfh, block, len))
648 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", dc->filename);
649 return; /* failure */
651 GNUNET_CRYPTO_hash (block, len, &key);
652 if (0 != memcmp (&key, &dr->chk.key, sizeof (GNUNET_HashCode)))
653 return; /* mismatch */
655 encrypt_existing_match (dc, &dr->chk, dr, block, len, GNUNET_NO))
657 /* hash matches but encrypted block does not, really bad */
658 dr->state = BRS_ERROR;
660 while (dr->parent != NULL)
663 dr->state = BRS_ERROR;
668 dr->state = BRS_DOWNLOAD_DOWN;
670 /* set CHKs for children */
671 up_done = GNUNET_YES;
672 chks = (const struct ContentHashKey *) block;
673 for (i = 0; i < dr->num_children; i++)
675 drc = dr->children[i];
676 GNUNET_assert (drc->offset >= dr->offset);
677 child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
678 GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);
679 chk_off = (drc->offset - dr->offset) / child_block_size;
680 if (drc->state == BRS_INIT)
682 drc->state = BRS_CHK_SET;
683 drc->chk = chks[chk_off];
684 try_top_down_reconstruction (dc, drc);
686 if (drc->state != BRS_DOWNLOAD_UP)
687 up_done = GNUNET_NO; /* children not all done */
689 if (up_done == GNUNET_YES)
690 propagate_up (dr); /* children all done (or no children...) */
695 * Schedule the download of the specified block in the tree.
697 * @param dc overall download this block belongs to
698 * @param dr request to schedule
701 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
702 struct DownloadRequest *dr)
711 case BRS_RECONSTRUCT_DOWN:
714 case BRS_RECONSTRUCT_META_UP:
717 case BRS_RECONSTRUCT_UP:
721 /* normal case, start download */
723 case BRS_DOWNLOAD_DOWN:
724 for (i = 0; i < dr->num_children; i++)
725 schedule_block_download (dc, dr->children[i]);
727 case BRS_DOWNLOAD_UP:
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Scheduling download at offset %llu and depth %u for `%s'\n",
737 (unsigned long long) dr->offset, dr->depth,
738 GNUNET_h2s (&dr->chk.query));
741 GNUNET_CONTAINER_multihashmap_contains_value (dc->active, &dr->chk.query,
743 return; /* already active */
744 GNUNET_CONTAINER_multihashmap_put (dc->active, &dr->chk.query, dr,
745 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
746 if (dc->client == NULL)
747 return; /* download not active */
748 GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
749 dr->is_pending = GNUNET_YES;
752 GNUNET_CLIENT_notify_transmit_ready (dc->client,
753 sizeof (struct SearchMessage),
754 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
756 &transmit_download_request, dc);
760 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
763 * We found an entry in a directory. Check if the respective child
764 * already exists and if not create the respective child download.
766 * @param cls the parent download
767 * @param filename name of the file in the directory
768 * @param uri URI of the file (CHK or LOC)
769 * @param meta meta data of the file
770 * @param length number of bytes in data
771 * @param data contents of the file (or NULL if they were not inlined)
774 trigger_recursive_download (void *cls, const char *filename,
775 const struct GNUNET_FS_Uri *uri,
776 const struct GNUNET_CONTAINER_MetaData *meta,
777 size_t length, const void *data)
779 struct GNUNET_FS_DownloadContext *dc = cls;
780 struct GNUNET_FS_DownloadContext *cpos;
791 return; /* entry for the directory itself */
792 cpos = dc->child_head;
795 if ((GNUNET_FS_uri_test_equal (uri, cpos->uri)) ||
796 ((filename != NULL) && (0 == strcmp (cpos->filename, filename))))
801 return; /* already exists */
803 if (NULL == filename)
805 fn = GNUNET_FS_meta_data_suggest_filename (meta);
808 us = GNUNET_FS_uri_to_string (uri);
809 fn = GNUNET_strdup (&us[strlen (GNUNET_FS_URI_CHK_PREFIX)]);
812 else if (fn[0] == '.')
815 us = GNUNET_FS_uri_to_string (uri);
816 GNUNET_asprintf (&fn, "%s%s", &us[strlen (GNUNET_FS_URI_CHK_PREFIX)],
821 /* change '\' to '/' (this should have happened
822 * during insertion, but malicious peers may
823 * not have done this) */
824 while (NULL != (pos = strstr (fn, "\\")))
826 /* remove '../' everywhere (again, well-behaved
827 * peers don't do this, but don't trust that
828 * we did not get something nasty) */
829 while (NULL != (pos = strstr (fn, "../")))
837 if (dc->filename == NULL)
843 dn = GNUNET_strdup (dc->filename);
844 GNUNET_break ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
846 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
847 GNUNET_FS_DIRECTORY_EXT)));
848 sfn = GNUNET_strdup (filename);
849 while ((strlen (sfn) > 0) && (filename[strlen (sfn) - 1] == '/'))
850 sfn[strlen (sfn) - 1] = '\0';
851 if ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
853 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
854 GNUNET_FS_DIRECTORY_EXT)))
855 dn[strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
856 if ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
857 ((strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
859 strstr (filename + strlen (filename) -
860 strlen (GNUNET_FS_DIRECTORY_EXT), GNUNET_FS_DIRECTORY_EXT))))
862 GNUNET_asprintf (&full_name, "%s%s%s%s", dn, DIR_SEPARATOR_STR, sfn,
863 GNUNET_FS_DIRECTORY_EXT);
867 GNUNET_asprintf (&full_name, "%s%s%s", dn, DIR_SEPARATOR_STR, sfn);
872 if ((full_name != NULL) &&
873 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (full_name)))
875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
877 ("Failed to create directory for recursive download of `%s'\n"),
879 GNUNET_free (full_name);
880 GNUNET_free_non_null (fn);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Triggering recursive download of size %llu with %u bytes MD\n",
888 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
890 GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
892 GNUNET_FS_download_start (dc->h, uri, meta, full_name, temp_name, 0,
893 GNUNET_FS_uri_chk_get_file_size (uri),
894 dc->anonymity, dc->options, NULL, dc);
895 GNUNET_free_non_null (full_name);
896 GNUNET_free_non_null (temp_name);
897 GNUNET_free_non_null (fn);
902 * (recursively) free download request structure
904 * @param dr request to free
907 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
913 for (i = 0; i < dr->num_children; i++)
914 GNUNET_FS_free_download_request_ (dr->children[i]);
915 GNUNET_free_non_null (dr->children);
921 * Iterator over entries in the pending requests in the 'active' map for the
922 * reply that we just got.
924 * @param cls closure (our 'struct ProcessResultClosure')
925 * @param key query for the given value / request
926 * @param value value in the hash map (a 'struct DownloadRequest')
927 * @return GNUNET_YES (we should continue to iterate); unless serious error
930 process_result_with_request (void *cls, const GNUNET_HashCode * key,
933 struct ProcessResultClosure *prc = cls;
934 struct DownloadRequest *dr = value;
935 struct GNUNET_FS_DownloadContext *dc = prc->dc;
936 struct DownloadRequest *drc;
937 struct GNUNET_DISK_FileHandle *fh = NULL;
938 struct GNUNET_CRYPTO_AesSessionKey skey;
939 struct GNUNET_CRYPTO_AesInitializationVector iv;
941 struct GNUNET_FS_ProgressInfo pi;
946 struct ContentHashKey *chkarr;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Received block `%s' matching pending request at depth %u and offset %llu/%llu\n",
951 GNUNET_h2s (key), dr->depth, (unsigned long long) dr->offset,
952 (unsigned long long) GNUNET_ntohll (dc->uri->data.
956 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll
957 (dc->uri->data.chk.file_length),
958 dr->offset, dr->depth);
961 GNUNET_asprintf (&dc->emsg,
963 ("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)\n"),
964 bs, dr->depth, (unsigned long long) dr->offset,
965 (unsigned long long) GNUNET_ntohll (dc->uri->data.
968 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s", dc->emsg);
969 while (dr->parent != NULL)
971 dr->state = BRS_ERROR;
974 dr->state = BRS_ERROR;
978 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &prc->query, dr);
979 if (GNUNET_YES == dr->is_pending)
981 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
982 dr->is_pending = GNUNET_NO;
985 GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key, &skey, &iv);
986 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data, prc->size, &skey, &iv, pt))
989 dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
993 compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
994 dr->offset, dr->depth);
996 if ((GNUNET_YES == prc->do_store) &&
997 ((dc->filename != NULL) || (is_recursive_download (dc))) &&
998 ((dr->depth == dc->treedepth) ||
999 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES))))
1001 fh = GNUNET_DISK_file_open (dc->filename !=
1002 NULL ? dc->filename : dc->temp_filename,
1003 GNUNET_DISK_OPEN_READWRITE |
1004 GNUNET_DISK_OPEN_CREATE,
1005 GNUNET_DISK_PERM_USER_READ |
1006 GNUNET_DISK_PERM_USER_WRITE |
1007 GNUNET_DISK_PERM_GROUP_READ |
1008 GNUNET_DISK_PERM_OTHER_READ);
1011 GNUNET_asprintf (&dc->emsg,
1012 _("Download failed: could not open file `%s': %s\n"),
1013 dc->filename, STRERROR (errno));
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Saving decrypted block to disk at offset %llu\n",
1019 (unsigned long long) off);
1021 if ((off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)))
1023 GNUNET_asprintf (&dc->emsg,
1024 _("Failed to seek to offset %llu in file `%s': %s\n"),
1025 (unsigned long long) off, dc->filename,
1029 if (prc->size != GNUNET_DISK_file_write (fh, pt, prc->size))
1031 GNUNET_asprintf (&dc->emsg,
1033 ("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1034 (unsigned int) prc->size, (unsigned long long) off,
1035 dc->filename, STRERROR (errno));
1038 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1044 /* DBLOCK, update progress and try recursion if applicable */
1046 if (dr->offset < dc->offset)
1048 /* starting offset begins in the middle of pt,
1049 * do not count first bytes as progress */
1050 GNUNET_assert (app > (dc->offset - dr->offset));
1051 app -= (dc->offset - dr->offset);
1053 if (dr->offset + prc->size > dc->offset + dc->length)
1055 /* end of block is after relevant range,
1056 * do not count last bytes as progress */
1057 GNUNET_assert (app >
1058 (dr->offset + prc->size) - (dc->offset + dc->length));
1059 app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1061 dc->completed += app;
1063 /* do recursive download if option is set and either meta data
1064 * says it is a directory or if no meta data is given AND filename
1065 * ends in '.gnd' (top-level case) */
1066 if (is_recursive_download (dc))
1067 GNUNET_FS_directory_list_contents (prc->size, pt, off,
1068 &trigger_recursive_download, dc);
1071 GNUNET_assert (dc->completed <= dc->length);
1072 dr->state = BRS_DOWNLOAD_DOWN;
1073 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1074 pi.value.download.specifics.progress.data = pt;
1075 pi.value.download.specifics.progress.offset = dr->offset;
1076 pi.value.download.specifics.progress.data_len = prc->size;
1077 pi.value.download.specifics.progress.depth = dr->depth;
1078 pi.value.download.specifics.progress.trust_offered = 0;
1079 if (prc->last_transmission.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
1080 pi.value.download.specifics.progress.block_download_duration =
1081 GNUNET_TIME_absolute_get_duration (prc->last_transmission);
1083 pi.value.download.specifics.progress.block_download_duration.rel_value =
1084 GNUNET_TIME_UNIT_FOREVER_REL.rel_value;
1085 GNUNET_FS_download_make_status_ (&pi, dc);
1089 if (dc->completed == dc->length)
1091 /* download completed, signal */
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Download completed, truncating file to desired length %llu\n",
1095 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1098 /* truncate file to size (since we store IBlocks at the end) */
1099 if (dc->filename != NULL)
1102 truncate (dc->filename,
1103 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1104 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1107 GNUNET_assert (dr->depth == 0);
1108 check_completed (dc);
1112 /* bottom of the tree, no child downloads possible, just sync */
1113 GNUNET_FS_download_sync_ (dc);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1120 dr->depth, (unsigned long long) dr->offset);
1122 GNUNET_assert (0 == (prc->size % sizeof (struct ContentHashKey)));
1123 chkarr = (struct ContentHashKey *) pt;
1124 for (i = (prc->size / sizeof (struct ContentHashKey)) - 1; i >= 0; i--)
1126 drc = dr->children[i];
1130 drc->chk = chkarr[i];
1131 drc->state = BRS_CHK_SET;
1132 schedule_block_download (dc, drc);
1134 case BRS_RECONSTRUCT_DOWN:
1137 case BRS_RECONSTRUCT_META_UP:
1140 case BRS_RECONSTRUCT_UP:
1146 case BRS_DOWNLOAD_DOWN:
1149 case BRS_DOWNLOAD_UP:
1160 GNUNET_FS_download_sync_ (dc);
1165 GNUNET_DISK_file_close (fh);
1166 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1167 pi.value.download.specifics.error.message = dc->emsg;
1168 GNUNET_FS_download_make_status_ (&pi, dc);
1169 /* abort all pending requests */
1172 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1175 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1176 dc->in_receive = GNUNET_NO;
1178 GNUNET_FS_free_download_request_ (dc->top_request);
1179 dc->top_request = NULL;
1180 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1182 dc->pending_head = NULL;
1183 dc->pending_tail = NULL;
1184 GNUNET_FS_download_sync_ (dc);
1190 * Process a download result.
1192 * @param dc our download context
1193 * @param type type of the result
1194 * @param last_transmission when was this block requested the last time? (FOREVER if unknown/not applicable)
1195 * @param data the (encrypted) response
1196 * @param size size of data
1199 process_result (struct GNUNET_FS_DownloadContext *dc,
1200 enum GNUNET_BLOCK_Type type,
1201 struct GNUNET_TIME_Absolute last_transmission,
1202 const void *data, size_t size)
1204 struct ProcessResultClosure prc;
1210 prc.do_store = GNUNET_YES;
1211 prc.last_transmission = last_transmission;
1212 GNUNET_CRYPTO_hash (data, size, &prc.query);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Received result for query `%s' from `%s'-service\n",
1216 GNUNET_h2s (&prc.query), "FS");
1218 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active, &prc.query,
1219 &process_result_with_request,
1225 * Type of a function to call when we receive a message
1228 * @param cls closure
1229 * @param msg message received, NULL on timeout or fatal error
1232 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
1234 struct GNUNET_FS_DownloadContext *dc = cls;
1235 const struct ClientPutMessage *cm;
1238 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1239 (sizeof (struct ClientPutMessage) > ntohs (msg->size)))
1241 GNUNET_break (msg == NULL);
1245 msize = ntohs (msg->size);
1246 cm = (const struct ClientPutMessage *) msg;
1247 process_result (dc, ntohl (cm->type),
1248 GNUNET_TIME_absolute_ntoh (cm->last_transmission), &cm[1],
1249 msize - sizeof (struct ClientPutMessage));
1250 if (dc->client == NULL)
1251 return; /* fatal error */
1252 /* continue receiving */
1253 GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1254 GNUNET_TIME_UNIT_FOREVER_REL);
1260 * We're ready to transmit a search request to the
1261 * file-sharing service. Do it. If there is
1262 * more than one request pending, try to send
1263 * multiple or request another transmission.
1265 * @param cls closure
1266 * @param size number of bytes available in buf
1267 * @param buf where the callee should write the message
1268 * @return number of bytes written to buf
1271 transmit_download_request (void *cls, size_t size, void *buf)
1273 struct GNUNET_FS_DownloadContext *dc = cls;
1275 struct SearchMessage *sm;
1276 struct DownloadRequest *dr;
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Transmitting download request failed, trying to reconnect\n");
1288 GNUNET_assert (size >= sizeof (struct SearchMessage));
1291 while ((NULL != (dr = dc->pending_head)) &&
1292 (size >= msize + sizeof (struct SearchMessage)))
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Transmitting download request for `%s' to `%s'-service\n",
1297 GNUNET_h2s (&dr->chk.query), "FS");
1299 memset (sm, 0, sizeof (struct SearchMessage));
1300 sm->header.size = htons (sizeof (struct SearchMessage));
1301 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1302 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1303 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY);
1305 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_NONE);
1307 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
1309 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
1310 sm->anonymity_level = htonl (dc->anonymity);
1311 sm->target = dc->target.hashPubKey;
1312 sm->query = dr->chk.query;
1313 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1314 dr->is_pending = GNUNET_NO;
1315 msize += sizeof (struct SearchMessage);
1318 if (dc->pending_head != NULL)
1321 GNUNET_CLIENT_notify_transmit_ready (dc->client,
1322 sizeof (struct SearchMessage),
1323 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1325 &transmit_download_request, dc);
1326 GNUNET_assert (dc->th != NULL);
1328 if (GNUNET_NO == dc->in_receive)
1330 dc->in_receive = GNUNET_YES;
1331 GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1332 GNUNET_TIME_UNIT_FOREVER_REL);
1339 * Reconnect to the FS service and transmit our queries NOW.
1341 * @param cls our download context
1345 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1347 struct GNUNET_FS_DownloadContext *dc = cls;
1348 struct GNUNET_CLIENT_Connection *client;
1350 dc->task = GNUNET_SCHEDULER_NO_TASK;
1351 client = GNUNET_CLIENT_connect ("fs", dc->h->cfg);
1354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1355 "Connecting to `%s'-service failed, will try again.\n", "FS");
1359 dc->client = client;
1360 if (dc->pending_head != NULL)
1363 GNUNET_CLIENT_notify_transmit_ready (client,
1364 sizeof (struct SearchMessage),
1365 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1367 &transmit_download_request, dc);
1368 GNUNET_assert (dc->th != NULL);
1374 * Add entries to the pending list.
1376 * @param cls our download context
1378 * @param entry entry of type "struct DownloadRequest"
1382 retry_entry (void *cls, const GNUNET_HashCode * key, void *entry)
1384 struct GNUNET_FS_DownloadContext *dc = cls;
1385 struct DownloadRequest *dr = entry;
1389 GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
1390 dr->is_pending = GNUNET_YES;
1396 * We've lost our connection with the FS service.
1397 * Re-establish it and re-transmit all of our
1400 * @param dc download context that is having trouble
1403 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1406 if (NULL != dc->client)
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "Moving all requests back to pending list\n");
1414 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1417 /* full reset of the pending list */
1418 dc->pending_head = NULL;
1419 dc->pending_tail = NULL;
1420 GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1421 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1422 dc->in_receive = GNUNET_NO;
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will try to reconnect in 1s\n");
1429 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1435 * We're allowed to ask the FS service for our blocks. Start the download.
1437 * @param cls the 'struct GNUNET_FS_DownloadContext'
1438 * @param client handle to use for communcation with FS (we must destroy it!)
1441 activate_fs_download (void *cls, struct GNUNET_CLIENT_Connection *client)
1443 struct GNUNET_FS_DownloadContext *dc = cls;
1444 struct GNUNET_FS_ProgressInfo pi;
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download activated\n");
1449 GNUNET_assert (NULL != client);
1450 GNUNET_assert (dc->client == NULL);
1451 GNUNET_assert (dc->th == NULL);
1452 dc->client = client;
1453 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1454 GNUNET_FS_download_make_status_ (&pi, dc);
1455 dc->pending_head = NULL;
1456 dc->pending_tail = NULL;
1457 GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Asking for transmission to FS service\n");
1462 if (dc->pending_head != NULL)
1465 GNUNET_CLIENT_notify_transmit_ready (dc->client,
1466 sizeof (struct SearchMessage),
1467 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1469 &transmit_download_request, dc);
1470 GNUNET_assert (dc->th != NULL);
1476 * We must stop to ask the FS service for our blocks. Pause the download.
1478 * @param cls the 'struct GNUNET_FS_DownloadContext'
1481 deactivate_fs_download (void *cls)
1483 struct GNUNET_FS_DownloadContext *dc = cls;
1484 struct GNUNET_FS_ProgressInfo pi;
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download deactivated\n");
1491 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1494 if (NULL != dc->client)
1496 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1497 dc->in_receive = GNUNET_NO;
1500 dc->pending_head = NULL;
1501 dc->pending_tail = NULL;
1502 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1503 GNUNET_FS_download_make_status_ (&pi, dc);
1508 * (recursively) Create a download request structure.
1510 * @param parent parent of the current entry
1511 * @param depth depth of the current entry, 0 are the DBLOCKs,
1512 * top level block is 'dc->treedepth - 1'
1513 * @param dr_offset offset in the original file this block maps to
1514 * (as in, offset of the first byte of the first DBLOCK
1515 * in the subtree rooted in the returned download request tree)
1516 * @param file_start_offset desired starting offset for the download
1517 * in the original file; requesting tree should not contain
1518 * DBLOCKs prior to the file_start_offset
1519 * @param desired_length desired number of bytes the user wanted to access
1520 * (from file_start_offset). Resulting tree should not contain
1521 * DBLOCKs after file_start_offset + file_length.
1522 * @return download request tree for the given range of DBLOCKs at
1523 * the specified depth
1525 static struct DownloadRequest *
1526 create_download_request (struct DownloadRequest *parent, unsigned int depth,
1527 uint64_t dr_offset, uint64_t file_start_offset,
1528 uint64_t desired_length)
1530 struct DownloadRequest *dr;
1532 unsigned int head_skip;
1533 uint64_t child_block_size;
1535 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1536 dr->parent = parent;
1538 dr->offset = dr_offset;
1541 child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1543 /* calculate how many blocks at this level are not interesting
1544 * from the start (rounded down), either because of the requested
1545 * file offset or because this IBlock is further along */
1546 if (dr_offset < file_start_offset)
1547 head_skip = file_start_offset / child_block_size;
1549 head_skip = dr_offset / child_block_size;
1551 /* calculate index of last block at this level that is interesting (rounded up) */
1552 dr->num_children = file_start_offset + desired_length / child_block_size;
1553 if (dr->num_children * child_block_size <
1554 file_start_offset + desired_length)
1555 dr->num_children++; /* round up */
1557 /* now we can get the total number of children for this block */
1558 dr->num_children -= head_skip;
1559 if (dr->num_children > CHK_PER_INODE)
1560 dr->num_children = CHK_PER_INODE; /* cap at max */
1562 /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1563 GNUNET_assert (dr->num_children > 0);
1566 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1567 for (i = 0; i < dr->num_children; i++)
1569 create_download_request (dr, depth - 1,
1570 dr_offset + i * child_block_size,
1571 file_start_offset, desired_length);
1578 * Continuation after a possible attempt to reconstruct
1579 * the current IBlock from the existing file.
1581 * @param cls the 'struct ReconstructContext'
1582 * @param tc scheduler context
1585 reconstruct_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1587 struct GNUNET_FS_DownloadContext *dc = cls;
1589 /* clean up state from tree encoder */
1592 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1595 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1597 GNUNET_SCHEDULER_cancel (dc->task);
1598 dc->task = GNUNET_SCHEDULER_NO_TASK;
1600 if (dc->rfh != NULL)
1602 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1605 /* start "normal" download */
1606 schedule_block_download (dc, dc->top_request);
1611 * Task requesting the next block from the tree encoder.
1613 * @param cls the 'struct GNUJNET_FS_DownloadContext' we're processing
1614 * @param tc task context
1617 get_next_block (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 struct GNUNET_FS_DownloadContext *dc = cls;
1621 dc->task = GNUNET_SCHEDULER_NO_TASK;
1622 GNUNET_FS_tree_encoder_next (dc->te);
1628 * Function called asking for the current (encoded)
1629 * block to be processed. After processing the
1630 * client should either call "GNUNET_FS_tree_encode_next"
1631 * or (on error) "GNUNET_FS_tree_encode_finish".
1633 * This function checks if the content on disk matches
1634 * the expected content based on the URI.
1636 * @param cls closure
1637 * @param chk content hash key for the block
1638 * @param offset offset of the block
1639 * @param depth depth of the block, 0 for DBLOCK
1640 * @param type type of the block (IBLOCK or DBLOCK)
1641 * @param block the (encrypted) block
1642 * @param block_size size of block (in bytes)
1645 reconstruct_cb (void *cls, const struct ContentHashKey *chk, uint64_t offset,
1646 unsigned int depth, enum GNUNET_BLOCK_Type type,
1647 const void *block, uint16_t block_size)
1649 struct GNUNET_FS_DownloadContext *dc = cls;
1650 struct GNUNET_FS_ProgressInfo pi;
1651 struct DownloadRequest *dr;
1655 /* find corresponding request entry */
1656 dr = dc->top_request;
1657 while (dr->depth > depth)
1659 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1660 chld = (offset - dr->offset) / blen;
1661 GNUNET_assert (chld < dr->num_children);
1662 dr = dr->children[chld];
1664 /* FIXME: this code needs more testing and might
1665 need to handle more states... */
1670 case BRS_RECONSTRUCT_DOWN:
1672 case BRS_RECONSTRUCT_META_UP:
1674 case BRS_RECONSTRUCT_UP:
1677 if (0 == memcmp (chk, &dr->chk, sizeof (struct ContentHashKey)))
1679 /* block matches, hence tree below matches;
1680 * this request is done! */
1681 dr->state = BRS_DOWNLOAD_UP;
1682 GNUNET_break (GNUNET_NO ==
1683 GNUNET_CONTAINER_multihashmap_remove (dc->active, &dr->chk.query, dr));
1684 if (GNUNET_YES == dr->is_pending)
1686 GNUNET_break (0); /* how did we get here? */
1687 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1688 dr->is_pending = GNUNET_NO;
1690 /* calculate how many bytes of payload this block
1692 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1693 /* how many of those bytes are in the requested range? */
1694 blen = GNUNET_MIN (blen, dc->length + dc->offset - dr->offset);
1695 /* signal progress */
1696 dc->completed += blen;
1697 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1698 pi.value.download.specifics.progress.data = NULL;
1699 pi.value.download.specifics.progress.offset = offset;
1700 pi.value.download.specifics.progress.data_len = 0;
1701 pi.value.download.specifics.progress.depth = 0;
1702 pi.value.download.specifics.progress.trust_offered = 0;
1703 GNUNET_FS_download_make_status_ (&pi, dc);
1704 /* FIXME: duplicated code from 'process_result_with_request - refactor */
1705 if (dc->completed == dc->length)
1707 /* download completed, signal */
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Download completed, truncating file to desired length %llu\n",
1711 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1714 /* truncate file to size (since we store IBlocks at the end) */
1715 if (dc->filename != NULL)
1718 truncate (dc->filename,
1719 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1720 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1726 case BRS_DOWNLOAD_DOWN:
1728 case BRS_DOWNLOAD_UP:
1736 if ((dr == dc->top_request) && (dr->state == BRS_DOWNLOAD_UP))
1738 check_completed (dc);
1741 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1746 * Function called by the tree encoder to obtain a block of plaintext
1747 * data (for the lowest level of the tree).
1749 * @param cls our 'struct ReconstructContext'
1750 * @param offset identifies which block to get
1751 * @param max (maximum) number of bytes to get; returning
1752 * fewer will also cause errors
1753 * @param buf where to copy the plaintext buffer
1754 * @param emsg location to store an error message (on error)
1755 * @return number of bytes copied to buf, 0 on error
1758 fh_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
1760 struct GNUNET_FS_DownloadContext *dc = cls;
1761 struct GNUNET_DISK_FileHandle *fh = dc->rfh;
1765 if (offset != GNUNET_DISK_file_seek (fh, offset, GNUNET_DISK_SEEK_SET))
1767 *emsg = GNUNET_strdup (strerror (errno));
1770 ret = GNUNET_DISK_file_read (fh, buf, max);
1773 *emsg = GNUNET_strdup (strerror (errno));
1781 * Task that creates the initial (top-level) download
1782 * request for the file.
1784 * @param cls the 'struct GNUNET_FS_DownloadContext'
1785 * @param tc scheduler context
1788 GNUNET_FS_download_start_task_ (void *cls,
1789 const struct GNUNET_SCHEDULER_TaskContext *tc)
1791 struct GNUNET_FS_DownloadContext *dc = cls;
1792 struct GNUNET_FS_ProgressInfo pi;
1793 struct GNUNET_DISK_FileHandle *fh;
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start task running...\n");
1798 dc->task = GNUNET_SCHEDULER_NO_TASK;
1799 if (dc->length == 0)
1801 /* no bytes required! */
1802 if (dc->filename != NULL)
1804 fh = GNUNET_DISK_file_open (dc->filename,
1805 GNUNET_DISK_OPEN_READWRITE |
1806 GNUNET_DISK_OPEN_CREATE |
1808 GNUNET_FS_uri_chk_get_file_size (dc->uri)) ?
1809 GNUNET_DISK_OPEN_TRUNCATE : 0),
1810 GNUNET_DISK_PERM_USER_READ |
1811 GNUNET_DISK_PERM_USER_WRITE |
1812 GNUNET_DISK_PERM_GROUP_READ |
1813 GNUNET_DISK_PERM_OTHER_READ);
1814 GNUNET_DISK_file_close (fh);
1816 GNUNET_FS_download_sync_ (dc);
1817 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1818 pi.value.download.specifics.start.meta = dc->meta;
1819 GNUNET_FS_download_make_status_ (&pi, dc);
1820 check_completed (dc);
1823 if (dc->emsg != NULL)
1825 if (dc->top_request == NULL)
1828 create_download_request (NULL, dc->treedepth - 1, 0, dc->offset,
1830 dc->top_request->state = BRS_CHK_SET;
1831 dc->top_request->chk =
1833 chk) ? dc->uri->data.chk.chk : dc->uri->data.loc.fi.chk;
1835 GNUNET_FS_download_sync_ (dc);
1836 if (NULL != dc->search)
1837 GNUNET_FS_search_result_sync_ (dc->search);
1838 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1839 pi.value.download.specifics.start.meta = dc->meta;
1840 GNUNET_FS_download_make_status_ (&pi, dc);
1842 GNUNET_FS_download_start_downloading_ (dc);
1843 /* attempt reconstruction from disk */
1844 if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
1846 GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
1847 GNUNET_DISK_PERM_NONE);
1848 if (dc->top_request->state == BRS_CHK_SET)
1850 if (dc->rfh != NULL)
1852 /* first, try top-down */
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 "Trying top-down reconstruction for `%s'\n", dc->filename);
1857 try_top_down_reconstruction (dc, dc->top_request);
1858 switch (dc->top_request->state)
1862 case BRS_DOWNLOAD_DOWN:
1863 break; /* normal, some blocks already down */
1864 case BRS_DOWNLOAD_UP:
1865 /* already done entirely, party! */
1866 if (dc->rfh != NULL)
1868 /* avoid hanging on to file handle longer than
1870 GNUNET_DISK_file_close (dc->rfh);
1875 GNUNET_asprintf (&dc->emsg, _("Invalid URI"));
1876 GNUNET_FS_download_sync_ (dc);
1877 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1878 pi.value.download.specifics.error.message = dc->emsg;
1879 GNUNET_FS_download_make_status_ (&pi, dc);
1887 /* attempt reconstruction from meta data */
1888 if ((GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1893 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
1894 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
1896 GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
1898 GNUNET_CONTAINER_meta_data_iterate (dc->meta, &match_full_data, dc);
1899 if (dc->top_request->state == BRS_DOWNLOAD_UP)
1901 if (dc->rfh != NULL)
1903 /* avoid hanging on to file handle longer than
1905 GNUNET_DISK_file_close (dc->rfh);
1908 return; /* finished, status update was already done for us */
1911 if (dc->rfh != NULL)
1913 /* finally, try bottom-up */
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "Trying bottom-up reconstruction of file `%s'\n", dc->filename);
1919 GNUNET_FS_tree_encoder_create (dc->h, dc->old_file_size, dc, &fh_reader,
1920 &reconstruct_cb, NULL,
1922 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1926 /* simple, top-level download */
1927 schedule_block_download (dc, dc->top_request);
1929 if (dc->top_request->state == BRS_DOWNLOAD_UP)
1930 check_completed (dc);
1935 * Create SUSPEND event for the given download operation
1936 * and then clean up our state (without stop signal).
1938 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1941 GNUNET_FS_download_signal_suspend_ (void *cls)
1943 struct GNUNET_FS_DownloadContext *dc = cls;
1944 struct GNUNET_FS_ProgressInfo pi;
1946 if (dc->top != NULL)
1947 GNUNET_FS_end_top (dc->h, dc->top);
1948 while (NULL != dc->child_head)
1949 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1950 if (dc->search != NULL)
1952 dc->search->download = NULL;
1955 if (dc->job_queue != NULL)
1957 GNUNET_FS_dequeue_ (dc->job_queue);
1958 dc->job_queue = NULL;
1960 if (dc->parent != NULL)
1961 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
1963 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1965 GNUNET_SCHEDULER_cancel (dc->task);
1966 dc->task = GNUNET_SCHEDULER_NO_TASK;
1968 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1969 GNUNET_FS_download_make_status_ (&pi, dc);
1972 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1975 if (dc->rfh != NULL)
1977 GNUNET_DISK_file_close (dc->rfh);
1980 GNUNET_FS_free_download_request_ (dc->top_request);
1981 if (dc->active != NULL)
1983 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1986 GNUNET_free_non_null (dc->filename);
1987 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1988 GNUNET_FS_uri_destroy (dc->uri);
1989 GNUNET_free_non_null (dc->temp_filename);
1990 GNUNET_free_non_null (dc->serialization);
1996 * Download parts of a file. Note that this will store
1997 * the blocks at the respective offset in the given file. Also, the
1998 * download is still using the blocking of the underlying FS
1999 * encoding. As a result, the download may *write* outside of the
2000 * given boundaries (if offset and length do not match the 32k FS
2001 * block boundaries). <p>
2003 * This function should be used to focus a download towards a
2004 * particular portion of the file (optimization), not to strictly
2005 * limit the download to exactly those bytes.
2007 * @param h handle to the file sharing subsystem
2008 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2009 * @param meta known metadata for the file (can be NULL)
2010 * @param filename where to store the file, maybe NULL (then no file is
2011 * created on disk and data must be grabbed from the callbacks)
2012 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2013 * can be NULL (in which case we will pick a name if needed); the temporary file
2014 * may already exist, in which case we will try to use the data that is there and
2015 * if it is not what is desired, will overwrite it
2016 * @param offset at what offset should we start the download (typically 0)
2017 * @param length how many bytes should be downloaded starting at offset
2018 * @param anonymity anonymity level to use for the download
2019 * @param options various options
2020 * @param cctx initial value for the client context for this download
2021 * @param parent parent download to associate this download with (use NULL
2022 * for top-level downloads; useful for manually-triggered recursive downloads)
2023 * @return context that can be used to control this download
2025 struct GNUNET_FS_DownloadContext *
2026 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2027 const struct GNUNET_FS_Uri *uri,
2028 const struct GNUNET_CONTAINER_MetaData *meta,
2029 const char *filename, const char *tempname,
2030 uint64_t offset, uint64_t length, uint32_t anonymity,
2031 enum GNUNET_FS_DownloadOptions options, void *cctx,
2032 struct GNUNET_FS_DownloadContext *parent)
2034 struct GNUNET_FS_DownloadContext *dc;
2036 GNUNET_assert (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri));
2038 if ((offset + length < offset) ||
2039 (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)))
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting download `%s' of %llu bytes\n",
2046 filename, (unsigned long long) length);
2048 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2050 dc->parent = parent;
2053 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2055 dc->uri = GNUNET_FS_uri_dup (uri);
2056 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
2057 dc->client_info = cctx;
2058 dc->start_time = GNUNET_TIME_absolute_get ();
2059 if (NULL != filename)
2061 dc->filename = GNUNET_strdup (filename);
2062 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2063 GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES);
2065 if (GNUNET_FS_uri_test_loc (dc->uri))
2066 GNUNET_assert (GNUNET_OK ==
2067 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2068 dc->offset = offset;
2069 dc->length = length;
2070 dc->anonymity = anonymity;
2071 dc->options = options;
2073 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2075 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2076 if ((filename == NULL) && (is_recursive_download (dc)))
2078 if (tempname != NULL)
2079 dc->temp_filename = GNUNET_strdup (tempname);
2081 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download tree has depth %u\n",
2091 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2093 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2099 * Download parts of a file based on a search result. The download
2100 * will be associated with the search result (and the association
2101 * will be preserved when serializing/deserializing the state).
2102 * If the search is stopped, the download will not be aborted but
2103 * be 'promoted' to a stand-alone download.
2105 * As with the other download function, this will store
2106 * the blocks at the respective offset in the given file. Also, the
2107 * download is still using the blocking of the underlying FS
2108 * encoding. As a result, the download may *write* outside of the
2109 * given boundaries (if offset and length do not match the 32k FS
2110 * block boundaries). <p>
2112 * The given range can be used to focus a download towards a
2113 * particular portion of the file (optimization), not to strictly
2114 * limit the download to exactly those bytes.
2116 * @param h handle to the file sharing subsystem
2117 * @param sr the search result to use for the download (determines uri and
2118 * meta data and associations)
2119 * @param filename where to store the file, maybe NULL (then no file is
2120 * created on disk and data must be grabbed from the callbacks)
2121 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2122 * can be NULL (in which case we will pick a name if needed); the temporary file
2123 * may already exist, in which case we will try to use the data that is there and
2124 * if it is not what is desired, will overwrite it
2125 * @param offset at what offset should we start the download (typically 0)
2126 * @param length how many bytes should be downloaded starting at offset
2127 * @param anonymity anonymity level to use for the download
2128 * @param options various download options
2129 * @param cctx initial value for the client context for this download
2130 * @return context that can be used to control this download
2132 struct GNUNET_FS_DownloadContext *
2133 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2134 struct GNUNET_FS_SearchResult *sr,
2135 const char *filename,
2136 const char *tempname, uint64_t offset,
2137 uint64_t length, uint32_t anonymity,
2138 enum GNUNET_FS_DownloadOptions options,
2141 struct GNUNET_FS_DownloadContext *dc;
2143 if ((sr == NULL) || (sr->download != NULL))
2148 GNUNET_assert (GNUNET_FS_uri_test_chk (sr->uri) ||
2149 GNUNET_FS_uri_test_loc (sr->uri));
2150 if ((offset + length < offset) ||
2151 (offset + length > sr->uri->data.chk.file_length))
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting download `%s' of %llu bytes\n",
2158 filename, (unsigned long long) length);
2160 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2164 if (sr->probe_ctx != NULL)
2166 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2167 sr->probe_ctx = NULL;
2169 dc->uri = GNUNET_FS_uri_dup (sr->uri);
2170 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (sr->meta);
2171 dc->client_info = cctx;
2172 dc->start_time = GNUNET_TIME_absolute_get ();
2173 if (NULL != filename)
2175 dc->filename = GNUNET_strdup (filename);
2176 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2177 GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES);
2179 if (GNUNET_FS_uri_test_loc (dc->uri))
2180 GNUNET_assert (GNUNET_OK ==
2181 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2182 dc->offset = offset;
2183 dc->length = length;
2184 dc->anonymity = anonymity;
2185 dc->options = options;
2187 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2189 GNUNET_FS_compute_depth (GNUNET_ntohll (dc->uri->data.chk.file_length));
2190 if ((filename == NULL) && (is_recursive_download (dc)))
2192 if (tempname != NULL)
2193 dc->temp_filename = GNUNET_strdup (tempname);
2195 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download tree has depth %u\n",
2202 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2208 * Start the downloading process (by entering the queue).
2210 * @param dc our download context
2213 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2215 if (dc->completed == dc->length)
2217 GNUNET_assert (dc->job_queue == NULL);
2219 GNUNET_FS_queue_ (dc->h, &activate_fs_download, &deactivate_fs_download,
2220 dc, (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE);
2225 * Stop a download (aborts if download is incomplete).
2227 * @param dc handle for the download
2228 * @param do_delete delete files of incomplete downloads
2231 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc, int do_delete)
2233 struct GNUNET_FS_ProgressInfo pi;
2236 if (dc->top != NULL)
2237 GNUNET_FS_end_top (dc->h, dc->top);
2240 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2242 GNUNET_SCHEDULER_cancel (dc->task);
2243 dc->task = GNUNET_SCHEDULER_NO_TASK;
2245 if (dc->search != NULL)
2247 dc->search->download = NULL;
2248 GNUNET_FS_search_result_sync_ (dc->search);
2251 if (dc->job_queue != NULL)
2253 GNUNET_FS_dequeue_ (dc->job_queue);
2254 dc->job_queue = NULL;
2258 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2261 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2262 while (NULL != dc->child_head)
2263 GNUNET_FS_download_stop (dc->child_head, do_delete);
2264 if (dc->parent != NULL)
2265 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
2267 if (dc->serialization != NULL)
2268 GNUNET_FS_remove_sync_file_ (dc->h,
2269 ((dc->parent != NULL) ||
2271 NULL)) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2272 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2274 if ((GNUNET_YES == have_children) && (dc->parent == NULL))
2275 GNUNET_FS_remove_sync_dir_ (dc->h,
2277 NULL) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2278 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2280 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2281 GNUNET_FS_download_make_status_ (&pi, dc);
2282 GNUNET_FS_free_download_request_ (dc->top_request);
2283 dc->top_request = NULL;
2284 if (dc->active != NULL)
2286 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2289 if (dc->filename != NULL)
2291 if ((dc->completed != dc->length) && (GNUNET_YES == do_delete))
2293 if (0 != UNLINK (dc->filename))
2294 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2297 GNUNET_free (dc->filename);
2299 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2300 GNUNET_FS_uri_destroy (dc->uri);
2301 if (NULL != dc->temp_filename)
2303 if (0 != UNLINK (dc->temp_filename))
2304 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
2306 GNUNET_free (dc->temp_filename);
2308 GNUNET_free_non_null (dc->serialization);
2312 /* end of fs_download.c */