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 http://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->anonymity;
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 (pcc->sc->h->sched,
160 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
164 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
165 pcc->sc->in_network_wait = GNUNET_NO;
166 if (GNUNET_OK != success)
168 GNUNET_asprintf (&pcc->p->emsg,
169 _("Publishing failed: %s"),
171 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
172 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
173 pi.value.publish.specifics.error.message = pcc->p->emsg;
174 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
175 if ( (pcc->p->is_directory == GNUNET_NO) &&
176 (pcc->p->filename != NULL) &&
177 (pcc->p->data.file.do_index == GNUNET_YES) )
179 /* run unindex to clean up */
180 GNUNET_FS_unindex_start (pcc->sc->h,
185 if (NULL != pcc->cont)
187 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
188 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
196 * Generate the callback that signals clients
197 * that a file (or directory) has been completely
200 * @param p the completed upload
201 * @param sc context of the publication
204 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
205 struct GNUNET_FS_PublishContext *sc)
207 struct GNUNET_FS_ProgressInfo pi;
209 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
210 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
211 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
212 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
213 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
218 * Generate the callback that signals clients
219 * that a file (or directory) has encountered
220 * a problem during publication.
222 * @param p the upload that had trouble
223 * @param sc context of the publication
224 * @param emsg error message
227 signal_publish_error (struct GNUNET_FS_FileInformation *p,
228 struct GNUNET_FS_PublishContext *sc,
231 struct GNUNET_FS_ProgressInfo pi;
233 p->emsg = GNUNET_strdup (emsg);
234 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
235 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
236 pi.value.publish.specifics.error.message =emsg;
237 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
238 if ( (p->is_directory == GNUNET_NO) &&
239 (p->filename != NULL) &&
240 (p->data.file.do_index == GNUNET_YES) )
242 /* run unindex to clean up */
243 GNUNET_FS_unindex_start (sc->h,
252 * Datastore returns from reservation cancel request.
254 * @param cls the 'struct GNUNET_FS_PublishContext'
255 * @param success success code (not used)
256 * @param msg error message (typically NULL, not used)
259 finish_release_reserve (void *cls,
263 struct GNUNET_FS_PublishContext *pc = cls;
266 signal_publish_completion (pc->fi, pc);
267 pc->all_done = GNUNET_YES;
268 GNUNET_FS_publish_sync_ (pc);
273 * We've finished publishing the SBlock as part of a larger upload.
274 * Check the result and complete the larger upload.
276 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
277 * @param uri URI of the published SBlock
278 * @param emsg NULL on success, otherwise error message
281 publish_sblocks_cont (void *cls,
282 const struct GNUNET_FS_Uri *uri,
285 struct GNUNET_FS_PublishContext *pc = cls;
288 signal_publish_error (pc->fi,
291 GNUNET_FS_publish_sync_ (pc);
294 GNUNET_assert (pc->qre == NULL);
295 if ( (pc->dsh != NULL) &&
298 pc->qre = GNUNET_DATASTORE_release_reserve (pc->dsh,
302 GNUNET_TIME_UNIT_FOREVER_REL,
303 &finish_release_reserve,
308 finish_release_reserve (pc, GNUNET_OK, NULL);
314 * We are almost done publishing the structure,
315 * add SBlocks (if needed).
317 * @param sc overall upload data
320 publish_sblock (struct GNUNET_FS_PublishContext *sc)
322 if (NULL != sc->namespace)
323 GNUNET_FS_publish_sks (sc->h,
329 sc->fi->expirationTime,
333 &publish_sblocks_cont,
336 publish_sblocks_cont (sc, NULL, NULL);
341 * We've finished publishing a KBlock as part of a larger upload.
342 * Check the result and continue the larger upload.
344 * @param cls the "struct GNUNET_FS_PublishContext*"
345 * of the larger upload
346 * @param uri URI of the published blocks
347 * @param emsg NULL on success, otherwise error message
350 publish_kblocks_cont (void *cls,
351 const struct GNUNET_FS_Uri *uri,
354 struct GNUNET_FS_PublishContext *pc = cls;
355 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Error uploading KSK blocks: %s\n",
364 signal_publish_error (p, pc, emsg);
365 GNUNET_FS_file_information_sync_ (p);
366 GNUNET_FS_publish_sync_ (pc);
368 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
369 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
370 &GNUNET_FS_publish_main_,
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "KSK blocks published, moving on to next file\n");
379 signal_publish_completion (p, pc);
380 /* move on to next file */
382 pc->fi_pos = p->next;
385 GNUNET_FS_publish_sync_ (pc);
387 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
388 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
389 &GNUNET_FS_publish_main_,
395 * Function called by the tree encoder to obtain
396 * a block of plaintext data (for the lowest level
399 * @param cls our publishing context
400 * @param offset identifies which block to get
401 * @param max (maximum) number of bytes to get; returning
402 * fewer will also cause errors
403 * @param buf where to copy the plaintext buffer
404 * @param emsg location to store an error message (on error)
405 * @return number of bytes copied to buf, 0 on error
408 block_reader (void *cls,
414 struct GNUNET_FS_PublishContext *sc = cls;
415 struct GNUNET_FS_FileInformation *p;
422 pt_size = GNUNET_MIN(max,
423 p->data.dir.dir_size - offset);
424 dd = p->data.dir.dir_data;
431 pt_size = GNUNET_MIN(max,
432 p->data.file.file_size - offset);
434 return 0; /* calling reader with pt_size==0
435 might free buf, so don't! */
437 p->data.file.reader (p->data.file.reader_cls,
449 * The tree encoder has finished processing a
450 * file. Call it's finish method and deal with
453 * @param cls our publishing context
454 * @param tc scheduler's task context (not used)
457 encode_cont (void *cls,
458 const struct GNUNET_SCHEDULER_TaskContext *tc)
460 struct GNUNET_FS_PublishContext *sc = cls;
461 struct GNUNET_FS_FileInformation *p;
462 struct GNUNET_FS_ProgressInfo pi;
466 GNUNET_FS_tree_encoder_finish (p->te,
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Error during tree walk: %s\n",
477 GNUNET_asprintf (&p->emsg,
478 _("Publishing failed: %s"),
481 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
482 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
483 pi.value.publish.specifics.error.message = p->emsg;
484 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Finished with tree encoder\n");
490 /* final progress event */
491 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
492 pi.value.publish.specifics.progress.data = NULL;
493 pi.value.publish.specifics.progress.offset = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
494 pi.value.publish.specifics.progress.data_len = 0;
495 pi.value.publish.specifics.progress.depth = 0;
496 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
497 GNUNET_FS_uri_chk_get_file_size (p->chk_uri));
499 /* continue with main */
501 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
502 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
503 &GNUNET_FS_publish_main_,
509 * Function called asking for the current (encoded)
510 * block to be processed. After processing the
511 * client should either call "GNUNET_FS_tree_encode_next"
512 * or (on error) "GNUNET_FS_tree_encode_finish".
515 * @param query the query for the block (key for lookup in the datastore)
516 * @param offset offset of the block in the file
517 * @param type type of the block (IBLOCK or DBLOCK)
518 * @param block the (encrypted) block
519 * @param block_size size of block (in bytes)
522 block_proc (void *cls,
523 const GNUNET_HashCode *query,
525 enum GNUNET_BLOCK_Type type,
529 struct GNUNET_FS_PublishContext *sc = cls;
530 struct GNUNET_FS_FileInformation *p;
531 struct PutContCtx * dpc_cls;
532 struct OnDemandBlock odb;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Waiting for datastore connection\n");
542 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
543 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
544 &GNUNET_FS_publish_main_,
549 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
550 sc->in_network_wait = GNUNET_YES;
551 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
552 dpc_cls->cont = &GNUNET_FS_publish_main_;
553 dpc_cls->cont_cls = sc;
556 if ( (! p->is_directory) &&
557 (GNUNET_YES == p->data.file.do_index) &&
558 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK) )
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Indexing block `%s' for offset %llu with index size %u\n",
564 (unsigned long long) offset,
565 sizeof (struct OnDemandBlock));
567 odb.offset = GNUNET_htonll (offset);
568 odb.file_id = p->data.file.file_id;
569 GNUNET_DATASTORE_put (sc->dsh,
570 (p->is_directory) ? 0 : sc->rid,
572 sizeof (struct OnDemandBlock),
574 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
579 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Publishing block `%s' for offset %llu with size %u\n",
588 (unsigned long long) offset,
589 (unsigned int) block_size);
591 GNUNET_DATASTORE_put (sc->dsh,
592 (p->is_directory) ? 0 : sc->rid,
601 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
608 * Function called with information about our
609 * progress in computing the tree encoding.
612 * @param offset where are we in the file
613 * @param pt_block plaintext of the currently processed block
614 * @param pt_size size of pt_block
615 * @param depth depth of the block in the tree
618 progress_proc (void *cls,
620 const void *pt_block,
624 struct GNUNET_FS_PublishContext *sc = cls;
625 struct GNUNET_FS_FileInformation *p;
626 struct GNUNET_FS_ProgressInfo pi;
629 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
630 pi.value.publish.specifics.progress.data = pt_block;
631 pi.value.publish.specifics.progress.offset = offset;
632 pi.value.publish.specifics.progress.data_len = pt_size;
633 pi.value.publish.specifics.progress.depth = depth;
634 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
639 * We are uploading a file or directory; load (if necessary) the next
640 * block into memory, encrypt it and send it to the FS service. Then
641 * continue with the main task.
643 * @param sc overall upload data
646 publish_content (struct GNUNET_FS_PublishContext *sc)
648 struct GNUNET_FS_FileInformation *p;
650 struct GNUNET_FS_DirectoryBuilder *db;
651 struct GNUNET_FS_FileInformation *dirpos;
656 GNUNET_assert (p != NULL);
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Creating directory\n");
665 db = GNUNET_FS_directory_builder_create (p->meta);
666 dirpos = p->data.dir.entries;
667 while (NULL != dirpos)
669 if (dirpos->is_directory)
671 raw_data = dirpos->data.dir.dir_data;
672 dirpos->data.dir.dir_data = NULL;
677 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
678 (dirpos->data.file.file_size > 0) )
680 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
682 if (dirpos->data.file.file_size !=
683 dirpos->data.file.reader (dirpos->data.file.reader_cls,
685 dirpos->data.file.file_size,
689 GNUNET_free_non_null (emsg);
690 GNUNET_free (raw_data);
695 GNUNET_FS_directory_builder_add (db,
699 GNUNET_free_non_null (raw_data);
700 dirpos = dirpos->next;
702 GNUNET_FS_directory_builder_finish (db,
703 &p->data.dir.dir_size,
704 &p->data.dir.dir_data);
705 GNUNET_FS_file_information_sync_ (p);
707 size = (p->is_directory)
708 ? p->data.dir.dir_size
709 : p->data.file.file_size;
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Creating tree encoder\n");
714 p->te = GNUNET_FS_tree_encoder_create (sc->h,
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Processing next block from tree\n");
727 GNUNET_FS_tree_encoder_next (p->te);
732 * Process the response (or lack thereof) from
733 * the "fs" service to our 'start index' request.
735 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
736 * @param msg the response we got
739 process_index_start_response (void *cls,
740 const struct GNUNET_MessageHeader *msg)
742 struct GNUNET_FS_PublishContext *sc = cls;
743 struct GNUNET_FS_FileInformation *p;
747 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("Can not index file `%s': %s. Will try to insert instead.\n"),
755 _("timeout on index-start request to `fs' service"));
756 p->data.file.do_index = GNUNET_NO;
757 GNUNET_FS_file_information_sync_ (p);
758 publish_content (sc);
761 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
763 msize = ntohs (msg->size);
764 emsg = (const char *) &msg[1];
765 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
766 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
767 emsg = gettext_noop ("unknown error");
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 _("Can not index file `%s': %s. Will try to insert instead.\n"),
772 p->data.file.do_index = GNUNET_NO;
773 GNUNET_FS_file_information_sync_ (p);
774 publish_content (sc);
777 p->data.file.index_start_confirmed = GNUNET_YES;
778 /* success! continue with indexing */
779 GNUNET_FS_file_information_sync_ (p);
780 publish_content (sc);
785 * Function called once the hash computation over an
786 * indexed file has completed.
788 * @param cls closure, our publishing context
789 * @param res resulting hash, NULL on error
792 hash_for_index_cb (void *cls,
793 const GNUNET_HashCode *
796 struct GNUNET_FS_PublishContext *sc = cls;
797 struct GNUNET_FS_FileInformation *p;
798 struct IndexStartMessage *ism;
800 struct GNUNET_CLIENT_Connection *client;
809 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
810 _("Can not index file `%s': %s. Will try to insert instead.\n"),
812 _("failed to compute hash"));
813 p->data.file.do_index = GNUNET_NO;
814 GNUNET_FS_file_information_sync_ (p);
815 publish_content (sc);
818 if (GNUNET_YES == p->data.file.index_start_confirmed)
820 publish_content (sc);
823 fn = GNUNET_STRINGS_filename_expand (p->filename);
824 GNUNET_assert (fn != NULL);
825 slen = strlen (fn) + 1;
826 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
829 _("Can not index file `%s': %s. Will try to insert instead.\n"),
831 _("filename too long"));
833 p->data.file.do_index = GNUNET_NO;
834 GNUNET_FS_file_information_sync_ (p);
835 publish_content (sc);
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Hash of indexed file `%s' is `%s'\n",
844 client = GNUNET_CLIENT_connect (sc->h->sched,
849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
850 _("Can not index file `%s': %s. Will try to insert instead.\n"),
852 _("could not connect to `fs' service"));
853 p->data.file.do_index = GNUNET_NO;
854 publish_content (sc);
858 if (p->data.file.have_hash != GNUNET_YES)
860 p->data.file.file_id = *res;
861 p->data.file.have_hash = GNUNET_YES;
862 GNUNET_FS_file_information_sync_ (p);
864 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
866 ism->header.size = htons(sizeof(struct IndexStartMessage) +
868 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
870 GNUNET_DISK_file_get_identifiers (p->filename,
874 ism->device = GNUNET_htonll (dev);
875 ism->inode = GNUNET_htonll(ino);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 _("Failed to get file identifiers for `%s'\n"),
891 GNUNET_break (GNUNET_YES ==
892 GNUNET_CLIENT_transmit_and_get_response (client,
894 GNUNET_TIME_UNIT_FOREVER_REL,
896 &process_index_start_response,
903 * Main function that performs the upload.
905 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
906 * @param tc task context
909 GNUNET_FS_publish_main_ (void *cls,
910 const struct GNUNET_SCHEDULER_TaskContext *tc)
912 struct GNUNET_FS_PublishContext *pc = cls;
913 struct GNUNET_FS_ProgressInfo pi;
914 struct GNUNET_FS_FileInformation *p;
915 struct GNUNET_FS_Uri *loc;
918 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Publishing complete, now publishing SKS and KSK blocks.\n");
926 /* upload of entire hierarchy complete,
927 publish namespace entries */
928 GNUNET_FS_publish_sync_ (pc);
932 /* find starting position */
933 while ( (p->is_directory) &&
934 (NULL != p->data.dir.entries) &&
936 (NULL == p->data.dir.entries->chk_uri) )
938 p = p->data.dir.entries;
940 GNUNET_FS_publish_sync_ (pc);
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Error uploading: %s\n",
950 /* error with current file, abort all
951 related files as well! */
952 while (NULL != p->dir)
954 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
955 EXTRACTOR_METATYPE_FILENAME);
959 GNUNET_asprintf (&p->emsg,
960 _("Recursive upload failed at `%s': %s"),
967 GNUNET_asprintf (&p->emsg,
968 _("Recursive upload failed: %s"),
971 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
972 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
973 pi.value.publish.specifics.error.message = p->emsg;
974 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
976 pc->all_done = GNUNET_YES;
977 GNUNET_FS_publish_sync_ (pc);
980 /* handle completion */
981 if (NULL != p->chk_uri)
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "File upload complete, now publishing KSK blocks.\n");
987 if (0 == p->anonymity)
989 /* zero anonymity, box CHK URI in LOC URI */
990 loc = GNUNET_FS_uri_loc_create (p->chk_uri,
993 GNUNET_FS_uri_destroy (p->chk_uri);
996 GNUNET_FS_publish_sync_ (pc);
997 /* upload of "p" complete, publish KBlocks! */
998 if (p->keywords != NULL)
1000 GNUNET_FS_publish_ksk (pc->h,
1008 &publish_kblocks_cont,
1013 publish_kblocks_cont (pc,
1019 if ( (!p->is_directory) &&
1020 (p->data.file.do_index) )
1022 if (NULL == p->filename)
1024 p->data.file.do_index = GNUNET_NO;
1025 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1026 _("Can not index file `%s': %s. Will try to insert instead.\n"),
1028 _("needs to be an actual file"));
1029 GNUNET_FS_file_information_sync_ (p);
1030 publish_content (pc);
1033 if (p->data.file.have_hash)
1035 hash_for_index_cb (pc,
1036 &p->data.file.file_id);
1040 p->start_time = GNUNET_TIME_absolute_get ();
1041 pc->fhc = GNUNET_CRYPTO_hash_file (pc->h->sched,
1042 GNUNET_SCHEDULER_PRIORITY_IDLE,
1050 publish_content (pc);
1055 * Signal the FS's progress function that we are starting
1058 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1059 * @param fi the entry in the publish-structure
1060 * @param length length of the file or directory
1061 * @param meta metadata for the file or directory (can be modified)
1062 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1063 * @param anonymity pointer to selected anonymity level (can be modified)
1064 * @param priority pointer to selected priority (can be modified)
1065 * @param do_index should we index?
1066 * @param expirationTime pointer to selected expiration time (can be modified)
1067 * @param client_info pointer to client context set upon creation (can be modified)
1068 * @return GNUNET_OK to continue (always)
1071 fip_signal_start(void *cls,
1072 struct GNUNET_FS_FileInformation *fi,
1074 struct GNUNET_CONTAINER_MetaData *meta,
1075 struct GNUNET_FS_Uri **uri,
1076 uint32_t *anonymity,
1079 struct GNUNET_TIME_Absolute *expirationTime,
1082 struct GNUNET_FS_PublishContext *sc = cls;
1083 struct GNUNET_FS_ProgressInfo pi;
1089 /* space for on-demand blocks */
1090 sc->reserve_space += ((length + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1094 /* space for DBlocks */
1095 sc->reserve_space += length;
1097 /* entries for IBlocks and DBlocks, space for IBlocks */
1101 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1102 sc->reserve_entries += left;
1105 left = left * sizeof (struct ContentHashKey);
1106 sc->reserve_space += left;
1108 sc->reserve_entries++;
1109 /* entries and space for keywords */
1112 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1113 sc->reserve_entries += kc;
1114 sc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1116 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1117 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1118 GNUNET_FS_file_information_sync_ (fi);
1124 * Signal the FS's progress function that we are suspending
1127 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1128 * @param fi the entry in the publish-structure
1129 * @param length length of the file or directory
1130 * @param meta metadata for the file or directory (can be modified)
1131 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1132 * @param anonymity pointer to selected anonymity level (can be modified)
1133 * @param priority pointer to selected priority (can be modified)
1134 * @param do_index should we index?
1135 * @param expirationTime pointer to selected expiration time (can be modified)
1136 * @param client_info pointer to client context set upon creation (can be modified)
1137 * @return GNUNET_OK to continue (always)
1140 fip_signal_suspend(void *cls,
1141 struct GNUNET_FS_FileInformation *fi,
1143 struct GNUNET_CONTAINER_MetaData *meta,
1144 struct GNUNET_FS_Uri **uri,
1145 uint32_t *anonymity,
1148 struct GNUNET_TIME_Absolute *expirationTime,
1151 struct GNUNET_FS_PublishContext*sc = cls;
1152 struct GNUNET_FS_ProgressInfo pi;
1155 GNUNET_free_non_null (fi->serialization);
1156 fi->serialization = NULL;
1157 off = (fi->chk_uri == NULL) ? 0 : length;
1158 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1159 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1160 *client_info = NULL;
1161 if (NULL != sc->dsh)
1163 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1171 * Create SUSPEND event for the given publish operation
1172 * and then clean up our state (without stop signal).
1174 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1177 GNUNET_FS_publish_signal_suspend_ (void *cls)
1179 struct GNUNET_FS_PublishContext *pc = cls;
1181 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1183 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1184 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1186 GNUNET_FS_file_information_inspect (pc->fi,
1187 &fip_signal_suspend,
1189 GNUNET_FS_end_top (pc->h, pc->top);
1190 publish_cleanup (pc, NULL);
1195 * We have gotten a reply for our space reservation request.
1196 * Either fail (insufficient space) or start publishing for good.
1198 * @param cls the 'struct GNUNET_FS_PublishContext*'
1199 * @param success positive reservation ID on success
1200 * @param msg error message on error, otherwise NULL
1203 finish_reserve (void *cls,
1207 struct GNUNET_FS_PublishContext *pc = cls;
1210 if ( (msg != NULL) ||
1213 GNUNET_asprintf (&pc->fi->emsg,
1214 _("Insufficient space for publishing: %s"),
1216 signal_publish_error (pc->fi,
1223 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
1224 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1225 &GNUNET_FS_publish_main_,
1231 * Publish a file or directory.
1233 * @param h handle to the file sharing subsystem
1234 * @param fi information about the file or directory structure to publish
1235 * @param namespace namespace to publish the file in, NULL for no namespace
1236 * @param nid identifier to use for the publishd content in the namespace
1237 * (can be NULL, must be NULL if namespace is NULL)
1238 * @param nuid update-identifier that will be used for future updates
1239 * (can be NULL, must be NULL if namespace or nid is NULL)
1240 * @param options options for the publication
1241 * @return context that can be used to control the publish operation
1243 struct GNUNET_FS_PublishContext *
1244 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1245 struct GNUNET_FS_FileInformation *fi,
1246 struct GNUNET_FS_Namespace *namespace,
1249 enum GNUNET_FS_PublishOptions options)
1251 struct GNUNET_FS_PublishContext *ret;
1252 struct GNUNET_DATASTORE_Handle *dsh;
1254 GNUNET_assert (NULL != h);
1255 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1257 dsh = GNUNET_DATASTORE_connect (h->cfg,
1266 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1270 ret->namespace = namespace;
1271 if (namespace != NULL)
1274 GNUNET_assert (NULL != nid);
1275 ret->nid = GNUNET_strdup (nid);
1277 ret->nuid = GNUNET_strdup (nuid);
1280 GNUNET_FS_file_information_inspect (ret->fi,
1283 ret->fi_pos = ret->fi;
1284 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1285 GNUNET_FS_publish_sync_ (ret);
1286 if (NULL != ret->dsh)
1288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1289 _("Reserving space for %u entries and %llu bytes for publication\n"),
1290 (unsigned int) ret->reserve_entries,
1291 (unsigned long long) ret->reserve_space);
1292 ret->qre = GNUNET_DATASTORE_reserve (ret->dsh,
1294 ret->reserve_entries,
1297 GNUNET_TIME_UNIT_FOREVER_REL,
1304 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1305 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1306 &GNUNET_FS_publish_main_,
1314 * Signal the FS's progress function that we are stopping
1317 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1318 * @param fi the entry in the publish-structure
1319 * @param length length of the file or directory
1320 * @param meta metadata for the file or directory (can be modified)
1321 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1322 * @param anonymity pointer to selected anonymity level (can be modified)
1323 * @param priority pointer to selected priority (can be modified)
1324 * @param do_index should we index?
1325 * @param expirationTime pointer to selected expiration time (can be modified)
1326 * @param client_info pointer to client context set upon creation (can be modified)
1327 * @return GNUNET_OK to continue (always)
1330 fip_signal_stop(void *cls,
1331 struct GNUNET_FS_FileInformation *fi,
1333 struct GNUNET_CONTAINER_MetaData *meta,
1334 struct GNUNET_FS_Uri **uri,
1335 uint32_t *anonymity,
1338 struct GNUNET_TIME_Absolute *expirationTime,
1341 struct GNUNET_FS_PublishContext*sc = cls;
1342 struct GNUNET_FS_ProgressInfo pi;
1345 if (fi->serialization != NULL)
1347 GNUNET_FS_remove_sync_file_ (sc->h,
1348 GNUNET_FS_SYNC_PATH_FILE_INFO,
1350 GNUNET_free (fi->serialization);
1351 fi->serialization = NULL;
1353 off = (fi->chk_uri == NULL) ? 0 : length;
1354 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1355 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1356 *client_info = NULL;
1362 * Stop an upload. Will abort incomplete uploads (but
1363 * not remove blocks that have already been publishd) or
1364 * simply clean up the state for completed uploads.
1365 * Must NOT be called from within the event callback!
1367 * @param pc context for the upload to stop
1370 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1372 GNUNET_FS_end_top (pc->h, pc->top);
1373 if (NULL != pc->qre)
1375 GNUNET_DATASTORE_cancel (pc->qre);
1378 if (NULL != pc->dsh)
1380 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1383 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1385 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1386 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1388 if (pc->serialization != NULL)
1390 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1391 GNUNET_free (pc->serialization);
1392 pc->serialization = NULL;
1394 GNUNET_FS_file_information_inspect (pc->fi,
1397 if (GNUNET_YES == pc->in_network_wait)
1399 pc->in_network_wait = GNUNET_SYSERR;
1402 publish_cleanup (pc, NULL);
1407 * Context for the KSK publication.
1409 struct PublishKskContext
1415 struct GNUNET_FS_Uri *ksk_uri;
1418 * Global FS context.
1420 struct GNUNET_FS_Handle *h;
1423 * The master block that we are sending
1424 * (in plaintext), has "mdsize+slen" more
1425 * bytes than the struct would suggest.
1430 * Buffer of the same size as "kb" for
1431 * the encrypted version.
1436 * Handle to the datastore, NULL if we are just
1439 struct GNUNET_DATASTORE_Handle *dsh;
1442 * Function to call once we're done.
1444 GNUNET_FS_PublishContinuation cont;
1452 * When should the KBlocks expire?
1454 struct GNUNET_TIME_Absolute expirationTime;
1457 * Size of the serialized metadata.
1462 * Size of the (CHK) URI as a string.
1467 * Keyword that we are currently processing.
1472 * Anonymity level for the KBlocks.
1477 * Priority for the KBlocks.
1484 * Continuation of "GNUNET_FS_publish_ksk" that performs
1485 * the actual publishing operation (iterating over all
1488 * @param cls closure of type "struct PublishKskContext*"
1492 publish_ksk_cont (void *cls,
1493 const struct GNUNET_SCHEDULER_TaskContext *tc);
1497 * Function called by the datastore API with
1498 * the result from the PUT request.
1500 * @param cls closure of type "struct PublishKskContext*"
1501 * @param success GNUNET_OK on success
1502 * @param msg error message (or NULL)
1505 kb_put_cont (void *cls,
1509 struct PublishKskContext *pkc = cls;
1511 if (GNUNET_OK != success)
1513 if (NULL != pkc->dsh)
1515 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1518 GNUNET_free (pkc->cpy);
1519 GNUNET_free (pkc->kb);
1520 pkc->cont (pkc->cont_cls,
1523 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1527 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1530 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1535 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1536 * publishing operation (iterating over all of the keywords).
1538 * @param cls closure of type "struct PublishKskContext*"
1542 publish_ksk_cont (void *cls,
1543 const struct GNUNET_SCHEDULER_TaskContext *tc)
1545 struct PublishKskContext *pkc = cls;
1546 const char *keyword;
1547 GNUNET_HashCode key;
1548 GNUNET_HashCode query;
1549 struct GNUNET_CRYPTO_AesSessionKey skey;
1550 struct GNUNET_CRYPTO_AesInitializationVector iv;
1551 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1554 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1555 (NULL == pkc->dsh) )
1557 if (NULL != pkc->dsh)
1559 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1562 GNUNET_free (pkc->cpy);
1563 GNUNET_free (pkc->kb);
1564 pkc->cont (pkc->cont_cls,
1567 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1571 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1572 /* first character of keyword indicates if it is
1573 mandatory or not -- ignore for hashing */
1574 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1575 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1576 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1577 pkc->slen + pkc->mdsize,
1581 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1582 GNUNET_assert (NULL != pk);
1583 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1584 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1585 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1587 GNUNET_assert (GNUNET_OK ==
1588 GNUNET_CRYPTO_rsa_sign (pk,
1590 &pkc->cpy->signature));
1591 GNUNET_CRYPTO_rsa_key_free (pk);
1592 GNUNET_DATASTORE_put (pkc->dsh,
1596 sizeof (struct KBlock) +
1599 GNUNET_BLOCK_TYPE_FS_KBLOCK,
1602 pkc->expirationTime,
1604 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1611 * Publish a CHK under various keywords on GNUnet.
1613 * @param h handle to the file sharing subsystem
1614 * @param ksk_uri keywords to use
1615 * @param meta metadata to use
1616 * @param uri URI to refer to in the KBlock
1617 * @param expirationTime when the KBlock expires
1618 * @param anonymity anonymity level for the KBlock
1619 * @param priority priority for the KBlock
1620 * @param options publication options
1621 * @param cont continuation
1622 * @param cont_cls closure for cont
1625 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1626 const struct GNUNET_FS_Uri *ksk_uri,
1627 const struct GNUNET_CONTAINER_MetaData *meta,
1628 const struct GNUNET_FS_Uri *uri,
1629 struct GNUNET_TIME_Absolute expirationTime,
1632 enum GNUNET_FS_PublishOptions options,
1633 GNUNET_FS_PublishContinuation cont,
1636 struct PublishKskContext *pkc;
1642 GNUNET_assert (NULL != uri);
1643 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1645 pkc->expirationTime = expirationTime;
1646 pkc->anonymity = anonymity;
1647 pkc->priority = priority;
1649 pkc->cont_cls = cont_cls;
1650 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1652 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1654 if (pkc->dsh == NULL)
1656 cont (cont_cls, NULL, _("Could not connect to datastore."));
1664 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1665 GNUNET_assert (pkc->mdsize >= 0);
1666 uris = GNUNET_FS_uri_to_string (uri);
1667 pkc->slen = strlen (uris) + 1;
1668 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1669 if (size > MAX_KBLOCK_SIZE)
1671 size = MAX_KBLOCK_SIZE;
1672 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1674 pkc->kb = GNUNET_malloc (size);
1675 kbe = (char *) &pkc->kb[1];
1676 memcpy (kbe, uris, pkc->slen);
1678 sptr = &kbe[pkc->slen];
1680 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1683 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1684 if (pkc->mdsize == -1)
1687 GNUNET_free (pkc->kb);
1688 if (pkc->dsh != NULL)
1690 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1693 cont (cont_cls, NULL, _("Internal error."));
1697 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1699 pkc->cpy = GNUNET_malloc (size);
1700 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1701 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1704 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1705 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1706 GNUNET_SCHEDULER_add_continuation (h->sched,
1709 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1713 /* end of fs_publish.c */