2 This file is part of GNUnet.
3 (C) 2001--2012 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"
34 * How many block requests can we have outstanding in parallel at a time by default?
36 #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
39 * How many downloads can we have outstanding in parallel at a time by default?
41 #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
44 * Start the given job (send signal, remove from pending queue, update
45 * counters and state).
47 * @param qe job to start
50 start_job (struct GNUNET_FS_QueueEntry *qe)
52 GNUNET_assert (NULL == qe->client);
53 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
54 if (NULL == qe->client)
59 qe->start (qe->cls, qe->client);
61 qe->h->active_blocks += qe->blocks;
62 qe->h->active_downloads++;
63 qe->start_time = GNUNET_TIME_absolute_get ();
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "Starting job %p (%u active)\n",
67 qe->h->active_downloads);
68 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
71 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
79 * Stop the given job (send signal, remove from active queue, update
80 * counters and state).
82 * @param qe job to stop
85 stop_job (struct GNUNET_FS_QueueEntry *qe)
89 GNUNET_assert (0 < qe->h->active_downloads);
90 qe->h->active_downloads--;
91 qe->h->active_blocks -= qe->blocks;
93 GNUNET_TIME_relative_add (qe->run_time,
94 GNUNET_TIME_absolute_get_duration
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Stopping job %p (%u active)\n",
99 qe->h->active_downloads);
100 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
101 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
102 qe->h->pending_tail, qe);
107 * Process the jobs in the job queue, possibly starting some
108 * and stopping others.
110 * @param cls the `struct GNUNET_FS_Handle *`
111 * @param tc scheduler context
114 process_job_queue (void *cls,
115 const struct GNUNET_SCHEDULER_TaskContext *tc)
117 struct GNUNET_FS_Handle *h = cls;
118 struct GNUNET_FS_QueueEntry *qe;
119 struct GNUNET_FS_QueueEntry *next;
120 struct GNUNET_TIME_Relative run_time;
121 struct GNUNET_TIME_Relative restart_at;
122 struct GNUNET_TIME_Relative rst;
123 struct GNUNET_TIME_Absolute end_time;
124 unsigned int num_downloads_waiting;
125 unsigned int num_downloads_active;
126 unsigned int num_downloads_expired;
127 unsigned int num_probes_active;
128 unsigned int num_probes_waiting;
129 unsigned int num_probes_expired;
130 int num_probes_change;
131 int num_downloads_change;
134 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
135 /* restart_at will be set to the time when it makes sense to
136 re-evaluate the job queue (unless, of course, jobs complete
137 or are added, then we'll be triggered immediately */
138 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
139 /* first, calculate some basic statistics on pending jobs */
140 num_probes_waiting = 0;
141 num_downloads_waiting = 0;
142 for (qe = h->pending_head; NULL != qe; qe = qe->next)
144 switch (qe->priority)
146 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
147 num_probes_waiting++;
149 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
150 num_downloads_waiting++;
157 /* now, calculate some basic statistics on running jobs */
158 num_probes_active = 0;
159 num_probes_expired = 0;
160 num_downloads_active = 0;
161 num_downloads_expired = 0;
162 next = h->running_head;
163 while (NULL != (qe = next))
166 switch (qe->priority)
168 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
169 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
170 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
171 rst = GNUNET_TIME_absolute_get_remaining (end_time);
172 if (0 == rst.rel_value_us)
174 num_probes_expired++;
180 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
183 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
185 GNUNET_TIME_relative_multiply (h->avg_block_latency,
186 qe->blocks * qe->start_times);
187 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
188 rst = GNUNET_TIME_absolute_get_remaining (end_time);
189 if (0 == rst.rel_value_us)
191 num_downloads_expired++;
196 num_downloads_active++;
197 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
210 num_downloads_active,
211 num_downloads_expired,
212 num_downloads_waiting);
213 /* calculate start/stop decisions */
214 if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
216 /* stop as many probes as there are downloads and probes */
217 num_probes_change = - GNUNET_MIN (num_probes_active,
218 num_downloads_waiting);
219 /* start as many downloads as there are free slots, including those
221 num_downloads_change = h->max_parallel_downloads - h->active_downloads - num_probes_change;
225 /* start all downloads (we can) */
226 num_downloads_change = num_downloads_waiting;
227 /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
228 if (h->max_parallel_downloads / 2 >= (h->active_downloads + num_downloads_change))
229 num_probes_change = GNUNET_MIN (num_probes_waiting,
230 (1 + h->max_parallel_downloads / 4) - (h->active_downloads + num_downloads_change));
232 num_probes_change = 0;
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Changing %d probes and %d downloads\n",
238 num_downloads_change);
239 /* actually stop probes */
240 next = h->running_head;
241 while (NULL != (qe = next))
244 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
246 if (num_probes_change < 0)
250 if (0 == num_probes_change)
254 GNUNET_break (0 <= num_probes_change);
256 /* start some more tasks if we now have empty slots */
257 block_limit_hit = GNUNET_NO;
258 next = h->pending_head;
259 while ( (NULL != (qe = next)) &&
260 ( (num_probes_change > 0) ||
261 (num_downloads_change > 0) ) )
264 switch (qe->priority)
266 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
267 if (num_probes_change > 0)
271 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
272 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
275 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
276 if ( (num_downloads_change > 0) &&
277 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
278 ( (qe->blocks > h->max_parallel_requests) &&
279 (0 == h->active_downloads) ) ) )
282 num_downloads_change--;
284 else if (num_downloads_change > 0)
285 block_limit_hit = GNUNET_YES;
292 GNUNET_break ( (0 == num_downloads_change) || (GNUNET_YES == block_limit_hit) );
293 GNUNET_break (0 == num_probes_change);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
298 h->max_parallel_requests,
300 num_downloads_change,
301 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
303 /* make sure we run again */
305 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
310 * Add a job to the queue.
312 * @param h handle to the overall FS state
313 * @param start function to call to begin the job
314 * @param stop function to call to pause the job, or on dequeue (if the job was running)
315 * @param cls closure for start and stop
316 * @param blocks number of blocks this jobs uses
317 * @param priority how important is this download
318 * @return queue handle
320 struct GNUNET_FS_QueueEntry *
321 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
322 GNUNET_FS_QueueStart start,
323 GNUNET_FS_QueueStop stop, void *cls,
325 enum GNUNET_FS_QueuePriority priority)
327 struct GNUNET_FS_QueueEntry *qe;
329 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
334 qe->queue_time = GNUNET_TIME_absolute_get ();
336 qe->priority = priority;
337 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
338 h->pending_tail, qe);
339 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
340 GNUNET_SCHEDULER_cancel (h->queue_job);
341 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 * Dequeue a job from the queue.
352 * @param qe handle for the job
355 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
357 struct GNUNET_FS_Handle *h;
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Dequeueing job %p\n",
363 if (NULL != qe->client)
365 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
367 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
368 GNUNET_SCHEDULER_cancel (h->queue_job);
369 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
374 * Create a top-level activity entry.
376 * @param h global fs handle
377 * @param ssf suspend signal function to use
378 * @param ssf_cls closure for @a ssf
379 * @return fresh top-level activity handle
381 struct TopLevelActivity *
382 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
383 SuspendSignalFunction ssf,
386 struct TopLevelActivity *ret;
388 ret = GNUNET_new (struct TopLevelActivity);
390 ret->ssf_cls = ssf_cls;
391 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
397 * Destroy a top-level activity entry.
399 * @param h global fs handle
400 * @param top top level activity entry
403 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
404 struct TopLevelActivity *top)
406 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
412 * Closure for #GNUNET_FS_data_reader_file_().
417 * Name of the file to read.
422 * File descriptor, NULL if it has not yet been opened.
424 struct GNUNET_DISK_FileHandle *fd;
429 * Function that provides data by reading from a file.
431 * @param cls closure with the `struct FileInfo *`
432 * @param offset offset to read from; it is possible
433 * that the caller might need to go backwards
434 * a bit at times; set to `UINT64_MAX` to tell
435 * the reader that we won't be reading for a while
436 * (used to close the file descriptor but NOT fully
437 * clean up the reader's state); in this case,
438 * a value of '0' for @a max should be ignored
439 * @param max maximum number of bytes that should be
440 * copied to @a buf; readers are not allowed
441 * to provide less data unless there is an error;
442 * a value of "0" will be used at the end to allow
443 * the reader to clean up its internal state
444 * @param buf where the reader should write the data
445 * @param emsg location for the reader to store an error message
446 * @return number of bytes written, usually @a max, 0 on error
449 GNUNET_FS_data_reader_file_ (void *cls,
455 struct FileInfo *fi = cls;
458 if (UINT64_MAX == offset)
462 GNUNET_DISK_file_close (fi->fd);
470 GNUNET_DISK_file_close (fi->fd);
471 GNUNET_free (fi->filename);
478 GNUNET_DISK_file_open (fi->filename,
479 GNUNET_DISK_OPEN_READ,
480 GNUNET_DISK_PERM_NONE);
483 GNUNET_asprintf (emsg,
484 _("Could not open file `%s': %s"),
490 if ( (GNUNET_SYSERR ==
491 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
492 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
494 GNUNET_asprintf (emsg,
495 _("Could not read file `%s': %s"),
502 GNUNET_asprintf (emsg,
503 _("Short read reading from file `%s'!"),
512 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
514 * @param filename file to read
515 * @return closure to use, NULL on error
518 GNUNET_FS_make_file_reader_context_ (const char *filename)
522 fi = GNUNET_new (struct FileInfo);
523 fi->filename = GNUNET_STRINGS_filename_expand (filename);
524 if (NULL == fi->filename)
534 * Function that provides data by copying from a buffer.
536 * @param cls closure (points to the buffer)
537 * @param offset offset to read from; it is possible
538 * that the caller might need to go backwards
539 * a bit at times; set to `UINT64_MAX` to tell
540 * the reader that we won't be reading for a while
541 * (used to close the file descriptor but NOT fully
542 * clean up the reader's state); in this case,
543 * a value of '0' for @a max should be ignored
544 * @param max maximum number of bytes that should be
545 * copied to @a buf; readers are not allowed
546 * to provide less data unless there is an error;
547 * a value of "0" will be used at the end to allow
548 * the reader to clean up its internal state
549 * @param buf where the reader should write the data
550 * @param emsg location for the reader to store an error message
551 * @return number of bytes written, usually @a max, 0 on error
554 GNUNET_FS_data_reader_copy_ (void *cls,
562 if (UINT64_MAX == offset)
566 GNUNET_free_non_null (data);
569 memcpy (buf, &data[offset], max);
575 * Return the full filename where we would store state information
576 * (for serialization/deserialization).
578 * @param h master context
579 * @param ext component of the path
580 * @param ent entity identifier (or emtpy string for the directory)
581 * @return NULL on error
584 get_serialization_file_name (struct GNUNET_FS_Handle *h,
591 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
592 return NULL; /* persistence not requested */
594 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
597 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
598 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
600 GNUNET_free (basename);
606 * Return the full filename where we would store state information
607 * (for serialization/deserialization) that is associated with a
610 * @param h master context
611 * @param ext component of the path
612 * @param uni name of the parent operation
613 * @param ent entity identifier (or emtpy string for the directory)
614 * @return NULL on error
617 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
625 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
626 return NULL; /* persistence not requested */
628 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
631 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
632 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
633 uni, DIR_SEPARATOR_STR, ent);
634 GNUNET_free (basename);
640 * Return a read handle for deserialization.
642 * @param h master context
643 * @param ext component of the path
644 * @param ent entity identifier (or emtpy string for the directory)
645 * @return NULL on error
647 static struct GNUNET_BIO_ReadHandle *
648 get_read_handle (struct GNUNET_FS_Handle *h,
653 struct GNUNET_BIO_ReadHandle *ret;
655 fn = get_serialization_file_name (h, ext, ent);
658 ret = GNUNET_BIO_read_open (fn);
665 * Return a write handle for serialization.
667 * @param h master context
668 * @param ext component of the path
669 * @param ent entity identifier (or emtpy string for the directory)
670 * @return NULL on error
672 static struct GNUNET_BIO_WriteHandle *
673 get_write_handle (struct GNUNET_FS_Handle *h,
678 struct GNUNET_BIO_WriteHandle *ret;
680 fn = get_serialization_file_name (h, ext, ent);
683 ret = GNUNET_BIO_write_open (fn);
684 GNUNET_break (NULL != ret);
691 * Return a write handle for serialization.
693 * @param h master context
694 * @param ext component of the path
695 * @param uni name of parent
696 * @param ent entity identifier (or emtpy string for the directory)
697 * @return NULL on error
699 static struct GNUNET_BIO_WriteHandle *
700 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
701 const char *uni, const char *ent)
704 struct GNUNET_BIO_WriteHandle *ret;
706 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
709 ret = GNUNET_BIO_write_open (fn);
716 * Remove serialization/deserialization file from disk.
718 * @param h master context
719 * @param ext component of the path
720 * @param ent entity identifier
723 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
729 if ((NULL == ent) || (0 == strlen (ent)))
734 filename = get_serialization_file_name (h, ext, ent);
735 if (NULL != filename)
737 if ( (0 != UNLINK (filename)) &&
739 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
740 GNUNET_free (filename);
746 * Remove serialization/deserialization file from disk.
748 * @param h master context
749 * @param ext component of the path
750 * @param uni parent name
751 * @param ent entity identifier
754 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
756 const char *uni, const char *ent)
760 if ((NULL == ent) || (0 == strlen (ent)))
765 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
766 if (NULL == filename)
768 if (0 != UNLINK (filename))
769 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
770 GNUNET_free (filename);
775 * Remove serialization/deserialization directory from disk.
777 * @param h master context
778 * @param ext component of the path
779 * @param uni unique name of parent
782 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
790 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
793 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
794 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
795 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
801 * Serialize a start-time. Since we use start-times to
802 * calculate the duration of some operation, we actually
803 * do not serialize the absolute time but the (relative)
804 * duration since the start time. When we then
805 * deserialize the start time, we take the current time and
806 * subtract that duration so that we get again an absolute
807 * time stamp that will result in correct performance
810 * @param wh handle for writing
811 * @param timestamp time to serialize
812 * @return #GNUNET_OK on success
815 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
816 struct GNUNET_TIME_Absolute timestamp)
818 struct GNUNET_TIME_Relative dur;
820 dur = GNUNET_TIME_absolute_get_duration (timestamp);
821 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
826 * Deserialize a start-time. Since we use start-times to
827 * calculate the duration of some operation, we actually
828 * do not serialize the absolute time but the (relative)
829 * duration since the start time. Thus, when we then
830 * deserialize the start time, we take the current time and
831 * subtract that duration so that we get again an absolute
832 * time stamp that will result in correct performance
835 * @param rh handle for reading
836 * @param timestamp where to write the deserialized timestamp
837 * @return #GNUNET_OK on success
840 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
841 struct GNUNET_TIME_Absolute *timestamp)
843 struct GNUNET_TIME_Relative dur;
845 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
846 return GNUNET_SYSERR;
847 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
853 * Using the given serialization filename, try to deserialize
854 * the file-information tree associated with it.
856 * @param h master context
857 * @param filename name of the file (without directory) with
859 * @return NULL on error
861 static struct GNUNET_FS_FileInformation *
862 deserialize_file_information (struct GNUNET_FS_Handle *h,
863 const char *filename);
867 * Using the given serialization filename, try to deserialize
868 * the file-information tree associated with it.
870 * @param h master context
871 * @param fn name of the file (without directory) with
873 * @param rh handle for reading
874 * @return NULL on error
876 static struct GNUNET_FS_FileInformation *
877 deserialize_fi_node (struct GNUNET_FS_Handle *h,
879 struct GNUNET_BIO_ReadHandle *rh)
881 struct GNUNET_FS_FileInformation *ret;
882 struct GNUNET_FS_FileInformation *nxt;
890 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
895 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
901 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
902 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
904 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
905 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
906 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
908 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
909 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
910 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
912 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
913 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
914 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
915 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
917 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
919 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
920 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
921 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
922 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
929 case 0: /* file-insert */
930 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
935 ret->is_directory = GNUNET_NO;
936 ret->data.file.do_index = GNUNET_NO;
937 ret->data.file.have_hash = GNUNET_NO;
938 ret->data.file.index_start_confirmed = GNUNET_NO;
939 if (GNUNET_NO == ret->is_published)
941 if (NULL == ret->filename)
943 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
944 ret->data.file.reader_cls =
945 GNUNET_malloc_large (ret->data.file.file_size);
946 if (ret->data.file.reader_cls == NULL)
949 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
950 ret->data.file.file_size))
958 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
959 ret->data.file.reader_cls =
960 GNUNET_FS_make_file_reader_context_ (ret->filename);
964 case 1: /* file-index, no hash */
965 if (NULL == ret->filename)
970 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
975 ret->is_directory = GNUNET_NO;
976 ret->data.file.do_index = GNUNET_YES;
977 ret->data.file.have_hash = GNUNET_NO;
978 ret->data.file.index_start_confirmed = GNUNET_NO;
979 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
980 ret->data.file.reader_cls =
981 GNUNET_FS_make_file_reader_context_ (ret->filename);
983 case 2: /* file-index-with-hash */
984 if (NULL == ret->filename)
989 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
991 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
992 sizeof (struct GNUNET_HashCode))))
997 ret->is_directory = GNUNET_NO;
998 ret->data.file.do_index = GNUNET_YES;
999 ret->data.file.have_hash = GNUNET_YES;
1000 ret->data.file.index_start_confirmed = GNUNET_NO;
1001 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1002 ret->data.file.reader_cls =
1003 GNUNET_FS_make_file_reader_context_ (ret->filename);
1005 case 3: /* file-index-with-hash-confirmed */
1006 if (NULL == ret->filename)
1011 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1013 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1014 sizeof (struct GNUNET_HashCode))))
1019 ret->is_directory = GNUNET_NO;
1020 ret->data.file.do_index = GNUNET_YES;
1021 ret->data.file.have_hash = GNUNET_YES;
1022 ret->data.file.index_start_confirmed = GNUNET_YES;
1023 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1024 ret->data.file.reader_cls =
1025 GNUNET_FS_make_file_reader_context_ (ret->filename);
1027 case 4: /* directory */
1028 ret->is_directory = GNUNET_YES;
1029 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1030 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1031 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1032 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1034 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1036 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1041 ret->data.dir.dir_size = (uint32_t) dsize;
1042 if (NULL != filename)
1044 ret->data.dir.entries = deserialize_file_information (h, filename);
1045 GNUNET_free (filename);
1047 nxt = ret->data.dir.entries;
1059 ret->serialization = GNUNET_strdup (fn);
1061 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1066 if (NULL != filename)
1068 ret->next = deserialize_file_information (h, filename);
1069 GNUNET_free (filename);
1072 GNUNET_free_non_null (ksks);
1073 GNUNET_free_non_null (skss);
1074 GNUNET_free_non_null (chks);
1077 GNUNET_free_non_null (ksks);
1078 GNUNET_free_non_null (chks);
1079 GNUNET_free_non_null (skss);
1080 GNUNET_free_non_null (filename);
1081 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1087 * Using the given serialization filename, try to deserialize
1088 * the file-information tree associated with it.
1090 * @param h master context
1091 * @param filename name of the file (without directory) with
1093 * @return NULL on error
1095 static struct GNUNET_FS_FileInformation *
1096 deserialize_file_information (struct GNUNET_FS_Handle *h,
1097 const char *filename)
1099 struct GNUNET_FS_FileInformation *ret;
1100 struct GNUNET_BIO_ReadHandle *rh;
1104 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1107 ret = deserialize_fi_node (h, filename, rh);
1108 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1110 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1111 _("Failed to resume publishing information `%s': %s\n"),
1117 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1120 if (0 != UNLINK (fn))
1121 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1130 * Given a serialization name (full absolute path), return the
1131 * basename of the file (without the path), which must only
1132 * consist of the 6 random characters.
1134 * @param fullname name to extract the basename from
1135 * @return copy of the basename, NULL on error
1138 get_serialization_short_name (const char *fullname)
1145 /* FIXME: we could do this faster since we know
1146 * the length of 'end'... */
1147 while ('\0' != *nxt)
1149 if (DIR_SEPARATOR == *nxt)
1153 if ((NULL == end) || (0 == strlen (end)))
1158 GNUNET_break (6 == strlen (end));
1159 return GNUNET_strdup (end);
1164 * Create a new random name for serialization. Also checks if persistence
1165 * is enabled and returns NULL if not.
1167 * @param h master context
1168 * @param ext component of the path
1169 * @return NULL on errror
1172 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1179 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1180 return NULL; /* persistence not requested */
1181 dn = get_serialization_file_name (h, ext, "");
1184 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1189 fn = GNUNET_DISK_mktemp (dn);
1192 return NULL; /* epic fail */
1193 ret = get_serialization_short_name (fn);
1200 * Create a new random name for serialization. Also checks if persistence
1201 * is enabled and returns NULL if not.
1203 * @param h master context
1204 * @param ext component of the path
1205 * @param uni name of parent
1206 * @return NULL on errror
1209 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1217 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1218 return NULL; /* persistence not requested */
1219 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1222 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1227 fn = GNUNET_DISK_mktemp (dn);
1230 return NULL; /* epic fail */
1231 ret = get_serialization_short_name (fn);
1238 * Copy all of the data from the reader to the write handle.
1240 * @param wh write handle
1241 * @param fi file with reader
1242 * @return #GNUNET_OK on success
1245 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1246 struct GNUNET_FS_FileInformation *fi)
1248 char buf[32 * 1024];
1256 while (off < fi->data.file.file_size)
1258 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1260 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1264 return GNUNET_SYSERR;
1266 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1267 return GNUNET_SYSERR;
1275 * Create a temporary file on disk to store the current
1276 * state of @a fi in.
1278 * @param fi file information to sync with disk
1281 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1284 struct GNUNET_BIO_WriteHandle *wh;
1290 if (NULL == fi->serialization)
1292 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1293 if (NULL == fi->serialization)
1295 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1299 GNUNET_free (fi->serialization);
1300 fi->serialization = NULL;
1303 if (GNUNET_YES == fi->is_directory)
1305 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1307 else if (GNUNET_YES == fi->data.file.have_hash)
1309 else if (GNUNET_YES == fi->data.file.do_index)
1313 if (NULL != fi->keywords)
1314 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1317 if (NULL != fi->chk_uri)
1318 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1321 if (NULL != fi->sks_uri)
1322 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1325 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1326 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1327 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1328 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1329 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1330 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1331 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1332 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1334 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1335 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1336 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1337 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1342 GNUNET_free_non_null (chks);
1344 GNUNET_free_non_null (ksks);
1346 GNUNET_free_non_null (skss);
1351 case 0: /* file-insert */
1352 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1357 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1358 if (GNUNET_OK != copy_from_reader (wh, fi))
1364 case 1: /* file-index, no hash */
1365 if (NULL == fi->filename)
1370 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1376 case 2: /* file-index-with-hash */
1377 case 3: /* file-index-with-hash-confirmed */
1378 if (NULL == fi->filename)
1383 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1385 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1386 sizeof (struct GNUNET_HashCode))))
1392 case 4: /* directory */
1393 if ( (NULL != fi->data.dir.entries) &&
1394 (NULL == fi->data.dir.entries->serialization) )
1395 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1396 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1397 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1398 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1400 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1401 (uint32_t) fi->data.dir.dir_size)) ||
1403 GNUNET_BIO_write_string (wh,
1404 (fi->data.dir.entries ==
1405 NULL) ? NULL : fi->data.dir.
1406 entries->serialization)))
1416 if ( (NULL != fi->next) &&
1417 (NULL == fi->next->serialization) )
1418 GNUNET_FS_file_information_sync_ (fi->next);
1420 GNUNET_BIO_write_string (wh,
1422 NULL) ? fi->next->serialization : NULL))
1427 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1436 (void) GNUNET_BIO_write_close (wh);
1437 GNUNET_free_non_null (chks);
1438 GNUNET_free_non_null (ksks);
1439 GNUNET_free_non_null (skss);
1440 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1444 if (0 != UNLINK (fn))
1445 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1448 GNUNET_free (fi->serialization);
1449 fi->serialization = NULL;
1455 * Find the entry in the file information struct where the
1456 * serialization filename matches the given name.
1458 * @param pos file information to search
1459 * @param srch filename to search for
1460 * @return NULL if srch was not found in this subtree
1462 static struct GNUNET_FS_FileInformation *
1463 find_file_position (struct GNUNET_FS_FileInformation *pos,
1466 struct GNUNET_FS_FileInformation *r;
1470 if (0 == strcmp (srch, pos->serialization))
1472 if ( (GNUNET_YES == pos->is_directory) &&
1473 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1482 * Signal the FS's progress function that we are resuming
1485 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1486 * @param fi the entry in the publish-structure
1487 * @param length length of the file or directory
1488 * @param meta metadata for the file or directory (can be modified)
1489 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1490 * @param bo block options (can be modified)
1491 * @param do_index should we index?
1492 * @param client_info pointer to client context set upon creation (can be modified)
1493 * @return #GNUNET_OK to continue (always)
1496 fip_signal_resume (void *cls,
1497 struct GNUNET_FS_FileInformation *fi,
1499 struct GNUNET_CONTAINER_MetaData *meta,
1500 struct GNUNET_FS_Uri **uri,
1501 struct GNUNET_FS_BlockOptions *bo,
1505 struct GNUNET_FS_PublishContext *pc = cls;
1506 struct GNUNET_FS_ProgressInfo pi;
1508 if (GNUNET_YES == pc->skip_next_fi_callback)
1510 pc->skip_next_fi_callback = GNUNET_NO;
1513 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1514 pi.value.publish.specifics.resume.message = fi->emsg;
1515 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1516 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1517 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1519 /* process entries in directory */
1520 pc->skip_next_fi_callback = GNUNET_YES;
1521 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1528 * Function called with a filename of serialized publishing operation
1531 * @param cls the `struct GNUNET_FS_Handle *`
1532 * @param filename complete filename (absolute path)
1533 * @return #GNUNET_OK (continue to iterate)
1536 deserialize_publish_file (void *cls,
1537 const char *filename)
1539 struct GNUNET_FS_Handle *h = cls;
1540 struct GNUNET_BIO_ReadHandle *rh;
1541 struct GNUNET_FS_PublishContext *pc;
1546 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1550 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1552 pc->serialization = get_serialization_short_name (filename);
1555 rh = GNUNET_BIO_read_open (filename);
1561 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1563 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1564 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1565 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1566 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1568 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1569 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1570 || ( (GNUNET_YES == have_ns) &&
1571 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1576 pc->options = options;
1577 pc->all_done = all_done;
1578 if (NULL == fi_root)
1583 pc->fi = deserialize_file_information (h, fi_root);
1589 if (GNUNET_YES == have_ns)
1591 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1594 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1595 (GNUNET_YES != pc->all_done))
1597 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1598 if (NULL == pc->dsh)
1603 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1604 GNUNET_free (fi_pos);
1606 if (NULL == pc->fi_pos)
1608 /* failed to find position for resuming, outch! Will start from root! */
1610 if (GNUNET_YES != pc->all_done)
1611 pc->fi_pos = pc->fi;
1614 GNUNET_free (fi_root);
1616 /* generate RESUME event(s) */
1617 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1619 /* re-start publishing (if needed)... */
1620 if (GNUNET_YES != pc->all_done)
1622 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1624 GNUNET_SCHEDULER_add_with_priority
1625 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1626 &GNUNET_FS_publish_main_, pc);
1628 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1631 _("Failure while resuming publishing operation `%s': %s\n"),
1635 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1638 GNUNET_free_non_null (pc->nid);
1639 GNUNET_free_non_null (pc->nuid);
1640 GNUNET_free_non_null (fi_root);
1641 GNUNET_free_non_null (fi_pos);
1642 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1644 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1645 _("Failed to resume publishing operation `%s': %s\n"), filename,
1650 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1651 if (0 != UNLINK (filename))
1652 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1653 GNUNET_free (pc->serialization);
1660 * Synchronize this publishing struct with its mirror
1661 * on disk. Note that all internal FS-operations that change
1662 * publishing structs should already call "sync" internally,
1663 * so this function is likely not useful for clients.
1665 * @param pc the struct to sync
1668 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1670 struct GNUNET_BIO_WriteHandle *wh;
1673 if (NULL == pc->serialization)
1675 make_serialization_file_name (pc->h,
1676 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1677 if (NULL == pc->serialization)
1681 if (NULL == pc->fi->serialization)
1686 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1693 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1694 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1695 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1696 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1697 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1698 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1699 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1701 GNUNET_BIO_write_string (wh,
1702 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1703 ( (NULL != pc->ns) &&
1704 (GNUNET_OK != GNUNET_BIO_write (wh,
1706 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1711 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1720 (void) GNUNET_BIO_write_close (wh);
1721 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1723 GNUNET_free (pc->serialization);
1724 pc->serialization = NULL;
1729 * Synchronize this unindex struct with its mirror
1730 * on disk. Note that all internal FS-operations that change
1731 * publishing structs should already call "sync" internally,
1732 * so this function is likely not useful for clients.
1734 * @param uc the struct to sync
1737 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1739 struct GNUNET_BIO_WriteHandle *wh;
1742 if (NULL == uc->serialization)
1744 make_serialization_file_name (uc->h,
1745 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1746 if (NULL == uc->serialization)
1748 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1755 if (NULL != uc->ksk_uri)
1756 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1759 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1760 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1761 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1762 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1764 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1765 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1766 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1767 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1769 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1770 ((uc->state == UNINDEX_STATE_ERROR) &&
1771 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1776 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1785 (void) GNUNET_BIO_write_close (wh);
1786 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1788 GNUNET_free (uc->serialization);
1789 uc->serialization = NULL;
1794 * Serialize a download request.
1796 * @param wh handle for writing the download request to disk
1797 * @param dr the the request to write to disk
1798 * @return #GNUNET_YES on success, #GNUNET_NO on error
1801 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1802 struct DownloadRequest *dr)
1806 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1807 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1808 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1809 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1811 if ((BRS_CHK_SET == dr->state) &&
1813 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1815 for (i = 0; i < dr->num_children; i++)
1816 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1823 * Read a download request tree.
1825 * @param rh cadet to read from
1826 * @return value the download request read from disk, NULL on error
1828 static struct DownloadRequest *
1829 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1831 struct DownloadRequest *dr;
1834 dr = GNUNET_new (struct DownloadRequest);
1835 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1836 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1837 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1838 (dr->num_children > CHK_PER_INODE) ||
1839 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1840 ( (0 == dr->depth) &&
1841 (dr->num_children > 0) ) ||
1842 ((dr->depth > 0) && (0 == dr->num_children)))
1845 dr->num_children = 0;
1848 if (dr->num_children > 0)
1850 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1854 case BRS_RECONSTRUCT_DOWN:
1855 case BRS_RECONSTRUCT_META_UP:
1856 case BRS_RECONSTRUCT_UP:
1860 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1863 case BRS_DOWNLOAD_DOWN:
1864 case BRS_DOWNLOAD_UP:
1871 for (i = 0; i < dr->num_children; i++)
1873 if (NULL == (dr->children[i] = read_download_request (rh)))
1875 dr->children[i]->parent = dr;
1879 GNUNET_FS_free_download_request_ (dr);
1885 * Compute the name of the sync file (or directory) for the given download
1888 * @param dc download context to compute for
1889 * @param uni unique filename to use, use "" for the directory name
1890 * @param ext extension to use, use ".dir" for our own subdirectory
1891 * @return the expanded file name, NULL for none
1894 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1901 if (dc->parent == NULL)
1902 return get_serialization_file_name (dc->h,
1903 (dc->search != NULL) ?
1904 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1905 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1907 if (NULL == dc->parent->serialization)
1909 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1912 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1919 * Synchronize this download struct with its mirror
1920 * on disk. Note that all internal FS-operations that change
1921 * publishing structs should already call "sync" internally,
1922 * so this function is likely not useful for clients.
1924 * @param dc the struct to sync
1927 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1929 struct GNUNET_BIO_WriteHandle *wh;
1934 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1935 return; /* we don't sync probes */
1936 if (NULL == dc->serialization)
1938 dir = get_download_sync_filename (dc, "", "");
1941 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1946 fn = GNUNET_DISK_mktemp (dir);
1950 dc->serialization = get_serialization_short_name (fn);
1954 fn = get_download_sync_filename (dc, dc->serialization, "");
1957 GNUNET_free (dc->serialization);
1958 dc->serialization = NULL;
1963 wh = GNUNET_BIO_write_open (fn);
1966 GNUNET_free (dc->serialization);
1967 dc->serialization = NULL;
1971 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1972 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1973 uris = GNUNET_FS_uri_to_string (dc->uri);
1974 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1975 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1976 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1977 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1978 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1979 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1980 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1981 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1982 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1983 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1984 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1985 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1986 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1991 if (NULL == dc->emsg)
1993 GNUNET_assert (dc->top_request != NULL);
1994 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2000 GNUNET_free_non_null (uris);
2002 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2012 (void) GNUNET_BIO_write_close (wh);
2013 GNUNET_free_non_null (uris);
2014 if (0 != UNLINK (fn))
2015 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2017 GNUNET_free (dc->serialization);
2018 dc->serialization = NULL;
2023 * Synchronize this search result with its mirror
2024 * on disk. Note that all internal FS-operations that change
2025 * publishing structs should already call "sync" internally,
2026 * so this function is likely not useful for clients.
2028 * @param sr the struct to sync
2031 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2033 struct GNUNET_BIO_WriteHandle *wh;
2039 if (NULL == sr->serialization)
2041 make_serialization_file_name_in_dir (sr->h,
2042 (sr->sc->psearch_result ==
2044 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2045 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2046 sr->sc->serialization);
2047 if (NULL == sr->serialization)
2049 wh = get_write_handle_in_dir (sr->h,
2050 (sr->sc->psearch_result ==
2051 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2052 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2053 sr->sc->serialization, sr->serialization);
2059 uris = GNUNET_FS_uri_to_string (sr->uri);
2060 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2062 GNUNET_BIO_write_string (wh,
2064 NULL ? sr->download->serialization : NULL)) ||
2066 GNUNET_BIO_write_string (wh,
2067 sr->update_search !=
2068 NULL ? sr->update_search->serialization : NULL))
2069 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2070 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2071 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2072 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2073 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2074 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2079 if ( (NULL != sr->uri) &&
2080 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2081 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2082 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2087 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2093 GNUNET_free_non_null (uris);
2096 GNUNET_free_non_null (uris);
2098 (void) GNUNET_BIO_write_close (wh);
2099 remove_sync_file_in_dir (sr->h,
2100 (NULL == sr->sc->psearch_result)
2101 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2102 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2103 sr->sc->serialization, sr->serialization);
2104 GNUNET_free (sr->serialization);
2105 sr->serialization = NULL;
2110 * Synchronize this search struct with its mirror
2111 * on disk. Note that all internal FS-operations that change
2112 * publishing structs should already call "sync" internally,
2113 * so this function is likely not useful for clients.
2115 * @param sc the struct to sync
2118 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2120 struct GNUNET_BIO_WriteHandle *wh;
2123 const char *category;
2126 (NULL == sc->psearch_result)
2127 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2128 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2129 if (NULL == sc->serialization)
2130 sc->serialization = make_serialization_file_name (sc->h, category);
2131 if (NULL == sc->serialization)
2134 wh = get_write_handle (sc->h, category, sc->serialization);
2140 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2141 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2142 uris = GNUNET_FS_uri_to_string (sc->uri);
2143 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2144 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2145 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2146 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2147 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2148 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2149 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2156 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2165 (void) GNUNET_BIO_write_close (wh);
2166 GNUNET_free_non_null (uris);
2167 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2168 GNUNET_free (sc->serialization);
2169 sc->serialization = NULL;
2174 * Function called with a filename of serialized unindexing operation
2177 * @param cls the `struct GNUNET_FS_Handle *`
2178 * @param filename complete filename (absolute path)
2179 * @return #GNUNET_OK (continue to iterate)
2182 deserialize_unindex_file (void *cls,
2183 const char *filename)
2185 struct GNUNET_FS_Handle *h = cls;
2186 struct GNUNET_BIO_ReadHandle *rh;
2187 struct GNUNET_FS_UnindexContext *uc;
2188 struct GNUNET_FS_ProgressInfo pi;
2193 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2195 uc->serialization = get_serialization_short_name (filename);
2196 rh = GNUNET_BIO_read_open (filename);
2204 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2205 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2206 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2207 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2208 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2209 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2210 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2212 GNUNET_free_non_null (uris);
2218 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2220 if (NULL == uc->ksk_uri)
2223 GNUNET_free_non_null (emsg);
2227 if ( (uc->ksk_offset > 0) &&
2228 ( (NULL == uc->ksk_uri) ||
2229 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2234 uc->state = (enum UnindexState) state;
2237 case UNINDEX_STATE_HASHING:
2239 case UNINDEX_STATE_FS_NOTIFY:
2241 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2242 sizeof (struct GNUNET_HashCode)))
2248 case UNINDEX_STATE_DS_REMOVE:
2249 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2250 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2252 case UNINDEX_STATE_COMPLETE:
2254 case UNINDEX_STATE_ERROR:
2256 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2266 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2267 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2268 pi.value.unindex.specifics.resume.message = uc->emsg;
2269 GNUNET_FS_unindex_make_status_ (&pi, uc,
2271 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2274 case UNINDEX_STATE_HASHING:
2276 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2278 &GNUNET_FS_unindex_process_hash_, uc);
2280 case UNINDEX_STATE_FS_NOTIFY:
2281 uc->state = UNINDEX_STATE_HASHING;
2282 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2284 case UNINDEX_STATE_DS_REMOVE:
2285 GNUNET_FS_unindex_do_remove_ (uc);
2287 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2288 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2290 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2291 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2293 case UNINDEX_STATE_COMPLETE:
2294 case UNINDEX_STATE_ERROR:
2295 /* no need to resume any operation, we were done */
2300 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2303 _("Failure while resuming unindexing operation `%s': %s\n"),
2309 GNUNET_free_non_null (uc->filename);
2310 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2313 _("Failed to resume unindexing operation `%s': %s\n"),
2318 if (NULL != uc->serialization)
2319 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2321 GNUNET_free_non_null (uc->serialization);
2328 * Deserialize a download.
2330 * @param h overall context
2331 * @param rh file to deserialize from
2332 * @param parent parent download
2333 * @param search associated search
2334 * @param serialization name under which the search was serialized
2337 deserialize_download (struct GNUNET_FS_Handle *h,
2338 struct GNUNET_BIO_ReadHandle *rh,
2339 struct GNUNET_FS_DownloadContext *parent,
2340 struct GNUNET_FS_SearchResult *search,
2341 const char *serialization);
2345 * Deserialize a search.
2347 * @param h overall context
2348 * @param rh file to deserialize from
2349 * @param psearch_result parent search result
2350 * @param serialization name under which the search was serialized
2352 static struct GNUNET_FS_SearchContext *
2353 deserialize_search (struct GNUNET_FS_Handle *h,
2354 struct GNUNET_BIO_ReadHandle *rh,
2355 struct GNUNET_FS_SearchResult *psearch_result,
2356 const char *serialization);
2360 * Function called with a filename of serialized search result
2363 * @param cls the `struct GNUNET_FS_SearchContext *`
2364 * @param filename complete filename (absolute path)
2365 * @return #GNUNET_OK (continue to iterate)
2368 deserialize_search_result (void *cls,
2369 const char *filename)
2371 struct GNUNET_FS_SearchContext *sc = cls;
2377 struct GNUNET_BIO_ReadHandle *rh;
2378 struct GNUNET_BIO_ReadHandle *drh;
2379 struct GNUNET_FS_SearchResult *sr;
2381 ser = get_serialization_short_name (filename);
2382 rh = GNUNET_BIO_read_open (filename);
2387 remove_sync_file_in_dir (sc->h,
2388 (NULL == sc->psearch_result)
2389 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2390 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2391 sc->serialization, ser);
2400 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2403 sr->serialization = ser;
2404 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2405 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2406 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2408 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2409 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2411 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2412 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2413 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2414 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2415 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2420 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2422 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2423 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2425 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2432 if (NULL != download)
2434 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2437 deserialize_download (sc->h, drh, NULL, sr, download);
2438 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2441 _("Failed to resume sub-download `%s': %s\n"),
2447 GNUNET_free (download);
2449 if (NULL != update_srch)
2452 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2455 deserialize_search (sc->h, drh, sr, update_srch);
2456 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2459 _("Failed to resume sub-search `%s': %s\n"),
2465 GNUNET_free (update_srch);
2467 GNUNET_break (GNUNET_YES ==
2468 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2470 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2471 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2474 _("Failure while resuming search operation `%s': %s\n"),
2480 GNUNET_free_non_null (download);
2481 GNUNET_free_non_null (emsg);
2482 GNUNET_free_non_null (uris);
2483 GNUNET_free_non_null (update_srch);
2484 if (NULL != sr->uri)
2485 GNUNET_FS_uri_destroy (sr->uri);
2486 if (NULL != sr->meta)
2487 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2488 GNUNET_free (sr->serialization);
2490 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2493 _("Failure while resuming search operation `%s': %s\n"),
2502 * Send the 'resume' signal to the callback; also actually
2503 * resume the download (put it in the queue). Does this
2504 * recursively for the top-level download and all child
2507 * @param dc download to resume
2510 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2512 struct GNUNET_FS_DownloadContext *dcc;
2513 struct GNUNET_FS_ProgressInfo pi;
2515 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2516 pi.value.download.specifics.resume.meta = dc->meta;
2517 pi.value.download.specifics.resume.message = dc->emsg;
2518 GNUNET_FS_download_make_status_ (&pi, dc);
2519 dcc = dc->child_head;
2522 signal_download_resume (dcc);
2525 if (NULL != dc->pending_head)
2526 GNUNET_FS_download_start_downloading_ (dc);
2531 * Signal resuming of a search to our clients (for the
2532 * top level search and all sub-searches).
2534 * @param sc search being resumed
2537 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2541 * Iterator over search results signaling resume to the client for
2544 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2545 * @param key current key code
2546 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2547 * @return #GNUNET_YES (we should continue to iterate)
2550 signal_result_resume (void *cls,
2551 const struct GNUNET_HashCode *key,
2554 struct GNUNET_FS_SearchContext *sc = cls;
2555 struct GNUNET_FS_ProgressInfo pi;
2556 struct GNUNET_FS_SearchResult *sr = value;
2558 if (0 == sr->mandatory_missing)
2560 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2561 pi.value.search.specifics.resume_result.meta = sr->meta;
2562 pi.value.search.specifics.resume_result.uri = sr->uri;
2563 pi.value.search.specifics.resume_result.result = sr;
2564 pi.value.search.specifics.resume_result.availability_rank =
2565 2 * sr->availability_success - sr->availability_trials;
2566 pi.value.search.specifics.resume_result.availability_certainty =
2567 sr->availability_trials;
2568 pi.value.search.specifics.resume_result.applicability_rank =
2569 sr->optional_support;
2570 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2572 if (NULL != sr->download)
2574 signal_download_resume (sr->download);
2578 GNUNET_FS_search_start_probe_ (sr);
2580 if (NULL != sr->update_search)
2581 signal_search_resume (sr->update_search);
2587 * Free memory allocated by the search context and its children
2589 * @param sc search context to free
2592 free_search_context (struct GNUNET_FS_SearchContext *sc);
2596 * Iterator over search results freeing each.
2598 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2599 * @param key current key code
2600 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2601 * @return #GNUNET_YES (we should continue to iterate)
2604 free_result (void *cls,
2605 const struct GNUNET_HashCode *key,
2608 struct GNUNET_FS_SearchResult *sr = value;
2610 if (NULL != sr->update_search)
2612 free_search_context (sr->update_search);
2613 GNUNET_assert (NULL == sr->update_search);
2615 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2616 GNUNET_FS_uri_destroy (sr->uri);
2623 * Free memory allocated by the search context and its children
2625 * @param sc search context to free
2628 free_search_context (struct GNUNET_FS_SearchContext *sc)
2630 if (NULL != sc->serialization)
2632 GNUNET_FS_remove_sync_file_ (sc->h,
2633 (sc->psearch_result ==
2634 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2635 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2637 GNUNET_FS_remove_sync_dir_ (sc->h,
2638 (sc->psearch_result ==
2639 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2640 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2643 GNUNET_free_non_null (sc->serialization);
2644 GNUNET_free_non_null (sc->emsg);
2645 if (NULL != sc->uri)
2646 GNUNET_FS_uri_destroy (sc->uri);
2647 if (NULL != sc->master_result_map)
2649 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2651 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2658 * Function called with a filename of serialized sub-download
2661 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2662 * @param filename complete filename (absolute path)
2663 * @return #GNUNET_OK (continue to iterate)
2666 deserialize_subdownload (void *cls,
2667 const char *filename)
2669 struct GNUNET_FS_DownloadContext *parent = cls;
2672 struct GNUNET_BIO_ReadHandle *rh;
2674 ser = get_serialization_short_name (filename);
2675 rh = GNUNET_BIO_read_open (filename);
2678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2679 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2685 deserialize_download (parent->h, rh, parent, NULL, ser);
2686 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2689 _("Failed to resume sub-download `%s': %s\n"),
2700 * Free this download context and all of its descendants.
2701 * (only works during deserialization since not all possible
2702 * state it taken care of).
2704 * @param dc context to free
2707 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2709 struct GNUNET_FS_DownloadContext *dcc;
2711 if (NULL != dc->meta)
2712 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2713 if (NULL != dc->uri)
2714 GNUNET_FS_uri_destroy (dc->uri);
2715 GNUNET_free_non_null (dc->temp_filename);
2716 GNUNET_free_non_null (dc->emsg);
2717 GNUNET_free_non_null (dc->filename);
2718 GNUNET_free_non_null (dc->serialization);
2719 while (NULL != (dcc = dc->child_head))
2721 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2724 free_download_context (dcc);
2726 GNUNET_FS_free_download_request_ (dc->top_request);
2727 if (NULL != dc->active)
2728 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2734 * Deserialize a download.
2736 * @param h overall context
2737 * @param rh file to deserialize from
2738 * @param parent parent download
2739 * @param search associated search
2740 * @param serialization name under which the search was serialized
2743 deserialize_download (struct GNUNET_FS_Handle *h,
2744 struct GNUNET_BIO_ReadHandle *rh,
2745 struct GNUNET_FS_DownloadContext *parent,
2746 struct GNUNET_FS_SearchResult *search,
2747 const char *serialization)
2749 struct GNUNET_FS_DownloadContext *dc;
2758 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2759 dc->parent = parent;
2761 dc->serialization = GNUNET_strdup (serialization);
2763 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2764 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2765 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2766 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2767 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2769 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2771 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2773 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2775 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2776 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2777 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2778 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2779 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2780 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2781 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2782 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2787 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2789 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2790 dc->has_finished = (int) status;
2792 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2793 if (GNUNET_FS_uri_test_loc (dc->uri))
2794 GNUNET_assert (GNUNET_OK ==
2795 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2796 if (NULL == dc->emsg)
2798 dc->top_request = read_download_request (rh);
2799 if (NULL == dc->top_request)
2805 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2808 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2809 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2814 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2818 dc->search = search;
2819 search->download = dc;
2821 if ((NULL == parent) && (NULL == search))
2824 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2825 signal_download_resume (dc);
2828 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2831 GNUNET_free_non_null (uris);
2832 GNUNET_free_non_null (emsg);
2833 free_download_context (dc);
2838 * Signal resuming of a search to our clients (for the
2839 * top level search and all sub-searches).
2841 * @param sc search being resumed
2844 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2846 struct GNUNET_FS_ProgressInfo pi;
2848 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2849 pi.value.search.specifics.resume.message = sc->emsg;
2850 pi.value.search.specifics.resume.is_paused =
2851 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2852 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2853 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2854 &signal_result_resume, sc);
2860 * Deserialize a search.
2862 * @param h overall context
2863 * @param rh file to deserialize from
2864 * @param psearch_result parent search result
2865 * @param serialization name under which the search was serialized
2867 static struct GNUNET_FS_SearchContext *
2868 deserialize_search (struct GNUNET_FS_Handle *h,
2869 struct GNUNET_BIO_ReadHandle *rh,
2870 struct GNUNET_FS_SearchResult *psearch_result,
2871 const char *serialization)
2873 struct GNUNET_FS_SearchContext *sc;
2880 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2887 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2888 if (NULL != psearch_result)
2890 sc->psearch_result = psearch_result;
2891 psearch_result->update_search = sc;
2894 sc->serialization = GNUNET_strdup (serialization);
2896 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2897 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2898 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2899 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2900 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2902 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2903 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2905 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2906 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2911 sc->options = (enum GNUNET_FS_SearchOptions) options;
2912 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2913 dn = get_serialization_file_name_in_dir (h,
2914 (NULL == sc->psearch_result) ?
2915 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2916 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2917 sc->serialization, "");
2920 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2921 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2924 if (('\0' == in_pause) &&
2925 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2928 _("Could not resume running search, will resume as paused search\n"));
2930 signal_search_resume (sc);
2934 GNUNET_free_non_null (emsg);
2935 free_search_context (sc);
2936 GNUNET_free_non_null (uris);
2942 * Function called with a filename of serialized search operation
2945 * @param cls the `struct GNUNET_FS_Handle *`
2946 * @param filename complete filename (absolute path)
2947 * @return #GNUNET_OK (continue to iterate)
2950 deserialize_search_file (void *cls,
2951 const char *filename)
2953 struct GNUNET_FS_Handle *h = cls;
2956 struct GNUNET_BIO_ReadHandle *rh;
2957 struct GNUNET_FS_SearchContext *sc;
2960 if (0 != STAT (filename, &buf))
2962 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2965 if (S_ISDIR (buf.st_mode))
2966 return GNUNET_OK; /* skip directories */
2967 ser = get_serialization_short_name (filename);
2968 rh = GNUNET_BIO_read_open (filename);
2973 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2978 sc = deserialize_search (h, rh, NULL, ser);
2980 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2982 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2984 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2985 _("Failure while resuming search operation `%s': %s\n"),
2994 * Function called with a filename of serialized download operation
2997 * @param cls the `struct GNUNET_FS_Handle *`
2998 * @param filename complete filename (absolute path)
2999 * @return #GNUNET_OK (continue to iterate)
3002 deserialize_download_file (void *cls, const char *filename)
3004 struct GNUNET_FS_Handle *h = cls;
3007 struct GNUNET_BIO_ReadHandle *rh;
3009 ser = get_serialization_short_name (filename);
3010 rh = GNUNET_BIO_read_open (filename);
3013 if (0 != UNLINK (filename))
3014 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3018 deserialize_download (h, rh, NULL, NULL, ser);
3020 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3023 _("Failure while resuming download operation `%s': %s\n"),
3032 * Deserialize informatin about pending operations.
3034 * @param master_path which master directory should be scanned
3035 * @param proc function to call for each entry (will get 'h' for 'cls')
3036 * @param h the 'struct GNUNET_FS_Handle*'
3039 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
3040 struct GNUNET_FS_Handle *h)
3044 dn = get_serialization_file_name (h, master_path, "");
3047 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3048 GNUNET_DISK_directory_scan (dn, proc, h);
3054 * Setup a connection to the file-sharing service.
3056 * @param cfg configuration to use
3057 * @param client_name unique identifier for this client
3058 * @param upcb function to call to notify about FS actions
3059 * @param upcb_cls closure for @a upcb
3060 * @param flags specific attributes for fs-operations
3061 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3062 * @return NULL on error
3064 struct GNUNET_FS_Handle *
3065 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3066 const char *client_name,
3067 GNUNET_FS_ProgressCallback upcb,
3069 enum GNUNET_FS_Flags flags, ...)
3071 struct GNUNET_FS_Handle *ret;
3072 enum GNUNET_FS_OPTIONS opt;
3075 ret = GNUNET_new (struct GNUNET_FS_Handle);
3077 ret->client_name = GNUNET_strdup (client_name);
3079 ret->upcb_cls = upcb_cls;
3081 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3082 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3083 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3084 va_start (ap, flags);
3085 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3089 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3090 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3093 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3094 ret->max_parallel_requests = va_arg (ap, unsigned int);
3099 GNUNET_free (ret->client_name);
3106 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3108 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3109 &deserialize_publish_file, ret);
3110 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3111 &deserialize_search_file, ret);
3112 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3113 &deserialize_download_file, ret);
3114 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3115 &deserialize_unindex_file, ret);
3122 * Close our connection with the file-sharing service.
3123 * The callback given to GNUNET_FS_start will no longer be
3124 * called after this function returns.
3126 * @param h handle that was returned from #GNUNET_FS_start()
3129 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3131 while (h->top_head != NULL)
3132 h->top_head->ssf (h->top_head->ssf_cls);
3133 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3134 GNUNET_SCHEDULER_cancel (h->queue_job);
3135 GNUNET_free (h->client_name);
3140 /* end of fs_api.c */