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 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
162 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
163 pi.value.publish.specifics.error.message = pcc->p->emsg;
164 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
166 if (NULL != pcc->cont)
168 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
169 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
177 * Generate the callback that signals clients
178 * that a file (or directory) has been completely
181 * @param p the completed upload
182 * @param sc context of the publication
185 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
186 struct GNUNET_FS_PublishContext *sc)
188 struct GNUNET_FS_ProgressInfo pi;
190 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
191 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
192 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
193 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
194 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
199 * Generate the callback that signals clients
200 * that a file (or directory) has encountered
201 * a problem during publication.
203 * @param p the upload that had trouble
204 * @param sc context of the publication
205 * @param emsg error message
208 signal_publish_error (struct GNUNET_FS_FileInformation *p,
209 struct GNUNET_FS_PublishContext *sc,
212 struct GNUNET_FS_ProgressInfo pi;
214 p->emsg = GNUNET_strdup (emsg);
215 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
216 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
217 pi.value.publish.specifics.error.message =emsg;
218 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
223 * We've finished publishing the SBlock as part of a larger upload.
224 * Check the result and complete the larger upload.
226 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
227 * @param uri URI of the published SBlock
228 * @param emsg NULL on success, otherwise error message
231 publish_sblocks_cont (void *cls,
232 const struct GNUNET_FS_Uri *uri,
235 struct GNUNET_FS_PublishContext *sc = cls;
238 signal_publish_error (sc->fi,
243 // FIXME: release the datastore reserve here!
244 signal_publish_completion (sc->fi, sc);
245 sc->all_done = GNUNET_YES;
250 * We are almost done publishing the structure,
251 * add SBlocks (if needed).
253 * @param sc overall upload data
256 publish_sblock (struct GNUNET_FS_PublishContext *sc)
258 if (NULL != sc->namespace)
259 GNUNET_FS_publish_sks (sc->h,
265 sc->fi->expirationTime,
269 &publish_sblocks_cont,
272 publish_sblocks_cont (sc, NULL, NULL);
277 * We've finished publishing a KBlock as part of a larger upload.
278 * Check the result and continue the larger upload.
280 * @param cls the "struct GNUNET_FS_PublishContext*"
281 * of the larger upload
282 * @param uri URI of the published blocks
283 * @param emsg NULL on success, otherwise error message
286 publish_kblocks_cont (void *cls,
287 const struct GNUNET_FS_Uri *uri,
290 struct GNUNET_FS_PublishContext *sc = cls;
291 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
295 signal_publish_error (p, sc, emsg);
297 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
298 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
299 &GNUNET_FS_publish_main_,
304 signal_publish_completion (p, sc);
305 /* move on to next file */
307 sc->fi_pos = p->next;
311 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
312 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
313 &GNUNET_FS_publish_main_,
319 * Function called by the tree encoder to obtain
320 * a block of plaintext data (for the lowest level
323 * @param cls our publishing context
324 * @param offset identifies which block to get
325 * @param max (maximum) number of bytes to get; returning
326 * fewer will also cause errors
327 * @param buf where to copy the plaintext buffer
328 * @param emsg location to store an error message (on error)
329 * @return number of bytes copied to buf, 0 on error
332 block_reader (void *cls,
338 struct GNUNET_FS_PublishContext *sc = cls;
339 struct GNUNET_FS_FileInformation *p;
346 pt_size = GNUNET_MIN(max,
347 p->data.dir.dir_size - offset);
348 dd = p->data.dir.dir_data;
355 pt_size = GNUNET_MIN(max,
356 p->data.file.file_size - offset);
358 return 0; /* calling reader with pt_size==0
359 might free buf, so don't! */
361 p->data.file.reader (p->data.file.reader_cls,
373 * The tree encoder has finished processing a
374 * file. Call it's finish method and deal with
377 * @param cls our publishing context
378 * @param tc scheduler's task context (not used)
381 encode_cont (void *cls,
382 const struct GNUNET_SCHEDULER_TaskContext *tc)
384 struct GNUNET_FS_PublishContext *sc = cls;
385 struct GNUNET_FS_FileInformation *p;
386 struct GNUNET_FS_ProgressInfo pi;
390 GNUNET_FS_tree_encoder_finish (p->te,
396 GNUNET_asprintf (&p->emsg,
397 _("Upload failed: %s"),
400 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
401 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
402 pi.value.publish.specifics.error.message = p->emsg;
403 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
405 /* continue with main */
407 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
408 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
409 &GNUNET_FS_publish_main_,
415 * Function called asking for the current (encoded)
416 * block to be processed. After processing the
417 * client should either call "GNUNET_FS_tree_encode_next"
418 * or (on error) "GNUNET_FS_tree_encode_finish".
421 * @param query the query for the block (key for lookup in the datastore)
422 * @param offset offset of the block in the file
423 * @param type type of the block (IBLOCK or DBLOCK)
424 * @param block the (encrypted) block
425 * @param block_size size of block (in bytes)
428 block_proc (void *cls,
429 const GNUNET_HashCode *query,
431 enum GNUNET_BLOCK_Type type,
435 struct GNUNET_FS_PublishContext *sc = cls;
436 struct GNUNET_FS_FileInformation *p;
437 struct PutContCtx * dpc_cls;
438 struct OnDemandBlock odb;
444 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
445 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
446 &GNUNET_FS_publish_main_,
451 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
452 sc->in_network_wait = GNUNET_YES;
453 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
454 dpc_cls->cont = &GNUNET_FS_publish_main_;
455 dpc_cls->cont_cls = sc;
458 if ( (! p->is_directory) &&
459 (GNUNET_YES == p->data.file.do_index) &&
460 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Indexing block `%s' for offset %llu with index size %u\n",
466 (unsigned long long) offset,
467 sizeof (struct OnDemandBlock));
469 odb.offset = GNUNET_htonll (offset);
470 odb.file_id = p->data.file.file_id;
471 GNUNET_DATASTORE_put (sc->dsh,
474 sizeof(struct OnDemandBlock),
476 GNUNET_BLOCK_TYPE_ONDEMAND,
480 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Publishing block `%s' for offset %llu with size %u\n",
489 (unsigned long long) offset,
490 (unsigned int) block_size);
492 GNUNET_DATASTORE_put (sc->dsh,
501 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
508 * Function called with information about our
509 * progress in computing the tree encoding.
512 * @param offset where are we in the file
513 * @param pt_block plaintext of the currently processed block
514 * @param pt_size size of pt_block
515 * @param depth depth of the block in the tree
518 progress_proc (void *cls,
520 const void *pt_block,
524 struct GNUNET_FS_PublishContext *sc = cls;
525 struct GNUNET_FS_FileInformation *p;
526 struct GNUNET_FS_ProgressInfo pi;
529 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
530 pi.value.publish.specifics.progress.data = pt_block;
531 pi.value.publish.specifics.progress.offset = offset;
532 pi.value.publish.specifics.progress.data_len = pt_size;
533 pi.value.publish.specifics.progress.depth = depth;
534 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
539 * We are uploading a file or directory; load (if necessary) the next
540 * block into memory, encrypt it and send it to the FS service. Then
541 * continue with the main task.
543 * @param sc overall upload data
546 publish_content (struct GNUNET_FS_PublishContext *sc)
548 struct GNUNET_FS_FileInformation *p;
550 struct GNUNET_FS_DirectoryBuilder *db;
551 struct GNUNET_FS_FileInformation *dirpos;
560 db = GNUNET_FS_directory_builder_create (p->meta);
561 dirpos = p->data.dir.entries;
562 while (NULL != dirpos)
564 if (dirpos->is_directory)
566 raw_data = dirpos->data.dir.dir_data;
567 dirpos->data.dir.dir_data = NULL;
572 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
573 (dirpos->data.file.file_size > 0) )
575 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
577 if (dirpos->data.file.file_size !=
578 dirpos->data.file.reader (dirpos->data.file.reader_cls,
580 dirpos->data.file.file_size,
584 GNUNET_free_non_null (emsg);
585 GNUNET_free (raw_data);
590 GNUNET_FS_directory_builder_add (db,
594 GNUNET_free_non_null (raw_data);
595 dirpos = dirpos->next;
597 GNUNET_FS_directory_builder_finish (db,
598 &p->data.dir.dir_size,
599 &p->data.dir.dir_data);
601 size = (p->is_directory)
602 ? p->data.dir.dir_size
603 : p->data.file.file_size;
604 p->te = GNUNET_FS_tree_encoder_create (sc->h,
613 GNUNET_FS_tree_encoder_next (p->te);
618 * Process the response (or lack thereof) from
619 * the "fs" service to our 'start index' request.
621 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
622 * @param msg the response we got
625 process_index_start_response (void *cls,
626 const struct GNUNET_MessageHeader *msg)
628 struct GNUNET_FS_PublishContext *sc = cls;
629 struct GNUNET_FS_FileInformation *p;
633 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639 _("Can not index file `%s': %s. Will try to insert instead.\n"),
641 _("timeout on index-start request to `fs' service"));
642 p->data.file.do_index = GNUNET_NO;
643 publish_content (sc);
646 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
648 msize = ntohs (msg->size);
649 emsg = (const char *) &msg[1];
650 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
651 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
652 emsg = gettext_noop ("unknown error");
653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
654 _("Can not index file `%s': %s. Will try to insert instead.\n"),
657 p->data.file.do_index = GNUNET_NO;
658 publish_content (sc);
661 p->data.file.index_start_confirmed = GNUNET_YES;
662 /* success! continue with indexing */
663 publish_content (sc);
668 * Function called once the hash computation over an
669 * indexed file has completed.
671 * @param cls closure, our publishing context
672 * @param res resulting hash, NULL on error
675 hash_for_index_cb (void *cls,
676 const GNUNET_HashCode *
679 struct GNUNET_FS_PublishContext *sc = cls;
680 struct GNUNET_FS_FileInformation *p;
681 struct IndexStartMessage *ism;
683 struct GNUNET_CLIENT_Connection *client;
691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
692 _("Can not index file `%s': %s. Will try to insert instead.\n"),
694 _("failed to compute hash"));
695 p->data.file.do_index = GNUNET_NO;
696 publish_content (sc);
699 if (GNUNET_YES == p->data.file.index_start_confirmed)
701 publish_content (sc);
704 fn = GNUNET_STRINGS_filename_expand (p->filename);
705 slen = strlen (fn) + 1;
706 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
708 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
709 _("Can not index file `%s': %s. Will try to insert instead.\n"),
711 _("filename too long"));
713 p->data.file.do_index = GNUNET_NO;
714 publish_content (sc);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Hash of indexed file `%s' is `%s'\n",
720 p->data.file.filename,
723 client = GNUNET_CLIENT_connect (sc->h->sched,
728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
729 _("Can not index file `%s': %s. Will try to insert instead.\n"),
731 _("could not connect to `fs' service"));
732 p->data.file.do_index = GNUNET_NO;
733 publish_content (sc);
737 p->data.file.file_id = *res;
738 p->data.file.have_hash = GNUNET_YES;
739 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
741 ism->header.size = htons(sizeof(struct IndexStartMessage) +
743 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
745 GNUNET_DISK_file_get_identifiers (p->filename,
749 ism->device = htonl (dev);
750 ism->inode = GNUNET_htonll(ino);
754 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
755 _("Failed to get file identifiers for `%s'\n"),
764 GNUNET_break (GNUNET_YES ==
765 GNUNET_CLIENT_transmit_and_get_response (client,
767 GNUNET_TIME_UNIT_FOREVER_REL,
769 &process_index_start_response,
776 * Main function that performs the upload.
778 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
779 * @param tc task context
782 GNUNET_FS_publish_main_ (void *cls,
783 const struct GNUNET_SCHEDULER_TaskContext *tc)
785 struct GNUNET_FS_PublishContext *sc = cls;
786 struct GNUNET_FS_ProgressInfo pi;
787 struct GNUNET_FS_FileInformation *p;
790 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
794 /* upload of entire hierarchy complete,
795 publish namespace entries */
799 /* find starting position */
800 while ( (p->is_directory) &&
801 (NULL != p->data.dir.entries) &&
803 (NULL == p->data.dir.entries->chk_uri) )
805 p = p->data.dir.entries;
811 /* error with current file, abort all
812 related files as well! */
813 while (NULL != p->dir)
815 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
816 EXTRACTOR_METATYPE_FILENAME);
820 GNUNET_asprintf (&p->emsg,
821 _("Recursive upload failed at `%s': %s"),
828 GNUNET_asprintf (&p->emsg,
829 _("Recursive upload failed: %s"),
832 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
833 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
834 pi.value.publish.specifics.error.message = p->emsg;
835 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
837 sc->all_done = GNUNET_YES;
840 /* handle completion */
841 if (NULL != p->chk_uri)
843 /* upload of "p" complete, publish KBlocks! */
844 if (p->keywords != NULL)
846 GNUNET_FS_publish_ksk (sc->h,
854 &publish_kblocks_cont,
859 publish_kblocks_cont (sc,
865 if ( (!p->is_directory) &&
866 (p->data.file.do_index) )
868 if (NULL == p->filename)
870 p->data.file.do_index = GNUNET_NO;
871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
872 _("Can not index file `%s': %s. Will try to insert instead.\n"),
874 _("needs to be an actual file"));
875 publish_content (sc);
878 if (p->data.file.have_hash)
879 hash_for_index_cb (sc,
880 &p->data.file.file_id);
883 p->start_time = GNUNET_TIME_absolute_get ();
884 GNUNET_CRYPTO_hash_file (sc->h->sched,
885 GNUNET_SCHEDULER_PRIORITY_IDLE,
893 publish_content (sc);
898 * Signal the FS's progress function that we are starting
901 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
902 * @param fi the entry in the publish-structure
903 * @param length length of the file or directory
904 * @param meta metadata for the file or directory (can be modified)
905 * @param uri pointer to the keywords that will be used for this entry (can be modified)
906 * @param anonymity pointer to selected anonymity level (can be modified)
907 * @param priority pointer to selected priority (can be modified)
908 * @param expirationTime pointer to selected expiration time (can be modified)
909 * @param client_info pointer to client context set upon creation (can be modified)
910 * @return GNUNET_OK to continue (always)
913 fip_signal_start(void *cls,
914 struct GNUNET_FS_FileInformation *fi,
916 struct GNUNET_CONTAINER_MetaData *meta,
917 struct GNUNET_FS_Uri **uri,
920 struct GNUNET_TIME_Absolute *expirationTime,
923 struct GNUNET_FS_PublishContext *sc = cls;
924 struct GNUNET_FS_ProgressInfo pi;
926 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
927 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
933 * Publish a file or directory.
935 * @param h handle to the file sharing subsystem
936 * @param fi information about the file or directory structure to publish
937 * @param namespace namespace to publish the file in, NULL for no namespace
938 * @param nid identifier to use for the publishd content in the namespace
939 * (can be NULL, must be NULL if namespace is NULL)
940 * @param nuid update-identifier that will be used for future updates
941 * (can be NULL, must be NULL if namespace or nid is NULL)
942 * @param options options for the publication
943 * @return context that can be used to control the publish operation
945 struct GNUNET_FS_PublishContext *
946 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
947 struct GNUNET_FS_FileInformation *fi,
948 struct GNUNET_FS_Namespace *namespace,
951 enum GNUNET_FS_PublishOptions options)
953 struct GNUNET_FS_PublishContext *ret;
954 struct GNUNET_DATASTORE_Handle *dsh;
956 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
958 dsh = GNUNET_DATASTORE_connect (h->cfg,
967 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
971 ret->namespace = namespace;
972 if (namespace != NULL)
975 GNUNET_assert (NULL != nid);
976 ret->nid = GNUNET_strdup (nid);
978 ret->nuid = GNUNET_strdup (nuid);
980 // FIXME: make upload persistent!
983 GNUNET_FS_file_information_inspect (ret->fi,
986 ret->fi_pos = ret->fi;
988 // FIXME: calculate space needed for "fi"
989 // and reserve as first task (then trigger
990 // "publish_main" from that continuation)!
992 = GNUNET_SCHEDULER_add_with_priority (h->sched,
993 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
994 &GNUNET_FS_publish_main_,
1001 * Signal the FS's progress function that we are stopping
1004 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1005 * @param fi the entry in the publish-structure
1006 * @param length length of the file or directory
1007 * @param meta metadata for the file or directory (can be modified)
1008 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1009 * @param anonymity pointer to selected anonymity level (can be modified)
1010 * @param priority pointer to selected priority (can be modified)
1011 * @param expirationTime pointer to selected expiration time (can be modified)
1012 * @param client_info pointer to client context set upon creation (can be modified)
1013 * @return GNUNET_OK to continue (always)
1016 fip_signal_stop(void *cls,
1017 struct GNUNET_FS_FileInformation *fi,
1019 struct GNUNET_CONTAINER_MetaData *meta,
1020 struct GNUNET_FS_Uri **uri,
1021 uint32_t *anonymity,
1023 struct GNUNET_TIME_Absolute *expirationTime,
1026 struct GNUNET_FS_PublishContext*sc = cls;
1027 struct GNUNET_FS_ProgressInfo pi;
1030 if (fi->serialization != NULL)
1032 if (0 != UNLINK (fi->serialization))
1034 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1038 GNUNET_free (fi->serialization);
1039 fi->serialization = NULL;
1041 off = (fi->chk_uri == NULL) ? 0 : length;
1042 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1043 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1044 *client_info = NULL;
1050 * Stop an upload. Will abort incomplete uploads (but
1051 * not remove blocks that have already been publishd) or
1052 * simply clean up the state for completed uploads.
1053 * Must NOT be called from within the event callback!
1055 * @param pc context for the upload to stop
1058 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1060 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1061 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1062 if (pc->serialization != NULL)
1064 if (0 != UNLINK (pc->serialization))
1065 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1068 GNUNET_free (pc->serialization);
1069 pc->serialization = NULL;
1071 GNUNET_FS_file_information_inspect (pc->fi,
1074 if (GNUNET_YES == pc->in_network_wait)
1076 pc->in_network_wait = GNUNET_SYSERR;
1079 publish_cleanup (pc);
1084 * Context for the KSK publication.
1086 struct PublishKskContext
1092 struct GNUNET_FS_Uri *ksk_uri;
1095 * Global FS context.
1097 struct GNUNET_FS_Handle *h;
1100 * The master block that we are sending
1101 * (in plaintext), has "mdsize+slen" more
1102 * bytes than the struct would suggest.
1107 * Buffer of the same size as "kb" for
1108 * the encrypted version.
1113 * Handle to the datastore, NULL if we are just
1116 struct GNUNET_DATASTORE_Handle *dsh;
1119 * Function to call once we're done.
1121 GNUNET_FS_PublishContinuation cont;
1129 * When should the KBlocks expire?
1131 struct GNUNET_TIME_Absolute expirationTime;
1134 * Size of the serialized metadata.
1139 * Size of the (CHK) URI as a string.
1144 * Keyword that we are currently processing.
1149 * Anonymity level for the KBlocks.
1154 * Priority for the KBlocks.
1161 * Continuation of "GNUNET_FS_publish_ksk" that performs
1162 * the actual publishing operation (iterating over all
1165 * @param cls closure of type "struct PublishKskContext*"
1169 publish_ksk_cont (void *cls,
1170 const struct GNUNET_SCHEDULER_TaskContext *tc);
1174 * Function called by the datastore API with
1175 * the result from the PUT request.
1177 * @param cls closure of type "struct PublishKskContext*"
1178 * @param success GNUNET_OK on success
1179 * @param msg error message (or NULL)
1182 kb_put_cont (void *cls,
1186 struct PublishKskContext *pkc = cls;
1188 if (GNUNET_OK != success)
1190 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1191 GNUNET_free (pkc->cpy);
1192 GNUNET_free (pkc->kb);
1193 pkc->cont (pkc->cont_cls,
1196 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1200 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1208 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1209 * publishing operation (iterating over all of the keywords).
1211 * @param cls closure of type "struct PublishKskContext*"
1215 publish_ksk_cont (void *cls,
1216 const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct PublishKskContext *pkc = cls;
1219 const char *keyword;
1220 GNUNET_HashCode key;
1221 GNUNET_HashCode query;
1222 struct GNUNET_CRYPTO_AesSessionKey skey;
1223 struct GNUNET_CRYPTO_AesInitializationVector iv;
1224 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1227 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1228 (NULL == pkc->dsh) )
1230 if (NULL != pkc->dsh)
1231 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1232 GNUNET_free (pkc->cpy);
1233 GNUNET_free (pkc->kb);
1234 pkc->cont (pkc->cont_cls,
1237 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1241 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1242 /* first character of keyword indicates if it is
1243 mandatory or not -- ignore for hashing */
1244 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1245 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1246 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1247 pkc->slen + pkc->mdsize,
1251 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1252 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1253 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1254 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1256 GNUNET_assert (GNUNET_OK ==
1257 GNUNET_CRYPTO_rsa_sign (pk,
1259 &pkc->cpy->signature));
1260 GNUNET_CRYPTO_rsa_key_free (pk);
1261 GNUNET_DATASTORE_put (pkc->dsh,
1265 sizeof (struct KBlock) +
1268 GNUNET_BLOCK_TYPE_KBLOCK,
1271 pkc->expirationTime,
1272 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1279 * Publish a CHK under various keywords on GNUnet.
1281 * @param h handle to the file sharing subsystem
1282 * @param ksk_uri keywords to use
1283 * @param meta metadata to use
1284 * @param uri URI to refer to in the KBlock
1285 * @param expirationTime when the KBlock expires
1286 * @param anonymity anonymity level for the KBlock
1287 * @param priority priority for the KBlock
1288 * @param options publication options
1289 * @param cont continuation
1290 * @param cont_cls closure for cont
1293 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1294 const struct GNUNET_FS_Uri *ksk_uri,
1295 const struct GNUNET_CONTAINER_MetaData *meta,
1296 const struct GNUNET_FS_Uri *uri,
1297 struct GNUNET_TIME_Absolute expirationTime,
1300 enum GNUNET_FS_PublishOptions options,
1301 GNUNET_FS_PublishContinuation cont,
1304 struct PublishKskContext *pkc;
1310 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1312 pkc->expirationTime = expirationTime;
1313 pkc->anonymity = anonymity;
1314 pkc->priority = priority;
1316 pkc->cont_cls = cont_cls;
1317 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1319 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1321 if (pkc->dsh == NULL)
1323 cont (cont_cls, NULL, _("Could not connect to datastore."));
1331 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1332 GNUNET_assert (pkc->mdsize >= 0);
1333 uris = GNUNET_FS_uri_to_string (uri);
1334 pkc->slen = strlen (uris) + 1;
1335 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1336 if (size > MAX_KBLOCK_SIZE)
1338 size = MAX_KBLOCK_SIZE;
1339 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1341 pkc->kb = GNUNET_malloc (size);
1342 kbe = (char *) &pkc->kb[1];
1343 memcpy (kbe, uris, pkc->slen);
1345 sptr = &kbe[pkc->slen];
1347 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1350 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1351 if (pkc->mdsize == -1)
1354 GNUNET_free (pkc->kb);
1355 if (pkc->dsh != NULL)
1356 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1357 cont (cont_cls, NULL, _("Internal error."));
1361 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1363 pkc->cpy = GNUNET_malloc (size);
1364 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1365 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1368 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1369 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1370 GNUNET_SCHEDULER_add_continuation (h->sched,
1373 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1378 * Context for the SKS publication.
1380 struct PublishSksContext
1384 * Global FS context.
1386 struct GNUNET_FS_Uri *uri;
1389 * Handle to the datastore.
1391 struct GNUNET_DATASTORE_Handle *dsh;
1394 * Function to call once we're done.
1396 GNUNET_FS_PublishContinuation cont;
1407 * Function called by the datastore API with
1408 * the result from the PUT (SBlock) request.
1410 * @param cls closure of type "struct PublishSksContext*"
1411 * @param success GNUNET_OK on success
1412 * @param msg error message (or NULL)
1415 sb_put_cont (void *cls,
1419 struct PublishSksContext *psc = cls;
1421 if (NULL != psc->dsh)
1422 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1423 if (GNUNET_OK != success)
1424 psc->cont (psc->cont_cls,
1428 psc->cont (psc->cont_cls,
1431 GNUNET_FS_uri_destroy (psc->uri);
1437 * Publish an SBlock on GNUnet.
1439 * @param h handle to the file sharing subsystem
1440 * @param namespace namespace to publish in
1441 * @param identifier identifier to use
1442 * @param update update identifier to use
1443 * @param meta metadata to use
1444 * @param uri URI to refer to in the SBlock
1445 * @param expirationTime when the SBlock expires
1446 * @param anonymity anonymity level for the SBlock
1447 * @param priority priority for the SBlock
1448 * @param options publication options
1449 * @param cont continuation
1450 * @param cont_cls closure for cont
1453 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1454 struct GNUNET_FS_Namespace *namespace,
1455 const char *identifier,
1457 const struct GNUNET_CONTAINER_MetaData *meta,
1458 const struct GNUNET_FS_Uri *uri,
1459 struct GNUNET_TIME_Absolute expirationTime,
1462 enum GNUNET_FS_PublishOptions options,
1463 GNUNET_FS_PublishContinuation cont,
1466 struct PublishSksContext *psc;
1467 struct GNUNET_CRYPTO_AesSessionKey sk;
1468 struct GNUNET_CRYPTO_AesInitializationVector iv;
1469 struct GNUNET_FS_Uri *sks_uri;
1477 struct SBlock *sb_enc;
1479 struct GNUNET_CONTAINER_MetaData *mmeta;
1480 GNUNET_HashCode key; /* hash of thisId = key */
1481 GNUNET_HashCode id; /* hash of hc = identifier */
1482 GNUNET_HashCode query; /* id ^ nsid = DB query */
1485 mmeta = GNUNET_CONTAINER_meta_data_create ();
1487 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1488 uris = GNUNET_FS_uri_to_string (uri);
1489 slen = strlen (uris) + 1;
1490 idlen = strlen (identifier);
1493 nidlen = strlen (update) + 1;
1494 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1495 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1496 if (size > MAX_SBLOCK_SIZE)
1498 size = MAX_SBLOCK_SIZE;
1499 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1501 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1502 dest = (char *) &sb[1];
1503 memcpy (dest, update, nidlen);
1505 memcpy (dest, uris, slen);
1507 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1510 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1511 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1519 _("Internal error."));
1522 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1523 sb_enc = GNUNET_malloc (size);
1524 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1525 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1526 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1527 sks_uri->type = sks;
1528 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1529 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1530 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1531 &sks_uri->data.sks.namespace);
1532 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1533 GNUNET_CRYPTO_hash_xor (&id,
1534 &sks_uri->data.sks.namespace,
1535 &sb_enc->identifier);
1536 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1537 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1538 size - sizeof (struct SBlock),
1542 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1543 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1544 + sizeof(struct SBlock)
1545 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1546 GNUNET_assert (GNUNET_OK ==
1547 GNUNET_CRYPTO_rsa_sign (namespace->key,
1549 &sb_enc->signature));
1550 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1553 psc->cont_cls = cont_cls;
1554 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1556 GNUNET_free (sb_enc);
1563 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1564 if (NULL == psc->dsh)
1566 GNUNET_free (sb_enc);
1570 _("Failed to connect to datastore."));
1573 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1576 GNUNET_DATASTORE_put (psc->dsh,
1578 &sb_enc->identifier,
1581 GNUNET_BLOCK_TYPE_SBLOCK,
1585 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1590 GNUNET_free (sb_enc);
1593 /* end of fs_publish.c */