2 This file is part of GNUnet.
3 Copyright (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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 : p->data.file.file_size;
59 pi->value.publish.eta =
60 GNUNET_TIME_calculate_eta (p->start_time, offset,
61 pi->value.publish.size);
62 pi->value.publish.completed = offset;
63 pi->value.publish.duration =
64 GNUNET_TIME_absolute_get_duration (p->start_time);
65 pi->value.publish.anonymity = p->bo.anonymity_level;
67 return pc->h->upcb (pc->h->upcb_cls, pi);
72 * Cleanup the publish context, we're done with it.
74 * @param pc struct to clean up
77 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "Cleaning up publish context (done!)\n");
83 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
86 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
87 GNUNET_free_non_null (pc->nid);
88 GNUNET_free_non_null (pc->nuid);
89 GNUNET_free_non_null (pc->serialization);
92 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
95 if (NULL != pc->client)
97 GNUNET_CLIENT_disconnect (pc->client);
100 GNUNET_assert (NULL == pc->upload_task);
106 * Function called by the datastore API with
107 * the result from the PUT request.
109 * @param cls the `struct GNUNET_FS_PublishContext *`
110 * @param success #GNUNET_OK on success
111 * @param min_expiration minimum expiration time required for content to be stored
112 * @param msg error message (or NULL)
115 ds_put_cont (void *cls,
117 struct GNUNET_TIME_Absolute min_expiration,
120 struct GNUNET_FS_PublishContext *pc = cls;
121 struct GNUNET_FS_ProgressInfo pi;
124 if (GNUNET_SYSERR == success)
126 GNUNET_asprintf (&pc->fi_pos->emsg,
127 _("Publishing failed: %s"),
129 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
130 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
131 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
132 pc->fi_pos->client_info =
133 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
134 if ((GNUNET_YES != pc->fi_pos->is_directory) &&
135 (NULL != pc->fi_pos->filename) &&
136 (GNUNET_YES == pc->any_done) &&
137 (GNUNET_YES == pc->fi_pos->data.file.do_index))
139 /* run unindex to clean up */
140 GNUNET_FS_unindex_start (pc->h,
141 pc->fi_pos->filename,
146 pc->any_done = GNUNET_YES;
147 GNUNET_assert (NULL == pc->upload_task);
149 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
150 &GNUNET_FS_publish_main_, pc);
155 * Generate the callback that signals clients
156 * that a file (or directory) has been completely
159 * @param p the completed upload
160 * @param pc context of the publication
163 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
164 struct GNUNET_FS_PublishContext *pc)
166 struct GNUNET_FS_ProgressInfo pi;
168 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
169 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
170 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
171 pi.value.publish.specifics.completed.sks_uri = p->sks_uri;
173 GNUNET_FS_publish_make_status_ (&pi, pc, p,
174 p->data.file.file_size);
179 * Generate the callback that signals clients
180 * that a file (or directory) has encountered
181 * a problem during publication.
183 * @param p the upload that had trouble
184 * @param pc context of the publication
185 * @param emsg error message
188 signal_publish_error (struct GNUNET_FS_FileInformation *p,
189 struct GNUNET_FS_PublishContext *pc,
192 struct GNUNET_FS_ProgressInfo pi;
194 p->emsg = GNUNET_strdup (emsg);
195 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
196 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
197 pi.value.publish.specifics.error.message = emsg;
198 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
199 if ((p->is_directory != GNUNET_YES) &&
200 (NULL != p->filename) &&
201 (GNUNET_YES == pc->any_done) &&
202 (p->data.file.do_index == GNUNET_YES))
204 /* run unindex to clean up */
205 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 GNUNET_TIME_UNIT_FOREVER_REL,
270 &finish_release_reserve, pc);
274 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
280 * We are almost done publishing the structure,
281 * add SBlocks (if needed).
283 * @param pc overall upload data
286 publish_sblock (struct GNUNET_FS_PublishContext *pc)
289 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
297 &publish_sblocks_cont, pc);
299 publish_sblocks_cont (pc, NULL, NULL);
304 * We've finished publishing a KBlock as part of a larger upload.
305 * Check the result and continue the larger upload.
307 * @param cls the `struct GNUNET_FS_PublishContext *`
308 * of the larger upload
309 * @param uri URI of the published blocks
310 * @param emsg NULL on success, otherwise error message
313 publish_kblocks_cont (void *cls,
314 const struct GNUNET_FS_Uri *uri,
317 struct GNUNET_FS_PublishContext *pc = cls;
318 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Error uploading KSK blocks: %s\n",
326 signal_publish_error (p, pc, emsg);
327 GNUNET_FS_file_information_sync_ (p);
328 GNUNET_FS_publish_sync_ (pc);
329 GNUNET_assert (NULL == pc->upload_task);
331 GNUNET_SCHEDULER_add_with_priority
332 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
333 &GNUNET_FS_publish_main_,
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "KSK blocks published, moving on to next file\n");
340 signal_publish_completion (p, pc);
341 /* move on to next file */
343 pc->fi_pos = p->next;
346 GNUNET_FS_publish_sync_ (pc);
347 GNUNET_assert (NULL == pc->upload_task);
349 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
350 &GNUNET_FS_publish_main_, pc);
355 * Function called by the tree encoder to obtain
356 * a block of plaintext data (for the lowest level
359 * @param cls our publishing context
360 * @param offset identifies which block to get
361 * @param max (maximum) number of bytes to get; returning
362 * fewer will also cause errors
363 * @param buf where to copy the plaintext buffer
364 * @param emsg location to store an error message (on error)
365 * @return number of bytes copied to buf, 0 on error
368 block_reader (void *cls,
374 struct GNUNET_FS_PublishContext *pc = cls;
375 struct GNUNET_FS_FileInformation *p;
380 if (GNUNET_YES == p->is_directory)
382 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
383 dd = p->data.dir.dir_data;
384 memcpy (buf, &dd[offset], pt_size);
388 if (UINT64_MAX == offset)
390 if (&GNUNET_FS_data_reader_file_ == p->data.file.reader)
392 /* force closing the file to avoid keeping too many files open */
393 p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
397 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
399 return 0; /* calling reader with pt_size==0
400 * might free buf, so don't! */
402 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
411 * The tree encoder has finished processing a
412 * file. Call it's finish method and deal with
415 * @param cls our publishing context
416 * @param tc scheduler's task context (not used)
419 encode_cont (void *cls,
420 const struct GNUNET_SCHEDULER_TaskContext *tc)
422 struct GNUNET_FS_PublishContext *pc = cls;
423 struct GNUNET_FS_FileInformation *p;
424 struct GNUNET_FS_ProgressInfo pi;
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Finished with tree encoder\n");
431 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
432 GNUNET_FS_file_information_sync_ (p);
433 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Error during tree walk: %s\n",
440 GNUNET_asprintf (&p->emsg,
441 _("Publishing failed: %s"),
444 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
445 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
446 pi.value.publish.specifics.error.message = p->emsg;
447 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
451 /* final progress event */
452 GNUNET_assert (NULL != p->chk_uri);
453 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
454 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
455 pi.value.publish.specifics.progress.data = NULL;
456 pi.value.publish.specifics.progress.offset = flen;
457 pi.value.publish.specifics.progress.data_len = 0;
458 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
459 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
461 /* continue with main */ /* continue with main */
462 GNUNET_assert (NULL == pc->upload_task);
464 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
465 &GNUNET_FS_publish_main_, pc);
470 * Function called asking for the current (encoded)
471 * block to be processed. After processing the
472 * client should either call #GNUNET_FS_tree_encoder_next
473 * or (on error) #GNUNET_FS_tree_encoder_finish.
476 * @param chk content hash key for the block
477 * @param offset offset of the block in the file
478 * @param depth depth of the block in the file, 0 for DBLOCK
479 * @param type type of the block (IBLOCK or DBLOCK)
480 * @param block the (encrypted) block
481 * @param block_size size of @a block (in bytes)
484 block_proc (void *cls,
485 const struct ContentHashKey *chk,
488 enum GNUNET_BLOCK_Type type,
492 struct GNUNET_FS_PublishContext *pc = cls;
493 struct GNUNET_FS_FileInformation *p;
494 struct OnDemandBlock odb;
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
500 GNUNET_assert (NULL == pc->upload_task);
502 GNUNET_SCHEDULER_add_with_priority
503 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
507 if ( (GNUNET_YES != p->is_directory) &&
508 (GNUNET_YES == p->data.file.do_index) &&
509 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) )
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "Indexing block `%s' for offset %llu with index size %u\n",
513 GNUNET_h2s (&chk->query), (unsigned long long) offset,
514 sizeof (struct OnDemandBlock));
515 odb.offset = GNUNET_htonll (offset);
516 odb.file_id = p->data.file.file_id;
517 GNUNET_assert (pc->qre == NULL);
519 GNUNET_DATASTORE_put (pc->dsh,
520 (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
522 sizeof (struct OnDemandBlock),
524 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
525 p->bo.content_priority,
526 p->bo.anonymity_level,
527 p->bo.replication_level,
528 p->bo.expiration_time,
530 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Publishing block `%s' for offset %llu with size %u\n",
536 GNUNET_h2s (&chk->query),
537 (unsigned long long) offset,
538 (unsigned int) block_size);
539 GNUNET_assert (pc->qre == NULL);
541 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
546 p->bo.content_priority,
547 p->bo.anonymity_level,
548 p->bo.replication_level,
549 p->bo.expiration_time,
551 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
558 * Function called with information about our
559 * progress in computing the tree encoding.
562 * @param offset where are we in the file
563 * @param pt_block plaintext of the currently processed block
564 * @param pt_size size of @a pt_block
565 * @param depth depth of the block in the tree, 0 for DBLOCK
568 progress_proc (void *cls, uint64_t offset,
569 const void *pt_block,
573 struct GNUNET_FS_PublishContext *pc = cls;
574 struct GNUNET_FS_FileInformation *p;
575 struct GNUNET_FS_FileInformation *par;
576 struct GNUNET_FS_ProgressInfo pi;
579 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
580 pi.value.publish.specifics.progress.data = pt_block;
581 pi.value.publish.specifics.progress.offset = offset;
582 pi.value.publish.specifics.progress.data_len = pt_size;
583 pi.value.publish.specifics.progress.depth = depth;
584 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
586 (GNUNET_YES == p->is_directory) )
588 while (NULL != (par = p->dir))
591 GNUNET_assert (GNUNET_YES == par->is_directory);
592 p->data.dir.contents_completed += pt_size;
593 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY;
594 pi.value.publish.specifics.progress_directory.completed = p->data.dir.contents_completed;
595 pi.value.publish.specifics.progress_directory.total = p->data.dir.contents_size;
596 pi.value.publish.specifics.progress_directory.eta = GNUNET_TIME_calculate_eta (p->start_time,
597 p->data.dir.contents_completed,
598 p->data.dir.contents_size);
599 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
606 * We are uploading a file or directory; load (if necessary) the next
607 * block into memory, encrypt it and send it to the FS service. Then
608 * continue with the main task.
610 * @param pc overall upload data
613 publish_content (struct GNUNET_FS_PublishContext *pc)
615 struct GNUNET_FS_FileInformation *p;
617 struct GNUNET_FS_DirectoryBuilder *db;
618 struct GNUNET_FS_FileInformation *dirpos;
623 GNUNET_assert (NULL != p);
626 if (GNUNET_YES == p->is_directory)
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
629 db = GNUNET_FS_directory_builder_create (p->meta);
630 dirpos = p->data.dir.entries;
631 while (NULL != dirpos)
633 if (GNUNET_YES == dirpos->is_directory)
635 raw_data = dirpos->data.dir.dir_data;
636 dirpos->data.dir.dir_data = NULL;
641 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
642 (dirpos->data.file.file_size > 0))
644 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
646 if (dirpos->data.file.file_size !=
647 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
648 dirpos->data.file.file_size, raw_data,
651 GNUNET_free_non_null (emsg);
652 GNUNET_free (raw_data);
655 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
658 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
660 GNUNET_free_non_null (raw_data);
661 dirpos = dirpos->next;
663 GNUNET_free_non_null (p->data.dir.dir_data);
664 p->data.dir.dir_data = NULL;
665 p->data.dir.dir_size = 0;
666 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
667 &p->data.dir.dir_data);
668 GNUNET_FS_file_information_sync_ (p);
670 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "Creating tree encoder\n");
674 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
675 &block_proc, &progress_proc,
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Processing next block from tree\n");
681 GNUNET_FS_tree_encoder_next (p->te);
686 * Process the response (or lack thereof) from
687 * the "fs" service to our 'start index' request.
689 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
690 * @param msg the response we got
693 process_index_start_response (void *cls,
694 const struct GNUNET_MessageHeader *msg)
696 struct GNUNET_FS_PublishContext *pc = cls;
697 struct GNUNET_FS_FileInformation *p;
701 GNUNET_CLIENT_disconnect (pc->client);
706 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
707 _("Can not index file `%s': %s. Will try to insert instead.\n"),
709 _("timeout on index-start request to `fs' service"));
710 p->data.file.do_index = GNUNET_NO;
711 GNUNET_FS_file_information_sync_ (p);
712 publish_content (pc);
715 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
717 msize = ntohs (msg->size);
718 emsg = (const char *) &msg[1];
719 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
720 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
721 emsg = gettext_noop ("unknown error");
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 ("Can not index file `%s': %s. Will try to insert instead.\n"),
725 p->filename, gettext (emsg));
726 p->data.file.do_index = GNUNET_NO;
727 GNUNET_FS_file_information_sync_ (p);
728 publish_content (pc);
731 p->data.file.index_start_confirmed = GNUNET_YES;
732 /* success! continue with indexing */
733 GNUNET_FS_file_information_sync_ (p);
734 publish_content (pc);
739 * Function called once the hash computation over an
740 * indexed file has completed.
742 * @param cls closure, our publishing context
743 * @param res resulting hash, NULL on error
746 hash_for_index_cb (void *cls,
747 const struct GNUNET_HashCode *res)
749 struct GNUNET_FS_PublishContext *pc = cls;
750 struct GNUNET_FS_FileInformation *p;
751 struct IndexStartMessage *ism;
753 struct GNUNET_CLIENT_Connection *client;
762 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
763 _("Can not index file `%s': %s. Will try to insert instead.\n"),
765 _("failed to compute hash"));
766 p->data.file.do_index = GNUNET_NO;
767 GNUNET_FS_file_information_sync_ (p);
768 publish_content (pc);
771 if (GNUNET_YES == p->data.file.index_start_confirmed)
773 publish_content (pc);
776 fn = GNUNET_STRINGS_filename_expand (p->filename);
777 GNUNET_assert (fn != NULL);
778 slen = strlen (fn) + 1;
780 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
782 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 ("Can not index file `%s': %s. Will try to insert instead.\n"),
785 fn, _("filename too long"));
787 p->data.file.do_index = GNUNET_NO;
788 GNUNET_FS_file_information_sync_ (p);
789 publish_content (pc);
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
793 p->filename, GNUNET_h2s (res));
794 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
796 p->data.file.file_id = *res;
797 p->data.file.have_hash = GNUNET_YES;
798 p->data.file.index_start_confirmed = GNUNET_YES;
799 GNUNET_FS_file_information_sync_ (p);
800 publish_content (pc);
804 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
808 _("Can not index file `%s': %s. Will try to insert instead.\n"),
810 _("could not connect to `fs' service"));
811 p->data.file.do_index = GNUNET_NO;
812 publish_content (pc);
816 if (p->data.file.have_hash != GNUNET_YES)
818 p->data.file.file_id = *res;
819 p->data.file.have_hash = GNUNET_YES;
820 GNUNET_FS_file_information_sync_ (p);
822 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
823 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
824 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
825 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
827 ism->device = GNUNET_htonll (dev);
828 ism->inode = GNUNET_htonll (ino);
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 _("Failed to get file identifiers for `%s'\n"), p->filename);
836 memcpy (&ism[1], fn, slen);
839 GNUNET_break (GNUNET_YES ==
840 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
841 GNUNET_TIME_UNIT_FOREVER_REL,
843 &process_index_start_response,
850 * We've computed the CHK/LOC URI, now publish the KSKs (if applicable).
852 * @param pc publishing context to do this for
855 publish_kblocks (struct GNUNET_FS_PublishContext *pc)
857 struct GNUNET_FS_FileInformation *p;
860 /* upload of "p" complete, publish KBlocks! */
861 if (NULL != p->keywords)
863 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h,
869 &publish_kblocks_cont, pc);
873 publish_kblocks_cont (pc, p->chk_uri, NULL);
879 * Process the response (or lack thereof) from
880 * the "fs" service to our LOC sign request.
882 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
883 * @param msg the response we got
886 process_signature_response (void *cls,
887 const struct GNUNET_MessageHeader *msg)
889 struct GNUNET_FS_PublishContext *pc = cls;
890 const struct ResponseLocSignatureMessage *sig;
891 struct GNUNET_FS_FileInformation *p;
896 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
897 _("Can not create LOC URI. Will continue with CHK instead.\n"));
898 publish_kblocks (pc);
901 if (sizeof (struct ResponseLocSignatureMessage) !=
905 publish_kblocks (pc);
908 sig = (const struct ResponseLocSignatureMessage *) msg;
909 p->chk_uri->type = GNUNET_FS_URI_LOC;
910 /* p->data.loc.fi kept from CHK before */
911 p->chk_uri->data.loc.peer = sig->peer;
912 p->chk_uri->data.loc.expirationTime = GNUNET_TIME_absolute_ntoh (sig->expiration_time);
913 p->chk_uri->data.loc.contentSignature = sig->signature;
914 GNUNET_FS_file_information_sync_ (p);
915 GNUNET_FS_publish_sync_ (pc);
916 publish_kblocks (pc);
921 * We're publishing without anonymity. Contact the FS service
922 * to create a signed LOC URI for further processing, then
923 * continue with KSKs.
925 * @param pc the publishing context do to this for
928 create_loc_uri (struct GNUNET_FS_PublishContext *pc)
930 struct RequestLocSignatureMessage req;
931 struct GNUNET_FS_FileInformation *p;
933 if (NULL == pc->client)
934 pc->client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
935 if (NULL == pc->client)
937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
938 _("Can not create LOC URI. Will continue with CHK instead.\n"));
939 publish_kblocks (pc);
943 req.header.size = htons (sizeof (struct RequestLocSignatureMessage));
944 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN);
945 req.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
946 req.expiration_time = GNUNET_TIME_absolute_hton (p->bo.expiration_time);
947 req.chk = p->chk_uri->data.chk.chk;
948 req.file_length = GNUNET_htonll (p->chk_uri->data.chk.file_length);
949 GNUNET_break (GNUNET_YES ==
950 GNUNET_CLIENT_transmit_and_get_response (pc->client,
952 GNUNET_TIME_UNIT_FOREVER_REL,
954 &process_signature_response,
960 * Main function that performs the upload.
962 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
963 * @param tc task context
966 GNUNET_FS_publish_main_ (void *cls,
967 const struct GNUNET_SCHEDULER_TaskContext *tc)
969 struct GNUNET_FS_PublishContext *pc = cls;
970 struct GNUNET_FS_ProgressInfo pi;
971 struct GNUNET_FS_FileInformation *p;
974 pc->upload_task = NULL;
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Publishing complete, now publishing SKS and KSK blocks.\n");
980 /* upload of entire hierarchy complete,
981 * publish namespace entries */
982 GNUNET_FS_publish_sync_ (pc);
986 /* find starting position */
987 while ( (GNUNET_YES == p->is_directory) &&
988 (NULL != p->data.dir.entries) &&
990 (NULL == p->data.dir.entries->chk_uri) )
992 p = p->data.dir.entries;
994 GNUNET_FS_publish_sync_ (pc);
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Error uploading: %s\n",
1002 /* error with current file, abort all
1003 * related files as well! */
1004 while (NULL != p->dir)
1006 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
1007 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
1011 GNUNET_asprintf (&p->emsg,
1012 _("Recursive upload failed at `%s': %s"),
1019 GNUNET_asprintf (&p->emsg,
1020 _("Recursive upload failed: %s"),
1023 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
1024 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
1025 pi.value.publish.specifics.error.message = p->emsg;
1026 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
1028 pc->all_done = GNUNET_YES;
1029 GNUNET_FS_publish_sync_ (pc);
1032 /* handle completion */
1033 if (NULL != p->chk_uri)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "File upload complete, now publishing KSK blocks.\n");
1037 GNUNET_FS_publish_sync_ (pc);
1039 if ( (0 == p->bo.anonymity_level) &&
1041 GNUNET_FS_uri_test_loc (p->chk_uri)) )
1043 /* zero anonymity, box CHK URI in LOC URI */
1044 create_loc_uri (pc);
1048 publish_kblocks (pc);
1052 if ((GNUNET_YES != p->is_directory) && (p->data.file.do_index))
1054 if (NULL == p->filename)
1056 p->data.file.do_index = GNUNET_NO;
1057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1058 _("Can not index file `%s': %s. Will try to insert instead.\n"),
1060 _("needs to be an actual file"));
1061 GNUNET_FS_file_information_sync_ (p);
1062 publish_content (pc);
1065 if (p->data.file.have_hash)
1067 hash_for_index_cb (pc, &p->data.file.file_id);
1071 p->start_time = GNUNET_TIME_absolute_get ();
1073 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
1074 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
1078 publish_content (pc);
1083 * Signal the FS's progress function that we are starting
1086 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1087 * @param fi the entry in the publish-structure
1088 * @param length length of the file or directory
1089 * @param meta metadata for the file or directory (can be modified)
1090 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1091 * @param bo block options
1092 * @param do_index should we index?
1093 * @param client_info pointer to client context set upon creation (can be modified)
1094 * @return #GNUNET_OK to continue (always)
1097 fip_signal_start (void *cls,
1098 struct GNUNET_FS_FileInformation *fi,
1100 struct GNUNET_CONTAINER_MetaData *meta,
1101 struct GNUNET_FS_Uri **uri,
1102 struct GNUNET_FS_BlockOptions *bo,
1106 struct GNUNET_FS_PublishContext *pc = cls;
1107 struct GNUNET_FS_ProgressInfo pi;
1111 if (GNUNET_YES == pc->skip_next_fi_callback)
1113 pc->skip_next_fi_callback = GNUNET_NO;
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Starting publish operation\n");
1120 /* space for on-demand blocks */
1121 pc->reserve_space +=
1122 ((length + DBLOCK_SIZE -
1123 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1127 /* space for DBlocks */
1128 pc->reserve_space += length;
1130 /* entries for IBlocks and DBlocks, space for IBlocks */
1134 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1135 pc->reserve_entries += left;
1138 left = left * sizeof (struct ContentHashKey);
1139 pc->reserve_space += left;
1141 pc->reserve_entries++;
1142 /* entries and space for keywords */
1145 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1146 pc->reserve_entries += kc;
1147 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1149 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1150 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1151 GNUNET_FS_file_information_sync_ (fi);
1152 if ((fi->is_directory) && (fi->dir != NULL))
1154 /* We are a directory, and we are not top-level; process entries in directory */
1155 pc->skip_next_fi_callback = GNUNET_YES;
1156 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1163 * Actually signal the FS's progress function that we are suspending
1166 * @param fi the entry in the publish-structure
1167 * @param pc the publish context of which a file is being suspended
1170 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1171 struct GNUNET_FS_PublishContext *pc)
1173 struct GNUNET_FS_ProgressInfo pi;
1176 if (NULL != pc->ksk_pc)
1178 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1181 if (NULL != pc->sks_pc)
1183 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Suspending publish operation\n");
1188 GNUNET_free_non_null (fi->serialization);
1189 fi->serialization = NULL;
1190 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ? fi->data.dir.dir_size : fi->data.file.file_size;
1191 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1192 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1193 if (NULL != pc->qre)
1195 GNUNET_DATASTORE_cancel (pc->qre);
1198 if (NULL != pc->dsh)
1200 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1208 * Signal the FS's progress function that we are suspending
1209 * an upload. Performs the recursion.
1211 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1212 * @param fi the entry in the publish-structure
1213 * @param length length of the file or directory
1214 * @param meta metadata for the file or directory (can be modified)
1215 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1216 * @param bo block options
1217 * @param do_index should we index?
1218 * @param client_info pointer to client context set upon creation (can be modified)
1219 * @return #GNUNET_OK to continue (always)
1222 fip_signal_suspend (void *cls,
1223 struct GNUNET_FS_FileInformation *fi,
1225 struct GNUNET_CONTAINER_MetaData *meta,
1226 struct GNUNET_FS_Uri **uri,
1227 struct GNUNET_FS_BlockOptions *bo,
1231 struct GNUNET_FS_PublishContext *pc = cls;
1233 if (GNUNET_YES == pc->skip_next_fi_callback)
1235 pc->skip_next_fi_callback = GNUNET_NO;
1238 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1240 /* process entries in directory */
1241 pc->skip_next_fi_callback = GNUNET_YES;
1242 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1244 suspend_operation (fi, pc);
1245 *client_info = NULL;
1251 * Create SUSPEND event for the given publish operation
1252 * and then clean up our state (without stop signal).
1254 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1257 GNUNET_FS_publish_signal_suspend_ (void *cls)
1259 struct GNUNET_FS_PublishContext *pc = cls;
1261 if (NULL != pc->upload_task)
1263 GNUNET_SCHEDULER_cancel (pc->upload_task);
1264 pc->upload_task = NULL;
1266 pc->skip_next_fi_callback = GNUNET_YES;
1267 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1268 suspend_operation (pc->fi, pc);
1269 GNUNET_FS_end_top (pc->h, pc->top);
1271 publish_cleanup (pc);
1276 * We have gotten a reply for our space reservation request.
1277 * Either fail (insufficient space) or start publishing for good.
1279 * @param cls the `struct GNUNET_FS_PublishContext *`
1280 * @param success positive reservation ID on success
1281 * @param min_expiration minimum expiration time required for content to be stored
1282 * @param msg error message on error, otherwise NULL
1285 finish_reserve (void *cls,
1287 struct GNUNET_TIME_Absolute min_expiration,
1290 struct GNUNET_FS_PublishContext *pc = cls;
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Reservation complete (%d)!\n",
1296 if ((msg != NULL) || (success <= 0))
1298 GNUNET_asprintf (&pc->fi->emsg,
1299 _("Datastore failure: %s"),
1301 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1305 GNUNET_assert (NULL == pc->upload_task);
1307 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1308 &GNUNET_FS_publish_main_, pc);
1313 * Calculate the total size of all of the files in the directory structure.
1315 * @param fi file structure to traverse
1318 compute_contents_size (struct GNUNET_FS_FileInformation *fi)
1320 struct GNUNET_FS_FileInformation *ent;
1322 if (GNUNET_YES != fi->is_directory)
1323 return fi->data.file.file_size;
1324 fi->data.dir.contents_size = 0;
1325 for (ent = fi->data.dir.entries; NULL != ent; ent = ent->next)
1326 fi->data.dir.contents_size += compute_contents_size (ent);
1327 return fi->data.dir.contents_size;
1332 * Publish a file or directory.
1334 * @param h handle to the file sharing subsystem
1335 * @param fi information about the file or directory structure to publish
1336 * @param ns namespace to publish the file in, NULL for no namespace
1337 * @param nid identifier to use for the publishd content in the namespace
1338 * (can be NULL, must be NULL if namespace is NULL)
1339 * @param nuid update-identifier that will be used for future updates
1340 * (can be NULL, must be NULL if namespace or nid is NULL)
1341 * @param options options for the publication
1342 * @return context that can be used to control the publish operation
1344 struct GNUNET_FS_PublishContext *
1345 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1346 struct GNUNET_FS_FileInformation *fi,
1347 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
1350 enum GNUNET_FS_PublishOptions options)
1352 struct GNUNET_FS_PublishContext *ret;
1353 struct GNUNET_DATASTORE_Handle *dsh;
1355 GNUNET_assert (NULL != h);
1356 compute_contents_size (fi);
1357 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1359 dsh = GNUNET_DATASTORE_connect (h->cfg);
1367 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1373 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1375 GNUNET_assert (NULL != nid);
1376 ret->nid = GNUNET_strdup (nid);
1378 ret->nuid = GNUNET_strdup (nuid);
1380 ret->options = options;
1382 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1383 ret->fi_pos = ret->fi;
1384 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1385 GNUNET_FS_publish_sync_ (ret);
1386 if (NULL != ret->dsh)
1388 GNUNET_assert (NULL == ret->qre);
1389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1390 _("Reserving space for %u entries and %llu bytes for publication\n"),
1391 (unsigned int) ret->reserve_entries,
1392 (unsigned long long) ret->reserve_space);
1394 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1395 ret->reserve_entries,
1401 GNUNET_assert (NULL == ret->upload_task);
1403 GNUNET_SCHEDULER_add_with_priority
1404 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1411 * Signal the FS's progress function that we are stopping
1414 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1415 * @param fi the entry in the publish-structure
1416 * @param length length of the file or directory
1417 * @param meta metadata for the file or directory (can be modified)
1418 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1419 * @param bo block options (can be modified)
1420 * @param do_index should we index?
1421 * @param client_info pointer to client context set upon creation (can be modified)
1422 * @return #GNUNET_OK to continue (always)
1425 fip_signal_stop (void *cls,
1426 struct GNUNET_FS_FileInformation *fi,
1428 struct GNUNET_CONTAINER_MetaData *meta,
1429 struct GNUNET_FS_Uri **uri,
1430 struct GNUNET_FS_BlockOptions *bo,
1431 int *do_index, void **client_info)
1433 struct GNUNET_FS_PublishContext *pc = cls;
1434 struct GNUNET_FS_ProgressInfo pi;
1437 if (GNUNET_YES == pc->skip_next_fi_callback)
1439 pc->skip_next_fi_callback = GNUNET_NO;
1442 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1444 /* process entries in directory first */
1445 pc->skip_next_fi_callback = GNUNET_YES;
1446 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1448 if (NULL != fi->serialization)
1450 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1452 GNUNET_free (fi->serialization);
1453 fi->serialization = NULL;
1455 off = (fi->chk_uri == NULL) ? 0 : length;
1456 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1457 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1458 *client_info = NULL;
1464 * Stop an upload. Will abort incomplete uploads (but
1465 * not remove blocks that have already been publishd) or
1466 * simply clean up the state for completed uploads.
1467 * Must NOT be called from within the event callback!
1469 * @param pc context for the upload to stop
1472 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1474 struct GNUNET_FS_ProgressInfo pi;
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "Publish stop called\n");
1479 GNUNET_FS_end_top (pc->h, pc->top);
1480 if (NULL != pc->ksk_pc)
1482 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1485 if (NULL != pc->sks_pc)
1487 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1490 if (NULL != pc->upload_task)
1492 GNUNET_SCHEDULER_cancel (pc->upload_task);
1493 pc->upload_task = NULL;
1495 pc->skip_next_fi_callback = GNUNET_YES;
1496 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1498 if (NULL != pc->fi->serialization)
1500 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1501 pc->fi->serialization);
1502 GNUNET_free (pc->fi->serialization);
1503 pc->fi->serialization = NULL;
1505 off = (NULL == pc->fi->chk_uri) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1507 if (NULL != pc->serialization)
1509 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1511 GNUNET_free (pc->serialization);
1512 pc->serialization = NULL;
1514 if (NULL != pc->qre)
1516 GNUNET_DATASTORE_cancel (pc->qre);
1519 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1520 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1521 publish_cleanup (pc);
1525 /* end of fs_publish.c */