053fd3dd0f818bc3af75f6880353ebd8578a4605
[oweals/gnunet.git] / src / fs / fs.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 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 2, 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.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.h"
31
32
33 /**
34  * Start the given job (send signal, remove from pending queue, update
35  * counters and state).
36  *
37  * @param qe job to start
38  */
39 static void
40 start_job (struct GNUNET_FS_QueueEntry *qe)
41 {
42   qe->client = GNUNET_CLIENT_connect (qe->h->sched, "fs", qe->h->cfg);
43   if (qe->client == NULL)
44     {
45       GNUNET_break (0);
46       return;
47     }
48   qe->start (qe->cls, qe->client);
49   qe->start_times++;
50   qe->h->active_blocks += qe->blocks;
51   qe->start_time = GNUNET_TIME_absolute_get ();
52   GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
53                                qe->h->pending_tail,
54                                qe);
55   GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
56                                      qe->h->running_tail,
57                                      qe->h->running_tail,
58                                      qe);
59 }
60
61
62 /**
63  * Stop the given job (send signal, remove from active queue, update
64  * counters and state).
65  *
66  * @param qe job to stop
67  */
68 static void
69 stop_job (struct GNUNET_FS_QueueEntry *qe)
70 {
71   qe->client = NULL;
72   qe->stop (qe->cls);
73   qe->h->active_downloads--;
74   qe->h->active_blocks -= qe->blocks;
75   qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
76                                            GNUNET_TIME_absolute_get_duration (qe->start_time));
77   GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
78                                qe->h->running_tail,
79                                qe);
80   GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
81                                      qe->h->pending_tail,
82                                      qe->h->pending_tail,
83                                      qe);
84 }
85
86
87 /**
88  * Process the jobs in the job queue, possibly starting some
89  * and stopping others.
90  *
91  * @param cls the 'struct GNUNET_FS_Handle'
92  * @param tc scheduler context
93  */
94 static void
95 process_job_queue (void *cls,
96                    const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   struct GNUNET_FS_Handle *h = cls;
99   struct GNUNET_FS_QueueEntry *qe;
100   struct GNUNET_FS_QueueEntry *next;
101   struct GNUNET_TIME_Relative run_time;
102   struct GNUNET_TIME_Relative restart_at;
103   struct GNUNET_TIME_Relative rst;
104   struct GNUNET_TIME_Absolute end_time;
105
106   h->queue_job = GNUNET_SCHEDULER_NO_TASK;
107   next = h->pending_head;
108   while (NULL != (qe = next))
109     {
110       next = qe->next;
111       if (h->running_head == NULL)
112         {
113           start_job (qe);
114           continue;
115         }
116       if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
117            (h->active_downloads + 1 <= h->max_parallel_downloads) )
118         {
119           start_job (qe);
120           continue;
121         }
122     }
123   if (h->pending_head == NULL)
124     return; /* no need to stop anything */
125   restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
126   next = h->running_head;
127   while (NULL != (qe = next))
128     {
129       next = qe->next;
130       run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
131                                                 qe->blocks * qe->start_times);
132       end_time = GNUNET_TIME_absolute_add (qe->start_time,
133                                            run_time);
134       rst = GNUNET_TIME_absolute_get_remaining (end_time);
135       restart_at = GNUNET_TIME_relative_min (rst, restart_at);
136       if (rst.value > 0)
137         continue;       
138       stop_job (qe);
139     }
140   h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
141                                                restart_at,
142                                                &process_job_queue,
143                                                h);
144 }
145
146
147 /**
148  * Add a job to the queue.
149  *
150  * @param h handle to the overall FS state
151  * @param start function to call to begin the job
152  * @param stop function to call to pause the job, or on dequeue (if the job was running)
153  * @param cls closure for start and stop
154  * @param blocks number of blocks this jobs uses
155  * @return queue handle
156  */
157 struct GNUNET_FS_QueueEntry *
158 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
159                   GNUNET_FS_QueueStart start,
160                   GNUNET_FS_QueueStop stop,
161                   void *cls,
162                   unsigned int blocks)
163 {
164   struct GNUNET_FS_QueueEntry *qe;
165
166   qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
167   qe->h = h;
168   qe->start = start;
169   qe->stop = stop;
170   qe->cls = cls;
171   qe->queue_time = GNUNET_TIME_absolute_get ();
172   qe->blocks = blocks;
173   GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
174                                      h->pending_tail,
175                                      h->pending_tail,
176                                      qe);
177   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
178     GNUNET_SCHEDULER_cancel (h->sched,
179                              h->queue_job);
180   h->queue_job 
181     = GNUNET_SCHEDULER_add_now (h->sched,
182                                 &process_job_queue,
183                                 h);
184   return qe;
185 }
186
187
188 /**
189  * Dequeue a job from the queue.
190  * @param qh handle for the job
191  */
192 void
193 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
194 {
195   struct GNUNET_FS_Handle *h;
196
197   h = qh->h;
198   if (qh->client != NULL)    
199     stop_job (qh);    
200   GNUNET_CONTAINER_DLL_remove (h->pending_head,
201                                h->pending_tail,
202                                qh);
203   GNUNET_free (qh);
204   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
205     GNUNET_SCHEDULER_cancel (h->sched,
206                              h->queue_job);
207   h->queue_job 
208     = GNUNET_SCHEDULER_add_now (h->sched,
209                                 &process_job_queue,
210                                 h);
211 }
212
213
214 /**
215  * Closure for "data_reader_file".
216  */
217 struct FileInfo
218 {
219   /**
220    * Name of the file to read.
221    */
222   char *filename;
223
224   /**
225    * File descriptor, NULL if it has not yet been opened.
226    */
227   struct GNUNET_DISK_FileHandle *fd;
228 };
229
230
231 /**
232  * Function that provides data by reading from a file.
233  *
234  * @param cls closure (points to the file information)
235  * @param offset offset to read from; it is possible
236  *            that the caller might need to go backwards
237  *            a bit at times
238  * @param max maximum number of bytes that should be 
239  *            copied to buf; readers are not allowed
240  *            to provide less data unless there is an error;
241  *            a value of "0" will be used at the end to allow
242  *            the reader to clean up its internal state
243  * @param buf where the reader should write the data
244  * @param emsg location for the reader to store an error message
245  * @return number of bytes written, usually "max", 0 on error
246  */
247 size_t
248 GNUNET_FS_data_reader_file_(void *cls, 
249                             uint64_t offset,
250                             size_t max, 
251                             void *buf,
252                             char **emsg)
253 {
254   struct FileInfo *fi = cls;
255   ssize_t ret;
256
257   if (max == 0)
258     {
259       if (fi->fd != NULL)
260         GNUNET_DISK_file_close (fi->fd);
261       GNUNET_free (fi->filename);
262       GNUNET_free (fi);
263       return 0;
264     }  
265   if (fi->fd == NULL)
266     {
267       fi->fd = GNUNET_DISK_file_open (fi->filename,
268                                       GNUNET_DISK_OPEN_READ,
269                                       GNUNET_DISK_PERM_NONE);
270       if (fi->fd == NULL)
271         {
272           GNUNET_asprintf (emsg, 
273                            _("Could not open file `%s': %s"),
274                            fi->filename,
275                            STRERROR (errno));
276           return 0;
277         }
278     }
279   GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
280   ret = GNUNET_DISK_file_read (fi->fd, buf, max);
281   if (ret == -1)
282     {
283       GNUNET_asprintf (emsg, 
284                        _("Could not read file `%s': %s"),
285                        fi->filename,
286                        STRERROR (errno));
287       return 0;
288     }
289   if (ret != max)
290     {
291       GNUNET_asprintf (emsg, 
292                        _("Short read reading from file `%s'!"),
293                        fi->filename);
294       return 0;
295     }
296   return max;
297 }
298
299
300 /**
301  * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
302  *
303  * @param filename file to read
304  * @return closure to use, NULL on error
305  */
306 void *
307 GNUNET_FS_make_file_reader_context_ (const char *filename)
308 {
309   struct FileInfo *fi;
310
311   fi = GNUNET_malloc (sizeof(struct FileInfo));
312   fi->filename = GNUNET_STRINGS_filename_expand (filename);
313   if (fi->filename == NULL)
314     {
315       GNUNET_free (fi);
316       return NULL;
317     }
318   return fi;
319 }
320
321
322 /**
323  * Function that provides data by copying from a buffer.
324  *
325  * @param cls closure (points to the buffer)
326  * @param offset offset to read from; it is possible
327  *            that the caller might need to go backwards
328  *            a bit at times
329  * @param max maximum number of bytes that should be 
330  *            copied to buf; readers are not allowed
331  *            to provide less data unless there is an error;
332  *            a value of "0" will be used at the end to allow
333  *            the reader to clean up its internal state
334  * @param buf where the reader should write the data
335  * @param emsg location for the reader to store an error message
336  * @return number of bytes written, usually "max", 0 on error
337  */
338 size_t
339 GNUNET_FS_data_reader_copy_ (void *cls, 
340                              uint64_t offset,
341                              size_t max, 
342                              void *buf,
343                              char **emsg)
344 {
345   char *data = cls;
346
347   if (max == 0)
348     {
349       GNUNET_free_non_null (data);
350       return 0;
351     }  
352   memcpy (buf, &data[offset], max);
353   return max;
354 }
355
356
357
358 /**
359  * Return the full filename where we would store state information
360  * (for serialization/deserialization).
361  *
362  * @param h master context
363  * @param ext component of the path 
364  * @param ent entity identifier (or emtpy string for the directory)
365  * @return NULL on error
366  */
367 static char *
368 get_serialization_file_name (struct GNUNET_FS_Handle *h,
369                              const char *ext,
370                              const char *ent)
371 {
372   char *basename;
373   char *ret;
374   if (GNUNET_OK !=
375       GNUNET_CONFIGURATION_get_value_filename (h->cfg,
376                                                "fs",
377                                                "STATE_DIR",
378                                                &basename))
379     return NULL;
380   GNUNET_asprintf (&ret,
381                    "%s%s%s-%s%s%s",
382                    basename,
383                    DIR_SEPARATOR_STR,
384                    h->client_name,
385                    ext,
386                    DIR_SEPARATOR_STR,
387                    ent);
388   GNUNET_free (basename);
389   return ret;
390 }
391
392
393 /**
394  * Return a read handle for deserialization.
395  *
396  * @param h master context
397  * @param ext component of the path 
398  * @param ent entity identifier (or emtpy string for the directory)
399  * @return NULL on error
400  */
401 static struct GNUNET_BIO_ReadHandle *
402 get_read_handle (struct GNUNET_FS_Handle *h,
403                  const char *ext,
404                  const char *ent)
405 {
406   char *fn;
407   struct GNUNET_BIO_ReadHandle *ret;
408
409   fn = get_serialization_file_name (h, ext, ent);
410   if (fn == NULL)
411     return NULL;
412   ret = GNUNET_BIO_read_open (fn);
413   GNUNET_free (fn);
414   return ret;
415 }
416
417
418 /**
419  * Return a write handle for serialization.
420  *
421  * @param h master context
422  * @param ext component of the path 
423  * @param ent entity identifier (or emtpy string for the directory)
424  * @return NULL on error
425  */
426 static struct GNUNET_BIO_WriteHandle *
427 get_write_handle (struct GNUNET_FS_Handle *h,
428                  const char *ext,
429                  const char *ent)
430 {
431   char *fn;
432   struct GNUNET_BIO_WriteHandle *ret;
433
434   fn = get_serialization_file_name (h, ext, ent);
435   if (fn == NULL)
436     return NULL;
437   ret = GNUNET_BIO_write_open (fn);
438   GNUNET_free (fn);
439   return ret;
440 }
441
442
443 /**
444  * Remove serialization/deserialization file from disk.
445  *
446  * @param h master context
447  * @param ext component of the path 
448  * @param ent entity identifier 
449  */
450 void
451 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
452                              const char *ext,
453                              const char *ent)
454 {
455   char *filename;
456
457   if ( (NULL == ent) ||
458        (0 == strlen (ent)) )
459     {
460       GNUNET_break (0);
461       return;
462     }
463   filename = get_serialization_file_name (h, ext, ent);
464   if (0 != UNLINK (filename))
465     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
466                               "unlink", 
467                               filename);
468   GNUNET_free (filename);
469 }
470
471
472
473 /**
474  * Using the given serialization filename, try to deserialize
475  * the file-information tree associated with it.
476  *
477  * @param h master context
478  * @param filename name of the file (without directory) with
479  *        the infromation
480  * @return NULL on error
481  */
482 static struct GNUNET_FS_FileInformation *
483 deserialize_file_information (struct GNUNET_FS_Handle *h,
484                               const char *filename);
485
486
487 /**
488  * Using the given serialization filename, try to deserialize
489  * the file-information tree associated with it.
490  *
491  * @param h master context
492  * @param fn name of the file (without directory) with
493  *        the infromation
494  * @param rh handle for reading
495  * @return NULL on error
496  */
497 static struct GNUNET_FS_FileInformation *
498 deserialize_fi_node (struct GNUNET_FS_Handle *h,
499                      const char *fn,
500                      struct GNUNET_BIO_ReadHandle *rh)
501 {
502   struct GNUNET_FS_FileInformation *ret;
503   struct GNUNET_FS_FileInformation *nxt;
504   char b;
505   char *ksks;
506   char *chks;
507   char *filename;
508   uint32_t dsize;
509
510   if (GNUNET_OK !=
511       GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
512     {
513       GNUNET_break (0);
514       return NULL;
515     }
516   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
517   ksks = NULL;
518   chks = NULL;
519   filename = NULL;
520   if ( (GNUNET_OK !=
521         GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
522        (GNUNET_OK !=
523         GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
524        ( (ksks != NULL) &&
525          (NULL == 
526           (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
527        (GNUNET_YES !=
528         GNUNET_FS_uri_test_ksk (ret->keywords)) ||
529        (GNUNET_OK !=
530         GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
531        ( (chks != NULL) &&
532          ( (NULL == 
533             (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
534            (GNUNET_YES !=
535             GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
536        (GNUNET_OK !=
537         GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
538        (GNUNET_OK !=
539         GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
540        (GNUNET_OK !=
541         GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
542        (GNUNET_OK !=
543         GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
544        (GNUNET_OK !=
545         GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
546        (GNUNET_OK !=
547         GNUNET_BIO_read_int32 (rh, &ret->priority)) )
548     goto cleanup;
549   switch (b)
550     {
551     case 0: /* file-insert */
552       if (GNUNET_OK !=
553           GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
554         goto cleanup;
555       ret->is_directory = GNUNET_NO;
556       ret->data.file.do_index = GNUNET_NO;
557       ret->data.file.have_hash = GNUNET_NO;
558       ret->data.file.index_start_confirmed = GNUNET_NO;
559       if (GNUNET_NO == ret->is_published) 
560         {
561           if (NULL == ret->filename)
562             {
563               ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
564               ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
565               if (ret->data.file.reader_cls == NULL)
566                 goto cleanup;
567               if (GNUNET_OK !=
568                   GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
569                 goto cleanup;
570             }      
571           else
572             {
573               ret->data.file.reader = &GNUNET_FS_data_reader_file_;
574               ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
575             }
576         }
577       break;
578     case 1: /* file-index, no hash */
579       if (NULL == ret->filename)
580         goto cleanup;
581       if (GNUNET_OK !=
582           GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
583         goto cleanup;
584       ret->is_directory = GNUNET_NO;
585       ret->data.file.do_index = GNUNET_YES;
586       ret->data.file.have_hash = GNUNET_NO;
587       ret->data.file.index_start_confirmed = GNUNET_NO;
588       ret->data.file.reader = &GNUNET_FS_data_reader_file_;
589       ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
590       break;
591     case 2: /* file-index-with-hash */
592       if (NULL == ret->filename)
593         goto cleanup;
594       if ( (GNUNET_OK !=
595             GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
596            (GNUNET_OK !=
597             GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
598         goto cleanup;
599       ret->is_directory = GNUNET_NO;
600       ret->data.file.do_index = GNUNET_YES;
601       ret->data.file.have_hash = GNUNET_YES;
602       ret->data.file.index_start_confirmed = GNUNET_NO;
603       ret->data.file.reader = &GNUNET_FS_data_reader_file_;
604       ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
605       break;
606     case 3: /* file-index-with-hash-confirmed */
607       if (NULL == ret->filename)
608         goto cleanup;
609       if ( (GNUNET_OK !=
610             GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
611            (GNUNET_OK !=
612             GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
613         goto cleanup;
614
615       ret->is_directory = GNUNET_NO;
616       ret->data.file.do_index = GNUNET_YES;
617       ret->data.file.have_hash = GNUNET_YES;
618       ret->data.file.index_start_confirmed = GNUNET_YES;
619       ret->data.file.reader = &GNUNET_FS_data_reader_file_;
620       ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
621       break;
622     case 4: /* directory */
623       if ( (GNUNET_OK !=
624             GNUNET_BIO_read_int32 (rh, &dsize)) ||
625            (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
626            (GNUNET_OK !=
627             GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
628            (GNUNET_OK !=
629             GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
630         goto cleanup;
631       ret->data.dir.dir_size = (uint32_t) dsize;
632       ret->is_directory = GNUNET_YES;
633       if (filename != NULL)
634         {
635           ret->data.dir.entries = deserialize_file_information (h, filename);
636           GNUNET_free (filename);
637           filename = NULL;
638           nxt = ret->data.dir.entries;
639           while (nxt != NULL)
640             {
641               nxt->dir = ret;
642               nxt = nxt->next;
643             }  
644         }
645       break;
646     default:
647       GNUNET_break (0);
648       goto cleanup;
649     }
650   /* FIXME: adjust ret->start_time! */
651   ret->serialization = GNUNET_strdup (fn);
652   if (GNUNET_OK !=
653       GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
654     goto cleanup;  
655   if (filename != NULL)
656     {
657       ret->next = deserialize_file_information (h, filename);
658       GNUNET_free (filename);
659       filename = NULL;
660     }
661   GNUNET_free_non_null (ksks);
662   GNUNET_free_non_null (chks);
663   return ret;
664  cleanup:
665   GNUNET_free_non_null (ksks);
666   GNUNET_free_non_null (chks);
667   GNUNET_free_non_null (filename);
668   GNUNET_FS_file_information_destroy (ret, NULL, NULL);
669   return NULL;
670 }
671
672
673 /**
674  * Using the given serialization filename, try to deserialize
675  * the file-information tree associated with it.
676  *
677  * @param h master context
678  * @param filename name of the file (without directory) with
679  *        the infromation
680  * @return NULL on error
681  */
682 static struct GNUNET_FS_FileInformation *
683 deserialize_file_information (struct GNUNET_FS_Handle *h,
684                               const char *filename)
685 {
686   struct GNUNET_FS_FileInformation *ret;
687   struct GNUNET_BIO_ReadHandle *rh;
688   char *emsg;
689
690   rh = get_read_handle (h, "publish-fi", filename);
691   if (rh == NULL)
692     return NULL;
693   ret = deserialize_fi_node (h, filename, rh);
694   if (GNUNET_OK !=
695       GNUNET_BIO_read_close (rh, &emsg))
696     {
697       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698                   _("Failed to resume publishing information `%s': %s\n"),
699                   filename,
700                   emsg);
701       GNUNET_free (emsg);
702     }
703   return ret;
704 }
705
706
707 /**
708  * Given a serialization name (full absolute path), return the
709  * basename of the file (without the path), which must only
710  * consist of the 6 random characters.
711  * 
712  * @param fullname name to extract the basename from
713  * @return copy of the basename, NULL on error
714  */
715 static char *
716 get_serialization_short_name (const char *fullname)
717 {
718   const char *end;
719   const char *nxt;
720
721   end = NULL;
722   nxt = fullname;
723   /* FIXME: we could do this faster since we know
724      the length of 'end'... */
725   while ('\0' != nxt)
726     {
727       if (DIR_SEPARATOR == *nxt)
728         end = nxt + 1;
729       nxt++;
730     }
731   if ( (end == NULL) ||
732        (strlen (end) == 0) )
733     {
734       GNUNET_break (0);
735       return NULL;
736     }
737   GNUNET_break (6 == strlen (end));
738   return GNUNET_strdup (end);  
739 }
740
741
742 /**
743  * Create a new random name for serialization.  Also checks if persistence
744  * is enabled and returns NULL if not.
745  *
746  * @param h master context
747  * @param ext component of the path 
748  * @return NULL on errror
749  */
750 static char *
751 make_serialization_file_name (struct GNUNET_FS_Handle *h,
752                               const char *ext)
753 {
754   char *fn;
755   char *dn;
756   char *ret;
757
758   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
759     return NULL; /* persistence not requested */
760   dn = get_serialization_file_name (h, ext, "");
761   fn = GNUNET_DISK_mktemp (dn);
762   GNUNET_free (dn);
763   if (fn == NULL)
764     return NULL; /* epic fail */
765   ret = get_serialization_short_name (fn);
766   GNUNET_free (fn);
767   return ret;
768 }
769
770
771 /**
772  * Copy all of the data from the reader to the write handle.
773  *
774  * @param wh write handle
775  * @param fi file with reader
776  * @return GNUNET_OK on success
777  */
778 static int
779 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
780                   struct GNUNET_FS_FileInformation * fi)
781 {
782   char buf[32 * 1024];
783   uint64_t off;
784   size_t ret;
785   char *emsg;
786
787   emsg = NULL;
788   off = 0;
789   while (off < fi->data.file.file_size)
790     {
791       ret = fi->data.file.reader (fi->data.file.reader_cls,
792                                   off, sizeof (buf),
793                                   buf,
794                                   &emsg);
795       if (ret == 0)
796         {
797           GNUNET_free (emsg);
798           return GNUNET_SYSERR;
799         }
800       if (GNUNET_OK != 
801           GNUNET_BIO_write (wh, buf, ret))
802         return GNUNET_SYSERR;
803       off += ret;
804     }
805   return GNUNET_OK;
806 }
807
808
809 /**
810  * Create a temporary file on disk to store the current
811  * state of "fi" in.
812  *
813  * @param fi file information to sync with disk
814  */
815 void
816 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
817 {
818   char *fn;
819   struct GNUNET_BIO_WriteHandle *wh;
820   char b;
821   char *ksks;
822   char *chks;
823
824   if (NULL == fi->serialization)    
825     fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
826   if (NULL == fi->serialization)
827     return;
828   wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
829   if (wh == NULL)
830     {
831       GNUNET_free (fi->serialization);
832       fi->serialization = NULL;
833       return;
834     }
835   if (GNUNET_YES == fi->is_directory)
836     b = 4;
837   else if (GNUNET_YES == fi->data.file.index_start_confirmed)
838     b = 3;
839   else if (GNUNET_YES == fi->data.file.have_hash)
840     b = 2;
841   else if (GNUNET_YES == fi->data.file.do_index)
842     b = 1;
843   else
844     b = 0;
845   if (fi->keywords != NULL)
846     ksks = GNUNET_FS_uri_to_string (fi->keywords);
847   else
848     ksks = NULL;
849   if (fi->chk_uri != NULL)
850     chks = GNUNET_FS_uri_to_string (fi->chk_uri);
851   else
852     chks = NULL;
853   if ( (GNUNET_OK !=
854         GNUNET_BIO_write (wh, &b, sizeof (b))) ||
855        (GNUNET_OK != 
856         GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
857        (GNUNET_OK !=
858         GNUNET_BIO_write_string (wh, ksks)) ||
859        (GNUNET_OK !=
860         GNUNET_BIO_write_string (wh, chks)) ||
861        (GNUNET_OK != 
862         GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
863        (GNUNET_OK != 
864         GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
865        (GNUNET_OK !=
866         GNUNET_BIO_write_string (wh, fi->emsg)) ||
867        (GNUNET_OK !=
868         GNUNET_BIO_write_string (wh, fi->filename)) ||
869        (GNUNET_OK != 
870         GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
871        (GNUNET_OK != 
872         GNUNET_BIO_write_int32 (wh, fi->priority)) )
873     goto cleanup;
874   GNUNET_free_non_null (chks);
875   chks = NULL;
876   GNUNET_free_non_null (ksks);
877   ksks = NULL;
878   
879   switch (b)
880     {
881     case 0: /* file-insert */
882       if (GNUNET_OK !=
883           GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
884         goto cleanup;
885       if ( (GNUNET_NO == fi->is_published) &&
886            (NULL == fi->filename) )     
887         if (GNUNET_OK != 
888             copy_from_reader (wh, fi))
889           goto cleanup;
890       break;
891     case 1: /* file-index, no hash */
892       if (NULL == fi->filename)
893         goto cleanup;
894       if (GNUNET_OK !=
895           GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
896         goto cleanup;
897       break;
898     case 2: /* file-index-with-hash */
899     case 3: /* file-index-with-hash-confirmed */
900       if (NULL == fi->filename)
901         goto cleanup;
902       if ( (GNUNET_OK !=
903             GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
904            (GNUNET_OK !=
905             GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
906         goto cleanup;
907       break;
908     case 4: /* directory */
909       if ( (GNUNET_OK !=
910             GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
911            (GNUNET_OK !=
912             GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
913            (GNUNET_OK !=
914             GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
915         goto cleanup;
916       break;
917     default:
918       GNUNET_assert (0);
919       goto cleanup;
920     }
921   if (GNUNET_OK !=
922       GNUNET_BIO_write_string (wh, fi->next->serialization))
923     goto cleanup;  
924   if (GNUNET_OK ==
925       GNUNET_BIO_write_close (wh))
926     return; /* done! */
927  cleanup:
928   (void) GNUNET_BIO_write_close (wh);
929   GNUNET_free_non_null (chks);
930   GNUNET_free_non_null (ksks);
931   fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
932   if (0 != UNLINK (fn))
933     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
934   GNUNET_free (fn);
935   GNUNET_free (fi->serialization);
936   fi->serialization = NULL;  
937 }
938
939
940
941 /**
942  * Find the entry in the file information struct where the
943  * serialization filename matches the given name.
944  *
945  * @param pos file information to search
946  * @param srch filename to search for
947  * @return NULL if srch was not found in this subtree
948  */
949 static struct GNUNET_FS_FileInformation *
950 find_file_position (struct GNUNET_FS_FileInformation *pos,
951                     const char *srch)
952 {
953   struct GNUNET_FS_FileInformation *r;
954
955   while (pos != NULL)
956     {
957       if (0 == strcmp (srch,
958                        pos->serialization))
959         return pos;
960       if (pos->is_directory)
961         {
962           r = find_file_position (pos->data.dir.entries,
963                                   srch);
964           if (r != NULL)
965             return r;
966         }
967       pos = pos->next;
968     }
969   return NULL;
970 }
971
972
973 /**
974  * Signal the FS's progress function that we are resuming
975  * an upload.
976  *
977  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
978  * @param fi the entry in the publish-structure
979  * @param length length of the file or directory
980  * @param meta metadata for the file or directory (can be modified)
981  * @param uri pointer to the keywords that will be used for this entry (can be modified)
982  * @param anonymity pointer to selected anonymity level (can be modified)
983  * @param priority pointer to selected priority (can be modified)
984  * @param expirationTime pointer to selected expiration time (can be modified)
985  * @param client_info pointer to client context set upon creation (can be modified)
986  * @return GNUNET_OK to continue (always)
987  */
988 static int
989 fip_signal_resume(void *cls,
990                   struct GNUNET_FS_FileInformation *fi,
991                   uint64_t length,
992                   struct GNUNET_CONTAINER_MetaData *meta,
993                   struct GNUNET_FS_Uri **uri,
994                   uint32_t *anonymity,
995                   uint32_t *priority,
996                   struct GNUNET_TIME_Absolute *expirationTime,
997                   void **client_info)
998 {
999   struct GNUNET_FS_PublishContext *sc = cls;
1000   struct GNUNET_FS_ProgressInfo pi;
1001
1002   pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1003   pi.value.publish.specifics.resume.message = sc->fi->emsg;
1004   pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1005   *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1006   return GNUNET_OK;
1007 }
1008
1009
1010 /**
1011  * Function called with a filename of serialized publishing operation
1012  * to deserialize.
1013  *
1014  * @param cls the 'struct GNUNET_FS_Handle*'
1015  * @param filename complete filename (absolute path)
1016  * @return GNUNET_OK (continue to iterate)
1017  */
1018 static int
1019 deserialize_publish_file (void *cls,
1020                           const char *filename)
1021 {
1022   struct GNUNET_FS_Handle *h = cls;
1023   struct GNUNET_BIO_ReadHandle *rh;
1024   struct GNUNET_FS_PublishContext *pc;
1025   int32_t options;
1026   int32_t all_done;
1027   char *fi_root;
1028   char *ns;
1029   char *fi_pos;
1030   char *emsg;
1031
1032   pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1033   pc->h = h;
1034   pc->serialization = get_serialization_short_name (filename);
1035   fi_root = NULL;
1036   fi_pos = NULL;
1037   ns = NULL;
1038   rh = GNUNET_BIO_read_open (filename);
1039   if (rh == NULL)
1040     goto cleanup;
1041   if ( (GNUNET_OK !=
1042         GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1043        (GNUNET_OK !=
1044         GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1045        (GNUNET_OK !=
1046         GNUNET_BIO_read_int32 (rh, &options)) ||
1047        (GNUNET_OK !=
1048         GNUNET_BIO_read_int32 (rh, &all_done)) ||
1049        (GNUNET_OK !=
1050         GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1051        (GNUNET_OK !=
1052         GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1053        (GNUNET_OK !=
1054         GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1055     goto cleanup;          
1056   pc->options = options;
1057   pc->all_done = all_done;
1058   pc->fi = deserialize_file_information (h, fi_root);
1059   if (pc->fi == NULL)
1060     goto cleanup;    
1061   if (ns != NULL)
1062     {
1063       pc->namespace = GNUNET_FS_namespace_create (h, ns);
1064       if (pc->namespace == NULL)
1065         {
1066           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1067                       _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1068                       ns);
1069           goto cleanup;
1070         }
1071     }
1072   if (fi_pos != NULL)
1073     {
1074       pc->fi_pos = find_file_position (pc->fi,
1075                                        fi_pos);
1076       GNUNET_free (fi_pos);
1077       fi_pos = NULL;
1078       if (pc->fi_pos == NULL)
1079         {
1080           /* failed to find position for resuming, outch! Will start from root! */
1081           GNUNET_break (0);
1082           if (pc->all_done != GNUNET_YES)
1083             pc->fi_pos = pc->fi;
1084         }
1085     }
1086   /* generate RESUME event(s) */
1087   GNUNET_FS_file_information_inspect (pc->fi,
1088                                       &fip_signal_resume,
1089                                       pc);
1090   
1091   /* re-start publishing (if needed)... */
1092   if (pc->all_done != GNUNET_YES)
1093     pc->upload_task 
1094       = GNUNET_SCHEDULER_add_with_priority (h->sched,
1095                                             GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1096                                             &GNUNET_FS_publish_main_,
1097                                             pc);       
1098   if (GNUNET_OK !=
1099       GNUNET_BIO_read_close (rh, &emsg))
1100     {
1101       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1102                   _("Failure while resuming publishing operation `%s': %s\n"),
1103                   filename,
1104                   emsg);
1105       GNUNET_free (emsg);
1106     }
1107   GNUNET_free_non_null (ns);
1108   return GNUNET_OK;
1109  cleanup:
1110   GNUNET_free_non_null (pc->nid);
1111   GNUNET_free_non_null (pc->nuid);
1112   GNUNET_free_non_null (fi_root);
1113   GNUNET_free_non_null (fi_pos);
1114   GNUNET_free_non_null (ns);
1115   if ( (rh != NULL) &&
1116        (GNUNET_OK !=
1117         GNUNET_BIO_read_close (rh, &emsg)) )
1118     {
1119       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120                   _("Failed to resume publishing operation `%s': %s\n"),
1121                   filename,
1122                   emsg);
1123       GNUNET_free (emsg);
1124     }
1125   if (pc->fi != NULL)
1126     GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1127   if (pc->serialization != NULL)
1128     GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1129   GNUNET_free_non_null (pc->serialization);
1130   GNUNET_free (pc);
1131   return GNUNET_OK;
1132 }
1133
1134
1135 /**
1136  * Synchronize this publishing struct with its mirror
1137  * on disk.  Note that all internal FS-operations that change
1138  * publishing structs should already call "sync" internally,
1139  * so this function is likely not useful for clients.
1140  * 
1141  * @param pc the struct to sync
1142  */
1143 void
1144 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1145 {  
1146   struct GNUNET_BIO_WriteHandle *wh;
1147
1148   if (NULL == pc->serialization)
1149     pc->serialization = make_serialization_file_name (pc->h,
1150                                                       "publish");
1151   if (NULL == pc->serialization)
1152     return;
1153   if (NULL == pc->fi)
1154     return;
1155   if (NULL == pc->fi->serialization)
1156     {
1157       GNUNET_break (0);
1158       return;
1159     }
1160   wh = get_write_handle (pc->h, "publish", pc->serialization);
1161   if ( (GNUNET_OK !=
1162         GNUNET_BIO_write_string (wh, pc->nid)) ||
1163        (GNUNET_OK !=
1164         GNUNET_BIO_write_string (wh, pc->nuid)) ||
1165        (GNUNET_OK !=
1166         GNUNET_BIO_write_int32 (wh, pc->options)) ||
1167        (GNUNET_OK !=
1168         GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1169        (GNUNET_OK !=
1170         GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1171        (GNUNET_OK !=
1172         GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1173        (GNUNET_OK !=
1174         GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1175    {
1176      (void) GNUNET_BIO_write_close (wh);
1177      GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1178      GNUNET_free (pc->serialization);
1179      pc->serialization = NULL;
1180      return;
1181    }
1182  if (GNUNET_OK !=
1183      GNUNET_BIO_write_close (wh))
1184    {
1185      GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1186      GNUNET_free (pc->serialization);
1187      pc->serialization = NULL;
1188      return;     
1189    }  
1190 }
1191
1192
1193 /**
1194  * Synchronize this unindex struct with its mirror
1195  * on disk.  Note that all internal FS-operations that change
1196  * publishing structs should already call "sync" internally,
1197  * so this function is likely not useful for clients.
1198  * 
1199  * @param uc the struct to sync
1200  */
1201 void
1202 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1203 {
1204   struct GNUNET_BIO_WriteHandle *wh;
1205
1206   if (UNINDEX_STATE_ABORTED == uc->state)
1207     return;
1208   if (NULL == uc->serialization)
1209     uc->serialization = make_serialization_file_name (uc->h,
1210                                                       "unindex");
1211   if (NULL == uc->serialization)
1212     return;
1213   wh = get_write_handle (uc->h, "unindex", uc->serialization);
1214   if ( (GNUNET_OK !=
1215         GNUNET_BIO_write_string (wh, uc->filename)) ||
1216        (GNUNET_OK !=
1217         GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1218        (GNUNET_OK !=
1219         GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1220        (GNUNET_OK !=
1221         GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1222        ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1223          (GNUNET_OK !=
1224           GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1225        ( (uc->state == UNINDEX_STATE_ERROR) &&
1226          (GNUNET_OK !=
1227           GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1228     {
1229       (void) GNUNET_BIO_write_close (wh);
1230       GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1231       GNUNET_free (uc->serialization);
1232       uc->serialization = NULL;
1233       return;
1234     }
1235   if (GNUNET_OK !=
1236       GNUNET_BIO_write_close (wh))
1237     {
1238       GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1239       GNUNET_free (uc->serialization);
1240       uc->serialization = NULL;
1241       return;     
1242     }  
1243 }
1244
1245
1246 /**
1247  * Synchronize this download struct with its mirror
1248  * on disk.  Note that all internal FS-operations that change
1249  * publishing structs should already call "sync" internally,
1250  * so this function is likely not useful for clients.
1251  * 
1252  * @param dc the struct to sync
1253  */
1254 void
1255 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1256 {
1257   /* FIXME */
1258 }
1259
1260
1261 /**
1262  * Synchronize this search result with its mirror
1263  * on disk.  Note that all internal FS-operations that change
1264  * publishing structs should already call "sync" internally,
1265  * so this function is likely not useful for clients.
1266  * 
1267  * @param key key for the search result
1268  * @param sr the struct to sync
1269  */
1270 void
1271 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1272                                struct SearchResult *sr)
1273 {
1274   struct GNUNET_BIO_WriteHandle *wh;
1275   char *uris;
1276
1277   GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1278                   (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1279   uris = NULL;
1280   if (NULL == sr->serialization)
1281     sr->serialization = make_serialization_file_name (sr->sc->h,
1282                                                       "search-results");
1283   if (NULL == sr->serialization)
1284     return;
1285   wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1286   uris = GNUNET_FS_uri_to_string (sr->uri);
1287   if ( (GNUNET_OK !=
1288         GNUNET_BIO_write_string (wh, uris)) ||
1289        (GNUNET_OK !=
1290         GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1291        (GNUNET_OK !=
1292         GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1293        (GNUNET_OK !=
1294         GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1295        (GNUNET_OK !=
1296         GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1297        (GNUNET_OK !=
1298         GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1299        (GNUNET_OK !=
1300         GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1301     goto cleanup;   
1302   if (GNUNET_OK !=
1303       GNUNET_BIO_write_close (wh))
1304     {
1305       wh = NULL;
1306       goto cleanup;
1307     }
1308   GNUNET_free_non_null (uris);
1309   return;
1310  cleanup:
1311   GNUNET_free_non_null (uris);
1312   if (wh != NULL)
1313     (void)  GNUNET_BIO_write_close (wh);
1314   GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1315   GNUNET_free (sr->serialization);
1316   sr->serialization = NULL;
1317 }
1318
1319
1320 /**
1321  * Synchronize this search struct with its mirror
1322  * on disk.  Note that all internal FS-operations that change
1323  * publishing structs should already call "sync" internally,
1324  * so this function is likely not useful for clients.
1325  * 
1326  * @param sc the struct to sync
1327  */
1328 void
1329 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1330 {  
1331   struct GNUNET_BIO_WriteHandle *wh;
1332   struct GNUNET_FS_SearchContext *scc;
1333   char *uris;
1334   char in_pause;
1335
1336   if (NULL == sc->serialization)
1337     sc->serialization = make_serialization_file_name (sc->h,
1338                                                       "search");
1339   if (NULL == sc->serialization)
1340     return;
1341   wh = get_write_handle (sc->h, "search", sc->serialization);
1342   GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1343                   (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1344   uris = GNUNET_FS_uri_to_string (sc->uri);
1345   in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1346   if ( (GNUNET_OK !=
1347         GNUNET_BIO_write_string (wh, uris)) ||
1348        (GNUNET_OK !=
1349         GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1350        (GNUNET_OK !=
1351         GNUNET_BIO_write_string (wh, sc->emsg)) ||
1352        (GNUNET_OK !=
1353         GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1354        (GNUNET_OK !=
1355         GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1356        (GNUNET_OK !=
1357         GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1358     goto cleanup;          
1359   GNUNET_free (uris);
1360   uris = NULL;
1361   scc = sc->child_head;
1362   while (NULL != scc)
1363     {
1364       if (scc->serialization == NULL)
1365         break;
1366       if (GNUNET_OK !=
1367           GNUNET_BIO_write_string (wh, scc->serialization))
1368         goto cleanup;
1369       scc = scc->next;
1370     }
1371   GNUNET_BIO_write_string (wh, NULL);
1372   if (GNUNET_OK !=
1373       GNUNET_BIO_write_close (wh))
1374     {
1375       wh = NULL;
1376       goto cleanup;
1377     }
1378   return;
1379  cleanup:
1380   if (wh != NULL)
1381     (void) GNUNET_BIO_write_close (wh);
1382   GNUNET_free_non_null (uris);
1383   GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1384   GNUNET_free (sc->serialization);
1385   sc->serialization = NULL;
1386 }
1387
1388
1389 /**
1390  * Deserialize information about pending publish operations.
1391  *
1392  * @param h master context
1393  */
1394 static void
1395 deserialize_publish (struct GNUNET_FS_Handle *h)
1396 {
1397   char *dn;
1398
1399   dn = get_serialization_file_name (h, "publish", "");
1400   if (dn == NULL)
1401     return;
1402   GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1403   GNUNET_free (dn);
1404 }
1405
1406
1407 /**
1408  * Function called with a filename of serialized unindexing operation
1409  * to deserialize.
1410  *
1411  * @param cls the 'struct GNUNET_FS_Handle*'
1412  * @param filename complete filename (absolute path)
1413  * @return GNUNET_OK (continue to iterate)
1414  */
1415 static int
1416 deserialize_unindex_file (void *cls,
1417                           const char *filename)
1418 {
1419   struct GNUNET_FS_Handle *h = cls;
1420   struct GNUNET_BIO_ReadHandle *rh;
1421   struct GNUNET_FS_UnindexContext *uc;
1422   struct GNUNET_FS_ProgressInfo pi;
1423   char *emsg;
1424   uint32_t state;
1425
1426   uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1427   uc->h = h;
1428   uc->serialization = get_serialization_short_name (filename);
1429   rh = GNUNET_BIO_read_open (filename);
1430   if (rh == NULL)
1431     goto cleanup;
1432   if ( (GNUNET_OK !=
1433         GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1434        (GNUNET_OK !=
1435         GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1436        (GNUNET_OK !=
1437         GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1438        (GNUNET_OK !=
1439         GNUNET_BIO_read_int32 (rh, &state)) )
1440     goto cleanup;          
1441   uc->state = (enum UnindexState) state;
1442   switch (state)
1443     {
1444     case UNINDEX_STATE_HASHING:
1445       break;
1446     case UNINDEX_STATE_FS_NOTIFY:
1447       if (GNUNET_OK !=
1448           GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1449         goto cleanup;
1450       break;
1451     case UNINDEX_STATE_DS_REMOVE:
1452       break;
1453     case UNINDEX_STATE_COMPLETE:
1454       break;
1455     case UNINDEX_STATE_ERROR:
1456       if (GNUNET_OK !=
1457           GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1458         goto cleanup;
1459       break;
1460     case UNINDEX_STATE_ABORTED:
1461       GNUNET_break (0);
1462       goto cleanup;
1463     default:
1464       GNUNET_break (0);
1465       goto cleanup;
1466     }
1467   pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1468   pi.value.unindex.specifics.resume.message = uc->emsg;
1469   GNUNET_FS_unindex_make_status_ (&pi,
1470                                   uc,
1471                                   (uc->state == UNINDEX_STATE_COMPLETE) 
1472                                   ? uc->file_size
1473                                   : 0);
1474   switch (uc->state)
1475     {
1476     case UNINDEX_STATE_HASHING:
1477       GNUNET_CRYPTO_hash_file (uc->h->sched,
1478                                GNUNET_SCHEDULER_PRIORITY_IDLE,
1479                                uc->filename,
1480                                HASHING_BLOCKSIZE,
1481                                &GNUNET_FS_unindex_process_hash_,
1482                                uc);
1483       break;
1484     case UNINDEX_STATE_FS_NOTIFY:
1485       uc->state = UNINDEX_STATE_HASHING;
1486       GNUNET_FS_unindex_process_hash_ (uc,
1487                                        &uc->file_id);
1488       break;
1489     case UNINDEX_STATE_DS_REMOVE:
1490       GNUNET_FS_unindex_do_remove_ (uc);
1491       break;
1492     case UNINDEX_STATE_COMPLETE:
1493     case UNINDEX_STATE_ERROR:
1494       /* no need to resume any operation, we were done */
1495       break;
1496     default:
1497       break;
1498     }
1499   if (GNUNET_OK !=
1500       GNUNET_BIO_read_close (rh, &emsg))
1501     {
1502       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1503                   _("Failure while resuming unindexing operation `%s': %s\n"),
1504                   filename,
1505                   emsg);
1506       GNUNET_free (emsg);
1507     }
1508   return GNUNET_OK;
1509  cleanup:
1510   GNUNET_free_non_null (uc->filename);
1511   if ( (rh != NULL) &&
1512        (GNUNET_OK !=
1513         GNUNET_BIO_read_close (rh, &emsg)) )
1514     {
1515       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1516                   _("Failed to resume unindexing operation `%s': %s\n"),
1517                   filename,
1518                   emsg);
1519       GNUNET_free (emsg);
1520     }
1521   if (uc->serialization != NULL)
1522     GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1523   GNUNET_free_non_null (uc->serialization);
1524   GNUNET_free (uc);
1525   return GNUNET_OK;
1526 }
1527
1528
1529 /**
1530  * Deserialize information about pending publish operations.
1531  *
1532  * @param h master context
1533  */
1534 static void
1535 deserialize_unindex (struct GNUNET_FS_Handle *h)
1536 {
1537   char *dn;
1538
1539   dn = get_serialization_file_name (h, "unindex", "");
1540   if (dn == NULL)
1541     return;
1542   GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1543   GNUNET_free (dn);
1544 }
1545
1546
1547 /**
1548  * Function called with a filename of serialized search result
1549  * to deserialize.
1550  *
1551  * @param cls the 'struct GNUNET_FS_SearchContext*'
1552  * @param filename complete filename (absolute path)
1553  * @return GNUNET_OK (continue to iterate)
1554  */
1555 static int
1556 deserialize_search_result (void *cls,
1557                            const char *filename)
1558 {
1559   struct GNUNET_FS_SearchContext *sc = cls;
1560   char pbuf[32];
1561   char *ser;
1562   char *uris;
1563   char *emsg;
1564   struct GNUNET_BIO_ReadHandle *rh;
1565   struct SearchResult *sr;
1566   GNUNET_HashCode key;
1567
1568   ser = get_serialization_short_name (filename);
1569   rh = GNUNET_BIO_read_open (filename);
1570   if (rh == NULL)
1571     {
1572       if (ser != NULL)
1573         {
1574           GNUNET_snprintf (pbuf,
1575                            sizeof (pbuf),
1576                            "%s%s%s",
1577                            "search-results",
1578                            DIR_SEPARATOR_STR,
1579                            sc->serialization);
1580           GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1581           GNUNET_free (ser);
1582         }
1583       return GNUNET_OK;
1584     }
1585   emsg = NULL;
1586   uris = NULL;
1587   sr = GNUNET_malloc (sizeof (struct SearchResult));
1588   sr->serialization = ser;  
1589   if ( (GNUNET_OK !=
1590         GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1591        (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||       
1592        ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1593          (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1594        (GNUNET_OK !=
1595         GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1596        (GNUNET_OK !=
1597         GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1598        (GNUNET_OK !=
1599         GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1600        (GNUNET_OK !=
1601         GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1602        (GNUNET_OK !=
1603         GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1604        (GNUNET_OK !=
1605         GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1606     goto cleanup;   
1607   GNUNET_free (uris);
1608   GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1609                                      &key,
1610                                      sr,
1611                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1612   return GNUNET_OK;
1613  cleanup:
1614   GNUNET_free_non_null (emsg);
1615   GNUNET_free_non_null (uris);
1616   if (sr->uri != NULL)
1617     GNUNET_FS_uri_destroy (sr->uri);
1618   if (sr->meta != NULL)
1619     GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1620   GNUNET_free (sr->serialization);
1621   GNUNET_free (sr);  
1622   return GNUNET_OK;
1623 }
1624
1625
1626 /**
1627  * Iterator over search results signaling resume to the client for
1628  * each result.
1629  *
1630  * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1631  * @param key current key code
1632  * @param value value in the hash map, the 'struct SearchResult'
1633  * @return GNUNET_YES (we should continue to iterate)
1634  */
1635 static int
1636 signal_result_resume (void *cls,
1637                       const GNUNET_HashCode * key,
1638                       void *value)
1639 {
1640   struct GNUNET_FS_SearchContext *sc = cls;
1641   struct GNUNET_FS_ProgressInfo pi;
1642   struct SearchResult *sr = value;
1643
1644   if (0 == sr->mandatory_missing)
1645     {
1646       pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1647       pi.value.search.specifics.resume_result.meta = sr->meta;
1648       pi.value.search.specifics.resume_result.uri = sr->uri;
1649       pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1650       pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1651       pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1652       sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1653                                                        sc);
1654     }
1655   GNUNET_FS_search_start_probe_ (sr);
1656   return GNUNET_YES;
1657 }
1658
1659
1660 /**
1661  * Iterator over search results freeing each.
1662  *
1663  * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1664  * @param key current key code
1665  * @param value value in the hash map, the 'struct SearchResult'
1666  * @return GNUNET_YES (we should continue to iterate)
1667  */
1668 static int
1669 free_result (void *cls,
1670              const GNUNET_HashCode * key,
1671              void *value)
1672 {
1673   struct SearchResult *sr = value;
1674
1675   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1676   GNUNET_FS_uri_destroy (sr->uri);
1677   GNUNET_free (sr);
1678   return GNUNET_YES;
1679 }
1680
1681
1682 /**
1683  * Free memory allocated by the search context and its children
1684  *
1685  * @param sc search context to free
1686  */
1687 static void
1688 free_search_context (struct GNUNET_FS_SearchContext *sc)
1689 {
1690   struct GNUNET_FS_SearchContext *scc;
1691
1692   while (NULL != (scc = sc->child_head))
1693     {
1694       GNUNET_CONTAINER_DLL_remove (sc->child_head,
1695                                    sc->child_tail,
1696                                    scc);      
1697       free_search_context (scc);
1698     }
1699   GNUNET_free_non_null (sc->emsg);
1700   if (sc->serialization != NULL)
1701     GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1702   /* FIXME: remove 'pbuf' directory with search results as well! */
1703   GNUNET_free_non_null (sc->serialization);
1704   if (sc->uri != NULL)
1705     GNUNET_FS_uri_destroy (sc->uri);
1706   if (sc->master_result_map != NULL)
1707     {
1708       GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1709                                              &free_result,
1710                                              sc);
1711       GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1712     }
1713   GNUNET_free (sc);
1714 }
1715
1716
1717 /**
1718  * Deserialize a download.
1719  *
1720  * @param h overall context
1721  * @param rh file to deserialize from
1722  * @param parent parent download
1723  * @param serialization name under which the search was serialized
1724  */
1725 static void
1726 deserialize_download (struct GNUNET_FS_Handle *h,
1727                       struct GNUNET_BIO_ReadHandle *rh,
1728                       struct GNUNET_FS_DownloadContext *parent,
1729                       const char *serialization);
1730
1731
1732 /**
1733  * Function called with a filename of serialized sub-download
1734  * to deserialize.
1735  *
1736  * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
1737  * @param filename complete filename (absolute path)
1738  * @return GNUNET_OK (continue to iterate)
1739  */
1740 static int
1741 deserialize_subdownload (void *cls,
1742                          const char *filename)
1743 {
1744   struct GNUNET_FS_DownloadContext *parent = cls;
1745   char *ser;
1746   char *emsg;
1747   struct GNUNET_BIO_ReadHandle *rh;
1748
1749   ser = get_serialization_short_name (filename);
1750   rh = GNUNET_BIO_read_open (filename);
1751   deserialize_download (parent->h,
1752                         rh,
1753                         parent,
1754                         ser);
1755   if (GNUNET_OK !=
1756       GNUNET_BIO_read_close (rh, &emsg))
1757     {
1758       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1759                   _("Failed to resume sub-download `%s': %s\n"),
1760                   ser,
1761                   emsg);
1762       GNUNET_free (emsg);
1763     }
1764   GNUNET_free (ser);
1765   return GNUNET_OK;
1766 }
1767
1768
1769 /**
1770  * Deserialize a download.
1771  *
1772  * @param h overall context
1773  * @param rh file to deserialize from
1774  * @param parent parent download
1775  * @param serialization name under which the search was serialized
1776  */
1777 static void
1778 deserialize_download (struct GNUNET_FS_Handle *h,
1779                       struct GNUNET_BIO_ReadHandle *rh,
1780                       struct GNUNET_FS_DownloadContext *parent,
1781                       const char *serialization)
1782 {
1783   struct GNUNET_FS_DownloadContext *dc;
1784   struct GNUNET_FS_DownloadContext *dcc;
1785   char pbuf[32];
1786   struct GNUNET_FS_ProgressInfo pi;
1787   char *emsg;
1788   char *uris;
1789   char *dn;
1790
1791   uris = NULL;
1792   emsg = NULL;
1793   dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
1794   dc->parent = parent;
1795   dc->h = h;
1796   dc->serialization = GNUNET_strdup (serialization);
1797 #if 0
1798   /* FIXME */
1799   if ( (GNUNET_OK !=
1800         GNUNET_BIO_read_string (rh, "-uri", &uris, 10*1024)) ||
1801        (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||       
1802        ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1803          (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1804        (GNUNET_OK !=
1805         GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1806        (GNUNET_OK !=
1807         GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1808        (GNUNET_OK !=
1809         GNUNET_BIO_read_int32 (rh, &options)) ||
1810        (GNUNET_OK !=
1811         GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1812        (GNUNET_OK !=
1813         GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1814     goto cleanup;          
1815   /* FIXME: adjust start_time.value */
1816   sc->options = (enum GNUNET_FS_SearchOptions) options;
1817   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1818 #endif
1819   GNUNET_snprintf (pbuf,
1820                    sizeof (pbuf),
1821                    "%s%s%s",
1822                    "subdownloads",
1823                    DIR_SEPARATOR_STR,
1824                    dc->serialization);
1825   dn = get_serialization_file_name (h, pbuf, "");
1826   if (dn != NULL)
1827     {
1828       GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
1829       GNUNET_free (dn);
1830     }
1831 #if 0
1832   if ('\0' == in_pause)
1833     {
1834       if (GNUNET_OK !=
1835           GNUNET_FS_search_start_searching_ (sc))
1836         goto cleanup;
1837     }
1838 #endif
1839   if (0)
1840     goto cleanup;
1841   if (parent != NULL)
1842     GNUNET_CONTAINER_DLL_insert (parent->child_head,
1843                                  parent->child_tail,
1844                                  dc);
1845   pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1846 #if 0
1847   pi.value.search.specifics.resume.message = sc->emsg;
1848   pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1849 #endif
1850   GNUNET_FS_download_make_status_ (&pi,
1851                                    dc);
1852   dcc = dc->child_head;
1853   while (NULL != dcc)
1854     {
1855       /* FIXME: wrong, need recursion! */
1856       pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1857 #if 0
1858       pi.value.search.specifics.resume.message = scc->emsg;
1859       pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1860 #endif
1861       GNUNET_FS_download_make_status_ (&pi,
1862                                        dcc);
1863       dcc = dcc->next;
1864     }
1865 #if 0
1866   GNUNET_free (uris);
1867 #endif
1868   return;
1869  cleanup:
1870 #if 0
1871   GNUNET_free_non_null (emsg);
1872   free_search_context (sc);
1873 #endif
1874   GNUNET_free_non_null (uris);
1875 }
1876
1877
1878 /**
1879  * Deserialize a search. 
1880  *
1881  * @param h overall context
1882  * @param rh file to deserialize from
1883  * @param parent parent search
1884  * @param serialization name under which the search was serialized
1885  */
1886 static struct GNUNET_FS_SearchContext *
1887 deserialize_search (struct GNUNET_FS_Handle *h,
1888                     struct GNUNET_BIO_ReadHandle *rh,
1889                     struct GNUNET_FS_SearchContext *parent,
1890                     const char *serialization)
1891 {
1892   struct GNUNET_FS_SearchContext *sc;
1893   struct GNUNET_FS_SearchContext *scc;
1894   struct GNUNET_BIO_ReadHandle *rhc;
1895   char pbuf[32];
1896   struct GNUNET_FS_ProgressInfo pi;
1897   char *emsg;
1898   char *uris;
1899   char *child_ser;
1900   char *dn;
1901   uint32_t options;
1902   char in_pause;
1903
1904   uris = NULL;
1905   emsg = NULL;
1906   sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1907   sc->parent = parent;
1908   sc->h = h;
1909   sc->serialization = GNUNET_strdup (serialization);
1910   if ( (GNUNET_OK !=
1911         GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
1912        (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||       
1913        ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1914          (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1915        (GNUNET_OK !=
1916         GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1917        (GNUNET_OK !=
1918         GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1919        (GNUNET_OK !=
1920         GNUNET_BIO_read_int32 (rh, &options)) ||
1921        (GNUNET_OK !=
1922         GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1923        (GNUNET_OK !=
1924         GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1925     goto cleanup;          
1926   /* FIXME: adjust start_time.value */
1927   sc->options = (enum GNUNET_FS_SearchOptions) options;
1928   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1929   GNUNET_snprintf (pbuf,
1930                    sizeof (pbuf),
1931                    "%s%s%s",
1932                    "search-results",
1933                    DIR_SEPARATOR_STR,
1934                    sc->serialization);
1935   dn = get_serialization_file_name (h, pbuf, "");
1936   if (dn != NULL)
1937     {
1938       GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
1939       GNUNET_free (dn);
1940     }
1941   if ('\0' == in_pause)
1942     {
1943       if (GNUNET_OK !=
1944           GNUNET_FS_search_start_searching_ (sc))
1945         goto cleanup;
1946     }
1947   while (1)
1948     {
1949       if ( (GNUNET_OK !=
1950             GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
1951         goto cleanup;
1952       if (child_ser == NULL)
1953         break;    
1954       rhc = get_read_handle (h, "search-children", child_ser);
1955       if (rhc != NULL)
1956         {
1957           scc = deserialize_search (h, rhc, sc, child_ser);
1958           if (scc != NULL)          
1959             GNUNET_CONTAINER_DLL_insert (sc->child_head,
1960                                          sc->child_tail,
1961                                          scc);      
1962           emsg = NULL;
1963           if (GNUNET_OK !=
1964               GNUNET_BIO_read_close (rhc, &emsg))
1965             {
1966               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1967                           _("Failed to resume sub-search `%s': %s\n"),
1968                           child_ser,
1969                           emsg);
1970               GNUNET_free (emsg);
1971             }
1972         }    
1973       GNUNET_free (child_ser);  
1974     }
1975   if (parent != NULL)
1976     GNUNET_CONTAINER_DLL_insert (parent->child_head,
1977                                  parent->child_tail,
1978                                  sc);
1979   pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1980   pi.value.search.specifics.resume.message = sc->emsg;
1981   pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1982   sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1983                                                    sc);
1984   scc = sc->child_head;
1985   while (NULL != scc)
1986     {
1987       /* FIXME: wrong, need recursion! */
1988       pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1989       pi.value.search.specifics.resume.message = scc->emsg;
1990       pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1991       scc->client_info = GNUNET_FS_search_make_status_ (&pi,
1992                                                         scc);
1993
1994       scc = scc->next;
1995     }
1996   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1997                                          &signal_result_resume,
1998                                          sc);
1999   GNUNET_free (uris);
2000   return sc;
2001  cleanup:
2002   GNUNET_free_non_null (emsg);
2003   free_search_context (sc);
2004   GNUNET_free_non_null (uris);
2005   return NULL;
2006 }
2007
2008
2009 /**
2010  * Function called with a filename of serialized search operation
2011  * to deserialize.
2012  *
2013  * @param cls the 'struct GNUNET_FS_Handle*'
2014  * @param filename complete filename (absolute path)
2015  * @return GNUNET_OK (continue to iterate)
2016  */
2017 static int
2018 deserialize_search_file (void *cls,
2019                           const char *filename)
2020 {
2021   struct GNUNET_FS_Handle *h = cls;
2022   char *ser;
2023   char *emsg;
2024   struct GNUNET_BIO_ReadHandle *rh;
2025   struct GNUNET_FS_SearchContext *sc;
2026
2027   ser = get_serialization_short_name (filename);
2028   rh = GNUNET_BIO_read_open (filename);
2029   if (rh == NULL)
2030     {
2031       if (ser != NULL)
2032         {
2033           GNUNET_FS_remove_sync_file_ (h, "search", ser);
2034           GNUNET_free (ser);
2035         }
2036       return GNUNET_OK;
2037     }
2038   sc = deserialize_search (h, rh, NULL, ser);
2039   GNUNET_free (ser);
2040   if (GNUNET_OK !=
2041       GNUNET_BIO_read_close (rh, &emsg))
2042     {
2043       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2044                   _("Failure while resuming search operation `%s': %s\n"),
2045                   filename,
2046                   emsg);
2047       GNUNET_free (emsg);
2048     }
2049   return GNUNET_OK;
2050 }
2051
2052
2053 /**
2054  * Deserialize information about pending search operations.
2055  *
2056  * @param h master context
2057  */
2058 static void
2059 deserialize_search_master (struct GNUNET_FS_Handle *h)
2060 {
2061   char *dn;
2062
2063   dn = get_serialization_file_name (h, "search", "");
2064   if (dn == NULL)
2065     return;
2066   GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
2067   GNUNET_free (dn);
2068 }
2069
2070
2071 /**
2072  * Function called with a filename of serialized download operation
2073  * to deserialize.
2074  *
2075  * @param cls the 'struct GNUNET_FS_Handle*'
2076  * @param filename complete filename (absolute path)
2077  * @return GNUNET_OK (continue to iterate)
2078  */
2079 static int
2080 deserialize_download_file (void *cls,
2081                            const char *filename)
2082 {
2083   struct GNUNET_FS_Handle *h = cls;
2084   char *ser;
2085   char *emsg;
2086   struct GNUNET_BIO_ReadHandle *rh;
2087
2088   ser = get_serialization_short_name (filename);
2089   rh = GNUNET_BIO_read_open (filename);
2090   if (rh == NULL)
2091     {
2092       if (ser != NULL)
2093         {
2094           GNUNET_FS_remove_sync_file_ (h, "download", ser);
2095           GNUNET_free (ser);
2096         }
2097       return GNUNET_OK;
2098     }
2099   deserialize_download (h, rh, NULL, ser);
2100   GNUNET_free (ser);
2101   if (GNUNET_OK !=
2102       GNUNET_BIO_read_close (rh, &emsg))
2103     {
2104       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2105                   _("Failure while resuming download operation `%s': %s\n"),
2106                   filename,
2107                   emsg);
2108       GNUNET_free (emsg);
2109     }
2110   return GNUNET_OK;
2111 }
2112
2113
2114 /**
2115  * Deserialize information about pending download operations.
2116  *
2117  * @param h master context
2118  */
2119 static void
2120 deserialize_download_master (struct GNUNET_FS_Handle *h)
2121 {
2122   char *dn;
2123
2124   dn = get_serialization_file_name (h, "download", "");
2125   if (dn == NULL)
2126     return;
2127   GNUNET_DISK_directory_scan (dn, &deserialize_download_file, h);
2128   GNUNET_free (dn);
2129 }
2130
2131
2132 /**
2133  * Setup a connection to the file-sharing service.
2134  *
2135  * @param sched scheduler to use
2136  * @param cfg configuration to use
2137  * @param client_name unique identifier for this client 
2138  * @param upcb function to call to notify about FS actions
2139  * @param upcb_cls closure for upcb
2140  * @param flags specific attributes for fs-operations
2141  * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2142  * @return NULL on error
2143  */
2144 struct GNUNET_FS_Handle *
2145 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2146                  const struct GNUNET_CONFIGURATION_Handle *cfg,
2147                  const char *client_name,
2148                  GNUNET_FS_ProgressCallback upcb,
2149                  void *upcb_cls,
2150                  enum GNUNET_FS_Flags flags,
2151                  ...)
2152 {
2153   struct GNUNET_FS_Handle *ret;
2154   struct GNUNET_CLIENT_Connection *client;
2155   enum GNUNET_FS_OPTIONS opt;
2156   va_list ap;
2157
2158   client = GNUNET_CLIENT_connect (sched,
2159                                   "fs",
2160                                   cfg);
2161   if (NULL == client)
2162     return NULL;
2163   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2164   ret->sched = sched;
2165   ret->cfg = cfg;
2166   ret->client_name = GNUNET_strdup (client_name);
2167   ret->upcb = upcb;
2168   ret->upcb_cls = upcb_cls;
2169   ret->client = client;
2170   ret->flags = flags;
2171   ret->max_parallel_downloads = 1;
2172   ret->max_parallel_requests = 1;
2173   ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2174   va_start (ap, flags);  
2175   while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2176     {
2177       switch (opt)
2178         {
2179         case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2180           ret->max_parallel_downloads = va_arg (ap, unsigned int);
2181           break;
2182         case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2183           ret->max_parallel_requests = va_arg (ap, unsigned int);
2184           break;
2185         default:
2186           GNUNET_break (0);
2187           GNUNET_free (ret->client_name);
2188           GNUNET_free (ret);
2189           va_end (ap);
2190           return NULL;
2191         }
2192     }
2193   va_end (ap);
2194   // FIXME: setup receive-loop with client (do we need one?)
2195   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2196     {
2197       /* FIXME: could write one generic deserialization
2198          function instead of these four... */
2199       deserialize_publish (ret);
2200       deserialize_search_master (ret);
2201       deserialize_download_master (ret);
2202       deserialize_unindex (ret);
2203     }
2204   return ret;
2205 }
2206
2207
2208 /**
2209  * Close our connection with the file-sharing service.
2210  * The callback given to GNUNET_FS_start will no longer be
2211  * called after this function returns.
2212  *
2213  * @param h handle that was returned from GNUNET_FS_start
2214  */                    
2215 void 
2216 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2217 {
2218   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
2219     {
2220       // FIXME: generate SUSPEND events and clean up state!
2221     }
2222   // FIXME: terminate receive-loop with client  (do we need one?)
2223   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2224     GNUNET_SCHEDULER_cancel (h->sched,
2225                              h->queue_job);
2226   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
2227   GNUNET_free (h->client_name);
2228   GNUNET_free (h);
2229 }
2230
2231
2232 /* end of fs.c */