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);
89 GNUNET_FS_namespace_delete (pc->ns, GNUNET_NO);
92 GNUNET_free_non_null (pc->nid);
93 GNUNET_free_non_null (pc->nuid);
94 GNUNET_free_non_null (pc->serialization);
97 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
100 if (pc->client != NULL)
102 GNUNET_CLIENT_disconnect (pc->client);
105 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
111 * Function called by the datastore API with
112 * the result from the PUT request.
114 * @param cls the 'struct GNUNET_FS_PublishContext'
115 * @param success GNUNET_OK on success
116 * @param min_expiration minimum expiration time required for content to be stored
117 * @param msg error message (or NULL)
120 ds_put_cont (void *cls, int success,
121 struct GNUNET_TIME_Absolute min_expiration,
124 struct GNUNET_FS_PublishContext *pc = cls;
125 struct GNUNET_FS_ProgressInfo pi;
128 if (GNUNET_SYSERR == success)
130 GNUNET_asprintf (&pc->fi_pos->emsg, _("Publishing failed: %s"), msg);
131 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
132 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
133 pi.value.publish.specifics.error.message = pc->fi_pos->emsg;
134 pc->fi_pos->client_info =
135 GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi_pos, 0);
136 if ((pc->fi_pos->is_directory != GNUNET_YES) &&
137 (pc->fi_pos->filename != NULL) &&
138 (pc->fi_pos->data.file.do_index == GNUNET_YES))
140 /* run unindex to clean up */
141 GNUNET_FS_unindex_start (pc->h, pc->fi_pos->filename, NULL);
144 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
146 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
147 &GNUNET_FS_publish_main_, pc);
152 * Generate the callback that signals clients
153 * that a file (or directory) has been completely
156 * @param p the completed upload
157 * @param pc context of the publication
160 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
161 struct GNUNET_FS_PublishContext *pc)
163 struct GNUNET_FS_ProgressInfo pi;
165 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
166 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
167 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
169 GNUNET_FS_publish_make_status_ (&pi, pc, p,
170 GNUNET_ntohll (p->chk_uri->data.
176 * Generate the callback that signals clients
177 * that a file (or directory) has encountered
178 * a problem during publication.
180 * @param p the upload that had trouble
181 * @param pc context of the publication
182 * @param emsg error message
185 signal_publish_error (struct GNUNET_FS_FileInformation *p,
186 struct GNUNET_FS_PublishContext *pc, const char *emsg)
188 struct GNUNET_FS_ProgressInfo pi;
190 p->emsg = GNUNET_strdup (emsg);
191 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
192 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
193 pi.value.publish.specifics.error.message = emsg;
194 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
195 if ((p->is_directory != GNUNET_YES) && (p->filename != NULL) &&
196 (p->data.file.do_index == GNUNET_YES))
198 /* run unindex to clean up */
199 GNUNET_FS_unindex_start (pc->h, p->filename, NULL);
206 * Datastore returns from reservation cancel request.
208 * @param cls the 'struct GNUNET_FS_PublishContext'
209 * @param success success code (not used)
210 * @param min_expiration minimum expiration time required for content to be stored
211 * @param msg error message (typically NULL, not used)
214 finish_release_reserve (void *cls, int success,
215 struct GNUNET_TIME_Absolute min_expiration,
218 struct GNUNET_FS_PublishContext *pc = cls;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Releasing reserve done!\n");
222 signal_publish_completion (pc->fi, pc);
223 pc->all_done = GNUNET_YES;
224 GNUNET_FS_publish_sync_ (pc);
229 * We've finished publishing the SBlock as part of a larger upload.
230 * Check the result and complete the larger upload.
232 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
233 * @param uri URI of the published SBlock
234 * @param emsg NULL on success, otherwise error message
237 publish_sblocks_cont (void *cls, 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);
249 GNUNET_assert (pc->qre == NULL);
250 if ((pc->dsh != NULL) && (pc->rid != 0))
253 GNUNET_DATASTORE_release_reserve (pc->dsh, pc->rid, UINT_MAX, UINT_MAX,
254 GNUNET_TIME_UNIT_FOREVER_REL,
255 &finish_release_reserve, pc);
259 finish_release_reserve (pc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
265 * We are almost done publishing the structure,
266 * add SBlocks (if needed).
268 * @param pc overall upload data
271 publish_sblock (struct GNUNET_FS_PublishContext *pc)
274 pc->sks_pc = GNUNET_FS_publish_sks (pc->h, pc->ns, pc->nid, pc->nuid,
275 pc->fi->meta, pc->fi->chk_uri, &pc->fi->bo,
276 pc->options, &publish_sblocks_cont, pc);
278 publish_sblocks_cont (pc, NULL, NULL);
283 * We've finished publishing a KBlock as part of a larger upload.
284 * Check the result and continue the larger upload.
286 * @param cls the "struct GNUNET_FS_PublishContext*"
287 * of the larger upload
288 * @param uri URI of the published blocks
289 * @param emsg NULL on success, otherwise error message
292 publish_kblocks_cont (void *cls, const struct GNUNET_FS_Uri *uri,
295 struct GNUNET_FS_PublishContext *pc = cls;
296 struct GNUNET_FS_FileInformation *p = pc->fi_pos;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading KSK blocks: %s\n",
303 signal_publish_error (p, pc, emsg);
304 GNUNET_FS_file_information_sync_ (p);
305 GNUNET_FS_publish_sync_ (pc);
306 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
308 GNUNET_SCHEDULER_add_with_priority
309 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "KSK blocks published, moving on to next file\n");
315 signal_publish_completion (p, pc);
316 /* move on to next file */
318 pc->fi_pos = p->next;
321 GNUNET_FS_publish_sync_ (pc);
322 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
324 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
325 &GNUNET_FS_publish_main_, pc);
330 * Function called by the tree encoder to obtain
331 * a block of plaintext data (for the lowest level
334 * @param cls our publishing context
335 * @param offset identifies which block to get
336 * @param max (maximum) number of bytes to get; returning
337 * fewer will also cause errors
338 * @param buf where to copy the plaintext buffer
339 * @param emsg location to store an error message (on error)
340 * @return number of bytes copied to buf, 0 on error
343 block_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
345 struct GNUNET_FS_PublishContext *pc = cls;
346 struct GNUNET_FS_FileInformation *p;
351 if (p->is_directory == GNUNET_YES)
353 pt_size = GNUNET_MIN (max, p->data.dir.dir_size - offset);
354 dd = p->data.dir.dir_data;
355 memcpy (buf, &dd[offset], pt_size);
359 if (UINT64_MAX == offset)
360 return p->data.file.reader (p->data.file.reader_cls, offset, 0, NULL, NULL);
361 pt_size = GNUNET_MIN (max, p->data.file.file_size - offset);
363 return 0; /* calling reader with pt_size==0
364 * might free buf, so don't! */
366 p->data.file.reader (p->data.file.reader_cls, offset, pt_size, buf,
375 * The tree encoder has finished processing a
376 * file. Call it's finish method and deal with
379 * @param cls our publishing context
380 * @param tc scheduler's task context (not used)
383 encode_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
385 struct GNUNET_FS_PublishContext *pc = cls;
386 struct GNUNET_FS_FileInformation *p;
387 struct GNUNET_FS_ProgressInfo pi;
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished with tree encoder\n");
393 GNUNET_FS_tree_encoder_finish (p->te, &p->chk_uri, &emsg);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error during tree walk: %s\n", emsg);
398 GNUNET_asprintf (&p->emsg, _("Publishing failed: %s"), emsg);
400 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
401 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
402 pi.value.publish.specifics.error.message = p->emsg;
403 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
407 /* final progress event */
408 GNUNET_assert (NULL != p->chk_uri);
409 flen = GNUNET_FS_uri_chk_get_file_size (p->chk_uri);
410 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
411 pi.value.publish.specifics.progress.data = NULL;
412 pi.value.publish.specifics.progress.offset = flen;
413 pi.value.publish.specifics.progress.data_len = 0;
414 pi.value.publish.specifics.progress.depth = GNUNET_FS_compute_depth (flen);
415 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, flen);
417 GNUNET_FS_file_information_sync_ (p);
418 /* continue with main */
419 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
421 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
422 &GNUNET_FS_publish_main_, pc);
427 * Function called asking for the current (encoded)
428 * block to be processed. After processing the
429 * client should either call "GNUNET_FS_tree_encode_next"
430 * or (on error) "GNUNET_FS_tree_encode_finish".
433 * @param chk content hash key for the block
434 * @param offset offset of the block in the file
435 * @param depth depth of the block in the file, 0 for DBLOCK
436 * @param type type of the block (IBLOCK or DBLOCK)
437 * @param block the (encrypted) block
438 * @param block_size size of block (in bytes)
441 block_proc (void *cls, const struct ContentHashKey *chk, uint64_t offset,
442 unsigned int depth, enum GNUNET_BLOCK_Type type, const void *block,
445 struct GNUNET_FS_PublishContext *pc = cls;
446 struct GNUNET_FS_FileInformation *p;
447 struct OnDemandBlock odb;
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for datastore connection\n");
453 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
455 GNUNET_SCHEDULER_add_with_priority
456 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, pc);
460 if ((p->is_directory != GNUNET_YES) && (GNUNET_YES == p->data.file.do_index) &&
461 (type == GNUNET_BLOCK_TYPE_FS_DBLOCK))
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Indexing block `%s' for offset %llu with index size %u\n",
465 GNUNET_h2s (&chk->query), (unsigned long long) offset,
466 sizeof (struct OnDemandBlock));
467 odb.offset = GNUNET_htonll (offset);
468 odb.file_id = p->data.file.file_id;
469 GNUNET_assert (pc->qre == NULL);
471 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
472 &chk->query, sizeof (struct OnDemandBlock), &odb,
473 GNUNET_BLOCK_TYPE_FS_ONDEMAND,
474 p->bo.content_priority, p->bo.anonymity_level,
475 p->bo.replication_level, p->bo.expiration_time,
476 -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Publishing block `%s' for offset %llu with size %u\n",
482 GNUNET_h2s (&chk->query), (unsigned long long) offset,
483 (unsigned int) block_size);
484 GNUNET_assert (pc->qre == NULL);
486 GNUNET_DATASTORE_put (pc->dsh, (p->is_directory == GNUNET_YES) ? 0 : pc->rid,
487 &chk->query, block_size, block, type,
488 p->bo.content_priority, p->bo.anonymity_level,
489 p->bo.replication_level, p->bo.expiration_time, -2,
490 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT, &ds_put_cont,
496 * Function called with information about our
497 * progress in computing the tree encoding.
500 * @param offset where are we in the file
501 * @param pt_block plaintext of the currently processed block
502 * @param pt_size size of pt_block
503 * @param depth depth of the block in the tree, 0 for DBLOCK
506 progress_proc (void *cls, uint64_t offset, const void *pt_block, size_t pt_size,
509 struct GNUNET_FS_PublishContext *pc = cls;
510 struct GNUNET_FS_FileInformation *p;
511 struct GNUNET_FS_ProgressInfo pi;
514 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
515 pi.value.publish.specifics.progress.data = pt_block;
516 pi.value.publish.specifics.progress.offset = offset;
517 pi.value.publish.specifics.progress.data_len = pt_size;
518 pi.value.publish.specifics.progress.depth = depth;
519 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, offset);
524 * We are uploading a file or directory; load (if necessary) the next
525 * block into memory, encrypt it and send it to the FS service. Then
526 * continue with the main task.
528 * @param pc overall upload data
531 publish_content (struct GNUNET_FS_PublishContext *pc)
533 struct GNUNET_FS_FileInformation *p;
535 struct GNUNET_FS_DirectoryBuilder *db;
536 struct GNUNET_FS_FileInformation *dirpos;
541 GNUNET_assert (p != NULL);
544 if (p->is_directory == GNUNET_YES)
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating directory\n");
547 db = GNUNET_FS_directory_builder_create (p->meta);
548 dirpos = p->data.dir.entries;
549 while (NULL != dirpos)
551 if (dirpos->is_directory == GNUNET_YES)
553 raw_data = dirpos->data.dir.dir_data;
554 dirpos->data.dir.dir_data = NULL;
559 if ((dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
560 (dirpos->data.file.file_size > 0))
562 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
564 if (dirpos->data.file.file_size !=
565 dirpos->data.file.reader (dirpos->data.file.reader_cls, 0,
566 dirpos->data.file.file_size, raw_data,
569 GNUNET_free_non_null (emsg);
570 GNUNET_free (raw_data);
573 dirpos->data.file.reader (dirpos->data.file.reader_cls, UINT64_MAX, 0, 0, NULL);
576 GNUNET_FS_directory_builder_add (db, dirpos->chk_uri, dirpos->meta,
578 GNUNET_free_non_null (raw_data);
579 dirpos = dirpos->next;
581 GNUNET_free_non_null (p->data.dir.dir_data);
582 p->data.dir.dir_data = NULL;
583 p->data.dir.dir_size = 0;
584 GNUNET_FS_directory_builder_finish (db, &p->data.dir.dir_size,
585 &p->data.dir.dir_data);
586 GNUNET_FS_file_information_sync_ (p);
588 size = (p->is_directory == GNUNET_YES) ? p->data.dir.dir_size : p->data.file.file_size;
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating tree encoder\n");
591 GNUNET_FS_tree_encoder_create (pc->h, size, pc, &block_reader,
592 &block_proc, &progress_proc,
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing next block from tree\n");
597 GNUNET_FS_tree_encoder_next (p->te);
602 * Process the response (or lack thereof) from
603 * the "fs" service to our 'start index' request.
605 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
606 * @param msg the response we got
609 process_index_start_response (void *cls, const struct GNUNET_MessageHeader *msg)
611 struct GNUNET_FS_PublishContext *pc = cls;
612 struct GNUNET_FS_FileInformation *p;
616 GNUNET_CLIENT_disconnect (pc->client);
621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 ("Can not index file `%s': %s. Will try to insert instead.\n"),
625 _("timeout on index-start request to `fs' service"));
626 p->data.file.do_index = GNUNET_NO;
627 GNUNET_FS_file_information_sync_ (p);
628 publish_content (pc);
631 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
633 msize = ntohs (msg->size);
634 emsg = (const char *) &msg[1];
635 if ((msize <= sizeof (struct GNUNET_MessageHeader)) ||
636 (emsg[msize - sizeof (struct GNUNET_MessageHeader) - 1] != '\0'))
637 emsg = gettext_noop ("unknown error");
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
640 ("Can not index file `%s': %s. Will try to insert instead.\n"),
641 p->filename, gettext (emsg));
642 p->data.file.do_index = GNUNET_NO;
643 GNUNET_FS_file_information_sync_ (p);
644 publish_content (pc);
647 p->data.file.index_start_confirmed = GNUNET_YES;
648 /* success! continue with indexing */
649 GNUNET_FS_file_information_sync_ (p);
650 publish_content (pc);
655 * Function called once the hash computation over an
656 * indexed file has completed.
658 * @param cls closure, our publishing context
659 * @param res resulting hash, NULL on error
662 hash_for_index_cb (void *cls, const struct GNUNET_HashCode * res)
664 struct GNUNET_FS_PublishContext *pc = cls;
665 struct GNUNET_FS_FileInformation *p;
666 struct IndexStartMessage *ism;
668 struct GNUNET_CLIENT_Connection *client;
677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
679 ("Can not index file `%s': %s. Will try to insert instead.\n"),
680 p->filename, _("failed to compute hash"));
681 p->data.file.do_index = GNUNET_NO;
682 GNUNET_FS_file_information_sync_ (p);
683 publish_content (pc);
686 if (GNUNET_YES == p->data.file.index_start_confirmed)
688 publish_content (pc);
691 fn = GNUNET_STRINGS_filename_expand (p->filename);
692 GNUNET_assert (fn != NULL);
693 slen = strlen (fn) + 1;
695 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct IndexStartMessage))
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
699 ("Can not index file `%s': %s. Will try to insert instead.\n"),
700 fn, _("filename too long"));
702 p->data.file.do_index = GNUNET_NO;
703 GNUNET_FS_file_information_sync_ (p);
704 publish_content (pc);
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hash of indexed file `%s' is `%s'\n",
708 p->filename, GNUNET_h2s (res));
709 if (0 != (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
711 p->data.file.file_id = *res;
712 p->data.file.have_hash = GNUNET_YES;
713 p->data.file.index_start_confirmed = GNUNET_YES;
714 GNUNET_FS_file_information_sync_ (p);
715 publish_content (pc);
719 client = GNUNET_CLIENT_connect ("fs", pc->h->cfg);
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 ("Can not index file `%s': %s. Will try to insert instead.\n"),
725 p->filename, _("could not connect to `fs' service"));
726 p->data.file.do_index = GNUNET_NO;
727 publish_content (pc);
731 if (p->data.file.have_hash != GNUNET_YES)
733 p->data.file.file_id = *res;
734 p->data.file.have_hash = GNUNET_YES;
735 GNUNET_FS_file_information_sync_ (p);
737 ism = GNUNET_malloc (sizeof (struct IndexStartMessage) + slen);
738 ism->header.size = htons (sizeof (struct IndexStartMessage) + slen);
739 ism->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_START);
740 if (GNUNET_OK == GNUNET_DISK_file_get_identifiers (p->filename, &dev, &ino))
742 ism->device = GNUNET_htonll (dev);
743 ism->inode = GNUNET_htonll (ino);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 _("Failed to get file identifiers for `%s'\n"), p->filename);
751 memcpy (&ism[1], fn, slen);
754 GNUNET_break (GNUNET_YES ==
755 GNUNET_CLIENT_transmit_and_get_response (client, &ism->header,
756 GNUNET_TIME_UNIT_FOREVER_REL,
758 &process_index_start_response,
765 * Main function that performs the upload.
767 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
768 * @param tc task context
771 GNUNET_FS_publish_main_ (void *cls,
772 const struct GNUNET_SCHEDULER_TaskContext *tc)
774 struct GNUNET_FS_PublishContext *pc = cls;
775 struct GNUNET_FS_ProgressInfo pi;
776 struct GNUNET_FS_FileInformation *p;
777 struct GNUNET_FS_Uri *loc;
780 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Publishing complete, now publishing SKS and KSK blocks.\n");
786 /* upload of entire hierarchy complete,
787 * publish namespace entries */
788 GNUNET_FS_publish_sync_ (pc);
792 /* find starting position */
793 while ((p->is_directory == GNUNET_YES) && (NULL != p->data.dir.entries) && (NULL == p->emsg)
794 && (NULL == p->data.dir.entries->chk_uri))
796 p = p->data.dir.entries;
798 GNUNET_FS_publish_sync_ (pc);
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error uploading: %s\n", p->emsg);
804 /* error with current file, abort all
805 * related files as well! */
806 while (NULL != p->dir)
808 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
809 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
813 GNUNET_asprintf (&p->emsg, _("Recursive upload failed at `%s': %s"), fn,
819 GNUNET_asprintf (&p->emsg, _("Recursive upload failed: %s"), p->emsg);
821 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
822 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
823 pi.value.publish.specifics.error.message = p->emsg;
824 p->client_info = GNUNET_FS_publish_make_status_ (&pi, pc, p, 0);
826 pc->all_done = GNUNET_YES;
827 GNUNET_FS_publish_sync_ (pc);
830 /* handle completion */
831 if (NULL != p->chk_uri)
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "File upload complete, now publishing KSK blocks.\n");
835 if (0 == p->bo.anonymity_level)
837 /* zero anonymity, box CHK URI in LOC URI */
839 GNUNET_FS_uri_loc_create (p->chk_uri, pc->h->cfg,
840 p->bo.expiration_time);
841 GNUNET_FS_uri_destroy (p->chk_uri);
844 GNUNET_FS_publish_sync_ (pc);
845 /* upload of "p" complete, publish KBlocks! */
846 if (p->keywords != NULL)
848 pc->ksk_pc = GNUNET_FS_publish_ksk (pc->h, p->keywords, p->meta, p->chk_uri, &p->bo,
849 pc->options, &publish_kblocks_cont, pc);
853 publish_kblocks_cont (pc, p->chk_uri, NULL);
857 if ((p->is_directory != GNUNET_YES) && (p->data.file.do_index))
859 if (NULL == p->filename)
861 p->data.file.do_index = GNUNET_NO;
862 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
864 ("Can not index file `%s': %s. Will try to insert instead.\n"),
865 "<no-name>", _("needs to be an actual file"));
866 GNUNET_FS_file_information_sync_ (p);
867 publish_content (pc);
870 if (p->data.file.have_hash)
872 hash_for_index_cb (pc, &p->data.file.file_id);
876 p->start_time = GNUNET_TIME_absolute_get ();
878 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, p->filename,
879 HASHING_BLOCKSIZE, &hash_for_index_cb, pc);
883 publish_content (pc);
888 * Signal the FS's progress function that we are starting
891 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
892 * @param fi the entry in the publish-structure
893 * @param length length of the file or directory
894 * @param meta metadata for the file or directory (can be modified)
895 * @param uri pointer to the keywords that will be used for this entry (can be modified)
896 * @param bo block options
897 * @param do_index should we index?
898 * @param client_info pointer to client context set upon creation (can be modified)
899 * @return GNUNET_OK to continue (always)
902 fip_signal_start (void *cls, struct GNUNET_FS_FileInformation *fi,
903 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
904 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
905 int *do_index, void **client_info)
907 struct GNUNET_FS_PublishContext *pc = cls;
908 struct GNUNET_FS_ProgressInfo pi;
912 if (GNUNET_YES == pc->skip_next_fi_callback)
914 pc->skip_next_fi_callback = GNUNET_NO;
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting publish operation\n");
920 /* space for on-demand blocks */
922 ((length + DBLOCK_SIZE -
923 1) / DBLOCK_SIZE) * sizeof (struct OnDemandBlock);
927 /* space for DBlocks */
928 pc->reserve_space += length;
930 /* entries for IBlocks and DBlocks, space for IBlocks */
934 left = (left + DBLOCK_SIZE - 1) / DBLOCK_SIZE;
935 pc->reserve_entries += left;
938 left = left * sizeof (struct ContentHashKey);
939 pc->reserve_space += left;
941 pc->reserve_entries++;
942 /* entries and space for keywords */
945 kc = GNUNET_FS_uri_ksk_get_keyword_count (*uri);
946 pc->reserve_entries += kc;
947 pc->reserve_space += GNUNET_SERVER_MAX_MESSAGE_SIZE * kc;
949 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
950 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
951 GNUNET_FS_file_information_sync_ (fi);
952 if ((fi->is_directory) && (fi->dir != NULL))
954 /* We are a directory, and we are not top-level; process entries in directory */
955 pc->skip_next_fi_callback = GNUNET_YES;
956 GNUNET_FS_file_information_inspect (fi, &fip_signal_start, pc);
963 * Actually signal the FS's progress function that we are suspending
966 * @param fi the entry in the publish-structure
967 * @param pc the publish context of which a file is being suspended
970 suspend_operation (struct GNUNET_FS_FileInformation *fi,
971 struct GNUNET_FS_PublishContext *pc)
973 struct GNUNET_FS_ProgressInfo pi;
976 if (NULL != pc->ksk_pc)
978 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
981 if (NULL != pc->sks_pc)
983 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suspending publish operation\n");
987 GNUNET_free_non_null (fi->serialization);
988 fi->serialization = NULL;
989 off = (fi->chk_uri == NULL) ? 0 : (fi->is_directory == GNUNET_YES) ? fi->data.dir.dir_size : fi->data.file.file_size;
990 pi.status = GNUNET_FS_STATUS_PUBLISH_SUSPEND;
991 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
994 GNUNET_DATASTORE_cancel (pc->qre);
999 GNUNET_DATASTORE_disconnect (pc->dsh, GNUNET_NO);
1007 * Signal the FS's progress function that we are suspending
1008 * an upload. Performs the recursion.
1010 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1011 * @param fi the entry in the publish-structure
1012 * @param length length of the file or directory
1013 * @param meta metadata for the file or directory (can be modified)
1014 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1015 * @param bo block options
1016 * @param do_index should we index?
1017 * @param client_info pointer to client context set upon creation (can be modified)
1018 * @return GNUNET_OK to continue (always)
1021 fip_signal_suspend (void *cls, struct GNUNET_FS_FileInformation *fi,
1022 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1023 struct GNUNET_FS_Uri **uri,
1024 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1027 struct GNUNET_FS_PublishContext *pc = cls;
1029 if (GNUNET_YES == pc->skip_next_fi_callback)
1031 pc->skip_next_fi_callback = GNUNET_NO;
1034 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1036 /* process entries in directory */
1037 pc->skip_next_fi_callback = GNUNET_YES;
1038 GNUNET_FS_file_information_inspect (fi, &fip_signal_suspend, pc);
1040 suspend_operation (fi, pc);
1041 *client_info = NULL;
1047 * Create SUSPEND event for the given publish operation
1048 * and then clean up our state (without stop signal).
1050 * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
1053 GNUNET_FS_publish_signal_suspend_ (void *cls)
1055 struct GNUNET_FS_PublishContext *pc = cls;
1057 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1059 GNUNET_SCHEDULER_cancel (pc->upload_task);
1060 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1062 pc->skip_next_fi_callback = GNUNET_YES;
1063 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_suspend, pc);
1064 suspend_operation (pc->fi, pc);
1065 GNUNET_FS_end_top (pc->h, pc->top);
1067 publish_cleanup (pc);
1072 * We have gotten a reply for our space reservation request.
1073 * Either fail (insufficient space) or start publishing for good.
1075 * @param cls the 'struct GNUNET_FS_PublishContext*'
1076 * @param success positive reservation ID on success
1077 * @param min_expiration minimum expiration time required for content to be stored
1078 * @param msg error message on error, otherwise NULL
1081 finish_reserve (void *cls, int success,
1082 struct GNUNET_TIME_Absolute min_expiration,
1085 struct GNUNET_FS_PublishContext *pc = cls;
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation complete (%d)!\n", success);
1089 if ((msg != NULL) || (success <= 0))
1091 GNUNET_asprintf (&pc->fi->emsg, _("Insufficient space for publishing: %s"),
1093 signal_publish_error (pc->fi, pc, pc->fi->emsg);
1097 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1099 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1100 &GNUNET_FS_publish_main_, pc);
1105 * Publish a file or directory.
1107 * @param h handle to the file sharing subsystem
1108 * @param fi information about the file or directory structure to publish
1109 * @param ns namespace to publish the file in, NULL for no namespace
1110 * @param nid identifier to use for the publishd content in the namespace
1111 * (can be NULL, must be NULL if namespace is NULL)
1112 * @param nuid update-identifier that will be used for future updates
1113 * (can be NULL, must be NULL if namespace or nid is NULL)
1114 * @param options options for the publication
1115 * @return context that can be used to control the publish operation
1117 struct GNUNET_FS_PublishContext *
1118 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1119 struct GNUNET_FS_FileInformation *fi,
1120 struct GNUNET_FS_Namespace *ns, const char *nid,
1122 enum GNUNET_FS_PublishOptions options)
1124 struct GNUNET_FS_PublishContext *ret;
1125 struct GNUNET_DATASTORE_Handle *dsh;
1127 GNUNET_assert (NULL != h);
1128 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1130 dsh = GNUNET_DATASTORE_connect (h->cfg);
1138 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1143 ret->options = options;
1147 GNUNET_assert (NULL != nid);
1148 ret->nid = GNUNET_strdup (nid);
1150 ret->nuid = GNUNET_strdup (nuid);
1153 GNUNET_FS_file_information_inspect (ret->fi, &fip_signal_start, ret);
1154 ret->fi_pos = ret->fi;
1155 ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, ret);
1156 GNUNET_FS_publish_sync_ (ret);
1157 if (NULL != ret->dsh)
1159 GNUNET_assert (NULL == ret->qre);
1160 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1162 ("Reserving space for %u entries and %llu bytes for publication\n"),
1163 (unsigned int) ret->reserve_entries,
1164 (unsigned long long) ret->reserve_space);
1166 GNUNET_DATASTORE_reserve (ret->dsh, ret->reserve_space,
1167 ret->reserve_entries, UINT_MAX, UINT_MAX,
1168 GNUNET_TIME_UNIT_FOREVER_REL, &finish_reserve,
1173 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ret->upload_task);
1175 GNUNET_SCHEDULER_add_with_priority
1176 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND, &GNUNET_FS_publish_main_, ret);
1183 * Signal the FS's progress function that we are stopping
1186 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1187 * @param fi the entry in the publish-structure
1188 * @param length length of the file or directory
1189 * @param meta metadata for the file or directory (can be modified)
1190 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1191 * @param bo block options (can be modified)
1192 * @param do_index should we index?
1193 * @param client_info pointer to client context set upon creation (can be modified)
1194 * @return GNUNET_OK to continue (always)
1197 fip_signal_stop (void *cls, struct GNUNET_FS_FileInformation *fi,
1198 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1199 struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo,
1200 int *do_index, void **client_info)
1202 struct GNUNET_FS_PublishContext *pc = cls;
1203 struct GNUNET_FS_ProgressInfo pi;
1206 if (GNUNET_YES == pc->skip_next_fi_callback)
1208 pc->skip_next_fi_callback = GNUNET_NO;
1211 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1213 /* process entries in directory first */
1214 pc->skip_next_fi_callback = GNUNET_YES;
1215 GNUNET_FS_file_information_inspect (fi, &fip_signal_stop, pc);
1217 if (fi->serialization != NULL)
1219 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1221 GNUNET_free (fi->serialization);
1222 fi->serialization = NULL;
1224 off = (fi->chk_uri == NULL) ? 0 : length;
1225 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1226 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, fi, off));
1227 *client_info = NULL;
1233 * Stop an upload. Will abort incomplete uploads (but
1234 * not remove blocks that have already been publishd) or
1235 * simply clean up the state for completed uploads.
1236 * Must NOT be called from within the event callback!
1238 * @param pc context for the upload to stop
1241 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc)
1243 struct GNUNET_FS_ProgressInfo pi;
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publish stop called\n");
1247 GNUNET_FS_end_top (pc->h, pc->top);
1248 if (NULL != pc->ksk_pc)
1250 GNUNET_FS_publish_ksk_cancel (pc->ksk_pc);
1253 if (NULL != pc->sks_pc)
1255 GNUNET_FS_publish_sks_cancel (pc->sks_pc);
1258 if (GNUNET_SCHEDULER_NO_TASK != pc->upload_task)
1260 GNUNET_SCHEDULER_cancel (pc->upload_task);
1261 pc->upload_task = GNUNET_SCHEDULER_NO_TASK;
1263 pc->skip_next_fi_callback = GNUNET_YES;
1264 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_stop, pc);
1266 if (pc->fi->serialization != NULL)
1268 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1269 pc->fi->serialization);
1270 GNUNET_free (pc->fi->serialization);
1271 pc->fi->serialization = NULL;
1273 off = (pc->fi->chk_uri == NULL) ? 0 : GNUNET_ntohll (pc->fi->chk_uri->data.chk.file_length);
1275 if (pc->serialization != NULL)
1277 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1279 GNUNET_free (pc->serialization);
1280 pc->serialization = NULL;
1282 if (NULL != pc->qre)
1284 GNUNET_DATASTORE_cancel (pc->qre);
1287 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1288 GNUNET_break (NULL == GNUNET_FS_publish_make_status_ (&pi, pc, pc->fi, off));
1289 publish_cleanup (pc);
1294 /* end of fs_publish.c */