e14f0570a1389bcdbe0a3222cc1281cdd062d9f7
[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
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       /* FIXME: might be faster/simpler to do this calculation only once
131          when we start a job (OTOH, this would allow us to dynamically
132          and easily adjust qe->blocks over time, given the right API...) */
133       run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
134                                                 qe->blocks * qe->start_times);
135       end_time = GNUNET_TIME_absolute_add (qe->start_time,
136                                            run_time);
137       rst = GNUNET_TIME_absolute_get_remaining (end_time);
138       restart_at = GNUNET_TIME_relative_min (rst, restart_at);
139       if (rst.value > 0)
140         continue;       
141       stop_job (qe);
142     }
143   h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
144                                                restart_at,
145                                                &process_job_queue,
146                                                h);
147 }
148
149
150 /**
151  * Add a job to the queue.
152  *
153  * @param h handle to the overall FS state
154  * @param start function to call to begin the job
155  * @param stop function to call to pause the job, or on dequeue (if the job was running)
156  * @param cls closure for start and stop
157  * @param blocks number of blocks this jobs uses
158  * @return queue handle
159  */
160 struct GNUNET_FS_QueueEntry *
161 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
162                   GNUNET_FS_QueueStart start,
163                   GNUNET_FS_QueueStop stop,
164                   void *cls,
165                   unsigned int blocks)
166 {
167   struct GNUNET_FS_QueueEntry *qe;
168
169   qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
170   qe->h = h;
171   qe->start = start;
172   qe->stop = stop;
173   qe->cls = cls;
174   qe->queue_time = GNUNET_TIME_absolute_get ();
175   qe->blocks = blocks;
176   GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
177                                      h->pending_tail,
178                                      h->pending_tail,
179                                      qe);
180   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
181     GNUNET_SCHEDULER_cancel (h->sched,
182                              h->queue_job);
183   h->queue_job 
184     = GNUNET_SCHEDULER_add_now (h->sched,
185                                 &process_job_queue,
186                                 h);
187   return qe;
188 }
189
190
191 /**
192  * Dequeue a job from the queue.
193  * @param qh handle for the job
194  */
195 void
196 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
197 {
198   struct GNUNET_FS_Handle *h;
199
200   h = qh->h;
201   if (qh->client != NULL)    
202     stop_job (qh);    
203   GNUNET_CONTAINER_DLL_remove (h->pending_head,
204                                h->pending_tail,
205                                qh);
206   GNUNET_free (qh);
207   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
208     GNUNET_SCHEDULER_cancel (h->sched,
209                              h->queue_job);
210   h->queue_job 
211     = GNUNET_SCHEDULER_add_now (h->sched,
212                                 &process_job_queue,
213                                 h);
214 }
215
216
217 /**
218  * Return the full filename where we would store state information
219  * (for serialization/deserialization).
220  *
221  * @param h master context
222  * @param ext component of the path 
223  * @param ent entity identifier (or emtpy string for the directory)
224  * @return NULL on error
225  */
226 static char *
227 get_serialization_file_name (struct GNUNET_FS_Handle *h,
228                              const char *ext,
229                              const char *ent)
230 {
231   char *basename;
232   char *ret;
233   if (GNUNET_OK !=
234       GNUNET_CONFIGURATION_get_value_filename (h->cfg,
235                                                "fs",
236                                                "STATE_DIR",
237                                                &basename))
238     return NULL;
239   GNUNET_asprintf (&ret,
240                    "%s%s%s-%s%s%s",
241                    basename,
242                    DIR_SEPARATOR_STR,
243                    h->client_name,
244                    ext,
245                    DIR_SEPARATOR_STR,
246                    ent);
247   GNUNET_free (basename);
248   return ret;
249 }
250
251
252 /**
253  * Return a read handle for deserialization.
254  *
255  * @param h master context
256  * @param ext component of the path 
257  * @param ent entity identifier (or emtpy string for the directory)
258  * @return NULL on error
259  */
260 static struct GNUNET_BIO_ReadHandle *
261 get_read_handle (struct GNUNET_FS_Handle *h,
262                  const char *ext,
263                  const char *ent)
264 {
265   char *fn;
266   struct GNUNET_BIO_ReadHandle *ret;
267
268   fn = get_serialization_file_name (h, ext, ent);
269   if (fn == NULL)
270     return NULL;
271   ret = GNUNET_BIO_read_open (fn);
272   GNUNET_free (fn);
273   return ret;
274 }
275
276
277 /**
278  * Using the given serialization filename, try to deserialize
279  * the file-information tree associated with it.
280  *
281  * @param h master context
282  * @param filename name of the file (without directory) with
283  *        the infromation
284  * @return NULL on error
285  */
286 static struct GNUNET_FS_FileInformation *
287 deserialize_file_information (struct GNUNET_FS_Handle *h,
288                               const char *filename);
289
290
291 /**
292  * Using the given serialization filename, try to deserialize
293  * the file-information tree associated with it.
294  *
295  * @param h master context
296  * @param fn name of the file (without directory) with
297  *        the infromation
298  * @param rh handle for reading
299  * @return NULL on error
300  */
301 static struct GNUNET_FS_FileInformation *
302 deserialize_fi_node (struct GNUNET_FS_Handle *h,
303                      const char *fn,
304                      struct GNUNET_BIO_ReadHandle *rh)
305 {
306   struct GNUNET_FS_FileInformation *ret;
307   struct GNUNET_FS_FileInformation *nxt;
308   char b;
309   char *ksks;
310   char *chks;
311   char *filename;
312   uint32_t dsize;
313
314   if (GNUNET_OK !=
315       GNUNET_BIO_read (rh, "termination flag", &b, sizeof(b)))
316     {
317       GNUNET_break (0);
318       return NULL;
319     }
320   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
321   ksks = NULL;
322   chks = NULL;
323   filename = NULL;
324   if ( (GNUNET_OK !=
325         GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
326        (GNUNET_OK !=
327         GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
328        (NULL == 
329         (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
330        (GNUNET_YES !=
331         GNUNET_FS_uri_test_ksk (ret->keywords)) ||
332        (GNUNET_OK !=
333         GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
334        ( (chks != NULL) &&
335          ( (NULL == 
336             (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
337            (GNUNET_YES !=
338             GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
339        (GNUNET_OK !=
340         GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
341        (GNUNET_OK !=
342         GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
343        (GNUNET_OK !=
344         GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
345        (GNUNET_OK !=
346         GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
347        (GNUNET_OK !=
348         GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
349        (GNUNET_OK !=
350         GNUNET_BIO_read_int32 (rh, &ret->priority)) )
351     goto cleanup;
352   switch (b)
353     {
354     case 0: /* file-insert */
355       if (GNUNET_OK !=
356           GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
357         goto cleanup;
358       ret->is_directory = GNUNET_NO;
359       ret->data.file.do_index = GNUNET_NO;
360       ret->data.file.have_hash = GNUNET_NO;
361       ret->data.file.index_start_confirmed = GNUNET_NO;
362       /* FIXME: what's our approach for dealing with the
363          'reader' and 'reader_cls' fields?  I guess the only
364          good way would be to dump "small" files into 
365          'rh' and to not support serialization of "large"
366          files (!?) */
367       break;
368     case 1: /* file-index, no hash */
369       if (GNUNET_OK !=
370           GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
371         goto cleanup;
372       ret->is_directory = GNUNET_NO;
373       ret->data.file.do_index = GNUNET_YES;
374       ret->data.file.have_hash = GNUNET_NO;
375       ret->data.file.index_start_confirmed = GNUNET_NO;
376       /* FIXME: what's our approach for dealing with the
377          'reader' and 'reader_cls' fields? 
378          (should be easy for indexing since we must have a file) */
379       break;
380     case 2: /* file-index-with-hash */
381       if ( (GNUNET_OK !=
382             GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
383            (GNUNET_OK !=
384             GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
385         goto cleanup;
386       ret->is_directory = GNUNET_NO;
387       ret->data.file.do_index = GNUNET_YES;
388       ret->data.file.have_hash = GNUNET_YES;
389       ret->data.file.index_start_confirmed = GNUNET_NO;
390       /* FIXME: what's our approach for dealing with the
391          'reader' and 'reader_cls' fields? 
392          (should be easy for indexing since we must have a file) */
393       break;
394     case 3: /* file-index-with-hash-confirmed */
395       if ( (GNUNET_OK !=
396             GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
397            (GNUNET_OK !=
398             GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
399         goto cleanup;
400       ret->is_directory = GNUNET_NO;
401       ret->data.file.do_index = GNUNET_YES;
402       ret->data.file.have_hash = GNUNET_YES;
403       ret->data.file.index_start_confirmed = GNUNET_YES;
404       /* FIXME: what's our approach for dealing with the
405          'reader' and 'reader_cls' fields? 
406          (should be easy for indexing since we must have a file) */
407       break;
408     case 4: /* directory */
409       if ( (GNUNET_OK !=
410             GNUNET_BIO_read_int32 (rh, &dsize)) ||
411            (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
412            (GNUNET_OK !=
413             GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
414            (GNUNET_OK !=
415             GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
416         goto cleanup;
417       ret->data.dir.dir_size = (uint32_t) dsize;
418       ret->is_directory = GNUNET_YES;
419       if (filename != NULL)
420         {
421           ret->data.dir.entries = deserialize_file_information (h, filename);
422           GNUNET_free (filename);
423           filename = NULL;
424           nxt = ret->data.dir.entries;
425           while (nxt != NULL)
426             {
427               nxt->dir = ret;
428               nxt = nxt->next;
429             }  
430         }
431       break;
432     default:
433       GNUNET_break (0);
434       goto cleanup;
435     }
436   /* FIXME: adjust ret->start_time! */
437   ret->serialization = GNUNET_strdup (fn);
438   if (GNUNET_OK !=
439       GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
440     goto cleanup;  
441   if (filename != NULL)
442     {
443       ret->next = deserialize_file_information (h, filename);
444       GNUNET_free (filename);
445       filename = NULL;
446     }
447   return ret;
448  cleanup:
449   GNUNET_free_non_null (ksks);
450   GNUNET_free_non_null (chks);
451   GNUNET_free_non_null (filename);
452   GNUNET_FS_file_information_destroy (ret, NULL, NULL);
453   return NULL;
454    
455 }
456
457
458 /**
459  * Using the given serialization filename, try to deserialize
460  * the file-information tree associated with it.
461  *
462  * @param h master context
463  * @param filename name of the file (without directory) with
464  *        the infromation
465  * @return NULL on error
466  */
467 static struct GNUNET_FS_FileInformation *
468 deserialize_file_information (struct GNUNET_FS_Handle *h,
469                               const char *filename)
470 {
471   struct GNUNET_FS_FileInformation *ret;
472   struct GNUNET_BIO_ReadHandle *rh;
473   char *emsg;
474
475   rh = get_read_handle (h, "publish-fi", filename);
476   if (rh == NULL)
477     return NULL;
478   ret = deserialize_fi_node (h, filename, rh);
479   if (GNUNET_OK !=
480       GNUNET_BIO_read_close (rh, &emsg))
481     {
482       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
483                   _("Failed to resume publishing information `%s': %s\n"),
484                   filename,
485                   emsg);
486       GNUNET_free (emsg);
487     }
488   return ret;
489 }
490
491
492 /**
493  * Find the entry in the file information struct where the
494  * serialization filename matches the given name.
495  *
496  * @param pos file information to search
497  * @param srch filename to search for
498  * @return NULL if srch was not found in this subtree
499  */
500 static struct GNUNET_FS_FileInformation *
501 find_file_position (struct GNUNET_FS_FileInformation *pos,
502                     const char *srch)
503 {
504   struct GNUNET_FS_FileInformation *r;
505
506   while (pos != NULL)
507     {
508       if (0 == strcmp (srch,
509                        pos->serialization))
510         return pos;
511       if (pos->is_directory)
512         {
513           r = find_file_position (pos->data.dir.entries,
514                                   srch);
515           if (r != NULL)
516             return r;
517         }
518       pos = pos->next;
519     }
520   return NULL;
521 }
522
523
524 /**
525  * Signal the FS's progress function that we are resuming
526  * an upload.
527  *
528  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
529  * @param fi the entry in the publish-structure
530  * @param length length of the file or directory
531  * @param meta metadata for the file or directory (can be modified)
532  * @param uri pointer to the keywords that will be used for this entry (can be modified)
533  * @param anonymity pointer to selected anonymity level (can be modified)
534  * @param priority pointer to selected priority (can be modified)
535  * @param expirationTime pointer to selected expiration time (can be modified)
536  * @param client_info pointer to client context set upon creation (can be modified)
537  * @return GNUNET_OK to continue (always)
538  */
539 static int
540 fip_signal_resume(void *cls,
541                   struct GNUNET_FS_FileInformation *fi,
542                   uint64_t length,
543                   struct GNUNET_CONTAINER_MetaData *meta,
544                   struct GNUNET_FS_Uri **uri,
545                   uint32_t *anonymity,
546                   uint32_t *priority,
547                   struct GNUNET_TIME_Absolute *expirationTime,
548                   void **client_info)
549 {
550   struct GNUNET_FS_PublishContext *sc = cls;
551   struct GNUNET_FS_ProgressInfo pi;
552
553   pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
554   pi.value.publish.specifics.resume.message = sc->fi->emsg;
555   pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
556   *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
557   return GNUNET_OK;
558 }
559
560
561 /**
562  * Function called with a filename of serialized publishing operation
563  * to deserialize.
564  *
565  * @param cls the 'struct GNUNET_FS_Handle*'
566  * @param filename complete filename (absolute path)
567  * @return GNUNET_OK (continue to iterate)
568  */
569 static int
570 deserialize_publish_file (void *cls,
571                           const char *filename)
572 {
573   struct GNUNET_FS_Handle *h = cls;
574   struct GNUNET_BIO_ReadHandle *rh;
575   struct GNUNET_FS_PublishContext *pc;
576   int32_t options;
577   int32_t all_done;
578   char *fi_root;
579   char *ns;
580   char *fi_pos;
581   char *emsg;
582
583   rh = GNUNET_BIO_read_open (filename);
584   if (rh == NULL)
585     {
586       if (0 != UNLINK (filename))
587         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
588                                   "unlink", 
589                                   filename);
590       return GNUNET_OK;
591     }
592   while (1)
593     {
594       fi_root = NULL;
595       fi_pos = NULL;
596       ns = NULL;
597       pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
598       pc->h = h;
599       if ( (GNUNET_OK !=
600             GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
601            (GNUNET_OK !=
602             GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
603            (GNUNET_OK !=
604             GNUNET_BIO_read_int32 (rh, &options)) ||
605            (GNUNET_OK !=
606             GNUNET_BIO_read_int32 (rh, &all_done)) ||
607            (GNUNET_OK !=
608             GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
609            (GNUNET_OK !=
610             GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
611            (GNUNET_OK !=
612             GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
613         {
614           GNUNET_free_non_null (pc->nid);
615           GNUNET_free_non_null (pc->nuid);
616           GNUNET_free_non_null (fi_root);
617           GNUNET_free_non_null (ns);
618           GNUNET_free (pc);
619           break;
620         }      
621        pc->options = options;
622        pc->all_done = all_done;
623        pc->fi = deserialize_file_information (h, fi_root);
624        if (pc->fi == NULL)
625          {
626            GNUNET_free_non_null (pc->nid);
627            GNUNET_free_non_null (pc->nuid);
628            GNUNET_free_non_null (fi_root);
629            GNUNET_free_non_null (ns);
630            GNUNET_free (pc);
631            continue;
632          }
633        if (ns != NULL)
634          {
635            pc->namespace = GNUNET_FS_namespace_create (h, ns);
636            if (pc->namespace == NULL)
637              {
638                GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639                            _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
640                            ns);
641                GNUNET_free_non_null (pc->nid);
642                GNUNET_free_non_null (pc->nuid);
643                GNUNET_free_non_null (fi_root);
644                GNUNET_free_non_null (ns);
645                GNUNET_free (pc);
646                continue;
647              }
648          }
649        if (fi_pos != NULL)
650          {
651            pc->fi_pos = find_file_position (pc->fi,
652                                             fi_pos);
653            GNUNET_free (fi_pos);
654            if (pc->fi_pos == NULL)
655              {
656                /* failed to find position for resuming, outch! Will start from root! */
657                GNUNET_break (0);
658                if (pc->all_done != GNUNET_YES)
659                  pc->fi_pos = pc->fi;
660              }
661          }
662        pc->serialization = GNUNET_strdup (filename);
663        /* generate RESUME event(s) */
664        GNUNET_FS_file_information_inspect (pc->fi,
665                                            &fip_signal_resume,
666                                            pc);
667        
668        /* re-start publishing (if needed)... */
669        if (pc->all_done != GNUNET_YES)
670          pc->upload_task 
671            = GNUNET_SCHEDULER_add_with_priority (h->sched,
672                                                  GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
673                                                  &GNUNET_FS_publish_main_,
674                                                  pc);       
675     }
676   if (GNUNET_OK !=
677       GNUNET_BIO_read_close (rh, &emsg))
678     {
679       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680                   _("Failed to resume publishing operation `%s': %s\n"),
681                   filename,
682                   emsg);
683       GNUNET_free (emsg);
684     }
685   return GNUNET_OK;
686 }
687
688
689 /**
690  * Deserialize information about pending publish operations.
691  *
692  * @param h master context
693  */
694 static void
695 deserialize_publish (struct GNUNET_FS_Handle *h)
696 {
697   char *dn;
698
699   dn = get_serialization_file_name (h, "publish", "");
700   if (dn == NULL)
701     return;
702   GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
703   GNUNET_free (dn);
704 }
705
706
707 /**
708  * Setup a connection to the file-sharing service.
709  *
710  * @param sched scheduler to use
711  * @param cfg configuration to use
712  * @param client_name unique identifier for this client 
713  * @param upcb function to call to notify about FS actions
714  * @param upcb_cls closure for upcb
715  * @param flags specific attributes for fs-operations
716  * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
717  * @return NULL on error
718  */
719 struct GNUNET_FS_Handle *
720 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
721                  const struct GNUNET_CONFIGURATION_Handle *cfg,
722                  const char *client_name,
723                  GNUNET_FS_ProgressCallback upcb,
724                  void *upcb_cls,
725                  enum GNUNET_FS_Flags flags,
726                  ...)
727 {
728   struct GNUNET_FS_Handle *ret;
729   struct GNUNET_CLIENT_Connection *client;
730   enum GNUNET_FS_OPTIONS opt;
731   va_list ap;
732
733   client = GNUNET_CLIENT_connect (sched,
734                                   "fs",
735                                   cfg);
736   if (NULL == client)
737     return NULL;
738   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
739   ret->sched = sched;
740   ret->cfg = cfg;
741   ret->client_name = GNUNET_strdup (client_name);
742   ret->upcb = upcb;
743   ret->upcb_cls = upcb_cls;
744   ret->client = client;
745   ret->flags = flags;
746   ret->max_parallel_downloads = 1;
747   ret->max_parallel_requests = 1;
748   ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
749   va_start (ap, flags);  
750   while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
751     {
752       switch (opt)
753         {
754         case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
755           ret->max_parallel_downloads = va_arg (ap, unsigned int);
756           break;
757         case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
758           ret->max_parallel_requests = va_arg (ap, unsigned int);
759           break;
760         default:
761           GNUNET_break (0);
762           GNUNET_free (ret->client_name);
763           GNUNET_free (ret);
764           va_end (ap);
765           return NULL;
766         }
767     }
768   va_end (ap);
769   // FIXME: setup receive-loop with client (do we need one?)
770
771   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
772     {
773       deserialize_publish (ret);
774       /* FIXME: not implemented! */
775       // Deserialize Search:
776       // * read search queries
777       // * for each query, read file with search results
778       // * for each search result with active download, deserialize download
779       // * for each directory search result, check for active downloads of contents
780       // Deserialize Download:
781       // * always part of search???
782       // Deserialize Unindex:
783       // * read FNs for unindex with progress offset
784     }
785   return ret;
786 }
787
788
789 /**
790  * Close our connection with the file-sharing service.
791  * The callback given to GNUNET_FS_start will no longer be
792  * called after this function returns.
793  *
794  * @param h handle that was returned from GNUNET_FS_start
795  */                    
796 void 
797 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
798 {
799   if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
800     {
801       // FIXME: generate SUSPEND events and clean up state!
802     }
803   // FIXME: terminate receive-loop with client  (do we need one?)
804   if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
805     GNUNET_SCHEDULER_cancel (h->sched,
806                              h->queue_job);
807   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
808   GNUNET_free (h->client_name);
809   GNUNET_free (h);
810 }
811
812
813 /* end of fs.c */