2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 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 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.
23 * @brief main FS functions (master initialization, serialization, deserialization, shared code)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
34 * Start the given job (send signal, remove from pending queue, update
35 * counters and state).
37 * @param qe job to start
40 start_job (struct GNUNET_FS_QueueEntry *qe)
42 qe->client = GNUNET_CLIENT_connect (qe->h->sched, "fs", qe->h->cfg);
43 if (qe->client == NULL)
48 qe->start (qe->cls, qe->client);
50 qe->h->active_blocks += qe->blocks;
51 qe->start_time = GNUNET_TIME_absolute_get ();
52 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
55 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
63 * Stop the given job (send signal, remove from active queue, update
64 * counters and state).
66 * @param qe job to stop
69 stop_job (struct GNUNET_FS_QueueEntry *qe)
73 qe->h->active_downloads--;
74 qe->h->active_blocks -= qe->blocks;
75 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
76 GNUNET_TIME_absolute_get_duration (qe->start_time));
77 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
80 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
88 * Process the jobs in the job queue, possibly starting some
89 * and stopping others.
91 * @param cls the 'struct GNUNET_FS_Handle'
92 * @param tc scheduler context
95 process_job_queue (void *cls,
96 const struct GNUNET_SCHEDULER_TaskContext *tc)
98 struct GNUNET_FS_Handle *h = cls;
99 struct GNUNET_FS_QueueEntry *qe;
100 struct GNUNET_FS_QueueEntry *next;
101 struct GNUNET_TIME_Relative run_time;
102 struct GNUNET_TIME_Relative restart_at;
103 struct GNUNET_TIME_Relative rst;
104 struct GNUNET_TIME_Absolute end_time;
106 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
107 next = h->pending_head;
108 while (NULL != (qe = next))
111 if (h->running_head == NULL)
116 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
117 (h->active_downloads + 1 <= h->max_parallel_downloads) )
123 if (h->pending_head == NULL)
124 return; /* no need to stop anything */
125 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
126 next = h->running_head;
127 while (NULL != (qe = next))
130 /* FIXME: might be faster/simpler to do this calculation only once
131 when we start a job (OTOH, this would allow us to dynamically
132 and easily adjust qe->blocks over time, given the right API...) */
133 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
134 qe->blocks * qe->start_times);
135 end_time = GNUNET_TIME_absolute_add (qe->start_time,
137 rst = GNUNET_TIME_absolute_get_remaining (end_time);
138 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
143 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
151 * Add a job to the queue.
153 * @param h handle to the overall FS state
154 * @param start function to call to begin the job
155 * @param stop function to call to pause the job, or on dequeue (if the job was running)
156 * @param cls closure for start and stop
157 * @param blocks number of blocks this jobs uses
158 * @return queue handle
160 struct GNUNET_FS_QueueEntry *
161 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
162 GNUNET_FS_QueueStart start,
163 GNUNET_FS_QueueStop stop,
167 struct GNUNET_FS_QueueEntry *qe;
169 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
174 qe->queue_time = GNUNET_TIME_absolute_get ();
176 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
180 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
181 GNUNET_SCHEDULER_cancel (h->sched,
184 = GNUNET_SCHEDULER_add_now (h->sched,
192 * Dequeue a job from the queue.
193 * @param qh handle for the job
196 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
198 struct GNUNET_FS_Handle *h;
201 if (qh->client != NULL)
203 GNUNET_CONTAINER_DLL_remove (h->pending_head,
207 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
208 GNUNET_SCHEDULER_cancel (h->sched,
211 = GNUNET_SCHEDULER_add_now (h->sched,
220 * Closure for "data_reader_file".
225 * Name of the file to read.
230 * File descriptor, NULL if it has not yet been opened.
232 struct GNUNET_DISK_FileHandle *fd;
237 * Function that provides data by reading from a file.
239 * @param cls closure (points to the file information)
240 * @param offset offset to read from; it is possible
241 * that the caller might need to go backwards
243 * @param max maximum number of bytes that should be
244 * copied to buf; readers are not allowed
245 * to provide less data unless there is an error;
246 * a value of "0" will be used at the end to allow
247 * the reader to clean up its internal state
248 * @param buf where the reader should write the data
249 * @param emsg location for the reader to store an error message
250 * @return number of bytes written, usually "max", 0 on error
253 GNUNET_FS_data_reader_file_(void *cls,
259 struct FileInfo *fi = cls;
265 GNUNET_DISK_file_close (fi->fd);
266 GNUNET_free (fi->filename);
272 fi->fd = GNUNET_DISK_file_open (fi->filename,
273 GNUNET_DISK_OPEN_READ,
274 GNUNET_DISK_PERM_NONE);
277 GNUNET_asprintf (emsg,
278 _("Could not open file `%s': %s"),
284 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
285 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
288 GNUNET_asprintf (emsg,
289 _("Could not read file `%s': %s"),
296 GNUNET_asprintf (emsg,
297 _("Short read reading from file `%s'!"),
306 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
308 * @param filename file to read
309 * @return closure to use, NULL on error
312 GNUNET_FS_make_file_reader_context_ (const char *filename)
316 fi = GNUNET_malloc (sizeof(struct FileInfo));
317 fi->filename = GNUNET_STRINGS_filename_expand (filename);
318 if (fi->filename == NULL)
328 * Return the full filename where we would store state information
329 * (for serialization/deserialization).
331 * @param h master context
332 * @param ext component of the path
333 * @param ent entity identifier (or emtpy string for the directory)
334 * @return NULL on error
337 get_serialization_file_name (struct GNUNET_FS_Handle *h,
344 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
349 GNUNET_asprintf (&ret,
357 GNUNET_free (basename);
363 * Return a read handle for deserialization.
365 * @param h master context
366 * @param ext component of the path
367 * @param ent entity identifier (or emtpy string for the directory)
368 * @return NULL on error
370 static struct GNUNET_BIO_ReadHandle *
371 get_read_handle (struct GNUNET_FS_Handle *h,
376 struct GNUNET_BIO_ReadHandle *ret;
378 fn = get_serialization_file_name (h, ext, ent);
381 ret = GNUNET_BIO_read_open (fn);
388 * Return a write handle for serialization.
390 * @param h master context
391 * @param ext component of the path
392 * @param ent entity identifier (or emtpy string for the directory)
393 * @return NULL on error
395 static struct GNUNET_BIO_WriteHandle *
396 get_write_handle (struct GNUNET_FS_Handle *h,
401 struct GNUNET_BIO_WriteHandle *ret;
403 fn = get_serialization_file_name (h, ext, ent);
406 ret = GNUNET_BIO_write_open (fn);
413 * Remove serialization/deserialization file from disk.
415 * @param h master context
416 * @param ext component of the path
417 * @param ent entity identifier
420 remove_sync_file (struct GNUNET_FS_Handle *h,
426 if ( (NULL == ent) ||
427 (0 == strlen (ent)) )
432 filename = get_serialization_file_name (h, ext, ent);
433 if (0 != UNLINK (filename))
434 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
437 GNUNET_free (filename);
443 * Using the given serialization filename, try to deserialize
444 * the file-information tree associated with it.
446 * @param h master context
447 * @param filename name of the file (without directory) with
449 * @return NULL on error
451 static struct GNUNET_FS_FileInformation *
452 deserialize_file_information (struct GNUNET_FS_Handle *h,
453 const char *filename);
457 * Using the given serialization filename, try to deserialize
458 * the file-information tree associated with it.
460 * @param h master context
461 * @param fn name of the file (without directory) with
463 * @param rh handle for reading
464 * @return NULL on error
466 static struct GNUNET_FS_FileInformation *
467 deserialize_fi_node (struct GNUNET_FS_Handle *h,
469 struct GNUNET_BIO_ReadHandle *rh)
471 struct GNUNET_FS_FileInformation *ret;
472 struct GNUNET_FS_FileInformation *nxt;
480 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
485 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
490 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
492 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
495 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
497 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
499 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
502 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
504 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
506 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
508 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
510 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
512 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
514 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
516 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
520 case 0: /* file-insert */
522 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
524 ret->is_directory = GNUNET_NO;
525 ret->data.file.do_index = GNUNET_NO;
526 ret->data.file.have_hash = GNUNET_NO;
527 ret->data.file.index_start_confirmed = GNUNET_NO;
528 /* FIXME: what's our approach for dealing with the
529 'reader' and 'reader_cls' fields? I guess the only
530 good way would be to dump "small" files into
531 'rh' and to not support serialization of "large"
534 case 1: /* file-index, no hash */
535 if (NULL == ret->filename)
538 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
540 ret->is_directory = GNUNET_NO;
541 ret->data.file.do_index = GNUNET_YES;
542 ret->data.file.have_hash = GNUNET_NO;
543 ret->data.file.index_start_confirmed = GNUNET_NO;
544 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
545 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
547 case 2: /* file-index-with-hash */
548 if (NULL == ret->filename)
551 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
553 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
555 ret->is_directory = GNUNET_NO;
556 ret->data.file.do_index = GNUNET_YES;
557 ret->data.file.have_hash = GNUNET_YES;
558 ret->data.file.index_start_confirmed = GNUNET_NO;
559 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
560 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
562 case 3: /* file-index-with-hash-confirmed */
563 if (NULL == ret->filename)
566 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
568 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
571 ret->is_directory = GNUNET_NO;
572 ret->data.file.do_index = GNUNET_YES;
573 ret->data.file.have_hash = GNUNET_YES;
574 ret->data.file.index_start_confirmed = GNUNET_YES;
575 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
576 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
578 case 4: /* directory */
580 GNUNET_BIO_read_int32 (rh, &dsize)) ||
581 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
583 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
585 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
587 ret->data.dir.dir_size = (uint32_t) dsize;
588 ret->is_directory = GNUNET_YES;
589 if (filename != NULL)
591 ret->data.dir.entries = deserialize_file_information (h, filename);
592 GNUNET_free (filename);
594 nxt = ret->data.dir.entries;
606 /* FIXME: adjust ret->start_time! */
607 ret->serialization = GNUNET_strdup (fn);
609 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
611 if (filename != NULL)
613 ret->next = deserialize_file_information (h, filename);
614 GNUNET_free (filename);
619 GNUNET_free_non_null (ksks);
620 GNUNET_free_non_null (chks);
621 GNUNET_free_non_null (filename);
622 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
628 * Using the given serialization filename, try to deserialize
629 * the file-information tree associated with it.
631 * @param h master context
632 * @param filename name of the file (without directory) with
634 * @return NULL on error
636 static struct GNUNET_FS_FileInformation *
637 deserialize_file_information (struct GNUNET_FS_Handle *h,
638 const char *filename)
640 struct GNUNET_FS_FileInformation *ret;
641 struct GNUNET_BIO_ReadHandle *rh;
644 rh = get_read_handle (h, "publish-fi", filename);
647 ret = deserialize_fi_node (h, filename, rh);
649 GNUNET_BIO_read_close (rh, &emsg))
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 _("Failed to resume publishing information `%s': %s\n"),
662 * Given a serialization name (full absolute path), return the
663 * basename of the file (without the path), which must only
664 * consist of the 6 random characters.
666 * @param fullname name to extract the basename from
667 * @return copy of the basename, NULL on error
670 get_serialization_short_name (const char *fullname)
677 /* FIXME: we could do this faster since we know
678 the length of 'end'... */
681 if (DIR_SEPARATOR == *nxt)
685 if ( (end == NULL) ||
686 (strlen (end) == 0) )
691 GNUNET_break (6 == strlen (end));
692 return GNUNET_strdup (end);
697 * Create a new random name for serialization. Also checks if persistence
698 * is enabled and returns NULL if not.
700 * @param h master context
701 * @param ext component of the path
702 * @return NULL on errror
705 make_serialization_file_name (struct GNUNET_FS_Handle *h,
712 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
713 return NULL; /* persistence not requested */
714 dn = get_serialization_file_name (h, ext, "");
715 fn = GNUNET_DISK_mktemp (dn);
718 return NULL; /* epic fail */
719 ret = get_serialization_short_name (fn);
726 * Copy all of the data from the reader to the write handle.
728 * @param wh write handle
729 * @param fi file with reader
730 * @return GNUNET_OK on success
733 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
734 struct GNUNET_FS_FileInformation * fi)
743 while (off < fi->data.file.file_size)
745 ret = fi->data.file.reader (fi->data.file.reader_cls,
752 return GNUNET_SYSERR;
755 GNUNET_BIO_write (wh, buf, ret))
756 return GNUNET_SYSERR;
764 * Create a temporary file on disk to store the current
767 * @param fi file information to sync with disk
770 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
773 struct GNUNET_BIO_WriteHandle *wh;
778 if (NULL == fi->serialization)
779 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
780 if (NULL == fi->serialization)
782 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
785 GNUNET_free (fi->serialization);
786 fi->serialization = NULL;
789 if (GNUNET_YES == fi->is_directory)
791 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
793 else if (GNUNET_YES == fi->data.file.have_hash)
795 else if (GNUNET_YES == fi->data.file.do_index)
799 if (fi->keywords != NULL)
800 ksks = GNUNET_FS_uri_to_string (fi->keywords);
803 if (fi->chk_uri != NULL)
804 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
808 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
810 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
812 GNUNET_BIO_write_string (wh, ksks)) ||
814 GNUNET_BIO_write_string (wh, chks)) ||
816 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
818 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
820 GNUNET_BIO_write_string (wh, fi->emsg)) ||
822 GNUNET_BIO_write_string (wh, fi->filename)) ||
824 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
826 GNUNET_BIO_write_int32 (wh, fi->priority)) )
828 GNUNET_free_non_null (chks);
830 GNUNET_free_non_null (ksks);
835 case 0: /* file-insert */
837 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
839 if ( (GNUNET_NO == fi->is_published) &&
840 (NULL == fi->filename) )
842 copy_from_reader (wh, fi))
845 case 1: /* file-index, no hash */
846 if (NULL == fi->filename)
849 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
852 case 2: /* file-index-with-hash */
853 case 3: /* file-index-with-hash-confirmed */
854 if (NULL == fi->filename)
857 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
859 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
862 case 4: /* directory */
864 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
866 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
868 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
876 GNUNET_BIO_write_string (wh, fi->next->serialization))
879 GNUNET_BIO_write_close (wh))
882 (void) GNUNET_BIO_write_close (wh);
883 GNUNET_free_non_null (chks);
884 GNUNET_free_non_null (ksks);
885 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
886 if (0 != UNLINK (fn))
887 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
889 GNUNET_free (fi->serialization);
890 fi->serialization = NULL;
896 * Find the entry in the file information struct where the
897 * serialization filename matches the given name.
899 * @param pos file information to search
900 * @param srch filename to search for
901 * @return NULL if srch was not found in this subtree
903 static struct GNUNET_FS_FileInformation *
904 find_file_position (struct GNUNET_FS_FileInformation *pos,
907 struct GNUNET_FS_FileInformation *r;
911 if (0 == strcmp (srch,
914 if (pos->is_directory)
916 r = find_file_position (pos->data.dir.entries,
928 * Signal the FS's progress function that we are resuming
931 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
932 * @param fi the entry in the publish-structure
933 * @param length length of the file or directory
934 * @param meta metadata for the file or directory (can be modified)
935 * @param uri pointer to the keywords that will be used for this entry (can be modified)
936 * @param anonymity pointer to selected anonymity level (can be modified)
937 * @param priority pointer to selected priority (can be modified)
938 * @param expirationTime pointer to selected expiration time (can be modified)
939 * @param client_info pointer to client context set upon creation (can be modified)
940 * @return GNUNET_OK to continue (always)
943 fip_signal_resume(void *cls,
944 struct GNUNET_FS_FileInformation *fi,
946 struct GNUNET_CONTAINER_MetaData *meta,
947 struct GNUNET_FS_Uri **uri,
950 struct GNUNET_TIME_Absolute *expirationTime,
953 struct GNUNET_FS_PublishContext *sc = cls;
954 struct GNUNET_FS_ProgressInfo pi;
956 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
957 pi.value.publish.specifics.resume.message = sc->fi->emsg;
958 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
959 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
965 * Function called with a filename of serialized publishing operation
968 * @param cls the 'struct GNUNET_FS_Handle*'
969 * @param filename complete filename (absolute path)
970 * @return GNUNET_OK (continue to iterate)
973 deserialize_publish_file (void *cls,
974 const char *filename)
976 struct GNUNET_FS_Handle *h = cls;
977 struct GNUNET_BIO_ReadHandle *rh;
978 struct GNUNET_FS_PublishContext *pc;
986 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
991 rh = GNUNET_BIO_read_open (filename);
995 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
997 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
999 GNUNET_BIO_read_int32 (rh, &options)) ||
1001 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1003 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1005 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1007 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1009 pc->options = options;
1010 pc->all_done = all_done;
1011 pc->fi = deserialize_file_information (h, fi_root);
1016 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1017 if (pc->namespace == NULL)
1019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1027 pc->fi_pos = find_file_position (pc->fi,
1029 GNUNET_free (fi_pos);
1030 if (pc->fi_pos == NULL)
1032 /* failed to find position for resuming, outch! Will start from root! */
1034 if (pc->all_done != GNUNET_YES)
1035 pc->fi_pos = pc->fi;
1038 pc->serialization = get_serialization_short_name (filename);
1039 /* generate RESUME event(s) */
1040 GNUNET_FS_file_information_inspect (pc->fi,
1044 /* re-start publishing (if needed)... */
1045 if (pc->all_done != GNUNET_YES)
1047 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1048 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1049 &GNUNET_FS_publish_main_,
1052 GNUNET_BIO_read_close (rh, &emsg))
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1055 _("Failed to resume publishing operation `%s': %s\n"),
1062 GNUNET_free_non_null (pc->nid);
1063 GNUNET_free_non_null (pc->nuid);
1064 GNUNET_free_non_null (fi_root);
1065 GNUNET_free_non_null (ns);
1066 if ( (rh != NULL) &&
1068 GNUNET_BIO_read_close (rh, &emsg)) )
1070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1071 _("Failed to resume publishing operation `%s': %s\n"),
1077 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1078 remove_sync_file (h, "publish", pc->serialization);
1079 GNUNET_free_non_null (pc->serialization);
1086 * Synchronize this publishing struct with its mirror
1087 * on disk. Note that all internal FS-operations that change
1088 * publishing structs should already call "sync" internally,
1089 * so this function is likely not useful for clients.
1091 * @param pc the struct to sync
1094 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1096 struct GNUNET_BIO_WriteHandle *wh;
1098 if (NULL == pc->serialization)
1099 pc->serialization = make_serialization_file_name (pc->h,
1101 if (NULL == pc->serialization)
1105 if (NULL == pc->fi->serialization)
1110 wh = get_write_handle (pc->h, "publish", pc->serialization);
1112 GNUNET_BIO_write_string (wh, pc->nid)) ||
1114 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1116 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1118 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1120 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1122 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1124 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1126 (void) GNUNET_BIO_write_close (wh);
1127 remove_sync_file (pc->h, "publish", pc->serialization);
1128 GNUNET_free (pc->serialization);
1129 pc->serialization = NULL;
1133 GNUNET_BIO_write_close (wh))
1135 remove_sync_file (pc->h, "publish", pc->serialization);
1136 GNUNET_free (pc->serialization);
1137 pc->serialization = NULL;
1144 * Deserialize information about pending publish operations.
1146 * @param h master context
1149 deserialize_publish (struct GNUNET_FS_Handle *h)
1153 dn = get_serialization_file_name (h, "publish", "");
1156 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1162 * Setup a connection to the file-sharing service.
1164 * @param sched scheduler to use
1165 * @param cfg configuration to use
1166 * @param client_name unique identifier for this client
1167 * @param upcb function to call to notify about FS actions
1168 * @param upcb_cls closure for upcb
1169 * @param flags specific attributes for fs-operations
1170 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1171 * @return NULL on error
1173 struct GNUNET_FS_Handle *
1174 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1175 const struct GNUNET_CONFIGURATION_Handle *cfg,
1176 const char *client_name,
1177 GNUNET_FS_ProgressCallback upcb,
1179 enum GNUNET_FS_Flags flags,
1182 struct GNUNET_FS_Handle *ret;
1183 struct GNUNET_CLIENT_Connection *client;
1184 enum GNUNET_FS_OPTIONS opt;
1187 client = GNUNET_CLIENT_connect (sched,
1192 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1195 ret->client_name = GNUNET_strdup (client_name);
1197 ret->upcb_cls = upcb_cls;
1198 ret->client = client;
1200 ret->max_parallel_downloads = 1;
1201 ret->max_parallel_requests = 1;
1202 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1203 va_start (ap, flags);
1204 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1208 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1209 ret->max_parallel_downloads = va_arg (ap, unsigned int);
1211 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1212 ret->max_parallel_requests = va_arg (ap, unsigned int);
1216 GNUNET_free (ret->client_name);
1223 // FIXME: setup receive-loop with client (do we need one?)
1225 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1227 deserialize_publish (ret);
1228 /* FIXME: not implemented! */
1229 // Deserialize Search:
1230 // * read search queries
1231 // * for each query, read file with search results
1232 // * for each search result with active download, deserialize download
1233 // * for each directory search result, check for active downloads of contents
1234 // Deserialize Download:
1235 // * always part of search???
1236 // Deserialize Unindex:
1237 // * read FNs for unindex with progress offset
1244 * Close our connection with the file-sharing service.
1245 * The callback given to GNUNET_FS_start will no longer be
1246 * called after this function returns.
1248 * @param h handle that was returned from GNUNET_FS_start
1251 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1253 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1255 // FIXME: generate SUSPEND events and clean up state!
1257 // FIXME: terminate receive-loop with client (do we need one?)
1258 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1259 GNUNET_SCHEDULER_cancel (h->sched,
1261 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1262 GNUNET_free (h->client_name);