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"
39 * Fill in all of the generic fields for
40 * a publish event and call the callback.
42 * @param pi structure to fill in
43 * @param pc overall publishing context
44 * @param p file information for the file being published
45 * @param offset where in the file are we so far
46 * @return value returned from callback
49 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
50 struct GNUNET_FS_PublishContext *pc,
51 const struct GNUNET_FS_FileInformation *p,
54 pi->value.publish.pc = pc;
55 pi->value.publish.fi = p;
56 pi->value.publish.cctx = p->client_info;
57 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
58 pi->value.publish.filename = p->filename;
59 pi->value.publish.size =
60 (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
61 pi->value.publish.eta =
62 GNUNET_TIME_calculate_eta (p->start_time, offset, pi->value.publish.size);
63 pi->value.publish.completed = offset;
64 pi->value.publish.duration =
65 GNUNET_TIME_absolute_get_duration (p->start_time);
66 pi->value.publish.anonymity = p->bo.anonymity_level;
68 return pc->h->upcb (pc->h->upcb_cls, pi);
73 * Cleanup the publish context, we're done with it.
75 * @param pc struct to clean up
78 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
80 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "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 (pc->client != NULL)
97 GNUNET_CLIENT_disconnect (pc->client);
100 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == 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, int success,
116 struct GNUNET_TIME_Absolute min_expiration,
119 struct GNUNET_FS_PublishContext *pc = cls;
120 struct GNUNET_FS_ProgressInfo pi;
123 if (GNUNET_SYSERR == success)
125 GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
126 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
127 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
128 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
129 pc->fi_pos->client_info =
130 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
131 if ((pc->fi_pos->is_directory != GNUNET_YES) &&
132 (pc->fi_pos->filename != NULL) &&
133 (pc->fi_pos->data.file.do_index == GNUNET_YES))
135 /* run unindex to clean up */
136 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
139 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
141 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
142 &GNUNET_FS_publish_main_, pc);
147 * Generate the callback that signals clients
148 * that a file (or directory) has been completely
151 * @param p the completed upload
152 * @param pc context of the publication
155 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
156 struct GNUNET_FS_PublishContext *pc)
158 struct GNUNET_FS_ProgressInfo pi;
160 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
161 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
162 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
164 GNUNET_FS_publish_make_status_ (&pi, pc, p,
165 GNUNET_ntohll (p->chk_uri->data.
171 * Generate the callback that signals clients
172 * that a file (or directory) has encountered
173 * a problem during publication.
175 * @param p the upload that had trouble
176 * @param pc context of the publication
177 * @param emsg error message
180 signal_publish_error (struct GNUNET_FS_FileInformation *p,
181 struct GNUNET_FS_PublishContext *pc, const char *emsg)
183 struct GNUNET_FS_ProgressInfo pi;
185 p->emsg = GNUNET_strdup (emsg);
186 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
187 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
188 pi.value.publish.specifics.error.message = emsg;
189 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
190 if ((p->is_directory != GNUNET_YES) && (p->filename != NULL) &&
191 (p->data.file.do_index == GNUNET_YES))
193 /* run unindex to clean up */
194 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
201 * Datastore returns from reservation cancel request.
203 * @param cls the 'struct GNUNET_FS_PublishContext'
204 * @param success success code (not used)
205 * @param min_expiration minimum expiration time required for content to be stored
206 * @param msg error message (typically NULL, not used)
209 finish_release_reserve (void *cls, int success,
210 struct GNUNET_TIME_Absolute min_expiration,
213 struct GNUNET_FS_PublishContext *pc = cls;
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Releasing reserve done!\n");
217 signal_publish_completion (pc->fi, pc);
218 pc->all_done = GNUNET_YES;
219 GNUNET_FS_publish_sync_ (pc);
224 * We've finished publishing the SBlock as part of a larger upload.
225 * Check the result and complete the larger upload.
227 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
228 * @param uri URI of the published SBlock
229 * @param emsg NULL on success, otherwise error message
232 publish_sblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
235 struct GNUNET_FS_PublishContext *pc = cls;
240 signal_publish_error (pc->fi, pc, emsg);
241 GNUNET_FS_publish_sync_ (pc);
244 GNUNET_assert (pc->qre == NULL);
245 if ((pc->dsh != NULL) && (pc->rid != 0))
248 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
249 GNUNET_TIME_UNIT_FOREVER_REL,
250 &finish_release_reserve, pc);
254 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
260 * We are almost done publishing the structure,
261 * add SBlocks (if needed).
263 * @param pc overall upload data
266 publish_sblock (struct GNUNET_FS_PublishContext *pc)
269 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
277 &publish_sblocks_cont, pc);
279 publish_sblocks_cont (pc, NULL, NULL);
284 * We've finished publishing a KBlock as part of a larger upload.
285 * Check the result and continue the larger upload.
287 * @param cls the "struct GNUNET_FS_PublishContext*"
288 * of the larger upload
289 * @param uri URI of the published blocks
290 * @param emsg NULL on success, otherwise error message
293 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
296 struct GNUNET_FS_PublishContext *pc = cls;
297 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading KSK blocks: %s\n",
304 signal_publish_error (p, pc, emsg);
305 GNUNET_FS_file_information_sync_ (p);
306 GNUNET_FS_publish_sync_ (pc);
307 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
309 GNUNET_SCHEDULER_add_with_priority
310 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "KSK blocks published, moving on to next file\n");
316 signal_publish_completion (p, pc);
317 /* move on to next file */
319 pc->fi_pos = p->next;
322 GNUNET_FS_publish_sync_ (pc);
323 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
325 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
326 &GNUNET_FS_publish_main_, pc);
331 * Function called by the tree encoder to obtain
332 * a block of plaintext data (for the lowest level
335 * @param cls our publishing context
336 * @param offset identifies which block to get
337 * @param max (maximum) number of bytes to get; returning
338 * fewer will also cause errors
339 * @param buf where to copy the plaintext buffer
340 * @param emsg location to store an error message (on error)
341 * @return number of bytes copied to buf, 0 on error
344 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
346 struct GNUNET_FS_PublishContext *pc = cls;
347 struct GNUNET_FS_FileInformation *p;
352 if (p->is_directory == GNUNET_YES)
354 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
355 dd = p->data.dir.dir_data;
356 memcpy (buf, &dd[offset], pt_size);
360 if (UINT64_MAX == offset)
361 return p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
362 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
364 return 0; /* calling reader with pt_size==0
365 * might free buf, so don't! */
367 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
376 * The tree encoder has finished processing a
377 * file. Call it's finish method and deal with
380 * @param cls our publishing context
381 * @param tc scheduler's task context (not used)
384 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
386 struct GNUNET_FS_PublishContext *pc = cls;
387 struct GNUNET_FS_FileInformation *p;
388 struct GNUNET_FS_ProgressInfo pi;
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
394 GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
399 GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
401 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
402 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
403 pi.value.publish.specifics.error.message = p->emsg;
404 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
408 /* final progress event */
409 GNUNET_assert (NULL != p->chk_uri);
410 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
411 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
412 pi.value.publish.specifics.progress.data = NULL;
413 pi.value.publish.specifics.progress.offset = flen;
414 pi.value.publish.specifics.progress.data_len = 0;
415 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
416 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
418 GNUNET_FS_file_information_sync_ (p);
419 /* continue with main */
420 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
422 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
423 &GNUNET_FS_publish_main_, pc);
428 * Function called asking for the current (encoded)
429 * block to be processed. After processing the
430 * client should either call "GNUNET_FS_tree_encode_next"
431 * or (on error) "GNUNET_FS_tree_encode_finish".
434 * @param chk content hash key for the block
435 * @param offset offset of the block in the file
436 * @param depth depth of the block in the file, 0 for DBLOCK
437 * @param type type of the block (IBLOCK or DBLOCK)
438 * @param block the (encrypted) block
439 * @param block_size size of block (in bytes)
442 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
443 unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
446 struct GNUNET_FS_PublishContext *pc = cls;
447 struct GNUNET_FS_FileInformation *p;
448 struct OnDemandBlock odb;
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
454 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
456 GNUNET_SCHEDULER_add_with_priority
457 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
461 if ((p->is_directory != GNUNET_YES) && (GNUNET_YES == p->data.file.do_index) &&
462 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Indexing block `%s' for offset %llu with index size %u\n",
466 GNUNET_h2s (&chk->query), (unsigned long long) offset,
467 sizeof (struct OnDemandBlock));
468 odb.offset = GNUNET_htonll (offset);
469 odb.file_id = p->data.file.file_id;
470 GNUNET_assert (pc->qre == NULL);
472 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
473 &chk->query, sizeof (struct OnDemandBlock), &odb,
474 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
475 p->bo.content_priority, p->bo.anonymity_level,
476 p->bo.replication_level, p->bo.expiration_time,
477 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Publishing block `%s' for offset %llu with size %u\n",
483 GNUNET_h2s (&chk->query), (unsigned long long) offset,
484 (unsigned int) block_size);
485 GNUNET_assert (pc->qre == NULL);
487 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
488 &chk->query, block_size, block, type,
489 p->bo.content_priority, p->bo.anonymity_level,
490 p->bo.replication_level, p->bo.expiration_time, -2,
491 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
497 * Function called with information about our
498 * progress in computing the tree encoding.
501 * @param offset where are we in the file
502 * @param pt_block plaintext of the currently processed block
503 * @param pt_size size of pt_block
504 * @param depth depth of the block in the tree, 0 for DBLOCK
507 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
510 struct GNUNET_FS_PublishContext *pc = cls;
511 struct GNUNET_FS_FileInformation *p;
512 struct GNUNET_FS_ProgressInfo pi;
515 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
516 pi.value.publish.specifics.progress.data = pt_block;
517 pi.value.publish.specifics.progress.offset = offset;
518 pi.value.publish.specifics.progress.data_len = pt_size;
519 pi.value.publish.specifics.progress.depth = depth;
520 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
525 * We are uploading a file or directory; load (if necessary) the next
526 * block into memory, encrypt it and send it to the FS service. Then
527 * continue with the main task.
529 * @param pc overall upload data
532 publish_content (struct GNUNET_FS_PublishContext *pc)
534 struct GNUNET_FS_FileInformation *p;
536 struct GNUNET_FS_DirectoryBuilder *db;
537 struct GNUNET_FS_FileInformation *dirpos;
542 GNUNET_assert (p != NULL);
545 if (p->is_directory == GNUNET_YES)
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
548 db = GNUNET_FS_directory_builder_create (p->meta);
549 dirpos = p->data.dir.entries;
550 while (NULL != dirpos)
552 if (dirpos->is_directory == GNUNET_YES)
554 raw_data = dirpos->data.dir.dir_data;
555 dirpos->data.dir.dir_data = NULL;
560 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
561 (dirpos->data.file.file_size > 0))
563 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
565 if (dirpos->data.file.file_size !=
566 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
567 dirpos->data.file.file_size, raw_data,
570 GNUNET_free_non_null (emsg);
571 GNUNET_free (raw_data);
574 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
577 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
579 GNUNET_free_non_null (raw_data);
580 dirpos = dirpos->next;
582 GNUNET_free_non_null (p->data.dir.dir_data);
583 p->data.dir.dir_data = NULL;
584 p->data.dir.dir_size = 0;
585 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
586 &p->data.dir.dir_data);
587 GNUNET_FS_file_information_sync_ (p);
589 size = (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
592 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
593 &block_proc, &progress_proc,
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
598 GNUNET_FS_tree_encoder_next (p->te);
603 * Process the response (or lack thereof) from
604 * the "fs" service to our 'start index' request.
606 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
607 * @param msg the response we got
610 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
612 struct GNUNET_FS_PublishContext *pc = cls;
613 struct GNUNET_FS_FileInformation *p;
617 GNUNET_CLIENT_disconnect (pc->client);
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
624 ("Can not index file `%s': %s. Will try to insert instead.\n"),
626 _("timeout on index-start request to `fs' service"));
627 p->data.file.do_index = GNUNET_NO;
628 GNUNET_FS_file_information_sync_ (p);
629 publish_content (pc);
632 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
634 msize = ntohs (msg->size);
635 emsg = (const char *) &msg[1];
636 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
637 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
638 emsg = gettext_noop ("unknown error");
639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
641 ("Can not index file `%s': %s. Will try to insert instead.\n"),
642 p->filename, gettext (emsg));
643 p->data.file.do_index = GNUNET_NO;
644 GNUNET_FS_file_information_sync_ (p);
645 publish_content (pc);
648 p->data.file.index_start_confirmed = GNUNET_YES;
649 /* success! continue with indexing */
650 GNUNET_FS_file_information_sync_ (p);
651 publish_content (pc);
656 * Function called once the hash computation over an
657 * indexed file has completed.
659 * @param cls closure, our publishing context
660 * @param res resulting hash, NULL on error
663 hash_for_index_cb (void *cls, const struct GNUNET_HashCode * res)
665 struct GNUNET_FS_PublishContext *pc = cls;
666 struct GNUNET_FS_FileInformation *p;
667 struct IndexStartMessage *ism;
669 struct GNUNET_CLIENT_Connection *client;
678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680 ("Can not index file `%s': %s. Will try to insert instead.\n"),
681 p->filename, _("failed to compute hash"));
682 p->data.file.do_index = GNUNET_NO;
683 GNUNET_FS_file_information_sync_ (p);
684 publish_content (pc);
687 if (GNUNET_YES == p->data.file.index_start_confirmed)
689 publish_content (pc);
692 fn = GNUNET_STRINGS_filename_expand (p->filename);
693 GNUNET_assert (fn != NULL);
694 slen = strlen (fn) + 1;
696 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 ("Can not index file `%s': %s. Will try to insert instead.\n"),
701 fn, _("filename too long"));
703 p->data.file.do_index = GNUNET_NO;
704 GNUNET_FS_file_information_sync_ (p);
705 publish_content (pc);
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
709 p->filename, GNUNET_h2s (res));
710 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
712 p->data.file.file_id = *res;
713 p->data.file.have_hash = GNUNET_YES;
714 p->data.file.index_start_confirmed = GNUNET_YES;
715 GNUNET_FS_file_information_sync_ (p);
716 publish_content (pc);
720 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725 ("Can not index file `%s': %s. Will try to insert instead.\n"),
726 p->filename, _("could not connect to `fs' service"));
727 p->data.file.do_index = GNUNET_NO;
728 publish_content (pc);
732 if (p->data.file.have_hash != GNUNET_YES)
734 p->data.file.file_id = *res;
735 p->data.file.have_hash = GNUNET_YES;
736 GNUNET_FS_file_information_sync_ (p);
738 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
739 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
740 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
741 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
743 ism->device = GNUNET_htonll (dev);
744 ism->inode = GNUNET_htonll (ino);
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 _("Failed to get file identifiers for `%s'\n"), p->filename);
752 memcpy (&ism[1], fn, slen);
755 GNUNET_break (GNUNET_YES ==
756 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
757 GNUNET_TIME_UNIT_FOREVER_REL,
759 &process_index_start_response,
766 * Main function that performs the upload.
768 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
769 * @param tc task context
772 GNUNET_FS_publish_main_ (void *cls,
773 const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct GNUNET_FS_PublishContext *pc = cls;
776 struct GNUNET_FS_ProgressInfo pi;
777 struct GNUNET_FS_FileInformation *p;
778 struct GNUNET_FS_Uri *loc;
781 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Publishing complete, now publishing SKS and KSK blocks.\n");
787 /* upload of entire hierarchy complete,
788 * publish namespace entries */
789 GNUNET_FS_publish_sync_ (pc);
793 /* find starting position */
794 while ((p->is_directory == GNUNET_YES) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
795 && (NULL == p->data.dir.entries->chk_uri))
797 p = p->data.dir.entries;
799 GNUNET_FS_publish_sync_ (pc);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
805 /* error with current file, abort all
806 * related files as well! */
807 while (NULL != p->dir)
809 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
810 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
814 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
820 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
822 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
823 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
824 pi.value.publish.specifics.error.message = p->emsg;
825 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
827 pc->all_done = GNUNET_YES;
828 GNUNET_FS_publish_sync_ (pc);
831 /* handle completion */
832 if (NULL != p->chk_uri)
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "File upload complete, now publishing KSK blocks.\n");
836 if (0 == p->bo.anonymity_level)
838 /* zero anonymity, box CHK URI in LOC URI */
840 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
841 p->bo.expiration_time);
842 GNUNET_FS_uri_destroy (p->chk_uri);
845 GNUNET_FS_publish_sync_ (pc);
846 /* upload of "p" complete, publish KBlocks! */
847 if (p->keywords != NULL)
849 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
850 pc->options, &publish_kblocks_cont, pc);
854 publish_kblocks_cont (pc, p->chk_uri, NULL);
858 if ((p->is_directory != GNUNET_YES) && (p->data.file.do_index))
860 if (NULL == p->filename)
862 p->data.file.do_index = GNUNET_NO;
863 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
865 ("Can not index file `%s': %s. Will try to insert instead.\n"),
866 "<no-name>", _("needs to be an actual file"));
867 GNUNET_FS_file_information_sync_ (p);
868 publish_content (pc);
871 if (p->data.file.have_hash)
873 hash_for_index_cb (pc, &p->data.file.file_id);
877 p->start_time = GNUNET_TIME_absolute_get ();
879 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
880 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
884 publish_content (pc);
889 * Signal the FS's progress function that we are starting
892 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
893 * @param fi the entry in the publish-structure
894 * @param length length of the file or directory
895 * @param meta metadata for the file or directory (can be modified)
896 * @param uri pointer to the keywords that will be used for this entry (can be modified)
897 * @param bo block options
898 * @param do_index should we index?
899 * @param client_info pointer to client context set upon creation (can be modified)
900 * @return GNUNET_OK to continue (always)
903 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
904 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
905 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
906 int *do_index, void **client_info)
908 struct GNUNET_FS_PublishContext *pc = cls;
909 struct GNUNET_FS_ProgressInfo pi;
913 if (GNUNET_YES == pc->skip_next_fi_callback)
915 pc->skip_next_fi_callback = GNUNET_NO;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
921 /* space for on-demand blocks */
923 ((length + DBLOCK_SIZE -
924 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
928 /* space for DBlocks */
929 pc->reserve_space += length;
931 /* entries for IBlocks and DBlocks, space for IBlocks */
935 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
936 pc->reserve_entries += left;
939 left = left * sizeof (struct ContentHashKey);
940 pc->reserve_space += left;
942 pc->reserve_entries++;
943 /* entries and space for keywords */
946 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
947 pc->reserve_entries += kc;
948 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
950 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
951 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
952 GNUNET_FS_file_information_sync_ (fi);
953 if ((fi->is_directory) && (fi->dir != NULL))
955 /* We are a directory, and we are not top-level; process entries in directory */
956 pc->skip_next_fi_callback = GNUNET_YES;
957 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
964 * Actually signal the FS's progress function that we are suspending
967 * @param fi the entry in the publish-structure
968 * @param pc the publish context of which a file is being suspended
971 suspend_operation (struct GNUNET_FS_FileInformation *fi,
972 struct GNUNET_FS_PublishContext *pc)
974 struct GNUNET_FS_ProgressInfo pi;
977 if (NULL != pc->ksk_pc)
979 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
982 if (NULL != pc->sks_pc)
984 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
988 GNUNET_free_non_null (fi->serialization);
989 fi->serialization = NULL;
990 off = (fi->chk_uri == NULL) ? 0 : (fi->is_directory == GNUNET_YES) ? fi->data.dir.dir_size : fi->data.file.file_size;
991 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
992 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
995 GNUNET_DATASTORE_cancel (pc->qre);
1000 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1008 * Signal the FS's progress function that we are suspending
1009 * an upload. Performs the recursion.
1011 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1012 * @param fi the entry in the publish-structure
1013 * @param length length of the file or directory
1014 * @param meta metadata for the file or directory (can be modified)
1015 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1016 * @param bo block options
1017 * @param do_index should we index?
1018 * @param client_info pointer to client context set upon creation (can be modified)
1019 * @return GNUNET_OK to continue (always)
1022 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
1023 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1024 struct GNUNET_FS_Uri **uri,
1025 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1028 struct GNUNET_FS_PublishContext *pc = cls;
1030 if (GNUNET_YES == pc->skip_next_fi_callback)
1032 pc->skip_next_fi_callback = GNUNET_NO;
1035 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1037 /* process entries in directory */
1038 pc->skip_next_fi_callback = GNUNET_YES;
1039 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1041 suspend_operation (fi, pc);
1042 *client_info = NULL;
1048 * Create SUSPEND event for the given publish operation
1049 * and then clean up our state (without stop signal).
1051 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1054 GNUNET_FS_publish_signal_suspend_ (void *cls)
1056 struct GNUNET_FS_PublishContext *pc = cls;
1058 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1060 GNUNET_SCHEDULER_cancel (pc->upload_task);
1061 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1063 pc->skip_next_fi_callback = GNUNET_YES;
1064 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1065 suspend_operation (pc->fi, pc);
1066 GNUNET_FS_end_top (pc->h, pc->top);
1068 publish_cleanup (pc);
1073 * We have gotten a reply for our space reservation request.
1074 * Either fail (insufficient space) or start publishing for good.
1076 * @param cls the 'struct GNUNET_FS_PublishContext*'
1077 * @param success positive reservation ID on success
1078 * @param min_expiration minimum expiration time required for content to be stored
1079 * @param msg error message on error, otherwise NULL
1082 finish_reserve (void *cls, int success,
1083 struct GNUNET_TIME_Absolute min_expiration,
1086 struct GNUNET_FS_PublishContext *pc = cls;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1090 if ((msg != NULL) || (success <= 0))
1092 GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1094 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1098 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1100 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1101 &GNUNET_FS_publish_main_, pc);
1106 * Publish a file or directory.
1108 * @param h handle to the file sharing subsystem
1109 * @param fi information about the file or directory structure to publish
1110 * @param ns namespace to publish the file in, NULL for no namespace
1111 * @param nid identifier to use for the publishd content in the namespace
1112 * (can be NULL, must be NULL if namespace is NULL)
1113 * @param nuid update-identifier that will be used for future updates
1114 * (can be NULL, must be NULL if namespace or nid is NULL)
1115 * @param options options for the publication
1116 * @return context that can be used to control the publish operation
1118 struct GNUNET_FS_PublishContext *
1119 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1120 struct GNUNET_FS_FileInformation *fi,
1121 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
1124 enum GNUNET_FS_PublishOptions options)
1126 struct GNUNET_FS_PublishContext *ret;
1127 struct GNUNET_DATASTORE_Handle *dsh;
1129 GNUNET_assert (NULL != h);
1130 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1132 dsh = GNUNET_DATASTORE_connect (h->cfg);
1140 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1146 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1148 GNUNET_assert (NULL != nid);
1149 ret->nid = GNUNET_strdup (nid);
1151 ret->nuid = GNUNET_strdup (nuid);
1153 ret->options = options;
1155 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1156 ret->fi_pos = ret->fi;
1157 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1158 GNUNET_FS_publish_sync_ (ret);
1159 if (NULL != ret->dsh)
1161 GNUNET_assert (NULL == ret->qre);
1162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 ("Reserving space for %u entries and %llu bytes for publication\n"),
1165 (unsigned int) ret->reserve_entries,
1166 (unsigned long long) ret->reserve_space);
1168 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1169 ret->reserve_entries, UINT_MAX, UINT_MAX,
1170 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1175 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1177 GNUNET_SCHEDULER_add_with_priority
1178 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1185 * Signal the FS's progress function that we are stopping
1188 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1189 * @param fi the entry in the publish-structure
1190 * @param length length of the file or directory
1191 * @param meta metadata for the file or directory (can be modified)
1192 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1193 * @param bo block options (can be modified)
1194 * @param do_index should we index?
1195 * @param client_info pointer to client context set upon creation (can be modified)
1196 * @return GNUNET_OK to continue (always)
1199 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1200 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1201 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1202 int *do_index, void **client_info)
1204 struct GNUNET_FS_PublishContext *pc = cls;
1205 struct GNUNET_FS_ProgressInfo pi;
1208 if (GNUNET_YES == pc->skip_next_fi_callback)
1210 pc->skip_next_fi_callback = GNUNET_NO;
1213 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1215 /* process entries in directory first */
1216 pc->skip_next_fi_callback = GNUNET_YES;
1217 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1219 if (fi->serialization != NULL)
1221 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1223 GNUNET_free (fi->serialization);
1224 fi->serialization = NULL;
1226 off = (fi->chk_uri == NULL) ? 0 : length;
1227 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1228 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1229 *client_info = NULL;
1235 * Stop an upload. Will abort incomplete uploads (but
1236 * not remove blocks that have already been publishd) or
1237 * simply clean up the state for completed uploads.
1238 * Must NOT be called from within the event callback!
1240 * @param pc context for the upload to stop
1243 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1245 struct GNUNET_FS_ProgressInfo pi;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1249 GNUNET_FS_end_top (pc->h, pc->top);
1250 if (NULL != pc->ksk_pc)
1252 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1255 if (NULL != pc->sks_pc)
1257 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1260 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1262 GNUNET_SCHEDULER_cancel (pc->upload_task);
1263 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1265 pc->skip_next_fi_callback = GNUNET_YES;
1266 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1268 if (pc->fi->serialization != NULL)
1270 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1271 pc->fi->serialization);
1272 GNUNET_free (pc->fi->serialization);
1273 pc->fi->serialization = NULL;
1275 off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1277 if (pc->serialization != NULL)
1279 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1281 GNUNET_free (pc->serialization);
1282 pc->serialization = NULL;
1284 if (NULL != pc->qre)
1286 GNUNET_DATASTORE_cancel (pc->qre);
1289 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1290 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1291 publish_cleanup (pc);
1296 /* end of fs_publish.c */