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.sc = 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;
307 signal_publish_error (p, pc, emsg);
308 GNUNET_FS_file_information_sync_ (p);
309 GNUNET_FS_publish_sync_ (pc);
311 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
312 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
313 &GNUNET_FS_publish_main_,
318 signal_publish_completion (p, pc);
319 /* move on to next file */
321 pc->fi_pos = p->next;
324 GNUNET_FS_publish_sync_ (pc);
326 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
327 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
328 &GNUNET_FS_publish_main_,
334 * Function called by the tree encoder to obtain
335 * a block of plaintext data (for the lowest level
338 * @param cls our publishing context
339 * @param offset identifies which block to get
340 * @param max (maximum) number of bytes to get; returning
341 * fewer will also cause errors
342 * @param buf where to copy the plaintext buffer
343 * @param emsg location to store an error message (on error)
344 * @return number of bytes copied to buf, 0 on error
347 block_reader (void *cls,
353 struct GNUNET_FS_PublishContext *sc = cls;
354 struct GNUNET_FS_FileInformation *p;
361 pt_size = GNUNET_MIN(max,
362 p->data.dir.dir_size - offset);
363 dd = p->data.dir.dir_data;
370 pt_size = GNUNET_MIN(max,
371 p->data.file.file_size - offset);
373 return 0; /* calling reader with pt_size==0
374 might free buf, so don't! */
376 p->data.file.reader (p->data.file.reader_cls,
388 * The tree encoder has finished processing a
389 * file. Call it's finish method and deal with
392 * @param cls our publishing context
393 * @param tc scheduler's task context (not used)
396 encode_cont (void *cls,
397 const struct GNUNET_SCHEDULER_TaskContext *tc)
399 struct GNUNET_FS_PublishContext *sc = cls;
400 struct GNUNET_FS_FileInformation *p;
401 struct GNUNET_FS_ProgressInfo pi;
405 GNUNET_FS_tree_encoder_finish (p->te,
411 GNUNET_asprintf (&p->emsg,
412 _("Upload failed: %s"),
415 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
416 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
417 pi.value.publish.specifics.error.message = p->emsg;
418 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
420 /* continue with main */
422 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
423 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
424 &GNUNET_FS_publish_main_,
430 * Function called asking for the current (encoded)
431 * block to be processed. After processing the
432 * client should either call "GNUNET_FS_tree_encode_next"
433 * or (on error) "GNUNET_FS_tree_encode_finish".
436 * @param query the query for the block (key for lookup in the datastore)
437 * @param offset offset of the block in the file
438 * @param type type of the block (IBLOCK or DBLOCK)
439 * @param block the (encrypted) block
440 * @param block_size size of block (in bytes)
443 block_proc (void *cls,
444 const GNUNET_HashCode *query,
446 enum GNUNET_BLOCK_Type type,
450 struct GNUNET_FS_PublishContext *sc = cls;
451 struct GNUNET_FS_FileInformation *p;
452 struct PutContCtx * dpc_cls;
453 struct OnDemandBlock odb;
459 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
460 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
461 &GNUNET_FS_publish_main_,
466 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
467 sc->in_network_wait = GNUNET_YES;
468 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
469 dpc_cls->cont = &GNUNET_FS_publish_main_;
470 dpc_cls->cont_cls = sc;
473 if ( (! p->is_directory) &&
474 (GNUNET_YES == p->data.file.do_index) &&
475 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Indexing block `%s' for offset %llu with index size %u\n",
481 (unsigned long long) offset,
482 sizeof (struct OnDemandBlock));
484 odb.offset = GNUNET_htonll (offset);
485 odb.file_id = p->data.file.file_id;
486 GNUNET_DATASTORE_put (sc->dsh,
489 sizeof(struct OnDemandBlock),
491 GNUNET_BLOCK_TYPE_ONDEMAND,
496 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Publishing block `%s' for offset %llu with size %u\n",
505 (unsigned long long) offset,
506 (unsigned int) block_size);
508 GNUNET_DATASTORE_put (sc->dsh,
518 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
525 * Function called with information about our
526 * progress in computing the tree encoding.
529 * @param offset where are we in the file
530 * @param pt_block plaintext of the currently processed block
531 * @param pt_size size of pt_block
532 * @param depth depth of the block in the tree
535 progress_proc (void *cls,
537 const void *pt_block,
541 struct GNUNET_FS_PublishContext *sc = cls;
542 struct GNUNET_FS_FileInformation *p;
543 struct GNUNET_FS_ProgressInfo pi;
546 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
547 pi.value.publish.specifics.progress.data = pt_block;
548 pi.value.publish.specifics.progress.offset = offset;
549 pi.value.publish.specifics.progress.data_len = pt_size;
550 pi.value.publish.specifics.progress.depth = depth;
551 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
556 * We are uploading a file or directory; load (if necessary) the next
557 * block into memory, encrypt it and send it to the FS service. Then
558 * continue with the main task.
560 * @param sc overall upload data
563 publish_content (struct GNUNET_FS_PublishContext *sc)
565 struct GNUNET_FS_FileInformation *p;
567 struct GNUNET_FS_DirectoryBuilder *db;
568 struct GNUNET_FS_FileInformation *dirpos;
573 GNUNET_assert (p != NULL);
578 db = GNUNET_FS_directory_builder_create (p->meta);
579 dirpos = p->data.dir.entries;
580 while (NULL != dirpos)
582 if (dirpos->is_directory)
584 raw_data = dirpos->data.dir.dir_data;
585 dirpos->data.dir.dir_data = NULL;
590 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
591 (dirpos->data.file.file_size > 0) )
593 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
595 if (dirpos->data.file.file_size !=
596 dirpos->data.file.reader (dirpos->data.file.reader_cls,
598 dirpos->data.file.file_size,
602 GNUNET_free_non_null (emsg);
603 GNUNET_free (raw_data);
608 GNUNET_FS_directory_builder_add (db,
612 GNUNET_free_non_null (raw_data);
613 dirpos = dirpos->next;
615 GNUNET_FS_directory_builder_finish (db,
616 &p->data.dir.dir_size,
617 &p->data.dir.dir_data);
618 GNUNET_FS_file_information_sync_ (p);
620 size = (p->is_directory)
621 ? p->data.dir.dir_size
622 : p->data.file.file_size;
623 p->te = GNUNET_FS_tree_encoder_create (sc->h,
632 GNUNET_FS_tree_encoder_next (p->te);
637 * Process the response (or lack thereof) from
638 * the "fs" service to our 'start index' request.
640 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
641 * @param msg the response we got
644 process_index_start_response (void *cls,
645 const struct GNUNET_MessageHeader *msg)
647 struct GNUNET_FS_PublishContext *sc = cls;
648 struct GNUNET_FS_FileInformation *p;
652 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 _("Can not index file `%s': %s. Will try to insert instead.\n"),
660 _("timeout on index-start request to `fs' service"));
661 p->data.file.do_index = GNUNET_NO;
662 GNUNET_FS_file_information_sync_ (p);
663 publish_content (sc);
666 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
668 msize = ntohs (msg->size);
669 emsg = (const char *) &msg[1];
670 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
671 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
672 emsg = gettext_noop ("unknown error");
673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
674 _("Can not index file `%s': %s. Will try to insert instead.\n"),
677 p->data.file.do_index = GNUNET_NO;
678 GNUNET_FS_file_information_sync_ (p);
679 publish_content (sc);
682 p->data.file.index_start_confirmed = GNUNET_YES;
683 /* success! continue with indexing */
684 GNUNET_FS_file_information_sync_ (p);
685 publish_content (sc);
690 * Function called once the hash computation over an
691 * indexed file has completed.
693 * @param cls closure, our publishing context
694 * @param res resulting hash, NULL on error
697 hash_for_index_cb (void *cls,
698 const GNUNET_HashCode *
701 struct GNUNET_FS_PublishContext *sc = cls;
702 struct GNUNET_FS_FileInformation *p;
703 struct IndexStartMessage *ism;
705 struct GNUNET_CLIENT_Connection *client;
713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
714 _("Can not index file `%s': %s. Will try to insert instead.\n"),
716 _("failed to compute hash"));
717 p->data.file.do_index = GNUNET_NO;
718 GNUNET_FS_file_information_sync_ (p);
719 publish_content (sc);
722 if (GNUNET_YES == p->data.file.index_start_confirmed)
724 publish_content (sc);
727 fn = GNUNET_STRINGS_filename_expand (p->filename);
728 slen = strlen (fn) + 1;
729 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
731 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
732 _("Can not index file `%s': %s. Will try to insert instead.\n"),
734 _("filename too long"));
736 p->data.file.do_index = GNUNET_NO;
737 GNUNET_FS_file_information_sync_ (p);
738 publish_content (sc);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Hash of indexed file `%s' is `%s'\n",
744 p->data.file.filename,
747 client = GNUNET_CLIENT_connect (sc->h->sched,
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("Can not index file `%s': %s. Will try to insert instead.\n"),
755 _("could not connect to `fs' service"));
756 p->data.file.do_index = GNUNET_NO;
757 publish_content (sc);
761 if (p->data.file.have_hash != GNUNET_YES)
763 p->data.file.file_id = *res;
764 p->data.file.have_hash = GNUNET_YES;
765 GNUNET_FS_file_information_sync_ (p);
767 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
769 ism->header.size = htons(sizeof(struct IndexStartMessage) +
771 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
773 GNUNET_DISK_file_get_identifiers (p->filename,
777 ism->device = htonl (dev);
778 ism->inode = GNUNET_htonll(ino);
782 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
783 _("Failed to get file identifiers for `%s'\n"),
792 GNUNET_break (GNUNET_YES ==
793 GNUNET_CLIENT_transmit_and_get_response (client,
795 GNUNET_TIME_UNIT_FOREVER_REL,
797 &process_index_start_response,
804 * Main function that performs the upload.
806 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
807 * @param tc task context
810 GNUNET_FS_publish_main_ (void *cls,
811 const struct GNUNET_SCHEDULER_TaskContext *tc)
813 struct GNUNET_FS_PublishContext *pc = cls;
814 struct GNUNET_FS_ProgressInfo pi;
815 struct GNUNET_FS_FileInformation *p;
818 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
822 /* upload of entire hierarchy complete,
823 publish namespace entries */
824 GNUNET_FS_publish_sync_ (pc);
828 /* find starting position */
829 while ( (p->is_directory) &&
830 (NULL != p->data.dir.entries) &&
832 (NULL == p->data.dir.entries->chk_uri) )
834 p = p->data.dir.entries;
836 GNUNET_FS_publish_sync_ (pc);
841 /* error with current file, abort all
842 related files as well! */
843 while (NULL != p->dir)
845 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
846 EXTRACTOR_METATYPE_FILENAME);
850 GNUNET_asprintf (&p->emsg,
851 _("Recursive upload failed at `%s': %s"),
858 GNUNET_asprintf (&p->emsg,
859 _("Recursive upload failed: %s"),
862 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
863 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
864 pi.value.publish.specifics.error.message = p->emsg;
865 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
867 pc->all_done = GNUNET_YES;
868 GNUNET_FS_publish_sync_ (pc);
871 /* handle completion */
872 if (NULL != p->chk_uri)
874 GNUNET_FS_publish_sync_ (pc);
875 /* upload of "p" complete, publish KBlocks! */
876 if (p->keywords != NULL)
878 GNUNET_FS_publish_ksk (pc->h,
886 &publish_kblocks_cont,
891 publish_kblocks_cont (pc,
897 if ( (!p->is_directory) &&
898 (p->data.file.do_index) )
900 if (NULL == p->filename)
902 p->data.file.do_index = GNUNET_NO;
903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
904 _("Can not index file `%s': %s. Will try to insert instead.\n"),
906 _("needs to be an actual file"));
907 GNUNET_FS_file_information_sync_ (p);
908 publish_content (pc);
911 if (p->data.file.have_hash)
913 hash_for_index_cb (pc,
914 &p->data.file.file_id);
918 p->start_time = GNUNET_TIME_absolute_get ();
919 GNUNET_CRYPTO_hash_file (pc->h->sched,
920 GNUNET_SCHEDULER_PRIORITY_IDLE,
928 publish_content (pc);
933 * Signal the FS's progress function that we are starting
936 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
937 * @param fi the entry in the publish-structure
938 * @param length length of the file or directory
939 * @param meta metadata for the file or directory (can be modified)
940 * @param uri pointer to the keywords that will be used for this entry (can be modified)
941 * @param anonymity pointer to selected anonymity level (can be modified)
942 * @param priority pointer to selected priority (can be modified)
943 * @param expirationTime pointer to selected expiration time (can be modified)
944 * @param client_info pointer to client context set upon creation (can be modified)
945 * @return GNUNET_OK to continue (always)
948 fip_signal_start(void *cls,
949 struct GNUNET_FS_FileInformation *fi,
951 struct GNUNET_CONTAINER_MetaData *meta,
952 struct GNUNET_FS_Uri **uri,
955 struct GNUNET_TIME_Absolute *expirationTime,
958 struct GNUNET_FS_PublishContext *sc = cls;
959 struct GNUNET_FS_ProgressInfo pi;
961 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
962 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
963 GNUNET_FS_file_information_sync_ (fi);
969 * Signal the FS's progress function that we are suspending
972 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
973 * @param fi the entry in the publish-structure
974 * @param length length of the file or directory
975 * @param meta metadata for the file or directory (can be modified)
976 * @param uri pointer to the keywords that will be used for this entry (can be modified)
977 * @param anonymity pointer to selected anonymity level (can be modified)
978 * @param priority pointer to selected priority (can be modified)
979 * @param expirationTime pointer to selected expiration time (can be modified)
980 * @param client_info pointer to client context set upon creation (can be modified)
981 * @return GNUNET_OK to continue (always)
984 fip_signal_suspend(void *cls,
985 struct GNUNET_FS_FileInformation *fi,
987 struct GNUNET_CONTAINER_MetaData *meta,
988 struct GNUNET_FS_Uri **uri,
991 struct GNUNET_TIME_Absolute *expirationTime,
994 struct GNUNET_FS_PublishContext*sc = cls;
995 struct GNUNET_FS_ProgressInfo pi;
998 GNUNET_free_non_null (fi->serialization);
999 fi->serialization = NULL;
1000 off = (fi->chk_uri == NULL) ? 0 : length;
1001 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1002 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1003 *client_info = NULL;
1004 if (NULL != sc->dsh)
1006 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1014 * Create SUSPEND event for the given publish operation
1015 * and then clean up our state (without stop signal).
1017 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1020 GNUNET_FS_publish_signal_suspend_ (void *cls)
1022 struct GNUNET_FS_PublishContext *pc = cls;
1024 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1026 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1027 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1029 GNUNET_FS_file_information_inspect (pc->fi,
1030 &fip_signal_suspend,
1032 GNUNET_FS_end_top (pc->h, pc->top);
1033 publish_cleanup (pc, NULL);
1038 * Publish a file or directory.
1040 * @param h handle to the file sharing subsystem
1041 * @param fi information about the file or directory structure to publish
1042 * @param namespace namespace to publish the file in, NULL for no namespace
1043 * @param nid identifier to use for the publishd content in the namespace
1044 * (can be NULL, must be NULL if namespace is NULL)
1045 * @param nuid update-identifier that will be used for future updates
1046 * (can be NULL, must be NULL if namespace or nid is NULL)
1047 * @param options options for the publication
1048 * @return context that can be used to control the publish operation
1050 struct GNUNET_FS_PublishContext *
1051 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1052 struct GNUNET_FS_FileInformation *fi,
1053 struct GNUNET_FS_Namespace *namespace,
1056 enum GNUNET_FS_PublishOptions options)
1058 struct GNUNET_FS_PublishContext *ret;
1059 struct GNUNET_DATASTORE_Handle *dsh;
1061 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1063 dsh = GNUNET_DATASTORE_connect (h->cfg,
1072 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1076 ret->namespace = namespace;
1077 if (namespace != NULL)
1080 GNUNET_assert (NULL != nid);
1081 ret->nid = GNUNET_strdup (nid);
1083 ret->nuid = GNUNET_strdup (nuid);
1086 GNUNET_FS_file_information_inspect (ret->fi,
1089 ret->fi_pos = ret->fi;
1090 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1091 GNUNET_FS_publish_sync_ (ret);
1092 // FIXME: calculate space needed for "fi"
1093 // and reserve as first task (then trigger
1094 // "publish_main" from that continuation)!
1096 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1097 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1098 &GNUNET_FS_publish_main_,
1105 * Signal the FS's progress function that we are stopping
1108 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1109 * @param fi the entry in the publish-structure
1110 * @param length length of the file or directory
1111 * @param meta metadata for the file or directory (can be modified)
1112 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1113 * @param anonymity pointer to selected anonymity level (can be modified)
1114 * @param priority pointer to selected priority (can be modified)
1115 * @param expirationTime pointer to selected expiration time (can be modified)
1116 * @param client_info pointer to client context set upon creation (can be modified)
1117 * @return GNUNET_OK to continue (always)
1120 fip_signal_stop(void *cls,
1121 struct GNUNET_FS_FileInformation *fi,
1123 struct GNUNET_CONTAINER_MetaData *meta,
1124 struct GNUNET_FS_Uri **uri,
1125 uint32_t *anonymity,
1127 struct GNUNET_TIME_Absolute *expirationTime,
1130 struct GNUNET_FS_PublishContext*sc = cls;
1131 struct GNUNET_FS_ProgressInfo pi;
1134 if (fi->serialization != NULL)
1136 GNUNET_FS_remove_sync_file_ (sc->h,
1137 GNUNET_FS_SYNC_PATH_FILE_INFO,
1139 GNUNET_free (fi->serialization);
1140 fi->serialization = NULL;
1142 off = (fi->chk_uri == NULL) ? 0 : length;
1143 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1144 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1145 *client_info = NULL;
1151 * Stop an upload. Will abort incomplete uploads (but
1152 * not remove blocks that have already been publishd) or
1153 * simply clean up the state for completed uploads.
1154 * Must NOT be called from within the event callback!
1156 * @param pc context for the upload to stop
1159 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1161 GNUNET_FS_end_top (pc->h, pc->top);
1162 if (NULL != pc->dsh)
1164 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1167 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1169 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1170 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1172 if (pc->serialization != NULL)
1174 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1175 GNUNET_free (pc->serialization);
1176 pc->serialization = NULL;
1178 GNUNET_FS_file_information_inspect (pc->fi,
1181 if (GNUNET_YES == pc->in_network_wait)
1183 pc->in_network_wait = GNUNET_SYSERR;
1186 publish_cleanup (pc, NULL);
1191 * Context for the KSK publication.
1193 struct PublishKskContext
1199 struct GNUNET_FS_Uri *ksk_uri;
1202 * Global FS context.
1204 struct GNUNET_FS_Handle *h;
1207 * The master block that we are sending
1208 * (in plaintext), has "mdsize+slen" more
1209 * bytes than the struct would suggest.
1214 * Buffer of the same size as "kb" for
1215 * the encrypted version.
1220 * Handle to the datastore, NULL if we are just
1223 struct GNUNET_DATASTORE_Handle *dsh;
1226 * Function to call once we're done.
1228 GNUNET_FS_PublishContinuation cont;
1236 * When should the KBlocks expire?
1238 struct GNUNET_TIME_Absolute expirationTime;
1241 * Size of the serialized metadata.
1246 * Size of the (CHK) URI as a string.
1251 * Keyword that we are currently processing.
1256 * Anonymity level for the KBlocks.
1261 * Priority for the KBlocks.
1268 * Continuation of "GNUNET_FS_publish_ksk" that performs
1269 * the actual publishing operation (iterating over all
1272 * @param cls closure of type "struct PublishKskContext*"
1276 publish_ksk_cont (void *cls,
1277 const struct GNUNET_SCHEDULER_TaskContext *tc);
1281 * Function called by the datastore API with
1282 * the result from the PUT request.
1284 * @param cls closure of type "struct PublishKskContext*"
1285 * @param success GNUNET_OK on success
1286 * @param msg error message (or NULL)
1289 kb_put_cont (void *cls,
1293 struct PublishKskContext *pkc = cls;
1295 if (GNUNET_OK != success)
1297 if (NULL != pkc->dsh)
1299 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1302 GNUNET_free (pkc->cpy);
1303 GNUNET_free (pkc->kb);
1304 pkc->cont (pkc->cont_cls,
1307 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1311 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1314 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1319 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1320 * publishing operation (iterating over all of the keywords).
1322 * @param cls closure of type "struct PublishKskContext*"
1326 publish_ksk_cont (void *cls,
1327 const struct GNUNET_SCHEDULER_TaskContext *tc)
1329 struct PublishKskContext *pkc = cls;
1330 const char *keyword;
1331 GNUNET_HashCode key;
1332 GNUNET_HashCode query;
1333 struct GNUNET_CRYPTO_AesSessionKey skey;
1334 struct GNUNET_CRYPTO_AesInitializationVector iv;
1335 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1338 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1339 (NULL == pkc->dsh) )
1341 if (NULL != pkc->dsh)
1343 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1346 GNUNET_free (pkc->cpy);
1347 GNUNET_free (pkc->kb);
1348 pkc->cont (pkc->cont_cls,
1351 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1355 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1356 /* first character of keyword indicates if it is
1357 mandatory or not -- ignore for hashing */
1358 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1359 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1360 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1361 pkc->slen + pkc->mdsize,
1365 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1366 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1367 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1368 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1370 GNUNET_assert (GNUNET_OK ==
1371 GNUNET_CRYPTO_rsa_sign (pk,
1373 &pkc->cpy->signature));
1374 GNUNET_CRYPTO_rsa_key_free (pk);
1375 GNUNET_DATASTORE_put (pkc->dsh,
1379 sizeof (struct KBlock) +
1382 GNUNET_BLOCK_TYPE_KBLOCK,
1385 pkc->expirationTime,
1387 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1394 * Publish a CHK under various keywords on GNUnet.
1396 * @param h handle to the file sharing subsystem
1397 * @param ksk_uri keywords to use
1398 * @param meta metadata to use
1399 * @param uri URI to refer to in the KBlock
1400 * @param expirationTime when the KBlock expires
1401 * @param anonymity anonymity level for the KBlock
1402 * @param priority priority for the KBlock
1403 * @param options publication options
1404 * @param cont continuation
1405 * @param cont_cls closure for cont
1408 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1409 const struct GNUNET_FS_Uri *ksk_uri,
1410 const struct GNUNET_CONTAINER_MetaData *meta,
1411 const struct GNUNET_FS_Uri *uri,
1412 struct GNUNET_TIME_Absolute expirationTime,
1415 enum GNUNET_FS_PublishOptions options,
1416 GNUNET_FS_PublishContinuation cont,
1419 struct PublishKskContext *pkc;
1425 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1427 pkc->expirationTime = expirationTime;
1428 pkc->anonymity = anonymity;
1429 pkc->priority = priority;
1431 pkc->cont_cls = cont_cls;
1432 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1434 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1436 if (pkc->dsh == NULL)
1438 cont (cont_cls, NULL, _("Could not connect to datastore."));
1446 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1447 GNUNET_assert (pkc->mdsize >= 0);
1448 uris = GNUNET_FS_uri_to_string (uri);
1449 pkc->slen = strlen (uris) + 1;
1450 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1451 if (size > MAX_KBLOCK_SIZE)
1453 size = MAX_KBLOCK_SIZE;
1454 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1456 pkc->kb = GNUNET_malloc (size);
1457 kbe = (char *) &pkc->kb[1];
1458 memcpy (kbe, uris, pkc->slen);
1460 sptr = &kbe[pkc->slen];
1462 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1465 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1466 if (pkc->mdsize == -1)
1469 GNUNET_free (pkc->kb);
1470 if (pkc->dsh != NULL)
1472 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1475 cont (cont_cls, NULL, _("Internal error."));
1479 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1481 pkc->cpy = GNUNET_malloc (size);
1482 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1483 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1486 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1487 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1488 GNUNET_SCHEDULER_add_continuation (h->sched,
1491 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1496 * Context for the SKS publication.
1498 struct PublishSksContext
1502 * Global FS context.
1504 struct GNUNET_FS_Uri *uri;
1507 * Handle to the datastore.
1509 struct GNUNET_DATASTORE_Handle *dsh;
1512 * Function to call once we're done.
1514 GNUNET_FS_PublishContinuation cont;
1525 * Function called by the datastore API with
1526 * the result from the PUT (SBlock) request.
1528 * @param cls closure of type "struct PublishSksContext*"
1529 * @param success GNUNET_OK on success
1530 * @param msg error message (or NULL)
1533 sb_put_cont (void *cls,
1537 struct PublishSksContext *psc = cls;
1539 if (NULL != psc->dsh)
1541 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1544 if (GNUNET_OK != success)
1545 psc->cont (psc->cont_cls,
1549 psc->cont (psc->cont_cls,
1552 GNUNET_FS_uri_destroy (psc->uri);
1558 * Publish an SBlock on GNUnet.
1560 * @param h handle to the file sharing subsystem
1561 * @param namespace namespace to publish in
1562 * @param identifier identifier to use
1563 * @param update update identifier to use
1564 * @param meta metadata to use
1565 * @param uri URI to refer to in the SBlock
1566 * @param expirationTime when the SBlock expires
1567 * @param anonymity anonymity level for the SBlock
1568 * @param priority priority for the SBlock
1569 * @param options publication options
1570 * @param cont continuation
1571 * @param cont_cls closure for cont
1574 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1575 struct GNUNET_FS_Namespace *namespace,
1576 const char *identifier,
1578 const struct GNUNET_CONTAINER_MetaData *meta,
1579 const struct GNUNET_FS_Uri *uri,
1580 struct GNUNET_TIME_Absolute expirationTime,
1583 enum GNUNET_FS_PublishOptions options,
1584 GNUNET_FS_PublishContinuation cont,
1587 struct PublishSksContext *psc;
1588 struct GNUNET_CRYPTO_AesSessionKey sk;
1589 struct GNUNET_CRYPTO_AesInitializationVector iv;
1590 struct GNUNET_FS_Uri *sks_uri;
1598 struct SBlock *sb_enc;
1600 struct GNUNET_CONTAINER_MetaData *mmeta;
1601 GNUNET_HashCode key; /* hash of thisId = key */
1602 GNUNET_HashCode id; /* hash of hc = identifier */
1603 GNUNET_HashCode query; /* id ^ nsid = DB query */
1606 mmeta = GNUNET_CONTAINER_meta_data_create ();
1608 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1609 uris = GNUNET_FS_uri_to_string (uri);
1610 slen = strlen (uris) + 1;
1611 idlen = strlen (identifier);
1614 nidlen = strlen (update) + 1;
1615 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1616 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1617 if (size > MAX_SBLOCK_SIZE)
1619 size = MAX_SBLOCK_SIZE;
1620 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1622 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1623 dest = (char *) &sb[1];
1624 memcpy (dest, update, nidlen);
1626 memcpy (dest, uris, slen);
1629 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1632 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1633 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1640 _("Internal error."));
1643 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1644 sb_enc = GNUNET_malloc (size);
1645 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1646 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1647 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1648 sks_uri->type = sks;
1649 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1650 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1651 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1652 &sks_uri->data.sks.namespace);
1653 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1654 GNUNET_CRYPTO_hash_xor (&id,
1655 &sks_uri->data.sks.namespace,
1656 &sb_enc->identifier);
1657 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1658 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1659 size - sizeof (struct SBlock),
1663 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1664 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1665 + sizeof(struct SBlock)
1666 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1667 GNUNET_assert (GNUNET_OK ==
1668 GNUNET_CRYPTO_rsa_sign (namespace->key,
1670 &sb_enc->signature));
1671 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1674 psc->cont_cls = cont_cls;
1675 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1677 GNUNET_free (sb_enc);
1684 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1685 if (NULL == psc->dsh)
1687 GNUNET_free (sb_enc);
1691 _("Failed to connect to datastore."));
1694 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1697 GNUNET_DATASTORE_put (psc->dsh,
1699 &sb_enc->identifier,
1702 GNUNET_BLOCK_TYPE_SBLOCK,
1707 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1711 GNUNET_free (sb_enc);
1714 /* end of fs_publish.c */