2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file fs/fs_publish.c
22 * @brief publish a file or directory in GNUnet
23 * @see https://gnunet.org/encoding
24 * @author Krista Bennett
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fs_service.h"
37 * Fill in all of the generic fields for
38 * a publish event and call the callback.
40 * @param pi structure to fill in
41 * @param pc overall publishing context
42 * @param p file information for the file being published
43 * @param offset where in the file are we so far
44 * @return value returned from callback
47 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
48 struct GNUNET_FS_PublishContext *pc,
49 const struct GNUNET_FS_FileInformation *p,
52 pi->value.publish.pc = pc;
53 pi->value.publish.fi = p;
54 pi->value.publish.cctx = p->client_info;
55 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
56 pi->value.publish.filename = p->filename;
57 pi->value.publish.size =
58 (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size :
59 p->data.file.file_size;
60 pi->value.publish.eta =
61 GNUNET_TIME_calculate_eta (p->start_time, offset,
62 pi->value.publish.size);
63 pi->value.publish.completed = offset;
64 pi->value.publish.duration =
65 GNUNET_TIME_absolute_get_duration (p->start_time);
66 pi->value.publish.anonymity = p->bo.anonymity_level;
68 return pc->h->upcb (pc->h->upcb_cls, pi);
73 * Cleanup the publish context, we're done with it.
75 * @param pc struct to clean up
78 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81 "Cleaning up publish context (done!)\n");
84 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
87 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
88 GNUNET_free_non_null (pc->nid);
89 GNUNET_free_non_null (pc->nuid);
90 GNUNET_free_non_null (pc->serialization);
93 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
98 GNUNET_MQ_destroy (pc->mq);
101 GNUNET_assert (NULL == pc->upload_task);
107 * Function called by the datastore API with
108 * the result from the PUT request.
110 * @param cls the `struct GNUNET_FS_PublishContext *`
111 * @param success #GNUNET_OK on success
112 * @param min_expiration minimum expiration time required for content to be stored
113 * @param msg error message (or NULL)
116 ds_put_cont (void *cls,
118 struct GNUNET_TIME_Absolute min_expiration,
121 struct GNUNET_FS_PublishContext *pc = cls;
122 struct GNUNET_FS_ProgressInfo pi;
125 if (GNUNET_SYSERR == success)
127 GNUNET_asprintf (&pc->fi_pos->emsg,
128 _ ("Publishing failed: %s"),
130 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
131 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
132 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
133 pc->fi_pos->client_info =
134 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
135 if ((GNUNET_YES != pc->fi_pos->is_directory) &&
136 (NULL != pc->fi_pos->filename) &&
137 (GNUNET_YES == pc->any_done) &&
138 (GNUNET_YES == pc->fi_pos->data.file.do_index))
140 /* run unindex to clean up */
141 GNUNET_FS_unindex_start (pc->h,
142 pc->fi_pos->filename,
147 pc->any_done = GNUNET_YES;
148 GNUNET_assert (NULL == pc->upload_task);
150 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
151 &GNUNET_FS_publish_main_, pc);
156 * Generate the callback that signals clients
157 * that a file (or directory) has been completely
160 * @param p the completed upload
161 * @param pc context of the publication
164 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
165 struct GNUNET_FS_PublishContext *pc)
167 struct GNUNET_FS_ProgressInfo pi;
169 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
170 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
171 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
172 pi.value.publish.specifics.completed.sks_uri = p->sks_uri;
174 GNUNET_FS_publish_make_status_ (&pi, pc, p,
175 p->data.file.file_size);
180 * Generate the callback that signals clients
181 * that a file (or directory) has encountered
182 * a problem during publication.
184 * @param p the upload that had trouble
185 * @param pc context of the publication
186 * @param emsg error message
189 signal_publish_error (struct GNUNET_FS_FileInformation *p,
190 struct GNUNET_FS_PublishContext *pc,
193 struct GNUNET_FS_ProgressInfo pi;
195 p->emsg = GNUNET_strdup (emsg);
196 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
197 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
198 pi.value.publish.specifics.error.message = emsg;
199 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
200 if ((p->is_directory != GNUNET_YES) &&
201 (NULL != p->filename) &&
202 (GNUNET_YES == pc->any_done) &&
203 (p->data.file.do_index == GNUNET_YES))
205 /* run unindex to clean up */
206 GNUNET_FS_unindex_start (pc->h,
214 * Datastore returns from reservation cancel request.
216 * @param cls the `struct GNUNET_FS_PublishContext *`
217 * @param success success code (not used)
218 * @param min_expiration minimum expiration time required for content to be stored
219 * @param msg error message (typically NULL, not used)
222 finish_release_reserve (void *cls, int success,
223 struct GNUNET_TIME_Absolute min_expiration,
226 struct GNUNET_FS_PublishContext *pc = cls;
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Releasing reserve done!\n");
231 signal_publish_completion (pc->fi, pc);
232 pc->all_done = GNUNET_YES;
233 GNUNET_FS_publish_sync_ (pc);
238 * We've finished publishing the SBlock as part of a larger upload.
239 * Check the result and complete the larger upload.
241 * @param cls the `struct GNUNET_FS_PublishContext *` of the larger upload
242 * @param uri URI of the published SBlock
243 * @param emsg NULL on success, otherwise error message
246 publish_sblocks_cont (void *cls,
247 const struct GNUNET_FS_Uri *uri,
250 struct GNUNET_FS_PublishContext *pc = cls;
255 signal_publish_error (pc->fi, pc, emsg);
256 GNUNET_FS_publish_sync_ (pc);
261 /* sks publication, remember namespace URI */
262 pc->fi->sks_uri = GNUNET_FS_uri_dup (uri);
264 GNUNET_assert (pc->qre == NULL);
265 if ((pc->dsh != NULL) && (pc->rid != 0))
268 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
269 &finish_release_reserve, pc);
273 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
279 * We are almost done publishing the structure,
280 * add SBlocks (if needed).
282 * @param pc overall upload data
285 publish_sblock (struct GNUNET_FS_PublishContext *pc)
288 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
296 &publish_sblocks_cont, pc);
298 publish_sblocks_cont (pc, NULL, NULL);
303 * We've finished publishing a KBlock as part of a larger upload.
304 * Check the result and continue the larger upload.
306 * @param cls the `struct GNUNET_FS_PublishContext *`
307 * of the larger upload
308 * @param uri URI of the published blocks
309 * @param emsg NULL on success, otherwise error message
312 publish_kblocks_cont (void *cls,
313 const struct GNUNET_FS_Uri *uri,
316 struct GNUNET_FS_PublishContext *pc = cls;
317 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Error uploading KSK blocks: %s\n",
325 signal_publish_error (p, pc, emsg);
326 GNUNET_FS_file_information_sync_ (p);
327 GNUNET_FS_publish_sync_ (pc);
328 GNUNET_assert (NULL == pc->upload_task);
330 GNUNET_SCHEDULER_add_with_priority
331 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
332 &GNUNET_FS_publish_main_,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "KSK blocks published, moving on to next file\n");
339 signal_publish_completion (p, pc);
340 /* move on to next file */
342 pc->fi_pos = p->next;
345 GNUNET_FS_publish_sync_ (pc);
346 GNUNET_assert (NULL == pc->upload_task);
348 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
349 &GNUNET_FS_publish_main_, pc);
354 * Function called by the tree encoder to obtain
355 * a block of plaintext data (for the lowest level
358 * @param cls our publishing context
359 * @param offset identifies which block to get
360 * @param max (maximum) number of bytes to get; returning
361 * fewer will also cause errors
362 * @param buf where to copy the plaintext buffer
363 * @param emsg location to store an error message (on error)
364 * @return number of bytes copied to buf, 0 on error
367 block_reader (void *cls,
373 struct GNUNET_FS_PublishContext *pc = cls;
374 struct GNUNET_FS_FileInformation *p;
379 if (GNUNET_YES == p->is_directory)
381 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
382 dd = p->data.dir.dir_data;
383 GNUNET_memcpy (buf, &dd[offset], pt_size);
387 if (UINT64_MAX == offset)
389 if (&GNUNET_FS_data_reader_file_ == p->data.file.reader)
391 /* force closing the file to avoid keeping too many files open */
392 p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
396 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
398 return 0; /* calling reader with pt_size==0
399 * might free buf, so don't! */
401 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
410 * The tree encoder has finished processing a
411 * file. Call it's finish method and deal with
414 * @param cls our publishing context
417 encode_cont (void *cls)
419 struct GNUNET_FS_PublishContext *pc = cls;
420 struct GNUNET_FS_FileInformation *p;
421 struct GNUNET_FS_ProgressInfo pi;
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Finished with tree encoder\n");
428 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
429 GNUNET_FS_file_information_sync_ (p);
430 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Error during tree walk: %s\n",
437 GNUNET_asprintf (&p->emsg,
438 _ ("Publishing failed: %s"),
441 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
442 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
443 pi.value.publish.specifics.error.message = p->emsg;
444 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
448 /* final progress event */
449 GNUNET_assert (NULL != p->chk_uri);
450 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
451 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
452 pi.value.publish.specifics.progress.data = NULL;
453 pi.value.publish.specifics.progress.offset = flen;
454 pi.value.publish.specifics.progress.data_len = 0;
455 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
456 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
458 /* continue with main */ /* continue with main */
459 GNUNET_assert (NULL == pc->upload_task);
461 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
462 &GNUNET_FS_publish_main_, pc);
467 * Function called asking for the current (encoded)
468 * block to be processed. After processing the
469 * client should either call #GNUNET_FS_tree_encoder_next
470 * or (on error) #GNUNET_FS_tree_encoder_finish.
473 * @param chk content hash key for the block
474 * @param offset offset of the block in the file
475 * @param depth depth of the block in the file, 0 for DBLOCK
476 * @param type type of the block (IBLOCK or DBLOCK)
477 * @param block the (encrypted) block
478 * @param block_size size of @a block (in bytes)
481 block_proc (void *cls,
482 const struct ContentHashKey *chk,
485 enum GNUNET_BLOCK_Type type,
489 struct GNUNET_FS_PublishContext *pc = cls;
490 struct GNUNET_FS_FileInformation *p;
491 struct OnDemandBlock odb;
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Waiting for datastore connection\n");
498 GNUNET_assert (NULL == pc->upload_task);
500 GNUNET_SCHEDULER_add_with_priority
501 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
505 if ((GNUNET_YES != p->is_directory) &&
506 (GNUNET_YES == p->data.file.do_index) &&
507 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type))
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Indexing block `%s' for offset %llu with index size %u\n",
511 GNUNET_h2s (&chk->query),
512 (unsigned long long) offset,
513 (unsigned int) sizeof(struct OnDemandBlock));
514 odb.offset = GNUNET_htonll (offset);
515 odb.file_id = p->data.file.file_id;
516 GNUNET_assert (pc->qre == NULL);
518 GNUNET_DATASTORE_put (pc->dsh,
519 (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
521 sizeof(struct OnDemandBlock),
523 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
524 p->bo.content_priority,
525 p->bo.anonymity_level,
526 p->bo.replication_level,
527 p->bo.expiration_time,
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Publishing block `%s' for offset %llu with size %u\n",
534 GNUNET_h2s (&chk->query),
535 (unsigned long long) offset,
536 (unsigned int) block_size);
537 GNUNET_assert (pc->qre == NULL);
539 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 :
545 p->bo.content_priority,
546 p->bo.anonymity_level,
547 p->bo.replication_level,
548 p->bo.expiration_time,
556 * Function called with information about our
557 * progress in computing the tree encoding.
560 * @param offset where are we in the file
561 * @param pt_block plaintext of the currently processed block
562 * @param pt_size size of @a pt_block
563 * @param depth depth of the block in the tree, 0 for DBLOCK
566 progress_proc (void *cls, uint64_t offset,
567 const void *pt_block,
571 struct GNUNET_FS_PublishContext *pc = cls;
572 struct GNUNET_FS_FileInformation *p;
573 struct GNUNET_FS_FileInformation *par;
574 struct GNUNET_FS_ProgressInfo pi;
577 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
578 pi.value.publish.specifics.progress.data = pt_block;
579 pi.value.publish.specifics.progress.offset = offset;
580 pi.value.publish.specifics.progress.data_len = pt_size;
581 pi.value.publish.specifics.progress.depth = depth;
582 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
584 (GNUNET_YES == p->is_directory))
586 while (NULL != (par = p->dir))
589 GNUNET_assert (GNUNET_YES == par->is_directory);
590 p->data.dir.contents_completed += pt_size;
591 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY;
592 pi.value.publish.specifics.progress_directory.completed =
593 p->data.dir.contents_completed;
594 pi.value.publish.specifics.progress_directory.total =
595 p->data.dir.contents_size;
596 pi.value.publish.specifics.progress_directory.eta =
597 GNUNET_TIME_calculate_eta (p->start_time,
599 ->data.dir.contents_completed,
601 ->data.dir.contents_size);
602 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
608 * We are uploading a file or directory; load (if necessary) the next
609 * block into memory, encrypt it and send it to the FS service. Then
610 * continue with the main task.
612 * @param pc overall upload data
615 publish_content (struct GNUNET_FS_PublishContext *pc)
617 struct GNUNET_FS_FileInformation *p;
619 struct GNUNET_FS_DirectoryBuilder *db;
620 struct GNUNET_FS_FileInformation *dirpos;
625 GNUNET_assert (NULL != p);
628 if (GNUNET_YES == p->is_directory)
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
631 db = GNUNET_FS_directory_builder_create (p->meta);
632 dirpos = p->data.dir.entries;
633 while (NULL != dirpos)
635 if (GNUNET_YES == dirpos->is_directory)
637 raw_data = dirpos->data.dir.dir_data;
638 dirpos->data.dir.dir_data = NULL;
643 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
644 (dirpos->data.file.file_size > 0))
646 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
648 if (dirpos->data.file.file_size !=
649 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
650 dirpos->data.file.file_size, raw_data,
653 GNUNET_free_non_null (emsg);
654 GNUNET_free (raw_data);
657 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX,
661 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
663 GNUNET_free_non_null (raw_data);
664 dirpos = dirpos->next;
666 GNUNET_free_non_null (p->data.dir.dir_data);
667 p->data.dir.dir_data = NULL;
668 p->data.dir.dir_size = 0;
669 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
670 &p->data.dir.dir_data);
671 GNUNET_FS_file_information_sync_ (p);
673 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size :
674 p->data.file.file_size;
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Creating tree encoder\n");
678 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
679 &block_proc, &progress_proc,
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Processing next block from tree\n");
684 GNUNET_FS_tree_encoder_next (p->te);
689 * Check the response from the "fs" service to our 'start index'
692 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
693 * @param msg the response we got
696 check_index_start_failed (void *cls,
697 const struct GNUNET_MessageHeader *msg)
699 size_t msize = ntohs (msg->size) - sizeof(*msg);
700 const char *emsg = (const char *) &msg[1];
702 if (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0')
705 return GNUNET_SYSERR;
712 * Process the response from the "fs" service to our 'start index'
715 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
716 * @param msg the response we got
719 handle_index_start_failed (void *cls,
720 const struct GNUNET_MessageHeader *msg)
722 struct GNUNET_FS_PublishContext *pc = cls;
723 struct GNUNET_FS_FileInformation *p;
724 const char *emsg = (const char *) &msg[1];
727 GNUNET_MQ_destroy (pc->mq);
730 GNUNET_asprintf (&msgtxt,
731 _ ("Can not index file `%s': %s.\n"),
734 signal_publish_error (p,
737 GNUNET_free (msgtxt);
738 GNUNET_FS_file_information_sync_ (p);
739 GNUNET_FS_publish_sync_ (pc);
744 * Process the response from the "fs" service to our 'start index'
747 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
748 * @param msg the response we got
751 handle_index_start_ok (void *cls,
752 const struct GNUNET_MessageHeader *msg)
754 struct GNUNET_FS_PublishContext *pc = cls;
755 struct GNUNET_FS_FileInformation *p;
757 GNUNET_MQ_destroy (pc->mq);
760 p->data.file.index_start_confirmed = GNUNET_YES;
761 GNUNET_FS_file_information_sync_ (p);
762 publish_content (pc);
767 * Generic error handler, called with the appropriate error code and
768 * the same closure specified at the creation of the message queue.
769 * Not every message queue implementation supports an error handler.
771 * @param cls closure with the `struct GNUNET_FS_PublishContext *`
772 * @param error error code
775 index_mq_error_handler (void *cls,
776 enum GNUNET_MQ_Error error)
778 struct GNUNET_FS_PublishContext *pc = cls;
779 struct GNUNET_FS_FileInformation *p;
783 GNUNET_MQ_destroy (pc->mq);
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 _ ("Can not index file `%s': %s. Will try to insert instead.\n"),
790 _ ("error on index-start request to `fs' service"));
791 p->data.file.do_index = GNUNET_NO;
792 GNUNET_FS_file_information_sync_ (p);
793 publish_content (pc);
798 * Function called once the hash computation over an
799 * indexed file has completed.
801 * @param cls closure, our publishing context
802 * @param res resulting hash, NULL on error
805 hash_for_index_cb (void *cls,
806 const struct GNUNET_HashCode *res)
808 struct GNUNET_FS_PublishContext *pc = cls;
809 struct GNUNET_MQ_MessageHandler handlers[] = {
810 GNUNET_MQ_hd_fixed_size (index_start_ok,
811 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK,
812 struct GNUNET_MessageHeader,
814 GNUNET_MQ_hd_var_size (index_start_failed,
815 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED,
816 struct GNUNET_MessageHeader,
818 GNUNET_MQ_handler_end ()
820 struct GNUNET_FS_FileInformation *p;
821 struct GNUNET_MQ_Envelope *env;
822 struct IndexStartMessage *ism;
832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
834 "Can not index file `%s': %s. Will try to insert instead.\n"),
836 _ ("failed to compute hash"));
837 p->data.file.do_index = GNUNET_NO;
838 GNUNET_FS_file_information_sync_ (p);
839 publish_content (pc);
842 if (GNUNET_YES == p->data.file.index_start_confirmed)
844 publish_content (pc);
847 fn = GNUNET_STRINGS_filename_expand (p->filename);
848 GNUNET_assert (fn != NULL);
849 slen = strlen (fn) + 1;
851 GNUNET_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
855 ("Can not index file `%s': %s. Will try to insert instead.\n"),
856 fn, _ ("filename too long"));
858 p->data.file.do_index = GNUNET_NO;
859 GNUNET_FS_file_information_sync_ (p);
860 publish_content (pc);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Hash of indexed file `%s' is `%s'\n",
867 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
869 p->data.file.file_id = *res;
870 p->data.file.have_hash = GNUNET_YES;
871 p->data.file.index_start_confirmed = GNUNET_YES;
872 GNUNET_FS_file_information_sync_ (p);
873 publish_content (pc);
877 pc->mq = GNUNET_CLIENT_connect (pc->h->cfg,
880 &index_mq_error_handler,
884 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
886 "Can not index file `%s': %s. Will try to insert instead.\n"),
888 _ ("could not connect to `fs' service"));
889 p->data.file.do_index = GNUNET_NO;
890 publish_content (pc);
894 if (p->data.file.have_hash != GNUNET_YES)
896 p->data.file.file_id = *res;
897 p->data.file.have_hash = GNUNET_YES;
898 GNUNET_FS_file_information_sync_ (p);
900 env = GNUNET_MQ_msg_extra (ism,
902 GNUNET_MESSAGE_TYPE_FS_INDEX_START);
904 GNUNET_DISK_file_get_identifiers (p->filename,
908 ism->device = GNUNET_htonll (dev);
909 ism->inode = GNUNET_htonll (ino);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 _ ("Failed to get file identifiers for `%s'\n"),
918 GNUNET_memcpy (&ism[1],
922 GNUNET_MQ_send (pc->mq,
928 * We've computed the CHK/LOC URI, now publish the KSKs (if applicable).
930 * @param pc publishing context to do this for
933 publish_kblocks (struct GNUNET_FS_PublishContext *pc)
935 struct GNUNET_FS_FileInformation *p;
938 /* upload of "p" complete, publish KBlocks! */
939 if (NULL != p->keywords)
941 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h,
947 &publish_kblocks_cont,
952 publish_kblocks_cont (pc, p->chk_uri, NULL);
958 * Process the response from the "fs" service to our LOC sign request.
960 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
961 * @param sig the response we got
964 handle_signature_response (void *cls,
965 const struct ResponseLocSignatureMessage *sig)
967 struct GNUNET_FS_PublishContext *pc = cls;
968 struct GNUNET_FS_FileInformation *p;
971 p->chk_uri->type = GNUNET_FS_URI_LOC;
972 /* p->data.loc.fi kept from CHK before */
973 p->chk_uri->data.loc.peer = sig->peer;
974 p->chk_uri->data.loc.expirationTime
975 = GNUNET_TIME_absolute_ntoh (sig->expiration_time);
976 p->chk_uri->data.loc.contentSignature = sig->signature;
977 GNUNET_FS_file_information_sync_ (p);
978 GNUNET_FS_publish_sync_ (pc);
979 publish_kblocks (pc);
984 * Generic error handler, called with the appropriate error code and
985 * the same closure specified at the creation of the message queue.
986 * Not every message queue implementation supports an error handler.
988 * @param cls closure with the `struct GNUNET_FS_PublishContext *`
989 * @param error error code
992 loc_mq_error_handler (void *cls,
993 enum GNUNET_MQ_Error error)
995 struct GNUNET_FS_PublishContext *pc = cls;
999 GNUNET_MQ_destroy (pc->mq);
1002 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1003 _ ("Can not create LOC URI. Will continue with CHK instead.\n"));
1004 publish_kblocks (pc);
1009 * We're publishing without anonymity. Contact the FS service
1010 * to create a signed LOC URI for further processing, then
1011 * continue with KSKs.
1013 * @param pc the publishing context do to this for
1016 create_loc_uri (struct GNUNET_FS_PublishContext *pc)
1018 struct GNUNET_MQ_MessageHandler handlers[] = {
1019 GNUNET_MQ_hd_fixed_size (signature_response,
1020 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE,
1021 struct ResponseLocSignatureMessage,
1023 GNUNET_MQ_handler_end ()
1025 struct GNUNET_MQ_Envelope *env;
1026 struct RequestLocSignatureMessage *req;
1027 struct GNUNET_FS_FileInformation *p;
1030 GNUNET_MQ_destroy (pc->mq);
1031 pc->mq = GNUNET_CLIENT_connect (pc->h->cfg,
1034 &loc_mq_error_handler,
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1040 "Can not create LOC URI. Will continue with CHK instead.\n"));
1041 publish_kblocks (pc);
1045 env = GNUNET_MQ_msg (req,
1046 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN);
1047 req->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
1048 req->expiration_time = GNUNET_TIME_absolute_hton (p->bo.expiration_time);
1049 req->chk = p->chk_uri->data.chk.chk;
1050 req->file_length = GNUNET_htonll (p->chk_uri->data.chk.file_length);
1051 GNUNET_MQ_send (pc->mq,
1057 * Main function that performs the upload.
1059 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
1062 GNUNET_FS_publish_main_ (void *cls)
1064 struct GNUNET_FS_PublishContext *pc = cls;
1065 struct GNUNET_FS_ProgressInfo pi;
1066 struct GNUNET_FS_FileInformation *p;
1069 pc->upload_task = NULL;
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Publishing complete, now publishing SKS and KSK blocks.\n");
1075 /* upload of entire hierarchy complete,
1076 * publish namespace entries */
1077 GNUNET_FS_publish_sync_ (pc);
1078 publish_sblock (pc);
1081 /* find starting position */
1082 while ((GNUNET_YES == p->is_directory) &&
1083 (NULL != p->data.dir.entries) &&
1084 (NULL == p->emsg) &&
1085 (NULL == p->data.dir.entries->chk_uri))
1087 p = p->data.dir.entries;
1089 GNUNET_FS_publish_sync_ (pc);
1091 /* abort on error */
1092 if (NULL != p->emsg)
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Error uploading: %s\n",
1097 /* error with current file, abort all
1098 * related files as well! */
1099 while (NULL != p->dir)
1101 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
1102 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
1106 GNUNET_asprintf (&p->emsg,
1107 _ ("Recursive upload failed at `%s': %s"),
1114 GNUNET_asprintf (&p->emsg,
1115 _ ("Recursive upload failed: %s"),
1118 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
1119 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
1120 pi.value.publish.specifics.error.message = p->emsg;
1121 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
1123 pc->all_done = GNUNET_YES;
1124 GNUNET_FS_publish_sync_ (pc);
1127 /* handle completion */
1128 if (NULL != p->chk_uri)
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "File upload complete, now publishing KSK blocks.\n");
1132 GNUNET_FS_publish_sync_ (pc);
1134 if ((0 == p->bo.anonymity_level) &&
1136 GNUNET_FS_uri_test_loc (p->chk_uri)))
1138 /* zero anonymity, box CHK URI in LOC URI */
1139 create_loc_uri (pc);
1143 publish_kblocks (pc);
1147 if ((GNUNET_YES != p->is_directory) && (p->data.file.do_index))
1149 if (NULL == p->filename)
1151 p->data.file.do_index = GNUNET_NO;
1152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1154 "Can not index file `%s': %s. Will try to insert instead.\n"),
1156 _ ("needs to be an actual file"));
1157 GNUNET_FS_file_information_sync_ (p);
1158 publish_content (pc);
1161 if (p->data.file.have_hash)
1163 hash_for_index_cb (pc, &p->data.file.file_id);
1167 p->start_time = GNUNET_TIME_absolute_get ();
1169 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
1170 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
1174 publish_content (pc);
1179 * Signal the FS's progress function that we are starting
1182 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1183 * @param fi the entry in the publish-structure
1184 * @param length length of the file or directory
1185 * @param meta metadata for the file or directory (can be modified)
1186 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1187 * @param bo block options
1188 * @param do_index should we index?
1189 * @param client_info pointer to client context set upon creation (can be modified)
1190 * @return #GNUNET_OK to continue (always)
1193 fip_signal_start (void *cls,
1194 struct GNUNET_FS_FileInformation *fi,
1196 struct GNUNET_CONTAINER_MetaData *meta,
1197 struct GNUNET_FS_Uri **uri,
1198 struct GNUNET_FS_BlockOptions *bo,
1202 struct GNUNET_FS_PublishContext *pc = cls;
1203 struct GNUNET_FS_ProgressInfo pi;
1207 if (GNUNET_YES == pc->skip_next_fi_callback)
1209 pc->skip_next_fi_callback = GNUNET_NO;
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Starting publish operation\n");
1216 /* space for on-demand blocks */
1217 pc->reserve_space +=
1218 ((length + DBLOCK_SIZE
1219 - 1) / DBLOCK_SIZE) * sizeof(struct OnDemandBlock);
1223 /* space for DBlocks */
1224 pc->reserve_space += length;
1226 /* entries for IBlocks and DBlocks, space for IBlocks */
1230 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1231 pc->reserve_entries += left;
1234 left = left * sizeof(struct ContentHashKey);
1235 pc->reserve_space += left;
1237 pc->reserve_entries++;
1238 /* entries and space for keywords */
1241 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1242 pc->reserve_entries += kc;
1243 pc->reserve_space += GNUNET_MAX_MESSAGE_SIZE * kc;
1245 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1246 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1247 GNUNET_FS_file_information_sync_ (fi);
1248 if ((fi->is_directory) && (fi->dir != NULL))
1250 /* We are a directory, and we are not top-level; process entries in directory */
1251 pc->skip_next_fi_callback = GNUNET_YES;
1252 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1259 * Actually signal the FS's progress function that we are suspending
1262 * @param fi the entry in the publish-structure
1263 * @param pc the publish context of which a file is being suspended
1266 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1267 struct GNUNET_FS_PublishContext *pc)
1269 struct GNUNET_FS_ProgressInfo pi;
1272 if (NULL != pc->ksk_pc)
1274 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1277 if (NULL != pc->sks_pc)
1279 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Suspending publish operation\n");
1284 GNUNET_free_non_null (fi->serialization);
1285 fi->serialization = NULL;
1286 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ?
1287 fi->data.dir.dir_size : fi->data.file.file_size;
1288 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1289 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1290 if (NULL != pc->qre)
1292 GNUNET_DATASTORE_cancel (pc->qre);
1295 if (NULL != pc->dsh)
1297 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1305 * Signal the FS's progress function that we are suspending
1306 * an upload. Performs the recursion.
1308 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1309 * @param fi the entry in the publish-structure
1310 * @param length length of the file or directory
1311 * @param meta metadata for the file or directory (can be modified)
1312 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1313 * @param bo block options
1314 * @param do_index should we index?
1315 * @param client_info pointer to client context set upon creation (can be modified)
1316 * @return #GNUNET_OK to continue (always)
1319 fip_signal_suspend (void *cls,
1320 struct GNUNET_FS_FileInformation *fi,
1322 struct GNUNET_CONTAINER_MetaData *meta,
1323 struct GNUNET_FS_Uri **uri,
1324 struct GNUNET_FS_BlockOptions *bo,
1328 struct GNUNET_FS_PublishContext *pc = cls;
1330 if (GNUNET_YES == pc->skip_next_fi_callback)
1332 pc->skip_next_fi_callback = GNUNET_NO;
1335 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1337 /* process entries in directory */
1338 pc->skip_next_fi_callback = GNUNET_YES;
1339 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1341 suspend_operation (fi, pc);
1342 *client_info = NULL;
1348 * Create SUSPEND event for the given publish operation
1349 * and then clean up our state (without stop signal).
1351 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1354 GNUNET_FS_publish_signal_suspend_ (void *cls)
1356 struct GNUNET_FS_PublishContext *pc = cls;
1358 if (NULL != pc->upload_task)
1360 GNUNET_SCHEDULER_cancel (pc->upload_task);
1361 pc->upload_task = NULL;
1363 pc->skip_next_fi_callback = GNUNET_YES;
1364 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1365 suspend_operation (pc->fi, pc);
1366 GNUNET_FS_end_top (pc->h, pc->top);
1368 publish_cleanup (pc);
1373 * We have gotten a reply for our space reservation request.
1374 * Either fail (insufficient space) or start publishing for good.
1376 * @param cls the `struct GNUNET_FS_PublishContext *`
1377 * @param success positive reservation ID on success
1378 * @param min_expiration minimum expiration time required for content to be stored
1379 * @param msg error message on error, otherwise NULL
1382 finish_reserve (void *cls,
1384 struct GNUNET_TIME_Absolute min_expiration,
1387 struct GNUNET_FS_PublishContext *pc = cls;
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "Reservation complete (%d)!\n",
1393 if ((msg != NULL) || (success <= 0))
1395 GNUNET_asprintf (&pc->fi->emsg,
1396 _ ("Datastore failure: %s"),
1398 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1402 GNUNET_assert (NULL == pc->upload_task);
1404 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1405 &GNUNET_FS_publish_main_, pc);
1410 * Calculate the total size of all of the files in the directory structure.
1412 * @param fi file structure to traverse
1415 compute_contents_size (struct GNUNET_FS_FileInformation *fi)
1417 struct GNUNET_FS_FileInformation *ent;
1419 if (GNUNET_YES != fi->is_directory)
1420 return fi->data.file.file_size;
1421 fi->data.dir.contents_size = 0;
1422 for (ent = fi->data.dir.entries; NULL != ent; ent = ent->next)
1423 fi->data.dir.contents_size += compute_contents_size (ent);
1424 return fi->data.dir.contents_size;
1429 * Publish a file or directory.
1431 * @param h handle to the file sharing subsystem
1432 * @param fi information about the file or directory structure to publish
1433 * @param ns namespace to publish the file in, NULL for no namespace
1434 * @param nid identifier to use for the publishd content in the namespace
1435 * (can be NULL, must be NULL if namespace is NULL)
1436 * @param nuid update-identifier that will be used for future updates
1437 * (can be NULL, must be NULL if namespace or nid is NULL)
1438 * @param options options for the publication
1439 * @return context that can be used to control the publish operation
1441 struct GNUNET_FS_PublishContext *
1442 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1443 struct GNUNET_FS_FileInformation *fi,
1444 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
1447 enum GNUNET_FS_PublishOptions options)
1449 struct GNUNET_FS_PublishContext *ret;
1450 struct GNUNET_DATASTORE_Handle *dsh;
1452 GNUNET_assert (NULL != h);
1453 compute_contents_size (fi);
1454 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1456 dsh = GNUNET_DATASTORE_connect (h->cfg);
1464 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1470 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1472 GNUNET_assert (NULL != nid);
1473 ret->nid = GNUNET_strdup (nid);
1475 ret->nuid = GNUNET_strdup (nuid);
1477 ret->options = options;
1479 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1480 ret->fi_pos = ret->fi;
1481 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1482 GNUNET_FS_publish_sync_ (ret);
1483 if (NULL != ret->dsh)
1485 GNUNET_assert (NULL == ret->qre);
1486 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1488 "Reserving space for %u entries and %llu bytes for publication\n"),
1489 (unsigned int) ret->reserve_entries,
1490 (unsigned long long) ret->reserve_space);
1492 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1493 ret->reserve_entries,
1499 GNUNET_assert (NULL == ret->upload_task);
1501 GNUNET_SCHEDULER_add_with_priority
1502 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1509 * Signal the FS's progress function that we are stopping
1512 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1513 * @param fi the entry in the publish-structure
1514 * @param length length of the file or directory
1515 * @param meta metadata for the file or directory (can be modified)
1516 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1517 * @param bo block options (can be modified)
1518 * @param do_index should we index?
1519 * @param client_info pointer to client context set upon creation (can be modified)
1520 * @return #GNUNET_OK to continue (always)
1523 fip_signal_stop (void *cls,
1524 struct GNUNET_FS_FileInformation *fi,
1526 struct GNUNET_CONTAINER_MetaData *meta,
1527 struct GNUNET_FS_Uri **uri,
1528 struct GNUNET_FS_BlockOptions *bo,
1529 int *do_index, void **client_info)
1531 struct GNUNET_FS_PublishContext *pc = cls;
1532 struct GNUNET_FS_ProgressInfo pi;
1535 if (GNUNET_YES == pc->skip_next_fi_callback)
1537 pc->skip_next_fi_callback = GNUNET_NO;
1540 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1542 /* process entries in directory first */
1543 pc->skip_next_fi_callback = GNUNET_YES;
1544 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1546 if (NULL != fi->serialization)
1548 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1550 GNUNET_free (fi->serialization);
1551 fi->serialization = NULL;
1553 off = (fi->chk_uri == NULL) ? 0 : length;
1554 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1555 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1556 *client_info = NULL;
1562 * Stop an upload. Will abort incomplete uploads (but
1563 * not remove blocks that have already been publishd) or
1564 * simply clean up the state for completed uploads.
1565 * Must NOT be called from within the event callback!
1567 * @param pc context for the upload to stop
1570 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1572 struct GNUNET_FS_ProgressInfo pi;
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Publish stop called\n");
1577 GNUNET_FS_end_top (pc->h, pc->top);
1578 if (NULL != pc->ksk_pc)
1580 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1583 if (NULL != pc->sks_pc)
1585 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1588 if (NULL != pc->upload_task)
1590 GNUNET_SCHEDULER_cancel (pc->upload_task);
1591 pc->upload_task = NULL;
1593 pc->skip_next_fi_callback = GNUNET_YES;
1594 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1596 if (NULL != pc->fi->serialization)
1598 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1599 pc->fi->serialization);
1600 GNUNET_free (pc->fi->serialization);
1601 pc->fi->serialization = NULL;
1603 off = (NULL == pc->fi->chk_uri) ? 0 : GNUNET_ntohll (
1604 pc->fi->chk_uri->data.chk.file_length);
1606 if (NULL != pc->serialization)
1608 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1610 GNUNET_free (pc->serialization);
1611 pc->serialization = NULL;
1613 if (NULL != pc->qre)
1615 GNUNET_DATASTORE_cancel (pc->qre);
1618 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1619 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1620 publish_cleanup (pc);
1624 /* end of fs_publish.c */