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;
564 db = GNUNET_FS_directory_builder_create (p->meta);
565 dirpos = p->data.dir.entries;
566 while (NULL != dirpos)
568 if (dirpos->is_directory)
570 raw_data = dirpos->data.dir.dir_data;
571 dirpos->data.dir.dir_data = NULL;
576 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
577 (dirpos->data.file.file_size > 0) )
579 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
581 if (dirpos->data.file.file_size !=
582 dirpos->data.file.reader (dirpos->data.file.reader_cls,
584 dirpos->data.file.file_size,
588 GNUNET_free_non_null (emsg);
589 GNUNET_free (raw_data);
594 GNUNET_FS_directory_builder_add (db,
598 GNUNET_free_non_null (raw_data);
599 dirpos = dirpos->next;
601 GNUNET_FS_directory_builder_finish (db,
602 &p->data.dir.dir_size,
603 &p->data.dir.dir_data);
604 GNUNET_FS_file_information_sync_ (p);
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"),
646 _("timeout on index-start request to `fs' service"));
647 p->data.file.do_index = GNUNET_NO;
648 GNUNET_FS_file_information_sync_ (p);
649 publish_content (sc);
652 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
654 msize = ntohs (msg->size);
655 emsg = (const char *) &msg[1];
656 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
657 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
658 emsg = gettext_noop ("unknown error");
659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
660 _("Can not index file `%s': %s. Will try to insert instead.\n"),
663 p->data.file.do_index = GNUNET_NO;
664 GNUNET_FS_file_information_sync_ (p);
665 publish_content (sc);
668 p->data.file.index_start_confirmed = GNUNET_YES;
669 /* success! continue with indexing */
670 GNUNET_FS_file_information_sync_ (p);
671 publish_content (sc);
676 * Function called once the hash computation over an
677 * indexed file has completed.
679 * @param cls closure, our publishing context
680 * @param res resulting hash, NULL on error
683 hash_for_index_cb (void *cls,
684 const GNUNET_HashCode *
687 struct GNUNET_FS_PublishContext *sc = cls;
688 struct GNUNET_FS_FileInformation *p;
689 struct IndexStartMessage *ism;
691 struct GNUNET_CLIENT_Connection *client;
699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 _("Can not index file `%s': %s. Will try to insert instead.\n"),
702 _("failed to compute hash"));
703 p->data.file.do_index = GNUNET_NO;
704 GNUNET_FS_file_information_sync_ (p);
705 publish_content (sc);
708 if (GNUNET_YES == p->data.file.index_start_confirmed)
710 publish_content (sc);
713 fn = GNUNET_STRINGS_filename_expand (p->filename);
714 slen = strlen (fn) + 1;
715 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
717 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
718 _("Can not index file `%s': %s. Will try to insert instead.\n"),
720 _("filename too long"));
722 p->data.file.do_index = GNUNET_NO;
723 GNUNET_FS_file_information_sync_ (p);
724 publish_content (sc);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Hash of indexed file `%s' is `%s'\n",
730 p->data.file.filename,
733 client = GNUNET_CLIENT_connect (sc->h->sched,
738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
739 _("Can not index file `%s': %s. Will try to insert instead.\n"),
741 _("could not connect to `fs' service"));
742 p->data.file.do_index = GNUNET_NO;
743 publish_content (sc);
747 if (p->data.file.have_hash != GNUNET_YES)
749 p->data.file.file_id = *res;
750 p->data.file.have_hash = GNUNET_YES;
751 GNUNET_FS_file_information_sync_ (p);
753 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
755 ism->header.size = htons(sizeof(struct IndexStartMessage) +
757 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
759 GNUNET_DISK_file_get_identifiers (p->filename,
763 ism->device = htonl (dev);
764 ism->inode = GNUNET_htonll(ino);
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 _("Failed to get file identifiers for `%s'\n"),
778 GNUNET_break (GNUNET_YES ==
779 GNUNET_CLIENT_transmit_and_get_response (client,
781 GNUNET_TIME_UNIT_FOREVER_REL,
783 &process_index_start_response,
790 * Main function that performs the upload.
792 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
793 * @param tc task context
796 GNUNET_FS_publish_main_ (void *cls,
797 const struct GNUNET_SCHEDULER_TaskContext *tc)
799 struct GNUNET_FS_PublishContext *sc = cls;
800 struct GNUNET_FS_ProgressInfo pi;
801 struct GNUNET_FS_FileInformation *p;
804 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
808 /* upload of entire hierarchy complete,
809 publish namespace entries */
810 GNUNET_FS_publish_sync_ (sc);
814 /* find starting position */
815 while ( (p->is_directory) &&
816 (NULL != p->data.dir.entries) &&
818 (NULL == p->data.dir.entries->chk_uri) )
820 p = p->data.dir.entries;
822 GNUNET_FS_publish_sync_ (sc);
827 /* error with current file, abort all
828 related files as well! */
829 while (NULL != p->dir)
831 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
832 EXTRACTOR_METATYPE_FILENAME);
836 GNUNET_asprintf (&p->emsg,
837 _("Recursive upload failed at `%s': %s"),
844 GNUNET_asprintf (&p->emsg,
845 _("Recursive upload failed: %s"),
848 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
849 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
850 pi.value.publish.specifics.error.message = p->emsg;
851 p->client_info = GNUNET_FS_publish_make_status_ (&pi, sc, p, 0);
853 sc->all_done = GNUNET_YES;
854 GNUNET_FS_publish_sync_ (sc);
857 /* handle completion */
858 if (NULL != p->chk_uri)
860 GNUNET_FS_publish_sync_ (sc);
861 /* upload of "p" complete, publish KBlocks! */
862 if (p->keywords != NULL)
864 GNUNET_FS_publish_ksk (sc->h,
872 &publish_kblocks_cont,
877 publish_kblocks_cont (sc,
883 if ( (!p->is_directory) &&
884 (p->data.file.do_index) )
886 if (NULL == p->filename)
888 p->data.file.do_index = GNUNET_NO;
889 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
890 _("Can not index file `%s': %s. Will try to insert instead.\n"),
892 _("needs to be an actual file"));
893 GNUNET_FS_file_information_sync_ (p);
894 publish_content (sc);
897 if (p->data.file.have_hash)
899 hash_for_index_cb (sc,
900 &p->data.file.file_id);
904 p->start_time = GNUNET_TIME_absolute_get ();
905 GNUNET_CRYPTO_hash_file (sc->h->sched,
906 GNUNET_SCHEDULER_PRIORITY_IDLE,
914 publish_content (sc);
919 * Signal the FS's progress function that we are starting
922 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
923 * @param fi the entry in the publish-structure
924 * @param length length of the file or directory
925 * @param meta metadata for the file or directory (can be modified)
926 * @param uri pointer to the keywords that will be used for this entry (can be modified)
927 * @param anonymity pointer to selected anonymity level (can be modified)
928 * @param priority pointer to selected priority (can be modified)
929 * @param expirationTime pointer to selected expiration time (can be modified)
930 * @param client_info pointer to client context set upon creation (can be modified)
931 * @return GNUNET_OK to continue (always)
934 fip_signal_start(void *cls,
935 struct GNUNET_FS_FileInformation *fi,
937 struct GNUNET_CONTAINER_MetaData *meta,
938 struct GNUNET_FS_Uri **uri,
941 struct GNUNET_TIME_Absolute *expirationTime,
944 struct GNUNET_FS_PublishContext *sc = cls;
945 struct GNUNET_FS_ProgressInfo pi;
947 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
948 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
949 GNUNET_FS_file_information_sync_ (fi);
955 * Signal the FS's progress function that we are suspending
958 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
959 * @param fi the entry in the publish-structure
960 * @param length length of the file or directory
961 * @param meta metadata for the file or directory (can be modified)
962 * @param uri pointer to the keywords that will be used for this entry (can be modified)
963 * @param anonymity pointer to selected anonymity level (can be modified)
964 * @param priority pointer to selected priority (can be modified)
965 * @param expirationTime pointer to selected expiration time (can be modified)
966 * @param client_info pointer to client context set upon creation (can be modified)
967 * @return GNUNET_OK to continue (always)
970 fip_signal_suspend(void *cls,
971 struct GNUNET_FS_FileInformation *fi,
973 struct GNUNET_CONTAINER_MetaData *meta,
974 struct GNUNET_FS_Uri **uri,
977 struct GNUNET_TIME_Absolute *expirationTime,
980 struct GNUNET_FS_PublishContext*sc = cls;
981 struct GNUNET_FS_ProgressInfo pi;
984 GNUNET_free_non_null (fi->serialization);
985 fi->serialization = NULL;
986 off = (fi->chk_uri == NULL) ? 0 : length;
987 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
988 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
995 * Create SUSPEND event for the given publish operation
996 * and then clean up our state (without stop signal).
998 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1001 publish_signal_suspend (void *cls)
1003 struct GNUNET_FS_PublishContext *pc = cls;
1005 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1007 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1008 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1010 GNUNET_FS_file_information_inspect (pc->fi,
1011 &fip_signal_suspend,
1013 GNUNET_FS_end_top (pc->h, pc->top);
1014 publish_cleanup (pc);
1018 * Publish a file or directory.
1020 * @param h handle to the file sharing subsystem
1021 * @param fi information about the file or directory structure to publish
1022 * @param namespace namespace to publish the file in, NULL for no namespace
1023 * @param nid identifier to use for the publishd content in the namespace
1024 * (can be NULL, must be NULL if namespace is NULL)
1025 * @param nuid update-identifier that will be used for future updates
1026 * (can be NULL, must be NULL if namespace or nid is NULL)
1027 * @param options options for the publication
1028 * @return context that can be used to control the publish operation
1030 struct GNUNET_FS_PublishContext *
1031 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1032 struct GNUNET_FS_FileInformation *fi,
1033 struct GNUNET_FS_Namespace *namespace,
1036 enum GNUNET_FS_PublishOptions options)
1038 struct GNUNET_FS_PublishContext *ret;
1039 struct GNUNET_DATASTORE_Handle *dsh;
1041 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1043 dsh = GNUNET_DATASTORE_connect (h->cfg,
1052 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1056 ret->namespace = namespace;
1057 if (namespace != NULL)
1060 GNUNET_assert (NULL != nid);
1061 ret->nid = GNUNET_strdup (nid);
1063 ret->nuid = GNUNET_strdup (nuid);
1065 GNUNET_FS_publish_sync_ (ret);
1067 GNUNET_FS_file_information_inspect (ret->fi,
1070 ret->fi_pos = ret->fi;
1071 ret->top = GNUNET_FS_make_top (h, &publish_signal_suspend, ret);
1072 // FIXME: calculate space needed for "fi"
1073 // and reserve as first task (then trigger
1074 // "publish_main" from that continuation)!
1076 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1077 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1078 &GNUNET_FS_publish_main_,
1085 * Signal the FS's progress function that we are stopping
1088 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1089 * @param fi the entry in the publish-structure
1090 * @param length length of the file or directory
1091 * @param meta metadata for the file or directory (can be modified)
1092 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1093 * @param anonymity pointer to selected anonymity level (can be modified)
1094 * @param priority pointer to selected priority (can be modified)
1095 * @param expirationTime pointer to selected expiration time (can be modified)
1096 * @param client_info pointer to client context set upon creation (can be modified)
1097 * @return GNUNET_OK to continue (always)
1100 fip_signal_stop(void *cls,
1101 struct GNUNET_FS_FileInformation *fi,
1103 struct GNUNET_CONTAINER_MetaData *meta,
1104 struct GNUNET_FS_Uri **uri,
1105 uint32_t *anonymity,
1107 struct GNUNET_TIME_Absolute *expirationTime,
1110 struct GNUNET_FS_PublishContext*sc = cls;
1111 struct GNUNET_FS_ProgressInfo pi;
1114 if (fi->serialization != NULL)
1116 if (0 != UNLINK (fi->serialization))
1118 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1122 GNUNET_free (fi->serialization);
1123 fi->serialization = NULL;
1125 off = (fi->chk_uri == NULL) ? 0 : length;
1126 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1127 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, sc, fi, off));
1128 *client_info = NULL;
1134 * Stop an upload. Will abort incomplete uploads (but
1135 * not remove blocks that have already been publishd) or
1136 * simply clean up the state for completed uploads.
1137 * Must NOT be called from within the event callback!
1139 * @param pc context for the upload to stop
1142 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1144 GNUNET_FS_end_top (pc->h, pc->top);
1145 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1147 GNUNET_SCHEDULER_cancel (pc->h->sched, pc->upload_task);
1148 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1150 if (pc->serialization != NULL)
1152 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1153 GNUNET_free (pc->serialization);
1154 pc->serialization = NULL;
1156 GNUNET_FS_file_information_inspect (pc->fi,
1159 if (GNUNET_YES == pc->in_network_wait)
1161 pc->in_network_wait = GNUNET_SYSERR;
1164 publish_cleanup (pc);
1169 * Context for the KSK publication.
1171 struct PublishKskContext
1177 struct GNUNET_FS_Uri *ksk_uri;
1180 * Global FS context.
1182 struct GNUNET_FS_Handle *h;
1185 * The master block that we are sending
1186 * (in plaintext), has "mdsize+slen" more
1187 * bytes than the struct would suggest.
1192 * Buffer of the same size as "kb" for
1193 * the encrypted version.
1198 * Handle to the datastore, NULL if we are just
1201 struct GNUNET_DATASTORE_Handle *dsh;
1204 * Function to call once we're done.
1206 GNUNET_FS_PublishContinuation cont;
1214 * When should the KBlocks expire?
1216 struct GNUNET_TIME_Absolute expirationTime;
1219 * Size of the serialized metadata.
1224 * Size of the (CHK) URI as a string.
1229 * Keyword that we are currently processing.
1234 * Anonymity level for the KBlocks.
1239 * Priority for the KBlocks.
1246 * Continuation of "GNUNET_FS_publish_ksk" that performs
1247 * the actual publishing operation (iterating over all
1250 * @param cls closure of type "struct PublishKskContext*"
1254 publish_ksk_cont (void *cls,
1255 const struct GNUNET_SCHEDULER_TaskContext *tc);
1259 * Function called by the datastore API with
1260 * the result from the PUT request.
1262 * @param cls closure of type "struct PublishKskContext*"
1263 * @param success GNUNET_OK on success
1264 * @param msg error message (or NULL)
1267 kb_put_cont (void *cls,
1271 struct PublishKskContext *pkc = cls;
1273 if (GNUNET_OK != success)
1275 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1276 GNUNET_free (pkc->cpy);
1277 GNUNET_free (pkc->kb);
1278 pkc->cont (pkc->cont_cls,
1281 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1285 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1288 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1293 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1294 * publishing operation (iterating over all of the keywords).
1296 * @param cls closure of type "struct PublishKskContext*"
1300 publish_ksk_cont (void *cls,
1301 const struct GNUNET_SCHEDULER_TaskContext *tc)
1303 struct PublishKskContext *pkc = cls;
1304 const char *keyword;
1305 GNUNET_HashCode key;
1306 GNUNET_HashCode query;
1307 struct GNUNET_CRYPTO_AesSessionKey skey;
1308 struct GNUNET_CRYPTO_AesInitializationVector iv;
1309 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1312 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1313 (NULL == pkc->dsh) )
1315 if (NULL != pkc->dsh)
1316 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1317 GNUNET_free (pkc->cpy);
1318 GNUNET_free (pkc->kb);
1319 pkc->cont (pkc->cont_cls,
1322 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1326 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1327 /* first character of keyword indicates if it is
1328 mandatory or not -- ignore for hashing */
1329 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1330 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1331 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1332 pkc->slen + pkc->mdsize,
1336 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1337 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1338 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1339 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1341 GNUNET_assert (GNUNET_OK ==
1342 GNUNET_CRYPTO_rsa_sign (pk,
1344 &pkc->cpy->signature));
1345 GNUNET_CRYPTO_rsa_key_free (pk);
1346 GNUNET_DATASTORE_put (pkc->dsh,
1350 sizeof (struct KBlock) +
1353 GNUNET_BLOCK_TYPE_KBLOCK,
1356 pkc->expirationTime,
1357 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1364 * Publish a CHK under various keywords on GNUnet.
1366 * @param h handle to the file sharing subsystem
1367 * @param ksk_uri keywords to use
1368 * @param meta metadata to use
1369 * @param uri URI to refer to in the KBlock
1370 * @param expirationTime when the KBlock expires
1371 * @param anonymity anonymity level for the KBlock
1372 * @param priority priority for the KBlock
1373 * @param options publication options
1374 * @param cont continuation
1375 * @param cont_cls closure for cont
1378 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1379 const struct GNUNET_FS_Uri *ksk_uri,
1380 const struct GNUNET_CONTAINER_MetaData *meta,
1381 const struct GNUNET_FS_Uri *uri,
1382 struct GNUNET_TIME_Absolute expirationTime,
1385 enum GNUNET_FS_PublishOptions options,
1386 GNUNET_FS_PublishContinuation cont,
1389 struct PublishKskContext *pkc;
1395 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1397 pkc->expirationTime = expirationTime;
1398 pkc->anonymity = anonymity;
1399 pkc->priority = priority;
1401 pkc->cont_cls = cont_cls;
1402 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1404 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1406 if (pkc->dsh == NULL)
1408 cont (cont_cls, NULL, _("Could not connect to datastore."));
1416 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1417 GNUNET_assert (pkc->mdsize >= 0);
1418 uris = GNUNET_FS_uri_to_string (uri);
1419 pkc->slen = strlen (uris) + 1;
1420 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1421 if (size > MAX_KBLOCK_SIZE)
1423 size = MAX_KBLOCK_SIZE;
1424 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1426 pkc->kb = GNUNET_malloc (size);
1427 kbe = (char *) &pkc->kb[1];
1428 memcpy (kbe, uris, pkc->slen);
1430 sptr = &kbe[pkc->slen];
1432 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1435 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1436 if (pkc->mdsize == -1)
1439 GNUNET_free (pkc->kb);
1440 if (pkc->dsh != NULL)
1441 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1442 cont (cont_cls, NULL, _("Internal error."));
1446 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1448 pkc->cpy = GNUNET_malloc (size);
1449 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1450 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1453 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1454 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1455 GNUNET_SCHEDULER_add_continuation (h->sched,
1458 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1463 * Context for the SKS publication.
1465 struct PublishSksContext
1469 * Global FS context.
1471 struct GNUNET_FS_Uri *uri;
1474 * Handle to the datastore.
1476 struct GNUNET_DATASTORE_Handle *dsh;
1479 * Function to call once we're done.
1481 GNUNET_FS_PublishContinuation cont;
1492 * Function called by the datastore API with
1493 * the result from the PUT (SBlock) request.
1495 * @param cls closure of type "struct PublishSksContext*"
1496 * @param success GNUNET_OK on success
1497 * @param msg error message (or NULL)
1500 sb_put_cont (void *cls,
1504 struct PublishSksContext *psc = cls;
1506 if (NULL != psc->dsh)
1507 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1508 if (GNUNET_OK != success)
1509 psc->cont (psc->cont_cls,
1513 psc->cont (psc->cont_cls,
1516 GNUNET_FS_uri_destroy (psc->uri);
1522 * Publish an SBlock on GNUnet.
1524 * @param h handle to the file sharing subsystem
1525 * @param namespace namespace to publish in
1526 * @param identifier identifier to use
1527 * @param update update identifier to use
1528 * @param meta metadata to use
1529 * @param uri URI to refer to in the SBlock
1530 * @param expirationTime when the SBlock expires
1531 * @param anonymity anonymity level for the SBlock
1532 * @param priority priority for the SBlock
1533 * @param options publication options
1534 * @param cont continuation
1535 * @param cont_cls closure for cont
1538 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1539 struct GNUNET_FS_Namespace *namespace,
1540 const char *identifier,
1542 const struct GNUNET_CONTAINER_MetaData *meta,
1543 const struct GNUNET_FS_Uri *uri,
1544 struct GNUNET_TIME_Absolute expirationTime,
1547 enum GNUNET_FS_PublishOptions options,
1548 GNUNET_FS_PublishContinuation cont,
1551 struct PublishSksContext *psc;
1552 struct GNUNET_CRYPTO_AesSessionKey sk;
1553 struct GNUNET_CRYPTO_AesInitializationVector iv;
1554 struct GNUNET_FS_Uri *sks_uri;
1562 struct SBlock *sb_enc;
1564 struct GNUNET_CONTAINER_MetaData *mmeta;
1565 GNUNET_HashCode key; /* hash of thisId = key */
1566 GNUNET_HashCode id; /* hash of hc = identifier */
1567 GNUNET_HashCode query; /* id ^ nsid = DB query */
1570 mmeta = GNUNET_CONTAINER_meta_data_create ();
1572 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
1573 uris = GNUNET_FS_uri_to_string (uri);
1574 slen = strlen (uris) + 1;
1575 idlen = strlen (identifier);
1578 nidlen = strlen (update) + 1;
1579 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
1580 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1581 if (size > MAX_SBLOCK_SIZE)
1583 size = MAX_SBLOCK_SIZE;
1584 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1586 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1587 dest = (char *) &sb[1];
1588 memcpy (dest, update, nidlen);
1590 memcpy (dest, uris, slen);
1592 mdsize = GNUNET_CONTAINER_meta_data_serialize (mmeta,
1595 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1596 GNUNET_CONTAINER_meta_data_destroy (mmeta);
1604 _("Internal error."));
1607 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1608 sb_enc = GNUNET_malloc (size);
1609 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1610 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1611 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1612 sks_uri->type = sks;
1613 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1614 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1615 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1616 &sks_uri->data.sks.namespace);
1617 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1618 GNUNET_CRYPTO_hash_xor (&id,
1619 &sks_uri->data.sks.namespace,
1620 &sb_enc->identifier);
1621 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1622 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1623 size - sizeof (struct SBlock),
1627 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1628 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1629 + sizeof(struct SBlock)
1630 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1631 GNUNET_assert (GNUNET_OK ==
1632 GNUNET_CRYPTO_rsa_sign (namespace->key,
1634 &sb_enc->signature));
1635 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1638 psc->cont_cls = cont_cls;
1639 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1641 GNUNET_free (sb_enc);
1648 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1649 if (NULL == psc->dsh)
1651 GNUNET_free (sb_enc);
1655 _("Failed to connect to datastore."));
1658 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace,
1661 GNUNET_DATASTORE_put (psc->dsh,
1663 &sb_enc->identifier,
1666 GNUNET_BLOCK_TYPE_SBLOCK,
1670 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1675 GNUNET_free (sb_enc);
1678 /* end of fs_publish.c */