2 This file is part of GNUnet.
3 (C) 2001--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief main FS functions (master initialization, serialization, deserialization, shared code)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
34 * How many block requests can we have outstanding in parallel at a time by default?
36 #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
39 * How many downloads can we have outstanding in parallel at a time by default?
41 #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
44 * Start the given job (send signal, remove from pending queue, update
45 * counters and state).
47 * @param qe job to start
50 start_job (struct GNUNET_FS_QueueEntry *qe)
52 GNUNET_assert (NULL == qe->client);
53 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
54 if (NULL == qe->client)
59 qe->start (qe->cls, qe->client);
61 qe->h->active_blocks += qe->blocks;
62 qe->h->active_downloads++;
63 qe->start_time = GNUNET_TIME_absolute_get ();
64 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head, qe->h->pending_tail, qe);
65 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head, qe->h->running_tail,
66 qe->h->running_tail, qe);
71 * Stop the given job (send signal, remove from active queue, update
72 * counters and state).
74 * @param qe job to stop
77 stop_job (struct GNUNET_FS_QueueEntry *qe)
81 GNUNET_assert (0 < qe->h->active_downloads);
82 qe->h->active_downloads--;
83 qe->h->active_blocks -= qe->blocks;
85 GNUNET_TIME_relative_add (qe->run_time,
86 GNUNET_TIME_absolute_get_duration
88 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
89 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
90 qe->h->pending_tail, qe);
95 * Process the jobs in the job queue, possibly starting some
96 * and stopping others.
98 * @param cls the 'struct GNUNET_FS_Handle'
99 * @param tc scheduler context
102 process_job_queue (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 struct GNUNET_FS_Handle *h = cls;
105 struct GNUNET_FS_QueueEntry *qe;
106 struct GNUNET_FS_QueueEntry *next;
107 struct GNUNET_TIME_Relative run_time;
108 struct GNUNET_TIME_Relative restart_at;
109 struct GNUNET_TIME_Relative rst;
110 struct GNUNET_TIME_Absolute end_time;
111 unsigned int num_downloads_waiting;
112 unsigned int num_downloads_active;
113 unsigned int num_downloads_expired;
114 unsigned int num_probes_active;
115 unsigned int num_probes_waiting;
116 unsigned int num_probes_expired;
117 int num_probes_change;
118 int num_downloads_change;
121 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
122 /* restart_at will be set to the time when it makes sense to
123 re-evaluate the job queue (unless, of course, jobs complete
124 or are added, then we'll be triggered immediately */
125 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
126 /* first, calculate some basic statistics on pending jobs */
127 num_probes_waiting = 0;
128 num_downloads_waiting = 0;
129 for (qe = h->pending_head; NULL != qe; qe = qe->next)
131 switch (qe->priority)
133 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
134 num_probes_waiting++;
136 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
137 num_downloads_waiting++;
144 /* now, calculate some basic statistics on running jobs */
145 num_probes_active = 0;
146 num_probes_expired = 0;
147 num_downloads_active = 0;
148 num_downloads_expired = 0;
149 for (qe = h->running_head; NULL != qe; qe = qe->next)
152 GNUNET_TIME_relative_multiply (h->avg_block_latency,
153 qe->blocks * qe->start_times);
154 switch (qe->priority)
156 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
158 /* run probes for at most 1s * number-of-restarts; note that
159 as the total runtime of a probe is limited to 2m, we don't
160 need to additionally limit the total time of a probe to
161 strictly limit its lifetime. */
162 run_time = GNUNET_TIME_relative_min (run_time,
163 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
164 1 + qe->start_times));
165 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
166 rst = GNUNET_TIME_absolute_get_remaining (end_time);
167 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
168 if (0 == rst.rel_value)
169 num_probes_expired++;
171 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
172 num_downloads_active++;
173 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
174 rst = GNUNET_TIME_absolute_get_remaining (end_time);
175 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
176 if (0 == rst.rel_value)
177 num_downloads_expired++;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
189 num_downloads_active,
190 num_downloads_expired,
191 num_downloads_waiting);
193 /* calculate stop decisions */
194 num_probes_change = 0;
195 num_downloads_change = 0;
196 if (h->active_downloads + num_downloads_waiting > h->max_parallel_requests)
198 if (num_probes_active > 0)
199 num_probes_change = - GNUNET_MIN (num_probes_active,
200 h->max_parallel_requests - (h->active_downloads + num_downloads_waiting));
201 else if (h->active_downloads + num_downloads_waiting > h->max_parallel_requests)
202 num_downloads_change = - GNUNET_MIN (num_downloads_expired,
203 h->max_parallel_requests - (h->active_downloads + num_downloads_waiting));
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Stopping %d probes and %d downloads\n",
209 num_downloads_change);
210 /* then, check if we should stop some jobs */
211 next = h->running_head;
212 while (NULL != (qe = next))
216 GNUNET_TIME_relative_multiply (h->avg_block_latency,
217 qe->blocks * qe->start_times);
218 switch (qe->priority)
220 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
221 /* run probes for at most 1s * number-of-restarts; note that
222 as the total runtime of a probe is limited to 2m, we don't
223 need to additionally limit the total time of a probe to
224 strictly limit its lifetime. */
225 run_time = GNUNET_TIME_relative_min (run_time,
226 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
227 1 + qe->start_times));
228 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
229 rst = GNUNET_TIME_absolute_get_remaining (end_time);
230 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
231 if ( (num_probes_change < 0) &&
232 ( (num_probes_expired < - num_probes_change) ||
233 (0 == rst.rel_value) ) )
237 if (0 == rst.rel_value)
238 num_probes_expired--;
241 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
242 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
243 rst = GNUNET_TIME_absolute_get_remaining (end_time);
244 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
245 if ( (num_downloads_change < 0) &&
246 ( (num_downloads_expired < - num_downloads_change) ||
247 (0 == rst.rel_value) ) )
250 num_downloads_change++;
251 if (0 == rst.rel_value)
252 num_downloads_expired--;
260 GNUNET_break (0 == num_downloads_change);
261 GNUNET_break (0 == num_probes_change);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 h->max_parallel_requests);
268 /* calculate start decisions */
269 if (h->active_downloads + num_downloads_waiting < h->max_parallel_requests)
271 /* can start all downloads, fill rest with probes */
272 num_downloads_change = num_downloads_waiting;
273 num_probes_change = GNUNET_MIN (num_probes_waiting,
274 h->max_parallel_requests - (h->active_downloads + num_downloads_waiting));
278 num_downloads_change = h->max_parallel_requests - h->active_downloads;
279 num_probes_change = 0;
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Starting %d probes and %d downloads\n",
284 num_downloads_change);
286 /* finally, start some more tasks if we now have empty slots */
287 block_limit_hit = GNUNET_NO;
288 next = h->pending_head;
289 while (NULL != (qe = next))
292 switch (qe->priority)
294 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
295 if (num_probes_change > 0)
301 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
302 if ( (num_downloads_change > 0) &&
303 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
304 ( (qe->blocks > h->max_parallel_requests) &&
305 (0 == h->active_downloads) ) ) )
308 num_downloads_change--;
310 else if (num_downloads_change > 0)
311 block_limit_hit = GNUNET_YES;
318 GNUNET_break ( (0 == num_downloads_change) || (GNUNET_YES == block_limit_hit) );
319 GNUNET_break (0 == num_probes_change);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Left with %d probes and %d downloads to start\n",
323 num_downloads_change);
325 /* make sure we run again */
327 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
332 * Add a job to the queue.
334 * @param h handle to the overall FS state
335 * @param start function to call to begin the job
336 * @param stop function to call to pause the job, or on dequeue (if the job was running)
337 * @param cls closure for start and stop
338 * @param blocks number of blocks this jobs uses
339 * @param priority how important is this download
340 * @return queue handle
342 struct GNUNET_FS_QueueEntry *
343 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h, GNUNET_FS_QueueStart start,
344 GNUNET_FS_QueueStop stop, void *cls, unsigned int blocks,
345 enum GNUNET_FS_QueuePriority priority)
347 struct GNUNET_FS_QueueEntry *qe;
349 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
354 qe->queue_time = GNUNET_TIME_absolute_get ();
356 qe->priority = priority;
357 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
358 h->pending_tail, qe);
359 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
360 GNUNET_SCHEDULER_cancel (h->queue_job);
361 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
367 * Dequeue a job from the queue.
368 * @param qh handle for the job
371 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
373 struct GNUNET_FS_Handle *h;
376 if (NULL != qh->client)
378 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qh);
380 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
381 GNUNET_SCHEDULER_cancel (h->queue_job);
382 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
387 * Create a top-level activity entry.
389 * @param h global fs handle
390 * @param ssf suspend signal function to use
391 * @param ssf_cls closure for ssf
392 * @return fresh top-level activity handle
394 struct TopLevelActivity *
395 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h, SuspendSignalFunction ssf,
398 struct TopLevelActivity *ret;
400 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
402 ret->ssf_cls = ssf_cls;
403 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
409 * Destroy a top-level activity entry.
411 * @param h global fs handle
412 * @param top top level activity entry
415 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top)
417 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
424 * Closure for "data_reader_file".
429 * Name of the file to read.
434 * File descriptor, NULL if it has not yet been opened.
436 struct GNUNET_DISK_FileHandle *fd;
441 * Function that provides data by reading from a file.
443 * @param cls closure (points to the file information)
444 * @param offset offset to read from; it is possible
445 * that the caller might need to go backwards
446 * a bit at times; set to UINT64_MAX to tell
447 * the reader that we won't be reading for a while
448 * (used to close the file descriptor but NOT fully
449 * clean up the reader's state); in this case,
450 * a value of '0' for max should be ignored
451 * @param max maximum number of bytes that should be
452 * copied to buf; readers are not allowed
453 * to provide less data unless there is an error;
454 * a value of "0" will be used at the end to allow
455 * the reader to clean up its internal state
456 * @param buf where the reader should write the data
457 * @param emsg location for the reader to store an error message
458 * @return number of bytes written, usually "max", 0 on error
461 GNUNET_FS_data_reader_file_ (void *cls, uint64_t offset, size_t max, void *buf,
464 struct FileInfo *fi = cls;
467 if (UINT64_MAX == offset)
471 GNUNET_DISK_file_close (fi->fd);
479 GNUNET_DISK_file_close (fi->fd);
480 GNUNET_free (fi->filename);
487 GNUNET_DISK_file_open (fi->filename, GNUNET_DISK_OPEN_READ,
488 GNUNET_DISK_PERM_NONE);
491 GNUNET_asprintf (emsg, _("Could not open file `%s': %s"), fi->filename,
496 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
497 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
500 GNUNET_asprintf (emsg, _("Could not read file `%s': %s"), fi->filename,
506 GNUNET_asprintf (emsg, _("Short read reading from file `%s'!"),
515 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
517 * @param filename file to read
518 * @return closure to use, NULL on error
521 GNUNET_FS_make_file_reader_context_ (const char *filename)
525 fi = GNUNET_malloc (sizeof (struct FileInfo));
526 fi->filename = GNUNET_STRINGS_filename_expand (filename);
527 if (NULL == fi->filename)
537 * Function that provides data by copying from a buffer.
539 * @param cls closure (points to the buffer)
540 * @param offset offset to read from; it is possible
541 * that the caller might need to go backwards
542 * a bit at times; set to UINT64_MAX to tell
543 * the reader that we won't be reading for a while
544 * (used to close the file descriptor but NOT fully
545 * clean up the reader's state); in this case,
546 * a value of '0' for max should be ignored
547 * @param max maximum number of bytes that should be
548 * copied to buf; readers are not allowed
549 * to provide less data unless there is an error;
550 * a value of "0" will be used at the end to allow
551 * the reader to clean up its internal state
552 * @param buf where the reader should write the data
553 * @param emsg location for the reader to store an error message
554 * @return number of bytes written, usually "max", 0 on error
557 GNUNET_FS_data_reader_copy_ (void *cls, uint64_t offset, size_t max, void *buf,
562 if (UINT64_MAX == offset)
566 GNUNET_free_non_null (data);
569 memcpy (buf, &data[offset], max);
575 * Return the full filename where we would store state information
576 * (for serialization/deserialization).
578 * @param h master context
579 * @param ext component of the path
580 * @param ent entity identifier (or emtpy string for the directory)
581 * @return NULL on error
584 get_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext,
590 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
591 return NULL; /* persistence not requested */
593 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
596 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
597 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
599 GNUNET_free (basename);
605 * Return the full filename where we would store state information
606 * (for serialization/deserialization) that is associated with a
609 * @param h master context
610 * @param ext component of the path
611 * @param uni name of the parent operation
612 * @param ent entity identifier (or emtpy string for the directory)
613 * @return NULL on error
616 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
617 const char *uni, const char *ent)
622 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
623 return NULL; /* persistence not requested */
625 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
628 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
629 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
630 uni, DIR_SEPARATOR_STR, ent);
631 GNUNET_free (basename);
637 * Return a read handle for deserialization.
639 * @param h master context
640 * @param ext component of the path
641 * @param ent entity identifier (or emtpy string for the directory)
642 * @return NULL on error
644 static struct GNUNET_BIO_ReadHandle *
645 get_read_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
648 struct GNUNET_BIO_ReadHandle *ret;
650 fn = get_serialization_file_name (h, ext, ent);
653 ret = GNUNET_BIO_read_open (fn);
660 * Return a write handle for serialization.
662 * @param h master context
663 * @param ext component of the path
664 * @param ent entity identifier (or emtpy string for the directory)
665 * @return NULL on error
667 static struct GNUNET_BIO_WriteHandle *
668 get_write_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
671 struct GNUNET_BIO_WriteHandle *ret;
673 fn = get_serialization_file_name (h, ext, ent);
676 ret = GNUNET_BIO_write_open (fn);
677 GNUNET_break (NULL != ret);
684 * Return a write handle for serialization.
686 * @param h master context
687 * @param ext component of the path
688 * @param uni name of parent
689 * @param ent entity identifier (or emtpy string for the directory)
690 * @return NULL on error
692 static struct GNUNET_BIO_WriteHandle *
693 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
694 const char *uni, const char *ent)
697 struct GNUNET_BIO_WriteHandle *ret;
699 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
702 ret = GNUNET_BIO_write_open (fn);
709 * Remove serialization/deserialization file from disk.
711 * @param h master context
712 * @param ext component of the path
713 * @param ent entity identifier
716 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h, const char *ext,
721 if ((NULL == ent) || (0 == strlen (ent)))
726 filename = get_serialization_file_name (h, ext, ent);
727 if (NULL != filename)
729 if (0 != UNLINK (filename))
730 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
731 GNUNET_free (filename);
737 * Remove serialization/deserialization file from disk.
739 * @param h master context
740 * @param ext component of the path
741 * @param uni parent name
742 * @param ent entity identifier
745 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
746 const char *uni, const char *ent)
750 if ((NULL == ent) || (0 == strlen (ent)))
755 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
756 if (NULL == filename)
758 if (0 != UNLINK (filename))
759 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
760 GNUNET_free (filename);
765 * Remove serialization/deserialization directory from disk.
767 * @param h master context
768 * @param ext component of the path
769 * @param uni unique name of parent
772 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h, const char *ext,
779 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
782 if ((GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
783 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
784 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
790 * Serialize a 'start_time'. Since we use start-times to
791 * calculate the duration of some operation, we actually
792 * do not serialize the absolute time but the (relative)
793 * duration since the start time. When we then
794 * deserialize the start time, we take the current time and
795 * subtract that duration so that we get again an absolute
796 * time stamp that will result in correct performance
799 * @param wh handle for writing
800 * @param timestamp time to serialize
801 * @return GNUNET_OK on success
804 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
805 struct GNUNET_TIME_Absolute timestamp)
807 struct GNUNET_TIME_Relative dur;
809 dur = GNUNET_TIME_absolute_get_duration (timestamp);
810 return GNUNET_BIO_write_int64 (wh, dur.rel_value);
815 * Serialize a 'start_time'. Since we use start-times to
816 * calculate the duration of some operation, we actually
817 * do not serialize the absolute time but the (relative)
818 * duration since the start time. When we then
819 * deserialize the start time, we take the current time and
820 * subtract that duration so that we get again an absolute
821 * time stamp that will result in correct performance
824 * @param rh handle for reading
825 * @param timestamp where to write the deserialized timestamp
826 * @return GNUNET_OK on success
829 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
830 struct GNUNET_TIME_Absolute *timestamp)
832 struct GNUNET_TIME_Relative dur;
834 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value))
835 return GNUNET_SYSERR;
836 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
842 * Using the given serialization filename, try to deserialize
843 * the file-information tree associated with it.
845 * @param h master context
846 * @param filename name of the file (without directory) with
848 * @return NULL on error
850 static struct GNUNET_FS_FileInformation *
851 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename);
855 * Using the given serialization filename, try to deserialize
856 * the file-information tree associated with it.
858 * @param h master context
859 * @param fn name of the file (without directory) with
861 * @param rh handle for reading
862 * @return NULL on error
864 static struct GNUNET_FS_FileInformation *
865 deserialize_fi_node (struct GNUNET_FS_Handle *h, const char *fn,
866 struct GNUNET_BIO_ReadHandle *rh)
868 struct GNUNET_FS_FileInformation *ret;
869 struct GNUNET_FS_FileInformation *nxt;
876 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
881 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
886 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
887 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
889 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
890 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
891 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
893 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
894 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
895 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
896 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
898 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
900 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value)) ||
901 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
902 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
903 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
910 case 0: /* file-insert */
911 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
916 ret->is_directory = GNUNET_NO;
917 ret->data.file.do_index = GNUNET_NO;
918 ret->data.file.have_hash = GNUNET_NO;
919 ret->data.file.index_start_confirmed = GNUNET_NO;
920 if (GNUNET_NO == ret->is_published)
922 if (NULL == ret->filename)
924 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
925 ret->data.file.reader_cls =
926 GNUNET_malloc_large (ret->data.file.file_size);
927 if (ret->data.file.reader_cls == NULL)
930 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
931 ret->data.file.file_size))
939 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
940 ret->data.file.reader_cls =
941 GNUNET_FS_make_file_reader_context_ (ret->filename);
945 case 1: /* file-index, no hash */
946 if (NULL == ret->filename)
951 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
956 ret->is_directory = GNUNET_NO;
957 ret->data.file.do_index = GNUNET_YES;
958 ret->data.file.have_hash = GNUNET_NO;
959 ret->data.file.index_start_confirmed = GNUNET_NO;
960 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
961 ret->data.file.reader_cls =
962 GNUNET_FS_make_file_reader_context_ (ret->filename);
964 case 2: /* file-index-with-hash */
965 if (NULL == ret->filename)
970 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
972 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
973 sizeof (struct GNUNET_HashCode))))
978 ret->is_directory = GNUNET_NO;
979 ret->data.file.do_index = GNUNET_YES;
980 ret->data.file.have_hash = GNUNET_YES;
981 ret->data.file.index_start_confirmed = GNUNET_NO;
982 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
983 ret->data.file.reader_cls =
984 GNUNET_FS_make_file_reader_context_ (ret->filename);
986 case 3: /* file-index-with-hash-confirmed */
987 if (NULL == ret->filename)
992 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
994 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
995 sizeof (struct GNUNET_HashCode))))
1000 ret->is_directory = GNUNET_NO;
1001 ret->data.file.do_index = GNUNET_YES;
1002 ret->data.file.have_hash = GNUNET_YES;
1003 ret->data.file.index_start_confirmed = GNUNET_YES;
1004 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1005 ret->data.file.reader_cls =
1006 GNUNET_FS_make_file_reader_context_ (ret->filename);
1008 case 4: /* directory */
1009 ret->is_directory = GNUNET_YES;
1010 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1011 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1013 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1015 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1020 ret->data.dir.dir_size = (uint32_t) dsize;
1021 if (NULL != filename)
1023 ret->data.dir.entries = deserialize_file_information (h, filename);
1024 GNUNET_free (filename);
1026 nxt = ret->data.dir.entries;
1038 ret->serialization = GNUNET_strdup (fn);
1040 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1045 if (NULL != filename)
1047 ret->next = deserialize_file_information (h, filename);
1048 GNUNET_free (filename);
1051 GNUNET_free_non_null (ksks);
1052 GNUNET_free_non_null (chks);
1055 GNUNET_free_non_null (ksks);
1056 GNUNET_free_non_null (chks);
1057 GNUNET_free_non_null (filename);
1058 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1064 * Using the given serialization filename, try to deserialize
1065 * the file-information tree associated with it.
1067 * @param h master context
1068 * @param filename name of the file (without directory) with
1070 * @return NULL on error
1072 static struct GNUNET_FS_FileInformation *
1073 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename)
1075 struct GNUNET_FS_FileInformation *ret;
1076 struct GNUNET_BIO_ReadHandle *rh;
1080 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1083 ret = deserialize_fi_node (h, filename, rh);
1084 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1087 _("Failed to resume publishing information `%s': %s\n"),
1093 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1096 if (0 != UNLINK (fn))
1097 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1106 * Given a serialization name (full absolute path), return the
1107 * basename of the file (without the path), which must only
1108 * consist of the 6 random characters.
1110 * @param fullname name to extract the basename from
1111 * @return copy of the basename, NULL on error
1114 get_serialization_short_name (const char *fullname)
1121 /* FIXME: we could do this faster since we know
1122 * the length of 'end'... */
1123 while ('\0' != *nxt)
1125 if (DIR_SEPARATOR == *nxt)
1129 if ((NULL == end) || (0 == strlen (end)))
1134 GNUNET_break (6 == strlen (end));
1135 return GNUNET_strdup (end);
1140 * Create a new random name for serialization. Also checks if persistence
1141 * is enabled and returns NULL if not.
1143 * @param h master context
1144 * @param ext component of the path
1145 * @return NULL on errror
1148 make_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext)
1154 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1155 return NULL; /* persistence not requested */
1156 dn = get_serialization_file_name (h, ext, "");
1159 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1164 fn = GNUNET_DISK_mktemp (dn);
1167 return NULL; /* epic fail */
1168 ret = get_serialization_short_name (fn);
1175 * Create a new random name for serialization. Also checks if persistence
1176 * is enabled and returns NULL if not.
1178 * @param h master context
1179 * @param ext component of the path
1180 * @param uni name of parent
1181 * @return NULL on errror
1184 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1185 const char *ext, const char *uni)
1191 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1192 return NULL; /* persistence not requested */
1193 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1196 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1201 fn = GNUNET_DISK_mktemp (dn);
1204 return NULL; /* epic fail */
1205 ret = get_serialization_short_name (fn);
1212 * Copy all of the data from the reader to the write handle.
1214 * @param wh write handle
1215 * @param fi file with reader
1216 * @return GNUNET_OK on success
1219 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1220 struct GNUNET_FS_FileInformation *fi)
1222 char buf[32 * 1024];
1230 while (off < fi->data.file.file_size)
1232 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1234 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1238 return GNUNET_SYSERR;
1240 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1241 return GNUNET_SYSERR;
1249 * Create a temporary file on disk to store the current
1252 * @param fi file information to sync with disk
1255 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1258 struct GNUNET_BIO_WriteHandle *wh;
1263 if (NULL == fi->serialization)
1265 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1266 if (NULL == fi->serialization)
1268 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1272 GNUNET_free (fi->serialization);
1273 fi->serialization = NULL;
1276 if (GNUNET_YES == fi->is_directory)
1278 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1280 else if (GNUNET_YES == fi->data.file.have_hash)
1282 else if (GNUNET_YES == fi->data.file.do_index)
1286 if (NULL != fi->keywords)
1287 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1290 if (NULL != fi->chk_uri)
1291 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1294 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1295 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1296 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1297 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1298 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1299 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1300 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1302 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value)) ||
1303 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1304 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1305 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1310 GNUNET_free_non_null (chks);
1312 GNUNET_free_non_null (ksks);
1317 case 0: /* file-insert */
1318 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1323 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1324 if (GNUNET_OK != copy_from_reader (wh, fi))
1330 case 1: /* file-index, no hash */
1331 if (NULL == fi->filename)
1336 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1342 case 2: /* file-index-with-hash */
1343 case 3: /* file-index-with-hash-confirmed */
1344 if (NULL == fi->filename)
1349 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1351 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1352 sizeof (struct GNUNET_HashCode))))
1358 case 4: /* directory */
1359 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1361 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1362 (uint32_t) fi->data.dir.dir_size)) ||
1364 GNUNET_BIO_write_string (wh,
1365 (fi->data.dir.entries ==
1366 NULL) ? NULL : fi->data.dir.
1367 entries->serialization)))
1378 GNUNET_BIO_write_string (wh,
1380 NULL) ? fi->next->serialization : NULL))
1385 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1394 (void) GNUNET_BIO_write_close (wh);
1395 GNUNET_free_non_null (chks);
1396 GNUNET_free_non_null (ksks);
1397 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1401 if (0 != UNLINK (fn))
1402 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1405 GNUNET_free (fi->serialization);
1406 fi->serialization = NULL;
1412 * Find the entry in the file information struct where the
1413 * serialization filename matches the given name.
1415 * @param pos file information to search
1416 * @param srch filename to search for
1417 * @return NULL if srch was not found in this subtree
1419 static struct GNUNET_FS_FileInformation *
1420 find_file_position (struct GNUNET_FS_FileInformation *pos, const char *srch)
1422 struct GNUNET_FS_FileInformation *r;
1426 if (0 == strcmp (srch, pos->serialization))
1428 if ( (GNUNET_YES == pos->is_directory) &&
1429 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1438 * Signal the FS's progress function that we are resuming
1441 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1442 * @param fi the entry in the publish-structure
1443 * @param length length of the file or directory
1444 * @param meta metadata for the file or directory (can be modified)
1445 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1446 * @param bo block options (can be modified)
1447 * @param do_index should we index?
1448 * @param client_info pointer to client context set upon creation (can be modified)
1449 * @return GNUNET_OK to continue (always)
1452 fip_signal_resume (void *cls, struct GNUNET_FS_FileInformation *fi,
1453 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1454 struct GNUNET_FS_Uri **uri,
1455 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1458 struct GNUNET_FS_PublishContext *pc = cls;
1459 struct GNUNET_FS_ProgressInfo pi;
1461 if (GNUNET_YES == pc->skip_next_fi_callback)
1463 pc->skip_next_fi_callback = GNUNET_NO;
1466 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1467 pi.value.publish.specifics.resume.message = pc->fi->emsg;
1468 pi.value.publish.specifics.resume.chk_uri = pc->fi->chk_uri;
1469 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1470 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1472 /* process entries in directory */
1473 pc->skip_next_fi_callback = GNUNET_YES;
1474 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1481 * Function called with a filename of serialized publishing operation
1484 * @param cls the 'struct GNUNET_FS_Handle*'
1485 * @param filename complete filename (absolute path)
1486 * @return GNUNET_OK (continue to iterate)
1489 deserialize_publish_file (void *cls, const char *filename)
1491 struct GNUNET_FS_Handle *h = cls;
1492 struct GNUNET_BIO_ReadHandle *rh;
1493 struct GNUNET_FS_PublishContext *pc;
1501 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1503 pc->serialization = get_serialization_short_name (filename);
1507 rh = GNUNET_BIO_read_open (filename);
1513 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1515 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1516 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1517 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1519 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1520 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1521 || (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)))
1526 pc->options = options;
1527 pc->all_done = all_done;
1528 if (NULL == fi_root)
1533 pc->fi = deserialize_file_information (h, fi_root);
1541 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1542 if (NULL == pc->namespace)
1544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1546 ("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1551 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1552 (GNUNET_YES != pc->all_done))
1554 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1555 if (NULL == pc->dsh)
1560 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1561 GNUNET_free (fi_pos);
1563 if (NULL == pc->fi_pos)
1565 /* failed to find position for resuming, outch! Will start from root! */
1567 if (GNUNET_YES != pc->all_done)
1568 pc->fi_pos = pc->fi;
1571 GNUNET_free (fi_root);
1573 /* generate RESUME event(s) */
1574 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1576 /* re-start publishing (if needed)... */
1577 if (GNUNET_YES != pc->all_done)
1579 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1581 GNUNET_SCHEDULER_add_with_priority
1582 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1583 &GNUNET_FS_publish_main_, pc);
1585 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1588 _("Failure while resuming publishing operation `%s': %s\n"),
1592 GNUNET_free_non_null (ns);
1593 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1596 GNUNET_free_non_null (pc->nid);
1597 GNUNET_free_non_null (pc->nuid);
1598 GNUNET_free_non_null (fi_root);
1599 GNUNET_free_non_null (fi_pos);
1600 GNUNET_free_non_null (ns);
1601 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1604 _("Failed to resume publishing operation `%s': %s\n"), filename,
1609 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1610 if (0 != UNLINK (filename))
1611 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1612 GNUNET_free (pc->serialization);
1619 * Synchronize this publishing struct with its mirror
1620 * on disk. Note that all internal FS-operations that change
1621 * publishing structs should already call "sync" internally,
1622 * so this function is likely not useful for clients.
1624 * @param pc the struct to sync
1627 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1629 struct GNUNET_BIO_WriteHandle *wh;
1631 if (NULL == pc->serialization)
1633 make_serialization_file_name (pc->h,
1634 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1635 if (NULL == pc->serialization)
1639 if (NULL == pc->fi->serialization)
1644 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1651 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1652 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1653 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1654 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1655 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1657 GNUNET_BIO_write_string (wh,
1658 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1660 GNUNET_BIO_write_string (wh,
1661 (NULL == pc->namespace) ? NULL : pc->namespace->name)))
1666 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1675 (void) GNUNET_BIO_write_close (wh);
1676 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1678 GNUNET_free (pc->serialization);
1679 pc->serialization = NULL;
1684 * Synchronize this unindex struct with its mirror
1685 * on disk. Note that all internal FS-operations that change
1686 * publishing structs should already call "sync" internally,
1687 * so this function is likely not useful for clients.
1689 * @param uc the struct to sync
1692 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1694 struct GNUNET_BIO_WriteHandle *wh;
1697 if (NULL == uc->serialization)
1699 make_serialization_file_name (uc->h,
1700 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1701 if (NULL == uc->serialization)
1703 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1710 if (NULL != uc->ksk_uri)
1711 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1714 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1715 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1716 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1717 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1719 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1720 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1721 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1722 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1724 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1725 ((uc->state == UNINDEX_STATE_ERROR) &&
1726 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1731 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1740 (void) GNUNET_BIO_write_close (wh);
1741 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1743 GNUNET_free (uc->serialization);
1744 uc->serialization = NULL;
1749 * Serialize a download request.
1751 * @param wh the 'struct GNUNET_BIO_WriteHandle*'
1752 * @param dr the 'struct DownloadRequest'
1753 * @return GNUNET_YES on success, GNUNET_NO on error
1756 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1757 struct DownloadRequest *dr)
1761 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1762 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1763 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1764 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1766 if ((BRS_CHK_SET == dr->state) &&
1768 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1770 for (i = 0; i < dr->num_children; i++)
1771 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1778 * Read a download request tree.
1780 * @param rh stream to read from
1781 * @return value the 'struct DownloadRequest', NULL on error
1783 static struct DownloadRequest *
1784 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1786 struct DownloadRequest *dr;
1789 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1790 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1791 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1792 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1793 (dr->num_children > CHK_PER_INODE) ||
1794 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) || ((0 == dr->depth)
1798 ((dr->depth > 0) && (0 == dr->num_children)))
1801 dr->num_children = 0;
1804 if (dr->num_children > 0)
1806 GNUNET_malloc (dr->num_children * sizeof (struct ContentHashKey));
1810 case BRS_RECONSTRUCT_DOWN:
1811 case BRS_RECONSTRUCT_META_UP:
1812 case BRS_RECONSTRUCT_UP:
1816 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1819 case BRS_DOWNLOAD_DOWN:
1820 case BRS_DOWNLOAD_UP:
1827 for (i = 0; i < dr->num_children; i++)
1829 if (NULL == (dr->children[i] = read_download_request (rh)))
1831 dr->children[i]->parent = dr;
1835 GNUNET_FS_free_download_request_ (dr);
1841 * Compute the name of the sync file (or directory) for the given download
1844 * @param dc download context to compute for
1845 * @param uni unique filename to use, use "" for the directory name
1846 * @param ext extension to use, use ".dir" for our own subdirectory
1847 * @return the expanded file name, NULL for none
1850 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1851 const char *uni, const char *ext)
1856 if (dc->parent == NULL)
1857 return get_serialization_file_name (dc->h,
1858 (dc->search != NULL) ?
1859 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1860 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1862 if (NULL == dc->parent->serialization)
1864 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1867 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1874 * Synchronize this download struct with its mirror
1875 * on disk. Note that all internal FS-operations that change
1876 * publishing structs should already call "sync" internally,
1877 * so this function is likely not useful for clients.
1879 * @param dc the struct to sync
1882 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1884 struct GNUNET_BIO_WriteHandle *wh;
1889 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1890 return; /* we don't sync probes */
1891 if (NULL == dc->serialization)
1893 dir = get_download_sync_filename (dc, "", "");
1896 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1901 fn = GNUNET_DISK_mktemp (dir);
1905 dc->serialization = get_serialization_short_name (fn);
1909 fn = get_download_sync_filename (dc, dc->serialization, "");
1912 GNUNET_free (dc->serialization);
1913 dc->serialization = NULL;
1918 wh = GNUNET_BIO_write_open (fn);
1921 GNUNET_free (dc->serialization);
1922 dc->serialization = NULL;
1926 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1927 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1928 uris = GNUNET_FS_uri_to_string (dc->uri);
1929 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1930 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1931 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1932 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1933 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1934 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1935 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1936 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1937 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1938 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1939 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1940 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1941 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1946 if (NULL == dc->emsg)
1948 GNUNET_assert (dc->top_request != NULL);
1949 if (GNUNET_YES != write_download_request (wh, dc->top_request))
1955 GNUNET_free_non_null (uris);
1957 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1967 (void) GNUNET_BIO_write_close (wh);
1968 GNUNET_free_non_null (uris);
1969 if (0 != UNLINK (fn))
1970 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1972 GNUNET_free (dc->serialization);
1973 dc->serialization = NULL;
1978 * Synchronize this search result with its mirror
1979 * on disk. Note that all internal FS-operations that change
1980 * publishing structs should already call "sync" internally,
1981 * so this function is likely not useful for clients.
1983 * @param sr the struct to sync
1986 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1988 struct GNUNET_BIO_WriteHandle *wh;
1992 if (NULL == sr->serialization)
1994 make_serialization_file_name_in_dir (sr->sc->h,
1995 (sr->sc->psearch_result ==
1997 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1998 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1999 sr->sc->serialization);
2000 if (NULL == sr->serialization)
2002 wh = get_write_handle_in_dir (sr->sc->h,
2003 (sr->sc->psearch_result ==
2004 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2005 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2006 sr->sc->serialization, sr->serialization);
2012 uris = GNUNET_FS_uri_to_string (sr->uri);
2013 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2015 GNUNET_BIO_write_string (wh,
2017 NULL ? sr->download->serialization : NULL)) ||
2019 GNUNET_BIO_write_string (wh,
2020 sr->update_search !=
2021 NULL ? sr->update_search->serialization : NULL))
2022 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2023 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2024 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2025 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2026 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2027 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2032 if ( (NULL != sr->uri) &&
2033 (ksk == sr->sc->uri->type) &&
2034 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2035 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2040 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2046 GNUNET_free_non_null (uris);
2049 GNUNET_free_non_null (uris);
2051 (void) GNUNET_BIO_write_close (wh);
2052 remove_sync_file_in_dir (sr->sc->h,
2053 (NULL == sr->sc->psearch_result)
2054 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2055 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2056 sr->sc->serialization, sr->serialization);
2057 GNUNET_free (sr->serialization);
2058 sr->serialization = NULL;
2063 * Synchronize this search struct with its mirror
2064 * on disk. Note that all internal FS-operations that change
2065 * publishing structs should already call "sync" internally,
2066 * so this function is likely not useful for clients.
2068 * @param sc the struct to sync
2071 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2073 struct GNUNET_BIO_WriteHandle *wh;
2076 const char *category;
2079 (NULL == sc->psearch_result)
2080 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2081 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2082 if (NULL == sc->serialization)
2083 sc->serialization = make_serialization_file_name (sc->h, category);
2084 if (NULL == sc->serialization)
2087 wh = get_write_handle (sc->h, category, sc->serialization);
2093 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2094 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2095 uris = GNUNET_FS_uri_to_string (sc->uri);
2096 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2097 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2098 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2099 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2100 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2101 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2102 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2109 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2118 (void) GNUNET_BIO_write_close (wh);
2119 GNUNET_free_non_null (uris);
2120 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2121 GNUNET_free (sc->serialization);
2122 sc->serialization = NULL;
2127 * Function called with a filename of serialized unindexing operation
2130 * @param cls the 'struct GNUNET_FS_Handle*'
2131 * @param filename complete filename (absolute path)
2132 * @return GNUNET_OK (continue to iterate)
2135 deserialize_unindex_file (void *cls, const char *filename)
2137 struct GNUNET_FS_Handle *h = cls;
2138 struct GNUNET_BIO_ReadHandle *rh;
2139 struct GNUNET_FS_UnindexContext *uc;
2140 struct GNUNET_FS_ProgressInfo pi;
2145 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
2147 uc->serialization = get_serialization_short_name (filename);
2148 rh = GNUNET_BIO_read_open (filename);
2156 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2157 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2158 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2159 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2160 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2161 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2162 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2164 GNUNET_free_non_null (uris);
2170 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2172 if (NULL == uc->ksk_uri)
2178 if ( (uc->ksk_offset > 0) &&
2179 ( (NULL == uc->ksk_uri) ||
2180 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2185 uc->state = (enum UnindexState) state;
2188 case UNINDEX_STATE_HASHING:
2190 case UNINDEX_STATE_FS_NOTIFY:
2192 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2193 sizeof (struct GNUNET_HashCode)))
2199 case UNINDEX_STATE_DS_REMOVE:
2200 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2201 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2203 case UNINDEX_STATE_COMPLETE:
2205 case UNINDEX_STATE_ERROR:
2207 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2217 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2218 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2219 pi.value.unindex.specifics.resume.message = uc->emsg;
2220 GNUNET_FS_unindex_make_status_ (&pi, uc,
2222 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2225 case UNINDEX_STATE_HASHING:
2227 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2229 &GNUNET_FS_unindex_process_hash_, uc);
2231 case UNINDEX_STATE_FS_NOTIFY:
2232 uc->state = UNINDEX_STATE_HASHING;
2233 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2235 case UNINDEX_STATE_DS_REMOVE:
2236 GNUNET_FS_unindex_do_remove_ (uc);
2238 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2239 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2241 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2242 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2244 case UNINDEX_STATE_COMPLETE:
2245 case UNINDEX_STATE_ERROR:
2246 /* no need to resume any operation, we were done */
2251 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2254 _("Failure while resuming unindexing operation `%s': %s\n"),
2260 GNUNET_free_non_null (uc->filename);
2261 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2264 _("Failed to resume unindexing operation `%s': %s\n"), filename,
2268 if (NULL != uc->serialization)
2269 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2271 GNUNET_free_non_null (uc->serialization);
2278 * Deserialize a download.
2280 * @param h overall context
2281 * @param rh file to deserialize from
2282 * @param parent parent download
2283 * @param search associated search
2284 * @param serialization name under which the search was serialized
2287 deserialize_download (struct GNUNET_FS_Handle *h,
2288 struct GNUNET_BIO_ReadHandle *rh,
2289 struct GNUNET_FS_DownloadContext *parent,
2290 struct GNUNET_FS_SearchResult *search,
2291 const char *serialization);
2295 * Deserialize a search.
2297 * @param h overall context
2298 * @param rh file to deserialize from
2299 * @param psearch_result parent search result
2300 * @param serialization name under which the search was serialized
2302 static struct GNUNET_FS_SearchContext *
2303 deserialize_search (struct GNUNET_FS_Handle *h,
2304 struct GNUNET_BIO_ReadHandle *rh,
2305 struct GNUNET_FS_SearchResult *psearch_result,
2306 const char *serialization);
2310 * Function called with a filename of serialized search result
2313 * @param cls the 'struct GNUNET_FS_SearchContext*'
2314 * @param filename complete filename (absolute path)
2315 * @return GNUNET_OK (continue to iterate)
2318 deserialize_search_result (void *cls, const char *filename)
2320 struct GNUNET_FS_SearchContext *sc = cls;
2326 struct GNUNET_BIO_ReadHandle *rh;
2327 struct GNUNET_BIO_ReadHandle *drh;
2328 struct GNUNET_FS_SearchResult *sr;
2330 ser = get_serialization_short_name (filename);
2331 rh = GNUNET_BIO_read_open (filename);
2336 remove_sync_file_in_dir (sc->h,
2337 (NULL == sc->psearch_result)
2338 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2339 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2340 sc->serialization, ser);
2349 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2351 sr->serialization = ser;
2352 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2353 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2354 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2356 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2357 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2359 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2360 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2361 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2362 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2363 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2368 if (ksk == sr->sc->uri->type)
2370 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2371 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2373 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2380 if (NULL != download)
2382 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2385 deserialize_download (sc->h, drh, NULL, sr, download);
2386 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2389 _("Failed to resume sub-download `%s': %s\n"), download,
2394 GNUNET_free (download);
2396 if (NULL != update_srch)
2399 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2402 deserialize_search (sc->h, drh, sr, update_srch);
2403 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2406 _("Failed to resume sub-search `%s': %s\n"), update_srch,
2411 GNUNET_free (update_srch);
2413 GNUNET_break (GNUNET_YES ==
2414 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &sr->key, sr,
2415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2416 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2419 _("Failure while resuming search operation `%s': %s\n"),
2425 GNUNET_free_non_null (download);
2426 GNUNET_free_non_null (emsg);
2427 GNUNET_free_non_null (uris);
2428 GNUNET_free_non_null (update_srch);
2429 if (NULL != sr->uri)
2430 GNUNET_FS_uri_destroy (sr->uri);
2431 if (NULL != sr->meta)
2432 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2433 GNUNET_free (sr->serialization);
2435 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2438 _("Failure while resuming search operation `%s': %s\n"),
2447 * Send the 'resume' signal to the callback; also actually
2448 * resume the download (put it in the queue). Does this
2449 * recursively for the top-level download and all child
2452 * @param dc download to resume
2455 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2457 struct GNUNET_FS_DownloadContext *dcc;
2458 struct GNUNET_FS_ProgressInfo pi;
2460 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2461 pi.value.download.specifics.resume.meta = dc->meta;
2462 pi.value.download.specifics.resume.message = dc->emsg;
2463 GNUNET_FS_download_make_status_ (&pi, dc);
2464 dcc = dc->child_head;
2467 signal_download_resume (dcc);
2470 if (NULL != dc->pending_head)
2471 GNUNET_FS_download_start_downloading_ (dc);
2476 * Signal resuming of a search to our clients (for the
2477 * top level search and all sub-searches).
2479 * @param sc search being resumed
2482 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2486 * Iterator over search results signaling resume to the client for
2489 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2490 * @param key current key code
2491 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2492 * @return GNUNET_YES (we should continue to iterate)
2495 signal_result_resume (void *cls, const struct GNUNET_HashCode * key, void *value)
2497 struct GNUNET_FS_SearchContext *sc = cls;
2498 struct GNUNET_FS_ProgressInfo pi;
2499 struct GNUNET_FS_SearchResult *sr = value;
2501 if (0 == sr->mandatory_missing)
2503 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2504 pi.value.search.specifics.resume_result.meta = sr->meta;
2505 pi.value.search.specifics.resume_result.uri = sr->uri;
2506 pi.value.search.specifics.resume_result.result = sr;
2507 pi.value.search.specifics.resume_result.availability_rank =
2508 2 * sr->availability_success - sr->availability_trials;
2509 pi.value.search.specifics.resume_result.availability_certainty =
2510 sr->availability_trials;
2511 pi.value.search.specifics.resume_result.applicability_rank =
2512 sr->optional_support;
2513 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2515 if (NULL != sr->download)
2517 signal_download_resume (sr->download);
2521 GNUNET_FS_search_start_probe_ (sr);
2523 if (NULL != sr->update_search)
2524 signal_search_resume (sr->update_search);
2530 * Free memory allocated by the search context and its children
2532 * @param sc search context to free
2535 free_search_context (struct GNUNET_FS_SearchContext *sc);
2539 * Iterator over search results freeing each.
2541 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2542 * @param key current key code
2543 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2544 * @return GNUNET_YES (we should continue to iterate)
2547 free_result (void *cls, const struct GNUNET_HashCode * key, void *value)
2549 struct GNUNET_FS_SearchResult *sr = value;
2551 if (NULL != sr->update_search)
2553 free_search_context (sr->update_search);
2554 GNUNET_assert (NULL == sr->update_search);
2556 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2557 GNUNET_FS_uri_destroy (sr->uri);
2564 * Free memory allocated by the search context and its children
2566 * @param sc search context to free
2569 free_search_context (struct GNUNET_FS_SearchContext *sc)
2571 if (NULL != sc->serialization)
2573 GNUNET_FS_remove_sync_file_ (sc->h,
2574 (sc->psearch_result ==
2575 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2576 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2578 GNUNET_FS_remove_sync_dir_ (sc->h,
2579 (sc->psearch_result ==
2580 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2581 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2584 GNUNET_free_non_null (sc->serialization);
2585 GNUNET_free_non_null (sc->emsg);
2586 if (NULL != sc->uri)
2587 GNUNET_FS_uri_destroy (sc->uri);
2588 if (NULL != sc->master_result_map)
2590 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2592 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2599 * Function called with a filename of serialized sub-download
2602 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2603 * @param filename complete filename (absolute path)
2604 * @return GNUNET_OK (continue to iterate)
2607 deserialize_subdownload (void *cls, const char *filename)
2609 struct GNUNET_FS_DownloadContext *parent = cls;
2612 struct GNUNET_BIO_ReadHandle *rh;
2614 ser = get_serialization_short_name (filename);
2615 rh = GNUNET_BIO_read_open (filename);
2618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2620 ("Failed to resume sub-download `%s': could not open file `%s'\n"),
2625 deserialize_download (parent->h, rh, parent, NULL, ser);
2626 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2629 _("Failed to resume sub-download `%s': %s\n"), ser, emsg);
2638 * Free this download context and all of its descendants.
2639 * (only works during deserialization since not all possible
2640 * state it taken care of).
2642 * @param dc context to free
2645 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2647 struct GNUNET_FS_DownloadContext *dcc;
2649 if (NULL != dc->meta)
2650 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2651 if (NULL != dc->uri)
2652 GNUNET_FS_uri_destroy (dc->uri);
2653 GNUNET_free_non_null (dc->temp_filename);
2654 GNUNET_free_non_null (dc->emsg);
2655 GNUNET_free_non_null (dc->filename);
2656 GNUNET_free_non_null (dc->serialization);
2657 while (NULL != (dcc = dc->child_head))
2659 GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
2660 free_download_context (dcc);
2662 GNUNET_FS_free_download_request_ (dc->top_request);
2663 if (NULL != dc->active)
2664 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2670 * Deserialize a download.
2672 * @param h overall context
2673 * @param rh file to deserialize from
2674 * @param parent parent download
2675 * @param search associated search
2676 * @param serialization name under which the search was serialized
2679 deserialize_download (struct GNUNET_FS_Handle *h,
2680 struct GNUNET_BIO_ReadHandle *rh,
2681 struct GNUNET_FS_DownloadContext *parent,
2682 struct GNUNET_FS_SearchResult *search,
2683 const char *serialization)
2685 struct GNUNET_FS_DownloadContext *dc;
2694 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2695 dc->parent = parent;
2697 dc->serialization = GNUNET_strdup (serialization);
2699 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2700 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2701 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2702 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2703 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2705 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2707 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2709 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2711 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2712 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2713 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2714 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2715 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2716 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2717 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2718 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2723 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2725 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE));
2726 dc->has_finished = (int) status;
2728 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2729 if (GNUNET_FS_uri_test_loc (dc->uri))
2730 GNUNET_assert (GNUNET_OK ==
2731 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2732 if (NULL == dc->emsg)
2734 dc->top_request = read_download_request (rh);
2735 if (NULL == dc->top_request)
2741 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2744 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2745 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2750 GNUNET_abort (); // for debugging for now - FIXME
2751 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2755 dc->search = search;
2756 search->download = dc;
2758 if ((NULL == parent) && (NULL == search))
2761 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2762 signal_download_resume (dc);
2765 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2768 GNUNET_free_non_null (uris);
2769 GNUNET_free_non_null (emsg);
2770 free_download_context (dc);
2775 * Signal resuming of a search to our clients (for the
2776 * top level search and all sub-searches).
2778 * @param sc search being resumed
2781 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2783 struct GNUNET_FS_ProgressInfo pi;
2785 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2786 pi.value.search.specifics.resume.message = sc->emsg;
2787 pi.value.search.specifics.resume.is_paused =
2788 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2789 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2790 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2791 &signal_result_resume, sc);
2797 * Deserialize a search.
2799 * @param h overall context
2800 * @param rh file to deserialize from
2801 * @param psearch_result parent search result
2802 * @param serialization name under which the search was serialized
2804 static struct GNUNET_FS_SearchContext *
2805 deserialize_search (struct GNUNET_FS_Handle *h,
2806 struct GNUNET_BIO_ReadHandle *rh,
2807 struct GNUNET_FS_SearchResult *psearch_result,
2808 const char *serialization)
2810 struct GNUNET_FS_SearchContext *sc;
2817 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2824 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2825 if (NULL != psearch_result)
2827 sc->psearch_result = psearch_result;
2828 psearch_result->update_search = sc;
2831 sc->serialization = GNUNET_strdup (serialization);
2832 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024))
2833 || (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2834 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2835 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2836 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2838 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2839 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2841 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2842 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2847 sc->options = (enum GNUNET_FS_SearchOptions) options;
2848 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2849 dn = get_serialization_file_name_in_dir (h,
2850 (sc->psearch_result ==
2852 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2853 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2854 sc->serialization, "");
2857 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2858 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2861 if (('\0' == in_pause) &&
2862 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2866 ("Could not resume running search, will resume as paused search\n"));
2868 signal_search_resume (sc);
2872 GNUNET_free_non_null (emsg);
2873 free_search_context (sc);
2874 GNUNET_free_non_null (uris);
2880 * Function called with a filename of serialized search operation
2883 * @param cls the 'struct GNUNET_FS_Handle*'
2884 * @param filename complete filename (absolute path)
2885 * @return GNUNET_OK (continue to iterate)
2888 deserialize_search_file (void *cls, const char *filename)
2890 struct GNUNET_FS_Handle *h = cls;
2893 struct GNUNET_BIO_ReadHandle *rh;
2894 struct GNUNET_FS_SearchContext *sc;
2897 if (0 != STAT (filename, &buf))
2899 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2902 if (S_ISDIR (buf.st_mode))
2903 return GNUNET_OK; /* skip directories */
2904 ser = get_serialization_short_name (filename);
2905 rh = GNUNET_BIO_read_open (filename);
2910 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2915 sc = deserialize_search (h, rh, NULL, ser);
2917 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2919 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2922 _("Failure while resuming search operation `%s': %s\n"),
2931 * Function called with a filename of serialized download operation
2934 * @param cls the 'struct GNUNET_FS_Handle*'
2935 * @param filename complete filename (absolute path)
2936 * @return GNUNET_OK (continue to iterate)
2939 deserialize_download_file (void *cls, const char *filename)
2941 struct GNUNET_FS_Handle *h = cls;
2944 struct GNUNET_BIO_ReadHandle *rh;
2946 ser = get_serialization_short_name (filename);
2947 rh = GNUNET_BIO_read_open (filename);
2950 if (0 != UNLINK (filename))
2951 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
2955 deserialize_download (h, rh, NULL, NULL, ser);
2957 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2960 _("Failure while resuming download operation `%s': %s\n"),
2969 * Deserialize informatin about pending operations.
2971 * @param master_path which master directory should be scanned
2972 * @param proc function to call for each entry (will get 'h' for 'cls')
2973 * @param h the 'struct GNUNET_FS_Handle*'
2976 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
2977 struct GNUNET_FS_Handle *h)
2981 dn = get_serialization_file_name (h, master_path, "");
2984 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2985 GNUNET_DISK_directory_scan (dn, proc, h);
2991 * Setup a connection to the file-sharing service.
2993 * @param cfg configuration to use
2994 * @param client_name unique identifier for this client
2995 * @param upcb function to call to notify about FS actions
2996 * @param upcb_cls closure for upcb
2997 * @param flags specific attributes for fs-operations
2998 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2999 * @return NULL on error
3001 struct GNUNET_FS_Handle *
3002 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3003 const char *client_name, GNUNET_FS_ProgressCallback upcb,
3004 void *upcb_cls, enum GNUNET_FS_Flags flags, ...)
3006 struct GNUNET_FS_Handle *ret;
3007 enum GNUNET_FS_OPTIONS opt;
3010 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
3012 ret->client_name = GNUNET_strdup (client_name);
3014 ret->upcb_cls = upcb_cls;
3016 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3017 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3018 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3019 va_start (ap, flags);
3020 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3024 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3025 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3028 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3029 ret->max_parallel_requests = va_arg (ap, unsigned int);
3034 GNUNET_free (ret->client_name);
3041 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3043 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3044 &deserialize_publish_file, ret);
3045 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3046 &deserialize_search_file, ret);
3047 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3048 &deserialize_download_file, ret);
3049 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3050 &deserialize_unindex_file, ret);
3057 * Close our connection with the file-sharing service.
3058 * The callback given to GNUNET_FS_start will no longer be
3059 * called after this function returns.
3061 * @param h handle that was returned from GNUNET_FS_start
3064 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3066 while (h->top_head != NULL)
3067 h->top_head->ssf (h->top_head->ssf_cls);
3068 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3069 GNUNET_SCHEDULER_cancel (h->queue_job);
3070 GNUNET_free (h->client_name);