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 2, 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
29 * - indexing cleanup: unindex on failure (can wait)
30 * - datastore reservation support (optimization)
31 * - location URIs (publish with anonymity-level zero)
35 #include "gnunet_constants.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_fs_service.h"
42 #define DEBUG_PUBLISH GNUNET_NO
46 * Context for "ds_put_cont".
51 * Current publishing context.
53 struct GNUNET_FS_PublishContext *sc;
56 * Specific file with the block.
58 struct GNUNET_FS_FileInformation *p;
61 * Function to run next, if any (can be NULL).
63 GNUNET_SCHEDULER_Task cont;
73 * Fill in all of the generic fields for
74 * a publish event and call the callback.
76 * @param pi structure to fill in
77 * @param sc overall publishing context
78 * @param p file information for the file being published
79 * @param offset where in the file are we so far
80 * @return value returned from callback
83 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
84 struct GNUNET_FS_PublishContext *sc,
85 const struct GNUNET_FS_FileInformation *p,
88 pi->value.publish.pc = sc;
89 pi->value.publish.fi = p;
90 pi->value.publish.cctx
92 pi->value.publish.pctx
93 = (NULL == p->dir) ? NULL : p->dir->client_info;
94 pi->value.publish.filename = p->filename;
95 pi->value.publish.size
96 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
98 = GNUNET_TIME_calculate_eta (p->start_time,
100 pi->value.publish.size);
101 pi->value.publish.completed = offset;
102 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
103 pi->value.publish.anonymity = p->anonymity;
104 return sc->h->upcb (sc->h->upcb_cls,
110 * Cleanup the publish context, we're done with it.
112 * @param cls struct to clean up after
113 * @param tc scheduler context
116 publish_cleanup (void *cls,
117 const struct GNUNET_SCHEDULER_TaskContext *tc)
119 struct GNUNET_FS_PublishContext *pc = cls;
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 _("Upload 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);
176 if (NULL != pcc->cont)
178 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
179 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
187 * Generate the callback that signals clients
188 * that a file (or directory) has been completely
191 * @param p the completed upload
192 * @param sc context of the publication
195 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
196 struct GNUNET_FS_PublishContext *sc)
198 struct GNUNET_FS_ProgressInfo pi;
200 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
201 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
202 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
203 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
204 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
209 * Generate the callback that signals clients
210 * that a file (or directory) has encountered
211 * a problem during publication.
213 * @param p the upload that had trouble
214 * @param sc context of the publication
215 * @param emsg error message
218 signal_publish_error (struct GNUNET_FS_FileInformation *p,
219 struct GNUNET_FS_PublishContext *sc,
222 struct GNUNET_FS_ProgressInfo pi;
224 p->emsg = GNUNET_strdup (emsg);
225 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
226 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
227 pi.value.publish.specifics.error.message =emsg;
228 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
233 * We've finished publishing the SBlock as part of a larger upload.
234 * Check the result and complete the larger upload.
236 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
237 * @param uri URI of the published SBlock
238 * @param emsg NULL on success, otherwise error message
241 publish_sblocks_cont (void *cls,
242 const struct GNUNET_FS_Uri *uri,
245 struct GNUNET_FS_PublishContext *pc = cls;
248 signal_publish_error (pc->fi,
251 GNUNET_FS_publish_sync_ (pc);
254 // FIXME: release the datastore reserve here!
255 signal_publish_completion (pc->fi, pc);
256 pc->all_done = GNUNET_YES;
257 GNUNET_FS_publish_sync_ (pc);
262 * We are almost done publishing the structure,
263 * add SBlocks (if needed).
265 * @param sc overall upload data
268 publish_sblock (struct GNUNET_FS_PublishContext *sc)
270 if (NULL != sc->namespace)
271 GNUNET_FS_publish_sks (sc->h,
277 sc->fi->expirationTime,
281 &publish_sblocks_cont,
284 publish_sblocks_cont (sc, NULL, NULL);
289 * We've finished publishing a KBlock as part of a larger upload.
290 * Check the result and continue the larger upload.
292 * @param cls the "struct GNUNET_FS_PublishContext*"
293 * of the larger upload
294 * @param uri URI of the published blocks
295 * @param emsg NULL on success, otherwise error message
298 publish_kblocks_cont (void *cls,
299 const struct GNUNET_FS_Uri *uri,
302 struct GNUNET_FS_PublishContext *pc = cls;
303 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Error uploading KSK blocks: %s\n",
312 signal_publish_error (p, pc, emsg);
313 GNUNET_FS_file_information_sync_ (p);
314 GNUNET_FS_publish_sync_ (pc);
316 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
317 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
318 &GNUNET_FS_publish_main_,
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "KSK blocks published, moving on to next file\n");
327 signal_publish_completion (p, pc);
328 /* move on to next file */
330 pc->fi_pos = p->next;
333 GNUNET_FS_publish_sync_ (pc);
335 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
336 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
337 &GNUNET_FS_publish_main_,
343 * Function called by the tree encoder to obtain
344 * a block of plaintext data (for the lowest level
347 * @param cls our publishing context
348 * @param offset identifies which block to get
349 * @param max (maximum) number of bytes to get; returning
350 * fewer will also cause errors
351 * @param buf where to copy the plaintext buffer
352 * @param emsg location to store an error message (on error)
353 * @return number of bytes copied to buf, 0 on error
356 block_reader (void *cls,
362 struct GNUNET_FS_PublishContext *sc = cls;
363 struct GNUNET_FS_FileInformation *p;
370 pt_size = GNUNET_MIN(max,
371 p->data.dir.dir_size - offset);
372 dd = p->data.dir.dir_data;
379 pt_size = GNUNET_MIN(max,
380 p->data.file.file_size - offset);
382 return 0; /* calling reader with pt_size==0
383 might free buf, so don't! */
385 p->data.file.reader (p->data.file.reader_cls,
397 * The tree encoder has finished processing a
398 * file. Call it's finish method and deal with
401 * @param cls our publishing context
402 * @param tc scheduler's task context (not used)
405 encode_cont (void *cls,
406 const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct GNUNET_FS_PublishContext *sc = cls;
409 struct GNUNET_FS_FileInformation *p;
410 struct GNUNET_FS_ProgressInfo pi;
414 GNUNET_FS_tree_encoder_finish (p->te,
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Error during tree walk: %s\n",
425 GNUNET_asprintf (&p->emsg,
426 _("Upload failed: %s"),
429 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
430 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
431 pi.value.publish.specifics.error.message = p->emsg;
432 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Finished with tree encoder\n");
438 /* continue with main */
440 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
441 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
442 &GNUNET_FS_publish_main_,
448 * Function called asking for the current (encoded)
449 * block to be processed. After processing the
450 * client should either call "GNUNET_FS_tree_encode_next"
451 * or (on error) "GNUNET_FS_tree_encode_finish".
454 * @param query the query for the block (key for lookup in the datastore)
455 * @param offset offset of the block in the file
456 * @param type type of the block (IBLOCK or DBLOCK)
457 * @param block the (encrypted) block
458 * @param block_size size of block (in bytes)
461 block_proc (void *cls,
462 const GNUNET_HashCode *query,
464 enum GNUNET_BLOCK_Type type,
468 struct GNUNET_FS_PublishContext *sc = cls;
469 struct GNUNET_FS_FileInformation *p;
470 struct PutContCtx * dpc_cls;
471 struct OnDemandBlock odb;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Waiting for datastore connection\n");
481 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
482 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
483 &GNUNET_FS_publish_main_,
488 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
489 sc->in_network_wait = GNUNET_YES;
490 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
491 dpc_cls->cont = &GNUNET_FS_publish_main_;
492 dpc_cls->cont_cls = sc;
495 if ( (! p->is_directory) &&
496 (GNUNET_YES == p->data.file.do_index) &&
497 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Indexing block `%s' for offset %llu with index size %u\n",
503 (unsigned long long) offset,
504 sizeof (struct OnDemandBlock));
506 odb.offset = GNUNET_htonll (offset);
507 odb.file_id = p->data.file.file_id;
508 GNUNET_DATASTORE_put (sc->dsh,
511 sizeof(struct OnDemandBlock),
513 GNUNET_BLOCK_TYPE_ONDEMAND,
518 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Publishing block `%s' for offset %llu with size %u\n",
527 (unsigned long long) offset,
528 (unsigned int) block_size);
530 GNUNET_DATASTORE_put (sc->dsh,
540 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
547 * Function called with information about our
548 * progress in computing the tree encoding.
551 * @param offset where are we in the file
552 * @param pt_block plaintext of the currently processed block
553 * @param pt_size size of pt_block
554 * @param depth depth of the block in the tree
557 progress_proc (void *cls,
559 const void *pt_block,
563 struct GNUNET_FS_PublishContext *sc = cls;
564 struct GNUNET_FS_FileInformation *p;
565 struct GNUNET_FS_ProgressInfo pi;
568 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
569 pi.value.publish.specifics.progress.data = pt_block;
570 pi.value.publish.specifics.progress.offset = offset;
571 pi.value.publish.specifics.progress.data_len = pt_size;
572 pi.value.publish.specifics.progress.depth = depth;
573 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
578 * We are uploading a file or directory; load (if necessary) the next
579 * block into memory, encrypt it and send it to the FS service. Then
580 * continue with the main task.
582 * @param sc overall upload data
585 publish_content (struct GNUNET_FS_PublishContext *sc)
587 struct GNUNET_FS_FileInformation *p;
589 struct GNUNET_FS_DirectoryBuilder *db;
590 struct GNUNET_FS_FileInformation *dirpos;
595 GNUNET_assert (p != NULL);
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Creating directory\n");
604 db = GNUNET_FS_directory_builder_create (p->meta);
605 dirpos = p->data.dir.entries;
606 while (NULL != dirpos)
608 if (dirpos->is_directory)
610 raw_data = dirpos->data.dir.dir_data;
611 dirpos->data.dir.dir_data = NULL;
616 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
617 (dirpos->data.file.file_size > 0) )
619 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
621 if (dirpos->data.file.file_size !=
622 dirpos->data.file.reader (dirpos->data.file.reader_cls,
624 dirpos->data.file.file_size,
628 GNUNET_free_non_null (emsg);
629 GNUNET_free (raw_data);
634 GNUNET_FS_directory_builder_add (db,
638 GNUNET_free_non_null (raw_data);
639 dirpos = dirpos->next;
641 GNUNET_FS_directory_builder_finish (db,
642 &p->data.dir.dir_size,
643 &p->data.dir.dir_data);
644 GNUNET_FS_file_information_sync_ (p);
646 size = (p->is_directory)
647 ? p->data.dir.dir_size
648 : p->data.file.file_size;
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Creating tree encoder\n");
653 p->te = GNUNET_FS_tree_encoder_create (sc->h,
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Processing next block from tree\n");
666 GNUNET_FS_tree_encoder_next (p->te);
671 * Process the response (or lack thereof) from
672 * the "fs" service to our 'start index' request.
674 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
675 * @param msg the response we got
678 process_index_start_response (void *cls,
679 const struct GNUNET_MessageHeader *msg)
681 struct GNUNET_FS_PublishContext *sc = cls;
682 struct GNUNET_FS_FileInformation *p;
686 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
692 _("Can not index file `%s': %s. Will try to insert instead.\n"),
694 _("timeout on index-start request to `fs' service"));
695 p->data.file.do_index = GNUNET_NO;
696 GNUNET_FS_file_information_sync_ (p);
697 publish_content (sc);
700 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
702 msize = ntohs (msg->size);
703 emsg = (const char *) &msg[1];
704 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
705 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
706 emsg = gettext_noop ("unknown error");
707 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
708 _("Can not index file `%s': %s. Will try to insert instead.\n"),
711 p->data.file.do_index = GNUNET_NO;
712 GNUNET_FS_file_information_sync_ (p);
713 publish_content (sc);
716 p->data.file.index_start_confirmed = GNUNET_YES;
717 /* success! continue with indexing */
718 GNUNET_FS_file_information_sync_ (p);
719 publish_content (sc);
724 * Function called once the hash computation over an
725 * indexed file has completed.
727 * @param cls closure, our publishing context
728 * @param res resulting hash, NULL on error
731 hash_for_index_cb (void *cls,
732 const GNUNET_HashCode *
735 struct GNUNET_FS_PublishContext *sc = cls;
736 struct GNUNET_FS_FileInformation *p;
737 struct IndexStartMessage *ism;
739 struct GNUNET_CLIENT_Connection *client;
747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
748 _("Can not index file `%s': %s. Will try to insert instead.\n"),
750 _("failed to compute hash"));
751 p->data.file.do_index = GNUNET_NO;
752 GNUNET_FS_file_information_sync_ (p);
753 publish_content (sc);
756 if (GNUNET_YES == p->data.file.index_start_confirmed)
758 publish_content (sc);
761 fn = GNUNET_STRINGS_filename_expand (p->filename);
762 GNUNET_assert (fn != NULL);
763 slen = strlen (fn) + 1;
764 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
767 _("Can not index file `%s': %s. Will try to insert instead.\n"),
769 _("filename too long"));
771 p->data.file.do_index = GNUNET_NO;
772 GNUNET_FS_file_information_sync_ (p);
773 publish_content (sc);
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Hash of indexed file `%s' is `%s'\n",
782 client = GNUNET_CLIENT_connect (sc->h->sched,
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 _("Can not index file `%s': %s. Will try to insert instead.\n"),
790 _("could not connect to `fs' service"));
791 p->data.file.do_index = GNUNET_NO;
792 publish_content (sc);
796 if (p->data.file.have_hash != GNUNET_YES)
798 p->data.file.file_id = *res;
799 p->data.file.have_hash = GNUNET_YES;
800 GNUNET_FS_file_information_sync_ (p);
802 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
804 ism->header.size = htons(sizeof(struct IndexStartMessage) +
806 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
808 GNUNET_DISK_file_get_identifiers (p->filename,
812 ism->device = GNUNET_htonll (dev);
813 ism->inode = GNUNET_htonll(ino);
817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
818 _("Failed to get file identifiers for `%s'\n"),
827 GNUNET_break (GNUNET_YES ==
828 GNUNET_CLIENT_transmit_and_get_response (client,
830 GNUNET_TIME_UNIT_FOREVER_REL,
832 &process_index_start_response,
839 * Main function that performs the upload.
841 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
842 * @param tc task context
845 GNUNET_FS_publish_main_ (void *cls,
846 const struct GNUNET_SCHEDULER_TaskContext *tc)
848 struct GNUNET_FS_PublishContext *pc = cls;
849 struct GNUNET_FS_ProgressInfo pi;
850 struct GNUNET_FS_FileInformation *p;
853 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Upload complete, now publishing SKS and KSK blocks.\n");
861 /* upload of entire hierarchy complete,
862 publish namespace entries */
863 GNUNET_FS_publish_sync_ (pc);
867 /* find starting position */
868 while ( (p->is_directory) &&
869 (NULL != p->data.dir.entries) &&
871 (NULL == p->data.dir.entries->chk_uri) )
873 p = p->data.dir.entries;
875 GNUNET_FS_publish_sync_ (pc);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Error uploading: %s\n",
885 /* error with current file, abort all
886 related files as well! */
887 while (NULL != p->dir)
889 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
890 EXTRACTOR_METATYPE_FILENAME);
894 GNUNET_asprintf (&p->emsg,
895 _("Recursive upload failed at `%s': %s"),
902 GNUNET_asprintf (&p->emsg,
903 _("Recursive upload failed: %s"),
906 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
907 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
908 pi.value.publish.specifics.error.message = p->emsg;
909 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
911 pc->all_done = GNUNET_YES;
912 GNUNET_FS_publish_sync_ (pc);
915 /* handle completion */
916 if (NULL != p->chk_uri)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "File upload complete, now publishing KSK blocks.\n");
922 GNUNET_FS_publish_sync_ (pc);
923 /* upload of "p" complete, publish KBlocks! */
924 if (p->keywords != NULL)
926 GNUNET_FS_publish_ksk (pc->h,
934 &publish_kblocks_cont,
939 publish_kblocks_cont (pc,
945 if ( (!p->is_directory) &&
946 (p->data.file.do_index) )
948 if (NULL == p->filename)
950 p->data.file.do_index = GNUNET_NO;
951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
952 _("Can not index file `%s': %s. Will try to insert instead.\n"),
954 _("needs to be an actual file"));
955 GNUNET_FS_file_information_sync_ (p);
956 publish_content (pc);
959 if (p->data.file.have_hash)
961 hash_for_index_cb (pc,
962 &p->data.file.file_id);
966 p->start_time = GNUNET_TIME_absolute_get ();
967 GNUNET_CRYPTO_hash_file (pc->h->sched,
968 GNUNET_SCHEDULER_PRIORITY_IDLE,
976 publish_content (pc);
981 * Signal the FS's progress function that we are starting
984 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
985 * @param fi the entry in the publish-structure
986 * @param length length of the file or directory
987 * @param meta metadata for the file or directory (can be modified)
988 * @param uri pointer to the keywords that will be used for this entry (can be modified)
989 * @param anonymity pointer to selected anonymity level (can be modified)
990 * @param priority pointer to selected priority (can be modified)
991 * @param do_index should we index?
992 * @param expirationTime pointer to selected expiration time (can be modified)
993 * @param client_info pointer to client context set upon creation (can be modified)
994 * @return GNUNET_OK to continue (always)
997 fip_signal_start(void *cls,
998 struct GNUNET_FS_FileInformation *fi,
1000 struct GNUNET_CONTAINER_MetaData *meta,
1001 struct GNUNET_FS_Uri **uri,
1002 uint32_t *anonymity,
1005 struct GNUNET_TIME_Absolute *expirationTime,
1008 struct GNUNET_FS_PublishContext *sc = cls;
1009 struct GNUNET_FS_ProgressInfo pi;
1011 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1012 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1013 GNUNET_FS_file_information_sync_ (fi);
1019 * Signal the FS's progress function that we are suspending
1022 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1023 * @param fi the entry in the publish-structure
1024 * @param length length of the file or directory
1025 * @param meta metadata for the file or directory (can be modified)
1026 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1027 * @param anonymity pointer to selected anonymity level (can be modified)
1028 * @param priority pointer to selected priority (can be modified)
1029 * @param do_index should we index?
1030 * @param expirationTime pointer to selected expiration time (can be modified)
1031 * @param client_info pointer to client context set upon creation (can be modified)
1032 * @return GNUNET_OK to continue (always)
1035 fip_signal_suspend(void *cls,
1036 struct GNUNET_FS_FileInformation *fi,
1038 struct GNUNET_CONTAINER_MetaData *meta,
1039 struct GNUNET_FS_Uri **uri,
1040 uint32_t *anonymity,
1043 struct GNUNET_TIME_Absolute *expirationTime,
1046 struct GNUNET_FS_PublishContext*sc = cls;
1047 struct GNUNET_FS_ProgressInfo pi;
1050 GNUNET_free_non_null (fi->serialization);
1051 fi->serialization = NULL;
1052 off = (fi->chk_uri == NULL) ? 0 : length;
1053 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1054 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1055 *client_info = NULL;
1056 if (NULL != sc->dsh)
1058 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1066 * Create SUSPEND event for the given publish operation
1067 * and then clean up our state (without stop signal).
1069 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1072 GNUNET_FS_publish_signal_suspend_ (void *cls)
1074 struct GNUNET_FS_PublishContext *pc = cls;
1076 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1078 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1079 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1081 GNUNET_FS_file_information_inspect (pc->fi,
1082 &fip_signal_suspend,
1084 GNUNET_FS_end_top (pc->h, pc->top);
1085 publish_cleanup (pc, NULL);
1090 * Publish a file or directory.
1092 * @param h handle to the file sharing subsystem
1093 * @param fi information about the file or directory structure to publish
1094 * @param namespace namespace to publish the file in, NULL for no namespace
1095 * @param nid identifier to use for the publishd content in the namespace
1096 * (can be NULL, must be NULL if namespace is NULL)
1097 * @param nuid update-identifier that will be used for future updates
1098 * (can be NULL, must be NULL if namespace or nid is NULL)
1099 * @param options options for the publication
1100 * @return context that can be used to control the publish operation
1102 struct GNUNET_FS_PublishContext *
1103 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1104 struct GNUNET_FS_FileInformation *fi,
1105 struct GNUNET_FS_Namespace *namespace,
1108 enum GNUNET_FS_PublishOptions options)
1110 struct GNUNET_FS_PublishContext *ret;
1111 struct GNUNET_DATASTORE_Handle *dsh;
1113 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1115 dsh = GNUNET_DATASTORE_connect (h->cfg,
1124 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1128 ret->namespace = namespace;
1129 if (namespace != NULL)
1132 GNUNET_assert (NULL != nid);
1133 ret->nid = GNUNET_strdup (nid);
1135 ret->nuid = GNUNET_strdup (nuid);
1138 GNUNET_FS_file_information_inspect (ret->fi,
1141 ret->fi_pos = ret->fi;
1142 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1143 GNUNET_FS_publish_sync_ (ret);
1144 // FIXME: calculate space needed for "fi"
1145 // and reserve as first task (then trigger
1146 // "publish_main" from that continuation)!
1148 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1149 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1150 &GNUNET_FS_publish_main_,
1157 * Signal the FS's progress function that we are stopping
1160 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1161 * @param fi the entry in the publish-structure
1162 * @param length length of the file or directory
1163 * @param meta metadata for the file or directory (can be modified)
1164 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1165 * @param anonymity pointer to selected anonymity level (can be modified)
1166 * @param priority pointer to selected priority (can be modified)
1167 * @param do_index should we index?
1168 * @param expirationTime pointer to selected expiration time (can be modified)
1169 * @param client_info pointer to client context set upon creation (can be modified)
1170 * @return GNUNET_OK to continue (always)
1173 fip_signal_stop(void *cls,
1174 struct GNUNET_FS_FileInformation *fi,
1176 struct GNUNET_CONTAINER_MetaData *meta,
1177 struct GNUNET_FS_Uri **uri,
1178 uint32_t *anonymity,
1181 struct GNUNET_TIME_Absolute *expirationTime,
1184 struct GNUNET_FS_PublishContext*sc = cls;
1185 struct GNUNET_FS_ProgressInfo pi;
1188 if (fi->serialization != NULL)
1190 GNUNET_FS_remove_sync_file_ (sc->h,
1191 GNUNET_FS_SYNC_PATH_FILE_INFO,
1193 GNUNET_free (fi->serialization);
1194 fi->serialization = NULL;
1196 off = (fi->chk_uri == NULL) ? 0 : length;
1197 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1198 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1199 *client_info = NULL;
1205 * Stop an upload. Will abort incomplete uploads (but
1206 * not remove blocks that have already been publishd) or
1207 * simply clean up the state for completed uploads.
1208 * Must NOT be called from within the event callback!
1210 * @param pc context for the upload to stop
1213 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1215 GNUNET_FS_end_top (pc->h, pc->top);
1216 if (NULL != pc->dsh)
1218 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1221 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1223 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1224 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1226 if (pc->serialization != NULL)
1228 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1229 GNUNET_free (pc->serialization);
1230 pc->serialization = NULL;
1232 GNUNET_FS_file_information_inspect (pc->fi,
1235 if (GNUNET_YES == pc->in_network_wait)
1237 pc->in_network_wait = GNUNET_SYSERR;
1240 publish_cleanup (pc, NULL);
1245 * Context for the KSK publication.
1247 struct PublishKskContext
1253 struct GNUNET_FS_Uri *ksk_uri;
1256 * Global FS context.
1258 struct GNUNET_FS_Handle *h;
1261 * The master block that we are sending
1262 * (in plaintext), has "mdsize+slen" more
1263 * bytes than the struct would suggest.
1268 * Buffer of the same size as "kb" for
1269 * the encrypted version.
1274 * Handle to the datastore, NULL if we are just
1277 struct GNUNET_DATASTORE_Handle *dsh;
1280 * Function to call once we're done.
1282 GNUNET_FS_PublishContinuation cont;
1290 * When should the KBlocks expire?
1292 struct GNUNET_TIME_Absolute expirationTime;
1295 * Size of the serialized metadata.
1300 * Size of the (CHK) URI as a string.
1305 * Keyword that we are currently processing.
1310 * Anonymity level for the KBlocks.
1315 * Priority for the KBlocks.
1322 * Continuation of "GNUNET_FS_publish_ksk" that performs
1323 * the actual publishing operation (iterating over all
1326 * @param cls closure of type "struct PublishKskContext*"
1330 publish_ksk_cont (void *cls,
1331 const struct GNUNET_SCHEDULER_TaskContext *tc);
1335 * Function called by the datastore API with
1336 * the result from the PUT request.
1338 * @param cls closure of type "struct PublishKskContext*"
1339 * @param success GNUNET_OK on success
1340 * @param msg error message (or NULL)
1343 kb_put_cont (void *cls,
1347 struct PublishKskContext *pkc = cls;
1349 if (GNUNET_OK != success)
1351 if (NULL != pkc->dsh)
1353 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1356 GNUNET_free (pkc->cpy);
1357 GNUNET_free (pkc->kb);
1358 pkc->cont (pkc->cont_cls,
1361 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1365 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1368 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1373 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1374 * publishing operation (iterating over all of the keywords).
1376 * @param cls closure of type "struct PublishKskContext*"
1380 publish_ksk_cont (void *cls,
1381 const struct GNUNET_SCHEDULER_TaskContext *tc)
1383 struct PublishKskContext *pkc = cls;
1384 const char *keyword;
1385 GNUNET_HashCode key;
1386 GNUNET_HashCode query;
1387 struct GNUNET_CRYPTO_AesSessionKey skey;
1388 struct GNUNET_CRYPTO_AesInitializationVector iv;
1389 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1392 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1393 (NULL == pkc->dsh) )
1395 if (NULL != pkc->dsh)
1397 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1400 GNUNET_free (pkc->cpy);
1401 GNUNET_free (pkc->kb);
1402 pkc->cont (pkc->cont_cls,
1405 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1409 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1410 /* first character of keyword indicates if it is
1411 mandatory or not -- ignore for hashing */
1412 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1413 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1414 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1415 pkc->slen + pkc->mdsize,
1419 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1420 GNUNET_assert (NULL != pk);
1421 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1422 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1423 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1425 GNUNET_assert (GNUNET_OK ==
1426 GNUNET_CRYPTO_rsa_sign (pk,
1428 &pkc->cpy->signature));
1429 GNUNET_CRYPTO_rsa_key_free (pk);
1430 GNUNET_DATASTORE_put (pkc->dsh,
1434 sizeof (struct KBlock) +
1437 GNUNET_BLOCK_TYPE_KBLOCK,
1440 pkc->expirationTime,
1442 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1449 * Publish a CHK under various keywords on GNUnet.
1451 * @param h handle to the file sharing subsystem
1452 * @param ksk_uri keywords to use
1453 * @param meta metadata to use
1454 * @param uri URI to refer to in the KBlock
1455 * @param expirationTime when the KBlock expires
1456 * @param anonymity anonymity level for the KBlock
1457 * @param priority priority for the KBlock
1458 * @param options publication options
1459 * @param cont continuation
1460 * @param cont_cls closure for cont
1463 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1464 const struct GNUNET_FS_Uri *ksk_uri,
1465 const struct GNUNET_CONTAINER_MetaData *meta,
1466 const struct GNUNET_FS_Uri *uri,
1467 struct GNUNET_TIME_Absolute expirationTime,
1470 enum GNUNET_FS_PublishOptions options,
1471 GNUNET_FS_PublishContinuation cont,
1474 struct PublishKskContext *pkc;
1480 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1482 pkc->expirationTime = expirationTime;
1483 pkc->anonymity = anonymity;
1484 pkc->priority = priority;
1486 pkc->cont_cls = cont_cls;
1487 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1489 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1491 if (pkc->dsh == NULL)
1493 cont (cont_cls, NULL, _("Could not connect to datastore."));
1501 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1502 GNUNET_assert (pkc->mdsize >= 0);
1503 uris = GNUNET_FS_uri_to_string (uri);
1504 pkc->slen = strlen (uris) + 1;
1505 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1506 if (size > MAX_KBLOCK_SIZE)
1508 size = MAX_KBLOCK_SIZE;
1509 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1511 pkc->kb = GNUNET_malloc (size);
1512 kbe = (char *) &pkc->kb[1];
1513 memcpy (kbe, uris, pkc->slen);
1515 sptr = &kbe[pkc->slen];
1517 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1520 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1521 if (pkc->mdsize == -1)
1524 GNUNET_free (pkc->kb);
1525 if (pkc->dsh != NULL)
1527 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1530 cont (cont_cls, NULL, _("Internal error."));
1534 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1536 pkc->cpy = GNUNET_malloc (size);
1537 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1538 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1541 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1542 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1543 GNUNET_SCHEDULER_add_continuation (h->sched,
1546 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1551 * Context for the SKS publication.
1553 struct PublishSksContext
1557 * Global FS context.
1559 struct GNUNET_FS_Uri *uri;
1562 * Handle to the datastore.
1564 struct GNUNET_DATASTORE_Handle *dsh;
1567 * Function to call once we're done.
1569 GNUNET_FS_PublishContinuation cont;
1580 * Function called by the datastore API with
1581 * the result from the PUT (SBlock) request.
1583 * @param cls closure of type "struct PublishSksContext*"
1584 * @param success GNUNET_OK on success
1585 * @param msg error message (or NULL)
1588 sb_put_cont (void *cls,
1592 struct PublishSksContext *psc = cls;
1594 if (NULL != psc->dsh)
1596 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1599 if (GNUNET_OK != success)
1600 psc->cont (psc->cont_cls,
1604 psc->cont (psc->cont_cls,
1607 GNUNET_FS_uri_destroy (psc->uri);
1613 * Publish an SBlock on GNUnet.
1615 * @param h handle to the file sharing subsystem
1616 * @param namespace namespace to publish in
1617 * @param identifier identifier to use
1618 * @param update update identifier to use
1619 * @param meta metadata to use
1620 * @param uri URI to refer to in the SBlock
1621 * @param expirationTime when the SBlock expires
1622 * @param anonymity anonymity level for the SBlock
1623 * @param priority priority for the SBlock
1624 * @param options publication options
1625 * @param cont continuation
1626 * @param cont_cls closure for cont
1629 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1630 struct GNUNET_FS_Namespace *namespace,
1631 const char *identifier,
1633 const struct GNUNET_CONTAINER_MetaData *meta,
1634 const struct GNUNET_FS_Uri *uri,
1635 struct GNUNET_TIME_Absolute expirationTime,
1638 enum GNUNET_FS_PublishOptions options,
1639 GNUNET_FS_PublishContinuation cont,
1642 struct PublishSksContext *psc;
1643 struct GNUNET_CRYPTO_AesSessionKey sk;
1644 struct GNUNET_CRYPTO_AesInitializationVector iv;
1645 struct GNUNET_FS_Uri *sks_uri;
1653 struct SBlock *sb_enc;
1655 struct GNUNET_CONTAINER_MetaData *mmeta;
1656 GNUNET_HashCode key; /* hash of thisId = key */
1657 GNUNET_HashCode id; /* hash of hc = identifier */
1658 GNUNET_HashCode query; /* id ^ nsid = DB query */
1661 mmeta = GNUNET_CONTAINER_meta_data_create ();
1663 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1664 uris = GNUNET_FS_uri_to_string (uri);
1665 slen = strlen (uris) + 1;
1666 idlen = strlen (identifier);
1669 nidlen = strlen (update) + 1;
1670 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1671 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1672 if (size > MAX_SBLOCK_SIZE)
1674 size = MAX_SBLOCK_SIZE;
1675 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1677 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1678 dest = (char *) &sb[1];
1679 memcpy (dest, update, nidlen);
1681 memcpy (dest, uris, slen);
1684 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1687 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1688 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1695 _("Internal error."));
1698 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1699 sb_enc = GNUNET_malloc (size);
1700 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1701 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1702 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1703 sks_uri->type = sks;
1704 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1705 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1706 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1707 &sks_uri->data.sks.namespace);
1708 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1709 GNUNET_CRYPTO_hash_xor (&id,
1710 &sks_uri->data.sks.namespace,
1711 &sb_enc->identifier);
1712 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1713 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1714 size - sizeof (struct SBlock),
1718 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1719 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1720 + sizeof(struct SBlock)
1721 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1722 GNUNET_assert (GNUNET_OK ==
1723 GNUNET_CRYPTO_rsa_sign (namespace->key,
1725 &sb_enc->signature));
1726 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1729 psc->cont_cls = cont_cls;
1730 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1732 GNUNET_free (sb_enc);
1739 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1740 if (NULL == psc->dsh)
1742 GNUNET_free (sb_enc);
1746 _("Failed to connect to datastore."));
1749 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1752 GNUNET_DATASTORE_put (psc->dsh,
1754 &sb_enc->identifier,
1757 GNUNET_BLOCK_TYPE_SBLOCK,
1762 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1766 GNUNET_free (sb_enc);
1769 /* end of fs_publish.c */