0ca7cef2df7b2e96222a7cbc87599c7a9ee0f957
[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 2, 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 http://gnunet.org/encoding
25  * @author Krista Bennett
26  * @author Christian Grothoff
27  *
28  * TODO:
29  * - indexing cleanup: unindex on failure (can wait)
30  * - datastore reservation support (optimization)
31  * - location URIs (publish with anonymity-level zero)
32  */
33
34 #include "platform.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_fs_service.h"
39 #include "fs.h"
40 #include "fs_tree.h"
41
42 #define DEBUG_PUBLISH GNUNET_NO
43
44
45 /**
46  * Context for "ds_put_cont".
47  */
48 struct PutContCtx
49 {
50   /**
51    * Current publishing context.
52    */
53   struct GNUNET_FS_PublishContext *sc;
54
55   /**
56    * Specific file with the block.
57    */
58   struct GNUNET_FS_FileInformation *p;
59
60   /**
61    * Function to run next, if any (can be NULL).
62    */
63   GNUNET_SCHEDULER_Task cont;
64
65   /**
66    * Closure for cont.
67    */
68   void *cont_cls;
69 };
70
71
72 /**
73  * Fill in all of the generic fields for 
74  * a publish event and call the callback.
75  *
76  * @param pi structure to fill in
77  * @param sc overall publishing context
78  * @param p file information for the file being published
79  * @param offset where in the file are we so far
80  * @return value returned from callback
81  */
82 void *
83 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
84                                 struct GNUNET_FS_PublishContext *sc,
85                                 const struct GNUNET_FS_FileInformation *p,
86                                 uint64_t offset)
87 {
88   pi->value.publish.sc = sc;
89   pi->value.publish.fi = p;
90   pi->value.publish.cctx
91     = p->client_info;
92   pi->value.publish.pctx
93     = (NULL == p->dir) ? NULL : p->dir->client_info;
94   pi->value.publish.filename = p->filename;
95   pi->value.publish.size 
96     = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
97   pi->value.publish.eta 
98     = GNUNET_TIME_calculate_eta (p->start_time,
99                                  offset,
100                                  pi->value.publish.size);
101   pi->value.publish.completed = offset;
102   pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
103   pi->value.publish.anonymity = p->anonymity;
104   return sc->h->upcb (sc->h->upcb_cls,
105                       pi);
106 }
107
108
109 /**
110  * Cleanup the publish context, we're done with it.
111  *
112  * @param pc struct to clean up after
113  */
114 static void
115 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
116 {
117   GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
118   if (pc->namespace != NULL)
119     GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
120   GNUNET_free_non_null (pc->nid);  
121   GNUNET_free_non_null (pc->nuid);
122   GNUNET_free_non_null (pc->serialization);
123   GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
124   if (pc->client != NULL)
125     GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
126   GNUNET_free (pc);
127 }
128
129
130 /**
131  * Function called by the datastore API with
132  * the result from the PUT request.
133  *
134  * @param cls our closure
135  * @param success GNUNET_OK on success
136  * @param msg error message (or NULL)
137  */
138 static void
139 ds_put_cont (void *cls,
140              int success,
141              const char *msg)
142 {
143   struct PutContCtx *pcc = cls;
144   struct GNUNET_FS_ProgressInfo pi;
145
146   if (GNUNET_SYSERR == pcc->sc->in_network_wait)
147     {
148       /* we were aborted in the meantime,
149          finish shutdown! */
150       publish_cleanup (pcc->sc);
151       return;
152     }
153   GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
154   pcc->sc->in_network_wait = GNUNET_NO;
155   if (GNUNET_OK != success)
156     {
157       GNUNET_asprintf (&pcc->p->emsg, 
158                        _("Upload failed: %s"),
159                        msg);
160       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
161       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
162       pi.value.publish.specifics.error.message = pcc->p->emsg;
163       pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
164     }
165   if (NULL != pcc->cont)
166     pcc->sc->upload_task 
167       = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
168                                             GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
169                                             pcc->cont,
170                                             pcc->cont_cls);
171   GNUNET_free (pcc);
172 }
173
174
175 /**
176  * Generate the callback that signals clients
177  * that a file (or directory) has been completely
178  * published.
179  *
180  * @param p the completed upload
181  * @param sc context of the publication
182  */
183 static void 
184 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
185                            struct GNUNET_FS_PublishContext *sc)
186 {
187   struct GNUNET_FS_ProgressInfo pi;
188   
189   pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
190   pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
191   pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
192   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
193                                                    GNUNET_ntohll (p->chk_uri->data.chk.file_length));
194 }
195
196
197 /**
198  * Generate the callback that signals clients
199  * that a file (or directory) has encountered
200  * a problem during publication.
201  *
202  * @param p the upload that had trouble
203  * @param sc context of the publication
204  * @param emsg error message
205  */
206 static void 
207 signal_publish_error (struct GNUNET_FS_FileInformation *p,
208                       struct GNUNET_FS_PublishContext *sc,
209                       const char *emsg)
210 {
211   struct GNUNET_FS_ProgressInfo pi;
212   
213   p->emsg = GNUNET_strdup (emsg);
214   pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
215   pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
216   pi.value.publish.specifics.error.message =emsg;
217   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
218 }
219
220
221 /**
222  * We've finished publishing the SBlock as part of a larger upload.
223  * Check the result and complete the larger upload.
224  *
225  * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
226  * @param uri URI of the published SBlock
227  * @param emsg NULL on success, otherwise error message
228  */
229 static void
230 publish_sblocks_cont (void *cls,
231                       const struct GNUNET_FS_Uri *uri,
232                       const char *emsg)
233 {
234   struct GNUNET_FS_PublishContext *pc = cls;
235   if (NULL != emsg)
236     {
237       signal_publish_error (pc->fi,
238                             pc,
239                             emsg);
240       GNUNET_FS_publish_sync_ (pc);
241       return;
242     }  
243   // FIXME: release the datastore reserve here!
244   signal_publish_completion (pc->fi, pc);
245   pc->all_done = GNUNET_YES;
246   GNUNET_FS_publish_sync_ (pc);
247 }
248
249
250 /**
251  * We are almost done publishing the structure,
252  * add SBlocks (if needed).
253  *
254  * @param sc overall upload data
255  */
256 static void
257 publish_sblock (struct GNUNET_FS_PublishContext *sc)
258 {
259   if (NULL != sc->namespace)
260     GNUNET_FS_publish_sks (sc->h,
261                            sc->namespace,
262                            sc->nid,
263                            sc->nuid,
264                            sc->fi->meta,
265                            sc->fi->chk_uri,
266                            sc->fi->expirationTime,
267                            sc->fi->anonymity,
268                            sc->fi->priority,
269                            sc->options,
270                            &publish_sblocks_cont,
271                            sc);
272   else
273     publish_sblocks_cont (sc, NULL, NULL);
274 }
275
276
277 /**
278  * We've finished publishing a KBlock as part of a larger upload.
279  * Check the result and continue the larger upload.
280  *
281  * @param cls the "struct GNUNET_FS_PublishContext*"
282  *        of the larger upload
283  * @param uri URI of the published blocks
284  * @param emsg NULL on success, otherwise error message
285  */
286 static void
287 publish_kblocks_cont (void *cls,
288                       const struct GNUNET_FS_Uri *uri,
289                       const char *emsg)
290 {
291   struct GNUNET_FS_PublishContext *pc = cls;
292   struct GNUNET_FS_FileInformation *p = pc->fi_pos;
293
294   if (NULL != emsg)
295     {
296       signal_publish_error (p, pc, emsg);
297       GNUNET_FS_file_information_sync_ (p);
298       GNUNET_FS_publish_sync_ (pc);
299       pc->upload_task 
300         = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
301                                               GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
302                                               &GNUNET_FS_publish_main_,
303                                               pc);
304       return;
305     }
306   if (NULL != p->dir)
307     signal_publish_completion (p, pc);    
308   /* move on to next file */
309   if (NULL != p->next)
310     pc->fi_pos = p->next;
311   else
312     pc->fi_pos = p->dir;
313   GNUNET_FS_publish_sync_ (pc);
314   pc->upload_task 
315     = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
316                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
317                                           &GNUNET_FS_publish_main_,
318                                           pc);
319 }
320
321
322 /**
323  * Function called by the tree encoder to obtain
324  * a block of plaintext data (for the lowest level
325  * of the tree).
326  *
327  * @param cls our publishing context
328  * @param offset identifies which block to get
329  * @param max (maximum) number of bytes to get; returning
330  *        fewer will also cause errors
331  * @param buf where to copy the plaintext buffer
332  * @param emsg location to store an error message (on error)
333  * @return number of bytes copied to buf, 0 on error
334  */
335 static size_t
336 block_reader (void *cls,
337               uint64_t offset,
338               size_t max, 
339               void *buf,
340               char **emsg)
341 {
342   struct GNUNET_FS_PublishContext *sc = cls;
343   struct GNUNET_FS_FileInformation *p;
344   size_t pt_size;
345   const char *dd;
346
347   p = sc->fi_pos;
348   if (p->is_directory)
349     {
350       pt_size = GNUNET_MIN(max,
351                            p->data.dir.dir_size - offset);
352       dd = p->data.dir.dir_data;
353       memcpy (buf,
354               &dd[offset],
355               pt_size);
356     }
357   else
358     {
359       pt_size = GNUNET_MIN(max,
360                            p->data.file.file_size - offset);
361       if (pt_size == 0)
362         return 0; /* calling reader with pt_size==0 
363                      might free buf, so don't! */
364       if (pt_size !=
365           p->data.file.reader (p->data.file.reader_cls,
366                                offset,
367                                pt_size,
368                                buf,
369                                emsg))
370         return 0;
371     }
372   return pt_size;
373 }
374
375
376 /**
377  * The tree encoder has finished processing a
378  * file.   Call it's finish method and deal with
379  * the final result.
380  *
381  * @param cls our publishing context
382  * @param tc scheduler's task context (not used)
383  */
384 static void 
385 encode_cont (void *cls,
386              const struct GNUNET_SCHEDULER_TaskContext *tc)
387 {
388   struct GNUNET_FS_PublishContext *sc = cls;
389   struct GNUNET_FS_FileInformation *p;
390   struct GNUNET_FS_ProgressInfo pi;
391   char *emsg;
392   
393   p = sc->fi_pos;
394   GNUNET_FS_tree_encoder_finish (p->te,
395                                  &p->chk_uri,
396                                  &emsg);
397   p->te = NULL;
398   if (NULL != emsg)
399     {
400       GNUNET_asprintf (&p->emsg, 
401                        _("Upload failed: %s"),
402                        emsg);
403       GNUNET_free (emsg);
404       pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
405       pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
406       pi.value.publish.specifics.error.message = p->emsg;
407       p->client_info =  GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
408     }
409   /* continue with main */
410   sc->upload_task 
411     = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
412                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
413                                           &GNUNET_FS_publish_main_,
414                                           sc);
415 }
416
417
418 /**
419  * Function called asking for the current (encoded)
420  * block to be processed.  After processing the
421  * client should either call "GNUNET_FS_tree_encode_next"
422  * or (on error) "GNUNET_FS_tree_encode_finish".
423  *
424  * @param cls closure
425  * @param query the query for the block (key for lookup in the datastore)
426  * @param offset offset of the block in the file
427  * @param type type of the block (IBLOCK or DBLOCK)
428  * @param block the (encrypted) block
429  * @param block_size size of block (in bytes)
430  */
431 static void 
432 block_proc (void *cls,
433             const GNUNET_HashCode *query,
434             uint64_t offset,
435             enum GNUNET_BLOCK_Type type,
436             const void *block,
437             uint16_t block_size)
438 {
439   struct GNUNET_FS_PublishContext *sc = cls;
440   struct GNUNET_FS_FileInformation *p;
441   struct PutContCtx * dpc_cls;
442   struct OnDemandBlock odb;
443
444   p = sc->fi_pos;
445   if (NULL == sc->dsh)
446     {
447       sc->upload_task
448         = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
449                                               GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
450                                               &GNUNET_FS_publish_main_,
451                                               sc);
452       return;
453     }
454   
455   GNUNET_assert (GNUNET_NO == sc->in_network_wait);
456   sc->in_network_wait = GNUNET_YES;
457   dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
458   dpc_cls->cont = &GNUNET_FS_publish_main_;
459   dpc_cls->cont_cls = sc;
460   dpc_cls->sc = sc;
461   dpc_cls->p = p;
462   if ( (! p->is_directory) &&
463        (GNUNET_YES == p->data.file.do_index) &&
464        (type == GNUNET_BLOCK_TYPE_DBLOCK) )
465     {
466 #if DEBUG_PUBLISH
467       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468                   "Indexing block `%s' for offset %llu with index size %u\n",
469                   GNUNET_h2s (query),
470                   (unsigned long long) offset,
471                   sizeof (struct OnDemandBlock));
472 #endif
473       odb.offset = GNUNET_htonll (offset);
474       odb.file_id = p->data.file.file_id;
475       GNUNET_DATASTORE_put (sc->dsh,
476                             sc->rid,
477                             query,
478                             sizeof(struct OnDemandBlock),
479                             &odb,
480                             GNUNET_BLOCK_TYPE_ONDEMAND,
481                             p->priority,
482                             p->anonymity,
483                             p->expirationTime,
484                             GNUNET_CONSTANTS_SERVICE_TIMEOUT,
485                             &ds_put_cont,
486                             dpc_cls);     
487       return;
488     }
489 #if DEBUG_PUBLISH
490   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491               "Publishing block `%s' for offset %llu with size %u\n",
492               GNUNET_h2s (query),
493               (unsigned long long) offset,
494               (unsigned int) block_size);
495 #endif
496   GNUNET_DATASTORE_put (sc->dsh,
497                         sc->rid,
498                         query,
499                         block_size,
500                         block,
501                         type,
502                         p->priority,
503                         p->anonymity,
504                         p->expirationTime,
505                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
506                         &ds_put_cont,
507                         dpc_cls);
508 }
509
510
511 /**
512  * Function called with information about our
513  * progress in computing the tree encoding.
514  *
515  * @param cls closure
516  * @param offset where are we in the file
517  * @param pt_block plaintext of the currently processed block
518  * @param pt_size size of pt_block
519  * @param depth depth of the block in the tree
520  */
521 static void 
522 progress_proc (void *cls,
523                uint64_t offset,
524                const void *pt_block,
525                size_t pt_size,
526                unsigned int depth)
527 {                      
528   struct GNUNET_FS_PublishContext *sc = cls;
529   struct GNUNET_FS_FileInformation *p;
530   struct GNUNET_FS_ProgressInfo pi;
531
532   p = sc->fi_pos;
533   pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
534   pi.value.publish.specifics.progress.data = pt_block;
535   pi.value.publish.specifics.progress.offset = offset;
536   pi.value.publish.specifics.progress.data_len = pt_size;
537   pi.value.publish.specifics.progress.depth = depth;
538   p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
539 }
540
541
542 /**
543  * We are uploading a file or directory; load (if necessary) the next
544  * block into memory, encrypt it and send it to the FS service.  Then
545  * continue with the main task.
546  *
547  * @param sc overall upload data
548  */
549 static void
550 publish_content (struct GNUNET_FS_PublishContext *sc) 
551 {
552   struct GNUNET_FS_FileInformation *p;
553   char *emsg;
554   struct GNUNET_FS_DirectoryBuilder *db;
555   struct GNUNET_FS_FileInformation *dirpos;
556   void *raw_data;
557   uint64_t size;
558
559   p = sc->fi_pos;
560   GNUNET_assert (p != NULL);
561   if (NULL == p->te)
562     {
563       if (p->is_directory)
564         {
565           db = GNUNET_FS_directory_builder_create (p->meta);
566           dirpos = p->data.dir.entries;
567           while (NULL != dirpos)
568             {
569               if (dirpos->is_directory)
570                 {
571                   raw_data = dirpos->data.dir.dir_data;
572                   dirpos->data.dir.dir_data = NULL;
573                 }
574               else
575                 {
576                   raw_data = NULL;
577                   if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
578                        (dirpos->data.file.file_size > 0) )
579                     {
580                       raw_data = GNUNET_malloc (dirpos->data.file.file_size);
581                       emsg = NULL;
582                       if (dirpos->data.file.file_size !=
583                           dirpos->data.file.reader (dirpos->data.file.reader_cls,
584                                                     0,
585                                                     dirpos->data.file.file_size,
586                                                     raw_data,
587                                                     &emsg))
588                         {
589                           GNUNET_free_non_null (emsg);
590                           GNUNET_free (raw_data);
591                           raw_data = NULL;
592                         } 
593                     }
594                 }
595               GNUNET_FS_directory_builder_add (db,
596                                                dirpos->chk_uri,
597                                                dirpos->meta,
598                                                raw_data);
599               GNUNET_free_non_null (raw_data);
600               dirpos = dirpos->next;
601             }
602           GNUNET_FS_directory_builder_finish (db,
603                                               &p->data.dir.dir_size,
604                                               &p->data.dir.dir_data);
605           GNUNET_FS_file_information_sync_ (p);
606         }
607       size = (p->is_directory) 
608         ? p->data.dir.dir_size 
609         : p->data.file.file_size;
610       p->te = GNUNET_FS_tree_encoder_create (sc->h,
611                                              size,
612                                              sc,
613                                              &block_reader,
614                                              &block_proc,
615                                              &progress_proc,
616                                              &encode_cont);
617
618     }
619   GNUNET_FS_tree_encoder_next (p->te);
620 }
621
622
623 /**
624  * Process the response (or lack thereof) from
625  * the "fs" service to our 'start index' request.
626  *
627  * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
628  * @param msg the response we got
629  */
630 static void
631 process_index_start_response (void *cls,
632                               const struct GNUNET_MessageHeader *msg)
633 {
634   struct GNUNET_FS_PublishContext *sc = cls;
635   struct GNUNET_FS_FileInformation *p;
636   const char *emsg;
637   uint16_t msize;
638
639   GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
640   sc->client = NULL;
641   p = sc->fi_pos;
642   if (msg == NULL)
643     {
644       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
645                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
646                   p->filename,
647                   _("timeout on index-start request to `fs' service"));
648       p->data.file.do_index = GNUNET_NO;
649       GNUNET_FS_file_information_sync_ (p);
650       publish_content (sc);
651       return;
652     }
653   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
654     {
655       msize = ntohs (msg->size);
656       emsg = (const char *) &msg[1];
657       if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
658            (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
659         emsg = gettext_noop ("unknown error");
660       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
662                   p->filename,
663                   gettext (emsg));
664       p->data.file.do_index = GNUNET_NO;
665       GNUNET_FS_file_information_sync_ (p);
666       publish_content (sc);
667       return;
668     }
669   p->data.file.index_start_confirmed = GNUNET_YES;
670   /* success! continue with indexing */
671   GNUNET_FS_file_information_sync_ (p);
672   publish_content (sc);
673 }
674
675
676 /**
677  * Function called once the hash computation over an
678  * indexed file has completed.
679  *
680  * @param cls closure, our publishing context
681  * @param res resulting hash, NULL on error
682  */
683 static void 
684 hash_for_index_cb (void *cls,
685                    const GNUNET_HashCode *
686                    res)
687 {
688   struct GNUNET_FS_PublishContext *sc = cls;
689   struct GNUNET_FS_FileInformation *p;
690   struct IndexStartMessage *ism;
691   size_t slen;
692   struct GNUNET_CLIENT_Connection *client;
693   uint32_t dev;
694   uint64_t ino;
695   char *fn;
696
697   p = sc->fi_pos;
698   if (NULL == res) 
699     {
700       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
702                   p->filename,
703                   _("failed to compute hash"));
704       p->data.file.do_index = GNUNET_NO;
705       GNUNET_FS_file_information_sync_ (p);
706       publish_content (sc);
707       return;
708     }
709   if (GNUNET_YES == p->data.file.index_start_confirmed)
710     {
711       publish_content (sc);
712       return;
713     }
714   fn = GNUNET_STRINGS_filename_expand (p->filename);
715   slen = strlen (fn) + 1;
716   if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
717     {
718       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
719                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
720                   fn,
721                   _("filename too long"));
722       GNUNET_free (fn);
723       p->data.file.do_index = GNUNET_NO;
724       GNUNET_FS_file_information_sync_ (p);
725       publish_content (sc);
726       return;
727     }
728 #if DEBUG_PUBLISH
729   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730               "Hash of indexed file `%s' is `%s'\n",
731               p->data.file.filename,
732               GNUNET_h2s (res));
733 #endif
734   client = GNUNET_CLIENT_connect (sc->h->sched,
735                                   "fs",
736                                   sc->h->cfg);
737   if (NULL == client)
738     {
739       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
740                   _("Can not index file `%s': %s.  Will try to insert instead.\n"),
741                   p->filename,
742                   _("could not connect to `fs' service"));
743       p->data.file.do_index = GNUNET_NO;
744       publish_content (sc);
745       GNUNET_free (fn);
746       return;
747     }
748   if (p->data.file.have_hash != GNUNET_YES)
749     {
750       p->data.file.file_id = *res;
751       p->data.file.have_hash = GNUNET_YES;
752       GNUNET_FS_file_information_sync_ (p);
753     }
754   ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
755                        slen);
756   ism->header.size = htons(sizeof(struct IndexStartMessage) +
757                            slen);
758   ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
759   if (GNUNET_OK ==
760       GNUNET_DISK_file_get_identifiers (p->filename,
761                                         &dev,
762                                         &ino))
763     {
764       ism->device = htonl (dev);
765       ism->inode = GNUNET_htonll(ino);
766     }
767   else
768     {
769       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770                   _("Failed to get file identifiers for `%s'\n"),
771                   p->filename);
772     }
773   ism->file_id = *res;
774   memcpy (&ism[1],
775           fn,
776           slen);
777   GNUNET_free (fn);
778   sc->client = client;
779   GNUNET_break (GNUNET_YES ==
780                 GNUNET_CLIENT_transmit_and_get_response (client,
781                                                          &ism->header,
782                                                          GNUNET_TIME_UNIT_FOREVER_REL,
783                                                          GNUNET_YES,
784                                                          &process_index_start_response,
785                                                          sc));
786   GNUNET_free (ism);
787 }
788
789
790 /**
791  * Main function that performs the upload.
792  *
793  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
794  * @param tc task context
795  */
796 void
797 GNUNET_FS_publish_main_ (void *cls,
798                          const struct GNUNET_SCHEDULER_TaskContext *tc)
799 {
800   struct GNUNET_FS_PublishContext *pc = cls;
801   struct GNUNET_FS_ProgressInfo pi;
802   struct GNUNET_FS_FileInformation *p;
803   char *fn;
804
805   pc->upload_task = GNUNET_SCHEDULER_NO_TASK;  
806   p = pc->fi_pos;
807   if (NULL == p)
808     {
809       /* upload of entire hierarchy complete,
810          publish namespace entries */
811       GNUNET_FS_publish_sync_ (pc);
812       publish_sblock (pc);
813       return;
814     }
815   /* find starting position */
816   while ( (p->is_directory) &&
817           (NULL != p->data.dir.entries) &&
818           (NULL == p->emsg) &&
819           (NULL == p->data.dir.entries->chk_uri) )
820     {
821       p = p->data.dir.entries;
822       pc->fi_pos = p;
823       GNUNET_FS_publish_sync_ (pc);
824     }
825   /* abort on error */
826   if (NULL != p->emsg)
827     {
828       /* error with current file, abort all
829          related files as well! */
830       while (NULL != p->dir)
831         {
832           fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
833                                                        EXTRACTOR_METATYPE_FILENAME);
834           p = p->dir;
835           if (fn != NULL)
836             {
837               GNUNET_asprintf (&p->emsg, 
838                                _("Recursive upload failed at `%s': %s"),
839                                fn,
840                                p->emsg);
841               GNUNET_free (fn);
842             }
843           else
844             {
845               GNUNET_asprintf (&p->emsg, 
846                                _("Recursive upload failed: %s"),
847                                p->emsg);              
848             }
849           pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
850           pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
851           pi.value.publish.specifics.error.message = p->emsg;
852           p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
853         }
854       pc->all_done = GNUNET_YES;
855       GNUNET_FS_publish_sync_ (pc);
856       return;
857     }
858   /* handle completion */
859   if (NULL != p->chk_uri)
860     {
861       GNUNET_FS_publish_sync_ (pc);
862       /* upload of "p" complete, publish KBlocks! */
863       if (p->keywords != NULL)
864         {
865           GNUNET_FS_publish_ksk (pc->h,
866                                  p->keywords,
867                                  p->meta,
868                                  p->chk_uri,
869                                  p->expirationTime,
870                                  p->anonymity,
871                                  p->priority,
872                                  pc->options,
873                                  &publish_kblocks_cont,
874                                  pc);
875         }
876       else
877         {
878           publish_kblocks_cont (pc,
879                                 p->chk_uri,
880                                 NULL);
881         }
882       return;
883     }
884   if ( (!p->is_directory) &&
885        (p->data.file.do_index) )
886     {
887       if (NULL == p->filename)
888         {
889           p->data.file.do_index = GNUNET_NO;
890           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
891                       _("Can not index file `%s': %s.  Will try to insert instead.\n"),
892                       "<no-name>",
893                       _("needs to be an actual file"));
894           GNUNET_FS_file_information_sync_ (p);
895           publish_content (pc);
896           return;
897         }      
898       if (p->data.file.have_hash)
899         {
900           hash_for_index_cb (pc,
901                              &p->data.file.file_id);
902         }
903       else
904         {
905           p->start_time = GNUNET_TIME_absolute_get ();
906           GNUNET_CRYPTO_hash_file (pc->h->sched,
907                                    GNUNET_SCHEDULER_PRIORITY_IDLE,
908                                    p->filename,
909                                    HASHING_BLOCKSIZE,
910                                    &hash_for_index_cb,
911                                    pc);
912         }
913       return;
914     }
915   publish_content (pc);
916 }
917
918
919 /**
920  * Signal the FS's progress function that we are starting
921  * an upload.
922  *
923  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
924  * @param fi the entry in the publish-structure
925  * @param length length of the file or directory
926  * @param meta metadata for the file or directory (can be modified)
927  * @param uri pointer to the keywords that will be used for this entry (can be modified)
928  * @param anonymity pointer to selected anonymity level (can be modified)
929  * @param priority pointer to selected priority (can be modified)
930  * @param expirationTime pointer to selected expiration time (can be modified)
931  * @param client_info pointer to client context set upon creation (can be modified)
932  * @return GNUNET_OK to continue (always)
933  */
934 static int
935 fip_signal_start(void *cls,
936                  struct GNUNET_FS_FileInformation *fi,
937                  uint64_t length,
938                  struct GNUNET_CONTAINER_MetaData *meta,
939                  struct GNUNET_FS_Uri **uri,
940                  uint32_t *anonymity,
941                  uint32_t *priority,
942                  struct GNUNET_TIME_Absolute *expirationTime,
943                  void **client_info)
944 {
945   struct GNUNET_FS_PublishContext *sc = cls;
946   struct GNUNET_FS_ProgressInfo pi;
947
948   pi.status = GNUNET_FS_STATUS_PUBLISH_START;
949   *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
950   GNUNET_FS_file_information_sync_ (fi);
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 anonymity pointer to selected anonymity level (can be modified)
965  * @param priority pointer to selected priority (can be modified)
966  * @param expirationTime pointer to selected expiration time (can be modified)
967  * @param client_info pointer to client context set upon creation (can be modified)
968  * @return GNUNET_OK to continue (always)
969  */
970 static int
971 fip_signal_suspend(void *cls,
972                    struct GNUNET_FS_FileInformation *fi,
973                    uint64_t length,
974                    struct GNUNET_CONTAINER_MetaData *meta,
975                    struct GNUNET_FS_Uri **uri,
976                    uint32_t *anonymity,
977                    uint32_t *priority,
978                    struct GNUNET_TIME_Absolute *expirationTime,
979                    void **client_info)
980 {
981   struct GNUNET_FS_PublishContext*sc = cls;
982   struct GNUNET_FS_ProgressInfo pi;
983   uint64_t off;
984
985   GNUNET_free_non_null (fi->serialization);
986   fi->serialization = NULL;    
987   off = (fi->chk_uri == NULL) ? 0 : length;
988   pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
989   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
990   *client_info = NULL;
991   return GNUNET_OK;
992 }
993
994
995 /**
996  * Create SUSPEND event for the given publish operation
997  * and then clean up our state (without stop signal).
998  *
999  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1000  */
1001 void
1002 GNUNET_FS_publish_signal_suspend_ (void *cls)
1003 {
1004   struct GNUNET_FS_PublishContext *pc = cls;
1005
1006   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1007     {
1008       GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1009       pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1010     }
1011   GNUNET_FS_file_information_inspect (pc->fi,
1012                                       &fip_signal_suspend,
1013                                       pc);
1014   GNUNET_FS_end_top (pc->h, pc->top);
1015   publish_cleanup (pc);
1016 }
1017
1018 /**
1019  * Publish a file or directory.
1020  *
1021  * @param h handle to the file sharing subsystem
1022  * @param fi information about the file or directory structure to publish
1023  * @param namespace namespace to publish the file in, NULL for no namespace
1024  * @param nid identifier to use for the publishd content in the namespace
1025  *        (can be NULL, must be NULL if namespace is NULL)
1026  * @param nuid update-identifier that will be used for future updates 
1027  *        (can be NULL, must be NULL if namespace or nid is NULL)
1028  * @param options options for the publication 
1029  * @return context that can be used to control the publish operation
1030  */
1031 struct GNUNET_FS_PublishContext *
1032 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1033                          struct GNUNET_FS_FileInformation *fi,
1034                          struct GNUNET_FS_Namespace *namespace,
1035                          const char *nid,
1036                          const char *nuid,
1037                          enum GNUNET_FS_PublishOptions options)
1038 {
1039   struct GNUNET_FS_PublishContext *ret;
1040   struct GNUNET_DATASTORE_Handle *dsh;
1041
1042   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1043     {
1044       dsh = GNUNET_DATASTORE_connect (h->cfg,
1045                                       h->sched);
1046       if (NULL == dsh)
1047         return NULL;
1048     }
1049   else
1050     {
1051       dsh = NULL;
1052     }
1053   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1054   ret->dsh = dsh;
1055   ret->h = h;
1056   ret->fi = fi;
1057   ret->namespace = namespace;
1058   if (namespace != NULL)
1059     {
1060       namespace->rc++;
1061       GNUNET_assert (NULL != nid);
1062       ret->nid = GNUNET_strdup (nid);
1063       if (NULL != nuid)
1064         ret->nuid = GNUNET_strdup (nuid);
1065     }
1066   /* signal start */
1067   GNUNET_FS_file_information_inspect (ret->fi,
1068                                       &fip_signal_start,
1069                                       ret);
1070   ret->fi_pos = ret->fi;
1071   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1072   GNUNET_FS_publish_sync_ (ret);
1073   // FIXME: calculate space needed for "fi"
1074   // and reserve as first task (then trigger
1075   // "publish_main" from that continuation)!
1076   ret->upload_task 
1077     = GNUNET_SCHEDULER_add_with_priority (h->sched,
1078                                           GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1079                                           &GNUNET_FS_publish_main_,
1080                                           ret);
1081   return ret;
1082 }
1083
1084
1085 /**
1086  * Signal the FS's progress function that we are stopping
1087  * an upload.
1088  *
1089  * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1090  * @param fi the entry in the publish-structure
1091  * @param length length of the file or directory
1092  * @param meta metadata for the file or directory (can be modified)
1093  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1094  * @param anonymity pointer to selected anonymity level (can be modified)
1095  * @param priority pointer to selected priority (can be modified)
1096  * @param expirationTime pointer to selected expiration time (can be modified)
1097  * @param client_info pointer to client context set upon creation (can be modified)
1098  * @return GNUNET_OK to continue (always)
1099  */
1100 static int
1101 fip_signal_stop(void *cls,
1102                 struct GNUNET_FS_FileInformation *fi,
1103                 uint64_t length,
1104                 struct GNUNET_CONTAINER_MetaData *meta,
1105                 struct GNUNET_FS_Uri **uri,
1106                 uint32_t *anonymity,
1107                 uint32_t *priority,
1108                 struct GNUNET_TIME_Absolute *expirationTime,
1109                 void **client_info)
1110 {
1111   struct GNUNET_FS_PublishContext*sc = cls;
1112   struct GNUNET_FS_ProgressInfo pi;
1113   uint64_t off;
1114
1115   if (fi->serialization != NULL) 
1116     {
1117       if (0 != UNLINK (fi->serialization))
1118         {
1119           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1120                                     "unlink",
1121                                     fi->serialization); 
1122         }
1123       GNUNET_free (fi->serialization);
1124       fi->serialization = NULL;
1125     }
1126   off = (fi->chk_uri == NULL) ? 0 : length;
1127   pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1128   GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1129   *client_info = NULL;
1130   return GNUNET_OK;
1131 }
1132
1133
1134 /**
1135  * Stop an upload.  Will abort incomplete uploads (but 
1136  * not remove blocks that have already been publishd) or
1137  * simply clean up the state for completed uploads.
1138  * Must NOT be called from within the event callback!
1139  *
1140  * @param pc context for the upload to stop
1141  */
1142 void 
1143 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1144 {
1145   GNUNET_FS_end_top (pc->h, pc->top);
1146   if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1147     {
1148       GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1149       pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1150     }
1151   if (pc->serialization != NULL) 
1152     {
1153       GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1154       GNUNET_free (pc->serialization);
1155       pc->serialization = NULL;
1156     }
1157   GNUNET_FS_file_information_inspect (pc->fi,
1158                                       &fip_signal_stop,
1159                                       pc);
1160   if (GNUNET_YES == pc->in_network_wait)
1161     {
1162       pc->in_network_wait = GNUNET_SYSERR;
1163       return;
1164     }
1165   publish_cleanup (pc);
1166 }
1167
1168
1169 /**
1170  * Context for the KSK publication.
1171  */
1172 struct PublishKskContext
1173 {
1174
1175   /**
1176    * Keywords to use.
1177    */
1178   struct GNUNET_FS_Uri *ksk_uri;
1179
1180   /**
1181    * Global FS context.
1182    */
1183   struct GNUNET_FS_Handle *h;
1184
1185   /**
1186    * The master block that we are sending
1187    * (in plaintext), has "mdsize+slen" more
1188    * bytes than the struct would suggest.
1189    */
1190   struct KBlock *kb;
1191
1192   /**
1193    * Buffer of the same size as "kb" for
1194    * the encrypted version.
1195    */ 
1196   struct KBlock *cpy;
1197
1198   /**
1199    * Handle to the datastore, NULL if we are just
1200    * simulating.
1201    */
1202   struct GNUNET_DATASTORE_Handle *dsh;
1203
1204   /**
1205    * Function to call once we're done.
1206    */
1207   GNUNET_FS_PublishContinuation cont;
1208
1209   /**
1210    * Closure for cont.
1211    */ 
1212   void *cont_cls;
1213
1214   /**
1215    * When should the KBlocks expire?
1216    */
1217   struct GNUNET_TIME_Absolute expirationTime;
1218
1219   /**
1220    * Size of the serialized metadata.
1221    */
1222   ssize_t mdsize;
1223
1224   /**
1225    * Size of the (CHK) URI as a string.
1226    */
1227   size_t slen;
1228
1229   /**
1230    * Keyword that we are currently processing.
1231    */
1232   unsigned int i;
1233
1234   /**
1235    * Anonymity level for the KBlocks.
1236    */
1237   uint32_t anonymity;
1238
1239   /**
1240    * Priority for the KBlocks.
1241    */
1242   uint32_t priority;
1243 };
1244
1245
1246 /**
1247  * Continuation of "GNUNET_FS_publish_ksk" that performs
1248  * the actual publishing operation (iterating over all
1249  * of the keywords).
1250  *
1251  * @param cls closure of type "struct PublishKskContext*"
1252  * @param tc unused
1253  */
1254 static void
1255 publish_ksk_cont (void *cls,
1256                   const struct GNUNET_SCHEDULER_TaskContext *tc);
1257
1258
1259 /**
1260  * Function called by the datastore API with
1261  * the result from the PUT request.
1262  *
1263  * @param cls closure of type "struct PublishKskContext*"
1264  * @param success GNUNET_OK on success
1265  * @param msg error message (or NULL)
1266  */
1267 static void
1268 kb_put_cont (void *cls,
1269              int success,
1270              const char *msg)
1271 {
1272   struct PublishKskContext *pkc = cls;
1273
1274   if (GNUNET_OK != success)
1275     {
1276       GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1277       GNUNET_free (pkc->cpy);
1278       GNUNET_free (pkc->kb);
1279       pkc->cont (pkc->cont_cls,
1280                  NULL,
1281                  msg);
1282       GNUNET_FS_uri_destroy (pkc->ksk_uri);
1283       GNUNET_free (pkc);
1284       return;
1285     }
1286   GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1287                                      &publish_ksk_cont,
1288                                      pkc,
1289                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1290 }
1291
1292
1293 /**
1294  * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1295  * publishing operation (iterating over all of the keywords).
1296  *
1297  * @param cls closure of type "struct PublishKskContext*"
1298  * @param tc unused
1299  */
1300 static void
1301 publish_ksk_cont (void *cls,
1302                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1303 {
1304   struct PublishKskContext *pkc = cls;
1305   const char *keyword;
1306   GNUNET_HashCode key;
1307   GNUNET_HashCode query;
1308   struct GNUNET_CRYPTO_AesSessionKey skey;
1309   struct GNUNET_CRYPTO_AesInitializationVector iv;
1310   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1311
1312
1313   if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1314        (NULL == pkc->dsh) )
1315     {
1316       if (NULL != pkc->dsh)
1317         GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1318       GNUNET_free (pkc->cpy);
1319       GNUNET_free (pkc->kb);
1320       pkc->cont (pkc->cont_cls,
1321                  pkc->ksk_uri,
1322                  NULL);
1323       GNUNET_FS_uri_destroy (pkc->ksk_uri);
1324       GNUNET_free (pkc);
1325       return;
1326     }
1327   keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1328   /* first character of keyword indicates if it is
1329      mandatory or not -- ignore for hashing */
1330   GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1331   GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1332   GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1333                              pkc->slen + pkc->mdsize,
1334                              &skey,
1335                              &iv,
1336                              &pkc->cpy[1]);
1337   pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1338   GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1339   GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1340                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1341                       &query);
1342   GNUNET_assert (GNUNET_OK == 
1343                  GNUNET_CRYPTO_rsa_sign (pk,
1344                                          &pkc->cpy->purpose,
1345                                          &pkc->cpy->signature));
1346   GNUNET_CRYPTO_rsa_key_free (pk);
1347   GNUNET_DATASTORE_put (pkc->dsh,
1348                         0,
1349                         &query,
1350                         pkc->mdsize + 
1351                         sizeof (struct KBlock) + 
1352                         pkc->slen,
1353                         pkc->cpy,
1354                         GNUNET_BLOCK_TYPE_KBLOCK, 
1355                         pkc->priority,
1356                         pkc->anonymity,
1357                         pkc->expirationTime,
1358                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1359                         &kb_put_cont,
1360                         pkc);
1361 }
1362
1363
1364 /**
1365  * Publish a CHK under various keywords on GNUnet.
1366  *
1367  * @param h handle to the file sharing subsystem
1368  * @param ksk_uri keywords to use
1369  * @param meta metadata to use
1370  * @param uri URI to refer to in the KBlock
1371  * @param expirationTime when the KBlock expires
1372  * @param anonymity anonymity level for the KBlock
1373  * @param priority priority for the KBlock
1374  * @param options publication options
1375  * @param cont continuation
1376  * @param cont_cls closure for cont
1377  */
1378 void
1379 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1380                        const struct GNUNET_FS_Uri *ksk_uri,
1381                        const struct GNUNET_CONTAINER_MetaData *meta,
1382                        const struct GNUNET_FS_Uri *uri,
1383                        struct GNUNET_TIME_Absolute expirationTime,
1384                        uint32_t anonymity,
1385                        uint32_t priority,
1386                        enum GNUNET_FS_PublishOptions options,
1387                        GNUNET_FS_PublishContinuation cont,
1388                        void *cont_cls)
1389 {
1390   struct PublishKskContext *pkc;
1391   char *uris;
1392   size_t size;
1393   char *kbe;
1394   char *sptr;
1395
1396   pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1397   pkc->h = h;
1398   pkc->expirationTime = expirationTime;
1399   pkc->anonymity = anonymity;
1400   pkc->priority = priority;
1401   pkc->cont = cont;
1402   pkc->cont_cls = cont_cls;
1403   if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1404     {
1405       pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1406                                            h->sched);
1407       if (pkc->dsh == NULL)
1408         {
1409           cont (cont_cls, NULL, _("Could not connect to datastore."));
1410           GNUNET_free (pkc);
1411           return;
1412         }
1413     }
1414   if (meta == NULL)
1415     pkc->mdsize = 0;
1416   else
1417     pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1418   GNUNET_assert (pkc->mdsize >= 0);
1419   uris = GNUNET_FS_uri_to_string (uri);
1420   pkc->slen = strlen (uris) + 1;
1421   size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1422   if (size > MAX_KBLOCK_SIZE)
1423     {
1424       size = MAX_KBLOCK_SIZE;
1425       pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1426     }
1427   pkc->kb = GNUNET_malloc (size);
1428   kbe = (char *) &pkc->kb[1];
1429   memcpy (kbe, uris, pkc->slen);
1430   GNUNET_free (uris);
1431   sptr = &kbe[pkc->slen];
1432   if (meta != NULL)
1433     pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1434                                                         &sptr,
1435                                                         pkc->mdsize,
1436                                                         GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1437   if (pkc->mdsize == -1)
1438     {
1439       GNUNET_break (0);
1440       GNUNET_free (pkc->kb);
1441       if (pkc->dsh != NULL)
1442         GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1443       cont (cont_cls, NULL, _("Internal error."));
1444       GNUNET_free (pkc);
1445       return;
1446     }
1447   size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1448
1449   pkc->cpy = GNUNET_malloc (size);
1450   pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) + 
1451                                   sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1452                                   pkc->mdsize + 
1453                                   pkc->slen);
1454   pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1455   pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1456   GNUNET_SCHEDULER_add_continuation (h->sched,
1457                                      &publish_ksk_cont,
1458                                      pkc,
1459                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1460 }
1461
1462
1463 /**
1464  * Context for the SKS publication.
1465  */
1466 struct PublishSksContext
1467 {
1468
1469   /**
1470    * Global FS context.
1471    */
1472   struct GNUNET_FS_Uri *uri;
1473
1474   /**
1475    * Handle to the datastore.
1476    */
1477   struct GNUNET_DATASTORE_Handle *dsh;
1478
1479   /**
1480    * Function to call once we're done.
1481    */
1482   GNUNET_FS_PublishContinuation cont;
1483
1484   /**
1485    * Closure for cont.
1486    */ 
1487   void *cont_cls;
1488
1489 };
1490
1491
1492 /**
1493  * Function called by the datastore API with
1494  * the result from the PUT (SBlock) request.
1495  *
1496  * @param cls closure of type "struct PublishSksContext*"
1497  * @param success GNUNET_OK on success
1498  * @param msg error message (or NULL)
1499  */
1500 static void
1501 sb_put_cont (void *cls,
1502              int success,
1503              const char *msg)
1504 {
1505   struct PublishSksContext *psc = cls;
1506
1507   if (NULL != psc->dsh)
1508     GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1509   if (GNUNET_OK != success)
1510     psc->cont (psc->cont_cls,
1511                NULL,
1512                msg);
1513   else
1514     psc->cont (psc->cont_cls,
1515                psc->uri,
1516                NULL);
1517   GNUNET_FS_uri_destroy (psc->uri);
1518   GNUNET_free (psc);
1519 }
1520
1521
1522 /**
1523  * Publish an SBlock on GNUnet.
1524  *
1525  * @param h handle to the file sharing subsystem
1526  * @param namespace namespace to publish in
1527  * @param identifier identifier to use
1528  * @param update update identifier to use
1529  * @param meta metadata to use
1530  * @param uri URI to refer to in the SBlock
1531  * @param expirationTime when the SBlock expires
1532  * @param anonymity anonymity level for the SBlock
1533  * @param priority priority for the SBlock
1534  * @param options publication options
1535  * @param cont continuation
1536  * @param cont_cls closure for cont
1537  */
1538 void
1539 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1540                        struct GNUNET_FS_Namespace *namespace,
1541                        const char *identifier,
1542                        const char *update,
1543                        const struct GNUNET_CONTAINER_MetaData *meta,
1544                        const struct GNUNET_FS_Uri *uri,
1545                        struct GNUNET_TIME_Absolute expirationTime,
1546                        uint32_t anonymity,
1547                        uint32_t priority,
1548                        enum GNUNET_FS_PublishOptions options,
1549                        GNUNET_FS_PublishContinuation cont,
1550                        void *cont_cls)
1551 {
1552   struct PublishSksContext *psc;
1553   struct GNUNET_CRYPTO_AesSessionKey sk;
1554   struct GNUNET_CRYPTO_AesInitializationVector iv;
1555   struct GNUNET_FS_Uri *sks_uri;
1556   char *uris;
1557   size_t size;
1558   size_t slen;
1559   size_t nidlen;
1560   size_t idlen;
1561   ssize_t mdsize;
1562   struct SBlock *sb;
1563   struct SBlock *sb_enc;
1564   char *dest;
1565   struct GNUNET_CONTAINER_MetaData *mmeta;
1566   GNUNET_HashCode key;         /* hash of thisId = key */
1567   GNUNET_HashCode id;          /* hash of hc = identifier */
1568   GNUNET_HashCode query;       /* id ^ nsid = DB query */
1569
1570   if (NULL == meta)
1571     mmeta = GNUNET_CONTAINER_meta_data_create ();
1572   else
1573     mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1574   uris = GNUNET_FS_uri_to_string (uri);
1575   slen = strlen (uris) + 1;
1576   idlen = strlen (identifier);
1577   if (update == NULL)
1578     update = "";
1579   nidlen = strlen (update) + 1;
1580   mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1581   size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1582   if (size > MAX_SBLOCK_SIZE)
1583     {
1584       size = MAX_SBLOCK_SIZE;
1585       mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1586     }
1587   sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1588   dest = (char *) &sb[1];
1589   memcpy (dest, update, nidlen);
1590   dest += nidlen;
1591   memcpy (dest, uris, slen);
1592   dest += slen;
1593   mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1594                                                  &dest,
1595                                                  mdsize, 
1596                                                  GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1597   GNUNET_CONTAINER_meta_data_destroy (mmeta);
1598   if (mdsize == -1)
1599     {
1600       GNUNET_break (0);
1601       GNUNET_free (uris);
1602       GNUNET_free (sb);
1603       cont (cont_cls,
1604             NULL,
1605             _("Internal error."));
1606       return;
1607     }
1608   size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1609   sb_enc = GNUNET_malloc (size);
1610   GNUNET_CRYPTO_hash (identifier, idlen, &key);
1611   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1612   sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1613   sks_uri->type = sks;
1614   GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1615   GNUNET_CRYPTO_hash (&sb_enc->subspace,
1616                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1617                       &sks_uri->data.sks.namespace);
1618   sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1619   GNUNET_CRYPTO_hash_xor (&id, 
1620                           &sks_uri->data.sks.namespace, 
1621                           &sb_enc->identifier);
1622   GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1623   GNUNET_CRYPTO_aes_encrypt (&sb[1],
1624                              size - sizeof (struct SBlock),
1625                              &sk,
1626                              &iv,
1627                              &sb_enc[1]);
1628   sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1629   sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1630                                + sizeof(struct SBlock)
1631                                - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1632   GNUNET_assert (GNUNET_OK == 
1633                  GNUNET_CRYPTO_rsa_sign (namespace->key,
1634                                          &sb_enc->purpose,
1635                                          &sb_enc->signature));
1636   psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1637   psc->uri = sks_uri;
1638   psc->cont = cont;
1639   psc->cont_cls = cont_cls;
1640   if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1641     {
1642       GNUNET_free (sb_enc);
1643       GNUNET_free (sb);
1644       sb_put_cont (psc,
1645                    GNUNET_OK,
1646                    NULL);
1647       return;
1648     }
1649   psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1650   if (NULL == psc->dsh)
1651     {
1652       GNUNET_free (sb_enc);
1653       GNUNET_free (sb);
1654       sb_put_cont (psc,
1655                    GNUNET_NO,
1656                    _("Failed to connect to datastore."));
1657       return;
1658     }
1659   GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1660                           &id,
1661                           &query);  
1662   GNUNET_DATASTORE_put (psc->dsh,
1663                         0,
1664                         &sb_enc->identifier,
1665                         size,
1666                         sb_enc,
1667                         GNUNET_BLOCK_TYPE_SBLOCK, 
1668                         priority,
1669                         anonymity,
1670                         expirationTime,
1671                         GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1672                         &sb_put_cont,
1673                         psc);
1674
1675   GNUNET_free (sb);
1676   GNUNET_free (sb_enc);
1677 }
1678
1679 /* end of fs_publish.c */