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;
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, "Cleaning up publish context (done!)\n");
82 GNUNET_CRYPTO_hash_file_cancel (pc->fhc);
85 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
86 if (pc->namespace != NULL)
88 GNUNET_FS_namespace_delete (pc->namespace, GNUNET_NO);
91 GNUNET_free_non_null (pc->nid);
92 GNUNET_free_non_null (pc->nuid);
93 GNUNET_free_non_null (pc->serialization);
96 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
99 if (pc->client != NULL)
101 GNUNET_CLIENT_disconnect (pc->client);
104 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
110 * Function called by the datastore API with
111 * the result from the PUT request.
113 * @param cls the 'struct GNUNET_FS_PublishContext'
114 * @param success GNUNET_OK on success
115 * @param min_expiration minimum expiration time required for content to be stored
116 * @param msg error message (or NULL)
119 ds_put_cont (void *cls, int success,
120 struct GNUNET_TIME_Absolute min_expiration,
123 struct GNUNET_FS_PublishContext *pc = cls;
124 struct GNUNET_FS_ProgressInfo pi;
127 if (GNUNET_SYSERR == success)
129 GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
130 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
131 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
132 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
133 pc->fi_pos->client_info =
134 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
135 if ((pc->fi_pos->is_directory != GNUNET_YES) &&
136 (pc->fi_pos->filename != NULL) &&
137 (pc->fi_pos->data.file.do_index == GNUNET_YES))
139 /* run unindex to clean up */
140 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
143 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
145 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
146 &GNUNET_FS_publish_main_, pc);
151 * Generate the callback that signals clients
152 * that a file (or directory) has been completely
155 * @param p the completed upload
156 * @param pc context of the publication
159 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
160 struct GNUNET_FS_PublishContext *pc)
162 struct GNUNET_FS_ProgressInfo pi;
164 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
165 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
166 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
168 GNUNET_FS_publish_make_status_ (&pi, pc, p,
169 GNUNET_ntohll (p->chk_uri->data.
175 * Generate the callback that signals clients
176 * that a file (or directory) has encountered
177 * a problem during publication.
179 * @param p the upload that had trouble
180 * @param pc context of the publication
181 * @param emsg error message
184 signal_publish_error (struct GNUNET_FS_FileInformation *p,
185 struct GNUNET_FS_PublishContext *pc, const char *emsg)
187 struct GNUNET_FS_ProgressInfo pi;
189 p->emsg = GNUNET_strdup (emsg);
190 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
191 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
192 pi.value.publish.specifics.error.message = emsg;
193 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
194 if ((p->is_directory != GNUNET_YES) && (p->filename != NULL) &&
195 (p->data.file.do_index == GNUNET_YES))
197 /* run unindex to clean up */
198 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
205 * Datastore returns from reservation cancel request.
207 * @param cls the 'struct GNUNET_FS_PublishContext'
208 * @param success success code (not used)
209 * @param min_expiration minimum expiration time required for content to be stored
210 * @param msg error message (typically NULL, not used)
213 finish_release_reserve (void *cls, int success,
214 struct GNUNET_TIME_Absolute min_expiration,
217 struct GNUNET_FS_PublishContext *pc = cls;
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "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, const struct GNUNET_FS_Uri *uri,
239 struct GNUNET_FS_PublishContext *pc = cls;
244 signal_publish_error (pc->fi, pc, emsg);
245 GNUNET_FS_publish_sync_ (pc);
248 GNUNET_assert (pc->qre == NULL);
249 if ((pc->dsh != NULL) && (pc->rid != 0))
252 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
253 GNUNET_TIME_UNIT_FOREVER_REL,
254 &finish_release_reserve, pc);
258 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
264 * We are almost done publishing the structure,
265 * add SBlocks (if needed).
267 * @param pc overall upload data
270 publish_sblock (struct GNUNET_FS_PublishContext *pc)
272 if (NULL != pc->namespace)
273 pc->sks_pc = GNUNET_FS_publish_sks (pc->h, pc->namespace, pc->nid, pc->nuid,
274 pc->fi->meta, pc->fi->chk_uri, &pc->fi->bo,
275 pc->options, &publish_sblocks_cont, pc);
277 publish_sblocks_cont (pc, NULL, NULL);
282 * We've finished publishing a KBlock as part of a larger upload.
283 * Check the result and continue the larger upload.
285 * @param cls the "struct GNUNET_FS_PublishContext*"
286 * of the larger upload
287 * @param uri URI of the published blocks
288 * @param emsg NULL on success, otherwise error message
291 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
294 struct GNUNET_FS_PublishContext *pc = cls;
295 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading KSK blocks: %s\n",
302 signal_publish_error (p, pc, emsg);
303 GNUNET_FS_file_information_sync_ (p);
304 GNUNET_FS_publish_sync_ (pc);
305 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
307 GNUNET_SCHEDULER_add_with_priority
308 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "KSK blocks published, moving on to next file\n");
314 signal_publish_completion (p, pc);
315 /* move on to next file */
317 pc->fi_pos = p->next;
320 GNUNET_FS_publish_sync_ (pc);
321 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
323 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
324 &GNUNET_FS_publish_main_, pc);
329 * Function called by the tree encoder to obtain
330 * a block of plaintext data (for the lowest level
333 * @param cls our publishing context
334 * @param offset identifies which block to get
335 * @param max (maximum) number of bytes to get; returning
336 * fewer will also cause errors
337 * @param buf where to copy the plaintext buffer
338 * @param emsg location to store an error message (on error)
339 * @return number of bytes copied to buf, 0 on error
342 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
344 struct GNUNET_FS_PublishContext *pc = cls;
345 struct GNUNET_FS_FileInformation *p;
350 if (p->is_directory == GNUNET_YES)
352 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
353 dd = p->data.dir.dir_data;
354 memcpy (buf, &dd[offset], pt_size);
358 if (UINT64_MAX == offset)
359 return p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
360 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
362 return 0; /* calling reader with pt_size==0
363 * might free buf, so don't! */
365 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
374 * The tree encoder has finished processing a
375 * file. Call it's finish method and deal with
378 * @param cls our publishing context
379 * @param tc scheduler's task context (not used)
382 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
384 struct GNUNET_FS_PublishContext *pc = cls;
385 struct GNUNET_FS_FileInformation *p;
386 struct GNUNET_FS_ProgressInfo pi;
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
392 GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
397 GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
399 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
400 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
401 pi.value.publish.specifics.error.message = p->emsg;
402 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
406 /* final progress event */
407 GNUNET_assert (NULL != p->chk_uri);
408 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
409 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
410 pi.value.publish.specifics.progress.data = NULL;
411 pi.value.publish.specifics.progress.offset = flen;
412 pi.value.publish.specifics.progress.data_len = 0;
413 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
414 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
416 GNUNET_FS_file_information_sync_ (p);
417 /* continue with main */
418 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
420 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
421 &GNUNET_FS_publish_main_, pc);
426 * Function called asking for the current (encoded)
427 * block to be processed. After processing the
428 * client should either call "GNUNET_FS_tree_encode_next"
429 * or (on error) "GNUNET_FS_tree_encode_finish".
432 * @param chk content hash key for the block
433 * @param offset offset of the block in the file
434 * @param depth depth of the block in the file, 0 for DBLOCK
435 * @param type type of the block (IBLOCK or DBLOCK)
436 * @param block the (encrypted) block
437 * @param block_size size of block (in bytes)
440 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
441 unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
444 struct GNUNET_FS_PublishContext *pc = cls;
445 struct GNUNET_FS_FileInformation *p;
446 struct OnDemandBlock odb;
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
452 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
454 GNUNET_SCHEDULER_add_with_priority
455 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
459 if ((p->is_directory != GNUNET_YES) && (GNUNET_YES == p->data.file.do_index) &&
460 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Indexing block `%s' for offset %llu with index size %u\n",
464 GNUNET_h2s (&chk->query), (unsigned long long) offset,
465 sizeof (struct OnDemandBlock));
466 odb.offset = GNUNET_htonll (offset);
467 odb.file_id = p->data.file.file_id;
468 GNUNET_assert (pc->qre == NULL);
470 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
471 &chk->query, sizeof (struct OnDemandBlock), &odb,
472 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
473 p->bo.content_priority, p->bo.anonymity_level,
474 p->bo.replication_level, p->bo.expiration_time,
475 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Publishing block `%s' for offset %llu with size %u\n",
481 GNUNET_h2s (&chk->query), (unsigned long long) offset,
482 (unsigned int) block_size);
483 GNUNET_assert (pc->qre == NULL);
485 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
486 &chk->query, block_size, block, type,
487 p->bo.content_priority, p->bo.anonymity_level,
488 p->bo.replication_level, p->bo.expiration_time, -2,
489 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
495 * Function called with information about our
496 * progress in computing the tree encoding.
499 * @param offset where are we in the file
500 * @param pt_block plaintext of the currently processed block
501 * @param pt_size size of pt_block
502 * @param depth depth of the block in the tree, 0 for DBLOCK
505 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
508 struct GNUNET_FS_PublishContext *pc = cls;
509 struct GNUNET_FS_FileInformation *p;
510 struct GNUNET_FS_ProgressInfo pi;
513 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
514 pi.value.publish.specifics.progress.data = pt_block;
515 pi.value.publish.specifics.progress.offset = offset;
516 pi.value.publish.specifics.progress.data_len = pt_size;
517 pi.value.publish.specifics.progress.depth = depth;
518 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
523 * We are uploading a file or directory; load (if necessary) the next
524 * block into memory, encrypt it and send it to the FS service. Then
525 * continue with the main task.
527 * @param pc overall upload data
530 publish_content (struct GNUNET_FS_PublishContext *pc)
532 struct GNUNET_FS_FileInformation *p;
534 struct GNUNET_FS_DirectoryBuilder *db;
535 struct GNUNET_FS_FileInformation *dirpos;
540 GNUNET_assert (p != NULL);
543 if (p->is_directory == GNUNET_YES)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
546 db = GNUNET_FS_directory_builder_create (p->meta);
547 dirpos = p->data.dir.entries;
548 while (NULL != dirpos)
550 if (dirpos->is_directory == GNUNET_YES)
552 raw_data = dirpos->data.dir.dir_data;
553 dirpos->data.dir.dir_data = NULL;
558 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
559 (dirpos->data.file.file_size > 0))
561 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
563 if (dirpos->data.file.file_size !=
564 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
565 dirpos->data.file.file_size, raw_data,
568 GNUNET_free_non_null (emsg);
569 GNUNET_free (raw_data);
574 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
576 GNUNET_free_non_null (raw_data);
577 dirpos = dirpos->next;
579 GNUNET_free_non_null (p->data.dir.dir_data);
580 p->data.dir.dir_data = NULL;
581 p->data.dir.dir_size = 0;
582 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
583 &p->data.dir.dir_data);
584 GNUNET_FS_file_information_sync_ (p);
586 size = (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
589 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
590 &block_proc, &progress_proc,
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
595 GNUNET_FS_tree_encoder_next (p->te);
600 * Process the response (or lack thereof) from
601 * the "fs" service to our 'start index' request.
603 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
604 * @param msg the response we got
607 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
609 struct GNUNET_FS_PublishContext *pc = cls;
610 struct GNUNET_FS_FileInformation *p;
614 GNUNET_CLIENT_disconnect (pc->client);
619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 ("Can not index file `%s': %s. Will try to insert instead.\n"),
623 _("timeout on index-start request to `fs' service"));
624 p->data.file.do_index = GNUNET_NO;
625 GNUNET_FS_file_information_sync_ (p);
626 publish_content (pc);
629 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
631 msize = ntohs (msg->size);
632 emsg = (const char *) &msg[1];
633 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
634 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
635 emsg = gettext_noop ("unknown error");
636 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 ("Can not index file `%s': %s. Will try to insert instead.\n"),
639 p->filename, gettext (emsg));
640 p->data.file.do_index = GNUNET_NO;
641 GNUNET_FS_file_information_sync_ (p);
642 publish_content (pc);
645 p->data.file.index_start_confirmed = GNUNET_YES;
646 /* success! continue with indexing */
647 GNUNET_FS_file_information_sync_ (p);
648 publish_content (pc);
653 * Function called once the hash computation over an
654 * indexed file has completed.
656 * @param cls closure, our publishing context
657 * @param res resulting hash, NULL on error
660 hash_for_index_cb (void *cls, const GNUNET_HashCode * res)
662 struct GNUNET_FS_PublishContext *pc = cls;
663 struct GNUNET_FS_FileInformation *p;
664 struct IndexStartMessage *ism;
666 struct GNUNET_CLIENT_Connection *client;
675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677 ("Can not index file `%s': %s. Will try to insert instead.\n"),
678 p->filename, _("failed to compute hash"));
679 p->data.file.do_index = GNUNET_NO;
680 GNUNET_FS_file_information_sync_ (p);
681 publish_content (pc);
684 if (GNUNET_YES == p->data.file.index_start_confirmed)
686 publish_content (pc);
689 fn = GNUNET_STRINGS_filename_expand (p->filename);
690 GNUNET_assert (fn != NULL);
691 slen = strlen (fn) + 1;
693 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 ("Can not index file `%s': %s. Will try to insert instead.\n"),
698 fn, _("filename too long"));
700 p->data.file.do_index = GNUNET_NO;
701 GNUNET_FS_file_information_sync_ (p);
702 publish_content (pc);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
706 p->filename, GNUNET_h2s (res));
707 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
709 p->data.file.file_id = *res;
710 p->data.file.have_hash = GNUNET_YES;
711 p->data.file.index_start_confirmed = GNUNET_YES;
712 GNUNET_FS_file_information_sync_ (p);
713 publish_content (pc);
717 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
720 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722 ("Can not index file `%s': %s. Will try to insert instead.\n"),
723 p->filename, _("could not connect to `fs' service"));
724 p->data.file.do_index = GNUNET_NO;
725 publish_content (pc);
729 if (p->data.file.have_hash != GNUNET_YES)
731 p->data.file.file_id = *res;
732 p->data.file.have_hash = GNUNET_YES;
733 GNUNET_FS_file_information_sync_ (p);
735 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
736 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
737 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
738 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
740 ism->device = GNUNET_htonll (dev);
741 ism->inode = GNUNET_htonll (ino);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 _("Failed to get file identifiers for `%s'\n"), p->filename);
749 memcpy (&ism[1], fn, slen);
752 GNUNET_break (GNUNET_YES ==
753 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
754 GNUNET_TIME_UNIT_FOREVER_REL,
756 &process_index_start_response,
763 * Main function that performs the upload.
765 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
766 * @param tc task context
769 GNUNET_FS_publish_main_ (void *cls,
770 const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct GNUNET_FS_PublishContext *pc = cls;
773 struct GNUNET_FS_ProgressInfo pi;
774 struct GNUNET_FS_FileInformation *p;
775 struct GNUNET_FS_Uri *loc;
778 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Publishing complete, now publishing SKS and KSK blocks.\n");
784 /* upload of entire hierarchy complete,
785 * publish namespace entries */
786 GNUNET_FS_publish_sync_ (pc);
790 /* find starting position */
791 while ((p->is_directory == GNUNET_YES) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
792 && (NULL == p->data.dir.entries->chk_uri))
794 p = p->data.dir.entries;
796 GNUNET_FS_publish_sync_ (pc);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
802 /* error with current file, abort all
803 * related files as well! */
804 while (NULL != p->dir)
806 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
807 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
811 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
817 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
819 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
820 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
821 pi.value.publish.specifics.error.message = p->emsg;
822 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
824 pc->all_done = GNUNET_YES;
825 GNUNET_FS_publish_sync_ (pc);
828 /* handle completion */
829 if (NULL != p->chk_uri)
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "File upload complete, now publishing KSK blocks.\n");
833 if (0 == p->bo.anonymity_level)
835 /* zero anonymity, box CHK URI in LOC URI */
837 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
838 p->bo.expiration_time);
839 GNUNET_FS_uri_destroy (p->chk_uri);
842 GNUNET_FS_publish_sync_ (pc);
843 /* upload of "p" complete, publish KBlocks! */
844 if (p->keywords != NULL)
846 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
847 pc->options, &publish_kblocks_cont, pc);
851 publish_kblocks_cont (pc, p->chk_uri, NULL);
855 if ((p->is_directory != GNUNET_YES) && (p->data.file.do_index))
857 if (NULL == p->filename)
859 p->data.file.do_index = GNUNET_NO;
860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
862 ("Can not index file `%s': %s. Will try to insert instead.\n"),
863 "<no-name>", _("needs to be an actual file"));
864 GNUNET_FS_file_information_sync_ (p);
865 publish_content (pc);
868 if (p->data.file.have_hash)
870 hash_for_index_cb (pc, &p->data.file.file_id);
874 p->start_time = GNUNET_TIME_absolute_get ();
876 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
877 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
881 publish_content (pc);
886 * Signal the FS's progress function that we are starting
889 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
890 * @param fi the entry in the publish-structure
891 * @param length length of the file or directory
892 * @param meta metadata for the file or directory (can be modified)
893 * @param uri pointer to the keywords that will be used for this entry (can be modified)
894 * @param bo block options
895 * @param do_index should we index?
896 * @param client_info pointer to client context set upon creation (can be modified)
897 * @return GNUNET_OK to continue (always)
900 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
901 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
902 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
903 int *do_index, void **client_info)
905 struct GNUNET_FS_PublishContext *pc = cls;
906 struct GNUNET_FS_ProgressInfo pi;
910 if (GNUNET_YES == pc->skip_next_fi_callback)
912 pc->skip_next_fi_callback = GNUNET_NO;
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
918 /* space for on-demand blocks */
920 ((length + DBLOCK_SIZE -
921 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
925 /* space for DBlocks */
926 pc->reserve_space += length;
928 /* entries for IBlocks and DBlocks, space for IBlocks */
932 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
933 pc->reserve_entries += left;
936 left = left * sizeof (struct ContentHashKey);
937 pc->reserve_space += left;
939 pc->reserve_entries++;
940 /* entries and space for keywords */
943 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
944 pc->reserve_entries += kc;
945 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
947 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
948 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
949 GNUNET_FS_file_information_sync_ (fi);
950 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)
951 && (fi->dir != NULL))
953 /* process entries in directory */
954 pc->skip_next_fi_callback = GNUNET_YES;
955 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
962 * Signal the FS's progress function that we are suspending
965 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
966 * @param fi the entry in the publish-structure
967 * @param length length of the file or directory
968 * @param meta metadata for the file or directory (can be modified)
969 * @param uri pointer to the keywords that will be used for this entry (can be modified)
970 * @param bo block options
971 * @param do_index should we index?
972 * @param client_info pointer to client context set upon creation (can be modified)
973 * @return GNUNET_OK to continue (always)
976 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
977 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
978 struct GNUNET_FS_Uri **uri,
979 struct GNUNET_FS_BlockOptions *bo, int *do_index,
982 struct GNUNET_FS_PublishContext *pc = cls;
983 struct GNUNET_FS_ProgressInfo pi;
986 if (GNUNET_YES == pc->skip_next_fi_callback)
988 pc->skip_next_fi_callback = GNUNET_NO;
991 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
993 /* process entries in directory */
994 pc->skip_next_fi_callback = GNUNET_YES;
995 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
997 if (NULL != pc->ksk_pc)
999 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1002 if (NULL != pc->sks_pc)
1004 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
1008 GNUNET_free_non_null (fi->serialization);
1009 fi->serialization = NULL;
1010 off = (fi->chk_uri == NULL) ? 0 : length;
1011 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
1012 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1013 *client_info = NULL;
1014 if (NULL != pc->qre)
1016 GNUNET_DATASTORE_cancel (pc->qre);
1019 if (NULL != pc->dsh)
1021 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1030 * Create SUSPEND event for the given publish operation
1031 * and then clean up our state (without stop signal).
1033 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1036 GNUNET_FS_publish_signal_suspend_ (void *cls)
1038 struct GNUNET_FS_PublishContext *pc = cls;
1040 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1042 GNUNET_SCHEDULER_cancel (pc->upload_task);
1043 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1045 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1046 GNUNET_FS_end_top (pc->h, pc->top);
1048 publish_cleanup (pc);
1053 * We have gotten a reply for our space reservation request.
1054 * Either fail (insufficient space) or start publishing for good.
1056 * @param cls the 'struct GNUNET_FS_PublishContext*'
1057 * @param success positive reservation ID on success
1058 * @param min_expiration minimum expiration time required for content to be stored
1059 * @param msg error message on error, otherwise NULL
1062 finish_reserve (void *cls, int success,
1063 struct GNUNET_TIME_Absolute min_expiration,
1066 struct GNUNET_FS_PublishContext *pc = cls;
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1070 if ((msg != NULL) || (success <= 0))
1072 GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1074 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1078 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1080 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1081 &GNUNET_FS_publish_main_, pc);
1086 * Publish a file or directory.
1088 * @param h handle to the file sharing subsystem
1089 * @param fi information about the file or directory structure to publish
1090 * @param namespace namespace to publish the file in, NULL for no namespace
1091 * @param nid identifier to use for the publishd content in the namespace
1092 * (can be NULL, must be NULL if namespace is NULL)
1093 * @param nuid update-identifier that will be used for future updates
1094 * (can be NULL, must be NULL if namespace or nid is NULL)
1095 * @param options options for the publication
1096 * @return context that can be used to control the publish operation
1098 struct GNUNET_FS_PublishContext *
1099 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1100 struct GNUNET_FS_FileInformation *fi,
1101 struct GNUNET_FS_Namespace *namespace, const char *nid,
1103 enum GNUNET_FS_PublishOptions options)
1105 struct GNUNET_FS_PublishContext *ret;
1106 struct GNUNET_DATASTORE_Handle *dsh;
1108 GNUNET_assert (NULL != h);
1109 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1111 dsh = GNUNET_DATASTORE_connect (h->cfg);
1119 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1123 ret->namespace = namespace;
1124 ret->options = options;
1125 if (namespace != NULL)
1128 GNUNET_assert (NULL != nid);
1129 ret->nid = GNUNET_strdup (nid);
1131 ret->nuid = GNUNET_strdup (nuid);
1134 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1135 ret->fi_pos = ret->fi;
1136 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1137 GNUNET_FS_publish_sync_ (ret);
1138 if (NULL != ret->dsh)
1140 GNUNET_assert (NULL == ret->qre);
1141 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1143 ("Reserving space for %u entries and %llu bytes for publication\n"),
1144 (unsigned int) ret->reserve_entries,
1145 (unsigned long long) ret->reserve_space);
1147 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1148 ret->reserve_entries, UINT_MAX, UINT_MAX,
1149 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1154 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1156 GNUNET_SCHEDULER_add_with_priority
1157 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1164 * Signal the FS's progress function that we are stopping
1167 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1168 * @param fi the entry in the publish-structure
1169 * @param length length of the file or directory
1170 * @param meta metadata for the file or directory (can be modified)
1171 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1172 * @param bo block options (can be modified)
1173 * @param do_index should we index?
1174 * @param client_info pointer to client context set upon creation (can be modified)
1175 * @return GNUNET_OK to continue (always)
1178 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1179 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1180 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1181 int *do_index, void **client_info)
1183 struct GNUNET_FS_PublishContext *pc = cls;
1184 struct GNUNET_FS_ProgressInfo pi;
1187 if (GNUNET_YES == pc->skip_next_fi_callback)
1189 pc->skip_next_fi_callback = GNUNET_NO;
1192 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1194 /* process entries in directory first */
1195 pc->skip_next_fi_callback = GNUNET_YES;
1196 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1198 if (fi->serialization != NULL)
1200 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1202 GNUNET_free (fi->serialization);
1203 fi->serialization = NULL;
1205 off = (fi->chk_uri == NULL) ? 0 : length;
1206 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1207 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1208 *client_info = NULL;
1214 * Stop an upload. Will abort incomplete uploads (but
1215 * not remove blocks that have already been publishd) or
1216 * simply clean up the state for completed uploads.
1217 * Must NOT be called from within the event callback!
1219 * @param pc context for the upload to stop
1222 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1224 struct GNUNET_FS_ProgressInfo pi;
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1228 GNUNET_FS_end_top (pc->h, pc->top);
1229 if (NULL != pc->ksk_pc)
1231 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1234 if (NULL != pc->sks_pc)
1236 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1239 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1241 GNUNET_SCHEDULER_cancel (pc->upload_task);
1242 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1244 pc->skip_next_fi_callback = GNUNET_YES;
1245 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1247 if (pc->fi->serialization != NULL)
1249 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1250 pc->fi->serialization);
1251 GNUNET_free (pc->fi->serialization);
1252 pc->fi->serialization = NULL;
1254 off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1256 if (pc->serialization != NULL)
1258 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1260 GNUNET_free (pc->serialization);
1261 pc->serialization = NULL;
1263 if (NULL != pc->qre)
1265 GNUNET_DATASTORE_cancel (pc->qre);
1268 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1269 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1270 publish_cleanup (pc);
1275 /* end of fs_publish.c */