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 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
131 qe->blocks * qe->start_times);
132 end_time = GNUNET_TIME_absolute_add (qe->start_time,
134 rst = GNUNET_TIME_absolute_get_remaining (end_time);
135 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
140 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
148 * Add a job to the queue.
150 * @param h handle to the overall FS state
151 * @param start function to call to begin the job
152 * @param stop function to call to pause the job, or on dequeue (if the job was running)
153 * @param cls closure for start and stop
154 * @param blocks number of blocks this jobs uses
155 * @return queue handle
157 struct GNUNET_FS_QueueEntry *
158 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
159 GNUNET_FS_QueueStart start,
160 GNUNET_FS_QueueStop stop,
164 struct GNUNET_FS_QueueEntry *qe;
166 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
171 qe->queue_time = GNUNET_TIME_absolute_get ();
173 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
177 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
178 GNUNET_SCHEDULER_cancel (h->sched,
181 = GNUNET_SCHEDULER_add_now (h->sched,
189 * Dequeue a job from the queue.
190 * @param qh handle for the job
193 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
195 struct GNUNET_FS_Handle *h;
198 if (qh->client != NULL)
200 GNUNET_CONTAINER_DLL_remove (h->pending_head,
204 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
205 GNUNET_SCHEDULER_cancel (h->sched,
208 = GNUNET_SCHEDULER_add_now (h->sched,
215 * Closure for "data_reader_file".
220 * Name of the file to read.
225 * File descriptor, NULL if it has not yet been opened.
227 struct GNUNET_DISK_FileHandle *fd;
232 * Function that provides data by reading from a file.
234 * @param cls closure (points to the file information)
235 * @param offset offset to read from; it is possible
236 * that the caller might need to go backwards
238 * @param max maximum number of bytes that should be
239 * copied to buf; readers are not allowed
240 * to provide less data unless there is an error;
241 * a value of "0" will be used at the end to allow
242 * the reader to clean up its internal state
243 * @param buf where the reader should write the data
244 * @param emsg location for the reader to store an error message
245 * @return number of bytes written, usually "max", 0 on error
248 GNUNET_FS_data_reader_file_(void *cls,
254 struct FileInfo *fi = cls;
260 GNUNET_DISK_file_close (fi->fd);
261 GNUNET_free (fi->filename);
267 fi->fd = GNUNET_DISK_file_open (fi->filename,
268 GNUNET_DISK_OPEN_READ,
269 GNUNET_DISK_PERM_NONE);
272 GNUNET_asprintf (emsg,
273 _("Could not open file `%s': %s"),
279 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
280 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
283 GNUNET_asprintf (emsg,
284 _("Could not read file `%s': %s"),
291 GNUNET_asprintf (emsg,
292 _("Short read reading from file `%s'!"),
301 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
303 * @param filename file to read
304 * @return closure to use, NULL on error
307 GNUNET_FS_make_file_reader_context_ (const char *filename)
311 fi = GNUNET_malloc (sizeof(struct FileInfo));
312 fi->filename = GNUNET_STRINGS_filename_expand (filename);
313 if (fi->filename == NULL)
323 * Function that provides data by copying from a buffer.
325 * @param cls closure (points to the buffer)
326 * @param offset offset to read from; it is possible
327 * that the caller might need to go backwards
329 * @param max maximum number of bytes that should be
330 * copied to buf; readers are not allowed
331 * to provide less data unless there is an error;
332 * a value of "0" will be used at the end to allow
333 * the reader to clean up its internal state
334 * @param buf where the reader should write the data
335 * @param emsg location for the reader to store an error message
336 * @return number of bytes written, usually "max", 0 on error
339 GNUNET_FS_data_reader_copy_ (void *cls,
349 GNUNET_free_non_null (data);
352 memcpy (buf, &data[offset], max);
359 * Return the full filename where we would store state information
360 * (for serialization/deserialization).
362 * @param h master context
363 * @param ext component of the path
364 * @param ent entity identifier (or emtpy string for the directory)
365 * @return NULL on error
368 get_serialization_file_name (struct GNUNET_FS_Handle *h,
375 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
380 GNUNET_asprintf (&ret,
388 GNUNET_free (basename);
394 * Return a read handle for deserialization.
396 * @param h master context
397 * @param ext component of the path
398 * @param ent entity identifier (or emtpy string for the directory)
399 * @return NULL on error
401 static struct GNUNET_BIO_ReadHandle *
402 get_read_handle (struct GNUNET_FS_Handle *h,
407 struct GNUNET_BIO_ReadHandle *ret;
409 fn = get_serialization_file_name (h, ext, ent);
412 ret = GNUNET_BIO_read_open (fn);
419 * Return a write handle for serialization.
421 * @param h master context
422 * @param ext component of the path
423 * @param ent entity identifier (or emtpy string for the directory)
424 * @return NULL on error
426 static struct GNUNET_BIO_WriteHandle *
427 get_write_handle (struct GNUNET_FS_Handle *h,
432 struct GNUNET_BIO_WriteHandle *ret;
434 fn = get_serialization_file_name (h, ext, ent);
437 ret = GNUNET_BIO_write_open (fn);
444 * Remove serialization/deserialization file from disk.
446 * @param h master context
447 * @param ext component of the path
448 * @param ent entity identifier
451 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
457 if ( (NULL == ent) ||
458 (0 == strlen (ent)) )
463 filename = get_serialization_file_name (h, ext, ent);
464 if (0 != UNLINK (filename))
465 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
468 GNUNET_free (filename);
474 * Using the given serialization filename, try to deserialize
475 * the file-information tree associated with it.
477 * @param h master context
478 * @param filename name of the file (without directory) with
480 * @return NULL on error
482 static struct GNUNET_FS_FileInformation *
483 deserialize_file_information (struct GNUNET_FS_Handle *h,
484 const char *filename);
488 * Using the given serialization filename, try to deserialize
489 * the file-information tree associated with it.
491 * @param h master context
492 * @param fn name of the file (without directory) with
494 * @param rh handle for reading
495 * @return NULL on error
497 static struct GNUNET_FS_FileInformation *
498 deserialize_fi_node (struct GNUNET_FS_Handle *h,
500 struct GNUNET_BIO_ReadHandle *rh)
502 struct GNUNET_FS_FileInformation *ret;
503 struct GNUNET_FS_FileInformation *nxt;
511 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
516 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
521 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
523 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
526 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
528 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
530 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
533 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
535 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
537 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
539 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
541 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
543 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
545 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
547 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
551 case 0: /* file-insert */
553 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
555 ret->is_directory = GNUNET_NO;
556 ret->data.file.do_index = GNUNET_NO;
557 ret->data.file.have_hash = GNUNET_NO;
558 ret->data.file.index_start_confirmed = GNUNET_NO;
559 if (GNUNET_NO == ret->is_published)
561 if (NULL == ret->filename)
563 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
564 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
565 if (ret->data.file.reader_cls == NULL)
568 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
573 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
574 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
578 case 1: /* file-index, no hash */
579 if (NULL == ret->filename)
582 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
584 ret->is_directory = GNUNET_NO;
585 ret->data.file.do_index = GNUNET_YES;
586 ret->data.file.have_hash = GNUNET_NO;
587 ret->data.file.index_start_confirmed = GNUNET_NO;
588 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
589 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
591 case 2: /* file-index-with-hash */
592 if (NULL == ret->filename)
595 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
597 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
599 ret->is_directory = GNUNET_NO;
600 ret->data.file.do_index = GNUNET_YES;
601 ret->data.file.have_hash = GNUNET_YES;
602 ret->data.file.index_start_confirmed = GNUNET_NO;
603 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
604 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
606 case 3: /* file-index-with-hash-confirmed */
607 if (NULL == ret->filename)
610 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
612 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
615 ret->is_directory = GNUNET_NO;
616 ret->data.file.do_index = GNUNET_YES;
617 ret->data.file.have_hash = GNUNET_YES;
618 ret->data.file.index_start_confirmed = GNUNET_YES;
619 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
620 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
622 case 4: /* directory */
624 GNUNET_BIO_read_int32 (rh, &dsize)) ||
625 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
627 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
629 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
631 ret->data.dir.dir_size = (uint32_t) dsize;
632 ret->is_directory = GNUNET_YES;
633 if (filename != NULL)
635 ret->data.dir.entries = deserialize_file_information (h, filename);
636 GNUNET_free (filename);
638 nxt = ret->data.dir.entries;
650 /* FIXME: adjust ret->start_time! */
651 ret->serialization = GNUNET_strdup (fn);
653 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
655 if (filename != NULL)
657 ret->next = deserialize_file_information (h, filename);
658 GNUNET_free (filename);
661 GNUNET_free_non_null (ksks);
662 GNUNET_free_non_null (chks);
665 GNUNET_free_non_null (ksks);
666 GNUNET_free_non_null (chks);
667 GNUNET_free_non_null (filename);
668 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
674 * Using the given serialization filename, try to deserialize
675 * the file-information tree associated with it.
677 * @param h master context
678 * @param filename name of the file (without directory) with
680 * @return NULL on error
682 static struct GNUNET_FS_FileInformation *
683 deserialize_file_information (struct GNUNET_FS_Handle *h,
684 const char *filename)
686 struct GNUNET_FS_FileInformation *ret;
687 struct GNUNET_BIO_ReadHandle *rh;
690 rh = get_read_handle (h, "publish-fi", filename);
693 ret = deserialize_fi_node (h, filename, rh);
695 GNUNET_BIO_read_close (rh, &emsg))
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698 _("Failed to resume publishing information `%s': %s\n"),
708 * Given a serialization name (full absolute path), return the
709 * basename of the file (without the path), which must only
710 * consist of the 6 random characters.
712 * @param fullname name to extract the basename from
713 * @return copy of the basename, NULL on error
716 get_serialization_short_name (const char *fullname)
723 /* FIXME: we could do this faster since we know
724 the length of 'end'... */
727 if (DIR_SEPARATOR == *nxt)
731 if ( (end == NULL) ||
732 (strlen (end) == 0) )
737 GNUNET_break (6 == strlen (end));
738 return GNUNET_strdup (end);
743 * Create a new random name for serialization. Also checks if persistence
744 * is enabled and returns NULL if not.
746 * @param h master context
747 * @param ext component of the path
748 * @return NULL on errror
751 make_serialization_file_name (struct GNUNET_FS_Handle *h,
758 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
759 return NULL; /* persistence not requested */
760 dn = get_serialization_file_name (h, ext, "");
761 fn = GNUNET_DISK_mktemp (dn);
764 return NULL; /* epic fail */
765 ret = get_serialization_short_name (fn);
772 * Copy all of the data from the reader to the write handle.
774 * @param wh write handle
775 * @param fi file with reader
776 * @return GNUNET_OK on success
779 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
780 struct GNUNET_FS_FileInformation * fi)
789 while (off < fi->data.file.file_size)
791 ret = fi->data.file.reader (fi->data.file.reader_cls,
798 return GNUNET_SYSERR;
801 GNUNET_BIO_write (wh, buf, ret))
802 return GNUNET_SYSERR;
810 * Create a temporary file on disk to store the current
813 * @param fi file information to sync with disk
816 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
819 struct GNUNET_BIO_WriteHandle *wh;
824 if (NULL == fi->serialization)
825 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
826 if (NULL == fi->serialization)
828 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
831 GNUNET_free (fi->serialization);
832 fi->serialization = NULL;
835 if (GNUNET_YES == fi->is_directory)
837 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
839 else if (GNUNET_YES == fi->data.file.have_hash)
841 else if (GNUNET_YES == fi->data.file.do_index)
845 if (fi->keywords != NULL)
846 ksks = GNUNET_FS_uri_to_string (fi->keywords);
849 if (fi->chk_uri != NULL)
850 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
854 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
856 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
858 GNUNET_BIO_write_string (wh, ksks)) ||
860 GNUNET_BIO_write_string (wh, chks)) ||
862 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
864 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
866 GNUNET_BIO_write_string (wh, fi->emsg)) ||
868 GNUNET_BIO_write_string (wh, fi->filename)) ||
870 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
872 GNUNET_BIO_write_int32 (wh, fi->priority)) )
874 GNUNET_free_non_null (chks);
876 GNUNET_free_non_null (ksks);
881 case 0: /* file-insert */
883 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
885 if ( (GNUNET_NO == fi->is_published) &&
886 (NULL == fi->filename) )
888 copy_from_reader (wh, fi))
891 case 1: /* file-index, no hash */
892 if (NULL == fi->filename)
895 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
898 case 2: /* file-index-with-hash */
899 case 3: /* file-index-with-hash-confirmed */
900 if (NULL == fi->filename)
903 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
905 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
908 case 4: /* directory */
910 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
912 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
914 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
922 GNUNET_BIO_write_string (wh, fi->next->serialization))
925 GNUNET_BIO_write_close (wh))
928 (void) GNUNET_BIO_write_close (wh);
929 GNUNET_free_non_null (chks);
930 GNUNET_free_non_null (ksks);
931 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
932 if (0 != UNLINK (fn))
933 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
935 GNUNET_free (fi->serialization);
936 fi->serialization = NULL;
942 * Find the entry in the file information struct where the
943 * serialization filename matches the given name.
945 * @param pos file information to search
946 * @param srch filename to search for
947 * @return NULL if srch was not found in this subtree
949 static struct GNUNET_FS_FileInformation *
950 find_file_position (struct GNUNET_FS_FileInformation *pos,
953 struct GNUNET_FS_FileInformation *r;
957 if (0 == strcmp (srch,
960 if (pos->is_directory)
962 r = find_file_position (pos->data.dir.entries,
974 * Signal the FS's progress function that we are resuming
977 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
978 * @param fi the entry in the publish-structure
979 * @param length length of the file or directory
980 * @param meta metadata for the file or directory (can be modified)
981 * @param uri pointer to the keywords that will be used for this entry (can be modified)
982 * @param anonymity pointer to selected anonymity level (can be modified)
983 * @param priority pointer to selected priority (can be modified)
984 * @param expirationTime pointer to selected expiration time (can be modified)
985 * @param client_info pointer to client context set upon creation (can be modified)
986 * @return GNUNET_OK to continue (always)
989 fip_signal_resume(void *cls,
990 struct GNUNET_FS_FileInformation *fi,
992 struct GNUNET_CONTAINER_MetaData *meta,
993 struct GNUNET_FS_Uri **uri,
996 struct GNUNET_TIME_Absolute *expirationTime,
999 struct GNUNET_FS_PublishContext *sc = cls;
1000 struct GNUNET_FS_ProgressInfo pi;
1002 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1003 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1004 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1005 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1011 * Function called with a filename of serialized publishing operation
1014 * @param cls the 'struct GNUNET_FS_Handle*'
1015 * @param filename complete filename (absolute path)
1016 * @return GNUNET_OK (continue to iterate)
1019 deserialize_publish_file (void *cls,
1020 const char *filename)
1022 struct GNUNET_FS_Handle *h = cls;
1023 struct GNUNET_BIO_ReadHandle *rh;
1024 struct GNUNET_FS_PublishContext *pc;
1032 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1034 pc->serialization = get_serialization_short_name (filename);
1038 rh = GNUNET_BIO_read_open (filename);
1042 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1044 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1046 GNUNET_BIO_read_int32 (rh, &options)) ||
1048 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1050 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1052 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1054 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1056 pc->options = options;
1057 pc->all_done = all_done;
1058 pc->fi = deserialize_file_information (h, fi_root);
1063 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1064 if (pc->namespace == NULL)
1066 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1067 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1074 pc->fi_pos = find_file_position (pc->fi,
1076 GNUNET_free (fi_pos);
1078 if (pc->fi_pos == NULL)
1080 /* failed to find position for resuming, outch! Will start from root! */
1082 if (pc->all_done != GNUNET_YES)
1083 pc->fi_pos = pc->fi;
1086 /* generate RESUME event(s) */
1087 GNUNET_FS_file_information_inspect (pc->fi,
1091 /* re-start publishing (if needed)... */
1092 if (pc->all_done != GNUNET_YES)
1094 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1095 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1096 &GNUNET_FS_publish_main_,
1099 GNUNET_BIO_read_close (rh, &emsg))
1101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1102 _("Failure while resuming publishing operation `%s': %s\n"),
1107 GNUNET_free_non_null (ns);
1110 GNUNET_free_non_null (pc->nid);
1111 GNUNET_free_non_null (pc->nuid);
1112 GNUNET_free_non_null (fi_root);
1113 GNUNET_free_non_null (fi_pos);
1114 GNUNET_free_non_null (ns);
1115 if ( (rh != NULL) &&
1117 GNUNET_BIO_read_close (rh, &emsg)) )
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Failed to resume publishing operation `%s': %s\n"),
1126 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1127 if (pc->serialization != NULL)
1128 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1129 GNUNET_free_non_null (pc->serialization);
1136 * Synchronize this publishing struct with its mirror
1137 * on disk. Note that all internal FS-operations that change
1138 * publishing structs should already call "sync" internally,
1139 * so this function is likely not useful for clients.
1141 * @param pc the struct to sync
1144 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1146 struct GNUNET_BIO_WriteHandle *wh;
1148 if (NULL == pc->serialization)
1149 pc->serialization = make_serialization_file_name (pc->h,
1151 if (NULL == pc->serialization)
1155 if (NULL == pc->fi->serialization)
1160 wh = get_write_handle (pc->h, "publish", pc->serialization);
1162 GNUNET_BIO_write_string (wh, pc->nid)) ||
1164 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1166 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1168 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1170 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1172 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1174 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1176 (void) GNUNET_BIO_write_close (wh);
1177 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1178 GNUNET_free (pc->serialization);
1179 pc->serialization = NULL;
1183 GNUNET_BIO_write_close (wh))
1185 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1186 GNUNET_free (pc->serialization);
1187 pc->serialization = NULL;
1194 * Synchronize this unindex struct with its mirror
1195 * on disk. Note that all internal FS-operations that change
1196 * publishing structs should already call "sync" internally,
1197 * so this function is likely not useful for clients.
1199 * @param uc the struct to sync
1202 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1204 struct GNUNET_BIO_WriteHandle *wh;
1206 if (UNINDEX_STATE_ABORTED == uc->state)
1208 if (NULL == uc->serialization)
1209 uc->serialization = make_serialization_file_name (uc->h,
1211 if (NULL == uc->serialization)
1213 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1215 GNUNET_BIO_write_string (wh, uc->filename)) ||
1217 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1219 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1221 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1222 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1224 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1225 ( (uc->state == UNINDEX_STATE_ERROR) &&
1227 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1229 (void) GNUNET_BIO_write_close (wh);
1230 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1231 GNUNET_free (uc->serialization);
1232 uc->serialization = NULL;
1236 GNUNET_BIO_write_close (wh))
1238 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1239 GNUNET_free (uc->serialization);
1240 uc->serialization = NULL;
1247 * Synchronize this search result with its mirror
1248 * on disk. Note that all internal FS-operations that change
1249 * publishing structs should already call "sync" internally,
1250 * so this function is likely not useful for clients.
1252 * @param key key for the search result
1253 * @param sr the struct to sync
1256 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1257 struct SearchResult *sr)
1259 struct GNUNET_BIO_WriteHandle *wh;
1262 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1263 (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1265 if (NULL == sr->serialization)
1266 sr->serialization = make_serialization_file_name (sr->sc->h,
1268 if (NULL == sr->serialization)
1270 wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1271 uris = GNUNET_FS_uri_to_string (sr->uri);
1273 GNUNET_BIO_write_string (wh, uris)) ||
1275 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1277 GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1279 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1281 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1283 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1285 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1288 GNUNET_BIO_write_close (wh))
1293 GNUNET_free_non_null (uris);
1296 GNUNET_free_non_null (uris);
1298 (void) GNUNET_BIO_write_close (wh);
1299 GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1300 GNUNET_free (sr->serialization);
1301 sr->serialization = NULL;
1306 * Synchronize this search struct with its mirror
1307 * on disk. Note that all internal FS-operations that change
1308 * publishing structs should already call "sync" internally,
1309 * so this function is likely not useful for clients.
1311 * @param sc the struct to sync
1314 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1316 struct GNUNET_BIO_WriteHandle *wh;
1317 struct GNUNET_FS_SearchContext *scc;
1321 if (NULL == sc->serialization)
1322 sc->serialization = make_serialization_file_name (sc->h,
1324 if (NULL == sc->serialization)
1326 wh = get_write_handle (sc->h, "search", sc->serialization);
1327 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1328 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1329 uris = GNUNET_FS_uri_to_string (sc->uri);
1330 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1332 GNUNET_BIO_write_string (wh, uris)) ||
1334 GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1336 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1338 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1340 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1342 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1346 scc = sc->child_head;
1349 if (scc->serialization == NULL)
1352 GNUNET_BIO_write_string (wh, scc->serialization))
1356 GNUNET_BIO_write_string (wh, NULL);
1358 GNUNET_BIO_write_close (wh))
1366 (void) GNUNET_BIO_write_close (wh);
1367 GNUNET_free_non_null (uris);
1368 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1369 GNUNET_free (sc->serialization);
1370 sc->serialization = NULL;
1375 * Deserialize information about pending publish operations.
1377 * @param h master context
1380 deserialize_publish (struct GNUNET_FS_Handle *h)
1384 dn = get_serialization_file_name (h, "publish", "");
1387 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1393 * Function called with a filename of serialized unindexing operation
1396 * @param cls the 'struct GNUNET_FS_Handle*'
1397 * @param filename complete filename (absolute path)
1398 * @return GNUNET_OK (continue to iterate)
1401 deserialize_unindex_file (void *cls,
1402 const char *filename)
1404 struct GNUNET_FS_Handle *h = cls;
1405 struct GNUNET_BIO_ReadHandle *rh;
1406 struct GNUNET_FS_UnindexContext *uc;
1407 struct GNUNET_FS_ProgressInfo pi;
1411 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1413 uc->serialization = get_serialization_short_name (filename);
1414 rh = GNUNET_BIO_read_open (filename);
1418 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1420 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1422 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1424 GNUNET_BIO_read_int32 (rh, &state)) )
1426 uc->state = (enum UnindexState) state;
1429 case UNINDEX_STATE_HASHING:
1431 case UNINDEX_STATE_FS_NOTIFY:
1433 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1436 case UNINDEX_STATE_DS_REMOVE:
1438 case UNINDEX_STATE_COMPLETE:
1440 case UNINDEX_STATE_ERROR:
1442 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1445 case UNINDEX_STATE_ABORTED:
1452 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1453 pi.value.unindex.specifics.resume.message = uc->emsg;
1454 GNUNET_FS_unindex_make_status_ (&pi,
1456 (uc->state == UNINDEX_STATE_COMPLETE)
1461 case UNINDEX_STATE_HASHING:
1462 GNUNET_CRYPTO_hash_file (uc->h->sched,
1463 GNUNET_SCHEDULER_PRIORITY_IDLE,
1466 &GNUNET_FS_unindex_process_hash_,
1469 case UNINDEX_STATE_FS_NOTIFY:
1470 uc->state = UNINDEX_STATE_HASHING;
1471 GNUNET_FS_unindex_process_hash_ (uc,
1474 case UNINDEX_STATE_DS_REMOVE:
1475 GNUNET_FS_unindex_do_remove_ (uc);
1477 case UNINDEX_STATE_COMPLETE:
1478 case UNINDEX_STATE_ERROR:
1479 /* no need to resume any operation, we were done */
1485 GNUNET_BIO_read_close (rh, &emsg))
1487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1488 _("Failure while resuming unindexing operation `%s': %s\n"),
1495 GNUNET_free_non_null (uc->filename);
1496 if ( (rh != NULL) &&
1498 GNUNET_BIO_read_close (rh, &emsg)) )
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 _("Failed to resume unindexing operation `%s': %s\n"),
1506 if (uc->serialization != NULL)
1507 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1508 GNUNET_free_non_null (uc->serialization);
1515 * Deserialize information about pending publish operations.
1517 * @param h master context
1520 deserialize_unindex (struct GNUNET_FS_Handle *h)
1524 dn = get_serialization_file_name (h, "unindex", "");
1527 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1533 * Function called with a filename of serialized search result
1536 * @param cls the 'struct GNUNET_FS_SearchContext*'
1537 * @param filename complete filename (absolute path)
1538 * @return GNUNET_OK (continue to iterate)
1541 deserialize_search_result (void *cls,
1542 const char *filename)
1544 struct GNUNET_FS_SearchContext *sc = cls;
1549 struct GNUNET_BIO_ReadHandle *rh;
1550 struct SearchResult *sr;
1551 GNUNET_HashCode key;
1553 ser = get_serialization_short_name (filename);
1554 rh = GNUNET_BIO_read_open (filename);
1559 GNUNET_snprintf (pbuf,
1565 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1572 sr = GNUNET_malloc (sizeof (struct SearchResult));
1573 sr->serialization = ser;
1575 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1576 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1577 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1578 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1580 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1582 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1584 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1586 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1588 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1590 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1593 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1599 GNUNET_free_non_null (emsg);
1600 GNUNET_free_non_null (uris);
1601 if (sr->uri != NULL)
1602 GNUNET_FS_uri_destroy (sr->uri);
1603 if (sr->meta != NULL)
1604 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1605 GNUNET_free (sr->serialization);
1612 * Iterator over search results signaling resume to the client for
1615 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1616 * @param key current key code
1617 * @param value value in the hash map, the 'struct SearchResult'
1618 * @return GNUNET_YES (we should continue to iterate)
1621 signal_result_resume (void *cls,
1622 const GNUNET_HashCode * key,
1625 struct GNUNET_FS_SearchContext *sc = cls;
1626 struct GNUNET_FS_ProgressInfo pi;
1627 struct SearchResult *sr = value;
1629 if (0 == sr->mandatory_missing)
1631 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1632 pi.value.search.specifics.resume_result.meta = sr->meta;
1633 pi.value.search.specifics.resume_result.uri = sr->uri;
1634 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1635 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1636 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1637 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1640 GNUNET_FS_search_start_probe_ (sr);
1646 * Iterator over search results freeing each.
1648 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1649 * @param key current key code
1650 * @param value value in the hash map, the 'struct SearchResult'
1651 * @return GNUNET_YES (we should continue to iterate)
1654 free_result (void *cls,
1655 const GNUNET_HashCode * key,
1658 struct SearchResult *sr = value;
1660 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1661 GNUNET_FS_uri_destroy (sr->uri);
1668 * Free memory allocated by the search context and its children
1670 * @param sc search context to free
1673 free_search_context (struct GNUNET_FS_SearchContext *sc)
1675 struct GNUNET_FS_SearchContext *scc;
1677 while (NULL != (scc = sc->child_head))
1679 GNUNET_CONTAINER_DLL_remove (sc->child_head,
1682 free_search_context (scc);
1684 GNUNET_free_non_null (sc->emsg);
1685 if (sc->serialization != NULL)
1686 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1687 /* FIXME: remove 'pbuf' directory with search results as well! */
1688 GNUNET_free_non_null (sc->serialization);
1689 if (sc->uri != NULL)
1690 GNUNET_FS_uri_destroy (sc->uri);
1691 if (sc->master_result_map != NULL)
1693 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1696 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1703 * Deserialize a search.
1705 * @param h overall context
1706 * @param rh file to deserialize from
1707 * @param parent parent search
1708 * @param serialization name under which the search was serialized
1710 static struct GNUNET_FS_SearchContext *
1711 deserialize_search (struct GNUNET_FS_Handle *h,
1712 struct GNUNET_BIO_ReadHandle *rh,
1713 struct GNUNET_FS_SearchContext *parent,
1714 const char *serialization)
1716 struct GNUNET_FS_SearchContext *sc;
1717 struct GNUNET_FS_SearchContext *scc;
1718 struct GNUNET_BIO_ReadHandle *rhc;
1720 struct GNUNET_FS_ProgressInfo pi;
1730 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1731 sc->parent = parent;
1733 sc->serialization = GNUNET_strdup (serialization);
1735 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
1736 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1737 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1738 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1740 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1742 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1744 GNUNET_BIO_read_int32 (rh, &options)) ||
1746 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1748 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1750 /* FIXME: adjust start_time.value */
1751 sc->options = (enum GNUNET_FS_SearchOptions) options;
1752 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1753 GNUNET_snprintf (pbuf,
1759 dn = get_serialization_file_name (h, pbuf, "");
1762 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
1765 if ('\0' == in_pause)
1768 GNUNET_FS_search_start_searching_ (sc))
1774 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
1776 if (child_ser == NULL)
1778 rhc = get_read_handle (h, "search-children", child_ser);
1781 scc = deserialize_search (h, rhc, sc, child_ser);
1783 GNUNET_CONTAINER_DLL_insert (sc->child_head,
1788 GNUNET_BIO_read_close (rhc, &emsg))
1790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1791 _("Failed to resume sub-search `%s': %s\n"),
1797 GNUNET_free (child_ser);
1800 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1803 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1804 pi.value.search.specifics.resume.message = sc->emsg;
1805 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1806 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1808 scc = sc->child_head;
1811 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1812 pi.value.search.specifics.resume.message = scc->emsg;
1813 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1814 scc->client_info = GNUNET_FS_search_make_status_ (&pi,
1819 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1820 &signal_result_resume,
1825 GNUNET_free_non_null (emsg);
1826 free_search_context (sc);
1827 GNUNET_free_non_null (uris);
1833 * Function called with a filename of serialized search operation
1836 * @param cls the 'struct GNUNET_FS_Handle*'
1837 * @param filename complete filename (absolute path)
1838 * @return GNUNET_OK (continue to iterate)
1841 deserialize_search_file (void *cls,
1842 const char *filename)
1844 struct GNUNET_FS_Handle *h = cls;
1847 struct GNUNET_BIO_ReadHandle *rh;
1848 struct GNUNET_FS_SearchContext *sc;
1850 ser = get_serialization_short_name (filename);
1851 rh = GNUNET_BIO_read_open (filename);
1856 GNUNET_FS_remove_sync_file_ (h, "search", ser);
1861 sc = deserialize_search (h, rh, NULL, ser);
1864 GNUNET_BIO_read_close (rh, &emsg))
1866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1867 _("Failure while resuming unindexing operation `%s': %s\n"),
1877 * Deserialize information about pending search operations.
1879 * @param h master context
1882 deserialize_search_master (struct GNUNET_FS_Handle *h)
1886 dn = get_serialization_file_name (h, "search", "");
1889 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
1895 * Setup a connection to the file-sharing service.
1897 * @param sched scheduler to use
1898 * @param cfg configuration to use
1899 * @param client_name unique identifier for this client
1900 * @param upcb function to call to notify about FS actions
1901 * @param upcb_cls closure for upcb
1902 * @param flags specific attributes for fs-operations
1903 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1904 * @return NULL on error
1906 struct GNUNET_FS_Handle *
1907 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1908 const struct GNUNET_CONFIGURATION_Handle *cfg,
1909 const char *client_name,
1910 GNUNET_FS_ProgressCallback upcb,
1912 enum GNUNET_FS_Flags flags,
1915 struct GNUNET_FS_Handle *ret;
1916 struct GNUNET_CLIENT_Connection *client;
1917 enum GNUNET_FS_OPTIONS opt;
1920 client = GNUNET_CLIENT_connect (sched,
1925 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1928 ret->client_name = GNUNET_strdup (client_name);
1930 ret->upcb_cls = upcb_cls;
1931 ret->client = client;
1933 ret->max_parallel_downloads = 1;
1934 ret->max_parallel_requests = 1;
1935 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1936 va_start (ap, flags);
1937 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1941 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1942 ret->max_parallel_downloads = va_arg (ap, unsigned int);
1944 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1945 ret->max_parallel_requests = va_arg (ap, unsigned int);
1949 GNUNET_free (ret->client_name);
1956 // FIXME: setup receive-loop with client (do we need one?)
1957 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1959 /* FIXME: could write one generic deserialization
1960 function instead of these four... */
1961 deserialize_publish (ret);
1962 deserialize_search_master (ret);
1963 /* FIXME: deserialize downloads that are NOT part of searches */
1964 deserialize_unindex (ret);
1971 * Close our connection with the file-sharing service.
1972 * The callback given to GNUNET_FS_start will no longer be
1973 * called after this function returns.
1975 * @param h handle that was returned from GNUNET_FS_start
1978 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1980 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1982 // FIXME: generate SUSPEND events and clean up state!
1984 // FIXME: terminate receive-loop with client (do we need one?)
1985 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1986 GNUNET_SCHEDULER_cancel (h->sched,
1988 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1989 GNUNET_free (h->client_name);