2 This file is part of GNUnet.
3 (C) 2001-2012 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 #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)) &&
40 ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
41 ((dc->meta == NULL) &&
42 ((NULL == dc->filename) ||
43 ((strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
45 strstr (dc->filename + strlen (dc->filename) -
46 strlen (GNUNET_FS_DIRECTORY_EXT),
47 GNUNET_FS_DIRECTORY_EXT))))));
52 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
53 * only have to truncate the file once we're done).
55 * Given the offset of a block (with respect to the DBLOCKS) and its
56 * depth, return the offset where we would store this block in the
59 * @param fsize overall file size
60 * @param off offset of the block in the file
61 * @param depth depth of the block in the tree, 0 for DBLOCK
62 * @return off for DBLOCKS (depth == treedepth),
63 * otherwise an offset past the end
64 * of the file that does not overlap
65 * with the range for any other block
68 compute_disk_offset (uint64_t fsize, uint64_t off, unsigned int depth)
71 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
72 uint64_t loff; /* where do IBlocks for depth "i" start? */
73 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
77 /* first IBlocks start at the end of file, rounded up
78 * to full DBLOCK_SIZE */
79 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
81 ((fsize + DBLOCK_SIZE -
82 1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
83 GNUNET_assert (0 == (off % DBLOCK_SIZE));
84 ioff = (off / DBLOCK_SIZE);
85 for (i = 1; i < depth; i++)
88 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
89 GNUNET_assert (lsize > 0);
90 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
91 ioff /= CHK_PER_INODE;
93 return loff + ioff * sizeof (struct ContentHashKey);
98 * Fill in all of the generic fields for a download event and call the
101 * @param pi structure to fill in
102 * @param dc overall download context
105 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
106 struct GNUNET_FS_DownloadContext *dc)
108 pi->value.download.dc = dc;
109 pi->value.download.cctx = dc->client_info;
110 pi->value.download.pctx =
111 (dc->parent == NULL) ? NULL : dc->parent->client_info;
112 pi->value.download.sctx =
113 (dc->search == NULL) ? NULL : dc->search->client_info;
114 pi->value.download.uri = dc->uri;
115 pi->value.download.filename = dc->filename;
116 pi->value.download.size = dc->length;
117 /* FIXME: Fix duration calculation to account for pauses */
118 pi->value.download.duration =
119 GNUNET_TIME_absolute_get_duration (dc->start_time);
120 pi->value.download.completed = dc->completed;
121 pi->value.download.anonymity = dc->anonymity;
122 pi->value.download.eta =
123 GNUNET_TIME_calculate_eta (dc->start_time, dc->completed, dc->length);
124 pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
125 if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
126 dc->client_info = dc->h->upcb (dc->h->upcb_cls, pi);
128 dc->client_info = GNUNET_FS_search_probe_progress_ (NULL, pi);
133 * We're ready to transmit a search request to the
134 * file-sharing service. Do it. If there is
135 * more than one request pending, try to send
136 * multiple or request another transmission.
139 * @param size number of bytes available in buf
140 * @param buf where the callee should write the message
141 * @return number of bytes written to buf
144 transmit_download_request (void *cls, size_t size, void *buf);
148 * Closure for iterator processing results.
150 struct ProcessResultClosure
156 GNUNET_HashCode query;
159 * Data found in P2P network.
164 * Our download context.
166 struct GNUNET_FS_DownloadContext *dc;
169 * Number of bytes in data.
176 enum GNUNET_BLOCK_Type type;
179 * Flag to indicate if this block should be stored on disk.
184 * When did we last transmit the request?
186 struct GNUNET_TIME_Absolute last_transmission;
192 * Iterator over entries in the pending requests in the 'active' map for the
193 * reply that we just got.
195 * @param cls closure (our 'struct ProcessResultClosure')
196 * @param key query for the given value / request
197 * @param value value in the hash map (a 'struct DownloadRequest')
198 * @return GNUNET_YES (we should continue to iterate); unless serious error
201 process_result_with_request (void *cls, const GNUNET_HashCode * key,
206 * We've found a matching block without downloading it.
207 * Encrypt it and pass it to our "receive" function as
208 * if we had received it from the network.
210 * @param dc download in question
211 * @param chk request this relates to
212 * @param dr request details
213 * @param block plaintext data matching request
214 * @param len number of bytes in block
215 * @param do_store should we still store the block on disk?
216 * @return GNUNET_OK on success
219 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
220 const struct ContentHashKey *chk,
221 struct DownloadRequest *dr, const char *block,
222 size_t len, int do_store)
224 struct ProcessResultClosure prc;
226 struct GNUNET_CRYPTO_AesSessionKey sk;
227 struct GNUNET_CRYPTO_AesInitializationVector iv;
228 GNUNET_HashCode query;
230 GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
231 if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len, &sk, &iv, enc))
234 return GNUNET_SYSERR;
236 GNUNET_CRYPTO_hash (enc, len, &query);
237 if (0 != memcmp (&query, &chk->query, sizeof (GNUNET_HashCode)))
240 return GNUNET_SYSERR;
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Matching %u byte block for `%s' at offset %llu already present, no need for download!\n",
245 dc->filename, (unsigned long long) dr->offset);
246 /* already got it! */
252 dr->depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK : GNUNET_BLOCK_TYPE_FS_IBLOCK;
253 prc.query = chk->query;
254 prc.do_store = do_store;
255 prc.last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
256 process_result_with_request (&prc, &chk->key, dr);
262 * We've lost our connection with the FS service.
263 * Re-establish it and re-transmit all of our
266 * @param dc download context that is having trouble
269 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
273 * We found an entry in a directory. Check if the respective child
274 * already exists and if not create the respective child download.
276 * @param cls the parent download
277 * @param filename name of the file in the directory
278 * @param uri URI of the file (CHK or LOC)
279 * @param meta meta data of the file
280 * @param length number of bytes in data
281 * @param data contents of the file (or NULL if they were not inlined)
284 trigger_recursive_download (void *cls, const char *filename,
285 const struct GNUNET_FS_Uri *uri,
286 const struct GNUNET_CONTAINER_MetaData *meta,
287 size_t length, const void *data);
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,
311 ("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
314 if (dc->filename != NULL)
316 h = GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
317 GNUNET_DISK_PERM_NONE);
321 GNUNET_assert (dc->temp_filename != NULL);
322 h = GNUNET_DISK_file_open (dc->temp_filename, GNUNET_DISK_OPEN_READ,
323 GNUNET_DISK_PERM_NONE);
327 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331 _("Directory too large for system address space\n"));
335 GNUNET_FS_directory_list_contents (size, data, 0,
336 &trigger_recursive_download, dc);
337 GNUNET_DISK_file_unmap (m);
339 GNUNET_DISK_file_close (h);
340 if (dc->filename == NULL)
342 if (0 != UNLINK (dc->temp_filename))
343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
345 GNUNET_free (dc->temp_filename);
346 dc->temp_filename = NULL;
352 * Check if all child-downloads have completed (or trigger them if
353 * necessary) and once we're completely done, signal completion (and
354 * possibly recurse to parent). This function MUST be called when the
355 * download of a file itself is done or when the download of a file is
356 * done and then later a direct child download has completed (and
357 * hence this download may complete itself).
359 * @param dc download to check for completion of children
362 check_completed (struct GNUNET_FS_DownloadContext *dc)
364 struct GNUNET_FS_ProgressInfo pi;
365 struct GNUNET_FS_DownloadContext *pos;
367 /* first, check if we need to download children */
368 if ((dc->child_head == NULL) && (is_recursive_download (dc)))
369 full_recursive_download (dc);
370 /* then, check if children are done already */
371 pos = dc->child_head;
374 if ((pos->emsg == NULL) && (pos->completed < pos->length))
375 return; /* not done yet */
376 if ((pos->child_head != NULL) && (pos->has_finished != GNUNET_YES))
377 return; /* not transitively done yet */
380 /* All of our children are done, so mark this download done */
381 dc->has_finished = GNUNET_YES;
382 if (dc->job_queue != NULL)
384 GNUNET_FS_dequeue_ (dc->job_queue);
385 dc->job_queue = NULL;
387 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
389 GNUNET_SCHEDULER_cancel (dc->task);
390 dc->task = GNUNET_SCHEDULER_NO_TASK;
394 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
397 GNUNET_FS_download_sync_ (dc);
399 /* signal completion */
400 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
401 GNUNET_FS_download_make_status_ (&pi, dc);
403 /* let parent know */
404 if (dc->parent != NULL)
405 check_completed (dc->parent);
410 * We got a block of plaintext data (from the meta data).
411 * Try it for upward reconstruction of the data. On success,
412 * the top-level block will move to state BRS_DOWNLOAD_UP.
414 * @param dc context for the download
415 * @param dr download request to match against
416 * @param data plaintext data, starting from the beginning of the file
417 * @param data_len number of bytes in data
420 try_match_block (struct GNUNET_FS_DownloadContext *dc,
421 struct DownloadRequest *dr, const char *data, size_t data_len)
423 struct GNUNET_FS_ProgressInfo pi;
425 char enc[DBLOCK_SIZE];
426 struct ContentHashKey chks[CHK_PER_INODE];
427 struct ContentHashKey in_chk;
428 struct GNUNET_CRYPTO_AesSessionKey sk;
429 struct GNUNET_CRYPTO_AesInitializationVector iv;
431 struct DownloadRequest *drc;
432 struct GNUNET_DISK_FileHandle *fh;
439 odata_len = data_len;
440 if (BRS_DOWNLOAD_UP == dr->state)
444 complete = GNUNET_YES;
445 for (i = 0; i < dr->num_children; i++)
447 drc = dr->children[i];
448 try_match_block (dc, drc, data, data_len);
449 if (drc->state != BRS_RECONSTRUCT_META_UP)
450 complete = GNUNET_NO;
454 if (GNUNET_YES != complete)
456 data = (const char *) chks;
457 dlen = dr->num_children * sizeof (struct ContentHashKey);
461 if (dr->offset > data_len)
463 dlen = GNUNET_MIN (data_len - dr->offset, DBLOCK_SIZE);
465 GNUNET_CRYPTO_hash (&data[dr->offset], dlen, &in_chk.key);
466 GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
467 if (-1 == GNUNET_CRYPTO_aes_encrypt (&data[dr->offset], dlen, &sk, &iv, enc))
472 GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
477 dr->state = BRS_RECONSTRUCT_META_UP;
480 if (0 != memcmp (&in_chk, &dr->chk, sizeof (struct ContentHashKey)))
482 /* other peer provided bogus meta data */
486 /* write block to disk */
487 fn = dc->filename != NULL ? dc->filename : dc->temp_filename;
488 fh = GNUNET_DISK_file_open (fn,
489 GNUNET_DISK_OPEN_READWRITE |
490 GNUNET_DISK_OPEN_CREATE |
491 GNUNET_DISK_OPEN_TRUNCATE,
492 GNUNET_DISK_PERM_USER_READ |
493 GNUNET_DISK_PERM_USER_WRITE |
494 GNUNET_DISK_PERM_GROUP_READ |
495 GNUNET_DISK_PERM_OTHER_READ);
498 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
499 GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
501 GNUNET_DISK_file_close (fh);
502 dr->state = BRS_ERROR;
503 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
504 pi.value.download.specifics.error.message = dc->emsg;
505 GNUNET_FS_download_make_status_ (&pi, dc);
508 if (data_len != GNUNET_DISK_file_write (fh, odata, odata_len))
510 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
511 GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
513 GNUNET_DISK_file_close (fh);
514 dr->state = BRS_ERROR;
515 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
516 pi.value.download.specifics.error.message = dc->emsg;
517 GNUNET_FS_download_make_status_ (&pi, dc);
520 GNUNET_DISK_file_close (fh);
522 dr->state = BRS_DOWNLOAD_UP;
523 dc->completed = dc->length;
524 GNUNET_FS_download_sync_ (dc);
525 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
526 pi.value.download.specifics.progress.data = data;
527 pi.value.download.specifics.progress.offset = 0;
528 pi.value.download.specifics.progress.data_len = dlen;
529 pi.value.download.specifics.progress.depth = 0;
530 pi.value.download.specifics.progress.trust_offered = 0;
531 pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
532 GNUNET_FS_download_make_status_ (&pi, dc);
533 if ((NULL != dc->filename) &&
535 truncate (dc->filename,
536 GNUNET_ntohll (dc->uri->data.chk.file_length))))
537 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
539 check_completed (dc);
542 /* how did we get here? */
550 * Type of a function that libextractor calls for each
551 * meta data item found. If we find full data meta data,
552 * call 'try_match_block' on it.
554 * @param cls our 'struct GNUNET_FS_DownloadContext*'
555 * @param plugin_name name of the plugin that produced this value;
556 * special values can be used (i.e. '<zlib>' for zlib being
557 * used in the main libextractor library and yielding
559 * @param type libextractor-type describing the meta data
560 * @param format basic format information about data
561 * @param data_mime_type mime-type of data (not of the original file);
562 * can be NULL (if mime-type is not known)
563 * @param data actual meta-data found
564 * @param data_len number of bytes in data
565 * @return 0 to continue extracting, 1 to abort
568 match_full_data (void *cls, const char *plugin_name,
569 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
570 const char *data_mime_type, const char *data, size_t data_len)
572 struct GNUNET_FS_DownloadContext *dc = cls;
574 if (type != EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u bytes of FD!\n",
577 (unsigned int) data_len);
578 if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
581 return 1; /* bogus meta data */
583 try_match_block (dc, dc->top_request, data, data_len);
589 * Set the state of the given download request to
590 * BRS_DOWNLOAD_UP and propagate it up the tree.
592 * @param dr download request that is done
595 propagate_up (struct DownloadRequest *dr)
601 dr->state = BRS_DOWNLOAD_UP;
605 for (i = 0; i < dr->num_children; i++)
606 if (dr->children[i]->state != BRS_DOWNLOAD_UP)
609 while (i == dr->num_children);
614 * Try top-down reconstruction. Before, the given request node
615 * must have the state BRS_CHK_SET. Afterwards, more nodes may
616 * have that state or advanced to BRS_DOWNLOAD_DOWN or even
617 * BRS_DOWNLOAD_UP. It is also possible to get BRS_ERROR on the
620 * @param dc overall download this block belongs to
621 * @param dr block to reconstruct
624 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
625 struct DownloadRequest *dr)
628 char block[DBLOCK_SIZE];
633 struct DownloadRequest *drc;
634 uint64_t child_block_size;
635 const struct ContentHashKey *chks;
638 GNUNET_assert (dc->rfh != NULL);
639 GNUNET_assert (dr->state == BRS_CHK_SET);
640 total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
641 GNUNET_assert (dr->depth < dc->treedepth);
642 len = GNUNET_FS_tree_calculate_block_size (total, dr->offset, dr->depth);
643 GNUNET_assert (len <= DBLOCK_SIZE);
644 off = compute_disk_offset (total, dr->offset, dr->depth);
645 if (dc->old_file_size < off + len)
646 return; /* failure */
647 if (off != GNUNET_DISK_file_seek (dc->rfh, off, GNUNET_DISK_SEEK_SET))
649 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "seek", dc->filename);
650 return; /* failure */
652 if (len != GNUNET_DISK_file_read (dc->rfh, block, len))
654 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", dc->filename);
655 return; /* failure */
657 GNUNET_CRYPTO_hash (block, len, &key);
658 if (0 != memcmp (&key, &dr->chk.key, sizeof (GNUNET_HashCode)))
659 return; /* mismatch */
661 encrypt_existing_match (dc, &dr->chk, dr, block, len, GNUNET_NO))
663 /* hash matches but encrypted block does not, really bad */
664 dr->state = BRS_ERROR;
666 while (dr->parent != NULL)
669 dr->state = BRS_ERROR;
674 dr->state = BRS_DOWNLOAD_DOWN;
676 /* set CHKs for children */
677 up_done = GNUNET_YES;
678 chks = (const struct ContentHashKey *) block;
679 for (i = 0; i < dr->num_children; i++)
681 drc = dr->children[i];
682 GNUNET_assert (drc->offset >= dr->offset);
683 child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
684 GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);
685 if (drc->state == BRS_INIT)
687 drc->state = BRS_CHK_SET;
688 drc->chk = chks[drc->chk_idx];
689 try_top_down_reconstruction (dc, drc);
691 if (drc->state != BRS_DOWNLOAD_UP)
692 up_done = GNUNET_NO; /* children not all done */
694 if (up_done == GNUNET_YES)
695 propagate_up (dr); /* children all done (or no children...) */
700 * Schedule the download of the specified block in the tree.
702 * @param dc overall download this block belongs to
703 * @param dr request to schedule
706 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
707 struct DownloadRequest *dr)
716 case BRS_RECONSTRUCT_DOWN:
719 case BRS_RECONSTRUCT_META_UP:
722 case BRS_RECONSTRUCT_UP:
726 /* normal case, start download */
728 case BRS_DOWNLOAD_DOWN:
729 for (i = 0; i < dr->num_children; i++)
730 schedule_block_download (dc, dr->children[i]);
732 case BRS_DOWNLOAD_UP:
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Scheduling download at offset %llu and depth %u for `%s'\n",
741 (unsigned long long) dr->offset, dr->depth,
742 GNUNET_h2s (&dr->chk.query));
744 GNUNET_CONTAINER_multihashmap_contains_value (dc->active, &dr->chk.query,
746 return; /* already active */
747 GNUNET_CONTAINER_multihashmap_put (dc->active, &dr->chk.query, dr,
748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
749 if (dc->client == NULL)
750 return; /* download not active */
751 GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
752 dr->is_pending = GNUNET_YES;
755 GNUNET_CLIENT_notify_transmit_ready (dc->client,
756 sizeof (struct SearchMessage),
757 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
759 &transmit_download_request, dc);
763 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
766 * We found an entry in a directory. Check if the respective child
767 * already exists and if not create the respective child download.
769 * @param cls the parent download
770 * @param filename name of the file in the directory
771 * @param uri URI of the file (CHK or LOC)
772 * @param meta meta data of the file
773 * @param length number of bytes in data
774 * @param data contents of the file (or NULL if they were not inlined)
777 trigger_recursive_download (void *cls, const char *filename,
778 const struct GNUNET_FS_Uri *uri,
779 const struct GNUNET_CONTAINER_MetaData *meta,
780 size_t length, const void *data)
782 struct GNUNET_FS_DownloadContext *dc = cls;
783 struct GNUNET_FS_DownloadContext *cpos;
794 return; /* entry for the directory itself */
795 cpos = dc->child_head;
798 if ((GNUNET_FS_uri_test_equal (uri, cpos->uri)) ||
799 ((filename != NULL) && (0 == strcmp (cpos->filename, filename))))
804 return; /* already exists */
806 if (NULL == filename)
808 fn = GNUNET_FS_meta_data_suggest_filename (meta);
811 us = GNUNET_FS_uri_to_string (uri);
812 fn = GNUNET_strdup (&us[strlen (GNUNET_FS_URI_CHK_PREFIX)]);
815 else if (fn[0] == '.')
818 us = GNUNET_FS_uri_to_string (uri);
819 GNUNET_asprintf (&fn, "%s%s", &us[strlen (GNUNET_FS_URI_CHK_PREFIX)],
824 /* change '\' to '/' (this should have happened
825 * during insertion, but malicious peers may
826 * not have done this) */
827 while (NULL != (pos = strstr (fn, "\\")))
829 /* remove '../' everywhere (again, well-behaved
830 * peers don't do this, but don't trust that
831 * we did not get something nasty) */
832 while (NULL != (pos = strstr (fn, "../")))
840 if (dc->filename == NULL)
846 dn = GNUNET_strdup (dc->filename);
847 GNUNET_break ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
849 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
850 GNUNET_FS_DIRECTORY_EXT)));
851 sfn = GNUNET_strdup (filename);
852 while ((strlen (sfn) > 0) && (filename[strlen (sfn) - 1] == '/'))
853 sfn[strlen (sfn) - 1] = '\0';
854 if ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
856 strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
857 GNUNET_FS_DIRECTORY_EXT)))
858 dn[strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
859 if ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
860 ((strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
862 strstr (filename + strlen (filename) -
863 strlen (GNUNET_FS_DIRECTORY_EXT), GNUNET_FS_DIRECTORY_EXT))))
865 GNUNET_asprintf (&full_name, "%s%s%s%s", dn, DIR_SEPARATOR_STR, sfn,
866 GNUNET_FS_DIRECTORY_EXT);
870 GNUNET_asprintf (&full_name, "%s%s%s", dn, DIR_SEPARATOR_STR, sfn);
875 if ((full_name != NULL) &&
876 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (full_name)))
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
880 ("Failed to create directory for recursive download of `%s'\n"),
882 GNUNET_free (full_name);
883 GNUNET_free_non_null (fn);
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Triggering recursive download of size %llu with %u bytes MD\n",
890 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
892 GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
893 GNUNET_FS_download_start (dc->h, uri, meta, full_name, temp_name, 0,
894 GNUNET_FS_uri_chk_get_file_size (uri),
895 dc->anonymity, dc->options, NULL, dc);
896 GNUNET_free_non_null (full_name);
897 GNUNET_free_non_null (temp_name);
898 GNUNET_free_non_null (fn);
903 * (recursively) free download request structure
905 * @param dr request to free
908 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
914 for (i = 0; i < dr->num_children; i++)
915 GNUNET_FS_free_download_request_ (dr->children[i]);
916 GNUNET_free_non_null (dr->children);
922 * Iterator over entries in the pending requests in the 'active' map for the
923 * reply that we just got.
925 * @param cls closure (our 'struct ProcessResultClosure')
926 * @param key query for the given value / request
927 * @param value value in the hash map (a 'struct DownloadRequest')
928 * @return GNUNET_YES (we should continue to iterate); unless serious error
931 process_result_with_request (void *cls, const GNUNET_HashCode * key,
934 struct ProcessResultClosure *prc = cls;
935 struct DownloadRequest *dr = value;
936 struct GNUNET_FS_DownloadContext *dc = prc->dc;
937 struct DownloadRequest *drc;
938 struct GNUNET_DISK_FileHandle *fh = NULL;
939 struct GNUNET_CRYPTO_AesSessionKey skey;
940 struct GNUNET_CRYPTO_AesInitializationVector iv;
942 struct GNUNET_FS_ProgressInfo pi;
947 struct ContentHashKey *chkarr;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Received %u byte block `%s' matching pending request at depth %u and offset %llu/%llu\n",
951 (unsigned int) prc->size,
952 GNUNET_h2s (key), dr->depth, (unsigned long long) dr->offset,
953 (unsigned long long) GNUNET_ntohll (dc->uri->data.
955 bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll
956 (dc->uri->data.chk.file_length),
957 dr->offset, dr->depth);
960 GNUNET_asprintf (&dc->emsg,
962 ("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)\n"),
963 bs, dr->depth, (unsigned long long) dr->offset,
964 (unsigned long long) GNUNET_ntohll (dc->uri->data.
967 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s", dc->emsg);
968 while (dr->parent != NULL)
970 dr->state = BRS_ERROR;
973 dr->state = BRS_ERROR;
977 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &prc->query, dr);
978 if (GNUNET_YES == dr->is_pending)
980 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
981 dr->is_pending = GNUNET_NO;
984 GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key, &skey, &iv);
985 if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data, prc->size, &skey, &iv, pt))
988 dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
992 compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
993 dr->offset, dr->depth);
995 if ((GNUNET_YES == prc->do_store) &&
996 ((dc->filename != NULL) || (is_recursive_download (dc))) &&
997 ((dr->depth == dc->treedepth) ||
998 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES))))
1000 fh = GNUNET_DISK_file_open (dc->filename !=
1001 NULL ? dc->filename : dc->temp_filename,
1002 GNUNET_DISK_OPEN_READWRITE |
1003 GNUNET_DISK_OPEN_CREATE,
1004 GNUNET_DISK_PERM_USER_READ |
1005 GNUNET_DISK_PERM_USER_WRITE |
1006 GNUNET_DISK_PERM_GROUP_READ |
1007 GNUNET_DISK_PERM_OTHER_READ);
1010 GNUNET_asprintf (&dc->emsg,
1011 _("Download failed: could not open file `%s': %s\n"),
1012 dc->filename, STRERROR (errno));
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Saving decrypted block to disk at offset %llu\n",
1017 (unsigned long long) off);
1018 if ((off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)))
1020 GNUNET_asprintf (&dc->emsg,
1021 _("Failed to seek to offset %llu in file `%s': %s\n"),
1022 (unsigned long long) off, dc->filename,
1026 if (prc->size != GNUNET_DISK_file_write (fh, pt, prc->size))
1028 GNUNET_asprintf (&dc->emsg,
1030 ("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1031 (unsigned int) prc->size, (unsigned long long) off,
1032 dc->filename, STRERROR (errno));
1035 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1041 /* DBLOCK, update progress and try recursion if applicable */
1043 if (dr->offset < dc->offset)
1045 /* starting offset begins in the middle of pt,
1046 * do not count first bytes as progress */
1047 GNUNET_assert (app > (dc->offset - dr->offset));
1048 app -= (dc->offset - dr->offset);
1050 if (dr->offset + prc->size > dc->offset + dc->length)
1052 /* end of block is after relevant range,
1053 * do not count last bytes as progress */
1054 GNUNET_assert (app >
1055 (dr->offset + prc->size) - (dc->offset + dc->length));
1056 app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1058 dc->completed += app;
1060 /* do recursive download if option is set and either meta data
1061 * says it is a directory or if no meta data is given AND filename
1062 * ends in '.gnd' (top-level case) */
1063 if (is_recursive_download (dc))
1064 GNUNET_FS_directory_list_contents (prc->size, pt, off,
1065 &trigger_recursive_download, dc);
1068 GNUNET_assert (dc->completed <= dc->length);
1069 dr->state = BRS_DOWNLOAD_DOWN;
1070 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1071 pi.value.download.specifics.progress.data = pt;
1072 pi.value.download.specifics.progress.offset = dr->offset;
1073 pi.value.download.specifics.progress.data_len = prc->size;
1074 pi.value.download.specifics.progress.depth = dr->depth;
1075 pi.value.download.specifics.progress.trust_offered = 0;
1076 if (prc->last_transmission.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
1077 pi.value.download.specifics.progress.block_download_duration
1078 = GNUNET_TIME_absolute_get_duration (prc->last_transmission);
1080 pi.value.download.specifics.progress.block_download_duration
1081 = GNUNET_TIME_UNIT_ZERO; /* found locally */
1082 GNUNET_FS_download_make_status_ (&pi, dc);
1086 if (dc->completed == dc->length)
1088 /* download completed, signal */
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Download completed, truncating file to desired length %llu\n",
1091 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1093 /* truncate file to size (since we store IBlocks at the end) */
1094 if (dc->filename != NULL)
1097 truncate (dc->filename,
1098 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1099 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1102 GNUNET_assert (dr->depth == 0);
1103 check_completed (dc);
1107 /* bottom of the tree, no child downloads possible, just sync */
1108 GNUNET_FS_download_sync_ (dc);
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1114 dr->depth, (unsigned long long) dr->offset);
1115 GNUNET_assert (0 == (prc->size % sizeof (struct ContentHashKey)));
1116 chkarr = (struct ContentHashKey *) pt;
1117 for (i = dr->num_children - 1; i >= 0; i--)
1119 drc = dr->children[i];
1123 drc->chk = chkarr[drc->chk_idx];
1124 drc->state = BRS_CHK_SET;
1125 if (GNUNET_YES == dc->issue_requests)
1126 schedule_block_download (dc, drc);
1128 case BRS_RECONSTRUCT_DOWN:
1131 case BRS_RECONSTRUCT_META_UP:
1134 case BRS_RECONSTRUCT_UP:
1140 case BRS_DOWNLOAD_DOWN:
1143 case BRS_DOWNLOAD_UP:
1154 GNUNET_FS_download_sync_ (dc);
1159 GNUNET_DISK_file_close (fh);
1160 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1161 pi.value.download.specifics.error.message = dc->emsg;
1162 GNUNET_FS_download_make_status_ (&pi, dc);
1163 /* abort all pending requests */
1166 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1169 GNUNET_CLIENT_disconnect (dc->client);
1170 dc->in_receive = GNUNET_NO;
1172 GNUNET_FS_free_download_request_ (dc->top_request);
1173 dc->top_request = NULL;
1174 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1176 dc->pending_head = NULL;
1177 dc->pending_tail = NULL;
1178 GNUNET_FS_download_sync_ (dc);
1184 * Process a download result.
1186 * @param dc our download context
1187 * @param type type of the result
1188 * @param last_transmission when was this block requested the last time? (FOREVER if unknown/not applicable)
1189 * @param data the (encrypted) response
1190 * @param size size of data
1193 process_result (struct GNUNET_FS_DownloadContext *dc,
1194 enum GNUNET_BLOCK_Type type,
1195 struct GNUNET_TIME_Absolute last_transmission,
1196 const void *data, size_t size)
1198 struct ProcessResultClosure prc;
1204 prc.do_store = GNUNET_YES;
1205 prc.last_transmission = last_transmission;
1206 GNUNET_CRYPTO_hash (data, size, &prc.query);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Received result for query `%s' from `%s'-service\n",
1209 GNUNET_h2s (&prc.query), "FS");
1210 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active, &prc.query,
1211 &process_result_with_request,
1217 * Type of a function to call when we receive a message
1220 * @param cls closure
1221 * @param msg message received, NULL on timeout or fatal error
1224 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
1226 struct GNUNET_FS_DownloadContext *dc = cls;
1227 const struct ClientPutMessage *cm;
1230 if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1231 (sizeof (struct ClientPutMessage) > ntohs (msg->size)))
1233 GNUNET_break (msg == NULL);
1237 msize = ntohs (msg->size);
1238 cm = (const struct ClientPutMessage *) msg;
1239 process_result (dc, ntohl (cm->type),
1240 GNUNET_TIME_absolute_ntoh (cm->last_transmission), &cm[1],
1241 msize - sizeof (struct ClientPutMessage));
1242 if (dc->client == NULL)
1243 return; /* fatal error */
1244 /* continue receiving */
1245 GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1246 GNUNET_TIME_UNIT_FOREVER_REL);
1252 * We're ready to transmit a search request to the
1253 * file-sharing service. Do it. If there is
1254 * more than one request pending, try to send
1255 * multiple or request another transmission.
1257 * @param cls closure
1258 * @param size number of bytes available in buf
1259 * @param buf where the callee should write the message
1260 * @return number of bytes written to buf
1263 transmit_download_request (void *cls, size_t size, void *buf)
1265 struct GNUNET_FS_DownloadContext *dc = cls;
1267 struct SearchMessage *sm;
1268 struct DownloadRequest *dr;
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "Transmitting download request failed, trying to reconnect\n");
1278 GNUNET_assert (size >= sizeof (struct SearchMessage));
1281 while ((NULL != (dr = dc->pending_head)) &&
1282 (size >= msize + sizeof (struct SearchMessage)))
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Transmitting download request for `%s' to `%s'-service\n",
1286 GNUNET_h2s (&dr->chk.query), "FS");
1287 memset (sm, 0, sizeof (struct SearchMessage));
1288 sm->header.size = htons (sizeof (struct SearchMessage));
1289 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1290 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1291 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY);
1293 sm->options = htonl (GNUNET_FS_SEARCH_OPTION_NONE);
1295 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
1297 sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
1298 sm->anonymity_level = htonl (dc->anonymity);
1299 sm->target = dc->target.hashPubKey;
1300 sm->query = dr->chk.query;
1301 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1302 dr->is_pending = GNUNET_NO;
1303 msize += sizeof (struct SearchMessage);
1306 if (dc->pending_head != NULL)
1309 GNUNET_CLIENT_notify_transmit_ready (dc->client,
1310 sizeof (struct SearchMessage),
1311 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1313 &transmit_download_request, dc);
1314 GNUNET_assert (dc->th != NULL);
1316 if (GNUNET_NO == dc->in_receive)
1318 dc->in_receive = GNUNET_YES;
1319 GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1320 GNUNET_TIME_UNIT_FOREVER_REL);
1327 * Reconnect to the FS service and transmit our queries NOW.
1329 * @param cls our download context
1333 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1335 struct GNUNET_FS_DownloadContext *dc = cls;
1336 struct GNUNET_CLIENT_Connection *client;
1338 dc->task = GNUNET_SCHEDULER_NO_TASK;
1339 client = GNUNET_CLIENT_connect ("fs", dc->h->cfg);
1342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1343 "Connecting to `%s'-service failed, will try again.\n", "FS");
1347 dc->client = client;
1348 if (dc->pending_head != NULL)
1351 GNUNET_CLIENT_notify_transmit_ready (client,
1352 sizeof (struct SearchMessage),
1353 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1355 &transmit_download_request, dc);
1356 GNUNET_assert (dc->th != NULL);
1362 * Add entries to the pending list.
1364 * @param cls our download context
1366 * @param entry entry of type "struct DownloadRequest"
1370 retry_entry (void *cls, const GNUNET_HashCode * key, void *entry)
1372 struct GNUNET_FS_DownloadContext *dc = cls;
1373 struct DownloadRequest *dr = entry;
1377 GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
1378 dr->is_pending = GNUNET_YES;
1384 * We've lost our connection with the FS service.
1385 * Re-establish it and re-transmit all of our
1388 * @param dc download context that is having trouble
1391 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1394 if (NULL != dc->client)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Moving all requests back to pending list\n");
1400 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1403 /* full reset of the pending list */
1404 dc->pending_head = NULL;
1405 dc->pending_tail = NULL;
1406 GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1407 GNUNET_CLIENT_disconnect (dc->client);
1408 dc->in_receive = GNUNET_NO;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will try to reconnect in 1s\n");
1413 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1419 * We're allowed to ask the FS service for our blocks. Start the download.
1421 * @param cls the 'struct GNUNET_FS_DownloadContext'
1422 * @param client handle to use for communcation with FS (we must destroy it!)
1425 activate_fs_download (void *cls, struct GNUNET_CLIENT_Connection *client)
1427 struct GNUNET_FS_DownloadContext *dc = cls;
1428 struct GNUNET_FS_ProgressInfo pi;
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download activated\n");
1431 GNUNET_assert (NULL != client);
1432 GNUNET_assert (dc->client == NULL);
1433 GNUNET_assert (dc->th == NULL);
1434 dc->client = client;
1435 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1436 GNUNET_FS_download_make_status_ (&pi, dc);
1437 dc->pending_head = NULL;
1438 dc->pending_tail = NULL;
1439 GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "Asking for transmission to FS service\n");
1442 if (dc->pending_head != NULL)
1445 GNUNET_CLIENT_notify_transmit_ready (dc->client,
1446 sizeof (struct SearchMessage),
1447 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1449 &transmit_download_request, dc);
1450 GNUNET_assert (dc->th != NULL);
1456 * We must stop to ask the FS service for our blocks. Pause the download.
1458 * @param cls the 'struct GNUNET_FS_DownloadContext'
1461 deactivate_fs_download (void *cls)
1463 struct GNUNET_FS_DownloadContext *dc = cls;
1464 struct GNUNET_FS_ProgressInfo pi;
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download deactivated\n");
1469 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1472 if (NULL != dc->client)
1474 GNUNET_CLIENT_disconnect (dc->client);
1475 dc->in_receive = GNUNET_NO;
1478 dc->pending_head = NULL;
1479 dc->pending_tail = NULL;
1480 pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1481 GNUNET_FS_download_make_status_ (&pi, dc);
1486 * (recursively) Create a download request structure.
1488 * @param parent parent of the current entry
1489 * @param chk_idx index of the chk for this block in the parent block
1490 * @param depth depth of the current entry, 0 are the DBLOCKs,
1491 * top level block is 'dc->treedepth - 1'
1492 * @param dr_offset offset in the original file this block maps to
1493 * (as in, offset of the first byte of the first DBLOCK
1494 * in the subtree rooted in the returned download request tree)
1495 * @param file_start_offset desired starting offset for the download
1496 * in the original file; requesting tree should not contain
1497 * DBLOCKs prior to the file_start_offset
1498 * @param desired_length desired number of bytes the user wanted to access
1499 * (from file_start_offset). Resulting tree should not contain
1500 * DBLOCKs after file_start_offset + file_length.
1501 * @return download request tree for the given range of DBLOCKs at
1502 * the specified depth
1504 static struct DownloadRequest *
1505 create_download_request (struct DownloadRequest *parent,
1506 unsigned int chk_idx,
1508 uint64_t dr_offset, uint64_t file_start_offset,
1509 uint64_t desired_length)
1511 struct DownloadRequest *dr;
1513 unsigned int head_skip;
1514 uint64_t child_block_size;
1516 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1517 dr->parent = parent;
1519 dr->offset = dr_offset;
1520 dr->chk_idx = chk_idx;
1523 child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1525 /* calculate how many blocks at this level are not interesting
1526 * from the start (rounded down), either because of the requested
1527 * file offset or because this IBlock is further along */
1528 if (dr_offset < file_start_offset)
1529 head_skip = file_start_offset / child_block_size;
1533 /* calculate index of last block at this level that is interesting (rounded up) */
1534 dr->num_children = (file_start_offset + desired_length - dr_offset) / child_block_size;
1535 if (dr->num_children * child_block_size <
1536 file_start_offset + desired_length - dr_offset)
1537 dr->num_children++; /* round up */
1538 if (dr->num_children > CHK_PER_INODE)
1539 dr->num_children = CHK_PER_INODE; /* cap at max */
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Block at offset %llu and depth %u has %u children\n",
1542 (unsigned long long) dr_offset,
1546 /* now we can get the total number of *interesting* children for this block */
1547 dr->num_children -= head_skip;
1549 /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1550 GNUNET_assert (dr->num_children > 0);
1553 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1554 for (i = 0; i < dr->num_children; i++)
1556 create_download_request (dr, i + head_skip, depth - 1,
1557 dr_offset + (i + head_skip) * child_block_size,
1558 file_start_offset, desired_length);
1565 * Continuation after a possible attempt to reconstruct
1566 * the current IBlock from the existing file.
1568 * @param cls the 'struct ReconstructContext'
1569 * @param tc scheduler context
1572 reconstruct_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1574 struct GNUNET_FS_DownloadContext *dc = cls;
1576 /* clean up state from tree encoder */
1577 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1579 GNUNET_SCHEDULER_cancel (dc->task);
1580 dc->task = GNUNET_SCHEDULER_NO_TASK;
1582 if (dc->rfh != NULL)
1584 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1587 /* start "normal" download */
1588 dc->issue_requests = GNUNET_YES;
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Starting normal download\n");
1591 schedule_block_download (dc, dc->top_request);
1596 * Task requesting the next block from the tree encoder.
1598 * @param cls the 'struct GNUJNET_FS_DownloadContext' we're processing
1599 * @param tc task context
1602 get_next_block (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1604 struct GNUNET_FS_DownloadContext *dc = cls;
1606 dc->task = GNUNET_SCHEDULER_NO_TASK;
1607 GNUNET_FS_tree_encoder_next (dc->te);
1613 * Function called asking for the current (encoded)
1614 * block to be processed. After processing the
1615 * client should either call "GNUNET_FS_tree_encode_next"
1616 * or (on error) "GNUNET_FS_tree_encode_finish".
1618 * This function checks if the content on disk matches
1619 * the expected content based on the URI.
1621 * @param cls closure
1622 * @param chk content hash key for the block
1623 * @param offset offset of the block
1624 * @param depth depth of the block, 0 for DBLOCK
1625 * @param type type of the block (IBLOCK or DBLOCK)
1626 * @param block the (encrypted) block
1627 * @param block_size size of block (in bytes)
1630 reconstruct_cb (void *cls, const struct ContentHashKey *chk, uint64_t offset,
1631 unsigned int depth, enum GNUNET_BLOCK_Type type,
1632 const void *block, uint16_t block_size)
1634 struct GNUNET_FS_DownloadContext *dc = cls;
1635 struct GNUNET_FS_ProgressInfo pi;
1636 struct DownloadRequest *dr;
1640 /* find corresponding request entry */
1641 dr = dc->top_request;
1642 while (dr->depth > depth)
1644 GNUNET_assert (dr->num_children > 0);
1645 blen = GNUNET_FS_tree_compute_tree_size (dr->depth - 1);
1646 chld = (offset - dr->offset) / blen;
1647 if (chld < dr->children[0]->chk_idx)
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Block %u < %u irrelevant for our range\n",
1652 dr->children[dr->num_children-1]->chk_idx);
1653 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1654 return; /* irrelevant block */
1656 if (chld > dr->children[dr->num_children-1]->chk_idx)
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Block %u > %u irrelevant for our range\n",
1661 dr->children[dr->num_children-1]->chk_idx);
1662 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1663 return; /* irrelevant block */
1665 dr = dr->children[chld - dr->children[0]->chk_idx];
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Matched TE block with request at offset %llu and depth %u in state %d\n",
1669 (unsigned long long) dr->offset,
1672 /* FIXME: this code needs more testing and might
1673 need to handle more states... */
1678 case BRS_RECONSTRUCT_DOWN:
1680 case BRS_RECONSTRUCT_META_UP:
1682 case BRS_RECONSTRUCT_UP:
1685 if (0 == memcmp (chk, &dr->chk, sizeof (struct ContentHashKey)))
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688 "Reconstruction succeeded, can use block at offset %llu, depth %u\n",
1689 (unsigned long long) offset,
1691 /* block matches, hence tree below matches;
1692 * this request is done! */
1693 dr->state = BRS_DOWNLOAD_UP;
1694 (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &dr->chk.query, dr);
1695 if (GNUNET_YES == dr->is_pending)
1697 GNUNET_break (0); /* how did we get here? */
1698 GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1699 dr->is_pending = GNUNET_NO;
1701 /* calculate how many bytes of payload this block
1703 blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1704 /* how many of those bytes are in the requested range? */
1705 blen = GNUNET_MIN (blen, dc->length + dc->offset - dr->offset);
1706 /* signal progress */
1707 dc->completed += blen;
1708 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1709 pi.value.download.specifics.progress.data = NULL;
1710 pi.value.download.specifics.progress.offset = offset;
1711 pi.value.download.specifics.progress.data_len = 0;
1712 pi.value.download.specifics.progress.depth = 0;
1713 pi.value.download.specifics.progress.trust_offered = 0;
1714 pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
1715 GNUNET_FS_download_make_status_ (&pi, dc);
1716 /* FIXME: duplicated code from 'process_result_with_request - refactor */
1717 if (dc->completed == dc->length)
1719 /* download completed, signal */
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1721 "Download completed, truncating file to desired length %llu\n",
1722 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1724 /* truncate file to size (since we store IBlocks at the end) */
1725 if (dc->filename != NULL)
1728 truncate (dc->filename,
1729 GNUNET_ntohll (dc->uri->data.chk.file_length)))
1730 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Reconstruction failed, need to download block at offset %llu, depth %u\n",
1738 (unsigned long long) offset,
1741 case BRS_DOWNLOAD_DOWN:
1743 case BRS_DOWNLOAD_UP:
1751 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1752 if ((dr == dc->top_request) && (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'
1800 * @param tc scheduler context
1803 GNUNET_FS_download_start_task_ (void *cls,
1804 const struct GNUNET_SCHEDULER_TaskContext *tc)
1806 struct GNUNET_FS_DownloadContext *dc = cls;
1807 struct GNUNET_FS_ProgressInfo pi;
1808 struct GNUNET_DISK_FileHandle *fh;
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start task running...\n");
1811 dc->task = GNUNET_SCHEDULER_NO_TASK;
1812 if (dc->length == 0)
1814 /* no bytes required! */
1815 if (dc->filename != NULL)
1817 fh = GNUNET_DISK_file_open (dc->filename,
1818 GNUNET_DISK_OPEN_READWRITE |
1819 GNUNET_DISK_OPEN_CREATE |
1821 GNUNET_FS_uri_chk_get_file_size (dc->uri)) ?
1822 GNUNET_DISK_OPEN_TRUNCATE : 0),
1823 GNUNET_DISK_PERM_USER_READ |
1824 GNUNET_DISK_PERM_USER_WRITE |
1825 GNUNET_DISK_PERM_GROUP_READ |
1826 GNUNET_DISK_PERM_OTHER_READ);
1827 GNUNET_DISK_file_close (fh);
1829 GNUNET_FS_download_sync_ (dc);
1830 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1831 pi.value.download.specifics.start.meta = dc->meta;
1832 GNUNET_FS_download_make_status_ (&pi, dc);
1833 check_completed (dc);
1836 if (dc->emsg != NULL)
1838 if (dc->top_request == NULL)
1841 create_download_request (NULL, 0, dc->treedepth - 1, 0, dc->offset,
1843 dc->top_request->state = BRS_CHK_SET;
1844 dc->top_request->chk =
1846 chk) ? dc->uri->data.chk.chk : dc->uri->data.loc.fi.chk;
1848 GNUNET_FS_download_sync_ (dc);
1849 if (NULL != dc->search)
1850 GNUNET_FS_search_result_sync_ (dc->search);
1851 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1852 pi.value.download.specifics.start.meta = dc->meta;
1853 GNUNET_FS_download_make_status_ (&pi, dc);
1855 GNUNET_FS_download_start_downloading_ (dc);
1856 /* attempt reconstruction from disk */
1857 if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
1859 GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
1860 GNUNET_DISK_PERM_NONE);
1861 if (dc->top_request->state == BRS_CHK_SET)
1863 if (dc->rfh != NULL)
1865 /* first, try top-down */
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "Trying top-down reconstruction for `%s'\n", dc->filename);
1868 try_top_down_reconstruction (dc, dc->top_request);
1869 switch (dc->top_request->state)
1873 case BRS_DOWNLOAD_DOWN:
1874 break; /* normal, some blocks already down */
1875 case BRS_DOWNLOAD_UP:
1876 /* already done entirely, party! */
1877 if (dc->rfh != NULL)
1879 /* avoid hanging on to file handle longer than
1881 GNUNET_DISK_file_close (dc->rfh);
1886 GNUNET_asprintf (&dc->emsg, _("Invalid URI"));
1887 GNUNET_FS_download_sync_ (dc);
1888 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1889 pi.value.download.specifics.error.message = dc->emsg;
1890 GNUNET_FS_download_make_status_ (&pi, dc);
1898 /* attempt reconstruction from meta data */
1899 if ((GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
1904 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
1906 GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
1907 GNUNET_CONTAINER_meta_data_iterate (dc->meta, &match_full_data, dc);
1908 if (dc->top_request->state == BRS_DOWNLOAD_UP)
1910 if (dc->rfh != NULL)
1912 /* avoid hanging on to file handle longer than
1914 GNUNET_DISK_file_close (dc->rfh);
1917 return; /* finished, status update was already done for us */
1920 if (dc->rfh != NULL)
1922 /* finally, actually run bottom-up */
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "Trying bottom-up reconstruction of file `%s'\n", dc->filename);
1926 GNUNET_FS_tree_encoder_create (dc->h,
1927 GNUNET_FS_uri_chk_get_file_size (dc->uri),
1929 &reconstruct_cb, NULL,
1931 dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1935 /* simple, top-level download */
1936 dc->issue_requests = GNUNET_YES;
1937 schedule_block_download (dc, dc->top_request);
1939 if (dc->top_request->state == BRS_DOWNLOAD_UP)
1940 check_completed (dc);
1945 * Create SUSPEND event for the given download operation
1946 * and then clean up our state (without stop signal).
1948 * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1951 GNUNET_FS_download_signal_suspend_ (void *cls)
1953 struct GNUNET_FS_DownloadContext *dc = cls;
1954 struct GNUNET_FS_ProgressInfo pi;
1956 if (dc->top != NULL)
1957 GNUNET_FS_end_top (dc->h, dc->top);
1958 while (NULL != dc->child_head)
1959 GNUNET_FS_download_signal_suspend_ (dc->child_head);
1960 if (dc->search != NULL)
1962 dc->search->download = NULL;
1965 if (dc->job_queue != NULL)
1967 GNUNET_FS_dequeue_ (dc->job_queue);
1968 dc->job_queue = NULL;
1970 if (dc->parent != NULL)
1971 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
1973 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1975 GNUNET_SCHEDULER_cancel (dc->task);
1976 dc->task = GNUNET_SCHEDULER_NO_TASK;
1978 pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1979 GNUNET_FS_download_make_status_ (&pi, dc);
1982 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1985 if (dc->rfh != NULL)
1987 GNUNET_DISK_file_close (dc->rfh);
1990 GNUNET_FS_free_download_request_ (dc->top_request);
1991 if (dc->active != NULL)
1993 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1996 GNUNET_free_non_null (dc->filename);
1997 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1998 GNUNET_FS_uri_destroy (dc->uri);
1999 GNUNET_free_non_null (dc->temp_filename);
2000 GNUNET_free_non_null (dc->serialization);
2006 * Helper function to setup the download context.
2008 * @param h handle to the file sharing subsystem
2009 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2010 * @param meta known metadata for the file (can be NULL)
2011 * @param filename where to store the file, maybe NULL (then no file is
2012 * created on disk and data must be grabbed from the callbacks)
2013 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2014 * can be NULL (in which case we will pick a name if needed); the temporary file
2015 * may already exist, in which case we will try to use the data that is there and
2016 * if it is not what is desired, will overwrite it
2017 * @param offset at what offset should we start the download (typically 0)
2018 * @param length how many bytes should be downloaded starting at offset
2019 * @param anonymity anonymity level to use for the download
2020 * @param options various options
2021 * @param cctx initial value for the client context for this download
2022 * @return context that can be used to control this download
2024 struct GNUNET_FS_DownloadContext *
2025 create_download_context (struct GNUNET_FS_Handle *h,
2026 const struct GNUNET_FS_Uri *uri,
2027 const struct GNUNET_CONTAINER_MetaData *meta,
2028 const char *filename, const char *tempname,
2029 uint64_t offset, uint64_t length, uint32_t anonymity,
2030 enum GNUNET_FS_DownloadOptions options, void *cctx)
2032 struct GNUNET_FS_DownloadContext *dc;
2034 GNUNET_assert (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri));
2035 if ((offset + length < offset) ||
2036 (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)))
2041 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2043 dc->uri = GNUNET_FS_uri_dup (uri);
2044 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
2045 dc->client_info = cctx;
2046 dc->start_time = GNUNET_TIME_absolute_get ();
2047 if (NULL != filename)
2049 dc->filename = GNUNET_strdup (filename);
2050 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2051 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, GNUNET_YES));
2053 if (GNUNET_FS_uri_test_loc (dc->uri))
2054 GNUNET_assert (GNUNET_OK ==
2055 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2056 dc->offset = offset;
2057 dc->length = length;
2058 dc->anonymity = anonymity;
2059 dc->options = options;
2061 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2063 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2064 if ((NULL == filename) && (is_recursive_download (dc)))
2066 if (NULL != tempname)
2067 dc->temp_filename = GNUNET_strdup (tempname);
2069 dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "Starting download `%s' of %llu bytes with tree depth %u\n",
2074 (unsigned long long) length,
2076 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2082 * Download parts of a file. Note that this will store
2083 * the blocks at the respective offset in the given file. Also, the
2084 * download is still using the blocking of the underlying FS
2085 * encoding. As a result, the download may *write* outside of the
2086 * given boundaries (if offset and length do not match the 32k FS
2087 * block boundaries). <p>
2089 * This function should be used to focus a download towards a
2090 * particular portion of the file (optimization), not to strictly
2091 * limit the download to exactly those bytes.
2093 * @param h handle to the file sharing subsystem
2094 * @param uri the URI of the file (determines what to download); CHK or LOC URI
2095 * @param meta known metadata for the file (can be NULL)
2096 * @param filename where to store the file, maybe NULL (then no file is
2097 * created on disk and data must be grabbed from the callbacks)
2098 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2099 * can be NULL (in which case we will pick a name if needed); the temporary file
2100 * may already exist, in which case we will try to use the data that is there and
2101 * if it is not what is desired, will overwrite it
2102 * @param offset at what offset should we start the download (typically 0)
2103 * @param length how many bytes should be downloaded starting at offset
2104 * @param anonymity anonymity level to use for the download
2105 * @param options various options
2106 * @param cctx initial value for the client context for this download
2107 * @param parent parent download to associate this download with (use NULL
2108 * for top-level downloads; useful for manually-triggered recursive downloads)
2109 * @return context that can be used to control this download
2111 struct GNUNET_FS_DownloadContext *
2112 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2113 const struct GNUNET_FS_Uri *uri,
2114 const struct GNUNET_CONTAINER_MetaData *meta,
2115 const char *filename, const char *tempname,
2116 uint64_t offset, uint64_t length, uint32_t anonymity,
2117 enum GNUNET_FS_DownloadOptions options, void *cctx,
2118 struct GNUNET_FS_DownloadContext *parent)
2120 struct GNUNET_FS_DownloadContext *dc;
2122 dc = create_download_context (h, uri, meta, filename, tempname,
2123 offset, length, anonymity, options, cctx);
2126 dc->parent = parent;
2128 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2131 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2137 * Download parts of a file based on a search result. The download
2138 * will be associated with the search result (and the association
2139 * will be preserved when serializing/deserializing the state).
2140 * If the search is stopped, the download will not be aborted but
2141 * be 'promoted' to a stand-alone download.
2143 * As with the other download function, this will store
2144 * the blocks at the respective offset in the given file. Also, the
2145 * download is still using the blocking of the underlying FS
2146 * encoding. As a result, the download may *write* outside of the
2147 * given boundaries (if offset and length do not match the 32k FS
2148 * block boundaries). <p>
2150 * The given range can be used to focus a download towards a
2151 * particular portion of the file (optimization), not to strictly
2152 * limit the download to exactly those bytes.
2154 * @param h handle to the file sharing subsystem
2155 * @param sr the search result to use for the download (determines uri and
2156 * meta data and associations)
2157 * @param filename where to store the file, maybe NULL (then no file is
2158 * created on disk and data must be grabbed from the callbacks)
2159 * @param tempname where to store temporary file data, not used if filename is non-NULL;
2160 * can be NULL (in which case we will pick a name if needed); the temporary file
2161 * may already exist, in which case we will try to use the data that is there and
2162 * if it is not what is desired, will overwrite it
2163 * @param offset at what offset should we start the download (typically 0)
2164 * @param length how many bytes should be downloaded starting at offset
2165 * @param anonymity anonymity level to use for the download
2166 * @param options various download options
2167 * @param cctx initial value for the client context for this download
2168 * @return context that can be used to control this download
2170 struct GNUNET_FS_DownloadContext *
2171 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2172 struct GNUNET_FS_SearchResult *sr,
2173 const char *filename,
2174 const char *tempname, uint64_t offset,
2175 uint64_t length, uint32_t anonymity,
2176 enum GNUNET_FS_DownloadOptions options,
2179 struct GNUNET_FS_DownloadContext *dc;
2181 if ((NULL == sr) || (NULL != sr->download))
2186 dc = create_download_context (h, sr->uri, sr->meta, filename, tempname,
2187 offset, length, anonymity, options, cctx);
2192 if (NULL != sr->probe_ctx)
2194 GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2195 sr->probe_ctx = NULL;
2202 * Start the downloading process (by entering the queue).
2204 * @param dc our download context
2207 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2209 if (dc->completed == dc->length)
2211 GNUNET_assert (dc->job_queue == NULL);
2213 GNUNET_FS_queue_ (dc->h, &activate_fs_download, &deactivate_fs_download,
2214 dc, (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE,
2215 (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
2216 ? GNUNET_FS_QUEUE_PRIORITY_NORMAL
2217 : GNUNET_FS_QUEUE_PRIORITY_PROBE);
2222 * Stop a download (aborts if download is incomplete).
2224 * @param dc handle for the download
2225 * @param do_delete delete files of incomplete downloads
2228 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc, int do_delete)
2230 struct GNUNET_FS_ProgressInfo pi;
2232 int search_was_null;
2234 if (dc->top != NULL)
2235 GNUNET_FS_end_top (dc->h, dc->top);
2236 if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2238 GNUNET_SCHEDULER_cancel (dc->task);
2239 dc->task = GNUNET_SCHEDULER_NO_TASK;
2241 search_was_null = (dc->search == NULL);
2242 if (dc->search != NULL)
2244 dc->search->download = NULL;
2245 GNUNET_FS_search_result_sync_ (dc->search);
2248 if (dc->job_queue != NULL)
2250 GNUNET_FS_dequeue_ (dc->job_queue);
2251 dc->job_queue = NULL;
2255 GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2258 have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2259 while (NULL != dc->child_head)
2260 GNUNET_FS_download_stop (dc->child_head, do_delete);
2261 if (dc->parent != NULL)
2262 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
2264 if (dc->serialization != NULL)
2265 GNUNET_FS_remove_sync_file_ (dc->h,
2266 ((dc->parent != NULL) ||
2267 (! search_was_null)) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2268 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2270 if ((GNUNET_YES == have_children) && (dc->parent == NULL))
2271 GNUNET_FS_remove_sync_dir_ (dc->h,
2272 (! search_was_null) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2273 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2275 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2276 GNUNET_FS_download_make_status_ (&pi, dc);
2277 GNUNET_FS_free_download_request_ (dc->top_request);
2278 dc->top_request = NULL;
2279 if (dc->active != NULL)
2281 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2284 if (dc->filename != NULL)
2286 if ((dc->completed != dc->length) && (GNUNET_YES == do_delete))
2288 if (0 != UNLINK (dc->filename))
2289 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2292 GNUNET_free (dc->filename);
2294 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2295 GNUNET_FS_uri_destroy (dc->uri);
2296 if (NULL != dc->temp_filename)
2298 if (0 != UNLINK (dc->temp_filename))
2299 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
2301 GNUNET_free (dc->temp_filename);
2303 GNUNET_free_non_null (dc->serialization);
2307 /* end of fs_download.c */