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;
363 cpos = dc->child_head;
366 if (0 == strcmp (cpos->filename,
369 GNUNET_break_op (GNUNET_FS_uri_test_equal (uri,
376 return; /* already exists */
379 /* determine on-disk filename, write data! */
380 GNUNET_break (0); // FIXME: not implemented
382 GNUNET_FS_download_start (dc->h,
385 filename, /* FIXME: prepend directory name! */
387 GNUNET_FS_uri_chk_get_file_size (uri),
396 * We're done downloading a directory. Open the file and
397 * trigger all of the (remaining) child downloads.
399 * @param dc context of download that just completed
402 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
407 struct GNUNET_DISK_FileHandle *h;
408 struct GNUNET_DISK_MapHandle *m;
410 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
411 size = (size_t) size64;
412 if (size64 != (uint64_t) size)
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
415 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
418 if (dc->filename != NULL)
420 h = GNUNET_DISK_file_open (dc->filename,
421 GNUNET_DISK_OPEN_READ,
422 GNUNET_DISK_PERM_NONE);
426 /* FIXME: need to initialize (and use) temp_filename
427 in various places in order for this assertion to
428 not fail; right now, it will always fail! */
429 GNUNET_assert (dc->temp_filename != NULL);
430 h = GNUNET_DISK_file_open (dc->temp_filename,
431 GNUNET_DISK_OPEN_READ,
432 GNUNET_DISK_PERM_NONE);
436 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 _("Directory too large for system address space\n"));
444 GNUNET_FS_directory_list_contents (size,
447 &trigger_recursive_download,
449 GNUNET_DISK_file_unmap (m);
451 GNUNET_DISK_file_close (h);
452 if (dc->filename == NULL)
454 if (0 != UNLINK (dc->temp_filename))
455 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
458 GNUNET_free (dc->temp_filename);
459 dc->temp_filename = NULL;
465 * Iterator over entries in the pending requests in the 'active' map for the
466 * reply that we just got.
468 * @param cls closure (our 'struct ProcessResultClosure')
469 * @param key query for the given value / request
470 * @param value value in the hash map (a 'struct DownloadRequest')
471 * @return GNUNET_YES (we should continue to iterate); unless serious error
474 process_result_with_request (void *cls,
475 const GNUNET_HashCode * key,
478 struct ProcessResultClosure *prc = cls;
479 struct DownloadRequest *sm = value;
480 struct GNUNET_FS_DownloadContext *dc = prc->dc;
481 struct GNUNET_CRYPTO_AesSessionKey skey;
482 struct GNUNET_CRYPTO_AesInitializationVector iv;
484 struct GNUNET_FS_ProgressInfo pi;
488 struct ContentHashKey *chk;
491 if (prc->size != calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
499 calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
505 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
507 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
508 make_download_status (&pi, dc);
509 pi.value.download.specifics.error.message = dc->emsg;
510 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
512 /* abort all pending requests */
515 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
518 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
522 GNUNET_assert (GNUNET_YES ==
523 GNUNET_CONTAINER_multihashmap_remove (dc->active,
526 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
527 GNUNET_CRYPTO_aes_decrypt (prc->data,
533 if ( (NULL != dc->handle) &&
534 ( (sm->depth == dc->treedepth) ||
535 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
537 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Saving decrypted block to disk at offset %llu\n",
545 (unsigned long long) off);
548 GNUNET_DISK_file_seek (dc->handle,
550 GNUNET_DISK_SEEK_SET) ) )
551 GNUNET_asprintf (&emsg,
552 _("Failed to seek to offset %llu in file `%s': %s\n"),
553 (unsigned long long) off,
556 else if (prc->size !=
557 GNUNET_DISK_file_write (dc->handle,
560 GNUNET_asprintf (&emsg,
561 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
562 (unsigned int) prc->size,
563 (unsigned long long) off,
569 // FIXME: make persistent
572 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
573 make_download_status (&pi, dc);
574 pi.value.download.specifics.error.message = emsg;
575 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
578 /* abort all pending requests */
581 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
584 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
590 if (sm->depth == dc->treedepth)
593 if (sm->offset < dc->offset)
595 /* starting offset begins in the middle of pt,
596 do not count first bytes as progress */
597 GNUNET_assert (app > (dc->offset - sm->offset));
598 app -= (dc->offset - sm->offset);
600 if (sm->offset + prc->size > dc->offset + dc->length)
602 /* end of block is after relevant range,
603 do not count last bytes as progress */
604 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
605 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
607 dc->completed += app;
609 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
610 (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) )
612 GNUNET_FS_directory_list_contents (prc->size,
615 &trigger_recursive_download,
621 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
622 make_download_status (&pi, dc);
623 pi.value.download.specifics.progress.data = pt;
624 pi.value.download.specifics.progress.offset = sm->offset;
625 pi.value.download.specifics.progress.data_len = prc->size;
626 pi.value.download.specifics.progress.depth = sm->depth;
627 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
629 GNUNET_assert (dc->completed <= dc->length);
630 if (dc->completed == dc->length)
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Download completed, truncating file to desired length %llu\n",
635 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
637 /* truncate file to size (since we store IBlocks at the end) */
638 if (dc->handle != NULL)
640 GNUNET_DISK_file_close (dc->handle);
642 if (0 != truncate (dc->filename,
643 GNUNET_ntohll (dc->uri->data.chk.file_length)))
644 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
649 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
650 (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) )
651 full_recursive_download (dc);
652 if (dc->child_head == NULL)
654 /* signal completion */
655 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
656 make_download_status (&pi, dc);
657 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
660 GNUNET_assert (sm->depth == dc->treedepth);
662 // FIXME: make persistent
663 if (sm->depth == dc->treedepth)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
672 (unsigned long long) sm->offset);
674 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
675 chk = (struct ContentHashKey*) pt;
676 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
678 off = compute_dblock_offset (sm->offset,
682 if ( (off + DBLOCK_SIZE >= dc->offset) &&
683 (off < dc->offset + dc->length) )
684 schedule_block_download (dc,
695 * Process a download result.
697 * @param dc our download context
698 * @param type type of the result
699 * @param data the (encrypted) response
700 * @param size size of data
703 process_result (struct GNUNET_FS_DownloadContext *dc,
708 struct ProcessResultClosure prc;
714 GNUNET_CRYPTO_hash (data, size, &prc.query);
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 "Received result for query `%s' from `%s'-service\n",
718 GNUNET_h2s (&prc.query),
721 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
723 &process_result_with_request,
729 * Type of a function to call when we receive a message
733 * @param msg message received, NULL on timeout or fatal error
736 receive_results (void *cls,
737 const struct GNUNET_MessageHeader * msg)
739 struct GNUNET_FS_DownloadContext *dc = cls;
740 const struct PutMessage *cm;
743 if ( (NULL == msg) ||
744 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
745 (sizeof (struct PutMessage) > ntohs(msg->size)) )
747 GNUNET_break (msg == NULL);
751 msize = ntohs(msg->size);
752 cm = (const struct PutMessage*) msg;
756 msize - sizeof (struct PutMessage));
757 if (dc->client == NULL)
758 return; /* fatal error */
759 /* continue receiving */
760 GNUNET_CLIENT_receive (dc->client,
763 GNUNET_TIME_UNIT_FOREVER_REL);
769 * We're ready to transmit a search request to the
770 * file-sharing service. Do it. If there is
771 * more than one request pending, try to send
772 * multiple or request another transmission.
775 * @param size number of bytes available in buf
776 * @param buf where the callee should write the message
777 * @return number of bytes written to buf
780 transmit_download_request (void *cls,
784 struct GNUNET_FS_DownloadContext *dc = cls;
786 struct SearchMessage *sm;
794 GNUNET_assert (size >= sizeof (struct SearchMessage));
797 while ( (dc->pending != NULL) &&
798 (size > msize + sizeof (struct SearchMessage)) )
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Transmitting download request for `%s' to `%s'-service\n",
803 GNUNET_h2s (&dc->pending->chk.query),
806 memset (sm, 0, sizeof (struct SearchMessage));
807 sm->header.size = htons (sizeof (struct SearchMessage));
808 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
809 if (dc->pending->depth == dc->treedepth)
810 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_DBLOCK);
812 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_IBLOCK);
813 sm->anonymity_level = htonl (dc->anonymity);
814 sm->target = dc->target.hashPubKey;
815 sm->query = dc->pending->chk.query;
816 dc->pending->is_pending = GNUNET_NO;
817 dc->pending = dc->pending->next;
818 msize += sizeof (struct SearchMessage);
821 if (dc->pending != NULL)
822 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
823 sizeof (struct SearchMessage),
824 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
826 &transmit_download_request,
833 * Reconnect to the FS service and transmit our queries NOW.
835 * @param cls our download context
839 do_reconnect (void *cls,
840 const struct GNUNET_SCHEDULER_TaskContext *tc)
842 struct GNUNET_FS_DownloadContext *dc = cls;
843 struct GNUNET_CLIENT_Connection *client;
845 dc->task = GNUNET_SCHEDULER_NO_TASK;
846 client = GNUNET_CLIENT_connect (dc->h->sched,
851 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
852 "Connecting to `%s'-service failed, will try again.\n",
858 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
859 sizeof (struct SearchMessage),
860 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
862 &transmit_download_request,
864 GNUNET_CLIENT_receive (client,
867 GNUNET_TIME_UNIT_FOREVER_REL);
872 * Add entries that are not yet pending back to the pending list.
874 * @param cls our download context
876 * @param entry entry of type "struct DownloadRequest"
880 retry_entry (void *cls,
881 const GNUNET_HashCode *key,
884 struct GNUNET_FS_DownloadContext *dc = cls;
885 struct DownloadRequest *dr = entry;
887 if (! dr->is_pending)
889 dr->next = dc->pending;
890 dr->is_pending = GNUNET_YES;
898 * We've lost our connection with the FS service.
899 * Re-establish it and re-transmit all of our
902 * @param dc download context that is having trouble
905 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
908 if (NULL != dc->client)
912 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
915 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
918 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
922 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
923 GNUNET_TIME_UNIT_SECONDS,
930 * Download parts of a file. Note that this will store
931 * the blocks at the respective offset in the given file. Also, the
932 * download is still using the blocking of the underlying FS
933 * encoding. As a result, the download may *write* outside of the
934 * given boundaries (if offset and length do not match the 32k FS
935 * block boundaries). <p>
937 * This function should be used to focus a download towards a
938 * particular portion of the file (optimization), not to strictly
939 * limit the download to exactly those bytes.
941 * @param h handle to the file sharing subsystem
942 * @param uri the URI of the file (determines what to download); CHK or LOC URI
943 * @param meta known metadata for the file (can be NULL)
944 * @param filename where to store the file, maybe NULL (then no file is
945 * created on disk and data must be grabbed from the callbacks)
946 * @param offset at what offset should we start the download (typically 0)
947 * @param length how many bytes should be downloaded starting at offset
948 * @param anonymity anonymity level to use for the download
949 * @param options various options
950 * @param cctx initial value for the client context for this download
951 * @param parent parent download to associate this download with (use NULL
952 * for top-level downloads; useful for manually-triggered recursive downloads)
953 * @return context that can be used to control this download
955 struct GNUNET_FS_DownloadContext *
956 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
957 const struct GNUNET_FS_Uri *uri,
958 const struct GNUNET_CONTAINER_MetaData *meta,
959 const char *filename,
963 enum GNUNET_FS_DownloadOptions options,
965 struct GNUNET_FS_DownloadContext *parent)
967 struct GNUNET_FS_ProgressInfo pi;
968 struct GNUNET_FS_DownloadContext *dc;
969 struct GNUNET_CLIENT_Connection *client;
971 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
972 if ( (offset + length < offset) ||
973 (offset + length > uri->data.chk.file_length) )
978 // FIXME: add support for "loc" URIs!
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Starting download `%s' of %llu bytes\n",
983 (unsigned long long) length);
985 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
990 GNUNET_CONTAINER_DLL_insert (parent->child_head,
994 dc->uri = GNUNET_FS_uri_dup (uri);
995 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
996 dc->client_info = cctx;
997 dc->start_time = GNUNET_TIME_absolute_get ();
998 if (NULL != filename)
1000 dc->filename = GNUNET_strdup (filename);
1001 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1002 GNUNET_DISK_file_size (filename,
1005 dc->handle = GNUNET_DISK_file_open (filename,
1006 GNUNET_DISK_OPEN_READWRITE |
1007 GNUNET_DISK_OPEN_CREATE,
1008 GNUNET_DISK_PERM_USER_READ |
1009 GNUNET_DISK_PERM_USER_WRITE |
1010 GNUNET_DISK_PERM_GROUP_READ |
1011 GNUNET_DISK_PERM_OTHER_READ);
1012 if (dc->handle == NULL)
1014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1015 _("Download failed: could not open file `%s': %s\n"),
1018 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1019 GNUNET_FS_uri_destroy (dc->uri);
1020 GNUNET_free (dc->filename);
1021 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1026 // FIXME: set "dc->target" for LOC uris!
1027 dc->offset = offset;
1028 dc->length = length;
1029 dc->anonymity = anonymity;
1030 dc->options = options;
1031 dc->active = GNUNET_CONTAINER_multihashmap_create (2 * (length / DBLOCK_SIZE));
1032 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Download tree has depth %u\n",
1038 // FIXME: make persistent
1040 // FIXME: bound parallelism here!
1041 client = GNUNET_CLIENT_connect (h->sched,
1044 dc->client = client;
1045 schedule_block_download (dc,
1046 &dc->uri->data.chk.chk,
1048 1 /* 0 == CHK, 1 == top */);
1049 GNUNET_CLIENT_receive (client,
1052 GNUNET_TIME_UNIT_FOREVER_REL);
1053 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1054 make_download_status (&pi, dc);
1055 pi.value.download.specifics.start.meta = meta;
1056 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1064 * Free entries in the map.
1066 * @param cls unused (NULL)
1068 * @param entry entry of type "struct DownloadRequest" which is freed
1072 free_entry (void *cls,
1073 const GNUNET_HashCode *key,
1076 GNUNET_free (entry);
1082 * Stop a download (aborts if download is incomplete).
1084 * @param dc handle for the download
1085 * @param do_delete delete files of incomplete downloads
1088 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1091 struct GNUNET_FS_ProgressInfo pi;
1093 while (NULL != dc->child_head)
1094 GNUNET_FS_download_stop (dc->child_head,
1096 // FIXME: make unpersistent
1097 if (dc->parent != NULL)
1098 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1099 dc->parent->child_tail,
1102 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1103 make_download_status (&pi, dc);
1104 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1107 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1108 GNUNET_SCHEDULER_cancel (dc->h->sched,
1112 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1115 if (NULL != dc->client)
1116 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1117 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1120 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1121 if (dc->filename != NULL)
1123 if (NULL != dc->handle)
1124 GNUNET_DISK_file_close (dc->handle);
1125 if ( (dc->completed != dc->length) &&
1126 (GNUNET_YES == do_delete) )
1128 if (0 != UNLINK (dc->filename))
1129 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1133 GNUNET_free (dc->filename);
1135 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1136 GNUNET_FS_uri_destroy (dc->uri);
1140 /* end of fs_download.c */