2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_publish.c
23 * @brief publish a file or directory in GNUnet
24 * @see http://gnunet.org/encoding
25 * @author Krista Bennett
26 * @author Christian Grothoff
29 * - indexing cleanup: unindex on failure (can wait)
30 * - datastore reservation support (optimization)
31 * - location URIs (publish with anonymity-level zero)
35 #include "gnunet_constants.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_fs_service.h"
42 #define DEBUG_PUBLISH GNUNET_NO
46 * Context for "ds_put_cont".
51 * Current publishing context.
53 struct GNUNET_FS_PublishContext *sc;
56 * Specific file with the block.
58 struct GNUNET_FS_FileInformation *p;
61 * Function to run next, if any (can be NULL).
63 GNUNET_SCHEDULER_Task cont;
73 * Fill in all of the generic fields for
74 * a publish event and call the callback.
76 * @param pi structure to fill in
77 * @param sc overall publishing context
78 * @param p file information for the file being published
79 * @param offset where in the file are we so far
80 * @return value returned from callback
83 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
84 struct GNUNET_FS_PublishContext *sc,
85 const struct GNUNET_FS_FileInformation *p,
88 pi->value.publish.sc = sc;
89 pi->value.publish.fi = p;
90 pi->value.publish.cctx
92 pi->value.publish.pctx
93 = (NULL == p->dir) ? NULL : p->dir->client_info;
94 pi->value.publish.filename = p->filename;
95 pi->value.publish.size
96 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
98 = GNUNET_TIME_calculate_eta (p->start_time,
100 pi->value.publish.size);
101 pi->value.publish.completed = offset;
102 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
103 pi->value.publish.anonymity = p->anonymity;
104 return sc->h->upcb (sc->h->upcb_cls,
110 * Cleanup the publish context, we're done with it.
112 * @param pc struct to clean up after
115 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
117 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
118 if (pc->namespace != NULL)
119 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
120 GNUNET_free_non_null (pc->nid);
121 GNUNET_free_non_null (pc->nuid);
122 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 *pc = cls;
238 signal_publish_error (pc->fi,
241 GNUNET_FS_publish_sync_ (pc);
244 // FIXME: release the datastore reserve here!
245 signal_publish_completion (pc->fi, pc);
246 pc->all_done = GNUNET_YES;
247 GNUNET_FS_publish_sync_ (pc);
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 *pc = cls;
293 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
297 signal_publish_error (p, pc, emsg);
298 GNUNET_FS_file_information_sync_ (p);
299 GNUNET_FS_publish_sync_ (pc);
301 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
302 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
303 &GNUNET_FS_publish_main_,
308 signal_publish_completion (p, pc);
309 /* move on to next file */
311 pc->fi_pos = p->next;
314 GNUNET_FS_publish_sync_ (pc);
316 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
317 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
318 &GNUNET_FS_publish_main_,
324 * Function called by the tree encoder to obtain
325 * a block of plaintext data (for the lowest level
328 * @param cls our publishing context
329 * @param offset identifies which block to get
330 * @param max (maximum) number of bytes to get; returning
331 * fewer will also cause errors
332 * @param buf where to copy the plaintext buffer
333 * @param emsg location to store an error message (on error)
334 * @return number of bytes copied to buf, 0 on error
337 block_reader (void *cls,
343 struct GNUNET_FS_PublishContext *sc = cls;
344 struct GNUNET_FS_FileInformation *p;
351 pt_size = GNUNET_MIN(max,
352 p->data.dir.dir_size - offset);
353 dd = p->data.dir.dir_data;
360 pt_size = GNUNET_MIN(max,
361 p->data.file.file_size - offset);
363 return 0; /* calling reader with pt_size==0
364 might free buf, so don't! */
366 p->data.file.reader (p->data.file.reader_cls,
378 * The tree encoder has finished processing a
379 * file. Call it's finish method and deal with
382 * @param cls our publishing context
383 * @param tc scheduler's task context (not used)
386 encode_cont (void *cls,
387 const struct GNUNET_SCHEDULER_TaskContext *tc)
389 struct GNUNET_FS_PublishContext *sc = cls;
390 struct GNUNET_FS_FileInformation *p;
391 struct GNUNET_FS_ProgressInfo pi;
395 GNUNET_FS_tree_encoder_finish (p->te,
401 GNUNET_asprintf (&p->emsg,
402 _("Upload failed: %s"),
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;
561 GNUNET_assert (p != NULL);
566 db = GNUNET_FS_directory_builder_create (p->meta);
567 dirpos = p->data.dir.entries;
568 while (NULL != dirpos)
570 if (dirpos->is_directory)
572 raw_data = dirpos->data.dir.dir_data;
573 dirpos->data.dir.dir_data = NULL;
578 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
579 (dirpos->data.file.file_size > 0) )
581 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
583 if (dirpos->data.file.file_size !=
584 dirpos->data.file.reader (dirpos->data.file.reader_cls,
586 dirpos->data.file.file_size,
590 GNUNET_free_non_null (emsg);
591 GNUNET_free (raw_data);
596 GNUNET_FS_directory_builder_add (db,
600 GNUNET_free_non_null (raw_data);
601 dirpos = dirpos->next;
603 GNUNET_FS_directory_builder_finish (db,
604 &p->data.dir.dir_size,
605 &p->data.dir.dir_data);
606 GNUNET_FS_file_information_sync_ (p);
608 size = (p->is_directory)
609 ? p->data.dir.dir_size
610 : p->data.file.file_size;
611 p->te = GNUNET_FS_tree_encoder_create (sc->h,
620 GNUNET_FS_tree_encoder_next (p->te);
625 * Process the response (or lack thereof) from
626 * the "fs" service to our 'start index' request.
628 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
629 * @param msg the response we got
632 process_index_start_response (void *cls,
633 const struct GNUNET_MessageHeader *msg)
635 struct GNUNET_FS_PublishContext *sc = cls;
636 struct GNUNET_FS_FileInformation *p;
640 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 _("Can not index file `%s': %s. Will try to insert instead.\n"),
648 _("timeout on index-start request to `fs' service"));
649 p->data.file.do_index = GNUNET_NO;
650 GNUNET_FS_file_information_sync_ (p);
651 publish_content (sc);
654 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
656 msize = ntohs (msg->size);
657 emsg = (const char *) &msg[1];
658 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
659 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
660 emsg = gettext_noop ("unknown error");
661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
662 _("Can not index file `%s': %s. Will try to insert instead.\n"),
665 p->data.file.do_index = GNUNET_NO;
666 GNUNET_FS_file_information_sync_ (p);
667 publish_content (sc);
670 p->data.file.index_start_confirmed = GNUNET_YES;
671 /* success! continue with indexing */
672 GNUNET_FS_file_information_sync_ (p);
673 publish_content (sc);
678 * Function called once the hash computation over an
679 * indexed file has completed.
681 * @param cls closure, our publishing context
682 * @param res resulting hash, NULL on error
685 hash_for_index_cb (void *cls,
686 const GNUNET_HashCode *
689 struct GNUNET_FS_PublishContext *sc = cls;
690 struct GNUNET_FS_FileInformation *p;
691 struct IndexStartMessage *ism;
693 struct GNUNET_CLIENT_Connection *client;
701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
702 _("Can not index file `%s': %s. Will try to insert instead.\n"),
704 _("failed to compute hash"));
705 p->data.file.do_index = GNUNET_NO;
706 GNUNET_FS_file_information_sync_ (p);
707 publish_content (sc);
710 if (GNUNET_YES == p->data.file.index_start_confirmed)
712 publish_content (sc);
715 fn = GNUNET_STRINGS_filename_expand (p->filename);
716 slen = strlen (fn) + 1;
717 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
720 _("Can not index file `%s': %s. Will try to insert instead.\n"),
722 _("filename too long"));
724 p->data.file.do_index = GNUNET_NO;
725 GNUNET_FS_file_information_sync_ (p);
726 publish_content (sc);
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Hash of indexed file `%s' is `%s'\n",
732 p->data.file.filename,
735 client = GNUNET_CLIENT_connect (sc->h->sched,
740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
741 _("Can not index file `%s': %s. Will try to insert instead.\n"),
743 _("could not connect to `fs' service"));
744 p->data.file.do_index = GNUNET_NO;
745 publish_content (sc);
749 if (p->data.file.have_hash != GNUNET_YES)
751 p->data.file.file_id = *res;
752 p->data.file.have_hash = GNUNET_YES;
753 GNUNET_FS_file_information_sync_ (p);
755 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
757 ism->header.size = htons(sizeof(struct IndexStartMessage) +
759 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
761 GNUNET_DISK_file_get_identifiers (p->filename,
765 ism->device = htonl (dev);
766 ism->inode = GNUNET_htonll(ino);
770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
771 _("Failed to get file identifiers for `%s'\n"),
780 GNUNET_break (GNUNET_YES ==
781 GNUNET_CLIENT_transmit_and_get_response (client,
783 GNUNET_TIME_UNIT_FOREVER_REL,
785 &process_index_start_response,
792 * Main function that performs the upload.
794 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
795 * @param tc task context
798 GNUNET_FS_publish_main_ (void *cls,
799 const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct GNUNET_FS_PublishContext *pc = cls;
802 struct GNUNET_FS_ProgressInfo pi;
803 struct GNUNET_FS_FileInformation *p;
806 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
810 /* upload of entire hierarchy complete,
811 publish namespace entries */
812 GNUNET_FS_publish_sync_ (pc);
816 /* find starting position */
817 while ( (p->is_directory) &&
818 (NULL != p->data.dir.entries) &&
820 (NULL == p->data.dir.entries->chk_uri) )
822 p = p->data.dir.entries;
824 GNUNET_FS_publish_sync_ (pc);
829 /* error with current file, abort all
830 related files as well! */
831 while (NULL != p->dir)
833 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
834 EXTRACTOR_METATYPE_FILENAME);
838 GNUNET_asprintf (&p->emsg,
839 _("Recursive upload failed at `%s': %s"),
846 GNUNET_asprintf (&p->emsg,
847 _("Recursive upload failed: %s"),
850 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
851 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
852 pi.value.publish.specifics.error.message = p->emsg;
853 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
855 pc->all_done = GNUNET_YES;
856 GNUNET_FS_publish_sync_ (pc);
859 /* handle completion */
860 if (NULL != p->chk_uri)
862 GNUNET_FS_publish_sync_ (pc);
863 /* upload of "p" complete, publish KBlocks! */
864 if (p->keywords != NULL)
866 GNUNET_FS_publish_ksk (pc->h,
874 &publish_kblocks_cont,
879 publish_kblocks_cont (pc,
885 if ( (!p->is_directory) &&
886 (p->data.file.do_index) )
888 if (NULL == p->filename)
890 p->data.file.do_index = GNUNET_NO;
891 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
892 _("Can not index file `%s': %s. Will try to insert instead.\n"),
894 _("needs to be an actual file"));
895 GNUNET_FS_file_information_sync_ (p);
896 publish_content (pc);
899 if (p->data.file.have_hash)
901 hash_for_index_cb (pc,
902 &p->data.file.file_id);
906 p->start_time = GNUNET_TIME_absolute_get ();
907 GNUNET_CRYPTO_hash_file (pc->h->sched,
908 GNUNET_SCHEDULER_PRIORITY_IDLE,
916 publish_content (pc);
921 * Signal the FS's progress function that we are starting
924 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
925 * @param fi the entry in the publish-structure
926 * @param length length of the file or directory
927 * @param meta metadata for the file or directory (can be modified)
928 * @param uri pointer to the keywords that will be used for this entry (can be modified)
929 * @param anonymity pointer to selected anonymity level (can be modified)
930 * @param priority pointer to selected priority (can be modified)
931 * @param expirationTime pointer to selected expiration time (can be modified)
932 * @param client_info pointer to client context set upon creation (can be modified)
933 * @return GNUNET_OK to continue (always)
936 fip_signal_start(void *cls,
937 struct GNUNET_FS_FileInformation *fi,
939 struct GNUNET_CONTAINER_MetaData *meta,
940 struct GNUNET_FS_Uri **uri,
943 struct GNUNET_TIME_Absolute *expirationTime,
946 struct GNUNET_FS_PublishContext *sc = cls;
947 struct GNUNET_FS_ProgressInfo pi;
949 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
950 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
951 GNUNET_FS_file_information_sync_ (fi);
957 * Signal the FS's progress function that we are suspending
960 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
961 * @param fi the entry in the publish-structure
962 * @param length length of the file or directory
963 * @param meta metadata for the file or directory (can be modified)
964 * @param uri pointer to the keywords that will be used for this entry (can be modified)
965 * @param anonymity pointer to selected anonymity level (can be modified)
966 * @param priority pointer to selected priority (can be modified)
967 * @param expirationTime pointer to selected expiration time (can be modified)
968 * @param client_info pointer to client context set upon creation (can be modified)
969 * @return GNUNET_OK to continue (always)
972 fip_signal_suspend(void *cls,
973 struct GNUNET_FS_FileInformation *fi,
975 struct GNUNET_CONTAINER_MetaData *meta,
976 struct GNUNET_FS_Uri **uri,
979 struct GNUNET_TIME_Absolute *expirationTime,
982 struct GNUNET_FS_PublishContext*sc = cls;
983 struct GNUNET_FS_ProgressInfo pi;
986 GNUNET_free_non_null (fi->serialization);
987 fi->serialization = NULL;
988 off = (fi->chk_uri == NULL) ? 0 : length;
989 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
990 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
997 * Create SUSPEND event for the given publish operation
998 * and then clean up our state (without stop signal).
1000 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1003 GNUNET_FS_publish_signal_suspend_ (void *cls)
1005 struct GNUNET_FS_PublishContext *pc = cls;
1007 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1009 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1010 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1012 GNUNET_FS_file_information_inspect (pc->fi,
1013 &fip_signal_suspend,
1015 GNUNET_FS_end_top (pc->h, pc->top);
1016 publish_cleanup (pc);
1020 * Publish a file or directory.
1022 * @param h handle to the file sharing subsystem
1023 * @param fi information about the file or directory structure to publish
1024 * @param namespace namespace to publish the file in, NULL for no namespace
1025 * @param nid identifier to use for the publishd content in the namespace
1026 * (can be NULL, must be NULL if namespace is NULL)
1027 * @param nuid update-identifier that will be used for future updates
1028 * (can be NULL, must be NULL if namespace or nid is NULL)
1029 * @param options options for the publication
1030 * @return context that can be used to control the publish operation
1032 struct GNUNET_FS_PublishContext *
1033 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1034 struct GNUNET_FS_FileInformation *fi,
1035 struct GNUNET_FS_Namespace *namespace,
1038 enum GNUNET_FS_PublishOptions options)
1040 struct GNUNET_FS_PublishContext *ret;
1041 struct GNUNET_DATASTORE_Handle *dsh;
1043 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1045 dsh = GNUNET_DATASTORE_connect (h->cfg,
1054 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1058 ret->namespace = namespace;
1059 if (namespace != NULL)
1062 GNUNET_assert (NULL != nid);
1063 ret->nid = GNUNET_strdup (nid);
1065 ret->nuid = GNUNET_strdup (nuid);
1068 GNUNET_FS_file_information_inspect (ret->fi,
1071 ret->fi_pos = ret->fi;
1072 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1073 GNUNET_FS_publish_sync_ (ret);
1074 // FIXME: calculate space needed for "fi"
1075 // and reserve as first task (then trigger
1076 // "publish_main" from that continuation)!
1078 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1079 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1080 &GNUNET_FS_publish_main_,
1087 * Signal the FS's progress function that we are stopping
1090 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1091 * @param fi the entry in the publish-structure
1092 * @param length length of the file or directory
1093 * @param meta metadata for the file or directory (can be modified)
1094 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1095 * @param anonymity pointer to selected anonymity level (can be modified)
1096 * @param priority pointer to selected priority (can be modified)
1097 * @param expirationTime pointer to selected expiration time (can be modified)
1098 * @param client_info pointer to client context set upon creation (can be modified)
1099 * @return GNUNET_OK to continue (always)
1102 fip_signal_stop(void *cls,
1103 struct GNUNET_FS_FileInformation *fi,
1105 struct GNUNET_CONTAINER_MetaData *meta,
1106 struct GNUNET_FS_Uri **uri,
1107 uint32_t *anonymity,
1109 struct GNUNET_TIME_Absolute *expirationTime,
1112 struct GNUNET_FS_PublishContext*sc = cls;
1113 struct GNUNET_FS_ProgressInfo pi;
1116 if (fi->serialization != NULL)
1118 if (0 != UNLINK (fi->serialization))
1120 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1124 GNUNET_free (fi->serialization);
1125 fi->serialization = NULL;
1127 off = (fi->chk_uri == NULL) ? 0 : length;
1128 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1129 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1130 *client_info = NULL;
1136 * Stop an upload. Will abort incomplete uploads (but
1137 * not remove blocks that have already been publishd) or
1138 * simply clean up the state for completed uploads.
1139 * Must NOT be called from within the event callback!
1141 * @param pc context for the upload to stop
1144 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1146 GNUNET_FS_end_top (pc->h, pc->top);
1147 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1149 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1150 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1152 if (pc->serialization != NULL)
1154 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1155 GNUNET_free (pc->serialization);
1156 pc->serialization = NULL;
1158 GNUNET_FS_file_information_inspect (pc->fi,
1161 if (GNUNET_YES == pc->in_network_wait)
1163 pc->in_network_wait = GNUNET_SYSERR;
1166 publish_cleanup (pc);
1171 * Context for the KSK publication.
1173 struct PublishKskContext
1179 struct GNUNET_FS_Uri *ksk_uri;
1182 * Global FS context.
1184 struct GNUNET_FS_Handle *h;
1187 * The master block that we are sending
1188 * (in plaintext), has "mdsize+slen" more
1189 * bytes than the struct would suggest.
1194 * Buffer of the same size as "kb" for
1195 * the encrypted version.
1200 * Handle to the datastore, NULL if we are just
1203 struct GNUNET_DATASTORE_Handle *dsh;
1206 * Function to call once we're done.
1208 GNUNET_FS_PublishContinuation cont;
1216 * When should the KBlocks expire?
1218 struct GNUNET_TIME_Absolute expirationTime;
1221 * Size of the serialized metadata.
1226 * Size of the (CHK) URI as a string.
1231 * Keyword that we are currently processing.
1236 * Anonymity level for the KBlocks.
1241 * Priority for the KBlocks.
1248 * Continuation of "GNUNET_FS_publish_ksk" that performs
1249 * the actual publishing operation (iterating over all
1252 * @param cls closure of type "struct PublishKskContext*"
1256 publish_ksk_cont (void *cls,
1257 const struct GNUNET_SCHEDULER_TaskContext *tc);
1261 * Function called by the datastore API with
1262 * the result from the PUT request.
1264 * @param cls closure of type "struct PublishKskContext*"
1265 * @param success GNUNET_OK on success
1266 * @param msg error message (or NULL)
1269 kb_put_cont (void *cls,
1273 struct PublishKskContext *pkc = cls;
1275 if (GNUNET_OK != success)
1277 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1278 GNUNET_free (pkc->cpy);
1279 GNUNET_free (pkc->kb);
1280 pkc->cont (pkc->cont_cls,
1283 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1287 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1290 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1295 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1296 * publishing operation (iterating over all of the keywords).
1298 * @param cls closure of type "struct PublishKskContext*"
1302 publish_ksk_cont (void *cls,
1303 const struct GNUNET_SCHEDULER_TaskContext *tc)
1305 struct PublishKskContext *pkc = cls;
1306 const char *keyword;
1307 GNUNET_HashCode key;
1308 GNUNET_HashCode query;
1309 struct GNUNET_CRYPTO_AesSessionKey skey;
1310 struct GNUNET_CRYPTO_AesInitializationVector iv;
1311 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1314 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1315 (NULL == pkc->dsh) )
1317 if (NULL != pkc->dsh)
1318 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1319 GNUNET_free (pkc->cpy);
1320 GNUNET_free (pkc->kb);
1321 pkc->cont (pkc->cont_cls,
1324 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1328 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1329 /* first character of keyword indicates if it is
1330 mandatory or not -- ignore for hashing */
1331 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1332 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1333 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1334 pkc->slen + pkc->mdsize,
1338 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1339 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1340 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1341 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1343 GNUNET_assert (GNUNET_OK ==
1344 GNUNET_CRYPTO_rsa_sign (pk,
1346 &pkc->cpy->signature));
1347 GNUNET_CRYPTO_rsa_key_free (pk);
1348 GNUNET_DATASTORE_put (pkc->dsh,
1352 sizeof (struct KBlock) +
1355 GNUNET_BLOCK_TYPE_KBLOCK,
1358 pkc->expirationTime,
1359 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1366 * Publish a CHK under various keywords on GNUnet.
1368 * @param h handle to the file sharing subsystem
1369 * @param ksk_uri keywords to use
1370 * @param meta metadata to use
1371 * @param uri URI to refer to in the KBlock
1372 * @param expirationTime when the KBlock expires
1373 * @param anonymity anonymity level for the KBlock
1374 * @param priority priority for the KBlock
1375 * @param options publication options
1376 * @param cont continuation
1377 * @param cont_cls closure for cont
1380 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1381 const struct GNUNET_FS_Uri *ksk_uri,
1382 const struct GNUNET_CONTAINER_MetaData *meta,
1383 const struct GNUNET_FS_Uri *uri,
1384 struct GNUNET_TIME_Absolute expirationTime,
1387 enum GNUNET_FS_PublishOptions options,
1388 GNUNET_FS_PublishContinuation cont,
1391 struct PublishKskContext *pkc;
1397 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1399 pkc->expirationTime = expirationTime;
1400 pkc->anonymity = anonymity;
1401 pkc->priority = priority;
1403 pkc->cont_cls = cont_cls;
1404 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1406 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1408 if (pkc->dsh == NULL)
1410 cont (cont_cls, NULL, _("Could not connect to datastore."));
1418 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1419 GNUNET_assert (pkc->mdsize >= 0);
1420 uris = GNUNET_FS_uri_to_string (uri);
1421 pkc->slen = strlen (uris) + 1;
1422 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1423 if (size > MAX_KBLOCK_SIZE)
1425 size = MAX_KBLOCK_SIZE;
1426 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1428 pkc->kb = GNUNET_malloc (size);
1429 kbe = (char *) &pkc->kb[1];
1430 memcpy (kbe, uris, pkc->slen);
1432 sptr = &kbe[pkc->slen];
1434 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1437 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1438 if (pkc->mdsize == -1)
1441 GNUNET_free (pkc->kb);
1442 if (pkc->dsh != NULL)
1443 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1444 cont (cont_cls, NULL, _("Internal error."));
1448 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1450 pkc->cpy = GNUNET_malloc (size);
1451 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1452 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1455 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1456 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1457 GNUNET_SCHEDULER_add_continuation (h->sched,
1460 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1465 * Context for the SKS publication.
1467 struct PublishSksContext
1471 * Global FS context.
1473 struct GNUNET_FS_Uri *uri;
1476 * Handle to the datastore.
1478 struct GNUNET_DATASTORE_Handle *dsh;
1481 * Function to call once we're done.
1483 GNUNET_FS_PublishContinuation cont;
1494 * Function called by the datastore API with
1495 * the result from the PUT (SBlock) request.
1497 * @param cls closure of type "struct PublishSksContext*"
1498 * @param success GNUNET_OK on success
1499 * @param msg error message (or NULL)
1502 sb_put_cont (void *cls,
1506 struct PublishSksContext *psc = cls;
1508 if (NULL != psc->dsh)
1509 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1510 if (GNUNET_OK != success)
1511 psc->cont (psc->cont_cls,
1515 psc->cont (psc->cont_cls,
1518 GNUNET_FS_uri_destroy (psc->uri);
1524 * Publish an SBlock on GNUnet.
1526 * @param h handle to the file sharing subsystem
1527 * @param namespace namespace to publish in
1528 * @param identifier identifier to use
1529 * @param update update identifier to use
1530 * @param meta metadata to use
1531 * @param uri URI to refer to in the SBlock
1532 * @param expirationTime when the SBlock expires
1533 * @param anonymity anonymity level for the SBlock
1534 * @param priority priority for the SBlock
1535 * @param options publication options
1536 * @param cont continuation
1537 * @param cont_cls closure for cont
1540 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1541 struct GNUNET_FS_Namespace *namespace,
1542 const char *identifier,
1544 const struct GNUNET_CONTAINER_MetaData *meta,
1545 const struct GNUNET_FS_Uri *uri,
1546 struct GNUNET_TIME_Absolute expirationTime,
1549 enum GNUNET_FS_PublishOptions options,
1550 GNUNET_FS_PublishContinuation cont,
1553 struct PublishSksContext *psc;
1554 struct GNUNET_CRYPTO_AesSessionKey sk;
1555 struct GNUNET_CRYPTO_AesInitializationVector iv;
1556 struct GNUNET_FS_Uri *sks_uri;
1564 struct SBlock *sb_enc;
1566 struct GNUNET_CONTAINER_MetaData *mmeta;
1567 GNUNET_HashCode key; /* hash of thisId = key */
1568 GNUNET_HashCode id; /* hash of hc = identifier */
1569 GNUNET_HashCode query; /* id ^ nsid = DB query */
1572 mmeta = GNUNET_CONTAINER_meta_data_create ();
1574 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1575 uris = GNUNET_FS_uri_to_string (uri);
1576 slen = strlen (uris) + 1;
1577 idlen = strlen (identifier);
1580 nidlen = strlen (update) + 1;
1581 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1582 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1583 if (size > MAX_SBLOCK_SIZE)
1585 size = MAX_SBLOCK_SIZE;
1586 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1588 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1589 dest = (char *) &sb[1];
1590 memcpy (dest, update, nidlen);
1592 memcpy (dest, uris, slen);
1594 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1597 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1598 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1606 _("Internal error."));
1609 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1610 sb_enc = GNUNET_malloc (size);
1611 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1612 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1613 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1614 sks_uri->type = sks;
1615 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1616 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1617 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1618 &sks_uri->data.sks.namespace);
1619 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1620 GNUNET_CRYPTO_hash_xor (&id,
1621 &sks_uri->data.sks.namespace,
1622 &sb_enc->identifier);
1623 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1624 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1625 size - sizeof (struct SBlock),
1629 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1630 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1631 + sizeof(struct SBlock)
1632 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1633 GNUNET_assert (GNUNET_OK ==
1634 GNUNET_CRYPTO_rsa_sign (namespace->key,
1636 &sb_enc->signature));
1637 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1640 psc->cont_cls = cont_cls;
1641 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1643 GNUNET_free (sb_enc);
1650 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1651 if (NULL == psc->dsh)
1653 GNUNET_free (sb_enc);
1657 _("Failed to connect to datastore."));
1660 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1663 GNUNET_DATASTORE_put (psc->dsh,
1665 &sb_enc->identifier,
1668 GNUNET_BLOCK_TYPE_SBLOCK,
1672 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1677 GNUNET_free (sb_enc);
1680 /* end of fs_publish.c */