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 GNUNET_assert (fn != NULL);
729 slen = strlen (fn) + 1;
730 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
733 _("Can not index file `%s': %s. Will try to insert instead.\n"),
735 _("filename too long"));
737 p->data.file.do_index = GNUNET_NO;
738 GNUNET_FS_file_information_sync_ (p);
739 publish_content (sc);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Hash of indexed file `%s' is `%s'\n",
745 p->data.file.filename,
748 client = GNUNET_CLIENT_connect (sc->h->sched,
753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
754 _("Can not index file `%s': %s. Will try to insert instead.\n"),
756 _("could not connect to `fs' service"));
757 p->data.file.do_index = GNUNET_NO;
758 publish_content (sc);
762 if (p->data.file.have_hash != GNUNET_YES)
764 p->data.file.file_id = *res;
765 p->data.file.have_hash = GNUNET_YES;
766 GNUNET_FS_file_information_sync_ (p);
768 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
770 ism->header.size = htons(sizeof(struct IndexStartMessage) +
772 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
774 GNUNET_DISK_file_get_identifiers (p->filename,
778 ism->device = htonl (dev);
779 ism->inode = GNUNET_htonll(ino);
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 _("Failed to get file identifiers for `%s'\n"),
793 GNUNET_break (GNUNET_YES ==
794 GNUNET_CLIENT_transmit_and_get_response (client,
796 GNUNET_TIME_UNIT_FOREVER_REL,
798 &process_index_start_response,
805 * Main function that performs the upload.
807 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
808 * @param tc task context
811 GNUNET_FS_publish_main_ (void *cls,
812 const struct GNUNET_SCHEDULER_TaskContext *tc)
814 struct GNUNET_FS_PublishContext *pc = cls;
815 struct GNUNET_FS_ProgressInfo pi;
816 struct GNUNET_FS_FileInformation *p;
819 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
823 /* upload of entire hierarchy complete,
824 publish namespace entries */
825 GNUNET_FS_publish_sync_ (pc);
829 /* find starting position */
830 while ( (p->is_directory) &&
831 (NULL != p->data.dir.entries) &&
833 (NULL == p->data.dir.entries->chk_uri) )
835 p = p->data.dir.entries;
837 GNUNET_FS_publish_sync_ (pc);
842 /* error with current file, abort all
843 related files as well! */
844 while (NULL != p->dir)
846 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
847 EXTRACTOR_METATYPE_FILENAME);
851 GNUNET_asprintf (&p->emsg,
852 _("Recursive upload failed at `%s': %s"),
859 GNUNET_asprintf (&p->emsg,
860 _("Recursive upload failed: %s"),
863 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
864 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
865 pi.value.publish.specifics.error.message = p->emsg;
866 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
868 pc->all_done = GNUNET_YES;
869 GNUNET_FS_publish_sync_ (pc);
872 /* handle completion */
873 if (NULL != p->chk_uri)
875 GNUNET_FS_publish_sync_ (pc);
876 /* upload of "p" complete, publish KBlocks! */
877 if (p->keywords != NULL)
879 GNUNET_FS_publish_ksk (pc->h,
887 &publish_kblocks_cont,
892 publish_kblocks_cont (pc,
898 if ( (!p->is_directory) &&
899 (p->data.file.do_index) )
901 if (NULL == p->filename)
903 p->data.file.do_index = GNUNET_NO;
904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
905 _("Can not index file `%s': %s. Will try to insert instead.\n"),
907 _("needs to be an actual file"));
908 GNUNET_FS_file_information_sync_ (p);
909 publish_content (pc);
912 if (p->data.file.have_hash)
914 hash_for_index_cb (pc,
915 &p->data.file.file_id);
919 p->start_time = GNUNET_TIME_absolute_get ();
920 GNUNET_CRYPTO_hash_file (pc->h->sched,
921 GNUNET_SCHEDULER_PRIORITY_IDLE,
929 publish_content (pc);
934 * Signal the FS's progress function that we are starting
937 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
938 * @param fi the entry in the publish-structure
939 * @param length length of the file or directory
940 * @param meta metadata for the file or directory (can be modified)
941 * @param uri pointer to the keywords that will be used for this entry (can be modified)
942 * @param anonymity pointer to selected anonymity level (can be modified)
943 * @param priority pointer to selected priority (can be modified)
944 * @param do_index should we index?
945 * @param expirationTime pointer to selected expiration time (can be modified)
946 * @param client_info pointer to client context set upon creation (can be modified)
947 * @return GNUNET_OK to continue (always)
950 fip_signal_start(void *cls,
951 struct GNUNET_FS_FileInformation *fi,
953 struct GNUNET_CONTAINER_MetaData *meta,
954 struct GNUNET_FS_Uri **uri,
958 struct GNUNET_TIME_Absolute *expirationTime,
961 struct GNUNET_FS_PublishContext *sc = cls;
962 struct GNUNET_FS_ProgressInfo pi;
964 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
965 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
966 GNUNET_FS_file_information_sync_ (fi);
972 * Signal the FS's progress function that we are suspending
975 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
976 * @param fi the entry in the publish-structure
977 * @param length length of the file or directory
978 * @param meta metadata for the file or directory (can be modified)
979 * @param uri pointer to the keywords that will be used for this entry (can be modified)
980 * @param anonymity pointer to selected anonymity level (can be modified)
981 * @param priority pointer to selected priority (can be modified)
982 * @param do_index should we index?
983 * @param expirationTime pointer to selected expiration time (can be modified)
984 * @param client_info pointer to client context set upon creation (can be modified)
985 * @return GNUNET_OK to continue (always)
988 fip_signal_suspend(void *cls,
989 struct GNUNET_FS_FileInformation *fi,
991 struct GNUNET_CONTAINER_MetaData *meta,
992 struct GNUNET_FS_Uri **uri,
996 struct GNUNET_TIME_Absolute *expirationTime,
999 struct GNUNET_FS_PublishContext*sc = cls;
1000 struct GNUNET_FS_ProgressInfo pi;
1003 GNUNET_free_non_null (fi->serialization);
1004 fi->serialization = NULL;
1005 off = (fi->chk_uri == NULL) ? 0 : length;
1006 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1007 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1008 *client_info = NULL;
1009 if (NULL != sc->dsh)
1011 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
1019 * Create SUSPEND event for the given publish operation
1020 * and then clean up our state (without stop signal).
1022 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1025 GNUNET_FS_publish_signal_suspend_ (void *cls)
1027 struct GNUNET_FS_PublishContext *pc = cls;
1029 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1031 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1032 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1034 GNUNET_FS_file_information_inspect (pc->fi,
1035 &fip_signal_suspend,
1037 GNUNET_FS_end_top (pc->h, pc->top);
1038 publish_cleanup (pc, NULL);
1043 * Publish a file or directory.
1045 * @param h handle to the file sharing subsystem
1046 * @param fi information about the file or directory structure to publish
1047 * @param namespace namespace to publish the file in, NULL for no namespace
1048 * @param nid identifier to use for the publishd content in the namespace
1049 * (can be NULL, must be NULL if namespace is NULL)
1050 * @param nuid update-identifier that will be used for future updates
1051 * (can be NULL, must be NULL if namespace or nid is NULL)
1052 * @param options options for the publication
1053 * @return context that can be used to control the publish operation
1055 struct GNUNET_FS_PublishContext *
1056 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1057 struct GNUNET_FS_FileInformation *fi,
1058 struct GNUNET_FS_Namespace *namespace,
1061 enum GNUNET_FS_PublishOptions options)
1063 struct GNUNET_FS_PublishContext *ret;
1064 struct GNUNET_DATASTORE_Handle *dsh;
1066 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1068 dsh = GNUNET_DATASTORE_connect (h->cfg,
1077 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1081 ret->namespace = namespace;
1082 if (namespace != NULL)
1085 GNUNET_assert (NULL != nid);
1086 ret->nid = GNUNET_strdup (nid);
1088 ret->nuid = GNUNET_strdup (nuid);
1091 GNUNET_FS_file_information_inspect (ret->fi,
1094 ret->fi_pos = ret->fi;
1095 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1096 GNUNET_FS_publish_sync_ (ret);
1097 // FIXME: calculate space needed for "fi"
1098 // and reserve as first task (then trigger
1099 // "publish_main" from that continuation)!
1101 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1102 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1103 &GNUNET_FS_publish_main_,
1110 * Signal the FS's progress function that we are stopping
1113 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1114 * @param fi the entry in the publish-structure
1115 * @param length length of the file or directory
1116 * @param meta metadata for the file or directory (can be modified)
1117 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1118 * @param anonymity pointer to selected anonymity level (can be modified)
1119 * @param priority pointer to selected priority (can be modified)
1120 * @param do_index should we index?
1121 * @param expirationTime pointer to selected expiration time (can be modified)
1122 * @param client_info pointer to client context set upon creation (can be modified)
1123 * @return GNUNET_OK to continue (always)
1126 fip_signal_stop(void *cls,
1127 struct GNUNET_FS_FileInformation *fi,
1129 struct GNUNET_CONTAINER_MetaData *meta,
1130 struct GNUNET_FS_Uri **uri,
1131 uint32_t *anonymity,
1134 struct GNUNET_TIME_Absolute *expirationTime,
1137 struct GNUNET_FS_PublishContext*sc = cls;
1138 struct GNUNET_FS_ProgressInfo pi;
1141 if (fi->serialization != NULL)
1143 GNUNET_FS_remove_sync_file_ (sc->h,
1144 GNUNET_FS_SYNC_PATH_FILE_INFO,
1146 GNUNET_free (fi->serialization);
1147 fi->serialization = NULL;
1149 off = (fi->chk_uri == NULL) ? 0 : length;
1150 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1151 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1152 *client_info = NULL;
1158 * Stop an upload. Will abort incomplete uploads (but
1159 * not remove blocks that have already been publishd) or
1160 * simply clean up the state for completed uploads.
1161 * Must NOT be called from within the event callback!
1163 * @param pc context for the upload to stop
1166 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1168 GNUNET_FS_end_top (pc->h, pc->top);
1169 if (NULL != pc->dsh)
1171 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1174 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1176 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1177 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1179 if (pc->serialization != NULL)
1181 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1182 GNUNET_free (pc->serialization);
1183 pc->serialization = NULL;
1185 GNUNET_FS_file_information_inspect (pc->fi,
1188 if (GNUNET_YES == pc->in_network_wait)
1190 pc->in_network_wait = GNUNET_SYSERR;
1193 publish_cleanup (pc, NULL);
1198 * Context for the KSK publication.
1200 struct PublishKskContext
1206 struct GNUNET_FS_Uri *ksk_uri;
1209 * Global FS context.
1211 struct GNUNET_FS_Handle *h;
1214 * The master block that we are sending
1215 * (in plaintext), has "mdsize+slen" more
1216 * bytes than the struct would suggest.
1221 * Buffer of the same size as "kb" for
1222 * the encrypted version.
1227 * Handle to the datastore, NULL if we are just
1230 struct GNUNET_DATASTORE_Handle *dsh;
1233 * Function to call once we're done.
1235 GNUNET_FS_PublishContinuation cont;
1243 * When should the KBlocks expire?
1245 struct GNUNET_TIME_Absolute expirationTime;
1248 * Size of the serialized metadata.
1253 * Size of the (CHK) URI as a string.
1258 * Keyword that we are currently processing.
1263 * Anonymity level for the KBlocks.
1268 * Priority for the KBlocks.
1275 * Continuation of "GNUNET_FS_publish_ksk" that performs
1276 * the actual publishing operation (iterating over all
1279 * @param cls closure of type "struct PublishKskContext*"
1283 publish_ksk_cont (void *cls,
1284 const struct GNUNET_SCHEDULER_TaskContext *tc);
1288 * Function called by the datastore API with
1289 * the result from the PUT request.
1291 * @param cls closure of type "struct PublishKskContext*"
1292 * @param success GNUNET_OK on success
1293 * @param msg error message (or NULL)
1296 kb_put_cont (void *cls,
1300 struct PublishKskContext *pkc = cls;
1302 if (GNUNET_OK != success)
1304 if (NULL != pkc->dsh)
1306 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1309 GNUNET_free (pkc->cpy);
1310 GNUNET_free (pkc->kb);
1311 pkc->cont (pkc->cont_cls,
1314 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1318 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1321 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1326 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1327 * publishing operation (iterating over all of the keywords).
1329 * @param cls closure of type "struct PublishKskContext*"
1333 publish_ksk_cont (void *cls,
1334 const struct GNUNET_SCHEDULER_TaskContext *tc)
1336 struct PublishKskContext *pkc = cls;
1337 const char *keyword;
1338 GNUNET_HashCode key;
1339 GNUNET_HashCode query;
1340 struct GNUNET_CRYPTO_AesSessionKey skey;
1341 struct GNUNET_CRYPTO_AesInitializationVector iv;
1342 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1345 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1346 (NULL == pkc->dsh) )
1348 if (NULL != pkc->dsh)
1350 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1353 GNUNET_free (pkc->cpy);
1354 GNUNET_free (pkc->kb);
1355 pkc->cont (pkc->cont_cls,
1358 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1362 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1363 /* first character of keyword indicates if it is
1364 mandatory or not -- ignore for hashing */
1365 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1366 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1367 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1368 pkc->slen + pkc->mdsize,
1372 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1373 GNUNET_assert (NULL != pk);
1374 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1375 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1376 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1378 GNUNET_assert (GNUNET_OK ==
1379 GNUNET_CRYPTO_rsa_sign (pk,
1381 &pkc->cpy->signature));
1382 GNUNET_CRYPTO_rsa_key_free (pk);
1383 GNUNET_DATASTORE_put (pkc->dsh,
1387 sizeof (struct KBlock) +
1390 GNUNET_BLOCK_TYPE_KBLOCK,
1393 pkc->expirationTime,
1395 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1402 * Publish a CHK under various keywords on GNUnet.
1404 * @param h handle to the file sharing subsystem
1405 * @param ksk_uri keywords to use
1406 * @param meta metadata to use
1407 * @param uri URI to refer to in the KBlock
1408 * @param expirationTime when the KBlock expires
1409 * @param anonymity anonymity level for the KBlock
1410 * @param priority priority for the KBlock
1411 * @param options publication options
1412 * @param cont continuation
1413 * @param cont_cls closure for cont
1416 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1417 const struct GNUNET_FS_Uri *ksk_uri,
1418 const struct GNUNET_CONTAINER_MetaData *meta,
1419 const struct GNUNET_FS_Uri *uri,
1420 struct GNUNET_TIME_Absolute expirationTime,
1423 enum GNUNET_FS_PublishOptions options,
1424 GNUNET_FS_PublishContinuation cont,
1427 struct PublishKskContext *pkc;
1433 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1435 pkc->expirationTime = expirationTime;
1436 pkc->anonymity = anonymity;
1437 pkc->priority = priority;
1439 pkc->cont_cls = cont_cls;
1440 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1442 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1444 if (pkc->dsh == NULL)
1446 cont (cont_cls, NULL, _("Could not connect to datastore."));
1454 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1455 GNUNET_assert (pkc->mdsize >= 0);
1456 uris = GNUNET_FS_uri_to_string (uri);
1457 pkc->slen = strlen (uris) + 1;
1458 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1459 if (size > MAX_KBLOCK_SIZE)
1461 size = MAX_KBLOCK_SIZE;
1462 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1464 pkc->kb = GNUNET_malloc (size);
1465 kbe = (char *) &pkc->kb[1];
1466 memcpy (kbe, uris, pkc->slen);
1468 sptr = &kbe[pkc->slen];
1470 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1473 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1474 if (pkc->mdsize == -1)
1477 GNUNET_free (pkc->kb);
1478 if (pkc->dsh != NULL)
1480 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1483 cont (cont_cls, NULL, _("Internal error."));
1487 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1489 pkc->cpy = GNUNET_malloc (size);
1490 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1491 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1494 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1495 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1496 GNUNET_SCHEDULER_add_continuation (h->sched,
1499 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1504 * Context for the SKS publication.
1506 struct PublishSksContext
1510 * Global FS context.
1512 struct GNUNET_FS_Uri *uri;
1515 * Handle to the datastore.
1517 struct GNUNET_DATASTORE_Handle *dsh;
1520 * Function to call once we're done.
1522 GNUNET_FS_PublishContinuation cont;
1533 * Function called by the datastore API with
1534 * the result from the PUT (SBlock) request.
1536 * @param cls closure of type "struct PublishSksContext*"
1537 * @param success GNUNET_OK on success
1538 * @param msg error message (or NULL)
1541 sb_put_cont (void *cls,
1545 struct PublishSksContext *psc = cls;
1547 if (NULL != psc->dsh)
1549 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1552 if (GNUNET_OK != success)
1553 psc->cont (psc->cont_cls,
1557 psc->cont (psc->cont_cls,
1560 GNUNET_FS_uri_destroy (psc->uri);
1566 * Publish an SBlock on GNUnet.
1568 * @param h handle to the file sharing subsystem
1569 * @param namespace namespace to publish in
1570 * @param identifier identifier to use
1571 * @param update update identifier to use
1572 * @param meta metadata to use
1573 * @param uri URI to refer to in the SBlock
1574 * @param expirationTime when the SBlock expires
1575 * @param anonymity anonymity level for the SBlock
1576 * @param priority priority for the SBlock
1577 * @param options publication options
1578 * @param cont continuation
1579 * @param cont_cls closure for cont
1582 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1583 struct GNUNET_FS_Namespace *namespace,
1584 const char *identifier,
1586 const struct GNUNET_CONTAINER_MetaData *meta,
1587 const struct GNUNET_FS_Uri *uri,
1588 struct GNUNET_TIME_Absolute expirationTime,
1591 enum GNUNET_FS_PublishOptions options,
1592 GNUNET_FS_PublishContinuation cont,
1595 struct PublishSksContext *psc;
1596 struct GNUNET_CRYPTO_AesSessionKey sk;
1597 struct GNUNET_CRYPTO_AesInitializationVector iv;
1598 struct GNUNET_FS_Uri *sks_uri;
1606 struct SBlock *sb_enc;
1608 struct GNUNET_CONTAINER_MetaData *mmeta;
1609 GNUNET_HashCode key; /* hash of thisId = key */
1610 GNUNET_HashCode id; /* hash of hc = identifier */
1611 GNUNET_HashCode query; /* id ^ nsid = DB query */
1614 mmeta = GNUNET_CONTAINER_meta_data_create ();
1616 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1617 uris = GNUNET_FS_uri_to_string (uri);
1618 slen = strlen (uris) + 1;
1619 idlen = strlen (identifier);
1622 nidlen = strlen (update) + 1;
1623 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1624 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1625 if (size > MAX_SBLOCK_SIZE)
1627 size = MAX_SBLOCK_SIZE;
1628 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1630 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1631 dest = (char *) &sb[1];
1632 memcpy (dest, update, nidlen);
1634 memcpy (dest, uris, slen);
1637 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1640 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1641 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1648 _("Internal error."));
1651 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1652 sb_enc = GNUNET_malloc (size);
1653 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1654 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1655 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1656 sks_uri->type = sks;
1657 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1658 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1659 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1660 &sks_uri->data.sks.namespace);
1661 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1662 GNUNET_CRYPTO_hash_xor (&id,
1663 &sks_uri->data.sks.namespace,
1664 &sb_enc->identifier);
1665 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1666 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1667 size - sizeof (struct SBlock),
1671 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1672 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1673 + sizeof(struct SBlock)
1674 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1675 GNUNET_assert (GNUNET_OK ==
1676 GNUNET_CRYPTO_rsa_sign (namespace->key,
1678 &sb_enc->signature));
1679 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1682 psc->cont_cls = cont_cls;
1683 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1685 GNUNET_free (sb_enc);
1692 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1693 if (NULL == psc->dsh)
1695 GNUNET_free (sb_enc);
1699 _("Failed to connect to datastore."));
1702 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1705 GNUNET_DATASTORE_put (psc->dsh,
1707 &sb_enc->identifier,
1710 GNUNET_BLOCK_TYPE_SBLOCK,
1715 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1719 GNUNET_free (sb_enc);
1722 /* end of fs_publish.c */