2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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"
35 * Start the given job (send signal, remove from pending queue, update
36 * counters and state).
38 * @param qe job to start
41 start_job (struct GNUNET_FS_QueueEntry *qe)
43 GNUNET_assert (NULL == qe->client);
44 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
45 if (qe->client == NULL)
50 qe->start (qe->cls, qe->client);
52 qe->h->active_blocks += qe->blocks;
53 qe->start_time = GNUNET_TIME_absolute_get ();
54 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
57 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
65 * Stop the given job (send signal, remove from active queue, update
66 * counters and state).
68 * @param qe job to stop
71 stop_job (struct GNUNET_FS_QueueEntry *qe)
75 qe->h->active_downloads--;
76 qe->h->active_blocks -= qe->blocks;
77 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
78 GNUNET_TIME_absolute_get_duration (qe->start_time));
79 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
82 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
90 * Process the jobs in the job queue, possibly starting some
91 * and stopping others.
93 * @param cls the 'struct GNUNET_FS_Handle'
94 * @param tc scheduler context
97 process_job_queue (void *cls,
98 const struct GNUNET_SCHEDULER_TaskContext *tc)
100 struct GNUNET_FS_Handle *h = cls;
101 struct GNUNET_FS_QueueEntry *qe;
102 struct GNUNET_FS_QueueEntry *next;
103 struct GNUNET_TIME_Relative run_time;
104 struct GNUNET_TIME_Relative restart_at;
105 struct GNUNET_TIME_Relative rst;
106 struct GNUNET_TIME_Absolute end_time;
108 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
109 next = h->pending_head;
110 while (NULL != (qe = next))
113 if (h->running_head == NULL)
118 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
119 (h->active_downloads + 1 <= h->max_parallel_downloads) )
125 if (h->pending_head == NULL)
126 return; /* no need to stop anything */
127 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
128 next = h->running_head;
129 while (NULL != (qe = next))
132 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
133 qe->blocks * qe->start_times);
134 end_time = GNUNET_TIME_absolute_add (qe->start_time,
136 rst = GNUNET_TIME_absolute_get_remaining (end_time);
137 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
138 if (rst.rel_value > 0)
142 h->queue_job = GNUNET_SCHEDULER_add_delayed (restart_at,
149 * Add a job to the queue.
151 * @param h handle to the overall FS state
152 * @param start function to call to begin the job
153 * @param stop function to call to pause the job, or on dequeue (if the job was running)
154 * @param cls closure for start and stop
155 * @param blocks number of blocks this jobs uses
156 * @return queue handle
158 struct GNUNET_FS_QueueEntry *
159 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
160 GNUNET_FS_QueueStart start,
161 GNUNET_FS_QueueStop stop,
165 struct GNUNET_FS_QueueEntry *qe;
167 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
172 qe->queue_time = GNUNET_TIME_absolute_get ();
174 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
178 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
179 GNUNET_SCHEDULER_cancel (h->queue_job);
181 = GNUNET_SCHEDULER_add_now (&process_job_queue,
188 * Dequeue a job from the queue.
189 * @param qh handle for the job
192 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
194 struct GNUNET_FS_Handle *h;
197 if (qh->client != NULL)
199 GNUNET_CONTAINER_DLL_remove (h->pending_head,
203 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
204 GNUNET_SCHEDULER_cancel (h->queue_job);
206 = GNUNET_SCHEDULER_add_now (&process_job_queue,
212 * Create a top-level activity entry.
214 * @param h global fs handle
215 * @param ssf suspend signal function to use
216 * @param ssf_cls closure for ssf
217 * @return fresh top-level activity handle
219 struct TopLevelActivity *
220 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
221 SuspendSignalFunction ssf,
224 struct TopLevelActivity *ret;
226 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
228 ret->ssf_cls = ssf_cls;
229 GNUNET_CONTAINER_DLL_insert (h->top_head,
237 * Destroy a top-level activity entry.
239 * @param h global fs handle
240 * @param top top level activity entry
243 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
244 struct TopLevelActivity *top)
246 GNUNET_CONTAINER_DLL_remove (h->top_head,
255 * Closure for "data_reader_file".
260 * Name of the file to read.
265 * File descriptor, NULL if it has not yet been opened.
267 struct GNUNET_DISK_FileHandle *fd;
272 * Function that provides data by reading from a file.
274 * @param cls closure (points to the file information)
275 * @param offset offset to read from; it is possible
276 * that the caller might need to go backwards
278 * @param max maximum number of bytes that should be
279 * copied to buf; readers are not allowed
280 * to provide less data unless there is an error;
281 * a value of "0" will be used at the end to allow
282 * the reader to clean up its internal state
283 * @param buf where the reader should write the data
284 * @param emsg location for the reader to store an error message
285 * @return number of bytes written, usually "max", 0 on error
288 GNUNET_FS_data_reader_file_(void *cls,
294 struct FileInfo *fi = cls;
300 GNUNET_DISK_file_close (fi->fd);
301 GNUNET_free (fi->filename);
307 fi->fd = GNUNET_DISK_file_open (fi->filename,
308 GNUNET_DISK_OPEN_READ,
309 GNUNET_DISK_PERM_NONE);
312 GNUNET_asprintf (emsg,
313 _("Could not open file `%s': %s"),
319 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
320 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
323 GNUNET_asprintf (emsg,
324 _("Could not read file `%s': %s"),
331 GNUNET_asprintf (emsg,
332 _("Short read reading from file `%s'!"),
341 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
343 * @param filename file to read
344 * @return closure to use, NULL on error
347 GNUNET_FS_make_file_reader_context_ (const char *filename)
351 fi = GNUNET_malloc (sizeof(struct FileInfo));
352 fi->filename = GNUNET_STRINGS_filename_expand (filename);
353 if (fi->filename == NULL)
363 * Function that provides data by copying from a buffer.
365 * @param cls closure (points to the buffer)
366 * @param offset offset to read from; it is possible
367 * that the caller might need to go backwards
369 * @param max maximum number of bytes that should be
370 * copied to buf; readers are not allowed
371 * to provide less data unless there is an error;
372 * a value of "0" will be used at the end to allow
373 * the reader to clean up its internal state
374 * @param buf where the reader should write the data
375 * @param emsg location for the reader to store an error message
376 * @return number of bytes written, usually "max", 0 on error
379 GNUNET_FS_data_reader_copy_ (void *cls,
389 GNUNET_free_non_null (data);
392 memcpy (buf, &data[offset], max);
398 * Return the full filename where we would store state information
399 * (for serialization/deserialization).
401 * @param h master context
402 * @param ext component of the path
403 * @param ent entity identifier (or emtpy string for the directory)
404 * @return NULL on error
407 get_serialization_file_name (struct GNUNET_FS_Handle *h,
414 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
415 return NULL; /* persistence not requested */
417 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
422 GNUNET_asprintf (&ret,
431 GNUNET_free (basename);
437 * Return the full filename where we would store state information
438 * (for serialization/deserialization) that is associated with a
441 * @param h master context
442 * @param ext component of the path
443 * @param uni name of the parent operation
444 * @param ent entity identifier (or emtpy string for the directory)
445 * @return NULL on error
448 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
456 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
457 return NULL; /* persistence not requested */
459 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
464 GNUNET_asprintf (&ret,
465 "%s%s%s%s%s%s%s.dir%s%s",
475 GNUNET_free (basename);
481 * Return a read handle for deserialization.
483 * @param h master context
484 * @param ext component of the path
485 * @param ent entity identifier (or emtpy string for the directory)
486 * @return NULL on error
488 static struct GNUNET_BIO_ReadHandle *
489 get_read_handle (struct GNUNET_FS_Handle *h,
494 struct GNUNET_BIO_ReadHandle *ret;
496 fn = get_serialization_file_name (h, ext, ent);
499 ret = GNUNET_BIO_read_open (fn);
506 * Return a write handle for serialization.
508 * @param h master context
509 * @param ext component of the path
510 * @param ent entity identifier (or emtpy string for the directory)
511 * @return NULL on error
513 static struct GNUNET_BIO_WriteHandle *
514 get_write_handle (struct GNUNET_FS_Handle *h,
519 struct GNUNET_BIO_WriteHandle *ret;
521 fn = get_serialization_file_name (h, ext, ent);
526 ret = GNUNET_BIO_write_open (fn);
535 * Return a write handle for serialization.
537 * @param h master context
538 * @param ext component of the path
539 * @param uni name of parent
540 * @param ent entity identifier (or emtpy string for the directory)
541 * @return NULL on error
543 static struct GNUNET_BIO_WriteHandle *
544 get_write_handle_in_dir (struct GNUNET_FS_Handle *h,
550 struct GNUNET_BIO_WriteHandle *ret;
552 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
555 ret = GNUNET_BIO_write_open (fn);
562 * Remove serialization/deserialization file from disk.
564 * @param h master context
565 * @param ext component of the path
566 * @param ent entity identifier
569 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
575 if ( (NULL == ent) ||
576 (0 == strlen (ent)) )
581 filename = get_serialization_file_name (h, ext, ent);
582 if (filename != NULL)
584 if (0 != UNLINK (filename))
585 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
588 GNUNET_free (filename);
594 * Remove serialization/deserialization file from disk.
596 * @param h master context
597 * @param ext component of the path
598 * @param uni parent name
599 * @param ent entity identifier
602 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
609 if ( (NULL == ent) ||
610 (0 == strlen (ent)) )
615 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
616 if (filename != NULL)
618 if (0 != UNLINK (filename))
619 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
622 GNUNET_free (filename);
628 * Remove serialization/deserialization directory from disk.
630 * @param h master context
631 * @param ext component of the path
632 * @param uni unique name of parent
635 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
643 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
646 if ( (GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
647 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)) )
648 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
656 * Serialize a 'start_time'. Since we use start-times to
657 * calculate the duration of some operation, we actually
658 * do not serialize the absolute time but the (relative)
659 * duration since the start time. When we then
660 * deserialize the start time, we take the current time and
661 * subtract that duration so that we get again an absolute
662 * time stamp that will result in correct performance
665 * @param wh handle for writing
666 * @param timestamp time to serialize
667 * @return GNUNET_OK on success
670 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
671 struct GNUNET_TIME_Absolute timestamp)
673 struct GNUNET_TIME_Relative dur;
675 dur = GNUNET_TIME_absolute_get_duration (timestamp);
676 return GNUNET_BIO_write_int64 (wh, dur.rel_value);
681 * Serialize a 'start_time'. Since we use start-times to
682 * calculate the duration of some operation, we actually
683 * do not serialize the absolute time but the (relative)
684 * duration since the start time. When we then
685 * deserialize the start time, we take the current time and
686 * subtract that duration so that we get again an absolute
687 * time stamp that will result in correct performance
690 * @param rh handle for reading
691 * @param timestamp where to write the deserialized timestamp
692 * @return GNUNET_OK on success
695 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
696 struct GNUNET_TIME_Absolute *timestamp)
698 struct GNUNET_TIME_Relative dur;
700 GNUNET_BIO_read_int64 (rh, &dur.rel_value))
701 return GNUNET_SYSERR;
702 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
709 * Using the given serialization filename, try to deserialize
710 * the file-information tree associated with it.
712 * @param h master context
713 * @param filename name of the file (without directory) with
715 * @return NULL on error
717 static struct GNUNET_FS_FileInformation *
718 deserialize_file_information (struct GNUNET_FS_Handle *h,
719 const char *filename);
723 * Using the given serialization filename, try to deserialize
724 * the file-information tree associated with it.
726 * @param h master context
727 * @param fn name of the file (without directory) with
729 * @param rh handle for reading
730 * @return NULL on error
732 static struct GNUNET_FS_FileInformation *
733 deserialize_fi_node (struct GNUNET_FS_Handle *h,
735 struct GNUNET_BIO_ReadHandle *rh)
737 struct GNUNET_FS_FileInformation *ret;
738 struct GNUNET_FS_FileInformation *nxt;
746 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
751 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
757 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
759 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
762 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
764 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
766 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
769 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
771 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
773 read_start_time (rh, &ret->start_time)) ||
775 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
777 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
779 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value)) ||
781 GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
783 GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
785 GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)) )
792 case 0: /* file-insert */
794 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
799 ret->is_directory = GNUNET_NO;
800 ret->data.file.do_index = GNUNET_NO;
801 ret->data.file.have_hash = GNUNET_NO;
802 ret->data.file.index_start_confirmed = GNUNET_NO;
803 if (GNUNET_NO == ret->is_published)
805 if (NULL == ret->filename)
807 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
808 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
809 if (ret->data.file.reader_cls == NULL)
812 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
820 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
821 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
825 case 1: /* file-index, no hash */
826 if (NULL == ret->filename)
832 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
837 ret->is_directory = GNUNET_NO;
838 ret->data.file.do_index = GNUNET_YES;
839 ret->data.file.have_hash = GNUNET_NO;
840 ret->data.file.index_start_confirmed = GNUNET_NO;
841 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
842 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
844 case 2: /* file-index-with-hash */
845 if (NULL == ret->filename)
851 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
853 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
858 ret->is_directory = GNUNET_NO;
859 ret->data.file.do_index = GNUNET_YES;
860 ret->data.file.have_hash = GNUNET_YES;
861 ret->data.file.index_start_confirmed = GNUNET_NO;
862 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
863 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
865 case 3: /* file-index-with-hash-confirmed */
866 if (NULL == ret->filename)
872 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
874 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
879 ret->is_directory = GNUNET_NO;
880 ret->data.file.do_index = GNUNET_YES;
881 ret->data.file.have_hash = GNUNET_YES;
882 ret->data.file.index_start_confirmed = GNUNET_YES;
883 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
884 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
886 case 4: /* directory */
887 ret->is_directory = GNUNET_YES;
889 GNUNET_BIO_read_int32 (rh, &dsize)) ||
890 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
892 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
894 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
899 ret->data.dir.dir_size = (uint32_t) dsize;
900 if (filename != NULL)
902 ret->data.dir.entries = deserialize_file_information (h, filename);
903 GNUNET_free (filename);
905 nxt = ret->data.dir.entries;
917 ret->serialization = GNUNET_strdup (fn);
919 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
924 if (filename != NULL)
926 ret->next = deserialize_file_information (h, filename);
927 GNUNET_free (filename);
930 GNUNET_free_non_null (ksks);
931 GNUNET_free_non_null (chks);
934 GNUNET_free_non_null (ksks);
935 GNUNET_free_non_null (chks);
936 GNUNET_free_non_null (filename);
937 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
943 * Using the given serialization filename, try to deserialize
944 * the file-information tree associated with it.
946 * @param h master context
947 * @param filename name of the file (without directory) with
949 * @return NULL on error
951 static struct GNUNET_FS_FileInformation *
952 deserialize_file_information (struct GNUNET_FS_Handle *h,
953 const char *filename)
955 struct GNUNET_FS_FileInformation *ret;
956 struct GNUNET_BIO_ReadHandle *rh;
959 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
962 ret = deserialize_fi_node (h, filename, rh);
964 GNUNET_BIO_read_close (rh, &emsg))
966 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
967 _("Failed to resume publishing information `%s': %s\n"),
974 if (0 != UNLINK (filename))
975 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
984 * Given a serialization name (full absolute path), return the
985 * basename of the file (without the path), which must only
986 * consist of the 6 random characters.
988 * @param fullname name to extract the basename from
989 * @return copy of the basename, NULL on error
992 get_serialization_short_name (const char *fullname)
999 /* FIXME: we could do this faster since we know
1000 the length of 'end'... */
1001 while ('\0' != *nxt)
1003 if (DIR_SEPARATOR == *nxt)
1007 if ( (end == NULL) ||
1008 (strlen (end) == 0) )
1013 GNUNET_break (6 == strlen (end));
1014 return GNUNET_strdup (end);
1019 * Create a new random name for serialization. Also checks if persistence
1020 * is enabled and returns NULL if not.
1022 * @param h master context
1023 * @param ext component of the path
1024 * @return NULL on errror
1027 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1034 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1035 return NULL; /* persistence not requested */
1036 dn = get_serialization_file_name (h, ext, "");
1040 GNUNET_DISK_directory_create_for_file (dn))
1045 fn = GNUNET_DISK_mktemp (dn);
1048 return NULL; /* epic fail */
1049 ret = get_serialization_short_name (fn);
1056 * Create a new random name for serialization. Also checks if persistence
1057 * is enabled and returns NULL if not.
1059 * @param h master context
1060 * @param ext component of the path
1061 * @param uni name of parent
1062 * @return NULL on errror
1065 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1073 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1074 return NULL; /* persistence not requested */
1075 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1079 GNUNET_DISK_directory_create_for_file (dn))
1084 fn = GNUNET_DISK_mktemp (dn);
1087 return NULL; /* epic fail */
1088 ret = get_serialization_short_name (fn);
1095 * Copy all of the data from the reader to the write handle.
1097 * @param wh write handle
1098 * @param fi file with reader
1099 * @return GNUNET_OK on success
1102 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1103 struct GNUNET_FS_FileInformation * fi)
1105 char buf[32 * 1024];
1113 while (off < fi->data.file.file_size)
1115 left = GNUNET_MIN (sizeof(buf), fi->data.file.file_size - off);
1116 ret = fi->data.file.reader (fi->data.file.reader_cls,
1123 return GNUNET_SYSERR;
1126 GNUNET_BIO_write (wh, buf, ret))
1127 return GNUNET_SYSERR;
1135 * Create a temporary file on disk to store the current
1138 * @param fi file information to sync with disk
1141 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
1144 struct GNUNET_BIO_WriteHandle *wh;
1149 if (NULL == fi->serialization)
1150 fi->serialization = make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1151 if (NULL == fi->serialization)
1153 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1156 GNUNET_free (fi->serialization);
1157 fi->serialization = NULL;
1160 if (GNUNET_YES == fi->is_directory)
1162 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1164 else if (GNUNET_YES == fi->data.file.have_hash)
1166 else if (GNUNET_YES == fi->data.file.do_index)
1170 if (fi->keywords != NULL)
1171 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1174 if (fi->chk_uri != NULL)
1175 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1179 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1181 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1183 GNUNET_BIO_write_string (wh, ksks)) ||
1185 GNUNET_BIO_write_string (wh, chks)) ||
1187 write_start_time (wh, fi->start_time)) ||
1189 GNUNET_BIO_write_string (wh, fi->emsg)) ||
1191 GNUNET_BIO_write_string (wh, fi->filename)) ||
1193 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value)) ||
1195 GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1197 GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1199 GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)) )
1204 GNUNET_free_non_null (chks);
1206 GNUNET_free_non_null (ksks);
1211 case 0: /* file-insert */
1213 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1218 if ( (GNUNET_NO == fi->is_published) &&
1219 (NULL == fi->filename) )
1221 copy_from_reader (wh, fi))
1227 case 1: /* file-index, no hash */
1228 if (NULL == fi->filename)
1234 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1240 case 2: /* file-index-with-hash */
1241 case 3: /* file-index-with-hash-confirmed */
1242 if (NULL == fi->filename)
1248 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1250 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
1256 case 4: /* directory */
1258 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1260 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
1262 GNUNET_BIO_write_string (wh,
1263 (fi->data.dir.entries == NULL)
1265 : fi->data.dir.entries->serialization)) )
1276 GNUNET_BIO_write_string (wh, (fi->next != NULL) ? fi->next->serialization : NULL))
1282 GNUNET_BIO_write_close (wh))
1291 (void) GNUNET_BIO_write_close (wh);
1292 GNUNET_free_non_null (chks);
1293 GNUNET_free_non_null (ksks);
1294 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1297 if (0 != UNLINK (fn))
1298 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1301 GNUNET_free (fi->serialization);
1302 fi->serialization = NULL;
1308 * Find the entry in the file information struct where the
1309 * serialization filename matches the given name.
1311 * @param pos file information to search
1312 * @param srch filename to search for
1313 * @return NULL if srch was not found in this subtree
1315 static struct GNUNET_FS_FileInformation *
1316 find_file_position (struct GNUNET_FS_FileInformation *pos,
1319 struct GNUNET_FS_FileInformation *r;
1323 if (0 == strcmp (srch,
1324 pos->serialization))
1326 if (pos->is_directory)
1328 r = find_file_position (pos->data.dir.entries,
1340 * Signal the FS's progress function that we are resuming
1343 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1344 * @param fi the entry in the publish-structure
1345 * @param length length of the file or directory
1346 * @param meta metadata for the file or directory (can be modified)
1347 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1348 * @param bo block options (can be modified)
1349 * @param do_index should we index?
1350 * @param client_info pointer to client context set upon creation (can be modified)
1351 * @return GNUNET_OK to continue (always)
1354 fip_signal_resume(void *cls,
1355 struct GNUNET_FS_FileInformation *fi,
1357 struct GNUNET_CONTAINER_MetaData *meta,
1358 struct GNUNET_FS_Uri **uri,
1359 struct GNUNET_FS_BlockOptions *bo,
1363 struct GNUNET_FS_PublishContext *sc = cls;
1364 struct GNUNET_FS_ProgressInfo pi;
1366 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1367 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1368 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1369 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1375 * Function called with a filename of serialized publishing operation
1378 * @param cls the 'struct GNUNET_FS_Handle*'
1379 * @param filename complete filename (absolute path)
1380 * @return GNUNET_OK (continue to iterate)
1383 deserialize_publish_file (void *cls,
1384 const char *filename)
1386 struct GNUNET_FS_Handle *h = cls;
1387 struct GNUNET_BIO_ReadHandle *rh;
1388 struct GNUNET_FS_PublishContext *pc;
1396 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1398 pc->serialization = get_serialization_short_name (filename);
1402 rh = GNUNET_BIO_read_open (filename);
1409 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1411 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1413 GNUNET_BIO_read_int32 (rh, &options)) ||
1415 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1417 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1419 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1421 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1426 pc->options = options;
1427 pc->all_done = all_done;
1428 if (NULL == fi_root)
1433 pc->fi = deserialize_file_information (h, fi_root);
1441 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1442 if (pc->namespace == NULL)
1444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1445 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1450 if ( (0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1451 (GNUNET_YES != pc->all_done) )
1453 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1454 if (NULL == pc->dsh)
1459 pc->fi_pos = find_file_position (pc->fi,
1461 GNUNET_free (fi_pos);
1463 if (pc->fi_pos == NULL)
1465 /* failed to find position for resuming, outch! Will start from root! */
1467 if (pc->all_done != GNUNET_YES)
1468 pc->fi_pos = pc->fi;
1471 GNUNET_free (fi_root);
1473 /* generate RESUME event(s) */
1474 GNUNET_FS_file_information_inspect (pc->fi,
1478 /* re-start publishing (if needed)... */
1479 if (pc->all_done != GNUNET_YES)
1481 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1482 &GNUNET_FS_publish_main_,
1485 GNUNET_BIO_read_close (rh, &emsg))
1487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1488 _("Failure while resuming publishing operation `%s': %s\n"),
1493 GNUNET_free_non_null (ns);
1494 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1497 GNUNET_free_non_null (pc->nid);
1498 GNUNET_free_non_null (pc->nuid);
1499 GNUNET_free_non_null (fi_root);
1500 GNUNET_free_non_null (fi_pos);
1501 GNUNET_free_non_null (ns);
1502 if ( (rh != NULL) &&
1504 GNUNET_BIO_read_close (rh, &emsg)) )
1506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1507 _("Failed to resume publishing operation `%s': %s\n"),
1513 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1514 if (0 != UNLINK (filename))
1515 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1516 GNUNET_free (pc->serialization);
1523 * Synchronize this publishing struct with its mirror
1524 * on disk. Note that all internal FS-operations that change
1525 * publishing structs should already call "sync" internally,
1526 * so this function is likely not useful for clients.
1528 * @param pc the struct to sync
1531 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1533 struct GNUNET_BIO_WriteHandle *wh;
1535 if (NULL == pc->serialization)
1536 pc->serialization = make_serialization_file_name (pc->h,
1537 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1538 if (NULL == pc->serialization)
1542 if (NULL == pc->fi->serialization)
1547 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1554 GNUNET_BIO_write_string (wh, pc->nid)) ||
1556 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1558 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1560 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1562 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1564 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1566 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1572 GNUNET_BIO_write_close (wh))
1581 (void) GNUNET_BIO_write_close (wh);
1582 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1583 GNUNET_free (pc->serialization);
1584 pc->serialization = NULL;
1589 * Synchronize this unindex struct with its mirror
1590 * on disk. Note that all internal FS-operations that change
1591 * publishing structs should already call "sync" internally,
1592 * so this function is likely not useful for clients.
1594 * @param uc the struct to sync
1597 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1599 struct GNUNET_BIO_WriteHandle *wh;
1601 if (NULL == uc->serialization)
1602 uc->serialization = make_serialization_file_name (uc->h,
1603 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1604 if (NULL == uc->serialization)
1606 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1613 GNUNET_BIO_write_string (wh, uc->filename)) ||
1615 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1617 write_start_time (wh, uc->start_time)) ||
1619 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1620 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1622 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1623 ( (uc->state == UNINDEX_STATE_ERROR) &&
1625 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1631 GNUNET_BIO_write_close (wh))
1640 (void) GNUNET_BIO_write_close (wh);
1641 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1642 GNUNET_free (uc->serialization);
1643 uc->serialization = NULL;
1648 * Serialize a download request.
1650 * @param wh the 'struct GNUNET_BIO_WriteHandle*'
1651 * @param dr the 'struct DownloadRequest'
1652 * @return GNUNET_YES on success, GNUNET_NO on error
1655 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1656 struct DownloadRequest *dr)
1661 GNUNET_BIO_write_int32 (wh, dr->state)) ||
1663 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1665 GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1667 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1669 if ( (dr->state == BRS_CHK_SET) &&
1671 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) )
1673 for (i=0;i<dr->num_children;i++)
1675 write_download_request (wh, dr->children[i]))
1682 * Read a download request tree.
1684 * @param rh stream to read from
1685 * @return value the 'struct DownloadRequest', NULL on error
1687 static struct DownloadRequest *
1688 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1690 struct DownloadRequest *dr;
1693 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1696 GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1698 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1700 GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1701 (dr->num_children > CHK_PER_INODE) ||
1703 GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1704 ( (dr->depth == 0) && (dr->num_children > 0) ) ||
1705 ( (dr->depth > 0) && (dr->num_children == 0) ) )
1708 dr->num_children = 0;
1711 if (dr->num_children > 0)
1712 dr->children = GNUNET_malloc (dr->num_children *
1713 sizeof (struct ContentHashKey));
1717 case BRS_RECONSTRUCT_DOWN:
1718 case BRS_RECONSTRUCT_META_UP:
1719 case BRS_RECONSTRUCT_UP:
1723 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1726 case BRS_DOWNLOAD_DOWN:
1727 case BRS_DOWNLOAD_UP:
1734 for (i=0;i<dr->num_children;i++)
1736 if (NULL == (dr->children[i] = read_download_request (rh)))
1738 dr->children[i]->parent = dr;
1742 GNUNET_FS_free_download_request_ (dr);
1748 * Compute the name of the sync file (or directory) for the given download
1751 * @param dc download context to compute for
1752 * @param uni unique filename to use, use "" for the directory name
1753 * @param ext extension to use, use ".dir" for our own subdirectory
1754 * @return the expanded file name, NULL for none
1757 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1764 if (dc->parent == NULL)
1765 return get_serialization_file_name (dc->h,
1766 (dc->search != NULL) ?
1767 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1768 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1770 if (dc->parent->serialization == NULL)
1772 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1775 GNUNET_asprintf (&epar,
1787 * Synchronize this download struct with its mirror
1788 * on disk. Note that all internal FS-operations that change
1789 * publishing structs should already call "sync" internally,
1790 * so this function is likely not useful for clients.
1792 * @param dc the struct to sync
1795 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1797 struct GNUNET_BIO_WriteHandle *wh;
1802 if (NULL == dc->serialization)
1804 dir = get_download_sync_filename (dc, "", "");
1808 GNUNET_DISK_directory_create_for_file (dir))
1813 fn = GNUNET_DISK_mktemp (dir);
1817 dc->serialization = get_serialization_short_name (fn);
1821 fn = get_download_sync_filename (dc, dc->serialization, "");
1824 GNUNET_free (dc->serialization);
1825 dc->serialization = NULL;
1830 wh = GNUNET_BIO_write_open (fn);
1833 GNUNET_free (dc->serialization);
1834 dc->serialization = NULL;
1838 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1839 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1840 uris = GNUNET_FS_uri_to_string (dc->uri);
1842 GNUNET_BIO_write_string (wh, uris)) ||
1844 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1846 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1848 GNUNET_BIO_write_string (wh, dc->filename)) ||
1850 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1852 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1854 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1856 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1858 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1860 write_start_time (wh, dc->start_time)) ||
1862 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1864 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1866 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) )
1871 if (NULL == dc->emsg)
1873 GNUNET_assert (dc->top_request != NULL);
1875 write_download_request (wh, dc->top_request))
1881 GNUNET_free_non_null (uris);
1884 GNUNET_BIO_write_close (wh))
1894 (void) GNUNET_BIO_write_close (wh);
1895 GNUNET_free_non_null (uris);
1896 if (0 != UNLINK (fn))
1897 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1899 GNUNET_free (dc->serialization);
1900 dc->serialization = NULL;
1905 * Synchronize this search result with its mirror
1906 * on disk. Note that all internal FS-operations that change
1907 * publishing structs should already call "sync" internally,
1908 * so this function is likely not useful for clients.
1910 * @param sr the struct to sync
1913 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1915 struct GNUNET_BIO_WriteHandle *wh;
1919 if (NULL == sr->serialization)
1920 sr->serialization = make_serialization_file_name_in_dir (sr->sc->h,
1921 (sr->sc->psearch_result == NULL)
1922 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1923 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1924 sr->sc->serialization);
1925 if (NULL == sr->serialization)
1927 wh = get_write_handle_in_dir (sr->sc->h,
1928 (sr->sc->psearch_result == NULL)
1929 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1930 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1931 sr->sc->serialization,
1938 uris = GNUNET_FS_uri_to_string (sr->uri);
1940 GNUNET_BIO_write_string (wh, uris)) ||
1942 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1944 GNUNET_BIO_write_string (wh, sr->update_search != NULL ? sr->update_search->serialization : NULL)) ||
1946 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1948 GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode))) ||
1950 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1952 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1954 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1956 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1962 GNUNET_BIO_write_close (wh))
1968 GNUNET_free_non_null (uris);
1971 GNUNET_free_non_null (uris);
1973 (void) GNUNET_BIO_write_close (wh);
1974 remove_sync_file_in_dir (sr->sc->h,
1975 (sr->sc->psearch_result == NULL)
1976 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1977 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1978 sr->sc->serialization,
1980 GNUNET_free (sr->serialization);
1981 sr->serialization = NULL;
1986 * Synchronize this search struct with its mirror
1987 * on disk. Note that all internal FS-operations that change
1988 * publishing structs should already call "sync" internally,
1989 * so this function is likely not useful for clients.
1991 * @param sc the struct to sync
1994 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1996 struct GNUNET_BIO_WriteHandle *wh;
1999 const char *category;
2001 category = (sc->psearch_result == NULL)
2002 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2003 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2004 if (NULL == sc->serialization)
2005 sc->serialization = make_serialization_file_name (sc->h,
2007 if (NULL == sc->serialization)
2010 wh = get_write_handle (sc->h, category, sc->serialization);
2016 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2017 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
2018 uris = GNUNET_FS_uri_to_string (sc->uri);
2019 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2021 GNUNET_BIO_write_string (wh, uris)) ||
2023 write_start_time (wh, sc->start_time)) ||
2025 GNUNET_BIO_write_string (wh, sc->emsg)) ||
2027 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2029 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2031 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
2039 GNUNET_BIO_write_close (wh))
2048 (void) GNUNET_BIO_write_close (wh);
2049 GNUNET_free_non_null (uris);
2050 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2051 GNUNET_free (sc->serialization);
2052 sc->serialization = NULL;
2057 * Function called with a filename of serialized unindexing operation
2060 * @param cls the 'struct GNUNET_FS_Handle*'
2061 * @param filename complete filename (absolute path)
2062 * @return GNUNET_OK (continue to iterate)
2065 deserialize_unindex_file (void *cls,
2066 const char *filename)
2068 struct GNUNET_FS_Handle *h = cls;
2069 struct GNUNET_BIO_ReadHandle *rh;
2070 struct GNUNET_FS_UnindexContext *uc;
2071 struct GNUNET_FS_ProgressInfo pi;
2075 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
2077 uc->serialization = get_serialization_short_name (filename);
2078 rh = GNUNET_BIO_read_open (filename);
2085 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
2087 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2089 read_start_time (rh, &uc->start_time)) ||
2091 GNUNET_BIO_read_int32 (rh, &state)) )
2096 uc->state = (enum UnindexState) state;
2099 case UNINDEX_STATE_HASHING:
2101 case UNINDEX_STATE_FS_NOTIFY:
2103 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
2109 case UNINDEX_STATE_DS_REMOVE:
2111 case UNINDEX_STATE_COMPLETE:
2113 case UNINDEX_STATE_ERROR:
2115 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
2125 uc->top = GNUNET_FS_make_top (h,
2126 &GNUNET_FS_unindex_signal_suspend_,
2128 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2129 pi.value.unindex.specifics.resume.message = uc->emsg;
2130 GNUNET_FS_unindex_make_status_ (&pi,
2132 (uc->state == UNINDEX_STATE_COMPLETE)
2137 case UNINDEX_STATE_HASHING:
2138 uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
2141 &GNUNET_FS_unindex_process_hash_,
2144 case UNINDEX_STATE_FS_NOTIFY:
2145 uc->state = UNINDEX_STATE_HASHING;
2146 GNUNET_FS_unindex_process_hash_ (uc,
2149 case UNINDEX_STATE_DS_REMOVE:
2150 GNUNET_FS_unindex_do_remove_ (uc);
2152 case UNINDEX_STATE_COMPLETE:
2153 case UNINDEX_STATE_ERROR:
2154 /* no need to resume any operation, we were done */
2160 GNUNET_BIO_read_close (rh, &emsg))
2162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2163 _("Failure while resuming unindexing operation `%s': %s\n"),
2170 GNUNET_free_non_null (uc->filename);
2171 if ( (rh != NULL) &&
2173 GNUNET_BIO_read_close (rh, &emsg)) )
2175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2176 _("Failed to resume unindexing operation `%s': %s\n"),
2181 if (uc->serialization != NULL)
2182 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
2183 GNUNET_free_non_null (uc->serialization);
2190 * Deserialize a download.
2192 * @param h overall context
2193 * @param rh file to deserialize from
2194 * @param parent parent download
2195 * @param search associated search
2196 * @param serialization name under which the search was serialized
2199 deserialize_download (struct GNUNET_FS_Handle *h,
2200 struct GNUNET_BIO_ReadHandle *rh,
2201 struct GNUNET_FS_DownloadContext *parent,
2202 struct GNUNET_FS_SearchResult *search,
2203 const char *serialization);
2207 * Deserialize a search.
2209 * @param h overall context
2210 * @param rh file to deserialize from
2211 * @param psearch_result parent search result
2212 * @param serialization name under which the search was serialized
2214 static struct GNUNET_FS_SearchContext *
2215 deserialize_search (struct GNUNET_FS_Handle *h,
2216 struct GNUNET_BIO_ReadHandle *rh,
2217 struct GNUNET_FS_SearchResult *psearch_result,
2218 const char *serialization);
2222 * Function called with a filename of serialized search result
2225 * @param cls the 'struct GNUNET_FS_SearchContext*'
2226 * @param filename complete filename (absolute path)
2227 * @return GNUNET_OK (continue to iterate)
2230 deserialize_search_result (void *cls,
2231 const char *filename)
2233 struct GNUNET_FS_SearchContext *sc = cls;
2239 struct GNUNET_BIO_ReadHandle *rh;
2240 struct GNUNET_BIO_ReadHandle *drh;
2241 struct GNUNET_FS_SearchResult *sr;
2243 ser = get_serialization_short_name (filename);
2244 rh = GNUNET_BIO_read_open (filename);
2249 remove_sync_file_in_dir (sc->h,
2250 (sc->psearch_result == NULL)
2251 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2252 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2263 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2264 sr->serialization = ser;
2266 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
2267 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2269 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
2271 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2273 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2275 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode))) ||
2277 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2279 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2281 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2283 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
2289 if (download != NULL)
2291 drh = get_read_handle (sc->h,
2292 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
2296 deserialize_download (sc->h,
2302 GNUNET_BIO_read_close (drh, &emsg))
2304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2305 _("Failed to resume sub-download `%s': %s\n"),
2311 GNUNET_free (download);
2313 if (update_srch != NULL)
2315 drh = get_read_handle (sc->h,
2316 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2320 deserialize_search (sc->h,
2325 GNUNET_BIO_read_close (drh, &emsg))
2327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2328 _("Failed to resume sub-search `%s': %s\n"),
2334 GNUNET_free (update_srch);
2336 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2341 GNUNET_BIO_read_close (rh, &emsg))
2343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2344 _("Failure while resuming search operation `%s': %s\n"),
2351 GNUNET_free_non_null (download);
2352 GNUNET_free_non_null (emsg);
2353 GNUNET_free_non_null (uris);
2354 GNUNET_free_non_null (update_srch);
2355 if (sr->uri != NULL)
2356 GNUNET_FS_uri_destroy (sr->uri);
2357 if (sr->meta != NULL)
2358 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2359 GNUNET_free (sr->serialization);
2362 GNUNET_BIO_read_close (rh, &emsg))
2364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2365 _("Failure while resuming search operation `%s': %s\n"),
2375 * Send the 'resume' signal to the callback; also actually
2376 * resume the download (put it in the queue). Does this
2377 * recursively for the top-level download and all child
2380 * @param dc download to resume
2383 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2385 struct GNUNET_FS_DownloadContext *dcc;
2386 struct GNUNET_FS_ProgressInfo pi;
2388 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2389 pi.value.download.specifics.resume.meta = dc->meta;
2390 pi.value.download.specifics.resume.message = dc->emsg;
2391 GNUNET_FS_download_make_status_ (&pi,
2393 dcc = dc->child_head;
2396 signal_download_resume (dcc);
2399 if (dc->pending_head != NULL)
2400 GNUNET_FS_download_start_downloading_ (dc);
2405 * Signal resuming of a search to our clients (for the
2406 * top level search and all sub-searches).
2408 * @param sc search being resumed
2411 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2415 * Iterator over search results signaling resume to the client for
2418 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2419 * @param key current key code
2420 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2421 * @return GNUNET_YES (we should continue to iterate)
2424 signal_result_resume (void *cls,
2425 const GNUNET_HashCode * key,
2428 struct GNUNET_FS_SearchContext *sc = cls;
2429 struct GNUNET_FS_ProgressInfo pi;
2430 struct GNUNET_FS_SearchResult *sr = value;
2432 if (0 == sr->mandatory_missing)
2434 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2435 pi.value.search.specifics.resume_result.meta = sr->meta;
2436 pi.value.search.specifics.resume_result.uri = sr->uri;
2437 pi.value.search.specifics.resume_result.result = sr;
2438 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
2439 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
2440 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
2441 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
2444 if (sr->download != NULL)
2446 signal_download_resume (sr->download);
2450 GNUNET_FS_search_start_probe_ (sr);
2452 if (sr->update_search != NULL)
2453 signal_search_resume (sr->update_search);
2459 * Free memory allocated by the search context and its children
2461 * @param sc search context to free
2464 free_search_context (struct GNUNET_FS_SearchContext *sc);
2468 * Iterator over search results freeing each.
2470 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2471 * @param key current key code
2472 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2473 * @return GNUNET_YES (we should continue to iterate)
2476 free_result (void *cls,
2477 const GNUNET_HashCode * key,
2480 struct GNUNET_FS_SearchResult *sr = value;
2482 if (sr->update_search != NULL)
2484 free_search_context (sr->update_search);
2485 GNUNET_assert (NULL == sr->update_search);
2487 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2488 GNUNET_FS_uri_destroy (sr->uri);
2495 * Free memory allocated by the search context and its children
2497 * @param sc search context to free
2500 free_search_context (struct GNUNET_FS_SearchContext *sc)
2502 if (sc->serialization != NULL)
2504 GNUNET_FS_remove_sync_file_ (sc->h,
2505 (sc->psearch_result == NULL)
2506 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2507 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2509 GNUNET_FS_remove_sync_dir_ (sc->h,
2510 (sc->psearch_result == NULL)
2511 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2512 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2515 GNUNET_free_non_null (sc->serialization);
2516 GNUNET_free_non_null (sc->emsg);
2517 if (sc->uri != NULL)
2518 GNUNET_FS_uri_destroy (sc->uri);
2519 if (sc->master_result_map != NULL)
2521 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2524 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2531 * Function called with a filename of serialized sub-download
2534 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2535 * @param filename complete filename (absolute path)
2536 * @return GNUNET_OK (continue to iterate)
2539 deserialize_subdownload (void *cls,
2540 const char *filename)
2542 struct GNUNET_FS_DownloadContext *parent = cls;
2545 struct GNUNET_BIO_ReadHandle *rh;
2547 ser = get_serialization_short_name (filename);
2548 rh = GNUNET_BIO_read_open (filename);
2551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2552 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2558 deserialize_download (parent->h,
2564 GNUNET_BIO_read_close (rh, &emsg))
2566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2567 _("Failed to resume sub-download `%s': %s\n"),
2578 * Free this download context and all of its descendants.
2579 * (only works during deserialization since not all possible
2580 * state it taken care of).
2582 * @param dc context to free
2585 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2587 struct GNUNET_FS_DownloadContext *dcc;
2589 if (dc->meta != NULL)
2590 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2591 if (dc->uri != NULL)
2592 GNUNET_FS_uri_destroy (dc->uri);
2593 GNUNET_free_non_null (dc->temp_filename);
2594 GNUNET_free_non_null (dc->emsg);
2595 GNUNET_free_non_null (dc->filename);
2596 GNUNET_free_non_null (dc->serialization);
2597 while (NULL != (dcc = dc->child_head))
2599 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2602 free_download_context (dcc);
2604 GNUNET_FS_free_download_request_ (dc->top_request);
2605 if (NULL != dc->active)
2606 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2612 * Deserialize a download.
2614 * @param h overall context
2615 * @param rh file to deserialize from
2616 * @param parent parent download
2617 * @param search associated search
2618 * @param serialization name under which the search was serialized
2621 deserialize_download (struct GNUNET_FS_Handle *h,
2622 struct GNUNET_BIO_ReadHandle *rh,
2623 struct GNUNET_FS_DownloadContext *parent,
2624 struct GNUNET_FS_SearchResult *search,
2625 const char *serialization)
2627 struct GNUNET_FS_DownloadContext *dc;
2636 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2637 dc->parent = parent;
2639 dc->serialization = GNUNET_strdup (serialization);
2641 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2642 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2643 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2644 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2646 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2648 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2650 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2652 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2654 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2656 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2658 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2660 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2662 read_start_time (rh, &dc->start_time)) ||
2664 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2666 GNUNET_BIO_read_int32 (rh, &options)) ||
2668 GNUNET_BIO_read_int32 (rh, &status)) )
2673 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2674 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE));
2675 dc->has_finished = (int) status;
2676 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2677 if (GNUNET_FS_uri_test_loc (dc->uri))
2678 GNUNET_assert (GNUNET_OK ==
2679 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2681 if (dc->emsg == NULL)
2683 dc->top_request = read_download_request (rh);
2684 if (dc->top_request == NULL)
2690 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2694 GNUNET_DISK_directory_test (dn))
2695 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2700 abort (); // for debugging for now
2701 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2707 dc->search = search;
2708 search->download = dc;
2710 if ( (parent == NULL) &&
2713 dc->top = GNUNET_FS_make_top (dc->h,
2714 &GNUNET_FS_download_signal_suspend_,
2716 signal_download_resume (dc);
2720 = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2723 GNUNET_free_non_null (uris);
2724 GNUNET_free_non_null (emsg);
2725 free_download_context (dc);
2730 * Signal resuming of a search to our clients (for the
2731 * top level search and all sub-searches).
2733 * @param sc search being resumed
2736 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2738 struct GNUNET_FS_ProgressInfo pi;
2740 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2741 pi.value.search.specifics.resume.message = sc->emsg;
2742 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2743 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2745 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2746 &signal_result_resume,
2753 * Deserialize a search.
2755 * @param h overall context
2756 * @param rh file to deserialize from
2757 * @param psearch_result parent search result
2758 * @param serialization name under which the search was serialized
2760 static struct GNUNET_FS_SearchContext *
2761 deserialize_search (struct GNUNET_FS_Handle *h,
2762 struct GNUNET_BIO_ReadHandle *rh,
2763 struct GNUNET_FS_SearchResult *psearch_result,
2764 const char *serialization)
2766 struct GNUNET_FS_SearchContext *sc;
2773 if ( (psearch_result != NULL) &&
2774 (psearch_result->update_search != NULL) )
2781 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2782 if (psearch_result != NULL)
2784 sc->psearch_result = psearch_result;
2785 psearch_result->update_search = sc;
2788 sc->serialization = GNUNET_strdup (serialization);
2790 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2791 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2792 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2793 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2795 read_start_time (rh, &sc->start_time)) ||
2797 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2799 GNUNET_BIO_read_int32 (rh, &options)) ||
2801 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2803 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2808 sc->options = (enum GNUNET_FS_SearchOptions) options;
2809 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2810 dn = get_serialization_file_name_in_dir (h,
2811 (sc->psearch_result == NULL)
2812 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2813 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2819 GNUNET_DISK_directory_test (dn))
2820 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2823 if ( ('\0' == in_pause) &&
2825 GNUNET_FS_search_start_searching_ (sc)) )
2827 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2828 _("Could not resume running search, will resume as paused search\n"));
2830 signal_search_resume (sc);
2834 GNUNET_free_non_null (emsg);
2835 free_search_context (sc);
2836 GNUNET_free_non_null (uris);
2842 * Function called with a filename of serialized search operation
2845 * @param cls the 'struct GNUNET_FS_Handle*'
2846 * @param filename complete filename (absolute path)
2847 * @return GNUNET_OK (continue to iterate)
2850 deserialize_search_file (void *cls,
2851 const char *filename)
2853 struct GNUNET_FS_Handle *h = cls;
2856 struct GNUNET_BIO_ReadHandle *rh;
2857 struct GNUNET_FS_SearchContext *sc;
2859 ser = get_serialization_short_name (filename);
2860 rh = GNUNET_BIO_read_open (filename);
2865 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2870 sc = deserialize_search (h, rh, NULL, ser);
2872 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2875 GNUNET_BIO_read_close (rh, &emsg))
2877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2878 _("Failure while resuming search operation `%s': %s\n"),
2888 * Function called with a filename of serialized download operation
2891 * @param cls the 'struct GNUNET_FS_Handle*'
2892 * @param filename complete filename (absolute path)
2893 * @return GNUNET_OK (continue to iterate)
2896 deserialize_download_file (void *cls,
2897 const char *filename)
2899 struct GNUNET_FS_Handle *h = cls;
2902 struct GNUNET_BIO_ReadHandle *rh;
2904 ser = get_serialization_short_name (filename);
2905 rh = GNUNET_BIO_read_open (filename);
2908 if (0 != UNLINK (filename))
2909 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2915 deserialize_download (h, rh, NULL, NULL, ser);
2918 GNUNET_BIO_read_close (rh, &emsg))
2920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2921 _("Failure while resuming download operation `%s': %s\n"),
2931 * Deserialize informatin about pending operations.
2933 * @param master_path which master directory should be scanned
2934 * @param proc function to call for each entry (will get 'h' for 'cls')
2935 * @param h the 'struct GNUNET_FS_Handle*'
2938 deserialization_master (const char *master_path,
2939 GNUNET_FileNameCallback proc,
2940 struct GNUNET_FS_Handle *h)
2944 dn = get_serialization_file_name (h, master_path, "");
2948 GNUNET_DISK_directory_test (dn))
2949 GNUNET_DISK_directory_scan (dn, proc, h);
2955 * Setup a connection to the file-sharing service.
2957 * @param cfg configuration to use
2958 * @param client_name unique identifier for this client
2959 * @param upcb function to call to notify about FS actions
2960 * @param upcb_cls closure for upcb
2961 * @param flags specific attributes for fs-operations
2962 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2963 * @return NULL on error
2965 struct GNUNET_FS_Handle *
2966 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2967 const char *client_name,
2968 GNUNET_FS_ProgressCallback upcb,
2970 enum GNUNET_FS_Flags flags,
2973 struct GNUNET_FS_Handle *ret;
2974 enum GNUNET_FS_OPTIONS opt;
2977 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2979 ret->client_name = GNUNET_strdup (client_name);
2981 ret->upcb_cls = upcb_cls;
2983 ret->max_parallel_downloads = 1;
2984 ret->max_parallel_requests = 1;
2985 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2986 va_start (ap, flags);
2987 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2991 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2992 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2994 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2995 ret->max_parallel_requests = va_arg (ap, unsigned int);
2999 GNUNET_free (ret->client_name);
3006 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3008 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3009 &deserialize_publish_file,
3011 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3012 &deserialize_search_file,
3014 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3015 &deserialize_download_file,
3017 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3018 &deserialize_unindex_file,
3026 * Close our connection with the file-sharing service.
3027 * The callback given to GNUNET_FS_start will no longer be
3028 * called after this function returns.
3030 * @param h handle that was returned from GNUNET_FS_start
3033 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3035 while (h->top_head != NULL)
3036 h->top_head->ssf (h->top_head->ssf_cls);
3037 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3038 GNUNET_SCHEDULER_cancel (h->queue_job);
3039 GNUNET_free (h->client_name);