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.php3
25 * @author Krista Bennett
26 * @author Christian Grothoff
29 * - indexing cleanup: unindex on failure (can wait)
30 * - persistence support (can wait)
31 * - datastore reservation support (optimization)
32 * - location URIs (publish with anonymity-level zero)
36 #include "gnunet_constants.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_fs_service.h"
43 #define DEBUG_PUBLISH GNUNET_NO
47 * Context for "ds_put_cont".
52 * Current publishing context.
54 struct GNUNET_FS_PublishContext *sc;
57 * Specific file with the block.
59 struct GNUNET_FS_FileInformation *p;
62 * Function to run next, if any (can be NULL).
64 GNUNET_SCHEDULER_Task cont;
74 * Fill in all of the generic fields for
75 * a publish event and call the callback.
77 * @param pi structure to fill in
78 * @param sc overall publishing context
79 * @param p file information for the file being published
80 * @param offset where in the file are we so far
81 * @return value returned from callback
84 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
85 struct GNUNET_FS_PublishContext *sc,
86 const struct GNUNET_FS_FileInformation *p,
89 pi->value.publish.sc = sc;
90 pi->value.publish.fi = p;
91 pi->value.publish.cctx
93 pi->value.publish.pctx
94 = (NULL == p->dir) ? NULL : p->dir->client_info;
95 pi->value.publish.filename = p->filename;
96 pi->value.publish.size
97 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
99 = GNUNET_TIME_calculate_eta (p->start_time,
101 pi->value.publish.size);
102 pi->value.publish.completed = offset;
103 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
104 pi->value.publish.anonymity = p->anonymity;
105 return sc->h->upcb (sc->h->upcb_cls,
111 * Cleanup the publish context, we're done with it.
113 * @param pc struct to clean up after
116 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
118 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
119 if (pc->namespace != NULL)
120 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
121 GNUNET_free_non_null (pc->nid);
122 GNUNET_free_non_null (pc->nuid);
123 GNUNET_free_non_null (pc->serialization);
124 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
125 if (pc->client != NULL)
126 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
132 * Function called by the datastore API with
133 * the result from the PUT request.
135 * @param cls our closure
136 * @param success GNUNET_OK on success
137 * @param msg error message (or NULL)
140 ds_put_cont (void *cls,
144 struct PutContCtx *pcc = cls;
145 struct GNUNET_FS_ProgressInfo pi;
147 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
149 /* we were aborted in the meantime,
151 publish_cleanup (pcc->sc);
154 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
155 pcc->sc->in_network_wait = GNUNET_NO;
156 if (GNUNET_OK != success)
158 GNUNET_asprintf (&pcc->p->emsg,
159 _("Upload failed: %s"),
161 GNUNET_FS_file_information_sync (pcc->p);
162 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
163 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
164 pi.value.publish.specifics.error.message = pcc->p->emsg;
165 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
167 GNUNET_FS_file_information_sync (pcc->p);
168 if (NULL != pcc->cont)
170 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
171 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
179 * Generate the callback that signals clients
180 * that a file (or directory) has been completely
183 * @param p the completed upload
184 * @param sc context of the publication
187 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
188 struct GNUNET_FS_PublishContext *sc)
190 struct GNUNET_FS_ProgressInfo pi;
192 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
193 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
194 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
195 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
196 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
201 * Generate the callback that signals clients
202 * that a file (or directory) has encountered
203 * a problem during publication.
205 * @param p the upload that had trouble
206 * @param sc context of the publication
207 * @param emsg error message
210 signal_publish_error (struct GNUNET_FS_FileInformation *p,
211 struct GNUNET_FS_PublishContext *sc,
214 struct GNUNET_FS_ProgressInfo pi;
216 p->emsg = GNUNET_strdup (emsg);
217 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
218 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
219 pi.value.publish.specifics.error.message =emsg;
220 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
225 * We've finished publishing the SBlock as part of a larger upload.
226 * Check the result and complete the larger upload.
228 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
229 * @param uri URI of the published SBlock
230 * @param emsg NULL on success, otherwise error message
233 publish_sblocks_cont (void *cls,
234 const struct GNUNET_FS_Uri *uri,
237 struct GNUNET_FS_PublishContext *sc = cls;
240 signal_publish_error (sc->fi,
245 // FIXME: release the datastore reserve here!
246 signal_publish_completion (sc->fi, sc);
247 sc->all_done = GNUNET_YES;
252 * We are almost done publishing the structure,
253 * add SBlocks (if needed).
255 * @param sc overall upload data
258 publish_sblock (struct GNUNET_FS_PublishContext *sc)
260 if (NULL != sc->namespace)
261 GNUNET_FS_publish_sks (sc->h,
267 sc->fi->expirationTime,
271 &publish_sblocks_cont,
274 publish_sblocks_cont (sc, NULL, NULL);
279 * We've finished publishing a KBlock as part of a larger upload.
280 * Check the result and continue the larger upload.
282 * @param cls the "struct GNUNET_FS_PublishContext*"
283 * of the larger upload
284 * @param uri URI of the published blocks
285 * @param emsg NULL on success, otherwise error message
288 publish_kblocks_cont (void *cls,
289 const struct GNUNET_FS_Uri *uri,
292 struct GNUNET_FS_PublishContext *sc = cls;
293 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
297 signal_publish_error (p, sc, emsg);
299 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
300 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
301 &GNUNET_FS_publish_main_,
305 GNUNET_FS_file_information_sync (p);
307 signal_publish_completion (p, sc);
308 /* move on to next file */
310 sc->fi_pos = p->next;
314 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
315 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
316 &GNUNET_FS_publish_main_,
322 * Function called by the tree encoder to obtain
323 * a block of plaintext data (for the lowest level
326 * @param cls our publishing context
327 * @param offset identifies which block to get
328 * @param max (maximum) number of bytes to get; returning
329 * fewer will also cause errors
330 * @param buf where to copy the plaintext buffer
331 * @param emsg location to store an error message (on error)
332 * @return number of bytes copied to buf, 0 on error
335 block_reader (void *cls,
341 struct GNUNET_FS_PublishContext *sc = cls;
342 struct GNUNET_FS_FileInformation *p;
349 pt_size = GNUNET_MIN(max,
350 p->data.dir.dir_size - offset);
351 dd = p->data.dir.dir_data;
358 pt_size = GNUNET_MIN(max,
359 p->data.file.file_size - offset);
361 return 0; /* calling reader with pt_size==0
362 might free buf, so don't! */
364 p->data.file.reader (p->data.file.reader_cls,
376 * The tree encoder has finished processing a
377 * file. Call it's finish method and deal with
380 * @param cls our publishing context
381 * @param tc scheduler's task context (not used)
384 encode_cont (void *cls,
385 const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct GNUNET_FS_PublishContext *sc = cls;
388 struct GNUNET_FS_FileInformation *p;
389 struct GNUNET_FS_ProgressInfo pi;
393 GNUNET_FS_tree_encoder_finish (p->te,
399 GNUNET_asprintf (&p->emsg,
400 _("Upload failed: %s"),
403 GNUNET_FS_file_information_sync (p);
404 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
405 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
406 pi.value.publish.specifics.error.message = p->emsg;
407 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
409 /* continue with main */
411 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
412 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
413 &GNUNET_FS_publish_main_,
419 * Function called asking for the current (encoded)
420 * block to be processed. After processing the
421 * client should either call "GNUNET_FS_tree_encode_next"
422 * or (on error) "GNUNET_FS_tree_encode_finish".
425 * @param query the query for the block (key for lookup in the datastore)
426 * @param offset offset of the block in the file
427 * @param type type of the block (IBLOCK or DBLOCK)
428 * @param block the (encrypted) block
429 * @param block_size size of block (in bytes)
432 block_proc (void *cls,
433 const GNUNET_HashCode *query,
435 enum GNUNET_BLOCK_Type type,
439 struct GNUNET_FS_PublishContext *sc = cls;
440 struct GNUNET_FS_FileInformation *p;
441 struct PutContCtx * dpc_cls;
442 struct OnDemandBlock odb;
448 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
449 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
450 &GNUNET_FS_publish_main_,
455 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
456 sc->in_network_wait = GNUNET_YES;
457 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
458 dpc_cls->cont = &GNUNET_FS_publish_main_;
459 dpc_cls->cont_cls = sc;
462 if ( (! p->is_directory) &&
463 (GNUNET_YES == p->data.file.do_index) &&
464 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Indexing block `%s' for offset %llu with index size %u\n",
470 (unsigned long long) offset,
471 sizeof (struct OnDemandBlock));
473 odb.offset = GNUNET_htonll (offset);
474 odb.file_id = p->data.file.file_id;
475 GNUNET_DATASTORE_put (sc->dsh,
478 sizeof(struct OnDemandBlock),
480 GNUNET_BLOCK_TYPE_ONDEMAND,
484 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Publishing block `%s' for offset %llu with size %u\n",
493 (unsigned long long) offset,
494 (unsigned int) block_size);
496 GNUNET_DATASTORE_put (sc->dsh,
505 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
512 * Function called with information about our
513 * progress in computing the tree encoding.
516 * @param offset where are we in the file
517 * @param pt_block plaintext of the currently processed block
518 * @param pt_size size of pt_block
519 * @param depth depth of the block in the tree
522 progress_proc (void *cls,
524 const void *pt_block,
528 struct GNUNET_FS_PublishContext *sc = cls;
529 struct GNUNET_FS_FileInformation *p;
530 struct GNUNET_FS_ProgressInfo pi;
533 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
534 pi.value.publish.specifics.progress.data = pt_block;
535 pi.value.publish.specifics.progress.offset = offset;
536 pi.value.publish.specifics.progress.data_len = pt_size;
537 pi.value.publish.specifics.progress.depth = depth;
538 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
543 * We are uploading a file or directory; load (if necessary) the next
544 * block into memory, encrypt it and send it to the FS service. Then
545 * continue with the main task.
547 * @param sc overall upload data
550 publish_content (struct GNUNET_FS_PublishContext *sc)
552 struct GNUNET_FS_FileInformation *p;
554 struct GNUNET_FS_DirectoryBuilder *db;
555 struct GNUNET_FS_FileInformation *dirpos;
564 db = GNUNET_FS_directory_builder_create (p->meta);
565 dirpos = p->data.dir.entries;
566 while (NULL != dirpos)
568 if (dirpos->is_directory)
570 raw_data = dirpos->data.dir.dir_data;
571 dirpos->data.dir.dir_data = NULL;
576 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
577 (dirpos->data.file.file_size > 0) )
579 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
581 if (dirpos->data.file.file_size !=
582 dirpos->data.file.reader (dirpos->data.file.reader_cls,
584 dirpos->data.file.file_size,
588 GNUNET_free_non_null (emsg);
589 GNUNET_free (raw_data);
594 GNUNET_FS_directory_builder_add (db,
598 GNUNET_free_non_null (raw_data);
599 dirpos = dirpos->next;
601 GNUNET_FS_directory_builder_finish (db,
602 &p->data.dir.dir_size,
603 &p->data.dir.dir_data);
605 size = (p->is_directory)
606 ? p->data.dir.dir_size
607 : p->data.file.file_size;
608 p->te = GNUNET_FS_tree_encoder_create (sc->h,
617 GNUNET_FS_tree_encoder_next (p->te);
622 * Process the response (or lack thereof) from
623 * the "fs" service to our 'start index' request.
625 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
626 * @param msg the response we got
629 process_index_start_response (void *cls,
630 const struct GNUNET_MessageHeader *msg)
632 struct GNUNET_FS_PublishContext *sc = cls;
633 struct GNUNET_FS_FileInformation *p;
637 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
642 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
643 _("Can not index file `%s': %s. Will try to insert instead.\n"),
645 _("timeout on index-start request to `fs' service"));
646 p->data.file.do_index = GNUNET_NO;
647 publish_content (sc);
650 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
652 msize = ntohs (msg->size);
653 emsg = (const char *) &msg[1];
654 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
655 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
656 emsg = gettext_noop ("unknown error");
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 _("Can not index file `%s': %s. Will try to insert instead.\n"),
661 p->data.file.do_index = GNUNET_NO;
662 publish_content (sc);
665 p->data.file.index_start_confirmed = GNUNET_YES;
666 /* success! continue with indexing */
667 publish_content (sc);
672 * Function called once the hash computation over an
673 * indexed file has completed.
675 * @param cls closure, our publishing context
676 * @param res resulting hash, NULL on error
679 hash_for_index_cb (void *cls,
680 const GNUNET_HashCode *
683 struct GNUNET_FS_PublishContext *sc = cls;
684 struct GNUNET_FS_FileInformation *p;
685 struct IndexStartMessage *ism;
687 struct GNUNET_CLIENT_Connection *client;
695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
696 _("Can not index file `%s': %s. Will try to insert instead.\n"),
698 _("failed to compute hash"));
699 p->data.file.do_index = GNUNET_NO;
700 publish_content (sc);
703 if (GNUNET_YES == p->data.file.index_start_confirmed)
705 publish_content (sc);
708 fn = GNUNET_STRINGS_filename_expand (p->filename);
709 slen = strlen (fn) + 1;
710 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
713 _("Can not index file `%s': %s. Will try to insert instead.\n"),
715 _("filename too long"));
717 p->data.file.do_index = GNUNET_NO;
718 publish_content (sc);
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Hash of indexed file `%s' is `%s'\n",
724 p->data.file.filename,
727 client = GNUNET_CLIENT_connect (sc->h->sched,
732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
733 _("Can not index file `%s': %s. Will try to insert instead.\n"),
735 _("could not connect to `fs' service"));
736 p->data.file.do_index = GNUNET_NO;
737 publish_content (sc);
741 p->data.file.file_id = *res;
742 p->data.file.have_hash = GNUNET_YES;
743 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
745 ism->header.size = htons(sizeof(struct IndexStartMessage) +
747 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
749 GNUNET_DISK_file_get_identifiers (p->filename,
753 ism->device = htonl (dev);
754 ism->inode = GNUNET_htonll(ino);
758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
759 _("Failed to get file identifiers for `%s'\n"),
768 GNUNET_break (GNUNET_YES ==
769 GNUNET_CLIENT_transmit_and_get_response (client,
771 GNUNET_TIME_UNIT_FOREVER_REL,
773 &process_index_start_response,
780 * Main function that performs the upload.
782 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
783 * @param tc task context
786 GNUNET_FS_publish_main_ (void *cls,
787 const struct GNUNET_SCHEDULER_TaskContext *tc)
789 struct GNUNET_FS_PublishContext *sc = cls;
790 struct GNUNET_FS_ProgressInfo pi;
791 struct GNUNET_FS_FileInformation *p;
794 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
798 /* upload of entire hierarchy complete,
799 publish namespace entries */
803 /* find starting position */
804 while ( (p->is_directory) &&
805 (NULL != p->data.dir.entries) &&
807 (NULL == p->data.dir.entries->chk_uri) )
809 p = p->data.dir.entries;
815 /* error with current file, abort all
816 related files as well! */
817 while (NULL != p->dir)
819 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
820 EXTRACTOR_METATYPE_FILENAME);
824 GNUNET_asprintf (&p->emsg,
825 _("Recursive upload failed at `%s': %s"),
832 GNUNET_asprintf (&p->emsg,
833 _("Recursive upload failed: %s"),
836 GNUNET_FS_file_information_sync (p);
837 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
838 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
839 pi.value.publish.specifics.error.message = p->emsg;
840 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
842 sc->all_done = GNUNET_YES;
845 /* handle completion */
846 if (NULL != p->chk_uri)
848 /* upload of "p" complete, publish KBlocks! */
849 if (p->keywords != NULL)
851 GNUNET_FS_publish_ksk (sc->h,
859 &publish_kblocks_cont,
864 publish_kblocks_cont (sc,
870 if ( (!p->is_directory) &&
871 (p->data.file.do_index) )
873 if (NULL == p->filename)
875 p->data.file.do_index = GNUNET_NO;
876 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
877 _("Can not index file `%s': %s. Will try to insert instead.\n"),
879 _("needs to be an actual file"));
880 publish_content (sc);
883 if (p->data.file.have_hash)
884 hash_for_index_cb (sc,
885 &p->data.file.file_id);
888 p->start_time = GNUNET_TIME_absolute_get ();
889 GNUNET_CRYPTO_hash_file (sc->h->sched,
890 GNUNET_SCHEDULER_PRIORITY_IDLE,
898 publish_content (sc);
903 * Signal the FS's progress function that we are starting
906 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
907 * @param fi the entry in the publish-structure
908 * @param length length of the file or directory
909 * @param meta metadata for the file or directory (can be modified)
910 * @param uri pointer to the keywords that will be used for this entry (can be modified)
911 * @param anonymity pointer to selected anonymity level (can be modified)
912 * @param priority pointer to selected priority (can be modified)
913 * @param expirationTime pointer to selected expiration time (can be modified)
914 * @param client_info pointer to client context set upon creation (can be modified)
915 * @return GNUNET_OK to continue (always)
918 fip_signal_start(void *cls,
919 struct GNUNET_FS_FileInformation *fi,
921 struct GNUNET_CONTAINER_MetaData *meta,
922 struct GNUNET_FS_Uri **uri,
925 struct GNUNET_TIME_Absolute *expirationTime,
928 struct GNUNET_FS_PublishContext *sc = cls;
929 struct GNUNET_FS_ProgressInfo pi;
931 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
932 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
938 * Publish a file or directory.
940 * @param h handle to the file sharing subsystem
941 * @param fi information about the file or directory structure to publish
942 * @param namespace namespace to publish the file in, NULL for no namespace
943 * @param nid identifier to use for the publishd content in the namespace
944 * (can be NULL, must be NULL if namespace is NULL)
945 * @param nuid update-identifier that will be used for future updates
946 * (can be NULL, must be NULL if namespace or nid is NULL)
947 * @param options options for the publication
948 * @return context that can be used to control the publish operation
950 struct GNUNET_FS_PublishContext *
951 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
952 struct GNUNET_FS_FileInformation *fi,
953 struct GNUNET_FS_Namespace *namespace,
956 enum GNUNET_FS_PublishOptions options)
958 struct GNUNET_FS_PublishContext *ret;
959 struct GNUNET_DATASTORE_Handle *dsh;
961 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
963 dsh = GNUNET_DATASTORE_connect (h->cfg,
972 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
976 ret->namespace = namespace;
977 if (namespace != NULL)
980 GNUNET_assert (NULL != nid);
981 ret->nid = GNUNET_strdup (nid);
983 ret->nuid = GNUNET_strdup (nuid);
985 // FIXME: make upload persistent!
988 GNUNET_FS_file_information_inspect (ret->fi,
991 ret->fi_pos = ret->fi;
993 // FIXME: calculate space needed for "fi"
994 // and reserve as first task (then trigger
995 // "publish_main" from that continuation)!
997 = GNUNET_SCHEDULER_add_with_priority (h->sched,
998 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
999 &GNUNET_FS_publish_main_,
1006 * Signal the FS's progress function that we are stopping
1009 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1010 * @param fi the entry in the publish-structure
1011 * @param length length of the file or directory
1012 * @param meta metadata for the file or directory (can be modified)
1013 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1014 * @param anonymity pointer to selected anonymity level (can be modified)
1015 * @param priority pointer to selected priority (can be modified)
1016 * @param expirationTime pointer to selected expiration time (can be modified)
1017 * @param client_info pointer to client context set upon creation (can be modified)
1018 * @return GNUNET_OK to continue (always)
1021 fip_signal_stop(void *cls,
1022 struct GNUNET_FS_FileInformation *fi,
1024 struct GNUNET_CONTAINER_MetaData *meta,
1025 struct GNUNET_FS_Uri **uri,
1026 uint32_t *anonymity,
1028 struct GNUNET_TIME_Absolute *expirationTime,
1031 struct GNUNET_FS_PublishContext*sc = cls;
1032 struct GNUNET_FS_ProgressInfo pi;
1035 if (fi->serialization != NULL)
1037 if (0 != UNLINK (fi->serialization))
1039 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1043 GNUNET_free (fi->serialization);
1044 fi->serialization = NULL;
1046 off = (fi->chk_uri == NULL) ? 0 : length;
1047 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1048 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1049 *client_info = NULL;
1055 * Stop an upload. Will abort incomplete uploads (but
1056 * not remove blocks that have already been publishd) or
1057 * simply clean up the state for completed uploads.
1058 * Must NOT be called from within the event callback!
1060 * @param pc context for the upload to stop
1063 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1065 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1066 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1067 if (pc->serialization != NULL)
1069 if (0 != UNLINK (pc->serialization))
1070 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1073 GNUNET_free (pc->serialization);
1074 pc->serialization = NULL;
1076 GNUNET_FS_file_information_inspect (pc->fi,
1079 if (GNUNET_YES == pc->in_network_wait)
1081 pc->in_network_wait = GNUNET_SYSERR;
1084 publish_cleanup (pc);
1089 * Context for the KSK publication.
1091 struct PublishKskContext
1097 struct GNUNET_FS_Uri *ksk_uri;
1100 * Global FS context.
1102 struct GNUNET_FS_Handle *h;
1105 * The master block that we are sending
1106 * (in plaintext), has "mdsize+slen" more
1107 * bytes than the struct would suggest.
1112 * Buffer of the same size as "kb" for
1113 * the encrypted version.
1118 * Handle to the datastore, NULL if we are just
1121 struct GNUNET_DATASTORE_Handle *dsh;
1124 * Function to call once we're done.
1126 GNUNET_FS_PublishContinuation cont;
1134 * When should the KBlocks expire?
1136 struct GNUNET_TIME_Absolute expirationTime;
1139 * Size of the serialized metadata.
1144 * Size of the (CHK) URI as a string.
1149 * Keyword that we are currently processing.
1154 * Anonymity level for the KBlocks.
1159 * Priority for the KBlocks.
1166 * Continuation of "GNUNET_FS_publish_ksk" that performs
1167 * the actual publishing operation (iterating over all
1170 * @param cls closure of type "struct PublishKskContext*"
1174 publish_ksk_cont (void *cls,
1175 const struct GNUNET_SCHEDULER_TaskContext *tc);
1179 * Function called by the datastore API with
1180 * the result from the PUT request.
1182 * @param cls closure of type "struct PublishKskContext*"
1183 * @param success GNUNET_OK on success
1184 * @param msg error message (or NULL)
1187 kb_put_cont (void *cls,
1191 struct PublishKskContext *pkc = cls;
1193 if (GNUNET_OK != success)
1195 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1196 GNUNET_free (pkc->cpy);
1197 GNUNET_free (pkc->kb);
1198 pkc->cont (pkc->cont_cls,
1201 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1205 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1208 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1213 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1214 * publishing operation (iterating over all of the keywords).
1216 * @param cls closure of type "struct PublishKskContext*"
1220 publish_ksk_cont (void *cls,
1221 const struct GNUNET_SCHEDULER_TaskContext *tc)
1223 struct PublishKskContext *pkc = cls;
1224 const char *keyword;
1225 GNUNET_HashCode key;
1226 GNUNET_HashCode query;
1227 struct GNUNET_CRYPTO_AesSessionKey skey;
1228 struct GNUNET_CRYPTO_AesInitializationVector iv;
1229 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1232 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1233 (NULL == pkc->dsh) )
1235 if (NULL != pkc->dsh)
1236 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1237 GNUNET_free (pkc->cpy);
1238 GNUNET_free (pkc->kb);
1239 pkc->cont (pkc->cont_cls,
1242 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1246 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1247 /* first character of keyword indicates if it is
1248 mandatory or not -- ignore for hashing */
1249 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1250 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1251 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1252 pkc->slen + pkc->mdsize,
1256 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1257 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1258 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1259 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1261 GNUNET_assert (GNUNET_OK ==
1262 GNUNET_CRYPTO_rsa_sign (pk,
1264 &pkc->cpy->signature));
1265 GNUNET_CRYPTO_rsa_key_free (pk);
1266 GNUNET_DATASTORE_put (pkc->dsh,
1270 sizeof (struct KBlock) +
1273 GNUNET_BLOCK_TYPE_KBLOCK,
1276 pkc->expirationTime,
1277 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1284 * Publish a CHK under various keywords on GNUnet.
1286 * @param h handle to the file sharing subsystem
1287 * @param ksk_uri keywords to use
1288 * @param meta metadata to use
1289 * @param uri URI to refer to in the KBlock
1290 * @param expirationTime when the KBlock expires
1291 * @param anonymity anonymity level for the KBlock
1292 * @param priority priority for the KBlock
1293 * @param options publication options
1294 * @param cont continuation
1295 * @param cont_cls closure for cont
1298 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1299 const struct GNUNET_FS_Uri *ksk_uri,
1300 const struct GNUNET_CONTAINER_MetaData *meta,
1301 const struct GNUNET_FS_Uri *uri,
1302 struct GNUNET_TIME_Absolute expirationTime,
1305 enum GNUNET_FS_PublishOptions options,
1306 GNUNET_FS_PublishContinuation cont,
1309 struct PublishKskContext *pkc;
1315 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1317 pkc->expirationTime = expirationTime;
1318 pkc->anonymity = anonymity;
1319 pkc->priority = priority;
1321 pkc->cont_cls = cont_cls;
1322 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1324 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1326 if (pkc->dsh == NULL)
1328 cont (cont_cls, NULL, _("Could not connect to datastore."));
1336 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1337 GNUNET_assert (pkc->mdsize >= 0);
1338 uris = GNUNET_FS_uri_to_string (uri);
1339 pkc->slen = strlen (uris) + 1;
1340 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1341 if (size > MAX_KBLOCK_SIZE)
1343 size = MAX_KBLOCK_SIZE;
1344 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1346 pkc->kb = GNUNET_malloc (size);
1347 kbe = (char *) &pkc->kb[1];
1348 memcpy (kbe, uris, pkc->slen);
1350 sptr = &kbe[pkc->slen];
1352 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1355 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1356 if (pkc->mdsize == -1)
1359 GNUNET_free (pkc->kb);
1360 if (pkc->dsh != NULL)
1361 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1362 cont (cont_cls, NULL, _("Internal error."));
1366 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1368 pkc->cpy = GNUNET_malloc (size);
1369 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1370 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1373 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1374 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1375 GNUNET_SCHEDULER_add_continuation (h->sched,
1378 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1383 * Context for the SKS publication.
1385 struct PublishSksContext
1389 * Global FS context.
1391 struct GNUNET_FS_Uri *uri;
1394 * Handle to the datastore.
1396 struct GNUNET_DATASTORE_Handle *dsh;
1399 * Function to call once we're done.
1401 GNUNET_FS_PublishContinuation cont;
1412 * Function called by the datastore API with
1413 * the result from the PUT (SBlock) request.
1415 * @param cls closure of type "struct PublishSksContext*"
1416 * @param success GNUNET_OK on success
1417 * @param msg error message (or NULL)
1420 sb_put_cont (void *cls,
1424 struct PublishSksContext *psc = cls;
1426 if (NULL != psc->dsh)
1427 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1428 if (GNUNET_OK != success)
1429 psc->cont (psc->cont_cls,
1433 psc->cont (psc->cont_cls,
1436 GNUNET_FS_uri_destroy (psc->uri);
1442 * Publish an SBlock on GNUnet.
1444 * @param h handle to the file sharing subsystem
1445 * @param namespace namespace to publish in
1446 * @param identifier identifier to use
1447 * @param update update identifier to use
1448 * @param meta metadata to use
1449 * @param uri URI to refer to in the SBlock
1450 * @param expirationTime when the SBlock expires
1451 * @param anonymity anonymity level for the SBlock
1452 * @param priority priority for the SBlock
1453 * @param options publication options
1454 * @param cont continuation
1455 * @param cont_cls closure for cont
1458 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1459 struct GNUNET_FS_Namespace *namespace,
1460 const char *identifier,
1462 const struct GNUNET_CONTAINER_MetaData *meta,
1463 const struct GNUNET_FS_Uri *uri,
1464 struct GNUNET_TIME_Absolute expirationTime,
1467 enum GNUNET_FS_PublishOptions options,
1468 GNUNET_FS_PublishContinuation cont,
1471 struct PublishSksContext *psc;
1472 struct GNUNET_CRYPTO_AesSessionKey sk;
1473 struct GNUNET_CRYPTO_AesInitializationVector iv;
1474 struct GNUNET_FS_Uri *sks_uri;
1482 struct SBlock *sb_enc;
1484 struct GNUNET_CONTAINER_MetaData *mmeta;
1485 GNUNET_HashCode key; /* hash of thisId = key */
1486 GNUNET_HashCode id; /* hash of hc = identifier */
1487 GNUNET_HashCode query; /* id ^ nsid = DB query */
1490 mmeta = GNUNET_CONTAINER_meta_data_create ();
1492 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1493 uris = GNUNET_FS_uri_to_string (uri);
1494 slen = strlen (uris) + 1;
1495 idlen = strlen (identifier);
1498 nidlen = strlen (update) + 1;
1499 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1500 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1501 if (size > MAX_SBLOCK_SIZE)
1503 size = MAX_SBLOCK_SIZE;
1504 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1506 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1507 dest = (char *) &sb[1];
1508 memcpy (dest, update, nidlen);
1510 memcpy (dest, uris, slen);
1512 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1515 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1516 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1524 _("Internal error."));
1527 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1528 sb_enc = GNUNET_malloc (size);
1529 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1530 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1531 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1532 sks_uri->type = sks;
1533 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1534 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1535 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1536 &sks_uri->data.sks.namespace);
1537 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1538 GNUNET_CRYPTO_hash_xor (&id,
1539 &sks_uri->data.sks.namespace,
1540 &sb_enc->identifier);
1541 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1542 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1543 size - sizeof (struct SBlock),
1547 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1548 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1549 + sizeof(struct SBlock)
1550 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1551 GNUNET_assert (GNUNET_OK ==
1552 GNUNET_CRYPTO_rsa_sign (namespace->key,
1554 &sb_enc->signature));
1555 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1558 psc->cont_cls = cont_cls;
1559 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1561 GNUNET_free (sb_enc);
1568 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1569 if (NULL == psc->dsh)
1571 GNUNET_free (sb_enc);
1575 _("Failed to connect to datastore."));
1578 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1581 GNUNET_DATASTORE_put (psc->dsh,
1583 &sb_enc->identifier,
1586 GNUNET_BLOCK_TYPE_SBLOCK,
1590 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1595 GNUNET_free (sb_enc);
1598 /* end of fs_publish.c */