start fewer FS probes concurrently, run them all in the same task to avoid undue...
[oweals/gnunet.git] / src / fs / fs_api.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 /**
22  * @file fs/fs_api.c
23  * @brief main FS functions (master initialization, serialization, deserialization, shared code)
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
30 #include "fs_api.h"
31 #include "fs_tree.h"
32
33 /**
34  * How many block requests can we have outstanding in parallel at a time by default?
35  */
36 #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
37
38 /**
39  * How many downloads can we have outstanding in parallel at a time by default?
40  */
41 #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
42
43 /**
44  * Start the given job (send signal, remove from pending queue, update
45  * counters and state).
46  *
47  * @param qe job to start
48  */
49 static void
50 start_job (struct GNUNET_FS_QueueEntry *qe)
51 {
52   GNUNET_assert (NULL == qe->client);
53   qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
54   if (NULL == qe->client)
55   {
56     GNUNET_break (0);
57     return;
58   }
59   qe->start (qe->cls, qe->client);
60   qe->start_times++;
61   qe->h->active_blocks += qe->blocks;
62   qe->h->active_downloads++;
63   qe->start_time = GNUNET_TIME_absolute_get ();
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65               "Starting job %p (%u active)\n",
66               qe,
67               qe->h->active_downloads);
68   GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
69                                qe->h->pending_tail,
70                                qe);
71   GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
72                                      qe->h->running_tail,
73                                      qe->h->running_tail,
74                                      qe);
75 }
76
77
78 /**
79  * Stop the given job (send signal, remove from active queue, update
80  * counters and state).
81  *
82  * @param qe job to stop
83  */
84 static void
85 stop_job (struct GNUNET_FS_QueueEntry *qe)
86 {
87   qe->client = NULL;
88   qe->stop (qe->cls);
89   GNUNET_assert (0 < qe->h->active_downloads);
90   qe->h->active_downloads--;
91   qe->h->active_blocks -= qe->blocks;
92   qe->run_time =
93       GNUNET_TIME_relative_add (qe->run_time,
94                                 GNUNET_TIME_absolute_get_duration
95                                 (qe->start_time));
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97               "Stopping job %p (%u active)\n",
98               qe,
99               qe->h->active_downloads);
100   GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
101   GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
102                                      qe->h->pending_tail, qe);
103 }
104
105
106 /**
107  * Process the jobs in the job queue, possibly starting some
108  * and stopping others.
109  *
110  * @param cls the `struct GNUNET_FS_Handle *`
111  * @param tc scheduler context
112  */
113 static void
114 process_job_queue (void *cls,
115                    const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   struct GNUNET_FS_Handle *h = cls;
118   struct GNUNET_FS_QueueEntry *qe;
119   struct GNUNET_FS_QueueEntry *next;
120   struct GNUNET_TIME_Relative run_time;
121   struct GNUNET_TIME_Relative restart_at;
122   struct GNUNET_TIME_Relative rst;
123   struct GNUNET_TIME_Absolute end_time;
124   unsigned int num_downloads_waiting;
125   unsigned int num_downloads_active;
126   unsigned int num_downloads_expired;
127   unsigned int num_probes_active;
128   unsigned int num_probes_waiting;
129   unsigned int num_probes_expired;
130   int num_probes_change;
131   int num_downloads_change;
132   int block_limit_hit;
133
134   h->queue_job = GNUNET_SCHEDULER_NO_TASK;
135   /* restart_at will be set to the time when it makes sense to
136      re-evaluate the job queue (unless, of course, jobs complete
137      or are added, then we'll be triggered immediately */
138   restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
139   /* first, calculate some basic statistics on pending jobs */
140   num_probes_waiting = 0;
141   num_downloads_waiting = 0;
142   for (qe = h->pending_head; NULL != qe; qe = qe->next)
143   {
144     switch (qe->priority)
145     {
146     case GNUNET_FS_QUEUE_PRIORITY_PROBE:
147       num_probes_waiting++;
148       break;
149     case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
150       num_downloads_waiting++;
151       break;
152     default:
153       GNUNET_break (0);
154       break;
155     }
156   }
157   /* now, calculate some basic statistics on running jobs */
158   num_probes_active = 0;
159   num_probes_expired = 0;
160   num_downloads_active = 0;
161   num_downloads_expired = 0;
162   next = h->running_head;
163   while (NULL != (qe = next))
164   {
165     next = qe->next;
166     switch (qe->priority)
167     {
168     case GNUNET_FS_QUEUE_PRIORITY_PROBE:
169       run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
170       end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
171       rst = GNUNET_TIME_absolute_get_remaining (end_time);
172       if (0 == rst.rel_value_us)
173       {
174         num_probes_expired++;
175         stop_job (qe);
176       }
177       else
178       {
179         num_probes_active++;
180         restart_at = GNUNET_TIME_relative_min (rst, restart_at);
181       }
182       break;
183     case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
184       run_time =
185         GNUNET_TIME_relative_multiply (h->avg_block_latency,
186                                        qe->blocks * qe->start_times);
187       end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
188       rst = GNUNET_TIME_absolute_get_remaining (end_time);
189       if (0 == rst.rel_value_us)
190       {
191         num_downloads_expired++;
192         stop_job (qe);
193       }
194       else
195       {
196         num_downloads_active++;
197         restart_at = GNUNET_TIME_relative_min (rst, restart_at);
198       }
199       break;
200     default:
201       GNUNET_break (0);
202       break;
203     }
204   }
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206               "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
207               num_probes_active,
208               num_probes_expired,
209               num_probes_waiting,
210               num_downloads_active,
211               num_downloads_expired,
212               num_downloads_waiting);
213   /* calculate start/stop decisions */
214   if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
215   {
216     /* stop as many probes as there are downloads and probes */
217     num_probes_change = - GNUNET_MIN (num_probes_active,
218                                       num_downloads_waiting);
219     /* start as many downloads as there are free slots, including those
220        we just opened up */
221     num_downloads_change = h->max_parallel_downloads - h->active_downloads - num_probes_change;
222   }
223   else
224   {
225     /* start all downloads (we can) */
226     num_downloads_change = num_downloads_waiting;
227     /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
228     if (h->max_parallel_downloads / 2 >= (h->active_downloads + num_downloads_change))
229       num_probes_change = GNUNET_MIN (num_probes_waiting,
230                                       (1 + h->max_parallel_downloads / 4) - (h->active_downloads + num_downloads_change));
231     else
232       num_probes_change = 0;
233   }
234
235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236               "Changing %d probes and %d downloads\n",
237               num_probes_change,
238               num_downloads_change);
239   /* actually stop probes */
240   next = h->running_head;
241   while (NULL != (qe = next))
242   {
243     next = qe->next;
244     if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
245       continue;
246     if (num_probes_change < 0)
247     {
248       stop_job (qe);
249       num_probes_change++;
250       if (0 == num_probes_change)
251         break;
252     }
253   }
254   GNUNET_break (0 <= num_probes_change);
255
256   /* start some more tasks if we now have empty slots */
257   block_limit_hit = GNUNET_NO;
258   next = h->pending_head;
259   while ( (NULL != (qe = next)) &&
260           ( (num_probes_change > 0) ||
261             (num_downloads_change > 0) ) )
262   {
263     next = qe->next;
264     switch (qe->priority)
265     {
266     case GNUNET_FS_QUEUE_PRIORITY_PROBE:
267       if (num_probes_change > 0)
268       {
269         start_job (qe);
270         num_probes_change--;
271         run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
272         restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
273       }
274       break;
275     case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
276       if ( (num_downloads_change > 0) &&
277            ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
278              ( (qe->blocks > h->max_parallel_requests) &&
279                (0 == h->active_downloads) ) ) )
280       {
281         start_job (qe);
282         num_downloads_change--;
283       }
284       else if (num_downloads_change > 0)
285         block_limit_hit = GNUNET_YES;
286       break;
287     default:
288       GNUNET_break (0);
289       break;
290     }
291   }
292   GNUNET_break ( (0 == num_downloads_change) || (GNUNET_YES == block_limit_hit) );
293   GNUNET_break (0 == num_probes_change);
294
295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296               "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
297               h->active_downloads,
298               h->max_parallel_requests,
299               num_probes_change,
300               num_downloads_change,
301               GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
302
303   /* make sure we run again */
304   h->queue_job =
305       GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
306 }
307
308
309 /**
310  * Add a job to the queue.
311  *
312  * @param h handle to the overall FS state
313  * @param start function to call to begin the job
314  * @param stop function to call to pause the job, or on dequeue (if the job was running)
315  * @param cls closure for start and stop
316  * @param blocks number of blocks this jobs uses
317  * @param priority how important is this download
318  * @return queue handle
319  */
320 struct GNUNET_FS_QueueEntry *
321 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
322                   GNUNET_FS_QueueStart start,
323                   GNUNET_FS_QueueStop stop, void *cls,
324                   unsigned int blocks,
325                   enum GNUNET_FS_QueuePriority priority)
326 {
327   struct GNUNET_FS_QueueEntry *qe;
328
329   qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
330   qe->h = h;
331   qe->start = start;
332   qe->stop = stop;
333   qe->cls = cls;
334   qe->queue_time = GNUNET_TIME_absolute_get ();
335   qe->blocks = blocks;
336   qe->priority = priority;
337   GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
338                                      h->pending_tail, qe);
339   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
340     GNUNET_SCHEDULER_cancel (h->queue_job);
341   h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
342   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343               "Queueing job %p\n",
344               qe);
345   return qe;
346 }
347
348
349 /**
350  * Dequeue a job from the queue.
351  *
352  * @param qe handle for the job
353  */
354 void
355 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
356 {
357   struct GNUNET_FS_Handle *h;
358
359   h = qe->h;
360   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361               "Dequeueing job %p\n",
362               qe);
363   if (NULL != qe->client)
364     stop_job (qe);
365   GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
366   GNUNET_free (qe);
367   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
368     GNUNET_SCHEDULER_cancel (h->queue_job);
369   h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
370 }
371
372
373 /**
374  * Create a top-level activity entry.
375  *
376  * @param h global fs handle
377  * @param ssf suspend signal function to use
378  * @param ssf_cls closure for @a ssf
379  * @return fresh top-level activity handle
380  */
381 struct TopLevelActivity *
382 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
383                     SuspendSignalFunction ssf,
384                     void *ssf_cls)
385 {
386   struct TopLevelActivity *ret;
387
388   ret = GNUNET_new (struct TopLevelActivity);
389   ret->ssf = ssf;
390   ret->ssf_cls = ssf_cls;
391   GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
392   return ret;
393 }
394
395
396 /**
397  * Destroy a top-level activity entry.
398  *
399  * @param h global fs handle
400  * @param top top level activity entry
401  */
402 void
403 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
404                    struct TopLevelActivity *top)
405 {
406   GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
407   GNUNET_free (top);
408 }
409
410
411 /**
412  * Closure for #GNUNET_FS_data_reader_file_().
413  */
414 struct FileInfo
415 {
416   /**
417    * Name of the file to read.
418    */
419   char *filename;
420
421   /**
422    * File descriptor, NULL if it has not yet been opened.
423    */
424   struct GNUNET_DISK_FileHandle *fd;
425 };
426
427
428 /**
429  * Function that provides data by reading from a file.
430  *
431  * @param cls closure with the `struct FileInfo *`
432  * @param offset offset to read from; it is possible
433  *            that the caller might need to go backwards
434  *            a bit at times; set to `UINT64_MAX` to tell
435  *            the reader that we won't be reading for a while
436  *            (used to close the file descriptor but NOT fully
437  *             clean up the reader's state); in this case,
438  *            a value of '0' for @a max should be ignored
439  * @param max maximum number of bytes that should be
440  *            copied to @a buf; readers are not allowed
441  *            to provide less data unless there is an error;
442  *            a value of "0" will be used at the end to allow
443  *            the reader to clean up its internal state
444  * @param buf where the reader should write the data
445  * @param emsg location for the reader to store an error message
446  * @return number of bytes written, usually @a max, 0 on error
447  */
448 size_t
449 GNUNET_FS_data_reader_file_ (void *cls,
450                              uint64_t offset,
451                              size_t max,
452                              void *buf,
453                              char **emsg)
454 {
455   struct FileInfo *fi = cls;
456   ssize_t ret;
457
458   if (UINT64_MAX == offset)
459   {
460     if (NULL != fi->fd)
461     {
462       GNUNET_DISK_file_close (fi->fd);
463       fi->fd = NULL;
464     }
465     return 0;
466   }
467   if (0 == max)
468   {
469     if (NULL != fi->fd)
470       GNUNET_DISK_file_close (fi->fd);
471     GNUNET_free (fi->filename);
472     GNUNET_free (fi);
473     return 0;
474   }
475   if (NULL == fi->fd)
476   {
477     fi->fd =
478         GNUNET_DISK_file_open (fi->filename,
479                                GNUNET_DISK_OPEN_READ,
480                                GNUNET_DISK_PERM_NONE);
481     if (NULL == fi->fd)
482     {
483       GNUNET_asprintf (emsg,
484                        _("Could not open file `%s': %s"),
485                        fi->filename,
486                        STRERROR (errno));
487       return 0;
488     }
489   }
490   if ( (GNUNET_SYSERR ==
491         GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
492        (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
493   {
494     GNUNET_asprintf (emsg,
495                      _("Could not read file `%s': %s"),
496                      fi->filename,
497                      STRERROR (errno));
498     return 0;
499   }
500   if (ret != max)
501   {
502     GNUNET_asprintf (emsg,
503                      _("Short read reading from file `%s'!"),
504                      fi->filename);
505     return 0;
506   }
507   return max;
508 }
509
510
511 /**
512  * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
513  *
514  * @param filename file to read
515  * @return closure to use, NULL on error
516  */
517 void *
518 GNUNET_FS_make_file_reader_context_ (const char *filename)
519 {
520   struct FileInfo *fi;
521
522   fi = GNUNET_new (struct FileInfo);
523   fi->filename = GNUNET_STRINGS_filename_expand (filename);
524   if (NULL == fi->filename)
525   {
526     GNUNET_free (fi);
527     return NULL;
528   }
529   return fi;
530 }
531
532
533 /**
534  * Function that provides data by copying from a buffer.
535  *
536  * @param cls closure (points to the buffer)
537  * @param offset offset to read from; it is possible
538  *            that the caller might need to go backwards
539  *            a bit at times; set to `UINT64_MAX` to tell
540  *            the reader that we won't be reading for a while
541  *            (used to close the file descriptor but NOT fully
542  *             clean up the reader's state); in this case,
543  *            a value of '0' for @a max should be ignored
544  * @param max maximum number of bytes that should be
545  *            copied to @a buf; readers are not allowed
546  *            to provide less data unless there is an error;
547  *            a value of "0" will be used at the end to allow
548  *            the reader to clean up its internal state
549  * @param buf where the reader should write the data
550  * @param emsg location for the reader to store an error message
551  * @return number of bytes written, usually @a max, 0 on error
552  */
553 size_t
554 GNUNET_FS_data_reader_copy_ (void *cls,
555                              uint64_t offset,
556                              size_t max,
557                              void *buf,
558                              char **emsg)
559 {
560   char *data = cls;
561
562   if (UINT64_MAX == offset)
563     return 0;
564   if (0 == max)
565   {
566     GNUNET_free_non_null (data);
567     return 0;
568   }
569   memcpy (buf, &data[offset], max);
570   return max;
571 }
572
573
574 /**
575  * Return the full filename where we would store state information
576  * (for serialization/deserialization).
577  *
578  * @param h master context
579  * @param ext component of the path
580  * @param ent entity identifier (or emtpy string for the directory)
581  * @return NULL on error
582  */
583 static char *
584 get_serialization_file_name (struct GNUNET_FS_Handle *h,
585                              const char *ext,
586                              const char *ent)
587 {
588   char *basename;
589   char *ret;
590
591   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
592     return NULL;                /* persistence not requested */
593   if (GNUNET_OK !=
594       GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
595                                                &basename))
596     return NULL;
597   GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
598                    h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
599                    ent);
600   GNUNET_free (basename);
601   return ret;
602 }
603
604
605 /**
606  * Return the full filename where we would store state information
607  * (for serialization/deserialization) that is associated with a
608  * parent operation.
609  *
610  * @param h master context
611  * @param ext component of the path
612  * @param uni name of the parent operation
613  * @param ent entity identifier (or emtpy string for the directory)
614  * @return NULL on error
615  */
616 static char *
617 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
618                                     const char *ext,
619                                     const char *uni,
620                                     const char *ent)
621 {
622   char *basename;
623   char *ret;
624
625   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
626     return NULL;                /* persistence not requested */
627   if (GNUNET_OK !=
628       GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
629                                                &basename))
630     return NULL;
631   GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
632                    h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
633                    uni, DIR_SEPARATOR_STR, ent);
634   GNUNET_free (basename);
635   return ret;
636 }
637
638
639 /**
640  * Return a read handle for deserialization.
641  *
642  * @param h master context
643  * @param ext component of the path
644  * @param ent entity identifier (or emtpy string for the directory)
645  * @return NULL on error
646  */
647 static struct GNUNET_BIO_ReadHandle *
648 get_read_handle (struct GNUNET_FS_Handle *h,
649                  const char *ext,
650                  const char *ent)
651 {
652   char *fn;
653   struct GNUNET_BIO_ReadHandle *ret;
654
655   fn = get_serialization_file_name (h, ext, ent);
656   if (NULL == fn)
657     return NULL;
658   ret = GNUNET_BIO_read_open (fn);
659   GNUNET_free (fn);
660   return ret;
661 }
662
663
664 /**
665  * Return a write handle for serialization.
666  *
667  * @param h master context
668  * @param ext component of the path
669  * @param ent entity identifier (or emtpy string for the directory)
670  * @return NULL on error
671  */
672 static struct GNUNET_BIO_WriteHandle *
673 get_write_handle (struct GNUNET_FS_Handle *h,
674                   const char *ext,
675                   const char *ent)
676 {
677   char *fn;
678   struct GNUNET_BIO_WriteHandle *ret;
679
680   fn = get_serialization_file_name (h, ext, ent);
681   if (NULL == fn)
682     return NULL;
683   ret = GNUNET_BIO_write_open (fn);
684   GNUNET_break (NULL != ret);
685   GNUNET_free (fn);
686   return ret;
687 }
688
689
690 /**
691  * Return a write handle for serialization.
692  *
693  * @param h master context
694  * @param ext component of the path
695  * @param uni name of parent
696  * @param ent entity identifier (or emtpy string for the directory)
697  * @return NULL on error
698  */
699 static struct GNUNET_BIO_WriteHandle *
700 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
701                          const char *uni, const char *ent)
702 {
703   char *fn;
704   struct GNUNET_BIO_WriteHandle *ret;
705
706   fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
707   if (NULL == fn)
708     return NULL;
709   ret = GNUNET_BIO_write_open (fn);
710   GNUNET_free (fn);
711   return ret;
712 }
713
714
715 /**
716  * Remove serialization/deserialization file from disk.
717  *
718  * @param h master context
719  * @param ext component of the path
720  * @param ent entity identifier
721  */
722 void
723 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
724                              const char *ext,
725                              const char *ent)
726 {
727   char *filename;
728
729   if ((NULL == ent) || (0 == strlen (ent)))
730   {
731     GNUNET_break (0);
732     return;
733   }
734   filename = get_serialization_file_name (h, ext, ent);
735   if (NULL != filename)
736   {
737     if ( (0 != UNLINK (filename)) &&
738          (ENOENT != errno) )
739       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
740     GNUNET_free (filename);
741   }
742 }
743
744
745 /**
746  * Remove serialization/deserialization file from disk.
747  *
748  * @param h master context
749  * @param ext component of the path
750  * @param uni parent name
751  * @param ent entity identifier
752  */
753 static void
754 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
755                          const char *ext,
756                          const char *uni, const char *ent)
757 {
758   char *filename;
759
760   if ((NULL == ent) || (0 == strlen (ent)))
761   {
762     GNUNET_break (0);
763     return;
764   }
765   filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
766   if (NULL == filename)
767     return;
768   if (0 != UNLINK (filename))
769     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
770   GNUNET_free (filename);
771 }
772
773
774 /**
775  * Remove serialization/deserialization directory from disk.
776  *
777  * @param h master context
778  * @param ext component of the path
779  * @param uni unique name of parent
780  */
781 void
782 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
783                             const char *ext,
784                             const char *uni)
785 {
786   char *dn;
787
788   if (NULL == uni)
789     return;
790   dn = get_serialization_file_name_in_dir (h, ext, uni, "");
791   if (NULL == dn)
792     return;
793   if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
794       (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
795     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
796   GNUNET_free (dn);
797 }
798
799
800 /**
801  * Serialize a start-time.  Since we use start-times to
802  * calculate the duration of some operation, we actually
803  * do not serialize the absolute time but the (relative)
804  * duration since the start time.  When we then
805  * deserialize the start time, we take the current time and
806  * subtract that duration so that we get again an absolute
807  * time stamp that will result in correct performance
808  * calculations.
809  *
810  * @param wh handle for writing
811  * @param timestamp time to serialize
812  * @return #GNUNET_OK on success
813  */
814 static int
815 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
816                   struct GNUNET_TIME_Absolute timestamp)
817 {
818   struct GNUNET_TIME_Relative dur;
819
820   dur = GNUNET_TIME_absolute_get_duration (timestamp);
821   return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
822 }
823
824
825 /**
826  * Deserialize a start-time.  Since we use start-times to
827  * calculate the duration of some operation, we actually
828  * do not serialize the absolute time but the (relative)
829  * duration since the start time.  Thus, when we then
830  * deserialize the start time, we take the current time and
831  * subtract that duration so that we get again an absolute
832  * time stamp that will result in correct performance
833  * calculations.
834  *
835  * @param rh handle for reading
836  * @param timestamp where to write the deserialized timestamp
837  * @return #GNUNET_OK on success
838  */
839 static int
840 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
841                  struct GNUNET_TIME_Absolute *timestamp)
842 {
843   struct GNUNET_TIME_Relative dur;
844
845   if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
846     return GNUNET_SYSERR;
847   *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
848   return GNUNET_OK;
849 }
850
851
852 /**
853  * Using the given serialization filename, try to deserialize
854  * the file-information tree associated with it.
855  *
856  * @param h master context
857  * @param filename name of the file (without directory) with
858  *        the infromation
859  * @return NULL on error
860  */
861 static struct GNUNET_FS_FileInformation *
862 deserialize_file_information (struct GNUNET_FS_Handle *h,
863                               const char *filename);
864
865
866 /**
867  * Using the given serialization filename, try to deserialize
868  * the file-information tree associated with it.
869  *
870  * @param h master context
871  * @param fn name of the file (without directory) with
872  *        the infromation
873  * @param rh handle for reading
874  * @return NULL on error
875  */
876 static struct GNUNET_FS_FileInformation *
877 deserialize_fi_node (struct GNUNET_FS_Handle *h,
878                      const char *fn,
879                      struct GNUNET_BIO_ReadHandle *rh)
880 {
881   struct GNUNET_FS_FileInformation *ret;
882   struct GNUNET_FS_FileInformation *nxt;
883   char b;
884   char *ksks;
885   char *chks;
886   char *skss;
887   char *filename;
888   uint32_t dsize;
889
890   if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
891   {
892     GNUNET_break (0);
893     return NULL;
894   }
895   ret = GNUNET_new (struct GNUNET_FS_FileInformation);
896   ret->h = h;
897   ksks = NULL;
898   chks = NULL;
899   skss = NULL;
900   filename = NULL;
901   if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
902       (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
903       ( (NULL != ksks) &&
904         ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
905           (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
906       (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
907       ( (NULL != chks) &&
908         ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
909           (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
910       (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
911       ( (NULL != skss) &&
912         ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
913           (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
914       (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
915       (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
916       || (GNUNET_OK !=
917           GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
918       (GNUNET_OK !=
919        GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
920       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
921       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
922       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
923   {
924     GNUNET_break (0);
925     goto cleanup;
926   }
927   switch (b)
928   {
929   case 0:                      /* file-insert */
930     if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
931     {
932       GNUNET_break (0);
933       goto cleanup;
934     }
935     ret->is_directory = GNUNET_NO;
936     ret->data.file.do_index = GNUNET_NO;
937     ret->data.file.have_hash = GNUNET_NO;
938     ret->data.file.index_start_confirmed = GNUNET_NO;
939     if (GNUNET_NO == ret->is_published)
940     {
941       if (NULL == ret->filename)
942       {
943         ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
944         ret->data.file.reader_cls =
945             GNUNET_malloc_large (ret->data.file.file_size);
946         if (ret->data.file.reader_cls == NULL)
947           goto cleanup;
948         if (GNUNET_OK !=
949             GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
950                              ret->data.file.file_size))
951         {
952           GNUNET_break (0);
953           goto cleanup;
954         }
955       }
956       else
957       {
958         ret->data.file.reader = &GNUNET_FS_data_reader_file_;
959         ret->data.file.reader_cls =
960             GNUNET_FS_make_file_reader_context_ (ret->filename);
961       }
962     }
963     break;
964   case 1:                      /* file-index, no hash */
965     if (NULL == ret->filename)
966     {
967       GNUNET_break (0);
968       goto cleanup;
969     }
970     if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
971     {
972       GNUNET_break (0);
973       goto cleanup;
974     }
975     ret->is_directory = GNUNET_NO;
976     ret->data.file.do_index = GNUNET_YES;
977     ret->data.file.have_hash = GNUNET_NO;
978     ret->data.file.index_start_confirmed = GNUNET_NO;
979     ret->data.file.reader = &GNUNET_FS_data_reader_file_;
980     ret->data.file.reader_cls =
981         GNUNET_FS_make_file_reader_context_ (ret->filename);
982     break;
983   case 2:                      /* file-index-with-hash */
984     if (NULL == ret->filename)
985     {
986       GNUNET_break (0);
987       goto cleanup;
988     }
989     if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
990         (GNUNET_OK !=
991          GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
992                           sizeof (struct GNUNET_HashCode))))
993     {
994       GNUNET_break (0);
995       goto cleanup;
996     }
997     ret->is_directory = GNUNET_NO;
998     ret->data.file.do_index = GNUNET_YES;
999     ret->data.file.have_hash = GNUNET_YES;
1000     ret->data.file.index_start_confirmed = GNUNET_NO;
1001     ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1002     ret->data.file.reader_cls =
1003         GNUNET_FS_make_file_reader_context_ (ret->filename);
1004     break;
1005   case 3:                      /* file-index-with-hash-confirmed */
1006     if (NULL == ret->filename)
1007     {
1008       GNUNET_break (0);
1009       goto cleanup;
1010     }
1011     if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1012         (GNUNET_OK !=
1013          GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1014                           sizeof (struct GNUNET_HashCode))))
1015     {
1016       GNUNET_break (0);
1017       goto cleanup;
1018     }
1019     ret->is_directory = GNUNET_NO;
1020     ret->data.file.do_index = GNUNET_YES;
1021     ret->data.file.have_hash = GNUNET_YES;
1022     ret->data.file.index_start_confirmed = GNUNET_YES;
1023     ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1024     ret->data.file.reader_cls =
1025         GNUNET_FS_make_file_reader_context_ (ret->filename);
1026     break;
1027   case 4:                      /* directory */
1028     ret->is_directory = GNUNET_YES;
1029     if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1030         (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1031         (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1032         (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1033         (GNUNET_OK !=
1034          GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1035         (GNUNET_OK !=
1036          GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1037     {
1038       GNUNET_break (0);
1039       goto cleanup;
1040     }
1041     ret->data.dir.dir_size = (uint32_t) dsize;
1042     if (NULL != filename)
1043     {
1044       ret->data.dir.entries = deserialize_file_information (h, filename);
1045       GNUNET_free (filename);
1046       filename = NULL;
1047       nxt = ret->data.dir.entries;
1048       while (NULL != nxt)
1049       {
1050         nxt->dir = ret;
1051         nxt = nxt->next;
1052       }
1053     }
1054     break;
1055   default:
1056     GNUNET_break (0);
1057     goto cleanup;
1058   }
1059   ret->serialization = GNUNET_strdup (fn);
1060   if (GNUNET_OK !=
1061       GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1062   {
1063     GNUNET_break (0);
1064     goto cleanup;
1065   }
1066   if (NULL != filename)
1067   {
1068     ret->next = deserialize_file_information (h, filename);
1069     GNUNET_free (filename);
1070     filename = NULL;
1071   }
1072   GNUNET_free_non_null (ksks);
1073   GNUNET_free_non_null (skss);
1074   GNUNET_free_non_null (chks);
1075   return ret;
1076 cleanup:
1077   GNUNET_free_non_null (ksks);
1078   GNUNET_free_non_null (chks);
1079   GNUNET_free_non_null (skss);
1080   GNUNET_free_non_null (filename);
1081   GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1082   return NULL;
1083 }
1084
1085
1086 /**
1087  * Using the given serialization filename, try to deserialize
1088  * the file-information tree associated with it.
1089  *
1090  * @param h master context
1091  * @param filename name of the file (without directory) with
1092  *        the infromation
1093  * @return NULL on error
1094  */
1095 static struct GNUNET_FS_FileInformation *
1096 deserialize_file_information (struct GNUNET_FS_Handle *h,
1097                               const char *filename)
1098 {
1099   struct GNUNET_FS_FileInformation *ret;
1100   struct GNUNET_BIO_ReadHandle *rh;
1101   char *emsg;
1102   char *fn;
1103
1104   rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1105   if (NULL == rh)
1106     return NULL;
1107   ret = deserialize_fi_node (h, filename, rh);
1108   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1109   {
1110     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1111                 _("Failed to resume publishing information `%s': %s\n"),
1112                 filename, emsg);
1113     GNUNET_free (emsg);
1114   }
1115   if (NULL == ret)
1116   {
1117     fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1118     if (NULL != fn)
1119     {
1120       if (0 != UNLINK (fn))
1121         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1122       GNUNET_free (fn);
1123     }
1124   }
1125   return ret;
1126 }
1127
1128
1129 /**
1130  * Given a serialization name (full absolute path), return the
1131  * basename of the file (without the path), which must only
1132  * consist of the 6 random characters.
1133  *
1134  * @param fullname name to extract the basename from
1135  * @return copy of the basename, NULL on error
1136  */
1137 static char *
1138 get_serialization_short_name (const char *fullname)
1139 {
1140   const char *end;
1141   const char *nxt;
1142
1143   end = NULL;
1144   nxt = fullname;
1145   /* FIXME: we could do this faster since we know
1146    * the length of 'end'... */
1147   while ('\0' != *nxt)
1148   {
1149     if (DIR_SEPARATOR == *nxt)
1150       end = nxt + 1;
1151     nxt++;
1152   }
1153   if ((NULL == end) || (0 == strlen (end)))
1154   {
1155     GNUNET_break (0);
1156     return NULL;
1157   }
1158   GNUNET_break (6 == strlen (end));
1159   return GNUNET_strdup (end);
1160 }
1161
1162
1163 /**
1164  * Create a new random name for serialization.  Also checks if persistence
1165  * is enabled and returns NULL if not.
1166  *
1167  * @param h master context
1168  * @param ext component of the path
1169  * @return NULL on errror
1170  */
1171 static char *
1172 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1173                               const char *ext)
1174 {
1175   char *fn;
1176   char *dn;
1177   char *ret;
1178
1179   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1180     return NULL;                /* persistence not requested */
1181   dn = get_serialization_file_name (h, ext, "");
1182   if (NULL == dn)
1183     return NULL;
1184   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1185   {
1186     GNUNET_free (dn);
1187     return NULL;
1188   }
1189   fn = GNUNET_DISK_mktemp (dn);
1190   GNUNET_free (dn);
1191   if (NULL == fn)
1192     return NULL;                /* epic fail */
1193   ret = get_serialization_short_name (fn);
1194   GNUNET_free (fn);
1195   return ret;
1196 }
1197
1198
1199 /**
1200  * Create a new random name for serialization.  Also checks if persistence
1201  * is enabled and returns NULL if not.
1202  *
1203  * @param h master context
1204  * @param ext component of the path
1205  * @param uni name of parent
1206  * @return NULL on errror
1207  */
1208 static char *
1209 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1210                                      const char *ext,
1211                                      const char *uni)
1212 {
1213   char *fn;
1214   char *dn;
1215   char *ret;
1216
1217   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1218     return NULL;                /* persistence not requested */
1219   dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1220   if (NULL == dn)
1221     return NULL;
1222   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1223   {
1224     GNUNET_free (dn);
1225     return NULL;
1226   }
1227   fn = GNUNET_DISK_mktemp (dn);
1228   GNUNET_free (dn);
1229   if (NULL == fn)
1230     return NULL;                /* epic fail */
1231   ret = get_serialization_short_name (fn);
1232   GNUNET_free (fn);
1233   return ret;
1234 }
1235
1236
1237 /**
1238  * Copy all of the data from the reader to the write handle.
1239  *
1240  * @param wh write handle
1241  * @param fi file with reader
1242  * @return #GNUNET_OK on success
1243  */
1244 static int
1245 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1246                   struct GNUNET_FS_FileInformation *fi)
1247 {
1248   char buf[32 * 1024];
1249   uint64_t off;
1250   size_t ret;
1251   size_t left;
1252   char *emsg;
1253
1254   emsg = NULL;
1255   off = 0;
1256   while (off < fi->data.file.file_size)
1257   {
1258     left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1259     ret =
1260         fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1261     if (0 == ret)
1262     {
1263       GNUNET_free (emsg);
1264       return GNUNET_SYSERR;
1265     }
1266     if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1267       return GNUNET_SYSERR;
1268     off += ret;
1269   }
1270   return GNUNET_OK;
1271 }
1272
1273
1274 /**
1275  * Create a temporary file on disk to store the current
1276  * state of @a fi in.
1277  *
1278  * @param fi file information to sync with disk
1279  */
1280 void
1281 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1282 {
1283   char *fn;
1284   struct GNUNET_BIO_WriteHandle *wh;
1285   char b;
1286   char *ksks;
1287   char *chks;
1288   char *skss;
1289
1290   if (NULL == fi->serialization)
1291     fi->serialization =
1292         make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1293   if (NULL == fi->serialization)
1294     return;
1295   wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1296                          fi->serialization);
1297   if (NULL == wh)
1298   {
1299     GNUNET_free (fi->serialization);
1300     fi->serialization = NULL;
1301     return;
1302   }
1303   if (GNUNET_YES == fi->is_directory)
1304     b = 4;
1305   else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1306     b = 3;
1307   else if (GNUNET_YES == fi->data.file.have_hash)
1308     b = 2;
1309   else if (GNUNET_YES == fi->data.file.do_index)
1310     b = 1;
1311   else
1312     b = 0;
1313   if (NULL != fi->keywords)
1314     ksks = GNUNET_FS_uri_to_string (fi->keywords);
1315   else
1316     ksks = NULL;
1317   if (NULL != fi->chk_uri)
1318     chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1319   else
1320     chks = NULL;
1321   if (NULL != fi->sks_uri)
1322     skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1323   else
1324     skss = NULL;
1325   if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1326       (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1327       (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1328       (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1329       (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1330       (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1331       (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1332       (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1333       (GNUNET_OK !=
1334        GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1335       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1336       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1337       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1338   {
1339     GNUNET_break (0);
1340     goto cleanup;
1341   }
1342   GNUNET_free_non_null (chks);
1343   chks = NULL;
1344   GNUNET_free_non_null (ksks);
1345   ksks = NULL;
1346   GNUNET_free_non_null (skss);
1347   skss = NULL;
1348
1349   switch (b)
1350   {
1351   case 0:                      /* file-insert */
1352     if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1353     {
1354       GNUNET_break (0);
1355       goto cleanup;
1356     }
1357     if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1358       if (GNUNET_OK != copy_from_reader (wh, fi))
1359       {
1360         GNUNET_break (0);
1361         goto cleanup;
1362       }
1363     break;
1364   case 1:                      /* file-index, no hash */
1365     if (NULL == fi->filename)
1366     {
1367       GNUNET_break (0);
1368       goto cleanup;
1369     }
1370     if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1371     {
1372       GNUNET_break (0);
1373       goto cleanup;
1374     }
1375     break;
1376   case 2:                      /* file-index-with-hash */
1377   case 3:                      /* file-index-with-hash-confirmed */
1378     if (NULL == fi->filename)
1379     {
1380       GNUNET_break (0);
1381       goto cleanup;
1382     }
1383     if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1384         (GNUNET_OK !=
1385          GNUNET_BIO_write (wh, &fi->data.file.file_id,
1386                            sizeof (struct GNUNET_HashCode))))
1387     {
1388       GNUNET_break (0);
1389       goto cleanup;
1390     }
1391     break;
1392   case 4:                      /* directory */
1393     if ( (NULL != fi->data.dir.entries) &&
1394          (NULL == fi->data.dir.entries->serialization) )
1395       GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1396     if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1397         (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1398         (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1399         (GNUNET_OK !=
1400          GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1401                            (uint32_t) fi->data.dir.dir_size)) ||
1402         (GNUNET_OK !=
1403          GNUNET_BIO_write_string (wh,
1404                                   (fi->data.dir.entries ==
1405                                    NULL) ? NULL : fi->data.dir.
1406                                   entries->serialization)))
1407     {
1408       GNUNET_break (0);
1409       goto cleanup;
1410     }
1411     break;
1412   default:
1413     GNUNET_assert (0);
1414     goto cleanup;
1415   }
1416   if ( (NULL != fi->next) &&
1417        (NULL == fi->next->serialization) )
1418     GNUNET_FS_file_information_sync_ (fi->next);
1419   if (GNUNET_OK !=
1420       GNUNET_BIO_write_string (wh,
1421                                (fi->next !=
1422                                 NULL) ? fi->next->serialization : NULL))
1423   {
1424     GNUNET_break (0);
1425     goto cleanup;
1426   }
1427   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1428   {
1429     wh = NULL;
1430     GNUNET_break (0);
1431     goto cleanup;
1432   }
1433   return;                       /* done! */
1434 cleanup:
1435   if (NULL != wh)
1436     (void) GNUNET_BIO_write_close (wh);
1437   GNUNET_free_non_null (chks);
1438   GNUNET_free_non_null (ksks);
1439   GNUNET_free_non_null (skss);
1440   fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1441                                     fi->serialization);
1442   if (NULL != fn)
1443   {
1444     if (0 != UNLINK (fn))
1445       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1446     GNUNET_free (fn);
1447   }
1448   GNUNET_free (fi->serialization);
1449   fi->serialization = NULL;
1450 }
1451
1452
1453
1454 /**
1455  * Find the entry in the file information struct where the
1456  * serialization filename matches the given name.
1457  *
1458  * @param pos file information to search
1459  * @param srch filename to search for
1460  * @return NULL if srch was not found in this subtree
1461  */
1462 static struct GNUNET_FS_FileInformation *
1463 find_file_position (struct GNUNET_FS_FileInformation *pos,
1464                     const char *srch)
1465 {
1466   struct GNUNET_FS_FileInformation *r;
1467
1468   while (NULL != pos)
1469   {
1470     if (0 == strcmp (srch, pos->serialization))
1471       return pos;
1472     if ( (GNUNET_YES == pos->is_directory) &&
1473          (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1474       return r;
1475     pos = pos->next;
1476   }
1477   return NULL;
1478 }
1479
1480
1481 /**
1482  * Signal the FS's progress function that we are resuming
1483  * an upload.
1484  *
1485  * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1486  * @param fi the entry in the publish-structure
1487  * @param length length of the file or directory
1488  * @param meta metadata for the file or directory (can be modified)
1489  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1490  * @param bo block options (can be modified)
1491  * @param do_index should we index?
1492  * @param client_info pointer to client context set upon creation (can be modified)
1493  * @return #GNUNET_OK to continue (always)
1494  */
1495 static int
1496 fip_signal_resume (void *cls,
1497                    struct GNUNET_FS_FileInformation *fi,
1498                    uint64_t length,
1499                    struct GNUNET_CONTAINER_MetaData *meta,
1500                    struct GNUNET_FS_Uri **uri,
1501                    struct GNUNET_FS_BlockOptions *bo,
1502                    int *do_index,
1503                    void **client_info)
1504 {
1505   struct GNUNET_FS_PublishContext *pc = cls;
1506   struct GNUNET_FS_ProgressInfo pi;
1507
1508   if (GNUNET_YES == pc->skip_next_fi_callback)
1509   {
1510     pc->skip_next_fi_callback = GNUNET_NO;
1511     return GNUNET_OK;
1512   }
1513   pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1514   pi.value.publish.specifics.resume.message = fi->emsg;
1515   pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1516   *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1517   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1518   {
1519     /* process entries in directory */
1520     pc->skip_next_fi_callback = GNUNET_YES;
1521     GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1522   }
1523   return GNUNET_OK;
1524 }
1525
1526
1527 /**
1528  * Function called with a filename of serialized publishing operation
1529  * to deserialize.
1530  *
1531  * @param cls the `struct GNUNET_FS_Handle *`
1532  * @param filename complete filename (absolute path)
1533  * @return #GNUNET_OK (continue to iterate)
1534  */
1535 static int
1536 deserialize_publish_file (void *cls,
1537                           const char *filename)
1538 {
1539   struct GNUNET_FS_Handle *h = cls;
1540   struct GNUNET_BIO_ReadHandle *rh;
1541   struct GNUNET_FS_PublishContext *pc;
1542   int32_t options;
1543   int32_t all_done;
1544   int32_t have_ns;
1545   char *fi_root;
1546   struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1547   char *fi_pos;
1548   char *emsg;
1549
1550   pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1551   pc->h = h;
1552   pc->serialization = get_serialization_short_name (filename);
1553   fi_root = NULL;
1554   fi_pos = NULL;
1555   rh = GNUNET_BIO_read_open (filename);
1556   if (NULL == rh)
1557   {
1558     GNUNET_break (0);
1559     goto cleanup;
1560   }
1561   if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1562       || (GNUNET_OK !=
1563           GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1564       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1565       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1566       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1567       (GNUNET_OK !=
1568        GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1569       (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1570       || ( (GNUNET_YES == have_ns) &&
1571            (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1572   {
1573     GNUNET_break (0);
1574     goto cleanup;
1575   }
1576   pc->options = options;
1577   pc->all_done = all_done;
1578   if (NULL == fi_root)
1579   {
1580     GNUNET_break (0);
1581     goto cleanup;
1582   }
1583   pc->fi = deserialize_file_information (h, fi_root);
1584   if (NULL == pc->fi)
1585   {
1586     GNUNET_break (0);
1587     goto cleanup;
1588   }
1589   if (GNUNET_YES == have_ns)
1590   {
1591     pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1592     *pc->ns = ns;
1593   }
1594   if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1595       (GNUNET_YES != pc->all_done))
1596   {
1597     pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1598     if (NULL == pc->dsh)
1599       goto cleanup;
1600   }
1601   if (NULL != fi_pos)
1602   {
1603     pc->fi_pos = find_file_position (pc->fi, fi_pos);
1604     GNUNET_free (fi_pos);
1605     fi_pos = NULL;
1606     if (NULL == pc->fi_pos)
1607     {
1608       /* failed to find position for resuming, outch! Will start from root! */
1609       GNUNET_break (0);
1610       if (GNUNET_YES != pc->all_done)
1611         pc->fi_pos = pc->fi;
1612     }
1613   }
1614   GNUNET_free (fi_root);
1615   fi_root = NULL;
1616   /* generate RESUME event(s) */
1617   GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1618
1619   /* re-start publishing (if needed)... */
1620   if (GNUNET_YES != pc->all_done)
1621   {
1622     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1623     pc->upload_task =
1624         GNUNET_SCHEDULER_add_with_priority
1625         (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1626          &GNUNET_FS_publish_main_, pc);
1627   }
1628   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1629   {
1630     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1631                 _("Failure while resuming publishing operation `%s': %s\n"),
1632                 filename, emsg);
1633     GNUNET_free (emsg);
1634   }
1635   pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1636   return GNUNET_OK;
1637 cleanup:
1638   GNUNET_free_non_null (pc->nid);
1639   GNUNET_free_non_null (pc->nuid);
1640   GNUNET_free_non_null (fi_root);
1641   GNUNET_free_non_null (fi_pos);
1642   if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1643   {
1644     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1645                 _("Failed to resume publishing operation `%s': %s\n"), filename,
1646                 emsg);
1647     GNUNET_free (emsg);
1648   }
1649   if (NULL != pc->fi)
1650     GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1651   if (0 != UNLINK (filename))
1652     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1653   GNUNET_free (pc->serialization);
1654   GNUNET_free (pc);
1655   return GNUNET_OK;
1656 }
1657
1658
1659 /**
1660  * Synchronize this publishing struct with its mirror
1661  * on disk.  Note that all internal FS-operations that change
1662  * publishing structs should already call "sync" internally,
1663  * so this function is likely not useful for clients.
1664  *
1665  * @param pc the struct to sync
1666  */
1667 void
1668 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1669 {
1670   struct GNUNET_BIO_WriteHandle *wh;
1671   int32_t have_ns;
1672
1673   if (NULL == pc->serialization)
1674     pc->serialization =
1675         make_serialization_file_name (pc->h,
1676                                       GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1677   if (NULL == pc->serialization)
1678     return;
1679   if (NULL == pc->fi)
1680     return;
1681   if (NULL == pc->fi->serialization)
1682   {
1683     GNUNET_break (0);
1684     return;
1685   }
1686   wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1687                          pc->serialization);
1688   if (NULL == wh)
1689   {
1690     GNUNET_break (0);
1691     goto cleanup;
1692   }
1693   have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1694   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1695       (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1696       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1697       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1698       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1699       (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1700       (GNUNET_OK !=
1701        GNUNET_BIO_write_string (wh,
1702                                 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1703       ( (NULL != pc->ns) &&
1704         (GNUNET_OK != GNUNET_BIO_write (wh,
1705                                         pc->ns,
1706                                         sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1707   {
1708     GNUNET_break (0);
1709     goto cleanup;
1710   }
1711   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1712   {
1713     wh = NULL;
1714     GNUNET_break (0);
1715     goto cleanup;
1716   }
1717   return;
1718 cleanup:
1719   if (NULL != wh)
1720     (void) GNUNET_BIO_write_close (wh);
1721   GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1722                                pc->serialization);
1723   GNUNET_free (pc->serialization);
1724   pc->serialization = NULL;
1725 }
1726
1727
1728 /**
1729  * Synchronize this unindex struct with its mirror
1730  * on disk.  Note that all internal FS-operations that change
1731  * publishing structs should already call "sync" internally,
1732  * so this function is likely not useful for clients.
1733  *
1734  * @param uc the struct to sync
1735  */
1736 void
1737 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1738 {
1739   struct GNUNET_BIO_WriteHandle *wh;
1740   char *uris;
1741
1742   if (NULL == uc->serialization)
1743     uc->serialization =
1744         make_serialization_file_name (uc->h,
1745                                       GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1746   if (NULL == uc->serialization)
1747     return;
1748   wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1749                          uc->serialization);
1750   if (NULL == wh)
1751   {
1752     GNUNET_break (0);
1753     goto cleanup;
1754   }
1755   if (NULL != uc->ksk_uri)
1756     uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1757   else
1758     uris = NULL;
1759   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1760       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1761       (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1762       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1763       (GNUNET_OK !=
1764        GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1765       (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1766       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1767       ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1768        (GNUNET_OK !=
1769         GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1770       ((uc->state == UNINDEX_STATE_ERROR) &&
1771        (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1772   {
1773     GNUNET_break (0);
1774     goto cleanup;
1775   }
1776   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1777   {
1778     wh = NULL;
1779     GNUNET_break (0);
1780     goto cleanup;
1781   }
1782   return;
1783 cleanup:
1784   if (NULL != wh)
1785     (void) GNUNET_BIO_write_close (wh);
1786   GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1787                                uc->serialization);
1788   GNUNET_free (uc->serialization);
1789   uc->serialization = NULL;
1790 }
1791
1792
1793 /**
1794  * Serialize a download request.
1795  *
1796  * @param wh handle for writing the download request to disk
1797  * @param dr the the request to write to disk
1798  * @return #GNUNET_YES on success, #GNUNET_NO on error
1799  */
1800 static int
1801 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1802                         struct DownloadRequest *dr)
1803 {
1804   unsigned int i;
1805
1806   if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1807       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1808       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1809       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1810     return GNUNET_NO;
1811   if ((BRS_CHK_SET == dr->state) &&
1812       (GNUNET_OK !=
1813        GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1814     return GNUNET_NO;
1815   for (i = 0; i < dr->num_children; i++)
1816     if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1817       return GNUNET_NO;
1818   return GNUNET_YES;
1819 }
1820
1821
1822 /**
1823  * Read a download request tree.
1824  *
1825  * @param rh cadet to read from
1826  * @return value the download request read from disk, NULL on error
1827  */
1828 static struct DownloadRequest *
1829 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1830 {
1831   struct DownloadRequest *dr;
1832   unsigned int i;
1833
1834   dr = GNUNET_new (struct DownloadRequest);
1835   if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1836       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1837       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1838       (dr->num_children > CHK_PER_INODE) ||
1839       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1840       ( (0 == dr->depth) &&
1841         (dr->num_children > 0) ) ||
1842       ((dr->depth > 0) && (0 == dr->num_children)))
1843   {
1844     GNUNET_break (0);
1845     dr->num_children = 0;
1846     goto cleanup;
1847   }
1848   if (dr->num_children > 0)
1849     dr->children =
1850         GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1851   switch (dr->state)
1852   {
1853   case BRS_INIT:
1854   case BRS_RECONSTRUCT_DOWN:
1855   case BRS_RECONSTRUCT_META_UP:
1856   case BRS_RECONSTRUCT_UP:
1857     break;
1858   case BRS_CHK_SET:
1859     if (GNUNET_OK !=
1860         GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1861       goto cleanup;
1862     break;
1863   case BRS_DOWNLOAD_DOWN:
1864   case BRS_DOWNLOAD_UP:
1865   case BRS_ERROR:
1866     break;
1867   default:
1868     GNUNET_break (0);
1869     goto cleanup;
1870   }
1871   for (i = 0; i < dr->num_children; i++)
1872   {
1873     if (NULL == (dr->children[i] = read_download_request (rh)))
1874       goto cleanup;
1875     dr->children[i]->parent = dr;
1876   }
1877   return dr;
1878 cleanup:
1879   GNUNET_FS_free_download_request_ (dr);
1880   return NULL;
1881 }
1882
1883
1884 /**
1885  * Compute the name of the sync file (or directory) for the given download
1886  * context.
1887  *
1888  * @param dc download context to compute for
1889  * @param uni unique filename to use, use "" for the directory name
1890  * @param ext extension to use, use ".dir" for our own subdirectory
1891  * @return the expanded file name, NULL for none
1892  */
1893 static char *
1894 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1895                             const char *uni,
1896                             const char *ext)
1897 {
1898   char *par;
1899   char *epar;
1900
1901   if (dc->parent == NULL)
1902     return get_serialization_file_name (dc->h,
1903                                         (dc->search != NULL) ?
1904                                         GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1905                                         GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1906                                         uni);
1907   if (NULL == dc->parent->serialization)
1908     return NULL;
1909   par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1910   if (NULL == par)
1911     return NULL;
1912   GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1913   GNUNET_free (par);
1914   return epar;
1915 }
1916
1917
1918 /**
1919  * Synchronize this download struct with its mirror
1920  * on disk.  Note that all internal FS-operations that change
1921  * publishing structs should already call "sync" internally,
1922  * so this function is likely not useful for clients.
1923  *
1924  * @param dc the struct to sync
1925  */
1926 void
1927 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1928 {
1929   struct GNUNET_BIO_WriteHandle *wh;
1930   char *uris;
1931   char *fn;
1932   char *dir;
1933
1934   if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1935     return; /* we don't sync probes */
1936   if (NULL == dc->serialization)
1937   {
1938     dir = get_download_sync_filename (dc, "", "");
1939     if (NULL == dir)
1940       return;
1941     if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1942     {
1943       GNUNET_free (dir);
1944       return;
1945     }
1946     fn = GNUNET_DISK_mktemp (dir);
1947     GNUNET_free (dir);
1948     if (NULL == fn)
1949       return;
1950     dc->serialization = get_serialization_short_name (fn);
1951   }
1952   else
1953   {
1954     fn = get_download_sync_filename (dc, dc->serialization, "");
1955     if (NULL == fn)
1956     {
1957       GNUNET_free (dc->serialization);
1958       dc->serialization = NULL;
1959       GNUNET_free (fn);
1960       return;
1961     }
1962   }
1963   wh = GNUNET_BIO_write_open (fn);
1964   if (NULL == wh)
1965   {
1966     GNUNET_free (dc->serialization);
1967     dc->serialization = NULL;
1968     GNUNET_free (fn);
1969     return;
1970   }
1971   GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1972                  (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1973   uris = GNUNET_FS_uri_to_string (dc->uri);
1974   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1975       (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1976       (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1977       (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1978       (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1979       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1980       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1981       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1982       (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1983       (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1984       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1985       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1986       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1987   {
1988     GNUNET_break (0);
1989     goto cleanup;
1990   }
1991   if (NULL == dc->emsg)
1992   {
1993     GNUNET_assert (dc->top_request != NULL);
1994     if (GNUNET_YES != write_download_request (wh, dc->top_request))
1995     {
1996       GNUNET_break (0);
1997       goto cleanup;
1998     }
1999   }
2000   GNUNET_free_non_null (uris);
2001   uris = NULL;
2002   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2003   {
2004     wh = NULL;
2005     GNUNET_break (0);
2006     goto cleanup;
2007   }
2008   GNUNET_free (fn);
2009   return;
2010 cleanup:
2011   if (NULL != wh)
2012     (void) GNUNET_BIO_write_close (wh);
2013   GNUNET_free_non_null (uris);
2014   if (0 != UNLINK (fn))
2015     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2016   GNUNET_free (fn);
2017   GNUNET_free (dc->serialization);
2018   dc->serialization = NULL;
2019 }
2020
2021
2022 /**
2023  * Synchronize this search result with its mirror
2024  * on disk.  Note that all internal FS-operations that change
2025  * publishing structs should already call "sync" internally,
2026  * so this function is likely not useful for clients.
2027  *
2028  * @param sr the struct to sync
2029  */
2030 void
2031 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2032 {
2033   struct GNUNET_BIO_WriteHandle *wh;
2034   char *uris;
2035
2036   if (NULL == sr->sc)
2037     return;
2038   uris = NULL;
2039   if (NULL == sr->serialization)
2040     sr->serialization =
2041         make_serialization_file_name_in_dir (sr->h,
2042                                              (sr->sc->psearch_result ==
2043                                               NULL) ?
2044                                              GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2045                                              GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2046                                              sr->sc->serialization);
2047   if (NULL == sr->serialization)
2048     return;
2049   wh = get_write_handle_in_dir (sr->h,
2050                                 (sr->sc->psearch_result ==
2051                                  NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2052                                 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2053                                 sr->sc->serialization, sr->serialization);
2054   if (NULL == wh)
2055   {
2056     GNUNET_break (0);
2057     goto cleanup;
2058   }
2059   uris = GNUNET_FS_uri_to_string (sr->uri);
2060   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2061       (GNUNET_OK !=
2062        GNUNET_BIO_write_string (wh,
2063                                 sr->download !=
2064                                 NULL ? sr->download->serialization : NULL)) ||
2065       (GNUNET_OK !=
2066        GNUNET_BIO_write_string (wh,
2067                                 sr->update_search !=
2068                                 NULL ? sr->update_search->serialization : NULL))
2069       || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2070       (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2071       || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2072       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2073       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2074       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2075   {
2076     GNUNET_break (0);
2077     goto cleanup;
2078   }
2079   if ( (NULL != sr->uri) &&
2080        (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2081        (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2082                                        (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2083   {
2084     GNUNET_break (0);
2085     goto cleanup;
2086   }
2087   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2088   {
2089     wh = NULL;
2090     GNUNET_break (0);
2091     goto cleanup;
2092   }
2093   GNUNET_free_non_null (uris);
2094   return;
2095 cleanup:
2096   GNUNET_free_non_null (uris);
2097   if (NULL != wh)
2098     (void) GNUNET_BIO_write_close (wh);
2099   remove_sync_file_in_dir (sr->h,
2100                            (NULL == sr->sc->psearch_result)
2101                            ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2102                            : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2103                            sr->sc->serialization, sr->serialization);
2104   GNUNET_free (sr->serialization);
2105   sr->serialization = NULL;
2106 }
2107
2108
2109 /**
2110  * Synchronize this search struct with its mirror
2111  * on disk.  Note that all internal FS-operations that change
2112  * publishing structs should already call "sync" internally,
2113  * so this function is likely not useful for clients.
2114  *
2115  * @param sc the struct to sync
2116  */
2117 void
2118 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2119 {
2120   struct GNUNET_BIO_WriteHandle *wh;
2121   char *uris;
2122   char in_pause;
2123   const char *category;
2124
2125   category =
2126       (NULL == sc->psearch_result)
2127     ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2128     : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2129   if (NULL == sc->serialization)
2130     sc->serialization = make_serialization_file_name (sc->h, category);
2131   if (NULL == sc->serialization)
2132     return;
2133   uris = NULL;
2134   wh = get_write_handle (sc->h, category, sc->serialization);
2135   if (NULL == wh)
2136   {
2137     GNUNET_break (0);
2138     goto cleanup;
2139   }
2140   GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2141                  (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2142   uris = GNUNET_FS_uri_to_string (sc->uri);
2143   in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2144   if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2145       (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2146       (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2147       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2148       (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2149       (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2150   {
2151     GNUNET_break (0);
2152     goto cleanup;
2153   }
2154   GNUNET_free (uris);
2155   uris = NULL;
2156   if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2157   {
2158     wh = NULL;
2159     GNUNET_break (0);
2160     goto cleanup;
2161   }
2162   return;
2163 cleanup:
2164   if (NULL != wh)
2165     (void) GNUNET_BIO_write_close (wh);
2166   GNUNET_free_non_null (uris);
2167   GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2168   GNUNET_free (sc->serialization);
2169   sc->serialization = NULL;
2170 }
2171
2172
2173 /**
2174  * Function called with a filename of serialized unindexing operation
2175  * to deserialize.
2176  *
2177  * @param cls the `struct GNUNET_FS_Handle *`
2178  * @param filename complete filename (absolute path)
2179  * @return #GNUNET_OK (continue to iterate)
2180  */
2181 static int
2182 deserialize_unindex_file (void *cls,
2183                           const char *filename)
2184 {
2185   struct GNUNET_FS_Handle *h = cls;
2186   struct GNUNET_BIO_ReadHandle *rh;
2187   struct GNUNET_FS_UnindexContext *uc;
2188   struct GNUNET_FS_ProgressInfo pi;
2189   char *emsg;
2190   char *uris;
2191   uint32_t state;
2192
2193   uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2194   uc->h = h;
2195   uc->serialization = get_serialization_short_name (filename);
2196   rh = GNUNET_BIO_read_open (filename);
2197   if (NULL == rh)
2198   {
2199     GNUNET_break (0);
2200     goto cleanup;
2201   }
2202   uris = NULL;
2203   if ((GNUNET_OK !=
2204        GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2205       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2206       (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2207       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2208       (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2209       (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2210       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2211   {
2212     GNUNET_free_non_null (uris);
2213     GNUNET_break (0);
2214     goto cleanup;
2215   }
2216   if (NULL != uris)
2217   {
2218     uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2219     GNUNET_free (uris);
2220     if (NULL == uc->ksk_uri)
2221     {
2222       GNUNET_break (0);
2223       GNUNET_free_non_null (emsg);
2224       goto cleanup;
2225     }
2226   }
2227   if ( (uc->ksk_offset > 0) &&
2228        ( (NULL == uc->ksk_uri) ||
2229          (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2230   {
2231     GNUNET_break (0);
2232     goto cleanup;
2233   }
2234   uc->state = (enum UnindexState) state;
2235   switch (state)
2236   {
2237   case UNINDEX_STATE_HASHING:
2238     break;
2239   case UNINDEX_STATE_FS_NOTIFY:
2240     if (GNUNET_OK !=
2241         GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2242                          sizeof (struct GNUNET_HashCode)))
2243     {
2244       GNUNET_break (0);
2245       goto cleanup;
2246     }
2247     break;
2248   case UNINDEX_STATE_DS_REMOVE:
2249   case UNINDEX_STATE_EXTRACT_KEYWORDS:
2250   case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2251     break;
2252   case UNINDEX_STATE_COMPLETE:
2253     break;
2254   case UNINDEX_STATE_ERROR:
2255     if (GNUNET_OK !=
2256         GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2257     {
2258       GNUNET_break (0);
2259       goto cleanup;
2260     }
2261     break;
2262   default:
2263     GNUNET_break (0);
2264     goto cleanup;
2265   }
2266   uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2267   pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2268   pi.value.unindex.specifics.resume.message = uc->emsg;
2269   GNUNET_FS_unindex_make_status_ (&pi, uc,
2270                                   (uc->state ==
2271                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2272   switch (uc->state)
2273   {
2274   case UNINDEX_STATE_HASHING:
2275     uc->fhc =
2276         GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2277                                  HASHING_BLOCKSIZE,
2278                                  &GNUNET_FS_unindex_process_hash_, uc);
2279     break;
2280   case UNINDEX_STATE_FS_NOTIFY:
2281     uc->state = UNINDEX_STATE_HASHING;
2282     GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2283     break;
2284   case UNINDEX_STATE_DS_REMOVE:
2285     GNUNET_FS_unindex_do_remove_ (uc);
2286     break;
2287   case UNINDEX_STATE_EXTRACT_KEYWORDS:
2288     GNUNET_FS_unindex_do_extract_keywords_ (uc);
2289     break;
2290   case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2291     GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2292     break;
2293   case UNINDEX_STATE_COMPLETE:
2294   case UNINDEX_STATE_ERROR:
2295     /* no need to resume any operation, we were done */
2296     break;
2297   default:
2298     break;
2299   }
2300   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2301   {
2302     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2303                 _("Failure while resuming unindexing operation `%s': %s\n"),
2304                 filename, emsg);
2305     GNUNET_free (emsg);
2306   }
2307   return GNUNET_OK;
2308 cleanup:
2309   GNUNET_free_non_null (uc->filename);
2310   if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2311   {
2312     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2313                 _("Failed to resume unindexing operation `%s': %s\n"),
2314                 filename,
2315                 emsg);
2316     GNUNET_free (emsg);
2317   }
2318   if (NULL != uc->serialization)
2319     GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2320                                  uc->serialization);
2321   GNUNET_free_non_null (uc->serialization);
2322   GNUNET_free (uc);
2323   return GNUNET_OK;
2324 }
2325
2326
2327 /**
2328  * Deserialize a download.
2329  *
2330  * @param h overall context
2331  * @param rh file to deserialize from
2332  * @param parent parent download
2333  * @param search associated search
2334  * @param serialization name under which the search was serialized
2335  */
2336 static void
2337 deserialize_download (struct GNUNET_FS_Handle *h,
2338                       struct GNUNET_BIO_ReadHandle *rh,
2339                       struct GNUNET_FS_DownloadContext *parent,
2340                       struct GNUNET_FS_SearchResult *search,
2341                       const char *serialization);
2342
2343
2344 /**
2345  * Deserialize a search.
2346  *
2347  * @param h overall context
2348  * @param rh file to deserialize from
2349  * @param psearch_result parent search result
2350  * @param serialization name under which the search was serialized
2351  */
2352 static struct GNUNET_FS_SearchContext *
2353 deserialize_search (struct GNUNET_FS_Handle *h,
2354                     struct GNUNET_BIO_ReadHandle *rh,
2355                     struct GNUNET_FS_SearchResult *psearch_result,
2356                     const char *serialization);
2357
2358
2359 /**
2360  * Function called with a filename of serialized search result
2361  * to deserialize.
2362  *
2363  * @param cls the `struct GNUNET_FS_SearchContext *`
2364  * @param filename complete filename (absolute path)
2365  * @return #GNUNET_OK (continue to iterate)
2366  */
2367 static int
2368 deserialize_search_result (void *cls,
2369                            const char *filename)
2370 {
2371   struct GNUNET_FS_SearchContext *sc = cls;
2372   char *ser;
2373   char *uris;
2374   char *emsg;
2375   char *download;
2376   char *update_srch;
2377   struct GNUNET_BIO_ReadHandle *rh;
2378   struct GNUNET_BIO_ReadHandle *drh;
2379   struct GNUNET_FS_SearchResult *sr;
2380
2381   ser = get_serialization_short_name (filename);
2382   rh = GNUNET_BIO_read_open (filename);
2383   if (NULL == rh)
2384   {
2385     if (NULL != ser)
2386     {
2387       remove_sync_file_in_dir (sc->h,
2388                                (NULL == sc->psearch_result)
2389                                ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2390                                : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2391                                sc->serialization, ser);
2392       GNUNET_free (ser);
2393     }
2394     return GNUNET_OK;
2395   }
2396   emsg = NULL;
2397   uris = NULL;
2398   download = NULL;
2399   update_srch = NULL;
2400   sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2401   sr->h = sc->h;
2402   sr->sc = sc;
2403   sr->serialization = ser;
2404   if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2405       || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2406       (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2407       || (GNUNET_OK !=
2408           GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2409       (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2410       (GNUNET_OK !=
2411        GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2412       || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2413       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2414       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2415       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2416   {
2417     GNUNET_break (0);
2418     goto cleanup;
2419   }
2420   if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2421   {
2422     sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2423     if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2424                                       sr->keyword_bitmap,
2425                                       (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2426     {
2427       GNUNET_break (0);
2428       goto cleanup;
2429     }
2430   }
2431   GNUNET_free (uris);
2432   if (NULL != download)
2433   {
2434     drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2435     if (NULL != drh)
2436     {
2437       deserialize_download (sc->h, drh, NULL, sr, download);
2438       if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2439       {
2440         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2441                     _("Failed to resume sub-download `%s': %s\n"),
2442                     download,
2443                     emsg);
2444         GNUNET_free (emsg);
2445       }
2446     }
2447     GNUNET_free (download);
2448   }
2449   if (NULL != update_srch)
2450   {
2451     drh =
2452         get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2453     if (NULL != drh)
2454     {
2455       deserialize_search (sc->h, drh, sr, update_srch);
2456       if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2457       {
2458         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2459                     _("Failed to resume sub-search `%s': %s\n"),
2460                     update_srch,
2461                     emsg);
2462         GNUNET_free (emsg);
2463       }
2464     }
2465     GNUNET_free (update_srch);
2466   }
2467   GNUNET_break (GNUNET_YES ==
2468                 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2469                                                    &sr->key, sr,
2470                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2471   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2472   {
2473     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2474                 _("Failure while resuming search operation `%s': %s\n"),
2475                 filename, emsg);
2476     GNUNET_free (emsg);
2477   }
2478   return GNUNET_OK;
2479 cleanup:
2480   GNUNET_free_non_null (download);
2481   GNUNET_free_non_null (emsg);
2482   GNUNET_free_non_null (uris);
2483   GNUNET_free_non_null (update_srch);
2484   if (NULL != sr->uri)
2485     GNUNET_FS_uri_destroy (sr->uri);
2486   if (NULL != sr->meta)
2487     GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2488   GNUNET_free (sr->serialization);
2489   GNUNET_free (sr);
2490   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2491   {
2492     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2493                 _("Failure while resuming search operation `%s': %s\n"),
2494                 filename, emsg);
2495     GNUNET_free (emsg);
2496   }
2497   return GNUNET_OK;
2498 }
2499
2500
2501 /**
2502  * Send the 'resume' signal to the callback; also actually
2503  * resume the download (put it in the queue).  Does this
2504  * recursively for the top-level download and all child
2505  * downloads.
2506  *
2507  * @param dc download to resume
2508  */
2509 static void
2510 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2511 {
2512   struct GNUNET_FS_DownloadContext *dcc;
2513   struct GNUNET_FS_ProgressInfo pi;
2514
2515   pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2516   pi.value.download.specifics.resume.meta = dc->meta;
2517   pi.value.download.specifics.resume.message = dc->emsg;
2518   GNUNET_FS_download_make_status_ (&pi, dc);
2519   dcc = dc->child_head;
2520   while (NULL != dcc)
2521   {
2522     signal_download_resume (dcc);
2523     dcc = dcc->next;
2524   }
2525   if (NULL != dc->pending_head)
2526     GNUNET_FS_download_start_downloading_ (dc);
2527 }
2528
2529
2530 /**
2531  * Signal resuming of a search to our clients (for the
2532  * top level search and all sub-searches).
2533  *
2534  * @param sc search being resumed
2535  */
2536 static void
2537 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2538
2539
2540 /**
2541  * Iterator over search results signaling resume to the client for
2542  * each result.
2543  *
2544  * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2545  * @param key current key code
2546  * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2547  * @return #GNUNET_YES (we should continue to iterate)
2548  */
2549 static int
2550 signal_result_resume (void *cls,
2551                       const struct GNUNET_HashCode *key,
2552                       void *value)
2553 {
2554   struct GNUNET_FS_SearchContext *sc = cls;
2555   struct GNUNET_FS_ProgressInfo pi;
2556   struct GNUNET_FS_SearchResult *sr = value;
2557
2558   if (0 == sr->mandatory_missing)
2559   {
2560     pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2561     pi.value.search.specifics.resume_result.meta = sr->meta;
2562     pi.value.search.specifics.resume_result.uri = sr->uri;
2563     pi.value.search.specifics.resume_result.result = sr;
2564     pi.value.search.specifics.resume_result.availability_rank =
2565         2 * sr->availability_success - sr->availability_trials;
2566     pi.value.search.specifics.resume_result.availability_certainty =
2567         sr->availability_trials;
2568     pi.value.search.specifics.resume_result.applicability_rank =
2569         sr->optional_support;
2570     sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2571   }
2572   if (NULL != sr->download)
2573   {
2574     signal_download_resume (sr->download);
2575   }
2576   else
2577   {
2578     GNUNET_FS_search_start_probe_ (sr);
2579   }
2580   if (NULL != sr->update_search)
2581     signal_search_resume (sr->update_search);
2582   return GNUNET_YES;
2583 }
2584
2585
2586 /**
2587  * Free memory allocated by the search context and its children
2588  *
2589  * @param sc search context to free
2590  */
2591 static void
2592 free_search_context (struct GNUNET_FS_SearchContext *sc);
2593
2594
2595 /**
2596  * Iterator over search results freeing each.
2597  *
2598  * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2599  * @param key current key code
2600  * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2601  * @return #GNUNET_YES (we should continue to iterate)
2602  */
2603 static int
2604 free_result (void *cls,
2605              const struct GNUNET_HashCode *key,
2606              void *value)
2607 {
2608   struct GNUNET_FS_SearchResult *sr = value;
2609
2610   if (NULL != sr->update_search)
2611   {
2612     free_search_context (sr->update_search);
2613     GNUNET_assert (NULL == sr->update_search);
2614   }
2615   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2616   GNUNET_FS_uri_destroy (sr->uri);
2617   GNUNET_free (sr);
2618   return GNUNET_YES;
2619 }
2620
2621
2622 /**
2623  * Free memory allocated by the search context and its children
2624  *
2625  * @param sc search context to free
2626  */
2627 static void
2628 free_search_context (struct GNUNET_FS_SearchContext *sc)
2629 {
2630   if (NULL != sc->serialization)
2631   {
2632     GNUNET_FS_remove_sync_file_ (sc->h,
2633                                  (sc->psearch_result ==
2634                                   NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2635                                  GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2636                                  sc->serialization);
2637     GNUNET_FS_remove_sync_dir_ (sc->h,
2638                                 (sc->psearch_result ==
2639                                  NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2640                                 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2641                                 sc->serialization);
2642   }
2643   GNUNET_free_non_null (sc->serialization);
2644   GNUNET_free_non_null (sc->emsg);
2645   if (NULL != sc->uri)
2646     GNUNET_FS_uri_destroy (sc->uri);
2647   if (NULL != sc->master_result_map)
2648   {
2649     GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2650                                            sc);
2651     GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2652   }
2653   GNUNET_free (sc);
2654 }
2655
2656
2657 /**
2658  * Function called with a filename of serialized sub-download
2659  * to deserialize.
2660  *
2661  * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2662  * @param filename complete filename (absolute path)
2663  * @return #GNUNET_OK (continue to iterate)
2664  */
2665 static int
2666 deserialize_subdownload (void *cls,
2667                          const char *filename)
2668 {
2669   struct GNUNET_FS_DownloadContext *parent = cls;
2670   char *ser;
2671   char *emsg;
2672   struct GNUNET_BIO_ReadHandle *rh;
2673
2674   ser = get_serialization_short_name (filename);
2675   rh = GNUNET_BIO_read_open (filename);
2676   if (NULL == rh)
2677   {
2678     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2679                 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2680                 ser,
2681                 filename);
2682     GNUNET_free (ser);
2683     return GNUNET_OK;
2684   }
2685   deserialize_download (parent->h, rh, parent, NULL, ser);
2686   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2687   {
2688     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2689                 _("Failed to resume sub-download `%s': %s\n"),
2690                 ser,
2691                 emsg);
2692     GNUNET_free (emsg);
2693   }
2694   GNUNET_free (ser);
2695   return GNUNET_OK;
2696 }
2697
2698
2699 /**
2700  * Free this download context and all of its descendants.
2701  * (only works during deserialization since not all possible
2702  * state it taken care of).
2703  *
2704  * @param dc context to free
2705  */
2706 static void
2707 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2708 {
2709   struct GNUNET_FS_DownloadContext *dcc;
2710
2711   if (NULL != dc->meta)
2712     GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2713   if (NULL != dc->uri)
2714     GNUNET_FS_uri_destroy (dc->uri);
2715   GNUNET_free_non_null (dc->temp_filename);
2716   GNUNET_free_non_null (dc->emsg);
2717   GNUNET_free_non_null (dc->filename);
2718   GNUNET_free_non_null (dc->serialization);
2719   while (NULL != (dcc = dc->child_head))
2720   {
2721     GNUNET_CONTAINER_DLL_remove (dc->child_head,
2722                                  dc->child_tail,
2723                                  dcc);
2724     free_download_context (dcc);
2725   }
2726   GNUNET_FS_free_download_request_ (dc->top_request);
2727   if (NULL != dc->active)
2728     GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2729   GNUNET_free (dc);
2730 }
2731
2732
2733 /**
2734  * Deserialize a download.
2735  *
2736  * @param h overall context
2737  * @param rh file to deserialize from
2738  * @param parent parent download
2739  * @param search associated search
2740  * @param serialization name under which the search was serialized
2741  */
2742 static void
2743 deserialize_download (struct GNUNET_FS_Handle *h,
2744                       struct GNUNET_BIO_ReadHandle *rh,
2745                       struct GNUNET_FS_DownloadContext *parent,
2746                       struct GNUNET_FS_SearchResult *search,
2747                       const char *serialization)
2748 {
2749   struct GNUNET_FS_DownloadContext *dc;
2750   char *emsg;
2751   char *uris;
2752   char *dn;
2753   uint32_t options;
2754   uint32_t status;
2755
2756   uris = NULL;
2757   emsg = NULL;
2758   dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2759   dc->parent = parent;
2760   dc->h = h;
2761   dc->serialization = GNUNET_strdup (serialization);
2762   if ((GNUNET_OK !=
2763        GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2764       (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2765       ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2766        (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2767       (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2768       || (GNUNET_OK !=
2769           GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2770       (GNUNET_OK !=
2771        GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2772       (GNUNET_OK !=
2773        GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2774                                10 * 1024)) ||
2775       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2776       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2777       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2778       (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2779       (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2780       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2781       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2782       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2783   {
2784     GNUNET_break (0);
2785     goto cleanup;
2786   }
2787   dc->options = (enum GNUNET_FS_DownloadOptions) options;
2788   dc->active =
2789     GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2790   dc->has_finished = (int) status;
2791   dc->treedepth =
2792       GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2793   if (GNUNET_FS_uri_test_loc (dc->uri))
2794     GNUNET_assert (GNUNET_OK ==
2795                    GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2796   if (NULL == dc->emsg)
2797   {
2798     dc->top_request = read_download_request (rh);
2799     if (NULL == dc->top_request)
2800     {
2801       GNUNET_break (0);
2802       goto cleanup;
2803     }
2804   }
2805   dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2806   if (NULL != dn)
2807   {
2808     if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2809       GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2810     GNUNET_free (dn);
2811   }
2812   if (NULL != parent)
2813   {
2814     GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2815   }
2816   if (NULL != search)
2817   {
2818     dc->search = search;
2819     search->download = dc;
2820   }
2821   if ((NULL == parent) && (NULL == search))
2822   {
2823     dc->top =
2824         GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2825     signal_download_resume (dc);
2826   }
2827   GNUNET_free (uris);
2828   dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2829   return;
2830 cleanup:
2831   GNUNET_free_non_null (uris);
2832   GNUNET_free_non_null (emsg);
2833   free_download_context (dc);
2834 }
2835
2836
2837 /**
2838  * Signal resuming of a search to our clients (for the
2839  * top level search and all sub-searches).
2840  *
2841  * @param sc search being resumed
2842  */
2843 static void
2844 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2845 {
2846   struct GNUNET_FS_ProgressInfo pi;
2847
2848   pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2849   pi.value.search.specifics.resume.message = sc->emsg;
2850   pi.value.search.specifics.resume.is_paused =
2851       (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2852   sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2853   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2854                                          &signal_result_resume, sc);
2855
2856 }
2857
2858
2859 /**
2860  * Deserialize a search.
2861  *
2862  * @param h overall context
2863  * @param rh file to deserialize from
2864  * @param psearch_result parent search result
2865  * @param serialization name under which the search was serialized
2866  */
2867 static struct GNUNET_FS_SearchContext *
2868 deserialize_search (struct GNUNET_FS_Handle *h,
2869                     struct GNUNET_BIO_ReadHandle *rh,
2870                     struct GNUNET_FS_SearchResult *psearch_result,
2871                     const char *serialization)
2872 {
2873   struct GNUNET_FS_SearchContext *sc;
2874   char *emsg;
2875   char *uris;
2876   char *dn;
2877   uint32_t options;
2878   char in_pause;
2879
2880   if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2881   {
2882     GNUNET_break (0);
2883     return NULL;
2884   }
2885   uris = NULL;
2886   emsg = NULL;
2887   sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2888   if (NULL != psearch_result)
2889   {
2890     sc->psearch_result = psearch_result;
2891     psearch_result->update_search = sc;
2892   }
2893   sc->h = h;
2894   sc->serialization = GNUNET_strdup (serialization);
2895   if ((GNUNET_OK !=
2896        GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2897       (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2898       ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2899        (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2900       (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2901       (GNUNET_OK !=
2902        GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2903       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2904       (GNUNET_OK !=
2905        GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2906       (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2907   {
2908     GNUNET_break (0);
2909     goto cleanup;
2910   }
2911   sc->options = (enum GNUNET_FS_SearchOptions) options;
2912   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2913   dn = get_serialization_file_name_in_dir (h,
2914                                            (NULL == sc->psearch_result) ?
2915                                            GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2916                                            GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2917                                            sc->serialization, "");
2918   if (NULL != dn)
2919   {
2920     if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2921       GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2922     GNUNET_free (dn);
2923   }
2924   if (('\0' == in_pause) &&
2925       (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2926   {
2927     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2928                 _("Could not resume running search, will resume as paused search\n"));
2929   }
2930   signal_search_resume (sc);
2931   GNUNET_free (uris);
2932   return sc;
2933 cleanup:
2934   GNUNET_free_non_null (emsg);
2935   free_search_context (sc);
2936   GNUNET_free_non_null (uris);
2937   return NULL;
2938 }
2939
2940
2941 /**
2942  * Function called with a filename of serialized search operation
2943  * to deserialize.
2944  *
2945  * @param cls the `struct GNUNET_FS_Handle *`
2946  * @param filename complete filename (absolute path)
2947  * @return #GNUNET_OK (continue to iterate)
2948  */
2949 static int
2950 deserialize_search_file (void *cls,
2951                          const char *filename)
2952 {
2953   struct GNUNET_FS_Handle *h = cls;
2954   char *ser;
2955   char *emsg;
2956   struct GNUNET_BIO_ReadHandle *rh;
2957   struct GNUNET_FS_SearchContext *sc;
2958   struct stat buf;
2959
2960   if (0 != STAT (filename, &buf))
2961   {
2962     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2963     return GNUNET_OK;
2964   }
2965   if (S_ISDIR (buf.st_mode))
2966     return GNUNET_OK; /* skip directories */
2967   ser = get_serialization_short_name (filename);
2968   rh = GNUNET_BIO_read_open (filename);
2969   if (NULL == rh)
2970   {
2971     if (NULL != ser)
2972     {
2973       GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2974       GNUNET_free (ser);
2975     }
2976     return GNUNET_OK;
2977   }
2978   sc = deserialize_search (h, rh, NULL, ser);
2979   if (NULL != sc)
2980     sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2981   GNUNET_free (ser);
2982   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2983   {
2984     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2985                 _("Failure while resuming search operation `%s': %s\n"),
2986                 filename, emsg);
2987     GNUNET_free (emsg);
2988   }
2989   return GNUNET_OK;
2990 }
2991
2992
2993 /**
2994  * Function called with a filename of serialized download operation
2995  * to deserialize.
2996  *
2997  * @param cls the `struct GNUNET_FS_Handle *`
2998  * @param filename complete filename (absolute path)
2999  * @return #GNUNET_OK (continue to iterate)
3000  */
3001 static int
3002 deserialize_download_file (void *cls, const char *filename)
3003 {
3004   struct GNUNET_FS_Handle *h = cls;
3005   char *ser;
3006   char *emsg;
3007   struct GNUNET_BIO_ReadHandle *rh;
3008
3009   ser = get_serialization_short_name (filename);
3010   rh = GNUNET_BIO_read_open (filename);
3011   if (NULL == rh)
3012   {
3013     if (0 != UNLINK (filename))
3014       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3015     GNUNET_free (ser);
3016     return GNUNET_OK;
3017   }
3018   deserialize_download (h, rh, NULL, NULL, ser);
3019   GNUNET_free (ser);
3020   if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3021   {
3022     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3023                 _("Failure while resuming download operation `%s': %s\n"),
3024                 filename, emsg);
3025     GNUNET_free (emsg);
3026   }
3027   return GNUNET_OK;
3028 }
3029
3030
3031 /**
3032  * Deserialize informatin about pending operations.
3033  *
3034  * @param master_path which master directory should be scanned
3035  * @param proc function to call for each entry (will get 'h' for 'cls')
3036  * @param h the 'struct GNUNET_FS_Handle*'
3037  */
3038 static void
3039 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
3040                         struct GNUNET_FS_Handle *h)
3041 {
3042   char *dn;
3043
3044   dn = get_serialization_file_name (h, master_path, "");
3045   if (NULL == dn)
3046     return;
3047   if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3048     GNUNET_DISK_directory_scan (dn, proc, h);
3049   GNUNET_free (dn);
3050 }
3051
3052
3053 /**
3054  * Setup a connection to the file-sharing service.
3055  *
3056  * @param cfg configuration to use
3057  * @param client_name unique identifier for this client
3058  * @param upcb function to call to notify about FS actions
3059  * @param upcb_cls closure for @a upcb
3060  * @param flags specific attributes for fs-operations
3061  * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3062  * @return NULL on error
3063  */
3064 struct GNUNET_FS_Handle *
3065 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3066                  const char *client_name,
3067                  GNUNET_FS_ProgressCallback upcb,
3068                  void *upcb_cls,
3069                  enum GNUNET_FS_Flags flags, ...)
3070 {
3071   struct GNUNET_FS_Handle *ret;
3072   enum GNUNET_FS_OPTIONS opt;
3073   va_list ap;
3074
3075   ret = GNUNET_new (struct GNUNET_FS_Handle);
3076   ret->cfg = cfg;
3077   ret->client_name = GNUNET_strdup (client_name);
3078   ret->upcb = upcb;
3079   ret->upcb_cls = upcb_cls;
3080   ret->flags = flags;
3081   ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3082   ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3083   ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES;    /* conservative starting point */
3084   va_start (ap, flags);
3085   while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3086   {
3087     switch (opt)
3088     {
3089     case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3090       ret->max_parallel_downloads = va_arg (ap, unsigned int);
3091
3092       break;
3093     case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3094       ret->max_parallel_requests = va_arg (ap, unsigned int);
3095
3096       break;
3097     default:
3098       GNUNET_break (0);
3099       GNUNET_free (ret->client_name);
3100       GNUNET_free (ret);
3101       va_end (ap);
3102       return NULL;
3103     }
3104   }
3105   va_end (ap);
3106   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3107   {
3108     deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3109                             &deserialize_publish_file, ret);
3110     deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3111                             &deserialize_search_file, ret);
3112     deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3113                             &deserialize_download_file, ret);
3114     deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3115                             &deserialize_unindex_file, ret);
3116   }
3117   return ret;
3118 }
3119
3120
3121 /**
3122  * Close our connection with the file-sharing service.
3123  * The callback given to GNUNET_FS_start will no longer be
3124  * called after this function returns.
3125  *
3126  * @param h handle that was returned from #GNUNET_FS_start()
3127  */
3128 void
3129 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3130 {
3131   while (h->top_head != NULL)
3132     h->top_head->ssf (h->top_head->ssf_cls);
3133   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3134     GNUNET_SCHEDULER_cancel (h->queue_job);
3135   GNUNET_free (h->client_name);
3136   GNUNET_free (h);
3137 }
3138
3139
3140 /* end of fs_api.c */