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