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,
394 const struct GNUNET_SCHEDULER_TaskContext *tc)
396 struct GNUNET_FS_PublishContext *pc = cls;
397 struct GNUNET_FS_FileInformation *p;
398 struct GNUNET_FS_ProgressInfo pi;
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Finished with tree encoder\n");
405 p->chk_uri = GNUNET_FS_tree_encoder_get_uri (p->te);
406 GNUNET_FS_file_information_sync_ (p);
407 GNUNET_FS_tree_encoder_finish (p->te, &emsg);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Error during tree walk: %s\n",
414 GNUNET_asprintf (&p->emsg,
415 _("Publishing failed: %s"),
418 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
419 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
420 pi.value.publish.specifics.error.message = p->emsg;
421 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
425 /* final progress event */
426 GNUNET_assert (NULL != p->chk_uri);
427 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
428 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
429 pi.value.publish.specifics.progress.data = NULL;
430 pi.value.publish.specifics.progress.offset = flen;
431 pi.value.publish.specifics.progress.data_len = 0;
432 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
433 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
435 /* continue with main */ /* continue with main */
436 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
438 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
439 &GNUNET_FS_publish_main_, pc);
444 * Function called asking for the current (encoded)
445 * block to be processed. After processing the
446 * client should either call #GNUNET_FS_tree_encoder_next
447 * or (on error) #GNUNET_FS_tree_encoder_finish.
450 * @param chk content hash key for the block
451 * @param offset offset of the block in the file
452 * @param depth depth of the block in the file, 0 for DBLOCK
453 * @param type type of the block (IBLOCK or DBLOCK)
454 * @param block the (encrypted) block
455 * @param block_size size of @a block (in bytes)
458 block_proc (void *cls,
459 const struct ContentHashKey *chk,
462 enum GNUNET_BLOCK_Type type,
466 struct GNUNET_FS_PublishContext *pc = cls;
467 struct GNUNET_FS_FileInformation *p;
468 struct OnDemandBlock odb;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
474 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
476 GNUNET_SCHEDULER_add_with_priority
477 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
481 if ( (GNUNET_YES != p->is_directory) &&
482 (GNUNET_YES == p->data.file.do_index) &&
483 (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) )
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Indexing block `%s' for offset %llu with index size %u\n",
487 GNUNET_h2s (&chk->query), (unsigned long long) offset,
488 sizeof (struct OnDemandBlock));
489 odb.offset = GNUNET_htonll (offset);
490 odb.file_id = p->data.file.file_id;
491 GNUNET_assert (pc->qre == NULL);
493 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
494 &chk->query, sizeof (struct OnDemandBlock), &odb,
495 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
496 p->bo.content_priority, p->bo.anonymity_level,
497 p->bo.replication_level, p->bo.expiration_time,
498 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Publishing block `%s' for offset %llu with size %u\n",
504 GNUNET_h2s (&chk->query), (unsigned long long) offset,
505 (unsigned int) block_size);
506 GNUNET_assert (pc->qre == NULL);
508 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
509 &chk->query, block_size, block, type,
510 p->bo.content_priority, p->bo.anonymity_level,
511 p->bo.replication_level, p->bo.expiration_time, -2,
512 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
518 * Function called with information about our
519 * progress in computing the tree encoding.
522 * @param offset where are we in the file
523 * @param pt_block plaintext of the currently processed block
524 * @param pt_size size of @a pt_block
525 * @param depth depth of the block in the tree, 0 for DBLOCK
528 progress_proc (void *cls, uint64_t offset,
529 const void *pt_block,
533 struct GNUNET_FS_PublishContext *pc = cls;
534 struct GNUNET_FS_FileInformation *p;
535 struct GNUNET_FS_FileInformation *par;
536 struct GNUNET_FS_ProgressInfo pi;
539 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
540 pi.value.publish.specifics.progress.data = pt_block;
541 pi.value.publish.specifics.progress.offset = offset;
542 pi.value.publish.specifics.progress.data_len = pt_size;
543 pi.value.publish.specifics.progress.depth = depth;
544 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
546 (GNUNET_YES == p->is_directory) )
548 while (NULL != (par = p->dir))
551 GNUNET_assert (GNUNET_YES == par->is_directory);
552 p->data.dir.contents_completed += pt_size;
553 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY;
554 pi.value.publish.specifics.progress_directory.completed = p->data.dir.contents_completed;
555 pi.value.publish.specifics.progress_directory.total = p->data.dir.contents_size;
556 pi.value.publish.specifics.progress_directory.eta = GNUNET_TIME_calculate_eta (p->start_time,
557 p->data.dir.contents_completed,
558 p->data.dir.contents_size);
559 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
566 * We are uploading a file or directory; load (if necessary) the next
567 * block into memory, encrypt it and send it to the FS service. Then
568 * continue with the main task.
570 * @param pc overall upload data
573 publish_content (struct GNUNET_FS_PublishContext *pc)
575 struct GNUNET_FS_FileInformation *p;
577 struct GNUNET_FS_DirectoryBuilder *db;
578 struct GNUNET_FS_FileInformation *dirpos;
583 GNUNET_assert (NULL != p);
586 if (GNUNET_YES == p->is_directory)
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
589 db = GNUNET_FS_directory_builder_create (p->meta);
590 dirpos = p->data.dir.entries;
591 while (NULL != dirpos)
593 if (GNUNET_YES == dirpos->is_directory)
595 raw_data = dirpos->data.dir.dir_data;
596 dirpos->data.dir.dir_data = NULL;
601 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
602 (dirpos->data.file.file_size > 0))
604 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
606 if (dirpos->data.file.file_size !=
607 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
608 dirpos->data.file.file_size, raw_data,
611 GNUNET_free_non_null (emsg);
612 GNUNET_free (raw_data);
615 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
618 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
620 GNUNET_free_non_null (raw_data);
621 dirpos = dirpos->next;
623 GNUNET_free_non_null (p->data.dir.dir_data);
624 p->data.dir.dir_data = NULL;
625 p->data.dir.dir_size = 0;
626 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
627 &p->data.dir.dir_data);
628 GNUNET_FS_file_information_sync_ (p);
630 size = (GNUNET_YES == p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Creating tree encoder\n");
634 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
635 &block_proc, &progress_proc,
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Processing next block from tree\n");
641 GNUNET_FS_tree_encoder_next (p->te);
646 * Process the response (or lack thereof) from
647 * the "fs" service to our 'start index' request.
649 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
650 * @param msg the response we got
653 process_index_start_response (void *cls,
654 const struct GNUNET_MessageHeader *msg)
656 struct GNUNET_FS_PublishContext *pc = cls;
657 struct GNUNET_FS_FileInformation *p;
661 GNUNET_CLIENT_disconnect (pc->client);
666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
667 _("Can not index file `%s': %s. Will try to insert instead.\n"),
669 _("timeout on index-start request to `fs' service"));
670 p->data.file.do_index = GNUNET_NO;
671 GNUNET_FS_file_information_sync_ (p);
672 publish_content (pc);
675 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
677 msize = ntohs (msg->size);
678 emsg = (const char *) &msg[1];
679 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
680 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
681 emsg = gettext_noop ("unknown error");
682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
684 ("Can not index file `%s': %s. Will try to insert instead.\n"),
685 p->filename, gettext (emsg));
686 p->data.file.do_index = GNUNET_NO;
687 GNUNET_FS_file_information_sync_ (p);
688 publish_content (pc);
691 p->data.file.index_start_confirmed = GNUNET_YES;
692 /* success! continue with indexing */
693 GNUNET_FS_file_information_sync_ (p);
694 publish_content (pc);
699 * Function called once the hash computation over an
700 * indexed file has completed.
702 * @param cls closure, our publishing context
703 * @param res resulting hash, NULL on error
706 hash_for_index_cb (void *cls,
707 const struct GNUNET_HashCode *res)
709 struct GNUNET_FS_PublishContext *pc = cls;
710 struct GNUNET_FS_FileInformation *p;
711 struct IndexStartMessage *ism;
713 struct GNUNET_CLIENT_Connection *client;
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
723 _("Can not index file `%s': %s. Will try to insert instead.\n"),
725 _("failed to compute hash"));
726 p->data.file.do_index = GNUNET_NO;
727 GNUNET_FS_file_information_sync_ (p);
728 publish_content (pc);
731 if (GNUNET_YES == p->data.file.index_start_confirmed)
733 publish_content (pc);
736 fn = GNUNET_STRINGS_filename_expand (p->filename);
737 GNUNET_assert (fn != NULL);
738 slen = strlen (fn) + 1;
740 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
742 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
744 ("Can not index file `%s': %s. Will try to insert instead.\n"),
745 fn, _("filename too long"));
747 p->data.file.do_index = GNUNET_NO;
748 GNUNET_FS_file_information_sync_ (p);
749 publish_content (pc);
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
753 p->filename, GNUNET_h2s (res));
754 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
756 p->data.file.file_id = *res;
757 p->data.file.have_hash = GNUNET_YES;
758 p->data.file.index_start_confirmed = GNUNET_YES;
759 GNUNET_FS_file_information_sync_ (p);
760 publish_content (pc);
764 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
768 _("Can not index file `%s': %s. Will try to insert instead.\n"),
770 _("could not connect to `fs' service"));
771 p->data.file.do_index = GNUNET_NO;
772 publish_content (pc);
776 if (p->data.file.have_hash != GNUNET_YES)
778 p->data.file.file_id = *res;
779 p->data.file.have_hash = GNUNET_YES;
780 GNUNET_FS_file_information_sync_ (p);
782 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
783 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
784 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
785 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
787 ism->device = GNUNET_htonll (dev);
788 ism->inode = GNUNET_htonll (ino);
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 _("Failed to get file identifiers for `%s'\n"), p->filename);
796 memcpy (&ism[1], fn, slen);
799 GNUNET_break (GNUNET_YES ==
800 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
801 GNUNET_TIME_UNIT_FOREVER_REL,
803 &process_index_start_response,
810 * Main function that performs the upload.
812 * @param cls `struct GNUNET_FS_PublishContext *` identifies the upload
813 * @param tc task context
816 GNUNET_FS_publish_main_ (void *cls,
817 const struct GNUNET_SCHEDULER_TaskContext *tc)
819 struct GNUNET_FS_PublishContext *pc = cls;
820 struct GNUNET_FS_ProgressInfo pi;
821 struct GNUNET_FS_FileInformation *p;
822 struct GNUNET_FS_Uri *loc;
825 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Publishing complete, now publishing SKS and KSK blocks.\n");
831 /* upload of entire hierarchy complete,
832 * publish namespace entries */
833 GNUNET_FS_publish_sync_ (pc);
837 /* find starting position */
838 while ( (GNUNET_YES == p->is_directory) &&
839 (NULL != p->data.dir.entries) &&
841 (NULL == p->data.dir.entries->chk_uri) )
843 p = p->data.dir.entries;
845 GNUNET_FS_publish_sync_ (pc);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Error uploading: %s\n",
853 /* error with current file, abort all
854 * related files as well! */
855 while (NULL != p->dir)
857 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
858 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
862 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
868 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
870 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
871 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
872 pi.value.publish.specifics.error.message = p->emsg;
873 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
875 pc->all_done = GNUNET_YES;
876 GNUNET_FS_publish_sync_ (pc);
879 /* handle completion */
880 if (NULL != p->chk_uri)
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "File upload complete, now publishing KSK blocks.\n");
884 if (0 == p->bo.anonymity_level)
886 /* zero anonymity, box CHK URI in LOC URI */
888 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
889 p->bo.expiration_time);
890 GNUNET_FS_uri_destroy (p->chk_uri);
892 GNUNET_FS_file_information_sync_ (p);
894 GNUNET_FS_publish_sync_ (pc);
895 /* upload of "p" complete, publish KBlocks! */
896 if (NULL != p->keywords)
898 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
899 pc->options, &publish_kblocks_cont, pc);
903 publish_kblocks_cont (pc, p->chk_uri, NULL);
907 if ((GNUNET_YES != p->is_directory) && (p->data.file.do_index))
909 if (NULL == p->filename)
911 p->data.file.do_index = GNUNET_NO;
912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 ("Can not index file `%s': %s. Will try to insert instead.\n"),
915 "<no-name>", _("needs to be an actual file"));
916 GNUNET_FS_file_information_sync_ (p);
917 publish_content (pc);
920 if (p->data.file.have_hash)
922 hash_for_index_cb (pc, &p->data.file.file_id);
926 p->start_time = GNUNET_TIME_absolute_get ();
928 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
929 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
933 publish_content (pc);
938 * Signal the FS's progress function that we are starting
941 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
942 * @param fi the entry in the publish-structure
943 * @param length length of the file or directory
944 * @param meta metadata for the file or directory (can be modified)
945 * @param uri pointer to the keywords that will be used for this entry (can be modified)
946 * @param bo block options
947 * @param do_index should we index?
948 * @param client_info pointer to client context set upon creation (can be modified)
949 * @return #GNUNET_OK to continue (always)
952 fip_signal_start (void *cls,
953 struct GNUNET_FS_FileInformation *fi,
955 struct GNUNET_CONTAINER_MetaData *meta,
956 struct GNUNET_FS_Uri **uri,
957 struct GNUNET_FS_BlockOptions *bo,
961 struct GNUNET_FS_PublishContext *pc = cls;
962 struct GNUNET_FS_ProgressInfo pi;
966 if (GNUNET_YES == pc->skip_next_fi_callback)
968 pc->skip_next_fi_callback = GNUNET_NO;
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Starting publish operation\n");
975 /* space for on-demand blocks */
977 ((length + DBLOCK_SIZE -
978 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
982 /* space for DBlocks */
983 pc->reserve_space += length;
985 /* entries for IBlocks and DBlocks, space for IBlocks */
989 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
990 pc->reserve_entries += left;
993 left = left * sizeof (struct ContentHashKey);
994 pc->reserve_space += left;
996 pc->reserve_entries++;
997 /* entries and space for keywords */
1000 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
1001 pc->reserve_entries += kc;
1002 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
1004 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
1005 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1006 GNUNET_FS_file_information_sync_ (fi);
1007 if ((fi->is_directory) && (fi->dir != NULL))
1009 /* We are a directory, and we are not top-level; process entries in directory */
1010 pc->skip_next_fi_callback = GNUNET_YES;
1011 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
1018 * Actually signal the FS's progress function that we are suspending
1021 * @param fi the entry in the publish-structure
1022 * @param pc the publish context of which a file is being suspended
1025 suspend_operation (struct GNUNET_FS_FileInformation *fi,
1026 struct GNUNET_FS_PublishContext *pc)
1028 struct GNUNET_FS_ProgressInfo pi;
1031 if (NULL != pc->ksk_pc)
1033 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1036 if (NULL != pc->sks_pc)
1038 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Suspending publish operation\n");
1043 GNUNET_free_non_null (fi->serialization);
1044 fi->serialization = NULL;
1045 off = (NULL == fi->chk_uri) ? 0 : (GNUNET_YES == fi->is_directory) ? fi->data.dir.dir_size : fi->data.file.file_size;
1046 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1047 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1048 if (NULL != pc->qre)
1050 GNUNET_DATASTORE_cancel (pc->qre);
1053 if (NULL != pc->dsh)
1055 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1063 * Signal the FS's progress function that we are suspending
1064 * an upload. Performs the recursion.
1066 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1067 * @param fi the entry in the publish-structure
1068 * @param length length of the file or directory
1069 * @param meta metadata for the file or directory (can be modified)
1070 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1071 * @param bo block options
1072 * @param do_index should we index?
1073 * @param client_info pointer to client context set upon creation (can be modified)
1074 * @return #GNUNET_OK to continue (always)
1077 fip_signal_suspend (void *cls,
1078 struct GNUNET_FS_FileInformation *fi,
1080 struct GNUNET_CONTAINER_MetaData *meta,
1081 struct GNUNET_FS_Uri **uri,
1082 struct GNUNET_FS_BlockOptions *bo,
1086 struct GNUNET_FS_PublishContext *pc = cls;
1088 if (GNUNET_YES == pc->skip_next_fi_callback)
1090 pc->skip_next_fi_callback = GNUNET_NO;
1093 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1095 /* process entries in directory */
1096 pc->skip_next_fi_callback = GNUNET_YES;
1097 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1099 suspend_operation (fi, pc);
1100 *client_info = NULL;
1106 * Create SUSPEND event for the given publish operation
1107 * and then clean up our state (without stop signal).
1109 * @param cls the `struct GNUNET_FS_PublishContext` to signal for
1112 GNUNET_FS_publish_signal_suspend_ (void *cls)
1114 struct GNUNET_FS_PublishContext *pc = cls;
1116 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1118 GNUNET_SCHEDULER_cancel (pc->upload_task);
1119 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1121 pc->skip_next_fi_callback = GNUNET_YES;
1122 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1123 suspend_operation (pc->fi, pc);
1124 GNUNET_FS_end_top (pc->h, pc->top);
1126 publish_cleanup (pc);
1131 * We have gotten a reply for our space reservation request.
1132 * Either fail (insufficient space) or start publishing for good.
1134 * @param cls the `struct GNUNET_FS_PublishContext *`
1135 * @param success positive reservation ID on success
1136 * @param min_expiration minimum expiration time required for content to be stored
1137 * @param msg error message on error, otherwise NULL
1140 finish_reserve (void *cls, int success,
1141 struct GNUNET_TIME_Absolute min_expiration,
1144 struct GNUNET_FS_PublishContext *pc = cls;
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1148 if ((msg != NULL) || (success <= 0))
1150 GNUNET_asprintf (&pc->fi->emsg,
1151 _("Insufficient space for publishing: %s"),
1153 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1157 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1159 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1160 &GNUNET_FS_publish_main_, pc);
1165 * Calculate the total size of all of the files in the directory structure.
1167 * @param fi file structure to traverse
1170 compute_contents_size (struct GNUNET_FS_FileInformation *fi)
1172 struct GNUNET_FS_FileInformation *ent;
1174 if (GNUNET_YES != fi->is_directory)
1175 return fi->data.file.file_size;
1176 fi->data.dir.contents_size = 0;
1177 for (ent = fi->data.dir.entries; NULL != ent; ent = ent->next)
1178 fi->data.dir.contents_size += compute_contents_size (ent);
1179 return fi->data.dir.contents_size;
1184 * Publish a file or directory.
1186 * @param h handle to the file sharing subsystem
1187 * @param fi information about the file or directory structure to publish
1188 * @param ns namespace to publish the file in, NULL for no namespace
1189 * @param nid identifier to use for the publishd content in the namespace
1190 * (can be NULL, must be NULL if namespace is NULL)
1191 * @param nuid update-identifier that will be used for future updates
1192 * (can be NULL, must be NULL if namespace or nid is NULL)
1193 * @param options options for the publication
1194 * @return context that can be used to control the publish operation
1196 struct GNUNET_FS_PublishContext *
1197 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1198 struct GNUNET_FS_FileInformation *fi,
1199 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
1202 enum GNUNET_FS_PublishOptions options)
1204 struct GNUNET_FS_PublishContext *ret;
1205 struct GNUNET_DATASTORE_Handle *dsh;
1207 GNUNET_assert (NULL != h);
1208 compute_contents_size (fi);
1209 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1211 dsh = GNUNET_DATASTORE_connect (h->cfg);
1219 ret = GNUNET_new (struct GNUNET_FS_PublishContext);
1225 ret->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1227 GNUNET_assert (NULL != nid);
1228 ret->nid = GNUNET_strdup (nid);
1230 ret->nuid = GNUNET_strdup (nuid);
1232 ret->options = options;
1234 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1235 ret->fi_pos = ret->fi;
1236 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1237 GNUNET_FS_publish_sync_ (ret);
1238 if (NULL != ret->dsh)
1240 GNUNET_assert (NULL == ret->qre);
1241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1243 ("Reserving space for %u entries and %llu bytes for publication\n"),
1244 (unsigned int) ret->reserve_entries,
1245 (unsigned long long) ret->reserve_space);
1247 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1248 ret->reserve_entries, UINT_MAX, UINT_MAX,
1249 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1254 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1256 GNUNET_SCHEDULER_add_with_priority
1257 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1264 * Signal the FS's progress function that we are stopping
1267 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`)
1268 * @param fi the entry in the publish-structure
1269 * @param length length of the file or directory
1270 * @param meta metadata for the file or directory (can be modified)
1271 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1272 * @param bo block options (can be modified)
1273 * @param do_index should we index?
1274 * @param client_info pointer to client context set upon creation (can be modified)
1275 * @return #GNUNET_OK to continue (always)
1278 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1279 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1280 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1281 int *do_index, void **client_info)
1283 struct GNUNET_FS_PublishContext *pc = cls;
1284 struct GNUNET_FS_ProgressInfo pi;
1287 if (GNUNET_YES == pc->skip_next_fi_callback)
1289 pc->skip_next_fi_callback = GNUNET_NO;
1292 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1294 /* process entries in directory first */
1295 pc->skip_next_fi_callback = GNUNET_YES;
1296 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1298 if (fi->serialization != NULL)
1300 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1302 GNUNET_free (fi->serialization);
1303 fi->serialization = NULL;
1305 off = (fi->chk_uri == NULL) ? 0 : length;
1306 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1307 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1308 *client_info = NULL;
1314 * Stop an upload. Will abort incomplete uploads (but
1315 * not remove blocks that have already been publishd) or
1316 * simply clean up the state for completed uploads.
1317 * Must NOT be called from within the event callback!
1319 * @param pc context for the upload to stop
1322 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1324 struct GNUNET_FS_ProgressInfo pi;
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1328 GNUNET_FS_end_top (pc->h, pc->top);
1329 if (NULL != pc->ksk_pc)
1331 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1334 if (NULL != pc->sks_pc)
1336 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1339 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1341 GNUNET_SCHEDULER_cancel (pc->upload_task);
1342 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1344 pc->skip_next_fi_callback = GNUNET_YES;
1345 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1347 if (pc->fi->serialization != NULL)
1349 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1350 pc->fi->serialization);
1351 GNUNET_free (pc->fi->serialization);
1352 pc->fi->serialization = NULL;
1354 off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1356 if (pc->serialization != NULL)
1358 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1360 GNUNET_free (pc->serialization);
1361 pc->serialization = NULL;
1363 if (NULL != pc->qre)
1365 GNUNET_DATASTORE_cancel (pc->qre);
1368 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1369 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1370 publish_cleanup (pc);
1375 /* end of fs_publish.c */