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