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
96 = (p->is_directory) ? p->data.dir.dirname : p->data.file.filename;
97 pi->value.publish.size
98 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
100 = GNUNET_TIME_calculate_eta (p->start_time,
102 pi->value.publish.size);
103 pi->value.publish.completed = offset;
104 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
105 pi->value.publish.anonymity = p->anonymity;
106 return sc->h->upcb (sc->h->upcb_cls,
112 * Cleanup the publish context, we're done with it.
114 * @param pc struct to clean up after
117 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
119 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
120 if (pc->namespace != NULL)
121 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
122 GNUNET_free_non_null (pc->nid);
123 GNUNET_free_non_null (pc->nuid);
124 GNUNET_free_non_null (pc->serialization);
125 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
126 if (pc->client != NULL)
127 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
133 * Function called by the datastore API with
134 * the result from the PUT request.
136 * @param cls our closure
137 * @param success GNUNET_OK on success
138 * @param msg error message (or NULL)
141 ds_put_cont (void *cls,
145 struct PutContCtx *pcc = cls;
146 struct GNUNET_FS_ProgressInfo pi;
148 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
150 /* we were aborted in the meantime,
152 publish_cleanup (pcc->sc);
155 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
156 pcc->sc->in_network_wait = GNUNET_NO;
157 if (GNUNET_OK != success)
159 GNUNET_asprintf (&pcc->p->emsg,
160 _("Upload failed: %s"),
162 GNUNET_FS_file_information_sync (pcc->p);
163 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
164 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
165 pi.value.publish.specifics.error.message = pcc->p->emsg;
166 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
168 GNUNET_FS_file_information_sync (pcc->p);
169 if (NULL != pcc->cont)
171 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
172 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
180 * Generate the callback that signals clients
181 * that a file (or directory) has been completely
184 * @param p the completed upload
185 * @param sc context of the publication
188 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
189 struct GNUNET_FS_PublishContext *sc)
191 struct GNUNET_FS_ProgressInfo pi;
193 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
194 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
195 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
196 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
197 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
202 * Generate the callback that signals clients
203 * that a file (or directory) has encountered
204 * a problem during publication.
206 * @param p the upload that had trouble
207 * @param sc context of the publication
208 * @param emsg error message
211 signal_publish_error (struct GNUNET_FS_FileInformation *p,
212 struct GNUNET_FS_PublishContext *sc,
215 struct GNUNET_FS_ProgressInfo pi;
217 p->emsg = GNUNET_strdup (emsg);
218 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
219 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
220 pi.value.publish.specifics.error.message =emsg;
221 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
226 * We've finished publishing the SBlock as part of a larger upload.
227 * Check the result and complete the larger upload.
229 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
230 * @param uri URI of the published SBlock
231 * @param emsg NULL on success, otherwise error message
234 publish_sblocks_cont (void *cls,
235 const struct GNUNET_FS_Uri *uri,
238 struct GNUNET_FS_PublishContext *sc = cls;
241 signal_publish_error (sc->fi,
246 // FIXME: release the datastore reserve here!
247 signal_publish_completion (sc->fi, sc);
248 sc->all_done = GNUNET_YES;
253 * We are almost done publishing the structure,
254 * add SBlocks (if needed).
256 * @param sc overall upload data
259 publish_sblock (struct GNUNET_FS_PublishContext *sc)
261 if (NULL != sc->namespace)
262 GNUNET_FS_publish_sks (sc->h,
268 sc->fi->expirationTime,
272 &publish_sblocks_cont,
275 publish_sblocks_cont (sc, NULL, NULL);
280 * We've finished publishing a KBlock as part of a larger upload.
281 * Check the result and continue the larger upload.
283 * @param cls the "struct GNUNET_FS_PublishContext*"
284 * of the larger upload
285 * @param uri URI of the published blocks
286 * @param emsg NULL on success, otherwise error message
289 publish_kblocks_cont (void *cls,
290 const struct GNUNET_FS_Uri *uri,
293 struct GNUNET_FS_PublishContext *sc = cls;
294 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
298 signal_publish_error (p, sc, emsg);
300 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
301 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
302 &GNUNET_FS_publish_main_,
306 GNUNET_FS_file_information_sync (p);
308 signal_publish_completion (p, sc);
309 /* move on to next file */
311 sc->fi_pos = p->next;
315 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
316 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
317 &GNUNET_FS_publish_main_,
323 * Function called by the tree encoder to obtain
324 * a block of plaintext data (for the lowest level
327 * @param cls our publishing context
328 * @param offset identifies which block to get
329 * @param max (maximum) number of bytes to get; returning
330 * fewer will also cause errors
331 * @param buf where to copy the plaintext buffer
332 * @param emsg location to store an error message (on error)
333 * @return number of bytes copied to buf, 0 on error
336 block_reader (void *cls,
342 struct GNUNET_FS_PublishContext *sc = cls;
343 struct GNUNET_FS_FileInformation *p;
350 pt_size = GNUNET_MIN(max,
351 p->data.dir.dir_size - offset);
352 dd = p->data.dir.dir_data;
359 pt_size = GNUNET_MIN(max,
360 p->data.file.file_size - offset);
362 return 0; /* calling reader with pt_size==0
363 might free buf, so don't! */
365 p->data.file.reader (p->data.file.reader_cls,
377 * The tree encoder has finished processing a
378 * file. Call it's finish method and deal with
381 * @param cls our publishing context
382 * @param tc scheduler's task context (not used)
385 encode_cont (void *cls,
386 const struct GNUNET_SCHEDULER_TaskContext *tc)
388 struct GNUNET_FS_PublishContext *sc = cls;
389 struct GNUNET_FS_FileInformation *p;
390 struct GNUNET_FS_ProgressInfo pi;
394 GNUNET_FS_tree_encoder_finish (p->te,
400 GNUNET_asprintf (&p->emsg,
401 _("Upload failed: %s"),
404 GNUNET_FS_file_information_sync (p);
405 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
406 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
407 pi.value.publish.specifics.error.message = p->emsg;
408 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
410 /* continue with main */
412 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
413 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
414 &GNUNET_FS_publish_main_,
420 * Function called asking for the current (encoded)
421 * block to be processed. After processing the
422 * client should either call "GNUNET_FS_tree_encode_next"
423 * or (on error) "GNUNET_FS_tree_encode_finish".
426 * @param query the query for the block (key for lookup in the datastore)
427 * @param offset offset of the block in the file
428 * @param type type of the block (IBLOCK or DBLOCK)
429 * @param block the (encrypted) block
430 * @param block_size size of block (in bytes)
433 block_proc (void *cls,
434 const GNUNET_HashCode *query,
436 enum GNUNET_BLOCK_Type type,
440 struct GNUNET_FS_PublishContext *sc = cls;
441 struct GNUNET_FS_FileInformation *p;
442 struct PutContCtx * dpc_cls;
443 struct OnDemandBlock odb;
449 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
450 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
451 &GNUNET_FS_publish_main_,
456 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
457 sc->in_network_wait = GNUNET_YES;
458 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
459 dpc_cls->cont = &GNUNET_FS_publish_main_;
460 dpc_cls->cont_cls = sc;
463 if ( (! p->is_directory) &&
464 (GNUNET_YES == p->data.file.do_index) &&
465 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "Indexing block `%s' for offset %llu with index size %u\n",
471 (unsigned long long) offset,
472 sizeof (struct OnDemandBlock));
474 odb.offset = GNUNET_htonll (offset);
475 odb.file_id = p->data.file.file_id;
476 GNUNET_DATASTORE_put (sc->dsh,
479 sizeof(struct OnDemandBlock),
481 GNUNET_BLOCK_TYPE_ONDEMAND,
485 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Publishing block `%s' for offset %llu with size %u\n",
494 (unsigned long long) offset,
495 (unsigned int) block_size);
497 GNUNET_DATASTORE_put (sc->dsh,
506 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
513 * Function called with information about our
514 * progress in computing the tree encoding.
517 * @param offset where are we in the file
518 * @param pt_block plaintext of the currently processed block
519 * @param pt_size size of pt_block
520 * @param depth depth of the block in the tree
523 progress_proc (void *cls,
525 const void *pt_block,
529 struct GNUNET_FS_PublishContext *sc = cls;
530 struct GNUNET_FS_FileInformation *p;
531 struct GNUNET_FS_ProgressInfo pi;
534 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
535 pi.value.publish.specifics.progress.data = pt_block;
536 pi.value.publish.specifics.progress.offset = offset;
537 pi.value.publish.specifics.progress.data_len = pt_size;
538 pi.value.publish.specifics.progress.depth = depth;
539 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
544 * We are uploading a file or directory; load (if necessary) the next
545 * block into memory, encrypt it and send it to the FS service. Then
546 * continue with the main task.
548 * @param sc overall upload data
551 publish_content (struct GNUNET_FS_PublishContext *sc)
553 struct GNUNET_FS_FileInformation *p;
555 struct GNUNET_FS_DirectoryBuilder *db;
556 struct GNUNET_FS_FileInformation *dirpos;
565 db = GNUNET_FS_directory_builder_create (p->meta);
566 dirpos = p->data.dir.entries;
567 while (NULL != dirpos)
569 if (dirpos->is_directory)
571 raw_data = dirpos->data.dir.dir_data;
572 dirpos->data.dir.dir_data = NULL;
577 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
578 (dirpos->data.file.file_size > 0) )
580 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
582 if (dirpos->data.file.file_size !=
583 dirpos->data.file.reader (dirpos->data.file.reader_cls,
585 dirpos->data.file.file_size,
589 GNUNET_free_non_null (emsg);
590 GNUNET_free (raw_data);
595 GNUNET_FS_directory_builder_add (db,
599 GNUNET_free_non_null (raw_data);
600 dirpos = dirpos->next;
602 GNUNET_FS_directory_builder_finish (db,
603 &p->data.dir.dir_size,
604 &p->data.dir.dir_data);
606 size = (p->is_directory)
607 ? p->data.dir.dir_size
608 : p->data.file.file_size;
609 p->te = GNUNET_FS_tree_encoder_create (sc->h,
618 GNUNET_FS_tree_encoder_next (p->te);
623 * Process the response (or lack thereof) from
624 * the "fs" service to our 'start index' request.
626 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
627 * @param msg the response we got
630 process_index_start_response (void *cls,
631 const struct GNUNET_MessageHeader *msg)
633 struct GNUNET_FS_PublishContext *sc = cls;
634 struct GNUNET_FS_FileInformation *p;
638 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
644 _("Can not index file `%s': %s. Will try to insert instead.\n"),
645 p->data.file.filename,
646 _("timeout on index-start request to `fs' service"));
647 p->data.file.do_index = GNUNET_NO;
648 publish_content (sc);
651 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
653 msize = ntohs (msg->size);
654 emsg = (const char *) &msg[1];
655 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
656 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
657 emsg = gettext_noop ("unknown error");
658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 _("Can not index file `%s': %s. Will try to insert instead.\n"),
660 p->data.file.filename,
662 p->data.file.do_index = GNUNET_NO;
663 publish_content (sc);
666 p->data.file.index_start_confirmed = GNUNET_YES;
667 /* success! continue with indexing */
668 publish_content (sc);
673 * Function called once the hash computation over an
674 * indexed file has completed.
676 * @param cls closure, our publishing context
677 * @param res resulting hash, NULL on error
680 hash_for_index_cb (void *cls,
681 const GNUNET_HashCode *
684 struct GNUNET_FS_PublishContext *sc = cls;
685 struct GNUNET_FS_FileInformation *p;
686 struct IndexStartMessage *ism;
688 struct GNUNET_CLIENT_Connection *client;
696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 _("Can not index file `%s': %s. Will try to insert instead.\n"),
698 p->data.file.filename,
699 _("failed to compute hash"));
700 p->data.file.do_index = GNUNET_NO;
701 publish_content (sc);
704 if (GNUNET_YES == p->data.file.index_start_confirmed)
706 publish_content (sc);
709 fn = GNUNET_STRINGS_filename_expand (p->data.file.filename);
710 slen = strlen (fn) + 1;
711 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
714 _("Can not index file `%s': %s. Will try to insert instead.\n"),
716 _("filename too long"));
718 p->data.file.do_index = GNUNET_NO;
719 publish_content (sc);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Hash of indexed file `%s' is `%s'\n",
725 p->data.file.filename,
728 client = GNUNET_CLIENT_connect (sc->h->sched,
733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
734 _("Can not index file `%s': %s. Will try to insert instead.\n"),
735 p->data.file.filename,
736 _("could not connect to `fs' service"));
737 p->data.file.do_index = GNUNET_NO;
738 publish_content (sc);
742 p->data.file.file_id = *res;
743 p->data.file.have_hash = GNUNET_YES;
744 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
746 ism->header.size = htons(sizeof(struct IndexStartMessage) +
748 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
750 GNUNET_DISK_file_get_identifiers (p->data.file.filename,
754 ism->device = htonl (dev);
755 ism->inode = GNUNET_htonll(ino);
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
760 _("Failed to get file identifiers for `%s'\n"),
761 p->data.file.filename);
769 GNUNET_break (GNUNET_YES ==
770 GNUNET_CLIENT_transmit_and_get_response (client,
772 GNUNET_TIME_UNIT_FOREVER_REL,
774 &process_index_start_response,
781 * Main function that performs the upload.
783 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
784 * @param tc task context
787 GNUNET_FS_publish_main_ (void *cls,
788 const struct GNUNET_SCHEDULER_TaskContext *tc)
790 struct GNUNET_FS_PublishContext *sc = cls;
791 struct GNUNET_FS_ProgressInfo pi;
792 struct GNUNET_FS_FileInformation *p;
795 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
799 /* upload of entire hierarchy complete,
800 publish namespace entries */
804 /* find starting position */
805 while ( (p->is_directory) &&
806 (NULL != p->data.dir.entries) &&
808 (NULL == p->data.dir.entries->chk_uri) )
810 p = p->data.dir.entries;
816 /* error with current file, abort all
817 related files as well! */
818 while (NULL != p->dir)
820 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
821 EXTRACTOR_METATYPE_FILENAME);
825 GNUNET_asprintf (&p->emsg,
826 _("Recursive upload failed at `%s': %s"),
833 GNUNET_asprintf (&p->emsg,
834 _("Recursive upload failed: %s"),
837 GNUNET_FS_file_information_sync (p);
838 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
839 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
840 pi.value.publish.specifics.error.message = p->emsg;
841 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
843 sc->all_done = GNUNET_YES;
846 /* handle completion */
847 if (NULL != p->chk_uri)
849 /* upload of "p" complete, publish KBlocks! */
850 if (p->keywords != NULL)
852 GNUNET_FS_publish_ksk (sc->h,
860 &publish_kblocks_cont,
865 publish_kblocks_cont (sc,
871 if ( (!p->is_directory) &&
872 (p->data.file.do_index) )
874 if (NULL == p->data.file.filename)
876 p->data.file.do_index = GNUNET_NO;
877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
878 _("Can not index file `%s': %s. Will try to insert instead.\n"),
880 _("needs to be an actual file"));
881 publish_content (sc);
884 if (p->data.file.have_hash)
885 hash_for_index_cb (sc,
886 &p->data.file.file_id);
889 p->start_time = GNUNET_TIME_absolute_get ();
890 GNUNET_CRYPTO_hash_file (sc->h->sched,
891 GNUNET_SCHEDULER_PRIORITY_IDLE,
892 p->data.file.filename,
899 publish_content (sc);
904 * Signal the FS's progress function that we are starting
907 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
908 * @param fi the entry in the publish-structure
909 * @param length length of the file or directory
910 * @param meta metadata for the file or directory (can be modified)
911 * @param uri pointer to the keywords that will be used for this entry (can be modified)
912 * @param anonymity pointer to selected anonymity level (can be modified)
913 * @param priority pointer to selected priority (can be modified)
914 * @param expirationTime pointer to selected expiration time (can be modified)
915 * @param client_info pointer to client context set upon creation (can be modified)
916 * @return GNUNET_OK to continue (always)
919 fip_signal_start(void *cls,
920 struct GNUNET_FS_FileInformation *fi,
922 struct GNUNET_CONTAINER_MetaData *meta,
923 struct GNUNET_FS_Uri **uri,
926 struct GNUNET_TIME_Absolute *expirationTime,
929 struct GNUNET_FS_PublishContext *sc = cls;
930 struct GNUNET_FS_ProgressInfo pi;
932 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
933 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
939 * Publish a file or directory.
941 * @param h handle to the file sharing subsystem
942 * @param fi information about the file or directory structure to publish
943 * @param namespace namespace to publish the file in, NULL for no namespace
944 * @param nid identifier to use for the publishd content in the namespace
945 * (can be NULL, must be NULL if namespace is NULL)
946 * @param nuid update-identifier that will be used for future updates
947 * (can be NULL, must be NULL if namespace or nid is NULL)
948 * @param options options for the publication
949 * @return context that can be used to control the publish operation
951 struct GNUNET_FS_PublishContext *
952 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
953 struct GNUNET_FS_FileInformation *fi,
954 struct GNUNET_FS_Namespace *namespace,
957 enum GNUNET_FS_PublishOptions options)
959 struct GNUNET_FS_PublishContext *ret;
960 struct GNUNET_DATASTORE_Handle *dsh;
962 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
964 dsh = GNUNET_DATASTORE_connect (h->cfg,
973 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
977 ret->namespace = namespace;
978 if (namespace != NULL)
981 GNUNET_assert (NULL != nid);
982 ret->nid = GNUNET_strdup (nid);
984 ret->nuid = GNUNET_strdup (nuid);
986 // FIXME: make upload persistent!
989 GNUNET_FS_file_information_inspect (ret->fi,
992 ret->fi_pos = ret->fi;
994 // FIXME: calculate space needed for "fi"
995 // and reserve as first task (then trigger
996 // "publish_main" from that continuation)!
998 = GNUNET_SCHEDULER_add_with_priority (h->sched,
999 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1000 &GNUNET_FS_publish_main_,
1007 * Signal the FS's progress function that we are stopping
1010 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1011 * @param fi the entry in the publish-structure
1012 * @param length length of the file or directory
1013 * @param meta metadata for the file or directory (can be modified)
1014 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1015 * @param anonymity pointer to selected anonymity level (can be modified)
1016 * @param priority pointer to selected priority (can be modified)
1017 * @param expirationTime pointer to selected expiration time (can be modified)
1018 * @param client_info pointer to client context set upon creation (can be modified)
1019 * @return GNUNET_OK to continue (always)
1022 fip_signal_stop(void *cls,
1023 struct GNUNET_FS_FileInformation *fi,
1025 struct GNUNET_CONTAINER_MetaData *meta,
1026 struct GNUNET_FS_Uri **uri,
1027 uint32_t *anonymity,
1029 struct GNUNET_TIME_Absolute *expirationTime,
1032 struct GNUNET_FS_PublishContext*sc = cls;
1033 struct GNUNET_FS_ProgressInfo pi;
1036 if (fi->serialization != NULL)
1038 if (0 != UNLINK (fi->serialization))
1040 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1044 GNUNET_free (fi->serialization);
1045 fi->serialization = NULL;
1047 off = (fi->chk_uri == NULL) ? 0 : length;
1048 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1049 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1050 *client_info = NULL;
1056 * Stop an upload. Will abort incomplete uploads (but
1057 * not remove blocks that have already been publishd) or
1058 * simply clean up the state for completed uploads.
1059 * Must NOT be called from within the event callback!
1061 * @param pc context for the upload to stop
1064 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1066 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1067 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1068 if (pc->serialization != NULL)
1070 if (0 != UNLINK (pc->serialization))
1071 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1074 GNUNET_free (pc->serialization);
1075 pc->serialization = NULL;
1077 GNUNET_FS_file_information_inspect (pc->fi,
1080 if (GNUNET_YES == pc->in_network_wait)
1082 pc->in_network_wait = GNUNET_SYSERR;
1085 publish_cleanup (pc);
1090 * Context for the KSK publication.
1092 struct PublishKskContext
1098 struct GNUNET_FS_Uri *ksk_uri;
1101 * Global FS context.
1103 struct GNUNET_FS_Handle *h;
1106 * The master block that we are sending
1107 * (in plaintext), has "mdsize+slen" more
1108 * bytes than the struct would suggest.
1113 * Buffer of the same size as "kb" for
1114 * the encrypted version.
1119 * Handle to the datastore, NULL if we are just
1122 struct GNUNET_DATASTORE_Handle *dsh;
1125 * Function to call once we're done.
1127 GNUNET_FS_PublishContinuation cont;
1135 * When should the KBlocks expire?
1137 struct GNUNET_TIME_Absolute expirationTime;
1140 * Size of the serialized metadata.
1145 * Size of the (CHK) URI as a string.
1150 * Keyword that we are currently processing.
1155 * Anonymity level for the KBlocks.
1160 * Priority for the KBlocks.
1167 * Continuation of "GNUNET_FS_publish_ksk" that performs
1168 * the actual publishing operation (iterating over all
1171 * @param cls closure of type "struct PublishKskContext*"
1175 publish_ksk_cont (void *cls,
1176 const struct GNUNET_SCHEDULER_TaskContext *tc);
1180 * Function called by the datastore API with
1181 * the result from the PUT request.
1183 * @param cls closure of type "struct PublishKskContext*"
1184 * @param success GNUNET_OK on success
1185 * @param msg error message (or NULL)
1188 kb_put_cont (void *cls,
1192 struct PublishKskContext *pkc = cls;
1194 if (GNUNET_OK != success)
1196 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1197 GNUNET_free (pkc->cpy);
1198 GNUNET_free (pkc->kb);
1199 pkc->cont (pkc->cont_cls,
1202 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1206 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1209 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1214 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1215 * publishing operation (iterating over all of the keywords).
1217 * @param cls closure of type "struct PublishKskContext*"
1221 publish_ksk_cont (void *cls,
1222 const struct GNUNET_SCHEDULER_TaskContext *tc)
1224 struct PublishKskContext *pkc = cls;
1225 const char *keyword;
1226 GNUNET_HashCode key;
1227 GNUNET_HashCode query;
1228 struct GNUNET_CRYPTO_AesSessionKey skey;
1229 struct GNUNET_CRYPTO_AesInitializationVector iv;
1230 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1233 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1234 (NULL == pkc->dsh) )
1236 if (NULL != pkc->dsh)
1237 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1238 GNUNET_free (pkc->cpy);
1239 GNUNET_free (pkc->kb);
1240 pkc->cont (pkc->cont_cls,
1243 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1247 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1248 /* first character of keyword indicates if it is
1249 mandatory or not -- ignore for hashing */
1250 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1251 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1252 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1253 pkc->slen + pkc->mdsize,
1257 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1258 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1259 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1260 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1262 GNUNET_assert (GNUNET_OK ==
1263 GNUNET_CRYPTO_rsa_sign (pk,
1265 &pkc->cpy->signature));
1266 GNUNET_CRYPTO_rsa_key_free (pk);
1267 GNUNET_DATASTORE_put (pkc->dsh,
1271 sizeof (struct KBlock) +
1274 GNUNET_BLOCK_TYPE_KBLOCK,
1277 pkc->expirationTime,
1278 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1285 * Publish a CHK under various keywords on GNUnet.
1287 * @param h handle to the file sharing subsystem
1288 * @param ksk_uri keywords to use
1289 * @param meta metadata to use
1290 * @param uri URI to refer to in the KBlock
1291 * @param expirationTime when the KBlock expires
1292 * @param anonymity anonymity level for the KBlock
1293 * @param priority priority for the KBlock
1294 * @param options publication options
1295 * @param cont continuation
1296 * @param cont_cls closure for cont
1299 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1300 const struct GNUNET_FS_Uri *ksk_uri,
1301 const struct GNUNET_CONTAINER_MetaData *meta,
1302 const struct GNUNET_FS_Uri *uri,
1303 struct GNUNET_TIME_Absolute expirationTime,
1306 enum GNUNET_FS_PublishOptions options,
1307 GNUNET_FS_PublishContinuation cont,
1310 struct PublishKskContext *pkc;
1316 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1318 pkc->expirationTime = expirationTime;
1319 pkc->anonymity = anonymity;
1320 pkc->priority = priority;
1322 pkc->cont_cls = cont_cls;
1323 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1325 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1327 if (pkc->dsh == NULL)
1329 cont (cont_cls, NULL, _("Could not connect to datastore."));
1337 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1338 GNUNET_assert (pkc->mdsize >= 0);
1339 uris = GNUNET_FS_uri_to_string (uri);
1340 pkc->slen = strlen (uris) + 1;
1341 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1342 if (size > MAX_KBLOCK_SIZE)
1344 size = MAX_KBLOCK_SIZE;
1345 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1347 pkc->kb = GNUNET_malloc (size);
1348 kbe = (char *) &pkc->kb[1];
1349 memcpy (kbe, uris, pkc->slen);
1351 sptr = &kbe[pkc->slen];
1353 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1356 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1357 if (pkc->mdsize == -1)
1360 GNUNET_free (pkc->kb);
1361 if (pkc->dsh != NULL)
1362 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1363 cont (cont_cls, NULL, _("Internal error."));
1367 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1369 pkc->cpy = GNUNET_malloc (size);
1370 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1371 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1374 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1375 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1376 GNUNET_SCHEDULER_add_continuation (h->sched,
1379 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1384 * Context for the SKS publication.
1386 struct PublishSksContext
1390 * Global FS context.
1392 struct GNUNET_FS_Uri *uri;
1395 * Handle to the datastore.
1397 struct GNUNET_DATASTORE_Handle *dsh;
1400 * Function to call once we're done.
1402 GNUNET_FS_PublishContinuation cont;
1413 * Function called by the datastore API with
1414 * the result from the PUT (SBlock) request.
1416 * @param cls closure of type "struct PublishSksContext*"
1417 * @param success GNUNET_OK on success
1418 * @param msg error message (or NULL)
1421 sb_put_cont (void *cls,
1425 struct PublishSksContext *psc = cls;
1427 if (NULL != psc->dsh)
1428 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1429 if (GNUNET_OK != success)
1430 psc->cont (psc->cont_cls,
1434 psc->cont (psc->cont_cls,
1437 GNUNET_FS_uri_destroy (psc->uri);
1443 * Publish an SBlock on GNUnet.
1445 * @param h handle to the file sharing subsystem
1446 * @param namespace namespace to publish in
1447 * @param identifier identifier to use
1448 * @param update update identifier to use
1449 * @param meta metadata to use
1450 * @param uri URI to refer to in the SBlock
1451 * @param expirationTime when the SBlock expires
1452 * @param anonymity anonymity level for the SBlock
1453 * @param priority priority for the SBlock
1454 * @param options publication options
1455 * @param cont continuation
1456 * @param cont_cls closure for cont
1459 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1460 struct GNUNET_FS_Namespace *namespace,
1461 const char *identifier,
1463 const struct GNUNET_CONTAINER_MetaData *meta,
1464 const struct GNUNET_FS_Uri *uri,
1465 struct GNUNET_TIME_Absolute expirationTime,
1468 enum GNUNET_FS_PublishOptions options,
1469 GNUNET_FS_PublishContinuation cont,
1472 struct PublishSksContext *psc;
1473 struct GNUNET_CRYPTO_AesSessionKey sk;
1474 struct GNUNET_CRYPTO_AesInitializationVector iv;
1475 struct GNUNET_FS_Uri *sks_uri;
1483 struct SBlock *sb_enc;
1485 struct GNUNET_CONTAINER_MetaData *mmeta;
1486 GNUNET_HashCode key; /* hash of thisId = key */
1487 GNUNET_HashCode id; /* hash of hc = identifier */
1488 GNUNET_HashCode query; /* id ^ nsid = DB query */
1491 mmeta = GNUNET_CONTAINER_meta_data_create ();
1493 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1494 uris = GNUNET_FS_uri_to_string (uri);
1495 slen = strlen (uris) + 1;
1496 idlen = strlen (identifier);
1499 nidlen = strlen (update) + 1;
1500 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1501 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1502 if (size > MAX_SBLOCK_SIZE)
1504 size = MAX_SBLOCK_SIZE;
1505 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1507 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1508 dest = (char *) &sb[1];
1509 memcpy (dest, update, nidlen);
1511 memcpy (dest, uris, slen);
1513 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1516 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1517 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1525 _("Internal error."));
1528 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1529 sb_enc = GNUNET_malloc (size);
1530 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1531 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1532 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1533 sks_uri->type = sks;
1534 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1535 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1536 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1537 &sks_uri->data.sks.namespace);
1538 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1539 GNUNET_CRYPTO_hash_xor (&id,
1540 &sks_uri->data.sks.namespace,
1541 &sb_enc->identifier);
1542 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1543 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1544 size - sizeof (struct SBlock),
1548 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1549 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1550 + sizeof(struct SBlock)
1551 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1552 GNUNET_assert (GNUNET_OK ==
1553 GNUNET_CRYPTO_rsa_sign (namespace->key,
1555 &sb_enc->signature));
1556 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1559 psc->cont_cls = cont_cls;
1560 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1562 GNUNET_free (sb_enc);
1569 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1570 if (NULL == psc->dsh)
1572 GNUNET_free (sb_enc);
1576 _("Failed to connect to datastore."));
1579 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1582 GNUNET_DATASTORE_put (psc->dsh,
1584 &sb_enc->identifier,
1587 GNUNET_BLOCK_TYPE_SBLOCK,
1591 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1596 GNUNET_free (sb_enc);
1599 /* end of fs_publish.c */