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)
1000 && (fi->dir != NULL))
1002 /* process entries in directory */
1003 pc->skip_next_fi_callback = GNUNET_YES;
1004 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1011 * Signal the FS's progress function that we are suspending
1014 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1015 * @param fi the entry in the publish-structure
1016 * @param length length of the file or directory
1017 * @param meta metadata for the file or directory (can be modified)
1018 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1019 * @param bo block options
1020 * @param do_index should we index?
1021 * @param client_info pointer to client context set upon creation (can be modified)
1022 * @return GNUNET_OK to continue (always)
1025 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
1026 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1027 struct GNUNET_FS_Uri **uri,
1028 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1031 struct GNUNET_FS_PublishContext *pc = cls;
1032 struct GNUNET_FS_ProgressInfo pi;
1035 if (GNUNET_YES == pc->skip_next_fi_callback)
1037 pc->skip_next_fi_callback = GNUNET_NO;
1040 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1042 /* process entries in directory */
1043 pc->skip_next_fi_callback = GNUNET_YES;
1044 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
1049 GNUNET_free_non_null (fi->serialization);
1050 fi->serialization = NULL;
1051 off = (fi->chk_uri == NULL) ? 0 : length;
1052 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1053 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1054 *client_info = NULL;
1055 if (NULL != pc->qre)
1057 GNUNET_DATASTORE_cancel (pc->qre);
1060 if (NULL != pc->dsh)
1062 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1071 * Create SUSPEND event for the given publish operation
1072 * and then clean up our state (without stop signal).
1074 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1077 GNUNET_FS_publish_signal_suspend_ (void *cls)
1079 struct GNUNET_FS_PublishContext *pc = cls;
1081 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1083 GNUNET_SCHEDULER_cancel (pc->upload_task);
1084 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1086 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1087 GNUNET_FS_end_top (pc->h, pc->top);
1089 publish_cleanup (pc, NULL);
1094 * We have gotten a reply for our space reservation request.
1095 * Either fail (insufficient space) or start publishing for good.
1097 * @param cls the 'struct GNUNET_FS_PublishContext*'
1098 * @param success positive reservation ID on success
1099 * @param min_expiration minimum expiration time required for content to be stored
1100 * @param msg error message on error, otherwise NULL
1103 finish_reserve (void *cls, int success,
1104 struct GNUNET_TIME_Absolute min_expiration,
1107 struct GNUNET_FS_PublishContext *pc = cls;
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1113 if ((msg != NULL) || (success <= 0))
1115 GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1117 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1121 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1123 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1124 &GNUNET_FS_publish_main_, pc);
1129 * Publish a file or directory.
1131 * @param h handle to the file sharing subsystem
1132 * @param fi information about the file or directory structure to publish
1133 * @param namespace namespace to publish the file in, NULL for no namespace
1134 * @param nid identifier to use for the publishd content in the namespace
1135 * (can be NULL, must be NULL if namespace is NULL)
1136 * @param nuid update-identifier that will be used for future updates
1137 * (can be NULL, must be NULL if namespace or nid is NULL)
1138 * @param options options for the publication
1139 * @return context that can be used to control the publish operation
1141 struct GNUNET_FS_PublishContext *
1142 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1143 struct GNUNET_FS_FileInformation *fi,
1144 struct GNUNET_FS_Namespace *namespace, const char *nid,
1146 enum GNUNET_FS_PublishOptions options)
1148 struct GNUNET_FS_PublishContext *ret;
1149 struct GNUNET_DATASTORE_Handle *dsh;
1151 GNUNET_assert (NULL != h);
1152 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1154 dsh = GNUNET_DATASTORE_connect (h->cfg);
1162 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1166 ret->namespace = namespace;
1167 ret->options = options;
1168 if (namespace != NULL)
1171 GNUNET_assert (NULL != nid);
1172 ret->nid = GNUNET_strdup (nid);
1174 ret->nuid = GNUNET_strdup (nuid);
1177 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1178 ret->fi_pos = ret->fi;
1179 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1180 GNUNET_FS_publish_sync_ (ret);
1181 if (NULL != ret->dsh)
1183 GNUNET_assert (NULL == ret->qre);
1184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 ("Reserving space for %u entries and %llu bytes for publication\n"),
1187 (unsigned int) ret->reserve_entries,
1188 (unsigned long long) ret->reserve_space);
1190 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1191 ret->reserve_entries, UINT_MAX, UINT_MAX,
1192 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1197 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1199 GNUNET_SCHEDULER_add_with_priority
1200 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1207 * Signal the FS's progress function that we are stopping
1210 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1211 * @param fi the entry in the publish-structure
1212 * @param length length of the file or directory
1213 * @param meta metadata for the file or directory (can be modified)
1214 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1215 * @param bo block options (can be modified)
1216 * @param do_index should we index?
1217 * @param client_info pointer to client context set upon creation (can be modified)
1218 * @return GNUNET_OK to continue (always)
1221 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1222 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1223 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1224 int *do_index, void **client_info)
1226 struct GNUNET_FS_PublishContext *pc = cls;
1227 struct GNUNET_FS_ProgressInfo pi;
1230 if (GNUNET_YES == pc->skip_next_fi_callback)
1232 pc->skip_next_fi_callback = GNUNET_NO;
1235 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1237 /* process entries in directory first */
1238 pc->skip_next_fi_callback = GNUNET_YES;
1239 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1241 if (fi->serialization != NULL)
1243 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1245 GNUNET_free (fi->serialization);
1246 fi->serialization = NULL;
1248 off = (fi->chk_uri == NULL) ? 0 : length;
1249 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1250 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1251 *client_info = NULL;
1257 * Stop an upload. Will abort incomplete uploads (but
1258 * not remove blocks that have already been publishd) or
1259 * simply clean up the state for completed uploads.
1260 * Must NOT be called from within the event callback!
1262 * @param pc context for the upload to stop
1265 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1270 GNUNET_FS_end_top (pc->h, pc->top);
1271 if (NULL != pc->qre)
1273 GNUNET_DATASTORE_cancel (pc->qre);
1276 if (NULL != pc->dsh)
1278 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1281 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1283 GNUNET_SCHEDULER_cancel (pc->upload_task);
1284 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1286 if (pc->serialization != NULL)
1288 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1290 GNUNET_free (pc->serialization);
1291 pc->serialization = NULL;
1293 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1294 if (GNUNET_YES == pc->in_network_wait)
1296 pc->in_network_wait = GNUNET_SYSERR;
1299 publish_cleanup (pc, NULL);
1304 * Context for the KSK publication.
1306 struct PublishKskContext
1312 struct GNUNET_FS_Uri *ksk_uri;
1315 * Global FS context.
1317 struct GNUNET_FS_Handle *h;
1320 * The master block that we are sending
1321 * (in plaintext), has "mdsize+slen" more
1322 * bytes than the struct would suggest.
1327 * Buffer of the same size as "kb" for
1328 * the encrypted version.
1333 * Handle to the datastore, NULL if we are just
1336 struct GNUNET_DATASTORE_Handle *dsh;
1339 * Handle to datastore PUT request.
1341 struct GNUNET_DATASTORE_QueueEntry *qre;
1344 * Function to call once we're done.
1346 GNUNET_FS_PublishContinuation cont;
1354 * When should the KBlocks expire?
1356 struct GNUNET_FS_BlockOptions bo;
1359 * Size of the serialized metadata.
1364 * Size of the (CHK) URI as a string.
1369 * Keyword that we are currently processing.
1377 * Continuation of "GNUNET_FS_publish_ksk" that performs
1378 * the actual publishing operation (iterating over all
1381 * @param cls closure of type "struct PublishKskContext*"
1385 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1389 * Function called by the datastore API with
1390 * the result from the PUT request.
1392 * @param cls closure of type "struct PublishKskContext*"
1393 * @param success GNUNET_OK on success
1394 * @param min_expiration minimum expiration time required for content to be stored
1395 * @param msg error message (or NULL)
1398 kb_put_cont (void *cls, int success,
1399 struct GNUNET_TIME_Absolute min_expiration,
1402 struct PublishKskContext *pkc = cls;
1405 if (GNUNET_OK != success)
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KB PUT operation complete\n");
1410 if (NULL != pkc->dsh)
1412 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1415 GNUNET_free (pkc->cpy);
1416 GNUNET_free (pkc->kb);
1417 pkc->cont (pkc->cont_cls, NULL, msg);
1418 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1422 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont, pkc,
1423 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1428 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1429 * publishing operation (iterating over all of the keywords).
1431 * @param cls closure of type "struct PublishKskContext*"
1435 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1437 struct PublishKskContext *pkc = cls;
1438 const char *keyword;
1439 GNUNET_HashCode key;
1440 GNUNET_HashCode query;
1441 struct GNUNET_CRYPTO_AesSessionKey skey;
1442 struct GNUNET_CRYPTO_AesInitializationVector iv;
1443 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1446 if ((pkc->i == pkc->ksk_uri->data.ksk.keywordCount) || (NULL == pkc->dsh))
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KSK PUT operation complete\n");
1451 if (NULL != pkc->dsh)
1453 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1456 GNUNET_free (pkc->cpy);
1457 GNUNET_free (pkc->kb);
1458 pkc->cont (pkc->cont_cls, pkc->ksk_uri, NULL);
1459 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1463 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing under keyword `%s'\n",
1468 /* first character of keyword indicates if it is
1469 * mandatory or not -- ignore for hashing */
1470 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1471 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1472 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1], pkc->slen + pkc->mdsize, &skey, &iv,
1474 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1475 GNUNET_assert (NULL != pk);
1476 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1477 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1478 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1480 GNUNET_assert (GNUNET_OK ==
1481 GNUNET_CRYPTO_rsa_sign (pk, &pkc->cpy->purpose,
1482 &pkc->cpy->signature));
1483 GNUNET_CRYPTO_rsa_key_free (pk);
1485 GNUNET_DATASTORE_put (pkc->dsh, 0, &query,
1486 pkc->mdsize + sizeof (struct KBlock) + pkc->slen,
1487 pkc->cpy, GNUNET_BLOCK_TYPE_FS_KBLOCK,
1488 pkc->bo.content_priority, pkc->bo.anonymity_level,
1489 pkc->bo.replication_level, pkc->bo.expiration_time,
1490 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1496 * Publish a CHK under various keywords on GNUnet.
1498 * @param h handle to the file sharing subsystem
1499 * @param ksk_uri keywords to use
1500 * @param meta metadata to use
1501 * @param uri URI to refer to in the KBlock
1502 * @param bo per-block options
1503 * @param options publication options
1504 * @param cont continuation
1505 * @param cont_cls closure for cont
1508 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1509 const struct GNUNET_FS_Uri *ksk_uri,
1510 const struct GNUNET_CONTAINER_MetaData *meta,
1511 const struct GNUNET_FS_Uri *uri,
1512 const struct GNUNET_FS_BlockOptions *bo,
1513 enum GNUNET_FS_PublishOptions options,
1514 GNUNET_FS_PublishContinuation cont, void *cont_cls)
1516 struct PublishKskContext *pkc;
1522 GNUNET_assert (NULL != uri);
1523 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1527 pkc->cont_cls = cont_cls;
1528 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1530 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1531 if (pkc->dsh == NULL)
1533 cont (cont_cls, NULL, _("Could not connect to datastore."));
1541 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1542 GNUNET_assert (pkc->mdsize >= 0);
1543 uris = GNUNET_FS_uri_to_string (uri);
1544 pkc->slen = strlen (uris) + 1;
1545 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1546 if (size > MAX_KBLOCK_SIZE)
1548 size = MAX_KBLOCK_SIZE;
1549 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1551 pkc->kb = GNUNET_malloc (size);
1552 kbe = (char *) &pkc->kb[1];
1553 memcpy (kbe, uris, pkc->slen);
1555 sptr = &kbe[pkc->slen];
1558 GNUNET_CONTAINER_meta_data_serialize (meta, &sptr, pkc->mdsize,
1559 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1560 if (pkc->mdsize == -1)
1563 GNUNET_free (pkc->kb);
1564 if (pkc->dsh != NULL)
1566 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1569 cont (cont_cls, NULL, _("Internal error."));
1573 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1575 pkc->cpy = GNUNET_malloc (size);
1576 pkc->cpy->purpose.size =
1577 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1578 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1579 pkc->mdsize + pkc->slen);
1580 pkc->cpy->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1581 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1582 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont, pkc,
1583 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1587 /* end of fs_publish.c */