6556fee18857f67f042af849fe31a12eea59a865
[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 search result 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 key key for the search result
1253  * @param sr the struct to sync
1254  */
1255 void
1256 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1257                                struct SearchResult *sr)
1258 {
1259   struct GNUNET_BIO_WriteHandle *wh;
1260   char *uris;
1261
1262   GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1263                   (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1264   uris = NULL;
1265   if (NULL == sr->serialization)
1266     sr->serialization = make_serialization_file_name (sr->sc->h,
1267                                                       "search-results");
1268   if (NULL == sr->serialization)
1269     return;
1270   wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1271   uris = GNUNET_FS_uri_to_string (sr->uri);
1272   if ( (GNUNET_OK !=
1273         GNUNET_BIO_write_string (wh, uris)) ||
1274        (GNUNET_OK !=
1275         GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1276        (GNUNET_OK !=
1277         GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1278        (GNUNET_OK !=
1279         GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1280        (GNUNET_OK !=
1281         GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1282        (GNUNET_OK !=
1283         GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1284        (GNUNET_OK !=
1285         GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1286     goto cleanup;   
1287   if (GNUNET_OK !=
1288       GNUNET_BIO_write_close (wh))
1289     {
1290       wh = NULL;
1291       goto cleanup;
1292     }
1293   GNUNET_free_non_null (uris);
1294   return;
1295  cleanup:
1296   GNUNET_free_non_null (uris);
1297   if (wh != NULL)
1298     (void)  GNUNET_BIO_write_close (wh);
1299   GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1300   GNUNET_free (sr->serialization);
1301   sr->serialization = NULL;
1302 }
1303
1304
1305 /**
1306  * Synchronize this search struct with its mirror
1307  * on disk.  Note that all internal FS-operations that change
1308  * publishing structs should already call "sync" internally,
1309  * so this function is likely not useful for clients.
1310  * 
1311  * @param sc the struct to sync
1312  */
1313 void
1314 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1315 {  
1316   struct GNUNET_BIO_WriteHandle *wh;
1317   struct GNUNET_FS_SearchContext *scc;
1318   char *uris;
1319   char in_pause;
1320
1321   if (NULL == sc->serialization)
1322     sc->serialization = make_serialization_file_name (sc->h,
1323                                                       "search");
1324   if (NULL == sc->serialization)
1325     return;
1326   wh = get_write_handle (sc->h, "search", sc->serialization);
1327   GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1328                   (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1329   uris = GNUNET_FS_uri_to_string (sc->uri);
1330   in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1331   if ( (GNUNET_OK !=
1332         GNUNET_BIO_write_string (wh, uris)) ||
1333        (GNUNET_OK !=
1334         GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1335        (GNUNET_OK !=
1336         GNUNET_BIO_write_string (wh, sc->emsg)) ||
1337        (GNUNET_OK !=
1338         GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1339        (GNUNET_OK !=
1340         GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1341        (GNUNET_OK !=
1342         GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1343     goto cleanup;          
1344   GNUNET_free (uris);
1345   uris = NULL;
1346   scc = sc->child_head;
1347   while (NULL != scc)
1348     {
1349       if (scc->serialization == NULL)
1350         break;
1351       if (GNUNET_OK !=
1352           GNUNET_BIO_write_string (wh, scc->serialization))
1353         goto cleanup;
1354       scc = scc->next;
1355     }
1356   GNUNET_BIO_write_string (wh, NULL);
1357   if (GNUNET_OK !=
1358       GNUNET_BIO_write_close (wh))
1359     {
1360       wh = NULL;
1361       goto cleanup;
1362     }
1363   return;
1364  cleanup:
1365   if (wh != NULL)
1366     (void) GNUNET_BIO_write_close (wh);
1367   GNUNET_free_non_null (uris);
1368   GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1369   GNUNET_free (sc->serialization);
1370   sc->serialization = NULL;
1371 }
1372
1373
1374 /**
1375  * Deserialize information about pending publish operations.
1376  *
1377  * @param h master context
1378  */
1379 static void
1380 deserialize_publish (struct GNUNET_FS_Handle *h)
1381 {
1382   char *dn;
1383
1384   dn = get_serialization_file_name (h, "publish", "");
1385   if (dn == NULL)
1386     return;
1387   GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1388   GNUNET_free (dn);
1389 }
1390
1391
1392 /**
1393  * Function called with a filename of serialized unindexing operation
1394  * to deserialize.
1395  *
1396  * @param cls the 'struct GNUNET_FS_Handle*'
1397  * @param filename complete filename (absolute path)
1398  * @return GNUNET_OK (continue to iterate)
1399  */
1400 static int
1401 deserialize_unindex_file (void *cls,
1402                           const char *filename)
1403 {
1404   struct GNUNET_FS_Handle *h = cls;
1405   struct GNUNET_BIO_ReadHandle *rh;
1406   struct GNUNET_FS_UnindexContext *uc;
1407   struct GNUNET_FS_ProgressInfo pi;
1408   char *emsg;
1409   uint32_t state;
1410
1411   uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1412   uc->h = h;
1413   uc->serialization = get_serialization_short_name (filename);
1414   rh = GNUNET_BIO_read_open (filename);
1415   if (rh == NULL)
1416     goto cleanup;
1417   if ( (GNUNET_OK !=
1418         GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1419        (GNUNET_OK !=
1420         GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1421        (GNUNET_OK !=
1422         GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1423        (GNUNET_OK !=
1424         GNUNET_BIO_read_int32 (rh, &state)) )
1425     goto cleanup;          
1426   uc->state = (enum UnindexState) state;
1427   switch (state)
1428     {
1429     case UNINDEX_STATE_HASHING:
1430       break;
1431     case UNINDEX_STATE_FS_NOTIFY:
1432       if (GNUNET_OK !=
1433           GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1434         goto cleanup;
1435       break;
1436     case UNINDEX_STATE_DS_REMOVE:
1437       break;
1438     case UNINDEX_STATE_COMPLETE:
1439       break;
1440     case UNINDEX_STATE_ERROR:
1441       if (GNUNET_OK !=
1442           GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1443         goto cleanup;
1444       break;
1445     case UNINDEX_STATE_ABORTED:
1446       GNUNET_break (0);
1447       goto cleanup;
1448     default:
1449       GNUNET_break (0);
1450       goto cleanup;
1451     }
1452   pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1453   pi.value.unindex.specifics.resume.message = uc->emsg;
1454   GNUNET_FS_unindex_make_status_ (&pi,
1455                                   uc,
1456                                   (uc->state == UNINDEX_STATE_COMPLETE) 
1457                                   ? uc->file_size
1458                                   : 0);
1459   switch (uc->state)
1460     {
1461     case UNINDEX_STATE_HASHING:
1462       GNUNET_CRYPTO_hash_file (uc->h->sched,
1463                                GNUNET_SCHEDULER_PRIORITY_IDLE,
1464                                uc->filename,
1465                                HASHING_BLOCKSIZE,
1466                                &GNUNET_FS_unindex_process_hash_,
1467                                uc);
1468       break;
1469     case UNINDEX_STATE_FS_NOTIFY:
1470       uc->state = UNINDEX_STATE_HASHING;
1471       GNUNET_FS_unindex_process_hash_ (uc,
1472                                        &uc->file_id);
1473       break;
1474     case UNINDEX_STATE_DS_REMOVE:
1475       GNUNET_FS_unindex_do_remove_ (uc);
1476       break;
1477     case UNINDEX_STATE_COMPLETE:
1478     case UNINDEX_STATE_ERROR:
1479       /* no need to resume any operation, we were done */
1480       break;
1481     default:
1482       break;
1483     }
1484   if (GNUNET_OK !=
1485       GNUNET_BIO_read_close (rh, &emsg))
1486     {
1487       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1488                   _("Failure while resuming unindexing operation `%s': %s\n"),
1489                   filename,
1490                   emsg);
1491       GNUNET_free (emsg);
1492     }
1493   return GNUNET_OK;
1494  cleanup:
1495   GNUNET_free_non_null (uc->filename);
1496   if ( (rh != NULL) &&
1497        (GNUNET_OK !=
1498         GNUNET_BIO_read_close (rh, &emsg)) )
1499     {
1500       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501                   _("Failed to resume unindexing operation `%s': %s\n"),
1502                   filename,
1503                   emsg);
1504       GNUNET_free (emsg);
1505     }
1506   if (uc->serialization != NULL)
1507     GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1508   GNUNET_free_non_null (uc->serialization);
1509   GNUNET_free (uc);
1510   return GNUNET_OK;
1511 }
1512
1513
1514 /**
1515  * Deserialize information about pending publish operations.
1516  *
1517  * @param h master context
1518  */
1519 static void
1520 deserialize_unindex (struct GNUNET_FS_Handle *h)
1521 {
1522   char *dn;
1523
1524   dn = get_serialization_file_name (h, "unindex", "");
1525   if (dn == NULL)
1526     return;
1527   GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1528   GNUNET_free (dn);
1529 }
1530
1531
1532 /**
1533  * Function called with a filename of serialized search result
1534  * to deserialize.
1535  *
1536  * @param cls the 'struct GNUNET_FS_SearchContext*'
1537  * @param filename complete filename (absolute path)
1538  * @return GNUNET_OK (continue to iterate)
1539  */
1540 static int
1541 deserialize_search_result (void *cls,
1542                            const char *filename)
1543 {
1544   struct GNUNET_FS_SearchContext *sc = cls;
1545   char pbuf[32];
1546   char *ser;
1547   char *uris;
1548   char *emsg;
1549   struct GNUNET_BIO_ReadHandle *rh;
1550   struct SearchResult *sr;
1551   GNUNET_HashCode key;
1552
1553   ser = get_serialization_short_name (filename);
1554   rh = GNUNET_BIO_read_open (filename);
1555   if (rh == NULL)
1556     {
1557       if (ser != NULL)
1558         {
1559           GNUNET_snprintf (pbuf,
1560                            sizeof (pbuf),
1561                            "%s%s%s",
1562                            "search-results",
1563                            DIR_SEPARATOR_STR,
1564                            sc->serialization);
1565           GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1566           GNUNET_free (ser);
1567         }
1568       return GNUNET_OK;
1569     }
1570   emsg = NULL;
1571   uris = NULL;
1572   sr = GNUNET_malloc (sizeof (struct SearchResult));
1573   sr->serialization = ser;  
1574   if ( (GNUNET_OK !=
1575         GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1576        (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||       
1577        ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1578          (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1579        (GNUNET_OK !=
1580         GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1581        (GNUNET_OK !=
1582         GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1583        (GNUNET_OK !=
1584         GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1585        (GNUNET_OK !=
1586         GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1587        (GNUNET_OK !=
1588         GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1589        (GNUNET_OK !=
1590         GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1591     goto cleanup;   
1592   GNUNET_free (uris);
1593   GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1594                                      &key,
1595                                      sr,
1596                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1597   return GNUNET_OK;
1598  cleanup:
1599   GNUNET_free_non_null (emsg);
1600   GNUNET_free_non_null (uris);
1601   if (sr->uri != NULL)
1602     GNUNET_FS_uri_destroy (sr->uri);
1603   if (sr->meta != NULL)
1604     GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1605   GNUNET_free (sr->serialization);
1606   GNUNET_free (sr);  
1607   return GNUNET_OK;
1608 }
1609
1610
1611 /**
1612  * Iterator over search results signaling resume to the client for
1613  * each result.
1614  *
1615  * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1616  * @param key current key code
1617  * @param value value in the hash map, the 'struct SearchResult'
1618  * @return GNUNET_YES (we should continue to iterate)
1619  */
1620 static int
1621 signal_result_resume (void *cls,
1622                       const GNUNET_HashCode * key,
1623                       void *value)
1624 {
1625   struct GNUNET_FS_SearchContext *sc = cls;
1626   struct GNUNET_FS_ProgressInfo pi;
1627   struct SearchResult *sr = value;
1628
1629   if (0 == sr->mandatory_missing)
1630     {
1631       pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1632       pi.value.search.specifics.resume_result.meta = sr->meta;
1633       pi.value.search.specifics.resume_result.uri = sr->uri;
1634       pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1635       pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1636       pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1637       sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1638                                                        sc);
1639     }
1640   GNUNET_FS_search_start_probe_ (sr);
1641   return GNUNET_YES;
1642 }
1643
1644
1645 /**
1646  * Iterator over search results freeing each.
1647  *
1648  * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1649  * @param key current key code
1650  * @param value value in the hash map, the 'struct SearchResult'
1651  * @return GNUNET_YES (we should continue to iterate)
1652  */
1653 static int
1654 free_result (void *cls,
1655              const GNUNET_HashCode * key,
1656              void *value)
1657 {
1658   struct SearchResult *sr = value;
1659
1660   GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1661   GNUNET_FS_uri_destroy (sr->uri);
1662   GNUNET_free (sr);
1663   return GNUNET_YES;
1664 }
1665
1666
1667 /**
1668  * Free memory allocated by the search context and its children
1669  *
1670  * @param sc search context to free
1671  */
1672 static void
1673 free_search_context (struct GNUNET_FS_SearchContext *sc)
1674 {
1675   struct GNUNET_FS_SearchContext *scc;
1676
1677   while (NULL != (scc = sc->child_head))
1678     {
1679       GNUNET_CONTAINER_DLL_remove (sc->child_head,
1680                                    sc->child_tail,
1681                                    scc);      
1682       free_search_context (scc);
1683     }
1684   GNUNET_free_non_null (sc->emsg);
1685   if (sc->serialization != NULL)
1686     GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1687   /* FIXME: remove 'pbuf' directory with search results as well! */
1688   GNUNET_free_non_null (sc->serialization);
1689   if (sc->uri != NULL)
1690     GNUNET_FS_uri_destroy (sc->uri);
1691   if (sc->master_result_map != NULL)
1692     {
1693       GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1694                                              &free_result,
1695                                              sc);
1696       GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1697     }
1698   GNUNET_free (sc);
1699 }
1700
1701
1702 /**
1703  * Deserialize a search. 
1704  *
1705  * @param h overall context
1706  * @param rh file to deserialize from
1707  * @param parent parent search
1708  * @param serialization name under which the search was serialized
1709  */
1710 static struct GNUNET_FS_SearchContext *
1711 deserialize_search (struct GNUNET_FS_Handle *h,
1712                     struct GNUNET_BIO_ReadHandle *rh,
1713                     struct GNUNET_FS_SearchContext *parent,
1714                     const char *serialization)
1715 {
1716   struct GNUNET_FS_SearchContext *sc;
1717   struct GNUNET_FS_SearchContext *scc;
1718   struct GNUNET_BIO_ReadHandle *rhc;
1719   char pbuf[32];
1720   struct GNUNET_FS_ProgressInfo pi;
1721   char *emsg;
1722   char *uris;
1723   char *child_ser;
1724   char *dn;
1725   uint32_t options;
1726   char in_pause;
1727
1728   uris = NULL;
1729   emsg = NULL;
1730   sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1731   sc->parent = parent;
1732   sc->h = h;
1733   sc->serialization = GNUNET_strdup (serialization);
1734   if ( (GNUNET_OK !=
1735         GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
1736        (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||       
1737        ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1738          (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1739        (GNUNET_OK !=
1740         GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1741        (GNUNET_OK !=
1742         GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1743        (GNUNET_OK !=
1744         GNUNET_BIO_read_int32 (rh, &options)) ||
1745        (GNUNET_OK !=
1746         GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1747        (GNUNET_OK !=
1748         GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1749     goto cleanup;          
1750   /* FIXME: adjust start_time.value */
1751   sc->options = (enum GNUNET_FS_SearchOptions) options;
1752   sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1753   GNUNET_snprintf (pbuf,
1754                    sizeof (pbuf),
1755                    "%s%s%s",
1756                    "search-results",
1757                    DIR_SEPARATOR_STR,
1758                    sc->serialization);
1759   dn = get_serialization_file_name (h, pbuf, "");
1760   if (dn != NULL)
1761     {
1762       GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
1763       GNUNET_free (dn);
1764     }
1765   if ('\0' == in_pause)
1766     {
1767       if (GNUNET_OK !=
1768           GNUNET_FS_search_start_searching_ (sc))
1769         goto cleanup;
1770     }
1771   while (1)
1772     {
1773       if ( (GNUNET_OK !=
1774             GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
1775         goto cleanup;
1776       if (child_ser == NULL)
1777         break;    
1778       rhc = get_read_handle (h, "search-children", child_ser);
1779       if (rhc != NULL)
1780         {
1781           scc = deserialize_search (h, rhc, sc, child_ser);
1782           if (scc != NULL)          
1783             GNUNET_CONTAINER_DLL_insert (sc->child_head,
1784                                          sc->child_tail,
1785                                          scc);      
1786           emsg = NULL;
1787           if (GNUNET_OK !=
1788               GNUNET_BIO_read_close (rhc, &emsg))
1789             {
1790               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1791                           _("Failed to resume sub-search `%s': %s\n"),
1792                           child_ser,
1793                           emsg);
1794               GNUNET_free (emsg);
1795             }
1796         }    
1797       GNUNET_free (child_ser);  
1798     }
1799   if (parent != NULL)
1800     GNUNET_CONTAINER_DLL_insert (parent->child_head,
1801                                  parent->child_tail,
1802                                  sc);
1803   pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1804   pi.value.search.specifics.resume.message = sc->emsg;
1805   pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1806   sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1807                                                    sc);
1808   scc = sc->child_head;
1809   while (NULL != scc)
1810     {
1811       pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1812       pi.value.search.specifics.resume.message = scc->emsg;
1813       pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1814       scc->client_info = GNUNET_FS_search_make_status_ (&pi,
1815                                                         scc);
1816
1817       scc = scc->next;
1818     }
1819   GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1820                                          &signal_result_resume,
1821                                          sc);
1822   GNUNET_free (uris);
1823   return sc;
1824  cleanup:
1825   GNUNET_free_non_null (emsg);
1826   free_search_context (sc);
1827   GNUNET_free_non_null (uris);
1828   return NULL;
1829 }
1830
1831
1832 /**
1833  * Function called with a filename of serialized search operation
1834  * to deserialize.
1835  *
1836  * @param cls the 'struct GNUNET_FS_Handle*'
1837  * @param filename complete filename (absolute path)
1838  * @return GNUNET_OK (continue to iterate)
1839  */
1840 static int
1841 deserialize_search_file (void *cls,
1842                           const char *filename)
1843 {
1844   struct GNUNET_FS_Handle *h = cls;
1845   char *ser;
1846   char *emsg;
1847   struct GNUNET_BIO_ReadHandle *rh;
1848   struct GNUNET_FS_SearchContext *sc;
1849
1850   ser = get_serialization_short_name (filename);
1851   rh = GNUNET_BIO_read_open (filename);
1852   if (rh == NULL)
1853     {
1854       if (ser != NULL)
1855         {
1856           GNUNET_FS_remove_sync_file_ (h, "search", ser);
1857           GNUNET_free (ser);
1858         }
1859       return GNUNET_OK;
1860     }
1861   sc = deserialize_search (h, rh, NULL, ser);
1862   GNUNET_free (ser);
1863   if (GNUNET_OK !=
1864       GNUNET_BIO_read_close (rh, &emsg))
1865     {
1866       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1867                   _("Failure while resuming unindexing operation `%s': %s\n"),
1868                   filename,
1869                   emsg);
1870       GNUNET_free (emsg);
1871     }
1872   return GNUNET_OK;
1873 }
1874
1875
1876 /**
1877  * Deserialize information about pending search operations.
1878  *
1879  * @param h master context
1880  */
1881 static void
1882 deserialize_search_master (struct GNUNET_FS_Handle *h)
1883 {
1884   char *dn;
1885
1886   dn = get_serialization_file_name (h, "search", "");
1887   if (dn == NULL)
1888     return;
1889   GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
1890   GNUNET_free (dn);
1891 }
1892
1893
1894 /**
1895  * Setup a connection to the file-sharing service.
1896  *
1897  * @param sched scheduler to use
1898  * @param cfg configuration to use
1899  * @param client_name unique identifier for this client 
1900  * @param upcb function to call to notify about FS actions
1901  * @param upcb_cls closure for upcb
1902  * @param flags specific attributes for fs-operations
1903  * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1904  * @return NULL on error
1905  */
1906 struct GNUNET_FS_Handle *
1907 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1908                  const struct GNUNET_CONFIGURATION_Handle *cfg,
1909                  const char *client_name,
1910                  GNUNET_FS_ProgressCallback upcb,
1911                  void *upcb_cls,
1912                  enum GNUNET_FS_Flags flags,
1913                  ...)
1914 {
1915   struct GNUNET_FS_Handle *ret;
1916   struct GNUNET_CLIENT_Connection *client;
1917   enum GNUNET_FS_OPTIONS opt;
1918   va_list ap;
1919
1920   client = GNUNET_CLIENT_connect (sched,
1921                                   "fs",
1922                                   cfg);
1923   if (NULL == client)
1924     return NULL;
1925   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1926   ret->sched = sched;
1927   ret->cfg = cfg;
1928   ret->client_name = GNUNET_strdup (client_name);
1929   ret->upcb = upcb;
1930   ret->upcb_cls = upcb_cls;
1931   ret->client = client;
1932   ret->flags = flags;
1933   ret->max_parallel_downloads = 1;
1934   ret->max_parallel_requests = 1;
1935   ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1936   va_start (ap, flags);  
1937   while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1938     {
1939       switch (opt)
1940         {
1941         case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1942           ret->max_parallel_downloads = va_arg (ap, unsigned int);
1943           break;
1944         case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1945           ret->max_parallel_requests = va_arg (ap, unsigned int);
1946           break;
1947         default:
1948           GNUNET_break (0);
1949           GNUNET_free (ret->client_name);
1950           GNUNET_free (ret);
1951           va_end (ap);
1952           return NULL;
1953         }
1954     }
1955   va_end (ap);
1956   // FIXME: setup receive-loop with client (do we need one?)
1957   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1958     {
1959       /* FIXME: could write one generic deserialization
1960          function instead of these four... */
1961       deserialize_publish (ret);
1962       deserialize_search_master (ret);
1963       /* FIXME: deserialize downloads that are NOT part of searches */
1964       deserialize_unindex (ret);
1965     }
1966   return ret;
1967 }
1968
1969
1970 /**
1971  * Close our connection with the file-sharing service.
1972  * The callback given to GNUNET_FS_start will no longer be
1973  * called after this function returns.
1974  *
1975  * @param h handle that was returned from GNUNET_FS_start
1976  */                    
1977 void 
1978 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1979 {
1980   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1981     {
1982       // FIXME: generate SUSPEND events and clean up state!
1983     }
1984   // FIXME: terminate receive-loop with client  (do we need one?)
1985   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1986     GNUNET_SCHEDULER_cancel (h->sched,
1987                              h->queue_job);
1988   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1989   GNUNET_free (h->client_name);
1990   GNUNET_free (h);
1991 }
1992
1993
1994 /* end of fs.c */