2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 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 GNUNET_assert (NULL == qe->client);
53 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
54 if (NULL == qe->client)
59 qe->start (qe->cls, qe->client);
61 qe->h->active_blocks += qe->blocks;
62 qe->h->active_downloads++;
63 qe->start_time = GNUNET_TIME_absolute_get ();
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "Starting job %p (%u active)\n",
67 qe->h->active_downloads);
68 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
71 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
79 * Stop the given job (send signal, remove from active queue, update
80 * counters and state).
82 * @param qe job to stop
85 stop_job (struct GNUNET_FS_QueueEntry *qe)
89 GNUNET_assert (0 < qe->h->active_downloads);
90 qe->h->active_downloads--;
91 qe->h->active_blocks -= qe->blocks;
93 GNUNET_TIME_relative_add (qe->run_time,
94 GNUNET_TIME_absolute_get_duration
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Stopping job %p (%u active)\n",
99 qe->h->active_downloads);
100 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
101 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
102 qe->h->pending_tail, qe);
107 * Process the jobs in the job queue, possibly starting some
108 * and stopping others.
110 * @param cls the `struct GNUNET_FS_Handle *`
111 * @param tc scheduler context
114 process_job_queue (void *cls,
115 const struct GNUNET_SCHEDULER_TaskContext *tc)
117 struct GNUNET_FS_Handle *h = cls;
118 struct GNUNET_FS_QueueEntry *qe;
119 struct GNUNET_FS_QueueEntry *next;
120 struct GNUNET_TIME_Relative run_time;
121 struct GNUNET_TIME_Relative restart_at;
122 struct GNUNET_TIME_Relative rst;
123 struct GNUNET_TIME_Absolute end_time;
124 unsigned int num_downloads_waiting;
125 unsigned int num_downloads_active;
126 unsigned int num_downloads_expired;
127 unsigned int num_probes_active;
128 unsigned int num_probes_waiting;
129 unsigned int num_probes_expired;
130 int num_probes_change;
131 int num_downloads_change;
135 /* restart_at will be set to the time when it makes sense to
136 re-evaluate the job queue (unless, of course, jobs complete
137 or are added, then we'll be triggered immediately */
138 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
139 /* first, calculate some basic statistics on pending jobs */
140 num_probes_waiting = 0;
141 num_downloads_waiting = 0;
142 for (qe = h->pending_head; NULL != qe; qe = qe->next)
144 switch (qe->priority)
146 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
147 num_probes_waiting++;
149 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
150 num_downloads_waiting++;
157 /* now, calculate some basic statistics on running jobs */
158 num_probes_active = 0;
159 num_probes_expired = 0;
160 num_downloads_active = 0;
161 num_downloads_expired = 0;
162 next = h->running_head;
163 while (NULL != (qe = next))
166 switch (qe->priority)
168 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
169 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
170 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
171 rst = GNUNET_TIME_absolute_get_remaining (end_time);
172 if (0 == rst.rel_value_us)
174 num_probes_expired++;
180 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
183 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
185 GNUNET_TIME_relative_multiply (h->avg_block_latency,
186 qe->blocks * qe->start_times);
187 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
188 rst = GNUNET_TIME_absolute_get_remaining (end_time);
189 if (0 == rst.rel_value_us)
191 num_downloads_expired++;
196 num_downloads_active++;
197 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
205 GNUNET_break (h->active_downloads ==
206 num_downloads_active + num_probes_active);
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
212 num_downloads_active,
213 num_downloads_expired,
214 num_downloads_waiting);
215 GNUNET_break (h->active_downloads + num_probes_active <=
216 h->max_parallel_downloads);
217 /* calculate start/stop decisions */
218 if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
220 /* stop as many probes as there are downloads and probes */
221 num_probes_change = - GNUNET_MIN (num_probes_active,
222 num_downloads_waiting);
223 /* start as many downloads as there are free slots, including those
225 num_downloads_change = h->max_parallel_downloads - h->active_downloads - num_probes_change;
229 /* start all downloads (we can) */
230 num_downloads_change = num_downloads_waiting;
231 /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
232 if (1 + h->max_parallel_downloads / 4 >= (h->active_downloads + num_downloads_change))
233 num_probes_change = GNUNET_MIN (num_probes_waiting,
234 (1 + h->max_parallel_downloads / 4) - (h->active_downloads + num_downloads_change));
236 num_probes_change = 0;
238 GNUNET_break (num_downloads_change <= num_downloads_waiting);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Changing %d probes and %d/%u/%u downloads\n",
242 num_downloads_change,
243 (unsigned int) h->active_downloads,
244 (unsigned int) h->max_parallel_downloads);
245 /* actually stop probes */
246 next = h->running_head;
247 while (NULL != (qe = next))
250 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
252 if (num_probes_change < 0)
256 if (0 == num_probes_change)
260 GNUNET_break (0 <= num_probes_change);
262 /* start some more tasks if we now have empty slots */
263 block_limit_hit = GNUNET_NO;
264 next = h->pending_head;
265 while ( (NULL != (qe = next)) &&
266 ( (num_probes_change > 0) ||
267 (num_downloads_change > 0) ) )
270 switch (qe->priority)
272 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
273 if (num_probes_change > 0)
277 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
278 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
281 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
282 if ( (num_downloads_change > 0) &&
283 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
284 ( (qe->blocks > h->max_parallel_requests) &&
285 (0 == h->active_downloads) ) ) )
288 num_downloads_change--;
290 else if (num_downloads_change > 0)
291 block_limit_hit = GNUNET_YES;
298 GNUNET_break ( (0 == num_downloads_change) ||
299 (GNUNET_YES == block_limit_hit) );
300 GNUNET_break (0 == num_probes_change);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
305 h->max_parallel_requests,
307 num_downloads_change,
308 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
310 /* make sure we run again */
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_FS_QueueStart start,
330 GNUNET_FS_QueueStop stop, void *cls,
332 enum GNUNET_FS_QueuePriority priority)
334 struct GNUNET_FS_QueueEntry *qe;
336 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
341 qe->queue_time = GNUNET_TIME_absolute_get ();
343 qe->priority = priority;
344 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
345 h->pending_tail, qe);
346 if (h->queue_job != NULL)
347 GNUNET_SCHEDULER_cancel (h->queue_job);
348 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 * Dequeue a job from the queue.
359 * @param qe handle for the job
362 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
364 struct GNUNET_FS_Handle *h;
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Dequeueing job %p\n",
370 if (NULL != qe->client)
372 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
374 if (h->queue_job != NULL)
375 GNUNET_SCHEDULER_cancel (h->queue_job);
376 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
381 * Create a top-level activity entry.
383 * @param h global fs handle
384 * @param ssf suspend signal function to use
385 * @param ssf_cls closure for @a ssf
386 * @return fresh top-level activity handle
388 struct TopLevelActivity *
389 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
390 SuspendSignalFunction ssf,
393 struct TopLevelActivity *ret;
395 ret = GNUNET_new (struct TopLevelActivity);
397 ret->ssf_cls = ssf_cls;
398 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
404 * Destroy a top-level activity entry.
406 * @param h global fs handle
407 * @param top top level activity entry
410 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
411 struct TopLevelActivity *top)
413 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
419 * Closure for #GNUNET_FS_data_reader_file_().
424 * Name of the file to read.
429 * File descriptor, NULL if it has not yet been opened.
431 struct GNUNET_DISK_FileHandle *fd;
436 * Function that provides data by reading from a file.
438 * @param cls closure with the `struct FileInfo *`
439 * @param offset offset to read from; it is possible
440 * that the caller might need to go backwards
441 * a bit at times; set to `UINT64_MAX` to tell
442 * the reader that we won't be reading for a while
443 * (used to close the file descriptor but NOT fully
444 * clean up the reader's state); in this case,
445 * a value of '0' for @a max should be ignored
446 * @param max maximum number of bytes that should be
447 * copied to @a buf; readers are not allowed
448 * to provide less data unless there is an error;
449 * a value of "0" will be used at the end to allow
450 * the reader to clean up its internal state
451 * @param buf where the reader should write the data
452 * @param emsg location for the reader to store an error message
453 * @return number of bytes written, usually @a max, 0 on error
456 GNUNET_FS_data_reader_file_ (void *cls,
462 struct FileInfo *fi = cls;
465 if (UINT64_MAX == offset)
469 GNUNET_DISK_file_close (fi->fd);
477 GNUNET_DISK_file_close (fi->fd);
478 GNUNET_free (fi->filename);
485 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 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 */
601 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
604 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
605 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
607 GNUNET_free (basename);
613 * Return the full filename where we would store state information
614 * (for serialization/deserialization) that is associated with a
617 * @param h master context
618 * @param ext component of the path
619 * @param uni name of the parent operation
620 * @param ent entity identifier (or emtpy string for the directory)
621 * @return NULL on error
624 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
632 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
633 return NULL; /* persistence not requested */
635 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
638 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
639 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
640 uni, DIR_SEPARATOR_STR, ent);
641 GNUNET_free (basename);
647 * Return a read handle for deserialization.
649 * @param h master context
650 * @param ext component of the path
651 * @param ent entity identifier (or emtpy string for the directory)
652 * @return NULL on error
654 static struct GNUNET_BIO_ReadHandle *
655 get_read_handle (struct GNUNET_FS_Handle *h,
660 struct GNUNET_BIO_ReadHandle *ret;
662 fn = get_serialization_file_name (h, ext, ent);
665 ret = GNUNET_BIO_read_open (fn);
672 * Return a write handle for serialization.
674 * @param h master context
675 * @param ext component of the path
676 * @param ent entity identifier (or emtpy string for the directory)
677 * @return NULL on error
679 static struct GNUNET_BIO_WriteHandle *
680 get_write_handle (struct GNUNET_FS_Handle *h,
685 struct GNUNET_BIO_WriteHandle *ret;
687 fn = get_serialization_file_name (h, ext, ent);
690 ret = GNUNET_BIO_write_open (fn);
691 GNUNET_break (NULL != ret);
698 * Return a write handle for serialization.
700 * @param h master context
701 * @param ext component of the path
702 * @param uni name of parent
703 * @param ent entity identifier (or emtpy string for the directory)
704 * @return NULL on error
706 static struct GNUNET_BIO_WriteHandle *
707 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
708 const char *uni, const char *ent)
711 struct GNUNET_BIO_WriteHandle *ret;
713 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
716 ret = GNUNET_BIO_write_open (fn);
723 * Remove serialization/deserialization file from disk.
725 * @param h master context
726 * @param ext component of the path
727 * @param ent entity identifier
730 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
736 if ((NULL == ent) || (0 == strlen (ent)))
741 filename = get_serialization_file_name (h, ext, ent);
742 if (NULL != filename)
744 if ( (0 != UNLINK (filename)) &&
746 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
747 GNUNET_free (filename);
753 * Remove serialization/deserialization file from disk.
755 * @param h master context
756 * @param ext component of the path
757 * @param uni parent name
758 * @param ent entity identifier
761 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
763 const char *uni, const char *ent)
767 if ((NULL == ent) || (0 == strlen (ent)))
772 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
773 if (NULL == filename)
775 if (0 != UNLINK (filename))
776 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
777 GNUNET_free (filename);
782 * Remove serialization/deserialization directory from disk.
784 * @param h master context
785 * @param ext component of the path
786 * @param uni unique name of parent
789 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
797 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
800 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
801 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
802 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
808 * Serialize a start-time. Since we use start-times to
809 * calculate the duration of some operation, we actually
810 * do not serialize the absolute time but the (relative)
811 * duration since the start time. When we then
812 * deserialize the start time, we take the current time and
813 * subtract that duration so that we get again an absolute
814 * time stamp that will result in correct performance
817 * @param wh handle for writing
818 * @param timestamp time to serialize
819 * @return #GNUNET_OK on success
822 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
823 struct GNUNET_TIME_Absolute timestamp)
825 struct GNUNET_TIME_Relative dur;
827 dur = GNUNET_TIME_absolute_get_duration (timestamp);
828 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
833 * Deserialize a start-time. Since we use start-times to
834 * calculate the duration of some operation, we actually
835 * do not serialize the absolute time but the (relative)
836 * duration since the start time. Thus, when we then
837 * deserialize the start time, we take the current time and
838 * subtract that duration so that we get again an absolute
839 * time stamp that will result in correct performance
842 * @param rh handle for reading
843 * @param timestamp where to write the deserialized timestamp
844 * @return #GNUNET_OK on success
847 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
848 struct GNUNET_TIME_Absolute *timestamp)
850 struct GNUNET_TIME_Relative dur;
852 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
853 return GNUNET_SYSERR;
854 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
860 * Using the given serialization filename, try to deserialize
861 * the file-information tree associated with it.
863 * @param h master context
864 * @param filename name of the file (without directory) with
866 * @return NULL on error
868 static struct GNUNET_FS_FileInformation *
869 deserialize_file_information (struct GNUNET_FS_Handle *h,
870 const char *filename);
874 * Using the given serialization filename, try to deserialize
875 * the file-information tree associated with it.
877 * @param h master context
878 * @param fn name of the file (without directory) with
880 * @param rh handle for reading
881 * @return NULL on error
883 static struct GNUNET_FS_FileInformation *
884 deserialize_fi_node (struct GNUNET_FS_Handle *h,
886 struct GNUNET_BIO_ReadHandle *rh)
888 struct GNUNET_FS_FileInformation *ret;
889 struct GNUNET_FS_FileInformation *nxt;
897 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
902 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
908 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
909 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
911 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
912 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
913 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
915 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
916 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
917 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
919 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
920 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
921 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
922 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
924 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
926 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
927 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
928 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
929 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
936 case 0: /* file-insert */
937 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
942 ret->is_directory = GNUNET_NO;
943 ret->data.file.do_index = GNUNET_NO;
944 ret->data.file.have_hash = GNUNET_NO;
945 ret->data.file.index_start_confirmed = GNUNET_NO;
946 if (GNUNET_NO == ret->is_published)
948 if (NULL == ret->filename)
950 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
951 ret->data.file.reader_cls =
952 GNUNET_malloc_large (ret->data.file.file_size);
953 if (ret->data.file.reader_cls == NULL)
956 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
957 ret->data.file.file_size))
965 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
966 ret->data.file.reader_cls =
967 GNUNET_FS_make_file_reader_context_ (ret->filename);
971 case 1: /* file-index, no hash */
972 if (NULL == ret->filename)
977 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
982 ret->is_directory = GNUNET_NO;
983 ret->data.file.do_index = GNUNET_YES;
984 ret->data.file.have_hash = GNUNET_NO;
985 ret->data.file.index_start_confirmed = GNUNET_NO;
986 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
987 ret->data.file.reader_cls =
988 GNUNET_FS_make_file_reader_context_ (ret->filename);
990 case 2: /* file-index-with-hash */
991 if (NULL == ret->filename)
996 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
998 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
999 sizeof (struct GNUNET_HashCode))))
1004 ret->is_directory = GNUNET_NO;
1005 ret->data.file.do_index = GNUNET_YES;
1006 ret->data.file.have_hash = GNUNET_YES;
1007 ret->data.file.index_start_confirmed = GNUNET_NO;
1008 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1009 ret->data.file.reader_cls =
1010 GNUNET_FS_make_file_reader_context_ (ret->filename);
1012 case 3: /* file-index-with-hash-confirmed */
1013 if (NULL == ret->filename)
1018 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1020 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1021 sizeof (struct GNUNET_HashCode))))
1026 ret->is_directory = GNUNET_NO;
1027 ret->data.file.do_index = GNUNET_YES;
1028 ret->data.file.have_hash = GNUNET_YES;
1029 ret->data.file.index_start_confirmed = GNUNET_YES;
1030 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1031 ret->data.file.reader_cls =
1032 GNUNET_FS_make_file_reader_context_ (ret->filename);
1034 case 4: /* directory */
1035 ret->is_directory = GNUNET_YES;
1036 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1037 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1038 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1039 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1041 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1043 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1048 ret->data.dir.dir_size = (uint32_t) dsize;
1049 if (NULL != filename)
1051 ret->data.dir.entries = deserialize_file_information (h, filename);
1052 GNUNET_free (filename);
1054 nxt = ret->data.dir.entries;
1066 ret->serialization = GNUNET_strdup (fn);
1068 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1073 if (NULL != filename)
1075 ret->next = deserialize_file_information (h, filename);
1076 GNUNET_free (filename);
1079 GNUNET_free_non_null (ksks);
1080 GNUNET_free_non_null (skss);
1081 GNUNET_free_non_null (chks);
1084 GNUNET_free_non_null (ksks);
1085 GNUNET_free_non_null (chks);
1086 GNUNET_free_non_null (skss);
1087 GNUNET_free_non_null (filename);
1088 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1094 * Using the given serialization filename, try to deserialize
1095 * the file-information tree associated with it.
1097 * @param h master context
1098 * @param filename name of the file (without directory) with
1100 * @return NULL on error
1102 static struct GNUNET_FS_FileInformation *
1103 deserialize_file_information (struct GNUNET_FS_Handle *h,
1104 const char *filename)
1106 struct GNUNET_FS_FileInformation *ret;
1107 struct GNUNET_BIO_ReadHandle *rh;
1111 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1114 ret = deserialize_fi_node (h, filename, rh);
1115 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1118 _("Failed to resume publishing information `%s': %s\n"),
1124 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1127 if (0 != UNLINK (fn))
1128 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1137 * Given a serialization name (full absolute path), return the
1138 * basename of the file (without the path), which must only
1139 * consist of the 6 random characters.
1141 * @param fullname name to extract the basename from
1142 * @return copy of the basename, NULL on error
1145 get_serialization_short_name (const char *fullname)
1152 /* FIXME: we could do this faster since we know
1153 * the length of 'end'... */
1154 while ('\0' != *nxt)
1156 if (DIR_SEPARATOR == *nxt)
1160 if ((NULL == end) || (0 == strlen (end)))
1165 GNUNET_break (6 == strlen (end));
1166 return GNUNET_strdup (end);
1171 * Create a new random name for serialization. Also checks if persistence
1172 * is enabled and returns NULL if not.
1174 * @param h master context
1175 * @param ext component of the path
1176 * @return NULL on errror
1179 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1186 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1187 return NULL; /* persistence not requested */
1188 dn = get_serialization_file_name (h, ext, "");
1191 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1196 fn = GNUNET_DISK_mktemp (dn);
1199 return NULL; /* epic fail */
1200 ret = get_serialization_short_name (fn);
1207 * Create a new random name for serialization. Also checks if persistence
1208 * is enabled and returns NULL if not.
1210 * @param h master context
1211 * @param ext component of the path
1212 * @param uni name of parent
1213 * @return NULL on errror
1216 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1224 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1225 return NULL; /* persistence not requested */
1226 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1229 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1234 fn = GNUNET_DISK_mktemp (dn);
1237 return NULL; /* epic fail */
1238 ret = get_serialization_short_name (fn);
1245 * Copy all of the data from the reader to the write handle.
1247 * @param wh write handle
1248 * @param fi file with reader
1249 * @return #GNUNET_OK on success
1252 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1253 struct GNUNET_FS_FileInformation *fi)
1255 char buf[32 * 1024];
1263 while (off < fi->data.file.file_size)
1265 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1267 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1271 return GNUNET_SYSERR;
1273 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1274 return GNUNET_SYSERR;
1282 * Create a temporary file on disk to store the current
1283 * state of @a fi in.
1285 * @param fi file information to sync with disk
1288 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1291 struct GNUNET_BIO_WriteHandle *wh;
1297 if (NULL == fi->serialization)
1299 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1300 if (NULL == fi->serialization)
1302 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1306 GNUNET_free (fi->serialization);
1307 fi->serialization = NULL;
1310 if (GNUNET_YES == fi->is_directory)
1312 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1314 else if (GNUNET_YES == fi->data.file.have_hash)
1316 else if (GNUNET_YES == fi->data.file.do_index)
1320 if (NULL != fi->keywords)
1321 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1324 if (NULL != fi->chk_uri)
1325 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1328 if (NULL != fi->sks_uri)
1329 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1332 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1333 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1334 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1335 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1336 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1337 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1338 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1339 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1341 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1342 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1343 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1344 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1349 GNUNET_free_non_null (chks);
1351 GNUNET_free_non_null (ksks);
1353 GNUNET_free_non_null (skss);
1358 case 0: /* file-insert */
1359 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1364 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1365 if (GNUNET_OK != copy_from_reader (wh, fi))
1371 case 1: /* file-index, no hash */
1372 if (NULL == fi->filename)
1377 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1383 case 2: /* file-index-with-hash */
1384 case 3: /* file-index-with-hash-confirmed */
1385 if (NULL == fi->filename)
1390 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1392 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1393 sizeof (struct GNUNET_HashCode))))
1399 case 4: /* directory */
1400 if ( (NULL != fi->data.dir.entries) &&
1401 (NULL == fi->data.dir.entries->serialization) )
1402 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1403 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1404 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1405 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1407 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1408 (uint32_t) fi->data.dir.dir_size)) ||
1410 GNUNET_BIO_write_string (wh,
1411 (fi->data.dir.entries ==
1412 NULL) ? NULL : fi->data.dir.
1413 entries->serialization)))
1423 if ( (NULL != fi->next) &&
1424 (NULL == fi->next->serialization) )
1425 GNUNET_FS_file_information_sync_ (fi->next);
1427 GNUNET_BIO_write_string (wh,
1429 NULL) ? fi->next->serialization : NULL))
1434 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1443 (void) GNUNET_BIO_write_close (wh);
1444 GNUNET_free_non_null (chks);
1445 GNUNET_free_non_null (ksks);
1446 GNUNET_free_non_null (skss);
1447 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1451 if (0 != UNLINK (fn))
1452 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1455 GNUNET_free (fi->serialization);
1456 fi->serialization = NULL;
1462 * Find the entry in the file information struct where the
1463 * serialization filename matches the given name.
1465 * @param pos file information to search
1466 * @param srch filename to search for
1467 * @return NULL if srch was not found in this subtree
1469 static struct GNUNET_FS_FileInformation *
1470 find_file_position (struct GNUNET_FS_FileInformation *pos,
1473 struct GNUNET_FS_FileInformation *r;
1477 if (0 == strcmp (srch, pos->serialization))
1479 if ( (GNUNET_YES == pos->is_directory) &&
1480 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1489 * Signal the FS's progress function that we are resuming
1492 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1493 * @param fi the entry in the publish-structure
1494 * @param length length of the file or directory
1495 * @param meta metadata for the file or directory (can be modified)
1496 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1497 * @param bo block options (can be modified)
1498 * @param do_index should we index?
1499 * @param client_info pointer to client context set upon creation (can be modified)
1500 * @return #GNUNET_OK to continue (always)
1503 fip_signal_resume (void *cls,
1504 struct GNUNET_FS_FileInformation *fi,
1506 struct GNUNET_CONTAINER_MetaData *meta,
1507 struct GNUNET_FS_Uri **uri,
1508 struct GNUNET_FS_BlockOptions *bo,
1512 struct GNUNET_FS_PublishContext *pc = cls;
1513 struct GNUNET_FS_ProgressInfo pi;
1515 if (GNUNET_YES == pc->skip_next_fi_callback)
1517 pc->skip_next_fi_callback = GNUNET_NO;
1520 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1521 pi.value.publish.specifics.resume.message = fi->emsg;
1522 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1523 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1524 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1526 /* process entries in directory */
1527 pc->skip_next_fi_callback = GNUNET_YES;
1528 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1535 * Function called with a filename of serialized publishing operation
1538 * @param cls the `struct GNUNET_FS_Handle *`
1539 * @param filename complete filename (absolute path)
1540 * @return #GNUNET_OK (continue to iterate)
1543 deserialize_publish_file (void *cls,
1544 const char *filename)
1546 struct GNUNET_FS_Handle *h = cls;
1547 struct GNUNET_BIO_ReadHandle *rh;
1548 struct GNUNET_FS_PublishContext *pc;
1553 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1557 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1559 pc->serialization = get_serialization_short_name (filename);
1562 rh = GNUNET_BIO_read_open (filename);
1568 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1570 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1571 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1572 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1573 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1575 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1576 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1577 || ( (GNUNET_YES == have_ns) &&
1578 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1583 pc->options = options;
1584 pc->all_done = all_done;
1585 if (NULL == fi_root)
1590 pc->fi = deserialize_file_information (h, fi_root);
1596 if (GNUNET_YES == have_ns)
1598 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1601 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1602 (GNUNET_YES != pc->all_done))
1604 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1605 if (NULL == pc->dsh)
1610 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1611 GNUNET_free (fi_pos);
1613 if (NULL == pc->fi_pos)
1615 /* failed to find position for resuming, outch! Will start from root! */
1617 if (GNUNET_YES != pc->all_done)
1618 pc->fi_pos = pc->fi;
1621 GNUNET_free (fi_root);
1623 /* generate RESUME event(s) */
1624 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1626 /* re-start publishing (if needed)... */
1627 if (GNUNET_YES != pc->all_done)
1629 GNUNET_assert (NULL == pc->upload_task);
1631 GNUNET_SCHEDULER_add_with_priority
1632 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1633 &GNUNET_FS_publish_main_, pc);
1635 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1638 _("Failure while resuming publishing operation `%s': %s\n"),
1642 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1645 GNUNET_free_non_null (pc->nid);
1646 GNUNET_free_non_null (pc->nuid);
1647 GNUNET_free_non_null (fi_root);
1648 GNUNET_free_non_null (fi_pos);
1649 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1652 _("Failed to resume publishing operation `%s': %s\n"), filename,
1657 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1658 if (0 != UNLINK (filename))
1659 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1660 GNUNET_free (pc->serialization);
1667 * Synchronize this publishing struct with its mirror
1668 * on disk. Note that all internal FS-operations that change
1669 * publishing structs should already call "sync" internally,
1670 * so this function is likely not useful for clients.
1672 * @param pc the struct to sync
1675 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1677 struct GNUNET_BIO_WriteHandle *wh;
1680 if (NULL == pc->serialization)
1682 make_serialization_file_name (pc->h,
1683 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1684 if (NULL == pc->serialization)
1688 if (NULL == pc->fi->serialization)
1693 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1700 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1701 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1702 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1703 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1704 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1705 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1706 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1708 GNUNET_BIO_write_string (wh,
1709 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1710 ( (NULL != pc->ns) &&
1711 (GNUNET_OK != GNUNET_BIO_write (wh,
1713 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1718 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1727 (void) GNUNET_BIO_write_close (wh);
1728 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1730 GNUNET_free (pc->serialization);
1731 pc->serialization = NULL;
1736 * Synchronize this unindex struct with its mirror
1737 * on disk. Note that all internal FS-operations that change
1738 * publishing structs should already call "sync" internally,
1739 * so this function is likely not useful for clients.
1741 * @param uc the struct to sync
1744 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1746 struct GNUNET_BIO_WriteHandle *wh;
1749 if (NULL == uc->serialization)
1751 make_serialization_file_name (uc->h,
1752 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1753 if (NULL == uc->serialization)
1755 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1762 if (NULL != uc->ksk_uri)
1763 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1766 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1767 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1768 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1769 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1771 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1772 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1773 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1774 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1776 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1777 ((uc->state == UNINDEX_STATE_ERROR) &&
1778 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1783 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1792 (void) GNUNET_BIO_write_close (wh);
1793 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1795 GNUNET_free (uc->serialization);
1796 uc->serialization = NULL;
1801 * Serialize a download request.
1803 * @param wh handle for writing the download request to disk
1804 * @param dr the the request to write to disk
1805 * @return #GNUNET_YES on success, #GNUNET_NO on error
1808 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1809 struct DownloadRequest *dr)
1813 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1814 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1815 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1816 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1818 if ((BRS_CHK_SET == dr->state) &&
1820 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1822 for (i = 0; i < dr->num_children; i++)
1823 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1830 * Read a download request tree.
1832 * @param rh cadet to read from
1833 * @return value the download request read from disk, NULL on error
1835 static struct DownloadRequest *
1836 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1838 struct DownloadRequest *dr;
1841 dr = GNUNET_new (struct DownloadRequest);
1842 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1843 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1844 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1845 (dr->num_children > CHK_PER_INODE) ||
1846 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1847 ( (0 == dr->depth) &&
1848 (dr->num_children > 0) ) ||
1849 ((dr->depth > 0) && (0 == dr->num_children)))
1852 dr->num_children = 0;
1855 if (dr->num_children > 0)
1857 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1861 case BRS_RECONSTRUCT_DOWN:
1862 case BRS_RECONSTRUCT_META_UP:
1863 case BRS_RECONSTRUCT_UP:
1867 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1870 case BRS_DOWNLOAD_DOWN:
1871 case BRS_DOWNLOAD_UP:
1878 for (i = 0; i < dr->num_children; i++)
1880 if (NULL == (dr->children[i] = read_download_request (rh)))
1882 dr->children[i]->parent = dr;
1886 GNUNET_FS_free_download_request_ (dr);
1892 * Compute the name of the sync file (or directory) for the given download
1895 * @param dc download context to compute for
1896 * @param uni unique filename to use, use "" for the directory name
1897 * @param ext extension to use, use ".dir" for our own subdirectory
1898 * @return the expanded file name, NULL for none
1901 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1908 if (dc->parent == NULL)
1909 return get_serialization_file_name (dc->h,
1910 (dc->search != NULL) ?
1911 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1912 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1914 if (NULL == dc->parent->serialization)
1916 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1919 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1926 * Synchronize this download struct with its mirror
1927 * on disk. Note that all internal FS-operations that change
1928 * publishing structs should already call "sync" internally,
1929 * so this function is likely not useful for clients.
1931 * @param dc the struct to sync
1934 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1936 struct GNUNET_BIO_WriteHandle *wh;
1941 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1942 return; /* we don't sync probes */
1943 if (NULL == dc->serialization)
1945 dir = get_download_sync_filename (dc, "", "");
1948 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1953 fn = GNUNET_DISK_mktemp (dir);
1957 dc->serialization = get_serialization_short_name (fn);
1961 fn = get_download_sync_filename (dc, dc->serialization, "");
1964 GNUNET_free (dc->serialization);
1965 dc->serialization = NULL;
1970 wh = GNUNET_BIO_write_open (fn);
1973 GNUNET_free (dc->serialization);
1974 dc->serialization = NULL;
1978 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1979 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1980 uris = GNUNET_FS_uri_to_string (dc->uri);
1981 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1982 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1983 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1984 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1985 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1986 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1987 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1988 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1989 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1990 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1991 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1992 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1993 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1998 if (NULL == dc->emsg)
2000 GNUNET_assert (dc->top_request != NULL);
2001 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2007 GNUNET_free_non_null (uris);
2009 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2019 (void) GNUNET_BIO_write_close (wh);
2020 GNUNET_free_non_null (uris);
2021 if (0 != UNLINK (fn))
2022 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2024 GNUNET_free (dc->serialization);
2025 dc->serialization = NULL;
2030 * Synchronize this search result with its mirror
2031 * on disk. Note that all internal FS-operations that change
2032 * publishing structs should already call "sync" internally,
2033 * so this function is likely not useful for clients.
2035 * @param sr the struct to sync
2038 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2040 struct GNUNET_BIO_WriteHandle *wh;
2046 if (NULL == sr->serialization)
2048 make_serialization_file_name_in_dir (sr->h,
2049 (sr->sc->psearch_result ==
2051 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2052 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2053 sr->sc->serialization);
2054 if (NULL == sr->serialization)
2056 wh = get_write_handle_in_dir (sr->h,
2057 (sr->sc->psearch_result ==
2058 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2059 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2060 sr->sc->serialization, sr->serialization);
2066 uris = GNUNET_FS_uri_to_string (sr->uri);
2067 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2069 GNUNET_BIO_write_string (wh,
2071 NULL ? sr->download->serialization : NULL)) ||
2073 GNUNET_BIO_write_string (wh,
2074 sr->update_search !=
2075 NULL ? sr->update_search->serialization : NULL))
2076 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2077 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2078 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2079 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2080 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2081 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2086 if ( (NULL != sr->uri) &&
2087 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2088 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2089 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2094 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2100 GNUNET_free_non_null (uris);
2103 GNUNET_free_non_null (uris);
2105 (void) GNUNET_BIO_write_close (wh);
2106 remove_sync_file_in_dir (sr->h,
2107 (NULL == sr->sc->psearch_result)
2108 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2109 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2110 sr->sc->serialization, sr->serialization);
2111 GNUNET_free (sr->serialization);
2112 sr->serialization = NULL;
2117 * Synchronize this search struct with its mirror
2118 * on disk. Note that all internal FS-operations that change
2119 * publishing structs should already call "sync" internally,
2120 * so this function is likely not useful for clients.
2122 * @param sc the struct to sync
2125 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2127 struct GNUNET_BIO_WriteHandle *wh;
2130 const char *category;
2133 (NULL == sc->psearch_result)
2134 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2135 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2136 if (NULL == sc->serialization)
2137 sc->serialization = make_serialization_file_name (sc->h, category);
2138 if (NULL == sc->serialization)
2141 wh = get_write_handle (sc->h, category, sc->serialization);
2147 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2148 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2149 uris = GNUNET_FS_uri_to_string (sc->uri);
2150 in_pause = (sc->task != NULL) ? 'r' : '\0';
2151 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2152 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2153 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2154 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2155 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2156 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2163 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2172 (void) GNUNET_BIO_write_close (wh);
2173 GNUNET_free_non_null (uris);
2174 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2175 GNUNET_free (sc->serialization);
2176 sc->serialization = NULL;
2181 * Function called with a filename of serialized unindexing operation
2184 * @param cls the `struct GNUNET_FS_Handle *`
2185 * @param filename complete filename (absolute path)
2186 * @return #GNUNET_OK (continue to iterate)
2189 deserialize_unindex_file (void *cls,
2190 const char *filename)
2192 struct GNUNET_FS_Handle *h = cls;
2193 struct GNUNET_BIO_ReadHandle *rh;
2194 struct GNUNET_FS_UnindexContext *uc;
2195 struct GNUNET_FS_ProgressInfo pi;
2200 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2202 uc->serialization = get_serialization_short_name (filename);
2203 rh = GNUNET_BIO_read_open (filename);
2211 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2212 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2213 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2214 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2215 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2216 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2217 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2219 GNUNET_free_non_null (uris);
2225 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2227 if (NULL == uc->ksk_uri)
2230 GNUNET_free_non_null (emsg);
2234 if ( (uc->ksk_offset > 0) &&
2235 ( (NULL == uc->ksk_uri) ||
2236 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2241 uc->state = (enum UnindexState) state;
2244 case UNINDEX_STATE_HASHING:
2246 case UNINDEX_STATE_FS_NOTIFY:
2248 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2249 sizeof (struct GNUNET_HashCode)))
2255 case UNINDEX_STATE_DS_REMOVE:
2256 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2257 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2259 case UNINDEX_STATE_COMPLETE:
2261 case UNINDEX_STATE_ERROR:
2263 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2273 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2274 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2275 pi.value.unindex.specifics.resume.message = uc->emsg;
2276 GNUNET_FS_unindex_make_status_ (&pi, uc,
2278 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2281 case UNINDEX_STATE_HASHING:
2283 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2285 &GNUNET_FS_unindex_process_hash_, uc);
2287 case UNINDEX_STATE_FS_NOTIFY:
2288 uc->state = UNINDEX_STATE_HASHING;
2289 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2291 case UNINDEX_STATE_DS_REMOVE:
2292 GNUNET_FS_unindex_do_remove_ (uc);
2294 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2295 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2297 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2298 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2300 case UNINDEX_STATE_COMPLETE:
2301 case UNINDEX_STATE_ERROR:
2302 /* no need to resume any operation, we were done */
2307 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2310 _("Failure while resuming unindexing operation `%s': %s\n"),
2316 GNUNET_free_non_null (uc->filename);
2317 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2320 _("Failed to resume unindexing operation `%s': %s\n"),
2325 if (NULL != uc->serialization)
2326 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2328 GNUNET_free_non_null (uc->serialization);
2335 * Deserialize a download.
2337 * @param h overall context
2338 * @param rh file to deserialize from
2339 * @param parent parent download
2340 * @param search associated search
2341 * @param serialization name under which the search was serialized
2344 deserialize_download (struct GNUNET_FS_Handle *h,
2345 struct GNUNET_BIO_ReadHandle *rh,
2346 struct GNUNET_FS_DownloadContext *parent,
2347 struct GNUNET_FS_SearchResult *search,
2348 const char *serialization);
2352 * Deserialize a search.
2354 * @param h overall context
2355 * @param rh file to deserialize from
2356 * @param psearch_result parent search result
2357 * @param serialization name under which the search was serialized
2359 static struct GNUNET_FS_SearchContext *
2360 deserialize_search (struct GNUNET_FS_Handle *h,
2361 struct GNUNET_BIO_ReadHandle *rh,
2362 struct GNUNET_FS_SearchResult *psearch_result,
2363 const char *serialization);
2367 * Function called with a filename of serialized search result
2370 * @param cls the `struct GNUNET_FS_SearchContext *`
2371 * @param filename complete filename (absolute path)
2372 * @return #GNUNET_OK (continue to iterate)
2375 deserialize_search_result (void *cls,
2376 const char *filename)
2378 struct GNUNET_FS_SearchContext *sc = cls;
2384 struct GNUNET_BIO_ReadHandle *rh;
2385 struct GNUNET_BIO_ReadHandle *drh;
2386 struct GNUNET_FS_SearchResult *sr;
2388 ser = get_serialization_short_name (filename);
2389 rh = GNUNET_BIO_read_open (filename);
2394 remove_sync_file_in_dir (sc->h,
2395 (NULL == sc->psearch_result)
2396 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2397 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2398 sc->serialization, ser);
2407 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2410 sr->serialization = ser;
2411 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2412 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2413 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2415 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2416 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2418 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2419 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2420 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2421 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2422 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2427 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2429 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2430 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2432 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2439 if (NULL != download)
2441 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2444 deserialize_download (sc->h, drh, NULL, sr, download);
2445 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2448 _("Failed to resume sub-download `%s': %s\n"),
2454 GNUNET_free (download);
2456 if (NULL != update_srch)
2459 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2462 deserialize_search (sc->h, drh, sr, update_srch);
2463 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2466 _("Failed to resume sub-search `%s': %s\n"),
2472 GNUNET_free (update_srch);
2474 GNUNET_break (GNUNET_YES ==
2475 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2478 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2481 _("Failure while resuming search operation `%s': %s\n"),
2487 GNUNET_free_non_null (download);
2488 GNUNET_free_non_null (emsg);
2489 GNUNET_free_non_null (uris);
2490 GNUNET_free_non_null (update_srch);
2491 if (NULL != sr->uri)
2492 GNUNET_FS_uri_destroy (sr->uri);
2493 if (NULL != sr->meta)
2494 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2495 GNUNET_free (sr->serialization);
2497 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2500 _("Failure while resuming search operation `%s': %s\n"),
2509 * Send the 'resume' signal to the callback; also actually
2510 * resume the download (put it in the queue). Does this
2511 * recursively for the top-level download and all child
2514 * @param dc download to resume
2517 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2519 struct GNUNET_FS_DownloadContext *dcc;
2520 struct GNUNET_FS_ProgressInfo pi;
2522 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2523 pi.value.download.specifics.resume.meta = dc->meta;
2524 pi.value.download.specifics.resume.message = dc->emsg;
2525 GNUNET_FS_download_make_status_ (&pi, dc);
2526 dcc = dc->child_head;
2529 signal_download_resume (dcc);
2532 if (NULL != dc->pending_head)
2533 GNUNET_FS_download_start_downloading_ (dc);
2538 * Signal resuming of a search to our clients (for the
2539 * top level search and all sub-searches).
2541 * @param sc search being resumed
2544 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2548 * Iterator over search results signaling resume to the client for
2551 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2552 * @param key current key code
2553 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2554 * @return #GNUNET_YES (we should continue to iterate)
2557 signal_result_resume (void *cls,
2558 const struct GNUNET_HashCode *key,
2561 struct GNUNET_FS_SearchContext *sc = cls;
2562 struct GNUNET_FS_ProgressInfo pi;
2563 struct GNUNET_FS_SearchResult *sr = value;
2565 if (0 == sr->mandatory_missing)
2567 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2568 pi.value.search.specifics.resume_result.meta = sr->meta;
2569 pi.value.search.specifics.resume_result.uri = sr->uri;
2570 pi.value.search.specifics.resume_result.result = sr;
2571 pi.value.search.specifics.resume_result.availability_rank =
2572 2 * sr->availability_success - sr->availability_trials;
2573 pi.value.search.specifics.resume_result.availability_certainty =
2574 sr->availability_trials;
2575 pi.value.search.specifics.resume_result.applicability_rank =
2576 sr->optional_support;
2577 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2579 if (NULL != sr->download)
2581 signal_download_resume (sr->download);
2585 GNUNET_FS_search_start_probe_ (sr);
2587 if (NULL != sr->update_search)
2588 signal_search_resume (sr->update_search);
2594 * Free memory allocated by the search context and its children
2596 * @param sc search context to free
2599 free_search_context (struct GNUNET_FS_SearchContext *sc);
2603 * Iterator over search results freeing each.
2605 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2606 * @param key current key code
2607 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2608 * @return #GNUNET_YES (we should continue to iterate)
2611 free_result (void *cls,
2612 const struct GNUNET_HashCode *key,
2615 struct GNUNET_FS_SearchResult *sr = value;
2617 if (NULL != sr->update_search)
2619 free_search_context (sr->update_search);
2620 GNUNET_assert (NULL == sr->update_search);
2622 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2623 GNUNET_FS_uri_destroy (sr->uri);
2630 * Free memory allocated by the search context and its children
2632 * @param sc search context to free
2635 free_search_context (struct GNUNET_FS_SearchContext *sc)
2637 if (NULL != sc->serialization)
2639 GNUNET_FS_remove_sync_file_ (sc->h,
2640 (sc->psearch_result ==
2641 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2642 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2644 GNUNET_FS_remove_sync_dir_ (sc->h,
2645 (sc->psearch_result ==
2646 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2647 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2650 GNUNET_free_non_null (sc->serialization);
2651 GNUNET_free_non_null (sc->emsg);
2652 if (NULL != sc->uri)
2653 GNUNET_FS_uri_destroy (sc->uri);
2654 if (NULL != sc->master_result_map)
2656 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2658 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2665 * Function called with a filename of serialized sub-download
2668 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2669 * @param filename complete filename (absolute path)
2670 * @return #GNUNET_OK (continue to iterate)
2673 deserialize_subdownload (void *cls,
2674 const char *filename)
2676 struct GNUNET_FS_DownloadContext *parent = cls;
2679 struct GNUNET_BIO_ReadHandle *rh;
2681 ser = get_serialization_short_name (filename);
2682 rh = GNUNET_BIO_read_open (filename);
2685 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2686 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2692 deserialize_download (parent->h, rh, parent, NULL, ser);
2693 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2696 _("Failed to resume sub-download `%s': %s\n"),
2707 * Free this download context and all of its descendants.
2708 * (only works during deserialization since not all possible
2709 * state it taken care of).
2711 * @param dc context to free
2714 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2716 struct GNUNET_FS_DownloadContext *dcc;
2718 if (NULL != dc->meta)
2719 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2720 if (NULL != dc->uri)
2721 GNUNET_FS_uri_destroy (dc->uri);
2722 GNUNET_free_non_null (dc->temp_filename);
2723 GNUNET_free_non_null (dc->emsg);
2724 GNUNET_free_non_null (dc->filename);
2725 GNUNET_free_non_null (dc->serialization);
2726 while (NULL != (dcc = dc->child_head))
2728 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2731 free_download_context (dcc);
2733 GNUNET_FS_free_download_request_ (dc->top_request);
2734 if (NULL != dc->active)
2735 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2741 * Deserialize a download.
2743 * @param h overall context
2744 * @param rh file to deserialize from
2745 * @param parent parent download
2746 * @param search associated search
2747 * @param serialization name under which the search was serialized
2750 deserialize_download (struct GNUNET_FS_Handle *h,
2751 struct GNUNET_BIO_ReadHandle *rh,
2752 struct GNUNET_FS_DownloadContext *parent,
2753 struct GNUNET_FS_SearchResult *search,
2754 const char *serialization)
2756 struct GNUNET_FS_DownloadContext *dc;
2765 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2766 dc->parent = parent;
2768 dc->serialization = GNUNET_strdup (serialization);
2770 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2771 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2772 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2773 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2774 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2776 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2778 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2780 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2782 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2783 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2784 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2785 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2786 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2787 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2788 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2789 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2794 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2796 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2797 dc->has_finished = (int) status;
2799 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2800 if (GNUNET_FS_uri_test_loc (dc->uri))
2801 GNUNET_assert (GNUNET_OK ==
2802 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2803 if (NULL == dc->emsg)
2805 dc->top_request = read_download_request (rh);
2806 if (NULL == dc->top_request)
2812 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2815 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2816 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2821 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2825 dc->search = search;
2826 search->download = dc;
2828 if ((NULL == parent) && (NULL == search))
2831 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2832 signal_download_resume (dc);
2835 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2838 GNUNET_free_non_null (uris);
2839 GNUNET_free_non_null (emsg);
2840 free_download_context (dc);
2845 * Signal resuming of a search to our clients (for the
2846 * top level search and all sub-searches).
2848 * @param sc search being resumed
2851 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2853 struct GNUNET_FS_ProgressInfo pi;
2855 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2856 pi.value.search.specifics.resume.message = sc->emsg;
2857 pi.value.search.specifics.resume.is_paused =
2858 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2859 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2860 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2861 &signal_result_resume, sc);
2867 * Deserialize a search.
2869 * @param h overall context
2870 * @param rh file to deserialize from
2871 * @param psearch_result parent search result
2872 * @param serialization name under which the search was serialized
2874 static struct GNUNET_FS_SearchContext *
2875 deserialize_search (struct GNUNET_FS_Handle *h,
2876 struct GNUNET_BIO_ReadHandle *rh,
2877 struct GNUNET_FS_SearchResult *psearch_result,
2878 const char *serialization)
2880 struct GNUNET_FS_SearchContext *sc;
2887 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2894 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2895 if (NULL != psearch_result)
2897 sc->psearch_result = psearch_result;
2898 psearch_result->update_search = sc;
2901 sc->serialization = GNUNET_strdup (serialization);
2903 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2904 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2905 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2906 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2907 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2909 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2910 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2912 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2913 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2918 sc->options = (enum GNUNET_FS_SearchOptions) options;
2919 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2920 dn = get_serialization_file_name_in_dir (h,
2921 (NULL == sc->psearch_result) ?
2922 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2923 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2924 sc->serialization, "");
2927 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2928 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2931 if (('\0' == in_pause) &&
2932 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2934 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2935 _("Could not resume running search, will resume as paused search\n"));
2937 signal_search_resume (sc);
2941 GNUNET_free_non_null (emsg);
2942 free_search_context (sc);
2943 GNUNET_free_non_null (uris);
2949 * Function called with a filename of serialized search operation
2952 * @param cls the `struct GNUNET_FS_Handle *`
2953 * @param filename complete filename (absolute path)
2954 * @return #GNUNET_OK (continue to iterate)
2957 deserialize_search_file (void *cls,
2958 const char *filename)
2960 struct GNUNET_FS_Handle *h = cls;
2963 struct GNUNET_BIO_ReadHandle *rh;
2964 struct GNUNET_FS_SearchContext *sc;
2967 if (0 != STAT (filename, &buf))
2969 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2972 if (S_ISDIR (buf.st_mode))
2973 return GNUNET_OK; /* skip directories */
2974 ser = get_serialization_short_name (filename);
2975 rh = GNUNET_BIO_read_open (filename);
2980 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2985 sc = deserialize_search (h, rh, NULL, ser);
2987 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2989 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2991 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2992 _("Failure while resuming search operation `%s': %s\n"),
3001 * Function called with a filename of serialized download operation
3004 * @param cls the `struct GNUNET_FS_Handle *`
3005 * @param filename complete filename (absolute path)
3006 * @return #GNUNET_OK (continue to iterate)
3009 deserialize_download_file (void *cls, const char *filename)
3011 struct GNUNET_FS_Handle *h = cls;
3014 struct GNUNET_BIO_ReadHandle *rh;
3016 ser = get_serialization_short_name (filename);
3017 rh = GNUNET_BIO_read_open (filename);
3020 if (0 != UNLINK (filename))
3021 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3025 deserialize_download (h, rh, NULL, NULL, ser);
3027 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3030 _("Failure while resuming download operation `%s': %s\n"),
3039 * Deserialize informatin about pending operations.
3041 * @param master_path which master directory should be scanned
3042 * @param proc function to call for each entry (will get 'h' for 'cls')
3043 * @param h the 'struct GNUNET_FS_Handle*'
3046 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
3047 struct GNUNET_FS_Handle *h)
3051 dn = get_serialization_file_name (h, master_path, "");
3054 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3055 GNUNET_DISK_directory_scan (dn, proc, h);
3061 * Setup a connection to the file-sharing service.
3063 * @param cfg configuration to use
3064 * @param client_name unique identifier for this client
3065 * @param upcb function to call to notify about FS actions
3066 * @param upcb_cls closure for @a upcb
3067 * @param flags specific attributes for fs-operations
3068 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3069 * @return NULL on error
3071 struct GNUNET_FS_Handle *
3072 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3073 const char *client_name,
3074 GNUNET_FS_ProgressCallback upcb,
3076 enum GNUNET_FS_Flags flags, ...)
3078 struct GNUNET_FS_Handle *ret;
3079 enum GNUNET_FS_OPTIONS opt;
3082 ret = GNUNET_new (struct GNUNET_FS_Handle);
3084 ret->client_name = GNUNET_strdup (client_name);
3086 ret->upcb_cls = upcb_cls;
3088 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3089 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3090 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3091 va_start (ap, flags);
3092 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3096 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3097 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3100 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3101 ret->max_parallel_requests = va_arg (ap, unsigned int);
3106 GNUNET_free (ret->client_name);
3113 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3115 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3116 &deserialize_publish_file, ret);
3117 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3118 &deserialize_search_file, ret);
3119 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3120 &deserialize_download_file, ret);
3121 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3122 &deserialize_unindex_file, ret);
3129 * Close our connection with the file-sharing service.
3130 * The callback given to GNUNET_FS_start will no longer be
3131 * called after this function returns.
3133 * @param h handle that was returned from #GNUNET_FS_start()
3136 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3138 while (h->top_head != NULL)
3139 h->top_head->ssf (h->top_head->ssf_cls);
3140 if (h->queue_job != NULL)
3141 GNUNET_SCHEDULER_cancel (h->queue_job);
3142 GNUNET_free (h->client_name);
3147 /* end of fs_api.c */