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/>.
19 * @file fs/fs_publish.c
20 * @brief publish a file or directory in GNUnet
21 * @see https://gnunet.org/encoding
22 * @author Krista Bennett
23 * @author Christian Grothoff
26 #include "gnunet_constants.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
35 * Fill in all of the generic fields for
36 * a publish event and call the callback.
38 * @param pi structure to fill in
39 * @param pc overall publishing context
40 * @param p file information for the file being published
41 * @param offset where in the file are we so far
42 * @return value returned from callback
45 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
46 struct GNUNET_FS_PublishContext *pc,
47 const struct GNUNET_FS_FileInformation *p,
50 pi->value.publish.pc = pc;
51 pi->value.publish.fi = p;
52 pi->value.publish.cctx = p->client_info;
53 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
54 pi->value.publish.filename = p->filename;
55 pi->value.publish.size =
56 (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
57 pi->value.publish.eta =
58 GNUNET_TIME_calculate_eta (p->start_time, offset,
59 pi->value.publish.size);
60 pi->value.publish.completed = offset;
61 pi->value.publish.duration =
62 GNUNET_TIME_absolute_get_duration (p->start_time);
63 pi->value.publish.anonymity = p->bo.anonymity_level;
65 return pc->h->upcb (pc->h->upcb_cls, pi);
70 * Cleanup the publish context, we're done with it.
72 * @param pc struct to clean up
75 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78 "Cleaning up publish context (done!)\n");
81 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
84 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
85 GNUNET_free_non_null (pc->nid);
86 GNUNET_free_non_null (pc->nuid);
87 GNUNET_free_non_null (pc->serialization);
90 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
95 GNUNET_MQ_destroy (pc->mq);
98 GNUNET_assert (NULL == pc->upload_task);
104 * Function called by the datastore API with
105 * the result from the PUT request.
107 * @param cls the `struct GNUNET_FS_PublishContext *`
108 * @param success #GNUNET_OK on success
109 * @param min_expiration minimum expiration time required for content to be stored
110 * @param msg error message (or NULL)
113 ds_put_cont (void *cls,
115 struct GNUNET_TIME_Absolute min_expiration,
118 struct GNUNET_FS_PublishContext *pc = cls;
119 struct GNUNET_FS_ProgressInfo pi;
122 if (GNUNET_SYSERR == success)
124 GNUNET_asprintf (&pc->fi_pos->emsg,
125 _("Publishing failed: %s"),
127 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
128 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
129 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
130 pc->fi_pos->client_info =
131 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
132 if ((GNUNET_YES != pc->fi_pos->is_directory) &&
133 (NULL != pc->fi_pos->filename) &&
134 (GNUNET_YES == pc->any_done) &&
135 (GNUNET_YES == pc->fi_pos->data.file.do_index))
137 /* run unindex to clean up */
138 GNUNET_FS_unindex_start (pc->h,
139 pc->fi_pos->filename,
144 pc->any_done = GNUNET_YES;
145 GNUNET_assert (NULL == pc->upload_task);
147 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
148 &GNUNET_FS_publish_main_, pc);
153 * Generate the callback that signals clients
154 * that a file (or directory) has been completely
157 * @param p the completed upload
158 * @param pc context of the publication
161 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
162 struct GNUNET_FS_PublishContext *pc)
164 struct GNUNET_FS_ProgressInfo pi;
166 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
167 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
168 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
169 pi.value.publish.specifics.completed.sks_uri = p->sks_uri;
171 GNUNET_FS_publish_make_status_ (&pi, pc, p,
172 p->data.file.file_size);
177 * Generate the callback that signals clients
178 * that a file (or directory) has encountered
179 * a problem during publication.
181 * @param p the upload that had trouble
182 * @param pc context of the publication
183 * @param emsg error message
186 signal_publish_error (struct GNUNET_FS_FileInformation *p,
187 struct GNUNET_FS_PublishContext *pc,
190 struct GNUNET_FS_ProgressInfo pi;
192 p->emsg = GNUNET_strdup (emsg);
193 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
194 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
195 pi.value.publish.specifics.error.message = emsg;
196 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
197 if ((p->is_directory != GNUNET_YES) &&
198 (NULL != p->filename) &&
199 (GNUNET_YES == pc->any_done) &&
200 (p->data.file.do_index == GNUNET_YES))
202 /* run unindex to clean up */
203 GNUNET_FS_unindex_start (pc->h,
212 * Datastore returns from reservation cancel request.
214 * @param cls the `struct GNUNET_FS_PublishContext *`
215 * @param success success code (not used)
216 * @param min_expiration minimum expiration time required for content to be stored
217 * @param msg error message (typically NULL, not used)
220 finish_release_reserve (void *cls, int success,
221 struct GNUNET_TIME_Absolute min_expiration,
224 struct GNUNET_FS_PublishContext *pc = cls;
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Releasing reserve done!\n");
229 signal_publish_completion (pc->fi, pc);
230 pc->all_done = GNUNET_YES;
231 GNUNET_FS_publish_sync_ (pc);
236 * We've finished publishing the SBlock as part of a larger upload.
237 * Check the result and complete the larger upload.
239 * @param cls the `struct GNUNET_FS_PublishContext *` of the larger upload
240 * @param uri URI of the published SBlock
241 * @param emsg NULL on success, otherwise error message
244 publish_sblocks_cont (void *cls,
245 const struct GNUNET_FS_Uri *uri,
248 struct GNUNET_FS_PublishContext *pc = cls;
253 signal_publish_error (pc->fi, pc, emsg);
254 GNUNET_FS_publish_sync_ (pc);
259 /* sks publication, remember namespace URI */
260 pc->fi->sks_uri = GNUNET_FS_uri_dup (uri);
262 GNUNET_assert (pc->qre == NULL);
263 if ((pc->dsh != NULL) && (pc->rid != 0))
266 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
267 &finish_release_reserve, pc);
271 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
277 * We are almost done publishing the structure,
278 * add SBlocks (if needed).
280 * @param pc overall upload data
283 publish_sblock (struct GNUNET_FS_PublishContext *pc)
286 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
294 &publish_sblocks_cont, pc);
296 publish_sblocks_cont (pc, NULL, NULL);
301 * We've finished publishing a KBlock as part of a larger upload.
302 * Check the result and continue the larger upload.
304 * @param cls the `struct GNUNET_FS_PublishContext *`
305 * of the larger upload
306 * @param uri URI of the published blocks
307 * @param emsg NULL on success, otherwise error message
310 publish_kblocks_cont (void *cls,
311 const struct GNUNET_FS_Uri *uri,
314 struct GNUNET_FS_PublishContext *pc = cls;
315 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Error uploading KSK blocks: %s\n",
323 signal_publish_error (p, pc, emsg);
324 GNUNET_FS_file_information_sync_ (p);
325 GNUNET_FS_publish_sync_ (pc);
326 GNUNET_assert (NULL == pc->upload_task);
328 GNUNET_SCHEDULER_add_with_priority
329 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
330 &GNUNET_FS_publish_main_,
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "KSK blocks published, moving on to next file\n");
337 signal_publish_completion (p, pc);
338 /* move on to next file */
340 pc->fi_pos = p->next;
343 GNUNET_FS_publish_sync_ (pc);
344 GNUNET_assert (NULL == pc->upload_task);
346 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
347 &GNUNET_FS_publish_main_, pc);
352 * Function called by the tree encoder to obtain
353 * a block of plaintext data (for the lowest level
356 * @param cls our publishing context
357 * @param offset identifies which block to get
358 * @param max (maximum) number of bytes to get; returning
359 * fewer will also cause errors
360 * @param buf where to copy the plaintext buffer
361 * @param emsg location to store an error message (on error)
362 * @return number of bytes copied to buf, 0 on error
365 block_reader (void *cls,
371 struct GNUNET_FS_PublishContext *pc = cls;
372 struct GNUNET_FS_FileInformation *p;
377 if (GNUNET_YES == p->is_directory)
379 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
380 dd = p->data.dir.dir_data;
381 GNUNET_memcpy (buf, &dd[offset], pt_size);
385 if (UINT64_MAX == offset)
387 if (&GNUNET_FS_data_reader_file_ == p->data.file.reader)
389 /* force closing the file to avoid keeping too many files open */
390 p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
394 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
396 return 0; /* calling reader with pt_size==0
397 * might free buf, so don't! */
399 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
408 * The tree encoder has finished processing a
409 * file. Call it's finish method and deal with
412 * @param cls our publishing context
415 encode_cont (void *cls)
417 struct GNUNET_FS_PublishContext *pc = cls;
418 struct GNUNET_FS_FileInformation *p;
419 struct GNUNET_FS_ProgressInfo pi;
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Finished with tree encoder\n");
426 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
427 GNUNET_FS_file_information_sync_ (p);
428 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Error during tree walk: %s\n",
435 GNUNET_asprintf (&p->emsg,
436 _("Publishing failed: %s"),
439 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
440 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
441 pi.value.publish.specifics.error.message = p->emsg;
442 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
446 /* final progress event */
447 GNUNET_assert (NULL != p->chk_uri);
448 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
449 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
450 pi.value.publish.specifics.progress.data = NULL;
451 pi.value.publish.specifics.progress.offset = flen;
452 pi.value.publish.specifics.progress.data_len = 0;
453 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
454 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
456 /* continue with main */ /* continue with main */
457 GNUNET_assert (NULL == pc->upload_task);
459 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
460 &GNUNET_FS_publish_main_, pc);
465 * Function called asking for the current (encoded)
466 * block to be processed. After processing the
467 * client should either call #GNUNET_FS_tree_encoder_next
468 * or (on error) #GNUNET_FS_tree_encoder_finish.
471 * @param chk content hash key for the block
472 * @param offset offset of the block in the file
473 * @param depth depth of the block in the file, 0 for DBLOCK
474 * @param type type of the block (IBLOCK or DBLOCK)
475 * @param block the (encrypted) block
476 * @param block_size size of @a block (in bytes)
479 block_proc (void *cls,
480 const struct ContentHashKey *chk,
483 enum GNUNET_BLOCK_Type type,
487 struct GNUNET_FS_PublishContext *pc = cls;
488 struct GNUNET_FS_FileInformation *p;
489 struct OnDemandBlock odb;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Waiting for datastore connection\n");
496 GNUNET_assert (NULL == pc->upload_task);
498 GNUNET_SCHEDULER_add_with_priority
499 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
503 if ( (GNUNET_YES != p->is_directory) &&
504 (GNUNET_YES == p->data.file.do_index) &&
505 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) )
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Indexing block `%s' for offset %llu with index size %u\n",
509 GNUNET_h2s (&chk->query),
510 (unsigned long long) offset,
511 (unsigned int) sizeof (struct OnDemandBlock));
512 odb.offset = GNUNET_htonll (offset);
513 odb.file_id = p->data.file.file_id;
514 GNUNET_assert (pc->qre == NULL);
516 GNUNET_DATASTORE_put (pc->dsh,
517 (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
519 sizeof (struct OnDemandBlock),
521 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
522 p->bo.content_priority,
523 p->bo.anonymity_level,
524 p->bo.replication_level,
525 p->bo.expiration_time,
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Publishing block `%s' for offset %llu with size %u\n",
532 GNUNET_h2s (&chk->query),
533 (unsigned long long) offset,
534 (unsigned int) block_size);
535 GNUNET_assert (pc->qre == NULL);
537 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
542 p->bo.content_priority,
543 p->bo.anonymity_level,
544 p->bo.replication_level,
545 p->bo.expiration_time,
553 * Function called with information about our
554 * progress in computing the tree encoding.
557 * @param offset where are we in the file
558 * @param pt_block plaintext of the currently processed block
559 * @param pt_size size of @a pt_block
560 * @param depth depth of the block in the tree, 0 for DBLOCK
563 progress_proc (void *cls, uint64_t offset,
564 const void *pt_block,
568 struct GNUNET_FS_PublishContext *pc = cls;
569 struct GNUNET_FS_FileInformation *p;
570 struct GNUNET_FS_FileInformation *par;
571 struct GNUNET_FS_ProgressInfo pi;
574 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
575 pi.value.publish.specifics.progress.data = pt_block;
576 pi.value.publish.specifics.progress.offset = offset;
577 pi.value.publish.specifics.progress.data_len = pt_size;
578 pi.value.publish.specifics.progress.depth = depth;
579 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
581 (GNUNET_YES == p->is_directory) )
583 while (NULL != (par = p->dir))
586 GNUNET_assert (GNUNET_YES == par->is_directory);
587 p->data.dir.contents_completed += pt_size;
588 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY;
589 pi.value.publish.specifics.progress_directory.completed = p->data.dir.contents_completed;
590 pi.value.publish.specifics.progress_directory.total = p->data.dir.contents_size;
591 pi.value.publish.specifics.progress_directory.eta = GNUNET_TIME_calculate_eta (p->start_time,
592 p->data.dir.contents_completed,
593 p->data.dir.contents_size);
594 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
601 * We are uploading a file or directory; load (if necessary) the next
602 * block into memory, encrypt it and send it to the FS service. Then
603 * continue with the main task.
605 * @param pc overall upload data
608 publish_content (struct GNUNET_FS_PublishContext *pc)
610 struct GNUNET_FS_FileInformation *p;
612 struct GNUNET_FS_DirectoryBuilder *db;
613 struct GNUNET_FS_FileInformation *dirpos;
618 GNUNET_assert (NULL != p);
621 if (GNUNET_YES == p->is_directory)
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
624 db = GNUNET_FS_directory_builder_create (p->meta);
625 dirpos = p->data.dir.entries;
626 while (NULL != dirpos)
628 if (GNUNET_YES == dirpos->is_directory)
630 raw_data = dirpos->data.dir.dir_data;
631 dirpos->data.dir.dir_data = NULL;
636 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
637 (dirpos->data.file.file_size > 0))
639 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
641 if (dirpos->data.file.file_size !=
642 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
643 dirpos->data.file.file_size, raw_data,
646 GNUNET_free_non_null (emsg);
647 GNUNET_free (raw_data);
650 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
653 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
655 GNUNET_free_non_null (raw_data);
656 dirpos = dirpos->next;
658 GNUNET_free_non_null (p->data.dir.dir_data);
659 p->data.dir.dir_data = NULL;
660 p->data.dir.dir_size = 0;
661 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
662 &p->data.dir.dir_data);
663 GNUNET_FS_file_information_sync_ (p);
665 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Creating tree encoder\n");
669 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
670 &block_proc, &progress_proc,
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Processing next block from tree\n");
676 GNUNET_FS_tree_encoder_next (p->te);
681 * Check the response from the "fs" service to our 'start index'
684 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
685 * @param msg the response we got
688 check_index_start_failed (void *cls,
689 const struct GNUNET_MessageHeader *msg)
691 size_t msize = ntohs (msg->size) - sizeof (*msg);
692 const char *emsg = (const char *) &msg[1];
694 if (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
697 return GNUNET_SYSERR;
704 * Process the response from the "fs" service to our 'start index'
707 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
708 * @param msg the response we got
711 handle_index_start_failed (void *cls,
712 const struct GNUNET_MessageHeader *msg)
714 struct GNUNET_FS_PublishContext *pc = cls;
715 struct GNUNET_FS_FileInformation *p;
716 const char *emsg = (const char *) &msg[1];
718 GNUNET_MQ_destroy (pc->mq);
721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722 _("Can not index file `%s': %s. Will try to insert instead.\n"),
725 p->data.file.do_index = GNUNET_NO;
726 GNUNET_FS_file_information_sync_ (p);
727 publish_content (pc);
732 * Process the response from the "fs" service to our 'start index'
735 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
736 * @param msg the response we got
739 handle_index_start_ok (void *cls,
740 const struct GNUNET_MessageHeader *msg)
742 struct GNUNET_FS_PublishContext *pc = cls;
743 struct GNUNET_FS_FileInformation *p;
745 GNUNET_MQ_destroy (pc->mq);
748 p->data.file.index_start_confirmed = GNUNET_YES;
749 GNUNET_FS_file_information_sync_ (p);
750 publish_content (pc);
755 * Generic error handler, called with the appropriate error code and
756 * the same closure specified at the creation of the message queue.
757 * Not every message queue implementation supports an error handler.
759 * @param cls closure with the `struct GNUNET_FS_PublishContext *`
760 * @param error error code
763 index_mq_error_handler (void *cls,
764 enum GNUNET_MQ_Error error)
766 struct GNUNET_FS_PublishContext *pc = cls;
767 struct GNUNET_FS_FileInformation *p;
771 GNUNET_MQ_destroy (pc->mq);
775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
776 _("Can not index file `%s': %s. Will try to insert instead.\n"),
778 _("error on index-start request to `fs' service"));
779 p->data.file.do_index = GNUNET_NO;
780 GNUNET_FS_file_information_sync_ (p);
781 publish_content (pc);
786 * Function called once the hash computation over an
787 * indexed file has completed.
789 * @param cls closure, our publishing context
790 * @param res resulting hash, NULL on error
793 hash_for_index_cb (void *cls,
794 const struct GNUNET_HashCode *res)
796 struct GNUNET_FS_PublishContext *pc = cls;
797 struct GNUNET_MQ_MessageHandler handlers[] = {
798 GNUNET_MQ_hd_fixed_size (index_start_ok,
799 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK,
800 struct GNUNET_MessageHeader,
802 GNUNET_MQ_hd_var_size (index_start_failed,
803 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED,
804 struct GNUNET_MessageHeader,
806 GNUNET_MQ_handler_end ()
808 struct GNUNET_FS_FileInformation *p;
809 struct GNUNET_MQ_Envelope *env;
810 struct IndexStartMessage *ism;
820 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
821 _("Can not index file `%s': %s. Will try to insert instead.\n"),
823 _("failed to compute hash"));
824 p->data.file.do_index = GNUNET_NO;
825 GNUNET_FS_file_information_sync_ (p);
826 publish_content (pc);
829 if (GNUNET_YES == p->data.file.index_start_confirmed)
831 publish_content (pc);
834 fn = GNUNET_STRINGS_filename_expand (p->filename);
835 GNUNET_assert (fn != NULL);
836 slen = strlen (fn) + 1;
838 GNUNET_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
840 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
842 ("Can not index file `%s': %s. Will try to insert instead.\n"),
843 fn, _("filename too long"));
845 p->data.file.do_index = GNUNET_NO;
846 GNUNET_FS_file_information_sync_ (p);
847 publish_content (pc);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Hash of indexed file `%s' is `%s'\n",
854 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
856 p->data.file.file_id = *res;
857 p->data.file.have_hash = GNUNET_YES;
858 p->data.file.index_start_confirmed = GNUNET_YES;
859 GNUNET_FS_file_information_sync_ (p);
860 publish_content (pc);
864 pc->mq = GNUNET_CLIENT_connect (pc->h->cfg,
867 &index_mq_error_handler,
871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
872 _("Can not index file `%s': %s. Will try to insert instead.\n"),
874 _("could not connect to `fs' service"));
875 p->data.file.do_index = GNUNET_NO;
876 publish_content (pc);
880 if (p->data.file.have_hash != GNUNET_YES)
882 p->data.file.file_id = *res;
883 p->data.file.have_hash = GNUNET_YES;
884 GNUNET_FS_file_information_sync_ (p);
886 env = GNUNET_MQ_msg_extra (ism,
888 GNUNET_MESSAGE_TYPE_FS_INDEX_START);
890 GNUNET_DISK_file_get_identifiers (p->filename,
894 ism->device = GNUNET_htonll (dev);
895 ism->inode = GNUNET_htonll (ino);
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 _("Failed to get file identifiers for `%s'\n"),
904 GNUNET_memcpy (&ism[1],
908 GNUNET_MQ_send (pc->mq,
914 * We've computed the CHK/LOC URI, now publish the KSKs (if applicable).
916 * @param pc publishing context to do this for
919 publish_kblocks (struct GNUNET_FS_PublishContext *pc)
921 struct GNUNET_FS_FileInformation *p;
924 /* upload of "p" complete, publish KBlocks! */
925 if (NULL != p->keywords)
927 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h,
933 &publish_kblocks_cont,
938 publish_kblocks_cont (pc, p->chk_uri, NULL);
944 * Process the response from the "fs" service to our LOC sign request.
946 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
947 * @param sig the response we got
950 handle_signature_response (void *cls,
951 const struct ResponseLocSignatureMessage *sig)
953 struct GNUNET_FS_PublishContext *pc = cls;
954 struct GNUNET_FS_FileInformation *p;
957 p->chk_uri->type = GNUNET_FS_URI_LOC;
958 /* p->data.loc.fi kept from CHK before */
959 p->chk_uri->data.loc.peer = sig->peer;
960 p->chk_uri->data.loc.expirationTime
961 = GNUNET_TIME_absolute_ntoh (sig->expiration_time);
962 p->chk_uri->data.loc.contentSignature = sig->signature;
963 GNUNET_FS_file_information_sync_ (p);
964 GNUNET_FS_publish_sync_ (pc);
965 publish_kblocks (pc);
970 * Generic error handler, called with the appropriate error code and
971 * the same closure specified at the creation of the message queue.
972 * Not every message queue implementation supports an error handler.
974 * @param cls closure with the `struct GNUNET_FS_PublishContext *`
975 * @param error error code
978 loc_mq_error_handler (void *cls,
979 enum GNUNET_MQ_Error error)
981 struct GNUNET_FS_PublishContext *pc = cls;
985 GNUNET_MQ_destroy (pc->mq);
988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
989 _("Can not create LOC URI. Will continue with CHK instead.\n"));
990 publish_kblocks (pc);
995 * We're publishing without anonymity. Contact the FS service
996 * to create a signed LOC URI for further processing, then
997 * continue with KSKs.
999 * @param pc the publishing context do to this for
1002 create_loc_uri (struct GNUNET_FS_PublishContext *pc)
1004 struct GNUNET_MQ_MessageHandler handlers[] = {
1005 GNUNET_MQ_hd_fixed_size (signature_response,
1006 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE,
1007 struct ResponseLocSignatureMessage,
1009 GNUNET_MQ_handler_end ()
1011 struct GNUNET_MQ_Envelope *env;
1012 struct RequestLocSignatureMessage *req;
1013 struct GNUNET_FS_FileInformation *p;
1016 GNUNET_MQ_destroy (pc->mq);
1017 pc->mq = GNUNET_CLIENT_connect (pc->h->cfg,
1020 &loc_mq_error_handler,
1024 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1025 _("Can not create LOC URI. Will continue with CHK instead.\n"));
1026 publish_kblocks (pc);
1030 env = GNUNET_MQ_msg (req,
1031 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN);
1032 req->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
1033 req->expiration_time = GNUNET_TIME_absolute_hton (p->bo.expiration_time);
1034 req->chk = p->chk_uri->data.chk.chk;
1035 req->file_length = GNUNET_htonll (p->chk_uri->data.chk.file_length);
1036 GNUNET_MQ_send (pc->mq,
1042 * Main function that performs the upload.
1044 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
1047 GNUNET_FS_publish_main_ (void *cls)
1049 struct GNUNET_FS_PublishContext *pc = cls;
1050 struct GNUNET_FS_ProgressInfo pi;
1051 struct GNUNET_FS_FileInformation *p;
1054 pc->upload_task = NULL;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Publishing complete, now publishing SKS and KSK blocks.\n");
1060 /* upload of entire hierarchy complete,
1061 * publish namespace entries */
1062 GNUNET_FS_publish_sync_ (pc);
1063 publish_sblock (pc);
1066 /* find starting position */
1067 while ( (GNUNET_YES == p->is_directory) &&
1068 (NULL != p->data.dir.entries) &&
1069 (NULL == p->emsg) &&
1070 (NULL == p->data.dir.entries->chk_uri) )
1072 p = p->data.dir.entries;
1074 GNUNET_FS_publish_sync_ (pc);
1076 /* abort on error */
1077 if (NULL != p->emsg)
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Error uploading: %s\n",
1082 /* error with current file, abort all
1083 * related files as well! */
1084 while (NULL != p->dir)
1086 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
1087 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
1091 GNUNET_asprintf (&p->emsg,
1092 _("Recursive upload failed at `%s': %s"),
1099 GNUNET_asprintf (&p->emsg,
1100 _("Recursive upload failed: %s"),
1103 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
1104 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
1105 pi.value.publish.specifics.error.message = p->emsg;
1106 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
1108 pc->all_done = GNUNET_YES;
1109 GNUNET_FS_publish_sync_ (pc);
1112 /* handle completion */
1113 if (NULL != p->chk_uri)
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "File upload complete, now publishing KSK blocks.\n");
1117 GNUNET_FS_publish_sync_ (pc);
1119 if ( (0 == p->bo.anonymity_level) &&
1121 GNUNET_FS_uri_test_loc (p->chk_uri)) )
1123 /* zero anonymity, box CHK URI in LOC URI */
1124 create_loc_uri (pc);
1128 publish_kblocks (pc);
1132 if ((GNUNET_YES != p->is_directory) && (p->data.file.do_index))
1134 if (NULL == p->filename)
1136 p->data.file.do_index = GNUNET_NO;
1137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1138 _("Can not index file `%s': %s. Will try to insert instead.\n"),
1140 _("needs to be an actual file"));
1141 GNUNET_FS_file_information_sync_ (p);
1142 publish_content (pc);
1145 if (p->data.file.have_hash)
1147 hash_for_index_cb (pc, &p->data.file.file_id);
1151 p->start_time = GNUNET_TIME_absolute_get ();
1153 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
1154 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
1158 publish_content (pc);
1163 * Signal the FS's progress function that we are starting
1166 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1167 * @param fi the entry in the publish-structure
1168 * @param length length of the file or directory
1169 * @param meta metadata for the file or directory (can be modified)
1170 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1171 * @param bo block options
1172 * @param do_index should we index?
1173 * @param client_info pointer to client context set upon creation (can be modified)
1174 * @return #GNUNET_OK to continue (always)
1177 fip_signal_start (void *cls,
1178 struct GNUNET_FS_FileInformation *fi,
1180 struct GNUNET_CONTAINER_MetaData *meta,
1181 struct GNUNET_FS_Uri **uri,
1182 struct GNUNET_FS_BlockOptions *bo,
1186 struct GNUNET_FS_PublishContext *pc = cls;
1187 struct GNUNET_FS_ProgressInfo pi;
1191 if (GNUNET_YES == pc->skip_next_fi_callback)
1193 pc->skip_next_fi_callback = GNUNET_NO;
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Starting publish operation\n");
1200 /* space for on-demand blocks */
1201 pc->reserve_space +=
1202 ((length + DBLOCK_SIZE -
1203 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1207 /* space for DBlocks */
1208 pc->reserve_space += length;
1210 /* entries for IBlocks and DBlocks, space for IBlocks */
1214 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1215 pc->reserve_entries += left;
1218 left = left * sizeof (struct ContentHashKey);
1219 pc->reserve_space += left;
1221 pc->reserve_entries++;
1222 /* entries and space for keywords */
1225 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1226 pc->reserve_entries += kc;
1227 pc->reserve_space += GNUNET_MAX_MESSAGE_SIZE * kc;
1229 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1230 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1231 GNUNET_FS_file_information_sync_ (fi);
1232 if ((fi->is_directory) && (fi->dir != NULL))
1234 /* We are a directory, and we are not top-level; process entries in directory */
1235 pc->skip_next_fi_callback = GNUNET_YES;
1236 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1243 * Actually signal the FS's progress function that we are suspending
1246 * @param fi the entry in the publish-structure
1247 * @param pc the publish context of which a file is being suspended
1250 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1251 struct GNUNET_FS_PublishContext *pc)
1253 struct GNUNET_FS_ProgressInfo pi;
1256 if (NULL != pc->ksk_pc)
1258 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1261 if (NULL != pc->sks_pc)
1263 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Suspending publish operation\n");
1268 GNUNET_free_non_null (fi->serialization);
1269 fi->serialization = NULL;
1270 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ? fi->data.dir.dir_size : fi->data.file.file_size;
1271 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1272 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1273 if (NULL != pc->qre)
1275 GNUNET_DATASTORE_cancel (pc->qre);
1278 if (NULL != pc->dsh)
1280 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1288 * Signal the FS's progress function that we are suspending
1289 * an upload. Performs the recursion.
1291 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1292 * @param fi the entry in the publish-structure
1293 * @param length length of the file or directory
1294 * @param meta metadata for the file or directory (can be modified)
1295 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1296 * @param bo block options
1297 * @param do_index should we index?
1298 * @param client_info pointer to client context set upon creation (can be modified)
1299 * @return #GNUNET_OK to continue (always)
1302 fip_signal_suspend (void *cls,
1303 struct GNUNET_FS_FileInformation *fi,
1305 struct GNUNET_CONTAINER_MetaData *meta,
1306 struct GNUNET_FS_Uri **uri,
1307 struct GNUNET_FS_BlockOptions *bo,
1311 struct GNUNET_FS_PublishContext *pc = cls;
1313 if (GNUNET_YES == pc->skip_next_fi_callback)
1315 pc->skip_next_fi_callback = GNUNET_NO;
1318 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1320 /* process entries in directory */
1321 pc->skip_next_fi_callback = GNUNET_YES;
1322 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1324 suspend_operation (fi, pc);
1325 *client_info = NULL;
1331 * Create SUSPEND event for the given publish operation
1332 * and then clean up our state (without stop signal).
1334 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1337 GNUNET_FS_publish_signal_suspend_ (void *cls)
1339 struct GNUNET_FS_PublishContext *pc = cls;
1341 if (NULL != pc->upload_task)
1343 GNUNET_SCHEDULER_cancel (pc->upload_task);
1344 pc->upload_task = NULL;
1346 pc->skip_next_fi_callback = GNUNET_YES;
1347 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1348 suspend_operation (pc->fi, pc);
1349 GNUNET_FS_end_top (pc->h, pc->top);
1351 publish_cleanup (pc);
1356 * We have gotten a reply for our space reservation request.
1357 * Either fail (insufficient space) or start publishing for good.
1359 * @param cls the `struct GNUNET_FS_PublishContext *`
1360 * @param success positive reservation ID on success
1361 * @param min_expiration minimum expiration time required for content to be stored
1362 * @param msg error message on error, otherwise NULL
1365 finish_reserve (void *cls,
1367 struct GNUNET_TIME_Absolute min_expiration,
1370 struct GNUNET_FS_PublishContext *pc = cls;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Reservation complete (%d)!\n",
1376 if ((msg != NULL) || (success <= 0))
1378 GNUNET_asprintf (&pc->fi->emsg,
1379 _("Datastore failure: %s"),
1381 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1385 GNUNET_assert (NULL == pc->upload_task);
1387 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1388 &GNUNET_FS_publish_main_, pc);
1393 * Calculate the total size of all of the files in the directory structure.
1395 * @param fi file structure to traverse
1398 compute_contents_size (struct GNUNET_FS_FileInformation *fi)
1400 struct GNUNET_FS_FileInformation *ent;
1402 if (GNUNET_YES != fi->is_directory)
1403 return fi->data.file.file_size;
1404 fi->data.dir.contents_size = 0;
1405 for (ent = fi->data.dir.entries; NULL != ent; ent = ent->next)
1406 fi->data.dir.contents_size += compute_contents_size (ent);
1407 return fi->data.dir.contents_size;
1412 * Publish a file or directory.
1414 * @param h handle to the file sharing subsystem
1415 * @param fi information about the file or directory structure to publish
1416 * @param ns namespace to publish the file in, NULL for no namespace
1417 * @param nid identifier to use for the publishd content in the namespace
1418 * (can be NULL, must be NULL if namespace is NULL)
1419 * @param nuid update-identifier that will be used for future updates
1420 * (can be NULL, must be NULL if namespace or nid is NULL)
1421 * @param options options for the publication
1422 * @return context that can be used to control the publish operation
1424 struct GNUNET_FS_PublishContext *
1425 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1426 struct GNUNET_FS_FileInformation *fi,
1427 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
1430 enum GNUNET_FS_PublishOptions options)
1432 struct GNUNET_FS_PublishContext *ret;
1433 struct GNUNET_DATASTORE_Handle *dsh;
1435 GNUNET_assert (NULL != h);
1436 compute_contents_size (fi);
1437 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1439 dsh = GNUNET_DATASTORE_connect (h->cfg);
1447 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1453 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1455 GNUNET_assert (NULL != nid);
1456 ret->nid = GNUNET_strdup (nid);
1458 ret->nuid = GNUNET_strdup (nuid);
1460 ret->options = options;
1462 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1463 ret->fi_pos = ret->fi;
1464 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1465 GNUNET_FS_publish_sync_ (ret);
1466 if (NULL != ret->dsh)
1468 GNUNET_assert (NULL == ret->qre);
1469 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1470 _("Reserving space for %u entries and %llu bytes for publication\n"),
1471 (unsigned int) ret->reserve_entries,
1472 (unsigned long long) ret->reserve_space);
1474 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1475 ret->reserve_entries,
1481 GNUNET_assert (NULL == ret->upload_task);
1483 GNUNET_SCHEDULER_add_with_priority
1484 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1491 * Signal the FS's progress function that we are stopping
1494 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1495 * @param fi the entry in the publish-structure
1496 * @param length length of the file or directory
1497 * @param meta metadata for the file or directory (can be modified)
1498 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1499 * @param bo block options (can be modified)
1500 * @param do_index should we index?
1501 * @param client_info pointer to client context set upon creation (can be modified)
1502 * @return #GNUNET_OK to continue (always)
1505 fip_signal_stop (void *cls,
1506 struct GNUNET_FS_FileInformation *fi,
1508 struct GNUNET_CONTAINER_MetaData *meta,
1509 struct GNUNET_FS_Uri **uri,
1510 struct GNUNET_FS_BlockOptions *bo,
1511 int *do_index, void **client_info)
1513 struct GNUNET_FS_PublishContext *pc = cls;
1514 struct GNUNET_FS_ProgressInfo pi;
1517 if (GNUNET_YES == pc->skip_next_fi_callback)
1519 pc->skip_next_fi_callback = GNUNET_NO;
1522 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1524 /* process entries in directory first */
1525 pc->skip_next_fi_callback = GNUNET_YES;
1526 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1528 if (NULL != fi->serialization)
1530 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1532 GNUNET_free (fi->serialization);
1533 fi->serialization = NULL;
1535 off = (fi->chk_uri == NULL) ? 0 : length;
1536 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1537 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1538 *client_info = NULL;
1544 * Stop an upload. Will abort incomplete uploads (but
1545 * not remove blocks that have already been publishd) or
1546 * simply clean up the state for completed uploads.
1547 * Must NOT be called from within the event callback!
1549 * @param pc context for the upload to stop
1552 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1554 struct GNUNET_FS_ProgressInfo pi;
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "Publish stop called\n");
1559 GNUNET_FS_end_top (pc->h, pc->top);
1560 if (NULL != pc->ksk_pc)
1562 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1565 if (NULL != pc->sks_pc)
1567 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1570 if (NULL != pc->upload_task)
1572 GNUNET_SCHEDULER_cancel (pc->upload_task);
1573 pc->upload_task = NULL;
1575 pc->skip_next_fi_callback = GNUNET_YES;
1576 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1578 if (NULL != pc->fi->serialization)
1580 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1581 pc->fi->serialization);
1582 GNUNET_free (pc->fi->serialization);
1583 pc->fi->serialization = NULL;
1585 off = (NULL == pc->fi->chk_uri) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1587 if (NULL != pc->serialization)
1589 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1591 GNUNET_free (pc->serialization);
1592 pc->serialization = NULL;
1594 if (NULL != pc->qre)
1596 GNUNET_DATASTORE_cancel (pc->qre);
1599 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1600 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1601 publish_cleanup (pc);
1605 /* end of fs_publish.c */