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);
123 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
124 if (pc->client != NULL)
125 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
131 * Function called by the datastore API with
132 * the result from the PUT request.
134 * @param cls our closure
135 * @param success GNUNET_OK on success
136 * @param msg error message (or NULL)
139 ds_put_cont (void *cls,
143 struct PutContCtx *pcc = cls;
144 struct GNUNET_FS_ProgressInfo pi;
146 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
148 /* we were aborted in the meantime,
150 publish_cleanup (pcc->sc);
153 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
154 pcc->sc->in_network_wait = GNUNET_NO;
155 if (GNUNET_OK != success)
157 GNUNET_asprintf (&pcc->p->emsg,
158 _("Upload failed: %s"),
160 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
161 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
162 pi.value.publish.specifics.error.message = pcc->p->emsg;
163 pcc->p->client_info = GNUNET_FS_publish_make_status_ (&pi, pcc->sc, pcc->p, 0);
165 if (NULL != pcc->cont)
167 = GNUNET_SCHEDULER_add_with_priority (pcc->sc->h->sched,
168 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
176 * Generate the callback that signals clients
177 * that a file (or directory) has been completely
180 * @param p the completed upload
181 * @param sc context of the publication
184 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
185 struct GNUNET_FS_PublishContext *sc)
187 struct GNUNET_FS_ProgressInfo pi;
189 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
190 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
191 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
192 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p,
193 GNUNET_ntohll (p->chk_uri->data.chk.file_length));
198 * Generate the callback that signals clients
199 * that a file (or directory) has encountered
200 * a problem during publication.
202 * @param p the upload that had trouble
203 * @param sc context of the publication
204 * @param emsg error message
207 signal_publish_error (struct GNUNET_FS_FileInformation *p,
208 struct GNUNET_FS_PublishContext *sc,
211 struct GNUNET_FS_ProgressInfo pi;
213 p->emsg = GNUNET_strdup (emsg);
214 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
215 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
216 pi.value.publish.specifics.error.message =emsg;
217 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
222 * We've finished publishing the SBlock as part of a larger upload.
223 * Check the result and complete the larger upload.
225 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
226 * @param uri URI of the published SBlock
227 * @param emsg NULL on success, otherwise error message
230 publish_sblocks_cont (void *cls,
231 const struct GNUNET_FS_Uri *uri,
234 struct GNUNET_FS_PublishContext *pc = cls;
237 signal_publish_error (pc->fi,
240 GNUNET_FS_publish_sync_ (pc);
243 // FIXME: release the datastore reserve here!
244 signal_publish_completion (pc->fi, pc);
245 pc->all_done = GNUNET_YES;
246 GNUNET_FS_publish_sync_ (pc);
251 * We are almost done publishing the structure,
252 * add SBlocks (if needed).
254 * @param sc overall upload data
257 publish_sblock (struct GNUNET_FS_PublishContext *sc)
259 if (NULL != sc->namespace)
260 GNUNET_FS_publish_sks (sc->h,
266 sc->fi->expirationTime,
270 &publish_sblocks_cont,
273 publish_sblocks_cont (sc, NULL, NULL);
278 * We've finished publishing a KBlock as part of a larger upload.
279 * Check the result and continue the larger upload.
281 * @param cls the "struct GNUNET_FS_PublishContext*"
282 * of the larger upload
283 * @param uri URI of the published blocks
284 * @param emsg NULL on success, otherwise error message
287 publish_kblocks_cont (void *cls,
288 const struct GNUNET_FS_Uri *uri,
291 struct GNUNET_FS_PublishContext *pc = cls;
292 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
296 signal_publish_error (p, pc, emsg);
297 GNUNET_FS_file_information_sync_ (p);
298 GNUNET_FS_publish_sync_ (pc);
300 = GNUNET_SCHEDULER_add_with_priority (pc->h->sched,
301 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
302 &GNUNET_FS_publish_main_,
307 signal_publish_completion (p, pc);
308 /* move on to next file */
310 pc->fi_pos = p->next;
313 GNUNET_FS_publish_sync_ (pc);
315 = GNUNET_SCHEDULER_add_with_priority (pc->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 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
405 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
406 pi.value.publish.specifics.error.message = p->emsg;
407 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
409 /* continue with main */
411 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
412 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
413 &GNUNET_FS_publish_main_,
419 * Function called asking for the current (encoded)
420 * block to be processed. After processing the
421 * client should either call "GNUNET_FS_tree_encode_next"
422 * or (on error) "GNUNET_FS_tree_encode_finish".
425 * @param query the query for the block (key for lookup in the datastore)
426 * @param offset offset of the block in the file
427 * @param type type of the block (IBLOCK or DBLOCK)
428 * @param block the (encrypted) block
429 * @param block_size size of block (in bytes)
432 block_proc (void *cls,
433 const GNUNET_HashCode *query,
435 enum GNUNET_BLOCK_Type type,
439 struct GNUNET_FS_PublishContext *sc = cls;
440 struct GNUNET_FS_FileInformation *p;
441 struct PutContCtx * dpc_cls;
442 struct OnDemandBlock odb;
448 = GNUNET_SCHEDULER_add_with_priority (sc->h->sched,
449 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
450 &GNUNET_FS_publish_main_,
455 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
456 sc->in_network_wait = GNUNET_YES;
457 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
458 dpc_cls->cont = &GNUNET_FS_publish_main_;
459 dpc_cls->cont_cls = sc;
462 if ( (! p->is_directory) &&
463 (GNUNET_YES == p->data.file.do_index) &&
464 (type == GNUNET_BLOCK_TYPE_DBLOCK) )
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Indexing block `%s' for offset %llu with index size %u\n",
470 (unsigned long long) offset,
471 sizeof (struct OnDemandBlock));
473 odb.offset = GNUNET_htonll (offset);
474 odb.file_id = p->data.file.file_id;
475 GNUNET_DATASTORE_put (sc->dsh,
478 sizeof(struct OnDemandBlock),
480 GNUNET_BLOCK_TYPE_ONDEMAND,
484 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Publishing block `%s' for offset %llu with size %u\n",
493 (unsigned long long) offset,
494 (unsigned int) block_size);
496 GNUNET_DATASTORE_put (sc->dsh,
505 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
512 * Function called with information about our
513 * progress in computing the tree encoding.
516 * @param offset where are we in the file
517 * @param pt_block plaintext of the currently processed block
518 * @param pt_size size of pt_block
519 * @param depth depth of the block in the tree
522 progress_proc (void *cls,
524 const void *pt_block,
528 struct GNUNET_FS_PublishContext *sc = cls;
529 struct GNUNET_FS_FileInformation *p;
530 struct GNUNET_FS_ProgressInfo pi;
533 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
534 pi.value.publish.specifics.progress.data = pt_block;
535 pi.value.publish.specifics.progress.offset = offset;
536 pi.value.publish.specifics.progress.data_len = pt_size;
537 pi.value.publish.specifics.progress.depth = depth;
538 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, offset);
543 * We are uploading a file or directory; load (if necessary) the next
544 * block into memory, encrypt it and send it to the FS service. Then
545 * continue with the main task.
547 * @param sc overall upload data
550 publish_content (struct GNUNET_FS_PublishContext *sc)
552 struct GNUNET_FS_FileInformation *p;
554 struct GNUNET_FS_DirectoryBuilder *db;
555 struct GNUNET_FS_FileInformation *dirpos;
560 GNUNET_assert (p != NULL);
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);
605 GNUNET_FS_file_information_sync_ (p);
607 size = (p->is_directory)
608 ? p->data.dir.dir_size
609 : p->data.file.file_size;
610 p->te = GNUNET_FS_tree_encoder_create (sc->h,
619 GNUNET_FS_tree_encoder_next (p->te);
624 * Process the response (or lack thereof) from
625 * the "fs" service to our 'start index' request.
627 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
628 * @param msg the response we got
631 process_index_start_response (void *cls,
632 const struct GNUNET_MessageHeader *msg)
634 struct GNUNET_FS_PublishContext *sc = cls;
635 struct GNUNET_FS_FileInformation *p;
639 GNUNET_CLIENT_disconnect (sc->client, GNUNET_NO);
644 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
645 _("Can not index file `%s': %s. Will try to insert instead.\n"),
647 _("timeout on index-start request to `fs' service"));
648 p->data.file.do_index = GNUNET_NO;
649 GNUNET_FS_file_information_sync_ (p);
650 publish_content (sc);
653 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
655 msize = ntohs (msg->size);
656 emsg = (const char *) &msg[1];
657 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
658 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
659 emsg = gettext_noop ("unknown error");
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661 _("Can not index file `%s': %s. Will try to insert instead.\n"),
664 p->data.file.do_index = GNUNET_NO;
665 GNUNET_FS_file_information_sync_ (p);
666 publish_content (sc);
669 p->data.file.index_start_confirmed = GNUNET_YES;
670 /* success! continue with indexing */
671 GNUNET_FS_file_information_sync_ (p);
672 publish_content (sc);
677 * Function called once the hash computation over an
678 * indexed file has completed.
680 * @param cls closure, our publishing context
681 * @param res resulting hash, NULL on error
684 hash_for_index_cb (void *cls,
685 const GNUNET_HashCode *
688 struct GNUNET_FS_PublishContext *sc = cls;
689 struct GNUNET_FS_FileInformation *p;
690 struct IndexStartMessage *ism;
692 struct GNUNET_CLIENT_Connection *client;
700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701 _("Can not index file `%s': %s. Will try to insert instead.\n"),
703 _("failed to compute hash"));
704 p->data.file.do_index = GNUNET_NO;
705 GNUNET_FS_file_information_sync_ (p);
706 publish_content (sc);
709 if (GNUNET_YES == p->data.file.index_start_confirmed)
711 publish_content (sc);
714 fn = GNUNET_STRINGS_filename_expand (p->filename);
715 slen = strlen (fn) + 1;
716 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
719 _("Can not index file `%s': %s. Will try to insert instead.\n"),
721 _("filename too long"));
723 p->data.file.do_index = GNUNET_NO;
724 GNUNET_FS_file_information_sync_ (p);
725 publish_content (sc);
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Hash of indexed file `%s' is `%s'\n",
731 p->data.file.filename,
734 client = GNUNET_CLIENT_connect (sc->h->sched,
739 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
740 _("Can not index file `%s': %s. Will try to insert instead.\n"),
742 _("could not connect to `fs' service"));
743 p->data.file.do_index = GNUNET_NO;
744 publish_content (sc);
748 if (p->data.file.have_hash != GNUNET_YES)
750 p->data.file.file_id = *res;
751 p->data.file.have_hash = GNUNET_YES;
752 GNUNET_FS_file_information_sync_ (p);
754 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
756 ism->header.size = htons(sizeof(struct IndexStartMessage) +
758 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
760 GNUNET_DISK_file_get_identifiers (p->filename,
764 ism->device = htonl (dev);
765 ism->inode = GNUNET_htonll(ino);
769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770 _("Failed to get file identifiers for `%s'\n"),
779 GNUNET_break (GNUNET_YES ==
780 GNUNET_CLIENT_transmit_and_get_response (client,
782 GNUNET_TIME_UNIT_FOREVER_REL,
784 &process_index_start_response,
791 * Main function that performs the upload.
793 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
794 * @param tc task context
797 GNUNET_FS_publish_main_ (void *cls,
798 const struct GNUNET_SCHEDULER_TaskContext *tc)
800 struct GNUNET_FS_PublishContext *pc = cls;
801 struct GNUNET_FS_ProgressInfo pi;
802 struct GNUNET_FS_FileInformation *p;
805 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
809 /* upload of entire hierarchy complete,
810 publish namespace entries */
811 GNUNET_FS_publish_sync_ (pc);
815 /* find starting position */
816 while ( (p->is_directory) &&
817 (NULL != p->data.dir.entries) &&
819 (NULL == p->data.dir.entries->chk_uri) )
821 p = p->data.dir.entries;
823 GNUNET_FS_publish_sync_ (pc);
828 /* error with current file, abort all
829 related files as well! */
830 while (NULL != p->dir)
832 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
833 EXTRACTOR_METATYPE_FILENAME);
837 GNUNET_asprintf (&p->emsg,
838 _("Recursive upload failed at `%s': %s"),
845 GNUNET_asprintf (&p->emsg,
846 _("Recursive upload failed: %s"),
849 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
850 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
851 pi.value.publish.specifics.error.message = p->emsg;
852 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
854 pc->all_done = GNUNET_YES;
855 GNUNET_FS_publish_sync_ (pc);
858 /* handle completion */
859 if (NULL != p->chk_uri)
861 GNUNET_FS_publish_sync_ (pc);
862 /* upload of "p" complete, publish KBlocks! */
863 if (p->keywords != NULL)
865 GNUNET_FS_publish_ksk (pc->h,
873 &publish_kblocks_cont,
878 publish_kblocks_cont (pc,
884 if ( (!p->is_directory) &&
885 (p->data.file.do_index) )
887 if (NULL == p->filename)
889 p->data.file.do_index = GNUNET_NO;
890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
891 _("Can not index file `%s': %s. Will try to insert instead.\n"),
893 _("needs to be an actual file"));
894 GNUNET_FS_file_information_sync_ (p);
895 publish_content (pc);
898 if (p->data.file.have_hash)
900 hash_for_index_cb (pc,
901 &p->data.file.file_id);
905 p->start_time = GNUNET_TIME_absolute_get ();
906 GNUNET_CRYPTO_hash_file (pc->h->sched,
907 GNUNET_SCHEDULER_PRIORITY_IDLE,
915 publish_content (pc);
920 * Signal the FS's progress function that we are starting
923 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
924 * @param fi the entry in the publish-structure
925 * @param length length of the file or directory
926 * @param meta metadata for the file or directory (can be modified)
927 * @param uri pointer to the keywords that will be used for this entry (can be modified)
928 * @param anonymity pointer to selected anonymity level (can be modified)
929 * @param priority pointer to selected priority (can be modified)
930 * @param expirationTime pointer to selected expiration time (can be modified)
931 * @param client_info pointer to client context set upon creation (can be modified)
932 * @return GNUNET_OK to continue (always)
935 fip_signal_start(void *cls,
936 struct GNUNET_FS_FileInformation *fi,
938 struct GNUNET_CONTAINER_MetaData *meta,
939 struct GNUNET_FS_Uri **uri,
942 struct GNUNET_TIME_Absolute *expirationTime,
945 struct GNUNET_FS_PublishContext *sc = cls;
946 struct GNUNET_FS_ProgressInfo pi;
948 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
949 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
950 GNUNET_FS_file_information_sync_ (fi);
956 * Signal the FS's progress function that we are suspending
959 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
960 * @param fi the entry in the publish-structure
961 * @param length length of the file or directory
962 * @param meta metadata for the file or directory (can be modified)
963 * @param uri pointer to the keywords that will be used for this entry (can be modified)
964 * @param anonymity pointer to selected anonymity level (can be modified)
965 * @param priority pointer to selected priority (can be modified)
966 * @param expirationTime pointer to selected expiration time (can be modified)
967 * @param client_info pointer to client context set upon creation (can be modified)
968 * @return GNUNET_OK to continue (always)
971 fip_signal_suspend(void *cls,
972 struct GNUNET_FS_FileInformation *fi,
974 struct GNUNET_CONTAINER_MetaData *meta,
975 struct GNUNET_FS_Uri **uri,
978 struct GNUNET_TIME_Absolute *expirationTime,
981 struct GNUNET_FS_PublishContext*sc = cls;
982 struct GNUNET_FS_ProgressInfo pi;
985 GNUNET_free_non_null (fi->serialization);
986 fi->serialization = NULL;
987 off = (fi->chk_uri == NULL) ? 0 : length;
988 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
989 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
996 * Create SUSPEND event for the given publish operation
997 * and then clean up our state (without stop signal).
999 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1002 GNUNET_FS_publish_signal_suspend_ (void *cls)
1004 struct GNUNET_FS_PublishContext *pc = cls;
1006 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1008 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1009 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1011 GNUNET_FS_file_information_inspect (pc->fi,
1012 &fip_signal_suspend,
1014 GNUNET_FS_end_top (pc->h, pc->top);
1015 publish_cleanup (pc);
1019 * Publish a file or directory.
1021 * @param h handle to the file sharing subsystem
1022 * @param fi information about the file or directory structure to publish
1023 * @param namespace namespace to publish the file in, NULL for no namespace
1024 * @param nid identifier to use for the publishd content in the namespace
1025 * (can be NULL, must be NULL if namespace is NULL)
1026 * @param nuid update-identifier that will be used for future updates
1027 * (can be NULL, must be NULL if namespace or nid is NULL)
1028 * @param options options for the publication
1029 * @return context that can be used to control the publish operation
1031 struct GNUNET_FS_PublishContext *
1032 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1033 struct GNUNET_FS_FileInformation *fi,
1034 struct GNUNET_FS_Namespace *namespace,
1037 enum GNUNET_FS_PublishOptions options)
1039 struct GNUNET_FS_PublishContext *ret;
1040 struct GNUNET_DATASTORE_Handle *dsh;
1042 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1044 dsh = GNUNET_DATASTORE_connect (h->cfg,
1053 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1057 ret->namespace = namespace;
1058 if (namespace != NULL)
1061 GNUNET_assert (NULL != nid);
1062 ret->nid = GNUNET_strdup (nid);
1064 ret->nuid = GNUNET_strdup (nuid);
1067 GNUNET_FS_file_information_inspect (ret->fi,
1070 ret->fi_pos = ret->fi;
1071 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1072 GNUNET_FS_publish_sync_ (ret);
1073 // FIXME: calculate space needed for "fi"
1074 // and reserve as first task (then trigger
1075 // "publish_main" from that continuation)!
1077 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1078 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1079 &GNUNET_FS_publish_main_,
1086 * Signal the FS's progress function that we are stopping
1089 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1090 * @param fi the entry in the publish-structure
1091 * @param length length of the file or directory
1092 * @param meta metadata for the file or directory (can be modified)
1093 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1094 * @param anonymity pointer to selected anonymity level (can be modified)
1095 * @param priority pointer to selected priority (can be modified)
1096 * @param expirationTime pointer to selected expiration time (can be modified)
1097 * @param client_info pointer to client context set upon creation (can be modified)
1098 * @return GNUNET_OK to continue (always)
1101 fip_signal_stop(void *cls,
1102 struct GNUNET_FS_FileInformation *fi,
1104 struct GNUNET_CONTAINER_MetaData *meta,
1105 struct GNUNET_FS_Uri **uri,
1106 uint32_t *anonymity,
1108 struct GNUNET_TIME_Absolute *expirationTime,
1111 struct GNUNET_FS_PublishContext*sc = cls;
1112 struct GNUNET_FS_ProgressInfo pi;
1115 if (fi->serialization != NULL)
1117 if (0 != UNLINK (fi->serialization))
1119 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1123 GNUNET_free (fi->serialization);
1124 fi->serialization = NULL;
1126 off = (fi->chk_uri == NULL) ? 0 : length;
1127 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1128 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1129 *client_info = NULL;
1135 * Stop an upload. Will abort incomplete uploads (but
1136 * not remove blocks that have already been publishd) or
1137 * simply clean up the state for completed uploads.
1138 * Must NOT be called from within the event callback!
1140 * @param pc context for the upload to stop
1143 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1145 GNUNET_FS_end_top (pc->h, pc->top);
1146 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1148 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1149 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1151 if (pc->serialization != NULL)
1153 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1154 GNUNET_free (pc->serialization);
1155 pc->serialization = NULL;
1157 GNUNET_FS_file_information_inspect (pc->fi,
1160 if (GNUNET_YES == pc->in_network_wait)
1162 pc->in_network_wait = GNUNET_SYSERR;
1165 publish_cleanup (pc);
1170 * Context for the KSK publication.
1172 struct PublishKskContext
1178 struct GNUNET_FS_Uri *ksk_uri;
1181 * Global FS context.
1183 struct GNUNET_FS_Handle *h;
1186 * The master block that we are sending
1187 * (in plaintext), has "mdsize+slen" more
1188 * bytes than the struct would suggest.
1193 * Buffer of the same size as "kb" for
1194 * the encrypted version.
1199 * Handle to the datastore, NULL if we are just
1202 struct GNUNET_DATASTORE_Handle *dsh;
1205 * Function to call once we're done.
1207 GNUNET_FS_PublishContinuation cont;
1215 * When should the KBlocks expire?
1217 struct GNUNET_TIME_Absolute expirationTime;
1220 * Size of the serialized metadata.
1225 * Size of the (CHK) URI as a string.
1230 * Keyword that we are currently processing.
1235 * Anonymity level for the KBlocks.
1240 * Priority for the KBlocks.
1247 * Continuation of "GNUNET_FS_publish_ksk" that performs
1248 * the actual publishing operation (iterating over all
1251 * @param cls closure of type "struct PublishKskContext*"
1255 publish_ksk_cont (void *cls,
1256 const struct GNUNET_SCHEDULER_TaskContext *tc);
1260 * Function called by the datastore API with
1261 * the result from the PUT request.
1263 * @param cls closure of type "struct PublishKskContext*"
1264 * @param success GNUNET_OK on success
1265 * @param msg error message (or NULL)
1268 kb_put_cont (void *cls,
1272 struct PublishKskContext *pkc = cls;
1274 if (GNUNET_OK != success)
1276 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1277 GNUNET_free (pkc->cpy);
1278 GNUNET_free (pkc->kb);
1279 pkc->cont (pkc->cont_cls,
1282 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1286 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1289 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1294 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1295 * publishing operation (iterating over all of the keywords).
1297 * @param cls closure of type "struct PublishKskContext*"
1301 publish_ksk_cont (void *cls,
1302 const struct GNUNET_SCHEDULER_TaskContext *tc)
1304 struct PublishKskContext *pkc = cls;
1305 const char *keyword;
1306 GNUNET_HashCode key;
1307 GNUNET_HashCode query;
1308 struct GNUNET_CRYPTO_AesSessionKey skey;
1309 struct GNUNET_CRYPTO_AesInitializationVector iv;
1310 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1313 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1314 (NULL == pkc->dsh) )
1316 if (NULL != pkc->dsh)
1317 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1318 GNUNET_free (pkc->cpy);
1319 GNUNET_free (pkc->kb);
1320 pkc->cont (pkc->cont_cls,
1323 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1327 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1328 /* first character of keyword indicates if it is
1329 mandatory or not -- ignore for hashing */
1330 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1331 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1332 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1333 pkc->slen + pkc->mdsize,
1337 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1338 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1339 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1340 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1342 GNUNET_assert (GNUNET_OK ==
1343 GNUNET_CRYPTO_rsa_sign (pk,
1345 &pkc->cpy->signature));
1346 GNUNET_CRYPTO_rsa_key_free (pk);
1347 GNUNET_DATASTORE_put (pkc->dsh,
1351 sizeof (struct KBlock) +
1354 GNUNET_BLOCK_TYPE_KBLOCK,
1357 pkc->expirationTime,
1358 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1365 * Publish a CHK under various keywords on GNUnet.
1367 * @param h handle to the file sharing subsystem
1368 * @param ksk_uri keywords to use
1369 * @param meta metadata to use
1370 * @param uri URI to refer to in the KBlock
1371 * @param expirationTime when the KBlock expires
1372 * @param anonymity anonymity level for the KBlock
1373 * @param priority priority for the KBlock
1374 * @param options publication options
1375 * @param cont continuation
1376 * @param cont_cls closure for cont
1379 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1380 const struct GNUNET_FS_Uri *ksk_uri,
1381 const struct GNUNET_CONTAINER_MetaData *meta,
1382 const struct GNUNET_FS_Uri *uri,
1383 struct GNUNET_TIME_Absolute expirationTime,
1386 enum GNUNET_FS_PublishOptions options,
1387 GNUNET_FS_PublishContinuation cont,
1390 struct PublishKskContext *pkc;
1396 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1398 pkc->expirationTime = expirationTime;
1399 pkc->anonymity = anonymity;
1400 pkc->priority = priority;
1402 pkc->cont_cls = cont_cls;
1403 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1405 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1407 if (pkc->dsh == NULL)
1409 cont (cont_cls, NULL, _("Could not connect to datastore."));
1417 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1418 GNUNET_assert (pkc->mdsize >= 0);
1419 uris = GNUNET_FS_uri_to_string (uri);
1420 pkc->slen = strlen (uris) + 1;
1421 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1422 if (size > MAX_KBLOCK_SIZE)
1424 size = MAX_KBLOCK_SIZE;
1425 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1427 pkc->kb = GNUNET_malloc (size);
1428 kbe = (char *) &pkc->kb[1];
1429 memcpy (kbe, uris, pkc->slen);
1431 sptr = &kbe[pkc->slen];
1433 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1436 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1437 if (pkc->mdsize == -1)
1440 GNUNET_free (pkc->kb);
1441 if (pkc->dsh != NULL)
1442 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1443 cont (cont_cls, NULL, _("Internal error."));
1447 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1449 pkc->cpy = GNUNET_malloc (size);
1450 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1451 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1454 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1455 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1456 GNUNET_SCHEDULER_add_continuation (h->sched,
1459 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1464 * Context for the SKS publication.
1466 struct PublishSksContext
1470 * Global FS context.
1472 struct GNUNET_FS_Uri *uri;
1475 * Handle to the datastore.
1477 struct GNUNET_DATASTORE_Handle *dsh;
1480 * Function to call once we're done.
1482 GNUNET_FS_PublishContinuation cont;
1493 * Function called by the datastore API with
1494 * the result from the PUT (SBlock) request.
1496 * @param cls closure of type "struct PublishSksContext*"
1497 * @param success GNUNET_OK on success
1498 * @param msg error message (or NULL)
1501 sb_put_cont (void *cls,
1505 struct PublishSksContext *psc = cls;
1507 if (NULL != psc->dsh)
1508 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1509 if (GNUNET_OK != success)
1510 psc->cont (psc->cont_cls,
1514 psc->cont (psc->cont_cls,
1517 GNUNET_FS_uri_destroy (psc->uri);
1523 * Publish an SBlock on GNUnet.
1525 * @param h handle to the file sharing subsystem
1526 * @param namespace namespace to publish in
1527 * @param identifier identifier to use
1528 * @param update update identifier to use
1529 * @param meta metadata to use
1530 * @param uri URI to refer to in the SBlock
1531 * @param expirationTime when the SBlock expires
1532 * @param anonymity anonymity level for the SBlock
1533 * @param priority priority for the SBlock
1534 * @param options publication options
1535 * @param cont continuation
1536 * @param cont_cls closure for cont
1539 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1540 struct GNUNET_FS_Namespace *namespace,
1541 const char *identifier,
1543 const struct GNUNET_CONTAINER_MetaData *meta,
1544 const struct GNUNET_FS_Uri *uri,
1545 struct GNUNET_TIME_Absolute expirationTime,
1548 enum GNUNET_FS_PublishOptions options,
1549 GNUNET_FS_PublishContinuation cont,
1552 struct PublishSksContext *psc;
1553 struct GNUNET_CRYPTO_AesSessionKey sk;
1554 struct GNUNET_CRYPTO_AesInitializationVector iv;
1555 struct GNUNET_FS_Uri *sks_uri;
1563 struct SBlock *sb_enc;
1565 struct GNUNET_CONTAINER_MetaData *mmeta;
1566 GNUNET_HashCode key; /* hash of thisId = key */
1567 GNUNET_HashCode id; /* hash of hc = identifier */
1568 GNUNET_HashCode query; /* id ^ nsid = DB query */
1571 mmeta = GNUNET_CONTAINER_meta_data_create ();
1573 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1574 uris = GNUNET_FS_uri_to_string (uri);
1575 slen = strlen (uris) + 1;
1576 idlen = strlen (identifier);
1579 nidlen = strlen (update) + 1;
1580 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1581 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1582 if (size > MAX_SBLOCK_SIZE)
1584 size = MAX_SBLOCK_SIZE;
1585 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1587 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1588 dest = (char *) &sb[1];
1589 memcpy (dest, update, nidlen);
1591 memcpy (dest, uris, slen);
1593 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1596 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1597 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1605 _("Internal error."));
1608 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1609 sb_enc = GNUNET_malloc (size);
1610 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1611 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1612 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1613 sks_uri->type = sks;
1614 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1615 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1616 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1617 &sks_uri->data.sks.namespace);
1618 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1619 GNUNET_CRYPTO_hash_xor (&id,
1620 &sks_uri->data.sks.namespace,
1621 &sb_enc->identifier);
1622 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1623 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1624 size - sizeof (struct SBlock),
1628 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1629 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1630 + sizeof(struct SBlock)
1631 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1632 GNUNET_assert (GNUNET_OK ==
1633 GNUNET_CRYPTO_rsa_sign (namespace->key,
1635 &sb_enc->signature));
1636 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1639 psc->cont_cls = cont_cls;
1640 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1642 GNUNET_free (sb_enc);
1649 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1650 if (NULL == psc->dsh)
1652 GNUNET_free (sb_enc);
1656 _("Failed to connect to datastore."));
1659 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1662 GNUNET_DATASTORE_put (psc->dsh,
1664 &sb_enc->identifier,
1667 GNUNET_BLOCK_TYPE_SBLOCK,
1671 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1676 GNUNET_free (sb_enc);
1679 /* end of fs_publish.c */