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 *sc;
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 sc 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 *sc,
80 const struct GNUNET_FS_FileInformation *p,
83 pi->value.publish.pc = sc;
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 sc->h->upcb (sc->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;
118 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
121 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
122 if (pc->namespace != NULL)
123 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
124 GNUNET_free_non_null (pc->nid);
125 GNUNET_free_non_null (pc->nuid);
126 GNUNET_free_non_null (pc->serialization);
129 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
132 if (pc->client != NULL)
133 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
139 * Function called by the datastore API with
140 * the result from the PUT request.
142 * @param cls our closure
143 * @param success GNUNET_OK on success
144 * @param msg error message (or NULL)
147 ds_put_cont (void *cls,
151 struct PutContCtx *pcc = cls;
152 struct GNUNET_FS_ProgressInfo pi;
154 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
156 /* we were aborted in the meantime, finish shutdown! */
157 GNUNET_SCHEDULER_add_continuation (&publish_cleanup,
159 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
163 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
164 pcc->sc->in_network_wait = GNUNET_NO;
165 if (GNUNET_SYSERR == success)
167 GNUNET_asprintf (&pcc->p->emsg,
168 _("Publishing failed: %s"),
170 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
171 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
172 pi.value.publish.specifics.error.message = pcc->p->emsg;
173 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
174 if ( (pcc->p->is_directory == GNUNET_NO) &&
175 (pcc->p->filename != NULL) &&
176 (pcc->p->data.file.do_index == GNUNET_YES) )
178 /* run unindex to clean up */
179 GNUNET_FS_unindex_start (pcc->sc->h,
184 if (NULL != pcc->cont)
186 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
194 * Generate the callback that signals clients
195 * that a file (or directory) has been completely
198 * @param p the completed upload
199 * @param sc context of the publication
202 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
203 struct GNUNET_FS_PublishContext *sc)
205 struct GNUNET_FS_ProgressInfo pi;
207 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
208 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
209 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
210 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
211 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
216 * Generate the callback that signals clients
217 * that a file (or directory) has encountered
218 * a problem during publication.
220 * @param p the upload that had trouble
221 * @param sc context of the publication
222 * @param emsg error message
225 signal_publish_error (struct GNUNET_FS_FileInformation *p,
226 struct GNUNET_FS_PublishContext *sc,
229 struct GNUNET_FS_ProgressInfo pi;
231 p->emsg = GNUNET_strdup (emsg);
232 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
233 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
234 pi.value.publish.specifics.error.message =emsg;
235 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
236 if ( (p->is_directory == GNUNET_NO) &&
237 (p->filename != NULL) &&
238 (p->data.file.do_index == GNUNET_YES) )
240 /* run unindex to clean up */
241 GNUNET_FS_unindex_start (sc->h,
250 * Datastore returns from reservation cancel request.
252 * @param cls the 'struct GNUNET_FS_PublishContext'
253 * @param success success code (not used)
254 * @param msg error message (typically NULL, not used)
257 finish_release_reserve (void *cls,
261 struct GNUNET_FS_PublishContext *pc = cls;
264 signal_publish_completion (pc->fi, pc);
265 pc->all_done = GNUNET_YES;
266 GNUNET_FS_publish_sync_ (pc);
271 * We've finished publishing the SBlock as part of a larger upload.
272 * Check the result and complete the larger upload.
274 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
275 * @param uri URI of the published SBlock
276 * @param emsg NULL on success, otherwise error message
279 publish_sblocks_cont (void *cls,
280 const struct GNUNET_FS_Uri *uri,
283 struct GNUNET_FS_PublishContext *pc = cls;
286 signal_publish_error (pc->fi,
289 GNUNET_FS_publish_sync_ (pc);
292 GNUNET_assert (pc->qre == NULL);
293 if ( (pc->dsh != NULL) &&
296 pc->qre = GNUNET_DATASTORE_release_reserve (pc->dsh,
300 GNUNET_TIME_UNIT_FOREVER_REL,
301 &finish_release_reserve,
306 finish_release_reserve (pc, GNUNET_OK, NULL);
312 * We are almost done publishing the structure,
313 * add SBlocks (if needed).
315 * @param sc overall upload data
318 publish_sblock (struct GNUNET_FS_PublishContext *sc)
320 if (NULL != sc->namespace)
321 GNUNET_FS_publish_sks (sc->h,
329 &publish_sblocks_cont,
332 publish_sblocks_cont (sc, NULL, NULL);
337 * We've finished publishing a KBlock as part of a larger upload.
338 * Check the result and continue the larger upload.
340 * @param cls the "struct GNUNET_FS_PublishContext*"
341 * of the larger upload
342 * @param uri URI of the published blocks
343 * @param emsg NULL on success, otherwise error message
346 publish_kblocks_cont (void *cls,
347 const struct GNUNET_FS_Uri *uri,
350 struct GNUNET_FS_PublishContext *pc = cls;
351 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Error uploading KSK blocks: %s\n",
360 signal_publish_error (p, pc, emsg);
361 GNUNET_FS_file_information_sync_ (p);
362 GNUNET_FS_publish_sync_ (pc);
364 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
365 &GNUNET_FS_publish_main_,
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "KSK blocks published, moving on to next file\n");
374 signal_publish_completion (p, pc);
375 /* move on to next file */
377 pc->fi_pos = p->next;
380 GNUNET_FS_publish_sync_ (pc);
382 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
383 &GNUNET_FS_publish_main_,
389 * Function called by the tree encoder to obtain
390 * a block of plaintext data (for the lowest level
393 * @param cls our publishing context
394 * @param offset identifies which block to get
395 * @param max (maximum) number of bytes to get; returning
396 * fewer will also cause errors
397 * @param buf where to copy the plaintext buffer
398 * @param emsg location to store an error message (on error)
399 * @return number of bytes copied to buf, 0 on error
402 block_reader (void *cls,
408 struct GNUNET_FS_PublishContext *sc = cls;
409 struct GNUNET_FS_FileInformation *p;
416 pt_size = GNUNET_MIN(max,
417 p->data.dir.dir_size - offset);
418 dd = p->data.dir.dir_data;
425 pt_size = GNUNET_MIN(max,
426 p->data.file.file_size - offset);
428 return 0; /* calling reader with pt_size==0
429 might free buf, so don't! */
431 p->data.file.reader (p->data.file.reader_cls,
443 * The tree encoder has finished processing a
444 * file. Call it's finish method and deal with
447 * @param cls our publishing context
448 * @param tc scheduler's task context (not used)
451 encode_cont (void *cls,
452 const struct GNUNET_SCHEDULER_TaskContext *tc)
454 struct GNUNET_FS_PublishContext *sc = cls;
455 struct GNUNET_FS_FileInformation *p;
456 struct GNUNET_FS_ProgressInfo pi;
461 GNUNET_FS_tree_encoder_finish (p->te,
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "Error during tree walk: %s\n",
472 GNUNET_asprintf (&p->emsg,
473 _("Publishing failed: %s"),
476 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
477 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
478 pi.value.publish.specifics.error.message = p->emsg;
479 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Finished with tree encoder\n");
485 /* final progress event */
486 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
487 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
488 pi.value.publish.specifics.progress.data = NULL;
489 pi.value.publish.specifics.progress.offset = flen;
490 pi.value.publish.specifics.progress.data_len = 0;
491 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
492 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, flen);
494 /* continue with main */
496 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
497 &GNUNET_FS_publish_main_,
503 * Function called asking for the current (encoded)
504 * block to be processed. After processing the
505 * client should either call "GNUNET_FS_tree_encode_next"
506 * or (on error) "GNUNET_FS_tree_encode_finish".
509 * @param chk content hash key for the block
510 * @param offset offset of the block in the file
511 * @param depth depth of the block in the file, 0 for DBLOCK
512 * @param type type of the block (IBLOCK or DBLOCK)
513 * @param block the (encrypted) block
514 * @param block_size size of block (in bytes)
517 block_proc (void *cls,
518 const struct ContentHashKey *chk,
521 enum GNUNET_BLOCK_Type type,
525 struct GNUNET_FS_PublishContext *sc = cls;
526 struct GNUNET_FS_FileInformation *p;
527 struct PutContCtx * dpc_cls;
528 struct OnDemandBlock odb;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Waiting for datastore connection\n");
538 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
539 &GNUNET_FS_publish_main_,
544 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
545 sc->in_network_wait = GNUNET_YES;
546 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
547 dpc_cls->cont = &GNUNET_FS_publish_main_;
548 dpc_cls->cont_cls = sc;
551 if ( (! p->is_directory) &&
552 (GNUNET_YES == p->data.file.do_index) &&
553 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK) )
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Indexing block `%s' for offset %llu with index size %u\n",
558 GNUNET_h2s (&chk->query),
559 (unsigned long long) offset,
560 sizeof (struct OnDemandBlock));
562 odb.offset = GNUNET_htonll (offset);
563 odb.file_id = p->data.file.file_id;
564 GNUNET_DATASTORE_put (sc->dsh,
565 (p->is_directory) ? 0 : sc->rid,
567 sizeof (struct OnDemandBlock),
569 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
570 p->bo.content_priority,
571 p->bo.anonymity_level,
572 p->bo.replication_level,
573 p->bo.expiration_time,
575 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Publishing block `%s' for offset %llu with size %u\n",
583 GNUNET_h2s (&chk->query),
584 (unsigned long long) offset,
585 (unsigned int) block_size);
587 GNUNET_DATASTORE_put (sc->dsh,
588 (p->is_directory) ? 0 : sc->rid,
593 p->bo.content_priority,
594 p->bo.anonymity_level,
595 p->bo.replication_level,
596 p->bo.expiration_time,
598 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
605 * Function called with information about our
606 * progress in computing the tree encoding.
609 * @param offset where are we in the file
610 * @param pt_block plaintext of the currently processed block
611 * @param pt_size size of pt_block
612 * @param depth depth of the block in the tree, 0 for DBLOCK
615 progress_proc (void *cls,
617 const void *pt_block,
621 struct GNUNET_FS_PublishContext *sc = cls;
622 struct GNUNET_FS_FileInformation *p;
623 struct GNUNET_FS_ProgressInfo pi;
626 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
627 pi.value.publish.specifics.progress.data = pt_block;
628 pi.value.publish.specifics.progress.offset = offset;
629 pi.value.publish.specifics.progress.data_len = pt_size;
630 pi.value.publish.specifics.progress.depth = depth;
631 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
636 * We are uploading a file or directory; load (if necessary) the next
637 * block into memory, encrypt it and send it to the FS service. Then
638 * continue with the main task.
640 * @param sc overall upload data
643 publish_content (struct GNUNET_FS_PublishContext *sc)
645 struct GNUNET_FS_FileInformation *p;
647 struct GNUNET_FS_DirectoryBuilder *db;
648 struct GNUNET_FS_FileInformation *dirpos;
653 GNUNET_assert (p != NULL);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Creating directory\n");
662 db = GNUNET_FS_directory_builder_create (p->meta);
663 dirpos = p->data.dir.entries;
664 while (NULL != dirpos)
666 if (dirpos->is_directory)
668 raw_data = dirpos->data.dir.dir_data;
669 dirpos->data.dir.dir_data = NULL;
674 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
675 (dirpos->data.file.file_size > 0) )
677 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
679 if (dirpos->data.file.file_size !=
680 dirpos->data.file.reader (dirpos->data.file.reader_cls,
682 dirpos->data.file.file_size,
686 GNUNET_free_non_null (emsg);
687 GNUNET_free (raw_data);
692 GNUNET_FS_directory_builder_add (db,
696 GNUNET_free_non_null (raw_data);
697 dirpos = dirpos->next;
699 GNUNET_FS_directory_builder_finish (db,
700 &p->data.dir.dir_size,
701 &p->data.dir.dir_data);
702 GNUNET_FS_file_information_sync_ (p);
704 size = (p->is_directory)
705 ? p->data.dir.dir_size
706 : p->data.file.file_size;
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Creating tree encoder\n");
711 p->te = GNUNET_FS_tree_encoder_create (sc->h,
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Processing next block from tree\n");
724 GNUNET_FS_tree_encoder_next (p->te);
729 * Process the response (or lack thereof) from
730 * the "fs" service to our 'start index' request.
732 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
733 * @param msg the response we got
736 process_index_start_response (void *cls,
737 const struct GNUNET_MessageHeader *msg)
739 struct GNUNET_FS_PublishContext *sc = cls;
740 struct GNUNET_FS_FileInformation *p;
744 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
750 _("Can not index file `%s': %s. Will try to insert instead.\n"),
752 _("timeout on index-start request to `fs' service"));
753 p->data.file.do_index = GNUNET_NO;
754 GNUNET_FS_file_information_sync_ (p);
755 publish_content (sc);
758 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
760 msize = ntohs (msg->size);
761 emsg = (const char *) &msg[1];
762 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
763 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
764 emsg = gettext_noop ("unknown error");
765 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
766 _("Can not index file `%s': %s. Will try to insert instead.\n"),
769 p->data.file.do_index = GNUNET_NO;
770 GNUNET_FS_file_information_sync_ (p);
771 publish_content (sc);
774 p->data.file.index_start_confirmed = GNUNET_YES;
775 /* success! continue with indexing */
776 GNUNET_FS_file_information_sync_ (p);
777 publish_content (sc);
782 * Function called once the hash computation over an
783 * indexed file has completed.
785 * @param cls closure, our publishing context
786 * @param res resulting hash, NULL on error
789 hash_for_index_cb (void *cls,
790 const GNUNET_HashCode *
793 struct GNUNET_FS_PublishContext *sc = cls;
794 struct GNUNET_FS_FileInformation *p;
795 struct IndexStartMessage *ism;
797 struct GNUNET_CLIENT_Connection *client;
806 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
807 _("Can not index file `%s': %s. Will try to insert instead.\n"),
809 _("failed to compute hash"));
810 p->data.file.do_index = GNUNET_NO;
811 GNUNET_FS_file_information_sync_ (p);
812 publish_content (sc);
815 if (GNUNET_YES == p->data.file.index_start_confirmed)
817 publish_content (sc);
820 fn = GNUNET_STRINGS_filename_expand (p->filename);
821 GNUNET_assert (fn != NULL);
822 slen = strlen (fn) + 1;
823 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 _("Can not index file `%s': %s. Will try to insert instead.\n"),
828 _("filename too long"));
830 p->data.file.do_index = GNUNET_NO;
831 GNUNET_FS_file_information_sync_ (p);
832 publish_content (sc);
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Hash of indexed file `%s' is `%s'\n",
841 if (0 != (sc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
843 p->data.file.file_id = *res;
844 p->data.file.have_hash = GNUNET_YES;
845 p->data.file.index_start_confirmed = GNUNET_YES;
846 GNUNET_FS_file_information_sync_ (p);
847 publish_content (sc);
851 client = GNUNET_CLIENT_connect ("fs",
855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
856 _("Can not index file `%s': %s. Will try to insert instead.\n"),
858 _("could not connect to `fs' service"));
859 p->data.file.do_index = GNUNET_NO;
860 publish_content (sc);
864 if (p->data.file.have_hash != GNUNET_YES)
866 p->data.file.file_id = *res;
867 p->data.file.have_hash = GNUNET_YES;
868 GNUNET_FS_file_information_sync_ (p);
870 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
872 ism->header.size = htons(sizeof(struct IndexStartMessage) +
874 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
876 GNUNET_DISK_file_get_identifiers (p->filename,
880 ism->device = GNUNET_htonll (dev);
881 ism->inode = GNUNET_htonll(ino);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 _("Failed to get file identifiers for `%s'\n"),
897 GNUNET_break (GNUNET_YES ==
898 GNUNET_CLIENT_transmit_and_get_response (client,
900 GNUNET_TIME_UNIT_FOREVER_REL,
902 &process_index_start_response,
909 * Main function that performs the upload.
911 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
912 * @param tc task context
915 GNUNET_FS_publish_main_ (void *cls,
916 const struct GNUNET_SCHEDULER_TaskContext *tc)
918 struct GNUNET_FS_PublishContext *pc = cls;
919 struct GNUNET_FS_ProgressInfo pi;
920 struct GNUNET_FS_FileInformation *p;
921 struct GNUNET_FS_Uri *loc;
924 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Publishing complete, now publishing SKS and KSK blocks.\n");
932 /* upload of entire hierarchy complete,
933 publish namespace entries */
934 GNUNET_FS_publish_sync_ (pc);
938 /* find starting position */
939 while ( (p->is_directory) &&
940 (NULL != p->data.dir.entries) &&
942 (NULL == p->data.dir.entries->chk_uri) )
944 p = p->data.dir.entries;
946 GNUNET_FS_publish_sync_ (pc);
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Error uploading: %s\n",
956 /* error with current file, abort all
957 related files as well! */
958 while (NULL != p->dir)
960 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
961 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
965 GNUNET_asprintf (&p->emsg,
966 _("Recursive upload failed at `%s': %s"),
973 GNUNET_asprintf (&p->emsg,
974 _("Recursive upload failed: %s"),
977 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
978 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
979 pi.value.publish.specifics.error.message = p->emsg;
980 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
982 pc->all_done = GNUNET_YES;
983 GNUNET_FS_publish_sync_ (pc);
986 /* handle completion */
987 if (NULL != p->chk_uri)
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "File upload complete, now publishing KSK blocks.\n");
993 if (0 == p->bo.anonymity_level)
995 /* zero anonymity, box CHK URI in LOC URI */
996 loc = GNUNET_FS_uri_loc_create (p->chk_uri,
998 p->bo.expiration_time);
999 GNUNET_FS_uri_destroy (p->chk_uri);
1002 GNUNET_FS_publish_sync_ (pc);
1003 /* upload of "p" complete, publish KBlocks! */
1004 if (p->keywords != NULL)
1006 GNUNET_FS_publish_ksk (pc->h,
1012 &publish_kblocks_cont,
1017 publish_kblocks_cont (pc,
1023 if ( (!p->is_directory) &&
1024 (p->data.file.do_index) )
1026 if (NULL == p->filename)
1028 p->data.file.do_index = GNUNET_NO;
1029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1030 _("Can not index file `%s': %s. Will try to insert instead.\n"),
1032 _("needs to be an actual file"));
1033 GNUNET_FS_file_information_sync_ (p);
1034 publish_content (pc);
1037 if (p->data.file.have_hash)
1039 hash_for_index_cb (pc,
1040 &p->data.file.file_id);
1044 p->start_time = GNUNET_TIME_absolute_get ();
1045 pc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
1053 publish_content (pc);
1058 * Signal the FS's progress function that we are starting
1061 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1062 * @param fi the entry in the publish-structure
1063 * @param length length of the file or directory
1064 * @param meta metadata for the file or directory (can be modified)
1065 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1066 * @param bo block options
1067 * @param do_index should we index?
1068 * @param client_info pointer to client context set upon creation (can be modified)
1069 * @return GNUNET_OK to continue (always)
1072 fip_signal_start(void *cls,
1073 struct GNUNET_FS_FileInformation *fi,
1075 struct GNUNET_CONTAINER_MetaData *meta,
1076 struct GNUNET_FS_Uri **uri,
1077 struct GNUNET_FS_BlockOptions *bo,
1081 struct GNUNET_FS_PublishContext *sc = cls;
1082 struct GNUNET_FS_ProgressInfo pi;
1088 /* space for on-demand blocks */
1089 sc->reserve_space += ((length + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1093 /* space for DBlocks */
1094 sc->reserve_space += length;
1096 /* entries for IBlocks and DBlocks, space for IBlocks */
1100 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1101 sc->reserve_entries += left;
1104 left = left * sizeof (struct ContentHashKey);
1105 sc->reserve_space += left;
1107 sc->reserve_entries++;
1108 /* entries and space for keywords */
1111 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1112 sc->reserve_entries += kc;
1113 sc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1115 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1116 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1117 GNUNET_FS_file_information_sync_ (fi);
1123 * Signal the FS's progress function that we are suspending
1126 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1127 * @param fi the entry in the publish-structure
1128 * @param length length of the file or directory
1129 * @param meta metadata for the file or directory (can be modified)
1130 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1131 * @param bo block options
1132 * @param do_index should we index?
1133 * @param client_info pointer to client context set upon creation (can be modified)
1134 * @return GNUNET_OK to continue (always)
1137 fip_signal_suspend(void *cls,
1138 struct GNUNET_FS_FileInformation *fi,
1140 struct GNUNET_CONTAINER_MetaData *meta,
1141 struct GNUNET_FS_Uri **uri,
1142 struct GNUNET_FS_BlockOptions *bo,
1146 struct GNUNET_FS_PublishContext*sc = cls;
1147 struct GNUNET_FS_ProgressInfo pi;
1150 GNUNET_free_non_null (fi->serialization);
1151 fi->serialization = NULL;
1152 off = (fi->chk_uri == NULL) ? 0 : length;
1153 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1154 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1155 *client_info = NULL;
1156 if (NULL != sc->dsh)
1158 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1166 * Create SUSPEND event for the given publish operation
1167 * and then clean up our state (without stop signal).
1169 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1172 GNUNET_FS_publish_signal_suspend_ (void *cls)
1174 struct GNUNET_FS_PublishContext *pc = cls;
1176 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1178 GNUNET_SCHEDULER_cancel (pc->upload_task);
1179 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1181 GNUNET_FS_file_information_inspect (pc->fi,
1182 &fip_signal_suspend,
1184 GNUNET_FS_end_top (pc->h, pc->top);
1185 publish_cleanup (pc, NULL);
1190 * We have gotten a reply for our space reservation request.
1191 * Either fail (insufficient space) or start publishing for good.
1193 * @param cls the 'struct GNUNET_FS_PublishContext*'
1194 * @param success positive reservation ID on success
1195 * @param msg error message on error, otherwise NULL
1198 finish_reserve (void *cls,
1202 struct GNUNET_FS_PublishContext *pc = cls;
1205 if ( (msg != NULL) ||
1208 GNUNET_asprintf (&pc->fi->emsg,
1209 _("Insufficient space for publishing: %s"),
1211 signal_publish_error (pc->fi,
1218 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1219 &GNUNET_FS_publish_main_,
1225 * Publish a file or directory.
1227 * @param h handle to the file sharing subsystem
1228 * @param fi information about the file or directory structure to publish
1229 * @param namespace namespace to publish the file in, NULL for no namespace
1230 * @param nid identifier to use for the publishd content in the namespace
1231 * (can be NULL, must be NULL if namespace is NULL)
1232 * @param nuid update-identifier that will be used for future updates
1233 * (can be NULL, must be NULL if namespace or nid is NULL)
1234 * @param options options for the publication
1235 * @return context that can be used to control the publish operation
1237 struct GNUNET_FS_PublishContext *
1238 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1239 struct GNUNET_FS_FileInformation *fi,
1240 struct GNUNET_FS_Namespace *namespace,
1243 enum GNUNET_FS_PublishOptions options)
1245 struct GNUNET_FS_PublishContext *ret;
1246 struct GNUNET_DATASTORE_Handle *dsh;
1248 GNUNET_assert (NULL != h);
1249 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1251 dsh = GNUNET_DATASTORE_connect (h->cfg);
1259 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1263 ret->namespace = namespace;
1264 ret->options = options;
1265 if (namespace != NULL)
1268 GNUNET_assert (NULL != nid);
1269 ret->nid = GNUNET_strdup (nid);
1271 ret->nuid = GNUNET_strdup (nuid);
1274 GNUNET_FS_file_information_inspect (ret->fi,
1277 ret->fi_pos = ret->fi;
1278 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1279 GNUNET_FS_publish_sync_ (ret);
1280 if (NULL != ret->dsh)
1282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1283 _("Reserving space for %u entries and %llu bytes for publication\n"),
1284 (unsigned int) ret->reserve_entries,
1285 (unsigned long long) ret->reserve_space);
1286 ret->qre = GNUNET_DATASTORE_reserve (ret->dsh,
1288 ret->reserve_entries,
1291 GNUNET_TIME_UNIT_FOREVER_REL,
1298 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1299 &GNUNET_FS_publish_main_,
1307 * Signal the FS's progress function that we are stopping
1310 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1311 * @param fi the entry in the publish-structure
1312 * @param length length of the file or directory
1313 * @param meta metadata for the file or directory (can be modified)
1314 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1315 * @param bo block options (can be modified)
1316 * @param do_index should we index?
1317 * @param client_info pointer to client context set upon creation (can be modified)
1318 * @return GNUNET_OK to continue (always)
1321 fip_signal_stop(void *cls,
1322 struct GNUNET_FS_FileInformation *fi,
1324 struct GNUNET_CONTAINER_MetaData *meta,
1325 struct GNUNET_FS_Uri **uri,
1326 struct GNUNET_FS_BlockOptions *bo,
1330 struct GNUNET_FS_PublishContext*sc = cls;
1331 struct GNUNET_FS_ProgressInfo pi;
1334 if (fi->serialization != NULL)
1336 GNUNET_FS_remove_sync_file_ (sc->h,
1337 GNUNET_FS_SYNC_PATH_FILE_INFO,
1339 GNUNET_free (fi->serialization);
1340 fi->serialization = NULL;
1342 off = (fi->chk_uri == NULL) ? 0 : length;
1343 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1344 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1345 *client_info = NULL;
1351 * Stop an upload. Will abort incomplete uploads (but
1352 * not remove blocks that have already been publishd) or
1353 * simply clean up the state for completed uploads.
1354 * Must NOT be called from within the event callback!
1356 * @param pc context for the upload to stop
1359 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1361 GNUNET_FS_end_top (pc->h, pc->top);
1362 if (NULL != pc->qre)
1364 GNUNET_DATASTORE_cancel (pc->qre);
1367 if (NULL != pc->dsh)
1369 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1372 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1374 GNUNET_SCHEDULER_cancel (pc->upload_task);
1375 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1377 if (pc->serialization != NULL)
1379 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1380 GNUNET_free (pc->serialization);
1381 pc->serialization = NULL;
1383 GNUNET_FS_file_information_inspect (pc->fi,
1386 if (GNUNET_YES == pc->in_network_wait)
1388 pc->in_network_wait = GNUNET_SYSERR;
1391 publish_cleanup (pc, NULL);
1396 * Context for the KSK publication.
1398 struct PublishKskContext
1404 struct GNUNET_FS_Uri *ksk_uri;
1407 * Global FS context.
1409 struct GNUNET_FS_Handle *h;
1412 * The master block that we are sending
1413 * (in plaintext), has "mdsize+slen" more
1414 * bytes than the struct would suggest.
1419 * Buffer of the same size as "kb" for
1420 * the encrypted version.
1425 * Handle to the datastore, NULL if we are just
1428 struct GNUNET_DATASTORE_Handle *dsh;
1431 * Function to call once we're done.
1433 GNUNET_FS_PublishContinuation cont;
1441 * When should the KBlocks expire?
1443 struct GNUNET_FS_BlockOptions bo;
1446 * Size of the serialized metadata.
1451 * Size of the (CHK) URI as a string.
1456 * Keyword that we are currently processing.
1464 * Continuation of "GNUNET_FS_publish_ksk" that performs
1465 * the actual publishing operation (iterating over all
1468 * @param cls closure of type "struct PublishKskContext*"
1472 publish_ksk_cont (void *cls,
1473 const struct GNUNET_SCHEDULER_TaskContext *tc);
1477 * Function called by the datastore API with
1478 * the result from the PUT request.
1480 * @param cls closure of type "struct PublishKskContext*"
1481 * @param success GNUNET_OK on success
1482 * @param msg error message (or NULL)
1485 kb_put_cont (void *cls,
1489 struct PublishKskContext *pkc = cls;
1491 if (GNUNET_OK != success)
1493 if (NULL != pkc->dsh)
1495 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1498 GNUNET_free (pkc->cpy);
1499 GNUNET_free (pkc->kb);
1500 pkc->cont (pkc->cont_cls,
1503 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1507 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1509 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1514 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1515 * publishing operation (iterating over all of the keywords).
1517 * @param cls closure of type "struct PublishKskContext*"
1521 publish_ksk_cont (void *cls,
1522 const struct GNUNET_SCHEDULER_TaskContext *tc)
1524 struct PublishKskContext *pkc = cls;
1525 const char *keyword;
1526 GNUNET_HashCode key;
1527 GNUNET_HashCode query;
1528 struct GNUNET_CRYPTO_AesSessionKey skey;
1529 struct GNUNET_CRYPTO_AesInitializationVector iv;
1530 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1533 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1534 (NULL == pkc->dsh) )
1536 if (NULL != pkc->dsh)
1538 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1541 GNUNET_free (pkc->cpy);
1542 GNUNET_free (pkc->kb);
1543 pkc->cont (pkc->cont_cls,
1546 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1550 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "Publishing under keyword `%s'\n",
1556 /* first character of keyword indicates if it is
1557 mandatory or not -- ignore for hashing */
1558 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1559 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1560 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1561 pkc->slen + pkc->mdsize,
1565 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1566 GNUNET_assert (NULL != pk);
1567 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1568 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1569 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1571 GNUNET_assert (GNUNET_OK ==
1572 GNUNET_CRYPTO_rsa_sign (pk,
1574 &pkc->cpy->signature));
1575 GNUNET_CRYPTO_rsa_key_free (pk);
1576 GNUNET_DATASTORE_put (pkc->dsh,
1580 sizeof (struct KBlock) +
1583 GNUNET_BLOCK_TYPE_FS_KBLOCK,
1584 pkc->bo.content_priority,
1585 pkc->bo.anonymity_level,
1586 pkc->bo.replication_level,
1587 pkc->bo.expiration_time,
1589 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1596 * Publish a CHK under various keywords on GNUnet.
1598 * @param h handle to the file sharing subsystem
1599 * @param ksk_uri keywords to use
1600 * @param meta metadata to use
1601 * @param uri URI to refer to in the KBlock
1602 * @param bo per-block options
1603 * @param options publication options
1604 * @param cont continuation
1605 * @param cont_cls closure for cont
1608 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1609 const struct GNUNET_FS_Uri *ksk_uri,
1610 const struct GNUNET_CONTAINER_MetaData *meta,
1611 const struct GNUNET_FS_Uri *uri,
1612 const struct GNUNET_FS_BlockOptions *bo,
1613 enum GNUNET_FS_PublishOptions options,
1614 GNUNET_FS_PublishContinuation cont,
1617 struct PublishKskContext *pkc;
1623 GNUNET_assert (NULL != uri);
1624 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1628 pkc->cont_cls = cont_cls;
1629 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1631 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1632 if (pkc->dsh == NULL)
1634 cont (cont_cls, NULL, _("Could not connect to datastore."));
1642 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1643 GNUNET_assert (pkc->mdsize >= 0);
1644 uris = GNUNET_FS_uri_to_string (uri);
1645 pkc->slen = strlen (uris) + 1;
1646 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1647 if (size > MAX_KBLOCK_SIZE)
1649 size = MAX_KBLOCK_SIZE;
1650 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1652 pkc->kb = GNUNET_malloc (size);
1653 kbe = (char *) &pkc->kb[1];
1654 memcpy (kbe, uris, pkc->slen);
1656 sptr = &kbe[pkc->slen];
1658 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1661 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1662 if (pkc->mdsize == -1)
1665 GNUNET_free (pkc->kb);
1666 if (pkc->dsh != NULL)
1668 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1671 cont (cont_cls, NULL, _("Internal error."));
1675 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1677 pkc->cpy = GNUNET_malloc (size);
1678 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1679 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1682 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1683 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1684 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1686 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1690 /* end of fs_publish.c */