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