-export FS handle during events, as we might generate events duing FS_start and clien...
[oweals/gnunet.git] / src / fs / fs_publish.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/fs_publish.c
23  * @brief publish a file or directory in GNUnet
24  * @see https://gnunet.org/encoding
25  * @author Krista Bennett
26  * @author Christian Grothoff
27  */
28
29 #include "platform.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_fs_service.h"
34 #include "fs_api.h"
35 #include "fs_tree.h"
36
37
38 /**
39  * Fill in all of the generic fields for
40  * a publish event and call the callback.
41  *
42  * @param pi structure to fill in
43  * @param pc overall publishing context
44  * @param p file information for the file being published
45  * @param offset where in the file are we so far
46  * @return value returned from callback
47  */
48 void *
49 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
50                                 struct GNUNET_FS_PublishContext *pc,
51                                 const struct GNUNET_FS_FileInformation *p,
52                                 uint64_t offset)
53 {
54   pi->value.publish.pc = pc;
55   pi->value.publish.fi = p;
56   pi->value.publish.cctx = p->client_info;
57   pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
58   pi->value.publish.filename = p->filename;
59   pi->value.publish.size =
60       (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
61   pi->value.publish.eta =
62       GNUNET_TIME_calculate_eta (p->start_time, offset, pi->value.publish.size);
63   pi->value.publish.completed = offset;
64   pi->value.publish.duration =
65       GNUNET_TIME_absolute_get_duration (p->start_time);
66   pi->value.publish.anonymity = p->bo.anonymity_level;
67   pi->fsh = pc->h;
68   return pc->h->upcb (pc->h->upcb_cls, pi);
69 }
70
71
72 /**
73  * Cleanup the publish context, we're done with it.
74  *
75  * @param pc struct to clean up
76  */
77 static void
78 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
79 {
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up publish context (done!)\n");
81   if (pc->fhc != NULL)
82   {
83     GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
84     pc->fhc = NULL;
85   }
86   GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
87   if (pc->ns != NULL)
88   {
89     GNUNET_FS_namespace_delete (pc->ns, GNUNET_NO);
90     pc->ns = NULL;
91   }
92   GNUNET_free_non_null (pc->nid);
93   GNUNET_free_non_null (pc->nuid);
94   GNUNET_free_non_null (pc->serialization);
95   if (pc->dsh != NULL)
96   {
97     GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
98     pc->dsh = NULL;
99   }
100   if (pc->client != NULL)
101   {
102     GNUNET_CLIENT_disconnect (pc->client);
103     pc->client = NULL;
104   }
105   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
106   GNUNET_free (pc);
107 }
108
109
110 /**
111  * Function called by the datastore API with
112  * the result from the PUT request.
113  *
114  * @param cls the 'struct GNUNET_FS_PublishContext'
115  * @param success GNUNET_OK on success
116  * @param min_expiration minimum expiration time required for content to be stored
117  * @param msg error message (or NULL)
118  */
119 static void
120 ds_put_cont (void *cls, int success, 
121              struct GNUNET_TIME_Absolute min_expiration,
122              const char *msg)
123 {
124   struct GNUNET_FS_PublishContext *pc = cls;
125   struct GNUNET_FS_ProgressInfo pi;
126
127   pc->qre = NULL;
128   if (GNUNET_SYSERR == success)
129   {
130     GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
131     pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
132     pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
133     pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
134     pc->fi_pos->client_info =
135         GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
136     if ((pc->fi_pos->is_directory != GNUNET_YES) &&
137         (pc->fi_pos->filename != NULL) &&
138         (pc->fi_pos->data.file.do_index == GNUNET_YES))
139     {
140       /* run unindex to clean up */
141       GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
142     }
143   }
144   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
145   pc->upload_task =
146       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
147                                           &GNUNET_FS_publish_main_, pc);
148 }
149
150
151 /**
152  * Generate the callback that signals clients
153  * that a file (or directory) has been completely
154  * published.
155  *
156  * @param p the completed upload
157  * @param pc context of the publication
158  */
159 static void
160 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
161                            struct GNUNET_FS_PublishContext *pc)
162 {
163   struct GNUNET_FS_ProgressInfo pi;
164
165   pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
166   pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
167   pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
168   p->client_info =
169       GNUNET_FS_publish_make_status_ (&pi, pc, p,
170                                       GNUNET_ntohll (p->chk_uri->data.
171                                                      chk.file_length));
172 }
173
174
175 /**
176  * Generate the callback that signals clients
177  * that a file (or directory) has encountered
178  * a problem during publication.
179  *
180  * @param p the upload that had trouble
181  * @param pc context of the publication
182  * @param emsg error message
183  */
184 static void
185 signal_publish_error (struct GNUNET_FS_FileInformation *p,
186                       struct GNUNET_FS_PublishContext *pc, const char *emsg)
187 {
188   struct GNUNET_FS_ProgressInfo pi;
189
190   p->emsg = GNUNET_strdup (emsg);
191   pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
192   pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
193   pi.value.publish.specifics.error.message = emsg;
194   p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
195   if ((p->is_directory != GNUNET_YES) && (p->filename != NULL) &&
196       (p->data.file.do_index == GNUNET_YES))
197   {
198     /* run unindex to clean up */
199     GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
200   }
201
202 }
203
204
205 /**
206  * Datastore returns from reservation cancel request.
207  *
208  * @param cls the 'struct GNUNET_FS_PublishContext'
209  * @param success success code (not used)
210  * @param min_expiration minimum expiration time required for content to be stored
211  * @param msg error message (typically NULL, not used)
212  */
213 static void
214 finish_release_reserve (void *cls, int success, 
215                         struct GNUNET_TIME_Absolute min_expiration,
216                         const char *msg)
217 {
218   struct GNUNET_FS_PublishContext *pc = cls;
219
220   pc->qre = NULL;
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Releasing reserve done!\n");
222   signal_publish_completion (pc->fi, pc);
223   pc->all_done = GNUNET_YES;
224   GNUNET_FS_publish_sync_ (pc);
225 }
226
227
228 /**
229  * We've finished publishing the SBlock as part of a larger upload.
230  * Check the result and complete the larger upload.
231  *
232  * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
233  * @param uri URI of the published SBlock
234  * @param emsg NULL on success, otherwise error message
235  */
236 static void
237 publish_sblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
238                       const char *emsg)
239 {
240   struct GNUNET_FS_PublishContext *pc = cls;
241
242   pc->sks_pc = NULL;
243   if (NULL != emsg)
244   {
245     signal_publish_error (pc->fi, pc, emsg);
246     GNUNET_FS_publish_sync_ (pc);
247     return;
248   }
249   GNUNET_assert (pc->qre == NULL);
250   if ((pc->dsh != NULL) && (pc->rid != 0))
251   {
252     pc->qre =
253         GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
254                                           GNUNET_TIME_UNIT_FOREVER_REL,
255                                           &finish_release_reserve, pc);
256   }
257   else
258   {
259     finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
260   }
261 }
262
263
264 /**
265  * We are almost done publishing the structure,
266  * add SBlocks (if needed).
267  *
268  * @param pc overall upload data
269  */
270 static void
271 publish_sblock (struct GNUNET_FS_PublishContext *pc)
272 {
273   if (NULL != pc->ns)
274     pc->sks_pc = GNUNET_FS_publish_sks (pc->h, pc->ns, pc->nid, pc->nuid,
275                                         pc->fi->meta, pc->fi->chk_uri, &pc->fi->bo,
276                                         pc->options, &publish_sblocks_cont, pc);
277   else
278     publish_sblocks_cont (pc, NULL, NULL);
279 }
280
281
282 /**
283  * We've finished publishing a KBlock as part of a larger upload.
284  * Check the result and continue the larger upload.
285  *
286  * @param cls the "struct GNUNET_FS_PublishContext*"
287  *        of the larger upload
288  * @param uri URI of the published blocks
289  * @param emsg NULL on success, otherwise error message
290  */
291 static void
292 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
293                       const char *emsg)
294 {
295   struct GNUNET_FS_PublishContext *pc = cls;
296   struct GNUNET_FS_FileInformation *p = pc->fi_pos;
297
298   pc->ksk_pc = NULL;
299   if (NULL != emsg)
300   {
301     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading KSK blocks: %s\n",
302                 emsg);
303     signal_publish_error (p, pc, emsg);
304     GNUNET_FS_file_information_sync_ (p);
305     GNUNET_FS_publish_sync_ (pc);
306     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
307     pc->upload_task =
308       GNUNET_SCHEDULER_add_with_priority
309       (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
310     return;
311   }
312   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313               "KSK blocks published, moving on to next file\n");
314   if (NULL != p->dir)
315     signal_publish_completion (p, pc);
316   /* move on to next file */
317   if (NULL != p->next)
318     pc->fi_pos = p->next;
319   else
320     pc->fi_pos = p->dir;
321   GNUNET_FS_publish_sync_ (pc);
322   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
323   pc->upload_task =
324       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
325                                           &GNUNET_FS_publish_main_, pc);
326 }
327
328
329 /**
330  * Function called by the tree encoder to obtain
331  * a block of plaintext data (for the lowest level
332  * of the tree).
333  *
334  * @param cls our publishing context
335  * @param offset identifies which block to get
336  * @param max (maximum) number of bytes to get; returning
337  *        fewer will also cause errors
338  * @param buf where to copy the plaintext buffer
339  * @param emsg location to store an error message (on error)
340  * @return number of bytes copied to buf, 0 on error
341  */
342 static size_t
343 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
344 {
345   struct GNUNET_FS_PublishContext *pc = cls;
346   struct GNUNET_FS_FileInformation *p;
347   size_t pt_size;
348   const char *dd;
349
350   p = pc->fi_pos;
351   if (p->is_directory == GNUNET_YES)
352   {
353     pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
354     dd = p->data.dir.dir_data;
355     memcpy (buf, &dd[offset], pt_size);
356   }
357   else
358   {
359     if (UINT64_MAX == offset)
360       return p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
361     pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
362     if (pt_size == 0)
363       return 0;                 /* calling reader with pt_size==0
364                                  * might free buf, so don't! */
365     if (pt_size !=
366         p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
367                              emsg))
368       return 0;
369   }
370   return pt_size;
371 }
372
373
374 /**
375  * The tree encoder has finished processing a
376  * file.   Call it's finish method and deal with
377  * the final result.
378  *
379  * @param cls our publishing context
380  * @param tc scheduler's task context (not used)
381  */
382 static void
383 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
384 {
385   struct GNUNET_FS_PublishContext *pc = cls;
386   struct GNUNET_FS_FileInformation *p;
387   struct GNUNET_FS_ProgressInfo pi;
388   char *emsg;
389   uint64_t flen;
390
391   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
392   p = pc->fi_pos;
393   GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
394   p->te = NULL;
395   if (NULL != emsg)
396   {
397     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
398     GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
399     GNUNET_free (emsg);
400     pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
401     pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
402     pi.value.publish.specifics.error.message = p->emsg;
403     p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
404   }
405   else
406   {
407   /* final progress event */
408     GNUNET_assert (NULL != p->chk_uri);
409     flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
410     pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
411     pi.value.publish.specifics.progress.data = NULL;
412     pi.value.publish.specifics.progress.offset = flen;
413     pi.value.publish.specifics.progress.data_len = 0;
414     pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
415     p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
416   }
417   GNUNET_FS_file_information_sync_ (p);
418   /* continue with main */
419   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
420   pc->upload_task =
421       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
422                                           &GNUNET_FS_publish_main_, pc);
423 }
424
425
426 /**
427  * Function called asking for the current (encoded)
428  * block to be processed.  After processing the
429  * client should either call "GNUNET_FS_tree_encode_next"
430  * or (on error) "GNUNET_FS_tree_encode_finish".
431  *
432  * @param cls closure
433  * @param chk content hash key for the block
434  * @param offset offset of the block in the file
435  * @param depth depth of the block in the file, 0 for DBLOCK
436  * @param type type of the block (IBLOCK or DBLOCK)
437  * @param block the (encrypted) block
438  * @param block_size size of block (in bytes)
439  */
440 static void
441 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
442             unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
443             uint16_t block_size)
444 {
445   struct GNUNET_FS_PublishContext *pc = cls;
446   struct GNUNET_FS_FileInformation *p;
447   struct OnDemandBlock odb;
448
449   p = pc->fi_pos;
450   if (NULL == pc->dsh)
451   {
452     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
453     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
454     pc->upload_task =
455         GNUNET_SCHEDULER_add_with_priority
456         (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
457     return;
458   }
459
460   if ((p->is_directory != GNUNET_YES) && (GNUNET_YES == p->data.file.do_index) &&
461       (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
462   {
463     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464                 "Indexing block `%s' for offset %llu with index size %u\n",
465                 GNUNET_h2s (&chk->query), (unsigned long long) offset,
466                 sizeof (struct OnDemandBlock));
467     odb.offset = GNUNET_htonll (offset);
468     odb.file_id = p->data.file.file_id;
469     GNUNET_assert (pc->qre == NULL);
470     pc->qre =
471         GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
472                               &chk->query, sizeof (struct OnDemandBlock), &odb,
473                               GNUNET_BLOCK_TYPE_FS_ONDEMAND,
474                               p->bo.content_priority, p->bo.anonymity_level,
475                               p->bo.replication_level, p->bo.expiration_time,
476                               -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
477                               &ds_put_cont, pc);
478     return;
479   }
480   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481               "Publishing block `%s' for offset %llu with size %u\n",
482               GNUNET_h2s (&chk->query), (unsigned long long) offset,
483               (unsigned int) block_size);
484   GNUNET_assert (pc->qre == NULL);
485   pc->qre =
486       GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
487                             &chk->query, block_size, block, type,
488                             p->bo.content_priority, p->bo.anonymity_level,
489                             p->bo.replication_level, p->bo.expiration_time, -2,
490                             1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
491                             pc);
492 }
493
494
495 /**
496  * Function called with information about our
497  * progress in computing the tree encoding.
498  *
499  * @param cls closure
500  * @param offset where are we in the file
501  * @param pt_block plaintext of the currently processed block
502  * @param pt_size size of pt_block
503  * @param depth depth of the block in the tree, 0 for DBLOCK
504  */
505 static void
506 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
507                unsigned int depth)
508 {
509   struct GNUNET_FS_PublishContext *pc = cls;
510   struct GNUNET_FS_FileInformation *p;
511   struct GNUNET_FS_ProgressInfo pi;
512
513   p = pc->fi_pos;
514   pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
515   pi.value.publish.specifics.progress.data = pt_block;
516   pi.value.publish.specifics.progress.offset = offset;
517   pi.value.publish.specifics.progress.data_len = pt_size;
518   pi.value.publish.specifics.progress.depth = depth;
519   p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
520 }
521
522
523 /**
524  * We are uploading a file or directory; load (if necessary) the next
525  * block into memory, encrypt it and send it to the FS service.  Then
526  * continue with the main task.
527  *
528  * @param pc overall upload data
529  */
530 static void
531 publish_content (struct GNUNET_FS_PublishContext *pc)
532 {
533   struct GNUNET_FS_FileInformation *p;
534   char *emsg;
535   struct GNUNET_FS_DirectoryBuilder *db;
536   struct GNUNET_FS_FileInformation *dirpos;
537   void *raw_data;
538   uint64_t size;
539
540   p = pc->fi_pos;
541   GNUNET_assert (p != NULL);
542   if (NULL == p->te)
543   {
544     if (p->is_directory == GNUNET_YES)
545     {
546       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
547       db = GNUNET_FS_directory_builder_create (p->meta);
548       dirpos = p->data.dir.entries;
549       while (NULL != dirpos)
550       {
551         if (dirpos->is_directory == GNUNET_YES)
552         {
553           raw_data = dirpos->data.dir.dir_data;
554           dirpos->data.dir.dir_data = NULL;
555         }
556         else
557         {
558           raw_data = NULL;
559           if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
560               (dirpos->data.file.file_size > 0))
561           {
562             raw_data = GNUNET_malloc (dirpos->data.file.file_size);
563             emsg = NULL;
564             if (dirpos->data.file.file_size !=
565                 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
566                                           dirpos->data.file.file_size, raw_data,
567                                           &emsg))
568             {
569               GNUNET_free_non_null (emsg);
570               GNUNET_free (raw_data);
571               raw_data = NULL;
572             }
573             dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
574           }
575         }
576         GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
577                                          raw_data);
578         GNUNET_free_non_null (raw_data);
579         dirpos = dirpos->next;
580       }
581       GNUNET_free_non_null (p->data.dir.dir_data);
582       p->data.dir.dir_data = NULL;
583       p->data.dir.dir_size = 0;
584       GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
585                                           &p->data.dir.dir_data);
586       GNUNET_FS_file_information_sync_ (p);
587     }
588     size = (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
589     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
590     p->te =
591         GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
592                                        &block_proc, &progress_proc,
593                                        &encode_cont);
594
595   }
596   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
597   GNUNET_FS_tree_encoder_next (p->te);
598 }
599
600
601 /**
602  * Process the response (or lack thereof) from
603  * the "fs" service to our 'start index' request.
604  *
605  * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
606  * @param msg the response we got
607  */
608 static void
609 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
610 {
611   struct GNUNET_FS_PublishContext *pc = cls;
612   struct GNUNET_FS_FileInformation *p;
613   const char *emsg;
614   uint16_t msize;
615
616   GNUNET_CLIENT_disconnect (pc->client);
617   pc->client = NULL;
618   p = pc->fi_pos;
619   if (msg == NULL)
620   {
621     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622                 _
623                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
624                 p->filename,
625                 _("timeout on index-start request to `fs' service"));
626     p->data.file.do_index = GNUNET_NO;
627     GNUNET_FS_file_information_sync_ (p);
628     publish_content (pc);
629     return;
630   }
631   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
632   {
633     msize = ntohs (msg->size);
634     emsg = (const char *) &msg[1];
635     if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
636         (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
637       emsg = gettext_noop ("unknown error");
638     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639                 _
640                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
641                 p->filename, gettext (emsg));
642     p->data.file.do_index = GNUNET_NO;
643     GNUNET_FS_file_information_sync_ (p);
644     publish_content (pc);
645     return;
646   }
647   p->data.file.index_start_confirmed = GNUNET_YES;
648   /* success! continue with indexing */
649   GNUNET_FS_file_information_sync_ (p);
650   publish_content (pc);
651 }
652
653
654 /**
655  * Function called once the hash computation over an
656  * indexed file has completed.
657  *
658  * @param cls closure, our publishing context
659  * @param res resulting hash, NULL on error
660  */
661 static void
662 hash_for_index_cb (void *cls, const struct GNUNET_HashCode * res)
663 {
664   struct GNUNET_FS_PublishContext *pc = cls;
665   struct GNUNET_FS_FileInformation *p;
666   struct IndexStartMessage *ism;
667   size_t slen;
668   struct GNUNET_CLIENT_Connection *client;
669   uint64_t dev;
670   uint64_t ino;
671   char *fn;
672
673   pc->fhc = NULL;
674   p = pc->fi_pos;
675   if (NULL == res)
676   {
677     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
678                 _
679                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
680                 p->filename, _("failed to compute hash"));
681     p->data.file.do_index = GNUNET_NO;
682     GNUNET_FS_file_information_sync_ (p);
683     publish_content (pc);
684     return;
685   }
686   if (GNUNET_YES == p->data.file.index_start_confirmed)
687   {
688     publish_content (pc);
689     return;
690   }
691   fn = GNUNET_STRINGS_filename_expand (p->filename);
692   GNUNET_assert (fn != NULL);
693   slen = strlen (fn) + 1;
694   if (slen >=
695       GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
696   {
697     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698                 _
699                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
700                 fn, _("filename too long"));
701     GNUNET_free (fn);
702     p->data.file.do_index = GNUNET_NO;
703     GNUNET_FS_file_information_sync_ (p);
704     publish_content (pc);
705     return;
706   }
707   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
708               p->filename, GNUNET_h2s (res));
709   if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
710   {
711     p->data.file.file_id = *res;
712     p->data.file.have_hash = GNUNET_YES;
713     p->data.file.index_start_confirmed = GNUNET_YES;
714     GNUNET_FS_file_information_sync_ (p);
715     publish_content (pc);
716     GNUNET_free (fn);
717     return;
718   }
719   client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
720   if (NULL == client)
721   {
722     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
723                 _
724                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
725                 p->filename, _("could not connect to `fs' service"));
726     p->data.file.do_index = GNUNET_NO;
727     publish_content (pc);
728     GNUNET_free (fn);
729     return;
730   }
731   if (p->data.file.have_hash != GNUNET_YES)
732   {
733     p->data.file.file_id = *res;
734     p->data.file.have_hash = GNUNET_YES;
735     GNUNET_FS_file_information_sync_ (p);
736   }
737   ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
738   ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
739   ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
740   if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
741   {
742     ism->device = GNUNET_htonll (dev);
743     ism->inode = GNUNET_htonll (ino);
744   }
745   else
746   {
747     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748                 _("Failed to get file identifiers for `%s'\n"), p->filename);
749   }
750   ism->file_id = *res;
751   memcpy (&ism[1], fn, slen);
752   GNUNET_free (fn);
753   pc->client = client;
754   GNUNET_break (GNUNET_YES ==
755                 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
756                                                          GNUNET_TIME_UNIT_FOREVER_REL,
757                                                          GNUNET_YES,
758                                                          &process_index_start_response,
759                                                          pc));
760   GNUNET_free (ism);
761 }
762
763
764 /**
765  * Main function that performs the upload.
766  *
767  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
768  * @param tc task context
769  */
770 void
771 GNUNET_FS_publish_main_ (void *cls,
772                          const struct GNUNET_SCHEDULER_TaskContext *tc)
773 {
774   struct GNUNET_FS_PublishContext *pc = cls;
775   struct GNUNET_FS_ProgressInfo pi;
776   struct GNUNET_FS_FileInformation *p;
777   struct GNUNET_FS_Uri *loc;
778   char *fn;
779
780   pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
781   p = pc->fi_pos;
782   if (NULL == p)
783   {
784     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785                 "Publishing complete, now publishing SKS and KSK blocks.\n");
786     /* upload of entire hierarchy complete,
787      * publish namespace entries */
788     GNUNET_FS_publish_sync_ (pc);
789     publish_sblock (pc);
790     return;
791   }
792   /* find starting position */
793   while ((p->is_directory == GNUNET_YES) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
794          && (NULL == p->data.dir.entries->chk_uri))
795   {
796     p = p->data.dir.entries;
797     pc->fi_pos = p;
798     GNUNET_FS_publish_sync_ (pc);
799   }
800   /* abort on error */
801   if (NULL != p->emsg)
802   {
803     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
804     /* error with current file, abort all
805      * related files as well! */
806     while (NULL != p->dir)
807     {
808       fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
809                                                    EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
810       p = p->dir;
811       if (fn != NULL)
812       {
813         GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
814                          p->emsg);
815         GNUNET_free (fn);
816       }
817       else
818       {
819         GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
820       }
821       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
822       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
823       pi.value.publish.specifics.error.message = p->emsg;
824       p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
825     }
826     pc->all_done = GNUNET_YES;
827     GNUNET_FS_publish_sync_ (pc);
828     return;
829   }
830   /* handle completion */
831   if (NULL != p->chk_uri)
832   {
833     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834                 "File upload complete, now publishing KSK blocks.\n");
835     if (0 == p->bo.anonymity_level)
836     {
837       /* zero anonymity, box CHK URI in LOC URI */
838       loc =
839           GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
840                                     p->bo.expiration_time);
841       GNUNET_FS_uri_destroy (p->chk_uri);
842       p->chk_uri = loc;
843     }
844     GNUNET_FS_publish_sync_ (pc);
845     /* upload of "p" complete, publish KBlocks! */
846     if (p->keywords != NULL)
847     {
848       pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
849                                           pc->options, &publish_kblocks_cont, pc);
850     }
851     else
852     {
853       publish_kblocks_cont (pc, p->chk_uri, NULL);
854     }
855     return;
856   }
857   if ((p->is_directory != GNUNET_YES) && (p->data.file.do_index))
858   {
859     if (NULL == p->filename)
860     {
861       p->data.file.do_index = GNUNET_NO;
862       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
863                   _
864                   ("Can not index file `%s': %s.  Will try to insert instead.\n"),
865                   "<no-name>", _("needs to be an actual file"));
866       GNUNET_FS_file_information_sync_ (p);
867       publish_content (pc);
868       return;
869     }
870     if (p->data.file.have_hash)
871     {
872       hash_for_index_cb (pc, &p->data.file.file_id);
873     }
874     else
875     {
876       p->start_time = GNUNET_TIME_absolute_get ();
877       pc->fhc =
878           GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
879                                    HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
880     }
881     return;
882   }
883   publish_content (pc);
884 }
885
886
887 /**
888  * Signal the FS's progress function that we are starting
889  * an upload.
890  *
891  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
892  * @param fi the entry in the publish-structure
893  * @param length length of the file or directory
894  * @param meta metadata for the file or directory (can be modified)
895  * @param uri pointer to the keywords that will be used for this entry (can be modified)
896  * @param bo block options
897  * @param do_index should we index?
898  * @param client_info pointer to client context set upon creation (can be modified)
899  * @return GNUNET_OK to continue (always)
900  */
901 static int
902 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
903                   uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
904                   struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
905                   int *do_index, void **client_info)
906 {
907   struct GNUNET_FS_PublishContext *pc = cls;
908   struct GNUNET_FS_ProgressInfo pi;
909   unsigned int kc;
910   uint64_t left;
911
912   if (GNUNET_YES == pc->skip_next_fi_callback)
913   {
914     pc->skip_next_fi_callback = GNUNET_NO;
915     return GNUNET_OK;
916   }
917   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
918   if (*do_index)
919   {
920     /* space for on-demand blocks */
921     pc->reserve_space +=
922         ((length + DBLOCK_SIZE -
923           1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
924   }
925   else
926   {
927     /* space for DBlocks */
928     pc->reserve_space += length;
929   }
930   /* entries for IBlocks and DBlocks, space for IBlocks */
931   left = length;
932   while (1)
933   {
934     left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
935     pc->reserve_entries += left;
936     if (left <= 1)
937       break;
938     left = left * sizeof (struct ContentHashKey);
939     pc->reserve_space += left;
940   }
941   pc->reserve_entries++;
942   /* entries and space for keywords */
943   if (NULL != *uri)
944   {
945     kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
946     pc->reserve_entries += kc;
947     pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
948   }
949   pi.status = GNUNET_FS_STATUS_PUBLISH_START;
950   *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
951   GNUNET_FS_file_information_sync_ (fi);
952   if ((fi->is_directory) && (fi->dir != NULL))
953   {
954     /* We are a directory, and we are not top-level; process entries in directory */
955     pc->skip_next_fi_callback = GNUNET_YES;
956     GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
957   }
958   return GNUNET_OK;
959 }
960
961
962 /**
963  * Actually signal the FS's progress function that we are suspending
964  * an upload.
965  *
966  * @param fi the entry in the publish-structure
967  * @param pc the publish context of which a file is being suspended
968  */
969 static void
970 suspend_operation (struct GNUNET_FS_FileInformation *fi,
971                    struct GNUNET_FS_PublishContext *pc)
972 {
973   struct GNUNET_FS_ProgressInfo pi;
974   uint64_t off;
975
976   if (NULL != pc->ksk_pc)
977   {
978     GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
979     pc->ksk_pc = NULL;
980   }
981   if (NULL != pc->sks_pc)
982   {
983     GNUNET_FS_publish_sks_cancel (pc->sks_pc);
984     pc->sks_pc = NULL;
985   }
986   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
987   GNUNET_free_non_null (fi->serialization);
988   fi->serialization = NULL;
989   off = (fi->chk_uri == NULL) ? 0 : (fi->is_directory == GNUNET_YES) ? fi->data.dir.dir_size : fi->data.file.file_size;
990   pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
991   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
992   if (NULL != pc->qre)
993   {
994     GNUNET_DATASTORE_cancel (pc->qre);
995     pc->qre = NULL;
996   }
997   if (NULL != pc->dsh)
998   {
999     GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1000     pc->dsh = NULL;
1001   }
1002   pc->rid = 0;
1003 }
1004
1005
1006 /**
1007  * Signal the FS's progress function that we are suspending
1008  * an upload.  Performs the recursion.
1009  *
1010  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1011  * @param fi the entry in the publish-structure
1012  * @param length length of the file or directory
1013  * @param meta metadata for the file or directory (can be modified)
1014  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1015  * @param bo block options
1016  * @param do_index should we index?
1017  * @param client_info pointer to client context set upon creation (can be modified)
1018  * @return GNUNET_OK to continue (always)
1019  */
1020 static int
1021 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
1022                     uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1023                     struct GNUNET_FS_Uri **uri,
1024                     struct GNUNET_FS_BlockOptions *bo, int *do_index,
1025                     void **client_info)
1026 {
1027   struct GNUNET_FS_PublishContext *pc = cls;
1028
1029   if (GNUNET_YES == pc->skip_next_fi_callback)
1030   {
1031     pc->skip_next_fi_callback = GNUNET_NO;
1032     return GNUNET_OK;
1033   }
1034   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1035   {
1036     /* process entries in directory */
1037     pc->skip_next_fi_callback = GNUNET_YES;
1038     GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1039   }
1040   suspend_operation (fi, pc);
1041   *client_info = NULL;
1042   return GNUNET_OK;
1043 }
1044
1045
1046 /**
1047  * Create SUSPEND event for the given publish operation
1048  * and then clean up our state (without stop signal).
1049  *
1050  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1051  */
1052 void
1053 GNUNET_FS_publish_signal_suspend_ (void *cls)
1054 {
1055   struct GNUNET_FS_PublishContext *pc = cls;
1056
1057   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1058   {
1059     GNUNET_SCHEDULER_cancel (pc->upload_task);
1060     pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1061   }
1062   pc->skip_next_fi_callback = GNUNET_YES;
1063   GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1064   suspend_operation (pc->fi, pc);
1065   GNUNET_FS_end_top (pc->h, pc->top);
1066   pc->top = NULL;
1067   publish_cleanup (pc);
1068 }
1069
1070
1071 /**
1072  * We have gotten a reply for our space reservation request.
1073  * Either fail (insufficient space) or start publishing for good.
1074  *
1075  * @param cls the 'struct GNUNET_FS_PublishContext*'
1076  * @param success positive reservation ID on success
1077  * @param min_expiration minimum expiration time required for content to be stored
1078  * @param msg error message on error, otherwise NULL
1079  */
1080 static void
1081 finish_reserve (void *cls, int success, 
1082                 struct GNUNET_TIME_Absolute min_expiration,
1083                 const char *msg)
1084 {
1085   struct GNUNET_FS_PublishContext *pc = cls;
1086
1087   pc->qre = NULL;
1088   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1089   if ((msg != NULL) || (success <= 0))
1090   {
1091     GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1092                      msg);
1093     signal_publish_error (pc->fi, pc, pc->fi->emsg);
1094     return;
1095   }
1096   pc->rid = success;
1097   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1098   pc->upload_task =
1099       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1100                                           &GNUNET_FS_publish_main_, pc);
1101 }
1102
1103
1104 /**
1105  * Publish a file or directory.
1106  *
1107  * @param h handle to the file sharing subsystem
1108  * @param fi information about the file or directory structure to publish
1109  * @param ns namespace to publish the file in, NULL for no namespace
1110  * @param nid identifier to use for the publishd content in the namespace
1111  *        (can be NULL, must be NULL if namespace is NULL)
1112  * @param nuid update-identifier that will be used for future updates
1113  *        (can be NULL, must be NULL if namespace or nid is NULL)
1114  * @param options options for the publication
1115  * @return context that can be used to control the publish operation
1116  */
1117 struct GNUNET_FS_PublishContext *
1118 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1119                          struct GNUNET_FS_FileInformation *fi,
1120                          struct GNUNET_FS_Namespace *ns, const char *nid,
1121                          const char *nuid,
1122                          enum GNUNET_FS_PublishOptions options)
1123 {
1124   struct GNUNET_FS_PublishContext *ret;
1125   struct GNUNET_DATASTORE_Handle *dsh;
1126
1127   GNUNET_assert (NULL != h);
1128   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1129   {
1130     dsh = GNUNET_DATASTORE_connect (h->cfg);
1131     if (NULL == dsh)
1132       return NULL;
1133   }
1134   else
1135   {
1136     dsh = NULL;
1137   }
1138   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1139   ret->dsh = dsh;
1140   ret->h = h;
1141   ret->fi = fi;
1142   ret->ns = ns;
1143   ret->options = options;
1144   if (ns != NULL)
1145   {
1146     ns->rc++;
1147     GNUNET_assert (NULL != nid);
1148     ret->nid = GNUNET_strdup (nid);
1149     if (NULL != nuid)
1150       ret->nuid = GNUNET_strdup (nuid);
1151   }
1152   /* signal start */
1153   GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1154   ret->fi_pos = ret->fi;
1155   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1156   GNUNET_FS_publish_sync_ (ret);
1157   if (NULL != ret->dsh)
1158   {
1159     GNUNET_assert (NULL == ret->qre);
1160     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1161                 _
1162                 ("Reserving space for %u entries and %llu bytes for publication\n"),
1163                 (unsigned int) ret->reserve_entries,
1164                 (unsigned long long) ret->reserve_space);
1165     ret->qre =
1166         GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1167                                   ret->reserve_entries, UINT_MAX, UINT_MAX,
1168                                   GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1169                                   ret);
1170   }
1171   else
1172   {
1173     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1174     ret->upload_task =
1175         GNUNET_SCHEDULER_add_with_priority
1176         (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1177   }
1178   return ret;
1179 }
1180
1181
1182 /**
1183  * Signal the FS's progress function that we are stopping
1184  * an upload.
1185  *
1186  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1187  * @param fi the entry in the publish-structure
1188  * @param length length of the file or directory
1189  * @param meta metadata for the file or directory (can be modified)
1190  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1191  * @param bo block options (can be modified)
1192  * @param do_index should we index?
1193  * @param client_info pointer to client context set upon creation (can be modified)
1194  * @return GNUNET_OK to continue (always)
1195  */
1196 static int
1197 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1198                  uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1199                  struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1200                  int *do_index, void **client_info)
1201 {
1202   struct GNUNET_FS_PublishContext *pc = cls;
1203   struct GNUNET_FS_ProgressInfo pi;
1204   uint64_t off;
1205
1206   if (GNUNET_YES == pc->skip_next_fi_callback)
1207   {
1208     pc->skip_next_fi_callback = GNUNET_NO;
1209     return GNUNET_OK;
1210   }
1211   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1212   {
1213     /* process entries in directory first */
1214     pc->skip_next_fi_callback = GNUNET_YES;
1215     GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1216   }
1217   if (fi->serialization != NULL)
1218   {
1219     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1220                                  fi->serialization);
1221     GNUNET_free (fi->serialization);
1222     fi->serialization = NULL;
1223   }
1224   off = (fi->chk_uri == NULL) ? 0 : length;
1225   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1226   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1227   *client_info = NULL;
1228   return GNUNET_OK;
1229 }
1230
1231
1232 /**
1233  * Stop an upload.  Will abort incomplete uploads (but
1234  * not remove blocks that have already been publishd) or
1235  * simply clean up the state for completed uploads.
1236  * Must NOT be called from within the event callback!
1237  *
1238  * @param pc context for the upload to stop
1239  */
1240 void
1241 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1242 {
1243   struct GNUNET_FS_ProgressInfo pi;
1244   uint64_t off;
1245
1246   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1247   GNUNET_FS_end_top (pc->h, pc->top);
1248   if (NULL != pc->ksk_pc)
1249   {
1250     GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1251     pc->ksk_pc = NULL;
1252   }
1253   if (NULL != pc->sks_pc)
1254   {
1255     GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1256     pc->sks_pc = NULL;
1257   }
1258   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1259   {
1260     GNUNET_SCHEDULER_cancel (pc->upload_task);
1261     pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1262   }
1263   pc->skip_next_fi_callback = GNUNET_YES;
1264   GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1265
1266   if (pc->fi->serialization != NULL)
1267   {
1268     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1269                                  pc->fi->serialization);
1270     GNUNET_free (pc->fi->serialization);
1271     pc->fi->serialization = NULL;
1272   }
1273   off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1274
1275   if (pc->serialization != NULL)
1276   {
1277     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1278                                  pc->serialization);
1279     GNUNET_free (pc->serialization);
1280     pc->serialization = NULL;
1281   }
1282   if (NULL != pc->qre)
1283   {
1284     GNUNET_DATASTORE_cancel (pc->qre);
1285     pc->qre = NULL;
1286   }
1287   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1288   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1289   publish_cleanup (pc);
1290 }
1291
1292
1293
1294 /* end of fs_publish.c */