2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 http://gnunet.org/encoding.php3
25 * @author Krista Bennett
26 * @author Christian Grothoff
29 * - code-sharing with unindex (write unindex code)
30 * - indexing cleanup: unindex on failure (can wait)
31 * - persistence support (can wait)
32 * - datastore reservation support (optimization)
33 * - location URIs (publish with anonymity-level zero)
37 #include "gnunet_constants.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_util_lib.h"
40 #include "gnunet_fs_service.h"
44 #define DEBUG_PUBLISH GNUNET_YES
47 * Maximum allowed size for a KBlock.
49 #define MAX_KBLOCK_SIZE 60000
52 * Maximum allowed size for an SBlock.
54 #define MAX_SBLOCK_SIZE 60000
57 * Main function that performs the upload.
58 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
59 * @param tc task context
63 const struct GNUNET_SCHEDULER_TaskContext *tc);
67 * Context for "ds_put_cont".
72 * Current publishing context.
74 struct GNUNET_FS_PublishContext *sc;
77 * Specific file with the block.
79 struct GNUNET_FS_FileInformation *p;
82 * Function to run next, if any (can be NULL).
84 GNUNET_SCHEDULER_Task cont;
94 * Fill in all of the generic fields for
97 * @param pc structure to fill in
98 * @param sc overall publishing context
99 * @param p file information for the file being published
100 * @param offset where in the file are we so far
103 make_publish_status (struct GNUNET_FS_ProgressInfo *pi,
104 struct GNUNET_FS_PublishContext *sc,
105 const struct GNUNET_FS_FileInformation *p,
108 pi->value.publish.sc = sc;
109 pi->value.publish.fi = p;
110 pi->value.publish.cctx
112 pi->value.publish.pctx
113 = (NULL == p->dir) ? NULL : p->dir->client_info;
114 pi->value.publish.size
115 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
116 pi->value.publish.eta
117 = GNUNET_TIME_calculate_eta (p->start_time,
119 pi->value.publish.size);
120 pi->value.publish.completed = offset;
121 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
122 pi->value.publish.anonymity = p->anonymity;
127 * Cleanup the publish context, we're done
130 * @param pc struct to clean up after
133 publish_cleanup (struct GNUNET_FS_PublishContext *sc)
135 GNUNET_FS_file_information_destroy (sc->fi, NULL, NULL);
136 GNUNET_FS_namespace_delete (sc->namespace, GNUNET_NO);
137 GNUNET_free_non_null (sc->nid);
138 GNUNET_free_non_null (sc->nuid);
139 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
145 * Function called by the datastore API with
146 * the result from the PUT request.
148 * @param cls our closure
149 * @param success GNUNET_OK on success
150 * @param msg error message (or NULL)
153 ds_put_cont (void *cls,
157 struct PutContCtx *pcc = cls;
158 struct GNUNET_FS_ProgressInfo pi;
160 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
162 /* we were aborted in the meantime,
164 publish_cleanup (pcc->sc);
167 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
168 pcc->sc->in_network_wait = GNUNET_NO;
169 if (GNUNET_OK != success)
171 GNUNET_asprintf (&pcc->p->emsg,
172 _("Upload failed: %s"),
174 GNUNET_FS_file_information_sync (pcc->p);
175 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
176 make_publish_status (&pi, pcc->sc, pcc->p, 0);
177 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
178 pi.value.publish.specifics.error.message = pcc->p->emsg;
180 = pcc->sc->h->upcb (pcc->sc->h->upcb_cls,
184 GNUNET_FS_file_information_sync (pcc->p);
185 if (NULL != pcc->cont)
187 = GNUNET_SCHEDULER_add_delayed (pcc->sc->h->sched,
189 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
190 GNUNET_SCHEDULER_NO_TASK,
191 GNUNET_TIME_UNIT_ZERO,
199 * Generate the callback that signals clients
200 * that a file (or directory) has been completely
203 * @param p the completed upload
204 * @param sc context of the publication
207 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
208 struct GNUNET_FS_PublishContext *sc)
210 struct GNUNET_FS_ProgressInfo pi;
212 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
213 make_publish_status (&pi, sc, p, p->chk_uri->data.chk.file_length);
214 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
215 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
217 = sc->h->upcb (sc->h->upcb_cls,
223 * Generate the callback that signals clients
224 * that a file (or directory) has encountered
225 * a problem during publication.
227 * @param p the upload that had trouble
228 * @param sc context of the publication
229 * @param emsg error message
232 signal_publish_error (struct GNUNET_FS_FileInformation *p,
233 struct GNUNET_FS_PublishContext *sc,
236 struct GNUNET_FS_ProgressInfo pi;
238 p->emsg = GNUNET_strdup (emsg);
239 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
240 make_publish_status (&pi, sc, p, 0);
241 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
242 pi.value.publish.specifics.error.message =emsg;
244 = sc->h->upcb (sc->h->upcb_cls,
250 * We've finished publishing the SBlock as part of a larger upload.
251 * Check the result and complete the larger upload.
253 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
254 * @param uri URI of the published SBlock
255 * @param emsg NULL on success, otherwise error message
258 publish_sblocks_cont (void *cls,
259 const struct GNUNET_FS_Uri *uri,
262 struct GNUNET_FS_PublishContext *sc = cls;
265 signal_publish_error (sc->fi,
270 // FIXME: release the datastore reserve here!
271 signal_publish_completion (sc->fi, sc);
276 * We are almost done publishing the structure,
277 * add SBlocks (if needed).
279 * @param sc overall upload data
282 publish_sblock (struct GNUNET_FS_PublishContext *sc)
284 if (NULL != sc->namespace)
285 GNUNET_FS_publish_sks (sc->h,
291 sc->fi->expirationTime,
295 &publish_sblocks_cont,
298 publish_sblocks_cont (sc, NULL, NULL);
303 * We've finished publishing a KBlock
304 * as part of a larger upload. Check
305 * the result and continue the larger
308 * @param cls the "struct GNUNET_FS_PublishContext*"
309 * of the larger upload
310 * @param uri URI of the published blocks
311 * @param emsg NULL on success, otherwise error message
314 publish_kblocks_cont (void *cls,
315 const struct GNUNET_FS_Uri *uri,
318 struct GNUNET_FS_PublishContext *sc = cls;
319 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
323 signal_publish_error (p, sc, emsg);
325 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
327 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
328 GNUNET_SCHEDULER_NO_TASK,
329 GNUNET_TIME_UNIT_ZERO,
334 GNUNET_FS_file_information_sync (p);
336 signal_publish_completion (p, sc);
337 /* move on to next file */
339 sc->fi_pos = p->next;
343 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
345 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
346 GNUNET_SCHEDULER_NO_TASK,
347 GNUNET_TIME_UNIT_ZERO,
354 * Function called by the tree encoder to obtain
355 * a block of plaintext data (for the lowest level
358 * @param cls our publishing context
359 * @param offset identifies which block to get
360 * @param max (maximum) number of bytes to get; returning
361 * fewer will also cause errors
362 * @param buf where to copy the plaintext buffer
363 * @param emsg location to store an error message (on error)
364 * @return number of bytes copied to buf, 0 on error
367 block_reader (void *cls,
373 struct GNUNET_FS_PublishContext *sc = cls;
374 struct GNUNET_FS_FileInformation *p;
381 pt_size = GNUNET_MIN(max,
382 p->data.dir.dir_size - offset);
383 dd = p->data.dir.dir_data;
390 pt_size = GNUNET_MIN(max,
391 p->data.file.file_size - offset);
393 p->data.file.reader (p->data.file.reader_cls,
405 * The tree encoder has finished processing a
406 * file. Call it's finish method and deal with
409 * @param cls our publishing context
410 * @param tc scheduler's task context (not used)
413 encode_cont (void *cls,
414 const struct GNUNET_SCHEDULER_TaskContext *tc)
416 struct GNUNET_FS_PublishContext *sc = cls;
417 struct GNUNET_FS_FileInformation *p;
418 struct GNUNET_FS_ProgressInfo pi;
422 GNUNET_FS_tree_encoder_finish (p->te,
428 GNUNET_asprintf (&p->emsg,
429 _("Upload failed: %s"),
432 GNUNET_FS_file_information_sync (p);
433 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
434 make_publish_status (&pi, sc, p, 0);
435 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
436 pi.value.publish.specifics.error.message = p->emsg;
438 = sc->h->upcb (sc->h->upcb_cls,
441 /* continue with main */
443 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
445 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
446 GNUNET_SCHEDULER_NO_TASK,
447 GNUNET_TIME_UNIT_ZERO,
454 * Function called asking for the current (encoded)
455 * block to be processed. After processing the
456 * client should either call "GNUNET_FS_tree_encode_next"
457 * or (on error) "GNUNET_FS_tree_encode_finish".
460 * @param query the query for the block (key for lookup in the datastore)
461 * @param type type of the block (IBLOCK or DBLOCK)
462 * @param block the (encrypted) block
463 * @param block_size size of block (in bytes)
466 block_proc (void *cls,
467 const GNUNET_HashCode *query,
473 struct GNUNET_FS_PublishContext *sc = cls;
474 struct GNUNET_FS_FileInformation *p;
475 struct PutContCtx * dpc_cls;
476 struct OnDemandBlock odb;
482 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
484 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
485 GNUNET_SCHEDULER_NO_TASK,
486 GNUNET_TIME_UNIT_ZERO,
492 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
493 sc->in_network_wait = GNUNET_YES;
494 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
495 dpc_cls->cont = &do_upload;
496 dpc_cls->cont_cls = sc;
498 if ( (p->is_directory) &&
499 (p->data.file.do_index) &&
500 (type == GNUNET_DATASTORE_BLOCKTYPE_DBLOCK) )
503 odb.file_id = p->data.file.file_id;
504 GNUNET_DATASTORE_put (sc->dsh,
507 sizeof(struct OnDemandBlock),
509 GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND,
513 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
518 GNUNET_DATASTORE_put (sc->dsh,
527 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
534 * Function called with information about our
535 * progress in computing the tree encoding.
538 * @param offset where are we in the file
539 * @param pt_block plaintext of the currently processed block
540 * @param pt_size size of pt_block
541 * @param depth depth of the block in the tree
544 progress_proc (void *cls,
546 const void *pt_block,
550 struct GNUNET_FS_PublishContext *sc = cls;
551 struct GNUNET_FS_FileInformation *p;
552 struct GNUNET_FS_ProgressInfo pi;
555 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
556 make_publish_status (&pi, sc, p, offset);
557 pi.value.publish.specifics.progress.data = pt_block;
558 pi.value.publish.specifics.progress.offset = offset;
559 pi.value.publish.specifics.progress.data_len = pt_size;
560 pi.value.publish.specifics.progress.depth = depth;
562 = sc->h->upcb (sc->h->upcb_cls,
568 * We are uploading a file or directory; load (if necessary) the next
569 * block into memory, encrypt it and send it to the FS service. Then
570 * continue with the main task.
572 * @param sc overall upload data
575 publish_content (struct GNUNET_FS_PublishContext *sc)
577 struct GNUNET_FS_FileInformation *p;
579 struct GNUNET_FS_DirectoryBuilder *db;
580 struct GNUNET_FS_FileInformation *dirpos;
589 db = GNUNET_FS_directory_builder_create (p->meta);
590 dirpos = p->data.dir.entries;
591 while (NULL != dirpos)
593 if (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,
609 dirpos->data.file.file_size,
613 GNUNET_free_non_null (emsg);
614 GNUNET_free (raw_data);
619 GNUNET_FS_directory_builder_add (db,
623 GNUNET_free_non_null (raw_data);
624 dirpos = dirpos->next;
626 GNUNET_FS_directory_builder_finish (db,
627 &p->data.dir.dir_size,
628 &p->data.dir.dir_data);
630 size = (p->is_directory)
631 ? p->data.dir.dir_size
632 : p->data.file.file_size;
633 p->te = GNUNET_FS_tree_encoder_create (sc->h,
642 GNUNET_FS_tree_encoder_next (p->te);
647 * Process the response (or lack thereof) from
648 * the "fs" service to our 'start index' request.
650 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
651 * @param msg the response we got
654 process_index_start_response (void *cls,
655 const struct GNUNET_MessageHeader *msg)
657 struct GNUNET_FS_PublishContext *sc = cls;
658 struct GNUNET_FS_FileInformation *p;
662 GNUNET_CLIENT_disconnect (sc->client);
667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668 _("Can not index file `%s': %s. Will try to insert instead.\n"),
669 p->data.file.filename,
670 _("timeout on index-start request to `fs' service"));
671 p->data.file.do_index = GNUNET_NO;
672 publish_content (sc);
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,
683 _("Can not index file `%s': %s. Will try to insert instead.\n"),
684 p->data.file.filename,
686 p->data.file.do_index = GNUNET_NO;
687 publish_content (sc);
690 /* success! continue with indexing */
691 publish_content (sc);
696 #include <sys/statvfs.h>
700 * Function called once the hash computation over an
701 * indexed file has completed.
703 * @param cls closure, our publishing context
704 * @param res resulting hash, NULL on error
707 hash_for_index_cb (void *cls,
708 const GNUNET_HashCode *
711 struct GNUNET_FS_PublishContext *sc = cls;
712 struct GNUNET_FS_FileInformation *p;
713 struct IndexStartMessage *ism;
715 struct GNUNET_CLIENT_Connection *client;
724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
725 _("Can not index file `%s': %s. Will try to insert instead.\n"),
726 p->data.file.filename,
727 _("failed to compute hash"));
728 p->data.file.do_index = GNUNET_NO;
729 publish_content (sc);
732 slen = strlen (p->data.file.filename) + 1;
733 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
735 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
736 _("Can not index file `%s': %s. Will try to insert instead.\n"),
737 p->data.file.filename,
738 _("filename too long"));
739 p->data.file.do_index = GNUNET_NO;
740 publish_content (sc);
743 client = GNUNET_CLIENT_connect (sc->h->sched,
748 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
749 _("Can not index file `%s': %s. Will try to insert instead.\n"),
750 p->data.file.filename,
751 _("could not connect to `fs' service"));
752 p->data.file.do_index = GNUNET_NO;
753 publish_content (sc);
756 p->data.file.file_id = *res;
757 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
759 ism->header.size = htons(sizeof(struct IndexStartMessage) +
761 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
762 /* FIXME: activate this on other OSes that
763 support it (or something very similar; make
764 sure to also adjust corresponding code
765 on the service-side) */
766 /* FIXME: the block below should probably be
767 abstracted into a function in the DISK API */
769 if ( (0 == stat(p->data.file.filename,
771 (0 == statvfs (p->data.file.filename,
774 ism->device = htonl ((uint32_t) fbuf.f_fsid);
775 ism->inode = GNUNET_htonll( (uint64_t) sbuf.st_ino);
779 p->data.file.filename,
782 GNUNET_CLIENT_transmit_and_get_response (client,
784 GNUNET_TIME_UNIT_FOREVER_REL,
785 &process_index_start_response,
792 * Main function that performs the upload.
793 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
794 * @param tc task context
797 do_upload (void *cls,
798 const struct GNUNET_SCHEDULER_TaskContext *tc)
800 struct GNUNET_FS_PublishContext *sc = cls;
801 struct GNUNET_FS_ProgressInfo pi;
802 struct GNUNET_FS_FileInformation *p;
805 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
809 /* upload of entire hierarchy complete,
810 publish namespace entries */
814 /* find starting position */
815 while ( (p->is_directory) &&
816 (NULL != p->data.dir.entries) &&
818 (NULL == p->data.dir.entries->chk_uri) )
820 p = p->data.dir.entries;
826 /* error with current file, abort all
827 related files as well! */
828 while (NULL != p->dir)
830 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
833 GNUNET_asprintf (&p->emsg,
834 _("Recursive upload failed at `%s'"),
837 GNUNET_FS_file_information_sync (p);
838 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
839 make_publish_status (&pi, sc, p, 0);
840 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
841 pi.value.publish.specifics.error.message = p->emsg;
843 = sc->h->upcb (sc->h->upcb_cls,
848 /* handle completion */
849 if (NULL != p->chk_uri)
851 /* upload of "p" complete, publish KBlocks! */
852 GNUNET_FS_publish_ksk (sc->h,
860 &publish_kblocks_cont,
864 if ( (!p->is_directory) &&
865 (p->data.file.do_index) )
867 if (NULL == p->data.file.filename)
869 p->data.file.do_index = GNUNET_NO;
870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
871 _("Can not index file `%s': %s. Will try to insert instead.\n"),
873 _("needs to be an actual file"));
874 publish_content (sc);
877 GNUNET_CRYPTO_hash_file (sc->h->sched,
878 GNUNET_SCHEDULER_PRIORITY_IDLE,
880 p->data.file.filename,
886 publish_content (sc);
891 * Signal the FS's progress function that we are starting
894 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
895 * @param fi the entry in the publish-structure
896 * @param length length of the file or directory
897 * @param meta metadata for the file or directory (can be modified)
898 * @param uri pointer to the keywords that will be used for this entry (can be modified)
899 * @param anonymity pointer to selected anonymity level (can be modified)
900 * @param priority pointer to selected priority (can be modified)
901 * @param expirationTime pointer to selected expiration time (can be modified)
902 * @param client_info pointer to client context set upon creation (can be modified)
903 * @return GNUNET_OK to continue (always)
906 fip_signal_start(void *cls,
907 struct GNUNET_FS_FileInformation *fi,
909 struct GNUNET_CONTAINER_MetaData *meta,
910 struct GNUNET_FS_Uri **uri,
911 unsigned int *anonymity,
912 unsigned int *priority,
913 struct GNUNET_TIME_Absolute *expirationTime,
916 struct GNUNET_FS_PublishContext *sc = cls;
917 struct GNUNET_FS_ProgressInfo pi;
919 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
920 make_publish_status (&pi, sc, fi, 0);
921 *client_info = sc->h->upcb (sc->h->upcb_cls,
928 * Publish a file or directory.
930 * @param h handle to the file sharing subsystem
931 * @param ctx initial value to use for the '*ctx'
932 * in the callback (for the GNUNET_FS_STATUS_PUBLISH_START event).
933 * @param fi information about the file or directory structure to publish
934 * @param namespace namespace to publish the file in, NULL for no namespace
935 * @param nid identifier to use for the publishd content in the namespace
936 * (can be NULL, must be NULL if namespace is NULL)
937 * @param nuid update-identifier that will be used for future updates
938 * (can be NULL, must be NULL if namespace or nid is NULL)
939 * @param options options for the publication
940 * @return context that can be used to control the publish operation
942 struct GNUNET_FS_PublishContext *
943 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
945 struct GNUNET_FS_FileInformation *fi,
946 struct GNUNET_FS_Namespace *namespace,
949 enum GNUNET_FS_PublishOptions options)
951 struct GNUNET_FS_PublishContext *ret;
952 struct GNUNET_DATASTORE_Handle *dsh;
954 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
956 dsh = GNUNET_DATASTORE_connect (h->cfg,
965 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
968 ret->client_ctx = ctx;
970 ret->namespace = namespace;
971 if (namespace != NULL)
974 GNUNET_assert (NULL != nid);
975 ret->nid = GNUNET_strdup (nid);
977 ret->nuid = GNUNET_strdup (nuid);
979 // FIXME: make upload persistent!
982 GNUNET_FS_file_information_inspect (ret->fi,
985 ret->fi_pos = ret->fi;
987 // FIXME: calculate space needed for "fi"
988 // and reserve as first task (then trigger
989 // "do_upload" from that continuation)!
991 = GNUNET_SCHEDULER_add_delayed (h->sched,
993 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
994 GNUNET_SCHEDULER_NO_TASK,
995 GNUNET_TIME_UNIT_ZERO,
1003 * Signal the FS's progress function that we are stopping
1006 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1007 * @param fi the entry in the publish-structure
1008 * @param length length of the file or directory
1009 * @param meta metadata for the file or directory (can be modified)
1010 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1011 * @param anonymity pointer to selected anonymity level (can be modified)
1012 * @param priority pointer to selected priority (can be modified)
1013 * @param expirationTime pointer to selected expiration time (can be modified)
1014 * @param client_info pointer to client context set upon creation (can be modified)
1015 * @return GNUNET_OK to continue (always)
1018 fip_signal_stop(void *cls,
1019 struct GNUNET_FS_FileInformation *fi,
1021 struct GNUNET_CONTAINER_MetaData *meta,
1022 struct GNUNET_FS_Uri **uri,
1023 unsigned int *anonymity,
1024 unsigned int *priority,
1025 struct GNUNET_TIME_Absolute *expirationTime,
1028 struct GNUNET_FS_PublishContext*sc = cls;
1029 struct GNUNET_FS_ProgressInfo pi;
1032 off = (fi->chk_uri == NULL) ? 0 : length;
1033 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1034 make_publish_status (&pi, sc, fi, off);
1035 GNUNET_break (NULL ==
1036 sc->h->upcb (sc->h->upcb_cls,
1038 *client_info = NULL;
1044 * Stop an upload. Will abort incomplete uploads (but
1045 * not remove blocks that have already been publishd) or
1046 * simply clean up the state for completed uploads.
1048 * @param sc context for the upload to stop
1051 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *sc)
1053 if (GNUNET_SCHEDULER_NO_TASK != sc->upload_task)
1054 GNUNET_SCHEDULER_cancel (sc->h->sched, sc->upload_task);
1055 // FIXME: remove from persistence DB (?) --- think more about
1056 // shutdown / persistent-resume APIs!!!
1057 GNUNET_FS_file_information_inspect (sc->fi,
1060 if (GNUNET_YES == sc->in_network_wait)
1062 sc->in_network_wait = GNUNET_SYSERR;
1065 publish_cleanup (sc);
1070 * Context for the KSK publication.
1072 struct PublishKskContext
1078 struct GNUNET_FS_Uri *ksk_uri;
1081 * Global FS context.
1083 struct GNUNET_FS_Handle *h;
1086 * The master block that we are sending
1087 * (in plaintext), has "mdsize+slen" more
1088 * bytes than the struct would suggest.
1093 * Buffer of the same size as "kb" for
1094 * the encrypted version.
1099 * Handle to the datastore, NULL if we are just
1102 struct GNUNET_DATASTORE_Handle *dsh;
1105 * Function to call once we're done.
1107 GNUNET_FS_PublishContinuation cont;
1115 * When should the KBlocks expire?
1117 struct GNUNET_TIME_Absolute expirationTime;
1120 * Size of the serialized metadata.
1125 * Size of the (CHK) URI as a string.
1130 * Keyword that we are currently processing.
1135 * Anonymity level for the KBlocks.
1137 unsigned int anonymity;
1140 * Priority for the KBlocks.
1142 unsigned int priority;
1147 * Continuation of "GNUNET_FS_publish_ksk" that performs
1148 * the actual publishing operation (iterating over all
1151 * @param cls closure of type "struct PublishKskContext*"
1155 publish_ksk_cont (void *cls,
1156 const struct GNUNET_SCHEDULER_TaskContext *tc);
1160 * Function called by the datastore API with
1161 * the result from the PUT request.
1163 * @param cls closure of type "struct PublishKskContext*"
1164 * @param success GNUNET_OK on success
1165 * @param msg error message (or NULL)
1168 kb_put_cont (void *cls,
1172 struct PublishKskContext *pkc = cls;
1174 if (GNUNET_OK != success)
1176 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1177 GNUNET_free (pkc->cpy);
1178 GNUNET_free (pkc->kb);
1179 pkc->cont (pkc->cont_cls,
1182 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1186 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1190 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1195 * Continuation of "GNUNET_FS_publish_ksk" that performs
1196 * the actual publishing operation (iterating over all
1199 * @param cls closure of type "struct PublishKskContext*"
1203 publish_ksk_cont (void *cls,
1204 const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 struct PublishKskContext *pkc = cls;
1207 const char *keyword;
1208 GNUNET_HashCode key;
1209 GNUNET_HashCode query;
1210 struct GNUNET_CRYPTO_AesSessionKey skey;
1211 struct GNUNET_CRYPTO_AesInitializationVector iv;
1212 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1215 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1216 (NULL == pkc->dsh) )
1218 if (NULL != pkc->dsh)
1219 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1220 GNUNET_free (pkc->cpy);
1221 GNUNET_free (pkc->kb);
1222 pkc->cont (pkc->cont_cls,
1225 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1229 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1230 /* first character of keyword indicates if it is
1231 mandatory or not -- ignore for hashing */
1232 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1233 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1234 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1235 pkc->slen + pkc->mdsize,
1239 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1240 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1241 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1242 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1244 GNUNET_assert (GNUNET_OK ==
1245 GNUNET_CRYPTO_rsa_sign (pk,
1247 &pkc->cpy->signature));
1248 GNUNET_CRYPTO_rsa_key_free (pk);
1249 GNUNET_DATASTORE_put (pkc->dsh,
1253 sizeof (struct KBlock) +
1256 GNUNET_DATASTORE_BLOCKTYPE_KBLOCK,
1259 pkc->expirationTime,
1260 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1267 * Publish a CHK under various keywords on GNUnet.
1269 * @param h handle to the file sharing subsystem
1270 * @param ksk_uri keywords to use
1271 * @param meta metadata to use
1272 * @param uri URI to refer to in the KBlock
1273 * @param expirationTime when the KBlock expires
1274 * @param anonymity anonymity level for the KBlock
1275 * @param priority priority for the KBlock
1276 * @param options publication options
1277 * @param cont continuation
1278 * @param cont_cls closure for cont
1281 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1282 struct GNUNET_FS_Uri *ksk_uri,
1283 struct GNUNET_CONTAINER_MetaData *meta,
1284 struct GNUNET_FS_Uri *uri,
1285 struct GNUNET_TIME_Absolute expirationTime,
1286 unsigned int anonymity,
1287 unsigned int priority,
1288 enum GNUNET_FS_PublishOptions options,
1289 GNUNET_FS_PublishContinuation cont,
1292 struct PublishKskContext *pkc;
1297 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1299 pkc->expirationTime = expirationTime;
1300 pkc->anonymity = anonymity;
1301 pkc->priority = priority;
1303 pkc->cont_cls = cont_cls;
1304 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1306 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1308 if (pkc->dsh == NULL)
1310 cont (cont_cls, NULL, _("Could not connect to datastore."));
1315 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1316 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1317 GNUNET_assert (pkc->mdsize >= 0);
1318 uris = GNUNET_FS_uri_to_string (uri);
1319 pkc->slen = strlen (uris) + 1;
1320 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1321 if (size > MAX_KBLOCK_SIZE)
1323 size = MAX_KBLOCK_SIZE;
1324 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1326 pkc->kb = GNUNET_malloc (size);
1327 kbe = (char *) &pkc->kb[1];
1328 memcpy (kbe, uris, pkc->slen);
1330 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1333 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1334 if (pkc->mdsize == -1)
1338 GNUNET_free (pkc->kb);
1339 if (pkc->dsh != NULL)
1340 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1341 cont (cont_cls, NULL, _("Internal error."));
1345 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1347 pkc->cpy = GNUNET_malloc (size);
1348 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1349 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1352 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1353 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1354 GNUNET_SCHEDULER_add_continuation (h->sched,
1358 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1363 * Context for the SKS publication.
1365 struct PublishSksContext
1369 * Global FS context.
1371 struct GNUNET_FS_Uri *uri;
1374 * Handle to the datastore.
1376 struct GNUNET_DATASTORE_Handle *dsh;
1379 * Function to call once we're done.
1381 GNUNET_FS_PublishContinuation cont;
1392 * Function called by the datastore API with
1393 * the result from the PUT (SBlock) request.
1395 * @param cls closure of type "struct PublishSksContext*"
1396 * @param success GNUNET_OK on success
1397 * @param msg error message (or NULL)
1400 sb_put_cont (void *cls,
1404 struct PublishSksContext *psc = cls;
1406 if (NULL != psc->dsh)
1407 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1408 if (GNUNET_OK != success)
1409 psc->cont (psc->cont_cls,
1413 psc->cont (psc->cont_cls,
1416 GNUNET_FS_uri_destroy (psc->uri);
1422 * Publish an SBlock on GNUnet.
1424 * @param h handle to the file sharing subsystem
1425 * @param namespace namespace to publish in
1426 * @param identifier identifier to use
1427 * @param update update identifier to use
1428 * @param meta metadata to use
1429 * @param uri URI to refer to in the SBlock
1430 * @param expirationTime when the SBlock expires
1431 * @param anonymity anonymity level for the SBlock
1432 * @param priority priority for the SBlock
1433 * @param options publication options
1434 * @param cont continuation
1435 * @param cont_cls closure for cont
1438 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1439 struct GNUNET_FS_Namespace *namespace,
1440 const char *identifier,
1442 struct GNUNET_CONTAINER_MetaData *meta,
1443 struct GNUNET_FS_Uri *uri,
1444 struct GNUNET_TIME_Absolute expirationTime,
1445 unsigned int anonymity,
1446 unsigned int priority,
1447 enum GNUNET_FS_PublishOptions options,
1448 GNUNET_FS_PublishContinuation cont,
1451 struct PublishSksContext *psc;
1452 struct GNUNET_CRYPTO_AesSessionKey sk;
1453 struct GNUNET_CRYPTO_AesInitializationVector iv;
1454 struct GNUNET_FS_Uri *sks_uri;
1462 struct SBlock *sb_enc;
1464 GNUNET_HashCode key; /* hash of thisId = key */
1465 GNUNET_HashCode id; /* hash of hc = identifier */
1467 uris = GNUNET_FS_uri_to_string (uri);
1468 slen = strlen (uris) + 1;
1469 idlen = strlen (identifier);
1472 nidlen = strlen (update) + 1;
1473 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1474 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1476 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1477 if (size > MAX_SBLOCK_SIZE)
1479 size = MAX_SBLOCK_SIZE;
1480 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1482 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1483 dest = (char *) &sb[1];
1484 memcpy (dest, update, nidlen);
1486 memcpy (dest, uris, slen);
1488 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1491 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1499 _("Internal error."));
1502 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1503 sb_enc = GNUNET_malloc (sizeof (struct SBlock) + size);
1504 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1505 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1506 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1507 sks_uri->type = sks;
1508 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1509 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1510 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1511 &sks_uri->data.sks.namespace);
1512 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1513 GNUNET_CRYPTO_hash_xor (&id,
1514 &sks_uri->data.sks.namespace,
1515 &sb_enc->identifier);
1516 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1517 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1518 size - sizeof (struct SBlock),
1523 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1524 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1525 + sizeof(struct SBlock)
1526 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1527 GNUNET_assert (GNUNET_OK ==
1528 GNUNET_CRYPTO_rsa_sign (namespace->key,
1530 &sb_enc->signature));
1531 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1534 psc->cont_cls = cont_cls;
1535 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1537 GNUNET_free (sb_enc);
1543 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1544 if (NULL == psc->dsh)
1546 GNUNET_free (sb_enc);
1549 _("Failed to connect to datastore."));
1553 GNUNET_DATASTORE_put (psc->dsh,
1558 GNUNET_DATASTORE_BLOCKTYPE_SBLOCK,
1562 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1565 GNUNET_free (sb_enc);
1569 /* end of fs_publish.c */