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 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 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
44 if (qe->client == NULL)
49 qe->start (qe->cls, qe->client);
51 qe->h->active_blocks += qe->blocks;
52 qe->start_time = GNUNET_TIME_absolute_get ();
53 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
56 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
64 * Stop the given job (send signal, remove from active queue, update
65 * counters and state).
67 * @param qe job to stop
70 stop_job (struct GNUNET_FS_QueueEntry *qe)
74 qe->h->active_downloads--;
75 qe->h->active_blocks -= qe->blocks;
76 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
77 GNUNET_TIME_absolute_get_duration (qe->start_time));
78 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
81 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
89 * Process the jobs in the job queue, possibly starting some
90 * and stopping others.
92 * @param cls the 'struct GNUNET_FS_Handle'
93 * @param tc scheduler context
96 process_job_queue (void *cls,
97 const struct GNUNET_SCHEDULER_TaskContext *tc)
99 struct GNUNET_FS_Handle *h = cls;
100 struct GNUNET_FS_QueueEntry *qe;
101 struct GNUNET_FS_QueueEntry *next;
102 struct GNUNET_TIME_Relative run_time;
103 struct GNUNET_TIME_Relative restart_at;
104 struct GNUNET_TIME_Relative rst;
105 struct GNUNET_TIME_Absolute end_time;
107 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
108 next = h->pending_head;
109 while (NULL != (qe = next))
112 if (h->running_head == NULL)
117 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
118 (h->active_downloads + 1 <= h->max_parallel_downloads) )
124 if (h->pending_head == NULL)
125 return; /* no need to stop anything */
126 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
127 next = h->running_head;
128 while (NULL != (qe = next))
131 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
132 qe->blocks * qe->start_times);
133 end_time = GNUNET_TIME_absolute_add (qe->start_time,
135 rst = GNUNET_TIME_absolute_get_remaining (end_time);
136 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
137 if (rst.rel_value > 0)
141 h->queue_job = GNUNET_SCHEDULER_add_delayed (restart_at,
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->queue_job);
180 = GNUNET_SCHEDULER_add_now (&process_job_queue,
187 * Dequeue a job from the queue.
188 * @param qh handle for the job
191 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
193 struct GNUNET_FS_Handle *h;
196 if (qh->client != NULL)
198 GNUNET_CONTAINER_DLL_remove (h->pending_head,
202 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
203 GNUNET_SCHEDULER_cancel (h->queue_job);
205 = GNUNET_SCHEDULER_add_now (&process_job_queue,
211 * Create a top-level activity entry.
213 * @param h global fs handle
214 * @param ssf suspend signal function to use
215 * @param ssf_cls closure for ssf
216 * @return fresh top-level activity handle
218 struct TopLevelActivity *
219 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
220 SuspendSignalFunction ssf,
223 struct TopLevelActivity *ret;
225 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
227 ret->ssf_cls = ssf_cls;
228 GNUNET_CONTAINER_DLL_insert (h->top_head,
236 * Destroy a top-level activity entry.
238 * @param h global fs handle
239 * @param top top level activity entry
242 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
243 struct TopLevelActivity *top)
245 GNUNET_CONTAINER_DLL_remove (h->top_head,
254 * Closure for "data_reader_file".
259 * Name of the file to read.
264 * File descriptor, NULL if it has not yet been opened.
266 struct GNUNET_DISK_FileHandle *fd;
271 * Function that provides data by reading from a file.
273 * @param cls closure (points to the file information)
274 * @param offset offset to read from; it is possible
275 * that the caller might need to go backwards
277 * @param max maximum number of bytes that should be
278 * copied to buf; readers are not allowed
279 * to provide less data unless there is an error;
280 * a value of "0" will be used at the end to allow
281 * the reader to clean up its internal state
282 * @param buf where the reader should write the data
283 * @param emsg location for the reader to store an error message
284 * @return number of bytes written, usually "max", 0 on error
287 GNUNET_FS_data_reader_file_(void *cls,
293 struct FileInfo *fi = cls;
299 GNUNET_DISK_file_close (fi->fd);
300 GNUNET_free (fi->filename);
306 fi->fd = GNUNET_DISK_file_open (fi->filename,
307 GNUNET_DISK_OPEN_READ,
308 GNUNET_DISK_PERM_NONE);
311 GNUNET_asprintf (emsg,
312 _("Could not open file `%s': %s"),
318 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
319 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
322 GNUNET_asprintf (emsg,
323 _("Could not read file `%s': %s"),
330 GNUNET_asprintf (emsg,
331 _("Short read reading from file `%s'!"),
340 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
342 * @param filename file to read
343 * @return closure to use, NULL on error
346 GNUNET_FS_make_file_reader_context_ (const char *filename)
350 fi = GNUNET_malloc (sizeof(struct FileInfo));
351 fi->filename = GNUNET_STRINGS_filename_expand (filename);
352 if (fi->filename == NULL)
362 * Function that provides data by copying from a buffer.
364 * @param cls closure (points to the buffer)
365 * @param offset offset to read from; it is possible
366 * that the caller might need to go backwards
368 * @param max maximum number of bytes that should be
369 * copied to buf; readers are not allowed
370 * to provide less data unless there is an error;
371 * a value of "0" will be used at the end to allow
372 * the reader to clean up its internal state
373 * @param buf where the reader should write the data
374 * @param emsg location for the reader to store an error message
375 * @return number of bytes written, usually "max", 0 on error
378 GNUNET_FS_data_reader_copy_ (void *cls,
388 GNUNET_free_non_null (data);
391 memcpy (buf, &data[offset], max);
397 * Return the full filename where we would store state information
398 * (for serialization/deserialization).
400 * @param h master context
401 * @param ext component of the path
402 * @param ent entity identifier (or emtpy string for the directory)
403 * @return NULL on error
406 get_serialization_file_name (struct GNUNET_FS_Handle *h,
413 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
414 return NULL; /* persistence not requested */
416 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
421 GNUNET_asprintf (&ret,
430 GNUNET_free (basename);
436 * Return the full filename where we would store state information
437 * (for serialization/deserialization) that is associated with a
440 * @param h master context
441 * @param ext component of the path
442 * @param uni name of the parent operation
443 * @param ent entity identifier (or emtpy string for the directory)
444 * @return NULL on error
447 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
455 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
456 return NULL; /* persistence not requested */
458 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
463 GNUNET_asprintf (&ret,
464 "%s%s%s%s%s%s%s.dir%s%s",
474 GNUNET_free (basename);
480 * Return a read handle for deserialization.
482 * @param h master context
483 * @param ext component of the path
484 * @param ent entity identifier (or emtpy string for the directory)
485 * @return NULL on error
487 static struct GNUNET_BIO_ReadHandle *
488 get_read_handle (struct GNUNET_FS_Handle *h,
493 struct GNUNET_BIO_ReadHandle *ret;
495 fn = get_serialization_file_name (h, ext, ent);
498 ret = GNUNET_BIO_read_open (fn);
505 * Return a write handle for serialization.
507 * @param h master context
508 * @param ext component of the path
509 * @param ent entity identifier (or emtpy string for the directory)
510 * @return NULL on error
512 static struct GNUNET_BIO_WriteHandle *
513 get_write_handle (struct GNUNET_FS_Handle *h,
518 struct GNUNET_BIO_WriteHandle *ret;
520 fn = get_serialization_file_name (h, ext, ent);
525 ret = GNUNET_BIO_write_open (fn);
534 * Return a write handle for serialization.
536 * @param h master context
537 * @param ext component of the path
538 * @param uni name of parent
539 * @param ent entity identifier (or emtpy string for the directory)
540 * @return NULL on error
542 static struct GNUNET_BIO_WriteHandle *
543 get_write_handle_in_dir (struct GNUNET_FS_Handle *h,
549 struct GNUNET_BIO_WriteHandle *ret;
551 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
554 ret = GNUNET_BIO_write_open (fn);
561 * Remove serialization/deserialization file from disk.
563 * @param h master context
564 * @param ext component of the path
565 * @param ent entity identifier
568 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
574 if ( (NULL == ent) ||
575 (0 == strlen (ent)) )
580 filename = get_serialization_file_name (h, ext, ent);
581 if (filename != NULL)
583 if (0 != UNLINK (filename))
584 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
587 GNUNET_free (filename);
593 * Remove serialization/deserialization file from disk.
595 * @param h master context
596 * @param ext component of the path
597 * @param uni parent name
598 * @param ent entity identifier
601 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
608 if ( (NULL == ent) ||
609 (0 == strlen (ent)) )
614 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
615 if (filename != NULL)
617 if (0 != UNLINK (filename))
618 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
621 GNUNET_free (filename);
627 * Remove serialization/deserialization directory from disk.
629 * @param h master context
630 * @param ext component of the path
631 * @param uni unique name of parent
634 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
642 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
645 if ( (GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
646 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)) )
647 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
655 * Serialize a 'start_time'. Since we use start-times to
656 * calculate the duration of some operation, we actually
657 * do not serialize the absolute time but the (relative)
658 * duration since the start time. When we then
659 * deserialize the start time, we take the current time and
660 * subtract that duration so that we get again an absolute
661 * time stamp that will result in correct performance
664 * @param wh handle for writing
665 * @param timestamp time to serialize
666 * @return GNUNET_OK on success
669 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
670 struct GNUNET_TIME_Absolute timestamp)
672 struct GNUNET_TIME_Relative dur;
674 dur = GNUNET_TIME_absolute_get_duration (timestamp);
675 return GNUNET_BIO_write_int64 (wh, dur.rel_value);
680 * Serialize a 'start_time'. Since we use start-times to
681 * calculate the duration of some operation, we actually
682 * do not serialize the absolute time but the (relative)
683 * duration since the start time. When we then
684 * deserialize the start time, we take the current time and
685 * subtract that duration so that we get again an absolute
686 * time stamp that will result in correct performance
689 * @param rh handle for reading
690 * @param timestamp where to write the deserialized timestamp
691 * @return GNUNET_OK on success
694 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
695 struct GNUNET_TIME_Absolute *timestamp)
697 struct GNUNET_TIME_Relative dur;
699 GNUNET_BIO_read_int64 (rh, &dur.rel_value))
700 return GNUNET_SYSERR;
701 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
708 * Using the given serialization filename, try to deserialize
709 * the file-information tree associated with it.
711 * @param h master context
712 * @param filename name of the file (without directory) with
714 * @return NULL on error
716 static struct GNUNET_FS_FileInformation *
717 deserialize_file_information (struct GNUNET_FS_Handle *h,
718 const char *filename);
722 * Using the given serialization filename, try to deserialize
723 * the file-information tree associated with it.
725 * @param h master context
726 * @param fn name of the file (without directory) with
728 * @param rh handle for reading
729 * @return NULL on error
731 static struct GNUNET_FS_FileInformation *
732 deserialize_fi_node (struct GNUNET_FS_Handle *h,
734 struct GNUNET_BIO_ReadHandle *rh)
736 struct GNUNET_FS_FileInformation *ret;
737 struct GNUNET_FS_FileInformation *nxt;
745 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
750 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
756 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
758 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
761 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
763 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
765 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
768 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
770 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
772 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.abs_value)) ||
774 read_start_time (rh, &ret->start_time)) ||
776 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
778 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
780 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
782 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
789 case 0: /* file-insert */
791 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
796 ret->is_directory = GNUNET_NO;
797 ret->data.file.do_index = GNUNET_NO;
798 ret->data.file.have_hash = GNUNET_NO;
799 ret->data.file.index_start_confirmed = GNUNET_NO;
800 if (GNUNET_NO == ret->is_published)
802 if (NULL == ret->filename)
804 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
805 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
806 if (ret->data.file.reader_cls == NULL)
809 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
817 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
818 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
822 case 1: /* file-index, no hash */
823 if (NULL == ret->filename)
829 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
834 ret->is_directory = GNUNET_NO;
835 ret->data.file.do_index = GNUNET_YES;
836 ret->data.file.have_hash = GNUNET_NO;
837 ret->data.file.index_start_confirmed = GNUNET_NO;
838 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
839 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
841 case 2: /* file-index-with-hash */
842 if (NULL == ret->filename)
848 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
850 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
855 ret->is_directory = GNUNET_NO;
856 ret->data.file.do_index = GNUNET_YES;
857 ret->data.file.have_hash = GNUNET_YES;
858 ret->data.file.index_start_confirmed = GNUNET_NO;
859 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
860 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
862 case 3: /* file-index-with-hash-confirmed */
863 if (NULL == ret->filename)
869 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
871 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
876 ret->is_directory = GNUNET_NO;
877 ret->data.file.do_index = GNUNET_YES;
878 ret->data.file.have_hash = GNUNET_YES;
879 ret->data.file.index_start_confirmed = GNUNET_YES;
880 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
881 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
883 case 4: /* directory */
884 ret->is_directory = GNUNET_YES;
886 GNUNET_BIO_read_int32 (rh, &dsize)) ||
887 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
889 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
891 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
896 ret->data.dir.dir_size = (uint32_t) dsize;
897 if (filename != NULL)
899 ret->data.dir.entries = deserialize_file_information (h, filename);
900 GNUNET_free (filename);
902 nxt = ret->data.dir.entries;
914 ret->serialization = GNUNET_strdup (fn);
916 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
921 if (filename != NULL)
923 ret->next = deserialize_file_information (h, filename);
924 GNUNET_free (filename);
927 GNUNET_free_non_null (ksks);
928 GNUNET_free_non_null (chks);
931 GNUNET_free_non_null (ksks);
932 GNUNET_free_non_null (chks);
933 GNUNET_free_non_null (filename);
934 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
940 * Using the given serialization filename, try to deserialize
941 * the file-information tree associated with it.
943 * @param h master context
944 * @param filename name of the file (without directory) with
946 * @return NULL on error
948 static struct GNUNET_FS_FileInformation *
949 deserialize_file_information (struct GNUNET_FS_Handle *h,
950 const char *filename)
952 struct GNUNET_FS_FileInformation *ret;
953 struct GNUNET_BIO_ReadHandle *rh;
956 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
959 ret = deserialize_fi_node (h, filename, rh);
961 GNUNET_BIO_read_close (rh, &emsg))
963 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
964 _("Failed to resume publishing information `%s': %s\n"),
971 if (0 != UNLINK (filename))
972 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
981 * Given a serialization name (full absolute path), return the
982 * basename of the file (without the path), which must only
983 * consist of the 6 random characters.
985 * @param fullname name to extract the basename from
986 * @return copy of the basename, NULL on error
989 get_serialization_short_name (const char *fullname)
996 /* FIXME: we could do this faster since we know
997 the length of 'end'... */
1000 if (DIR_SEPARATOR == *nxt)
1004 if ( (end == NULL) ||
1005 (strlen (end) == 0) )
1010 GNUNET_break (6 == strlen (end));
1011 return GNUNET_strdup (end);
1016 * Create a new random name for serialization. Also checks if persistence
1017 * is enabled and returns NULL if not.
1019 * @param h master context
1020 * @param ext component of the path
1021 * @return NULL on errror
1024 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1031 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1032 return NULL; /* persistence not requested */
1033 dn = get_serialization_file_name (h, ext, "");
1037 GNUNET_DISK_directory_create_for_file (dn))
1042 fn = GNUNET_DISK_mktemp (dn);
1045 return NULL; /* epic fail */
1046 ret = get_serialization_short_name (fn);
1053 * Create a new random name for serialization. Also checks if persistence
1054 * is enabled and returns NULL if not.
1056 * @param h master context
1057 * @param ext component of the path
1058 * @param uni name of parent
1059 * @return NULL on errror
1062 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1070 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1071 return NULL; /* persistence not requested */
1072 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1076 GNUNET_DISK_directory_create_for_file (dn))
1081 fn = GNUNET_DISK_mktemp (dn);
1084 return NULL; /* epic fail */
1085 ret = get_serialization_short_name (fn);
1092 * Copy all of the data from the reader to the write handle.
1094 * @param wh write handle
1095 * @param fi file with reader
1096 * @return GNUNET_OK on success
1099 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1100 struct GNUNET_FS_FileInformation * fi)
1102 char buf[32 * 1024];
1110 while (off < fi->data.file.file_size)
1112 left = GNUNET_MIN (sizeof(buf), fi->data.file.file_size - off);
1113 ret = fi->data.file.reader (fi->data.file.reader_cls,
1120 return GNUNET_SYSERR;
1123 GNUNET_BIO_write (wh, buf, ret))
1124 return GNUNET_SYSERR;
1132 * Create a temporary file on disk to store the current
1135 * @param fi file information to sync with disk
1138 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
1141 struct GNUNET_BIO_WriteHandle *wh;
1146 if (NULL == fi->serialization)
1147 fi->serialization = make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1148 if (NULL == fi->serialization)
1150 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1153 GNUNET_free (fi->serialization);
1154 fi->serialization = NULL;
1157 if (GNUNET_YES == fi->is_directory)
1159 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1161 else if (GNUNET_YES == fi->data.file.have_hash)
1163 else if (GNUNET_YES == fi->data.file.do_index)
1167 if (fi->keywords != NULL)
1168 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1171 if (fi->chk_uri != NULL)
1172 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1176 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1178 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1180 GNUNET_BIO_write_string (wh, ksks)) ||
1182 GNUNET_BIO_write_string (wh, chks)) ||
1184 GNUNET_BIO_write_int64 (wh, fi->expirationTime.abs_value)) ||
1186 write_start_time (wh, fi->start_time)) ||
1188 GNUNET_BIO_write_string (wh, fi->emsg)) ||
1190 GNUNET_BIO_write_string (wh, fi->filename)) ||
1192 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
1194 GNUNET_BIO_write_int32 (wh, fi->priority)) )
1199 GNUNET_free_non_null (chks);
1201 GNUNET_free_non_null (ksks);
1206 case 0: /* file-insert */
1208 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1213 if ( (GNUNET_NO == fi->is_published) &&
1214 (NULL == fi->filename) )
1216 copy_from_reader (wh, fi))
1222 case 1: /* file-index, no hash */
1223 if (NULL == fi->filename)
1229 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1235 case 2: /* file-index-with-hash */
1236 case 3: /* file-index-with-hash-confirmed */
1237 if (NULL == fi->filename)
1243 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1245 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
1251 case 4: /* directory */
1253 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1255 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
1257 GNUNET_BIO_write_string (wh,
1258 (fi->data.dir.entries == NULL)
1260 : fi->data.dir.entries->serialization)) )
1271 GNUNET_BIO_write_string (wh, (fi->next != NULL) ? fi->next->serialization : NULL))
1277 GNUNET_BIO_write_close (wh))
1286 (void) GNUNET_BIO_write_close (wh);
1287 GNUNET_free_non_null (chks);
1288 GNUNET_free_non_null (ksks);
1289 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1292 if (0 != UNLINK (fn))
1293 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1296 GNUNET_free (fi->serialization);
1297 fi->serialization = NULL;
1303 * Find the entry in the file information struct where the
1304 * serialization filename matches the given name.
1306 * @param pos file information to search
1307 * @param srch filename to search for
1308 * @return NULL if srch was not found in this subtree
1310 static struct GNUNET_FS_FileInformation *
1311 find_file_position (struct GNUNET_FS_FileInformation *pos,
1314 struct GNUNET_FS_FileInformation *r;
1318 if (0 == strcmp (srch,
1319 pos->serialization))
1321 if (pos->is_directory)
1323 r = find_file_position (pos->data.dir.entries,
1335 * Signal the FS's progress function that we are resuming
1338 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1339 * @param fi the entry in the publish-structure
1340 * @param length length of the file or directory
1341 * @param meta metadata for the file or directory (can be modified)
1342 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1343 * @param anonymity pointer to selected anonymity level (can be modified)
1344 * @param priority pointer to selected priority (can be modified)
1345 * @param do_index should we index?
1346 * @param expirationTime pointer to selected expiration time (can be modified)
1347 * @param client_info pointer to client context set upon creation (can be modified)
1348 * @return GNUNET_OK to continue (always)
1351 fip_signal_resume(void *cls,
1352 struct GNUNET_FS_FileInformation *fi,
1354 struct GNUNET_CONTAINER_MetaData *meta,
1355 struct GNUNET_FS_Uri **uri,
1356 uint32_t *anonymity,
1359 struct GNUNET_TIME_Absolute *expirationTime,
1362 struct GNUNET_FS_PublishContext *sc = cls;
1363 struct GNUNET_FS_ProgressInfo pi;
1365 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1366 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1367 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1368 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1374 * Function called with a filename of serialized publishing operation
1377 * @param cls the 'struct GNUNET_FS_Handle*'
1378 * @param filename complete filename (absolute path)
1379 * @return GNUNET_OK (continue to iterate)
1382 deserialize_publish_file (void *cls,
1383 const char *filename)
1385 struct GNUNET_FS_Handle *h = cls;
1386 struct GNUNET_BIO_ReadHandle *rh;
1387 struct GNUNET_FS_PublishContext *pc;
1395 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1397 pc->serialization = get_serialization_short_name (filename);
1401 rh = GNUNET_BIO_read_open (filename);
1408 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1410 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1412 GNUNET_BIO_read_int32 (rh, &options)) ||
1414 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1416 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1418 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1420 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1425 pc->options = options;
1426 pc->all_done = all_done;
1427 pc->fi = deserialize_file_information (h, fi_root);
1435 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1436 if (pc->namespace == NULL)
1438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1439 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1444 if ( (0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1445 (GNUNET_YES != pc->all_done) )
1447 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1448 if (NULL == pc->dsh)
1453 pc->fi_pos = find_file_position (pc->fi,
1455 GNUNET_free (fi_pos);
1457 if (pc->fi_pos == NULL)
1459 /* failed to find position for resuming, outch! Will start from root! */
1461 if (pc->all_done != GNUNET_YES)
1462 pc->fi_pos = pc->fi;
1465 GNUNET_free (fi_root);
1467 /* generate RESUME event(s) */
1468 GNUNET_FS_file_information_inspect (pc->fi,
1472 /* re-start publishing (if needed)... */
1473 if (pc->all_done != GNUNET_YES)
1475 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1476 &GNUNET_FS_publish_main_,
1479 GNUNET_BIO_read_close (rh, &emsg))
1481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1482 _("Failure while resuming publishing operation `%s': %s\n"),
1487 GNUNET_free_non_null (ns);
1488 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1491 GNUNET_free_non_null (pc->nid);
1492 GNUNET_free_non_null (pc->nuid);
1493 GNUNET_free_non_null (fi_root);
1494 GNUNET_free_non_null (fi_pos);
1495 GNUNET_free_non_null (ns);
1496 if ( (rh != NULL) &&
1498 GNUNET_BIO_read_close (rh, &emsg)) )
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 _("Failed to resume publishing operation `%s': %s\n"),
1507 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1508 if (0 != UNLINK (filename))
1509 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1510 GNUNET_free (pc->serialization);
1517 * Synchronize this publishing struct with its mirror
1518 * on disk. Note that all internal FS-operations that change
1519 * publishing structs should already call "sync" internally,
1520 * so this function is likely not useful for clients.
1522 * @param pc the struct to sync
1525 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1527 struct GNUNET_BIO_WriteHandle *wh;
1529 if (NULL == pc->serialization)
1530 pc->serialization = make_serialization_file_name (pc->h,
1531 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1532 if (NULL == pc->serialization)
1536 if (NULL == pc->fi->serialization)
1541 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1548 GNUNET_BIO_write_string (wh, pc->nid)) ||
1550 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1552 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1554 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1556 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1558 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1560 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1566 GNUNET_BIO_write_close (wh))
1575 (void) GNUNET_BIO_write_close (wh);
1576 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1577 GNUNET_free (pc->serialization);
1578 pc->serialization = NULL;
1583 * Synchronize this unindex struct with its mirror
1584 * on disk. Note that all internal FS-operations that change
1585 * publishing structs should already call "sync" internally,
1586 * so this function is likely not useful for clients.
1588 * @param uc the struct to sync
1591 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1593 struct GNUNET_BIO_WriteHandle *wh;
1595 if (NULL == uc->serialization)
1596 uc->serialization = make_serialization_file_name (uc->h,
1597 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1598 if (NULL == uc->serialization)
1600 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1607 GNUNET_BIO_write_string (wh, uc->filename)) ||
1609 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1611 write_start_time (wh, uc->start_time)) ||
1613 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1614 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1616 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1617 ( (uc->state == UNINDEX_STATE_ERROR) &&
1619 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1625 GNUNET_BIO_write_close (wh))
1634 (void) GNUNET_BIO_write_close (wh);
1635 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1636 GNUNET_free (uc->serialization);
1637 uc->serialization = NULL;
1642 * Serialize an active or pending download request.
1644 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
1645 * @param key unused, can be NULL
1646 * @param value the 'struct DownloadRequest'
1647 * @return GNUNET_YES on success, GNUNET_NO on error
1650 write_download_request (void *cls,
1651 const GNUNET_HashCode *key,
1654 struct GNUNET_BIO_WriteHandle *wh = cls;
1655 struct DownloadRequest *dr = value;
1658 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) ||
1660 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1662 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1669 * Count active download requests.
1671 * @param cls the 'uint32_t*' counter
1672 * @param key unused, can be NULL
1673 * @param value the 'struct DownloadRequest'
1674 * @return GNUNET_YES (continue iteration)
1677 count_download_requests (void *cls,
1678 const GNUNET_HashCode *key,
1681 uint32_t *counter = cls;
1689 * Compute the name of the sync file (or directory) for the given download
1692 * @param dc download context to compute for
1693 * @param uni unique filename to use, use "" for the directory name
1694 * @param ext extension to use, use ".dir" for our own subdirectory
1695 * @return the expanded file name, NULL for none
1698 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1705 if (dc->parent == NULL)
1706 return get_serialization_file_name (dc->h,
1707 (dc->search != NULL) ?
1708 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1709 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1711 if (dc->parent->serialization == NULL)
1713 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1716 GNUNET_asprintf (&epar,
1728 * Synchronize this download struct with its mirror
1729 * on disk. Note that all internal FS-operations that change
1730 * publishing structs should already call "sync" internally,
1731 * so this function is likely not useful for clients.
1733 * @param dc the struct to sync
1736 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1738 struct GNUNET_BIO_WriteHandle *wh;
1742 uint32_t num_pending;
1744 if (NULL == dc->serialization)
1746 dir = get_download_sync_filename (dc, "", "");
1750 GNUNET_DISK_directory_create_for_file (dir))
1755 fn = GNUNET_DISK_mktemp (dir);
1759 dc->serialization = get_serialization_short_name (fn);
1763 fn = get_download_sync_filename (dc, dc->serialization, "");
1766 GNUNET_free (dc->serialization);
1767 dc->serialization = NULL;
1772 wh = GNUNET_BIO_write_open (fn);
1775 GNUNET_free (dc->serialization);
1776 dc->serialization = NULL;
1780 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1781 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1782 uris = GNUNET_FS_uri_to_string (dc->uri);
1784 if (dc->emsg == NULL)
1785 (void) GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1786 &count_download_requests,
1788 GNUNET_assert ( (dc->length == dc->completed) ||
1789 (dc->emsg != NULL) ||
1790 (num_pending > 0) );
1792 GNUNET_BIO_write_string (wh, uris)) ||
1794 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1796 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1798 GNUNET_BIO_write_string (wh, dc->filename)) ||
1800 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1802 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1804 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1806 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1808 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1810 write_start_time (wh, dc->start_time)) ||
1812 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1814 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1816 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) ||
1818 GNUNET_BIO_write_int32 (wh, num_pending)) ||
1820 GNUNET_BIO_write_int32 (wh, dc->start_task != GNUNET_SCHEDULER_NO_TASK)) )
1825 if (GNUNET_SYSERR ==
1826 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1827 &write_download_request,
1833 GNUNET_free_non_null (uris);
1836 GNUNET_BIO_write_close (wh))
1846 (void) GNUNET_BIO_write_close (wh);
1847 GNUNET_free_non_null (uris);
1848 if (0 != UNLINK (fn))
1849 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1851 GNUNET_free (dc->serialization);
1852 dc->serialization = NULL;
1857 * Synchronize this search result with its mirror
1858 * on disk. Note that all internal FS-operations that change
1859 * publishing structs should already call "sync" internally,
1860 * so this function is likely not useful for clients.
1862 * @param sr the struct to sync
1865 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1867 struct GNUNET_BIO_WriteHandle *wh;
1871 if (NULL == sr->serialization)
1872 sr->serialization = make_serialization_file_name_in_dir (sr->sc->h,
1873 (sr->sc->psearch_result == NULL)
1874 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1875 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1876 sr->sc->serialization);
1877 if (NULL == sr->serialization)
1879 wh = get_write_handle_in_dir (sr->sc->h,
1880 (sr->sc->psearch_result == NULL)
1881 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1882 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1883 sr->sc->serialization,
1890 uris = GNUNET_FS_uri_to_string (sr->uri);
1892 GNUNET_BIO_write_string (wh, uris)) ||
1894 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1896 GNUNET_BIO_write_string (wh, sr->update_search != NULL ? sr->update_search->serialization : NULL)) ||
1898 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1900 GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode))) ||
1902 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1904 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1906 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1908 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1914 GNUNET_BIO_write_close (wh))
1920 GNUNET_free_non_null (uris);
1923 GNUNET_free_non_null (uris);
1925 (void) GNUNET_BIO_write_close (wh);
1926 remove_sync_file_in_dir (sr->sc->h,
1927 (sr->sc->psearch_result == NULL)
1928 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1929 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1930 sr->sc->serialization,
1932 GNUNET_free (sr->serialization);
1933 sr->serialization = NULL;
1938 * Synchronize this search struct with its mirror
1939 * on disk. Note that all internal FS-operations that change
1940 * publishing structs should already call "sync" internally,
1941 * so this function is likely not useful for clients.
1943 * @param sc the struct to sync
1946 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1948 struct GNUNET_BIO_WriteHandle *wh;
1951 const char *category;
1953 category = (sc->psearch_result == NULL)
1954 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1955 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
1956 if (NULL == sc->serialization)
1957 sc->serialization = make_serialization_file_name (sc->h,
1959 if (NULL == sc->serialization)
1962 wh = get_write_handle (sc->h, category, sc->serialization);
1968 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1969 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1970 uris = GNUNET_FS_uri_to_string (sc->uri);
1971 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1973 GNUNET_BIO_write_string (wh, uris)) ||
1975 write_start_time (wh, sc->start_time)) ||
1977 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1979 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1981 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1983 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1991 GNUNET_BIO_write_close (wh))
2000 (void) GNUNET_BIO_write_close (wh);
2001 GNUNET_free_non_null (uris);
2002 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2003 GNUNET_free (sc->serialization);
2004 sc->serialization = NULL;
2009 * Function called with a filename of serialized unindexing operation
2012 * @param cls the 'struct GNUNET_FS_Handle*'
2013 * @param filename complete filename (absolute path)
2014 * @return GNUNET_OK (continue to iterate)
2017 deserialize_unindex_file (void *cls,
2018 const char *filename)
2020 struct GNUNET_FS_Handle *h = cls;
2021 struct GNUNET_BIO_ReadHandle *rh;
2022 struct GNUNET_FS_UnindexContext *uc;
2023 struct GNUNET_FS_ProgressInfo pi;
2027 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
2029 uc->serialization = get_serialization_short_name (filename);
2030 rh = GNUNET_BIO_read_open (filename);
2037 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
2039 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2041 read_start_time (rh, &uc->start_time)) ||
2043 GNUNET_BIO_read_int32 (rh, &state)) )
2048 uc->state = (enum UnindexState) state;
2051 case UNINDEX_STATE_HASHING:
2053 case UNINDEX_STATE_FS_NOTIFY:
2055 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
2061 case UNINDEX_STATE_DS_REMOVE:
2063 case UNINDEX_STATE_COMPLETE:
2065 case UNINDEX_STATE_ERROR:
2067 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
2077 uc->top = GNUNET_FS_make_top (h,
2078 &GNUNET_FS_unindex_signal_suspend_,
2080 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2081 pi.value.unindex.specifics.resume.message = uc->emsg;
2082 GNUNET_FS_unindex_make_status_ (&pi,
2084 (uc->state == UNINDEX_STATE_COMPLETE)
2089 case UNINDEX_STATE_HASHING:
2090 uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
2093 &GNUNET_FS_unindex_process_hash_,
2096 case UNINDEX_STATE_FS_NOTIFY:
2097 uc->state = UNINDEX_STATE_HASHING;
2098 GNUNET_FS_unindex_process_hash_ (uc,
2101 case UNINDEX_STATE_DS_REMOVE:
2102 GNUNET_FS_unindex_do_remove_ (uc);
2104 case UNINDEX_STATE_COMPLETE:
2105 case UNINDEX_STATE_ERROR:
2106 /* no need to resume any operation, we were done */
2112 GNUNET_BIO_read_close (rh, &emsg))
2114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2115 _("Failure while resuming unindexing operation `%s': %s\n"),
2122 GNUNET_free_non_null (uc->filename);
2123 if ( (rh != NULL) &&
2125 GNUNET_BIO_read_close (rh, &emsg)) )
2127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2128 _("Failed to resume unindexing operation `%s': %s\n"),
2133 if (uc->serialization != NULL)
2134 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
2135 GNUNET_free_non_null (uc->serialization);
2142 * Deserialize a download.
2144 * @param h overall context
2145 * @param rh file to deserialize from
2146 * @param parent parent download
2147 * @param search associated search
2148 * @param serialization name under which the search was serialized
2151 deserialize_download (struct GNUNET_FS_Handle *h,
2152 struct GNUNET_BIO_ReadHandle *rh,
2153 struct GNUNET_FS_DownloadContext *parent,
2154 struct GNUNET_FS_SearchResult *search,
2155 const char *serialization);
2159 * Deserialize a search.
2161 * @param h overall context
2162 * @param rh file to deserialize from
2163 * @param psearch_result parent search result
2164 * @param serialization name under which the search was serialized
2166 static struct GNUNET_FS_SearchContext *
2167 deserialize_search (struct GNUNET_FS_Handle *h,
2168 struct GNUNET_BIO_ReadHandle *rh,
2169 struct GNUNET_FS_SearchResult *psearch_result,
2170 const char *serialization);
2174 * Function called with a filename of serialized search result
2177 * @param cls the 'struct GNUNET_FS_SearchContext*'
2178 * @param filename complete filename (absolute path)
2179 * @return GNUNET_OK (continue to iterate)
2182 deserialize_search_result (void *cls,
2183 const char *filename)
2185 struct GNUNET_FS_SearchContext *sc = cls;
2191 struct GNUNET_BIO_ReadHandle *rh;
2192 struct GNUNET_BIO_ReadHandle *drh;
2193 struct GNUNET_FS_SearchResult *sr;
2195 ser = get_serialization_short_name (filename);
2196 rh = GNUNET_BIO_read_open (filename);
2201 remove_sync_file_in_dir (sc->h,
2202 (sc->psearch_result == NULL)
2203 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2204 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2215 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2216 sr->serialization = ser;
2218 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
2219 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2221 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
2223 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2225 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2227 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode))) ||
2229 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2231 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2233 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2235 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
2241 if (download != NULL)
2243 drh = get_read_handle (sc->h,
2244 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
2248 deserialize_download (sc->h,
2254 GNUNET_BIO_read_close (drh, &emsg))
2256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2257 _("Failed to resume sub-download `%s': %s\n"),
2263 GNUNET_free (download);
2265 if (update_srch != NULL)
2267 drh = get_read_handle (sc->h,
2268 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2272 deserialize_search (sc->h,
2277 GNUNET_BIO_read_close (drh, &emsg))
2279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2280 _("Failed to resume sub-search `%s': %s\n"),
2286 GNUNET_free (update_srch);
2288 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2291 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2293 GNUNET_BIO_read_close (rh, &emsg))
2295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2296 _("Failure while resuming search operation `%s': %s\n"),
2303 GNUNET_free_non_null (download);
2304 GNUNET_free_non_null (emsg);
2305 GNUNET_free_non_null (uris);
2306 GNUNET_free_non_null (update_srch);
2307 if (sr->uri != NULL)
2308 GNUNET_FS_uri_destroy (sr->uri);
2309 if (sr->meta != NULL)
2310 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2311 GNUNET_free (sr->serialization);
2314 GNUNET_BIO_read_close (rh, &emsg))
2316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2317 _("Failure while resuming search operation `%s': %s\n"),
2327 * Send the 'resume' signal to the callback; also actually
2328 * resume the download (put it in the queue). Does this
2329 * recursively for the top-level download and all child
2332 * @param dc download to resume
2335 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2337 struct GNUNET_FS_DownloadContext *dcc;
2338 struct GNUNET_FS_ProgressInfo pi;
2340 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2341 pi.value.download.specifics.resume.meta = dc->meta;
2342 pi.value.download.specifics.resume.message = dc->emsg;
2343 GNUNET_FS_download_make_status_ (&pi,
2345 dcc = dc->child_head;
2348 signal_download_resume (dcc);
2351 if (dc->pending != NULL)
2352 GNUNET_FS_download_start_downloading_ (dc);
2357 * Signal resuming of a search to our clients (for the
2358 * top level search and all sub-searches).
2360 * @param sc search being resumed
2363 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2367 * Iterator over search results signaling resume to the client for
2370 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2371 * @param key current key code
2372 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2373 * @return GNUNET_YES (we should continue to iterate)
2376 signal_result_resume (void *cls,
2377 const GNUNET_HashCode * key,
2380 struct GNUNET_FS_SearchContext *sc = cls;
2381 struct GNUNET_FS_ProgressInfo pi;
2382 struct GNUNET_FS_SearchResult *sr = value;
2384 if (0 == sr->mandatory_missing)
2386 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2387 pi.value.search.specifics.resume_result.meta = sr->meta;
2388 pi.value.search.specifics.resume_result.uri = sr->uri;
2389 pi.value.search.specifics.resume_result.result = sr;
2390 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
2391 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
2392 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
2393 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
2396 if (sr->download != NULL)
2398 signal_download_resume (sr->download);
2402 GNUNET_FS_search_start_probe_ (sr);
2404 if (sr->update_search != NULL)
2405 signal_search_resume (sr->update_search);
2411 * Free memory allocated by the search context and its children
2413 * @param sc search context to free
2416 free_search_context (struct GNUNET_FS_SearchContext *sc);
2420 * Iterator over search results freeing each.
2422 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2423 * @param key current key code
2424 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2425 * @return GNUNET_YES (we should continue to iterate)
2428 free_result (void *cls,
2429 const GNUNET_HashCode * key,
2432 struct GNUNET_FS_SearchResult *sr = value;
2434 if (sr->update_search != NULL)
2436 free_search_context (sr->update_search);
2437 GNUNET_assert (NULL == sr->update_search);
2439 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2440 GNUNET_FS_uri_destroy (sr->uri);
2447 * Free memory allocated by the search context and its children
2449 * @param sc search context to free
2452 free_search_context (struct GNUNET_FS_SearchContext *sc)
2454 if (sc->serialization != NULL)
2456 GNUNET_FS_remove_sync_file_ (sc->h,
2457 (sc->psearch_result == NULL)
2458 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2459 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2461 GNUNET_FS_remove_sync_dir_ (sc->h,
2462 (sc->psearch_result == NULL)
2463 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2464 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2467 GNUNET_free_non_null (sc->serialization);
2468 GNUNET_free_non_null (sc->emsg);
2469 if (sc->uri != NULL)
2470 GNUNET_FS_uri_destroy (sc->uri);
2471 if (sc->master_result_map != NULL)
2473 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2476 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2483 * Function called with a filename of serialized sub-download
2486 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2487 * @param filename complete filename (absolute path)
2488 * @return GNUNET_OK (continue to iterate)
2491 deserialize_subdownload (void *cls,
2492 const char *filename)
2494 struct GNUNET_FS_DownloadContext *parent = cls;
2497 struct GNUNET_BIO_ReadHandle *rh;
2499 ser = get_serialization_short_name (filename);
2500 rh = GNUNET_BIO_read_open (filename);
2503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2504 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2510 deserialize_download (parent->h,
2516 GNUNET_BIO_read_close (rh, &emsg))
2518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2519 _("Failed to resume sub-download `%s': %s\n"),
2530 * Free this download context and all of its descendants.
2531 * (only works during deserialization since not all possible
2532 * state it taken care of).
2534 * @param dc context to free
2537 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2539 struct GNUNET_FS_DownloadContext *dcc;
2540 struct DownloadRequest *dr;
2541 if (dc->meta != NULL)
2542 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2543 if (dc->uri != NULL)
2544 GNUNET_FS_uri_destroy (dc->uri);
2545 GNUNET_free_non_null (dc->temp_filename);
2546 GNUNET_free_non_null (dc->emsg);
2547 GNUNET_free_non_null (dc->filename);
2548 while (NULL != (dcc = dc->child_head))
2550 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2553 free_download_context (dcc);
2555 while (NULL != (dr = dc->pending))
2557 dc->pending = dr->next;
2565 * Deserialize a download.
2567 * @param h overall context
2568 * @param rh file to deserialize from
2569 * @param parent parent download
2570 * @param search associated search
2571 * @param serialization name under which the search was serialized
2574 deserialize_download (struct GNUNET_FS_Handle *h,
2575 struct GNUNET_BIO_ReadHandle *rh,
2576 struct GNUNET_FS_DownloadContext *parent,
2577 struct GNUNET_FS_SearchResult *search,
2578 const char *serialization)
2580 struct GNUNET_FS_DownloadContext *dc;
2581 struct DownloadRequest *dr;
2587 uint32_t num_pending;
2588 int32_t start_pending;
2593 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2594 dc->parent = parent;
2596 dc->serialization = GNUNET_strdup (serialization);
2598 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2599 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2600 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2601 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2603 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2605 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2607 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2609 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2611 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2613 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2615 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2617 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2619 read_start_time (rh, &dc->start_time)) ||
2621 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2623 GNUNET_BIO_read_int32 (rh, &options)) ||
2625 GNUNET_BIO_read_int32 (rh, &status)) ||
2627 GNUNET_BIO_read_int32 (rh, &num_pending)) ||
2629 GNUNET_BIO_read_int32 (rh, &start_pending)) )
2634 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2635 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
2636 dc->has_finished = (int) status;
2637 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2638 if (GNUNET_FS_uri_test_loc (dc->uri))
2639 GNUNET_assert (GNUNET_OK ==
2640 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2642 if ( (dc->length > dc->completed) &&
2643 (num_pending == 0) )
2648 while (0 < num_pending--)
2650 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
2652 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
2654 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
2656 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
2661 dr->is_pending = GNUNET_YES;
2662 dr->next = dc->pending;
2664 GNUNET_CONTAINER_multihashmap_put (dc->active,
2667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2671 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2675 GNUNET_DISK_directory_test (dn))
2676 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2681 abort (); // for debugging for now
2682 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2688 dc->search = search;
2689 search->download = dc;
2691 if ( (parent == NULL) &&
2694 dc->top = GNUNET_FS_make_top (dc->h,
2695 &GNUNET_FS_download_signal_suspend_,
2697 signal_download_resume (dc);
2702 = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2705 GNUNET_free_non_null (uris);
2706 GNUNET_free_non_null (dr);
2707 GNUNET_free_non_null (emsg);
2708 free_download_context (dc);
2713 * Signal resuming of a search to our clients (for the
2714 * top level search and all sub-searches).
2716 * @param sc search being resumed
2719 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2721 struct GNUNET_FS_ProgressInfo pi;
2723 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2724 pi.value.search.specifics.resume.message = sc->emsg;
2725 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2726 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2728 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2729 &signal_result_resume,
2736 * Deserialize a search.
2738 * @param h overall context
2739 * @param rh file to deserialize from
2740 * @param psearch_result parent search result
2741 * @param serialization name under which the search was serialized
2743 static struct GNUNET_FS_SearchContext *
2744 deserialize_search (struct GNUNET_FS_Handle *h,
2745 struct GNUNET_BIO_ReadHandle *rh,
2746 struct GNUNET_FS_SearchResult *psearch_result,
2747 const char *serialization)
2749 struct GNUNET_FS_SearchContext *sc;
2756 if ( (psearch_result != NULL) &&
2757 (psearch_result->update_search != NULL) )
2764 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2765 if (psearch_result != NULL)
2767 sc->psearch_result = psearch_result;
2768 psearch_result->update_search = sc;
2771 sc->serialization = GNUNET_strdup (serialization);
2773 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2774 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2775 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2776 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2778 read_start_time (rh, &sc->start_time)) ||
2780 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2782 GNUNET_BIO_read_int32 (rh, &options)) ||
2784 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2786 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2791 sc->options = (enum GNUNET_FS_SearchOptions) options;
2792 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2793 dn = get_serialization_file_name_in_dir (h,
2794 (sc->psearch_result == NULL)
2795 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2796 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2802 GNUNET_DISK_directory_test (dn))
2803 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2806 if ( ('\0' == in_pause) &&
2808 GNUNET_FS_search_start_searching_ (sc)) )
2810 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2811 _("Could not resume running search, will resume as paused search\n"));
2813 signal_search_resume (sc);
2817 GNUNET_free_non_null (emsg);
2818 free_search_context (sc);
2819 GNUNET_free_non_null (uris);
2825 * Function called with a filename of serialized search operation
2828 * @param cls the 'struct GNUNET_FS_Handle*'
2829 * @param filename complete filename (absolute path)
2830 * @return GNUNET_OK (continue to iterate)
2833 deserialize_search_file (void *cls,
2834 const char *filename)
2836 struct GNUNET_FS_Handle *h = cls;
2839 struct GNUNET_BIO_ReadHandle *rh;
2840 struct GNUNET_FS_SearchContext *sc;
2842 ser = get_serialization_short_name (filename);
2843 rh = GNUNET_BIO_read_open (filename);
2848 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2853 sc = deserialize_search (h, rh, NULL, ser);
2855 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2858 GNUNET_BIO_read_close (rh, &emsg))
2860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2861 _("Failure while resuming search operation `%s': %s\n"),
2871 * Function called with a filename of serialized download operation
2874 * @param cls the 'struct GNUNET_FS_Handle*'
2875 * @param filename complete filename (absolute path)
2876 * @return GNUNET_OK (continue to iterate)
2879 deserialize_download_file (void *cls,
2880 const char *filename)
2882 struct GNUNET_FS_Handle *h = cls;
2885 struct GNUNET_BIO_ReadHandle *rh;
2887 ser = get_serialization_short_name (filename);
2888 rh = GNUNET_BIO_read_open (filename);
2891 if (0 != UNLINK (filename))
2892 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2898 deserialize_download (h, rh, NULL, NULL, ser);
2901 GNUNET_BIO_read_close (rh, &emsg))
2903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2904 _("Failure while resuming download operation `%s': %s\n"),
2914 * Deserialize informatin about pending operations.
2916 * @param master_path which master directory should be scanned
2917 * @param proc function to call for each entry (will get 'h' for 'cls')
2918 * @param h the 'struct GNUNET_FS_Handle*'
2921 deserialization_master (const char *master_path,
2922 GNUNET_FileNameCallback proc,
2923 struct GNUNET_FS_Handle *h)
2927 dn = get_serialization_file_name (h, master_path, "");
2931 GNUNET_DISK_directory_test (dn))
2932 GNUNET_DISK_directory_scan (dn, proc, h);
2938 * Setup a connection to the file-sharing service.
2940 * @param cfg configuration to use
2941 * @param client_name unique identifier for this client
2942 * @param upcb function to call to notify about FS actions
2943 * @param upcb_cls closure for upcb
2944 * @param flags specific attributes for fs-operations
2945 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2946 * @return NULL on error
2948 struct GNUNET_FS_Handle *
2949 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2950 const char *client_name,
2951 GNUNET_FS_ProgressCallback upcb,
2953 enum GNUNET_FS_Flags flags,
2956 struct GNUNET_FS_Handle *ret;
2957 enum GNUNET_FS_OPTIONS opt;
2960 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2962 ret->client_name = GNUNET_strdup (client_name);
2964 ret->upcb_cls = upcb_cls;
2966 ret->max_parallel_downloads = 1;
2967 ret->max_parallel_requests = 1;
2968 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2969 va_start (ap, flags);
2970 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2974 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2975 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2977 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2978 ret->max_parallel_requests = va_arg (ap, unsigned int);
2982 GNUNET_free (ret->client_name);
2989 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2991 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
2992 &deserialize_publish_file,
2994 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
2995 &deserialize_search_file,
2997 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2998 &deserialize_download_file,
3000 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3001 &deserialize_unindex_file,
3009 * Close our connection with the file-sharing service.
3010 * The callback given to GNUNET_FS_start will no longer be
3011 * called after this function returns.
3013 * @param h handle that was returned from GNUNET_FS_start
3016 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3018 while (h->top_head != NULL)
3019 h->top_head->ssf (h->top_head->ssf_cls);
3020 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3021 GNUNET_SCHEDULER_cancel (h->queue_job);
3022 GNUNET_free (h->client_name);