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 block in the tree.
180 * @param dc overall download this block belongs to
181 * @param chk content-hash-key of the block
182 * @param offset offset of the block in the file
183 * (for IBlocks, the offset is the lowest
184 * offset of any DBlock in the subtree under
186 * @param depth depth of the block, 0 is the root of the tree
189 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
190 const struct ContentHashKey *chk,
194 struct DownloadRequest *sm;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Scheduling download at offset %llu and depth %u for `%s'\n",
200 (unsigned long long) offset,
202 GNUNET_h2s (&chk->query));
204 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
208 if ( (dc->old_file_size > off) &&
209 (dc->handle != NULL) &&
211 GNUNET_DISK_file_seek (dc->handle,
213 GNUNET_DISK_SEEK_SET) ) )
215 // FIXME: check if block exists on disk!
216 // (read block, encode, compare with
217 // query; if matches, simply return)
219 if (depth < dc->treedepth)
221 // FIXME: try if we could
222 // reconstitute this IBLOCK
223 // from the existing blocks on disk (can wait)
224 // (read block(s), encode, compare with
225 // query; if matches, simply return)
227 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
231 sm->is_pending = GNUNET_YES;
232 sm->next = dc->pending;
234 GNUNET_CONTAINER_multihashmap_put (dc->active,
237 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
239 if ( (dc->th == NULL) &&
240 (dc->client != NULL) )
241 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
242 sizeof (struct SearchMessage),
243 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
245 &transmit_download_request,
252 * Suggest a filename based on given metadata.
254 * @param md given meta data
255 * @return NULL if meta data is useless for suggesting a filename
258 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData *md)
260 static const char *mimeMap[][2] = {
261 {"application/bz2", ".bz2"},
262 {"application/gnunet-directory", ".gnd"},
263 {"application/java", ".class"},
264 {"application/msword", ".doc"},
265 {"application/ogg", ".ogg"},
266 {"application/pdf", ".pdf"},
267 {"application/pgp-keys", ".key"},
268 {"application/pgp-signature", ".pgp"},
269 {"application/postscript", ".ps"},
270 {"application/rar", ".rar"},
271 {"application/rtf", ".rtf"},
272 {"application/xml", ".xml"},
273 {"application/x-debian-package", ".deb"},
274 {"application/x-dvi", ".dvi"},
275 {"applixation/x-flac", ".flac"},
276 {"applixation/x-gzip", ".gz"},
277 {"application/x-java-archive", ".jar"},
278 {"application/x-java-vm", ".class"},
279 {"application/x-python-code", ".pyc"},
280 {"application/x-redhat-package-manager", ".rpm"},
281 {"application/x-rpm", ".rpm"},
282 {"application/x-tar", ".tar"},
283 {"application/x-tex-pk", ".pk"},
284 {"application/x-texinfo", ".texinfo"},
285 {"application/x-xcf", ".xcf"},
286 {"application/x-xfig", ".xfig"},
287 {"application/zip", ".zip"},
289 {"audio/midi", ".midi"},
290 {"audio/mpeg", ".mp3"},
291 {"audio/real", ".rm"},
292 {"audio/x-wav", ".wav"},
294 {"image/gif", ".gif"},
295 {"image/jpeg", ".jpg"},
296 {"image/pcx", ".pcx"},
297 {"image/png", ".png"},
298 {"image/tiff", ".tiff"},
299 {"image/x-ms-bmp", ".bmp"},
300 {"image/x-xpixmap", ".xpm"},
302 {"text/css", ".css"},
303 {"text/html", ".html"},
304 {"text/plain", ".txt"},
305 {"text/rtf", ".rtf"},
306 {"text/x-c++hdr", ".h++"},
307 {"text/x-c++src", ".c++"},
308 {"text/x-chdr", ".h"},
309 {"text/x-csrc", ".c"},
310 {"text/x-java", ".java"},
311 {"text/x-moc", ".moc"},
312 {"text/x-pascal", ".pas"},
313 {"text/x-perl", ".pl"},
314 {"text/x-python", ".py"},
315 {"text/x-tex", ".tex"},
317 {"video/avi", ".avi"},
318 {"video/mpeg", ".mpeg"},
319 {"video/quicktime", ".qt"},
320 {"video/real", ".rm"},
321 {"video/x-msvideo", ".avi"},
330 ret = GNUNET_CONTAINER_meta_data_get_by_type (md,
331 EXTRACTOR_METATYPE_FILENAME);
335 mime = GNUNET_CONTAINER_meta_data_get_by_type (md,
336 EXTRACTOR_METATYPE_MIMETYPE);
340 while ( (mimeMap[i][0] != NULL) &&
341 (0 != strcmp (mime, mimeMap[i][0])))
343 if (mimeMap[i][1] == NULL)
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG |
345 GNUNET_ERROR_TYPE_BULK,
346 _("Did not find mime type `%s' in extension list.\n"),
352 base = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
353 EXTRACTOR_METATYPE_TITLE,
354 EXTRACTOR_METATYPE_BOOK_TITLE,
355 EXTRACTOR_METATYPE_ORIGINAL_TITLE,
356 EXTRACTOR_METATYPE_PACKAGE_NAME,
357 EXTRACTOR_METATYPE_URL,
358 EXTRACTOR_METATYPE_URI,
359 EXTRACTOR_METATYPE_DESCRIPTION,
360 EXTRACTOR_METATYPE_ISRC,
361 EXTRACTOR_METATYPE_JOURNAL_NAME,
362 EXTRACTOR_METATYPE_AUTHOR_NAME,
363 EXTRACTOR_METATYPE_SUBJECT,
364 EXTRACTOR_METATYPE_ALBUM,
365 EXTRACTOR_METATYPE_ARTIST,
366 EXTRACTOR_METATYPE_KEYWORDS,
367 EXTRACTOR_METATYPE_COMMENT,
368 EXTRACTOR_METATYPE_UNKNOWN,
370 if ( (base == NULL) &&
374 return GNUNET_strdup (ext);
377 GNUNET_asprintf (&ret,
387 * We've lost our connection with the FS service.
388 * Re-establish it and re-transmit all of our
391 * @param dc download context that is having trouble
394 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
398 * Compute how many bytes of data should be stored in
399 * the specified node.
401 * @param fsize overall file size
402 * @param totaldepth depth of the entire tree
403 * @param offset offset of the node
404 * @param depth depth of the node
405 * @return number of bytes stored in this node
408 calculate_block_size (uint64_t fsize,
409 unsigned int totaldepth,
419 GNUNET_assert (offset < fsize);
420 if (depth == totaldepth)
423 if (offset + ret > fsize)
424 ret = (size_t) (fsize - offset);
429 for (i = totaldepth-1; i > depth; i--)
430 rsize *= CHK_PER_INODE;
431 epos = offset + rsize * CHK_PER_INODE;
432 GNUNET_assert (epos > offset);
435 /* round up when computing #CHKs in our IBlock */
436 chks = (epos - offset + rsize - 1) / rsize;
437 GNUNET_assert (chks <= CHK_PER_INODE);
438 return chks * sizeof (struct ContentHashKey);
443 * Closure for iterator processing results.
445 struct ProcessResultClosure
451 GNUNET_HashCode query;
454 * Data found in P2P network.
459 * Our download context.
461 struct GNUNET_FS_DownloadContext *dc;
464 * Number of bytes in data.
477 * We found an entry in a directory. Check if the respective child
478 * already exists and if not create the respective child download.
480 * @param cls the parent download
481 * @param filename name of the file in the directory
482 * @param uri URI of the file (CHK or LOC)
483 * @param meta meta data of the file
484 * @param length number of bytes in data
485 * @param data contents of the file (or NULL if they were not inlined)
488 trigger_recursive_download (void *cls,
489 const char *filename,
490 const struct GNUNET_FS_Uri *uri,
491 const struct GNUNET_CONTAINER_MetaData *meta,
495 struct GNUNET_FS_DownloadContext *dc = cls;
496 struct GNUNET_FS_DownloadContext *cpos;
497 struct GNUNET_DISK_FileHandle *fh;
505 return; /* entry for the directory itself */
506 cpos = dc->child_head;
509 if ( (GNUNET_FS_uri_test_equal (uri,
511 ( (filename != NULL) &&
512 (0 == strcmp (cpos->filename,
518 return; /* already exists */
520 if (NULL == filename)
522 fn = GNUNET_FS_meta_data_suggest_filename (meta);
525 us = GNUNET_FS_uri_to_string (uri);
526 fn = GNUNET_strdup (&us [strlen (GNUNET_FS_URI_PREFIX
527 GNUNET_FS_URI_CHK_INFIX)]);
530 else if (fn[0] == '.')
533 us = GNUNET_FS_uri_to_string (uri);
534 GNUNET_asprintf (&fn,
536 &us[strlen (GNUNET_FS_URI_PREFIX
537 GNUNET_FS_URI_CHK_INFIX)], ext);
543 if (dc->filename == NULL)
549 dn = GNUNET_strdup (dc->filename);
550 GNUNET_break ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
552 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
553 GNUNET_FS_DIRECTORY_EXT)) );
554 if ( (strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
556 strstr (dn + strlen(dn) - strlen(GNUNET_FS_DIRECTORY_EXT),
557 GNUNET_FS_DIRECTORY_EXT)) )
558 dn[strlen(dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
559 if ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
560 ( (strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
562 strstr (filename + strlen(filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
563 GNUNET_FS_DIRECTORY_EXT)) ) )
565 GNUNET_asprintf (&full_name,
570 GNUNET_FS_DIRECTORY_EXT);
574 GNUNET_asprintf (&full_name,
582 if ( (full_name != NULL) &&
584 GNUNET_DISK_directory_create_for_file (full_name)) )
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 _("Failed to create directory for recursive download of `%s'\n"),
589 GNUNET_free (full_name);
590 GNUNET_free_non_null (fn);
596 if (full_name != NULL)
598 fh = GNUNET_DISK_file_open (full_name,
599 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
600 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
603 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
606 GNUNET_free (full_name);
607 GNUNET_free_non_null (fn);
611 GNUNET_DISK_file_write (fh,
615 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
619 GNUNET_DISK_file_close (fh);
623 /* FIXME: generate 'progress' events and move to
624 instant completion! */
625 GNUNET_break (0); // FIXME: not implemented
628 GNUNET_FS_download_start (dc->h,
633 GNUNET_FS_uri_chk_get_file_size (uri),
638 GNUNET_free_non_null (full_name);
639 GNUNET_free_non_null (fn);
644 * We're done downloading a directory. Open the file and
645 * trigger all of the (remaining) child downloads.
647 * @param dc context of download that just completed
650 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
655 struct GNUNET_DISK_FileHandle *h;
656 struct GNUNET_DISK_MapHandle *m;
658 size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
659 size = (size_t) size64;
660 if (size64 != (uint64_t) size)
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
663 _("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
666 if (dc->filename != NULL)
668 h = GNUNET_DISK_file_open (dc->filename,
669 GNUNET_DISK_OPEN_READ,
670 GNUNET_DISK_PERM_NONE);
674 /* FIXME: need to initialize (and use) temp_filename
675 in various places in order for this assertion to
676 not fail; right now, it will always fail! */
677 GNUNET_assert (dc->temp_filename != NULL);
678 h = GNUNET_DISK_file_open (dc->temp_filename,
679 GNUNET_DISK_OPEN_READ,
680 GNUNET_DISK_PERM_NONE);
684 data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
688 _("Directory too large for system address space\n"));
692 GNUNET_FS_directory_list_contents (size,
695 &trigger_recursive_download,
697 GNUNET_DISK_file_unmap (m);
699 GNUNET_DISK_file_close (h);
700 if (dc->filename == NULL)
702 if (0 != UNLINK (dc->temp_filename))
703 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
706 GNUNET_free (dc->temp_filename);
707 dc->temp_filename = NULL;
713 * Check if all child-downloads have completed and
714 * if so, signal completion (and possibly recurse to
718 check_completed (struct GNUNET_FS_DownloadContext *dc)
720 struct GNUNET_FS_ProgressInfo pi;
721 struct GNUNET_FS_DownloadContext *pos;
723 pos = dc->child_head;
726 if ( (pos->emsg == NULL) &&
727 (pos->completed < pos->length) )
728 return; /* not done yet */
729 if ( (pos->child_head != NULL) &&
730 (pos->has_finished != GNUNET_YES) )
731 return; /* not transitively done yet */
734 dc->has_finished = GNUNET_YES;
735 /* signal completion */
736 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
737 make_download_status (&pi, dc);
738 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
740 if (dc->parent != NULL)
741 check_completed (dc->parent);
746 * Iterator over entries in the pending requests in the 'active' map for the
747 * reply that we just got.
749 * @param cls closure (our 'struct ProcessResultClosure')
750 * @param key query for the given value / request
751 * @param value value in the hash map (a 'struct DownloadRequest')
752 * @return GNUNET_YES (we should continue to iterate); unless serious error
755 process_result_with_request (void *cls,
756 const GNUNET_HashCode * key,
759 struct ProcessResultClosure *prc = cls;
760 struct DownloadRequest *sm = value;
761 struct GNUNET_FS_DownloadContext *dc = prc->dc;
762 struct GNUNET_CRYPTO_AesSessionKey skey;
763 struct GNUNET_CRYPTO_AesInitializationVector iv;
765 struct GNUNET_FS_ProgressInfo pi;
769 struct ContentHashKey *chk;
772 if (prc->size != calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Internal error or bogus download URI (expected %u bytes, got %u)\n",
780 calculate_block_size (GNUNET_ntohll (dc->uri->data.chk.file_length),
786 dc->emsg = GNUNET_strdup ("Internal error or bogus download URI");
788 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
789 make_download_status (&pi, dc);
790 pi.value.download.specifics.error.message = dc->emsg;
791 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
793 /* abort all pending requests */
796 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
799 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
803 GNUNET_assert (GNUNET_YES ==
804 GNUNET_CONTAINER_multihashmap_remove (dc->active,
807 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
808 GNUNET_CRYPTO_aes_decrypt (prc->data,
813 off = compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
818 if ( (NULL != dc->handle) &&
819 ( (sm->depth == dc->treedepth) ||
820 (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES)) ) )
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Saving decrypted block to disk at offset %llu\n",
826 (unsigned long long) off);
829 GNUNET_DISK_file_seek (dc->handle,
831 GNUNET_DISK_SEEK_SET) ) )
832 GNUNET_asprintf (&emsg,
833 _("Failed to seek to offset %llu in file `%s': %s\n"),
834 (unsigned long long) off,
837 else if (prc->size !=
838 GNUNET_DISK_file_write (dc->handle,
841 GNUNET_asprintf (&emsg,
842 _("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
843 (unsigned int) prc->size,
844 (unsigned long long) off,
850 // FIXME: make persistent
853 pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
854 make_download_status (&pi, dc);
855 pi.value.download.specifics.error.message = emsg;
856 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
859 /* abort all pending requests */
862 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
865 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
871 if (sm->depth == dc->treedepth)
874 if (sm->offset < dc->offset)
876 /* starting offset begins in the middle of pt,
877 do not count first bytes as progress */
878 GNUNET_assert (app > (dc->offset - sm->offset));
879 app -= (dc->offset - sm->offset);
881 if (sm->offset + prc->size > dc->offset + dc->length)
883 /* end of block is after relevant range,
884 do not count last bytes as progress */
885 GNUNET_assert (app > (sm->offset + prc->size) - (dc->offset + dc->length));
886 app -= (sm->offset + prc->size) - (dc->offset + dc->length);
888 dc->completed += app;
890 /* do recursive download if option is set and either meta data
891 says it is a directory or if no meta data is given AND filename
892 ends in '.gnd' (top-level case) */
893 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
894 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
895 ( (dc->meta == NULL) &&
896 ( (NULL == dc->filename) ||
897 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
899 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
900 GNUNET_FS_DIRECTORY_EXT)) ) ) ) ) )
901 GNUNET_FS_directory_list_contents (prc->size,
904 &trigger_recursive_download,
908 pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
909 make_download_status (&pi, dc);
910 pi.value.download.specifics.progress.data = pt;
911 pi.value.download.specifics.progress.offset = sm->offset;
912 pi.value.download.specifics.progress.data_len = prc->size;
913 pi.value.download.specifics.progress.depth = sm->depth;
914 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
916 GNUNET_assert (dc->completed <= dc->length);
917 if (dc->completed == dc->length)
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Download completed, truncating file to desired length %llu\n",
922 (unsigned long long) GNUNET_ntohll (dc->uri->data.chk.file_length));
924 /* truncate file to size (since we store IBlocks at the end) */
925 if (dc->handle != NULL)
927 GNUNET_DISK_file_close (dc->handle);
929 if (0 != truncate (dc->filename,
930 GNUNET_ntohll (dc->uri->data.chk.file_length)))
931 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
936 if ( (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
937 ( (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
938 ( (dc->meta == NULL) &&
939 ( (NULL == dc->filename) ||
940 ( (strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
942 strstr (dc->filename + strlen(dc->filename) - strlen(GNUNET_FS_DIRECTORY_EXT),
943 GNUNET_FS_DIRECTORY_EXT)) ) ) ) ) )
944 full_recursive_download (dc);
945 if (dc->child_head == NULL)
947 /* signal completion */
948 pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
949 make_download_status (&pi, dc);
950 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
952 if (dc->parent != NULL)
953 check_completed (dc->parent);
955 GNUNET_assert (sm->depth == dc->treedepth);
957 // FIXME: make persistent
958 if (sm->depth == dc->treedepth)
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
967 (unsigned long long) sm->offset);
969 GNUNET_assert (0 == (prc->size % sizeof(struct ContentHashKey)));
970 chk = (struct ContentHashKey*) pt;
971 for (i=(prc->size / sizeof(struct ContentHashKey))-1;i>=0;i--)
973 off = compute_dblock_offset (sm->offset,
977 if ( (off + DBLOCK_SIZE >= dc->offset) &&
978 (off < dc->offset + dc->length) )
979 schedule_block_download (dc,
990 * Process a download result.
992 * @param dc our download context
993 * @param type type of the result
994 * @param data the (encrypted) response
995 * @param size size of data
998 process_result (struct GNUNET_FS_DownloadContext *dc,
1003 struct ProcessResultClosure prc;
1009 GNUNET_CRYPTO_hash (data, size, &prc.query);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Received result for query `%s' from `%s'-service\n",
1013 GNUNET_h2s (&prc.query),
1016 GNUNET_CONTAINER_multihashmap_get_multiple (dc->active,
1018 &process_result_with_request,
1024 * Type of a function to call when we receive a message
1027 * @param cls closure
1028 * @param msg message received, NULL on timeout or fatal error
1031 receive_results (void *cls,
1032 const struct GNUNET_MessageHeader * msg)
1034 struct GNUNET_FS_DownloadContext *dc = cls;
1035 const struct PutMessage *cm;
1038 if ( (NULL == msg) ||
1039 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1040 (sizeof (struct PutMessage) > ntohs(msg->size)) )
1042 GNUNET_break (msg == NULL);
1046 msize = ntohs(msg->size);
1047 cm = (const struct PutMessage*) msg;
1051 msize - sizeof (struct PutMessage));
1052 if (dc->client == NULL)
1053 return; /* fatal error */
1054 /* continue receiving */
1055 GNUNET_CLIENT_receive (dc->client,
1058 GNUNET_TIME_UNIT_FOREVER_REL);
1064 * We're ready to transmit a search request to the
1065 * file-sharing service. Do it. If there is
1066 * more than one request pending, try to send
1067 * multiple or request another transmission.
1069 * @param cls closure
1070 * @param size number of bytes available in buf
1071 * @param buf where the callee should write the message
1072 * @return number of bytes written to buf
1075 transmit_download_request (void *cls,
1079 struct GNUNET_FS_DownloadContext *dc = cls;
1081 struct SearchMessage *sm;
1089 GNUNET_assert (size >= sizeof (struct SearchMessage));
1092 while ( (dc->pending != NULL) &&
1093 (size > msize + sizeof (struct SearchMessage)) )
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Transmitting download request for `%s' to `%s'-service\n",
1098 GNUNET_h2s (&dc->pending->chk.query),
1101 memset (sm, 0, sizeof (struct SearchMessage));
1102 sm->header.size = htons (sizeof (struct SearchMessage));
1103 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1104 if (dc->pending->depth == dc->treedepth)
1105 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_DBLOCK);
1107 sm->type = htonl (GNUNET_DATASTORE_BLOCKTYPE_IBLOCK);
1108 sm->anonymity_level = htonl (dc->anonymity);
1109 sm->target = dc->target.hashPubKey;
1110 sm->query = dc->pending->chk.query;
1111 dc->pending->is_pending = GNUNET_NO;
1112 dc->pending = dc->pending->next;
1113 msize += sizeof (struct SearchMessage);
1116 if (dc->pending != NULL)
1117 dc->th = GNUNET_CLIENT_notify_transmit_ready (dc->client,
1118 sizeof (struct SearchMessage),
1119 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1121 &transmit_download_request,
1128 * Reconnect to the FS service and transmit our queries NOW.
1130 * @param cls our download context
1134 do_reconnect (void *cls,
1135 const struct GNUNET_SCHEDULER_TaskContext *tc)
1137 struct GNUNET_FS_DownloadContext *dc = cls;
1138 struct GNUNET_CLIENT_Connection *client;
1140 dc->task = GNUNET_SCHEDULER_NO_TASK;
1141 client = GNUNET_CLIENT_connect (dc->h->sched,
1146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1147 "Connecting to `%s'-service failed, will try again.\n",
1152 dc->client = client;
1153 dc->th = GNUNET_CLIENT_notify_transmit_ready (client,
1154 sizeof (struct SearchMessage),
1155 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1157 &transmit_download_request,
1159 GNUNET_CLIENT_receive (client,
1162 GNUNET_TIME_UNIT_FOREVER_REL);
1167 * Add entries that are not yet pending back to the pending list.
1169 * @param cls our download context
1171 * @param entry entry of type "struct DownloadRequest"
1175 retry_entry (void *cls,
1176 const GNUNET_HashCode *key,
1179 struct GNUNET_FS_DownloadContext *dc = cls;
1180 struct DownloadRequest *dr = entry;
1182 if (! dr->is_pending)
1184 dr->next = dc->pending;
1185 dr->is_pending = GNUNET_YES;
1186 dc->pending = entry;
1193 * We've lost our connection with the FS service.
1194 * Re-establish it and re-transmit all of our
1197 * @param dc download context that is having trouble
1200 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1203 if (NULL != dc->client)
1207 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1210 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1213 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1217 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
1218 GNUNET_TIME_UNIT_SECONDS,
1225 * Download parts of a file. Note that this will store
1226 * the blocks at the respective offset in the given file. Also, the
1227 * download is still using the blocking of the underlying FS
1228 * encoding. As a result, the download may *write* outside of the
1229 * given boundaries (if offset and length do not match the 32k FS
1230 * block boundaries). <p>
1232 * This function should be used to focus a download towards a
1233 * particular portion of the file (optimization), not to strictly
1234 * limit the download to exactly those bytes.
1236 * @param h handle to the file sharing subsystem
1237 * @param uri the URI of the file (determines what to download); CHK or LOC URI
1238 * @param meta known metadata for the file (can be NULL)
1239 * @param filename where to store the file, maybe NULL (then no file is
1240 * created on disk and data must be grabbed from the callbacks)
1241 * @param offset at what offset should we start the download (typically 0)
1242 * @param length how many bytes should be downloaded starting at offset
1243 * @param anonymity anonymity level to use for the download
1244 * @param options various options
1245 * @param cctx initial value for the client context for this download
1246 * @param parent parent download to associate this download with (use NULL
1247 * for top-level downloads; useful for manually-triggered recursive downloads)
1248 * @return context that can be used to control this download
1250 struct GNUNET_FS_DownloadContext *
1251 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
1252 const struct GNUNET_FS_Uri *uri,
1253 const struct GNUNET_CONTAINER_MetaData *meta,
1254 const char *filename,
1258 enum GNUNET_FS_DownloadOptions options,
1260 struct GNUNET_FS_DownloadContext *parent)
1262 struct GNUNET_FS_ProgressInfo pi;
1263 struct GNUNET_FS_DownloadContext *dc;
1264 struct GNUNET_CLIENT_Connection *client;
1266 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
1267 if ( (offset + length < offset) ||
1268 (offset + length > uri->data.chk.file_length) )
1273 // FIXME: add support for "loc" URIs!
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Starting download `%s' of %llu bytes\n",
1278 (unsigned long long) length);
1280 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
1282 dc->parent = parent;
1285 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1289 dc->uri = GNUNET_FS_uri_dup (uri);
1290 dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1291 dc->client_info = cctx;
1292 dc->start_time = GNUNET_TIME_absolute_get ();
1293 if (NULL != filename)
1295 dc->filename = GNUNET_strdup (filename);
1296 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
1297 GNUNET_DISK_file_size (filename,
1300 dc->handle = GNUNET_DISK_file_open (filename,
1301 GNUNET_DISK_OPEN_READWRITE |
1302 GNUNET_DISK_OPEN_CREATE,
1303 GNUNET_DISK_PERM_USER_READ |
1304 GNUNET_DISK_PERM_USER_WRITE |
1305 GNUNET_DISK_PERM_GROUP_READ |
1306 GNUNET_DISK_PERM_OTHER_READ);
1307 if (dc->handle == NULL)
1309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1310 _("Download failed: could not open file `%s': %s\n"),
1313 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1314 GNUNET_FS_uri_destroy (dc->uri);
1315 GNUNET_free (dc->filename);
1320 // FIXME: set "dc->target" for LOC uris!
1321 dc->offset = offset;
1322 dc->length = length;
1323 dc->anonymity = anonymity;
1324 dc->options = options;
1325 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
1326 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_ntohll(dc->uri->data.chk.file_length));
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Download tree has depth %u\n",
1332 // FIXME: make persistent
1334 // FIXME: bound parallelism here!
1335 client = GNUNET_CLIENT_connect (h->sched,
1338 dc->client = client;
1339 schedule_block_download (dc,
1340 &dc->uri->data.chk.chk,
1342 1 /* 0 == CHK, 1 == top */);
1343 GNUNET_CLIENT_receive (client,
1346 GNUNET_TIME_UNIT_FOREVER_REL);
1347 pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1348 make_download_status (&pi, dc);
1349 pi.value.download.specifics.start.meta = meta;
1350 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1358 * Free entries in the map.
1360 * @param cls unused (NULL)
1362 * @param entry entry of type "struct DownloadRequest" which is freed
1366 free_entry (void *cls,
1367 const GNUNET_HashCode *key,
1370 GNUNET_free (entry);
1376 * Stop a download (aborts if download is incomplete).
1378 * @param dc handle for the download
1379 * @param do_delete delete files of incomplete downloads
1382 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc,
1385 struct GNUNET_FS_ProgressInfo pi;
1387 while (NULL != dc->child_head)
1388 GNUNET_FS_download_stop (dc->child_head,
1390 // FIXME: make unpersistent
1391 if (dc->parent != NULL)
1392 GNUNET_CONTAINER_DLL_remove (dc->parent->child_head,
1393 dc->parent->child_tail,
1396 pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
1397 make_download_status (&pi, dc);
1398 dc->client_info = dc->h->upcb (dc->h->upcb_cls,
1401 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
1402 GNUNET_SCHEDULER_cancel (dc->h->sched,
1406 GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1409 if (NULL != dc->client)
1410 GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1411 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1414 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1415 if (dc->filename != NULL)
1417 if (NULL != dc->handle)
1418 GNUNET_DISK_file_close (dc->handle);
1419 if ( (dc->completed != dc->length) &&
1420 (GNUNET_YES == do_delete) )
1422 if (0 != UNLINK (dc->filename))
1423 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1427 GNUNET_free (dc->filename);
1429 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1430 GNUNET_FS_uri_destroy (dc->uri);
1434 /* end of fs_download.c */