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.
21 * @file fs/fs_publish.c
22 * @brief publish a file or directory in GNUnet
23 * @see https://gnunet.org/encoding
24 * @author Krista Bennett
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fs_service.h"
37 * Fill in all of the generic fields for
38 * a publish event and call the callback.
40 * @param pi structure to fill in
41 * @param pc overall publishing context
42 * @param p file information for the file being published
43 * @param offset where in the file are we so far
44 * @return value returned from callback
47 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
48 struct GNUNET_FS_PublishContext *pc,
49 const struct GNUNET_FS_FileInformation *p,
52 pi->value.publish.pc = pc;
53 pi->value.publish.fi = p;
54 pi->value.publish.cctx = p->client_info;
55 pi->value.publish.pctx = (NULL == p->dir) ? NULL : p->dir->client_info;
56 pi->value.publish.filename = p->filename;
57 pi->value.publish.size =
58 (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
59 pi->value.publish.eta =
60 GNUNET_TIME_calculate_eta (p->start_time, offset,
61 pi->value.publish.size);
62 pi->value.publish.completed = offset;
63 pi->value.publish.duration =
64 GNUNET_TIME_absolute_get_duration (p->start_time);
65 pi->value.publish.anonymity = p->bo.anonymity_level;
67 return pc->h->upcb (pc->h->upcb_cls, pi);
72 * Cleanup the publish context, we're done with it.
74 * @param pc struct to clean up
77 publish_cleanup (struct GNUNET_FS_PublishContext *pc)
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "Cleaning up publish context (done!)\n");
83 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
86 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
87 GNUNET_free_non_null (pc->nid);
88 GNUNET_free_non_null (pc->nuid);
89 GNUNET_free_non_null (pc->serialization);
92 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
95 if (NULL != pc->client)
97 GNUNET_CLIENT_disconnect (pc->client);
100 GNUNET_assert (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,
126 _("Publishing failed: %s"),
128 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
129 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
130 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
131 pc->fi_pos->client_info =
132 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
133 if ((pc->fi_pos->is_directory != GNUNET_YES) &&
134 (pc->fi_pos->filename != NULL) &&
135 (pc->fi_pos->data.file.do_index == GNUNET_YES))
137 /* run unindex to clean up */
138 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
141 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
143 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
144 &GNUNET_FS_publish_main_, pc);
149 * Generate the callback that signals clients
150 * that a file (or directory) has been completely
153 * @param p the completed upload
154 * @param pc context of the publication
157 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
158 struct GNUNET_FS_PublishContext *pc)
160 struct GNUNET_FS_ProgressInfo pi;
162 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
163 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
164 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
165 pi.value.publish.specifics.completed.sks_uri = p->sks_uri;
167 GNUNET_FS_publish_make_status_ (&pi, pc, p,
168 p->data.file.file_size);
173 * Generate the callback that signals clients
174 * that a file (or directory) has encountered
175 * a problem during publication.
177 * @param p the upload that had trouble
178 * @param pc context of the publication
179 * @param emsg error message
182 signal_publish_error (struct GNUNET_FS_FileInformation *p,
183 struct GNUNET_FS_PublishContext *pc,
186 struct GNUNET_FS_ProgressInfo pi;
188 p->emsg = GNUNET_strdup (emsg);
189 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
190 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
191 pi.value.publish.specifics.error.message = emsg;
192 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
193 if ((p->is_directory != GNUNET_YES) && (p->filename != NULL) &&
194 (p->data.file.do_index == GNUNET_YES))
196 /* run unindex to clean up */
197 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
204 * Datastore returns from reservation cancel request.
206 * @param cls the `struct GNUNET_FS_PublishContext *`
207 * @param success success code (not used)
208 * @param min_expiration minimum expiration time required for content to be stored
209 * @param msg error message (typically NULL, not used)
212 finish_release_reserve (void *cls, int success,
213 struct GNUNET_TIME_Absolute min_expiration,
216 struct GNUNET_FS_PublishContext *pc = cls;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Releasing reserve done!\n");
221 signal_publish_completion (pc->fi, pc);
222 pc->all_done = GNUNET_YES;
223 GNUNET_FS_publish_sync_ (pc);
228 * We've finished publishing the SBlock as part of a larger upload.
229 * Check the result and complete the larger upload.
231 * @param cls the `struct GNUNET_FS_PublishContext *` of the larger upload
232 * @param uri URI of the published SBlock
233 * @param emsg NULL on success, otherwise error message
236 publish_sblocks_cont (void *cls,
237 const struct GNUNET_FS_Uri *uri,
240 struct GNUNET_FS_PublishContext *pc = cls;
245 signal_publish_error (pc->fi, pc, emsg);
246 GNUNET_FS_publish_sync_ (pc);
251 /* sks publication, remember namespace URI */
252 pc->fi->sks_uri = GNUNET_FS_uri_dup (uri);
254 GNUNET_assert (pc->qre == NULL);
255 if ((pc->dsh != NULL) && (pc->rid != 0))
258 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
259 GNUNET_TIME_UNIT_FOREVER_REL,
260 &finish_release_reserve, pc);
264 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
270 * We are almost done publishing the structure,
271 * add SBlocks (if needed).
273 * @param pc overall upload data
276 publish_sblock (struct GNUNET_FS_PublishContext *pc)
279 pc->sks_pc = GNUNET_FS_publish_sks (pc->h,
287 &publish_sblocks_cont, pc);
289 publish_sblocks_cont (pc, NULL, NULL);
294 * We've finished publishing a KBlock as part of a larger upload.
295 * Check the result and continue the larger upload.
297 * @param cls the `struct GNUNET_FS_PublishContext *`
298 * of the larger upload
299 * @param uri URI of the published blocks
300 * @param emsg NULL on success, otherwise error message
303 publish_kblocks_cont (void *cls,
304 const struct GNUNET_FS_Uri *uri,
307 struct GNUNET_FS_PublishContext *pc = cls;
308 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Error uploading KSK blocks: %s\n",
316 signal_publish_error (p, pc, emsg);
317 GNUNET_FS_file_information_sync_ (p);
318 GNUNET_FS_publish_sync_ (pc);
319 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
321 GNUNET_SCHEDULER_add_with_priority
322 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
323 &GNUNET_FS_publish_main_,
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "KSK blocks published, moving on to next file\n");
330 signal_publish_completion (p, pc);
331 /* move on to next file */
333 pc->fi_pos = p->next;
336 GNUNET_FS_publish_sync_ (pc);
337 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
339 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
340 &GNUNET_FS_publish_main_, pc);
345 * Function called by the tree encoder to obtain
346 * a block of plaintext data (for the lowest level
349 * @param cls our publishing context
350 * @param offset identifies which block to get
351 * @param max (maximum) number of bytes to get; returning
352 * fewer will also cause errors
353 * @param buf where to copy the plaintext buffer
354 * @param emsg location to store an error message (on error)
355 * @return number of bytes copied to buf, 0 on error
358 block_reader (void *cls,
364 struct GNUNET_FS_PublishContext *pc = cls;
365 struct GNUNET_FS_FileInformation *p;
370 if (GNUNET_YES == p->is_directory)
372 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
373 dd = p->data.dir.dir_data;
374 memcpy (buf, &dd[offset], pt_size);
378 if (UINT64_MAX == offset)
380 if (&GNUNET_FS_data_reader_file_ == p->data.file.reader)
382 /* force closing the file to avoid keeping too many files open */
383 p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
387 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
389 return 0; /* calling reader with pt_size==0
390 * might free buf, so don't! */
392 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
401 * The tree encoder has finished processing a
402 * file. Call it's finish method and deal with
405 * @param cls our publishing context
406 * @param tc scheduler's task context (not used)
409 encode_cont (void *cls,
410 const struct GNUNET_SCHEDULER_TaskContext *tc)
412 struct GNUNET_FS_PublishContext *pc = cls;
413 struct GNUNET_FS_FileInformation *p;
414 struct GNUNET_FS_ProgressInfo pi;
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Finished with tree encoder\n");
421 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
422 GNUNET_FS_file_information_sync_ (p);
423 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Error during tree walk: %s\n",
430 GNUNET_asprintf (&p->emsg,
431 _("Publishing failed: %s"),
434 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
435 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
436 pi.value.publish.specifics.error.message = p->emsg;
437 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
441 /* final progress event */
442 GNUNET_assert (NULL != p->chk_uri);
443 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
444 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
445 pi.value.publish.specifics.progress.data = NULL;
446 pi.value.publish.specifics.progress.offset = flen;
447 pi.value.publish.specifics.progress.data_len = 0;
448 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
449 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
451 /* continue with main */ /* continue with main */
452 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
454 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
455 &GNUNET_FS_publish_main_, pc);
460 * Function called asking for the current (encoded)
461 * block to be processed. After processing the
462 * client should either call #GNUNET_FS_tree_encoder_next
463 * or (on error) #GNUNET_FS_tree_encoder_finish.
466 * @param chk content hash key for the block
467 * @param offset offset of the block in the file
468 * @param depth depth of the block in the file, 0 for DBLOCK
469 * @param type type of the block (IBLOCK or DBLOCK)
470 * @param block the (encrypted) block
471 * @param block_size size of @a block (in bytes)
474 block_proc (void *cls,
475 const struct ContentHashKey *chk,
478 enum GNUNET_BLOCK_Type type,
482 struct GNUNET_FS_PublishContext *pc = cls;
483 struct GNUNET_FS_FileInformation *p;
484 struct OnDemandBlock odb;
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
490 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
492 GNUNET_SCHEDULER_add_with_priority
493 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
497 if ( (GNUNET_YES != p->is_directory) &&
498 (GNUNET_YES == p->data.file.do_index) &&
499 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) )
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Indexing block `%s' for offset %llu with index size %u\n",
503 GNUNET_h2s (&chk->query), (unsigned long long) offset,
504 sizeof (struct OnDemandBlock));
505 odb.offset = GNUNET_htonll (offset);
506 odb.file_id = p->data.file.file_id;
507 GNUNET_assert (pc->qre == NULL);
509 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
510 &chk->query, sizeof (struct OnDemandBlock), &odb,
511 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
512 p->bo.content_priority, p->bo.anonymity_level,
513 p->bo.replication_level, p->bo.expiration_time,
514 -2, 1, 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), (unsigned long long) offset,
521 (unsigned int) block_size);
522 GNUNET_assert (pc->qre == NULL);
524 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
525 &chk->query, block_size, block, type,
526 p->bo.content_priority, p->bo.anonymity_level,
527 p->bo.replication_level, p->bo.expiration_time, -2,
528 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
534 * Function called with information about our
535 * progress in computing the tree encoding.
538 * @param offset where are we in the file
539 * @param pt_block plaintext of the currently processed block
540 * @param pt_size size of @a pt_block
541 * @param depth depth of the block in the tree, 0 for DBLOCK
544 progress_proc (void *cls, uint64_t offset,
545 const void *pt_block,
549 struct GNUNET_FS_PublishContext *pc = cls;
550 struct GNUNET_FS_FileInformation *p;
551 struct GNUNET_FS_FileInformation *par;
552 struct GNUNET_FS_ProgressInfo pi;
555 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
556 pi.value.publish.specifics.progress.data = pt_block;
557 pi.value.publish.specifics.progress.offset = offset;
558 pi.value.publish.specifics.progress.data_len = pt_size;
559 pi.value.publish.specifics.progress.depth = depth;
560 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
562 (GNUNET_YES == p->is_directory) )
564 while (NULL != (par = p->dir))
567 GNUNET_assert (GNUNET_YES == par->is_directory);
568 p->data.dir.contents_completed += pt_size;
569 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY;
570 pi.value.publish.specifics.progress_directory.completed = p->data.dir.contents_completed;
571 pi.value.publish.specifics.progress_directory.total = p->data.dir.contents_size;
572 pi.value.publish.specifics.progress_directory.eta = GNUNET_TIME_calculate_eta (p->start_time,
573 p->data.dir.contents_completed,
574 p->data.dir.contents_size);
575 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
582 * We are uploading a file or directory; load (if necessary) the next
583 * block into memory, encrypt it and send it to the FS service. Then
584 * continue with the main task.
586 * @param pc overall upload data
589 publish_content (struct GNUNET_FS_PublishContext *pc)
591 struct GNUNET_FS_FileInformation *p;
593 struct GNUNET_FS_DirectoryBuilder *db;
594 struct GNUNET_FS_FileInformation *dirpos;
599 GNUNET_assert (NULL != p);
602 if (GNUNET_YES == p->is_directory)
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
605 db = GNUNET_FS_directory_builder_create (p->meta);
606 dirpos = p->data.dir.entries;
607 while (NULL != dirpos)
609 if (GNUNET_YES == dirpos->is_directory)
611 raw_data = dirpos->data.dir.dir_data;
612 dirpos->data.dir.dir_data = NULL;
617 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
618 (dirpos->data.file.file_size > 0))
620 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
622 if (dirpos->data.file.file_size !=
623 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
624 dirpos->data.file.file_size, raw_data,
627 GNUNET_free_non_null (emsg);
628 GNUNET_free (raw_data);
631 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
634 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
636 GNUNET_free_non_null (raw_data);
637 dirpos = dirpos->next;
639 GNUNET_free_non_null (p->data.dir.dir_data);
640 p->data.dir.dir_data = NULL;
641 p->data.dir.dir_size = 0;
642 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
643 &p->data.dir.dir_data);
644 GNUNET_FS_file_information_sync_ (p);
646 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "Creating tree encoder\n");
650 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
651 &block_proc, &progress_proc,
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Processing next block from tree\n");
657 GNUNET_FS_tree_encoder_next (p->te);
662 * Process the response (or lack thereof) from
663 * the "fs" service to our 'start index' request.
665 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
666 * @param msg the response we got
669 process_index_start_response (void *cls,
670 const struct GNUNET_MessageHeader *msg)
672 struct GNUNET_FS_PublishContext *pc = cls;
673 struct GNUNET_FS_FileInformation *p;
677 GNUNET_CLIENT_disconnect (pc->client);
682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
683 _("Can not index file `%s': %s. Will try to insert instead.\n"),
685 _("timeout on index-start request to `fs' service"));
686 p->data.file.do_index = GNUNET_NO;
687 GNUNET_FS_file_information_sync_ (p);
688 publish_content (pc);
691 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
693 msize = ntohs (msg->size);
694 emsg = (const char *) &msg[1];
695 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
696 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
697 emsg = gettext_noop ("unknown error");
698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 ("Can not index file `%s': %s. Will try to insert instead.\n"),
701 p->filename, gettext (emsg));
702 p->data.file.do_index = GNUNET_NO;
703 GNUNET_FS_file_information_sync_ (p);
704 publish_content (pc);
707 p->data.file.index_start_confirmed = GNUNET_YES;
708 /* success! continue with indexing */
709 GNUNET_FS_file_information_sync_ (p);
710 publish_content (pc);
715 * Function called once the hash computation over an
716 * indexed file has completed.
718 * @param cls closure, our publishing context
719 * @param res resulting hash, NULL on error
722 hash_for_index_cb (void *cls,
723 const struct GNUNET_HashCode *res)
725 struct GNUNET_FS_PublishContext *pc = cls;
726 struct GNUNET_FS_FileInformation *p;
727 struct IndexStartMessage *ism;
729 struct GNUNET_CLIENT_Connection *client;
738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
739 _("Can not index file `%s': %s. Will try to insert instead.\n"),
741 _("failed to compute hash"));
742 p->data.file.do_index = GNUNET_NO;
743 GNUNET_FS_file_information_sync_ (p);
744 publish_content (pc);
747 if (GNUNET_YES == p->data.file.index_start_confirmed)
749 publish_content (pc);
752 fn = GNUNET_STRINGS_filename_expand (p->filename);
753 GNUNET_assert (fn != NULL);
754 slen = strlen (fn) + 1;
756 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
760 ("Can not index file `%s': %s. Will try to insert instead.\n"),
761 fn, _("filename too long"));
763 p->data.file.do_index = GNUNET_NO;
764 GNUNET_FS_file_information_sync_ (p);
765 publish_content (pc);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
769 p->filename, GNUNET_h2s (res));
770 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
772 p->data.file.file_id = *res;
773 p->data.file.have_hash = GNUNET_YES;
774 p->data.file.index_start_confirmed = GNUNET_YES;
775 GNUNET_FS_file_information_sync_ (p);
776 publish_content (pc);
780 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 _("Can not index file `%s': %s. Will try to insert instead.\n"),
786 _("could not connect to `fs' service"));
787 p->data.file.do_index = GNUNET_NO;
788 publish_content (pc);
792 if (p->data.file.have_hash != GNUNET_YES)
794 p->data.file.file_id = *res;
795 p->data.file.have_hash = GNUNET_YES;
796 GNUNET_FS_file_information_sync_ (p);
798 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
799 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
800 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
801 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
803 ism->device = GNUNET_htonll (dev);
804 ism->inode = GNUNET_htonll (ino);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 _("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, &ism->header,
817 GNUNET_TIME_UNIT_FOREVER_REL,
819 &process_index_start_response,
826 * Main function that performs the upload.
828 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
829 * @param tc task context
832 GNUNET_FS_publish_main_ (void *cls,
833 const struct GNUNET_SCHEDULER_TaskContext *tc)
835 struct GNUNET_FS_PublishContext *pc = cls;
836 struct GNUNET_FS_ProgressInfo pi;
837 struct GNUNET_FS_FileInformation *p;
838 struct GNUNET_FS_Uri *loc;
841 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Publishing complete, now publishing SKS and KSK blocks.\n");
847 /* upload of entire hierarchy complete,
848 * publish namespace entries */
849 GNUNET_FS_publish_sync_ (pc);
853 /* find starting position */
854 while ( (GNUNET_YES == p->is_directory) &&
855 (NULL != p->data.dir.entries) &&
857 (NULL == p->data.dir.entries->chk_uri) )
859 p = p->data.dir.entries;
861 GNUNET_FS_publish_sync_ (pc);
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867 "Error uploading: %s\n",
869 /* error with current file, abort all
870 * related files as well! */
871 while (NULL != p->dir)
873 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
874 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
878 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
884 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
886 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
887 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
888 pi.value.publish.specifics.error.message = p->emsg;
889 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
891 pc->all_done = GNUNET_YES;
892 GNUNET_FS_publish_sync_ (pc);
895 /* handle completion */
896 if (NULL != p->chk_uri)
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "File upload complete, now publishing KSK blocks.\n");
900 if (0 == p->bo.anonymity_level)
902 /* zero anonymity, box CHK URI in LOC URI */
903 loc = GNUNET_FS_uri_loc_create (p->chk_uri,
905 p->bo.expiration_time);
906 GNUNET_FS_uri_destroy (p->chk_uri);
908 GNUNET_FS_file_information_sync_ (p);
910 GNUNET_FS_publish_sync_ (pc);
911 /* upload of "p" complete, publish KBlocks! */
912 if (NULL != p->keywords)
914 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h,
920 &publish_kblocks_cont, pc);
924 publish_kblocks_cont (pc, p->chk_uri, NULL);
928 if ((GNUNET_YES != 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,
934 _("Can not index file `%s': %s. Will try to insert instead.\n"),
936 _("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 ();
949 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
950 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
954 publish_content (pc);
959 * Signal the FS's progress function that we are starting
962 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
963 * @param fi the entry in the publish-structure
964 * @param length length of the file or directory
965 * @param meta metadata for the file or directory (can be modified)
966 * @param uri pointer to the keywords that will be used for this entry (can be modified)
967 * @param bo block options
968 * @param do_index should we index?
969 * @param client_info pointer to client context set upon creation (can be modified)
970 * @return #GNUNET_OK to continue (always)
973 fip_signal_start (void *cls,
974 struct GNUNET_FS_FileInformation *fi,
976 struct GNUNET_CONTAINER_MetaData *meta,
977 struct GNUNET_FS_Uri **uri,
978 struct GNUNET_FS_BlockOptions *bo,
982 struct GNUNET_FS_PublishContext *pc = cls;
983 struct GNUNET_FS_ProgressInfo pi;
987 if (GNUNET_YES == pc->skip_next_fi_callback)
989 pc->skip_next_fi_callback = GNUNET_NO;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Starting publish operation\n");
996 /* space for on-demand blocks */
998 ((length + DBLOCK_SIZE -
999 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
1003 /* space for DBlocks */
1004 pc->reserve_space += length;
1006 /* entries for IBlocks and DBlocks, space for IBlocks */
1010 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
1011 pc->reserve_entries += left;
1014 left = left * sizeof (struct ContentHashKey);
1015 pc->reserve_space += left;
1017 pc->reserve_entries++;
1018 /* entries and space for keywords */
1021 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1022 pc->reserve_entries += kc;
1023 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1025 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1026 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1027 GNUNET_FS_file_information_sync_ (fi);
1028 if ((fi->is_directory) && (fi->dir != NULL))
1030 /* We are a directory, and we are not top-level; process entries in directory */
1031 pc->skip_next_fi_callback = GNUNET_YES;
1032 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1039 * Actually signal the FS's progress function that we are suspending
1042 * @param fi the entry in the publish-structure
1043 * @param pc the publish context of which a file is being suspended
1046 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1047 struct GNUNET_FS_PublishContext *pc)
1049 struct GNUNET_FS_ProgressInfo pi;
1052 if (NULL != pc->ksk_pc)
1054 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1057 if (NULL != pc->sks_pc)
1059 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "Suspending publish operation\n");
1064 GNUNET_free_non_null (fi->serialization);
1065 fi->serialization = NULL;
1066 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ? fi->data.dir.dir_size : fi->data.file.file_size;
1067 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1068 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1069 if (NULL != pc->qre)
1071 GNUNET_DATASTORE_cancel (pc->qre);
1074 if (NULL != pc->dsh)
1076 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1084 * Signal the FS's progress function that we are suspending
1085 * an upload. Performs the recursion.
1087 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1088 * @param fi the entry in the publish-structure
1089 * @param length length of the file or directory
1090 * @param meta metadata for the file or directory (can be modified)
1091 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1092 * @param bo block options
1093 * @param do_index should we index?
1094 * @param client_info pointer to client context set upon creation (can be modified)
1095 * @return #GNUNET_OK to continue (always)
1098 fip_signal_suspend (void *cls,
1099 struct GNUNET_FS_FileInformation *fi,
1101 struct GNUNET_CONTAINER_MetaData *meta,
1102 struct GNUNET_FS_Uri **uri,
1103 struct GNUNET_FS_BlockOptions *bo,
1107 struct GNUNET_FS_PublishContext *pc = cls;
1109 if (GNUNET_YES == pc->skip_next_fi_callback)
1111 pc->skip_next_fi_callback = GNUNET_NO;
1114 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1116 /* process entries in directory */
1117 pc->skip_next_fi_callback = GNUNET_YES;
1118 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1120 suspend_operation (fi, pc);
1121 *client_info = NULL;
1127 * Create SUSPEND event for the given publish operation
1128 * and then clean up our state (without stop signal).
1130 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1133 GNUNET_FS_publish_signal_suspend_ (void *cls)
1135 struct GNUNET_FS_PublishContext *pc = cls;
1137 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1139 GNUNET_SCHEDULER_cancel (pc->upload_task);
1140 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1142 pc->skip_next_fi_callback = GNUNET_YES;
1143 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1144 suspend_operation (pc->fi, pc);
1145 GNUNET_FS_end_top (pc->h, pc->top);
1147 publish_cleanup (pc);
1152 * We have gotten a reply for our space reservation request.
1153 * Either fail (insufficient space) or start publishing for good.
1155 * @param cls the `struct GNUNET_FS_PublishContext *`
1156 * @param success positive reservation ID on success
1157 * @param min_expiration minimum expiration time required for content to be stored
1158 * @param msg error message on error, otherwise NULL
1161 finish_reserve (void *cls, int success,
1162 struct GNUNET_TIME_Absolute min_expiration,
1165 struct GNUNET_FS_PublishContext *pc = cls;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1169 if ((msg != NULL) || (success <= 0))
1171 GNUNET_asprintf (&pc->fi->emsg,
1172 _("Insufficient space for publishing: %s"),
1174 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1178 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1180 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1181 &GNUNET_FS_publish_main_, pc);
1186 * Calculate the total size of all of the files in the directory structure.
1188 * @param fi file structure to traverse
1191 compute_contents_size (struct GNUNET_FS_FileInformation *fi)
1193 struct GNUNET_FS_FileInformation *ent;
1195 if (GNUNET_YES != fi->is_directory)
1196 return fi->data.file.file_size;
1197 fi->data.dir.contents_size = 0;
1198 for (ent = fi->data.dir.entries; NULL != ent; ent = ent->next)
1199 fi->data.dir.contents_size += compute_contents_size (ent);
1200 return fi->data.dir.contents_size;
1205 * Publish a file or directory.
1207 * @param h handle to the file sharing subsystem
1208 * @param fi information about the file or directory structure to publish
1209 * @param ns namespace to publish the file in, NULL for no namespace
1210 * @param nid identifier to use for the publishd content in the namespace
1211 * (can be NULL, must be NULL if namespace is NULL)
1212 * @param nuid update-identifier that will be used for future updates
1213 * (can be NULL, must be NULL if namespace or nid is NULL)
1214 * @param options options for the publication
1215 * @return context that can be used to control the publish operation
1217 struct GNUNET_FS_PublishContext *
1218 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1219 struct GNUNET_FS_FileInformation *fi,
1220 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
1223 enum GNUNET_FS_PublishOptions options)
1225 struct GNUNET_FS_PublishContext *ret;
1226 struct GNUNET_DATASTORE_Handle *dsh;
1228 GNUNET_assert (NULL != h);
1229 compute_contents_size (fi);
1230 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1232 dsh = GNUNET_DATASTORE_connect (h->cfg);
1240 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1246 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1248 GNUNET_assert (NULL != nid);
1249 ret->nid = GNUNET_strdup (nid);
1251 ret->nuid = GNUNET_strdup (nuid);
1253 ret->options = options;
1255 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1256 ret->fi_pos = ret->fi;
1257 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1258 GNUNET_FS_publish_sync_ (ret);
1259 if (NULL != ret->dsh)
1261 GNUNET_assert (NULL == ret->qre);
1262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1263 _("Reserving space for %u entries and %llu bytes for publication\n"),
1264 (unsigned int) ret->reserve_entries,
1265 (unsigned long long) ret->reserve_space);
1267 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1268 ret->reserve_entries,
1270 GNUNET_TIME_UNIT_FOREVER_REL,
1276 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1278 GNUNET_SCHEDULER_add_with_priority
1279 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1286 * Signal the FS's progress function that we are stopping
1289 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1290 * @param fi the entry in the publish-structure
1291 * @param length length of the file or directory
1292 * @param meta metadata for the file or directory (can be modified)
1293 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1294 * @param bo block options (can be modified)
1295 * @param do_index should we index?
1296 * @param client_info pointer to client context set upon creation (can be modified)
1297 * @return #GNUNET_OK to continue (always)
1300 fip_signal_stop (void *cls,
1301 struct GNUNET_FS_FileInformation *fi,
1303 struct GNUNET_CONTAINER_MetaData *meta,
1304 struct GNUNET_FS_Uri **uri,
1305 struct GNUNET_FS_BlockOptions *bo,
1306 int *do_index, void **client_info)
1308 struct GNUNET_FS_PublishContext *pc = cls;
1309 struct GNUNET_FS_ProgressInfo pi;
1312 if (GNUNET_YES == pc->skip_next_fi_callback)
1314 pc->skip_next_fi_callback = GNUNET_NO;
1317 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1319 /* process entries in directory first */
1320 pc->skip_next_fi_callback = GNUNET_YES;
1321 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1323 if (fi->serialization != NULL)
1325 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1327 GNUNET_free (fi->serialization);
1328 fi->serialization = NULL;
1330 off = (fi->chk_uri == NULL) ? 0 : length;
1331 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1332 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1333 *client_info = NULL;
1339 * Stop an upload. Will abort incomplete uploads (but
1340 * not remove blocks that have already been publishd) or
1341 * simply clean up the state for completed uploads.
1342 * Must NOT be called from within the event callback!
1344 * @param pc context for the upload to stop
1347 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1349 struct GNUNET_FS_ProgressInfo pi;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Publish stop called\n");
1354 GNUNET_FS_end_top (pc->h, pc->top);
1355 if (NULL != pc->ksk_pc)
1357 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1360 if (NULL != pc->sks_pc)
1362 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1365 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1367 GNUNET_SCHEDULER_cancel (pc->upload_task);
1368 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1370 pc->skip_next_fi_callback = GNUNET_YES;
1371 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1373 if (NULL != pc->fi->serialization)
1375 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1376 pc->fi->serialization);
1377 GNUNET_free (pc->fi->serialization);
1378 pc->fi->serialization = NULL;
1380 off = (NULL == pc->fi->chk_uri) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1382 if (NULL != pc->serialization)
1384 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1386 GNUNET_free (pc->serialization);
1387 pc->serialization = NULL;
1389 if (NULL != pc->qre)
1391 GNUNET_DATASTORE_cancel (pc->qre);
1394 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1395 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1396 publish_cleanup (pc);
1400 /* end of fs_publish.c */