2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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.
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.
22 * @file fs/fs_publish.c
23 * @brief publish a file or directory in GNUnet
24 * @see https://gnunet.org/encoding
25 * @author Krista Bennett
26 * @author Christian Grothoff
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_fs_service.h"
37 #define DEBUG_PUBLISH GNUNET_EXTRA_LOGGING
40 * Maximum legal size for a kblock.
42 #define MAX_KBLOCK_SIZE (60 * 1024)
46 * Fill in all of the generic fields for
47 * a publish event and call the callback.
49 * @param pi structure to fill in
50 * @param pc overall publishing context
51 * @param p file information for the file being published
52 * @param offset where in the file are we so far
53 * @return value returned from callback
56 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
57 struct GNUNET_FS_PublishContext *pc,
58 const struct GNUNET_FS_FileInformation *p,
61 pi->value.publish.pc = pc;
62 pi->value.publish.fi = p;
63 pi->value.publish.cctx = p->client_info;
64 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
65 pi->value.publish.filename = p->filename;
66 pi->value.publish.size =
67 (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
68 pi->value.publish.eta =
69 GNUNET_TIME_calculate_eta (p->start_time, offset, pi->value.publish.size);
70 pi->value.publish.completed = offset;
71 pi->value.publish.duration =
72 GNUNET_TIME_absolute_get_duration (p->start_time);
73 pi->value.publish.anonymity = p->bo.anonymity_level;
74 return pc->h->upcb (pc->h->upcb_cls, pi);
79 * Cleanup the publish context, we're done with it.
81 * @param cls struct to clean up after
82 * @param tc scheduler context
85 publish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 struct GNUNET_FS_PublishContext *pc = cls;
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up publish context (done!)\n");
94 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
97 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
98 if (pc->namespace != NULL)
100 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
101 pc->namespace = NULL;
103 GNUNET_free_non_null (pc->nid);
104 GNUNET_free_non_null (pc->nuid);
105 GNUNET_free_non_null (pc->serialization);
108 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
111 if (pc->client != NULL)
113 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
116 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
122 * Function called by the datastore API with
123 * the result from the PUT request.
125 * @param cls the 'struct GNUNET_FS_PublishContext'
126 * @param success GNUNET_OK on success
127 * @param min_expiration minimum expiration time required for content to be stored
128 * @param msg error message (or NULL)
131 ds_put_cont (void *cls, int success,
132 struct GNUNET_TIME_Absolute min_expiration,
135 struct GNUNET_FS_PublishContext *pc = cls;
136 struct GNUNET_FS_ProgressInfo pi;
139 if (GNUNET_SYSERR == pc->in_network_wait)
141 /* we were aborted in the meantime, finish shutdown! */
142 GNUNET_SCHEDULER_add_continuation (&publish_cleanup, pc,
143 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
146 GNUNET_assert (GNUNET_YES == pc->in_network_wait);
147 pc->in_network_wait = GNUNET_NO;
148 if (GNUNET_SYSERR == success)
150 GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
151 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
152 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
153 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
154 pc->fi_pos->client_info =
155 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
156 if ((pc->fi_pos->is_directory == GNUNET_NO) &&
157 (pc->fi_pos->filename != NULL) &&
158 (pc->fi_pos->data.file.do_index == GNUNET_YES))
160 /* run unindex to clean up */
161 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
164 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
166 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
167 &GNUNET_FS_publish_main_, pc);
172 * Generate the callback that signals clients
173 * that a file (or directory) has been completely
176 * @param p the completed upload
177 * @param pc context of the publication
180 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
181 struct GNUNET_FS_PublishContext *pc)
183 struct GNUNET_FS_ProgressInfo pi;
185 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
186 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
187 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
189 GNUNET_FS_publish_make_status_ (&pi, pc, p,
190 GNUNET_ntohll (p->chk_uri->data.
196 * Generate the callback that signals clients
197 * that a file (or directory) has encountered
198 * a problem during publication.
200 * @param p the upload that had trouble
201 * @param pc context of the publication
202 * @param emsg error message
205 signal_publish_error (struct GNUNET_FS_FileInformation *p,
206 struct GNUNET_FS_PublishContext *pc, const char *emsg)
208 struct GNUNET_FS_ProgressInfo pi;
210 p->emsg = GNUNET_strdup (emsg);
211 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
212 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
213 pi.value.publish.specifics.error.message = emsg;
214 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
215 if ((p->is_directory == GNUNET_NO) && (p->filename != NULL) &&
216 (p->data.file.do_index == GNUNET_YES))
218 /* run unindex to clean up */
219 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
226 * Datastore returns from reservation cancel request.
228 * @param cls the 'struct GNUNET_FS_PublishContext'
229 * @param success success code (not used)
230 * @param min_expiration minimum expiration time required for content to be stored
231 * @param msg error message (typically NULL, not used)
234 finish_release_reserve (void *cls, int success,
235 struct GNUNET_TIME_Absolute min_expiration,
238 struct GNUNET_FS_PublishContext *pc = cls;
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Releasing reserve done!\n");
244 signal_publish_completion (pc->fi, pc);
245 pc->all_done = GNUNET_YES;
246 GNUNET_FS_publish_sync_ (pc);
251 * We've finished publishing the SBlock as part of a larger upload.
252 * Check the result and complete the larger upload.
254 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
255 * @param uri URI of the published SBlock
256 * @param emsg NULL on success, otherwise error message
259 publish_sblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
262 struct GNUNET_FS_PublishContext *pc = cls;
266 signal_publish_error (pc->fi, pc, emsg);
267 GNUNET_FS_publish_sync_ (pc);
270 GNUNET_assert (pc->qre == NULL);
271 if ((pc->dsh != NULL) && (pc->rid != 0))
274 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
275 GNUNET_TIME_UNIT_FOREVER_REL,
276 &finish_release_reserve, pc);
280 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
286 * We are almost done publishing the structure,
287 * add SBlocks (if needed).
289 * @param pc overall upload data
292 publish_sblock (struct GNUNET_FS_PublishContext *pc)
294 if (NULL != pc->namespace)
295 GNUNET_FS_publish_sks (pc->h, pc->namespace, pc->nid, pc->nuid,
296 pc->fi->meta, pc->fi->chk_uri, &pc->fi->bo,
297 pc->options, &publish_sblocks_cont, pc);
299 publish_sblocks_cont (pc, NULL, NULL);
304 * We've finished publishing a KBlock as part of a larger upload.
305 * Check the result and continue the larger upload.
307 * @param cls the "struct GNUNET_FS_PublishContext*"
308 * of the larger upload
309 * @param uri URI of the published blocks
310 * @param emsg NULL on success, otherwise error message
313 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
316 struct GNUNET_FS_PublishContext *pc = cls;
317 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading KSK blocks: %s\n",
325 signal_publish_error (p, pc, emsg);
326 GNUNET_FS_file_information_sync_ (p);
327 GNUNET_FS_publish_sync_ (pc);
328 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
330 GNUNET_SCHEDULER_add_with_priority
331 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "KSK blocks published, moving on to next file\n");
339 signal_publish_completion (p, pc);
340 /* move on to next file */
342 pc->fi_pos = p->next;
345 GNUNET_FS_publish_sync_ (pc);
346 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
348 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
349 &GNUNET_FS_publish_main_, pc);
354 * Function called by the tree encoder to obtain
355 * a block of plaintext data (for the lowest level
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
367 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
369 struct GNUNET_FS_PublishContext *pc = cls;
370 struct GNUNET_FS_FileInformation *p;
377 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
378 dd = p->data.dir.dir_data;
379 memcpy (buf, &dd[offset], pt_size);
383 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
385 return 0; /* calling reader with pt_size==0
386 * might free buf, so don't! */
388 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
397 * The tree encoder has finished processing a
398 * file. Call it's finish method and deal with
401 * @param cls our publishing context
402 * @param tc scheduler's task context (not used)
405 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
407 struct GNUNET_FS_PublishContext *pc = cls;
408 struct GNUNET_FS_FileInformation *p;
409 struct GNUNET_FS_ProgressInfo pi;
414 GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
421 GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
423 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
424 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
425 pi.value.publish.specifics.error.message = p->emsg;
426 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
431 /* final progress event */
432 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
433 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
434 pi.value.publish.specifics.progress.data = NULL;
435 pi.value.publish.specifics.progress.offset = flen;
436 pi.value.publish.specifics.progress.data_len = 0;
437 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
438 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
440 /* continue with main */
441 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
443 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
444 &GNUNET_FS_publish_main_, pc);
449 * Function called asking for the current (encoded)
450 * block to be processed. After processing the
451 * client should either call "GNUNET_FS_tree_encode_next"
452 * or (on error) "GNUNET_FS_tree_encode_finish".
455 * @param chk content hash key for the block
456 * @param offset offset of the block in the file
457 * @param depth depth of the block in the file, 0 for DBLOCK
458 * @param type type of the block (IBLOCK or DBLOCK)
459 * @param block the (encrypted) block
460 * @param block_size size of block (in bytes)
463 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
464 unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
467 struct GNUNET_FS_PublishContext *pc = cls;
468 struct GNUNET_FS_FileInformation *p;
469 struct OnDemandBlock odb;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
477 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
479 GNUNET_SCHEDULER_add_with_priority
480 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
484 GNUNET_assert (GNUNET_NO == pc->in_network_wait);
485 pc->in_network_wait = GNUNET_YES;
486 if ((!p->is_directory) && (GNUNET_YES == p->data.file.do_index) &&
487 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Indexing block `%s' for offset %llu with index size %u\n",
492 GNUNET_h2s (&chk->query), (unsigned long long) offset,
493 sizeof (struct OnDemandBlock));
495 odb.offset = GNUNET_htonll (offset);
496 odb.file_id = p->data.file.file_id;
497 GNUNET_assert (pc->qre == NULL);
499 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory) ? 0 : pc->rid,
500 &chk->query, sizeof (struct OnDemandBlock), &odb,
501 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
502 p->bo.content_priority, p->bo.anonymity_level,
503 p->bo.replication_level, p->bo.expiration_time,
504 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Publishing block `%s' for offset %llu with size %u\n",
511 GNUNET_h2s (&chk->query), (unsigned long long) offset,
512 (unsigned int) block_size);
514 GNUNET_assert (pc->qre == NULL);
516 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory) ? 0 : pc->rid,
517 &chk->query, block_size, block, type,
518 p->bo.content_priority, p->bo.anonymity_level,
519 p->bo.replication_level, p->bo.expiration_time, -2,
520 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
526 * Function called with information about our
527 * progress in computing the tree encoding.
530 * @param offset where are we in the file
531 * @param pt_block plaintext of the currently processed block
532 * @param pt_size size of pt_block
533 * @param depth depth of the block in the tree, 0 for DBLOCK
536 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
539 struct GNUNET_FS_PublishContext *pc = cls;
540 struct GNUNET_FS_FileInformation *p;
541 struct GNUNET_FS_ProgressInfo pi;
544 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
545 pi.value.publish.specifics.progress.data = pt_block;
546 pi.value.publish.specifics.progress.offset = offset;
547 pi.value.publish.specifics.progress.data_len = pt_size;
548 pi.value.publish.specifics.progress.depth = depth;
549 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
554 * We are uploading a file or directory; load (if necessary) the next
555 * block into memory, encrypt it and send it to the FS service. Then
556 * continue with the main task.
558 * @param pc overall upload data
561 publish_content (struct GNUNET_FS_PublishContext *pc)
563 struct GNUNET_FS_FileInformation *p;
565 struct GNUNET_FS_DirectoryBuilder *db;
566 struct GNUNET_FS_FileInformation *dirpos;
571 GNUNET_assert (p != NULL);
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
579 db = GNUNET_FS_directory_builder_create (p->meta);
580 dirpos = p->data.dir.entries;
581 while (NULL != dirpos)
583 if (dirpos->is_directory)
585 raw_data = dirpos->data.dir.dir_data;
586 dirpos->data.dir.dir_data = NULL;
591 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
592 (dirpos->data.file.file_size > 0))
594 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
596 if (dirpos->data.file.file_size !=
597 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
598 dirpos->data.file.file_size, raw_data,
601 GNUNET_free_non_null (emsg);
602 GNUNET_free (raw_data);
607 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
609 GNUNET_free_non_null (raw_data);
610 dirpos = dirpos->next;
612 GNUNET_free_non_null (p->data.dir.dir_data);
613 p->data.dir.dir_data = NULL;
614 p->data.dir.dir_size = 0;
615 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
616 &p->data.dir.dir_data);
617 GNUNET_FS_file_information_sync_ (p);
619 size = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
624 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
625 &block_proc, &progress_proc,
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
632 GNUNET_FS_tree_encoder_next (p->te);
637 * Process the response (or lack thereof) from
638 * the "fs" service to our 'start index' request.
640 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
641 * @param msg the response we got
644 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
646 struct GNUNET_FS_PublishContext *pc = cls;
647 struct GNUNET_FS_FileInformation *p;
651 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 ("Can not index file `%s': %s. Will try to insert instead.\n"),
660 _("timeout on index-start request to `fs' service"));
661 p->data.file.do_index = GNUNET_NO;
662 GNUNET_FS_file_information_sync_ (p);
663 publish_content (pc);
666 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
668 msize = ntohs (msg->size);
669 emsg = (const char *) &msg[1];
670 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
671 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
672 emsg = gettext_noop ("unknown error");
673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
675 ("Can not index file `%s': %s. Will try to insert instead.\n"),
676 p->filename, gettext (emsg));
677 p->data.file.do_index = GNUNET_NO;
678 GNUNET_FS_file_information_sync_ (p);
679 publish_content (pc);
682 p->data.file.index_start_confirmed = GNUNET_YES;
683 /* success! continue with indexing */
684 GNUNET_FS_file_information_sync_ (p);
685 publish_content (pc);
690 * Function called once the hash computation over an
691 * indexed file has completed.
693 * @param cls closure, our publishing context
694 * @param res resulting hash, NULL on error
697 hash_for_index_cb (void *cls, const GNUNET_HashCode * res)
699 struct GNUNET_FS_PublishContext *pc = cls;
700 struct GNUNET_FS_FileInformation *p;
701 struct IndexStartMessage *ism;
703 struct GNUNET_CLIENT_Connection *client;
712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
714 ("Can not index file `%s': %s. Will try to insert instead.\n"),
715 p->filename, _("failed to compute hash"));
716 p->data.file.do_index = GNUNET_NO;
717 GNUNET_FS_file_information_sync_ (p);
718 publish_content (pc);
721 if (GNUNET_YES == p->data.file.index_start_confirmed)
723 publish_content (pc);
726 fn = GNUNET_STRINGS_filename_expand (p->filename);
727 GNUNET_assert (fn != NULL);
728 slen = strlen (fn) + 1;
730 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
734 ("Can not index file `%s': %s. Will try to insert instead.\n"),
735 fn, _("filename too long"));
737 p->data.file.do_index = GNUNET_NO;
738 GNUNET_FS_file_information_sync_ (p);
739 publish_content (pc);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
744 p->filename, GNUNET_h2s (res));
746 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
748 p->data.file.file_id = *res;
749 p->data.file.have_hash = GNUNET_YES;
750 p->data.file.index_start_confirmed = GNUNET_YES;
751 GNUNET_FS_file_information_sync_ (p);
752 publish_content (pc);
756 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
761 ("Can not index file `%s': %s. Will try to insert instead.\n"),
762 p->filename, _("could not connect to `fs' service"));
763 p->data.file.do_index = GNUNET_NO;
764 publish_content (pc);
768 if (p->data.file.have_hash != GNUNET_YES)
770 p->data.file.file_id = *res;
771 p->data.file.have_hash = GNUNET_YES;
772 GNUNET_FS_file_information_sync_ (p);
774 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
775 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
776 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
777 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
779 ism->device = GNUNET_htonll (dev);
780 ism->inode = GNUNET_htonll (ino);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 _("Failed to get file identifiers for `%s'\n"), p->filename);
790 memcpy (&ism[1], fn, slen);
793 GNUNET_break (GNUNET_YES ==
794 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
795 GNUNET_TIME_UNIT_FOREVER_REL,
797 &process_index_start_response,
804 * Main function that performs the upload.
806 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
807 * @param tc task context
810 GNUNET_FS_publish_main_ (void *cls,
811 const struct GNUNET_SCHEDULER_TaskContext *tc)
813 struct GNUNET_FS_PublishContext *pc = cls;
814 struct GNUNET_FS_ProgressInfo pi;
815 struct GNUNET_FS_FileInformation *p;
816 struct GNUNET_FS_Uri *loc;
819 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Publishing complete, now publishing SKS and KSK blocks.\n");
827 /* upload of entire hierarchy complete,
828 * publish namespace entries */
829 GNUNET_FS_publish_sync_ (pc);
833 /* find starting position */
834 while ((p->is_directory) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
835 && (NULL == p->data.dir.entries->chk_uri))
837 p = p->data.dir.entries;
839 GNUNET_FS_publish_sync_ (pc);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
847 /* error with current file, abort all
848 * related files as well! */
849 while (NULL != p->dir)
851 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
852 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
856 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
862 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
864 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
865 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
866 pi.value.publish.specifics.error.message = p->emsg;
867 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
869 pc->all_done = GNUNET_YES;
870 GNUNET_FS_publish_sync_ (pc);
873 /* handle completion */
874 if (NULL != p->chk_uri)
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "File upload complete, now publishing KSK blocks.\n");
880 if (0 == p->bo.anonymity_level)
882 /* zero anonymity, box CHK URI in LOC URI */
884 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
885 p->bo.expiration_time);
886 GNUNET_FS_uri_destroy (p->chk_uri);
889 GNUNET_FS_publish_sync_ (pc);
890 /* upload of "p" complete, publish KBlocks! */
891 if (p->keywords != NULL)
893 GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
894 pc->options, &publish_kblocks_cont, pc);
898 publish_kblocks_cont (pc, p->chk_uri, NULL);
902 if ((!p->is_directory) && (p->data.file.do_index))
904 if (NULL == p->filename)
906 p->data.file.do_index = GNUNET_NO;
907 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
909 ("Can not index file `%s': %s. Will try to insert instead.\n"),
910 "<no-name>", _("needs to be an actual file"));
911 GNUNET_FS_file_information_sync_ (p);
912 publish_content (pc);
915 if (p->data.file.have_hash)
917 hash_for_index_cb (pc, &p->data.file.file_id);
921 p->start_time = GNUNET_TIME_absolute_get ();
923 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
924 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
928 publish_content (pc);
933 * Signal the FS's progress function that we are starting
936 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
937 * @param fi the entry in the publish-structure
938 * @param length length of the file or directory
939 * @param meta metadata for the file or directory (can be modified)
940 * @param uri pointer to the keywords that will be used for this entry (can be modified)
941 * @param bo block options
942 * @param do_index should we index?
943 * @param client_info pointer to client context set upon creation (can be modified)
944 * @return GNUNET_OK to continue (always)
947 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
948 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
949 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
950 int *do_index, void **client_info)
952 struct GNUNET_FS_PublishContext *pc = cls;
953 struct GNUNET_FS_ProgressInfo pi;
957 if (GNUNET_YES == pc->skip_next_fi_callback)
959 pc->skip_next_fi_callback = GNUNET_NO;
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
967 /* space for on-demand blocks */
969 ((length + DBLOCK_SIZE -
970 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
974 /* space for DBlocks */
975 pc->reserve_space += length;
977 /* entries for IBlocks and DBlocks, space for IBlocks */
981 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
982 pc->reserve_entries += left;
985 left = left * sizeof (struct ContentHashKey);
986 pc->reserve_space += left;
988 pc->reserve_entries++;
989 /* entries and space for keywords */
992 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
993 pc->reserve_entries += kc;
994 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
996 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
997 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
998 GNUNET_FS_file_information_sync_ (fi);
999 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1001 /* process entries in directory */
1002 pc->skip_next_fi_callback = GNUNET_YES;
1003 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1010 * Signal the FS's progress function that we are suspending
1013 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1014 * @param fi the entry in the publish-structure
1015 * @param length length of the file or directory
1016 * @param meta metadata for the file or directory (can be modified)
1017 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1018 * @param bo block options
1019 * @param do_index should we index?
1020 * @param client_info pointer to client context set upon creation (can be modified)
1021 * @return GNUNET_OK to continue (always)
1024 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
1025 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1026 struct GNUNET_FS_Uri **uri,
1027 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1030 struct GNUNET_FS_PublishContext *pc = cls;
1031 struct GNUNET_FS_ProgressInfo pi;
1034 if (GNUNET_YES == pc->skip_next_fi_callback)
1036 pc->skip_next_fi_callback = GNUNET_NO;
1039 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1041 /* process entries in directory */
1042 pc->skip_next_fi_callback = GNUNET_YES;
1043 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
1048 GNUNET_free_non_null (fi->serialization);
1049 fi->serialization = NULL;
1050 off = (fi->chk_uri == NULL) ? 0 : length;
1051 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1052 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1053 *client_info = NULL;
1054 if (NULL != pc->qre)
1056 GNUNET_DATASTORE_cancel (pc->qre);
1059 if (NULL != pc->dsh)
1061 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1070 * Create SUSPEND event for the given publish operation
1071 * and then clean up our state (without stop signal).
1073 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1076 GNUNET_FS_publish_signal_suspend_ (void *cls)
1078 struct GNUNET_FS_PublishContext *pc = cls;
1080 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1082 GNUNET_SCHEDULER_cancel (pc->upload_task);
1083 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1085 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1086 GNUNET_FS_end_top (pc->h, pc->top);
1088 publish_cleanup (pc, NULL);
1093 * We have gotten a reply for our space reservation request.
1094 * Either fail (insufficient space) or start publishing for good.
1096 * @param cls the 'struct GNUNET_FS_PublishContext*'
1097 * @param success positive reservation ID on success
1098 * @param min_expiration minimum expiration time required for content to be stored
1099 * @param msg error message on error, otherwise NULL
1102 finish_reserve (void *cls, int success,
1103 struct GNUNET_TIME_Absolute min_expiration,
1106 struct GNUNET_FS_PublishContext *pc = cls;
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1112 if ((msg != NULL) || (success <= 0))
1114 GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1116 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1120 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1122 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1123 &GNUNET_FS_publish_main_, pc);
1128 * Publish a file or directory.
1130 * @param h handle to the file sharing subsystem
1131 * @param fi information about the file or directory structure to publish
1132 * @param namespace namespace to publish the file in, NULL for no namespace
1133 * @param nid identifier to use for the publishd content in the namespace
1134 * (can be NULL, must be NULL if namespace is NULL)
1135 * @param nuid update-identifier that will be used for future updates
1136 * (can be NULL, must be NULL if namespace or nid is NULL)
1137 * @param options options for the publication
1138 * @return context that can be used to control the publish operation
1140 struct GNUNET_FS_PublishContext *
1141 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1142 struct GNUNET_FS_FileInformation *fi,
1143 struct GNUNET_FS_Namespace *namespace, const char *nid,
1145 enum GNUNET_FS_PublishOptions options)
1147 struct GNUNET_FS_PublishContext *ret;
1148 struct GNUNET_DATASTORE_Handle *dsh;
1150 GNUNET_assert (NULL != h);
1151 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1153 dsh = GNUNET_DATASTORE_connect (h->cfg);
1161 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1165 ret->namespace = namespace;
1166 ret->options = options;
1167 if (namespace != NULL)
1170 GNUNET_assert (NULL != nid);
1171 ret->nid = GNUNET_strdup (nid);
1173 ret->nuid = GNUNET_strdup (nuid);
1176 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1177 ret->fi_pos = ret->fi;
1178 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1179 GNUNET_FS_publish_sync_ (ret);
1180 if (NULL != ret->dsh)
1182 GNUNET_assert (NULL == ret->qre);
1183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1185 ("Reserving space for %u entries and %llu bytes for publication\n"),
1186 (unsigned int) ret->reserve_entries,
1187 (unsigned long long) ret->reserve_space);
1189 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1190 ret->reserve_entries, UINT_MAX, UINT_MAX,
1191 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1196 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1198 GNUNET_SCHEDULER_add_with_priority
1199 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1206 * Signal the FS's progress function that we are stopping
1209 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1210 * @param fi the entry in the publish-structure
1211 * @param length length of the file or directory
1212 * @param meta metadata for the file or directory (can be modified)
1213 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1214 * @param bo block options (can be modified)
1215 * @param do_index should we index?
1216 * @param client_info pointer to client context set upon creation (can be modified)
1217 * @return GNUNET_OK to continue (always)
1220 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1221 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1222 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1223 int *do_index, void **client_info)
1225 struct GNUNET_FS_PublishContext *pc = cls;
1226 struct GNUNET_FS_ProgressInfo pi;
1229 if (GNUNET_YES == pc->skip_next_fi_callback)
1231 pc->skip_next_fi_callback = GNUNET_NO;
1234 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1236 /* process entries in directory first */
1237 pc->skip_next_fi_callback = GNUNET_YES;
1238 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1240 if (fi->serialization != NULL)
1242 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1244 GNUNET_free (fi->serialization);
1245 fi->serialization = NULL;
1247 off = (fi->chk_uri == NULL) ? 0 : length;
1248 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1249 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1250 *client_info = NULL;
1256 * Stop an upload. Will abort incomplete uploads (but
1257 * not remove blocks that have already been publishd) or
1258 * simply clean up the state for completed uploads.
1259 * Must NOT be called from within the event callback!
1261 * @param pc context for the upload to stop
1264 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1269 GNUNET_FS_end_top (pc->h, pc->top);
1270 if (NULL != pc->qre)
1272 GNUNET_DATASTORE_cancel (pc->qre);
1275 if (NULL != pc->dsh)
1277 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1280 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1282 GNUNET_SCHEDULER_cancel (pc->upload_task);
1283 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1285 if (pc->serialization != NULL)
1287 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1289 GNUNET_free (pc->serialization);
1290 pc->serialization = NULL;
1292 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1293 if (GNUNET_YES == pc->in_network_wait)
1295 pc->in_network_wait = GNUNET_SYSERR;
1298 publish_cleanup (pc, NULL);
1303 * Context for the KSK publication.
1305 struct PublishKskContext
1311 struct GNUNET_FS_Uri *ksk_uri;
1314 * Global FS context.
1316 struct GNUNET_FS_Handle *h;
1319 * The master block that we are sending
1320 * (in plaintext), has "mdsize+slen" more
1321 * bytes than the struct would suggest.
1326 * Buffer of the same size as "kb" for
1327 * the encrypted version.
1332 * Handle to the datastore, NULL if we are just
1335 struct GNUNET_DATASTORE_Handle *dsh;
1338 * Handle to datastore PUT request.
1340 struct GNUNET_DATASTORE_QueueEntry *qre;
1343 * Function to call once we're done.
1345 GNUNET_FS_PublishContinuation cont;
1353 * When should the KBlocks expire?
1355 struct GNUNET_FS_BlockOptions bo;
1358 * Size of the serialized metadata.
1363 * Size of the (CHK) URI as a string.
1368 * Keyword that we are currently processing.
1376 * Continuation of "GNUNET_FS_publish_ksk" that performs
1377 * the actual publishing operation (iterating over all
1380 * @param cls closure of type "struct PublishKskContext*"
1384 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1388 * Function called by the datastore API with
1389 * the result from the PUT request.
1391 * @param cls closure of type "struct PublishKskContext*"
1392 * @param success GNUNET_OK on success
1393 * @param min_expiration minimum expiration time required for content to be stored
1394 * @param msg error message (or NULL)
1397 kb_put_cont (void *cls, int success,
1398 struct GNUNET_TIME_Absolute min_expiration,
1401 struct PublishKskContext *pkc = cls;
1404 if (GNUNET_OK != success)
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KB PUT operation complete\n");
1409 if (NULL != pkc->dsh)
1411 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1414 GNUNET_free (pkc->cpy);
1415 GNUNET_free (pkc->kb);
1416 pkc->cont (pkc->cont_cls, NULL, msg);
1417 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1421 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont, pkc,
1422 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1427 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1428 * publishing operation (iterating over all of the keywords).
1430 * @param cls closure of type "struct PublishKskContext*"
1434 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1436 struct PublishKskContext *pkc = cls;
1437 const char *keyword;
1438 GNUNET_HashCode key;
1439 GNUNET_HashCode query;
1440 struct GNUNET_CRYPTO_AesSessionKey skey;
1441 struct GNUNET_CRYPTO_AesInitializationVector iv;
1442 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1445 if ((pkc->i == pkc->ksk_uri->data.ksk.keywordCount) || (NULL == pkc->dsh))
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KSK PUT operation complete\n");
1450 if (NULL != pkc->dsh)
1452 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1455 GNUNET_free (pkc->cpy);
1456 GNUNET_free (pkc->kb);
1457 pkc->cont (pkc->cont_cls, pkc->ksk_uri, NULL);
1458 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1462 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing under keyword `%s'\n",
1467 /* first character of keyword indicates if it is
1468 * mandatory or not -- ignore for hashing */
1469 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1470 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1471 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1], pkc->slen + pkc->mdsize, &skey, &iv,
1473 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1474 GNUNET_assert (NULL != pk);
1475 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1476 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1477 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1479 GNUNET_assert (GNUNET_OK ==
1480 GNUNET_CRYPTO_rsa_sign (pk, &pkc->cpy->purpose,
1481 &pkc->cpy->signature));
1482 GNUNET_CRYPTO_rsa_key_free (pk);
1484 GNUNET_DATASTORE_put (pkc->dsh, 0, &query,
1485 pkc->mdsize + sizeof (struct KBlock) + pkc->slen,
1486 pkc->cpy, GNUNET_BLOCK_TYPE_FS_KBLOCK,
1487 pkc->bo.content_priority, pkc->bo.anonymity_level,
1488 pkc->bo.replication_level, pkc->bo.expiration_time,
1489 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1495 * Publish a CHK under various keywords on GNUnet.
1497 * @param h handle to the file sharing subsystem
1498 * @param ksk_uri keywords to use
1499 * @param meta metadata to use
1500 * @param uri URI to refer to in the KBlock
1501 * @param bo per-block options
1502 * @param options publication options
1503 * @param cont continuation
1504 * @param cont_cls closure for cont
1507 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1508 const struct GNUNET_FS_Uri *ksk_uri,
1509 const struct GNUNET_CONTAINER_MetaData *meta,
1510 const struct GNUNET_FS_Uri *uri,
1511 const struct GNUNET_FS_BlockOptions *bo,
1512 enum GNUNET_FS_PublishOptions options,
1513 GNUNET_FS_PublishContinuation cont, void *cont_cls)
1515 struct PublishKskContext *pkc;
1521 GNUNET_assert (NULL != uri);
1522 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1526 pkc->cont_cls = cont_cls;
1527 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1529 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1530 if (pkc->dsh == NULL)
1532 cont (cont_cls, NULL, _("Could not connect to datastore."));
1540 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1541 GNUNET_assert (pkc->mdsize >= 0);
1542 uris = GNUNET_FS_uri_to_string (uri);
1543 pkc->slen = strlen (uris) + 1;
1544 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1545 if (size > MAX_KBLOCK_SIZE)
1547 size = MAX_KBLOCK_SIZE;
1548 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1550 pkc->kb = GNUNET_malloc (size);
1551 kbe = (char *) &pkc->kb[1];
1552 memcpy (kbe, uris, pkc->slen);
1554 sptr = &kbe[pkc->slen];
1557 GNUNET_CONTAINER_meta_data_serialize (meta, &sptr, pkc->mdsize,
1558 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1559 if (pkc->mdsize == -1)
1562 GNUNET_free (pkc->kb);
1563 if (pkc->dsh != NULL)
1565 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1568 cont (cont_cls, NULL, _("Internal error."));
1572 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1574 pkc->cpy = GNUNET_malloc (size);
1575 pkc->cpy->purpose.size =
1576 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1577 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1578 pkc->mdsize + pkc->slen);
1579 pkc->cpy->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1580 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1581 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont, pkc,
1582 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1586 /* end of fs_publish.c */