38b55ba4dc555fa54499d4878f675edd6ccf48ab
[oweals/gnunet.git] / src / fs / fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20 /**
21  * @file fs/fs_download.c
22  * @brief download methods
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_fs_service.h"
28 #include "fs_api.h"
29 #include "fs_tree.h"
30
31
32 /**
33  * Determine if the given download (options and meta data) should cause
34  * use to try to do a recursive download.
35  */
36 static int
37 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
38 {
39   return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
40       ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
41        ((dc->meta == NULL) &&
42         ((NULL == dc->filename) ||
43          ((strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
44           (NULL !=
45            strstr (dc->filename + strlen (dc->filename) -
46                    strlen (GNUNET_FS_DIRECTORY_EXT),
47                    GNUNET_FS_DIRECTORY_EXT))))));
48 }
49
50
51 /**
52  * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
53  * only have to truncate the file once we're done).
54  *
55  * Given the offset of a block (with respect to the DBLOCKS) and its
56  * depth, return the offset where we would store this block in the
57  * file.
58  *
59  * @param fsize overall file size
60  * @param off offset of the block in the file
61  * @param depth depth of the block in the tree, 0 for DBLOCK
62  * @return off for DBLOCKS (depth == treedepth),
63  *         otherwise an offset past the end
64  *         of the file that does not overlap
65  *         with the range for any other block
66  */
67 static uint64_t
68 compute_disk_offset (uint64_t fsize, uint64_t off, unsigned int depth)
69 {
70   unsigned int i;
71   uint64_t lsize;               /* what is the size of all IBlocks for depth "i"? */
72   uint64_t loff;                /* where do IBlocks for depth "i" start? */
73   unsigned int ioff;            /* which IBlock corresponds to "off" at depth "i"? */
74
75   if (depth == 0)
76     return off;
77   /* first IBlocks start at the end of file, rounded up
78    * to full DBLOCK_SIZE */
79   loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
80   lsize =
81       ((fsize + DBLOCK_SIZE -
82         1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
83   GNUNET_assert (0 == (off % DBLOCK_SIZE));
84   ioff = (off / DBLOCK_SIZE);
85   for (i = 1; i < depth; i++)
86   {
87     loff += lsize;
88     lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
89     GNUNET_assert (lsize > 0);
90     GNUNET_assert (0 == (ioff % CHK_PER_INODE));
91     ioff /= CHK_PER_INODE;
92   }
93   return loff + ioff * sizeof (struct ContentHashKey);
94 }
95
96
97 /**
98  * Fill in all of the generic fields for a download event and call the
99  * callback.
100  *
101  * @param pi structure to fill in
102  * @param dc overall download context
103  */
104 void
105 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
106                                  struct GNUNET_FS_DownloadContext *dc)
107 {
108   pi->value.download.dc = dc;
109   pi->value.download.cctx = dc->client_info;
110   pi->value.download.pctx =
111       (dc->parent == NULL) ? NULL : dc->parent->client_info;
112   pi->value.download.sctx =
113       (dc->search == NULL) ? NULL : dc->search->client_info;
114   pi->value.download.uri = dc->uri;
115   pi->value.download.filename = dc->filename;
116   pi->value.download.size = dc->length;
117   /* FIXME: Fix duration calculation to account for pauses */
118   pi->value.download.duration =
119       GNUNET_TIME_absolute_get_duration (dc->start_time);
120   pi->value.download.completed = dc->completed;
121   pi->value.download.anonymity = dc->anonymity;
122   pi->value.download.eta =
123       GNUNET_TIME_calculate_eta (dc->start_time, dc->completed, dc->length);
124   pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
125   if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
126     dc->client_info = dc->h->upcb (dc->h->upcb_cls, pi);
127   else
128     dc->client_info = GNUNET_FS_search_probe_progress_ (NULL, pi);
129 }
130
131
132 /**
133  * We're ready to transmit a search request to the
134  * file-sharing service.  Do it.  If there is
135  * more than one request pending, try to send
136  * multiple or request another transmission.
137  *
138  * @param cls closure
139  * @param size number of bytes available in buf
140  * @param buf where the callee should write the message
141  * @return number of bytes written to buf
142  */
143 static size_t
144 transmit_download_request (void *cls, size_t size, void *buf);
145
146
147 /**
148  * Closure for iterator processing results.
149  */
150 struct ProcessResultClosure
151 {
152
153   /**
154    * Hash of data.
155    */
156   GNUNET_HashCode query;
157
158   /**
159    * Data found in P2P network.
160    */
161   const void *data;
162
163   /**
164    * Our download context.
165    */
166   struct GNUNET_FS_DownloadContext *dc;
167
168   /**
169    * Number of bytes in data.
170    */
171   size_t size;
172
173   /**
174    * Type of data.
175    */
176   enum GNUNET_BLOCK_Type type;
177
178   /**
179    * Flag to indicate if this block should be stored on disk.
180    */
181   int do_store;
182
183   /**
184    * When did we last transmit the request?
185    */
186   struct GNUNET_TIME_Absolute last_transmission;
187
188 };
189
190
191 /**
192  * Iterator over entries in the pending requests in the 'active' map for the
193  * reply that we just got.
194  *
195  * @param cls closure (our 'struct ProcessResultClosure')
196  * @param key query for the given value / request
197  * @param value value in the hash map (a 'struct DownloadRequest')
198  * @return GNUNET_YES (we should continue to iterate); unless serious error
199  */
200 static int
201 process_result_with_request (void *cls, const GNUNET_HashCode * key,
202                              void *value);
203
204
205 /**
206  * We've found a matching block without downloading it.
207  * Encrypt it and pass it to our "receive" function as
208  * if we had received it from the network.
209  *
210  * @param dc download in question
211  * @param chk request this relates to
212  * @param dr request details
213  * @param block plaintext data matching request
214  * @param len number of bytes in block
215  * @param do_store should we still store the block on disk?
216  * @return GNUNET_OK on success
217  */
218 static int
219 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
220                         const struct ContentHashKey *chk,
221                         struct DownloadRequest *dr, const char *block,
222                         size_t len, int do_store)
223 {
224   struct ProcessResultClosure prc;
225   char enc[len];
226   struct GNUNET_CRYPTO_AesSessionKey sk;
227   struct GNUNET_CRYPTO_AesInitializationVector iv;
228   GNUNET_HashCode query;
229
230   GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
231   if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len, &sk, &iv, enc))
232   {
233     GNUNET_break (0);
234     return GNUNET_SYSERR;
235   }
236   GNUNET_CRYPTO_hash (enc, len, &query);
237   if (0 != memcmp (&query, &chk->query, sizeof (GNUNET_HashCode)))
238   {
239     GNUNET_break_op (0);
240     return GNUNET_SYSERR;
241   }
242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243               "Matching %u byte block for `%s' at offset %llu already present, no need for download!\n",
244               (unsigned int) len,
245               dc->filename, (unsigned long long) dr->offset);
246   /* already got it! */
247   prc.dc = dc;
248   prc.data = enc;
249   prc.size = len;
250   prc.type =
251       (0 ==
252        dr->depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK : GNUNET_BLOCK_TYPE_FS_IBLOCK;
253   prc.query = chk->query;
254   prc.do_store = do_store;
255   prc.last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
256   process_result_with_request (&prc, &chk->key, dr);
257   return GNUNET_OK;
258 }
259
260
261 /**
262  * We've lost our connection with the FS service.
263  * Re-establish it and re-transmit all of our
264  * pending requests.
265  *
266  * @param dc download context that is having trouble
267  */
268 static void
269 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
270
271
272 /**
273  * We found an entry in a directory.  Check if the respective child
274  * already exists and if not create the respective child download.
275  *
276  * @param cls the parent download
277  * @param filename name of the file in the directory
278  * @param uri URI of the file (CHK or LOC)
279  * @param meta meta data of the file
280  * @param length number of bytes in data
281  * @param data contents of the file (or NULL if they were not inlined)
282  */
283 static void
284 trigger_recursive_download (void *cls, const char *filename,
285                             const struct GNUNET_FS_Uri *uri,
286                             const struct GNUNET_CONTAINER_MetaData *meta,
287                             size_t length, const void *data);
288
289
290 /**
291  * We're done downloading a directory.  Open the file and
292  * trigger all of the (remaining) child downloads.
293  *
294  * @param dc context of download that just completed
295  */
296 static void
297 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
298 {
299   size_t size;
300   uint64_t size64;
301   void *data;
302   struct GNUNET_DISK_FileHandle *h;
303   struct GNUNET_DISK_MapHandle *m;
304
305   size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
306   size = (size_t) size64;
307   if (size64 != (uint64_t) size)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 _
311                 ("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
312     return;
313   }
314   if (dc->filename != NULL)
315   {
316     h = GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
317                                GNUNET_DISK_PERM_NONE);
318   }
319   else
320   {
321     GNUNET_assert (dc->temp_filename != NULL);
322     h = GNUNET_DISK_file_open (dc->temp_filename, GNUNET_DISK_OPEN_READ,
323                                GNUNET_DISK_PERM_NONE);
324   }
325   if (h == NULL)
326     return;                     /* oops */
327   data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
328   if (data == NULL)
329   {
330     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331                 _("Directory too large for system address space\n"));
332   }
333   else
334   {
335     GNUNET_FS_directory_list_contents (size, data, 0,
336                                        &trigger_recursive_download, dc);
337     GNUNET_DISK_file_unmap (m);
338   }
339   GNUNET_DISK_file_close (h);
340   if (dc->filename == NULL)
341   {
342     if (0 != UNLINK (dc->temp_filename))
343       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
344                                 dc->temp_filename);
345     GNUNET_free (dc->temp_filename);
346     dc->temp_filename = NULL;
347   }
348 }
349
350
351 /**
352  * Check if all child-downloads have completed (or trigger them if
353  * necessary) and once we're completely done, signal completion (and
354  * possibly recurse to parent).  This function MUST be called when the
355  * download of a file itself is done or when the download of a file is
356  * done and then later a direct child download has completed (and
357  * hence this download may complete itself).
358  *
359  * @param dc download to check for completion of children
360  */
361 static void
362 check_completed (struct GNUNET_FS_DownloadContext *dc)
363 {
364   struct GNUNET_FS_ProgressInfo pi;
365   struct GNUNET_FS_DownloadContext *pos;
366
367   /* first, check if we need to download children */
368   if ((dc->child_head == NULL) && (is_recursive_download (dc)))
369     full_recursive_download (dc);
370   /* then, check if children are done already */
371   pos = dc->child_head;
372   while (pos != NULL)
373   {
374     if ((pos->emsg == NULL) && (pos->completed < pos->length))
375       return;                   /* not done yet */
376     if ((pos->child_head != NULL) && (pos->has_finished != GNUNET_YES))
377       return;                   /* not transitively done yet */
378     pos = pos->next;
379   }
380   /* All of our children are done, so mark this download done */
381   dc->has_finished = GNUNET_YES;
382   if (dc->job_queue != NULL)
383   {
384     GNUNET_FS_dequeue_ (dc->job_queue);
385     dc->job_queue = NULL;
386   }
387   if (GNUNET_SCHEDULER_NO_TASK != dc->task)
388   {
389     GNUNET_SCHEDULER_cancel (dc->task);
390     dc->task = GNUNET_SCHEDULER_NO_TASK;
391   }
392   if (dc->rfh != NULL)
393   {
394     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
395     dc->rfh = NULL;
396   }
397   GNUNET_FS_download_sync_ (dc);
398
399   /* signal completion */
400   pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
401   GNUNET_FS_download_make_status_ (&pi, dc);
402
403   /* let parent know */
404   if (dc->parent != NULL)
405     check_completed (dc->parent);
406 }
407
408
409 /**
410  * We got a block of plaintext data (from the meta data).
411  * Try it for upward reconstruction of the data.  On success,
412  * the top-level block will move to state BRS_DOWNLOAD_UP.
413  *
414  * @param dc context for the download
415  * @param dr download request to match against
416  * @param data plaintext data, starting from the beginning of the file
417  * @param data_len number of bytes in data
418  */
419 static void
420 try_match_block (struct GNUNET_FS_DownloadContext *dc,
421                  struct DownloadRequest *dr, const char *data, size_t data_len)
422 {
423   struct GNUNET_FS_ProgressInfo pi;
424   unsigned int i;
425   char enc[DBLOCK_SIZE];
426   struct ContentHashKey chks[CHK_PER_INODE];
427   struct ContentHashKey in_chk;
428   struct GNUNET_CRYPTO_AesSessionKey sk;
429   struct GNUNET_CRYPTO_AesInitializationVector iv;
430   size_t dlen;
431   struct DownloadRequest *drc;
432   struct GNUNET_DISK_FileHandle *fh;
433   int complete;
434   const char *fn;
435   const char *odata;
436   size_t odata_len;
437
438   odata = data;
439   odata_len = data_len;
440   if (BRS_DOWNLOAD_UP == dr->state)
441     return;
442   if (dr->depth > 0)
443   {
444     complete = GNUNET_YES;
445     for (i = 0; i < dr->num_children; i++)
446     {
447       drc = dr->children[i];
448       try_match_block (dc, drc, data, data_len);
449       if (drc->state != BRS_RECONSTRUCT_META_UP)
450         complete = GNUNET_NO;
451       else
452         chks[i] = drc->chk;
453     }
454     if (GNUNET_YES != complete)
455       return;
456     data = (const char *) chks;
457     dlen = dr->num_children * sizeof (struct ContentHashKey);
458   }
459   else
460   {
461     if (dr->offset > data_len)
462       return;                   /* oops */
463     dlen = GNUNET_MIN (data_len - dr->offset, DBLOCK_SIZE);
464   }
465   GNUNET_CRYPTO_hash (&data[dr->offset], dlen, &in_chk.key);
466   GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
467   if (-1 == GNUNET_CRYPTO_aes_encrypt (&data[dr->offset], dlen, &sk, &iv, enc))
468   {
469     GNUNET_break (0);
470     return;
471   }
472   GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
473   switch (dr->state)
474   {
475   case BRS_INIT:
476     dr->chk = in_chk;
477     dr->state = BRS_RECONSTRUCT_META_UP;
478     break;
479   case BRS_CHK_SET:
480     if (0 != memcmp (&in_chk, &dr->chk, sizeof (struct ContentHashKey)))
481     {
482       /* other peer provided bogus meta data */
483       GNUNET_break_op (0);
484       break;
485     }
486     /* write block to disk */
487     fn = dc->filename != NULL ? dc->filename : dc->temp_filename;
488     fh = GNUNET_DISK_file_open (fn,
489                                 GNUNET_DISK_OPEN_READWRITE |
490                                 GNUNET_DISK_OPEN_CREATE |
491                                 GNUNET_DISK_OPEN_TRUNCATE,
492                                 GNUNET_DISK_PERM_USER_READ |
493                                 GNUNET_DISK_PERM_USER_WRITE |
494                                 GNUNET_DISK_PERM_GROUP_READ |
495                                 GNUNET_DISK_PERM_OTHER_READ);
496     if (fh == NULL)
497     {
498       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
499       GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
500                        fn);
501       GNUNET_DISK_file_close (fh);
502       dr->state = BRS_ERROR;
503       pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
504       pi.value.download.specifics.error.message = dc->emsg;
505       GNUNET_FS_download_make_status_ (&pi, dc);
506       return;
507     }
508     if (data_len != GNUNET_DISK_file_write (fh, odata, odata_len))
509     {
510       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
511       GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
512                        fn);
513       GNUNET_DISK_file_close (fh);
514       dr->state = BRS_ERROR;
515       pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
516       pi.value.download.specifics.error.message = dc->emsg;
517       GNUNET_FS_download_make_status_ (&pi, dc);
518       return;
519     }
520     GNUNET_DISK_file_close (fh);
521     /* signal success */
522     dr->state = BRS_DOWNLOAD_UP;
523     dc->completed = dc->length;
524     GNUNET_FS_download_sync_ (dc);
525     pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
526     pi.value.download.specifics.progress.data = data;
527     pi.value.download.specifics.progress.offset = 0;
528     pi.value.download.specifics.progress.data_len = dlen;
529     pi.value.download.specifics.progress.depth = 0;
530     pi.value.download.specifics.progress.trust_offered = 0;
531     pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
532     GNUNET_FS_download_make_status_ (&pi, dc);
533     if ((NULL != dc->filename) &&
534         (0 !=
535          truncate (dc->filename,
536                    GNUNET_ntohll (dc->uri->data.chk.file_length))))
537       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
538                                 dc->filename);
539     check_completed (dc);
540     break;
541   default:
542     /* how did we get here? */
543     GNUNET_break (0);
544     break;
545   }
546 }
547
548
549 /**
550  * Type of a function that libextractor calls for each
551  * meta data item found.  If we find full data meta data,
552  * call 'try_match_block' on it.
553  *
554  * @param cls our 'struct GNUNET_FS_DownloadContext*'
555  * @param plugin_name name of the plugin that produced this value;
556  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
557  *        used in the main libextractor library and yielding
558  *        meta data).
559  * @param type libextractor-type describing the meta data
560  * @param format basic format information about data
561  * @param data_mime_type mime-type of data (not of the original file);
562  *        can be NULL (if mime-type is not known)
563  * @param data actual meta-data found
564  * @param data_len number of bytes in data
565  * @return 0 to continue extracting, 1 to abort
566  */
567 static int
568 match_full_data (void *cls, const char *plugin_name,
569                  enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
570                  const char *data_mime_type, const char *data, size_t data_len)
571 {
572   struct GNUNET_FS_DownloadContext *dc = cls;
573
574   if (type != EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
575     return 0;
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u bytes of FD!\n",
577               (unsigned int) data_len);
578   if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
579   {
580     GNUNET_break_op (0);
581     return 1;                   /* bogus meta data */
582   }
583   try_match_block (dc, dc->top_request, data, data_len);
584   return 1;
585 }
586
587
588 /**
589  * Set the state of the given download request to
590  * BRS_DOWNLOAD_UP and propagate it up the tree.
591  *
592  * @param dr download request that is done
593  */
594 static void
595 propagate_up (struct DownloadRequest *dr)
596 {
597   unsigned int i;
598
599   do
600   {
601     dr->state = BRS_DOWNLOAD_UP;
602     dr = dr->parent;
603     if (dr == NULL)
604       break;
605     for (i = 0; i < dr->num_children; i++)
606       if (dr->children[i]->state != BRS_DOWNLOAD_UP)
607         break;
608   }
609   while (i == dr->num_children);
610 }
611
612
613 /**
614  * Try top-down reconstruction.  Before, the given request node
615  * must have the state BRS_CHK_SET.  Afterwards, more nodes may
616  * have that state or advanced to BRS_DOWNLOAD_DOWN or even
617  * BRS_DOWNLOAD_UP.  It is also possible to get BRS_ERROR on the
618  * top level.
619  *
620  * @param dc overall download this block belongs to
621  * @param dr block to reconstruct
622  */
623 static void
624 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
625                              struct DownloadRequest *dr)
626 {
627   uint64_t off;
628   char block[DBLOCK_SIZE];
629   GNUNET_HashCode key;
630   uint64_t total;
631   size_t len;
632   unsigned int i;
633   struct DownloadRequest *drc;
634   uint64_t child_block_size;
635   const struct ContentHashKey *chks;
636   int up_done;
637
638   GNUNET_assert (dc->rfh != NULL);
639   GNUNET_assert (dr->state == BRS_CHK_SET);
640   total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
641   GNUNET_assert (dr->depth < dc->treedepth);
642   len = GNUNET_FS_tree_calculate_block_size (total, dr->offset, dr->depth);
643   GNUNET_assert (len <= DBLOCK_SIZE);
644   off = compute_disk_offset (total, dr->offset, dr->depth);
645   if (dc->old_file_size < off + len)
646     return;                     /* failure */
647   if (off != GNUNET_DISK_file_seek (dc->rfh, off, GNUNET_DISK_SEEK_SET))
648   {
649     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "seek", dc->filename);
650     return;                     /* failure */
651   }
652   if (len != GNUNET_DISK_file_read (dc->rfh, block, len))
653   {
654     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", dc->filename);
655     return;                     /* failure */
656   }
657   GNUNET_CRYPTO_hash (block, len, &key);
658   if (0 != memcmp (&key, &dr->chk.key, sizeof (GNUNET_HashCode)))
659     return;                     /* mismatch */
660   if (GNUNET_OK !=
661       encrypt_existing_match (dc, &dr->chk, dr, block, len, GNUNET_NO))
662   {
663     /* hash matches but encrypted block does not, really bad */
664     dr->state = BRS_ERROR;
665     /* propagate up */
666     while (dr->parent != NULL)
667     {
668       dr = dr->parent;
669       dr->state = BRS_ERROR;
670     }
671     return;
672   }
673   /* block matches */
674   dr->state = BRS_DOWNLOAD_DOWN;
675
676   /* set CHKs for children */
677   up_done = GNUNET_YES;
678   chks = (const struct ContentHashKey *) block;
679   for (i = 0; i < dr->num_children; i++)
680   {
681     drc = dr->children[i];
682     GNUNET_assert (drc->offset >= dr->offset);
683     child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
684     GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);     
685     if (drc->state == BRS_INIT)
686     {
687       drc->state = BRS_CHK_SET;
688       drc->chk = chks[drc->chk_idx];
689       try_top_down_reconstruction (dc, drc);
690     }
691     if (drc->state != BRS_DOWNLOAD_UP)
692       up_done = GNUNET_NO;      /* children not all done */
693   }
694   if (up_done == GNUNET_YES)
695     propagate_up (dr);          /* children all done (or no children...) */
696 }
697
698
699 /**
700  * Schedule the download of the specified block in the tree.
701  *
702  * @param dc overall download this block belongs to
703  * @param dr request to schedule
704  */
705 static void
706 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
707                          struct DownloadRequest *dr)
708 {
709   unsigned int i;
710
711   switch (dr->state)
712   {
713   case BRS_INIT:
714     GNUNET_assert (0);
715     break;
716   case BRS_RECONSTRUCT_DOWN:
717     GNUNET_assert (0);
718     break;
719   case BRS_RECONSTRUCT_META_UP:
720     GNUNET_assert (0);
721     break;
722   case BRS_RECONSTRUCT_UP:
723     GNUNET_assert (0);
724     break;
725   case BRS_CHK_SET:
726     /* normal case, start download */
727     break;
728   case BRS_DOWNLOAD_DOWN:
729     for (i = 0; i < dr->num_children; i++)
730       schedule_block_download (dc, dr->children[i]);
731     return;
732   case BRS_DOWNLOAD_UP:
733     /* We're done! */
734     return;
735   case BRS_ERROR:
736     GNUNET_break (0);
737     return;
738   }
739   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740               "Scheduling download at offset %llu and depth %u for `%s'\n",
741               (unsigned long long) dr->offset, dr->depth,
742               GNUNET_h2s (&dr->chk.query));
743   if (GNUNET_NO !=
744       GNUNET_CONTAINER_multihashmap_contains_value (dc->active, &dr->chk.query,
745                                                     dr))
746     return;                     /* already active */
747   GNUNET_CONTAINER_multihashmap_put (dc->active, &dr->chk.query, dr,
748                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
749   if (dc->client == NULL)
750     return;                     /* download not active */
751   GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
752   dr->is_pending = GNUNET_YES;
753   if (NULL == dc->th)
754     dc->th =
755         GNUNET_CLIENT_notify_transmit_ready (dc->client,
756                                              sizeof (struct SearchMessage),
757                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
758                                              GNUNET_NO,
759                                              &transmit_download_request, dc);
760 }
761
762
763 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
764
765 /**
766  * We found an entry in a directory.  Check if the respective child
767  * already exists and if not create the respective child download.
768  *
769  * @param cls the parent download
770  * @param filename name of the file in the directory
771  * @param uri URI of the file (CHK or LOC)
772  * @param meta meta data of the file
773  * @param length number of bytes in data
774  * @param data contents of the file (or NULL if they were not inlined)
775  */
776 static void
777 trigger_recursive_download (void *cls, const char *filename,
778                             const struct GNUNET_FS_Uri *uri,
779                             const struct GNUNET_CONTAINER_MetaData *meta,
780                             size_t length, const void *data)
781 {
782   struct GNUNET_FS_DownloadContext *dc = cls;
783   struct GNUNET_FS_DownloadContext *cpos;
784   char *temp_name;
785   char *fn;
786   char *us;
787   char *ext;
788   char *dn;
789   char *pos;
790   char *full_name;
791   char *sfn;
792
793   if (NULL == uri)
794     return;                     /* entry for the directory itself */
795   cpos = dc->child_head;
796   while (cpos != NULL)
797   {
798     if ((GNUNET_FS_uri_test_equal (uri, cpos->uri)) ||
799         ((filename != NULL) && (0 == strcmp (cpos->filename, filename))))
800       break;
801     cpos = cpos->next;
802   }
803   if (cpos != NULL)
804     return;                     /* already exists */
805   fn = NULL;
806   if (NULL == filename)
807   {
808     fn = GNUNET_FS_meta_data_suggest_filename (meta);
809     if (fn == NULL)
810     {
811       us = GNUNET_FS_uri_to_string (uri);
812       fn = GNUNET_strdup (&us[strlen (GNUNET_FS_URI_CHK_PREFIX)]);
813       GNUNET_free (us);
814     }
815     else if (fn[0] == '.')
816     {
817       ext = fn;
818       us = GNUNET_FS_uri_to_string (uri);
819       GNUNET_asprintf (&fn, "%s%s", &us[strlen (GNUNET_FS_URI_CHK_PREFIX)],
820                        ext);
821       GNUNET_free (ext);
822       GNUNET_free (us);
823     }
824     /* change '\' to '/' (this should have happened
825      * during insertion, but malicious peers may
826      * not have done this) */
827     while (NULL != (pos = strstr (fn, "\\")))
828       *pos = '/';
829     /* remove '../' everywhere (again, well-behaved
830      * peers don't do this, but don't trust that
831      * we did not get something nasty) */
832     while (NULL != (pos = strstr (fn, "../")))
833     {
834       pos[0] = '_';
835       pos[1] = '_';
836       pos[2] = '_';
837     }
838     filename = fn;
839   }
840   if (dc->filename == NULL)
841   {
842     full_name = NULL;
843   }
844   else
845   {
846     dn = GNUNET_strdup (dc->filename);
847     GNUNET_break ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
848                   (NULL !=
849                    strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
850                            GNUNET_FS_DIRECTORY_EXT)));
851     sfn = GNUNET_strdup (filename);
852     while ((strlen (sfn) > 0) && (filename[strlen (sfn) - 1] == '/'))
853       sfn[strlen (sfn) - 1] = '\0';
854     if ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
855         (NULL !=
856          strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
857                  GNUNET_FS_DIRECTORY_EXT)))
858       dn[strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
859     if ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
860         ((strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
861          (NULL ==
862           strstr (filename + strlen (filename) -
863                   strlen (GNUNET_FS_DIRECTORY_EXT), GNUNET_FS_DIRECTORY_EXT))))
864     {
865       GNUNET_asprintf (&full_name, "%s%s%s%s", dn, DIR_SEPARATOR_STR, sfn,
866                        GNUNET_FS_DIRECTORY_EXT);
867     }
868     else
869     {
870       GNUNET_asprintf (&full_name, "%s%s%s", dn, DIR_SEPARATOR_STR, sfn);
871     }
872     GNUNET_free (sfn);
873     GNUNET_free (dn);
874   }
875   if ((full_name != NULL) &&
876       (GNUNET_OK != GNUNET_DISK_directory_create_for_file (full_name)))
877   {
878     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
879                 _
880                 ("Failed to create directory for recursive download of `%s'\n"),
881                 full_name);
882     GNUNET_free (full_name);
883     GNUNET_free_non_null (fn);
884     return;
885   }
886
887   temp_name = NULL;
888   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889               "Triggering recursive download of size %llu with %u bytes MD\n",
890               (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
891               (unsigned int)
892               GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
893   GNUNET_FS_download_start (dc->h, uri, meta, full_name, temp_name, 0,
894                             GNUNET_FS_uri_chk_get_file_size (uri),
895                             dc->anonymity, dc->options, NULL, dc);
896   GNUNET_free_non_null (full_name);
897   GNUNET_free_non_null (temp_name);
898   GNUNET_free_non_null (fn);
899 }
900
901
902 /**
903  * (recursively) free download request structure
904  *
905  * @param dr request to free
906  */
907 void
908 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
909 {
910   unsigned int i;
911
912   if (dr == NULL)
913     return;
914   for (i = 0; i < dr->num_children; i++)
915     GNUNET_FS_free_download_request_ (dr->children[i]);
916   GNUNET_free_non_null (dr->children);
917   GNUNET_free (dr);
918 }
919
920
921 /**
922  * Iterator over entries in the pending requests in the 'active' map for the
923  * reply that we just got.
924  *
925  * @param cls closure (our 'struct ProcessResultClosure')
926  * @param key query for the given value / request
927  * @param value value in the hash map (a 'struct DownloadRequest')
928  * @return GNUNET_YES (we should continue to iterate); unless serious error
929  */
930 static int
931 process_result_with_request (void *cls, const GNUNET_HashCode * key,
932                              void *value)
933 {
934   struct ProcessResultClosure *prc = cls;
935   struct DownloadRequest *dr = value;
936   struct GNUNET_FS_DownloadContext *dc = prc->dc;
937   struct DownloadRequest *drc;
938   struct GNUNET_DISK_FileHandle *fh = NULL;
939   struct GNUNET_CRYPTO_AesSessionKey skey;
940   struct GNUNET_CRYPTO_AesInitializationVector iv;
941   char pt[prc->size];
942   struct GNUNET_FS_ProgressInfo pi;
943   uint64_t off;
944   size_t bs;
945   size_t app;
946   int i;
947   struct ContentHashKey *chkarr;
948
949   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950               "Received %u byte block `%s' matching pending request at depth %u and offset %llu/%llu\n",
951               (unsigned int) prc->size,
952               GNUNET_h2s (key), dr->depth, (unsigned long long) dr->offset,
953               (unsigned long long) GNUNET_ntohll (dc->uri->data.
954                                                   chk.file_length));
955   bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll
956                                             (dc->uri->data.chk.file_length),
957                                             dr->offset, dr->depth);
958   if (prc->size != bs)
959   {
960     GNUNET_asprintf (&dc->emsg,
961                      _
962                      ("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)\n"),
963                      bs, dr->depth, (unsigned long long) dr->offset,
964                      (unsigned long long) GNUNET_ntohll (dc->uri->data.
965                                                          chk.file_length),
966                      prc->size);
967     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s", dc->emsg);
968     while (dr->parent != NULL)
969     {
970       dr->state = BRS_ERROR;
971       dr = dr->parent;
972     }
973     dr->state = BRS_ERROR;
974     goto signal_error;
975   }
976
977   (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &prc->query, dr);
978   if (GNUNET_YES == dr->is_pending)
979   {
980     GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
981     dr->is_pending = GNUNET_NO;
982   }
983
984   GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key, &skey, &iv);
985   if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data, prc->size, &skey, &iv, pt))
986   {
987     GNUNET_break (0);
988     dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
989     goto signal_error;
990   }
991   off =
992       compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
993                            dr->offset, dr->depth);
994   /* save to disk */
995   if ((GNUNET_YES == prc->do_store) &&
996       ((dc->filename != NULL) || (is_recursive_download (dc))) &&
997       ((dr->depth == dc->treedepth) ||
998        (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES))))
999   {
1000     fh = GNUNET_DISK_file_open (dc->filename !=
1001                                 NULL ? dc->filename : dc->temp_filename,
1002                                 GNUNET_DISK_OPEN_READWRITE |
1003                                 GNUNET_DISK_OPEN_CREATE,
1004                                 GNUNET_DISK_PERM_USER_READ |
1005                                 GNUNET_DISK_PERM_USER_WRITE |
1006                                 GNUNET_DISK_PERM_GROUP_READ |
1007                                 GNUNET_DISK_PERM_OTHER_READ);
1008     if (NULL == fh)
1009     {
1010       GNUNET_asprintf (&dc->emsg,
1011                        _("Download failed: could not open file `%s': %s\n"),
1012                        dc->filename, STRERROR (errno));
1013       goto signal_error;
1014     }
1015     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016                 "Saving decrypted block to disk at offset %llu\n",
1017                 (unsigned long long) off);
1018     if ((off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)))
1019     {
1020       GNUNET_asprintf (&dc->emsg,
1021                        _("Failed to seek to offset %llu in file `%s': %s\n"),
1022                        (unsigned long long) off, dc->filename,
1023                        STRERROR (errno));
1024       goto signal_error;
1025     }
1026     if (prc->size != GNUNET_DISK_file_write (fh, pt, prc->size))
1027     {
1028       GNUNET_asprintf (&dc->emsg,
1029                        _
1030                        ("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1031                        (unsigned int) prc->size, (unsigned long long) off,
1032                        dc->filename, STRERROR (errno));
1033       goto signal_error;
1034     }
1035     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1036     fh = NULL;
1037   }
1038
1039   if (dr->depth == 0)
1040   {
1041     /* DBLOCK, update progress and try recursion if applicable */
1042     app = prc->size;
1043     if (dr->offset < dc->offset)
1044     {
1045       /* starting offset begins in the middle of pt,
1046        * do not count first bytes as progress */
1047       GNUNET_assert (app > (dc->offset - dr->offset));
1048       app -= (dc->offset - dr->offset);
1049     }
1050     if (dr->offset + prc->size > dc->offset + dc->length)
1051     {
1052       /* end of block is after relevant range,
1053        * do not count last bytes as progress */
1054       GNUNET_assert (app >
1055                      (dr->offset + prc->size) - (dc->offset + dc->length));
1056       app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1057     }
1058     dc->completed += app;
1059
1060     /* do recursive download if option is set and either meta data
1061      * says it is a directory or if no meta data is given AND filename
1062      * ends in '.gnd' (top-level case) */
1063     if (is_recursive_download (dc))
1064       GNUNET_FS_directory_list_contents (prc->size, pt, off,
1065                                          &trigger_recursive_download, dc);
1066   }
1067   GNUNET_assert (dc->completed <= dc->length);
1068   dr->state = BRS_DOWNLOAD_DOWN;
1069   pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1070   pi.value.download.specifics.progress.data = pt;
1071   pi.value.download.specifics.progress.offset = dr->offset;
1072   pi.value.download.specifics.progress.data_len = prc->size;
1073   pi.value.download.specifics.progress.depth = dr->depth;
1074   pi.value.download.specifics.progress.trust_offered = 0;
1075   if (prc->last_transmission.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
1076     pi.value.download.specifics.progress.block_download_duration 
1077       = GNUNET_TIME_absolute_get_duration (prc->last_transmission);
1078   else
1079     pi.value.download.specifics.progress.block_download_duration
1080       = GNUNET_TIME_UNIT_ZERO; /* found locally */
1081   GNUNET_FS_download_make_status_ (&pi, dc);
1082   if (dr->depth == 0)
1083     propagate_up (dr);
1084
1085   if (dc->completed == dc->length)
1086   {
1087     /* download completed, signal */
1088     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089                 "Download completed, truncating file to desired length %llu\n",
1090                 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1091                                                     chk.file_length));
1092     /* truncate file to size (since we store IBlocks at the end) */
1093     if (dc->filename != NULL)
1094     {
1095       if (0 !=
1096           truncate (dc->filename,
1097                     GNUNET_ntohll (dc->uri->data.chk.file_length)))
1098         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1099                                   dc->filename);
1100     }
1101     GNUNET_assert (dr->depth == 0);
1102     check_completed (dc);
1103   }
1104   if (dr->depth == 0)
1105   {
1106     /* bottom of the tree, no child downloads possible, just sync */
1107     GNUNET_FS_download_sync_ (dc);
1108     return GNUNET_YES;
1109   }
1110
1111   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112               "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1113               dr->depth, (unsigned long long) dr->offset);
1114   GNUNET_assert (0 == (prc->size % sizeof (struct ContentHashKey)));
1115   chkarr = (struct ContentHashKey *) pt;
1116   for (i = dr->num_children - 1; i >= 0; i--)
1117   {
1118     drc = dr->children[i];
1119     switch (drc->state)
1120     {
1121     case BRS_INIT:
1122       if ((drc->chk_idx + 1) * sizeof (struct ContentHashKey) > prc->size)
1123       {
1124         /* 'chkarr' does not have enough space for this chk_idx;
1125            internal error! */
1126         GNUNET_break (0);
1127         return GNUNET_SYSERR;
1128       }
1129       drc->chk = chkarr[drc->chk_idx];
1130       drc->state = BRS_CHK_SET;
1131       if (GNUNET_YES == dc->issue_requests)
1132         schedule_block_download (dc, drc);
1133       break;
1134     case BRS_RECONSTRUCT_DOWN:
1135       GNUNET_assert (0);
1136       break;
1137     case BRS_RECONSTRUCT_META_UP:
1138       GNUNET_assert (0);
1139       break;
1140     case BRS_RECONSTRUCT_UP:
1141       GNUNET_assert (0);
1142       break;
1143     case BRS_CHK_SET:
1144       GNUNET_assert (0);
1145       break;
1146     case BRS_DOWNLOAD_DOWN:
1147       GNUNET_assert (0);
1148       break;
1149     case BRS_DOWNLOAD_UP:
1150       GNUNET_assert (0);
1151       break;
1152     case BRS_ERROR:
1153       GNUNET_assert (0);
1154       break;
1155     default:
1156       GNUNET_assert (0);
1157       break;
1158     }
1159   }
1160   GNUNET_FS_download_sync_ (dc);
1161   return GNUNET_YES;
1162
1163 signal_error:
1164   if (fh != NULL)
1165     GNUNET_DISK_file_close (fh);
1166   pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1167   pi.value.download.specifics.error.message = dc->emsg;
1168   GNUNET_FS_download_make_status_ (&pi, dc);
1169   /* abort all pending requests */
1170   if (NULL != dc->th)
1171   {
1172     GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1173     dc->th = NULL;
1174   }
1175   GNUNET_CLIENT_disconnect (dc->client);
1176   dc->in_receive = GNUNET_NO;
1177   dc->client = NULL;
1178   GNUNET_FS_free_download_request_ (dc->top_request);
1179   dc->top_request = NULL;
1180   GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1181   dc->active = NULL;
1182   dc->pending_head = NULL;
1183   dc->pending_tail = NULL;
1184   GNUNET_FS_download_sync_ (dc);
1185   return GNUNET_NO;
1186 }
1187
1188
1189 /**
1190  * Process a download result.
1191  *
1192  * @param dc our download context
1193  * @param type type of the result
1194  * @param last_transmission when was this block requested the last time? (FOREVER if unknown/not applicable)
1195  * @param data the (encrypted) response
1196  * @param size size of data
1197  */
1198 static void
1199 process_result (struct GNUNET_FS_DownloadContext *dc,
1200                 enum GNUNET_BLOCK_Type type,
1201                 struct GNUNET_TIME_Absolute last_transmission,
1202                 const void *data, size_t size)
1203 {
1204   struct ProcessResultClosure prc;
1205
1206   prc.dc = dc;
1207   prc.data = data;
1208   prc.size = size;
1209   prc.type = type;
1210   prc.do_store = GNUNET_YES;
1211   prc.last_transmission = last_transmission;
1212   GNUNET_CRYPTO_hash (data, size, &prc.query);
1213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214               "Received result for query `%s' from `%s'-service\n",
1215               GNUNET_h2s (&prc.query), "FS");
1216   GNUNET_CONTAINER_multihashmap_get_multiple (dc->active, &prc.query,
1217                                               &process_result_with_request,
1218                                               &prc);
1219 }
1220
1221
1222 /**
1223  * Type of a function to call when we receive a message
1224  * from the service.
1225  *
1226  * @param cls closure
1227  * @param msg message received, NULL on timeout or fatal error
1228  */
1229 static void
1230 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
1231 {
1232   struct GNUNET_FS_DownloadContext *dc = cls;
1233   const struct ClientPutMessage *cm;
1234   uint16_t msize;
1235
1236   if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1237       (sizeof (struct ClientPutMessage) > ntohs (msg->size)))
1238   {
1239     GNUNET_break (msg == NULL);
1240     try_reconnect (dc);
1241     return;
1242   }
1243   msize = ntohs (msg->size);
1244   cm = (const struct ClientPutMessage *) msg;
1245   process_result (dc, ntohl (cm->type),
1246                   GNUNET_TIME_absolute_ntoh (cm->last_transmission), &cm[1],
1247                   msize - sizeof (struct ClientPutMessage));
1248   if (dc->client == NULL)
1249     return;                     /* fatal error */
1250   /* continue receiving */
1251   GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1252                          GNUNET_TIME_UNIT_FOREVER_REL);
1253 }
1254
1255
1256
1257 /**
1258  * We're ready to transmit a search request to the
1259  * file-sharing service.  Do it.  If there is
1260  * more than one request pending, try to send
1261  * multiple or request another transmission.
1262  *
1263  * @param cls closure
1264  * @param size number of bytes available in buf
1265  * @param buf where the callee should write the message
1266  * @return number of bytes written to buf
1267  */
1268 static size_t
1269 transmit_download_request (void *cls, size_t size, void *buf)
1270 {
1271   struct GNUNET_FS_DownloadContext *dc = cls;
1272   size_t msize;
1273   struct SearchMessage *sm;
1274   struct DownloadRequest *dr;
1275
1276   dc->th = NULL;
1277   if (NULL == buf)
1278   {
1279     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280                 "Transmitting download request failed, trying to reconnect\n");
1281     try_reconnect (dc);
1282     return 0;
1283   }
1284   GNUNET_assert (size >= sizeof (struct SearchMessage));
1285   msize = 0;
1286   sm = buf;
1287   while ((NULL != (dr = dc->pending_head)) &&
1288          (size >= msize + sizeof (struct SearchMessage)))
1289   {
1290     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291                 "Transmitting download request for `%s' to `%s'-service\n",
1292                 GNUNET_h2s (&dr->chk.query), "FS");
1293     memset (sm, 0, sizeof (struct SearchMessage));
1294     sm->header.size = htons (sizeof (struct SearchMessage));
1295     sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1296     if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1297       sm->options = htonl (GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY);
1298     else
1299       sm->options = htonl (GNUNET_FS_SEARCH_OPTION_NONE);
1300     if (dr->depth == 0)
1301       sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
1302     else
1303       sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
1304     sm->anonymity_level = htonl (dc->anonymity);
1305     sm->target = dc->target.hashPubKey;
1306     sm->query = dr->chk.query;
1307     GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1308     dr->is_pending = GNUNET_NO;
1309     msize += sizeof (struct SearchMessage);
1310     sm++;
1311   }
1312   if (dc->pending_head != NULL)
1313   {
1314     dc->th =
1315         GNUNET_CLIENT_notify_transmit_ready (dc->client,
1316                                              sizeof (struct SearchMessage),
1317                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1318                                              GNUNET_NO,
1319                                              &transmit_download_request, dc);
1320     GNUNET_assert (dc->th != NULL);
1321   }
1322   if (GNUNET_NO == dc->in_receive)
1323   {
1324     dc->in_receive = GNUNET_YES;
1325     GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1326                            GNUNET_TIME_UNIT_FOREVER_REL);
1327   }
1328   return msize;
1329 }
1330
1331
1332 /**
1333  * Reconnect to the FS service and transmit our queries NOW.
1334  *
1335  * @param cls our download context
1336  * @param tc unused
1337  */
1338 static void
1339 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1340 {
1341   struct GNUNET_FS_DownloadContext *dc = cls;
1342   struct GNUNET_CLIENT_Connection *client;
1343
1344   dc->task = GNUNET_SCHEDULER_NO_TASK;
1345   client = GNUNET_CLIENT_connect ("fs", dc->h->cfg);
1346   if (NULL == client)
1347   {
1348     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1349                 "Connecting to `%s'-service failed, will try again.\n", "FS");
1350     try_reconnect (dc);
1351     return;
1352   }
1353   dc->client = client;
1354   if (dc->pending_head != NULL)
1355   {
1356     dc->th =
1357         GNUNET_CLIENT_notify_transmit_ready (client,
1358                                              sizeof (struct SearchMessage),
1359                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1360                                              GNUNET_NO,
1361                                              &transmit_download_request, dc);
1362     GNUNET_assert (dc->th != NULL);
1363   }
1364 }
1365
1366
1367 /**
1368  * Add entries to the pending list.
1369  *
1370  * @param cls our download context
1371  * @param key unused
1372  * @param entry entry of type "struct DownloadRequest"
1373  * @return GNUNET_OK
1374  */
1375 static int
1376 retry_entry (void *cls, const GNUNET_HashCode * key, void *entry)
1377 {
1378   struct GNUNET_FS_DownloadContext *dc = cls;
1379   struct DownloadRequest *dr = entry;
1380
1381   dr->next = NULL;
1382   dr->prev = NULL;
1383   GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
1384   dr->is_pending = GNUNET_YES;
1385   return GNUNET_OK;
1386 }
1387
1388
1389 /**
1390  * We've lost our connection with the FS service.
1391  * Re-establish it and re-transmit all of our
1392  * pending requests.
1393  *
1394  * @param dc download context that is having trouble
1395  */
1396 static void
1397 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1398 {
1399
1400   if (NULL != dc->client)
1401   {
1402     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403                 "Moving all requests back to pending list\n");
1404     if (NULL != dc->th)
1405     {
1406       GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1407       dc->th = NULL;
1408     }
1409     /* full reset of the pending list */
1410     dc->pending_head = NULL;
1411     dc->pending_tail = NULL;
1412     GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1413     GNUNET_CLIENT_disconnect (dc->client);
1414     dc->in_receive = GNUNET_NO;
1415     dc->client = NULL;
1416   }
1417   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will try to reconnect in 1s\n");
1418   dc->task =
1419       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1420                                     dc);
1421 }
1422
1423
1424 /**
1425  * We're allowed to ask the FS service for our blocks.  Start the download.
1426  *
1427  * @param cls the 'struct GNUNET_FS_DownloadContext'
1428  * @param client handle to use for communcation with FS (we must destroy it!)
1429  */
1430 static void
1431 activate_fs_download (void *cls, struct GNUNET_CLIENT_Connection *client)
1432 {
1433   struct GNUNET_FS_DownloadContext *dc = cls;
1434   struct GNUNET_FS_ProgressInfo pi;
1435
1436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download activated\n");
1437   GNUNET_assert (NULL != client);
1438   GNUNET_assert (dc->client == NULL);
1439   GNUNET_assert (dc->th == NULL);
1440   dc->client = client;
1441   pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1442   GNUNET_FS_download_make_status_ (&pi, dc);
1443   dc->pending_head = NULL;
1444   dc->pending_tail = NULL;
1445   GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1447               "Asking for transmission to FS service\n");
1448   if (dc->pending_head != NULL)
1449   {
1450     dc->th =
1451         GNUNET_CLIENT_notify_transmit_ready (dc->client,
1452                                              sizeof (struct SearchMessage),
1453                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1454                                              GNUNET_NO,
1455                                              &transmit_download_request, dc);
1456     GNUNET_assert (dc->th != NULL);
1457   }
1458 }
1459
1460
1461 /**
1462  * We must stop to ask the FS service for our blocks.  Pause the download.
1463  *
1464  * @param cls the 'struct GNUNET_FS_DownloadContext'
1465  */
1466 static void
1467 deactivate_fs_download (void *cls)
1468 {
1469   struct GNUNET_FS_DownloadContext *dc = cls;
1470   struct GNUNET_FS_ProgressInfo pi;
1471
1472   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download deactivated\n");
1473   if (NULL != dc->th)
1474   {
1475     GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1476     dc->th = NULL;
1477   }
1478   if (NULL != dc->client)
1479   {
1480     GNUNET_CLIENT_disconnect (dc->client);
1481     dc->in_receive = GNUNET_NO;
1482     dc->client = NULL;
1483   }
1484   dc->pending_head = NULL;
1485   dc->pending_tail = NULL;
1486   pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1487   GNUNET_FS_download_make_status_ (&pi, dc);
1488 }
1489
1490
1491 /**
1492  * (recursively) Create a download request structure.
1493  *
1494  * @param parent parent of the current entry
1495  * @param chk_idx index of the chk for this block in the parent block
1496  * @param depth depth of the current entry, 0 are the DBLOCKs,
1497  *              top level block is 'dc->treedepth - 1'
1498  * @param dr_offset offset in the original file this block maps to
1499  *              (as in, offset of the first byte of the first DBLOCK
1500  *               in the subtree rooted in the returned download request tree)
1501  * @param file_start_offset desired starting offset for the download
1502  *             in the original file; requesting tree should not contain
1503  *             DBLOCKs prior to the file_start_offset
1504  * @param desired_length desired number of bytes the user wanted to access
1505  *        (from file_start_offset).  Resulting tree should not contain
1506  *        DBLOCKs after file_start_offset + file_length.
1507  * @return download request tree for the given range of DBLOCKs at
1508  *         the specified depth
1509  */
1510 static struct DownloadRequest *
1511 create_download_request (struct DownloadRequest *parent, 
1512                          unsigned int chk_idx,
1513                          unsigned int depth,
1514                          uint64_t dr_offset, uint64_t file_start_offset,
1515                          uint64_t desired_length)
1516 {
1517   struct DownloadRequest *dr;
1518   unsigned int i;
1519   unsigned int head_skip;
1520   uint64_t child_block_size;
1521
1522   dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1523   dr->parent = parent;
1524   dr->depth = depth;
1525   dr->offset = dr_offset;
1526   dr->chk_idx = chk_idx;
1527   if (0 == depth)
1528     return dr;
1529   child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1530   
1531   /* calculate how many blocks at this level are not interesting
1532    * from the start (rounded down), either because of the requested
1533    * file offset or because this IBlock is further along */
1534   if (dr_offset < file_start_offset)
1535     head_skip = file_start_offset / child_block_size;
1536   else
1537     head_skip = 0;
1538   
1539   /* calculate index of last block at this level that is interesting (rounded up) */
1540   dr->num_children = (file_start_offset + desired_length - dr_offset) / child_block_size;
1541   if (dr->num_children * child_block_size <
1542       file_start_offset + desired_length - dr_offset)
1543     dr->num_children++;       /* round up */
1544   dr->num_children -= head_skip;
1545   if (dr->num_children > CHK_PER_INODE)
1546     dr->num_children = CHK_PER_INODE; /* cap at max */
1547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548               "Block at offset %llu and depth %u has %u children\n",
1549               (unsigned long long) dr_offset,
1550               depth,
1551               dr->num_children);
1552   
1553   /* now we can get the total number of *interesting* children for this block */
1554
1555   /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1556   GNUNET_assert (dr->num_children > 0);
1557   
1558   dr->children =
1559     GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1560   for (i = 0; i < dr->num_children; i++)
1561     dr->children[i] =
1562       create_download_request (dr, i + head_skip, depth - 1,
1563                                dr_offset + (i + head_skip) * child_block_size,
1564                                file_start_offset, desired_length);
1565   return dr;
1566 }
1567
1568
1569 /**
1570  * Continuation after a possible attempt to reconstruct
1571  * the current IBlock from the existing file.
1572  *
1573  * @param cls the 'struct ReconstructContext'
1574  * @param tc scheduler context
1575  */
1576 static void
1577 reconstruct_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1578 {
1579   struct GNUNET_FS_DownloadContext *dc = cls;
1580
1581   /* clean up state from tree encoder */  
1582   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1583   {
1584     GNUNET_SCHEDULER_cancel (dc->task);
1585     dc->task = GNUNET_SCHEDULER_NO_TASK;
1586   }
1587   if (dc->rfh != NULL)
1588   {
1589     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1590     dc->rfh = NULL;
1591   }
1592   /* start "normal" download */
1593   dc->issue_requests = GNUNET_YES;
1594   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595               "Starting normal download\n");
1596   schedule_block_download (dc, dc->top_request);
1597 }
1598
1599
1600 /**
1601  * Task requesting the next block from the tree encoder.
1602  *
1603  * @param cls the 'struct GNUJNET_FS_DownloadContext' we're processing
1604  * @param tc task context
1605  */
1606 static void
1607 get_next_block (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1608 {
1609   struct GNUNET_FS_DownloadContext *dc = cls;
1610
1611   dc->task = GNUNET_SCHEDULER_NO_TASK;
1612   GNUNET_FS_tree_encoder_next (dc->te);
1613 }
1614
1615
1616
1617 /**
1618  * Function called asking for the current (encoded)
1619  * block to be processed.  After processing the
1620  * client should either call "GNUNET_FS_tree_encode_next"
1621  * or (on error) "GNUNET_FS_tree_encode_finish".
1622  *
1623  * This function checks if the content on disk matches
1624  * the expected content based on the URI.
1625  *
1626  * @param cls closure
1627  * @param chk content hash key for the block
1628  * @param offset offset of the block
1629  * @param depth depth of the block, 0 for DBLOCK
1630  * @param type type of the block (IBLOCK or DBLOCK)
1631  * @param block the (encrypted) block
1632  * @param block_size size of block (in bytes)
1633  */
1634 static void
1635 reconstruct_cb (void *cls, const struct ContentHashKey *chk, uint64_t offset,
1636                 unsigned int depth, enum GNUNET_BLOCK_Type type,
1637                 const void *block, uint16_t block_size)
1638 {
1639   struct GNUNET_FS_DownloadContext *dc = cls;
1640   struct GNUNET_FS_ProgressInfo pi;
1641   struct DownloadRequest *dr;
1642   uint64_t blen;
1643   unsigned int chld;
1644
1645   /* find corresponding request entry */
1646   dr = dc->top_request;
1647   while (dr->depth > depth)
1648   {
1649     GNUNET_assert (dr->num_children > 0);
1650     blen = GNUNET_FS_tree_compute_tree_size (dr->depth - 1);
1651     chld = (offset - dr->offset) / blen;
1652     if (chld < dr->children[0]->chk_idx)
1653     {
1654       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655                   "Block %u < %u irrelevant for our range\n",
1656                   chld,
1657                   dr->children[0]->chk_idx);
1658       dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1659       return; /* irrelevant block */
1660     }
1661     if (chld > dr->children[dr->num_children-1]->chk_idx)
1662     {
1663       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664                   "Block %u > %u irrelevant for our range\n",
1665                   chld,
1666                   dr->children[dr->num_children-1]->chk_idx);
1667       dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1668       return; /* irrelevant block */
1669     }
1670     dr = dr->children[chld - dr->children[0]->chk_idx];
1671   }
1672   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673               "Matched TE block with request at offset %llu and depth %u in state %d\n",
1674               (unsigned long long) dr->offset,
1675               dr->depth,
1676               dr->state);
1677   /* FIXME: this code needs more testing and might
1678      need to handle more states... */
1679   switch (dr->state)
1680   {
1681   case BRS_INIT:
1682     break;
1683   case BRS_RECONSTRUCT_DOWN:
1684     break;
1685   case BRS_RECONSTRUCT_META_UP:
1686     break;
1687   case BRS_RECONSTRUCT_UP:
1688     break;
1689   case BRS_CHK_SET:
1690     if (0 == memcmp (chk, &dr->chk, sizeof (struct ContentHashKey)))
1691     {
1692       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1693                   "Reconstruction succeeded, can use block at offset %llu, depth %u\n",
1694                   (unsigned long long) offset,
1695                   depth);
1696       /* block matches, hence tree below matches;
1697        * this request is done! */
1698       dr->state = BRS_DOWNLOAD_UP;
1699       (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &dr->chk.query, dr);
1700       if (GNUNET_YES == dr->is_pending)
1701       {
1702         GNUNET_break (0); /* how did we get here? */
1703         GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1704         dr->is_pending = GNUNET_NO;
1705       }
1706       /* calculate how many bytes of payload this block
1707        * corresponds to */
1708       blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1709       /* how many of those bytes are in the requested range? */
1710       blen = GNUNET_MIN (blen, dc->length + dc->offset - dr->offset);
1711       /* signal progress */
1712       dc->completed += blen;
1713       pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1714       pi.value.download.specifics.progress.data = NULL;
1715       pi.value.download.specifics.progress.offset = offset;
1716       pi.value.download.specifics.progress.data_len = 0;
1717       pi.value.download.specifics.progress.depth = 0;
1718       pi.value.download.specifics.progress.trust_offered = 0;
1719       pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
1720       GNUNET_FS_download_make_status_ (&pi, dc);
1721       /* FIXME: duplicated code from 'process_result_with_request - refactor */
1722       if (dc->completed == dc->length)
1723       {
1724         /* download completed, signal */
1725         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726                     "Download completed, truncating file to desired length %llu\n",
1727                     (unsigned long long) GNUNET_ntohll (dc->uri->data.
1728                                                         chk.file_length));
1729         /* truncate file to size (since we store IBlocks at the end) */
1730         if (dc->filename != NULL)
1731         {
1732           if (0 !=
1733               truncate (dc->filename,
1734                         GNUNET_ntohll (dc->uri->data.chk.file_length)))
1735             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1736                                       dc->filename);
1737         }
1738       }
1739     }
1740     else
1741       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1742                   "Reconstruction failed, need to download block at offset %llu, depth %u\n",
1743                   (unsigned long long) offset,
1744                   depth);
1745     break;
1746   case BRS_DOWNLOAD_DOWN:
1747     break;
1748   case BRS_DOWNLOAD_UP:
1749     break;
1750   case BRS_ERROR:
1751     break;
1752   default:
1753     GNUNET_assert (0);
1754     break;
1755   }
1756   dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1757   if ((dr == dc->top_request) && (dr->state == BRS_DOWNLOAD_UP))
1758     check_completed (dc);
1759 }
1760
1761
1762 /**
1763  * Function called by the tree encoder to obtain a block of plaintext
1764  * data (for the lowest level of the tree).
1765  *
1766  * @param cls our 'struct ReconstructContext'
1767  * @param offset identifies which block to get
1768  * @param max (maximum) number of bytes to get; returning
1769  *        fewer will also cause errors
1770  * @param buf where to copy the plaintext buffer
1771  * @param emsg location to store an error message (on error)
1772  * @return number of bytes copied to buf, 0 on error
1773  */
1774 static size_t
1775 fh_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
1776 {
1777   struct GNUNET_FS_DownloadContext *dc = cls;
1778   struct GNUNET_DISK_FileHandle *fh = dc->rfh;
1779   ssize_t ret;
1780
1781   if (NULL != emsg)
1782     *emsg = NULL;
1783   if (offset != GNUNET_DISK_file_seek (fh, offset, GNUNET_DISK_SEEK_SET))
1784   {
1785     if (NULL != emsg)
1786       *emsg = GNUNET_strdup (strerror (errno));
1787     return 0;
1788   }
1789   ret = GNUNET_DISK_file_read (fh, buf, max);
1790   if (ret < 0)
1791   {
1792     if (NULL != emsg)
1793       *emsg = GNUNET_strdup (strerror (errno));
1794     return 0;
1795   }
1796   return ret;
1797 }
1798
1799
1800 /**
1801  * Task that creates the initial (top-level) download
1802  * request for the file.
1803  *
1804  * @param cls the 'struct GNUNET_FS_DownloadContext'
1805  * @param tc scheduler context
1806  */
1807 void
1808 GNUNET_FS_download_start_task_ (void *cls,
1809                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
1810 {
1811   struct GNUNET_FS_DownloadContext *dc = cls;
1812   struct GNUNET_FS_ProgressInfo pi;
1813   struct GNUNET_DISK_FileHandle *fh;
1814
1815   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start task running...\n");
1816   dc->task = GNUNET_SCHEDULER_NO_TASK;
1817   if (dc->length == 0)
1818   {
1819     /* no bytes required! */
1820     if (dc->filename != NULL)
1821     {
1822       fh = GNUNET_DISK_file_open (dc->filename,
1823                                   GNUNET_DISK_OPEN_READWRITE |
1824                                   GNUNET_DISK_OPEN_CREATE |
1825                                   ((0 ==
1826                                     GNUNET_FS_uri_chk_get_file_size (dc->uri)) ?
1827                                    GNUNET_DISK_OPEN_TRUNCATE : 0),
1828                                   GNUNET_DISK_PERM_USER_READ |
1829                                   GNUNET_DISK_PERM_USER_WRITE |
1830                                   GNUNET_DISK_PERM_GROUP_READ |
1831                                   GNUNET_DISK_PERM_OTHER_READ);
1832       GNUNET_DISK_file_close (fh);
1833     }
1834     GNUNET_FS_download_sync_ (dc);
1835     pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1836     pi.value.download.specifics.start.meta = dc->meta;
1837     GNUNET_FS_download_make_status_ (&pi, dc);
1838     check_completed (dc);
1839     return;
1840   }
1841   if (dc->emsg != NULL)
1842     return;
1843   if (dc->top_request == NULL)
1844   {
1845     dc->top_request =
1846       create_download_request (NULL, 0, dc->treedepth - 1, 0, dc->offset,
1847                                  dc->length);
1848     dc->top_request->state = BRS_CHK_SET;
1849     dc->top_request->chk =
1850         (dc->uri->type ==
1851          chk) ? dc->uri->data.chk.chk : dc->uri->data.loc.fi.chk;
1852     /* signal start */
1853     GNUNET_FS_download_sync_ (dc);
1854     if (NULL != dc->search)
1855       GNUNET_FS_search_result_sync_ (dc->search);
1856     pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1857     pi.value.download.specifics.start.meta = dc->meta;
1858     GNUNET_FS_download_make_status_ (&pi, dc);
1859   }
1860   GNUNET_FS_download_start_downloading_ (dc);
1861   /* attempt reconstruction from disk */
1862   if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
1863     dc->rfh =
1864         GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
1865                                GNUNET_DISK_PERM_NONE);
1866   if (dc->top_request->state == BRS_CHK_SET)
1867   {
1868     if (dc->rfh != NULL)
1869     {
1870       /* first, try top-down */
1871       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872                   "Trying top-down reconstruction for `%s'\n", dc->filename);
1873       try_top_down_reconstruction (dc, dc->top_request);
1874       switch (dc->top_request->state)
1875       {
1876       case BRS_CHK_SET:
1877         break;                  /* normal */
1878       case BRS_DOWNLOAD_DOWN:
1879         break;                  /* normal, some blocks already down */
1880       case BRS_DOWNLOAD_UP:
1881         /* already done entirely, party! */
1882         if (dc->rfh != NULL)
1883         {
1884           /* avoid hanging on to file handle longer than
1885            * necessary */
1886           GNUNET_DISK_file_close (dc->rfh);
1887           dc->rfh = NULL;
1888         }
1889         return;
1890       case BRS_ERROR:
1891         GNUNET_asprintf (&dc->emsg, _("Invalid URI"));
1892         GNUNET_FS_download_sync_ (dc);
1893         pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1894         pi.value.download.specifics.error.message = dc->emsg;
1895         GNUNET_FS_download_make_status_ (&pi, dc);
1896         return;
1897       default:
1898         GNUNET_assert (0);
1899         break;
1900       }
1901     }
1902   }
1903   /* attempt reconstruction from meta data */
1904   if ((GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
1905       (NULL != dc->meta))
1906   {
1907     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908                 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
1909                 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
1910                 (unsigned int)
1911                 GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
1912     GNUNET_CONTAINER_meta_data_iterate (dc->meta, &match_full_data, dc);
1913     if (dc->top_request->state == BRS_DOWNLOAD_UP)
1914     {
1915       if (dc->rfh != NULL)
1916       {
1917         /* avoid hanging on to file handle longer than
1918          * necessary */
1919         GNUNET_DISK_file_close (dc->rfh);
1920         dc->rfh = NULL;
1921       }
1922       return;                   /* finished, status update was already done for us */
1923     }
1924   }
1925   if (dc->rfh != NULL)
1926   {
1927     /* finally, actually run bottom-up */
1928     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929                 "Trying bottom-up reconstruction of file `%s'\n", dc->filename);
1930     dc->te =
1931       GNUNET_FS_tree_encoder_create (dc->h, 
1932                                      GNUNET_FS_uri_chk_get_file_size (dc->uri),
1933                                      dc, &fh_reader,
1934                                      &reconstruct_cb, NULL,
1935                                      &reconstruct_cont);
1936     dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1937   }
1938   else
1939   {
1940     /* simple, top-level download */
1941     dc->issue_requests = GNUNET_YES;
1942     schedule_block_download (dc, dc->top_request);
1943   }
1944   if (dc->top_request->state == BRS_DOWNLOAD_UP)
1945     check_completed (dc);
1946 }
1947
1948
1949 /**
1950  * Create SUSPEND event for the given download operation
1951  * and then clean up our state (without stop signal).
1952  *
1953  * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1954  */
1955 void
1956 GNUNET_FS_download_signal_suspend_ (void *cls)
1957 {
1958   struct GNUNET_FS_DownloadContext *dc = cls;
1959   struct GNUNET_FS_ProgressInfo pi;
1960
1961   if (dc->top != NULL)
1962     GNUNET_FS_end_top (dc->h, dc->top);
1963   while (NULL != dc->child_head)
1964     GNUNET_FS_download_signal_suspend_ (dc->child_head);
1965   if (dc->search != NULL)
1966   {
1967     dc->search->download = NULL;
1968     dc->search = NULL;
1969   }
1970   if (dc->job_queue != NULL)
1971   {
1972     GNUNET_FS_dequeue_ (dc->job_queue);
1973     dc->job_queue = NULL;
1974   }
1975   if (dc->parent != NULL)
1976     GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
1977                                  dc);
1978   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1979   {
1980     GNUNET_SCHEDULER_cancel (dc->task);
1981     dc->task = GNUNET_SCHEDULER_NO_TASK;
1982   }
1983   pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1984   GNUNET_FS_download_make_status_ (&pi, dc);
1985   if (dc->te != NULL)
1986   {
1987     GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1988     dc->te = NULL;
1989   }
1990   if (dc->rfh != NULL)
1991   {
1992     GNUNET_DISK_file_close (dc->rfh);
1993     dc->rfh = NULL;
1994   }
1995   GNUNET_FS_free_download_request_ (dc->top_request);
1996   if (dc->active != NULL)
1997   {
1998     GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1999     dc->active = NULL;
2000   }
2001   GNUNET_free_non_null (dc->filename);
2002   GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2003   GNUNET_FS_uri_destroy (dc->uri);
2004   GNUNET_free_non_null (dc->temp_filename);
2005   GNUNET_free_non_null (dc->serialization);
2006   GNUNET_free (dc);
2007 }
2008
2009
2010 /**
2011  * Helper function to setup the download context.
2012  *
2013  * @param h handle to the file sharing subsystem
2014  * @param uri the URI of the file (determines what to download); CHK or LOC URI
2015  * @param meta known metadata for the file (can be NULL)
2016  * @param filename where to store the file, maybe NULL (then no file is
2017  *        created on disk and data must be grabbed from the callbacks)
2018  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2019  *        can be NULL (in which case we will pick a name if needed); the temporary file
2020  *        may already exist, in which case we will try to use the data that is there and
2021  *        if it is not what is desired, will overwrite it
2022  * @param offset at what offset should we start the download (typically 0)
2023  * @param length how many bytes should be downloaded starting at offset
2024  * @param anonymity anonymity level to use for the download
2025  * @param options various options
2026  * @param cctx initial value for the client context for this download
2027  * @return context that can be used to control this download
2028  */
2029 struct GNUNET_FS_DownloadContext *
2030 create_download_context (struct GNUNET_FS_Handle *h,
2031                          const struct GNUNET_FS_Uri *uri,
2032                          const struct GNUNET_CONTAINER_MetaData *meta,
2033                          const char *filename, const char *tempname,
2034                          uint64_t offset, uint64_t length, uint32_t anonymity,
2035                          enum GNUNET_FS_DownloadOptions options, void *cctx)
2036 {
2037   struct GNUNET_FS_DownloadContext *dc;
2038
2039   GNUNET_assert (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri));
2040   if ((offset + length < offset) ||
2041       (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)))
2042   {
2043     GNUNET_break (0);
2044     return NULL;
2045   }
2046   dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2047   dc->h = h;
2048   dc->uri = GNUNET_FS_uri_dup (uri);
2049   dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
2050   dc->client_info = cctx;
2051   dc->start_time = GNUNET_TIME_absolute_get ();
2052   if (NULL != filename)
2053   {
2054     dc->filename = GNUNET_strdup (filename);
2055     if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2056       GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, GNUNET_YES));
2057   }
2058   if (GNUNET_FS_uri_test_loc (dc->uri))
2059     GNUNET_assert (GNUNET_OK ==
2060                    GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2061   dc->offset = offset;
2062   dc->length = length;
2063   dc->anonymity = anonymity;
2064   dc->options = options;
2065   dc->active =
2066       GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2067   dc->treedepth =
2068       GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2069   if ((NULL == filename) && (is_recursive_download (dc)))
2070   {
2071     if (NULL != tempname)
2072       dc->temp_filename = GNUNET_strdup (tempname);
2073     else
2074       dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2075   }
2076   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
2077               "Starting download `%s' of %llu bytes with tree depth %u\n",
2078               filename,
2079               (unsigned long long) length,
2080               dc->treedepth);
2081   dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2082   return dc;
2083 }
2084
2085
2086 /**
2087  * Download parts of a file.  Note that this will store
2088  * the blocks at the respective offset in the given file.  Also, the
2089  * download is still using the blocking of the underlying FS
2090  * encoding.  As a result, the download may *write* outside of the
2091  * given boundaries (if offset and length do not match the 32k FS
2092  * block boundaries). <p>
2093  *
2094  * This function should be used to focus a download towards a
2095  * particular portion of the file (optimization), not to strictly
2096  * limit the download to exactly those bytes.
2097  *
2098  * @param h handle to the file sharing subsystem
2099  * @param uri the URI of the file (determines what to download); CHK or LOC URI
2100  * @param meta known metadata for the file (can be NULL)
2101  * @param filename where to store the file, maybe NULL (then no file is
2102  *        created on disk and data must be grabbed from the callbacks)
2103  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2104  *        can be NULL (in which case we will pick a name if needed); the temporary file
2105  *        may already exist, in which case we will try to use the data that is there and
2106  *        if it is not what is desired, will overwrite it
2107  * @param offset at what offset should we start the download (typically 0)
2108  * @param length how many bytes should be downloaded starting at offset
2109  * @param anonymity anonymity level to use for the download
2110  * @param options various options
2111  * @param cctx initial value for the client context for this download
2112  * @param parent parent download to associate this download with (use NULL
2113  *        for top-level downloads; useful for manually-triggered recursive downloads)
2114  * @return context that can be used to control this download
2115  */
2116 struct GNUNET_FS_DownloadContext *
2117 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2118                           const struct GNUNET_FS_Uri *uri,
2119                           const struct GNUNET_CONTAINER_MetaData *meta,
2120                           const char *filename, const char *tempname,
2121                           uint64_t offset, uint64_t length, uint32_t anonymity,
2122                           enum GNUNET_FS_DownloadOptions options, void *cctx,
2123                           struct GNUNET_FS_DownloadContext *parent)
2124 {
2125   struct GNUNET_FS_DownloadContext *dc;
2126
2127   dc = create_download_context (h, uri, meta, filename, tempname,
2128                                 offset, length, anonymity, options, cctx);
2129   if (NULL == dc)
2130     return NULL;
2131   dc->parent = parent;
2132   if (NULL != parent)
2133     GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2134   else
2135     dc->top =
2136         GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2137   return dc;
2138 }
2139
2140
2141 /**
2142  * Download parts of a file based on a search result.  The download
2143  * will be associated with the search result (and the association
2144  * will be preserved when serializing/deserializing the state).
2145  * If the search is stopped, the download will not be aborted but
2146  * be 'promoted' to a stand-alone download.
2147  *
2148  * As with the other download function, this will store
2149  * the blocks at the respective offset in the given file.  Also, the
2150  * download is still using the blocking of the underlying FS
2151  * encoding.  As a result, the download may *write* outside of the
2152  * given boundaries (if offset and length do not match the 32k FS
2153  * block boundaries). <p>
2154  *
2155  * The given range can be used to focus a download towards a
2156  * particular portion of the file (optimization), not to strictly
2157  * limit the download to exactly those bytes.
2158  *
2159  * @param h handle to the file sharing subsystem
2160  * @param sr the search result to use for the download (determines uri and
2161  *        meta data and associations)
2162  * @param filename where to store the file, maybe NULL (then no file is
2163  *        created on disk and data must be grabbed from the callbacks)
2164  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2165  *        can be NULL (in which case we will pick a name if needed); the temporary file
2166  *        may already exist, in which case we will try to use the data that is there and
2167  *        if it is not what is desired, will overwrite it
2168  * @param offset at what offset should we start the download (typically 0)
2169  * @param length how many bytes should be downloaded starting at offset
2170  * @param anonymity anonymity level to use for the download
2171  * @param options various download options
2172  * @param cctx initial value for the client context for this download
2173  * @return context that can be used to control this download
2174  */
2175 struct GNUNET_FS_DownloadContext *
2176 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2177                                       struct GNUNET_FS_SearchResult *sr,
2178                                       const char *filename,
2179                                       const char *tempname, uint64_t offset,
2180                                       uint64_t length, uint32_t anonymity,
2181                                       enum GNUNET_FS_DownloadOptions options,
2182                                       void *cctx)
2183 {
2184   struct GNUNET_FS_DownloadContext *dc;
2185
2186   if ((NULL == sr) || (NULL != sr->download))
2187   {
2188     GNUNET_break (0);
2189     return NULL;
2190   }
2191   dc = create_download_context (h, sr->uri, sr->meta, filename, tempname,
2192                                 offset, length, anonymity, options, cctx);
2193   if (NULL == dc)
2194     return NULL;
2195   dc->search = sr;
2196   sr->download = dc;
2197   if (NULL != sr->probe_ctx)
2198   {
2199     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2200     sr->probe_ctx = NULL;
2201   }
2202   return dc;
2203 }
2204
2205
2206 /**
2207  * Start the downloading process (by entering the queue).
2208  *
2209  * @param dc our download context
2210  */
2211 void
2212 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2213 {
2214   if (dc->completed == dc->length)
2215     return;
2216   GNUNET_assert (dc->job_queue == NULL);
2217   dc->job_queue =
2218       GNUNET_FS_queue_ (dc->h, &activate_fs_download, &deactivate_fs_download,
2219                         dc, (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE,
2220                         (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
2221                         ? GNUNET_FS_QUEUE_PRIORITY_NORMAL 
2222                         : GNUNET_FS_QUEUE_PRIORITY_PROBE);
2223 }
2224
2225
2226 /**
2227  * Stop a download (aborts if download is incomplete).
2228  *
2229  * @param dc handle for the download
2230  * @param do_delete delete files of incomplete downloads
2231  */
2232 void
2233 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc, int do_delete)
2234 {
2235   struct GNUNET_FS_ProgressInfo pi;
2236   int have_children;
2237   int search_was_null;
2238
2239   if (dc->top != NULL)
2240     GNUNET_FS_end_top (dc->h, dc->top);
2241   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2242   {
2243     GNUNET_SCHEDULER_cancel (dc->task);
2244     dc->task = GNUNET_SCHEDULER_NO_TASK;
2245   }
2246   search_was_null = (dc->search == NULL);
2247   if (dc->search != NULL)
2248   {
2249     dc->search->download = NULL;
2250     GNUNET_FS_search_result_sync_ (dc->search);
2251     dc->search = NULL;
2252   }
2253   if (dc->job_queue != NULL)
2254   {
2255     GNUNET_FS_dequeue_ (dc->job_queue);
2256     dc->job_queue = NULL;
2257   }
2258   if (dc->te != NULL)
2259   {
2260     GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2261     dc->te = NULL;
2262   }
2263   have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2264   while (NULL != dc->child_head)
2265     GNUNET_FS_download_stop (dc->child_head, do_delete);
2266   if (dc->parent != NULL)
2267     GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
2268                                  dc);
2269   if (dc->serialization != NULL)
2270     GNUNET_FS_remove_sync_file_ (dc->h,
2271                                  ((dc->parent != NULL) ||
2272                                   (! search_was_null)) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2273                                  GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2274                                  dc->serialization);
2275   if ((GNUNET_YES == have_children) && (dc->parent == NULL))
2276     GNUNET_FS_remove_sync_dir_ (dc->h,
2277                                 (! search_was_null) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2278                                 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2279                                 dc->serialization);
2280   pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2281   GNUNET_FS_download_make_status_ (&pi, dc);
2282   GNUNET_FS_free_download_request_ (dc->top_request);
2283   dc->top_request = NULL;
2284   if (dc->active != NULL)
2285   {
2286     GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2287     dc->active = NULL;
2288   }
2289   if (dc->filename != NULL)
2290   {
2291     if ((dc->completed != dc->length) && (GNUNET_YES == do_delete))
2292     {
2293       if (0 != UNLINK (dc->filename))
2294         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2295                                   dc->filename);
2296     }
2297     GNUNET_free (dc->filename);
2298   }
2299   GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2300   GNUNET_FS_uri_destroy (dc->uri);
2301   if (NULL != dc->temp_filename)
2302   {
2303     if (0 != UNLINK (dc->temp_filename))
2304       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
2305                                 dc->temp_filename);
2306     GNUNET_free (dc->temp_filename);
2307   }
2308   GNUNET_free_non_null (dc->serialization);
2309   GNUNET_free (dc);
2310 }
2311
2312 /* end of fs_download.c */