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