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_NO
41 * Context for "ds_put_cont".
46 * Current publishing context.
48 struct GNUNET_FS_PublishContext *pc;
51 * Specific file with the block.
53 struct GNUNET_FS_FileInformation *p;
56 * Function to run next, if any (can be NULL).
58 GNUNET_SCHEDULER_Task cont;
68 * Fill in all of the generic fields for
69 * a publish event and call the callback.
71 * @param pi structure to fill in
72 * @param pc overall publishing context
73 * @param p file information for the file being published
74 * @param offset where in the file are we so far
75 * @return value returned from callback
78 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
79 struct GNUNET_FS_PublishContext *pc,
80 const struct GNUNET_FS_FileInformation *p,
83 pi->value.publish.pc = pc;
84 pi->value.publish.fi = p;
85 pi->value.publish.cctx
87 pi->value.publish.pctx
88 = (NULL == p->dir) ? NULL : p->dir->client_info;
89 pi->value.publish.filename = p->filename;
90 pi->value.publish.size
91 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
93 = GNUNET_TIME_calculate_eta (p->start_time,
95 pi->value.publish.size);
96 pi->value.publish.completed = offset;
97 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
98 pi->value.publish.anonymity = p->bo.anonymity_level;
99 return pc->h->upcb (pc->h->upcb_cls,
105 * Cleanup the publish context, we're done with it.
107 * @param cls struct to clean up after
108 * @param tc scheduler context
111 publish_cleanup (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc)
114 struct GNUNET_FS_PublishContext *pc = cls;
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Cleaning up publish context (done!)\n");
122 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
125 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
126 if (pc->namespace != NULL)
128 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
129 pc->namespace = NULL;
131 GNUNET_free_non_null (pc->nid);
132 GNUNET_free_non_null (pc->nuid);
133 GNUNET_free_non_null (pc->serialization);
136 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
139 if (pc->client != NULL)
141 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
144 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
150 * Function called by the datastore API with
151 * the result from the PUT request.
153 * @param cls our closure
154 * @param success GNUNET_OK on success
155 * @param msg error message (or NULL)
158 ds_put_cont (void *cls,
162 struct PutContCtx *pcc = cls;
163 struct GNUNET_FS_ProgressInfo pi;
166 if (GNUNET_SYSERR == pcc->pc->in_network_wait)
168 /* we were aborted in the meantime, finish shutdown! */
169 GNUNET_SCHEDULER_add_continuation (&publish_cleanup,
171 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175 GNUNET_assert (GNUNET_YES == pcc->pc->in_network_wait);
176 pcc->pc->in_network_wait = GNUNET_NO;
177 if (GNUNET_SYSERR == success)
179 GNUNET_asprintf (&pcc->p->emsg,
180 _("Publishing failed: %s"),
182 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
183 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
184 pi.value.publish.specifics.error.message = pcc->p->emsg;
185 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->pc, pcc->p, 0);
186 if ( (pcc->p->is_directory == GNUNET_NO) &&
187 (pcc->p->filename != NULL) &&
188 (pcc->p->data.file.do_index == GNUNET_YES) )
190 /* run unindex to clean up */
191 GNUNET_FS_unindex_start (pcc->pc->h,
196 if (NULL != pcc->cont)
198 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pcc->pc->upload_task);
200 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
209 * Generate the callback that signals clients
210 * that a file (or directory) has been completely
213 * @param p the completed upload
214 * @param pc context of the publication
217 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
218 struct GNUNET_FS_PublishContext *pc)
220 struct GNUNET_FS_ProgressInfo pi;
222 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
223 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
224 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
225 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p,
226 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
231 * Generate the callback that signals clients
232 * that a file (or directory) has encountered
233 * a problem during publication.
235 * @param p the upload that had trouble
236 * @param pc context of the publication
237 * @param emsg error message
240 signal_publish_error (struct GNUNET_FS_FileInformation *p,
241 struct GNUNET_FS_PublishContext *pc,
244 struct GNUNET_FS_ProgressInfo pi;
246 p->emsg = GNUNET_strdup (emsg);
247 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
248 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
249 pi.value.publish.specifics.error.message =emsg;
250 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
251 if ( (p->is_directory == GNUNET_NO) &&
252 (p->filename != NULL) &&
253 (p->data.file.do_index == GNUNET_YES) )
255 /* run unindex to clean up */
256 GNUNET_FS_unindex_start (pc->h,
265 * Datastore returns from reservation cancel request.
267 * @param cls the 'struct GNUNET_FS_PublishContext'
268 * @param success success code (not used)
269 * @param msg error message (typically NULL, not used)
272 finish_release_reserve (void *cls,
276 struct GNUNET_FS_PublishContext *pc = cls;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Releasing reserve done!\n");
283 signal_publish_completion (pc->fi, pc);
284 pc->all_done = GNUNET_YES;
285 GNUNET_FS_publish_sync_ (pc);
290 * We've finished publishing the SBlock as part of a larger upload.
291 * Check the result and complete the larger upload.
293 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
294 * @param uri URI of the published SBlock
295 * @param emsg NULL on success, otherwise error message
298 publish_sblocks_cont (void *cls,
299 const struct GNUNET_FS_Uri *uri,
302 struct GNUNET_FS_PublishContext *pc = cls;
305 signal_publish_error (pc->fi,
308 GNUNET_FS_publish_sync_ (pc);
311 GNUNET_assert (pc->qre == NULL);
312 if ( (pc->dsh != NULL) &&
315 pc->qre = GNUNET_DATASTORE_release_reserve (pc->dsh,
319 GNUNET_TIME_UNIT_FOREVER_REL,
320 &finish_release_reserve,
325 finish_release_reserve (pc, GNUNET_OK, NULL);
331 * We are almost done publishing the structure,
332 * add SBlocks (if needed).
334 * @param pc overall upload data
337 publish_sblock (struct GNUNET_FS_PublishContext *pc)
339 if (NULL != pc->namespace)
340 GNUNET_FS_publish_sks (pc->h,
348 &publish_sblocks_cont,
351 publish_sblocks_cont (pc, NULL, NULL);
356 * We've finished publishing a KBlock as part of a larger upload.
357 * Check the result and continue the larger upload.
359 * @param cls the "struct GNUNET_FS_PublishContext*"
360 * of the larger upload
361 * @param uri URI of the published blocks
362 * @param emsg NULL on success, otherwise error message
365 publish_kblocks_cont (void *cls,
366 const struct GNUNET_FS_Uri *uri,
369 struct GNUNET_FS_PublishContext *pc = cls;
370 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Error uploading KSK blocks: %s\n",
379 signal_publish_error (p, pc, emsg);
380 GNUNET_FS_file_information_sync_ (p);
381 GNUNET_FS_publish_sync_ (pc);
382 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
384 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
385 &GNUNET_FS_publish_main_,
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "KSK blocks published, moving on to next file\n");
394 signal_publish_completion (p, pc);
395 /* move on to next file */
397 pc->fi_pos = p->next;
400 GNUNET_FS_publish_sync_ (pc);
401 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
403 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
404 &GNUNET_FS_publish_main_,
410 * Function called by the tree encoder to obtain
411 * a block of plaintext data (for the lowest level
414 * @param cls our publishing context
415 * @param offset identifies which block to get
416 * @param max (maximum) number of bytes to get; returning
417 * fewer will also cause errors
418 * @param buf where to copy the plaintext buffer
419 * @param emsg location to store an error message (on error)
420 * @return number of bytes copied to buf, 0 on error
423 block_reader (void *cls,
429 struct GNUNET_FS_PublishContext *pc = cls;
430 struct GNUNET_FS_FileInformation *p;
437 pt_size = GNUNET_MIN(max,
438 p->data.dir.dir_size - offset);
439 dd = p->data.dir.dir_data;
446 pt_size = GNUNET_MIN(max,
447 p->data.file.file_size - offset);
449 return 0; /* calling reader with pt_size==0
450 might free buf, so don't! */
452 p->data.file.reader (p->data.file.reader_cls,
464 * The tree encoder has finished processing a
465 * file. Call it's finish method and deal with
468 * @param cls our publishing context
469 * @param tc scheduler's task context (not used)
472 encode_cont (void *cls,
473 const struct GNUNET_SCHEDULER_TaskContext *tc)
475 struct GNUNET_FS_PublishContext *pc = cls;
476 struct GNUNET_FS_FileInformation *p;
477 struct GNUNET_FS_ProgressInfo pi;
482 GNUNET_FS_tree_encoder_finish (p->te,
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Error during tree walk: %s\n",
493 GNUNET_asprintf (&p->emsg,
494 _("Publishing failed: %s"),
497 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
498 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
499 pi.value.publish.specifics.error.message = p->emsg;
500 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Finished with tree encoder\n");
506 /* final progress event */
507 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
508 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
509 pi.value.publish.specifics.progress.data = NULL;
510 pi.value.publish.specifics.progress.offset = flen;
511 pi.value.publish.specifics.progress.data_len = 0;
512 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
513 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
515 /* continue with main */
516 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
518 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
519 &GNUNET_FS_publish_main_,
525 * Function called asking for the current (encoded)
526 * block to be processed. After processing the
527 * client should either call "GNUNET_FS_tree_encode_next"
528 * or (on error) "GNUNET_FS_tree_encode_finish".
531 * @param chk content hash key for the block
532 * @param offset offset of the block in the file
533 * @param depth depth of the block in the file, 0 for DBLOCK
534 * @param type type of the block (IBLOCK or DBLOCK)
535 * @param block the (encrypted) block
536 * @param block_size size of block (in bytes)
539 block_proc (void *cls,
540 const struct ContentHashKey *chk,
543 enum GNUNET_BLOCK_Type type,
547 struct GNUNET_FS_PublishContext *pc = cls;
548 struct GNUNET_FS_FileInformation *p;
549 struct PutContCtx * dpc_cls;
550 struct OnDemandBlock odb;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Waiting for datastore connection\n");
559 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
561 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
562 &GNUNET_FS_publish_main_,
567 GNUNET_assert (GNUNET_NO == pc->in_network_wait);
568 pc->in_network_wait = GNUNET_YES;
569 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
570 dpc_cls->cont = &GNUNET_FS_publish_main_;
571 dpc_cls->cont_cls = pc;
574 if ( (! p->is_directory) &&
575 (GNUNET_YES == p->data.file.do_index) &&
576 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK) )
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Indexing block `%s' for offset %llu with index size %u\n",
581 GNUNET_h2s (&chk->query),
582 (unsigned long long) offset,
583 sizeof (struct OnDemandBlock));
585 odb.offset = GNUNET_htonll (offset);
586 odb.file_id = p->data.file.file_id;
587 GNUNET_assert (pc->qre == NULL);
588 pc->qre = GNUNET_DATASTORE_put (pc->dsh,
589 (p->is_directory) ? 0 : pc->rid,
591 sizeof (struct OnDemandBlock),
593 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
594 p->bo.content_priority,
595 p->bo.anonymity_level,
596 p->bo.replication_level,
597 p->bo.expiration_time,
599 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Publishing block `%s' for offset %llu with size %u\n",
607 GNUNET_h2s (&chk->query),
608 (unsigned long long) offset,
609 (unsigned int) block_size);
611 GNUNET_assert (pc->qre == NULL);
612 pc->qre = GNUNET_DATASTORE_put (pc->dsh,
613 (p->is_directory) ? 0 : pc->rid,
618 p->bo.content_priority,
619 p->bo.anonymity_level,
620 p->bo.replication_level,
621 p->bo.expiration_time,
623 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
630 * Function called with information about our
631 * progress in computing the tree encoding.
634 * @param offset where are we in the file
635 * @param pt_block plaintext of the currently processed block
636 * @param pt_size size of pt_block
637 * @param depth depth of the block in the tree, 0 for DBLOCK
640 progress_proc (void *cls,
642 const void *pt_block,
646 struct GNUNET_FS_PublishContext *pc = cls;
647 struct GNUNET_FS_FileInformation *p;
648 struct GNUNET_FS_ProgressInfo pi;
651 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
652 pi.value.publish.specifics.progress.data = pt_block;
653 pi.value.publish.specifics.progress.offset = offset;
654 pi.value.publish.specifics.progress.data_len = pt_size;
655 pi.value.publish.specifics.progress.depth = depth;
656 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
661 * We are uploading a file or directory; load (if necessary) the next
662 * block into memory, encrypt it and send it to the FS service. Then
663 * continue with the main task.
665 * @param pc overall upload data
668 publish_content (struct GNUNET_FS_PublishContext *pc)
670 struct GNUNET_FS_FileInformation *p;
672 struct GNUNET_FS_DirectoryBuilder *db;
673 struct GNUNET_FS_FileInformation *dirpos;
678 GNUNET_assert (p != NULL);
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Creating directory\n");
687 db = GNUNET_FS_directory_builder_create (p->meta);
688 dirpos = p->data.dir.entries;
689 while (NULL != dirpos)
691 if (dirpos->is_directory)
693 raw_data = dirpos->data.dir.dir_data;
694 dirpos->data.dir.dir_data = NULL;
699 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
700 (dirpos->data.file.file_size > 0) )
702 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
704 if (dirpos->data.file.file_size !=
705 dirpos->data.file.reader (dirpos->data.file.reader_cls,
707 dirpos->data.file.file_size,
711 GNUNET_free_non_null (emsg);
712 GNUNET_free (raw_data);
717 GNUNET_FS_directory_builder_add (db,
721 GNUNET_free_non_null (raw_data);
722 dirpos = dirpos->next;
724 GNUNET_FS_directory_builder_finish (db,
725 &p->data.dir.dir_size,
726 &p->data.dir.dir_data);
727 GNUNET_FS_file_information_sync_ (p);
729 size = (p->is_directory)
730 ? p->data.dir.dir_size
731 : p->data.file.file_size;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "Creating tree encoder\n");
736 p->te = GNUNET_FS_tree_encoder_create (pc->h,
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Processing next block from tree\n");
749 GNUNET_FS_tree_encoder_next (p->te);
754 * Process the response (or lack thereof) from
755 * the "fs" service to our 'start index' request.
757 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
758 * @param msg the response we got
761 process_index_start_response (void *cls,
762 const struct GNUNET_MessageHeader *msg)
764 struct GNUNET_FS_PublishContext *pc = cls;
765 struct GNUNET_FS_FileInformation *p;
769 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
774 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
775 _("Can not index file `%s': %s. Will try to insert instead.\n"),
777 _("timeout on index-start request to `fs' service"));
778 p->data.file.do_index = GNUNET_NO;
779 GNUNET_FS_file_information_sync_ (p);
780 publish_content (pc);
783 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
785 msize = ntohs (msg->size);
786 emsg = (const char *) &msg[1];
787 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
788 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
789 emsg = gettext_noop ("unknown error");
790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791 _("Can not index file `%s': %s. Will try to insert instead.\n"),
794 p->data.file.do_index = GNUNET_NO;
795 GNUNET_FS_file_information_sync_ (p);
796 publish_content (pc);
799 p->data.file.index_start_confirmed = GNUNET_YES;
800 /* success! continue with indexing */
801 GNUNET_FS_file_information_sync_ (p);
802 publish_content (pc);
807 * Function called once the hash computation over an
808 * indexed file has completed.
810 * @param cls closure, our publishing context
811 * @param res resulting hash, NULL on error
814 hash_for_index_cb (void *cls,
815 const GNUNET_HashCode *
818 struct GNUNET_FS_PublishContext *pc = cls;
819 struct GNUNET_FS_FileInformation *p;
820 struct IndexStartMessage *ism;
822 struct GNUNET_CLIENT_Connection *client;
831 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
832 _("Can not index file `%s': %s. Will try to insert instead.\n"),
834 _("failed to compute hash"));
835 p->data.file.do_index = GNUNET_NO;
836 GNUNET_FS_file_information_sync_ (p);
837 publish_content (pc);
840 if (GNUNET_YES == p->data.file.index_start_confirmed)
842 publish_content (pc);
845 fn = GNUNET_STRINGS_filename_expand (p->filename);
846 GNUNET_assert (fn != NULL);
847 slen = strlen (fn) + 1;
848 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
851 _("Can not index file `%s': %s. Will try to insert instead.\n"),
853 _("filename too long"));
855 p->data.file.do_index = GNUNET_NO;
856 GNUNET_FS_file_information_sync_ (p);
857 publish_content (pc);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Hash of indexed file `%s' is `%s'\n",
866 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
868 p->data.file.file_id = *res;
869 p->data.file.have_hash = GNUNET_YES;
870 p->data.file.index_start_confirmed = GNUNET_YES;
871 GNUNET_FS_file_information_sync_ (p);
872 publish_content (pc);
876 client = GNUNET_CLIENT_connect ("fs",
880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
881 _("Can not index file `%s': %s. Will try to insert instead.\n"),
883 _("could not connect to `fs' service"));
884 p->data.file.do_index = GNUNET_NO;
885 publish_content (pc);
889 if (p->data.file.have_hash != GNUNET_YES)
891 p->data.file.file_id = *res;
892 p->data.file.have_hash = GNUNET_YES;
893 GNUNET_FS_file_information_sync_ (p);
895 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
897 ism->header.size = htons(sizeof(struct IndexStartMessage) +
899 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
901 GNUNET_DISK_file_get_identifiers (p->filename,
905 ism->device = GNUNET_htonll (dev);
906 ism->inode = GNUNET_htonll(ino);
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912 _("Failed to get file identifiers for `%s'\n"),
922 GNUNET_break (GNUNET_YES ==
923 GNUNET_CLIENT_transmit_and_get_response (client,
925 GNUNET_TIME_UNIT_FOREVER_REL,
927 &process_index_start_response,
934 * Main function that performs the upload.
936 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
937 * @param tc task context
940 GNUNET_FS_publish_main_ (void *cls,
941 const struct GNUNET_SCHEDULER_TaskContext *tc)
943 struct GNUNET_FS_PublishContext *pc = cls;
944 struct GNUNET_FS_ProgressInfo pi;
945 struct GNUNET_FS_FileInformation *p;
946 struct GNUNET_FS_Uri *loc;
949 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Publishing complete, now publishing SKS and KSK blocks.\n");
957 /* upload of entire hierarchy complete,
958 publish namespace entries */
959 GNUNET_FS_publish_sync_ (pc);
963 /* find starting position */
964 while ( (p->is_directory) &&
965 (NULL != p->data.dir.entries) &&
967 (NULL == p->data.dir.entries->chk_uri) )
969 p = p->data.dir.entries;
971 GNUNET_FS_publish_sync_ (pc);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Error uploading: %s\n",
981 /* error with current file, abort all
982 related files as well! */
983 while (NULL != p->dir)
985 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
986 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
990 GNUNET_asprintf (&p->emsg,
991 _("Recursive upload failed at `%s': %s"),
998 GNUNET_asprintf (&p->emsg,
999 _("Recursive upload failed: %s"),
1002 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
1003 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
1004 pi.value.publish.specifics.error.message = p->emsg;
1005 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
1007 pc->all_done = GNUNET_YES;
1008 GNUNET_FS_publish_sync_ (pc);
1011 /* handle completion */
1012 if (NULL != p->chk_uri)
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "File upload complete, now publishing KSK blocks.\n");
1018 if (0 == p->bo.anonymity_level)
1020 /* zero anonymity, box CHK URI in LOC URI */
1021 loc = GNUNET_FS_uri_loc_create (p->chk_uri,
1023 p->bo.expiration_time);
1024 GNUNET_FS_uri_destroy (p->chk_uri);
1027 GNUNET_FS_publish_sync_ (pc);
1028 /* upload of "p" complete, publish KBlocks! */
1029 if (p->keywords != NULL)
1031 GNUNET_FS_publish_ksk (pc->h,
1037 &publish_kblocks_cont,
1042 publish_kblocks_cont (pc,
1048 if ( (!p->is_directory) &&
1049 (p->data.file.do_index) )
1051 if (NULL == p->filename)
1053 p->data.file.do_index = GNUNET_NO;
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1055 _("Can not index file `%s': %s. Will try to insert instead.\n"),
1057 _("needs to be an actual file"));
1058 GNUNET_FS_file_information_sync_ (p);
1059 publish_content (pc);
1062 if (p->data.file.have_hash)
1064 hash_for_index_cb (pc,
1065 &p->data.file.file_id);
1069 p->start_time = GNUNET_TIME_absolute_get ();
1070 pc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
1078 publish_content (pc);
1083 * Signal the FS's progress function that we are starting
1086 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1087 * @param fi the entry in the publish-structure
1088 * @param length length of the file or directory
1089 * @param meta metadata for the file or directory (can be modified)
1090 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1091 * @param bo block options
1092 * @param do_index should we index?
1093 * @param client_info pointer to client context set upon creation (can be modified)
1094 * @return GNUNET_OK to continue (always)
1097 fip_signal_start(void *cls,
1098 struct GNUNET_FS_FileInformation *fi,
1100 struct GNUNET_CONTAINER_MetaData *meta,
1101 struct GNUNET_FS_Uri **uri,
1102 struct GNUNET_FS_BlockOptions *bo,
1106 struct GNUNET_FS_PublishContext *pc = cls;
1107 struct GNUNET_FS_ProgressInfo pi;
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Starting publish operation\n");
1117 /* space for on-demand blocks */
1118 pc->reserve_space += ((length + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1122 /* space for DBlocks */
1123 pc->reserve_space += length;
1125 /* entries for IBlocks and DBlocks, space for IBlocks */
1129 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1130 pc->reserve_entries += left;
1133 left = left * sizeof (struct ContentHashKey);
1134 pc->reserve_space += left;
1136 pc->reserve_entries++;
1137 /* entries and space for keywords */
1140 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1141 pc->reserve_entries += kc;
1142 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1144 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1145 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1146 GNUNET_FS_file_information_sync_ (fi);
1152 * Signal the FS's progress function that we are suspending
1155 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1156 * @param fi the entry in the publish-structure
1157 * @param length length of the file or directory
1158 * @param meta metadata for the file or directory (can be modified)
1159 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1160 * @param bo block options
1161 * @param do_index should we index?
1162 * @param client_info pointer to client context set upon creation (can be modified)
1163 * @return GNUNET_OK to continue (always)
1166 fip_signal_suspend(void *cls,
1167 struct GNUNET_FS_FileInformation *fi,
1169 struct GNUNET_CONTAINER_MetaData *meta,
1170 struct GNUNET_FS_Uri **uri,
1171 struct GNUNET_FS_BlockOptions *bo,
1175 struct GNUNET_FS_PublishContext*pc = cls;
1176 struct GNUNET_FS_ProgressInfo pi;
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Suspending publish operation\n");
1183 GNUNET_free_non_null (fi->serialization);
1184 fi->serialization = NULL;
1185 off = (fi->chk_uri == NULL) ? 0 : length;
1186 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1187 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1188 *client_info = NULL;
1189 if (NULL != pc->qre)
1191 GNUNET_DATASTORE_cancel (pc->qre);
1194 if (NULL != pc->dsh)
1196 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1205 * Create SUSPEND event for the given publish operation
1206 * and then clean up our state (without stop signal).
1208 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1211 GNUNET_FS_publish_signal_suspend_ (void *cls)
1213 struct GNUNET_FS_PublishContext *pc = cls;
1215 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1217 GNUNET_SCHEDULER_cancel (pc->upload_task);
1218 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1220 GNUNET_FS_file_information_inspect (pc->fi,
1221 &fip_signal_suspend,
1223 GNUNET_FS_end_top (pc->h, pc->top);
1225 publish_cleanup (pc, NULL);
1230 * We have gotten a reply for our space reservation request.
1231 * Either fail (insufficient space) or start publishing for good.
1233 * @param cls the 'struct GNUNET_FS_PublishContext*'
1234 * @param success positive reservation ID on success
1235 * @param msg error message on error, otherwise NULL
1238 finish_reserve (void *cls,
1242 struct GNUNET_FS_PublishContext *pc = cls;
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Reservation complete (%d)!\n",
1250 if ( (msg != NULL) ||
1253 GNUNET_asprintf (&pc->fi->emsg,
1254 _("Insufficient space for publishing: %s"),
1256 signal_publish_error (pc->fi,
1262 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1264 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1265 &GNUNET_FS_publish_main_,
1271 * Publish a file or directory.
1273 * @param h handle to the file sharing subsystem
1274 * @param fi information about the file or directory structure to publish
1275 * @param namespace namespace to publish the file in, NULL for no namespace
1276 * @param nid identifier to use for the publishd content in the namespace
1277 * (can be NULL, must be NULL if namespace is NULL)
1278 * @param nuid update-identifier that will be used for future updates
1279 * (can be NULL, must be NULL if namespace or nid is NULL)
1280 * @param options options for the publication
1281 * @return context that can be used to control the publish operation
1283 struct GNUNET_FS_PublishContext *
1284 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1285 struct GNUNET_FS_FileInformation *fi,
1286 struct GNUNET_FS_Namespace *namespace,
1289 enum GNUNET_FS_PublishOptions options)
1291 struct GNUNET_FS_PublishContext *ret;
1292 struct GNUNET_DATASTORE_Handle *dsh;
1294 GNUNET_assert (NULL != h);
1295 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1297 dsh = GNUNET_DATASTORE_connect (h->cfg);
1305 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1309 ret->namespace = namespace;
1310 ret->options = options;
1311 if (namespace != NULL)
1314 GNUNET_assert (NULL != nid);
1315 ret->nid = GNUNET_strdup (nid);
1317 ret->nuid = GNUNET_strdup (nuid);
1320 GNUNET_FS_file_information_inspect (ret->fi,
1323 ret->fi_pos = ret->fi;
1324 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1325 GNUNET_FS_publish_sync_ (ret);
1326 if (NULL != ret->dsh)
1328 GNUNET_assert (NULL == ret->qre);
1329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1330 _("Reserving space for %u entries and %llu bytes for publication\n"),
1331 (unsigned int) ret->reserve_entries,
1332 (unsigned long long) ret->reserve_space);
1333 ret->qre = GNUNET_DATASTORE_reserve (ret->dsh,
1335 ret->reserve_entries,
1338 GNUNET_TIME_UNIT_FOREVER_REL,
1344 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1346 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1347 &GNUNET_FS_publish_main_,
1355 * Signal the FS's progress function that we are stopping
1358 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1359 * @param fi the entry in the publish-structure
1360 * @param length length of the file or directory
1361 * @param meta metadata for the file or directory (can be modified)
1362 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1363 * @param bo block options (can be modified)
1364 * @param do_index should we index?
1365 * @param client_info pointer to client context set upon creation (can be modified)
1366 * @return GNUNET_OK to continue (always)
1369 fip_signal_stop(void *cls,
1370 struct GNUNET_FS_FileInformation *fi,
1372 struct GNUNET_CONTAINER_MetaData *meta,
1373 struct GNUNET_FS_Uri **uri,
1374 struct GNUNET_FS_BlockOptions *bo,
1378 struct GNUNET_FS_PublishContext*pc = cls;
1379 struct GNUNET_FS_ProgressInfo pi;
1382 if (fi->serialization != NULL)
1384 GNUNET_FS_remove_sync_file_ (pc->h,
1385 GNUNET_FS_SYNC_PATH_FILE_INFO,
1387 GNUNET_free (fi->serialization);
1388 fi->serialization = NULL;
1390 off = (fi->chk_uri == NULL) ? 0 : length;
1391 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1392 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1393 *client_info = NULL;
1399 * Stop an upload. Will abort incomplete uploads (but
1400 * not remove blocks that have already been publishd) or
1401 * simply clean up the state for completed uploads.
1402 * Must NOT be called from within the event callback!
1404 * @param pc context for the upload to stop
1407 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Publish stop called\n");
1413 GNUNET_FS_end_top (pc->h, pc->top);
1414 if (NULL != pc->qre)
1416 GNUNET_DATASTORE_cancel (pc->qre);
1419 if (NULL != pc->dsh)
1421 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1424 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1426 GNUNET_SCHEDULER_cancel (pc->upload_task);
1427 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1429 if (pc->serialization != NULL)
1431 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1432 GNUNET_free (pc->serialization);
1433 pc->serialization = NULL;
1435 GNUNET_FS_file_information_inspect (pc->fi,
1438 if (GNUNET_YES == pc->in_network_wait)
1440 pc->in_network_wait = GNUNET_SYSERR;
1443 publish_cleanup (pc, NULL);
1448 * Context for the KSK publication.
1450 struct PublishKskContext
1456 struct GNUNET_FS_Uri *ksk_uri;
1459 * Global FS context.
1461 struct GNUNET_FS_Handle *h;
1464 * The master block that we are sending
1465 * (in plaintext), has "mdsize+slen" more
1466 * bytes than the struct would suggest.
1471 * Buffer of the same size as "kb" for
1472 * the encrypted version.
1477 * Handle to the datastore, NULL if we are just
1480 struct GNUNET_DATASTORE_Handle *dsh;
1483 * Handle to datastore PUT request.
1485 struct GNUNET_DATASTORE_QueueEntry *qre;
1488 * Function to call once we're done.
1490 GNUNET_FS_PublishContinuation cont;
1498 * When should the KBlocks expire?
1500 struct GNUNET_FS_BlockOptions bo;
1503 * Size of the serialized metadata.
1508 * Size of the (CHK) URI as a string.
1513 * Keyword that we are currently processing.
1521 * Continuation of "GNUNET_FS_publish_ksk" that performs
1522 * the actual publishing operation (iterating over all
1525 * @param cls closure of type "struct PublishKskContext*"
1529 publish_ksk_cont (void *cls,
1530 const struct GNUNET_SCHEDULER_TaskContext *tc);
1534 * Function called by the datastore API with
1535 * the result from the PUT request.
1537 * @param cls closure of type "struct PublishKskContext*"
1538 * @param success GNUNET_OK on success
1539 * @param msg error message (or NULL)
1542 kb_put_cont (void *cls,
1546 struct PublishKskContext *pkc = cls;
1549 if (GNUNET_OK != success)
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "KB PUT operation complete\n");
1555 if (NULL != pkc->dsh)
1557 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1560 GNUNET_free (pkc->cpy);
1561 GNUNET_free (pkc->kb);
1562 pkc->cont (pkc->cont_cls,
1565 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1569 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1571 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1576 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1577 * publishing operation (iterating over all of the keywords).
1579 * @param cls closure of type "struct PublishKskContext*"
1583 publish_ksk_cont (void *cls,
1584 const struct GNUNET_SCHEDULER_TaskContext *tc)
1586 struct PublishKskContext *pkc = cls;
1587 const char *keyword;
1588 GNUNET_HashCode key;
1589 GNUNET_HashCode query;
1590 struct GNUNET_CRYPTO_AesSessionKey skey;
1591 struct GNUNET_CRYPTO_AesInitializationVector iv;
1592 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1595 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1596 (NULL == pkc->dsh) )
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "KSK PUT operation complete\n");
1602 if (NULL != pkc->dsh)
1604 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1607 GNUNET_free (pkc->cpy);
1608 GNUNET_free (pkc->kb);
1609 pkc->cont (pkc->cont_cls,
1612 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1616 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Publishing under keyword `%s'\n",
1622 /* first character of keyword indicates if it is
1623 mandatory or not -- ignore for hashing */
1624 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1625 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1626 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1627 pkc->slen + pkc->mdsize,
1631 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1632 GNUNET_assert (NULL != pk);
1633 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1634 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1635 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1637 GNUNET_assert (GNUNET_OK ==
1638 GNUNET_CRYPTO_rsa_sign (pk,
1640 &pkc->cpy->signature));
1641 GNUNET_CRYPTO_rsa_key_free (pk);
1642 pkc->qre = GNUNET_DATASTORE_put (pkc->dsh,
1646 sizeof (struct KBlock) +
1649 GNUNET_BLOCK_TYPE_FS_KBLOCK,
1650 pkc->bo.content_priority,
1651 pkc->bo.anonymity_level,
1652 pkc->bo.replication_level,
1653 pkc->bo.expiration_time,
1655 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1662 * Publish a CHK under various keywords on GNUnet.
1664 * @param h handle to the file sharing subsystem
1665 * @param ksk_uri keywords to use
1666 * @param meta metadata to use
1667 * @param uri URI to refer to in the KBlock
1668 * @param bo per-block options
1669 * @param options publication options
1670 * @param cont continuation
1671 * @param cont_cls closure for cont
1674 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1675 const struct GNUNET_FS_Uri *ksk_uri,
1676 const struct GNUNET_CONTAINER_MetaData *meta,
1677 const struct GNUNET_FS_Uri *uri,
1678 const struct GNUNET_FS_BlockOptions *bo,
1679 enum GNUNET_FS_PublishOptions options,
1680 GNUNET_FS_PublishContinuation cont,
1683 struct PublishKskContext *pkc;
1689 GNUNET_assert (NULL != uri);
1690 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1694 pkc->cont_cls = cont_cls;
1695 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1697 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1698 if (pkc->dsh == NULL)
1700 cont (cont_cls, NULL, _("Could not connect to datastore."));
1708 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1709 GNUNET_assert (pkc->mdsize >= 0);
1710 uris = GNUNET_FS_uri_to_string (uri);
1711 pkc->slen = strlen (uris) + 1;
1712 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1713 if (size > MAX_KBLOCK_SIZE)
1715 size = MAX_KBLOCK_SIZE;
1716 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1718 pkc->kb = GNUNET_malloc (size);
1719 kbe = (char *) &pkc->kb[1];
1720 memcpy (kbe, uris, pkc->slen);
1722 sptr = &kbe[pkc->slen];
1724 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1727 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1728 if (pkc->mdsize == -1)
1731 GNUNET_free (pkc->kb);
1732 if (pkc->dsh != NULL)
1734 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1737 cont (cont_cls, NULL, _("Internal error."));
1741 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1743 pkc->cpy = GNUNET_malloc (size);
1744 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1745 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1748 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1749 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1750 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1752 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1756 /* end of fs_publish.c */