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 * - indexing cleanup: unindex on failure (can wait)
30 * - code-sharing with unindex (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"
43 #define DEBUG_PUBLISH GNUNET_YES
46 * Maximum allowed size for a KBlock.
48 #define MAX_KBLOCK_SIZE 60000
51 * Maximum allowed size for an SBlock.
53 #define MAX_SBLOCK_SIZE 60000
56 * Blocksize to use when hashing files
57 * for indexing (blocksize for IO, not for
58 * the DBlocks). Larger blocksizes can
59 * be more efficient but will be more disruptive
60 * as far as the scheduler is concerned.
62 #define HASHING_BLOCKSIZE (1024 * 1024)
65 * Main function that performs the upload.
66 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
67 * @param tc task context
71 const struct GNUNET_SCHEDULER_TaskContext *tc);
75 * Context for "ds_put_cont".
80 * Current publishing context.
82 struct GNUNET_FS_PublishContext *sc;
85 * Specific file with the block.
87 struct GNUNET_FS_FileInformation *p;
90 * Function to run next, if any (can be NULL).
92 GNUNET_SCHEDULER_Task cont;
102 * Fill in all of the generic fields for
105 * @param pc structure to fill in
106 * @param sc overall publishing context
107 * @param p file information for the file being published
110 make_publish_status (struct GNUNET_FS_ProgressInfo *pi,
111 struct GNUNET_FS_PublishContext *sc,
112 const struct GNUNET_FS_FileInformation *p)
114 pi->value.publish.sc = sc;
115 pi->value.publish.fi = p;
116 pi->value.publish.cctx
118 pi->value.publish.pctx
119 = (NULL == p->dir) ? NULL : p->dir->client_info;
120 pi->value.publish.size
121 = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
122 pi->value.publish.eta
123 = GNUNET_TIME_calculate_eta (p->start_time,
125 pi->value.publish.size);
126 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (p->start_time);
127 pi->value.publish.completed = p->publish_offset;
128 pi->value.publish.anonymity = p->anonymity;
133 * Cleanup the publish context, we're done
136 * @param pc struct to clean up after
139 publish_cleanup (struct GNUNET_FS_PublishContext *sc)
141 GNUNET_FS_file_information_destroy (sc->fi, NULL, NULL);
142 GNUNET_FS_namespace_delete (sc->namespace, GNUNET_NO);
143 GNUNET_free_non_null (sc->nid);
144 GNUNET_free_non_null (sc->nuid);
145 GNUNET_DATASTORE_disconnect (sc->dsh, GNUNET_NO);
151 * Function called by the datastore API with
152 * the result from the PUT request.
154 * @param cls our closure
155 * @param success GNUNET_OK on success
156 * @param msg error message (or NULL)
159 ds_put_cont (void *cls,
163 struct PutContCtx *pcc = cls;
164 struct GNUNET_FS_ProgressInfo pi;
166 if (GNUNET_SYSERR == pcc->sc->in_network_wait)
168 /* we were aborted in the meantime,
170 publish_cleanup (pcc->sc);
173 GNUNET_assert (GNUNET_YES == pcc->sc->in_network_wait);
174 pcc->sc->in_network_wait = GNUNET_NO;
175 if (GNUNET_OK != success)
177 GNUNET_asprintf (&pcc->p->emsg,
178 _("Upload failed: %s"),
180 GNUNET_FS_file_information_sync (pcc->p);
181 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
182 make_publish_status (&pi, pcc->sc, pcc->p);
183 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
184 pi.value.publish.specifics.error.message = pcc->p->emsg;
186 = pcc->sc->h->upcb (pcc->sc->h->upcb_cls,
190 GNUNET_FS_file_information_sync (pcc->p);
191 if (NULL != pcc->cont)
193 = GNUNET_SCHEDULER_add_delayed (pcc->sc->h->sched,
195 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
196 GNUNET_SCHEDULER_NO_TASK,
197 GNUNET_TIME_UNIT_ZERO,
205 * Generate the callback that signals clients
206 * that a file (or directory) has been completely
209 * @param p the completed upload
210 * @param sc context of the publication
213 signal_publish_completion (struct GNUNET_FS_FileInformation *p,
214 struct GNUNET_FS_PublishContext *sc)
216 struct GNUNET_FS_ProgressInfo pi;
218 pi.status = GNUNET_FS_STATUS_PUBLISH_COMPLETED;
219 make_publish_status (&pi, sc, p);
220 pi.value.publish.eta = GNUNET_TIME_UNIT_ZERO;
221 pi.value.publish.specifics.completed.chk_uri = p->chk_uri;
223 = sc->h->upcb (sc->h->upcb_cls,
229 * Generate the callback that signals clients
230 * that a file (or directory) has encountered
231 * a problem during publication.
233 * @param p the upload that had trouble
234 * @param sc context of the publication
235 * @param emsg error message
238 signal_publish_error (struct GNUNET_FS_FileInformation *p,
239 struct GNUNET_FS_PublishContext *sc,
242 struct GNUNET_FS_ProgressInfo pi;
244 p->emsg = GNUNET_strdup (emsg);
245 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
246 make_publish_status (&pi, sc, p);
247 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
248 pi.value.publish.specifics.error.message =emsg;
250 = sc->h->upcb (sc->h->upcb_cls,
256 * We've finished publishing the SBlock as part of a larger upload.
257 * Check the result and complete the larger upload.
259 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
260 * @param uri URI of the published SBlock
261 * @param emsg NULL on success, otherwise error message
264 publish_sblocks_cont (void *cls,
265 const struct GNUNET_FS_Uri *uri,
268 struct GNUNET_FS_PublishContext *sc = cls;
271 signal_publish_error (sc->fi,
276 // FIXME: release the datastore reserve here!
277 signal_publish_completion (sc->fi, sc);
282 * We are almost done publishing the structure,
283 * add SBlocks (if needed).
285 * @param sc overall upload data
288 publish_sblock (struct GNUNET_FS_PublishContext *sc)
290 if (NULL != sc->namespace)
291 GNUNET_FS_publish_sks (sc->h,
297 sc->fi->expirationTime,
301 &publish_sblocks_cont,
304 publish_sblocks_cont (sc, NULL, NULL);
309 * We've finished publishing a KBlock
310 * as part of a larger upload. Check
311 * the result and continue the larger
314 * @param cls the "struct GNUNET_FS_PublishContext*" of the larger upload
315 * @param uri URI of the published blocks
316 * @param emsg NULL on success, otherwise error message
319 publish_kblocks_cont (void *cls,
320 const struct GNUNET_FS_Uri *uri,
323 struct GNUNET_FS_PublishContext *sc = cls;
324 struct GNUNET_FS_FileInformation *p = sc->fi_pos;
328 signal_publish_error (p, sc, emsg);
330 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
332 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
333 GNUNET_SCHEDULER_NO_TASK,
334 GNUNET_TIME_UNIT_ZERO,
339 GNUNET_FS_file_information_sync (p);
341 signal_publish_completion (p, sc);
342 /* move on to next file */
344 sc->fi_pos = p->next;
348 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
350 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
351 GNUNET_SCHEDULER_NO_TASK,
352 GNUNET_TIME_UNIT_ZERO,
359 * Compute the depth of the CHK tree.
361 * @param flen file length for which to compute the depth
362 * @return depth of the tree
365 compute_depth (uint64_t flen)
367 unsigned int treeDepth;
375 if (fl * CHK_PER_INODE < fl)
377 /* integer overflow, this is a HUGE file... */
380 fl = fl * CHK_PER_INODE;
387 * Compute the size of the current IBlock.
389 * @param height height of the IBlock in the tree (aka overall
390 * number of tree levels minus depth); 0 == DBlock
391 * @param offset current offset in the overall file
392 * @return size of the corresponding IBlock
395 compute_iblock_size (unsigned int height,
403 GNUNET_assert (height > 0);
404 bds = DBLOCK_SIZE; /* number of bytes each CHK at level "i"
406 for (i=0;i<height;i++)
407 bds *= CHK_PER_INODE;
411 /* we were triggered at the end of a full block */
416 /* we were triggered at the end of the file */
417 bds /= CHK_PER_INODE;
422 return (uint16_t) (ret * sizeof(struct ContentHashKey));
427 * Compute the offset of the CHK for the
428 * current block in the IBlock above.
430 * @param height height of the IBlock in the tree (aka overall
431 * number of tree levels minus depth); 0 == DBlock
432 * @param offset current offset in the overall file
433 * @return (array of CHKs') offset in the above IBlock
436 compute_chk_offset (unsigned int height,
443 bds = DBLOCK_SIZE; /* number of bytes each CHK at level "i"
445 for (i=0;i<height;i++)
446 bds *= CHK_PER_INODE;
447 GNUNET_assert (0 == (offset % bds));
449 return ret % CHK_PER_INODE;
454 * We are uploading a file or directory; load (if necessary) the next
455 * block into memory, encrypt it and send it to the FS service. Then
456 * continue with the main task.
458 * @param sc overall upload data
459 * @param p specific file or directory for which kblocks
463 publish_content (struct GNUNET_FS_PublishContext *sc,
464 struct GNUNET_FS_FileInformation *p)
466 struct GNUNET_FS_ProgressInfo pi;
467 struct ContentHashKey *mychk;
468 const void *pt_block;
471 char iob[DBLOCK_SIZE];
472 char enc[DBLOCK_SIZE];
473 struct GNUNET_CRYPTO_AesSessionKey sk;
474 struct GNUNET_CRYPTO_AesInitializationVector iv;
477 struct GNUNET_FS_DirectoryBuilder *db;
478 struct GNUNET_FS_FileInformation *dirpos;
481 struct PutContCtx * dpc_cls;
482 struct OnDemandBlock odb;
484 // FIXME: figure out how to share this code
486 size = (p->is_directory) ? p->data.dir.dir_size : p->data.file.file_size;
487 if (NULL == p->chk_tree)
491 db = GNUNET_FS_directory_builder_create (p->meta);
492 dirpos = p->data.dir.entries;
493 while (NULL != dirpos)
495 if (dirpos->is_directory)
497 raw_data = dirpos->data.dir.dir_data;
498 dirpos->data.dir.dir_data = NULL;
503 if ( (dirpos->data.file.file_size < MAX_INLINE_SIZE) &&
504 (dirpos->data.file.file_size > 0) )
506 raw_data = GNUNET_malloc (dirpos->data.file.file_size);
508 if (dirpos->data.file.file_size !=
509 dirpos->data.file.reader (dirpos->data.file.reader_cls,
511 dirpos->data.file.file_size,
515 GNUNET_free_non_null (emsg);
516 GNUNET_free (raw_data);
521 GNUNET_FS_directory_builder_add (db,
525 GNUNET_free_non_null (raw_data);
526 dirpos = dirpos->next;
528 GNUNET_FS_directory_builder_finish (db,
529 &p->data.dir.dir_size,
530 &p->data.dir.dir_data);
531 size = p->data.dir.dir_size;
533 p->chk_tree_depth = compute_depth (size);
534 p->chk_tree = GNUNET_malloc (p->chk_tree_depth *
535 sizeof (struct ContentHashKey) *
537 p->current_depth = p->chk_tree_depth;
539 if (p->current_depth == p->chk_tree_depth)
543 pt_size = GNUNET_MIN(DBLOCK_SIZE,
544 p->data.dir.dir_size - p->publish_offset);
545 dd = p->data.dir.dir_data;
546 pt_block = &dd[p->publish_offset];
550 pt_size = GNUNET_MIN(DBLOCK_SIZE,
551 p->data.file.file_size - p->publish_offset);
554 p->data.file.reader (p->data.file.reader_cls,
560 GNUNET_asprintf (&p->emsg,
561 _("Upload failed: %s"),
564 GNUNET_FS_file_information_sync (p);
565 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
566 make_publish_status (&pi, sc, p);
567 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
568 pi.value.publish.specifics.error.message = p->emsg;
570 = sc->h->upcb (sc->h->upcb_cls,
572 /* continue with main (to propagate error up) */
574 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
576 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
577 GNUNET_SCHEDULER_NO_TASK,
578 GNUNET_TIME_UNIT_ZERO,
588 pt_size = compute_iblock_size (p->chk_tree_depth - p->current_depth,
590 pt_block = &p->chk_tree[p->current_depth *
593 off = compute_chk_offset (p->chk_tree_depth - p->current_depth,
595 mychk = &p->chk_tree[(p->current_depth-1)*CHK_PER_INODE+off];
596 GNUNET_CRYPTO_hash (pt_block, pt_size, &mychk->key);
597 GNUNET_CRYPTO_hash_to_aes_key (&mychk->key, &sk, &iv);
598 GNUNET_CRYPTO_aes_encrypt (pt_block,
603 // NOTE: this block below is all that really differs
604 // between publish/unindex! Parameterize & move this code!
608 = GNUNET_SCHEDULER_add_delayed (sc->h->sched,
610 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
611 GNUNET_SCHEDULER_NO_TASK,
612 GNUNET_TIME_UNIT_ZERO,
618 GNUNET_assert (GNUNET_NO == sc->in_network_wait);
619 sc->in_network_wait = GNUNET_YES;
620 dpc_cls = GNUNET_malloc(sizeof(struct PutContCtx));
621 dpc_cls->cont = &do_upload;
622 dpc_cls->cont_cls = sc;
624 if ( (p->is_directory) &&
625 (p->data.file.do_index) &&
626 (p->current_depth == p->chk_tree_depth) )
628 odb.offset = p->publish_offset;
629 odb.file_id = p->data.file.file_id;
630 GNUNET_DATASTORE_put (sc->dsh,
633 sizeof(struct OnDemandBlock),
635 GNUNET_DATASTORE_BLOCKTYPE_ONDEMAND,
639 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
645 GNUNET_DATASTORE_put (sc->dsh,
650 (p->current_depth == p->chk_tree_depth)
651 ? GNUNET_DATASTORE_BLOCKTYPE_DBLOCK
652 : GNUNET_DATASTORE_BLOCKTYPE_IBLOCK,
656 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
661 if (p->current_depth == p->chk_tree_depth)
663 pi.status = GNUNET_FS_STATUS_PUBLISH_PROGRESS;
664 make_publish_status (&pi, sc, p);
665 pi.value.publish.specifics.progress.data = pt_block;
666 pi.value.publish.specifics.progress.offset = p->publish_offset;
667 pi.value.publish.specifics.progress.data_len = pt_size;
669 = sc->h->upcb (sc->h->upcb_cls,
672 GNUNET_CRYPTO_hash (enc, pt_size, &mychk->query);
673 if (p->current_depth == p->chk_tree_depth)
675 p->publish_offset += pt_size;
676 if ( (p->publish_offset == size) ||
677 (0 == p->publish_offset % (CHK_PER_INODE * DBLOCK_SIZE) ) )
682 if ( (off == CHK_PER_INODE) ||
683 (p->publish_offset == size) )
686 p->current_depth = p->chk_tree_depth;
688 if (0 == p->current_depth)
690 p->chk_uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
691 p->chk_uri->type = chk;
692 p->chk_uri->data.chk.chk = p->chk_tree[0];
693 p->chk_uri->data.chk.file_length = size;
694 GNUNET_free (p->chk_tree);
703 * Process the response (or lack thereof) from
704 * the "fs" service to our 'start index' request.
706 * @param cls closure (of type "struct GNUNET_FS_PublishContext*"_)
707 * @param msg the response we got
710 process_index_start_response (void *cls,
711 const struct GNUNET_MessageHeader *msg)
713 struct GNUNET_FS_PublishContext *sc = cls;
714 struct GNUNET_FS_FileInformation *p;
718 GNUNET_CLIENT_disconnect (sc->client);
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 _("Can not index file `%s': %s. Will try to insert instead.\n"),
725 p->data.file.filename,
726 _("timeout on index-start request to `fs' service"));
727 p->data.file.do_index = GNUNET_NO;
728 publish_content (sc, p);
731 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK)
733 msize = ntohs (msg->size);
734 emsg = (const char *) &msg[1];
735 if ( (msize <= sizeof (struct GNUNET_MessageHeader)) ||
736 (emsg[msize - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') )
737 emsg = gettext_noop ("unknown error");
738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
739 _("Can not index file `%s': %s. Will try to insert instead.\n"),
740 p->data.file.filename,
742 p->data.file.do_index = GNUNET_NO;
743 publish_content (sc, p);
746 /* success! continue with indexing */
747 publish_content (sc, p);
752 #include <sys/statvfs.h>
756 * Function called once the hash computation over an
757 * indexed file has completed.
759 * @param cls closure, our publishing context
760 * @param res resulting hash, NULL on error
763 hash_for_index_cb (void *cls,
764 const GNUNET_HashCode *
767 struct GNUNET_FS_PublishContext *sc = cls;
768 struct GNUNET_FS_FileInformation *p;
769 struct IndexStartMessage *ism;
771 struct GNUNET_CLIENT_Connection *client;
780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
781 _("Can not index file `%s': %s. Will try to insert instead.\n"),
782 p->data.file.filename,
783 _("failed to compute hash"));
784 p->data.file.do_index = GNUNET_NO;
785 publish_content (sc, p);
788 slen = strlen (p->data.file.filename) + 1;
789 if (slen > GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct IndexStartMessage))
791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
792 _("Can not index file `%s': %s. Will try to insert instead.\n"),
793 p->data.file.filename,
794 _("filename too long"));
795 p->data.file.do_index = GNUNET_NO;
796 publish_content (sc, p);
799 client = GNUNET_CLIENT_connect (sc->h->sched,
804 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
805 _("Can not index file `%s': %s. Will try to insert instead.\n"),
806 p->data.file.filename,
807 _("could not connect to `fs' service"));
808 p->data.file.do_index = GNUNET_NO;
809 publish_content (sc, p);
812 p->data.file.file_id = *res;
813 ism = GNUNET_malloc (sizeof(struct IndexStartMessage) +
815 ism->header.size = htons(sizeof(struct IndexStartMessage) +
817 ism->header.type = htons(GNUNET_MESSAGE_TYPE_FS_INDEX_START);
818 /* FIXME: activate this on other OSes that
819 support it (or something very similar; make
820 sure to also adjust corresponding code
821 on the service-side) */
822 /* FIXME: the block below should probably be
823 abstracted into a function in the DISK API */
825 if ( (0 == stat(p->data.file.filename,
827 (0 == statvfs (p->data.file.filename,
830 ism->device = htonl ((uint32_t) fbuf.f_fsid);
831 ism->inode = GNUNET_htonll( (uint64_t) sbuf.st_ino);
835 p->data.file.filename,
838 GNUNET_CLIENT_transmit_and_get_response (client,
840 GNUNET_TIME_UNIT_FOREVER_REL,
841 &process_index_start_response,
848 * Main function that performs the upload.
849 * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
850 * @param tc task context
853 do_upload (void *cls,
854 const struct GNUNET_SCHEDULER_TaskContext *tc)
856 struct GNUNET_FS_PublishContext *sc = cls;
857 struct GNUNET_FS_ProgressInfo pi;
858 struct GNUNET_FS_FileInformation *p;
861 sc->upload_task = GNUNET_SCHEDULER_NO_TASK;
865 /* upload of entire hierarchy complete,
866 publish namespace entries */
870 /* find starting position */
871 while ( (p->is_directory) &&
872 (NULL != p->data.dir.entries) &&
874 (NULL == p->data.dir.entries->chk_uri) )
876 p = p->data.dir.entries;
882 /* error with current file, abort all
883 related files as well! */
884 while (NULL != p->dir)
886 fn = GNUNET_CONTAINER_meta_data_get_by_type (p->meta,
889 GNUNET_asprintf (&p->emsg,
890 _("Recursive upload failed at `%s'"),
893 GNUNET_FS_file_information_sync (p);
894 pi.status = GNUNET_FS_STATUS_PUBLISH_ERROR;
895 make_publish_status (&pi, sc, p);
896 pi.value.publish.eta = GNUNET_TIME_UNIT_FOREVER_REL;
897 pi.value.publish.specifics.error.message = p->emsg;
899 = sc->h->upcb (sc->h->upcb_cls,
904 /* handle completion */
905 if (NULL != p->chk_uri)
907 /* upload of "p" complete, publish KBlocks! */
908 GNUNET_FS_publish_ksk (sc->h,
916 &publish_kblocks_cont,
920 if ( (!p->is_directory) &&
921 (p->data.file.do_index) )
923 if (NULL == p->data.file.filename)
925 p->data.file.do_index = GNUNET_NO;
926 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
927 _("Can not index file `%s': %s. Will try to insert instead.\n"),
929 _("needs to be an actual file"));
930 publish_content (sc, p);
933 GNUNET_CRYPTO_hash_file (sc->h->sched,
934 GNUNET_SCHEDULER_PRIORITY_IDLE,
936 p->data.file.filename,
942 publish_content (sc, p);
947 * Signal the FS's progress function that we are starting
950 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
951 * @param fi the entry in the publish-structure
952 * @param length length of the file or directory
953 * @param meta metadata for the file or directory (can be modified)
954 * @param uri pointer to the keywords that will be used for this entry (can be modified)
955 * @param anonymity pointer to selected anonymity level (can be modified)
956 * @param priority pointer to selected priority (can be modified)
957 * @param expirationTime pointer to selected expiration time (can be modified)
958 * @param client_info pointer to client context set upon creation (can be modified)
959 * @return GNUNET_OK to continue (always)
962 fip_signal_start(void *cls,
963 struct GNUNET_FS_FileInformation *fi,
965 struct GNUNET_CONTAINER_MetaData *meta,
966 struct GNUNET_FS_Uri **uri,
967 unsigned int *anonymity,
968 unsigned int *priority,
969 struct GNUNET_TIME_Absolute *expirationTime,
972 struct GNUNET_FS_PublishContext *sc = cls;
973 struct GNUNET_FS_ProgressInfo pi;
975 pi.status = GNUNET_FS_STATUS_PUBLISH_START;
976 make_publish_status (&pi, sc, fi);
977 *client_info = sc->h->upcb (sc->h->upcb_cls,
984 * Publish a file or directory.
986 * @param h handle to the file sharing subsystem
987 * @param ctx initial value to use for the '*ctx'
988 * in the callback (for the GNUNET_FS_STATUS_PUBLISH_START event).
989 * @param fi information about the file or directory structure to publish
990 * @param namespace namespace to publish the file in, NULL for no namespace
991 * @param nid identifier to use for the publishd content in the namespace
992 * (can be NULL, must be NULL if namespace is NULL)
993 * @param nuid update-identifier that will be used for future updates
994 * (can be NULL, must be NULL if namespace or nid is NULL)
995 * @param options options for the publication
996 * @return context that can be used to control the publish operation
998 struct GNUNET_FS_PublishContext *
999 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
1001 struct GNUNET_FS_FileInformation *fi,
1002 struct GNUNET_FS_Namespace *namespace,
1005 enum GNUNET_FS_PublishOptions options)
1007 struct GNUNET_FS_PublishContext *ret;
1008 struct GNUNET_DATASTORE_Handle *dsh;
1010 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1012 dsh = GNUNET_DATASTORE_connect (h->cfg,
1021 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1024 ret->client_ctx = ctx;
1026 ret->namespace = namespace;
1027 if (namespace != NULL)
1030 GNUNET_assert (NULL != nid);
1031 ret->nid = GNUNET_strdup (nid);
1033 ret->nuid = GNUNET_strdup (nuid);
1035 // FIXME: make upload persistent!
1038 GNUNET_FS_file_information_inspect (ret->fi,
1041 ret->fi_pos = ret->fi;
1043 // FIXME: calculate space needed for "fi"
1044 // and reserve as first task (then trigger
1045 // "do_upload" from that continuation)!
1047 = GNUNET_SCHEDULER_add_delayed (h->sched,
1049 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1050 GNUNET_SCHEDULER_NO_TASK,
1051 GNUNET_TIME_UNIT_ZERO,
1059 * Signal the FS's progress function that we are stopping
1062 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1063 * @param fi the entry in the publish-structure
1064 * @param length length of the file or directory
1065 * @param meta metadata for the file or directory (can be modified)
1066 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1067 * @param anonymity pointer to selected anonymity level (can be modified)
1068 * @param priority pointer to selected priority (can be modified)
1069 * @param expirationTime pointer to selected expiration time (can be modified)
1070 * @param client_info pointer to client context set upon creation (can be modified)
1071 * @return GNUNET_OK to continue (always)
1074 fip_signal_stop(void *cls,
1075 struct GNUNET_FS_FileInformation *fi,
1077 struct GNUNET_CONTAINER_MetaData *meta,
1078 struct GNUNET_FS_Uri **uri,
1079 unsigned int *anonymity,
1080 unsigned int *priority,
1081 struct GNUNET_TIME_Absolute *expirationTime,
1084 struct GNUNET_FS_PublishContext*sc = cls;
1085 struct GNUNET_FS_ProgressInfo pi;
1087 pi.status = GNUNET_FS_STATUS_PUBLISH_STOPPED;
1088 make_publish_status (&pi, sc, fi);
1089 GNUNET_break (NULL ==
1090 sc->h->upcb (sc->h->upcb_cls,
1092 *client_info = NULL;
1098 * Stop an upload. Will abort incomplete uploads (but
1099 * not remove blocks that have already been publishd) or
1100 * simply clean up the state for completed uploads.
1102 * @param sc context for the upload to stop
1105 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *sc)
1107 if (GNUNET_SCHEDULER_NO_TASK != sc->upload_task)
1108 GNUNET_SCHEDULER_cancel (sc->h->sched, sc->upload_task);
1109 // FIXME: remove from persistence DB (?) --- think more about
1110 // shutdown / persistent-resume APIs!!!
1111 GNUNET_FS_file_information_inspect (sc->fi,
1114 if (GNUNET_YES == sc->in_network_wait)
1116 sc->in_network_wait = GNUNET_SYSERR;
1119 publish_cleanup (sc);
1124 * Context for the KSK publication.
1126 struct PublishKskContext
1132 struct GNUNET_FS_Uri *ksk_uri;
1135 * Global FS context.
1137 struct GNUNET_FS_Handle *h;
1140 * The master block that we are sending
1141 * (in plaintext), has "mdsize+slen" more
1142 * bytes than the struct would suggest.
1147 * Buffer of the same size as "kb" for
1148 * the encrypted version.
1153 * Handle to the datastore, NULL if we are just
1156 struct GNUNET_DATASTORE_Handle *dsh;
1159 * Function to call once we're done.
1161 GNUNET_FS_PublishContinuation cont;
1169 * When should the KBlocks expire?
1171 struct GNUNET_TIME_Absolute expirationTime;
1174 * Size of the serialized metadata.
1179 * Size of the (CHK) URI as a string.
1184 * Keyword that we are currently processing.
1189 * Anonymity level for the KBlocks.
1191 unsigned int anonymity;
1194 * Priority for the KBlocks.
1196 unsigned int priority;
1201 * Continuation of "GNUNET_FS_publish_ksk" that performs
1202 * the actual publishing operation (iterating over all
1205 * @param cls closure of type "struct PublishKskContext*"
1209 publish_ksk_cont (void *cls,
1210 const struct GNUNET_SCHEDULER_TaskContext *tc);
1214 * Function called by the datastore API with
1215 * the result from the PUT request.
1217 * @param cls closure of type "struct PublishKskContext*"
1218 * @param success GNUNET_OK on success
1219 * @param msg error message (or NULL)
1222 kb_put_cont (void *cls,
1226 struct PublishKskContext *pkc = cls;
1228 if (GNUNET_OK != success)
1230 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1231 GNUNET_free (pkc->cpy);
1232 GNUNET_free (pkc->kb);
1233 pkc->cont (pkc->cont_cls,
1236 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1240 GNUNET_SCHEDULER_add_continuation (pkc->h->sched,
1244 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1249 * Continuation of "GNUNET_FS_publish_ksk" that performs
1250 * the actual publishing operation (iterating over all
1253 * @param cls closure of type "struct PublishKskContext*"
1257 publish_ksk_cont (void *cls,
1258 const struct GNUNET_SCHEDULER_TaskContext *tc)
1260 struct PublishKskContext *pkc = cls;
1261 const char *keyword;
1262 GNUNET_HashCode key;
1263 GNUNET_HashCode query;
1264 struct GNUNET_CRYPTO_AesSessionKey skey;
1265 struct GNUNET_CRYPTO_AesInitializationVector iv;
1266 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1269 if ( (pkc->i == pkc->ksk_uri->data.ksk.keywordCount) ||
1270 (NULL == pkc->dsh) )
1272 if (NULL != pkc->dsh)
1273 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1274 GNUNET_free (pkc->cpy);
1275 GNUNET_free (pkc->kb);
1276 pkc->cont (pkc->cont_cls,
1279 GNUNET_FS_uri_destroy (pkc->ksk_uri);
1283 keyword = pkc->ksk_uri->data.ksk.keywords[pkc->i++];
1284 /* first character of keyword indicates if it is
1285 mandatory or not -- ignore for hashing */
1286 GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
1287 GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
1288 GNUNET_CRYPTO_aes_encrypt (&pkc->kb[1],
1289 pkc->slen + pkc->mdsize,
1293 pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
1294 GNUNET_CRYPTO_rsa_key_get_public (pk, &pkc->cpy->keyspace);
1295 GNUNET_CRYPTO_hash (&pkc->cpy->keyspace,
1296 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1298 GNUNET_assert (GNUNET_OK ==
1299 GNUNET_CRYPTO_rsa_sign (pk,
1301 &pkc->cpy->signature));
1302 GNUNET_CRYPTO_rsa_key_free (pk);
1303 GNUNET_DATASTORE_put (pkc->dsh,
1307 sizeof (struct KBlock) +
1310 GNUNET_DATASTORE_BLOCKTYPE_KBLOCK,
1313 pkc->expirationTime,
1314 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1321 * Publish a CHK under various keywords on GNUnet.
1323 * @param h handle to the file sharing subsystem
1324 * @param ksk_uri keywords to use
1325 * @param meta metadata to use
1326 * @param uri URI to refer to in the KBlock
1327 * @param expirationTime when the KBlock expires
1328 * @param anonymity anonymity level for the KBlock
1329 * @param priority priority for the KBlock
1330 * @param options publication options
1331 * @param cont continuation
1332 * @param cont_cls closure for cont
1335 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
1336 struct GNUNET_FS_Uri *ksk_uri,
1337 struct GNUNET_CONTAINER_MetaData *meta,
1338 struct GNUNET_FS_Uri *uri,
1339 struct GNUNET_TIME_Absolute expirationTime,
1340 unsigned int anonymity,
1341 unsigned int priority,
1342 enum GNUNET_FS_PublishOptions options,
1343 GNUNET_FS_PublishContinuation cont,
1346 struct PublishKskContext *pkc;
1351 pkc = GNUNET_malloc (sizeof (struct PublishKskContext));
1353 pkc->expirationTime = expirationTime;
1354 pkc->anonymity = anonymity;
1355 pkc->priority = priority;
1357 pkc->cont_cls = cont_cls;
1358 if (0 == (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1360 pkc->dsh = GNUNET_DATASTORE_connect (h->cfg,
1362 if (pkc->dsh == NULL)
1364 cont (cont_cls, NULL, _("Could not connect to datastore."));
1369 pkc->mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1370 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1371 GNUNET_assert (pkc->mdsize >= 0);
1372 uris = GNUNET_FS_uri_to_string (uri);
1373 pkc->slen = strlen (uris) + 1;
1374 size = pkc->mdsize + sizeof (struct KBlock) + pkc->slen;
1375 if (size > MAX_KBLOCK_SIZE)
1377 size = MAX_KBLOCK_SIZE;
1378 pkc->mdsize = size - sizeof (struct KBlock) - pkc->slen;
1380 pkc->kb = GNUNET_malloc (size);
1381 kbe = (char *) &pkc->kb[1];
1382 memcpy (kbe, uris, pkc->slen);
1384 pkc->mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1387 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1388 if (pkc->mdsize == -1)
1392 GNUNET_free (pkc->kb);
1393 if (pkc->dsh != NULL)
1394 GNUNET_DATASTORE_disconnect (pkc->dsh, GNUNET_NO);
1395 cont (cont_cls, NULL, _("Internal error."));
1399 size = sizeof (struct KBlock) + pkc->slen + pkc->mdsize;
1401 pkc->cpy = GNUNET_malloc (size);
1402 pkc->cpy->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1403 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1406 pkc->cpy->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK);
1407 pkc->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
1408 GNUNET_SCHEDULER_add_continuation (h->sched,
1412 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1417 * Context for the SKS publication.
1419 struct PublishSksContext
1423 * Global FS context.
1425 struct GNUNET_FS_Uri *uri;
1428 * Handle to the datastore.
1430 struct GNUNET_DATASTORE_Handle *dsh;
1433 * Function to call once we're done.
1435 GNUNET_FS_PublishContinuation cont;
1446 * Function called by the datastore API with
1447 * the result from the PUT (SBlock) request.
1449 * @param cls closure of type "struct PublishSksContext*"
1450 * @param success GNUNET_OK on success
1451 * @param msg error message (or NULL)
1454 sb_put_cont (void *cls,
1458 struct PublishSksContext *psc = cls;
1460 if (NULL != psc->dsh)
1461 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
1462 if (GNUNET_OK != success)
1463 psc->cont (psc->cont_cls,
1467 psc->cont (psc->cont_cls,
1470 GNUNET_FS_uri_destroy (psc->uri);
1476 * Publish an SBlock on GNUnet.
1478 * @param h handle to the file sharing subsystem
1479 * @param namespace namespace to publish in
1480 * @param identifier identifier to use
1481 * @param update update identifier to use
1482 * @param meta metadata to use
1483 * @param uri URI to refer to in the SBlock
1484 * @param expirationTime when the SBlock expires
1485 * @param anonymity anonymity level for the SBlock
1486 * @param priority priority for the SBlock
1487 * @param options publication options
1488 * @param cont continuation
1489 * @param cont_cls closure for cont
1492 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
1493 struct GNUNET_FS_Namespace *namespace,
1494 const char *identifier,
1496 struct GNUNET_CONTAINER_MetaData *meta,
1497 struct GNUNET_FS_Uri *uri,
1498 struct GNUNET_TIME_Absolute expirationTime,
1499 unsigned int anonymity,
1500 unsigned int priority,
1501 enum GNUNET_FS_PublishOptions options,
1502 GNUNET_FS_PublishContinuation cont,
1505 struct PublishSksContext *psc;
1506 struct GNUNET_CRYPTO_AesSessionKey sk;
1507 struct GNUNET_CRYPTO_AesInitializationVector iv;
1508 struct GNUNET_FS_Uri *sks_uri;
1516 struct SBlock *sb_enc;
1518 GNUNET_HashCode key; /* hash of thisId = key */
1519 GNUNET_HashCode id; /* hash of hc = identifier */
1521 uris = GNUNET_FS_uri_to_string (uri);
1522 slen = strlen (uris) + 1;
1523 idlen = strlen (identifier);
1526 nidlen = strlen (update) + 1;
1527 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta,
1528 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1530 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
1531 if (size > MAX_SBLOCK_SIZE)
1533 size = MAX_SBLOCK_SIZE;
1534 mdsize = size - (sizeof (struct SBlock) + slen + nidlen);
1536 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
1537 dest = (char *) &sb[1];
1538 memcpy (dest, update, nidlen);
1540 memcpy (dest, uris, slen);
1542 mdsize = GNUNET_CONTAINER_meta_data_serialize (meta,
1545 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
1553 _("Internal error."));
1556 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
1557 sb_enc = GNUNET_malloc (sizeof (struct SBlock) + size);
1558 GNUNET_CRYPTO_hash (identifier, idlen, &key);
1559 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
1560 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1561 sks_uri->type = sks;
1562 GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
1563 GNUNET_CRYPTO_hash (&sb_enc->subspace,
1564 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1565 &sks_uri->data.sks.namespace);
1566 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
1567 GNUNET_CRYPTO_hash_xor (&id,
1568 &sks_uri->data.sks.namespace,
1569 &sb_enc->identifier);
1570 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
1571 GNUNET_CRYPTO_aes_encrypt (&sb[1],
1572 size - sizeof (struct SBlock),
1577 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
1578 sb_enc->purpose.size = htonl(slen + mdsize + nidlen
1579 + sizeof(struct SBlock)
1580 - sizeof(struct GNUNET_CRYPTO_RsaSignature));
1581 GNUNET_assert (GNUNET_OK ==
1582 GNUNET_CRYPTO_rsa_sign (namespace->key,
1584 &sb_enc->signature));
1585 psc = GNUNET_malloc (sizeof(struct PublishSksContext));
1588 psc->cont_cls = cont_cls;
1589 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
1591 GNUNET_free (sb_enc);
1597 psc->dsh = GNUNET_DATASTORE_connect (h->cfg, h->sched);
1598 if (NULL == psc->dsh)
1600 GNUNET_free (sb_enc);
1603 _("Failed to connect to datastore."));
1607 GNUNET_DATASTORE_put (psc->dsh,
1612 GNUNET_DATASTORE_BLOCKTYPE_SBLOCK,
1616 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1619 GNUNET_free (sb_enc);
1623 /* end of fs_publish.c */