2 This file is part of GNUnet.
3 Copyright (C) 2001-2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file fs/fs_download.c
22 * @brief download methods
23 * @author Christian Grothoff
26 #include "gnunet_constants.h"
27 #include "gnunet_fs_service.h"
33 * Determine if the given download (options and meta data) should cause
34 * use to try to do a recursive download.
37 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
39 return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
41 GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
42 ( (NULL == dc->meta) &&
43 ( (NULL == dc->filename) ||
44 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
46 strstr (dc->filename + strlen (dc->filename) -
47 strlen (GNUNET_FS_DIRECTORY_EXT),
48 GNUNET_FS_DIRECTORY_EXT)) ) ) ) );
53 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
54 * only have to truncate the file once we're done).
56 * Given the offset of a block (with respect to the DBLOCKS) and its
57 * depth, return the offset where we would store this block in the
60 * @param fsize overall file size
61 * @param off offset of the block in the file
62 * @param depth depth of the block in the tree, 0 for DBLOCK
63 * @return off for DBLOCKS (depth == treedepth),
64 * otherwise an offset past the end
65 * of the file that does not overlap
66 * with the range for any other block
69 compute_disk_offset (uint64_t fsize, uint64_t off, unsigned int depth)
72 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
73 uint64_t loff; /* where do IBlocks for depth "i" start? */
74 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
78 /* first IBlocks start at the end of file, rounded up
79 * to full DBLOCK_SIZE */
80 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
82 ((fsize + DBLOCK_SIZE -
83 1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
84 GNUNET_assert (0 == (off % DBLOCK_SIZE));
85 ioff = (off / DBLOCK_SIZE);
86 for (i = 1; i < depth; i++)
89 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
90 GNUNET_assert (lsize > 0);
91 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
92 ioff /= CHK_PER_INODE;
94 return loff + ioff * sizeof (struct ContentHashKey);
99 * Fill in all of the generic fields for a download event and call the
102 * @param pi structure to fill in
103 * @param dc overall download context
106 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
107 struct GNUNET_FS_DownloadContext *dc)
109 pi->value.download.dc = dc;
110 pi->value.download.cctx = dc->client_info;
111 pi->value.download.pctx =
112 (NULL == dc->parent) ? NULL : dc->parent->client_info;
113 pi->value.download.sctx =
114 (NULL == dc->search) ? NULL : dc->search->client_info;
115 pi->value.download.uri = dc->uri;
116 pi->value.download.filename = dc->filename;
117 pi->value.download.size = dc->length;
118 /* FIXME: Fix duration calculation to account for pauses */
119 pi->value.download.duration =
120 GNUNET_TIME_absolute_get_duration (dc->start_time);
121 pi->value.download.completed = dc->completed;
122 pi->value.download.anonymity = dc->anonymity;
123 pi->value.download.eta =
124 GNUNET_TIME_calculate_eta (dc->start_time, dc->completed, dc->length);
125 pi->value.download.is_active = (NULL == dc->mq) ? GNUNET_NO : GNUNET_YES;
127 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
128 dc->client_info = dc->h->upcb (dc->h->upcb_cls, pi);
130 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL, pi);
135 * Closure for iterator processing results.
137 struct ProcessResultClosure
143 struct GNUNET_HashCode query;
146 * Data found in P2P network.
151 * Our download context.
153 struct GNUNET_FS_DownloadContext *dc;
156 * When did we last transmit the request?
158 struct GNUNET_TIME_Absolute last_transmission;
161 * Number of bytes in data.
168 enum GNUNET_BLOCK_Type type;
171 * Flag to indicate if this block should be stored on disk.
176 * how much respect did we offer to get this reply?
178 uint32_t respect_offered;
181 * how often did we transmit the query?
183 uint32_t num_transmissions;
189 * Iterator over entries in the pending requests in the 'active' map for the
190 * reply that we just got.
192 * @param cls closure (our 'struct ProcessResultClosure')
193 * @param key query for the given value / request
194 * @param value value in the hash map (a 'struct DownloadRequest')
195 * @return #GNUNET_YES (we should continue to iterate); unless serious error
198 process_result_with_request (void *cls,
199 const struct GNUNET_HashCode * key,
204 * We've found a matching block without downloading it.
205 * Encrypt it and pass it to our "receive" function as
206 * if we had received it from the network.
208 * @param dc download in question
209 * @param chk request this relates to
210 * @param dr request details
211 * @param block plaintext data matching request
212 * @param len number of bytes in block
213 * @param do_store should we still store the block on disk?
214 * @return GNUNET_OK on success
217 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
218 const struct ContentHashKey *chk,
219 struct DownloadRequest *dr, const char *block,
220 size_t len, int do_store)
222 struct ProcessResultClosure prc;
224 struct GNUNET_CRYPTO_SymmetricSessionKey sk;
225 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
226 struct GNUNET_HashCode query;
228 GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
229 if (-1 == GNUNET_CRYPTO_symmetric_encrypt (block, len, &sk, &iv, enc))
232 return GNUNET_SYSERR;
234 GNUNET_CRYPTO_hash (enc, len, &query);
235 if (0 != memcmp (&query, &chk->query, sizeof (struct GNUNET_HashCode)))
238 return GNUNET_SYSERR;
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Matching %u byte block for `%s' at offset %llu already present, no need for download!\n",
243 dc->filename, (unsigned long long) dr->offset);
244 /* already got it! */
250 dr->depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK : GNUNET_BLOCK_TYPE_FS_IBLOCK;
251 prc.query = chk->query;
252 prc.do_store = do_store;
253 prc.last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
254 process_result_with_request (&prc, &chk->key, dr);
260 * We've lost our connection with the FS service.
261 * Re-establish it and re-transmit all of our
264 * @param dc download context that is having trouble
267 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
271 * We found an entry in a directory. Check if the respective child
272 * already exists and if not create the respective child download.
274 * @param cls the parent download
275 * @param filename name of the file in the directory
276 * @param uri URI of the file (CHK or LOC)
277 * @param meta meta data of the file
278 * @param length number of bytes in data
279 * @param data contents of the file (or NULL if they were not inlined)
282 trigger_recursive_download (void *cls,
283 const char *filename,
284 const struct GNUNET_FS_Uri *uri,
285 const struct GNUNET_CONTAINER_MetaData *meta,
291 * We're done downloading a directory. Open the file and
292 * trigger all of the (remaining) child downloads.
294 * @param dc context of download that just completed
297 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
302 struct GNUNET_DISK_FileHandle *h;
303 struct GNUNET_DISK_MapHandle *m;
305 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
306 size = (size_t) size64;
307 if (size64 != (uint64_t) size)
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
313 if (NULL != dc->filename)
315 h = GNUNET_DISK_file_open (dc->filename,
316 GNUNET_DISK_OPEN_READ,
317 GNUNET_DISK_PERM_NONE);
321 GNUNET_assert (NULL != dc->temp_filename);
322 h = GNUNET_DISK_file_open (dc->temp_filename,
323 GNUNET_DISK_OPEN_READ,
324 GNUNET_DISK_PERM_NONE);
328 data = GNUNET_DISK_file_map (h,
330 GNUNET_DISK_MAP_TYPE_READ,
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 _("Directory too large for system address space\n"));
340 GNUNET_FS_directory_list_contents (size,
343 &trigger_recursive_download,
346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
347 _("Failed to access full directroy contents of `%s' for recursive download\n"),
350 GNUNET_DISK_file_unmap (m);
352 GNUNET_DISK_file_close (h);
353 if (NULL == dc->filename)
355 if (0 != UNLINK (dc->temp_filename))
356 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
359 GNUNET_free (dc->temp_filename);
360 dc->temp_filename = NULL;
366 * Check if all child-downloads have completed (or trigger them if
367 * necessary) and once we're completely done, signal completion (and
368 * possibly recurse to parent). This function MUST be called when the
369 * download of a file itself is done or when the download of a file is
370 * done and then later a direct child download has completed (and
371 * hence this download may complete itself).
373 * @param dc download to check for completion of children
376 check_completed (struct GNUNET_FS_DownloadContext *dc)
378 struct GNUNET_FS_ProgressInfo pi;
379 struct GNUNET_FS_DownloadContext *pos;
381 /* first, check if we need to download children */
382 if (is_recursive_download (dc))
383 full_recursive_download (dc);
384 /* then, check if children are done already */
385 for (pos = dc->child_head; NULL != pos; pos = pos->next)
387 if ( (NULL == pos->emsg) &&
388 (pos->completed < pos->length) )
389 return; /* not done yet */
390 if ( (NULL != pos->child_head) &&
391 (pos->has_finished != GNUNET_YES) )
392 return; /* not transitively done yet */
394 /* All of our children are done, so mark this download done */
395 dc->has_finished = GNUNET_YES;
396 if (NULL != dc->job_queue)
398 GNUNET_FS_dequeue_ (dc->job_queue);
399 dc->job_queue = NULL;
401 if (NULL != dc->task)
403 GNUNET_SCHEDULER_cancel (dc->task);
408 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
411 GNUNET_FS_download_sync_ (dc);
413 /* signal completion */
414 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
415 GNUNET_FS_download_make_status_ (&pi, dc);
417 /* let parent know */
418 if (NULL != dc->parent)
419 check_completed (dc->parent);
424 * We got a block of plaintext data (from the meta data).
425 * Try it for upward reconstruction of the data. On success,
426 * the top-level block will move to state BRS_DOWNLOAD_UP.
428 * @param dc context for the download
429 * @param dr download request to match against
430 * @param data plaintext data, starting from the beginning of the file
431 * @param data_len number of bytes in data
434 try_match_block (struct GNUNET_FS_DownloadContext *dc,
435 struct DownloadRequest *dr, const char *data, size_t data_len)
437 struct GNUNET_FS_ProgressInfo pi;
439 char enc[DBLOCK_SIZE];
440 struct ContentHashKey chks[CHK_PER_INODE];
441 struct ContentHashKey in_chk;
442 struct GNUNET_CRYPTO_SymmetricSessionKey sk;
443 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
445 struct DownloadRequest *drc;
446 struct GNUNET_DISK_FileHandle *fh;
453 odata_len = data_len;
454 if (BRS_DOWNLOAD_UP == dr->state)
458 if ( (dc->offset > 0) ||
459 (dc->length < GNUNET_ntohll (dc->uri->data.chk.file_length)) )
461 /* NOTE: this test is not tight, but should suffice; the issue
462 here is that 'dr->num_children' may inherently only specify a
463 smaller range than what is in the original file;
464 thus, reconstruction of (some) inner blocks will fail.
465 FIXME: we might eventually want to write a tighter test to
466 maximize the circumstances under which we do succeed with
467 IBlock reconstruction. (need good tests though). */
470 complete = GNUNET_YES;
471 for (i = 0; i < dr->num_children; i++)
473 drc = dr->children[i];
474 try_match_block (dc, drc, data, data_len);
475 if (drc->state != BRS_RECONSTRUCT_META_UP)
476 complete = GNUNET_NO;
480 if (GNUNET_YES != complete)
482 data = (const char *) chks;
483 dlen = dr->num_children * sizeof (struct ContentHashKey);
487 if (dr->offset > data_len)
489 dlen = GNUNET_MIN (data_len - dr->offset, DBLOCK_SIZE);
491 GNUNET_CRYPTO_hash (&data[dr->offset], dlen, &in_chk.key);
492 GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
493 if (-1 == GNUNET_CRYPTO_symmetric_encrypt (&data[dr->offset],
502 GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
507 dr->state = BRS_RECONSTRUCT_META_UP;
510 if (0 != memcmp (&in_chk,
512 sizeof (struct ContentHashKey)))
514 /* other peer provided bogus meta data */
518 /* write block to disk */
519 fn = (NULL != dc->filename) ? dc->filename : dc->temp_filename;
522 fh = GNUNET_DISK_file_open (fn,
523 GNUNET_DISK_OPEN_READWRITE |
524 GNUNET_DISK_OPEN_CREATE |
525 GNUNET_DISK_OPEN_TRUNCATE,
526 GNUNET_DISK_PERM_USER_READ |
527 GNUNET_DISK_PERM_USER_WRITE |
528 GNUNET_DISK_PERM_GROUP_READ |
529 GNUNET_DISK_PERM_OTHER_READ);
532 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
533 GNUNET_asprintf (&dc->emsg,
534 _("Failed to open file `%s' for writing"),
536 GNUNET_DISK_file_close (fh);
537 dr->state = BRS_ERROR;
538 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
539 pi.value.download.specifics.error.message = dc->emsg;
540 GNUNET_FS_download_make_status_ (&pi, dc);
543 if (data_len != GNUNET_DISK_file_write (fh, odata, odata_len))
545 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
546 GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
548 GNUNET_DISK_file_close (fh);
549 dr->state = BRS_ERROR;
550 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
551 pi.value.download.specifics.error.message = dc->emsg;
552 GNUNET_FS_download_make_status_ (&pi, dc);
555 GNUNET_DISK_file_close (fh);
558 dr->state = BRS_DOWNLOAD_UP;
559 dc->completed = dc->length;
560 GNUNET_FS_download_sync_ (dc);
561 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
562 pi.value.download.specifics.progress.data = data;
563 pi.value.download.specifics.progress.offset = 0;
564 pi.value.download.specifics.progress.data_len = dlen;
565 pi.value.download.specifics.progress.depth = 0;
566 pi.value.download.specifics.progress.respect_offered = 0;
567 pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
568 GNUNET_FS_download_make_status_ (&pi, dc);
569 if ((NULL != dc->filename) &&
571 TRUNCATE (dc->filename,
572 GNUNET_ntohll (dc->uri->data.chk.file_length))))
573 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
575 check_completed (dc);
578 /* how did we get here? */
586 * Type of a function that libextractor calls for each
587 * meta data item found. If we find full data meta data,
588 * call 'try_match_block' on it.
590 * @param cls our 'struct GNUNET_FS_DownloadContext*'
591 * @param plugin_name name of the plugin that produced this value;
592 * special values can be used (i.e. '<zlib>' for zlib being
593 * used in the main libextractor library and yielding
595 * @param type libextractor-type describing the meta data
596 * @param format basic format information about data
597 * @param data_mime_type mime-type of data (not of the original file);
598 * can be NULL (if mime-type is not known)
599 * @param data actual meta-data found
600 * @param data_len number of bytes in data
601 * @return 0 to continue extracting, 1 to abort
604 match_full_data (void *cls, const char *plugin_name,
605 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
606 const char *data_mime_type, const char *data, size_t data_len)
608 struct GNUNET_FS_DownloadContext *dc = cls;
610 if (EXTRACTOR_METATYPE_GNUNET_FULL_DATA != type)
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u bytes of FD!\n",
613 (unsigned int) data_len);
614 if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
617 return 1; /* bogus meta data */
628 * Set the state of the given download request to
629 * BRS_DOWNLOAD_UP and propagate it up the tree.
631 * @param dr download request that is done
634 propagate_up (struct DownloadRequest *dr)
640 dr->state = BRS_DOWNLOAD_UP;
644 for (i = 0; i < dr->num_children; i++)
645 if (dr->children[i]->state != BRS_DOWNLOAD_UP)
648 while (i == dr->num_children);
653 * Try top-down reconstruction. Before, the given request node
654 * must have the state BRS_CHK_SET. Afterwards, more nodes may
655 * have that state or advanced to BRS_DOWNLOAD_DOWN or even
656 * BRS_DOWNLOAD_UP. It is also possible to get BRS_ERROR on the
659 * @param dc overall download this block belongs to
660 * @param dr block to reconstruct
663 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
664 struct DownloadRequest *dr)
667 char block[DBLOCK_SIZE];
668 struct GNUNET_HashCode key;
672 struct DownloadRequest *drc;
673 uint64_t child_block_size;
674 const struct ContentHashKey *chks;
677 GNUNET_assert (NULL != dc->rfh);
678 GNUNET_assert (BRS_CHK_SET == dr->state);
679 total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
680 GNUNET_assert (dr->depth < dc->treedepth);
681 len = GNUNET_FS_tree_calculate_block_size (total, dr->offset, dr->depth);
682 GNUNET_assert (len <= DBLOCK_SIZE);
683 off = compute_disk_offset (total, dr->offset, dr->depth);
684 if (dc->old_file_size < off + len)
685 return; /* failure */
686 if (off != GNUNET_DISK_file_seek (dc->rfh, off, GNUNET_DISK_SEEK_SET))
688 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "seek", dc->filename);
689 return; /* failure */
691 if (len != GNUNET_DISK_file_read (dc->rfh, block, len))
693 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", dc->filename);
694 return; /* failure */
696 GNUNET_CRYPTO_hash (block, len, &key);
697 if (0 != memcmp (&key, &dr->chk.key, sizeof (struct GNUNET_HashCode)))
698 return; /* mismatch */
700 encrypt_existing_match (dc, &dr->chk, dr, block, len, GNUNET_NO))
702 /* hash matches but encrypted block does not, really bad */
703 dr->state = BRS_ERROR;
705 while (NULL != dr->parent)
708 dr->state = BRS_ERROR;
713 dr->state = BRS_DOWNLOAD_DOWN;
715 /* set CHKs for children */
716 up_done = GNUNET_YES;
717 chks = (const struct ContentHashKey *) block;
718 for (i = 0; i < dr->num_children; i++)
720 drc = dr->children[i];
721 GNUNET_assert (drc->offset >= dr->offset);
722 child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
723 GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);
724 if (BRS_INIT == drc->state)
726 drc->state = BRS_CHK_SET;
727 drc->chk = chks[drc->chk_idx];
728 try_top_down_reconstruction (dc, drc);
730 if (BRS_DOWNLOAD_UP != drc->state)
731 up_done = GNUNET_NO; /* children not all done */
733 if (GNUNET_YES == up_done)
734 propagate_up (dr); /* children all done (or no children...) */
739 * Add entries to the message queue.
741 * @param cls our download context
743 * @param entry entry of type `struct DownloadRequest`
747 retry_entry (void *cls,
748 const struct GNUNET_HashCode *key,
751 struct GNUNET_FS_DownloadContext *dc = cls;
752 struct DownloadRequest *dr = entry;
753 struct SearchMessage *sm;
754 struct GNUNET_MQ_Envelope *env;
756 env = GNUNET_MQ_msg (sm,
757 GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
758 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
759 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY);
761 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_NONE);
763 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
765 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
766 sm->anonymity_level = htonl (dc->anonymity);
767 sm->target = dc->target;
768 sm->query = dr->chk.query;
769 GNUNET_MQ_send (dc->mq,
776 * Schedule the download of the specified block in the tree.
778 * @param dc overall download this block belongs to
779 * @param dr request to schedule
782 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
783 struct DownloadRequest *dr)
792 case BRS_RECONSTRUCT_DOWN:
795 case BRS_RECONSTRUCT_META_UP:
798 case BRS_RECONSTRUCT_UP:
802 /* normal case, start download */
804 case BRS_DOWNLOAD_DOWN:
805 for (i = 0; i < dr->num_children; i++)
806 schedule_block_download (dc, dr->children[i]);
808 case BRS_DOWNLOAD_UP:
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Scheduling download at offset %llu and depth %u for `%s'\n",
817 (unsigned long long) dr->offset,
819 GNUNET_h2s (&dr->chk.query));
821 GNUNET_CONTAINER_multihashmap_contains_value (dc->active,
824 return; /* already active */
825 GNUNET_CONTAINER_multihashmap_put (dc->active,
828 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
830 return; /* download not active */
837 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
840 * We found an entry in a directory. Check if the respective child
841 * already exists and if not create the respective child download.
843 * @param cls the parent download
844 * @param filename name of the file in the directory
845 * @param uri URI of the file (CHK or LOC)
846 * @param meta meta data of the file
847 * @param length number of bytes in data
848 * @param data contents of the file (or NULL if they were not inlined)
851 trigger_recursive_download (void *cls,
852 const char *filename,
853 const struct GNUNET_FS_Uri *uri,
854 const struct GNUNET_CONTAINER_MetaData *meta,
858 struct GNUNET_FS_DownloadContext *dc = cls;
859 struct GNUNET_FS_DownloadContext *cpos;
870 return; /* entry for the directory itself */
871 cpos = dc->child_head;
874 if ((GNUNET_FS_uri_test_equal (uri, cpos->uri)) ||
875 ((NULL != filename) && (0 == strcmp (cpos->filename, filename))))
880 return; /* already exists */
882 if (NULL == filename)
884 fn = GNUNET_FS_meta_data_suggest_filename (meta);
887 us = GNUNET_FS_uri_to_string (uri);
888 fn = GNUNET_strdup (&us[strlen (GNUNET_FS_URI_CHK_PREFIX)]);
891 else if ('.' == fn[0])
894 us = GNUNET_FS_uri_to_string (uri);
895 GNUNET_asprintf (&fn, "%s%s", &us[strlen (GNUNET_FS_URI_CHK_PREFIX)],
900 /* change '\' to '/' (this should have happened
901 * during insertion, but malicious peers may
902 * not have done this) */
903 while (NULL != (pos = strstr (fn, "\\")))
905 /* remove '../' everywhere (again, well-behaved
906 * peers don't do this, but don't trust that
907 * we did not get something nasty) */
908 while (NULL != (pos = strstr (fn, "../")))
916 if (NULL == dc->filename)
922 dn = GNUNET_strdup (dc->filename);
923 GNUNET_break ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
925 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
926 GNUNET_FS_DIRECTORY_EXT)));
927 sfn = GNUNET_strdup (filename);
928 while ((strlen (sfn) > 0) && ('/' == filename[strlen (sfn) - 1]))
929 sfn[strlen (sfn) - 1] = '\0';
930 if ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
932 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
933 GNUNET_FS_DIRECTORY_EXT)))
934 dn[strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
935 if ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
936 ((strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
938 strstr (filename + strlen (filename) -
939 strlen (GNUNET_FS_DIRECTORY_EXT), GNUNET_FS_DIRECTORY_EXT))))
941 GNUNET_asprintf (&full_name, "%s%s%s%s", dn, DIR_SEPARATOR_STR, sfn,
942 GNUNET_FS_DIRECTORY_EXT);
946 GNUNET_asprintf (&full_name, "%s%s%s", dn, DIR_SEPARATOR_STR, sfn);
951 if ((NULL != full_name) &&
952 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (full_name)))
954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
956 ("Failed to create directory for recursive download of `%s'\n"),
958 GNUNET_free (full_name);
959 GNUNET_free_non_null (fn);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Triggering recursive download of size %llu with %u bytes MD\n",
966 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
968 GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
969 GNUNET_FS_download_start (dc->h,
975 GNUNET_FS_uri_chk_get_file_size (uri),
980 GNUNET_free_non_null (full_name);
981 GNUNET_free_non_null (temp_name);
982 GNUNET_free_non_null (fn);
987 * (recursively) free download request structure
989 * @param dr request to free
992 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
996 for (unsigned int i = 0; i < dr->num_children; i++)
997 GNUNET_FS_free_download_request_ (dr->children[i]);
998 GNUNET_free_non_null (dr->children);
1004 * Iterator over entries in the pending requests in the 'active' map for the
1005 * reply that we just got.
1007 * @param cls closure (our `struct ProcessResultClosure`)
1008 * @param key query for the given value / request
1009 * @param value value in the hash map (a `struct DownloadRequest`)
1010 * @return #GNUNET_YES (we should continue to iterate); unless serious error
1013 process_result_with_request (void *cls,
1014 const struct GNUNET_HashCode *key,
1017 struct ProcessResultClosure *prc = cls;
1018 struct DownloadRequest *dr = value;
1019 struct GNUNET_FS_DownloadContext *dc = prc->dc;
1020 struct DownloadRequest *drc;
1021 struct GNUNET_DISK_FileHandle *fh = NULL;
1022 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
1023 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
1025 struct GNUNET_FS_ProgressInfo pi;
1030 struct ContentHashKey *chkarr;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Received %u byte block `%s' matching pending request at depth %u and offset %llu/%llu\n",
1034 (unsigned int) prc->size,
1037 (unsigned long long) dr->offset,
1038 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1040 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll
1041 (dc->uri->data.chk.file_length),
1042 dr->offset, dr->depth);
1043 if (prc->size != bs)
1045 GNUNET_asprintf (&dc->emsg,
1047 ("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)"),
1048 bs, dr->depth, (unsigned long long) dr->offset,
1049 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1052 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", dc->emsg);
1053 while (NULL != dr->parent)
1055 dr->state = BRS_ERROR;
1058 dr->state = BRS_ERROR;
1062 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active,
1065 GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key,
1068 if (-1 == GNUNET_CRYPTO_symmetric_decrypt (prc->data,
1075 dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
1079 compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
1083 if ((GNUNET_YES == prc->do_store) &&
1084 ((NULL != dc->filename) || (is_recursive_download (dc))) &&
1085 ((dr->depth == dc->treedepth) ||
1086 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES))))
1088 fh = GNUNET_DISK_file_open (NULL != dc->filename
1089 ? dc->filename : dc->temp_filename,
1090 GNUNET_DISK_OPEN_READWRITE |
1091 GNUNET_DISK_OPEN_CREATE,
1092 GNUNET_DISK_PERM_USER_READ |
1093 GNUNET_DISK_PERM_USER_WRITE |
1094 GNUNET_DISK_PERM_GROUP_READ |
1095 GNUNET_DISK_PERM_OTHER_READ);
1098 GNUNET_asprintf (&dc->emsg,
1099 _("Download failed: could not open file `%s': %s"),
1100 dc->filename, STRERROR (errno));
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Saving decrypted block to disk at offset %llu\n",
1105 (unsigned long long) off);
1106 if ((off != GNUNET_DISK_file_seek (fh,
1108 GNUNET_DISK_SEEK_SET)))
1110 GNUNET_asprintf (&dc->emsg,
1111 _("Failed to seek to offset %llu in file `%s': %s"),
1112 (unsigned long long) off,
1117 if (prc->size != GNUNET_DISK_file_write (fh, pt, prc->size))
1119 GNUNET_asprintf (&dc->emsg,
1120 _("Failed to write block of %u bytes at offset %llu in file `%s': %s"),
1121 (unsigned int) prc->size,
1122 (unsigned long long) off,
1127 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1133 /* DBLOCK, update progress and try recursion if applicable */
1135 if (dr->offset < dc->offset)
1137 /* starting offset begins in the middle of pt,
1138 * do not count first bytes as progress */
1139 GNUNET_assert (app > (dc->offset - dr->offset));
1140 app -= (dc->offset - dr->offset);
1142 if (dr->offset + prc->size > dc->offset + dc->length)
1144 /* end of block is after relevant range,
1145 * do not count last bytes as progress */
1146 GNUNET_assert (app >
1147 (dr->offset + prc->size) - (dc->offset + dc->length));
1148 app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1150 dc->completed += app;
1152 /* do recursive download if option is set and either meta data
1153 * says it is a directory or if no meta data is given AND filename
1154 * ends in '.gnd' (top-level case) */
1155 if (is_recursive_download (dc))
1156 GNUNET_FS_directory_list_contents (prc->size, pt, off,
1157 &trigger_recursive_download, dc);
1159 GNUNET_assert (dc->completed <= dc->length);
1160 dr->state = BRS_DOWNLOAD_DOWN;
1161 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1162 pi.value.download.specifics.progress.data = pt;
1163 pi.value.download.specifics.progress.offset = dr->offset;
1164 pi.value.download.specifics.progress.data_len = prc->size;
1165 pi.value.download.specifics.progress.depth = dr->depth;
1166 pi.value.download.specifics.progress.respect_offered = prc->respect_offered;
1167 pi.value.download.specifics.progress.num_transmissions = prc->num_transmissions;
1168 if (prc->last_transmission.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
1169 pi.value.download.specifics.progress.block_download_duration
1170 = GNUNET_TIME_absolute_get_duration (prc->last_transmission);
1172 pi.value.download.specifics.progress.block_download_duration
1173 = GNUNET_TIME_UNIT_ZERO; /* found locally */
1174 GNUNET_FS_download_make_status_ (&pi, dc);
1178 if (dc->completed == dc->length)
1180 /* download completed, signal */
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Download completed, truncating file to desired length %llu\n",
1183 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1185 /* truncate file to size (since we store IBlocks at the end) */
1186 if (NULL != dc->filename)
1189 TRUNCATE (dc->filename,
1190 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1191 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1194 GNUNET_assert (0 == dr->depth);
1195 check_completed (dc);
1199 /* bottom of the tree, no child downloads possible, just sync */
1200 GNUNET_FS_download_sync_ (dc);
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1206 dr->depth, (unsigned long long) dr->offset);
1207 GNUNET_assert (0 == (prc->size % sizeof (struct ContentHashKey)));
1208 chkarr = (struct ContentHashKey *) pt;
1209 for (i = dr->num_children - 1; i >= 0; i--)
1211 drc = dr->children[i];
1215 if ((drc->chk_idx + 1) * sizeof (struct ContentHashKey) > prc->size)
1217 /* 'chkarr' does not have enough space for this chk_idx;
1219 GNUNET_break (0); GNUNET_assert (0);
1220 dc->emsg = GNUNET_strdup (_("internal error decoding tree"));
1223 drc->chk = chkarr[drc->chk_idx];
1224 drc->state = BRS_CHK_SET;
1225 if (GNUNET_YES == dc->issue_requests)
1226 schedule_block_download (dc, drc);
1228 case BRS_RECONSTRUCT_DOWN:
1231 case BRS_RECONSTRUCT_META_UP:
1234 case BRS_RECONSTRUCT_UP:
1240 case BRS_DOWNLOAD_DOWN:
1243 case BRS_DOWNLOAD_UP:
1254 GNUNET_FS_download_sync_ (dc);
1259 GNUNET_DISK_file_close (fh);
1260 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1261 pi.value.download.specifics.error.message = dc->emsg;
1262 GNUNET_FS_download_make_status_ (&pi, dc);
1263 GNUNET_MQ_destroy (dc->mq);
1265 GNUNET_FS_free_download_request_ (dc->top_request);
1266 dc->top_request = NULL;
1267 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1269 if (NULL != dc->job_queue)
1271 GNUNET_FS_dequeue_ (dc->job_queue);
1272 dc->job_queue = NULL;
1274 GNUNET_FS_download_sync_ (dc);
1280 * Type of a function to call when we check the PUT message
1283 * @param cls closure
1284 * @param msg message received
1287 check_put (void *cls,
1288 const struct ClientPutMessage *cm)
1290 /* any varsize length is OK */
1296 * Type of a function to call when we receive a message
1299 * @param cls closure
1300 * @param msg message received
1303 handle_put (void *cls,
1304 const struct ClientPutMessage *cm)
1306 struct GNUNET_FS_DownloadContext *dc = cls;
1307 uint16_t msize = ntohs (cm->header.size) - sizeof (*cm);
1308 struct ProcessResultClosure prc;
1312 prc.last_transmission = GNUNET_TIME_absolute_ntoh (cm->last_transmission);
1314 prc.type = ntohl (cm->type);
1315 prc.do_store = GNUNET_YES;
1316 prc.respect_offered = ntohl (cm->respect_offered);
1317 prc.num_transmissions = ntohl (cm->num_transmissions);
1318 GNUNET_CRYPTO_hash (prc.data,
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Received result for query `%s' from FS service\n",
1323 GNUNET_h2s (&prc.query));
1324 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1326 &process_result_with_request,
1332 * Generic error handler, called with the appropriate error code and
1333 * the same closure specified at the creation of the message queue.
1334 * Not every message queue implementation supports an error handler.
1336 * @param cls closure with the `struct GNUNET_FS_DownloadContext *`
1337 * @param error error code
1340 download_mq_error_handler (void *cls,
1341 enum GNUNET_MQ_Error error)
1343 struct GNUNET_FS_DownloadContext *dc = cls;
1347 GNUNET_MQ_destroy (dc->mq);
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Transmitting download request failed, trying to reconnect\n");
1357 * Reconnect to the FS service and transmit our queries NOW.
1359 * @param cls our download context
1362 do_reconnect (void *cls)
1364 struct GNUNET_FS_DownloadContext *dc = cls;
1365 struct GNUNET_MQ_MessageHandler handlers[] = {
1366 GNUNET_MQ_hd_var_size (put,
1367 GNUNET_MESSAGE_TYPE_FS_PUT,
1368 struct ClientPutMessage,
1370 GNUNET_MQ_handler_end ()
1374 dc->mq = GNUNET_CLIENT_connect (dc->h->cfg,
1377 &download_mq_error_handler,
1381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1382 "Connecting to `%s'-service failed, will try again.\n", "FS");
1386 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1393 * We've lost our connection with the FS service.
1394 * Re-establish it and re-transmit all of our
1397 * @param dc download context that is having trouble
1400 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Moving all requests back to pending list\n");
1406 GNUNET_MQ_destroy (dc->mq);
1409 if (0 == dc->reconnect_backoff.rel_value_us)
1410 dc->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1412 dc->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (dc->reconnect_backoff);
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "Will try to reconnect in %s\n",
1416 GNUNET_STRINGS_relative_time_to_string (dc->reconnect_backoff,
1418 GNUNET_break (NULL != dc->job_queue);
1420 GNUNET_SCHEDULER_add_delayed (dc->reconnect_backoff,
1427 * We're allowed to ask the FS service for our blocks. Start the download.
1429 * @param cls the 'struct GNUNET_FS_DownloadContext'
1430 * @param mq handle to use for communcation with FS (we must destroy it!)
1433 activate_fs_download (void *cls)
1435 struct GNUNET_FS_DownloadContext *dc = cls;
1436 struct GNUNET_FS_ProgressInfo pi;
1438 GNUNET_assert (NULL == dc->mq);
1439 GNUNET_assert (NULL != dc->active);
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Download activated\n");
1444 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1445 GNUNET_FS_download_make_status_ (&pi,
1451 * We must stop to ask the FS service for our blocks. Pause the download.
1453 * @param cls the `struct GNUNET_FS_DownloadContext`
1456 deactivate_fs_download (void *cls)
1458 struct GNUNET_FS_DownloadContext *dc = cls;
1459 struct GNUNET_FS_ProgressInfo pi;
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Download deactivated\n");
1465 GNUNET_MQ_destroy (dc->mq);
1468 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1469 GNUNET_FS_download_make_status_ (&pi,
1475 * (recursively) Create a download request structure.
1477 * @param parent parent of the current entry
1478 * @param chk_idx index of the chk for this block in the parent block
1479 * @param depth depth of the current entry, 0 are the DBLOCKs,
1480 * top level block is 'dc->treedepth - 1'
1481 * @param dr_offset offset in the original file this block maps to
1482 * (as in, offset of the first byte of the first DBLOCK
1483 * in the subtree rooted in the returned download request tree)
1484 * @param file_start_offset desired starting offset for the download
1485 * in the original file; requesting tree should not contain
1486 * DBLOCKs prior to the file_start_offset
1487 * @param desired_length desired number of bytes the user wanted to access
1488 * (from file_start_offset). Resulting tree should not contain
1489 * DBLOCKs after file_start_offset + file_length.
1490 * @return download request tree for the given range of DBLOCKs at
1491 * the specified depth
1493 static struct DownloadRequest *
1494 create_download_request (struct DownloadRequest *parent,
1495 unsigned int chk_idx,
1498 uint64_t file_start_offset,
1499 uint64_t desired_length)
1501 struct DownloadRequest *dr;
1503 unsigned int head_skip;
1504 uint64_t child_block_size;
1506 dr = GNUNET_new (struct DownloadRequest);
1507 dr->parent = parent;
1509 dr->offset = dr_offset;
1510 dr->chk_idx = chk_idx;
1513 child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1515 /* calculate how many blocks at this level are not interesting
1516 * from the start (rounded down), either because of the requested
1517 * file offset or because this IBlock is further along */
1518 if (dr_offset < file_start_offset)
1520 head_skip = (file_start_offset - dr_offset) / child_block_size;
1527 /* calculate index of last block at this level that is interesting (rounded up) */
1528 dr->num_children = (file_start_offset + desired_length - dr_offset) / child_block_size;
1529 if (dr->num_children * child_block_size <
1530 file_start_offset + desired_length - dr_offset)
1531 dr->num_children++; /* round up */
1532 GNUNET_assert (dr->num_children > head_skip);
1533 dr->num_children -= head_skip;
1534 if (dr->num_children > CHK_PER_INODE)
1535 dr->num_children = CHK_PER_INODE; /* cap at max */
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "Block at offset %llu and depth %u has %u children\n",
1538 (unsigned long long) dr_offset,
1542 /* now we can get the total number of *interesting* children for this block */
1544 /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1545 GNUNET_assert (dr->num_children > 0);
1548 GNUNET_new_array (dr->num_children,
1549 struct DownloadRequest *);
1550 for (i = 0; i < dr->num_children; i++)
1553 create_download_request (dr,
1556 dr_offset + (i + head_skip) * child_block_size,
1565 * Continuation after a possible attempt to reconstruct
1566 * the current IBlock from the existing file.
1568 * @param cls the 'struct ReconstructContext'
1571 reconstruct_cont (void *cls)
1573 struct GNUNET_FS_DownloadContext *dc = cls;
1575 /* clean up state from tree encoder */
1576 if (NULL != dc->task)
1578 GNUNET_SCHEDULER_cancel (dc->task);
1581 if (NULL != dc->rfh)
1583 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1586 /* start "normal" download */
1587 dc->issue_requests = GNUNET_YES;
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Starting normal download\n");
1590 schedule_block_download (dc, dc->top_request);
1595 * Task requesting the next block from the tree encoder.
1597 * @param cls the 'struct GNUJNET_FS_DownloadContext' we're processing
1600 get_next_block (void *cls)
1602 struct GNUNET_FS_DownloadContext *dc = cls;
1605 GNUNET_FS_tree_encoder_next (dc->te);
1610 * Function called asking for the current (encoded)
1611 * block to be processed. After processing the
1612 * client should either call "GNUNET_FS_tree_encode_next"
1613 * or (on error) "GNUNET_FS_tree_encode_finish".
1615 * This function checks if the content on disk matches
1616 * the expected content based on the URI.
1618 * @param cls closure
1619 * @param chk content hash key for the block
1620 * @param offset offset of the block
1621 * @param depth depth of the block, 0 for DBLOCK
1622 * @param type type of the block (IBLOCK or DBLOCK)
1623 * @param block the (encrypted) block
1624 * @param block_size size of block (in bytes)
1627 reconstruct_cb (void *cls,
1628 const struct ContentHashKey *chk,
1631 enum GNUNET_BLOCK_Type type,
1633 uint16_t block_size)
1635 struct GNUNET_FS_DownloadContext *dc = cls;
1636 struct GNUNET_FS_ProgressInfo pi;
1637 struct DownloadRequest *dr;
1641 /* find corresponding request entry */
1642 dr = dc->top_request;
1643 while (dr->depth > depth)
1645 GNUNET_assert (dr->num_children > 0);
1646 blen = GNUNET_FS_tree_compute_tree_size (dr->depth - 1);
1647 chld = (offset - dr->offset) / blen;
1648 if (chld < dr->children[0]->chk_idx)
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Block %u < %u irrelevant for our range\n",
1653 dr->children[0]->chk_idx);
1654 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block,
1656 return; /* irrelevant block */
1658 if (chld > dr->children[dr->num_children-1]->chk_idx)
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Block %u > %u irrelevant for our range\n",
1663 dr->children[dr->num_children-1]->chk_idx);
1664 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1665 return; /* irrelevant block */
1667 dr = dr->children[chld - dr->children[0]->chk_idx];
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "Matched TE block with request at offset %llu and depth %u in state %d\n",
1671 (unsigned long long) dr->offset,
1674 /* FIXME: this code needs more testing and might
1675 need to handle more states... */
1680 case BRS_RECONSTRUCT_DOWN:
1682 case BRS_RECONSTRUCT_META_UP:
1684 case BRS_RECONSTRUCT_UP:
1687 if (0 == memcmp (chk, &dr->chk, sizeof (struct ContentHashKey)))
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Reconstruction succeeded, can use block at offset %llu, depth %u\n",
1691 (unsigned long long) offset,
1693 /* block matches, hence tree below matches;
1694 * this request is done! */
1695 dr->state = BRS_DOWNLOAD_UP;
1696 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active,
1699 /* calculate how many bytes of payload this block
1701 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1702 /* how many of those bytes are in the requested range? */
1703 blen = GNUNET_MIN (blen, dc->length + dc->offset - dr->offset);
1704 /* signal progress */
1705 dc->completed += blen;
1706 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1707 pi.value.download.specifics.progress.data = NULL;
1708 pi.value.download.specifics.progress.offset = offset;
1709 pi.value.download.specifics.progress.data_len = 0;
1710 pi.value.download.specifics.progress.depth = 0;
1711 pi.value.download.specifics.progress.respect_offered = 0;
1712 pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
1713 GNUNET_FS_download_make_status_ (&pi, dc);
1714 /* FIXME: duplicated code from 'process_result_with_request - refactor */
1715 if (dc->completed == dc->length)
1717 /* download completed, signal */
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Download completed, truncating file to desired length %llu\n",
1720 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1722 /* truncate file to size (since we store IBlocks at the end) */
1723 if (NULL != dc->filename)
1726 TRUNCATE (dc->filename,
1727 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1728 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Reconstruction failed, need to download block at offset %llu, depth %u\n",
1736 (unsigned long long) offset,
1739 case BRS_DOWNLOAD_DOWN:
1741 case BRS_DOWNLOAD_UP:
1749 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block,
1751 if ( (dr == dc->top_request) &&
1752 (dr->state == BRS_DOWNLOAD_UP) )
1753 check_completed (dc);
1758 * Function called by the tree encoder to obtain a block of plaintext
1759 * data (for the lowest level of the tree).
1761 * @param cls our 'struct ReconstructContext'
1762 * @param offset identifies which block to get
1763 * @param max (maximum) number of bytes to get; returning
1764 * fewer will also cause errors
1765 * @param buf where to copy the plaintext buffer
1766 * @param emsg location to store an error message (on error)
1767 * @return number of bytes copied to buf, 0 on error
1770 fh_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
1772 struct GNUNET_FS_DownloadContext *dc = cls;
1773 struct GNUNET_DISK_FileHandle *fh = dc->rfh;
1778 if (offset != GNUNET_DISK_file_seek (fh, offset, GNUNET_DISK_SEEK_SET))
1781 *emsg = GNUNET_strdup (strerror (errno));
1784 ret = GNUNET_DISK_file_read (fh, buf, max);
1788 *emsg = GNUNET_strdup (strerror (errno));
1796 * Task that creates the initial (top-level) download
1797 * request for the file.
1799 * @param cls the 'struct GNUNET_FS_DownloadContext'
1802 GNUNET_FS_download_start_task_ (void *cls)
1804 struct GNUNET_FS_DownloadContext *dc = cls;
1805 struct GNUNET_FS_ProgressInfo pi;
1806 struct GNUNET_DISK_FileHandle *fh;
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Start task running...\n");
1811 if (0 == dc->length)
1813 /* no bytes required! */
1814 if (NULL != dc->filename)
1816 fh = GNUNET_DISK_file_open (dc->filename,
1817 GNUNET_DISK_OPEN_READWRITE |
1818 GNUNET_DISK_OPEN_CREATE |
1820 GNUNET_FS_uri_chk_get_file_size (dc->uri)) ?
1821 GNUNET_DISK_OPEN_TRUNCATE : 0),
1822 GNUNET_DISK_PERM_USER_READ |
1823 GNUNET_DISK_PERM_USER_WRITE |
1824 GNUNET_DISK_PERM_GROUP_READ |
1825 GNUNET_DISK_PERM_OTHER_READ);
1826 GNUNET_DISK_file_close (fh);
1828 GNUNET_FS_download_sync_ (dc);
1829 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1830 pi.value.download.specifics.start.meta = dc->meta;
1831 GNUNET_FS_download_make_status_ (&pi, dc);
1832 check_completed (dc);
1835 if (NULL != dc->emsg)
1837 if (NULL == dc->top_request)
1840 create_download_request (NULL, 0, dc->treedepth - 1, 0, dc->offset,
1842 dc->top_request->state = BRS_CHK_SET;
1843 dc->top_request->chk =
1845 GNUNET_FS_URI_CHK) ? dc->uri->data.chk.chk : dc->uri->data.loc.fi.chk;
1847 GNUNET_FS_download_sync_ (dc);
1848 if (NULL != dc->search)
1849 GNUNET_FS_search_result_sync_ (dc->search);
1850 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1851 pi.value.download.specifics.start.meta = dc->meta;
1852 GNUNET_FS_download_make_status_ (&pi, dc);
1854 GNUNET_FS_download_start_downloading_ (dc);
1855 /* attempt reconstruction from disk */
1856 if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
1858 GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
1859 GNUNET_DISK_PERM_NONE);
1860 if (dc->top_request->state == BRS_CHK_SET)
1862 if (NULL != dc->rfh)
1864 /* first, try top-down */
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Trying top-down reconstruction for `%s'\n", dc->filename);
1867 try_top_down_reconstruction (dc, dc->top_request);
1868 switch (dc->top_request->state)
1872 case BRS_DOWNLOAD_DOWN:
1873 break; /* normal, some blocks already down */
1874 case BRS_DOWNLOAD_UP:
1875 /* already done entirely, party! */
1876 if (NULL != dc->rfh)
1878 /* avoid hanging on to file handle longer than
1880 GNUNET_DISK_file_close (dc->rfh);
1885 GNUNET_asprintf (&dc->emsg, _("Invalid URI"));
1886 GNUNET_FS_download_sync_ (dc);
1887 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1888 pi.value.download.specifics.error.message = dc->emsg;
1889 GNUNET_FS_download_make_status_ (&pi, dc);
1897 /* attempt reconstruction from meta data */
1898 if ((GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
1903 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
1905 GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
1906 GNUNET_CONTAINER_meta_data_iterate (dc->meta, &match_full_data, dc);
1907 if (BRS_DOWNLOAD_UP == dc->top_request->state)
1909 if (NULL != dc->rfh)
1911 /* avoid hanging on to file handle longer than
1913 GNUNET_DISK_file_close (dc->rfh);
1916 return; /* finished, status update was already done for us */
1919 if (NULL != dc->rfh)
1921 /* finally, actually run bottom-up */
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "Trying bottom-up reconstruction of file `%s'\n", dc->filename);
1925 GNUNET_FS_tree_encoder_create (dc->h,
1926 GNUNET_FS_uri_chk_get_file_size (dc->uri),
1932 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block,
1937 /* simple, top-level download */
1938 dc->issue_requests = GNUNET_YES;
1939 schedule_block_download (dc, dc->top_request);
1941 if (BRS_DOWNLOAD_UP == dc->top_request->state)
1942 check_completed (dc);
1947 * Create SUSPEND event for the given download operation
1948 * and then clean up our state (without stop signal).
1950 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1953 GNUNET_FS_download_signal_suspend_ (void *cls)
1955 struct GNUNET_FS_DownloadContext *dc = cls;
1956 struct GNUNET_FS_ProgressInfo pi;
1958 if (NULL != dc->top)
1959 GNUNET_FS_end_top (dc->h, dc->top);
1960 while (NULL != dc->child_head)
1961 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1962 if (NULL != dc->search)
1964 dc->search->download = NULL;
1967 if (NULL != dc->job_queue)
1969 GNUNET_FS_dequeue_ (dc->job_queue);
1970 dc->job_queue = NULL;
1972 if (NULL != dc->parent)
1973 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
1975 if (NULL != dc->task)
1977 GNUNET_SCHEDULER_cancel (dc->task);
1980 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1981 GNUNET_FS_download_make_status_ (&pi, dc);
1984 GNUNET_FS_tree_encoder_finish (dc->te, NULL);
1987 if (NULL != dc->rfh)
1989 GNUNET_DISK_file_close (dc->rfh);
1992 GNUNET_FS_free_download_request_ (dc->top_request);
1993 if (NULL != dc->active)
1995 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1998 GNUNET_free_non_null (dc->filename);
1999 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2000 GNUNET_FS_uri_destroy (dc->uri);
2001 GNUNET_free_non_null (dc->temp_filename);
2002 GNUNET_free_non_null (dc->serialization);
2003 GNUNET_assert (NULL == dc->job_queue);
2009 * Helper function to setup the download context.
2011 * @param h handle to the file sharing subsystem
2012 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2013 * @param meta known metadata for the file (can be NULL)
2014 * @param filename where to store the file, maybe NULL (then no file is
2015 * created on disk and data must be grabbed from the callbacks)
2016 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2017 * can be NULL (in which case we will pick a name if needed); the temporary file
2018 * may already exist, in which case we will try to use the data that is there and
2019 * if it is not what is desired, will overwrite it
2020 * @param offset at what offset should we start the download (typically 0)
2021 * @param length how many bytes should be downloaded starting at offset
2022 * @param anonymity anonymity level to use for the download
2023 * @param options various options
2024 * @param cctx initial value for the client context for this download
2025 * @return context that can be used to control this download
2027 struct GNUNET_FS_DownloadContext *
2028 create_download_context (struct GNUNET_FS_Handle *h,
2029 const struct GNUNET_FS_Uri *uri,
2030 const struct GNUNET_CONTAINER_MetaData *meta,
2031 const char *filename,
2032 const char *tempname,
2036 enum GNUNET_FS_DownloadOptions options,
2039 struct GNUNET_FS_DownloadContext *dc;
2041 GNUNET_assert (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri));
2042 if ((offset + length < offset) ||
2043 (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)))
2048 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2050 "Starting download %p, %u bytes at offset %llu\n",
2052 (unsigned int) length,
2053 (unsigned long long) offset);
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_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, 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), GNUNET_NO);
2075 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2076 if ((NULL == filename) && (is_recursive_download (dc)))
2078 if (NULL != tempname)
2079 dc->temp_filename = GNUNET_strdup (tempname);
2081 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "Starting download `%s' of %llu bytes with tree depth %u\n",
2086 (unsigned long long) length,
2088 GNUNET_assert (NULL == dc->job_queue);
2089 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_,
2096 * Download parts of a file. Note that this will store
2097 * the blocks at the respective offset in the given file. Also, the
2098 * download is still using the blocking of the underlying FS
2099 * encoding. As a result, the download may *write* outside of the
2100 * given boundaries (if offset and length do not match the 32k FS
2101 * block boundaries). <p>
2103 * This function should be used to focus a download towards a
2104 * particular portion of the file (optimization), not to strictly
2105 * limit the download to exactly those bytes.
2107 * @param h handle to the file sharing subsystem
2108 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2109 * @param meta known metadata for the file (can be NULL)
2110 * @param filename where to store the file, maybe NULL (then no file is
2111 * created on disk and data must be grabbed from the callbacks)
2112 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2113 * can be NULL (in which case we will pick a name if needed); the temporary file
2114 * may already exist, in which case we will try to use the data that is there and
2115 * if it is not what is desired, will overwrite it
2116 * @param offset at what offset should we start the download (typically 0)
2117 * @param length how many bytes should be downloaded starting at offset
2118 * @param anonymity anonymity level to use for the download
2119 * @param options various options
2120 * @param cctx initial value for the client context for this download
2121 * @param parent parent download to associate this download with (use NULL
2122 * for top-level downloads; useful for manually-triggered recursive downloads)
2123 * @return context that can be used to control this download
2125 struct GNUNET_FS_DownloadContext *
2126 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2127 const struct GNUNET_FS_Uri *uri,
2128 const struct GNUNET_CONTAINER_MetaData *meta,
2129 const char *filename,
2130 const char *tempname,
2134 enum GNUNET_FS_DownloadOptions options,
2136 struct GNUNET_FS_DownloadContext *parent)
2138 struct GNUNET_FS_DownloadContext *dc;
2140 dc = create_download_context (h,
2152 dc->parent = parent;
2154 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2155 else if (0 == (GNUNET_FS_DOWNLOAD_IS_PROBE & options) )
2157 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2163 * Download parts of a file based on a search result. The download
2164 * will be associated with the search result (and the association
2165 * will be preserved when serializing/deserializing the state).
2166 * If the search is stopped, the download will not be aborted but
2167 * be 'promoted' to a stand-alone download.
2169 * As with the other download function, this will store
2170 * the blocks at the respective offset in the given file. Also, the
2171 * download is still using the blocking of the underlying FS
2172 * encoding. As a result, the download may *write* outside of the
2173 * given boundaries (if offset and length do not match the 32k FS
2174 * block boundaries). <p>
2176 * The given range can be used to focus a download towards a
2177 * particular portion of the file (optimization), not to strictly
2178 * limit the download to exactly those bytes.
2180 * @param h handle to the file sharing subsystem
2181 * @param sr the search result to use for the download (determines uri and
2182 * meta data and associations)
2183 * @param filename where to store the file, maybe NULL (then no file is
2184 * created on disk and data must be grabbed from the callbacks)
2185 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2186 * can be NULL (in which case we will pick a name if needed); the temporary file
2187 * may already exist, in which case we will try to use the data that is there and
2188 * if it is not what is desired, will overwrite it
2189 * @param offset at what offset should we start the download (typically 0)
2190 * @param length how many bytes should be downloaded starting at offset
2191 * @param anonymity anonymity level to use for the download
2192 * @param options various download options
2193 * @param cctx initial value for the client context for this download
2194 * @return context that can be used to control this download
2196 struct GNUNET_FS_DownloadContext *
2197 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2198 struct GNUNET_FS_SearchResult *sr,
2199 const char *filename,
2200 const char *tempname,
2204 enum GNUNET_FS_DownloadOptions options,
2207 struct GNUNET_FS_DownloadContext *dc;
2209 if ((NULL == sr) || (NULL != sr->download))
2214 dc = create_download_context (h,
2228 if (NULL != sr->probe_ctx)
2230 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2231 sr->probe_ctx = NULL;
2232 GNUNET_FS_stop_probe_ping_task_ (sr);
2239 * Start the downloading process (by entering the queue).
2241 * @param dc our download context
2244 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2246 if (dc->completed == dc->length)
2249 return; /* already running */
2250 GNUNET_assert (NULL == dc->job_queue);
2251 GNUNET_assert (NULL == dc->task);
2252 GNUNET_assert (NULL != dc->active);
2254 = GNUNET_FS_queue_ (dc->h,
2255 &activate_fs_download,
2256 &deactivate_fs_download,
2258 (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE,
2259 (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
2260 ? GNUNET_FS_QUEUE_PRIORITY_NORMAL
2261 : GNUNET_FS_QUEUE_PRIORITY_PROBE);
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 "Download %p put into queue as job %p\n",
2269 * Suspend a download.
2271 * @param dc handle for the download
2274 GNUNET_FS_download_suspend (struct GNUNET_FS_DownloadContext *dc)
2276 deactivate_fs_download(dc);
2281 * Resume a suspended download.
2283 * @param dc handle for the download
2286 GNUNET_FS_download_resume (struct GNUNET_FS_DownloadContext *dc)
2288 struct GNUNET_FS_ProgressInfo pi;
2290 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
2291 GNUNET_FS_download_make_status_ (&pi, dc);
2293 GNUNET_assert (NULL == dc->task);
2295 = GNUNET_FS_queue_ (dc->h,
2296 &activate_fs_download,
2297 &deactivate_fs_download,
2298 dc, (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE,
2299 (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
2300 ? GNUNET_FS_QUEUE_PRIORITY_NORMAL
2301 : GNUNET_FS_QUEUE_PRIORITY_PROBE);
2307 * Stop a download (aborts if download is incomplete).
2309 * @param dc handle for the download
2310 * @param do_delete delete files of incomplete downloads
2313 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
2316 struct GNUNET_FS_ProgressInfo pi;
2318 int search_was_null;
2320 if (NULL != dc->top)
2321 GNUNET_FS_end_top (dc->h, dc->top);
2322 if (NULL != dc->task)
2324 GNUNET_SCHEDULER_cancel (dc->task);
2327 search_was_null = (NULL == dc->search);
2328 if (NULL != dc->search)
2330 dc->search->download = NULL;
2331 GNUNET_FS_search_result_sync_ (dc->search);
2334 if (NULL != dc->job_queue)
2336 GNUNET_FS_dequeue_ (dc->job_queue);
2337 dc->job_queue = NULL;
2341 GNUNET_FS_tree_encoder_finish (dc->te, NULL);
2344 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2345 while (NULL != dc->child_head)
2346 GNUNET_FS_download_stop (dc->child_head, do_delete);
2347 if (NULL != dc->parent)
2348 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
2349 dc->parent->child_tail,
2351 if (NULL != dc->serialization)
2352 GNUNET_FS_remove_sync_file_ (dc->h,
2353 ((NULL != dc->parent) ||
2354 (! search_was_null)) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2355 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2357 if ((GNUNET_YES == have_children) && (NULL == dc->parent))
2358 GNUNET_FS_remove_sync_dir_ (dc->h,
2359 (! search_was_null) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2360 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2362 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2363 GNUNET_FS_download_make_status_ (&pi, dc);
2364 GNUNET_FS_free_download_request_ (dc->top_request);
2365 dc->top_request = NULL;
2366 if (NULL != dc->active)
2368 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2371 if (NULL != dc->filename)
2373 if ((dc->completed != dc->length) && (GNUNET_YES == do_delete))
2375 if ( (0 != UNLINK (dc->filename)) &&
2377 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2381 GNUNET_free (dc->filename);
2383 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2384 GNUNET_FS_uri_destroy (dc->uri);
2385 if (NULL != dc->temp_filename)
2387 if (0 != UNLINK (dc->temp_filename))
2388 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2391 GNUNET_free (dc->temp_filename);
2393 GNUNET_free_non_null (dc->serialization);
2394 GNUNET_assert (NULL == dc->job_queue);
2398 /* end of fs_download.c */