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
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);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 _("Failed to get file identifiers for `%s'\n"),
829 GNUNET_break (GNUNET_YES ==
830 GNUNET_CLIENT_transmit_and_get_response (client,
832 GNUNET_TIME_UNIT_FOREVER_REL,
834 &process_index_start_response,
841 * Main function that performs the upload.
843 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
844 * @param tc task context
847 GNUNET_FS_publish_main_ (void *cls,
848 const struct GNUNET_SCHEDULER_TaskContext *tc)
850 struct GNUNET_FS_PublishContext *pc = cls;
851 struct GNUNET_FS_ProgressInfo pi;
852 struct GNUNET_FS_FileInformation *p;
855 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Upload complete, now publishing SKS and KSK blocks.\n");
863 /* upload of entire hierarchy complete,
864 publish namespace entries */
865 GNUNET_FS_publish_sync_ (pc);
869 /* find starting position */
870 while ( (p->is_directory) &&
871 (NULL != p->data.dir.entries) &&
873 (NULL == p->data.dir.entries->chk_uri) )
875 p = p->data.dir.entries;
877 GNUNET_FS_publish_sync_ (pc);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Error uploading: %s\n",
887 /* error with current file, abort all
888 related files as well! */
889 while (NULL != p->dir)
891 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
892 EXTRACTOR_METATYPE_FILENAME);
896 GNUNET_asprintf (&p->emsg,
897 _("Recursive upload failed at `%s': %s"),
904 GNUNET_asprintf (&p->emsg,
905 _("Recursive upload failed: %s"),
908 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
909 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
910 pi.value.publish.specifics.error.message = p->emsg;
911 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
913 pc->all_done = GNUNET_YES;
914 GNUNET_FS_publish_sync_ (pc);
917 /* handle completion */
918 if (NULL != p->chk_uri)
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "File upload complete, now publishing KSK blocks.\n");
924 GNUNET_FS_publish_sync_ (pc);
925 /* upload of "p" complete, publish KBlocks! */
926 if (p->keywords != NULL)
928 GNUNET_FS_publish_ksk (pc->h,
936 &publish_kblocks_cont,
941 publish_kblocks_cont (pc,
947 if ( (!p->is_directory) &&
948 (p->data.file.do_index) )
950 if (NULL == p->filename)
952 p->data.file.do_index = GNUNET_NO;
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 _("Can not index file `%s': %s. Will try to insert instead.\n"),
956 _("needs to be an actual file"));
957 GNUNET_FS_file_information_sync_ (p);
958 publish_content (pc);
961 if (p->data.file.have_hash)
963 hash_for_index_cb (pc,
964 &p->data.file.file_id);
968 p->start_time = GNUNET_TIME_absolute_get ();
969 GNUNET_CRYPTO_hash_file (pc->h->sched,
970 GNUNET_SCHEDULER_PRIORITY_IDLE,
978 publish_content (pc);
983 * Signal the FS's progress function that we are starting
986 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
987 * @param fi the entry in the publish-structure
988 * @param length length of the file or directory
989 * @param meta metadata for the file or directory (can be modified)
990 * @param uri pointer to the keywords that will be used for this entry (can be modified)
991 * @param anonymity pointer to selected anonymity level (can be modified)
992 * @param priority pointer to selected priority (can be modified)
993 * @param do_index should we index?
994 * @param expirationTime pointer to selected expiration time (can be modified)
995 * @param client_info pointer to client context set upon creation (can be modified)
996 * @return GNUNET_OK to continue (always)
999 fip_signal_start(void *cls,
1000 struct GNUNET_FS_FileInformation *fi,
1002 struct GNUNET_CONTAINER_MetaData *meta,
1003 struct GNUNET_FS_Uri **uri,
1004 uint32_t *anonymity,
1007 struct GNUNET_TIME_Absolute *expirationTime,
1010 struct GNUNET_FS_PublishContext *sc = cls;
1011 struct GNUNET_FS_ProgressInfo pi;
1013 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1014 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1015 GNUNET_FS_file_information_sync_ (fi);
1021 * Signal the FS's progress function that we are suspending
1024 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1025 * @param fi the entry in the publish-structure
1026 * @param length length of the file or directory
1027 * @param meta metadata for the file or directory (can be modified)
1028 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1029 * @param anonymity pointer to selected anonymity level (can be modified)
1030 * @param priority pointer to selected priority (can be modified)
1031 * @param do_index should we index?
1032 * @param expirationTime pointer to selected expiration time (can be modified)
1033 * @param client_info pointer to client context set upon creation (can be modified)
1034 * @return GNUNET_OK to continue (always)
1037 fip_signal_suspend(void *cls,
1038 struct GNUNET_FS_FileInformation *fi,
1040 struct GNUNET_CONTAINER_MetaData *meta,
1041 struct GNUNET_FS_Uri **uri,
1042 uint32_t *anonymity,
1045 struct GNUNET_TIME_Absolute *expirationTime,
1048 struct GNUNET_FS_PublishContext*sc = cls;
1049 struct GNUNET_FS_ProgressInfo pi;
1052 GNUNET_free_non_null (fi->serialization);
1053 fi->serialization = NULL;
1054 off = (fi->chk_uri == NULL) ? 0 : length;
1055 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1056 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1057 *client_info = NULL;
1058 if (NULL != sc->dsh)
1060 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1068 * Create SUSPEND event for the given publish operation
1069 * and then clean up our state (without stop signal).
1071 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1074 GNUNET_FS_publish_signal_suspend_ (void *cls)
1076 struct GNUNET_FS_PublishContext *pc = cls;
1078 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1080 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1081 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1083 GNUNET_FS_file_information_inspect (pc->fi,
1084 &fip_signal_suspend,
1086 GNUNET_FS_end_top (pc->h, pc->top);
1087 publish_cleanup (pc, NULL);
1092 * Publish a file or directory.
1094 * @param h handle to the file sharing subsystem
1095 * @param fi information about the file or directory structure to publish
1096 * @param namespace namespace to publish the file in, NULL for no namespace
1097 * @param nid identifier to use for the publishd content in the namespace
1098 * (can be NULL, must be NULL if namespace is NULL)
1099 * @param nuid update-identifier that will be used for future updates
1100 * (can be NULL, must be NULL if namespace or nid is NULL)
1101 * @param options options for the publication
1102 * @return context that can be used to control the publish operation
1104 struct GNUNET_FS_PublishContext *
1105 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1106 struct GNUNET_FS_FileInformation *fi,
1107 struct GNUNET_FS_Namespace *namespace,
1110 enum GNUNET_FS_PublishOptions options)
1112 struct GNUNET_FS_PublishContext *ret;
1113 struct GNUNET_DATASTORE_Handle *dsh;
1115 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1117 dsh = GNUNET_DATASTORE_connect (h->cfg,
1126 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1130 ret->namespace = namespace;
1131 if (namespace != NULL)
1134 GNUNET_assert (NULL != nid);
1135 ret->nid = GNUNET_strdup (nid);
1137 ret->nuid = GNUNET_strdup (nuid);
1140 GNUNET_FS_file_information_inspect (ret->fi,
1143 ret->fi_pos = ret->fi;
1144 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1145 GNUNET_FS_publish_sync_ (ret);
1146 // FIXME: calculate space needed for "fi"
1147 // and reserve as first task (then trigger
1148 // "publish_main" from that continuation)!
1150 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1151 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1152 &GNUNET_FS_publish_main_,
1159 * Signal the FS's progress function that we are stopping
1162 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1163 * @param fi the entry in the publish-structure
1164 * @param length length of the file or directory
1165 * @param meta metadata for the file or directory (can be modified)
1166 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1167 * @param anonymity pointer to selected anonymity level (can be modified)
1168 * @param priority pointer to selected priority (can be modified)
1169 * @param do_index should we index?
1170 * @param expirationTime pointer to selected expiration time (can be modified)
1171 * @param client_info pointer to client context set upon creation (can be modified)
1172 * @return GNUNET_OK to continue (always)
1175 fip_signal_stop(void *cls,
1176 struct GNUNET_FS_FileInformation *fi,
1178 struct GNUNET_CONTAINER_MetaData *meta,
1179 struct GNUNET_FS_Uri **uri,
1180 uint32_t *anonymity,
1183 struct GNUNET_TIME_Absolute *expirationTime,
1186 struct GNUNET_FS_PublishContext*sc = cls;
1187 struct GNUNET_FS_ProgressInfo pi;
1190 if (fi->serialization != NULL)
1192 GNUNET_FS_remove_sync_file_ (sc->h,
1193 GNUNET_FS_SYNC_PATH_FILE_INFO,
1195 GNUNET_free (fi->serialization);
1196 fi->serialization = NULL;
1198 off = (fi->chk_uri == NULL) ? 0 : length;
1199 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1200 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1201 *client_info = NULL;
1207 * Stop an upload. Will abort incomplete uploads (but
1208 * not remove blocks that have already been publishd) or
1209 * simply clean up the state for completed uploads.
1210 * Must NOT be called from within the event callback!
1212 * @param pc context for the upload to stop
1215 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1217 GNUNET_FS_end_top (pc->h, pc->top);
1218 if (NULL != pc->dsh)
1220 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1223 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1225 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1226 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1228 if (pc->serialization != NULL)
1230 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1231 GNUNET_free (pc->serialization);
1232 pc->serialization = NULL;
1234 GNUNET_FS_file_information_inspect (pc->fi,
1237 if (GNUNET_YES == pc->in_network_wait)
1239 pc->in_network_wait = GNUNET_SYSERR;
1242 publish_cleanup (pc, NULL);
1247 * Context for the KSK publication.
1249 struct PublishKskContext
1255 struct GNUNET_FS_Uri *ksk_uri;
1258 * Global FS context.
1260 struct GNUNET_FS_Handle *h;
1263 * The master block that we are sending
1264 * (in plaintext), has "mdsize+slen" more
1265 * bytes than the struct would suggest.
1270 * Buffer of the same size as "kb" for
1271 * the encrypted version.
1276 * Handle to the datastore, NULL if we are just
1279 struct GNUNET_DATASTORE_Handle *dsh;
1282 * Function to call once we're done.
1284 GNUNET_FS_PublishContinuation cont;
1292 * When should the KBlocks expire?
1294 struct GNUNET_TIME_Absolute expirationTime;
1297 * Size of the serialized metadata.
1302 * Size of the (CHK) URI as a string.
1307 * Keyword that we are currently processing.
1312 * Anonymity level for the KBlocks.
1317 * Priority for the KBlocks.
1324 * Continuation of "GNUNET_FS_publish_ksk" that performs
1325 * the actual publishing operation (iterating over all
1328 * @param cls closure of type "struct PublishKskContext*"
1332 publish_ksk_cont (void *cls,
1333 const struct GNUNET_SCHEDULER_TaskContext *tc);
1337 * Function called by the datastore API with
1338 * the result from the PUT request.
1340 * @param cls closure of type "struct PublishKskContext*"
1341 * @param success GNUNET_OK on success
1342 * @param msg error message (or NULL)
1345 kb_put_cont (void *cls,
1349 struct PublishKskContext *pkc = cls;
1351 if (GNUNET_OK != success)
1353 if (NULL != pkc->dsh)
1355 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1358 GNUNET_free (pkc->cpy);
1359 GNUNET_free (pkc->kb);
1360 pkc->cont (pkc->cont_cls,
1363 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1367 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1370 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1375 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1376 * publishing operation (iterating over all of the keywords).
1378 * @param cls closure of type "struct PublishKskContext*"
1382 publish_ksk_cont (void *cls,
1383 const struct GNUNET_SCHEDULER_TaskContext *tc)
1385 struct PublishKskContext *pkc = cls;
1386 const char *keyword;
1387 GNUNET_HashCode key;
1388 GNUNET_HashCode query;
1389 struct GNUNET_CRYPTO_AesSessionKey skey;
1390 struct GNUNET_CRYPTO_AesInitializationVector iv;
1391 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1394 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1395 (NULL == pkc->dsh) )
1397 if (NULL != pkc->dsh)
1399 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1402 GNUNET_free (pkc->cpy);
1403 GNUNET_free (pkc->kb);
1404 pkc->cont (pkc->cont_cls,
1407 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1411 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1412 /* first character of keyword indicates if it is
1413 mandatory or not -- ignore for hashing */
1414 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1415 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1416 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1417 pkc->slen + pkc->mdsize,
1421 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1422 GNUNET_assert (NULL != pk);
1423 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1424 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1425 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1427 GNUNET_assert (GNUNET_OK ==
1428 GNUNET_CRYPTO_rsa_sign (pk,
1430 &pkc->cpy->signature));
1431 GNUNET_CRYPTO_rsa_key_free (pk);
1432 GNUNET_DATASTORE_put (pkc->dsh,
1436 sizeof (struct KBlock) +
1439 GNUNET_BLOCK_TYPE_KBLOCK,
1442 pkc->expirationTime,
1444 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1451 * Publish a CHK under various keywords on GNUnet.
1453 * @param h handle to the file sharing subsystem
1454 * @param ksk_uri keywords to use
1455 * @param meta metadata to use
1456 * @param uri URI to refer to in the KBlock
1457 * @param expirationTime when the KBlock expires
1458 * @param anonymity anonymity level for the KBlock
1459 * @param priority priority for the KBlock
1460 * @param options publication options
1461 * @param cont continuation
1462 * @param cont_cls closure for cont
1465 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1466 const struct GNUNET_FS_Uri *ksk_uri,
1467 const struct GNUNET_CONTAINER_MetaData *meta,
1468 const struct GNUNET_FS_Uri *uri,
1469 struct GNUNET_TIME_Absolute expirationTime,
1472 enum GNUNET_FS_PublishOptions options,
1473 GNUNET_FS_PublishContinuation cont,
1476 struct PublishKskContext *pkc;
1482 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1484 pkc->expirationTime = expirationTime;
1485 pkc->anonymity = anonymity;
1486 pkc->priority = priority;
1488 pkc->cont_cls = cont_cls;
1489 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1491 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1493 if (pkc->dsh == NULL)
1495 cont (cont_cls, NULL, _("Could not connect to datastore."));
1503 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1504 GNUNET_assert (pkc->mdsize >= 0);
1505 uris = GNUNET_FS_uri_to_string (uri);
1506 pkc->slen = strlen (uris) + 1;
1507 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1508 if (size > MAX_KBLOCK_SIZE)
1510 size = MAX_KBLOCK_SIZE;
1511 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1513 pkc->kb = GNUNET_malloc (size);
1514 kbe = (char *) &pkc->kb[1];
1515 memcpy (kbe, uris, pkc->slen);
1517 sptr = &kbe[pkc->slen];
1519 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1522 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1523 if (pkc->mdsize == -1)
1526 GNUNET_free (pkc->kb);
1527 if (pkc->dsh != NULL)
1529 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1532 cont (cont_cls, NULL, _("Internal error."));
1536 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1538 pkc->cpy = GNUNET_malloc (size);
1539 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1540 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1543 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1544 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1545 GNUNET_SCHEDULER_add_continuation (h->sched,
1548 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1553 * Context for the SKS publication.
1555 struct PublishSksContext
1559 * Global FS context.
1561 struct GNUNET_FS_Uri *uri;
1564 * Handle to the datastore.
1566 struct GNUNET_DATASTORE_Handle *dsh;
1569 * Function to call once we're done.
1571 GNUNET_FS_PublishContinuation cont;
1582 * Function called by the datastore API with
1583 * the result from the PUT (SBlock) request.
1585 * @param cls closure of type "struct PublishSksContext*"
1586 * @param success GNUNET_OK on success
1587 * @param msg error message (or NULL)
1590 sb_put_cont (void *cls,
1594 struct PublishSksContext *psc = cls;
1596 if (NULL != psc->dsh)
1598 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1601 if (GNUNET_OK != success)
1602 psc->cont (psc->cont_cls,
1606 psc->cont (psc->cont_cls,
1609 GNUNET_FS_uri_destroy (psc->uri);
1615 * Publish an SBlock on GNUnet.
1617 * @param h handle to the file sharing subsystem
1618 * @param namespace namespace to publish in
1619 * @param identifier identifier to use
1620 * @param update update identifier to use
1621 * @param meta metadata to use
1622 * @param uri URI to refer to in the SBlock
1623 * @param expirationTime when the SBlock expires
1624 * @param anonymity anonymity level for the SBlock
1625 * @param priority priority for the SBlock
1626 * @param options publication options
1627 * @param cont continuation
1628 * @param cont_cls closure for cont
1631 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1632 struct GNUNET_FS_Namespace *namespace,
1633 const char *identifier,
1635 const struct GNUNET_CONTAINER_MetaData *meta,
1636 const struct GNUNET_FS_Uri *uri,
1637 struct GNUNET_TIME_Absolute expirationTime,
1640 enum GNUNET_FS_PublishOptions options,
1641 GNUNET_FS_PublishContinuation cont,
1644 struct PublishSksContext *psc;
1645 struct GNUNET_CRYPTO_AesSessionKey sk;
1646 struct GNUNET_CRYPTO_AesInitializationVector iv;
1647 struct GNUNET_FS_Uri *sks_uri;
1655 struct SBlock *sb_enc;
1657 struct GNUNET_CONTAINER_MetaData *mmeta;
1658 GNUNET_HashCode key; /* hash of thisId = key */
1659 GNUNET_HashCode id; /* hash of hc = identifier */
1660 GNUNET_HashCode query; /* id ^ nsid = DB query */
1663 mmeta = GNUNET_CONTAINER_meta_data_create ();
1665 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1666 uris = GNUNET_FS_uri_to_string (uri);
1667 slen = strlen (uris) + 1;
1668 idlen = strlen (identifier);
1671 nidlen = strlen (update) + 1;
1672 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1673 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1674 if (size > MAX_SBLOCK_SIZE)
1676 size = MAX_SBLOCK_SIZE;
1677 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1679 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1680 dest = (char *) &sb[1];
1681 memcpy (dest, update, nidlen);
1683 memcpy (dest, uris, slen);
1686 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1689 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1690 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1697 _("Internal error."));
1700 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1701 sb_enc = GNUNET_malloc (size);
1702 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1703 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1704 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1705 sks_uri->type = sks;
1706 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1707 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1708 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1709 &sks_uri->data.sks.namespace);
1710 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1711 GNUNET_CRYPTO_hash_xor (&id,
1712 &sks_uri->data.sks.namespace,
1713 &sb_enc->identifier);
1714 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1715 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1716 size - sizeof (struct SBlock),
1720 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1721 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1722 + sizeof(struct SBlock)
1723 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1724 GNUNET_assert (GNUNET_OK ==
1725 GNUNET_CRYPTO_rsa_sign (namespace->key,
1727 &sb_enc->signature));
1728 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1731 psc->cont_cls = cont_cls;
1732 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1734 GNUNET_free (sb_enc);
1741 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1742 if (NULL == psc->dsh)
1744 GNUNET_free (sb_enc);
1748 _("Failed to connect to datastore."));
1751 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1754 GNUNET_DATASTORE_put (psc->dsh,
1756 &sb_enc->identifier,
1759 GNUNET_BLOCK_TYPE_SBLOCK,
1764 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1768 GNUNET_free (sb_enc);
1771 /* end of fs_publish.c */