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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 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,
65 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
73 * Stop the given job (send signal, remove from active queue, update
74 * counters and state).
76 * @param qe job to stop
79 stop_job (struct GNUNET_FS_QueueEntry *qe)
81 qe->active = GNUNET_NO;
83 GNUNET_assert (0 < qe->h->active_downloads);
84 qe->h->active_downloads--;
85 qe->h->active_blocks -= qe->blocks;
87 GNUNET_TIME_relative_add (qe->run_time,
88 GNUNET_TIME_absolute_get_duration
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Stopping job %p (%u active)\n",
93 qe->h->active_downloads);
94 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
97 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
105 * Process the jobs in the job queue, possibly starting some
106 * and stopping others.
108 * @param cls the `struct GNUNET_FS_Handle *`
111 process_job_queue (void *cls)
113 struct GNUNET_FS_Handle *h = cls;
114 struct GNUNET_FS_QueueEntry *qe;
115 struct GNUNET_FS_QueueEntry *next;
116 struct GNUNET_TIME_Relative run_time;
117 struct GNUNET_TIME_Relative restart_at;
118 struct GNUNET_TIME_Relative rst;
119 struct GNUNET_TIME_Absolute end_time;
120 unsigned int num_downloads_waiting;
121 unsigned int num_downloads_active;
122 unsigned int num_downloads_expired;
123 unsigned int num_probes_active;
124 unsigned int num_probes_waiting;
125 unsigned int num_probes_expired;
126 int num_probes_change;
127 int num_downloads_change;
131 /* restart_at will be set to the time when it makes sense to
132 re-evaluate the job queue (unless, of course, jobs complete
133 or are added, then we'll be triggered immediately */
134 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
135 /* first, calculate some basic statistics on pending jobs */
136 num_probes_waiting = 0;
137 num_downloads_waiting = 0;
138 for (qe = h->pending_head; NULL != qe; qe = qe->next)
140 switch (qe->priority)
142 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
143 num_probes_waiting++;
145 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
146 num_downloads_waiting++;
153 /* now, calculate some basic statistics on running jobs */
154 num_probes_active = 0;
155 num_probes_expired = 0;
156 num_downloads_active = 0;
157 num_downloads_expired = 0;
158 next = h->running_head;
159 while (NULL != (qe = next))
162 switch (qe->priority)
164 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
165 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
166 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
167 rst = GNUNET_TIME_absolute_get_remaining (end_time);
168 if (0 == rst.rel_value_us)
170 num_probes_expired++;
176 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
179 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
181 GNUNET_TIME_relative_multiply (h->avg_block_latency,
182 qe->blocks * qe->start_times);
183 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
184 rst = GNUNET_TIME_absolute_get_remaining (end_time);
185 if (0 == rst.rel_value_us)
187 num_downloads_expired++;
192 num_downloads_active++;
193 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
201 GNUNET_break (h->active_downloads ==
202 num_downloads_active + num_probes_active);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
208 num_downloads_active,
209 num_downloads_expired,
210 num_downloads_waiting);
211 GNUNET_break (h->active_downloads + num_probes_active <=
212 h->max_parallel_downloads);
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 (1 + h->max_parallel_downloads / 4 >= (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;
234 GNUNET_break (num_downloads_change <= num_downloads_waiting);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Changing %d probes and %d/%u/%u downloads\n",
238 num_downloads_change,
239 (unsigned int) h->active_downloads,
240 (unsigned int) h->max_parallel_downloads);
241 /* actually stop probes */
242 next = h->running_head;
243 while (NULL != (qe = next))
246 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
248 if (num_probes_change < 0)
252 if (0 == num_probes_change)
256 GNUNET_break (0 <= num_probes_change);
258 /* start some more tasks if we now have empty slots */
259 block_limit_hit = GNUNET_NO;
260 next = h->pending_head;
261 while ( (NULL != (qe = next)) &&
262 ( (num_probes_change > 0) ||
263 (num_downloads_change > 0) ) )
266 switch (qe->priority)
268 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
269 if (num_probes_change > 0)
273 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
274 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
277 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
278 if ( (num_downloads_change > 0) &&
279 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
280 ( (qe->blocks > h->max_parallel_requests) &&
281 (0 == h->active_downloads) ) ) )
284 num_downloads_change--;
286 else if (num_downloads_change > 0)
287 block_limit_hit = GNUNET_YES;
294 GNUNET_break ( (0 == num_downloads_change) ||
295 (GNUNET_YES == block_limit_hit) );
296 GNUNET_break (0 == num_probes_change);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
301 h->max_parallel_requests,
303 num_downloads_change,
304 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
306 /* make sure we run again, callbacks might have
307 already re-scheduled the job, so cancel such
308 an operation (if it exists) */
309 if (NULL != h->queue_job)
310 GNUNET_SCHEDULER_cancel (h->queue_job);
312 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
317 * Add a job to the queue.
319 * @param h handle to the overall FS state
320 * @param start function to call to begin the job
321 * @param stop function to call to pause the job, or on dequeue (if the job was running)
322 * @param cls closure for start and stop
323 * @param blocks number of blocks this jobs uses
324 * @param priority how important is this download
325 * @return queue handle
327 struct GNUNET_FS_QueueEntry *
328 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
329 GNUNET_SCHEDULER_TaskCallback start,
330 GNUNET_SCHEDULER_TaskCallback stop,
333 enum GNUNET_FS_QueuePriority priority)
335 struct GNUNET_FS_QueueEntry *qe;
337 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
342 qe->queue_time = GNUNET_TIME_absolute_get ();
344 qe->priority = priority;
345 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
346 h->pending_tail, qe);
347 if (NULL != h->queue_job)
348 GNUNET_SCHEDULER_cancel (h->queue_job);
349 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 * Dequeue a job from the queue.
360 * @param qe handle for the job
363 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
365 struct GNUNET_FS_Handle *h;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Dequeueing job %p\n",
371 if (GNUNET_YES == qe->active)
373 GNUNET_CONTAINER_DLL_remove (h->pending_head,
377 if (NULL != h->queue_job)
378 GNUNET_SCHEDULER_cancel (h->queue_job);
379 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue,
385 * Create a top-level activity entry.
387 * @param h global fs handle
388 * @param ssf suspend signal function to use
389 * @param ssf_cls closure for @a ssf
390 * @return fresh top-level activity handle
392 struct TopLevelActivity *
393 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
394 SuspendSignalFunction ssf,
397 struct TopLevelActivity *ret;
399 ret = GNUNET_new (struct TopLevelActivity);
401 ret->ssf_cls = ssf_cls;
402 GNUNET_CONTAINER_DLL_insert (h->top_head,
410 * Destroy a top-level activity entry.
412 * @param h global fs handle
413 * @param top top level activity entry
416 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
417 struct TopLevelActivity *top)
419 GNUNET_CONTAINER_DLL_remove (h->top_head,
427 * Closure for #GNUNET_FS_data_reader_file_().
432 * Name of the file to read.
437 * File descriptor, NULL if it has not yet been opened.
439 struct GNUNET_DISK_FileHandle *fd;
444 * Function that provides data by reading from a file.
446 * @param cls closure with the `struct FileInfo *`
447 * @param offset offset to read from; it is possible
448 * that the caller might need to go backwards
449 * a bit at times; set to `UINT64_MAX` to tell
450 * the reader that we won't be reading for a while
451 * (used to close the file descriptor but NOT fully
452 * clean up the reader's state); in this case,
453 * a value of '0' for @a max should be ignored
454 * @param max maximum number of bytes that should be
455 * copied to @a buf; readers are not allowed
456 * to provide less data unless there is an error;
457 * a value of "0" will be used at the end to allow
458 * the reader to clean up its internal state
459 * @param buf where the reader should write the data
460 * @param emsg location for the reader to store an error message
461 * @return number of bytes written, usually @a max, 0 on error
464 GNUNET_FS_data_reader_file_ (void *cls,
470 struct FileInfo *fi = cls;
473 if (UINT64_MAX == offset)
477 GNUNET_DISK_file_close (fi->fd);
485 GNUNET_DISK_file_close (fi->fd);
486 GNUNET_free (fi->filename);
493 GNUNET_DISK_file_open (fi->filename,
494 GNUNET_DISK_OPEN_READ,
495 GNUNET_DISK_PERM_NONE);
498 GNUNET_asprintf (emsg,
499 _("Could not open file `%s': %s"),
505 if ( (GNUNET_SYSERR ==
506 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
507 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
509 GNUNET_asprintf (emsg,
510 _("Could not read file `%s': %s"),
517 GNUNET_asprintf (emsg,
518 _("Short read reading from file `%s'!"),
527 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
529 * @param filename file to read
530 * @return closure to use, NULL on error
533 GNUNET_FS_make_file_reader_context_ (const char *filename)
537 fi = GNUNET_new (struct FileInfo);
538 fi->filename = GNUNET_STRINGS_filename_expand (filename);
539 if (NULL == fi->filename)
549 * Function that provides data by copying from a buffer.
551 * @param cls closure (points to the buffer)
552 * @param offset offset to read from; it is possible
553 * that the caller might need to go backwards
554 * a bit at times; set to `UINT64_MAX` to tell
555 * the reader that we won't be reading for a while
556 * (used to close the file descriptor but NOT fully
557 * clean up the reader's state); in this case,
558 * a value of '0' for @a max should be ignored
559 * @param max maximum number of bytes that should be
560 * copied to @a buf; readers are not allowed
561 * to provide less data unless there is an error;
562 * a value of "0" will be used at the end to allow
563 * the reader to clean up its internal state
564 * @param buf where the reader should write the data
565 * @param emsg location for the reader to store an error message
566 * @return number of bytes written, usually @a max, 0 on error
569 GNUNET_FS_data_reader_copy_ (void *cls,
577 if (UINT64_MAX == offset)
581 GNUNET_free_non_null (data);
584 GNUNET_memcpy (buf, &data[offset], max);
590 * Return the full filename where we would store state information
591 * (for serialization/deserialization).
593 * @param h master context
594 * @param ext component of the path
595 * @param ent entity identifier (or emtpy string for the directory)
596 * @return NULL on error
599 get_serialization_file_name (struct GNUNET_FS_Handle *h,
606 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
607 return NULL; /* persistence not requested */
609 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
612 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
613 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
615 GNUNET_free (basename);
621 * Return the full filename where we would store state information
622 * (for serialization/deserialization) that is associated with a
625 * @param h master context
626 * @param ext component of the path
627 * @param uni name of the parent operation
628 * @param ent entity identifier (or emtpy string for the directory)
629 * @return NULL on error
632 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
640 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
641 return NULL; /* persistence not requested */
643 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
646 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
647 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
648 uni, DIR_SEPARATOR_STR, ent);
649 GNUNET_free (basename);
655 * Return a read handle for deserialization.
657 * @param h master context
658 * @param ext component of the path
659 * @param ent entity identifier (or emtpy string for the directory)
660 * @return NULL on error
662 static struct GNUNET_BIO_ReadHandle *
663 get_read_handle (struct GNUNET_FS_Handle *h,
668 struct GNUNET_BIO_ReadHandle *ret;
670 fn = get_serialization_file_name (h, ext, ent);
673 ret = GNUNET_BIO_read_open (fn);
680 * Return a write handle for serialization.
682 * @param h master context
683 * @param ext component of the path
684 * @param ent entity identifier (or emtpy string for the directory)
685 * @return NULL on error
687 static struct GNUNET_BIO_WriteHandle *
688 get_write_handle (struct GNUNET_FS_Handle *h,
693 struct GNUNET_BIO_WriteHandle *ret;
695 fn = get_serialization_file_name (h, ext, ent);
698 ret = GNUNET_BIO_write_open (fn);
699 GNUNET_break (NULL != ret);
706 * Return a write handle for serialization.
708 * @param h master context
709 * @param ext component of the path
710 * @param uni name of parent
711 * @param ent entity identifier (or emtpy string for the directory)
712 * @return NULL on error
714 static struct GNUNET_BIO_WriteHandle *
715 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
716 const char *uni, const char *ent)
719 struct GNUNET_BIO_WriteHandle *ret;
721 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
724 ret = GNUNET_BIO_write_open (fn);
731 * Remove serialization/deserialization file from disk.
733 * @param h master context
734 * @param ext component of the path
735 * @param ent entity identifier
738 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
744 if ((NULL == ent) || (0 == strlen (ent)))
749 filename = get_serialization_file_name (h, ext, ent);
750 if (NULL != filename)
752 if ( (0 != UNLINK (filename)) &&
754 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
755 GNUNET_free (filename);
761 * Remove serialization/deserialization file from disk.
763 * @param h master context
764 * @param ext component of the path
765 * @param uni parent name
766 * @param ent entity identifier
769 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
771 const char *uni, const char *ent)
775 if ((NULL == ent) || (0 == strlen (ent)))
780 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
781 if (NULL == filename)
783 if (0 != UNLINK (filename))
784 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
785 GNUNET_free (filename);
790 * Remove serialization/deserialization directory from disk.
792 * @param h master context
793 * @param ext component of the path
794 * @param uni unique name of parent
797 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
805 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
808 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
809 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
810 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
816 * Serialize a start-time. Since we use start-times to
817 * calculate the duration of some operation, we actually
818 * do not serialize the absolute time but the (relative)
819 * duration since the start time. When we then
820 * deserialize the start time, we take the current time and
821 * subtract that duration so that we get again an absolute
822 * time stamp that will result in correct performance
825 * @param wh handle for writing
826 * @param timestamp time to serialize
827 * @return #GNUNET_OK on success
830 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
831 struct GNUNET_TIME_Absolute timestamp)
833 struct GNUNET_TIME_Relative dur;
835 dur = GNUNET_TIME_absolute_get_duration (timestamp);
836 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
841 * Deserialize a start-time. Since we use start-times to
842 * calculate the duration of some operation, we actually
843 * do not serialize the absolute time but the (relative)
844 * duration since the start time. Thus, when we then
845 * deserialize the start time, we take the current time and
846 * subtract that duration so that we get again an absolute
847 * time stamp that will result in correct performance
850 * @param rh handle for reading
851 * @param timestamp where to write the deserialized timestamp
852 * @return #GNUNET_OK on success
855 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
856 struct GNUNET_TIME_Absolute *timestamp)
858 struct GNUNET_TIME_Relative dur;
860 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
861 return GNUNET_SYSERR;
862 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
868 * Using the given serialization filename, try to deserialize
869 * the file-information tree associated with it.
871 * @param h master context
872 * @param filename name of the file (without directory) with
874 * @return NULL on error
876 static struct GNUNET_FS_FileInformation *
877 deserialize_file_information (struct GNUNET_FS_Handle *h,
878 const char *filename);
882 * Using the given serialization filename, try to deserialize
883 * the file-information tree associated with it.
885 * @param h master context
886 * @param fn name of the file (without directory) with
888 * @param rh handle for reading
889 * @return NULL on error
891 static struct GNUNET_FS_FileInformation *
892 deserialize_fi_node (struct GNUNET_FS_Handle *h,
894 struct GNUNET_BIO_ReadHandle *rh)
896 struct GNUNET_FS_FileInformation *ret;
897 struct GNUNET_FS_FileInformation *nxt;
905 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
910 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
916 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
917 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
919 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
920 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
921 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
923 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
924 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
925 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
927 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
928 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
929 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
930 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
932 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
934 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
935 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
936 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
937 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
944 case 0: /* file-insert */
945 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
950 ret->is_directory = GNUNET_NO;
951 ret->data.file.do_index = GNUNET_NO;
952 ret->data.file.have_hash = GNUNET_NO;
953 ret->data.file.index_start_confirmed = GNUNET_NO;
954 if (GNUNET_NO == ret->is_published)
956 if (NULL == ret->filename)
958 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
959 ret->data.file.reader_cls =
960 GNUNET_malloc_large (ret->data.file.file_size);
961 if (ret->data.file.reader_cls == NULL)
964 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
965 ret->data.file.file_size))
973 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
974 ret->data.file.reader_cls =
975 GNUNET_FS_make_file_reader_context_ (ret->filename);
979 case 1: /* file-index, no hash */
980 if (NULL == ret->filename)
985 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
990 ret->is_directory = GNUNET_NO;
991 ret->data.file.do_index = GNUNET_YES;
992 ret->data.file.have_hash = GNUNET_NO;
993 ret->data.file.index_start_confirmed = GNUNET_NO;
994 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
995 ret->data.file.reader_cls =
996 GNUNET_FS_make_file_reader_context_ (ret->filename);
998 case 2: /* file-index-with-hash */
999 if (NULL == ret->filename)
1004 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1006 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1007 sizeof (struct GNUNET_HashCode))))
1012 ret->is_directory = GNUNET_NO;
1013 ret->data.file.do_index = GNUNET_YES;
1014 ret->data.file.have_hash = GNUNET_YES;
1015 ret->data.file.index_start_confirmed = GNUNET_NO;
1016 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1017 ret->data.file.reader_cls =
1018 GNUNET_FS_make_file_reader_context_ (ret->filename);
1020 case 3: /* file-index-with-hash-confirmed */
1021 if (NULL == ret->filename)
1026 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1028 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1029 sizeof (struct GNUNET_HashCode))))
1034 ret->is_directory = GNUNET_NO;
1035 ret->data.file.do_index = GNUNET_YES;
1036 ret->data.file.have_hash = GNUNET_YES;
1037 ret->data.file.index_start_confirmed = GNUNET_YES;
1038 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1039 ret->data.file.reader_cls =
1040 GNUNET_FS_make_file_reader_context_ (ret->filename);
1042 case 4: /* directory */
1043 ret->is_directory = GNUNET_YES;
1044 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1045 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1046 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1047 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1049 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1051 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1056 ret->data.dir.dir_size = (uint32_t) dsize;
1057 if (NULL != filename)
1059 ret->data.dir.entries = deserialize_file_information (h, filename);
1060 GNUNET_free (filename);
1062 nxt = ret->data.dir.entries;
1074 ret->serialization = GNUNET_strdup (fn);
1076 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1081 if (NULL != filename)
1083 ret->next = deserialize_file_information (h, filename);
1084 GNUNET_free (filename);
1087 GNUNET_free_non_null (ksks);
1088 GNUNET_free_non_null (skss);
1089 GNUNET_free_non_null (chks);
1092 GNUNET_free_non_null (ksks);
1093 GNUNET_free_non_null (chks);
1094 GNUNET_free_non_null (skss);
1095 GNUNET_free_non_null (filename);
1096 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1102 * Using the given serialization filename, try to deserialize
1103 * the file-information tree associated with it.
1105 * @param h master context
1106 * @param filename name of the file (without directory) with
1108 * @return NULL on error
1110 static struct GNUNET_FS_FileInformation *
1111 deserialize_file_information (struct GNUNET_FS_Handle *h,
1112 const char *filename)
1114 struct GNUNET_FS_FileInformation *ret;
1115 struct GNUNET_BIO_ReadHandle *rh;
1119 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1122 ret = deserialize_fi_node (h, filename, rh);
1123 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1126 _("Failed to resume publishing information `%s': %s\n"),
1132 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1135 if (0 != UNLINK (fn))
1136 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1145 * Given a serialization name (full absolute path), return the
1146 * basename of the file (without the path), which must only
1147 * consist of the 6 random characters.
1149 * @param fullname name to extract the basename from
1150 * @return copy of the basename, NULL on error
1153 get_serialization_short_name (const char *fullname)
1160 /* FIXME: we could do this faster since we know
1161 * the length of 'end'... */
1162 while ('\0' != *nxt)
1164 if (DIR_SEPARATOR == *nxt)
1168 if ((NULL == end) || (0 == strlen (end)))
1173 GNUNET_break (6 == strlen (end));
1174 return GNUNET_strdup (end);
1179 * Create a new random name for serialization. Also checks if persistence
1180 * is enabled and returns NULL if not.
1182 * @param h master context
1183 * @param ext component of the path
1184 * @return NULL on errror
1187 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1194 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1195 return NULL; /* persistence not requested */
1196 dn = get_serialization_file_name (h, ext, "");
1199 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1204 fn = GNUNET_DISK_mktemp (dn);
1207 return NULL; /* epic fail */
1208 ret = get_serialization_short_name (fn);
1215 * Create a new random name for serialization. Also checks if persistence
1216 * is enabled and returns NULL if not.
1218 * @param h master context
1219 * @param ext component of the path
1220 * @param uni name of parent
1221 * @return NULL on errror
1224 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1232 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1233 return NULL; /* persistence not requested */
1234 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1237 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1242 fn = GNUNET_DISK_mktemp (dn);
1245 return NULL; /* epic fail */
1246 ret = get_serialization_short_name (fn);
1253 * Copy all of the data from the reader to the write handle.
1255 * @param wh write handle
1256 * @param fi file with reader
1257 * @return #GNUNET_OK on success
1260 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1261 struct GNUNET_FS_FileInformation *fi)
1263 char buf[32 * 1024];
1271 while (off < fi->data.file.file_size)
1273 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1275 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1279 return GNUNET_SYSERR;
1281 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1282 return GNUNET_SYSERR;
1290 * Create a temporary file on disk to store the current
1291 * state of @a fi in.
1293 * @param fi file information to sync with disk
1296 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1299 struct GNUNET_BIO_WriteHandle *wh;
1305 if (NULL == fi->serialization)
1307 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1308 if (NULL == fi->serialization)
1310 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1314 GNUNET_free (fi->serialization);
1315 fi->serialization = NULL;
1318 if (GNUNET_YES == fi->is_directory)
1320 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1322 else if (GNUNET_YES == fi->data.file.have_hash)
1324 else if (GNUNET_YES == fi->data.file.do_index)
1328 if (NULL != fi->keywords)
1329 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1332 if (NULL != fi->chk_uri)
1333 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1336 if (NULL != fi->sks_uri)
1337 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1340 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1341 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1342 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1343 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1344 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1345 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1346 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1347 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1349 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1350 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1351 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1352 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1357 GNUNET_free_non_null (chks);
1359 GNUNET_free_non_null (ksks);
1361 GNUNET_free_non_null (skss);
1366 case 0: /* file-insert */
1367 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1372 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1373 if (GNUNET_OK != copy_from_reader (wh, fi))
1379 case 1: /* file-index, no hash */
1380 if (NULL == fi->filename)
1385 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1391 case 2: /* file-index-with-hash */
1392 case 3: /* file-index-with-hash-confirmed */
1393 if (NULL == fi->filename)
1398 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1400 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1401 sizeof (struct GNUNET_HashCode))))
1407 case 4: /* directory */
1408 if ( (NULL != fi->data.dir.entries) &&
1409 (NULL == fi->data.dir.entries->serialization) )
1410 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1411 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1412 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1413 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1415 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1416 (uint32_t) fi->data.dir.dir_size)) ||
1418 GNUNET_BIO_write_string (wh,
1419 (fi->data.dir.entries ==
1420 NULL) ? NULL : fi->data.dir.
1421 entries->serialization)))
1431 if ( (NULL != fi->next) &&
1432 (NULL == fi->next->serialization) )
1433 GNUNET_FS_file_information_sync_ (fi->next);
1435 GNUNET_BIO_write_string (wh,
1437 NULL) ? fi->next->serialization : NULL))
1442 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1451 (void) GNUNET_BIO_write_close (wh);
1452 GNUNET_free_non_null (chks);
1453 GNUNET_free_non_null (ksks);
1454 GNUNET_free_non_null (skss);
1455 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1459 if (0 != UNLINK (fn))
1460 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1463 GNUNET_free (fi->serialization);
1464 fi->serialization = NULL;
1470 * Find the entry in the file information struct where the
1471 * serialization filename matches the given name.
1473 * @param pos file information to search
1474 * @param srch filename to search for
1475 * @return NULL if srch was not found in this subtree
1477 static struct GNUNET_FS_FileInformation *
1478 find_file_position (struct GNUNET_FS_FileInformation *pos,
1481 struct GNUNET_FS_FileInformation *r;
1485 if (0 == strcmp (srch, pos->serialization))
1487 if ( (GNUNET_YES == pos->is_directory) &&
1488 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1497 * Signal the FS's progress function that we are resuming
1500 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1501 * @param fi the entry in the publish-structure
1502 * @param length length of the file or directory
1503 * @param meta metadata for the file or directory (can be modified)
1504 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1505 * @param bo block options (can be modified)
1506 * @param do_index should we index?
1507 * @param client_info pointer to client context set upon creation (can be modified)
1508 * @return #GNUNET_OK to continue (always)
1511 fip_signal_resume (void *cls,
1512 struct GNUNET_FS_FileInformation *fi,
1514 struct GNUNET_CONTAINER_MetaData *meta,
1515 struct GNUNET_FS_Uri **uri,
1516 struct GNUNET_FS_BlockOptions *bo,
1520 struct GNUNET_FS_PublishContext *pc = cls;
1521 struct GNUNET_FS_ProgressInfo pi;
1523 if (GNUNET_YES == pc->skip_next_fi_callback)
1525 pc->skip_next_fi_callback = GNUNET_NO;
1528 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1529 pi.value.publish.specifics.resume.message = fi->emsg;
1530 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1531 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1532 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1534 /* process entries in directory */
1535 pc->skip_next_fi_callback = GNUNET_YES;
1536 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1543 * Function called with a filename of serialized publishing operation
1546 * @param cls the `struct GNUNET_FS_Handle *`
1547 * @param filename complete filename (absolute path)
1548 * @return #GNUNET_OK (continue to iterate)
1551 deserialize_publish_file (void *cls,
1552 const char *filename)
1554 struct GNUNET_FS_Handle *h = cls;
1555 struct GNUNET_BIO_ReadHandle *rh;
1556 struct GNUNET_FS_PublishContext *pc;
1561 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1565 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1567 pc->serialization = get_serialization_short_name (filename);
1570 rh = GNUNET_BIO_read_open (filename);
1576 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1578 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1579 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1580 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1581 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1583 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1584 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1585 || ( (GNUNET_YES == have_ns) &&
1586 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1591 pc->options = options;
1592 pc->all_done = all_done;
1593 if (NULL == fi_root)
1598 pc->fi = deserialize_file_information (h, fi_root);
1604 if (GNUNET_YES == have_ns)
1606 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1609 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1610 (GNUNET_YES != pc->all_done))
1612 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1613 if (NULL == pc->dsh)
1618 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1619 GNUNET_free (fi_pos);
1621 if (NULL == pc->fi_pos)
1623 /* failed to find position for resuming, outch! Will start from root! */
1625 if (GNUNET_YES != pc->all_done)
1626 pc->fi_pos = pc->fi;
1629 GNUNET_free (fi_root);
1631 /* generate RESUME event(s) */
1632 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1634 /* re-start publishing (if needed)... */
1635 if (GNUNET_YES != pc->all_done)
1637 GNUNET_assert (NULL == pc->upload_task);
1639 GNUNET_SCHEDULER_add_with_priority
1640 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1641 &GNUNET_FS_publish_main_, pc);
1643 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1646 _("Failure while resuming publishing operation `%s': %s\n"),
1650 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1653 GNUNET_free_non_null (pc->nid);
1654 GNUNET_free_non_null (pc->nuid);
1655 GNUNET_free_non_null (fi_root);
1656 GNUNET_free_non_null (fi_pos);
1657 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1660 _("Failed to resume publishing operation `%s': %s\n"), filename,
1665 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1666 if (0 != UNLINK (filename))
1667 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1668 GNUNET_free (pc->serialization);
1675 * Synchronize this publishing struct with its mirror
1676 * on disk. Note that all internal FS-operations that change
1677 * publishing structs should already call "sync" internally,
1678 * so this function is likely not useful for clients.
1680 * @param pc the struct to sync
1683 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1685 struct GNUNET_BIO_WriteHandle *wh;
1688 if (NULL == pc->serialization)
1690 make_serialization_file_name (pc->h,
1691 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1692 if (NULL == pc->serialization)
1696 if (NULL == pc->fi->serialization)
1701 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1708 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1709 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1710 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1711 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1712 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1713 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1714 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1716 GNUNET_BIO_write_string (wh,
1717 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1718 ( (NULL != pc->ns) &&
1719 (GNUNET_OK != GNUNET_BIO_write (wh,
1721 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1726 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1735 (void) GNUNET_BIO_write_close (wh);
1736 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1738 GNUNET_free (pc->serialization);
1739 pc->serialization = NULL;
1744 * Synchronize this unindex struct with its mirror
1745 * on disk. Note that all internal FS-operations that change
1746 * publishing structs should already call "sync" internally,
1747 * so this function is likely not useful for clients.
1749 * @param uc the struct to sync
1752 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1754 struct GNUNET_BIO_WriteHandle *wh;
1757 if (NULL == uc->serialization)
1759 make_serialization_file_name (uc->h,
1760 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1761 if (NULL == uc->serialization)
1763 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1770 if (NULL != uc->ksk_uri)
1771 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1774 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1775 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1776 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1777 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1779 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1780 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1781 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1782 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1784 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1785 ((uc->state == UNINDEX_STATE_ERROR) &&
1786 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1791 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1800 (void) GNUNET_BIO_write_close (wh);
1801 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1803 GNUNET_free (uc->serialization);
1804 uc->serialization = NULL;
1809 * Serialize a download request.
1811 * @param wh handle for writing the download request to disk
1812 * @param dr the the request to write to disk
1813 * @return #GNUNET_YES on success, #GNUNET_NO on error
1816 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1817 struct DownloadRequest *dr)
1821 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1822 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1823 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1824 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1826 if ((BRS_CHK_SET == dr->state) &&
1828 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1830 for (i = 0; i < dr->num_children; i++)
1831 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1838 * Read a download request tree.
1840 * @param rh cadet to read from
1841 * @return value the download request read from disk, NULL on error
1843 static struct DownloadRequest *
1844 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1846 struct DownloadRequest *dr;
1849 dr = GNUNET_new (struct DownloadRequest);
1850 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1851 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1852 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1853 (dr->num_children > CHK_PER_INODE) ||
1854 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1855 ( (0 == dr->depth) &&
1856 (dr->num_children > 0) ) ||
1857 ((dr->depth > 0) && (0 == dr->num_children)))
1860 dr->num_children = 0;
1863 if (dr->num_children > 0)
1865 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1869 case BRS_RECONSTRUCT_DOWN:
1870 case BRS_RECONSTRUCT_META_UP:
1871 case BRS_RECONSTRUCT_UP:
1875 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1878 case BRS_DOWNLOAD_DOWN:
1879 case BRS_DOWNLOAD_UP:
1886 for (i = 0; i < dr->num_children; i++)
1888 if (NULL == (dr->children[i] = read_download_request (rh)))
1890 dr->children[i]->parent = dr;
1894 GNUNET_FS_free_download_request_ (dr);
1900 * Compute the name of the sync file (or directory) for the given download
1903 * @param dc download context to compute for
1904 * @param uni unique filename to use, use "" for the directory name
1905 * @param ext extension to use, use ".dir" for our own subdirectory
1906 * @return the expanded file name, NULL for none
1909 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1916 if (dc->parent == NULL)
1917 return get_serialization_file_name (dc->h,
1918 (dc->search != NULL) ?
1919 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1920 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1922 if (NULL == dc->parent->serialization)
1924 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1927 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1934 * Synchronize this download struct with its mirror
1935 * on disk. Note that all internal FS-operations that change
1936 * publishing structs should already call "sync" internally,
1937 * so this function is likely not useful for clients.
1939 * @param dc the struct to sync
1942 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1944 struct GNUNET_BIO_WriteHandle *wh;
1949 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1950 return; /* we don't sync probes */
1951 if (NULL == dc->serialization)
1953 dir = get_download_sync_filename (dc, "", "");
1956 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1961 fn = GNUNET_DISK_mktemp (dir);
1965 dc->serialization = get_serialization_short_name (fn);
1969 fn = get_download_sync_filename (dc, dc->serialization, "");
1972 GNUNET_free (dc->serialization);
1973 dc->serialization = NULL;
1978 wh = GNUNET_BIO_write_open (fn);
1981 GNUNET_free (dc->serialization);
1982 dc->serialization = NULL;
1986 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1987 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1988 uris = GNUNET_FS_uri_to_string (dc->uri);
1989 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1990 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1991 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1992 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1993 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1994 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1995 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1996 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1997 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1998 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1999 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
2000 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
2001 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
2006 if (NULL == dc->emsg)
2008 GNUNET_assert (dc->top_request != NULL);
2009 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2015 GNUNET_free_non_null (uris);
2017 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2027 (void) GNUNET_BIO_write_close (wh);
2028 GNUNET_free_non_null (uris);
2029 if (0 != UNLINK (fn))
2030 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2032 GNUNET_free (dc->serialization);
2033 dc->serialization = NULL;
2038 * Synchronize this search result with its mirror
2039 * on disk. Note that all internal FS-operations that change
2040 * publishing structs should already call "sync" internally,
2041 * so this function is likely not useful for clients.
2043 * @param sr the struct to sync
2046 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2048 struct GNUNET_BIO_WriteHandle *wh;
2054 if (NULL == sr->serialization)
2056 make_serialization_file_name_in_dir (sr->h,
2057 (sr->sc->psearch_result ==
2059 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2060 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2061 sr->sc->serialization);
2062 if (NULL == sr->serialization)
2064 wh = get_write_handle_in_dir (sr->h,
2065 (sr->sc->psearch_result ==
2066 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2067 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2068 sr->sc->serialization, sr->serialization);
2074 uris = GNUNET_FS_uri_to_string (sr->uri);
2075 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2077 GNUNET_BIO_write_string (wh,
2079 NULL ? sr->download->serialization : NULL)) ||
2081 GNUNET_BIO_write_string (wh,
2082 sr->update_search !=
2083 NULL ? sr->update_search->serialization : NULL))
2084 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2085 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2086 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2087 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2088 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2089 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2094 if ( (NULL != sr->uri) &&
2095 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2096 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2097 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2102 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2108 GNUNET_free_non_null (uris);
2111 GNUNET_free_non_null (uris);
2113 (void) GNUNET_BIO_write_close (wh);
2114 remove_sync_file_in_dir (sr->h,
2115 (NULL == sr->sc->psearch_result)
2116 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2117 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2118 sr->sc->serialization, sr->serialization);
2119 GNUNET_free (sr->serialization);
2120 sr->serialization = NULL;
2125 * Synchronize this search struct with its mirror
2126 * on disk. Note that all internal FS-operations that change
2127 * publishing structs should already call "sync" internally,
2128 * so this function is likely not useful for clients.
2130 * @param sc the struct to sync
2133 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2135 struct GNUNET_BIO_WriteHandle *wh;
2138 const char *category;
2141 (NULL == sc->psearch_result)
2142 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2143 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2144 if (NULL == sc->serialization)
2145 sc->serialization = make_serialization_file_name (sc->h, category);
2146 if (NULL == sc->serialization)
2149 wh = get_write_handle (sc->h, category, sc->serialization);
2155 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2156 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2157 uris = GNUNET_FS_uri_to_string (sc->uri);
2158 in_pause = (sc->task != NULL) ? 'r' : '\0';
2159 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2160 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2161 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2162 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2163 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2164 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2171 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2180 (void) GNUNET_BIO_write_close (wh);
2181 GNUNET_free_non_null (uris);
2182 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2183 GNUNET_free (sc->serialization);
2184 sc->serialization = NULL;
2189 * Function called with a filename of serialized unindexing operation
2192 * @param cls the `struct GNUNET_FS_Handle *`
2193 * @param filename complete filename (absolute path)
2194 * @return #GNUNET_OK (continue to iterate)
2197 deserialize_unindex_file (void *cls,
2198 const char *filename)
2200 struct GNUNET_FS_Handle *h = cls;
2201 struct GNUNET_BIO_ReadHandle *rh;
2202 struct GNUNET_FS_UnindexContext *uc;
2203 struct GNUNET_FS_ProgressInfo pi;
2208 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2210 uc->serialization = get_serialization_short_name (filename);
2211 rh = GNUNET_BIO_read_open (filename);
2219 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2220 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2221 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2222 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2223 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2224 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2225 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2227 GNUNET_free_non_null (uris);
2233 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2235 if (NULL == uc->ksk_uri)
2238 GNUNET_free_non_null (emsg);
2242 if ( (uc->ksk_offset > 0) &&
2243 ( (NULL == uc->ksk_uri) ||
2244 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2249 uc->state = (enum UnindexState) state;
2252 case UNINDEX_STATE_HASHING:
2254 case UNINDEX_STATE_FS_NOTIFY:
2256 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2257 sizeof (struct GNUNET_HashCode)))
2263 case UNINDEX_STATE_DS_REMOVE:
2264 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2265 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2267 case UNINDEX_STATE_COMPLETE:
2269 case UNINDEX_STATE_ERROR:
2271 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2281 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2282 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2283 pi.value.unindex.specifics.resume.message = uc->emsg;
2284 GNUNET_FS_unindex_make_status_ (&pi, uc,
2286 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2289 case UNINDEX_STATE_HASHING:
2291 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2293 &GNUNET_FS_unindex_process_hash_, uc);
2295 case UNINDEX_STATE_FS_NOTIFY:
2296 uc->state = UNINDEX_STATE_HASHING;
2297 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2299 case UNINDEX_STATE_DS_REMOVE:
2300 GNUNET_FS_unindex_do_remove_ (uc);
2302 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2303 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2305 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2306 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2308 case UNINDEX_STATE_COMPLETE:
2309 case UNINDEX_STATE_ERROR:
2310 /* no need to resume any operation, we were done */
2315 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2318 _("Failure while resuming unindexing operation `%s': %s\n"),
2324 GNUNET_free_non_null (uc->filename);
2325 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2328 _("Failed to resume unindexing operation `%s': %s\n"),
2333 if (NULL != uc->serialization)
2334 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2336 GNUNET_free_non_null (uc->serialization);
2343 * Deserialize a download.
2345 * @param h overall context
2346 * @param rh file to deserialize from
2347 * @param parent parent download
2348 * @param search associated search
2349 * @param serialization name under which the search was serialized
2352 deserialize_download (struct GNUNET_FS_Handle *h,
2353 struct GNUNET_BIO_ReadHandle *rh,
2354 struct GNUNET_FS_DownloadContext *parent,
2355 struct GNUNET_FS_SearchResult *search,
2356 const char *serialization);
2360 * Deserialize a search.
2362 * @param h overall context
2363 * @param rh file to deserialize from
2364 * @param psearch_result parent search result
2365 * @param serialization name under which the search was serialized
2367 static struct GNUNET_FS_SearchContext *
2368 deserialize_search (struct GNUNET_FS_Handle *h,
2369 struct GNUNET_BIO_ReadHandle *rh,
2370 struct GNUNET_FS_SearchResult *psearch_result,
2371 const char *serialization);
2375 * Function called with a filename of serialized search result
2378 * @param cls the `struct GNUNET_FS_SearchContext *`
2379 * @param filename complete filename (absolute path)
2380 * @return #GNUNET_OK (continue to iterate)
2383 deserialize_search_result (void *cls,
2384 const char *filename)
2386 struct GNUNET_FS_SearchContext *sc = cls;
2392 struct GNUNET_BIO_ReadHandle *rh;
2393 struct GNUNET_BIO_ReadHandle *drh;
2394 struct GNUNET_FS_SearchResult *sr;
2396 ser = get_serialization_short_name (filename);
2397 rh = GNUNET_BIO_read_open (filename);
2402 remove_sync_file_in_dir (sc->h,
2403 (NULL == sc->psearch_result)
2404 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2405 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2406 sc->serialization, ser);
2415 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2418 sr->serialization = ser;
2419 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2420 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2421 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2423 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2424 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2426 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2427 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2428 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2429 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2430 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2435 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2437 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2438 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2440 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2447 if (NULL != download)
2449 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2452 deserialize_download (sc->h, drh, NULL, sr, download);
2453 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2456 _("Failed to resume sub-download `%s': %s\n"),
2462 GNUNET_free (download);
2464 if (NULL != update_srch)
2467 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2470 deserialize_search (sc->h, drh, sr, update_srch);
2471 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2474 _("Failed to resume sub-search `%s': %s\n"),
2480 GNUNET_free (update_srch);
2482 GNUNET_break (GNUNET_YES ==
2483 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2485 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2486 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2489 _("Failure while resuming search operation `%s': %s\n"),
2495 GNUNET_free_non_null (download);
2496 GNUNET_free_non_null (emsg);
2497 GNUNET_free_non_null (uris);
2498 GNUNET_free_non_null (update_srch);
2499 if (NULL != sr->uri)
2500 GNUNET_FS_uri_destroy (sr->uri);
2501 if (NULL != sr->meta)
2502 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2503 GNUNET_free (sr->serialization);
2505 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2508 _("Failure while resuming search operation `%s': %s\n"),
2517 * Send the 'resume' signal to the callback; also actually
2518 * resume the download (put it in the queue). Does this
2519 * recursively for the top-level download and all child
2522 * @param dc download to resume
2525 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2527 struct GNUNET_FS_DownloadContext *dcc;
2528 struct GNUNET_FS_ProgressInfo pi;
2530 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2531 pi.value.download.specifics.resume.meta = dc->meta;
2532 pi.value.download.specifics.resume.message = dc->emsg;
2533 GNUNET_FS_download_make_status_ (&pi, dc);
2534 dcc = dc->child_head;
2537 signal_download_resume (dcc);
2540 GNUNET_FS_download_start_downloading_ (dc);
2545 * Signal resuming of a search to our clients (for the
2546 * top level search and all sub-searches).
2548 * @param sc search being resumed
2551 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2555 * Iterator over search results signaling resume to the client for
2558 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2559 * @param key current key code
2560 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2561 * @return #GNUNET_YES (we should continue to iterate)
2564 signal_result_resume (void *cls,
2565 const struct GNUNET_HashCode *key,
2568 struct GNUNET_FS_SearchContext *sc = cls;
2569 struct GNUNET_FS_ProgressInfo pi;
2570 struct GNUNET_FS_SearchResult *sr = value;
2572 if (0 == sr->mandatory_missing)
2574 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2575 pi.value.search.specifics.resume_result.meta = sr->meta;
2576 pi.value.search.specifics.resume_result.uri = sr->uri;
2577 pi.value.search.specifics.resume_result.result = sr;
2578 pi.value.search.specifics.resume_result.availability_rank =
2579 2 * sr->availability_success - sr->availability_trials;
2580 pi.value.search.specifics.resume_result.availability_certainty =
2581 sr->availability_trials;
2582 pi.value.search.specifics.resume_result.applicability_rank =
2583 sr->optional_support;
2584 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2586 if (NULL != sr->download)
2588 signal_download_resume (sr->download);
2592 GNUNET_FS_search_start_probe_ (sr);
2594 if (NULL != sr->update_search)
2595 signal_search_resume (sr->update_search);
2601 * Free memory allocated by the search context and its children
2603 * @param sc search context to free
2606 free_search_context (struct GNUNET_FS_SearchContext *sc);
2610 * Iterator over search results freeing each.
2612 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2613 * @param key current key code
2614 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2615 * @return #GNUNET_YES (we should continue to iterate)
2618 free_result (void *cls,
2619 const struct GNUNET_HashCode *key,
2622 struct GNUNET_FS_SearchResult *sr = value;
2624 if (NULL != sr->update_search)
2626 free_search_context (sr->update_search);
2627 GNUNET_assert (NULL == sr->update_search);
2629 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2630 GNUNET_FS_uri_destroy (sr->uri);
2637 * Free memory allocated by the search context and its children
2639 * @param sc search context to free
2642 free_search_context (struct GNUNET_FS_SearchContext *sc)
2644 if (NULL != sc->serialization)
2646 GNUNET_FS_remove_sync_file_ (sc->h,
2647 (sc->psearch_result ==
2648 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2649 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2651 GNUNET_FS_remove_sync_dir_ (sc->h,
2652 (sc->psearch_result ==
2653 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2654 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2657 GNUNET_free_non_null (sc->serialization);
2658 GNUNET_free_non_null (sc->emsg);
2659 if (NULL != sc->uri)
2660 GNUNET_FS_uri_destroy (sc->uri);
2661 if (NULL != sc->master_result_map)
2663 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2665 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2672 * Function called with a filename of serialized sub-download
2675 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2676 * @param filename complete filename (absolute path)
2677 * @return #GNUNET_OK (continue to iterate)
2680 deserialize_subdownload (void *cls,
2681 const char *filename)
2683 struct GNUNET_FS_DownloadContext *parent = cls;
2686 struct GNUNET_BIO_ReadHandle *rh;
2688 ser = get_serialization_short_name (filename);
2689 rh = GNUNET_BIO_read_open (filename);
2692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2693 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2699 deserialize_download (parent->h, rh, parent, NULL, ser);
2700 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2703 _("Failed to resume sub-download `%s': %s\n"),
2714 * Free this download context and all of its descendants.
2715 * (only works during deserialization since not all possible
2716 * state it taken care of).
2718 * @param dc context to free
2721 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2723 struct GNUNET_FS_DownloadContext *dcc;
2725 if (NULL != dc->meta)
2726 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2727 if (NULL != dc->uri)
2728 GNUNET_FS_uri_destroy (dc->uri);
2729 GNUNET_free_non_null (dc->temp_filename);
2730 GNUNET_free_non_null (dc->emsg);
2731 GNUNET_free_non_null (dc->filename);
2732 GNUNET_free_non_null (dc->serialization);
2733 while (NULL != (dcc = dc->child_head))
2735 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2738 free_download_context (dcc);
2740 GNUNET_FS_free_download_request_ (dc->top_request);
2741 if (NULL != dc->active)
2742 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2748 * Deserialize a download.
2750 * @param h overall context
2751 * @param rh file to deserialize from
2752 * @param parent parent download
2753 * @param search associated search
2754 * @param serialization name under which the search was serialized
2757 deserialize_download (struct GNUNET_FS_Handle *h,
2758 struct GNUNET_BIO_ReadHandle *rh,
2759 struct GNUNET_FS_DownloadContext *parent,
2760 struct GNUNET_FS_SearchResult *search,
2761 const char *serialization)
2763 struct GNUNET_FS_DownloadContext *dc;
2772 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2773 dc->parent = parent;
2775 dc->serialization = GNUNET_strdup (serialization);
2777 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2778 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2779 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2780 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2781 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2783 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2785 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2787 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2789 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2790 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2791 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2792 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2793 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2794 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2795 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2796 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2801 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2803 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2804 dc->has_finished = (int) status;
2806 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2807 if (GNUNET_FS_uri_test_loc (dc->uri))
2808 GNUNET_assert (GNUNET_OK ==
2809 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2810 if (NULL == dc->emsg)
2812 dc->top_request = read_download_request (rh);
2813 if (NULL == dc->top_request)
2819 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2822 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2823 GNUNET_DISK_directory_scan (dn,
2824 &deserialize_subdownload,
2830 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2836 dc->search = search;
2837 search->download = dc;
2839 if ((NULL == parent) && (NULL == search))
2842 GNUNET_FS_make_top (dc->h,
2843 &GNUNET_FS_download_signal_suspend_,
2845 signal_download_resume (dc);
2848 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_,
2852 GNUNET_free_non_null (uris);
2853 GNUNET_free_non_null (emsg);
2854 free_download_context (dc);
2859 * Signal resuming of a search to our clients (for the
2860 * top level search and all sub-searches).
2862 * @param sc search being resumed
2865 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2867 struct GNUNET_FS_ProgressInfo pi;
2869 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2870 pi.value.search.specifics.resume.message = sc->emsg;
2871 pi.value.search.specifics.resume.is_paused =
2872 (NULL == sc->mq) ? GNUNET_YES : GNUNET_NO;
2873 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2874 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2875 &signal_result_resume, sc);
2881 * Deserialize a search.
2883 * @param h overall context
2884 * @param rh file to deserialize from
2885 * @param psearch_result parent search result
2886 * @param serialization name under which the search was serialized
2888 static struct GNUNET_FS_SearchContext *
2889 deserialize_search (struct GNUNET_FS_Handle *h,
2890 struct GNUNET_BIO_ReadHandle *rh,
2891 struct GNUNET_FS_SearchResult *psearch_result,
2892 const char *serialization)
2894 struct GNUNET_FS_SearchContext *sc;
2901 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2908 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2909 if (NULL != psearch_result)
2911 sc->psearch_result = psearch_result;
2912 psearch_result->update_search = sc;
2915 sc->serialization = GNUNET_strdup (serialization);
2917 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2918 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2919 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2920 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2921 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2923 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2924 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2926 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2927 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2932 sc->options = (enum GNUNET_FS_SearchOptions) options;
2933 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2934 dn = get_serialization_file_name_in_dir (h,
2935 (NULL == sc->psearch_result) ?
2936 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2937 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2938 sc->serialization, "");
2941 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2942 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2945 if (('\0' == in_pause) &&
2946 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2949 _("Could not resume running search, will resume as paused search\n"));
2951 signal_search_resume (sc);
2955 GNUNET_free_non_null (emsg);
2956 free_search_context (sc);
2957 GNUNET_free_non_null (uris);
2963 * Function called with a filename of serialized search operation
2966 * @param cls the `struct GNUNET_FS_Handle *`
2967 * @param filename complete filename (absolute path)
2968 * @return #GNUNET_OK (continue to iterate)
2971 deserialize_search_file (void *cls,
2972 const char *filename)
2974 struct GNUNET_FS_Handle *h = cls;
2977 struct GNUNET_BIO_ReadHandle *rh;
2978 struct GNUNET_FS_SearchContext *sc;
2981 if (0 != STAT (filename, &buf))
2983 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2986 if (S_ISDIR (buf.st_mode))
2987 return GNUNET_OK; /* skip directories */
2988 ser = get_serialization_short_name (filename);
2989 rh = GNUNET_BIO_read_open (filename);
2994 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2999 sc = deserialize_search (h, rh, NULL, ser);
3001 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
3003 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3005 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3006 _("Failure while resuming search operation `%s': %s\n"),
3015 * Function called with a filename of serialized download operation
3018 * @param cls the `struct GNUNET_FS_Handle *`
3019 * @param filename complete filename (absolute path)
3020 * @return #GNUNET_OK (continue to iterate)
3023 deserialize_download_file (void *cls, const char *filename)
3025 struct GNUNET_FS_Handle *h = cls;
3028 struct GNUNET_BIO_ReadHandle *rh;
3030 ser = get_serialization_short_name (filename);
3031 rh = GNUNET_BIO_read_open (filename);
3034 if (0 != UNLINK (filename))
3035 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3039 deserialize_download (h, rh, NULL, NULL, ser);
3041 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3044 _("Failure while resuming download operation `%s': %s\n"),
3053 * Deserialize informatin about pending operations.
3055 * @param master_path which master directory should be scanned
3056 * @param proc function to call for each entry (will get @a h for 'cls')
3057 * @param h the `struct GNUNET_FS_Handle *`
3060 deserialization_master (const char *master_path,
3061 GNUNET_FileNameCallback proc,
3062 struct GNUNET_FS_Handle *h)
3066 dn = get_serialization_file_name (h, master_path, "");
3069 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3070 GNUNET_DISK_directory_scan (dn, proc, h);
3076 * Setup a connection to the file-sharing service.
3078 * @param cfg configuration to use
3079 * @param client_name unique identifier for this client
3080 * @param upcb function to call to notify about FS actions
3081 * @param upcb_cls closure for @a upcb
3082 * @param flags specific attributes for fs-operations
3083 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3084 * @return NULL on error
3086 struct GNUNET_FS_Handle *
3087 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3088 const char *client_name,
3089 GNUNET_FS_ProgressCallback upcb,
3091 enum GNUNET_FS_Flags flags, ...)
3093 struct GNUNET_FS_Handle *ret;
3094 enum GNUNET_FS_OPTIONS opt;
3097 ret = GNUNET_new (struct GNUNET_FS_Handle);
3099 ret->client_name = GNUNET_strdup (client_name);
3101 ret->upcb_cls = upcb_cls;
3103 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3104 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3105 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3106 va_start (ap, flags);
3107 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3111 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3112 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3115 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3116 ret->max_parallel_requests = va_arg (ap, unsigned int);
3121 GNUNET_free (ret->client_name);
3128 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3130 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3131 &deserialize_publish_file, ret);
3132 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3133 &deserialize_search_file, ret);
3134 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3135 &deserialize_download_file, ret);
3136 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3137 &deserialize_unindex_file, ret);
3144 * Close our connection with the file-sharing service.
3145 * The callback given to #GNUNET_FS_start() will no longer be
3146 * called after this function returns.
3147 * This function MUST NOT be called from within the
3150 * @param h handle that was returned from #GNUNET_FS_start()
3153 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3155 while (NULL != h->top_head)
3156 h->top_head->ssf (h->top_head->ssf_cls);
3157 if (NULL != h->queue_job)
3158 GNUNET_SCHEDULER_cancel (h->queue_job);
3159 GNUNET_free (h->client_name);
3164 /* end of fs_api.c */