134dd21e41a67ed78744f714463249b93a6fe77c
[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) ? 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, GNUNET_NO);
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_NO) &&
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_NO) && (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)
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     pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
359     if (pt_size == 0)
360       return 0;                 /* calling reader with pt_size==0
361                                  * might free buf, so don't! */
362     if (pt_size !=
363         p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
364                              emsg))
365       return 0;
366   }
367   return pt_size;
368 }
369
370
371 /**
372  * The tree encoder has finished processing a
373  * file.   Call it's finish method and deal with
374  * the final result.
375  *
376  * @param cls our publishing context
377  * @param tc scheduler's task context (not used)
378  */
379 static void
380 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
381 {
382   struct GNUNET_FS_PublishContext *pc = cls;
383   struct GNUNET_FS_FileInformation *p;
384   struct GNUNET_FS_ProgressInfo pi;
385   char *emsg;
386   uint64_t flen;
387
388   p = pc->fi_pos;
389   GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
390   p->te = NULL;
391   if (NULL != emsg)
392   {
393     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
394     GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
395     GNUNET_free (emsg);
396     pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
397     pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
398     pi.value.publish.specifics.error.message = p->emsg;
399     p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
400   }
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
402   /* final progress event */
403   flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
404   pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
405   pi.value.publish.specifics.progress.data = NULL;
406   pi.value.publish.specifics.progress.offset = flen;
407   pi.value.publish.specifics.progress.data_len = 0;
408   pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
409   p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
410
411   /* continue with main */
412   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
413   pc->upload_task =
414       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
415                                           &GNUNET_FS_publish_main_, pc);
416 }
417
418
419 /**
420  * Function called asking for the current (encoded)
421  * block to be processed.  After processing the
422  * client should either call "GNUNET_FS_tree_encode_next"
423  * or (on error) "GNUNET_FS_tree_encode_finish".
424  *
425  * @param cls closure
426  * @param chk content hash key for the block
427  * @param offset offset of the block in the file
428  * @param depth depth of the block in the file, 0 for DBLOCK
429  * @param type type of the block (IBLOCK or DBLOCK)
430  * @param block the (encrypted) block
431  * @param block_size size of block (in bytes)
432  */
433 static void
434 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
435             unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
436             uint16_t block_size)
437 {
438   struct GNUNET_FS_PublishContext *pc = cls;
439   struct GNUNET_FS_FileInformation *p;
440   struct OnDemandBlock odb;
441
442   p = pc->fi_pos;
443   if (NULL == pc->dsh)
444   {
445     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
446     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
447     pc->upload_task =
448         GNUNET_SCHEDULER_add_with_priority
449         (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
450     return;
451   }
452
453   if ((!p->is_directory) && (GNUNET_YES == p->data.file.do_index) &&
454       (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
455   {
456     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457                 "Indexing block `%s' for offset %llu with index size %u\n",
458                 GNUNET_h2s (&chk->query), (unsigned long long) offset,
459                 sizeof (struct OnDemandBlock));
460     odb.offset = GNUNET_htonll (offset);
461     odb.file_id = p->data.file.file_id;
462     GNUNET_assert (pc->qre == NULL);
463     pc->qre =
464         GNUNET_DATASTORE_put (pc->dsh, (p->is_directory) ? 0 : pc->rid,
465                               &chk->query, sizeof (struct OnDemandBlock), &odb,
466                               GNUNET_BLOCK_TYPE_FS_ONDEMAND,
467                               p->bo.content_priority, p->bo.anonymity_level,
468                               p->bo.replication_level, p->bo.expiration_time,
469                               -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
470                               &ds_put_cont, pc);
471     return;
472   }
473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474               "Publishing block `%s' for offset %llu with size %u\n",
475               GNUNET_h2s (&chk->query), (unsigned long long) offset,
476               (unsigned int) block_size);
477   GNUNET_assert (pc->qre == NULL);
478   pc->qre =
479       GNUNET_DATASTORE_put (pc->dsh, (p->is_directory) ? 0 : pc->rid,
480                             &chk->query, block_size, block, type,
481                             p->bo.content_priority, p->bo.anonymity_level,
482                             p->bo.replication_level, p->bo.expiration_time, -2,
483                             1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
484                             pc);
485 }
486
487
488 /**
489  * Function called with information about our
490  * progress in computing the tree encoding.
491  *
492  * @param cls closure
493  * @param offset where are we in the file
494  * @param pt_block plaintext of the currently processed block
495  * @param pt_size size of pt_block
496  * @param depth depth of the block in the tree, 0 for DBLOCK
497  */
498 static void
499 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
500                unsigned int depth)
501 {
502   struct GNUNET_FS_PublishContext *pc = cls;
503   struct GNUNET_FS_FileInformation *p;
504   struct GNUNET_FS_ProgressInfo pi;
505
506   p = pc->fi_pos;
507   pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
508   pi.value.publish.specifics.progress.data = pt_block;
509   pi.value.publish.specifics.progress.offset = offset;
510   pi.value.publish.specifics.progress.data_len = pt_size;
511   pi.value.publish.specifics.progress.depth = depth;
512   p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
513 }
514
515
516 /**
517  * We are uploading a file or directory; load (if necessary) the next
518  * block into memory, encrypt it and send it to the FS service.  Then
519  * continue with the main task.
520  *
521  * @param pc overall upload data
522  */
523 static void
524 publish_content (struct GNUNET_FS_PublishContext *pc)
525 {
526   struct GNUNET_FS_FileInformation *p;
527   char *emsg;
528   struct GNUNET_FS_DirectoryBuilder *db;
529   struct GNUNET_FS_FileInformation *dirpos;
530   void *raw_data;
531   uint64_t size;
532
533   p = pc->fi_pos;
534   GNUNET_assert (p != NULL);
535   if (NULL == p->te)
536   {
537     if (p->is_directory)
538     {
539       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
540       db = GNUNET_FS_directory_builder_create (p->meta);
541       dirpos = p->data.dir.entries;
542       while (NULL != dirpos)
543       {
544         if (dirpos->is_directory)
545         {
546           raw_data = dirpos->data.dir.dir_data;
547           dirpos->data.dir.dir_data = NULL;
548         }
549         else
550         {
551           raw_data = NULL;
552           if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
553               (dirpos->data.file.file_size > 0))
554           {
555             raw_data = GNUNET_malloc (dirpos->data.file.file_size);
556             emsg = NULL;
557             if (dirpos->data.file.file_size !=
558                 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
559                                           dirpos->data.file.file_size, raw_data,
560                                           &emsg))
561             {
562               GNUNET_free_non_null (emsg);
563               GNUNET_free (raw_data);
564               raw_data = NULL;
565             }
566           }
567         }
568         GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
569                                          raw_data);
570         GNUNET_free_non_null (raw_data);
571         dirpos = dirpos->next;
572       }
573       GNUNET_free_non_null (p->data.dir.dir_data);
574       p->data.dir.dir_data = NULL;
575       p->data.dir.dir_size = 0;
576       GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
577                                           &p->data.dir.dir_data);
578       GNUNET_FS_file_information_sync_ (p);
579     }
580     size = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
581     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
582     p->te =
583         GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
584                                        &block_proc, &progress_proc,
585                                        &encode_cont);
586
587   }
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
589   GNUNET_FS_tree_encoder_next (p->te);
590 }
591
592
593 /**
594  * Process the response (or lack thereof) from
595  * the "fs" service to our 'start index' request.
596  *
597  * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
598  * @param msg the response we got
599  */
600 static void
601 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
602 {
603   struct GNUNET_FS_PublishContext *pc = cls;
604   struct GNUNET_FS_FileInformation *p;
605   const char *emsg;
606   uint16_t msize;
607
608   GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
609   pc->client = NULL;
610   p = pc->fi_pos;
611   if (msg == NULL)
612   {
613     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
614                 _
615                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
616                 p->filename,
617                 _("timeout on index-start request to `fs' service"));
618     p->data.file.do_index = GNUNET_NO;
619     GNUNET_FS_file_information_sync_ (p);
620     publish_content (pc);
621     return;
622   }
623   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
624   {
625     msize = ntohs (msg->size);
626     emsg = (const char *) &msg[1];
627     if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
628         (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
629       emsg = gettext_noop ("unknown error");
630     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
631                 _
632                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
633                 p->filename, gettext (emsg));
634     p->data.file.do_index = GNUNET_NO;
635     GNUNET_FS_file_information_sync_ (p);
636     publish_content (pc);
637     return;
638   }
639   p->data.file.index_start_confirmed = GNUNET_YES;
640   /* success! continue with indexing */
641   GNUNET_FS_file_information_sync_ (p);
642   publish_content (pc);
643 }
644
645
646 /**
647  * Function called once the hash computation over an
648  * indexed file has completed.
649  *
650  * @param cls closure, our publishing context
651  * @param res resulting hash, NULL on error
652  */
653 static void
654 hash_for_index_cb (void *cls, const GNUNET_HashCode * res)
655 {
656   struct GNUNET_FS_PublishContext *pc = cls;
657   struct GNUNET_FS_FileInformation *p;
658   struct IndexStartMessage *ism;
659   size_t slen;
660   struct GNUNET_CLIENT_Connection *client;
661   uint64_t dev;
662   uint64_t ino;
663   char *fn;
664
665   pc->fhc = NULL;
666   p = pc->fi_pos;
667   if (NULL == res)
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
670                 _
671                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
672                 p->filename, _("failed to compute hash"));
673     p->data.file.do_index = GNUNET_NO;
674     GNUNET_FS_file_information_sync_ (p);
675     publish_content (pc);
676     return;
677   }
678   if (GNUNET_YES == p->data.file.index_start_confirmed)
679   {
680     publish_content (pc);
681     return;
682   }
683   fn = GNUNET_STRINGS_filename_expand (p->filename);
684   GNUNET_assert (fn != NULL);
685   slen = strlen (fn) + 1;
686   if (slen >=
687       GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
688   {
689     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
690                 _
691                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
692                 fn, _("filename too long"));
693     GNUNET_free (fn);
694     p->data.file.do_index = GNUNET_NO;
695     GNUNET_FS_file_information_sync_ (p);
696     publish_content (pc);
697     return;
698   }
699   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
700               p->filename, GNUNET_h2s (res));
701   if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
702   {
703     p->data.file.file_id = *res;
704     p->data.file.have_hash = GNUNET_YES;
705     p->data.file.index_start_confirmed = GNUNET_YES;
706     GNUNET_FS_file_information_sync_ (p);
707     publish_content (pc);
708     GNUNET_free (fn);
709     return;
710   }
711   client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
712   if (NULL == client)
713   {
714     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
715                 _
716                 ("Can not index file `%s': %s.  Will try to insert instead.\n"),
717                 p->filename, _("could not connect to `fs' service"));
718     p->data.file.do_index = GNUNET_NO;
719     publish_content (pc);
720     GNUNET_free (fn);
721     return;
722   }
723   if (p->data.file.have_hash != GNUNET_YES)
724   {
725     p->data.file.file_id = *res;
726     p->data.file.have_hash = GNUNET_YES;
727     GNUNET_FS_file_information_sync_ (p);
728   }
729   ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
730   ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
731   ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
732   if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
733   {
734     ism->device = GNUNET_htonll (dev);
735     ism->inode = GNUNET_htonll (ino);
736   }
737   else
738   {
739     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740                 _("Failed to get file identifiers for `%s'\n"), p->filename);
741   }
742   ism->file_id = *res;
743   memcpy (&ism[1], fn, slen);
744   GNUNET_free (fn);
745   pc->client = client;
746   GNUNET_break (GNUNET_YES ==
747                 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
748                                                          GNUNET_TIME_UNIT_FOREVER_REL,
749                                                          GNUNET_YES,
750                                                          &process_index_start_response,
751                                                          pc));
752   GNUNET_free (ism);
753 }
754
755
756 /**
757  * Main function that performs the upload.
758  *
759  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
760  * @param tc task context
761  */
762 void
763 GNUNET_FS_publish_main_ (void *cls,
764                          const struct GNUNET_SCHEDULER_TaskContext *tc)
765 {
766   struct GNUNET_FS_PublishContext *pc = cls;
767   struct GNUNET_FS_ProgressInfo pi;
768   struct GNUNET_FS_FileInformation *p;
769   struct GNUNET_FS_Uri *loc;
770   char *fn;
771
772   pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
773   p = pc->fi_pos;
774   if (NULL == p)
775   {
776     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777                 "Publishing complete, now publishing SKS and KSK blocks.\n");
778     /* upload of entire hierarchy complete,
779      * publish namespace entries */
780     GNUNET_FS_publish_sync_ (pc);
781     publish_sblock (pc);
782     return;
783   }
784   /* find starting position */
785   while ((p->is_directory) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
786          && (NULL == p->data.dir.entries->chk_uri))
787   {
788     p = p->data.dir.entries;
789     pc->fi_pos = p;
790     GNUNET_FS_publish_sync_ (pc);
791   }
792   /* abort on error */
793   if (NULL != p->emsg)
794   {
795     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
796     /* error with current file, abort all
797      * related files as well! */
798     while (NULL != p->dir)
799     {
800       fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
801                                                    EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
802       p = p->dir;
803       if (fn != NULL)
804       {
805         GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
806                          p->emsg);
807         GNUNET_free (fn);
808       }
809       else
810       {
811         GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
812       }
813       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
814       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
815       pi.value.publish.specifics.error.message = p->emsg;
816       p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
817     }
818     pc->all_done = GNUNET_YES;
819     GNUNET_FS_publish_sync_ (pc);
820     return;
821   }
822   /* handle completion */
823   if (NULL != p->chk_uri)
824   {
825     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826                 "File upload complete, now publishing KSK blocks.\n");
827     if (0 == p->bo.anonymity_level)
828     {
829       /* zero anonymity, box CHK URI in LOC URI */
830       loc =
831           GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
832                                     p->bo.expiration_time);
833       GNUNET_FS_uri_destroy (p->chk_uri);
834       p->chk_uri = loc;
835     }
836     GNUNET_FS_publish_sync_ (pc);
837     /* upload of "p" complete, publish KBlocks! */
838     if (p->keywords != NULL)
839     {
840       pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
841                                           pc->options, &publish_kblocks_cont, pc);
842     }
843     else
844     {
845       publish_kblocks_cont (pc, p->chk_uri, NULL);
846     }
847     return;
848   }
849   if ((!p->is_directory) && (p->data.file.do_index))
850   {
851     if (NULL == p->filename)
852     {
853       p->data.file.do_index = GNUNET_NO;
854       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
855                   _
856                   ("Can not index file `%s': %s.  Will try to insert instead.\n"),
857                   "<no-name>", _("needs to be an actual file"));
858       GNUNET_FS_file_information_sync_ (p);
859       publish_content (pc);
860       return;
861     }
862     if (p->data.file.have_hash)
863     {
864       hash_for_index_cb (pc, &p->data.file.file_id);
865     }
866     else
867     {
868       p->start_time = GNUNET_TIME_absolute_get ();
869       pc->fhc =
870           GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
871                                    HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
872     }
873     return;
874   }
875   publish_content (pc);
876 }
877
878
879 /**
880  * Signal the FS's progress function that we are starting
881  * an upload.
882  *
883  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
884  * @param fi the entry in the publish-structure
885  * @param length length of the file or directory
886  * @param meta metadata for the file or directory (can be modified)
887  * @param uri pointer to the keywords that will be used for this entry (can be modified)
888  * @param bo block options
889  * @param do_index should we index?
890  * @param client_info pointer to client context set upon creation (can be modified)
891  * @return GNUNET_OK to continue (always)
892  */
893 static int
894 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
895                   uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
896                   struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
897                   int *do_index, void **client_info)
898 {
899   struct GNUNET_FS_PublishContext *pc = cls;
900   struct GNUNET_FS_ProgressInfo pi;
901   unsigned int kc;
902   uint64_t left;
903
904   if (GNUNET_YES == pc->skip_next_fi_callback)
905   {
906     pc->skip_next_fi_callback = GNUNET_NO;
907     return GNUNET_OK;
908   }
909   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
910   if (*do_index)
911   {
912     /* space for on-demand blocks */
913     pc->reserve_space +=
914         ((length + DBLOCK_SIZE -
915           1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
916   }
917   else
918   {
919     /* space for DBlocks */
920     pc->reserve_space += length;
921   }
922   /* entries for IBlocks and DBlocks, space for IBlocks */
923   left = length;
924   while (1)
925   {
926     left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
927     pc->reserve_entries += left;
928     if (left <= 1)
929       break;
930     left = left * sizeof (struct ContentHashKey);
931     pc->reserve_space += left;
932   }
933   pc->reserve_entries++;
934   /* entries and space for keywords */
935   if (NULL != *uri)
936   {
937     kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
938     pc->reserve_entries += kc;
939     pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
940   }
941   pi.status = GNUNET_FS_STATUS_PUBLISH_START;
942   *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
943   GNUNET_FS_file_information_sync_ (fi);
944   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)
945       && (fi->dir != NULL))
946   {
947     /* process entries in directory */
948     pc->skip_next_fi_callback = GNUNET_YES;
949     GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
950   }
951   return GNUNET_OK;
952 }
953
954
955 /**
956  * Signal the FS's progress function that we are suspending
957  * an upload.
958  *
959  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
960  * @param fi the entry in the publish-structure
961  * @param length length of the file or directory
962  * @param meta metadata for the file or directory (can be modified)
963  * @param uri pointer to the keywords that will be used for this entry (can be modified)
964  * @param bo block options
965  * @param do_index should we index?
966  * @param client_info pointer to client context set upon creation (can be modified)
967  * @return GNUNET_OK to continue (always)
968  */
969 static int
970 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
971                     uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
972                     struct GNUNET_FS_Uri **uri,
973                     struct GNUNET_FS_BlockOptions *bo, int *do_index,
974                     void **client_info)
975 {
976   struct GNUNET_FS_PublishContext *pc = cls;
977   struct GNUNET_FS_ProgressInfo pi;
978   uint64_t off;
979
980   if (GNUNET_YES == pc->skip_next_fi_callback)
981   {
982     pc->skip_next_fi_callback = GNUNET_NO;
983     return GNUNET_OK;
984   }
985   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
986   {
987     /* process entries in directory */
988     pc->skip_next_fi_callback = GNUNET_YES;
989     GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
990   }
991   if (NULL != pc->ksk_pc)
992   {
993     GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
994     pc->ksk_pc = NULL;
995   }
996   if (NULL != pc->sks_pc)
997   {
998     GNUNET_FS_publish_sks_cancel (pc->sks_pc);
999     pc->sks_pc = NULL;
1000   }
1001   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
1002   GNUNET_free_non_null (fi->serialization);
1003   fi->serialization = NULL;
1004   off = (fi->chk_uri == NULL) ? 0 : length;
1005   pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1006   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1007   *client_info = NULL;
1008   if (NULL != pc->qre)
1009   {
1010     GNUNET_DATASTORE_cancel (pc->qre);
1011     pc->qre = NULL;
1012   }
1013   if (NULL != pc->dsh)
1014   {
1015     GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1016     pc->dsh = NULL;
1017   }
1018   pc->rid = 0;
1019   return GNUNET_OK;
1020 }
1021
1022
1023 /**
1024  * Create SUSPEND event for the given publish operation
1025  * and then clean up our state (without stop signal).
1026  *
1027  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1028  */
1029 void
1030 GNUNET_FS_publish_signal_suspend_ (void *cls)
1031 {
1032   struct GNUNET_FS_PublishContext *pc = cls;
1033
1034   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1035   {
1036     GNUNET_SCHEDULER_cancel (pc->upload_task);
1037     pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1038   }
1039   GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1040   GNUNET_FS_end_top (pc->h, pc->top);
1041   pc->top = NULL;
1042   publish_cleanup (pc);
1043 }
1044
1045
1046 /**
1047  * We have gotten a reply for our space reservation request.
1048  * Either fail (insufficient space) or start publishing for good.
1049  *
1050  * @param cls the 'struct GNUNET_FS_PublishContext*'
1051  * @param success positive reservation ID on success
1052  * @param min_expiration minimum expiration time required for content to be stored
1053  * @param msg error message on error, otherwise NULL
1054  */
1055 static void
1056 finish_reserve (void *cls, int success, 
1057                 struct GNUNET_TIME_Absolute min_expiration,
1058                 const char *msg)
1059 {
1060   struct GNUNET_FS_PublishContext *pc = cls;
1061
1062   pc->qre = NULL;
1063   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1064   if ((msg != NULL) || (success <= 0))
1065   {
1066     GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1067                      msg);
1068     signal_publish_error (pc->fi, pc, pc->fi->emsg);
1069     return;
1070   }
1071   pc->rid = success;
1072   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1073   pc->upload_task =
1074       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1075                                           &GNUNET_FS_publish_main_, pc);
1076 }
1077
1078
1079 /**
1080  * Publish a file or directory.
1081  *
1082  * @param h handle to the file sharing subsystem
1083  * @param fi information about the file or directory structure to publish
1084  * @param namespace namespace to publish the file in, NULL for no namespace
1085  * @param nid identifier to use for the publishd content in the namespace
1086  *        (can be NULL, must be NULL if namespace is NULL)
1087  * @param nuid update-identifier that will be used for future updates
1088  *        (can be NULL, must be NULL if namespace or nid is NULL)
1089  * @param options options for the publication
1090  * @return context that can be used to control the publish operation
1091  */
1092 struct GNUNET_FS_PublishContext *
1093 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1094                          struct GNUNET_FS_FileInformation *fi,
1095                          struct GNUNET_FS_Namespace *namespace, const char *nid,
1096                          const char *nuid,
1097                          enum GNUNET_FS_PublishOptions options)
1098 {
1099   struct GNUNET_FS_PublishContext *ret;
1100   struct GNUNET_DATASTORE_Handle *dsh;
1101
1102   GNUNET_assert (NULL != h);
1103   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1104   {
1105     dsh = GNUNET_DATASTORE_connect (h->cfg);
1106     if (NULL == dsh)
1107       return NULL;
1108   }
1109   else
1110   {
1111     dsh = NULL;
1112   }
1113   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1114   ret->dsh = dsh;
1115   ret->h = h;
1116   ret->fi = fi;
1117   ret->namespace = namespace;
1118   ret->options = options;
1119   if (namespace != NULL)
1120   {
1121     namespace->rc++;
1122     GNUNET_assert (NULL != nid);
1123     ret->nid = GNUNET_strdup (nid);
1124     if (NULL != nuid)
1125       ret->nuid = GNUNET_strdup (nuid);
1126   }
1127   /* signal start */
1128   GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1129   ret->fi_pos = ret->fi;
1130   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1131   GNUNET_FS_publish_sync_ (ret);
1132   if (NULL != ret->dsh)
1133   {
1134     GNUNET_assert (NULL == ret->qre);
1135     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1136                 _
1137                 ("Reserving space for %u entries and %llu bytes for publication\n"),
1138                 (unsigned int) ret->reserve_entries,
1139                 (unsigned long long) ret->reserve_space);
1140     ret->qre =
1141         GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1142                                   ret->reserve_entries, UINT_MAX, UINT_MAX,
1143                                   GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1144                                   ret);
1145   }
1146   else
1147   {
1148     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1149     ret->upload_task =
1150         GNUNET_SCHEDULER_add_with_priority
1151         (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1152   }
1153   return ret;
1154 }
1155
1156
1157 /**
1158  * Signal the FS's progress function that we are stopping
1159  * an upload.
1160  *
1161  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1162  * @param fi the entry in the publish-structure
1163  * @param length length of the file or directory
1164  * @param meta metadata for the file or directory (can be modified)
1165  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1166  * @param bo block options (can be modified)
1167  * @param do_index should we index?
1168  * @param client_info pointer to client context set upon creation (can be modified)
1169  * @return GNUNET_OK to continue (always)
1170  */
1171 static int
1172 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1173                  uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1174                  struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1175                  int *do_index, void **client_info)
1176 {
1177   struct GNUNET_FS_PublishContext *pc = cls;
1178   struct GNUNET_FS_ProgressInfo pi;
1179   uint64_t off;
1180
1181   if (GNUNET_YES == pc->skip_next_fi_callback)
1182   {
1183     pc->skip_next_fi_callback = GNUNET_NO;
1184     return GNUNET_OK;
1185   }
1186   if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1187   {
1188     /* process entries in directory first */
1189     pc->skip_next_fi_callback = GNUNET_YES;
1190     GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1191   }
1192   if (fi->serialization != NULL)
1193   {
1194     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1195                                  fi->serialization);
1196     GNUNET_free (fi->serialization);
1197     fi->serialization = NULL;
1198   }
1199   off = (fi->chk_uri == NULL) ? 0 : length;
1200   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1201   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1202   *client_info = NULL;
1203   return GNUNET_OK;
1204 }
1205
1206
1207 /**
1208  * Stop an upload.  Will abort incomplete uploads (but
1209  * not remove blocks that have already been publishd) or
1210  * simply clean up the state for completed uploads.
1211  * Must NOT be called from within the event callback!
1212  *
1213  * @param pc context for the upload to stop
1214  */
1215 void
1216 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1217 {
1218   struct GNUNET_FS_ProgressInfo pi;
1219   uint64_t off;
1220
1221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1222   GNUNET_FS_end_top (pc->h, pc->top);
1223   if (NULL != pc->ksk_pc)
1224   {
1225     GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1226     pc->ksk_pc = NULL;
1227   }
1228   if (NULL != pc->sks_pc)
1229   {
1230     GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1231     pc->sks_pc = NULL;
1232   }
1233   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1234   {
1235     GNUNET_SCHEDULER_cancel (pc->upload_task);
1236     pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1237   }
1238   pc->skip_next_fi_callback = GNUNET_YES;
1239   GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1240
1241   if (pc->fi->serialization != NULL)
1242   {
1243     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1244                                  pc->fi->serialization);
1245     GNUNET_free (pc->fi->serialization);
1246     pc->fi->serialization = NULL;
1247   }
1248   off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1249
1250   if (pc->serialization != NULL)
1251   {
1252     GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1253                                  pc->serialization);
1254     GNUNET_free (pc->serialization);
1255     pc->serialization = NULL;
1256   }
1257   if (NULL != pc->qre)
1258   {
1259     GNUNET_DATASTORE_cancel (pc->qre);
1260     pc->qre = NULL;
1261   }
1262   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1263   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1264   publish_cleanup (pc);
1265 }
1266
1267
1268
1269 /* end of fs_publish.c */