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,
253 * Suggest a filename based on given metadata.
255 * @param md given meta data
256 * @return NULL if meta data is useless for suggesting a filename
259 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
266 * We've lost our connection with the FS service.
267 * Re-establish it and re-transmit all of our
270 * @param dc download context that is having trouble
273 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
277 * Compute how many bytes of data should be stored in
278 * the specified node.
280 * @param fsize overall file size
281 * @param totaldepth depth of the entire tree
282 * @param offset offset of the node
283 * @param depth depth of the node
284 * @return number of bytes stored in this node
287 calculate_block_size (uint64_t fsize,
288 unsigned int totaldepth,
298 GNUNET_assert (offset < fsize);
299 if (depth == totaldepth)
302 if (offset + ret > fsize)
303 ret = (size_t) (fsize - offset);
308 for (i = totaldepth-1; i > depth; i--)
309 rsize *= CHK_PER_INODE;
310 epos = offset + rsize * CHK_PER_INODE;
311 GNUNET_assert (epos > offset);
314 /* round up when computing #CHKs in our IBlock */
315 chks = (epos - offset + rsize - 1) / rsize;
316 GNUNET_assert (chks <= CHK_PER_INODE);
317 return chks * sizeof (struct ContentHashKey);
322 * Closure for iterator processing results.
324 struct ProcessResultClosure
330 GNUNET_HashCode query;
333 * Data found in P2P network.
338 * Our download context.
340 struct GNUNET_FS_DownloadContext *dc;
343 * Number of bytes in data.
356 * We found an entry in a directory. Check if the respective child
357 * already exists and if not create the respective child download.
359 * @param cls the parent download
360 * @param filename name of the file in the directory
361 * @param uri URI of the file (CHK or LOC)
362 * @param meta meta data of the file
363 * @param length number of bytes in data
364 * @param data contents of the file (or NULL if they were not inlined)
367 trigger_recursive_download (void *cls,
368 const char *filename,
369 const struct GNUNET_FS_Uri *uri,
370 const struct GNUNET_CONTAINER_MetaData *meta,
374 struct GNUNET_FS_DownloadContext *dc = cls;
375 struct GNUNET_FS_DownloadContext *cpos;
383 return; /* entry for the directory itself */
384 cpos = dc->child_head;
387 if ( (GNUNET_FS_uri_test_equal (uri,
389 ( (filename != NULL) &&
390 (0 == strcmp (cpos->filename,
396 return; /* already exists */
398 if (NULL == filename)
400 fn = GNUNET_FS_meta_data_suggest_filename (meta);
403 fn = GNUNET_FS_uri_to_string (uri);
406 else if (fn[0] == '.')
409 us = GNUNET_FS_uri_to_string (uri);
410 GNUNET_asprintf (&fn,
418 if (dc->filename == NULL)
424 dn = GNUNET_strdup (dc->filename);
425 GNUNET_break ( (strlen (dn) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
427 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
428 GNUNET_FS_DIRECTORY_EXT)) );
429 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
430 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
431 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
433 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
434 GNUNET_FS_DIRECTORY_EXT)) ) )
436 GNUNET_asprintf (&full_name,
441 GNUNET_FS_DIRECTORY_EXT);
445 GNUNET_asprintf (&full_name,
455 if (full_name != NULL)
457 /* determine on-disk filename, write data! */
458 GNUNET_break (0); // FIXME: not implemented
462 /* FIXME: generate 'progress' events and move to
463 instant completion! */
464 GNUNET_break (0); // FIXME: not implemented
467 GNUNET_FS_download_start (dc->h,
472 GNUNET_FS_uri_chk_get_file_size (uri),
477 GNUNET_free_non_null (full_name);
478 GNUNET_free_non_null (fn);
483 * We're done downloading a directory. Open the file and
484 * trigger all of the (remaining) child downloads.
486 * @param dc context of download that just completed
489 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
494 struct GNUNET_DISK_FileHandle *h;
495 struct GNUNET_DISK_MapHandle *m;
497 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
498 size = (size_t) size64;
499 if (size64 != (uint64_t) size)
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
505 if (dc->filename != NULL)
507 h = GNUNET_DISK_file_open (dc->filename,
508 GNUNET_DISK_OPEN_READ,
509 GNUNET_DISK_PERM_NONE);
513 /* FIXME: need to initialize (and use) temp_filename
514 in various places in order for this assertion to
515 not fail; right now, it will always fail! */
516 GNUNET_assert (dc->temp_filename != NULL);
517 h = GNUNET_DISK_file_open (dc->temp_filename,
518 GNUNET_DISK_OPEN_READ,
519 GNUNET_DISK_PERM_NONE);
523 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
527 _("Directory too large for system address space\n"));
531 GNUNET_FS_directory_list_contents (size,
534 &trigger_recursive_download,
536 GNUNET_DISK_file_unmap (m);
538 GNUNET_DISK_file_close (h);
539 if (dc->filename == NULL)
541 if (0 != UNLINK (dc->temp_filename))
542 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
545 GNUNET_free (dc->temp_filename);
546 dc->temp_filename = NULL;
552 * Iterator over entries in the pending requests in the 'active' map for the
553 * reply that we just got.
555 * @param cls closure (our 'struct ProcessResultClosure')
556 * @param key query for the given value / request
557 * @param value value in the hash map (a 'struct DownloadRequest')
558 * @return GNUNET_YES (we should continue to iterate); unless serious error
561 process_result_with_request (void *cls,
562 const GNUNET_HashCode * key,
565 struct ProcessResultClosure *prc = cls;
566 struct DownloadRequest *sm = value;
567 struct GNUNET_FS_DownloadContext *dc = prc->dc;
568 struct GNUNET_CRYPTO_AesSessionKey skey;
569 struct GNUNET_CRYPTO_AesInitializationVector iv;
571 struct GNUNET_FS_ProgressInfo pi;
575 struct ContentHashKey *chk;
578 if (prc->size != calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
586 calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
592 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
594 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
595 make_download_status (&pi, dc);
596 pi.value.download.specifics.error.message = dc->emsg;
597 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
599 /* abort all pending requests */
602 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
605 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
609 GNUNET_assert (GNUNET_YES ==
610 GNUNET_CONTAINER_multihashmap_remove (dc->active,
613 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
614 GNUNET_CRYPTO_aes_decrypt (prc->data,
619 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
624 if ( (NULL != dc->handle) &&
625 ( (sm->depth == dc->treedepth) ||
626 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Saving decrypted block to disk at offset %llu\n",
632 (unsigned long long) off);
635 GNUNET_DISK_file_seek (dc->handle,
637 GNUNET_DISK_SEEK_SET) ) )
638 GNUNET_asprintf (&emsg,
639 _("Failed to seek to offset %llu in file `%s': %s\n"),
640 (unsigned long long) off,
643 else if (prc->size !=
644 GNUNET_DISK_file_write (dc->handle,
647 GNUNET_asprintf (&emsg,
648 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
649 (unsigned int) prc->size,
650 (unsigned long long) off,
656 // FIXME: make persistent
659 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
660 make_download_status (&pi, dc);
661 pi.value.download.specifics.error.message = emsg;
662 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
665 /* abort all pending requests */
668 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
671 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
677 if (sm->depth == dc->treedepth)
680 if (sm->offset < dc->offset)
682 /* starting offset begins in the middle of pt,
683 do not count first bytes as progress */
684 GNUNET_assert (app > (dc->offset - sm->offset));
685 app -= (dc->offset - sm->offset);
687 if (sm->offset + prc->size > dc->offset + dc->length)
689 /* end of block is after relevant range,
690 do not count last bytes as progress */
691 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
692 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
694 dc->completed += app;
696 /* do recursive download if option is set and either meta data
697 says it is a directory or if no meta data is given AND filename
698 ends in '.gnd' (top-level case) */
699 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
700 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
701 ( (dc->meta == NULL) &&
702 ( (NULL == dc->filename) ||
703 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
705 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
706 GNUNET_FS_DIRECTORY_EXT)) ) ) ) ) )
708 GNUNET_FS_directory_list_contents (prc->size,
711 &trigger_recursive_download,
717 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
718 make_download_status (&pi, dc);
719 pi.value.download.specifics.progress.data = pt;
720 pi.value.download.specifics.progress.offset = sm->offset;
721 pi.value.download.specifics.progress.data_len = prc->size;
722 pi.value.download.specifics.progress.depth = sm->depth;
723 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
725 GNUNET_assert (dc->completed <= dc->length);
726 if (dc->completed == dc->length)
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Download completed, truncating file to desired length %llu\n",
731 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
733 /* truncate file to size (since we store IBlocks at the end) */
734 if (dc->handle != NULL)
736 GNUNET_DISK_file_close (dc->handle);
738 if (0 != truncate (dc->filename,
739 GNUNET_ntohll (dc->uri->data.chk.file_length)))
740 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
745 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
746 (GNUNET_NO != GNUNET_FS_meta_data_test_for_directory (dc->meta)) )
747 full_recursive_download (dc);
748 if (dc->child_head == NULL)
750 /* signal completion */
751 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
752 make_download_status (&pi, dc);
753 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
756 GNUNET_assert (sm->depth == dc->treedepth);
758 // FIXME: make persistent
759 if (sm->depth == dc->treedepth)
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
768 (unsigned long long) sm->offset);
770 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
771 chk = (struct ContentHashKey*) pt;
772 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
774 off = compute_dblock_offset (sm->offset,
778 if ( (off + DBLOCK_SIZE >= dc->offset) &&
779 (off < dc->offset + dc->length) )
780 schedule_block_download (dc,
791 * Process a download result.
793 * @param dc our download context
794 * @param type type of the result
795 * @param data the (encrypted) response
796 * @param size size of data
799 process_result (struct GNUNET_FS_DownloadContext *dc,
804 struct ProcessResultClosure prc;
810 GNUNET_CRYPTO_hash (data, size, &prc.query);
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Received result for query `%s' from `%s'-service\n",
814 GNUNET_h2s (&prc.query),
817 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
819 &process_result_with_request,
825 * Type of a function to call when we receive a message
829 * @param msg message received, NULL on timeout or fatal error
832 receive_results (void *cls,
833 const struct GNUNET_MessageHeader * msg)
835 struct GNUNET_FS_DownloadContext *dc = cls;
836 const struct PutMessage *cm;
839 if ( (NULL == msg) ||
840 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
841 (sizeof (struct PutMessage) > ntohs(msg->size)) )
843 GNUNET_break (msg == NULL);
847 msize = ntohs(msg->size);
848 cm = (const struct PutMessage*) msg;
852 msize - sizeof (struct PutMessage));
853 if (dc->client == NULL)
854 return; /* fatal error */
855 /* continue receiving */
856 GNUNET_CLIENT_receive (dc->client,
859 GNUNET_TIME_UNIT_FOREVER_REL);
865 * We're ready to transmit a search request to the
866 * file-sharing service. Do it. If there is
867 * more than one request pending, try to send
868 * multiple or request another transmission.
871 * @param size number of bytes available in buf
872 * @param buf where the callee should write the message
873 * @return number of bytes written to buf
876 transmit_download_request (void *cls,
880 struct GNUNET_FS_DownloadContext *dc = cls;
882 struct SearchMessage *sm;
890 GNUNET_assert (size >= sizeof (struct SearchMessage));
893 while ( (dc->pending != NULL) &&
894 (size > msize + sizeof (struct SearchMessage)) )
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Transmitting download request for `%s' to `%s'-service\n",
899 GNUNET_h2s (&dc->pending->chk.query),
902 memset (sm, 0, sizeof (struct SearchMessage));
903 sm->header.size = htons (sizeof (struct SearchMessage));
904 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
905 if (dc->pending->depth == dc->treedepth)
906 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_DBLOCK);
908 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_IBLOCK);
909 sm->anonymity_level = htonl (dc->anonymity);
910 sm->target = dc->target.hashPubKey;
911 sm->query = dc->pending->chk.query;
912 dc->pending->is_pending = GNUNET_NO;
913 dc->pending = dc->pending->next;
914 msize += sizeof (struct SearchMessage);
917 if (dc->pending != NULL)
918 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
919 sizeof (struct SearchMessage),
920 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
922 &transmit_download_request,
929 * Reconnect to the FS service and transmit our queries NOW.
931 * @param cls our download context
935 do_reconnect (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc)
938 struct GNUNET_FS_DownloadContext *dc = cls;
939 struct GNUNET_CLIENT_Connection *client;
941 dc->task = GNUNET_SCHEDULER_NO_TASK;
942 client = GNUNET_CLIENT_connect (dc->h->sched,
947 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
948 "Connecting to `%s'-service failed, will try again.\n",
954 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
955 sizeof (struct SearchMessage),
956 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
958 &transmit_download_request,
960 GNUNET_CLIENT_receive (client,
963 GNUNET_TIME_UNIT_FOREVER_REL);
968 * Add entries that are not yet pending back to the pending list.
970 * @param cls our download context
972 * @param entry entry of type "struct DownloadRequest"
976 retry_entry (void *cls,
977 const GNUNET_HashCode *key,
980 struct GNUNET_FS_DownloadContext *dc = cls;
981 struct DownloadRequest *dr = entry;
983 if (! dr->is_pending)
985 dr->next = dc->pending;
986 dr->is_pending = GNUNET_YES;
994 * We've lost our connection with the FS service.
995 * Re-establish it and re-transmit all of our
998 * @param dc download context that is having trouble
1001 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1004 if (NULL != dc->client)
1008 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1011 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1014 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1018 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1019 GNUNET_TIME_UNIT_SECONDS,
1026 * Download parts of a file. Note that this will store
1027 * the blocks at the respective offset in the given file. Also, the
1028 * download is still using the blocking of the underlying FS
1029 * encoding. As a result, the download may *write* outside of the
1030 * given boundaries (if offset and length do not match the 32k FS
1031 * block boundaries). <p>
1033 * This function should be used to focus a download towards a
1034 * particular portion of the file (optimization), not to strictly
1035 * limit the download to exactly those bytes.
1037 * @param h handle to the file sharing subsystem
1038 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1039 * @param meta known metadata for the file (can be NULL)
1040 * @param filename where to store the file, maybe NULL (then no file is
1041 * created on disk and data must be grabbed from the callbacks)
1042 * @param offset at what offset should we start the download (typically 0)
1043 * @param length how many bytes should be downloaded starting at offset
1044 * @param anonymity anonymity level to use for the download
1045 * @param options various options
1046 * @param cctx initial value for the client context for this download
1047 * @param parent parent download to associate this download with (use NULL
1048 * for top-level downloads; useful for manually-triggered recursive downloads)
1049 * @return context that can be used to control this download
1051 struct GNUNET_FS_DownloadContext *
1052 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1053 const struct GNUNET_FS_Uri *uri,
1054 const struct GNUNET_CONTAINER_MetaData *meta,
1055 const char *filename,
1059 enum GNUNET_FS_DownloadOptions options,
1061 struct GNUNET_FS_DownloadContext *parent)
1063 struct GNUNET_FS_ProgressInfo pi;
1064 struct GNUNET_FS_DownloadContext *dc;
1065 struct GNUNET_CLIENT_Connection *client;
1067 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1068 if ( (offset + length < offset) ||
1069 (offset + length > uri->data.chk.file_length) )
1074 // FIXME: add support for "loc" URIs!
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "Starting download `%s' of %llu bytes\n",
1079 (unsigned long long) length);
1081 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1083 dc->parent = parent;
1086 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1090 dc->uri = GNUNET_FS_uri_dup (uri);
1091 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1092 dc->client_info = cctx;
1093 dc->start_time = GNUNET_TIME_absolute_get ();
1094 if (NULL != filename)
1096 dc->filename = GNUNET_strdup (filename);
1097 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1098 GNUNET_DISK_file_size (filename,
1101 dc->handle = GNUNET_DISK_file_open (filename,
1102 GNUNET_DISK_OPEN_READWRITE |
1103 GNUNET_DISK_OPEN_CREATE,
1104 GNUNET_DISK_PERM_USER_READ |
1105 GNUNET_DISK_PERM_USER_WRITE |
1106 GNUNET_DISK_PERM_GROUP_READ |
1107 GNUNET_DISK_PERM_OTHER_READ);
1108 if (dc->handle == NULL)
1110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1111 _("Download failed: could not open file `%s': %s\n"),
1114 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1115 GNUNET_FS_uri_destroy (dc->uri);
1116 GNUNET_free (dc->filename);
1117 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1122 // FIXME: set "dc->target" for LOC uris!
1123 dc->offset = offset;
1124 dc->length = length;
1125 dc->anonymity = anonymity;
1126 dc->options = options;
1127 dc->active = GNUNET_CONTAINER_multihashmap_create (2 * (length / DBLOCK_SIZE));
1128 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Download tree has depth %u\n",
1134 // FIXME: make persistent
1136 // FIXME: bound parallelism here!
1137 client = GNUNET_CLIENT_connect (h->sched,
1140 dc->client = client;
1141 schedule_block_download (dc,
1142 &dc->uri->data.chk.chk,
1144 1 /* 0 == CHK, 1 == top */);
1145 GNUNET_CLIENT_receive (client,
1148 GNUNET_TIME_UNIT_FOREVER_REL);
1149 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1150 make_download_status (&pi, dc);
1151 pi.value.download.specifics.start.meta = meta;
1152 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1160 * Free entries in the map.
1162 * @param cls unused (NULL)
1164 * @param entry entry of type "struct DownloadRequest" which is freed
1168 free_entry (void *cls,
1169 const GNUNET_HashCode *key,
1172 GNUNET_free (entry);
1178 * Stop a download (aborts if download is incomplete).
1180 * @param dc handle for the download
1181 * @param do_delete delete files of incomplete downloads
1184 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1187 struct GNUNET_FS_ProgressInfo pi;
1189 while (NULL != dc->child_head)
1190 GNUNET_FS_download_stop (dc->child_head,
1192 // FIXME: make unpersistent
1193 if (dc->parent != NULL)
1194 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1195 dc->parent->child_tail,
1198 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1199 make_download_status (&pi, dc);
1200 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1203 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1204 GNUNET_SCHEDULER_cancel (dc->h->sched,
1208 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1211 if (NULL != dc->client)
1212 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1213 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1216 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1217 if (dc->filename != NULL)
1219 if (NULL != dc->handle)
1220 GNUNET_DISK_file_close (dc->handle);
1221 if ( (dc->completed != dc->length) &&
1222 (GNUNET_YES == do_delete) )
1224 if (0 != UNLINK (dc->filename))
1225 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1229 GNUNET_free (dc->filename);
1231 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1232 GNUNET_FS_uri_destroy (dc->uri);
1236 /* end of fs_download.c */