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