2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_publish.c
23 * @brief publish a file or directory in GNUnet
24 * @see https://gnunet.org/encoding
25 * @author Krista Bennett
26 * @author Christian Grothoff
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_fs_service.h"
37 #define DEBUG_PUBLISH GNUNET_NO
41 * Fill in all of the generic fields for
42 * a publish event and call the callback.
44 * @param pi structure to fill in
45 * @param pc overall publishing context
46 * @param p file information for the file being published
47 * @param offset where in the file are we so far
48 * @return value returned from callback
51 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
52 struct GNUNET_FS_PublishContext *pc,
53 const struct GNUNET_FS_FileInformation *p,
56 pi->value.publish.pc = pc;
57 pi->value.publish.fi = p;
58 pi->value.publish.cctx = p->client_info;
59 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
60 pi->value.publish.filename = p->filename;
61 pi->value.publish.size
62 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
64 = GNUNET_TIME_calculate_eta (p->start_time,
65 offset, pi->value.publish.size);
66 pi->value.publish.completed = offset;
67 pi->value.publish.duration =
68 GNUNET_TIME_absolute_get_duration (p->start_time);
69 pi->value.publish.anonymity = p->bo.anonymity_level;
70 return pc->h->upcb (pc->h->upcb_cls, pi);
75 * Cleanup the publish context, we're done with it.
77 * @param cls struct to clean up after
78 * @param tc scheduler context
81 publish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 struct GNUNET_FS_PublishContext *pc = cls;
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up publish context (done!)\n");
90 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
93 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
94 if (pc->namespace != NULL)
96 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
99 GNUNET_free_non_null (pc->nid);
100 GNUNET_free_non_null (pc->nuid);
101 GNUNET_free_non_null (pc->serialization);
104 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
107 if (pc->client != NULL)
109 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
112 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
118 * Function called by the datastore API with
119 * the result from the PUT request.
121 * @param cls the 'struct GNUNET_FS_PublishContext'
122 * @param success GNUNET_OK on success
123 * @param msg error message (or NULL)
126 ds_put_cont (void *cls, int success, const char *msg)
128 struct GNUNET_FS_PublishContext *pc = cls;
129 struct GNUNET_FS_ProgressInfo pi;
132 if (GNUNET_SYSERR == pc->in_network_wait)
134 /* we were aborted in the meantime, finish shutdown! */
135 GNUNET_SCHEDULER_add_continuation (&publish_cleanup,
136 pc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
139 GNUNET_assert (GNUNET_YES == pc->in_network_wait);
140 pc->in_network_wait = GNUNET_NO;
141 if (GNUNET_SYSERR == success)
143 GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
144 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
145 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
146 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
147 pc->fi_pos->client_info =
148 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
149 if ((pc->fi_pos->is_directory == GNUNET_NO) &&
150 (pc->fi_pos->filename != NULL) &&
151 (pc->fi_pos->data.file.do_index == GNUNET_YES))
153 /* run unindex to clean up */
154 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
157 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
160 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
161 &GNUNET_FS_publish_main_, pc);
166 * Generate the callback that signals clients
167 * that a file (or directory) has been completely
170 * @param p the completed upload
171 * @param pc context of the publication
174 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
175 struct GNUNET_FS_PublishContext *pc)
177 struct GNUNET_FS_ProgressInfo pi;
179 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
180 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
181 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
182 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p,
190 * Generate the callback that signals clients
191 * that a file (or directory) has encountered
192 * a problem during publication.
194 * @param p the upload that had trouble
195 * @param pc context of the publication
196 * @param emsg error message
199 signal_publish_error (struct GNUNET_FS_FileInformation *p,
200 struct GNUNET_FS_PublishContext *pc, const char *emsg)
202 struct GNUNET_FS_ProgressInfo pi;
204 p->emsg = GNUNET_strdup (emsg);
205 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
206 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
207 pi.value.publish.specifics.error.message = emsg;
208 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
209 if ((p->is_directory == GNUNET_NO) &&
210 (p->filename != NULL) && (p->data.file.do_index == GNUNET_YES))
212 /* run unindex to clean up */
213 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
220 * Datastore returns from reservation cancel request.
222 * @param cls the 'struct GNUNET_FS_PublishContext'
223 * @param success success code (not used)
224 * @param msg error message (typically NULL, not used)
227 finish_release_reserve (void *cls, int success, const char *msg)
229 struct GNUNET_FS_PublishContext *pc = cls;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Releasing reserve done!\n");
235 signal_publish_completion (pc->fi, pc);
236 pc->all_done = GNUNET_YES;
237 GNUNET_FS_publish_sync_ (pc);
242 * We've finished publishing the SBlock as part of a larger upload.
243 * Check the result and complete the larger upload.
245 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
246 * @param uri URI of the published SBlock
247 * @param emsg NULL on success, otherwise error message
250 publish_sblocks_cont (void *cls,
251 const struct GNUNET_FS_Uri *uri, const char *emsg)
253 struct GNUNET_FS_PublishContext *pc = cls;
257 signal_publish_error (pc->fi, pc, emsg);
258 GNUNET_FS_publish_sync_ (pc);
261 GNUNET_assert (pc->qre == NULL);
262 if ((pc->dsh != NULL) && (pc->rid != 0))
264 pc->qre = GNUNET_DATASTORE_release_reserve (pc->dsh,
268 GNUNET_TIME_UNIT_FOREVER_REL,
269 &finish_release_reserve, pc);
273 finish_release_reserve (pc, GNUNET_OK, 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)
287 if (NULL != pc->namespace)
288 GNUNET_FS_publish_sks (pc->h,
294 &pc->fi->bo, pc->options, &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, const char *emsg)
313 struct GNUNET_FS_PublishContext *pc = cls;
314 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Error uploading KSK blocks: %s\n", emsg);
322 signal_publish_error (p, pc, emsg);
323 GNUNET_FS_file_information_sync_ (p);
324 GNUNET_FS_publish_sync_ (pc);
325 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
328 GNUNET_SCHEDULER_add_with_priority
329 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "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 (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
347 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
348 &GNUNET_FS_publish_main_, pc);
353 * Function called by the tree encoder to obtain
354 * a block of plaintext data (for the lowest level
357 * @param cls our publishing context
358 * @param offset identifies which block to get
359 * @param max (maximum) number of bytes to get; returning
360 * fewer will also cause errors
361 * @param buf where to copy the plaintext buffer
362 * @param emsg location to store an error message (on error)
363 * @return number of bytes copied to buf, 0 on error
366 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
368 struct GNUNET_FS_PublishContext *pc = cls;
369 struct GNUNET_FS_FileInformation *p;
376 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
377 dd = p->data.dir.dir_data;
378 memcpy (buf, &dd[offset], pt_size);
382 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
384 return 0; /* calling reader with pt_size==0
385 * might free buf, so don't! */
387 p->data.file.reader (p->data.file.reader_cls,
388 offset, pt_size, buf, emsg))
396 * The tree encoder has finished processing a
397 * file. Call it's finish method and deal with
400 * @param cls our publishing context
401 * @param tc scheduler's task context (not used)
404 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
406 struct GNUNET_FS_PublishContext *pc = cls;
407 struct GNUNET_FS_FileInformation *p;
408 struct GNUNET_FS_ProgressInfo pi;
413 GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
420 GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
422 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
423 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
424 pi.value.publish.specifics.error.message = p->emsg;
425 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
430 /* final progress event */
431 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
432 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
433 pi.value.publish.specifics.progress.data = NULL;
434 pi.value.publish.specifics.progress.offset = flen;
435 pi.value.publish.specifics.progress.data_len = 0;
436 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
437 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
439 /* continue with main */
440 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
443 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
444 &GNUNET_FS_publish_main_, pc);
449 * Function called asking for the current (encoded)
450 * block to be processed. After processing the
451 * client should either call "GNUNET_FS_tree_encode_next"
452 * or (on error) "GNUNET_FS_tree_encode_finish".
455 * @param chk content hash key for the block
456 * @param offset offset of the block in the file
457 * @param depth depth of the block in the file, 0 for DBLOCK
458 * @param type type of the block (IBLOCK or DBLOCK)
459 * @param block the (encrypted) block
460 * @param block_size size of block (in bytes)
463 block_proc (void *cls,
464 const struct ContentHashKey *chk,
467 enum GNUNET_BLOCK_Type type, const void *block, uint16_t block_size)
469 struct GNUNET_FS_PublishContext *pc = cls;
470 struct GNUNET_FS_FileInformation *p;
471 struct OnDemandBlock odb;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
479 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
482 GNUNET_SCHEDULER_add_with_priority
483 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
487 GNUNET_assert (GNUNET_NO == pc->in_network_wait);
488 pc->in_network_wait = GNUNET_YES;
489 if ((!p->is_directory) &&
490 (GNUNET_YES == p->data.file.do_index) &&
491 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Indexing block `%s' for offset %llu with index size %u\n",
496 GNUNET_h2s (&chk->query),
497 (unsigned long long) offset, sizeof (struct OnDemandBlock));
499 odb.offset = GNUNET_htonll (offset);
500 odb.file_id = p->data.file.file_id;
501 GNUNET_assert (pc->qre == NULL);
502 pc->qre = GNUNET_DATASTORE_put (pc->dsh,
503 (p->is_directory) ? 0 : pc->rid,
505 sizeof (struct OnDemandBlock),
507 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
508 p->bo.content_priority,
509 p->bo.anonymity_level,
510 p->bo.replication_level,
511 p->bo.expiration_time,
513 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Publishing block `%s' for offset %llu with size %u\n",
520 GNUNET_h2s (&chk->query),
521 (unsigned long long) offset, (unsigned int) block_size);
523 GNUNET_assert (pc->qre == NULL);
524 pc->qre = GNUNET_DATASTORE_put (pc->dsh,
525 (p->is_directory) ? 0 : pc->rid,
530 p->bo.content_priority,
531 p->bo.anonymity_level,
532 p->bo.replication_level,
533 p->bo.expiration_time,
535 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
541 * Function called with information about our
542 * progress in computing the tree encoding.
545 * @param offset where are we in the file
546 * @param pt_block plaintext of the currently processed block
547 * @param pt_size size of pt_block
548 * @param depth depth of the block in the tree, 0 for DBLOCK
551 progress_proc (void *cls,
553 const void *pt_block, size_t pt_size, unsigned int depth)
555 struct GNUNET_FS_PublishContext *pc = cls;
556 struct GNUNET_FS_FileInformation *p;
557 struct GNUNET_FS_ProgressInfo pi;
560 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
561 pi.value.publish.specifics.progress.data = pt_block;
562 pi.value.publish.specifics.progress.offset = offset;
563 pi.value.publish.specifics.progress.data_len = pt_size;
564 pi.value.publish.specifics.progress.depth = depth;
565 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
570 * We are uploading a file or directory; load (if necessary) the next
571 * block into memory, encrypt it and send it to the FS service. Then
572 * continue with the main task.
574 * @param pc overall upload data
577 publish_content (struct GNUNET_FS_PublishContext *pc)
579 struct GNUNET_FS_FileInformation *p;
581 struct GNUNET_FS_DirectoryBuilder *db;
582 struct GNUNET_FS_FileInformation *dirpos;
587 GNUNET_assert (p != NULL);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
595 db = GNUNET_FS_directory_builder_create (p->meta);
596 dirpos = p->data.dir.entries;
597 while (NULL != dirpos)
599 if (dirpos->is_directory)
601 raw_data = dirpos->data.dir.dir_data;
602 dirpos->data.dir.dir_data = NULL;
607 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
608 (dirpos->data.file.file_size > 0))
610 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
612 if (dirpos->data.file.file_size !=
613 dirpos->data.file.reader (dirpos->data.file.reader_cls,
615 dirpos->data.file.file_size,
618 GNUNET_free_non_null (emsg);
619 GNUNET_free (raw_data);
624 GNUNET_FS_directory_builder_add (db,
626 dirpos->meta, raw_data);
627 GNUNET_free_non_null (raw_data);
628 dirpos = dirpos->next;
630 GNUNET_free_non_null (p->data.dir.dir_data);
631 p->data.dir.dir_data = NULL;
632 p->data.dir.dir_size = 0;
633 GNUNET_FS_directory_builder_finish (db,
634 &p->data.dir.dir_size,
635 &p->data.dir.dir_data);
636 GNUNET_FS_file_information_sync_ (p);
638 size = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
642 p->te = GNUNET_FS_tree_encoder_create (pc->h,
647 &progress_proc, &encode_cont);
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
653 GNUNET_FS_tree_encoder_next (p->te);
658 * Process the response (or lack thereof) from
659 * the "fs" service to our 'start index' request.
661 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
662 * @param msg the response we got
665 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
667 struct GNUNET_FS_PublishContext *pc = cls;
668 struct GNUNET_FS_FileInformation *p;
672 GNUNET_CLIENT_disconnect (pc->client, GNUNET_NO);
677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
679 ("Can not index file `%s': %s. Will try to insert instead.\n"),
681 _("timeout on index-start request to `fs' service"));
682 p->data.file.do_index = GNUNET_NO;
683 GNUNET_FS_file_information_sync_ (p);
684 publish_content (pc);
687 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
689 msize = ntohs (msg->size);
690 emsg = (const char *) &msg[1];
691 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
692 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
693 emsg = gettext_noop ("unknown error");
694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
696 ("Can not index file `%s': %s. Will try to insert instead.\n"),
697 p->filename, gettext (emsg));
698 p->data.file.do_index = GNUNET_NO;
699 GNUNET_FS_file_information_sync_ (p);
700 publish_content (pc);
703 p->data.file.index_start_confirmed = GNUNET_YES;
704 /* success! continue with indexing */
705 GNUNET_FS_file_information_sync_ (p);
706 publish_content (pc);
711 * Function called once the hash computation over an
712 * indexed file has completed.
714 * @param cls closure, our publishing context
715 * @param res resulting hash, NULL on error
718 hash_for_index_cb (void *cls, const GNUNET_HashCode * res)
720 struct GNUNET_FS_PublishContext *pc = cls;
721 struct GNUNET_FS_FileInformation *p;
722 struct IndexStartMessage *ism;
724 struct GNUNET_CLIENT_Connection *client;
733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
735 ("Can not index file `%s': %s. Will try to insert instead.\n"),
736 p->filename, _("failed to compute hash"));
737 p->data.file.do_index = GNUNET_NO;
738 GNUNET_FS_file_information_sync_ (p);
739 publish_content (pc);
742 if (GNUNET_YES == p->data.file.index_start_confirmed)
744 publish_content (pc);
747 fn = GNUNET_STRINGS_filename_expand (p->filename);
748 GNUNET_assert (fn != NULL);
749 slen = strlen (fn) + 1;
751 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
755 ("Can not index file `%s': %s. Will try to insert instead.\n"),
756 fn, _("filename too long"));
758 p->data.file.do_index = GNUNET_NO;
759 GNUNET_FS_file_information_sync_ (p);
760 publish_content (pc);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Hash of indexed file `%s' is `%s'\n",
766 p->filename, GNUNET_h2s (res));
768 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
770 p->data.file.file_id = *res;
771 p->data.file.have_hash = GNUNET_YES;
772 p->data.file.index_start_confirmed = GNUNET_YES;
773 GNUNET_FS_file_information_sync_ (p);
774 publish_content (pc);
778 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
783 ("Can not index file `%s': %s. Will try to insert instead.\n"),
784 p->filename, _("could not connect to `fs' service"));
785 p->data.file.do_index = GNUNET_NO;
786 publish_content (pc);
790 if (p->data.file.have_hash != GNUNET_YES)
792 p->data.file.file_id = *res;
793 p->data.file.have_hash = GNUNET_YES;
794 GNUNET_FS_file_information_sync_ (p);
796 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
797 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
798 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
799 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
801 ism->device = GNUNET_htonll (dev);
802 ism->inode = GNUNET_htonll (ino);
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 _("Failed to get file identifiers for `%s'\n"), p->filename);
812 memcpy (&ism[1], fn, slen);
815 GNUNET_break (GNUNET_YES ==
816 GNUNET_CLIENT_transmit_and_get_response (client,
818 GNUNET_TIME_UNIT_FOREVER_REL,
820 &process_index_start_response,
827 * Main function that performs the upload.
829 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
830 * @param tc task context
833 GNUNET_FS_publish_main_ (void *cls,
834 const struct GNUNET_SCHEDULER_TaskContext *tc)
836 struct GNUNET_FS_PublishContext *pc = cls;
837 struct GNUNET_FS_ProgressInfo pi;
838 struct GNUNET_FS_FileInformation *p;
839 struct GNUNET_FS_Uri *loc;
842 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Publishing complete, now publishing SKS and KSK blocks.\n");
850 /* upload of entire hierarchy complete,
851 * publish namespace entries */
852 GNUNET_FS_publish_sync_ (pc);
856 /* find starting position */
857 while ((p->is_directory) &&
858 (NULL != p->data.dir.entries) &&
859 (NULL == p->emsg) && (NULL == p->data.dir.entries->chk_uri))
861 p = p->data.dir.entries;
863 GNUNET_FS_publish_sync_ (pc);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
871 /* error with current file, abort all
872 * related files as well! */
873 while (NULL != p->dir)
875 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
876 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
880 GNUNET_asprintf (&p->emsg,
881 _("Recursive upload failed at `%s': %s"), fn, p->emsg);
886 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
888 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
889 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
890 pi.value.publish.specifics.error.message = p->emsg;
891 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
893 pc->all_done = GNUNET_YES;
894 GNUNET_FS_publish_sync_ (pc);
897 /* handle completion */
898 if (NULL != p->chk_uri)
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "File upload complete, now publishing KSK blocks.\n");
904 if (0 == p->bo.anonymity_level)
906 /* zero anonymity, box CHK URI in LOC URI */
907 loc = GNUNET_FS_uri_loc_create (p->chk_uri,
908 pc->h->cfg, p->bo.expiration_time);
909 GNUNET_FS_uri_destroy (p->chk_uri);
912 GNUNET_FS_publish_sync_ (pc);
913 /* upload of "p" complete, publish KBlocks! */
914 if (p->keywords != NULL)
916 GNUNET_FS_publish_ksk (pc->h,
920 &p->bo, pc->options, &publish_kblocks_cont, pc);
924 publish_kblocks_cont (pc, p->chk_uri, NULL);
928 if ((!p->is_directory) && (p->data.file.do_index))
930 if (NULL == p->filename)
932 p->data.file.do_index = GNUNET_NO;
933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
935 ("Can not index file `%s': %s. Will try to insert instead.\n"),
936 "<no-name>", _("needs to be an actual file"));
937 GNUNET_FS_file_information_sync_ (p);
938 publish_content (pc);
941 if (p->data.file.have_hash)
943 hash_for_index_cb (pc, &p->data.file.file_id);
947 p->start_time = GNUNET_TIME_absolute_get ();
948 pc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
951 &hash_for_index_cb, pc);
955 publish_content (pc);
960 * Signal the FS's progress function that we are starting
963 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
964 * @param fi the entry in the publish-structure
965 * @param length length of the file or directory
966 * @param meta metadata for the file or directory (can be modified)
967 * @param uri pointer to the keywords that will be used for this entry (can be modified)
968 * @param bo block options
969 * @param do_index should we index?
970 * @param client_info pointer to client context set upon creation (can be modified)
971 * @return GNUNET_OK to continue (always)
974 fip_signal_start (void *cls,
975 struct GNUNET_FS_FileInformation *fi,
977 struct GNUNET_CONTAINER_MetaData *meta,
978 struct GNUNET_FS_Uri **uri,
979 struct GNUNET_FS_BlockOptions *bo,
980 int *do_index, void **client_info)
982 struct GNUNET_FS_PublishContext *pc = cls;
983 struct GNUNET_FS_ProgressInfo pi;
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
992 /* space for on-demand blocks */
994 ((length + DBLOCK_SIZE -
995 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
999 /* space for DBlocks */
1000 pc->reserve_space += length;
1002 /* entries for IBlocks and DBlocks, space for IBlocks */
1006 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1007 pc->reserve_entries += left;
1010 left = left * sizeof (struct ContentHashKey);
1011 pc->reserve_space += left;
1013 pc->reserve_entries++;
1014 /* entries and space for keywords */
1017 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1018 pc->reserve_entries += kc;
1019 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1021 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1022 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1023 GNUNET_FS_file_information_sync_ (fi);
1029 * Signal the FS's progress function that we are suspending
1032 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1033 * @param fi the entry in the publish-structure
1034 * @param length length of the file or directory
1035 * @param meta metadata for the file or directory (can be modified)
1036 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1037 * @param bo block options
1038 * @param do_index should we index?
1039 * @param client_info pointer to client context set upon creation (can be modified)
1040 * @return GNUNET_OK to continue (always)
1043 fip_signal_suspend (void *cls,
1044 struct GNUNET_FS_FileInformation *fi,
1046 struct GNUNET_CONTAINER_MetaData *meta,
1047 struct GNUNET_FS_Uri **uri,
1048 struct GNUNET_FS_BlockOptions *bo,
1049 int *do_index, void **client_info)
1051 struct GNUNET_FS_PublishContext *pc = cls;
1052 struct GNUNET_FS_ProgressInfo pi;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
1058 GNUNET_free_non_null (fi->serialization);
1059 fi->serialization = NULL;
1060 off = (fi->chk_uri == NULL) ? 0 : length;
1061 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1062 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1063 *client_info = NULL;
1064 if (NULL != pc->qre)
1066 GNUNET_DATASTORE_cancel (pc->qre);
1069 if (NULL != pc->dsh)
1071 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1080 * Create SUSPEND event for the given publish operation
1081 * and then clean up our state (without stop signal).
1083 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1086 GNUNET_FS_publish_signal_suspend_ (void *cls)
1088 struct GNUNET_FS_PublishContext *pc = cls;
1090 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1092 GNUNET_SCHEDULER_cancel (pc->upload_task);
1093 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1095 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1096 GNUNET_FS_end_top (pc->h, pc->top);
1098 publish_cleanup (pc, NULL);
1103 * We have gotten a reply for our space reservation request.
1104 * Either fail (insufficient space) or start publishing for good.
1106 * @param cls the 'struct GNUNET_FS_PublishContext*'
1107 * @param success positive reservation ID on success
1108 * @param msg error message on error, otherwise NULL
1111 finish_reserve (void *cls, int success, const char *msg)
1113 struct GNUNET_FS_PublishContext *pc = cls;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1119 if ((msg != NULL) || (success <= 0))
1121 GNUNET_asprintf (&pc->fi->emsg,
1122 _("Insufficient space for publishing: %s"), msg);
1123 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1127 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1130 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1131 &GNUNET_FS_publish_main_, pc);
1136 * Publish a file or directory.
1138 * @param h handle to the file sharing subsystem
1139 * @param fi information about the file or directory structure to publish
1140 * @param namespace namespace to publish the file in, NULL for no namespace
1141 * @param nid identifier to use for the publishd content in the namespace
1142 * (can be NULL, must be NULL if namespace is NULL)
1143 * @param nuid update-identifier that will be used for future updates
1144 * (can be NULL, must be NULL if namespace or nid is NULL)
1145 * @param options options for the publication
1146 * @return context that can be used to control the publish operation
1148 struct GNUNET_FS_PublishContext *
1149 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1150 struct GNUNET_FS_FileInformation *fi,
1151 struct GNUNET_FS_Namespace *namespace,
1154 enum GNUNET_FS_PublishOptions options)
1156 struct GNUNET_FS_PublishContext *ret;
1157 struct GNUNET_DATASTORE_Handle *dsh;
1159 GNUNET_assert (NULL != h);
1160 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1162 dsh = GNUNET_DATASTORE_connect (h->cfg);
1170 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1174 ret->namespace = namespace;
1175 ret->options = options;
1176 if (namespace != NULL)
1179 GNUNET_assert (NULL != nid);
1180 ret->nid = GNUNET_strdup (nid);
1182 ret->nuid = GNUNET_strdup (nuid);
1185 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1186 ret->fi_pos = ret->fi;
1187 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1188 GNUNET_FS_publish_sync_ (ret);
1189 if (NULL != ret->dsh)
1191 GNUNET_assert (NULL == ret->qre);
1192 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1194 ("Reserving space for %u entries and %llu bytes for publication\n"),
1195 (unsigned int) ret->reserve_entries,
1196 (unsigned long long) ret->reserve_space);
1198 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1199 ret->reserve_entries, UINT_MAX, UINT_MAX,
1200 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1205 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1208 GNUNET_SCHEDULER_add_with_priority
1209 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1216 * Signal the FS's progress function that we are stopping
1219 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1220 * @param fi the entry in the publish-structure
1221 * @param length length of the file or directory
1222 * @param meta metadata for the file or directory (can be modified)
1223 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1224 * @param bo block options (can be modified)
1225 * @param do_index should we index?
1226 * @param client_info pointer to client context set upon creation (can be modified)
1227 * @return GNUNET_OK to continue (always)
1230 fip_signal_stop (void *cls,
1231 struct GNUNET_FS_FileInformation *fi,
1233 struct GNUNET_CONTAINER_MetaData *meta,
1234 struct GNUNET_FS_Uri **uri,
1235 struct GNUNET_FS_BlockOptions *bo,
1236 int *do_index, void **client_info)
1238 struct GNUNET_FS_PublishContext *pc = cls;
1239 struct GNUNET_FS_ProgressInfo pi;
1242 if (fi->serialization != NULL)
1244 GNUNET_FS_remove_sync_file_ (pc->h,
1245 GNUNET_FS_SYNC_PATH_FILE_INFO,
1247 GNUNET_free (fi->serialization);
1248 fi->serialization = NULL;
1250 off = (fi->chk_uri == NULL) ? 0 : length;
1251 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1252 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1253 *client_info = NULL;
1259 * Stop an upload. Will abort incomplete uploads (but
1260 * not remove blocks that have already been publishd) or
1261 * simply clean up the state for completed uploads.
1262 * Must NOT be called from within the event callback!
1264 * @param pc context for the upload to stop
1267 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1272 GNUNET_FS_end_top (pc->h, pc->top);
1273 if (NULL != pc->qre)
1275 GNUNET_DATASTORE_cancel (pc->qre);
1278 if (NULL != pc->dsh)
1280 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1283 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1285 GNUNET_SCHEDULER_cancel (pc->upload_task);
1286 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1288 if (pc->serialization != NULL)
1290 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1292 GNUNET_free (pc->serialization);
1293 pc->serialization = NULL;
1295 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1296 if (GNUNET_YES == pc->in_network_wait)
1298 pc->in_network_wait = GNUNET_SYSERR;
1301 publish_cleanup (pc, NULL);
1306 * Context for the KSK publication.
1308 struct PublishKskContext
1314 struct GNUNET_FS_Uri *ksk_uri;
1317 * Global FS context.
1319 struct GNUNET_FS_Handle *h;
1322 * The master block that we are sending
1323 * (in plaintext), has "mdsize+slen" more
1324 * bytes than the struct would suggest.
1329 * Buffer of the same size as "kb" for
1330 * the encrypted version.
1335 * Handle to the datastore, NULL if we are just
1338 struct GNUNET_DATASTORE_Handle *dsh;
1341 * Handle to datastore PUT request.
1343 struct GNUNET_DATASTORE_QueueEntry *qre;
1346 * Function to call once we're done.
1348 GNUNET_FS_PublishContinuation cont;
1356 * When should the KBlocks expire?
1358 struct GNUNET_FS_BlockOptions bo;
1361 * Size of the serialized metadata.
1366 * Size of the (CHK) URI as a string.
1371 * Keyword that we are currently processing.
1379 * Continuation of "GNUNET_FS_publish_ksk" that performs
1380 * the actual publishing operation (iterating over all
1383 * @param cls closure of type "struct PublishKskContext*"
1387 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1391 * Function called by the datastore API with
1392 * the result from the PUT request.
1394 * @param cls closure of type "struct PublishKskContext*"
1395 * @param success GNUNET_OK on success
1396 * @param msg error message (or NULL)
1399 kb_put_cont (void *cls, int success, const char *msg)
1401 struct PublishKskContext *pkc = cls;
1404 if (GNUNET_OK != success)
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KB PUT operation complete\n");
1409 if (NULL != pkc->dsh)
1411 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1414 GNUNET_free (pkc->cpy);
1415 GNUNET_free (pkc->kb);
1416 pkc->cont (pkc->cont_cls, NULL, msg);
1417 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1421 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1422 pkc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1427 * Continuation of "GNUNET_FS_publish_ksk" that performs the actual
1428 * publishing operation (iterating over all of the keywords).
1430 * @param cls closure of type "struct PublishKskContext*"
1434 publish_ksk_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1436 struct PublishKskContext *pkc = cls;
1437 const char *keyword;
1438 GNUNET_HashCode key;
1439 GNUNET_HashCode query;
1440 struct GNUNET_CRYPTO_AesSessionKey skey;
1441 struct GNUNET_CRYPTO_AesInitializationVector iv;
1442 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1445 if ((pkc->i == pkc->ksk_uri->data.ksk.keywordCount) || (NULL == pkc->dsh))
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "KSK PUT operation complete\n");
1450 if (NULL != pkc->dsh)
1452 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1455 GNUNET_free (pkc->cpy);
1456 GNUNET_free (pkc->kb);
1457 pkc->cont (pkc->cont_cls, pkc->ksk_uri, NULL);
1458 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1462 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 "Publishing under keyword `%s'\n", keyword);
1467 /* first character of keyword indicates if it is
1468 * mandatory or not -- ignore for hashing */
1469 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1470 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1471 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1472 pkc->slen + pkc->mdsize, &skey, &iv, &pkc->cpy[1]);
1473 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1474 GNUNET_assert (NULL != pk);
1475 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1476 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1477 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1479 GNUNET_assert (GNUNET_OK ==
1480 GNUNET_CRYPTO_rsa_sign (pk,
1482 &pkc->cpy->signature));
1483 GNUNET_CRYPTO_rsa_key_free (pk);
1484 pkc->qre = GNUNET_DATASTORE_put (pkc->dsh,
1488 sizeof (struct KBlock) +
1491 GNUNET_BLOCK_TYPE_FS_KBLOCK,
1492 pkc->bo.content_priority,
1493 pkc->bo.anonymity_level,
1494 pkc->bo.replication_level,
1495 pkc->bo.expiration_time,
1497 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1503 * Publish a CHK under various keywords on GNUnet.
1505 * @param h handle to the file sharing subsystem
1506 * @param ksk_uri keywords to use
1507 * @param meta metadata to use
1508 * @param uri URI to refer to in the KBlock
1509 * @param bo per-block options
1510 * @param options publication options
1511 * @param cont continuation
1512 * @param cont_cls closure for cont
1515 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1516 const struct GNUNET_FS_Uri *ksk_uri,
1517 const struct GNUNET_CONTAINER_MetaData *meta,
1518 const struct GNUNET_FS_Uri *uri,
1519 const struct GNUNET_FS_BlockOptions *bo,
1520 enum GNUNET_FS_PublishOptions options,
1521 GNUNET_FS_PublishContinuation cont, void *cont_cls)
1523 struct PublishKskContext *pkc;
1529 GNUNET_assert (NULL != uri);
1530 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1534 pkc->cont_cls = cont_cls;
1535 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1537 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1538 if (pkc->dsh == NULL)
1540 cont (cont_cls, NULL, _("Could not connect to datastore."));
1548 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
1549 GNUNET_assert (pkc->mdsize >= 0);
1550 uris = GNUNET_FS_uri_to_string (uri);
1551 pkc->slen = strlen (uris) + 1;
1552 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1553 if (size > MAX_KBLOCK_SIZE)
1555 size = MAX_KBLOCK_SIZE;
1556 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1558 pkc->kb = GNUNET_malloc (size);
1559 kbe = (char *) &pkc->kb[1];
1560 memcpy (kbe, uris, pkc->slen);
1562 sptr = &kbe[pkc->slen];
1564 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1567 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1568 if (pkc->mdsize == -1)
1571 GNUNET_free (pkc->kb);
1572 if (pkc->dsh != NULL)
1574 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1577 cont (cont_cls, NULL, _("Internal error."));
1581 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1583 pkc->cpy = GNUNET_malloc (size);
1584 pkc->cpy->purpose.size =
1585 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1586 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1587 pkc->mdsize + pkc->slen);
1588 pkc->cpy->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1589 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1590 GNUNET_SCHEDULER_add_continuation (&publish_ksk_cont,
1591 pkc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1595 /* end of fs_publish.c */