2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 qe->active = GNUNET_YES;
55 qe->h->active_blocks += qe->blocks;
56 qe->h->active_downloads++;
57 qe->start_time = GNUNET_TIME_absolute_get ();
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59 "Starting job %p (%u active)\n",
61 qe->h->active_downloads);
62 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head, qe->h->pending_tail, qe);
63 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
71 * Stop the given job (send signal, remove from active queue, update
72 * counters and state).
74 * @param qe job to stop
77 stop_job (struct GNUNET_FS_QueueEntry *qe)
79 qe->active = GNUNET_NO;
81 GNUNET_assert (0 < qe->h->active_downloads);
82 qe->h->active_downloads--;
83 qe->h->active_blocks -= qe->blocks;
84 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
85 GNUNET_TIME_absolute_get_duration (
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "Stopping job %p (%u active)\n",
90 qe->h->active_downloads);
91 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
92 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
100 * Process the jobs in the job queue, possibly starting some
101 * and stopping others.
103 * @param cls the `struct GNUNET_FS_Handle *`
106 process_job_queue (void *cls)
108 struct GNUNET_FS_Handle *h = cls;
109 struct GNUNET_FS_QueueEntry *qe;
110 struct GNUNET_FS_QueueEntry *next;
111 struct GNUNET_TIME_Relative run_time;
112 struct GNUNET_TIME_Relative restart_at;
113 struct GNUNET_TIME_Relative rst;
114 struct GNUNET_TIME_Absolute end_time;
115 unsigned int num_downloads_waiting;
116 unsigned int num_downloads_active;
117 unsigned int num_downloads_expired;
118 unsigned int num_probes_active;
119 unsigned int num_probes_waiting;
120 unsigned int num_probes_expired;
121 int num_probes_change;
122 int num_downloads_change;
126 /* restart_at will be set to the time when it makes sense to
127 re-evaluate the job queue (unless, of course, jobs complete
128 or are added, then we'll be triggered immediately */
129 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
130 /* first, calculate some basic statistics on pending jobs */
131 num_probes_waiting = 0;
132 num_downloads_waiting = 0;
133 for (qe = h->pending_head; NULL != qe; qe = qe->next)
135 switch (qe->priority)
137 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
138 num_probes_waiting++;
141 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
142 num_downloads_waiting++;
150 /* now, calculate some basic statistics on running jobs */
151 num_probes_active = 0;
152 num_probes_expired = 0;
153 num_downloads_active = 0;
154 num_downloads_expired = 0;
155 next = h->running_head;
156 while (NULL != (qe = next))
159 switch (qe->priority)
161 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
162 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
163 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
164 rst = GNUNET_TIME_absolute_get_remaining (end_time);
165 if (0 == rst.rel_value_us)
167 num_probes_expired++;
173 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
177 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
179 GNUNET_TIME_relative_saturating_multiply (h->avg_block_latency,
180 qe->blocks * qe->start_times);
181 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
182 rst = GNUNET_TIME_absolute_get_remaining (end_time);
183 if (0 == rst.rel_value_us)
185 num_downloads_expired++;
190 num_downloads_active++;
191 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
200 GNUNET_break (h->active_downloads ==
201 num_downloads_active + num_probes_active);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
207 num_downloads_active,
208 num_downloads_expired,
209 num_downloads_waiting);
210 GNUNET_break (h->active_downloads + num_probes_active <=
211 h->max_parallel_downloads);
212 /* calculate start/stop decisions */
213 if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
215 /* stop as many probes as there are downloads and probes */
216 num_probes_change = -GNUNET_MIN (num_probes_active, num_downloads_waiting);
217 /* start as many downloads as there are free slots, including those
219 num_downloads_change =
220 h->max_parallel_downloads - h->active_downloads - num_probes_change;
224 /* start all downloads (we can) */
225 num_downloads_change = num_downloads_waiting;
226 /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
227 if (1 + h->max_parallel_downloads / 4 >=
228 (h->active_downloads + num_downloads_change))
230 GNUNET_MIN (num_probes_waiting,
231 (1 + h->max_parallel_downloads / 4)
232 - (h->active_downloads + num_downloads_change));
234 num_probes_change = 0;
236 GNUNET_break (num_downloads_change <= num_downloads_waiting);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Changing %d probes and %d/%u/%u downloads\n",
240 num_downloads_change,
241 (unsigned int) h->active_downloads,
242 (unsigned int) h->max_parallel_downloads);
243 /* actually stop probes */
244 next = h->running_head;
245 while (NULL != (qe = next))
248 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
250 if (num_probes_change < 0)
254 if (0 == num_probes_change)
258 GNUNET_break (0 <= num_probes_change);
260 /* start some more tasks if we now have empty slots */
261 block_limit_hit = GNUNET_NO;
262 next = h->pending_head;
263 while ((NULL != (qe = next)) &&
264 ((num_probes_change > 0) || (num_downloads_change > 0)))
267 switch (qe->priority)
269 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
270 if (num_probes_change > 0)
274 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
275 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
279 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
280 if ((num_downloads_change > 0) &&
281 ((qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
282 ((qe->blocks > h->max_parallel_requests) &&
283 (0 == h->active_downloads))))
286 num_downloads_change--;
288 else if (num_downloads_change > 0)
289 block_limit_hit = GNUNET_YES;
297 GNUNET_break ((0 == num_downloads_change) || (GNUNET_YES == block_limit_hit));
298 GNUNET_break (0 == num_probes_change);
301 GNUNET_ERROR_TYPE_DEBUG,
302 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
304 h->max_parallel_requests,
306 num_downloads_change,
307 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
309 /* make sure we run again, callbacks might have
310 already re-scheduled the job, so cancel such
311 an operation (if it exists) */
312 if (NULL != h->queue_job)
313 GNUNET_SCHEDULER_cancel (h->queue_job);
315 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
320 * Add a job to the queue.
322 * @param h handle to the overall FS state
323 * @param start function to call to begin the job
324 * @param stop function to call to pause the job, or on dequeue (if the job was running)
325 * @param cls closure for start and stop
326 * @param blocks number of blocks this jobs uses
327 * @param priority how important is this download
328 * @return queue handle
330 struct GNUNET_FS_QueueEntry *
331 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
332 GNUNET_SCHEDULER_TaskCallback start,
333 GNUNET_SCHEDULER_TaskCallback stop,
336 enum GNUNET_FS_QueuePriority priority)
338 struct GNUNET_FS_QueueEntry *qe;
340 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
345 qe->queue_time = GNUNET_TIME_absolute_get ();
347 qe->priority = priority;
348 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
352 if (NULL != h->queue_job)
353 GNUNET_SCHEDULER_cancel (h->queue_job);
354 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing job %p\n", qe);
361 * Dequeue a job from the queue.
363 * @param qe handle for the job
366 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
368 struct GNUNET_FS_Handle *h;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dequeueing job %p\n", qe);
372 if (GNUNET_YES == qe->active)
374 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
376 if (NULL != h->queue_job)
377 GNUNET_SCHEDULER_cancel (h->queue_job);
378 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
383 * Create a top-level activity entry.
385 * @param h global fs handle
386 * @param ssf suspend signal function to use
387 * @param ssf_cls closure for @a ssf
388 * @return fresh top-level activity handle
390 struct TopLevelActivity *
391 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
392 SuspendSignalFunction ssf,
395 struct TopLevelActivity *ret;
397 ret = GNUNET_new (struct TopLevelActivity);
399 ret->ssf_cls = ssf_cls;
400 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
406 * Destroy a top-level activity entry.
408 * @param h global fs handle
409 * @param top top level activity entry
412 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top)
414 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
420 * Closure for #GNUNET_FS_data_reader_file_().
425 * Name of the file to read.
430 * File descriptor, NULL if it has not yet been opened.
432 struct GNUNET_DISK_FileHandle *fd;
437 * Function that provides data by reading from a file.
439 * @param cls closure with the `struct FileInfo *`
440 * @param offset offset to read from; it is possible
441 * that the caller might need to go backwards
442 * a bit at times; set to `UINT64_MAX` to tell
443 * the reader that we won't be reading for a while
444 * (used to close the file descriptor but NOT fully
445 * clean up the reader's state); in this case,
446 * a value of '0' for @a max should be ignored
447 * @param max maximum number of bytes that should be
448 * copied to @a buf; readers are not allowed
449 * to provide less data unless there is an error;
450 * a value of "0" will be used at the end to allow
451 * the reader to clean up its internal state
452 * @param buf where the reader should write the data
453 * @param emsg location for the reader to store an error message
454 * @return number of bytes written, usually @a max, 0 on error
457 GNUNET_FS_data_reader_file_ (void *cls,
463 struct FileInfo *fi = cls;
466 if (UINT64_MAX == offset)
470 GNUNET_DISK_file_close (fi->fd);
478 GNUNET_DISK_file_close (fi->fd);
479 GNUNET_free (fi->filename);
485 fi->fd = GNUNET_DISK_file_open (fi->filename,
486 GNUNET_DISK_OPEN_READ,
487 GNUNET_DISK_PERM_NONE);
490 GNUNET_asprintf (emsg,
491 _ ("Could not open file `%s': %s"),
497 if ((GNUNET_SYSERR ==
498 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
499 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))))
501 GNUNET_asprintf (emsg,
502 _ ("Could not read file `%s': %s"),
509 GNUNET_asprintf (emsg,
510 _ ("Short read reading from file `%s'!"),
519 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
521 * @param filename file to read
522 * @return closure to use, NULL on error
525 GNUNET_FS_make_file_reader_context_ (const char *filename)
529 fi = GNUNET_new (struct FileInfo);
530 fi->filename = GNUNET_STRINGS_filename_expand (filename);
531 if (NULL == fi->filename)
541 * Function that provides data by copying from a buffer.
543 * @param cls closure (points to the buffer)
544 * @param offset offset to read from; it is possible
545 * that the caller might need to go backwards
546 * a bit at times; set to `UINT64_MAX` to tell
547 * the reader that we won't be reading for a while
548 * (used to close the file descriptor but NOT fully
549 * clean up the reader's state); in this case,
550 * a value of '0' for @a max should be ignored
551 * @param max maximum number of bytes that should be
552 * copied to @a buf; readers are not allowed
553 * to provide less data unless there is an error;
554 * a value of "0" will be used at the end to allow
555 * the reader to clean up its internal state
556 * @param buf where the reader should write the data
557 * @param emsg location for the reader to store an error message
558 * @return number of bytes written, usually @a max, 0 on error
561 GNUNET_FS_data_reader_copy_ (void *cls,
569 if (UINT64_MAX == offset)
573 GNUNET_free_non_null (data);
576 GNUNET_memcpy (buf, &data[offset], max);
582 * Return the full filename where we would store state information
583 * (for serialization/deserialization).
585 * @param h master context
586 * @param ext component of the path
587 * @param ent entity identifier (or emtpy string for the directory)
588 * @return NULL on error
591 get_serialization_file_name (struct GNUNET_FS_Handle *h,
598 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
599 return NULL; /* persistence not requested */
600 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (h->cfg,
605 GNUNET_asprintf (&ret,
614 GNUNET_free (basename);
620 * Return the full filename where we would store state information
621 * (for serialization/deserialization) that is associated with a
624 * @param h master context
625 * @param ext component of the path
626 * @param uni name of the parent operation
627 * @param ent entity identifier (or emtpy string for the directory)
628 * @return NULL on error
631 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
639 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
640 return NULL; /* persistence not requested */
641 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (h->cfg,
646 GNUNET_asprintf (&ret,
647 "%s%s%s%s%s%s%s.dir%s%s",
657 GNUNET_free (basename);
663 * Return a read handle for deserialization.
665 * @param h master context
666 * @param ext component of the path
667 * @param ent entity identifier (or emtpy string for the directory)
668 * @return NULL on error
670 static struct GNUNET_BIO_ReadHandle *
671 get_read_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
674 struct GNUNET_BIO_ReadHandle *ret;
676 fn = get_serialization_file_name (h, ext, ent);
679 ret = GNUNET_BIO_read_open (fn);
686 * Return a write handle for serialization.
688 * @param h master context
689 * @param ext component of the path
690 * @param ent entity identifier (or emtpy string for the directory)
691 * @return NULL on error
693 static struct GNUNET_BIO_WriteHandle *
694 get_write_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
697 struct GNUNET_BIO_WriteHandle *ret;
699 fn = get_serialization_file_name (h, ext, ent);
702 ret = GNUNET_BIO_write_open (fn);
703 GNUNET_break (NULL != ret);
710 * Return a write handle for serialization.
712 * @param h master context
713 * @param ext component of the path
714 * @param uni name of parent
715 * @param ent entity identifier (or emtpy string for the directory)
716 * @return NULL on error
718 static struct GNUNET_BIO_WriteHandle *
719 get_write_handle_in_dir (struct GNUNET_FS_Handle *h,
725 struct GNUNET_BIO_WriteHandle *ret;
727 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
730 ret = GNUNET_BIO_write_open (fn);
737 * Remove serialization/deserialization file from disk.
739 * @param h master context
740 * @param ext component of the path
741 * @param ent entity identifier
744 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
750 if ((NULL == ent) || (0 == strlen (ent)))
755 filename = get_serialization_file_name (h, ext, ent);
756 if (NULL != filename)
758 if ((0 != unlink (filename)) && (ENOENT != errno))
759 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
760 GNUNET_free (filename);
766 * Remove serialization/deserialization file from disk.
768 * @param h master context
769 * @param ext component of the path
770 * @param uni parent name
771 * @param ent entity identifier
774 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
781 if ((NULL == ent) || (0 == strlen (ent)))
786 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
787 if (NULL == filename)
789 if (0 != unlink (filename))
790 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
791 GNUNET_free (filename);
796 * Remove serialization/deserialization directory from disk.
798 * @param h master context
799 * @param ext component of the path
800 * @param uni unique name of parent
803 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
811 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
814 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
815 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
816 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
822 * Serialize a start-time. Since we use start-times to
823 * calculate the duration of some operation, we actually
824 * do not serialize the absolute time but the (relative)
825 * duration since the start time. When we then
826 * deserialize the start time, we take the current time and
827 * subtract that duration so that we get again an absolute
828 * time stamp that will result in correct performance
831 * @param wh handle for writing
832 * @param timestamp time to serialize
833 * @return #GNUNET_OK on success
836 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
837 struct GNUNET_TIME_Absolute timestamp)
839 struct GNUNET_TIME_Relative dur;
841 dur = GNUNET_TIME_absolute_get_duration (timestamp);
842 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
847 * Deserialize a start-time. Since we use start-times to
848 * calculate the duration of some operation, we actually
849 * do not serialize the absolute time but the (relative)
850 * duration since the start time. Thus, when we then
851 * deserialize the start time, we take the current time and
852 * subtract that duration so that we get again an absolute
853 * time stamp that will result in correct performance
856 * @param rh handle for reading
857 * @param timestamp where to write the deserialized timestamp
858 * @return #GNUNET_OK on success
861 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
862 struct GNUNET_TIME_Absolute *timestamp)
864 struct GNUNET_TIME_Relative dur;
866 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
867 return GNUNET_SYSERR;
868 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
874 * Using the given serialization filename, try to deserialize
875 * the file-information tree associated with it.
877 * @param h master context
878 * @param filename name of the file (without directory) with
880 * @return NULL on error
882 static struct GNUNET_FS_FileInformation *
883 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename);
887 * Using the given serialization filename, try to deserialize
888 * the file-information tree associated with it.
890 * @param h master context
891 * @param fn name of the file (without directory) with
893 * @param rh handle for reading
894 * @return NULL on error
896 static struct GNUNET_FS_FileInformation *
897 deserialize_fi_node (struct GNUNET_FS_Handle *h,
899 struct GNUNET_BIO_ReadHandle *rh)
901 struct GNUNET_FS_FileInformation *ret;
902 struct GNUNET_FS_FileInformation *nxt;
910 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
915 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
921 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
922 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
924 ((NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
925 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)))) ||
926 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
928 ((NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
929 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri)))) ||
930 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
932 ((NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
933 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri)))) ||
934 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
936 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024)) ||
938 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
940 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
941 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
942 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
943 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
950 case 0: /* file-insert */
951 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
956 ret->is_directory = GNUNET_NO;
957 ret->data.file.do_index = GNUNET_NO;
958 ret->data.file.have_hash = GNUNET_NO;
959 ret->data.file.index_start_confirmed = GNUNET_NO;
960 if (GNUNET_NO == ret->is_published)
962 if (NULL == ret->filename)
964 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
965 ret->data.file.reader_cls =
966 GNUNET_malloc_large (ret->data.file.file_size);
967 if (ret->data.file.reader_cls == NULL)
969 if (GNUNET_OK != GNUNET_BIO_read (rh,
971 ret->data.file.reader_cls,
972 ret->data.file.file_size))
980 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
981 ret->data.file.reader_cls =
982 GNUNET_FS_make_file_reader_context_ (ret->filename);
987 case 1: /* file-index, no hash */
988 if (NULL == ret->filename)
993 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
998 ret->is_directory = GNUNET_NO;
999 ret->data.file.do_index = GNUNET_YES;
1000 ret->data.file.have_hash = GNUNET_NO;
1001 ret->data.file.index_start_confirmed = GNUNET_NO;
1002 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1003 ret->data.file.reader_cls =
1004 GNUNET_FS_make_file_reader_context_ (ret->filename);
1007 case 2: /* file-index-with-hash */
1008 if (NULL == ret->filename)
1013 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1014 (GNUNET_OK != GNUNET_BIO_read (rh,
1016 &ret->data.file.file_id,
1017 sizeof(struct GNUNET_HashCode))))
1022 ret->is_directory = GNUNET_NO;
1023 ret->data.file.do_index = GNUNET_YES;
1024 ret->data.file.have_hash = GNUNET_YES;
1025 ret->data.file.index_start_confirmed = GNUNET_NO;
1026 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1027 ret->data.file.reader_cls =
1028 GNUNET_FS_make_file_reader_context_ (ret->filename);
1031 case 3: /* file-index-with-hash-confirmed */
1032 if (NULL == ret->filename)
1037 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1038 (GNUNET_OK != GNUNET_BIO_read (rh,
1040 &ret->data.file.file_id,
1041 sizeof(struct GNUNET_HashCode))))
1046 ret->is_directory = GNUNET_NO;
1047 ret->data.file.do_index = GNUNET_YES;
1048 ret->data.file.have_hash = GNUNET_YES;
1049 ret->data.file.index_start_confirmed = GNUNET_YES;
1050 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1051 ret->data.file.reader_cls =
1052 GNUNET_FS_make_file_reader_context_ (ret->filename);
1055 case 4: /* directory */
1056 ret->is_directory = GNUNET_YES;
1057 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1059 GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1061 GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1062 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1064 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1066 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1071 ret->data.dir.dir_size = (uint32_t) dsize;
1072 if (NULL != filename)
1074 ret->data.dir.entries = deserialize_file_information (h, filename);
1075 GNUNET_free (filename);
1077 nxt = ret->data.dir.entries;
1090 ret->serialization = GNUNET_strdup (fn);
1092 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1097 if (NULL != filename)
1099 ret->next = deserialize_file_information (h, filename);
1100 GNUNET_free (filename);
1103 GNUNET_free_non_null (ksks);
1104 GNUNET_free_non_null (skss);
1105 GNUNET_free_non_null (chks);
1108 GNUNET_free_non_null (ksks);
1109 GNUNET_free_non_null (chks);
1110 GNUNET_free_non_null (skss);
1111 GNUNET_free_non_null (filename);
1112 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1118 * Using the given serialization filename, try to deserialize
1119 * the file-information tree associated with it.
1121 * @param h master context
1122 * @param filename name of the file (without directory) with
1124 * @return NULL on error
1126 static struct GNUNET_FS_FileInformation *
1127 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename)
1129 struct GNUNET_FS_FileInformation *ret;
1130 struct GNUNET_BIO_ReadHandle *rh;
1134 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1137 ret = deserialize_fi_node (h, filename, rh);
1138 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1141 _ ("Failed to resume publishing information `%s': %s\n"),
1149 get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1152 if (0 != unlink (fn))
1153 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1162 * Given a serialization name (full absolute path), return the
1163 * basename of the file (without the path), which must only
1164 * consist of the 6 random characters.
1166 * @param fullname name to extract the basename from
1167 * @return copy of the basename, NULL on error
1170 get_serialization_short_name (const char *fullname)
1177 /* FIXME: we could do this faster since we know
1178 * the length of 'end'... */
1179 while ('\0' != *nxt)
1181 if (DIR_SEPARATOR == *nxt)
1185 if ((NULL == end) || (0 == strlen (end)))
1190 GNUNET_break (6 == strlen (end));
1191 return GNUNET_strdup (end);
1196 * Create a new random name for serialization. Also checks if persistence
1197 * is enabled and returns NULL if not.
1199 * @param h master context
1200 * @param ext component of the path
1201 * @return NULL on errror
1204 make_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext)
1210 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1211 return NULL; /* persistence not requested */
1212 dn = get_serialization_file_name (h, ext, "");
1215 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1220 fn = GNUNET_DISK_mktemp (dn);
1223 return NULL; /* epic fail */
1224 ret = get_serialization_short_name (fn);
1231 * Create a new random name for serialization. Also checks if persistence
1232 * is enabled and returns NULL if not.
1234 * @param h master context
1235 * @param ext component of the path
1236 * @param uni name of parent
1237 * @return NULL on errror
1240 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1248 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1249 return NULL; /* persistence not requested */
1250 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1253 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1258 fn = GNUNET_DISK_mktemp (dn);
1261 return NULL; /* epic fail */
1262 ret = get_serialization_short_name (fn);
1269 * Copy all of the data from the reader to the write handle.
1271 * @param wh write handle
1272 * @param fi file with reader
1273 * @return #GNUNET_OK on success
1276 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1277 struct GNUNET_FS_FileInformation *fi)
1279 char buf[32 * 1024];
1287 while (off < fi->data.file.file_size)
1289 left = GNUNET_MIN (sizeof(buf), fi->data.file.file_size - off);
1291 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1295 return GNUNET_SYSERR;
1297 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1298 return GNUNET_SYSERR;
1306 * Create a temporary file on disk to store the current
1307 * state of @a fi in.
1309 * @param fi file information to sync with disk
1312 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1315 struct GNUNET_BIO_WriteHandle *wh;
1321 if (NULL == fi->serialization)
1323 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1324 if (NULL == fi->serialization)
1327 get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1330 GNUNET_free (fi->serialization);
1331 fi->serialization = NULL;
1334 if (GNUNET_YES == fi->is_directory)
1336 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1338 else if (GNUNET_YES == fi->data.file.have_hash)
1340 else if (GNUNET_YES == fi->data.file.do_index)
1344 if (NULL != fi->keywords)
1345 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1348 if (NULL != fi->chk_uri)
1349 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1352 if (NULL != fi->sks_uri)
1353 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1356 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof(b))) ||
1357 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1358 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1359 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1360 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1361 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1362 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1363 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1365 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1366 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1367 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1368 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1373 GNUNET_free_non_null (chks);
1375 GNUNET_free_non_null (ksks);
1377 GNUNET_free_non_null (skss);
1382 case 0: /* file-insert */
1383 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1388 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1389 if (GNUNET_OK != copy_from_reader (wh, fi))
1396 case 1: /* file-index, no hash */
1397 if (NULL == fi->filename)
1402 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1409 case 2: /* file-index-with-hash */
1410 case 3: /* file-index-with-hash-confirmed */
1411 if (NULL == fi->filename)
1416 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1417 (GNUNET_OK != GNUNET_BIO_write (wh,
1418 &fi->data.file.file_id,
1419 sizeof(struct GNUNET_HashCode))))
1426 case 4: /* directory */
1427 if ((NULL != fi->data.dir.entries) &&
1428 (NULL == fi->data.dir.entries->serialization))
1429 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1430 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1432 GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1434 GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1435 (GNUNET_OK != GNUNET_BIO_write (wh,
1436 fi->data.dir.dir_data,
1437 (uint32_t) fi->data.dir.dir_size)) ||
1439 GNUNET_BIO_write_string (wh,
1440 (fi->data.dir.entries == NULL)
1442 : fi->data.dir.entries->serialization)))
1453 if ((NULL != fi->next) && (NULL == fi->next->serialization))
1454 GNUNET_FS_file_information_sync_ (fi->next);
1455 if (GNUNET_OK != GNUNET_BIO_write_string (wh,
1457 ? fi->next->serialization
1463 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1472 (void) GNUNET_BIO_write_close (wh);
1473 GNUNET_free_non_null (chks);
1474 GNUNET_free_non_null (ksks);
1475 GNUNET_free_non_null (skss);
1476 fn = get_serialization_file_name (fi->h,
1477 GNUNET_FS_SYNC_PATH_FILE_INFO,
1481 if (0 != unlink (fn))
1482 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1485 GNUNET_free (fi->serialization);
1486 fi->serialization = NULL;
1491 * Find the entry in the file information struct where the
1492 * serialization filename matches the given name.
1494 * @param pos file information to search
1495 * @param srch filename to search for
1496 * @return NULL if srch was not found in this subtree
1498 static struct GNUNET_FS_FileInformation *
1499 find_file_position (struct GNUNET_FS_FileInformation *pos, const char *srch)
1501 struct GNUNET_FS_FileInformation *r;
1505 if (0 == strcmp (srch, pos->serialization))
1507 if ((GNUNET_YES == pos->is_directory) &&
1508 (NULL != (r = find_file_position (pos->data.dir.entries, srch))))
1517 * Signal the FS's progress function that we are resuming
1520 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1521 * @param fi the entry in the publish-structure
1522 * @param length length of the file or directory
1523 * @param meta metadata for the file or directory (can be modified)
1524 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1525 * @param bo block options (can be modified)
1526 * @param do_index should we index?
1527 * @param client_info pointer to client context set upon creation (can be modified)
1528 * @return #GNUNET_OK to continue (always)
1531 fip_signal_resume (void *cls,
1532 struct GNUNET_FS_FileInformation *fi,
1534 struct GNUNET_CONTAINER_MetaData *meta,
1535 struct GNUNET_FS_Uri **uri,
1536 struct GNUNET_FS_BlockOptions *bo,
1540 struct GNUNET_FS_PublishContext *pc = cls;
1541 struct GNUNET_FS_ProgressInfo pi;
1543 if (GNUNET_YES == pc->skip_next_fi_callback)
1545 pc->skip_next_fi_callback = GNUNET_NO;
1548 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1549 pi.value.publish.specifics.resume.message = fi->emsg;
1550 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1551 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1552 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1554 /* process entries in directory */
1555 pc->skip_next_fi_callback = GNUNET_YES;
1556 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1563 * Function called with a filename of serialized publishing operation
1566 * @param cls the `struct GNUNET_FS_Handle *`
1567 * @param filename complete filename (absolute path)
1568 * @return #GNUNET_OK (continue to iterate)
1571 deserialize_publish_file (void *cls, const char *filename)
1573 struct GNUNET_FS_Handle *h = cls;
1574 struct GNUNET_BIO_ReadHandle *rh;
1575 struct GNUNET_FS_PublishContext *pc;
1580 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1584 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1586 pc->serialization = get_serialization_short_name (filename);
1589 rh = GNUNET_BIO_read_open (filename);
1596 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1598 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1599 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1600 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1601 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1603 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1605 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1606 ((GNUNET_YES == have_ns) &&
1607 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof(ns)))))
1612 pc->options = options;
1613 pc->all_done = all_done;
1614 if (NULL == fi_root)
1619 pc->fi = deserialize_file_information (h, fi_root);
1625 if (GNUNET_YES == have_ns)
1627 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1630 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1631 (GNUNET_YES != pc->all_done))
1633 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1634 if (NULL == pc->dsh)
1639 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1640 GNUNET_free (fi_pos);
1642 if (NULL == pc->fi_pos)
1644 /* failed to find position for resuming, outch! Will start from root! */
1646 if (GNUNET_YES != pc->all_done)
1647 pc->fi_pos = pc->fi;
1650 GNUNET_free (fi_root);
1652 /* generate RESUME event(s) */
1653 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1655 /* re-start publishing (if needed)... */
1656 if (GNUNET_YES != pc->all_done)
1658 GNUNET_assert (NULL == pc->upload_task);
1660 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1661 &GNUNET_FS_publish_main_,
1664 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1667 _ ("Failure while resuming publishing operation `%s': %s\n"),
1672 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1675 GNUNET_free_non_null (pc->nid);
1676 GNUNET_free_non_null (pc->nuid);
1677 GNUNET_free_non_null (fi_root);
1678 GNUNET_free_non_null (fi_pos);
1679 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1682 _ ("Failed to resume publishing operation `%s': %s\n"),
1688 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1689 if (0 != unlink (filename))
1690 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1691 GNUNET_free (pc->serialization);
1698 * Synchronize this publishing struct with its mirror
1699 * on disk. Note that all internal FS-operations that change
1700 * publishing structs should already call "sync" internally,
1701 * so this function is likely not useful for clients.
1703 * @param pc the struct to sync
1706 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1708 struct GNUNET_BIO_WriteHandle *wh;
1711 if (NULL == pc->serialization)
1713 make_serialization_file_name (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1714 if (NULL == pc->serialization)
1718 if (NULL == pc->fi->serialization)
1723 wh = get_write_handle (pc->h,
1724 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1731 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1732 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1733 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1734 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1735 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1736 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1737 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1738 (GNUNET_OK != GNUNET_BIO_write_string (wh,
1739 (NULL == pc->fi_pos)
1741 : pc->fi_pos->serialization)) ||
1742 ((NULL != pc->ns) &&
1744 GNUNET_BIO_write (wh,
1746 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))))
1751 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1760 (void) GNUNET_BIO_write_close (wh);
1761 GNUNET_FS_remove_sync_file_ (pc->h,
1762 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1764 GNUNET_free (pc->serialization);
1765 pc->serialization = NULL;
1770 * Synchronize this unindex struct with its mirror
1771 * on disk. Note that all internal FS-operations that change
1772 * publishing structs should already call "sync" internally,
1773 * so this function is likely not useful for clients.
1775 * @param uc the struct to sync
1778 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1780 struct GNUNET_BIO_WriteHandle *wh;
1783 if (NULL == uc->serialization)
1785 make_serialization_file_name (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1786 if (NULL == uc->serialization)
1788 wh = get_write_handle (uc->h,
1789 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1796 if (NULL != uc->ksk_uri)
1797 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1800 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1801 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1802 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1803 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1805 GNUNET_BIO_write (wh, &uc->chk, sizeof(struct ContentHashKey))) ||
1806 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1807 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1808 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1809 (GNUNET_OK != GNUNET_BIO_write (wh,
1811 sizeof(struct GNUNET_HashCode)))) ||
1812 ((uc->state == UNINDEX_STATE_ERROR) &&
1813 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1818 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1827 (void) GNUNET_BIO_write_close (wh);
1828 GNUNET_FS_remove_sync_file_ (uc->h,
1829 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1831 GNUNET_free (uc->serialization);
1832 uc->serialization = NULL;
1837 * Serialize a download request.
1839 * @param wh handle for writing the download request to disk
1840 * @param dr the the request to write to disk
1841 * @return #GNUNET_YES on success, #GNUNET_NO on error
1844 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1845 struct DownloadRequest *dr)
1849 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1850 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1851 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1852 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1854 if ((BRS_CHK_SET == dr->state) &&
1856 GNUNET_BIO_write (wh, &dr->chk, sizeof(struct ContentHashKey))))
1858 for (i = 0; i < dr->num_children; i++)
1859 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1866 * Read a download request tree.
1868 * @param rh cadet to read from
1869 * @return value the download request read from disk, NULL on error
1871 static struct DownloadRequest *
1872 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1874 struct DownloadRequest *dr;
1877 dr = GNUNET_new (struct DownloadRequest);
1878 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1879 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1880 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1881 (dr->num_children > CHK_PER_INODE) ||
1882 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1883 ((0 == dr->depth) && (dr->num_children > 0)) ||
1884 ((dr->depth > 0) && (0 == dr->num_children)))
1887 dr->num_children = 0;
1890 if (dr->num_children > 0)
1892 GNUNET_malloc (dr->num_children * sizeof(struct DownloadRequest *));
1896 case BRS_RECONSTRUCT_DOWN:
1897 case BRS_RECONSTRUCT_META_UP:
1898 case BRS_RECONSTRUCT_UP:
1903 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof(struct ContentHashKey)))
1907 case BRS_DOWNLOAD_DOWN:
1908 case BRS_DOWNLOAD_UP:
1916 for (i = 0; i < dr->num_children; i++)
1918 if (NULL == (dr->children[i] = read_download_request (rh)))
1920 dr->children[i]->parent = dr;
1924 GNUNET_FS_free_download_request_ (dr);
1930 * Compute the name of the sync file (or directory) for the given download
1933 * @param dc download context to compute for
1934 * @param uni unique filename to use, use "" for the directory name
1935 * @param ext extension to use, use ".dir" for our own subdirectory
1936 * @return the expanded file name, NULL for none
1939 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1946 if (dc->parent == NULL)
1947 return get_serialization_file_name (dc->h,
1948 (dc->search != NULL)
1949 ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
1950 : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1952 if (NULL == dc->parent->serialization)
1954 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1957 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1964 * Synchronize this download struct with its mirror
1965 * on disk. Note that all internal FS-operations that change
1966 * publishing structs should already call "sync" internally,
1967 * so this function is likely not useful for clients.
1969 * @param dc the struct to sync
1972 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1974 struct GNUNET_BIO_WriteHandle *wh;
1979 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1980 return; /* we don't sync probes */
1981 if (NULL == dc->serialization)
1983 dir = get_download_sync_filename (dc, "", "");
1986 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1991 fn = GNUNET_DISK_mktemp (dir);
1995 dc->serialization = get_serialization_short_name (fn);
1999 fn = get_download_sync_filename (dc, dc->serialization, "");
2002 GNUNET_free (dc->serialization);
2003 dc->serialization = NULL;
2008 wh = GNUNET_BIO_write_open (fn);
2011 GNUNET_free (dc->serialization);
2012 dc->serialization = NULL;
2016 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
2017 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
2018 uris = GNUNET_FS_uri_to_string (dc->uri);
2019 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2020 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
2021 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
2022 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
2023 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
2024 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
2025 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
2026 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
2027 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
2028 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
2029 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
2030 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
2031 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
2036 if (NULL == dc->emsg)
2038 GNUNET_assert (dc->top_request != NULL);
2039 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2045 GNUNET_free_non_null (uris);
2047 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2057 (void) GNUNET_BIO_write_close (wh);
2058 GNUNET_free_non_null (uris);
2059 if (0 != unlink (fn))
2060 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2062 GNUNET_free (dc->serialization);
2063 dc->serialization = NULL;
2068 * Synchronize this search result with its mirror
2069 * on disk. Note that all internal FS-operations that change
2070 * publishing structs should already call "sync" internally,
2071 * so this function is likely not useful for clients.
2073 * @param sr the struct to sync
2076 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2078 struct GNUNET_BIO_WriteHandle *wh;
2084 if (NULL == sr->serialization)
2086 make_serialization_file_name_in_dir (sr->h,
2087 (sr->sc->psearch_result == NULL)
2088 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2089 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2090 sr->sc->serialization);
2091 if (NULL == sr->serialization)
2093 wh = get_write_handle_in_dir (sr->h,
2094 (sr->sc->psearch_result == NULL)
2095 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2096 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2097 sr->sc->serialization,
2104 uris = GNUNET_FS_uri_to_string (sr->uri);
2105 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2106 (GNUNET_OK != GNUNET_BIO_write_string (wh,
2107 (sr->download != NULL)
2108 ? sr->download->serialization
2111 GNUNET_BIO_write_string (wh,
2112 (sr->update_search != NULL)
2113 ? sr->update_search->serialization
2115 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2117 GNUNET_BIO_write (wh, &sr->key, sizeof(struct GNUNET_HashCode))) ||
2118 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2119 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2120 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2121 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)))
2126 if ((NULL != sr->uri) && (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2128 GNUNET_BIO_write (wh,
2130 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)))
2135 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2141 GNUNET_free_non_null (uris);
2144 GNUNET_free_non_null (uris);
2146 (void) GNUNET_BIO_write_close (wh);
2147 remove_sync_file_in_dir (sr->h,
2148 (NULL == sr->sc->psearch_result)
2149 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2150 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2151 sr->sc->serialization,
2153 GNUNET_free (sr->serialization);
2154 sr->serialization = NULL;
2159 * Synchronize this search struct with its mirror
2160 * on disk. Note that all internal FS-operations that change
2161 * publishing structs should already call "sync" internally,
2162 * so this function is likely not useful for clients.
2164 * @param sc the struct to sync
2167 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2169 struct GNUNET_BIO_WriteHandle *wh;
2172 const char *category;
2174 category = (NULL == sc->psearch_result) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2175 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2176 if (NULL == sc->serialization)
2177 sc->serialization = make_serialization_file_name (sc->h, category);
2178 if (NULL == sc->serialization)
2181 wh = get_write_handle (sc->h, category, sc->serialization);
2187 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2188 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2189 uris = GNUNET_FS_uri_to_string (sc->uri);
2190 in_pause = (sc->task != NULL) ? 'r' : '\0';
2191 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2192 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2193 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2194 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2195 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof(in_pause))) ||
2196 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2203 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2212 (void) GNUNET_BIO_write_close (wh);
2213 GNUNET_free_non_null (uris);
2214 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2215 GNUNET_free (sc->serialization);
2216 sc->serialization = NULL;
2221 * Function called with a filename of serialized unindexing operation
2224 * @param cls the `struct GNUNET_FS_Handle *`
2225 * @param filename complete filename (absolute path)
2226 * @return #GNUNET_OK (continue to iterate)
2229 deserialize_unindex_file (void *cls, const char *filename)
2231 struct GNUNET_FS_Handle *h = cls;
2232 struct GNUNET_BIO_ReadHandle *rh;
2233 struct GNUNET_FS_UnindexContext *uc;
2234 struct GNUNET_FS_ProgressInfo pi;
2239 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2241 uc->serialization = get_serialization_short_name (filename);
2242 rh = GNUNET_BIO_read_open (filename);
2250 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2251 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2252 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2253 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2255 GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof(struct ContentHashKey))) ||
2257 GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2258 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)))
2260 GNUNET_free_non_null (uris);
2266 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2268 if (NULL == uc->ksk_uri)
2271 GNUNET_free_non_null (emsg);
2275 if ((uc->ksk_offset > 0) &&
2276 ((NULL == uc->ksk_uri) ||
2277 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount)))
2282 uc->state = (enum UnindexState) state;
2285 case UNINDEX_STATE_HASHING:
2288 case UNINDEX_STATE_FS_NOTIFY:
2289 if (GNUNET_OK != GNUNET_BIO_read (rh,
2292 sizeof(struct GNUNET_HashCode)))
2299 case UNINDEX_STATE_DS_REMOVE:
2300 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2301 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2304 case UNINDEX_STATE_COMPLETE:
2307 case UNINDEX_STATE_ERROR:
2309 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2320 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2321 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2322 pi.value.unindex.specifics.resume.message = uc->emsg;
2323 GNUNET_FS_unindex_make_status_ (&pi,
2325 (uc->state == UNINDEX_STATE_COMPLETE)
2330 case UNINDEX_STATE_HASHING:
2331 uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
2334 &GNUNET_FS_unindex_process_hash_,
2338 case UNINDEX_STATE_FS_NOTIFY:
2339 uc->state = UNINDEX_STATE_HASHING;
2340 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2343 case UNINDEX_STATE_DS_REMOVE:
2344 GNUNET_FS_unindex_do_remove_ (uc);
2347 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2348 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2351 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2352 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2355 case UNINDEX_STATE_COMPLETE:
2356 case UNINDEX_STATE_ERROR:
2357 /* no need to resume any operation, we were done */
2363 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2366 _ ("Failure while resuming unindexing operation `%s': %s\n"),
2373 GNUNET_free_non_null (uc->filename);
2374 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2377 _ ("Failed to resume unindexing operation `%s': %s\n"),
2382 if (NULL != uc->serialization)
2383 GNUNET_FS_remove_sync_file_ (h,
2384 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2386 GNUNET_free_non_null (uc->serialization);
2393 * Deserialize a download.
2395 * @param h overall context
2396 * @param rh file to deserialize from
2397 * @param parent parent download
2398 * @param search associated search
2399 * @param serialization name under which the search was serialized
2402 deserialize_download (struct GNUNET_FS_Handle *h,
2403 struct GNUNET_BIO_ReadHandle *rh,
2404 struct GNUNET_FS_DownloadContext *parent,
2405 struct GNUNET_FS_SearchResult *search,
2406 const char *serialization);
2410 * Deserialize a search.
2412 * @param h overall context
2413 * @param rh file to deserialize from
2414 * @param psearch_result parent search result
2415 * @param serialization name under which the search was serialized
2417 static struct GNUNET_FS_SearchContext *
2418 deserialize_search (struct GNUNET_FS_Handle *h,
2419 struct GNUNET_BIO_ReadHandle *rh,
2420 struct GNUNET_FS_SearchResult *psearch_result,
2421 const char *serialization);
2425 * Function called with a filename of serialized search result
2428 * @param cls the `struct GNUNET_FS_SearchContext *`
2429 * @param filename complete filename (absolute path)
2430 * @return #GNUNET_OK (continue to iterate)
2433 deserialize_search_result (void *cls, const char *filename)
2435 struct GNUNET_FS_SearchContext *sc = cls;
2441 struct GNUNET_BIO_ReadHandle *rh;
2442 struct GNUNET_BIO_ReadHandle *drh;
2443 struct GNUNET_FS_SearchResult *sr;
2445 ser = get_serialization_short_name (filename);
2446 rh = GNUNET_BIO_read_open (filename);
2451 remove_sync_file_in_dir (sc->h,
2452 (NULL == sc->psearch_result)
2453 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2454 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2465 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2468 sr->serialization = ser;
2470 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024)) ||
2471 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2473 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
2475 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2476 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2477 (GNUNET_OK != GNUNET_BIO_read (rh,
2480 sizeof(struct GNUNET_HashCode))) ||
2481 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2482 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2483 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2484 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2489 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2491 sr->keyword_bitmap = GNUNET_malloc (
2492 (sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2494 GNUNET_BIO_read (rh,
2497 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2504 if (NULL != download)
2506 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2509 deserialize_download (sc->h, drh, NULL, sr, download);
2510 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2513 _ ("Failed to resume sub-download `%s': %s\n"),
2519 GNUNET_free (download);
2521 if (NULL != update_srch)
2524 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2527 deserialize_search (sc->h, drh, sr, update_srch);
2528 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2531 _ ("Failed to resume sub-search `%s': %s\n"),
2537 GNUNET_free (update_srch);
2539 GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put (
2540 sc->master_result_map,
2543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2544 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2546 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2547 _ ("Failure while resuming search operation `%s': %s\n"),
2554 GNUNET_free_non_null (download);
2555 GNUNET_free_non_null (emsg);
2556 GNUNET_free_non_null (uris);
2557 GNUNET_free_non_null (update_srch);
2558 if (NULL != sr->uri)
2559 GNUNET_FS_uri_destroy (sr->uri);
2560 if (NULL != sr->meta)
2561 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2562 GNUNET_free (sr->serialization);
2564 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2567 _ ("Failure while resuming search operation `%s': %s\n"),
2577 * Send the 'resume' signal to the callback; also actually
2578 * resume the download (put it in the queue). Does this
2579 * recursively for the top-level download and all child
2582 * @param dc download to resume
2585 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2587 struct GNUNET_FS_DownloadContext *dcc;
2588 struct GNUNET_FS_ProgressInfo pi;
2590 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2591 pi.value.download.specifics.resume.meta = dc->meta;
2592 pi.value.download.specifics.resume.message = dc->emsg;
2593 GNUNET_FS_download_make_status_ (&pi, dc);
2594 dcc = dc->child_head;
2597 signal_download_resume (dcc);
2604 * Signal resuming of a search to our clients (for the
2605 * top level search and all sub-searches).
2607 * @param sc search being resumed
2610 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2614 * Iterator over search results signaling resume to the client for
2617 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2618 * @param key current key code
2619 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2620 * @return #GNUNET_YES (we should continue to iterate)
2623 signal_result_resume (void *cls, const struct GNUNET_HashCode *key, void *value)
2625 struct GNUNET_FS_SearchContext *sc = cls;
2626 struct GNUNET_FS_ProgressInfo pi;
2627 struct GNUNET_FS_SearchResult *sr = value;
2629 if (0 == sr->mandatory_missing)
2631 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2632 pi.value.search.specifics.resume_result.meta = sr->meta;
2633 pi.value.search.specifics.resume_result.uri = sr->uri;
2634 pi.value.search.specifics.resume_result.result = sr;
2635 pi.value.search.specifics.resume_result.availability_rank =
2636 2 * sr->availability_success - sr->availability_trials;
2637 pi.value.search.specifics.resume_result.availability_certainty =
2638 sr->availability_trials;
2639 pi.value.search.specifics.resume_result.applicability_rank =
2640 sr->optional_support;
2641 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2643 if (NULL != sr->download)
2645 signal_download_resume (sr->download);
2649 GNUNET_FS_search_start_probe_ (sr);
2651 if (NULL != sr->update_search)
2652 signal_search_resume (sr->update_search);
2658 * Free memory allocated by the search context and its children
2660 * @param sc search context to free
2663 free_search_context (struct GNUNET_FS_SearchContext *sc);
2667 * Iterator over search results freeing each.
2669 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2670 * @param key current key code
2671 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2672 * @return #GNUNET_YES (we should continue to iterate)
2675 free_result (void *cls, const struct GNUNET_HashCode *key, void *value)
2677 struct GNUNET_FS_SearchResult *sr = value;
2679 if (NULL != sr->update_search)
2681 free_search_context (sr->update_search);
2682 GNUNET_assert (NULL == sr->update_search);
2684 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2685 GNUNET_FS_uri_destroy (sr->uri);
2692 * Free memory allocated by the search context and its children
2694 * @param sc search context to free
2697 free_search_context (struct GNUNET_FS_SearchContext *sc)
2699 if (NULL != sc->serialization)
2701 GNUNET_FS_remove_sync_file_ (sc->h,
2702 (sc->psearch_result == NULL)
2703 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2704 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2706 GNUNET_FS_remove_sync_dir_ (sc->h,
2707 (sc->psearch_result == NULL)
2708 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2709 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2712 GNUNET_free_non_null (sc->serialization);
2713 GNUNET_free_non_null (sc->emsg);
2714 if (NULL != sc->uri)
2715 GNUNET_FS_uri_destroy (sc->uri);
2716 if (NULL != sc->master_result_map)
2718 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2721 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2728 * Function called with a filename of serialized sub-download
2731 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2732 * @param filename complete filename (absolute path)
2733 * @return #GNUNET_OK (continue to iterate)
2736 deserialize_subdownload (void *cls, const char *filename)
2738 struct GNUNET_FS_DownloadContext *parent = cls;
2741 struct GNUNET_BIO_ReadHandle *rh;
2743 ser = get_serialization_short_name (filename);
2744 rh = GNUNET_BIO_read_open (filename);
2747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2749 "Failed to resume sub-download `%s': could not open file `%s'\n"),
2755 deserialize_download (parent->h, rh, parent, NULL, ser);
2756 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2759 _ ("Failed to resume sub-download `%s': %s\n"),
2770 * Free this download context and all of its descendants.
2771 * (only works during deserialization since not all possible
2772 * state it taken care of).
2774 * @param dc context to free
2777 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2779 struct GNUNET_FS_DownloadContext *dcc;
2781 if (NULL != dc->meta)
2782 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2783 if (NULL != dc->uri)
2784 GNUNET_FS_uri_destroy (dc->uri);
2785 GNUNET_free_non_null (dc->temp_filename);
2786 GNUNET_free_non_null (dc->emsg);
2787 GNUNET_free_non_null (dc->filename);
2788 GNUNET_free_non_null (dc->serialization);
2789 while (NULL != (dcc = dc->child_head))
2791 GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
2792 free_download_context (dcc);
2794 GNUNET_FS_free_download_request_ (dc->top_request);
2795 if (NULL != dc->active)
2796 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2802 * Deserialize a download.
2804 * @param h overall context
2805 * @param rh file to deserialize from
2806 * @param parent parent download
2807 * @param search associated search
2808 * @param serialization name under which the search was serialized
2811 deserialize_download (struct GNUNET_FS_Handle *h,
2812 struct GNUNET_BIO_ReadHandle *rh,
2813 struct GNUNET_FS_DownloadContext *parent,
2814 struct GNUNET_FS_SearchResult *search,
2815 const char *serialization)
2817 struct GNUNET_FS_DownloadContext *dc;
2826 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2827 dc->parent = parent;
2829 dc->serialization = GNUNET_strdup (serialization);
2831 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2832 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2833 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2834 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2836 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2838 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2840 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2841 (GNUNET_OK != GNUNET_BIO_read_string (rh,
2845 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2846 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2847 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2848 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2849 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2850 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2851 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2852 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2857 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2859 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE),
2861 dc->has_finished = (int) status;
2863 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2864 if (GNUNET_FS_uri_test_loc (dc->uri))
2865 GNUNET_assert (GNUNET_OK ==
2866 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2867 if (NULL == dc->emsg)
2869 dc->top_request = read_download_request (rh);
2870 if (NULL == dc->top_request)
2876 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2879 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2880 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2885 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2889 dc->search = search;
2890 search->download = dc;
2892 if ((NULL == parent) && (NULL == search))
2895 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2896 signal_download_resume (dc);
2899 GNUNET_assert (NULL == dc->job_queue);
2900 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2903 GNUNET_free_non_null (uris);
2904 GNUNET_free_non_null (emsg);
2905 free_download_context (dc);
2910 * Signal resuming of a search to our clients (for the
2911 * top level search and all sub-searches).
2913 * @param sc search being resumed
2916 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2918 struct GNUNET_FS_ProgressInfo pi;
2920 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2921 pi.value.search.specifics.resume.message = sc->emsg;
2922 pi.value.search.specifics.resume.is_paused =
2923 (NULL == sc->mq) ? GNUNET_YES : GNUNET_NO;
2924 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2925 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2926 &signal_result_resume,
2932 * Deserialize a search.
2934 * @param h overall context
2935 * @param rh file to deserialize from
2936 * @param psearch_result parent search result
2937 * @param serialization name under which the search was serialized
2939 static struct GNUNET_FS_SearchContext *
2940 deserialize_search (struct GNUNET_FS_Handle *h,
2941 struct GNUNET_BIO_ReadHandle *rh,
2942 struct GNUNET_FS_SearchResult *psearch_result,
2943 const char *serialization)
2945 struct GNUNET_FS_SearchContext *sc;
2952 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2959 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2960 if (NULL != psearch_result)
2962 sc->psearch_result = psearch_result;
2963 psearch_result->update_search = sc;
2966 sc->serialization = GNUNET_strdup (serialization);
2968 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2969 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2970 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2971 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2972 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2974 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2975 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2977 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof(in_pause))) ||
2978 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2983 sc->options = (enum GNUNET_FS_SearchOptions) options;
2984 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2985 dn = get_serialization_file_name_in_dir (h,
2986 (NULL == sc->psearch_result)
2987 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2988 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2993 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2994 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2997 if (('\0' == in_pause) &&
2998 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
3001 GNUNET_ERROR_TYPE_WARNING,
3002 _ ("Could not resume running search, will resume as paused search\n"));
3004 signal_search_resume (sc);
3008 GNUNET_free_non_null (emsg);
3009 free_search_context (sc);
3010 GNUNET_free_non_null (uris);
3016 * Function called with a filename of serialized search operation
3019 * @param cls the `struct GNUNET_FS_Handle *`
3020 * @param filename complete filename (absolute path)
3021 * @return #GNUNET_OK (continue to iterate)
3024 deserialize_search_file (void *cls, const char *filename)
3026 struct GNUNET_FS_Handle *h = cls;
3029 struct GNUNET_BIO_ReadHandle *rh;
3030 struct GNUNET_FS_SearchContext *sc;
3033 if (0 != stat (filename, &buf))
3035 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
3038 if (S_ISDIR (buf.st_mode))
3039 return GNUNET_OK; /* skip directories */
3040 ser = get_serialization_short_name (filename);
3041 rh = GNUNET_BIO_read_open (filename);
3046 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
3051 sc = deserialize_search (h, rh, NULL, ser);
3053 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
3055 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3058 _ ("Failure while resuming search operation `%s': %s\n"),
3068 * Function called with a filename of serialized download operation
3071 * @param cls the `struct GNUNET_FS_Handle *`
3072 * @param filename complete filename (absolute path)
3073 * @return #GNUNET_OK (continue to iterate)
3076 deserialize_download_file (void *cls, const char *filename)
3078 struct GNUNET_FS_Handle *h = cls;
3081 struct GNUNET_BIO_ReadHandle *rh;
3083 ser = get_serialization_short_name (filename);
3084 rh = GNUNET_BIO_read_open (filename);
3087 if (0 != unlink (filename))
3088 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3092 deserialize_download (h, rh, NULL, NULL, ser);
3094 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3097 _ ("Failure while resuming download operation `%s': %s\n"),
3107 * Deserialize informatin about pending operations.
3109 * @param master_path which master directory should be scanned
3110 * @param proc function to call for each entry (will get @a h for 'cls')
3111 * @param h the `struct GNUNET_FS_Handle *`
3114 deserialization_master (const char *master_path,
3115 GNUNET_FileNameCallback proc,
3116 struct GNUNET_FS_Handle *h)
3120 dn = get_serialization_file_name (h, master_path, "");
3123 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3124 GNUNET_DISK_directory_scan (dn, proc, h);
3130 * Setup a connection to the file-sharing service.
3132 * @param cfg configuration to use
3133 * @param client_name unique identifier for this client
3134 * @param upcb function to call to notify about FS actions
3135 * @param upcb_cls closure for @a upcb
3136 * @param flags specific attributes for fs-operations
3137 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3138 * @return NULL on error
3140 struct GNUNET_FS_Handle *
3141 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3142 const char *client_name,
3143 GNUNET_FS_ProgressCallback upcb,
3145 enum GNUNET_FS_Flags flags,
3148 struct GNUNET_FS_Handle *ret;
3149 enum GNUNET_FS_OPTIONS opt;
3152 ret = GNUNET_new (struct GNUNET_FS_Handle);
3154 ret->client_name = GNUNET_strdup (client_name);
3156 ret->upcb_cls = upcb_cls;
3158 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3159 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3160 ret->avg_block_latency =
3161 GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3162 va_start (ap, flags);
3163 while (GNUNET_FS_OPTIONS_END !=
3164 (opt = GNUNET_VA_ARG_ENUM (ap, GNUNET_FS_OPTIONS)))
3168 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3169 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3173 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3174 ret->max_parallel_requests = va_arg (ap, unsigned int);
3180 GNUNET_free (ret->client_name);
3187 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3189 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3190 &deserialize_publish_file,
3192 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3193 &deserialize_search_file,
3195 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3196 &deserialize_download_file,
3198 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3199 &deserialize_unindex_file,
3207 * Close our connection with the file-sharing service.
3208 * The callback given to #GNUNET_FS_start() will no longer be
3209 * called after this function returns.
3210 * This function MUST NOT be called from within the
3213 * @param h handle that was returned from #GNUNET_FS_start()
3216 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3218 while (NULL != h->top_head)
3219 h->top_head->ssf (h->top_head->ssf_cls);
3220 if (NULL != h->queue_job)
3221 GNUNET_SCHEDULER_cancel (h->queue_job);
3222 GNUNET_free (h->client_name);
3227 /* end of fs_api.c */