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_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1483 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1484 &GNUNET_FS_publish_main_,
1488 GNUNET_BIO_read_close (rh, &emsg))
1490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1491 _("Failure while resuming publishing operation `%s': %s\n"),
1496 GNUNET_free_non_null (ns);
1497 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1500 GNUNET_free_non_null (pc->nid);
1501 GNUNET_free_non_null (pc->nuid);
1502 GNUNET_free_non_null (fi_root);
1503 GNUNET_free_non_null (fi_pos);
1504 GNUNET_free_non_null (ns);
1505 if ( (rh != NULL) &&
1507 GNUNET_BIO_read_close (rh, &emsg)) )
1509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1510 _("Failed to resume publishing operation `%s': %s\n"),
1516 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1517 if (0 != UNLINK (filename))
1518 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1519 GNUNET_free (pc->serialization);
1526 * Synchronize this publishing struct with its mirror
1527 * on disk. Note that all internal FS-operations that change
1528 * publishing structs should already call "sync" internally,
1529 * so this function is likely not useful for clients.
1531 * @param pc the struct to sync
1534 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1536 struct GNUNET_BIO_WriteHandle *wh;
1538 if (NULL == pc->serialization)
1539 pc->serialization = make_serialization_file_name (pc->h,
1540 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1541 if (NULL == pc->serialization)
1545 if (NULL == pc->fi->serialization)
1550 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1557 GNUNET_BIO_write_string (wh, pc->nid)) ||
1559 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1561 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1563 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1565 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1567 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1569 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1575 GNUNET_BIO_write_close (wh))
1584 (void) GNUNET_BIO_write_close (wh);
1585 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1586 GNUNET_free (pc->serialization);
1587 pc->serialization = NULL;
1592 * Synchronize this unindex struct with its mirror
1593 * on disk. Note that all internal FS-operations that change
1594 * publishing structs should already call "sync" internally,
1595 * so this function is likely not useful for clients.
1597 * @param uc the struct to sync
1600 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1602 struct GNUNET_BIO_WriteHandle *wh;
1604 if (NULL == uc->serialization)
1605 uc->serialization = make_serialization_file_name (uc->h,
1606 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1607 if (NULL == uc->serialization)
1609 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1616 GNUNET_BIO_write_string (wh, uc->filename)) ||
1618 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1620 write_start_time (wh, uc->start_time)) ||
1622 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1623 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1625 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1626 ( (uc->state == UNINDEX_STATE_ERROR) &&
1628 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1634 GNUNET_BIO_write_close (wh))
1643 (void) GNUNET_BIO_write_close (wh);
1644 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1645 GNUNET_free (uc->serialization);
1646 uc->serialization = NULL;
1651 * Serialize a download request.
1653 * @param wh the 'struct GNUNET_BIO_WriteHandle*'
1654 * @param dr the 'struct DownloadRequest'
1655 * @return GNUNET_YES on success, GNUNET_NO on error
1658 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1659 struct DownloadRequest *dr)
1664 GNUNET_BIO_write_int32 (wh, dr->state)) ||
1666 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1668 GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1670 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1672 if ( (dr->state == BRS_CHK_SET) &&
1674 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) )
1676 for (i=0;i<dr->num_children;i++)
1678 write_download_request (wh, dr->children[i]))
1685 * Read a download request tree.
1687 * @param rh stream to read from
1688 * @return value the 'struct DownloadRequest', NULL on error
1690 static struct DownloadRequest *
1691 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1693 struct DownloadRequest *dr;
1696 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1699 GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1701 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1703 GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1704 (dr->num_children > CHK_PER_INODE) ||
1706 GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1707 ( (dr->depth == 0) && (dr->num_children > 0) ) ||
1708 ( (dr->depth > 0) && (dr->num_children == 0) ) )
1711 dr->num_children = 0;
1714 if (dr->num_children > 0)
1715 dr->children = GNUNET_malloc (dr->num_children *
1716 sizeof (struct ContentHashKey));
1720 case BRS_RECONSTRUCT_DOWN:
1721 case BRS_RECONSTRUCT_META_UP:
1722 case BRS_RECONSTRUCT_UP:
1726 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1729 case BRS_DOWNLOAD_DOWN:
1730 case BRS_DOWNLOAD_UP:
1737 for (i=0;i<dr->num_children;i++)
1739 if (NULL == (dr->children[i] = read_download_request (rh)))
1741 dr->children[i]->parent = dr;
1745 GNUNET_FS_free_download_request_ (dr);
1751 * Compute the name of the sync file (or directory) for the given download
1754 * @param dc download context to compute for
1755 * @param uni unique filename to use, use "" for the directory name
1756 * @param ext extension to use, use ".dir" for our own subdirectory
1757 * @return the expanded file name, NULL for none
1760 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1767 if (dc->parent == NULL)
1768 return get_serialization_file_name (dc->h,
1769 (dc->search != NULL) ?
1770 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1771 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1773 if (dc->parent->serialization == NULL)
1775 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1778 GNUNET_asprintf (&epar,
1790 * Synchronize this download struct with its mirror
1791 * on disk. Note that all internal FS-operations that change
1792 * publishing structs should already call "sync" internally,
1793 * so this function is likely not useful for clients.
1795 * @param dc the struct to sync
1798 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1800 struct GNUNET_BIO_WriteHandle *wh;
1805 if (NULL == dc->serialization)
1807 dir = get_download_sync_filename (dc, "", "");
1811 GNUNET_DISK_directory_create_for_file (dir))
1816 fn = GNUNET_DISK_mktemp (dir);
1820 dc->serialization = get_serialization_short_name (fn);
1824 fn = get_download_sync_filename (dc, dc->serialization, "");
1827 GNUNET_free (dc->serialization);
1828 dc->serialization = NULL;
1833 wh = GNUNET_BIO_write_open (fn);
1836 GNUNET_free (dc->serialization);
1837 dc->serialization = NULL;
1841 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1842 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1843 uris = GNUNET_FS_uri_to_string (dc->uri);
1845 GNUNET_BIO_write_string (wh, uris)) ||
1847 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1849 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1851 GNUNET_BIO_write_string (wh, dc->filename)) ||
1853 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1855 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1857 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1859 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1861 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1863 write_start_time (wh, dc->start_time)) ||
1865 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1867 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1869 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) )
1874 if (NULL == dc->emsg)
1876 GNUNET_assert (dc->top_request != NULL);
1878 write_download_request (wh, dc->top_request))
1884 GNUNET_free_non_null (uris);
1887 GNUNET_BIO_write_close (wh))
1897 (void) GNUNET_BIO_write_close (wh);
1898 GNUNET_free_non_null (uris);
1899 if (0 != UNLINK (fn))
1900 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1902 GNUNET_free (dc->serialization);
1903 dc->serialization = NULL;
1908 * Synchronize this search result with its mirror
1909 * on disk. Note that all internal FS-operations that change
1910 * publishing structs should already call "sync" internally,
1911 * so this function is likely not useful for clients.
1913 * @param sr the struct to sync
1916 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1918 struct GNUNET_BIO_WriteHandle *wh;
1922 if (NULL == sr->serialization)
1923 sr->serialization = make_serialization_file_name_in_dir (sr->sc->h,
1924 (sr->sc->psearch_result == NULL)
1925 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1926 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1927 sr->sc->serialization);
1928 if (NULL == sr->serialization)
1930 wh = get_write_handle_in_dir (sr->sc->h,
1931 (sr->sc->psearch_result == NULL)
1932 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1933 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1934 sr->sc->serialization,
1941 uris = GNUNET_FS_uri_to_string (sr->uri);
1943 GNUNET_BIO_write_string (wh, uris)) ||
1945 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1947 GNUNET_BIO_write_string (wh, sr->update_search != NULL ? sr->update_search->serialization : NULL)) ||
1949 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1951 GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode))) ||
1953 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1955 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1957 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1959 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1965 GNUNET_BIO_write_close (wh))
1971 GNUNET_free_non_null (uris);
1974 GNUNET_free_non_null (uris);
1976 (void) GNUNET_BIO_write_close (wh);
1977 remove_sync_file_in_dir (sr->sc->h,
1978 (sr->sc->psearch_result == NULL)
1979 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1980 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1981 sr->sc->serialization,
1983 GNUNET_free (sr->serialization);
1984 sr->serialization = NULL;
1989 * Synchronize this search struct with its mirror
1990 * on disk. Note that all internal FS-operations that change
1991 * publishing structs should already call "sync" internally,
1992 * so this function is likely not useful for clients.
1994 * @param sc the struct to sync
1997 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1999 struct GNUNET_BIO_WriteHandle *wh;
2002 const char *category;
2004 category = (sc->psearch_result == NULL)
2005 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2006 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2007 if (NULL == sc->serialization)
2008 sc->serialization = make_serialization_file_name (sc->h,
2010 if (NULL == sc->serialization)
2013 wh = get_write_handle (sc->h, category, sc->serialization);
2019 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2020 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
2021 uris = GNUNET_FS_uri_to_string (sc->uri);
2022 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2024 GNUNET_BIO_write_string (wh, uris)) ||
2026 write_start_time (wh, sc->start_time)) ||
2028 GNUNET_BIO_write_string (wh, sc->emsg)) ||
2030 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2032 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2034 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
2042 GNUNET_BIO_write_close (wh))
2051 (void) GNUNET_BIO_write_close (wh);
2052 GNUNET_free_non_null (uris);
2053 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2054 GNUNET_free (sc->serialization);
2055 sc->serialization = NULL;
2060 * Function called with a filename of serialized unindexing operation
2063 * @param cls the 'struct GNUNET_FS_Handle*'
2064 * @param filename complete filename (absolute path)
2065 * @return GNUNET_OK (continue to iterate)
2068 deserialize_unindex_file (void *cls,
2069 const char *filename)
2071 struct GNUNET_FS_Handle *h = cls;
2072 struct GNUNET_BIO_ReadHandle *rh;
2073 struct GNUNET_FS_UnindexContext *uc;
2074 struct GNUNET_FS_ProgressInfo pi;
2078 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
2080 uc->serialization = get_serialization_short_name (filename);
2081 rh = GNUNET_BIO_read_open (filename);
2088 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
2090 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2092 read_start_time (rh, &uc->start_time)) ||
2094 GNUNET_BIO_read_int32 (rh, &state)) )
2099 uc->state = (enum UnindexState) state;
2102 case UNINDEX_STATE_HASHING:
2104 case UNINDEX_STATE_FS_NOTIFY:
2106 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
2112 case UNINDEX_STATE_DS_REMOVE:
2114 case UNINDEX_STATE_COMPLETE:
2116 case UNINDEX_STATE_ERROR:
2118 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
2128 uc->top = GNUNET_FS_make_top (h,
2129 &GNUNET_FS_unindex_signal_suspend_,
2131 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2132 pi.value.unindex.specifics.resume.message = uc->emsg;
2133 GNUNET_FS_unindex_make_status_ (&pi,
2135 (uc->state == UNINDEX_STATE_COMPLETE)
2140 case UNINDEX_STATE_HASHING:
2141 uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
2144 &GNUNET_FS_unindex_process_hash_,
2147 case UNINDEX_STATE_FS_NOTIFY:
2148 uc->state = UNINDEX_STATE_HASHING;
2149 GNUNET_FS_unindex_process_hash_ (uc,
2152 case UNINDEX_STATE_DS_REMOVE:
2153 GNUNET_FS_unindex_do_remove_ (uc);
2155 case UNINDEX_STATE_COMPLETE:
2156 case UNINDEX_STATE_ERROR:
2157 /* no need to resume any operation, we were done */
2163 GNUNET_BIO_read_close (rh, &emsg))
2165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2166 _("Failure while resuming unindexing operation `%s': %s\n"),
2173 GNUNET_free_non_null (uc->filename);
2174 if ( (rh != NULL) &&
2176 GNUNET_BIO_read_close (rh, &emsg)) )
2178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2179 _("Failed to resume unindexing operation `%s': %s\n"),
2184 if (uc->serialization != NULL)
2185 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
2186 GNUNET_free_non_null (uc->serialization);
2193 * Deserialize a download.
2195 * @param h overall context
2196 * @param rh file to deserialize from
2197 * @param parent parent download
2198 * @param search associated search
2199 * @param serialization name under which the search was serialized
2202 deserialize_download (struct GNUNET_FS_Handle *h,
2203 struct GNUNET_BIO_ReadHandle *rh,
2204 struct GNUNET_FS_DownloadContext *parent,
2205 struct GNUNET_FS_SearchResult *search,
2206 const char *serialization);
2210 * Deserialize a search.
2212 * @param h overall context
2213 * @param rh file to deserialize from
2214 * @param psearch_result parent search result
2215 * @param serialization name under which the search was serialized
2217 static struct GNUNET_FS_SearchContext *
2218 deserialize_search (struct GNUNET_FS_Handle *h,
2219 struct GNUNET_BIO_ReadHandle *rh,
2220 struct GNUNET_FS_SearchResult *psearch_result,
2221 const char *serialization);
2225 * Function called with a filename of serialized search result
2228 * @param cls the 'struct GNUNET_FS_SearchContext*'
2229 * @param filename complete filename (absolute path)
2230 * @return GNUNET_OK (continue to iterate)
2233 deserialize_search_result (void *cls,
2234 const char *filename)
2236 struct GNUNET_FS_SearchContext *sc = cls;
2242 struct GNUNET_BIO_ReadHandle *rh;
2243 struct GNUNET_BIO_ReadHandle *drh;
2244 struct GNUNET_FS_SearchResult *sr;
2246 ser = get_serialization_short_name (filename);
2247 rh = GNUNET_BIO_read_open (filename);
2252 remove_sync_file_in_dir (sc->h,
2253 (sc->psearch_result == NULL)
2254 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2255 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2266 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2267 sr->serialization = ser;
2269 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
2270 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2272 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
2274 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2276 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2278 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode))) ||
2280 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2282 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2284 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2286 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
2292 if (download != NULL)
2294 drh = get_read_handle (sc->h,
2295 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
2299 deserialize_download (sc->h,
2305 GNUNET_BIO_read_close (drh, &emsg))
2307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2308 _("Failed to resume sub-download `%s': %s\n"),
2314 GNUNET_free (download);
2316 if (update_srch != NULL)
2318 drh = get_read_handle (sc->h,
2319 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2323 deserialize_search (sc->h,
2328 GNUNET_BIO_read_close (drh, &emsg))
2330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2331 _("Failed to resume sub-search `%s': %s\n"),
2337 GNUNET_free (update_srch);
2339 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2344 GNUNET_BIO_read_close (rh, &emsg))
2346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2347 _("Failure while resuming search operation `%s': %s\n"),
2354 GNUNET_free_non_null (download);
2355 GNUNET_free_non_null (emsg);
2356 GNUNET_free_non_null (uris);
2357 GNUNET_free_non_null (update_srch);
2358 if (sr->uri != NULL)
2359 GNUNET_FS_uri_destroy (sr->uri);
2360 if (sr->meta != NULL)
2361 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2362 GNUNET_free (sr->serialization);
2365 GNUNET_BIO_read_close (rh, &emsg))
2367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2368 _("Failure while resuming search operation `%s': %s\n"),
2378 * Send the 'resume' signal to the callback; also actually
2379 * resume the download (put it in the queue). Does this
2380 * recursively for the top-level download and all child
2383 * @param dc download to resume
2386 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2388 struct GNUNET_FS_DownloadContext *dcc;
2389 struct GNUNET_FS_ProgressInfo pi;
2391 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2392 pi.value.download.specifics.resume.meta = dc->meta;
2393 pi.value.download.specifics.resume.message = dc->emsg;
2394 GNUNET_FS_download_make_status_ (&pi,
2396 dcc = dc->child_head;
2399 signal_download_resume (dcc);
2402 if (dc->pending_head != NULL)
2403 GNUNET_FS_download_start_downloading_ (dc);
2408 * Signal resuming of a search to our clients (for the
2409 * top level search and all sub-searches).
2411 * @param sc search being resumed
2414 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2418 * Iterator over search results signaling resume to the client for
2421 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2422 * @param key current key code
2423 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2424 * @return GNUNET_YES (we should continue to iterate)
2427 signal_result_resume (void *cls,
2428 const GNUNET_HashCode * key,
2431 struct GNUNET_FS_SearchContext *sc = cls;
2432 struct GNUNET_FS_ProgressInfo pi;
2433 struct GNUNET_FS_SearchResult *sr = value;
2435 if (0 == sr->mandatory_missing)
2437 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2438 pi.value.search.specifics.resume_result.meta = sr->meta;
2439 pi.value.search.specifics.resume_result.uri = sr->uri;
2440 pi.value.search.specifics.resume_result.result = sr;
2441 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
2442 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
2443 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
2444 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
2447 if (sr->download != NULL)
2449 signal_download_resume (sr->download);
2453 GNUNET_FS_search_start_probe_ (sr);
2455 if (sr->update_search != NULL)
2456 signal_search_resume (sr->update_search);
2462 * Free memory allocated by the search context and its children
2464 * @param sc search context to free
2467 free_search_context (struct GNUNET_FS_SearchContext *sc);
2471 * Iterator over search results freeing each.
2473 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2474 * @param key current key code
2475 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2476 * @return GNUNET_YES (we should continue to iterate)
2479 free_result (void *cls,
2480 const GNUNET_HashCode * key,
2483 struct GNUNET_FS_SearchResult *sr = value;
2485 if (sr->update_search != NULL)
2487 free_search_context (sr->update_search);
2488 GNUNET_assert (NULL == sr->update_search);
2490 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2491 GNUNET_FS_uri_destroy (sr->uri);
2498 * Free memory allocated by the search context and its children
2500 * @param sc search context to free
2503 free_search_context (struct GNUNET_FS_SearchContext *sc)
2505 if (sc->serialization != NULL)
2507 GNUNET_FS_remove_sync_file_ (sc->h,
2508 (sc->psearch_result == NULL)
2509 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2510 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2512 GNUNET_FS_remove_sync_dir_ (sc->h,
2513 (sc->psearch_result == NULL)
2514 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2515 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2518 GNUNET_free_non_null (sc->serialization);
2519 GNUNET_free_non_null (sc->emsg);
2520 if (sc->uri != NULL)
2521 GNUNET_FS_uri_destroy (sc->uri);
2522 if (sc->master_result_map != NULL)
2524 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2527 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2534 * Function called with a filename of serialized sub-download
2537 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2538 * @param filename complete filename (absolute path)
2539 * @return GNUNET_OK (continue to iterate)
2542 deserialize_subdownload (void *cls,
2543 const char *filename)
2545 struct GNUNET_FS_DownloadContext *parent = cls;
2548 struct GNUNET_BIO_ReadHandle *rh;
2550 ser = get_serialization_short_name (filename);
2551 rh = GNUNET_BIO_read_open (filename);
2554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2555 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2561 deserialize_download (parent->h,
2567 GNUNET_BIO_read_close (rh, &emsg))
2569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2570 _("Failed to resume sub-download `%s': %s\n"),
2581 * Free this download context and all of its descendants.
2582 * (only works during deserialization since not all possible
2583 * state it taken care of).
2585 * @param dc context to free
2588 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2590 struct GNUNET_FS_DownloadContext *dcc;
2592 if (dc->meta != NULL)
2593 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2594 if (dc->uri != NULL)
2595 GNUNET_FS_uri_destroy (dc->uri);
2596 GNUNET_free_non_null (dc->temp_filename);
2597 GNUNET_free_non_null (dc->emsg);
2598 GNUNET_free_non_null (dc->filename);
2599 GNUNET_free_non_null (dc->serialization);
2600 while (NULL != (dcc = dc->child_head))
2602 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2605 free_download_context (dcc);
2607 GNUNET_FS_free_download_request_ (dc->top_request);
2608 if (NULL != dc->active)
2609 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2615 * Deserialize a download.
2617 * @param h overall context
2618 * @param rh file to deserialize from
2619 * @param parent parent download
2620 * @param search associated search
2621 * @param serialization name under which the search was serialized
2624 deserialize_download (struct GNUNET_FS_Handle *h,
2625 struct GNUNET_BIO_ReadHandle *rh,
2626 struct GNUNET_FS_DownloadContext *parent,
2627 struct GNUNET_FS_SearchResult *search,
2628 const char *serialization)
2630 struct GNUNET_FS_DownloadContext *dc;
2639 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2640 dc->parent = parent;
2642 dc->serialization = GNUNET_strdup (serialization);
2644 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2645 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2646 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2647 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2649 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2651 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2653 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2655 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2657 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2659 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2661 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2663 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2665 read_start_time (rh, &dc->start_time)) ||
2667 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2669 GNUNET_BIO_read_int32 (rh, &options)) ||
2671 GNUNET_BIO_read_int32 (rh, &status)) )
2676 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2677 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE));
2678 dc->has_finished = (int) status;
2679 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2680 if (GNUNET_FS_uri_test_loc (dc->uri))
2681 GNUNET_assert (GNUNET_OK ==
2682 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2684 if (dc->emsg == NULL)
2686 dc->top_request = read_download_request (rh);
2687 if (dc->top_request == NULL)
2693 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2697 GNUNET_DISK_directory_test (dn))
2698 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2703 abort (); // for debugging for now
2704 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2710 dc->search = search;
2711 search->download = dc;
2713 if ( (parent == NULL) &&
2716 dc->top = GNUNET_FS_make_top (dc->h,
2717 &GNUNET_FS_download_signal_suspend_,
2719 signal_download_resume (dc);
2723 = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2726 GNUNET_free_non_null (uris);
2727 GNUNET_free_non_null (emsg);
2728 free_download_context (dc);
2733 * Signal resuming of a search to our clients (for the
2734 * top level search and all sub-searches).
2736 * @param sc search being resumed
2739 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2741 struct GNUNET_FS_ProgressInfo pi;
2743 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2744 pi.value.search.specifics.resume.message = sc->emsg;
2745 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2746 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2748 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2749 &signal_result_resume,
2756 * Deserialize a search.
2758 * @param h overall context
2759 * @param rh file to deserialize from
2760 * @param psearch_result parent search result
2761 * @param serialization name under which the search was serialized
2763 static struct GNUNET_FS_SearchContext *
2764 deserialize_search (struct GNUNET_FS_Handle *h,
2765 struct GNUNET_BIO_ReadHandle *rh,
2766 struct GNUNET_FS_SearchResult *psearch_result,
2767 const char *serialization)
2769 struct GNUNET_FS_SearchContext *sc;
2776 if ( (psearch_result != NULL) &&
2777 (psearch_result->update_search != NULL) )
2784 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2785 if (psearch_result != NULL)
2787 sc->psearch_result = psearch_result;
2788 psearch_result->update_search = sc;
2791 sc->serialization = GNUNET_strdup (serialization);
2793 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2794 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2795 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2796 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2798 read_start_time (rh, &sc->start_time)) ||
2800 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2802 GNUNET_BIO_read_int32 (rh, &options)) ||
2804 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2806 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2811 sc->options = (enum GNUNET_FS_SearchOptions) options;
2812 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2813 dn = get_serialization_file_name_in_dir (h,
2814 (sc->psearch_result == NULL)
2815 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2816 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2822 GNUNET_DISK_directory_test (dn))
2823 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2826 if ( ('\0' == in_pause) &&
2828 GNUNET_FS_search_start_searching_ (sc)) )
2830 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2831 _("Could not resume running search, will resume as paused search\n"));
2833 signal_search_resume (sc);
2837 GNUNET_free_non_null (emsg);
2838 free_search_context (sc);
2839 GNUNET_free_non_null (uris);
2845 * Function called with a filename of serialized search operation
2848 * @param cls the 'struct GNUNET_FS_Handle*'
2849 * @param filename complete filename (absolute path)
2850 * @return GNUNET_OK (continue to iterate)
2853 deserialize_search_file (void *cls,
2854 const char *filename)
2856 struct GNUNET_FS_Handle *h = cls;
2859 struct GNUNET_BIO_ReadHandle *rh;
2860 struct GNUNET_FS_SearchContext *sc;
2862 ser = get_serialization_short_name (filename);
2863 rh = GNUNET_BIO_read_open (filename);
2868 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2873 sc = deserialize_search (h, rh, NULL, ser);
2875 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2878 GNUNET_BIO_read_close (rh, &emsg))
2880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2881 _("Failure while resuming search operation `%s': %s\n"),
2891 * Function called with a filename of serialized download operation
2894 * @param cls the 'struct GNUNET_FS_Handle*'
2895 * @param filename complete filename (absolute path)
2896 * @return GNUNET_OK (continue to iterate)
2899 deserialize_download_file (void *cls,
2900 const char *filename)
2902 struct GNUNET_FS_Handle *h = cls;
2905 struct GNUNET_BIO_ReadHandle *rh;
2907 ser = get_serialization_short_name (filename);
2908 rh = GNUNET_BIO_read_open (filename);
2911 if (0 != UNLINK (filename))
2912 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2918 deserialize_download (h, rh, NULL, NULL, ser);
2921 GNUNET_BIO_read_close (rh, &emsg))
2923 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2924 _("Failure while resuming download operation `%s': %s\n"),
2934 * Deserialize informatin about pending operations.
2936 * @param master_path which master directory should be scanned
2937 * @param proc function to call for each entry (will get 'h' for 'cls')
2938 * @param h the 'struct GNUNET_FS_Handle*'
2941 deserialization_master (const char *master_path,
2942 GNUNET_FileNameCallback proc,
2943 struct GNUNET_FS_Handle *h)
2947 dn = get_serialization_file_name (h, master_path, "");
2951 GNUNET_DISK_directory_test (dn))
2952 GNUNET_DISK_directory_scan (dn, proc, h);
2958 * Setup a connection to the file-sharing service.
2960 * @param cfg configuration to use
2961 * @param client_name unique identifier for this client
2962 * @param upcb function to call to notify about FS actions
2963 * @param upcb_cls closure for upcb
2964 * @param flags specific attributes for fs-operations
2965 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2966 * @return NULL on error
2968 struct GNUNET_FS_Handle *
2969 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2970 const char *client_name,
2971 GNUNET_FS_ProgressCallback upcb,
2973 enum GNUNET_FS_Flags flags,
2976 struct GNUNET_FS_Handle *ret;
2977 enum GNUNET_FS_OPTIONS opt;
2980 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2982 ret->client_name = GNUNET_strdup (client_name);
2984 ret->upcb_cls = upcb_cls;
2986 ret->max_parallel_downloads = 1;
2987 ret->max_parallel_requests = 1;
2988 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2989 va_start (ap, flags);
2990 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2994 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2995 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2997 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2998 ret->max_parallel_requests = va_arg (ap, unsigned int);
3002 GNUNET_free (ret->client_name);
3009 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3011 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3012 &deserialize_publish_file,
3014 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3015 &deserialize_search_file,
3017 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3018 &deserialize_download_file,
3020 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3021 &deserialize_unindex_file,
3029 * Close our connection with the file-sharing service.
3030 * The callback given to GNUNET_FS_start will no longer be
3031 * called after this function returns.
3033 * @param h handle that was returned from GNUNET_FS_start
3036 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3038 while (h->top_head != NULL)
3039 h->top_head->ssf (h->top_head->ssf_cls);
3040 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3041 GNUNET_SCHEDULER_cancel (h->queue_job);
3042 GNUNET_free (h->client_name);