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,
217 "Releasing reserve done!\n");
218 signal_publish_completion (pc->fi, pc);
219 pc->all_done = GNUNET_YES;
220 GNUNET_FS_publish_sync_ (pc);
225 * We've finished publishing the SBlock as part of a larger upload.
226 * Check the result and complete the larger upload.
228 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
229 * @param uri URI of the published SBlock
230 * @param emsg NULL on success, otherwise error message
233 publish_sblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
236 struct GNUNET_FS_PublishContext *pc = cls;
241 signal_publish_error (pc->fi, pc, emsg);
242 GNUNET_FS_publish_sync_ (pc);
245 GNUNET_assert (pc->qre == NULL);
246 if ((pc->dsh != NULL) && (pc->rid != 0))
249 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
250 GNUNET_TIME_UNIT_FOREVER_REL,
251 &finish_release_reserve, pc);
255 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
261 * We are almost done publishing the structure,
262 * add SBlocks (if needed).
264 * @param pc overall upload data
267 publish_sblock (struct GNUNET_FS_PublishContext *pc)
270 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
278 &publish_sblocks_cont, pc);
280 publish_sblocks_cont (pc, NULL, NULL);
285 * We've finished publishing a KBlock as part of a larger upload.
286 * Check the result and continue the larger upload.
288 * @param cls the `struct GNUNET_FS_PublishContext *`
289 * of the larger upload
290 * @param uri URI of the published blocks
291 * @param emsg NULL on success, otherwise error message
294 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
297 struct GNUNET_FS_PublishContext *pc = cls;
298 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Error uploading KSK blocks: %s\n",
306 signal_publish_error (p, pc, emsg);
307 GNUNET_FS_file_information_sync_ (p);
308 GNUNET_FS_publish_sync_ (pc);
309 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
311 GNUNET_SCHEDULER_add_with_priority
312 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "KSK blocks published, moving on to next file\n");
318 signal_publish_completion (p, pc);
319 /* move on to next file */
321 pc->fi_pos = p->next;
324 GNUNET_FS_publish_sync_ (pc);
325 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
327 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
328 &GNUNET_FS_publish_main_, pc);
333 * Function called by the tree encoder to obtain
334 * a block of plaintext data (for the lowest level
337 * @param cls our publishing context
338 * @param offset identifies which block to get
339 * @param max (maximum) number of bytes to get; returning
340 * fewer will also cause errors
341 * @param buf where to copy the plaintext buffer
342 * @param emsg location to store an error message (on error)
343 * @return number of bytes copied to buf, 0 on error
346 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
348 struct GNUNET_FS_PublishContext *pc = cls;
349 struct GNUNET_FS_FileInformation *p;
354 if (GNUNET_YES == p->is_directory)
356 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
357 dd = p->data.dir.dir_data;
358 memcpy (buf, &dd[offset], pt_size);
362 if (UINT64_MAX == offset)
364 if (&GNUNET_FS_data_reader_file_ == p->data.file.reader)
366 /* force closing the file to avoid keeping too many files open */
367 p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
371 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
373 return 0; /* calling reader with pt_size==0
374 * might free buf, so don't! */
376 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
385 * The tree encoder has finished processing a
386 * file. Call it's finish method and deal with
389 * @param cls our publishing context
390 * @param tc scheduler's task context (not used)
393 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
395 struct GNUNET_FS_PublishContext *pc = cls;
396 struct GNUNET_FS_FileInformation *p;
397 struct GNUNET_FS_ProgressInfo pi;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Finished with tree encoder\n");
404 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
405 GNUNET_FS_file_information_sync_ (p);
406 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Error during tree walk: %s\n",
413 GNUNET_asprintf (&p->emsg,
414 _("Publishing failed: %s"),
417 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
418 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
419 pi.value.publish.specifics.error.message = p->emsg;
420 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
424 /* final progress event */
425 GNUNET_assert (NULL != p->chk_uri);
426 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
427 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
428 pi.value.publish.specifics.progress.data = NULL;
429 pi.value.publish.specifics.progress.offset = flen;
430 pi.value.publish.specifics.progress.data_len = 0;
431 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
432 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
434 /* continue with main */ /* continue with main */
435 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
437 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
438 &GNUNET_FS_publish_main_, pc);
443 * Function called asking for the current (encoded)
444 * block to be processed. After processing the
445 * client should either call #GNUNET_FS_tree_encoder_next
446 * or (on error) #GNUNET_FS_tree_encoder_finish.
449 * @param chk content hash key for the block
450 * @param offset offset of the block in the file
451 * @param depth depth of the block in the file, 0 for DBLOCK
452 * @param type type of the block (IBLOCK or DBLOCK)
453 * @param block the (encrypted) block
454 * @param block_size size of @a block (in bytes)
457 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
458 unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
461 struct GNUNET_FS_PublishContext *pc = cls;
462 struct GNUNET_FS_FileInformation *p;
463 struct OnDemandBlock odb;
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
469 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
471 GNUNET_SCHEDULER_add_with_priority
472 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
476 if ( (GNUNET_YES != p->is_directory) &&
477 (GNUNET_YES == p->data.file.do_index) &&
478 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) )
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Indexing block `%s' for offset %llu with index size %u\n",
482 GNUNET_h2s (&chk->query), (unsigned long long) offset,
483 sizeof (struct OnDemandBlock));
484 odb.offset = GNUNET_htonll (offset);
485 odb.file_id = p->data.file.file_id;
486 GNUNET_assert (pc->qre == NULL);
488 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
489 &chk->query, sizeof (struct OnDemandBlock), &odb,
490 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
491 p->bo.content_priority, p->bo.anonymity_level,
492 p->bo.replication_level, p->bo.expiration_time,
493 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Publishing block `%s' for offset %llu with size %u\n",
499 GNUNET_h2s (&chk->query), (unsigned long long) offset,
500 (unsigned int) block_size);
501 GNUNET_assert (pc->qre == NULL);
503 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
504 &chk->query, block_size, block, type,
505 p->bo.content_priority, p->bo.anonymity_level,
506 p->bo.replication_level, p->bo.expiration_time, -2,
507 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
513 * Function called with information about our
514 * progress in computing the tree encoding.
517 * @param offset where are we in the file
518 * @param pt_block plaintext of the currently processed block
519 * @param pt_size size of @a pt_block
520 * @param depth depth of the block in the tree, 0 for DBLOCK
523 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
526 struct GNUNET_FS_PublishContext *pc = cls;
527 struct GNUNET_FS_FileInformation *p;
528 struct GNUNET_FS_ProgressInfo pi;
531 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
532 pi.value.publish.specifics.progress.data = pt_block;
533 pi.value.publish.specifics.progress.offset = offset;
534 pi.value.publish.specifics.progress.data_len = pt_size;
535 pi.value.publish.specifics.progress.depth = depth;
536 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
541 * We are uploading a file or directory; load (if necessary) the next
542 * block into memory, encrypt it and send it to the FS service. Then
543 * continue with the main task.
545 * @param pc overall upload data
548 publish_content (struct GNUNET_FS_PublishContext *pc)
550 struct GNUNET_FS_FileInformation *p;
552 struct GNUNET_FS_DirectoryBuilder *db;
553 struct GNUNET_FS_FileInformation *dirpos;
558 GNUNET_assert (NULL != p);
561 if (GNUNET_YES == p->is_directory)
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
564 db = GNUNET_FS_directory_builder_create (p->meta);
565 dirpos = p->data.dir.entries;
566 while (NULL != dirpos)
568 if (GNUNET_YES == dirpos->is_directory)
570 raw_data = dirpos->data.dir.dir_data;
571 dirpos->data.dir.dir_data = NULL;
576 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
577 (dirpos->data.file.file_size > 0))
579 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
581 if (dirpos->data.file.file_size !=
582 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
583 dirpos->data.file.file_size, raw_data,
586 GNUNET_free_non_null (emsg);
587 GNUNET_free (raw_data);
590 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
593 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
595 GNUNET_free_non_null (raw_data);
596 dirpos = dirpos->next;
598 GNUNET_free_non_null (p->data.dir.dir_data);
599 p->data.dir.dir_data = NULL;
600 p->data.dir.dir_size = 0;
601 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
602 &p->data.dir.dir_data);
603 GNUNET_FS_file_information_sync_ (p);
605 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Creating tree encoder\n");
609 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
610 &block_proc, &progress_proc,
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Processing next block from tree\n");
616 GNUNET_FS_tree_encoder_next (p->te);
621 * Process the response (or lack thereof) from
622 * the "fs" service to our 'start index' request.
624 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
625 * @param msg the response we got
628 process_index_start_response (void *cls,
629 const struct GNUNET_MessageHeader *msg)
631 struct GNUNET_FS_PublishContext *pc = cls;
632 struct GNUNET_FS_FileInformation *p;
636 GNUNET_CLIENT_disconnect (pc->client);
641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
642 _("Can not index file `%s': %s. Will try to insert instead.\n"),
644 _("timeout on index-start request to `fs' service"));
645 p->data.file.do_index = GNUNET_NO;
646 GNUNET_FS_file_information_sync_ (p);
647 publish_content (pc);
650 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
652 msize = ntohs (msg->size);
653 emsg = (const char *) &msg[1];
654 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
655 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
656 emsg = gettext_noop ("unknown error");
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 ("Can not index file `%s': %s. Will try to insert instead.\n"),
660 p->filename, gettext (emsg));
661 p->data.file.do_index = GNUNET_NO;
662 GNUNET_FS_file_information_sync_ (p);
663 publish_content (pc);
666 p->data.file.index_start_confirmed = GNUNET_YES;
667 /* success! continue with indexing */
668 GNUNET_FS_file_information_sync_ (p);
669 publish_content (pc);
674 * Function called once the hash computation over an
675 * indexed file has completed.
677 * @param cls closure, our publishing context
678 * @param res resulting hash, NULL on error
681 hash_for_index_cb (void *cls,
682 const struct GNUNET_HashCode *res)
684 struct GNUNET_FS_PublishContext *pc = cls;
685 struct GNUNET_FS_FileInformation *p;
686 struct IndexStartMessage *ism;
688 struct GNUNET_CLIENT_Connection *client;
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698 _("Can not index file `%s': %s. Will try to insert instead.\n"),
700 _("failed to compute hash"));
701 p->data.file.do_index = GNUNET_NO;
702 GNUNET_FS_file_information_sync_ (p);
703 publish_content (pc);
706 if (GNUNET_YES == p->data.file.index_start_confirmed)
708 publish_content (pc);
711 fn = GNUNET_STRINGS_filename_expand (p->filename);
712 GNUNET_assert (fn != NULL);
713 slen = strlen (fn) + 1;
715 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
717 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
719 ("Can not index file `%s': %s. Will try to insert instead.\n"),
720 fn, _("filename too long"));
722 p->data.file.do_index = GNUNET_NO;
723 GNUNET_FS_file_information_sync_ (p);
724 publish_content (pc);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
728 p->filename, GNUNET_h2s (res));
729 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
731 p->data.file.file_id = *res;
732 p->data.file.have_hash = GNUNET_YES;
733 p->data.file.index_start_confirmed = GNUNET_YES;
734 GNUNET_FS_file_information_sync_ (p);
735 publish_content (pc);
739 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
742 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
743 _("Can not index file `%s': %s. Will try to insert instead.\n"),
745 _("could not connect to `fs' service"));
746 p->data.file.do_index = GNUNET_NO;
747 publish_content (pc);
751 if (p->data.file.have_hash != GNUNET_YES)
753 p->data.file.file_id = *res;
754 p->data.file.have_hash = GNUNET_YES;
755 GNUNET_FS_file_information_sync_ (p);
757 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
758 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
759 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
760 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
762 ism->device = GNUNET_htonll (dev);
763 ism->inode = GNUNET_htonll (ino);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 _("Failed to get file identifiers for `%s'\n"), p->filename);
771 memcpy (&ism[1], fn, slen);
774 GNUNET_break (GNUNET_YES ==
775 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
776 GNUNET_TIME_UNIT_FOREVER_REL,
778 &process_index_start_response,
785 * Main function that performs the upload.
787 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
788 * @param tc task context
791 GNUNET_FS_publish_main_ (void *cls,
792 const struct GNUNET_SCHEDULER_TaskContext *tc)
794 struct GNUNET_FS_PublishContext *pc = cls;
795 struct GNUNET_FS_ProgressInfo pi;
796 struct GNUNET_FS_FileInformation *p;
797 struct GNUNET_FS_Uri *loc;
800 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Publishing complete, now publishing SKS and KSK blocks.\n");
806 /* upload of entire hierarchy complete,
807 * publish namespace entries */
808 GNUNET_FS_publish_sync_ (pc);
812 /* find starting position */
813 while ( (GNUNET_YES == p->is_directory) &&
814 (NULL != p->data.dir.entries) &&
816 (NULL == p->data.dir.entries->chk_uri) )
818 p = p->data.dir.entries;
820 GNUNET_FS_publish_sync_ (pc);
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Error uploading: %s\n",
828 /* error with current file, abort all
829 * related files as well! */
830 while (NULL != p->dir)
832 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
833 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
837 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
843 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
845 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
846 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
847 pi.value.publish.specifics.error.message = p->emsg;
848 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
850 pc->all_done = GNUNET_YES;
851 GNUNET_FS_publish_sync_ (pc);
854 /* handle completion */
855 if (NULL != p->chk_uri)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "File upload complete, now publishing KSK blocks.\n");
859 if (0 == p->bo.anonymity_level)
861 /* zero anonymity, box CHK URI in LOC URI */
863 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
864 p->bo.expiration_time);
865 GNUNET_FS_uri_destroy (p->chk_uri);
867 GNUNET_FS_file_information_sync_ (p);
869 GNUNET_FS_publish_sync_ (pc);
870 /* upload of "p" complete, publish KBlocks! */
871 if (NULL != p->keywords)
873 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
874 pc->options, &publish_kblocks_cont, pc);
878 publish_kblocks_cont (pc, p->chk_uri, NULL);
882 if ((GNUNET_YES != p->is_directory) && (p->data.file.do_index))
884 if (NULL == p->filename)
886 p->data.file.do_index = GNUNET_NO;
887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
889 ("Can not index file `%s': %s. Will try to insert instead.\n"),
890 "<no-name>", _("needs to be an actual file"));
891 GNUNET_FS_file_information_sync_ (p);
892 publish_content (pc);
895 if (p->data.file.have_hash)
897 hash_for_index_cb (pc, &p->data.file.file_id);
901 p->start_time = GNUNET_TIME_absolute_get ();
903 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
904 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
908 publish_content (pc);
913 * Signal the FS's progress function that we are starting
916 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
917 * @param fi the entry in the publish-structure
918 * @param length length of the file or directory
919 * @param meta metadata for the file or directory (can be modified)
920 * @param uri pointer to the keywords that will be used for this entry (can be modified)
921 * @param bo block options
922 * @param do_index should we index?
923 * @param client_info pointer to client context set upon creation (can be modified)
924 * @return #GNUNET_OK to continue (always)
927 fip_signal_start (void *cls,
928 struct GNUNET_FS_FileInformation *fi,
930 struct GNUNET_CONTAINER_MetaData *meta,
931 struct GNUNET_FS_Uri **uri,
932 struct GNUNET_FS_BlockOptions *bo,
936 struct GNUNET_FS_PublishContext *pc = cls;
937 struct GNUNET_FS_ProgressInfo pi;
941 if (GNUNET_YES == pc->skip_next_fi_callback)
943 pc->skip_next_fi_callback = GNUNET_NO;
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Starting publish operation\n");
950 /* space for on-demand blocks */
952 ((length + DBLOCK_SIZE -
953 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
957 /* space for DBlocks */
958 pc->reserve_space += length;
960 /* entries for IBlocks and DBlocks, space for IBlocks */
964 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
965 pc->reserve_entries += left;
968 left = left * sizeof (struct ContentHashKey);
969 pc->reserve_space += left;
971 pc->reserve_entries++;
972 /* entries and space for keywords */
975 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
976 pc->reserve_entries += kc;
977 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
979 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
980 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
981 GNUNET_FS_file_information_sync_ (fi);
982 if ((fi->is_directory) && (fi->dir != NULL))
984 /* We are a directory, and we are not top-level; process entries in directory */
985 pc->skip_next_fi_callback = GNUNET_YES;
986 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
993 * Actually signal the FS's progress function that we are suspending
996 * @param fi the entry in the publish-structure
997 * @param pc the publish context of which a file is being suspended
1000 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1001 struct GNUNET_FS_PublishContext *pc)
1003 struct GNUNET_FS_ProgressInfo pi;
1006 if (NULL != pc->ksk_pc)
1008 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1011 if (NULL != pc->sks_pc)
1013 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "Suspending publish operation\n");
1018 GNUNET_free_non_null (fi->serialization);
1019 fi->serialization = NULL;
1020 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ? fi->data.dir.dir_size : fi->data.file.file_size;
1021 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1022 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1023 if (NULL != pc->qre)
1025 GNUNET_DATASTORE_cancel (pc->qre);
1028 if (NULL != pc->dsh)
1030 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1038 * Signal the FS's progress function that we are suspending
1039 * an upload. Performs the recursion.
1041 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1042 * @param fi the entry in the publish-structure
1043 * @param length length of the file or directory
1044 * @param meta metadata for the file or directory (can be modified)
1045 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1046 * @param bo block options
1047 * @param do_index should we index?
1048 * @param client_info pointer to client context set upon creation (can be modified)
1049 * @return #GNUNET_OK to continue (always)
1052 fip_signal_suspend (void *cls,
1053 struct GNUNET_FS_FileInformation *fi,
1055 struct GNUNET_CONTAINER_MetaData *meta,
1056 struct GNUNET_FS_Uri **uri,
1057 struct GNUNET_FS_BlockOptions *bo,
1061 struct GNUNET_FS_PublishContext *pc = cls;
1063 if (GNUNET_YES == pc->skip_next_fi_callback)
1065 pc->skip_next_fi_callback = GNUNET_NO;
1068 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1070 /* process entries in directory */
1071 pc->skip_next_fi_callback = GNUNET_YES;
1072 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1074 suspend_operation (fi, pc);
1075 *client_info = NULL;
1081 * Create SUSPEND event for the given publish operation
1082 * and then clean up our state (without stop signal).
1084 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1087 GNUNET_FS_publish_signal_suspend_ (void *cls)
1089 struct GNUNET_FS_PublishContext *pc = cls;
1091 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1093 GNUNET_SCHEDULER_cancel (pc->upload_task);
1094 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1096 pc->skip_next_fi_callback = GNUNET_YES;
1097 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1098 suspend_operation (pc->fi, pc);
1099 GNUNET_FS_end_top (pc->h, pc->top);
1101 publish_cleanup (pc);
1106 * We have gotten a reply for our space reservation request.
1107 * Either fail (insufficient space) or start publishing for good.
1109 * @param cls the `struct GNUNET_FS_PublishContext *`
1110 * @param success positive reservation ID on success
1111 * @param min_expiration minimum expiration time required for content to be stored
1112 * @param msg error message on error, otherwise NULL
1115 finish_reserve (void *cls, int success,
1116 struct GNUNET_TIME_Absolute min_expiration,
1119 struct GNUNET_FS_PublishContext *pc = cls;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1123 if ((msg != NULL) || (success <= 0))
1125 GNUNET_asprintf (&pc->fi->emsg,
1126 _("Insufficient space for publishing: %s"),
1128 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1132 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1134 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1135 &GNUNET_FS_publish_main_, pc);
1140 * Publish a file or directory.
1142 * @param h handle to the file sharing subsystem
1143 * @param fi information about the file or directory structure to publish
1144 * @param ns namespace to publish the file in, NULL for no namespace
1145 * @param nid identifier to use for the publishd content in the namespace
1146 * (can be NULL, must be NULL if namespace is NULL)
1147 * @param nuid update-identifier that will be used for future updates
1148 * (can be NULL, must be NULL if namespace or nid is NULL)
1149 * @param options options for the publication
1150 * @return context that can be used to control the publish operation
1152 struct GNUNET_FS_PublishContext *
1153 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1154 struct GNUNET_FS_FileInformation *fi,
1155 const struct GNUNET_CRYPTO_EccPrivateKey *ns,
1158 enum GNUNET_FS_PublishOptions options)
1160 struct GNUNET_FS_PublishContext *ret;
1161 struct GNUNET_DATASTORE_Handle *dsh;
1163 GNUNET_assert (NULL != h);
1164 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1166 dsh = GNUNET_DATASTORE_connect (h->cfg);
1174 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1180 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1182 GNUNET_assert (NULL != nid);
1183 ret->nid = GNUNET_strdup (nid);
1185 ret->nuid = GNUNET_strdup (nuid);
1187 ret->options = options;
1189 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1190 ret->fi_pos = ret->fi;
1191 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1192 GNUNET_FS_publish_sync_ (ret);
1193 if (NULL != ret->dsh)
1195 GNUNET_assert (NULL == ret->qre);
1196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1198 ("Reserving space for %u entries and %llu bytes for publication\n"),
1199 (unsigned int) ret->reserve_entries,
1200 (unsigned long long) ret->reserve_space);
1202 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1203 ret->reserve_entries, UINT_MAX, UINT_MAX,
1204 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1209 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1211 GNUNET_SCHEDULER_add_with_priority
1212 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1219 * Signal the FS's progress function that we are stopping
1222 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1223 * @param fi the entry in the publish-structure
1224 * @param length length of the file or directory
1225 * @param meta metadata for the file or directory (can be modified)
1226 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1227 * @param bo block options (can be modified)
1228 * @param do_index should we index?
1229 * @param client_info pointer to client context set upon creation (can be modified)
1230 * @return #GNUNET_OK to continue (always)
1233 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1234 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1235 struct GNUNET_FS_Uri **uri, 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 (GNUNET_YES == pc->skip_next_fi_callback)
1244 pc->skip_next_fi_callback = GNUNET_NO;
1247 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1249 /* process entries in directory first */
1250 pc->skip_next_fi_callback = GNUNET_YES;
1251 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1253 if (fi->serialization != NULL)
1255 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1257 GNUNET_free (fi->serialization);
1258 fi->serialization = NULL;
1260 off = (fi->chk_uri == NULL) ? 0 : length;
1261 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1262 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1263 *client_info = NULL;
1269 * Stop an upload. Will abort incomplete uploads (but
1270 * not remove blocks that have already been publishd) or
1271 * simply clean up the state for completed uploads.
1272 * Must NOT be called from within the event callback!
1274 * @param pc context for the upload to stop
1277 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1279 struct GNUNET_FS_ProgressInfo pi;
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1283 GNUNET_FS_end_top (pc->h, pc->top);
1284 if (NULL != pc->ksk_pc)
1286 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1289 if (NULL != pc->sks_pc)
1291 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1294 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1296 GNUNET_SCHEDULER_cancel (pc->upload_task);
1297 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1299 pc->skip_next_fi_callback = GNUNET_YES;
1300 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1302 if (pc->fi->serialization != NULL)
1304 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1305 pc->fi->serialization);
1306 GNUNET_free (pc->fi->serialization);
1307 pc->fi->serialization = NULL;
1309 off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1311 if (pc->serialization != NULL)
1313 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1315 GNUNET_free (pc->serialization);
1316 pc->serialization = NULL;
1318 if (NULL != pc->qre)
1320 GNUNET_DATASTORE_cancel (pc->qre);
1323 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1324 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1325 publish_cleanup (pc);
1330 /* end of fs_publish.c */