2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, 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 * - handle recursive downloads (need directory &
27 * fs-level download-parallelism management)
28 * - handle recursive downloads where directory file is
29 * NOT saved on disk (need temporary file instead then!)
30 * - location URI suppport (can wait, easy)
31 * - check if blocks exist already (can wait, easy)
32 * - check if iblocks can be computed from existing blocks (can wait, hard)
33 * - persistence (can wait)
36 #include "gnunet_constants.h"
37 #include "gnunet_fs_service.h"
41 #define DEBUG_DOWNLOAD GNUNET_NO
44 * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
45 * only have to truncate the file once we're done).
47 * Given the offset of a block (with respect to the DBLOCKS) and its
48 * depth, return the offset where we would store this block in the
51 * @param fsize overall file size
52 * @param off offset of the block in the file
53 * @param depth depth of the block in the tree
54 * @param treedepth maximum depth of the tree
55 * @return off for DBLOCKS (depth == treedepth),
56 * otherwise an offset past the end
57 * of the file that does not overlap
58 * with the range for any other block
61 compute_disk_offset (uint64_t fsize,
64 unsigned int treedepth)
67 uint64_t lsize; /* what is the size of all IBlocks for depth "i"? */
68 uint64_t loff; /* where do IBlocks for depth "i" start? */
69 unsigned int ioff; /* which IBlock corresponds to "off" at depth "i"? */
71 if (depth == treedepth)
73 /* first IBlocks start at the end of file, rounded up
74 to full DBLOCK_SIZE */
75 loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
76 lsize = ( (fsize + DBLOCK_SIZE-1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
77 GNUNET_assert (0 == (off % DBLOCK_SIZE));
78 ioff = (off / DBLOCK_SIZE);
79 for (i=treedepth-1;i>depth;i--)
82 lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
83 GNUNET_assert (lsize > 0);
84 GNUNET_assert (0 == (ioff % CHK_PER_INODE));
85 ioff /= CHK_PER_INODE;
87 return loff + ioff * sizeof (struct ContentHashKey);
92 * Given a file of the specified treedepth and a block at the given
93 * offset and depth, calculate the offset for the CHK at the given
96 * @param offset the offset of the first
97 * DBLOCK in the subtree of the
99 * @param depth the depth of the IBLOCK in the tree
100 * @param treedepth overall depth of the tree
101 * @param k which CHK in the IBLOCK are we
103 * @return offset if k=0, otherwise an appropriately
104 * larger value (i.e., if depth = treedepth-1,
105 * the returned value should be offset+DBLOCK_SIZE)
108 compute_dblock_offset (uint64_t offset,
110 unsigned int treedepth,
114 uint64_t lsize; /* what is the size of the sum of all DBlocks
115 that a CHK at depth i corresponds to? */
117 if (depth == treedepth)
120 for (i=treedepth-1;i>depth;i--)
121 lsize *= CHK_PER_INODE;
122 return offset + k * lsize;
127 * Fill in all of the generic fields for
130 * @param pi structure to fill in
131 * @param dc overall download context
134 make_download_status (struct GNUNET_FS_ProgressInfo *pi,
135 struct GNUNET_FS_DownloadContext *dc)
137 pi->value.download.dc = dc;
138 pi->value.download.cctx
140 pi->value.download.pctx
141 = (dc->parent == NULL) ? NULL : dc->parent->client_info;
142 pi->value.download.uri
144 pi->value.download.filename
146 pi->value.download.size
148 pi->value.download.duration
149 = GNUNET_TIME_absolute_get_duration (dc->start_time);
150 pi->value.download.completed
152 pi->value.download.anonymity
154 pi->value.download.eta
155 = GNUNET_TIME_calculate_eta (dc->start_time,
161 * We're ready to transmit a search request to the
162 * file-sharing service. Do it. If there is
163 * more than one request pending, try to send
164 * multiple or request another transmission.
167 * @param size number of bytes available in buf
168 * @param buf where the callee should write the message
169 * @return number of bytes written to buf
172 transmit_download_request (void *cls,
178 * Schedule the download of the specified
181 * @param dc overall download this block belongs to
182 * @param chk content-hash-key of the block
183 * @param offset offset of the block in the file
184 * (for IBlocks, the offset is the lowest
185 * offset of any DBlock in the subtree under
187 * @param depth depth of the block, 0 is the root of the tree
190 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
191 const struct ContentHashKey *chk,
195 struct DownloadRequest *sm;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Scheduling download at offset %llu and depth %u for `%s'\n",
201 (unsigned long long) offset,
203 GNUNET_h2s (&chk->query));
205 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
209 if ( (dc->old_file_size > off) &&
210 (dc->handle != NULL) &&
212 GNUNET_DISK_file_seek (dc->handle,
214 GNUNET_DISK_SEEK_SET) ) )
216 // FIXME: check if block exists on disk!
217 // (read block, encode, compare with
218 // query; if matches, simply return)
220 if (depth < dc->treedepth)
222 // FIXME: try if we could
223 // reconstitute this IBLOCK
224 // from the existing blocks on disk (can wait)
225 // (read block(s), encode, compare with
226 // query; if matches, simply return)
228 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
232 sm->is_pending = GNUNET_YES;
233 sm->next = dc->pending;
235 GNUNET_CONTAINER_multihashmap_put (dc->active,
238 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
240 if ( (dc->th == NULL) &&
241 (dc->client != NULL) )
242 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
243 sizeof (struct SearchMessage),
244 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
246 &transmit_download_request,
252 * We've lost our connection with the FS service.
253 * Re-establish it and re-transmit all of our
256 * @param dc download context that is having trouble
259 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
263 * Compute how many bytes of data should be stored in
264 * the specified node.
266 * @param fsize overall file size
267 * @param totaldepth depth of the entire tree
268 * @param offset offset of the node
269 * @param depth depth of the node
270 * @return number of bytes stored in this node
273 calculate_block_size (uint64_t fsize,
274 unsigned int totaldepth,
284 GNUNET_assert (offset < fsize);
285 if (depth == totaldepth)
288 if (offset + ret > fsize)
289 ret = (size_t) (fsize - offset);
294 for (i = totaldepth-1; i > depth; i--)
295 rsize *= CHK_PER_INODE;
296 epos = offset + rsize * CHK_PER_INODE;
297 GNUNET_assert (epos > offset);
300 /* round up when computing #CHKs in our IBlock */
301 chks = (epos - offset + rsize - 1) / rsize;
302 GNUNET_assert (chks <= CHK_PER_INODE);
303 return chks * sizeof (struct ContentHashKey);
308 * Closure for iterator processing results.
310 struct ProcessResultClosure
316 GNUNET_HashCode query;
319 * Data found in P2P network.
324 * Our download context.
326 struct GNUNET_FS_DownloadContext *dc;
329 * Number of bytes in data.
342 * We found an entry in a directory. Check if the respective child
343 * already exists and if not create the respective child download.
345 * @param cls the parent download
346 * @param filename name of the file in the directory
347 * @param uri URI of the file (CHK or LOC)
348 * @param meta meta data of the file
349 * @param length number of bytes in data
350 * @param data contents of the file (or NULL if they were not inlined)
353 trigger_recursive_download (void *cls,
354 const char *filename,
355 const struct GNUNET_FS_Uri *uri,
356 const struct GNUNET_CONTAINER_MetaData *meta,
360 struct GNUNET_FS_DownloadContext *dc = cls;
361 struct GNUNET_FS_DownloadContext *cpos;
364 return; /* entry for the directory itself */
365 cpos = dc->child_head;
368 if ( (GNUNET_FS_uri_test_equal (uri,
370 ( (filename != NULL) &&
371 (0 == strcmp (cpos->filename,
377 return; /* already exists */
380 /* determine on-disk filename, write data! */
381 GNUNET_break (0); // FIXME: not implemented
383 /* FIXME: filename MAY be NULL => make one up! */
384 GNUNET_FS_download_start (dc->h,
387 filename, /* FIXME: prepend directory name! */
389 GNUNET_FS_uri_chk_get_file_size (uri),
398 * We're done downloading a directory. Open the file and
399 * trigger all of the (remaining) child downloads.
401 * @param dc context of download that just completed
404 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
409 struct GNUNET_DISK_FileHandle *h;
410 struct GNUNET_DISK_MapHandle *m;
412 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
413 size = (size_t) size64;
414 if (size64 != (uint64_t) size)
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
420 if (dc->filename != NULL)
422 h = GNUNET_DISK_file_open (dc->filename,
423 GNUNET_DISK_OPEN_READ,
424 GNUNET_DISK_PERM_NONE);
428 /* FIXME: need to initialize (and use) temp_filename
429 in various places in order for this assertion to
430 not fail; right now, it will always fail! */
431 GNUNET_assert (dc->temp_filename != NULL);
432 h = GNUNET_DISK_file_open (dc->temp_filename,
433 GNUNET_DISK_OPEN_READ,
434 GNUNET_DISK_PERM_NONE);
438 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("Directory too large for system address space\n"));
446 GNUNET_FS_directory_list_contents (size,
449 &trigger_recursive_download,
451 GNUNET_DISK_file_unmap (m);
453 GNUNET_DISK_file_close (h);
454 if (dc->filename == NULL)
456 if (0 != UNLINK (dc->temp_filename))
457 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
460 GNUNET_free (dc->temp_filename);
461 dc->temp_filename = NULL;
467 * Iterator over entries in the pending requests in the 'active' map for the
468 * reply that we just got.
470 * @param cls closure (our 'struct ProcessResultClosure')
471 * @param key query for the given value / request
472 * @param value value in the hash map (a 'struct DownloadRequest')
473 * @return GNUNET_YES (we should continue to iterate); unless serious error
476 process_result_with_request (void *cls,
477 const GNUNET_HashCode * key,
480 struct ProcessResultClosure *prc = cls;
481 struct DownloadRequest *sm = value;
482 struct GNUNET_FS_DownloadContext *dc = prc->dc;
483 struct GNUNET_CRYPTO_AesSessionKey skey;
484 struct GNUNET_CRYPTO_AesInitializationVector iv;
486 struct GNUNET_FS_ProgressInfo pi;
490 struct ContentHashKey *chk;
493 if (prc->size != calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
501 calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
507 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
509 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
510 make_download_status (&pi, dc);
511 pi.value.download.specifics.error.message = dc->emsg;
512 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
514 /* abort all pending requests */
517 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
520 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
524 GNUNET_assert (GNUNET_YES ==
525 GNUNET_CONTAINER_multihashmap_remove (dc->active,
528 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
529 GNUNET_CRYPTO_aes_decrypt (prc->data,
534 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
539 if ( (NULL != dc->handle) &&
540 ( (sm->depth == dc->treedepth) ||
541 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Saving decrypted block to disk at offset %llu\n",
547 (unsigned long long) off);
550 GNUNET_DISK_file_seek (dc->handle,
552 GNUNET_DISK_SEEK_SET) ) )
553 GNUNET_asprintf (&emsg,
554 _("Failed to seek to offset %llu in file `%s': %s\n"),
555 (unsigned long long) off,
558 else if (prc->size !=
559 GNUNET_DISK_file_write (dc->handle,
562 GNUNET_asprintf (&emsg,
563 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
564 (unsigned int) prc->size,
565 (unsigned long long) off,
571 // FIXME: make persistent
574 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
575 make_download_status (&pi, dc);
576 pi.value.download.specifics.error.message = emsg;
577 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
580 /* abort all pending requests */
583 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
586 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
592 if (sm->depth == dc->treedepth)
595 if (sm->offset < dc->offset)
597 /* starting offset begins in the middle of pt,
598 do not count first bytes as progress */
599 GNUNET_assert (app > (dc->offset - sm->offset));
600 app -= (dc->offset - sm->offset);
602 if (sm->offset + prc->size > dc->offset + dc->length)
604 /* end of block is after relevant range,
605 do not count last bytes as progress */
606 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
607 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
609 dc->completed += app;
611 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
612 (GNUNET_NO != GNUNET_FS_meta_data_test_for_directory (dc->meta)) )
614 GNUNET_FS_directory_list_contents (prc->size,
617 &trigger_recursive_download,
623 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
624 make_download_status (&pi, dc);
625 pi.value.download.specifics.progress.data = pt;
626 pi.value.download.specifics.progress.offset = sm->offset;
627 pi.value.download.specifics.progress.data_len = prc->size;
628 pi.value.download.specifics.progress.depth = sm->depth;
629 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
631 GNUNET_assert (dc->completed <= dc->length);
632 if (dc->completed == dc->length)
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Download completed, truncating file to desired length %llu\n",
637 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
639 /* truncate file to size (since we store IBlocks at the end) */
640 if (dc->handle != NULL)
642 GNUNET_DISK_file_close (dc->handle);
644 if (0 != truncate (dc->filename,
645 GNUNET_ntohll (dc->uri->data.chk.file_length)))
646 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
651 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
652 (GNUNET_NO != GNUNET_FS_meta_data_test_for_directory (dc->meta)) )
653 full_recursive_download (dc);
654 if (dc->child_head == NULL)
656 /* signal completion */
657 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
658 make_download_status (&pi, dc);
659 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
662 GNUNET_assert (sm->depth == dc->treedepth);
664 // FIXME: make persistent
665 if (sm->depth == dc->treedepth)
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
674 (unsigned long long) sm->offset);
676 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
677 chk = (struct ContentHashKey*) pt;
678 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
680 off = compute_dblock_offset (sm->offset,
684 if ( (off + DBLOCK_SIZE >= dc->offset) &&
685 (off < dc->offset + dc->length) )
686 schedule_block_download (dc,
697 * Process a download result.
699 * @param dc our download context
700 * @param type type of the result
701 * @param data the (encrypted) response
702 * @param size size of data
705 process_result (struct GNUNET_FS_DownloadContext *dc,
710 struct ProcessResultClosure prc;
716 GNUNET_CRYPTO_hash (data, size, &prc.query);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Received result for query `%s' from `%s'-service\n",
720 GNUNET_h2s (&prc.query),
723 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
725 &process_result_with_request,
731 * Type of a function to call when we receive a message
735 * @param msg message received, NULL on timeout or fatal error
738 receive_results (void *cls,
739 const struct GNUNET_MessageHeader * msg)
741 struct GNUNET_FS_DownloadContext *dc = cls;
742 const struct PutMessage *cm;
745 if ( (NULL == msg) ||
746 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
747 (sizeof (struct PutMessage) > ntohs(msg->size)) )
749 GNUNET_break (msg == NULL);
753 msize = ntohs(msg->size);
754 cm = (const struct PutMessage*) msg;
758 msize - sizeof (struct PutMessage));
759 if (dc->client == NULL)
760 return; /* fatal error */
761 /* continue receiving */
762 GNUNET_CLIENT_receive (dc->client,
765 GNUNET_TIME_UNIT_FOREVER_REL);
771 * We're ready to transmit a search request to the
772 * file-sharing service. Do it. If there is
773 * more than one request pending, try to send
774 * multiple or request another transmission.
777 * @param size number of bytes available in buf
778 * @param buf where the callee should write the message
779 * @return number of bytes written to buf
782 transmit_download_request (void *cls,
786 struct GNUNET_FS_DownloadContext *dc = cls;
788 struct SearchMessage *sm;
796 GNUNET_assert (size >= sizeof (struct SearchMessage));
799 while ( (dc->pending != NULL) &&
800 (size > msize + sizeof (struct SearchMessage)) )
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "Transmitting download request for `%s' to `%s'-service\n",
805 GNUNET_h2s (&dc->pending->chk.query),
808 memset (sm, 0, sizeof (struct SearchMessage));
809 sm->header.size = htons (sizeof (struct SearchMessage));
810 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
811 if (dc->pending->depth == dc->treedepth)
812 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_DBLOCK);
814 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_IBLOCK);
815 sm->anonymity_level = htonl (dc->anonymity);
816 sm->target = dc->target.hashPubKey;
817 sm->query = dc->pending->chk.query;
818 dc->pending->is_pending = GNUNET_NO;
819 dc->pending = dc->pending->next;
820 msize += sizeof (struct SearchMessage);
823 if (dc->pending != NULL)
824 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
825 sizeof (struct SearchMessage),
826 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
828 &transmit_download_request,
835 * Reconnect to the FS service and transmit our queries NOW.
837 * @param cls our download context
841 do_reconnect (void *cls,
842 const struct GNUNET_SCHEDULER_TaskContext *tc)
844 struct GNUNET_FS_DownloadContext *dc = cls;
845 struct GNUNET_CLIENT_Connection *client;
847 dc->task = GNUNET_SCHEDULER_NO_TASK;
848 client = GNUNET_CLIENT_connect (dc->h->sched,
853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
854 "Connecting to `%s'-service failed, will try again.\n",
860 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
861 sizeof (struct SearchMessage),
862 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
864 &transmit_download_request,
866 GNUNET_CLIENT_receive (client,
869 GNUNET_TIME_UNIT_FOREVER_REL);
874 * Add entries that are not yet pending back to the pending list.
876 * @param cls our download context
878 * @param entry entry of type "struct DownloadRequest"
882 retry_entry (void *cls,
883 const GNUNET_HashCode *key,
886 struct GNUNET_FS_DownloadContext *dc = cls;
887 struct DownloadRequest *dr = entry;
889 if (! dr->is_pending)
891 dr->next = dc->pending;
892 dr->is_pending = GNUNET_YES;
900 * We've lost our connection with the FS service.
901 * Re-establish it and re-transmit all of our
904 * @param dc download context that is having trouble
907 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
910 if (NULL != dc->client)
914 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
917 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
920 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
924 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
925 GNUNET_TIME_UNIT_SECONDS,
932 * Download parts of a file. Note that this will store
933 * the blocks at the respective offset in the given file. Also, the
934 * download is still using the blocking of the underlying FS
935 * encoding. As a result, the download may *write* outside of the
936 * given boundaries (if offset and length do not match the 32k FS
937 * block boundaries). <p>
939 * This function should be used to focus a download towards a
940 * particular portion of the file (optimization), not to strictly
941 * limit the download to exactly those bytes.
943 * @param h handle to the file sharing subsystem
944 * @param uri the URI of the file (determines what to download); CHK or LOC URI
945 * @param meta known metadata for the file (can be NULL)
946 * @param filename where to store the file, maybe NULL (then no file is
947 * created on disk and data must be grabbed from the callbacks)
948 * @param offset at what offset should we start the download (typically 0)
949 * @param length how many bytes should be downloaded starting at offset
950 * @param anonymity anonymity level to use for the download
951 * @param options various options
952 * @param cctx initial value for the client context for this download
953 * @param parent parent download to associate this download with (use NULL
954 * for top-level downloads; useful for manually-triggered recursive downloads)
955 * @return context that can be used to control this download
957 struct GNUNET_FS_DownloadContext *
958 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
959 const struct GNUNET_FS_Uri *uri,
960 const struct GNUNET_CONTAINER_MetaData *meta,
961 const char *filename,
965 enum GNUNET_FS_DownloadOptions options,
967 struct GNUNET_FS_DownloadContext *parent)
969 struct GNUNET_FS_ProgressInfo pi;
970 struct GNUNET_FS_DownloadContext *dc;
971 struct GNUNET_CLIENT_Connection *client;
973 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
974 if ( (offset + length < offset) ||
975 (offset + length > uri->data.chk.file_length) )
980 // FIXME: add support for "loc" URIs!
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Starting download `%s' of %llu bytes\n",
985 (unsigned long long) length);
987 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
992 GNUNET_CONTAINER_DLL_insert (parent->child_head,
996 dc->uri = GNUNET_FS_uri_dup (uri);
997 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
998 dc->client_info = cctx;
999 dc->start_time = GNUNET_TIME_absolute_get ();
1000 if (NULL != filename)
1002 dc->filename = GNUNET_strdup (filename);
1003 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1004 GNUNET_DISK_file_size (filename,
1007 dc->handle = GNUNET_DISK_file_open (filename,
1008 GNUNET_DISK_OPEN_READWRITE |
1009 GNUNET_DISK_OPEN_CREATE,
1010 GNUNET_DISK_PERM_USER_READ |
1011 GNUNET_DISK_PERM_USER_WRITE |
1012 GNUNET_DISK_PERM_GROUP_READ |
1013 GNUNET_DISK_PERM_OTHER_READ);
1014 if (dc->handle == NULL)
1016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017 _("Download failed: could not open file `%s': %s\n"),
1020 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1021 GNUNET_FS_uri_destroy (dc->uri);
1022 GNUNET_free (dc->filename);
1023 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1028 // FIXME: set "dc->target" for LOC uris!
1029 dc->offset = offset;
1030 dc->length = length;
1031 dc->anonymity = anonymity;
1032 dc->options = options;
1033 dc->active = GNUNET_CONTAINER_multihashmap_create (2 * (length / DBLOCK_SIZE));
1034 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Download tree has depth %u\n",
1040 // FIXME: make persistent
1042 // FIXME: bound parallelism here!
1043 client = GNUNET_CLIENT_connect (h->sched,
1046 dc->client = client;
1047 schedule_block_download (dc,
1048 &dc->uri->data.chk.chk,
1050 1 /* 0 == CHK, 1 == top */);
1051 GNUNET_CLIENT_receive (client,
1054 GNUNET_TIME_UNIT_FOREVER_REL);
1055 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1056 make_download_status (&pi, dc);
1057 pi.value.download.specifics.start.meta = meta;
1058 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1066 * Free entries in the map.
1068 * @param cls unused (NULL)
1070 * @param entry entry of type "struct DownloadRequest" which is freed
1074 free_entry (void *cls,
1075 const GNUNET_HashCode *key,
1078 GNUNET_free (entry);
1084 * Stop a download (aborts if download is incomplete).
1086 * @param dc handle for the download
1087 * @param do_delete delete files of incomplete downloads
1090 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1093 struct GNUNET_FS_ProgressInfo pi;
1095 while (NULL != dc->child_head)
1096 GNUNET_FS_download_stop (dc->child_head,
1098 // FIXME: make unpersistent
1099 if (dc->parent != NULL)
1100 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1101 dc->parent->child_tail,
1104 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1105 make_download_status (&pi, dc);
1106 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1109 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1110 GNUNET_SCHEDULER_cancel (dc->h->sched,
1114 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1117 if (NULL != dc->client)
1118 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1119 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1122 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1123 if (dc->filename != NULL)
1125 if (NULL != dc->handle)
1126 GNUNET_DISK_file_close (dc->handle);
1127 if ( (dc->completed != dc->length) &&
1128 (GNUNET_YES == do_delete) )
1130 if (0 != UNLINK (dc->filename))
1131 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1135 GNUNET_free (dc->filename);
1137 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1138 GNUNET_FS_uri_destroy (dc->uri);
1142 /* end of fs_download.c */