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