2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief main FS functions (master initialization, serialization, deserialization, shared code)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
34 * How many block requests can we have outstanding in parallel at a time by default?
36 #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
39 * How many downloads can we have outstanding in parallel at a time by default?
41 #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
44 * Start the given job (send signal, remove from pending queue, update
45 * counters and state).
47 * @param qe job to start
50 start_job (struct GNUNET_FS_QueueEntry *qe)
52 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 *`
113 process_job_queue (void *cls)
115 struct GNUNET_FS_Handle *h = cls;
116 struct GNUNET_FS_QueueEntry *qe;
117 struct GNUNET_FS_QueueEntry *next;
118 struct GNUNET_TIME_Relative run_time;
119 struct GNUNET_TIME_Relative restart_at;
120 struct GNUNET_TIME_Relative rst;
121 struct GNUNET_TIME_Absolute end_time;
122 unsigned int num_downloads_waiting;
123 unsigned int num_downloads_active;
124 unsigned int num_downloads_expired;
125 unsigned int num_probes_active;
126 unsigned int num_probes_waiting;
127 unsigned int num_probes_expired;
128 int num_probes_change;
129 int num_downloads_change;
133 /* restart_at will be set to the time when it makes sense to
134 re-evaluate the job queue (unless, of course, jobs complete
135 or are added, then we'll be triggered immediately */
136 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
137 /* first, calculate some basic statistics on pending jobs */
138 num_probes_waiting = 0;
139 num_downloads_waiting = 0;
140 for (qe = h->pending_head; NULL != qe; qe = qe->next)
142 switch (qe->priority)
144 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
145 num_probes_waiting++;
147 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
148 num_downloads_waiting++;
155 /* now, calculate some basic statistics on running jobs */
156 num_probes_active = 0;
157 num_probes_expired = 0;
158 num_downloads_active = 0;
159 num_downloads_expired = 0;
160 next = h->running_head;
161 while (NULL != (qe = next))
164 switch (qe->priority)
166 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
167 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
168 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
169 rst = GNUNET_TIME_absolute_get_remaining (end_time);
170 if (0 == rst.rel_value_us)
172 num_probes_expired++;
178 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
181 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
183 GNUNET_TIME_relative_multiply (h->avg_block_latency,
184 qe->blocks * qe->start_times);
185 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
186 rst = GNUNET_TIME_absolute_get_remaining (end_time);
187 if (0 == rst.rel_value_us)
189 num_downloads_expired++;
194 num_downloads_active++;
195 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
203 GNUNET_break (h->active_downloads ==
204 num_downloads_active + num_probes_active);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
210 num_downloads_active,
211 num_downloads_expired,
212 num_downloads_waiting);
213 GNUNET_break (h->active_downloads + num_probes_active <=
214 h->max_parallel_downloads);
215 /* calculate start/stop decisions */
216 if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
218 /* stop as many probes as there are downloads and probes */
219 num_probes_change = - GNUNET_MIN (num_probes_active,
220 num_downloads_waiting);
221 /* start as many downloads as there are free slots, including those
223 num_downloads_change = h->max_parallel_downloads - h->active_downloads - num_probes_change;
227 /* start all downloads (we can) */
228 num_downloads_change = num_downloads_waiting;
229 /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
230 if (1 + h->max_parallel_downloads / 4 >= (h->active_downloads + num_downloads_change))
231 num_probes_change = GNUNET_MIN (num_probes_waiting,
232 (1 + h->max_parallel_downloads / 4) - (h->active_downloads + num_downloads_change));
234 num_probes_change = 0;
236 GNUNET_break (num_downloads_change <= num_downloads_waiting);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Changing %d probes and %d/%u/%u downloads\n",
240 num_downloads_change,
241 (unsigned int) h->active_downloads,
242 (unsigned int) h->max_parallel_downloads);
243 /* actually stop probes */
244 next = h->running_head;
245 while (NULL != (qe = next))
248 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
250 if (num_probes_change < 0)
254 if (0 == num_probes_change)
258 GNUNET_break (0 <= num_probes_change);
260 /* start some more tasks if we now have empty slots */
261 block_limit_hit = GNUNET_NO;
262 next = h->pending_head;
263 while ( (NULL != (qe = next)) &&
264 ( (num_probes_change > 0) ||
265 (num_downloads_change > 0) ) )
268 switch (qe->priority)
270 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
271 if (num_probes_change > 0)
275 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
276 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
279 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
280 if ( (num_downloads_change > 0) &&
281 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
282 ( (qe->blocks > h->max_parallel_requests) &&
283 (0 == h->active_downloads) ) ) )
286 num_downloads_change--;
288 else if (num_downloads_change > 0)
289 block_limit_hit = GNUNET_YES;
296 GNUNET_break ( (0 == num_downloads_change) ||
297 (GNUNET_YES == block_limit_hit) );
298 GNUNET_break (0 == num_probes_change);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
303 h->max_parallel_requests,
305 num_downloads_change,
306 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
308 /* make sure we run again, callbacks might have
309 already re-scheduled the job, so cancel such
310 an operation (if it exists) */
311 if (NULL != h->queue_job)
312 GNUNET_SCHEDULER_cancel (h->queue_job);
314 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
319 * Add a job to the queue.
321 * @param h handle to the overall FS state
322 * @param start function to call to begin the job
323 * @param stop function to call to pause the job, or on dequeue (if the job was running)
324 * @param cls closure for start and stop
325 * @param blocks number of blocks this jobs uses
326 * @param priority how important is this download
327 * @return queue handle
329 struct GNUNET_FS_QueueEntry *
330 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
331 GNUNET_FS_QueueStart start,
332 GNUNET_FS_QueueStop stop, void *cls,
334 enum GNUNET_FS_QueuePriority priority)
336 struct GNUNET_FS_QueueEntry *qe;
338 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
343 qe->queue_time = GNUNET_TIME_absolute_get ();
345 qe->priority = priority;
346 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
347 h->pending_tail, qe);
348 if (NULL != h->queue_job)
349 GNUNET_SCHEDULER_cancel (h->queue_job);
350 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 * Dequeue a job from the queue.
361 * @param qe handle for the job
364 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
366 struct GNUNET_FS_Handle *h;
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Dequeueing job %p\n",
372 if (NULL != qe->client)
374 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
376 if (NULL != h->queue_job)
377 GNUNET_SCHEDULER_cancel (h->queue_job);
378 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
383 * Create a top-level activity entry.
385 * @param h global fs handle
386 * @param ssf suspend signal function to use
387 * @param ssf_cls closure for @a ssf
388 * @return fresh top-level activity handle
390 struct TopLevelActivity *
391 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
392 SuspendSignalFunction ssf,
395 struct TopLevelActivity *ret;
397 ret = GNUNET_new (struct TopLevelActivity);
399 ret->ssf_cls = ssf_cls;
400 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
406 * Destroy a top-level activity entry.
408 * @param h global fs handle
409 * @param top top level activity entry
412 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
413 struct TopLevelActivity *top)
415 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
421 * Closure for #GNUNET_FS_data_reader_file_().
426 * Name of the file to read.
431 * File descriptor, NULL if it has not yet been opened.
433 struct GNUNET_DISK_FileHandle *fd;
438 * Function that provides data by reading from a file.
440 * @param cls closure with the `struct FileInfo *`
441 * @param offset offset to read from; it is possible
442 * that the caller might need to go backwards
443 * a bit at times; set to `UINT64_MAX` to tell
444 * the reader that we won't be reading for a while
445 * (used to close the file descriptor but NOT fully
446 * clean up the reader's state); in this case,
447 * a value of '0' for @a max should be ignored
448 * @param max maximum number of bytes that should be
449 * copied to @a buf; readers are not allowed
450 * to provide less data unless there is an error;
451 * a value of "0" will be used at the end to allow
452 * the reader to clean up its internal state
453 * @param buf where the reader should write the data
454 * @param emsg location for the reader to store an error message
455 * @return number of bytes written, usually @a max, 0 on error
458 GNUNET_FS_data_reader_file_ (void *cls,
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,
488 GNUNET_DISK_OPEN_READ,
489 GNUNET_DISK_PERM_NONE);
492 GNUNET_asprintf (emsg,
493 _("Could not open file `%s': %s"),
499 if ( (GNUNET_SYSERR ==
500 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
501 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
503 GNUNET_asprintf (emsg,
504 _("Could not read file `%s': %s"),
511 GNUNET_asprintf (emsg,
512 _("Short read reading from file `%s'!"),
521 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
523 * @param filename file to read
524 * @return closure to use, NULL on error
527 GNUNET_FS_make_file_reader_context_ (const char *filename)
531 fi = GNUNET_new (struct FileInfo);
532 fi->filename = GNUNET_STRINGS_filename_expand (filename);
533 if (NULL == fi->filename)
543 * Function that provides data by copying from a buffer.
545 * @param cls closure (points to the buffer)
546 * @param offset offset to read from; it is possible
547 * that the caller might need to go backwards
548 * a bit at times; set to `UINT64_MAX` to tell
549 * the reader that we won't be reading for a while
550 * (used to close the file descriptor but NOT fully
551 * clean up the reader's state); in this case,
552 * a value of '0' for @a max should be ignored
553 * @param max maximum number of bytes that should be
554 * copied to @a buf; readers are not allowed
555 * to provide less data unless there is an error;
556 * a value of "0" will be used at the end to allow
557 * the reader to clean up its internal state
558 * @param buf where the reader should write the data
559 * @param emsg location for the reader to store an error message
560 * @return number of bytes written, usually @a max, 0 on error
563 GNUNET_FS_data_reader_copy_ (void *cls,
571 if (UINT64_MAX == offset)
575 GNUNET_free_non_null (data);
578 memcpy (buf, &data[offset], max);
584 * Return the full filename where we would store state information
585 * (for serialization/deserialization).
587 * @param h master context
588 * @param ext component of the path
589 * @param ent entity identifier (or emtpy string for the directory)
590 * @return NULL on error
593 get_serialization_file_name (struct GNUNET_FS_Handle *h,
600 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
601 return NULL; /* persistence not requested */
603 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
606 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
607 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
609 GNUNET_free (basename);
615 * Return the full filename where we would store state information
616 * (for serialization/deserialization) that is associated with a
619 * @param h master context
620 * @param ext component of the path
621 * @param uni name of the parent operation
622 * @param ent entity identifier (or emtpy string for the directory)
623 * @return NULL on error
626 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
634 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
635 return NULL; /* persistence not requested */
637 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
640 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
641 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
642 uni, DIR_SEPARATOR_STR, ent);
643 GNUNET_free (basename);
649 * Return a read handle for deserialization.
651 * @param h master context
652 * @param ext component of the path
653 * @param ent entity identifier (or emtpy string for the directory)
654 * @return NULL on error
656 static struct GNUNET_BIO_ReadHandle *
657 get_read_handle (struct GNUNET_FS_Handle *h,
662 struct GNUNET_BIO_ReadHandle *ret;
664 fn = get_serialization_file_name (h, ext, ent);
667 ret = GNUNET_BIO_read_open (fn);
674 * Return a write handle for serialization.
676 * @param h master context
677 * @param ext component of the path
678 * @param ent entity identifier (or emtpy string for the directory)
679 * @return NULL on error
681 static struct GNUNET_BIO_WriteHandle *
682 get_write_handle (struct GNUNET_FS_Handle *h,
687 struct GNUNET_BIO_WriteHandle *ret;
689 fn = get_serialization_file_name (h, ext, ent);
692 ret = GNUNET_BIO_write_open (fn);
693 GNUNET_break (NULL != ret);
700 * Return a write handle for serialization.
702 * @param h master context
703 * @param ext component of the path
704 * @param uni name of parent
705 * @param ent entity identifier (or emtpy string for the directory)
706 * @return NULL on error
708 static struct GNUNET_BIO_WriteHandle *
709 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
710 const char *uni, const char *ent)
713 struct GNUNET_BIO_WriteHandle *ret;
715 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
718 ret = GNUNET_BIO_write_open (fn);
725 * Remove serialization/deserialization file from disk.
727 * @param h master context
728 * @param ext component of the path
729 * @param ent entity identifier
732 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
738 if ((NULL == ent) || (0 == strlen (ent)))
743 filename = get_serialization_file_name (h, ext, ent);
744 if (NULL != filename)
746 if ( (0 != UNLINK (filename)) &&
748 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
749 GNUNET_free (filename);
755 * Remove serialization/deserialization file from disk.
757 * @param h master context
758 * @param ext component of the path
759 * @param uni parent name
760 * @param ent entity identifier
763 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
765 const char *uni, const char *ent)
769 if ((NULL == ent) || (0 == strlen (ent)))
774 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
775 if (NULL == filename)
777 if (0 != UNLINK (filename))
778 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
779 GNUNET_free (filename);
784 * Remove serialization/deserialization directory from disk.
786 * @param h master context
787 * @param ext component of the path
788 * @param uni unique name of parent
791 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
799 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
802 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
803 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
804 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
810 * Serialize a start-time. Since we use start-times to
811 * calculate the duration of some operation, we actually
812 * do not serialize the absolute time but the (relative)
813 * duration since the start time. When we then
814 * deserialize the start time, we take the current time and
815 * subtract that duration so that we get again an absolute
816 * time stamp that will result in correct performance
819 * @param wh handle for writing
820 * @param timestamp time to serialize
821 * @return #GNUNET_OK on success
824 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
825 struct GNUNET_TIME_Absolute timestamp)
827 struct GNUNET_TIME_Relative dur;
829 dur = GNUNET_TIME_absolute_get_duration (timestamp);
830 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
835 * Deserialize a start-time. Since we use start-times to
836 * calculate the duration of some operation, we actually
837 * do not serialize the absolute time but the (relative)
838 * duration since the start time. Thus, when we then
839 * deserialize the start time, we take the current time and
840 * subtract that duration so that we get again an absolute
841 * time stamp that will result in correct performance
844 * @param rh handle for reading
845 * @param timestamp where to write the deserialized timestamp
846 * @return #GNUNET_OK on success
849 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
850 struct GNUNET_TIME_Absolute *timestamp)
852 struct GNUNET_TIME_Relative dur;
854 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
855 return GNUNET_SYSERR;
856 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
862 * Using the given serialization filename, try to deserialize
863 * the file-information tree associated with it.
865 * @param h master context
866 * @param filename name of the file (without directory) with
868 * @return NULL on error
870 static struct GNUNET_FS_FileInformation *
871 deserialize_file_information (struct GNUNET_FS_Handle *h,
872 const char *filename);
876 * Using the given serialization filename, try to deserialize
877 * the file-information tree associated with it.
879 * @param h master context
880 * @param fn name of the file (without directory) with
882 * @param rh handle for reading
883 * @return NULL on error
885 static struct GNUNET_FS_FileInformation *
886 deserialize_fi_node (struct GNUNET_FS_Handle *h,
888 struct GNUNET_BIO_ReadHandle *rh)
890 struct GNUNET_FS_FileInformation *ret;
891 struct GNUNET_FS_FileInformation *nxt;
899 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
904 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
910 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
911 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
913 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
914 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
915 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
917 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
918 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
919 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
921 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
922 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
923 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
924 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
926 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
928 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
929 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
930 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
931 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
938 case 0: /* file-insert */
939 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
944 ret->is_directory = GNUNET_NO;
945 ret->data.file.do_index = GNUNET_NO;
946 ret->data.file.have_hash = GNUNET_NO;
947 ret->data.file.index_start_confirmed = GNUNET_NO;
948 if (GNUNET_NO == ret->is_published)
950 if (NULL == ret->filename)
952 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
953 ret->data.file.reader_cls =
954 GNUNET_malloc_large (ret->data.file.file_size);
955 if (ret->data.file.reader_cls == NULL)
958 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
959 ret->data.file.file_size))
967 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
968 ret->data.file.reader_cls =
969 GNUNET_FS_make_file_reader_context_ (ret->filename);
973 case 1: /* file-index, no hash */
974 if (NULL == ret->filename)
979 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
984 ret->is_directory = GNUNET_NO;
985 ret->data.file.do_index = GNUNET_YES;
986 ret->data.file.have_hash = GNUNET_NO;
987 ret->data.file.index_start_confirmed = GNUNET_NO;
988 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
989 ret->data.file.reader_cls =
990 GNUNET_FS_make_file_reader_context_ (ret->filename);
992 case 2: /* file-index-with-hash */
993 if (NULL == ret->filename)
998 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1000 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1001 sizeof (struct GNUNET_HashCode))))
1006 ret->is_directory = GNUNET_NO;
1007 ret->data.file.do_index = GNUNET_YES;
1008 ret->data.file.have_hash = GNUNET_YES;
1009 ret->data.file.index_start_confirmed = GNUNET_NO;
1010 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1011 ret->data.file.reader_cls =
1012 GNUNET_FS_make_file_reader_context_ (ret->filename);
1014 case 3: /* file-index-with-hash-confirmed */
1015 if (NULL == ret->filename)
1020 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1022 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1023 sizeof (struct GNUNET_HashCode))))
1028 ret->is_directory = GNUNET_NO;
1029 ret->data.file.do_index = GNUNET_YES;
1030 ret->data.file.have_hash = GNUNET_YES;
1031 ret->data.file.index_start_confirmed = GNUNET_YES;
1032 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1033 ret->data.file.reader_cls =
1034 GNUNET_FS_make_file_reader_context_ (ret->filename);
1036 case 4: /* directory */
1037 ret->is_directory = GNUNET_YES;
1038 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1039 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1040 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1041 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1043 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1045 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1050 ret->data.dir.dir_size = (uint32_t) dsize;
1051 if (NULL != filename)
1053 ret->data.dir.entries = deserialize_file_information (h, filename);
1054 GNUNET_free (filename);
1056 nxt = ret->data.dir.entries;
1068 ret->serialization = GNUNET_strdup (fn);
1070 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1075 if (NULL != filename)
1077 ret->next = deserialize_file_information (h, filename);
1078 GNUNET_free (filename);
1081 GNUNET_free_non_null (ksks);
1082 GNUNET_free_non_null (skss);
1083 GNUNET_free_non_null (chks);
1086 GNUNET_free_non_null (ksks);
1087 GNUNET_free_non_null (chks);
1088 GNUNET_free_non_null (skss);
1089 GNUNET_free_non_null (filename);
1090 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1096 * Using the given serialization filename, try to deserialize
1097 * the file-information tree associated with it.
1099 * @param h master context
1100 * @param filename name of the file (without directory) with
1102 * @return NULL on error
1104 static struct GNUNET_FS_FileInformation *
1105 deserialize_file_information (struct GNUNET_FS_Handle *h,
1106 const char *filename)
1108 struct GNUNET_FS_FileInformation *ret;
1109 struct GNUNET_BIO_ReadHandle *rh;
1113 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1116 ret = deserialize_fi_node (h, filename, rh);
1117 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Failed to resume publishing information `%s': %s\n"),
1126 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1129 if (0 != UNLINK (fn))
1130 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1139 * Given a serialization name (full absolute path), return the
1140 * basename of the file (without the path), which must only
1141 * consist of the 6 random characters.
1143 * @param fullname name to extract the basename from
1144 * @return copy of the basename, NULL on error
1147 get_serialization_short_name (const char *fullname)
1154 /* FIXME: we could do this faster since we know
1155 * the length of 'end'... */
1156 while ('\0' != *nxt)
1158 if (DIR_SEPARATOR == *nxt)
1162 if ((NULL == end) || (0 == strlen (end)))
1167 GNUNET_break (6 == strlen (end));
1168 return GNUNET_strdup (end);
1173 * Create a new random name for serialization. Also checks if persistence
1174 * is enabled and returns NULL if not.
1176 * @param h master context
1177 * @param ext component of the path
1178 * @return NULL on errror
1181 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1188 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1189 return NULL; /* persistence not requested */
1190 dn = get_serialization_file_name (h, ext, "");
1193 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1198 fn = GNUNET_DISK_mktemp (dn);
1201 return NULL; /* epic fail */
1202 ret = get_serialization_short_name (fn);
1209 * Create a new random name for serialization. Also checks if persistence
1210 * is enabled and returns NULL if not.
1212 * @param h master context
1213 * @param ext component of the path
1214 * @param uni name of parent
1215 * @return NULL on errror
1218 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1226 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1227 return NULL; /* persistence not requested */
1228 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1231 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1236 fn = GNUNET_DISK_mktemp (dn);
1239 return NULL; /* epic fail */
1240 ret = get_serialization_short_name (fn);
1247 * Copy all of the data from the reader to the write handle.
1249 * @param wh write handle
1250 * @param fi file with reader
1251 * @return #GNUNET_OK on success
1254 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1255 struct GNUNET_FS_FileInformation *fi)
1257 char buf[32 * 1024];
1265 while (off < fi->data.file.file_size)
1267 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1269 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1273 return GNUNET_SYSERR;
1275 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1276 return GNUNET_SYSERR;
1284 * Create a temporary file on disk to store the current
1285 * state of @a fi in.
1287 * @param fi file information to sync with disk
1290 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1293 struct GNUNET_BIO_WriteHandle *wh;
1299 if (NULL == fi->serialization)
1301 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1302 if (NULL == fi->serialization)
1304 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1308 GNUNET_free (fi->serialization);
1309 fi->serialization = NULL;
1312 if (GNUNET_YES == fi->is_directory)
1314 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1316 else if (GNUNET_YES == fi->data.file.have_hash)
1318 else if (GNUNET_YES == fi->data.file.do_index)
1322 if (NULL != fi->keywords)
1323 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1326 if (NULL != fi->chk_uri)
1327 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1330 if (NULL != fi->sks_uri)
1331 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1334 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1335 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1336 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1337 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1338 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1339 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1340 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1341 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1343 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1344 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1345 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1346 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1351 GNUNET_free_non_null (chks);
1353 GNUNET_free_non_null (ksks);
1355 GNUNET_free_non_null (skss);
1360 case 0: /* file-insert */
1361 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1366 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1367 if (GNUNET_OK != copy_from_reader (wh, fi))
1373 case 1: /* file-index, no hash */
1374 if (NULL == fi->filename)
1379 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1385 case 2: /* file-index-with-hash */
1386 case 3: /* file-index-with-hash-confirmed */
1387 if (NULL == fi->filename)
1392 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1394 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1395 sizeof (struct GNUNET_HashCode))))
1401 case 4: /* directory */
1402 if ( (NULL != fi->data.dir.entries) &&
1403 (NULL == fi->data.dir.entries->serialization) )
1404 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1405 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1406 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1407 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1409 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1410 (uint32_t) fi->data.dir.dir_size)) ||
1412 GNUNET_BIO_write_string (wh,
1413 (fi->data.dir.entries ==
1414 NULL) ? NULL : fi->data.dir.
1415 entries->serialization)))
1425 if ( (NULL != fi->next) &&
1426 (NULL == fi->next->serialization) )
1427 GNUNET_FS_file_information_sync_ (fi->next);
1429 GNUNET_BIO_write_string (wh,
1431 NULL) ? fi->next->serialization : NULL))
1436 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1445 (void) GNUNET_BIO_write_close (wh);
1446 GNUNET_free_non_null (chks);
1447 GNUNET_free_non_null (ksks);
1448 GNUNET_free_non_null (skss);
1449 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1453 if (0 != UNLINK (fn))
1454 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1457 GNUNET_free (fi->serialization);
1458 fi->serialization = NULL;
1464 * Find the entry in the file information struct where the
1465 * serialization filename matches the given name.
1467 * @param pos file information to search
1468 * @param srch filename to search for
1469 * @return NULL if srch was not found in this subtree
1471 static struct GNUNET_FS_FileInformation *
1472 find_file_position (struct GNUNET_FS_FileInformation *pos,
1475 struct GNUNET_FS_FileInformation *r;
1479 if (0 == strcmp (srch, pos->serialization))
1481 if ( (GNUNET_YES == pos->is_directory) &&
1482 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1491 * Signal the FS's progress function that we are resuming
1494 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1495 * @param fi the entry in the publish-structure
1496 * @param length length of the file or directory
1497 * @param meta metadata for the file or directory (can be modified)
1498 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1499 * @param bo block options (can be modified)
1500 * @param do_index should we index?
1501 * @param client_info pointer to client context set upon creation (can be modified)
1502 * @return #GNUNET_OK to continue (always)
1505 fip_signal_resume (void *cls,
1506 struct GNUNET_FS_FileInformation *fi,
1508 struct GNUNET_CONTAINER_MetaData *meta,
1509 struct GNUNET_FS_Uri **uri,
1510 struct GNUNET_FS_BlockOptions *bo,
1514 struct GNUNET_FS_PublishContext *pc = cls;
1515 struct GNUNET_FS_ProgressInfo pi;
1517 if (GNUNET_YES == pc->skip_next_fi_callback)
1519 pc->skip_next_fi_callback = GNUNET_NO;
1522 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1523 pi.value.publish.specifics.resume.message = fi->emsg;
1524 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1525 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1526 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1528 /* process entries in directory */
1529 pc->skip_next_fi_callback = GNUNET_YES;
1530 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1537 * Function called with a filename of serialized publishing operation
1540 * @param cls the `struct GNUNET_FS_Handle *`
1541 * @param filename complete filename (absolute path)
1542 * @return #GNUNET_OK (continue to iterate)
1545 deserialize_publish_file (void *cls,
1546 const char *filename)
1548 struct GNUNET_FS_Handle *h = cls;
1549 struct GNUNET_BIO_ReadHandle *rh;
1550 struct GNUNET_FS_PublishContext *pc;
1555 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1559 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1561 pc->serialization = get_serialization_short_name (filename);
1564 rh = GNUNET_BIO_read_open (filename);
1570 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1572 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1573 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1574 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1575 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1577 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1578 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1579 || ( (GNUNET_YES == have_ns) &&
1580 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1585 pc->options = options;
1586 pc->all_done = all_done;
1587 if (NULL == fi_root)
1592 pc->fi = deserialize_file_information (h, fi_root);
1598 if (GNUNET_YES == have_ns)
1600 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1603 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1604 (GNUNET_YES != pc->all_done))
1606 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1607 if (NULL == pc->dsh)
1612 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1613 GNUNET_free (fi_pos);
1615 if (NULL == pc->fi_pos)
1617 /* failed to find position for resuming, outch! Will start from root! */
1619 if (GNUNET_YES != pc->all_done)
1620 pc->fi_pos = pc->fi;
1623 GNUNET_free (fi_root);
1625 /* generate RESUME event(s) */
1626 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1628 /* re-start publishing (if needed)... */
1629 if (GNUNET_YES != pc->all_done)
1631 GNUNET_assert (NULL == pc->upload_task);
1633 GNUNET_SCHEDULER_add_with_priority
1634 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1635 &GNUNET_FS_publish_main_, pc);
1637 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1640 _("Failure while resuming publishing operation `%s': %s\n"),
1644 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1647 GNUNET_free_non_null (pc->nid);
1648 GNUNET_free_non_null (pc->nuid);
1649 GNUNET_free_non_null (fi_root);
1650 GNUNET_free_non_null (fi_pos);
1651 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1654 _("Failed to resume publishing operation `%s': %s\n"), filename,
1659 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1660 if (0 != UNLINK (filename))
1661 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1662 GNUNET_free (pc->serialization);
1669 * Synchronize this publishing struct with its mirror
1670 * on disk. Note that all internal FS-operations that change
1671 * publishing structs should already call "sync" internally,
1672 * so this function is likely not useful for clients.
1674 * @param pc the struct to sync
1677 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1679 struct GNUNET_BIO_WriteHandle *wh;
1682 if (NULL == pc->serialization)
1684 make_serialization_file_name (pc->h,
1685 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1686 if (NULL == pc->serialization)
1690 if (NULL == pc->fi->serialization)
1695 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1702 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1703 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1704 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1705 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1706 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1707 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1708 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1710 GNUNET_BIO_write_string (wh,
1711 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1712 ( (NULL != pc->ns) &&
1713 (GNUNET_OK != GNUNET_BIO_write (wh,
1715 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1720 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1729 (void) GNUNET_BIO_write_close (wh);
1730 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1732 GNUNET_free (pc->serialization);
1733 pc->serialization = NULL;
1738 * Synchronize this unindex struct with its mirror
1739 * on disk. Note that all internal FS-operations that change
1740 * publishing structs should already call "sync" internally,
1741 * so this function is likely not useful for clients.
1743 * @param uc the struct to sync
1746 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1748 struct GNUNET_BIO_WriteHandle *wh;
1751 if (NULL == uc->serialization)
1753 make_serialization_file_name (uc->h,
1754 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1755 if (NULL == uc->serialization)
1757 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1764 if (NULL != uc->ksk_uri)
1765 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1768 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1769 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1770 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1771 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1773 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1774 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1775 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1776 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1778 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1779 ((uc->state == UNINDEX_STATE_ERROR) &&
1780 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1785 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1794 (void) GNUNET_BIO_write_close (wh);
1795 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1797 GNUNET_free (uc->serialization);
1798 uc->serialization = NULL;
1803 * Serialize a download request.
1805 * @param wh handle for writing the download request to disk
1806 * @param dr the the request to write to disk
1807 * @return #GNUNET_YES on success, #GNUNET_NO on error
1810 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1811 struct DownloadRequest *dr)
1815 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1816 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1817 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1818 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1820 if ((BRS_CHK_SET == dr->state) &&
1822 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1824 for (i = 0; i < dr->num_children; i++)
1825 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1832 * Read a download request tree.
1834 * @param rh cadet to read from
1835 * @return value the download request read from disk, NULL on error
1837 static struct DownloadRequest *
1838 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1840 struct DownloadRequest *dr;
1843 dr = GNUNET_new (struct DownloadRequest);
1844 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1845 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1846 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1847 (dr->num_children > CHK_PER_INODE) ||
1848 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1849 ( (0 == dr->depth) &&
1850 (dr->num_children > 0) ) ||
1851 ((dr->depth > 0) && (0 == dr->num_children)))
1854 dr->num_children = 0;
1857 if (dr->num_children > 0)
1859 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1863 case BRS_RECONSTRUCT_DOWN:
1864 case BRS_RECONSTRUCT_META_UP:
1865 case BRS_RECONSTRUCT_UP:
1869 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1872 case BRS_DOWNLOAD_DOWN:
1873 case BRS_DOWNLOAD_UP:
1880 for (i = 0; i < dr->num_children; i++)
1882 if (NULL == (dr->children[i] = read_download_request (rh)))
1884 dr->children[i]->parent = dr;
1888 GNUNET_FS_free_download_request_ (dr);
1894 * Compute the name of the sync file (or directory) for the given download
1897 * @param dc download context to compute for
1898 * @param uni unique filename to use, use "" for the directory name
1899 * @param ext extension to use, use ".dir" for our own subdirectory
1900 * @return the expanded file name, NULL for none
1903 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1910 if (dc->parent == NULL)
1911 return get_serialization_file_name (dc->h,
1912 (dc->search != NULL) ?
1913 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1914 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1916 if (NULL == dc->parent->serialization)
1918 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1921 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1928 * Synchronize this download struct with its mirror
1929 * on disk. Note that all internal FS-operations that change
1930 * publishing structs should already call "sync" internally,
1931 * so this function is likely not useful for clients.
1933 * @param dc the struct to sync
1936 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1938 struct GNUNET_BIO_WriteHandle *wh;
1943 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1944 return; /* we don't sync probes */
1945 if (NULL == dc->serialization)
1947 dir = get_download_sync_filename (dc, "", "");
1950 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1955 fn = GNUNET_DISK_mktemp (dir);
1959 dc->serialization = get_serialization_short_name (fn);
1963 fn = get_download_sync_filename (dc, dc->serialization, "");
1966 GNUNET_free (dc->serialization);
1967 dc->serialization = NULL;
1972 wh = GNUNET_BIO_write_open (fn);
1975 GNUNET_free (dc->serialization);
1976 dc->serialization = NULL;
1980 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1981 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1982 uris = GNUNET_FS_uri_to_string (dc->uri);
1983 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1984 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1985 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1986 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1987 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1988 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1989 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1990 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1991 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1992 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1993 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1994 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1995 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
2000 if (NULL == dc->emsg)
2002 GNUNET_assert (dc->top_request != NULL);
2003 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2009 GNUNET_free_non_null (uris);
2011 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2021 (void) GNUNET_BIO_write_close (wh);
2022 GNUNET_free_non_null (uris);
2023 if (0 != UNLINK (fn))
2024 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2026 GNUNET_free (dc->serialization);
2027 dc->serialization = NULL;
2032 * Synchronize this search result with its mirror
2033 * on disk. Note that all internal FS-operations that change
2034 * publishing structs should already call "sync" internally,
2035 * so this function is likely not useful for clients.
2037 * @param sr the struct to sync
2040 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2042 struct GNUNET_BIO_WriteHandle *wh;
2048 if (NULL == sr->serialization)
2050 make_serialization_file_name_in_dir (sr->h,
2051 (sr->sc->psearch_result ==
2053 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2054 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2055 sr->sc->serialization);
2056 if (NULL == sr->serialization)
2058 wh = get_write_handle_in_dir (sr->h,
2059 (sr->sc->psearch_result ==
2060 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2061 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2062 sr->sc->serialization, sr->serialization);
2068 uris = GNUNET_FS_uri_to_string (sr->uri);
2069 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2071 GNUNET_BIO_write_string (wh,
2073 NULL ? sr->download->serialization : NULL)) ||
2075 GNUNET_BIO_write_string (wh,
2076 sr->update_search !=
2077 NULL ? sr->update_search->serialization : NULL))
2078 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2079 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2080 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2081 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2082 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2083 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2088 if ( (NULL != sr->uri) &&
2089 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2090 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2091 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2096 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2102 GNUNET_free_non_null (uris);
2105 GNUNET_free_non_null (uris);
2107 (void) GNUNET_BIO_write_close (wh);
2108 remove_sync_file_in_dir (sr->h,
2109 (NULL == sr->sc->psearch_result)
2110 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2111 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2112 sr->sc->serialization, sr->serialization);
2113 GNUNET_free (sr->serialization);
2114 sr->serialization = NULL;
2119 * Synchronize this search struct with its mirror
2120 * on disk. Note that all internal FS-operations that change
2121 * publishing structs should already call "sync" internally,
2122 * so this function is likely not useful for clients.
2124 * @param sc the struct to sync
2127 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2129 struct GNUNET_BIO_WriteHandle *wh;
2132 const char *category;
2135 (NULL == sc->psearch_result)
2136 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2137 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2138 if (NULL == sc->serialization)
2139 sc->serialization = make_serialization_file_name (sc->h, category);
2140 if (NULL == sc->serialization)
2143 wh = get_write_handle (sc->h, category, sc->serialization);
2149 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2150 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2151 uris = GNUNET_FS_uri_to_string (sc->uri);
2152 in_pause = (sc->task != NULL) ? 'r' : '\0';
2153 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2154 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2155 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2156 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2157 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2158 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2165 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2174 (void) GNUNET_BIO_write_close (wh);
2175 GNUNET_free_non_null (uris);
2176 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2177 GNUNET_free (sc->serialization);
2178 sc->serialization = NULL;
2183 * Function called with a filename of serialized unindexing operation
2186 * @param cls the `struct GNUNET_FS_Handle *`
2187 * @param filename complete filename (absolute path)
2188 * @return #GNUNET_OK (continue to iterate)
2191 deserialize_unindex_file (void *cls,
2192 const char *filename)
2194 struct GNUNET_FS_Handle *h = cls;
2195 struct GNUNET_BIO_ReadHandle *rh;
2196 struct GNUNET_FS_UnindexContext *uc;
2197 struct GNUNET_FS_ProgressInfo pi;
2202 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2204 uc->serialization = get_serialization_short_name (filename);
2205 rh = GNUNET_BIO_read_open (filename);
2213 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2214 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2215 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2216 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2217 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2218 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2219 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2221 GNUNET_free_non_null (uris);
2227 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2229 if (NULL == uc->ksk_uri)
2232 GNUNET_free_non_null (emsg);
2236 if ( (uc->ksk_offset > 0) &&
2237 ( (NULL == uc->ksk_uri) ||
2238 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2243 uc->state = (enum UnindexState) state;
2246 case UNINDEX_STATE_HASHING:
2248 case UNINDEX_STATE_FS_NOTIFY:
2250 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2251 sizeof (struct GNUNET_HashCode)))
2257 case UNINDEX_STATE_DS_REMOVE:
2258 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2259 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2261 case UNINDEX_STATE_COMPLETE:
2263 case UNINDEX_STATE_ERROR:
2265 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2275 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2276 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2277 pi.value.unindex.specifics.resume.message = uc->emsg;
2278 GNUNET_FS_unindex_make_status_ (&pi, uc,
2280 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2283 case UNINDEX_STATE_HASHING:
2285 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2287 &GNUNET_FS_unindex_process_hash_, uc);
2289 case UNINDEX_STATE_FS_NOTIFY:
2290 uc->state = UNINDEX_STATE_HASHING;
2291 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2293 case UNINDEX_STATE_DS_REMOVE:
2294 GNUNET_FS_unindex_do_remove_ (uc);
2296 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2297 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2299 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2300 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2302 case UNINDEX_STATE_COMPLETE:
2303 case UNINDEX_STATE_ERROR:
2304 /* no need to resume any operation, we were done */
2309 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2312 _("Failure while resuming unindexing operation `%s': %s\n"),
2318 GNUNET_free_non_null (uc->filename);
2319 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2322 _("Failed to resume unindexing operation `%s': %s\n"),
2327 if (NULL != uc->serialization)
2328 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2330 GNUNET_free_non_null (uc->serialization);
2337 * Deserialize a download.
2339 * @param h overall context
2340 * @param rh file to deserialize from
2341 * @param parent parent download
2342 * @param search associated search
2343 * @param serialization name under which the search was serialized
2346 deserialize_download (struct GNUNET_FS_Handle *h,
2347 struct GNUNET_BIO_ReadHandle *rh,
2348 struct GNUNET_FS_DownloadContext *parent,
2349 struct GNUNET_FS_SearchResult *search,
2350 const char *serialization);
2354 * Deserialize a search.
2356 * @param h overall context
2357 * @param rh file to deserialize from
2358 * @param psearch_result parent search result
2359 * @param serialization name under which the search was serialized
2361 static struct GNUNET_FS_SearchContext *
2362 deserialize_search (struct GNUNET_FS_Handle *h,
2363 struct GNUNET_BIO_ReadHandle *rh,
2364 struct GNUNET_FS_SearchResult *psearch_result,
2365 const char *serialization);
2369 * Function called with a filename of serialized search result
2372 * @param cls the `struct GNUNET_FS_SearchContext *`
2373 * @param filename complete filename (absolute path)
2374 * @return #GNUNET_OK (continue to iterate)
2377 deserialize_search_result (void *cls,
2378 const char *filename)
2380 struct GNUNET_FS_SearchContext *sc = cls;
2386 struct GNUNET_BIO_ReadHandle *rh;
2387 struct GNUNET_BIO_ReadHandle *drh;
2388 struct GNUNET_FS_SearchResult *sr;
2390 ser = get_serialization_short_name (filename);
2391 rh = GNUNET_BIO_read_open (filename);
2396 remove_sync_file_in_dir (sc->h,
2397 (NULL == sc->psearch_result)
2398 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2399 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2400 sc->serialization, ser);
2409 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2412 sr->serialization = ser;
2413 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2414 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2415 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2417 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2418 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2420 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2421 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2422 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2423 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2424 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2429 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2431 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2432 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2434 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2441 if (NULL != download)
2443 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2446 deserialize_download (sc->h, drh, NULL, sr, download);
2447 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2450 _("Failed to resume sub-download `%s': %s\n"),
2456 GNUNET_free (download);
2458 if (NULL != update_srch)
2461 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2464 deserialize_search (sc->h, drh, sr, update_srch);
2465 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2468 _("Failed to resume sub-search `%s': %s\n"),
2474 GNUNET_free (update_srch);
2476 GNUNET_break (GNUNET_YES ==
2477 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2480 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2483 _("Failure while resuming search operation `%s': %s\n"),
2489 GNUNET_free_non_null (download);
2490 GNUNET_free_non_null (emsg);
2491 GNUNET_free_non_null (uris);
2492 GNUNET_free_non_null (update_srch);
2493 if (NULL != sr->uri)
2494 GNUNET_FS_uri_destroy (sr->uri);
2495 if (NULL != sr->meta)
2496 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2497 GNUNET_free (sr->serialization);
2499 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2502 _("Failure while resuming search operation `%s': %s\n"),
2511 * Send the 'resume' signal to the callback; also actually
2512 * resume the download (put it in the queue). Does this
2513 * recursively for the top-level download and all child
2516 * @param dc download to resume
2519 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2521 struct GNUNET_FS_DownloadContext *dcc;
2522 struct GNUNET_FS_ProgressInfo pi;
2524 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2525 pi.value.download.specifics.resume.meta = dc->meta;
2526 pi.value.download.specifics.resume.message = dc->emsg;
2527 GNUNET_FS_download_make_status_ (&pi, dc);
2528 dcc = dc->child_head;
2531 signal_download_resume (dcc);
2534 if (NULL != dc->pending_head)
2535 GNUNET_FS_download_start_downloading_ (dc);
2540 * Signal resuming of a search to our clients (for the
2541 * top level search and all sub-searches).
2543 * @param sc search being resumed
2546 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2550 * Iterator over search results signaling resume to the client for
2553 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2554 * @param key current key code
2555 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2556 * @return #GNUNET_YES (we should continue to iterate)
2559 signal_result_resume (void *cls,
2560 const struct GNUNET_HashCode *key,
2563 struct GNUNET_FS_SearchContext *sc = cls;
2564 struct GNUNET_FS_ProgressInfo pi;
2565 struct GNUNET_FS_SearchResult *sr = value;
2567 if (0 == sr->mandatory_missing)
2569 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2570 pi.value.search.specifics.resume_result.meta = sr->meta;
2571 pi.value.search.specifics.resume_result.uri = sr->uri;
2572 pi.value.search.specifics.resume_result.result = sr;
2573 pi.value.search.specifics.resume_result.availability_rank =
2574 2 * sr->availability_success - sr->availability_trials;
2575 pi.value.search.specifics.resume_result.availability_certainty =
2576 sr->availability_trials;
2577 pi.value.search.specifics.resume_result.applicability_rank =
2578 sr->optional_support;
2579 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2581 if (NULL != sr->download)
2583 signal_download_resume (sr->download);
2587 GNUNET_FS_search_start_probe_ (sr);
2589 if (NULL != sr->update_search)
2590 signal_search_resume (sr->update_search);
2596 * Free memory allocated by the search context and its children
2598 * @param sc search context to free
2601 free_search_context (struct GNUNET_FS_SearchContext *sc);
2605 * Iterator over search results freeing each.
2607 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2608 * @param key current key code
2609 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2610 * @return #GNUNET_YES (we should continue to iterate)
2613 free_result (void *cls,
2614 const struct GNUNET_HashCode *key,
2617 struct GNUNET_FS_SearchResult *sr = value;
2619 if (NULL != sr->update_search)
2621 free_search_context (sr->update_search);
2622 GNUNET_assert (NULL == sr->update_search);
2624 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2625 GNUNET_FS_uri_destroy (sr->uri);
2632 * Free memory allocated by the search context and its children
2634 * @param sc search context to free
2637 free_search_context (struct GNUNET_FS_SearchContext *sc)
2639 if (NULL != sc->serialization)
2641 GNUNET_FS_remove_sync_file_ (sc->h,
2642 (sc->psearch_result ==
2643 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2644 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2646 GNUNET_FS_remove_sync_dir_ (sc->h,
2647 (sc->psearch_result ==
2648 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2649 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2652 GNUNET_free_non_null (sc->serialization);
2653 GNUNET_free_non_null (sc->emsg);
2654 if (NULL != sc->uri)
2655 GNUNET_FS_uri_destroy (sc->uri);
2656 if (NULL != sc->master_result_map)
2658 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2660 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2667 * Function called with a filename of serialized sub-download
2670 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2671 * @param filename complete filename (absolute path)
2672 * @return #GNUNET_OK (continue to iterate)
2675 deserialize_subdownload (void *cls,
2676 const char *filename)
2678 struct GNUNET_FS_DownloadContext *parent = cls;
2681 struct GNUNET_BIO_ReadHandle *rh;
2683 ser = get_serialization_short_name (filename);
2684 rh = GNUNET_BIO_read_open (filename);
2687 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2688 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2694 deserialize_download (parent->h, rh, parent, NULL, ser);
2695 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2698 _("Failed to resume sub-download `%s': %s\n"),
2709 * Free this download context and all of its descendants.
2710 * (only works during deserialization since not all possible
2711 * state it taken care of).
2713 * @param dc context to free
2716 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2718 struct GNUNET_FS_DownloadContext *dcc;
2720 if (NULL != dc->meta)
2721 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2722 if (NULL != dc->uri)
2723 GNUNET_FS_uri_destroy (dc->uri);
2724 GNUNET_free_non_null (dc->temp_filename);
2725 GNUNET_free_non_null (dc->emsg);
2726 GNUNET_free_non_null (dc->filename);
2727 GNUNET_free_non_null (dc->serialization);
2728 while (NULL != (dcc = dc->child_head))
2730 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2733 free_download_context (dcc);
2735 GNUNET_FS_free_download_request_ (dc->top_request);
2736 if (NULL != dc->active)
2737 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2743 * Deserialize a download.
2745 * @param h overall context
2746 * @param rh file to deserialize from
2747 * @param parent parent download
2748 * @param search associated search
2749 * @param serialization name under which the search was serialized
2752 deserialize_download (struct GNUNET_FS_Handle *h,
2753 struct GNUNET_BIO_ReadHandle *rh,
2754 struct GNUNET_FS_DownloadContext *parent,
2755 struct GNUNET_FS_SearchResult *search,
2756 const char *serialization)
2758 struct GNUNET_FS_DownloadContext *dc;
2767 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2768 dc->parent = parent;
2770 dc->serialization = GNUNET_strdup (serialization);
2772 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2773 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2774 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2775 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2776 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2778 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2780 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2782 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2784 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2785 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2786 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2787 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2788 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2789 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2790 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2791 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2796 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2798 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2799 dc->has_finished = (int) status;
2801 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2802 if (GNUNET_FS_uri_test_loc (dc->uri))
2803 GNUNET_assert (GNUNET_OK ==
2804 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2805 if (NULL == dc->emsg)
2807 dc->top_request = read_download_request (rh);
2808 if (NULL == dc->top_request)
2814 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2817 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2818 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2823 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2827 dc->search = search;
2828 search->download = dc;
2830 if ((NULL == parent) && (NULL == search))
2833 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2834 signal_download_resume (dc);
2837 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2840 GNUNET_free_non_null (uris);
2841 GNUNET_free_non_null (emsg);
2842 free_download_context (dc);
2847 * Signal resuming of a search to our clients (for the
2848 * top level search and all sub-searches).
2850 * @param sc search being resumed
2853 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2855 struct GNUNET_FS_ProgressInfo pi;
2857 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2858 pi.value.search.specifics.resume.message = sc->emsg;
2859 pi.value.search.specifics.resume.is_paused =
2860 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2861 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2862 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2863 &signal_result_resume, sc);
2869 * Deserialize a search.
2871 * @param h overall context
2872 * @param rh file to deserialize from
2873 * @param psearch_result parent search result
2874 * @param serialization name under which the search was serialized
2876 static struct GNUNET_FS_SearchContext *
2877 deserialize_search (struct GNUNET_FS_Handle *h,
2878 struct GNUNET_BIO_ReadHandle *rh,
2879 struct GNUNET_FS_SearchResult *psearch_result,
2880 const char *serialization)
2882 struct GNUNET_FS_SearchContext *sc;
2889 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2896 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2897 if (NULL != psearch_result)
2899 sc->psearch_result = psearch_result;
2900 psearch_result->update_search = sc;
2903 sc->serialization = GNUNET_strdup (serialization);
2905 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2906 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2907 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2908 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2909 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2911 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2912 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2914 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2915 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2920 sc->options = (enum GNUNET_FS_SearchOptions) options;
2921 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2922 dn = get_serialization_file_name_in_dir (h,
2923 (NULL == sc->psearch_result) ?
2924 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2925 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2926 sc->serialization, "");
2929 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2930 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2933 if (('\0' == in_pause) &&
2934 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2937 _("Could not resume running search, will resume as paused search\n"));
2939 signal_search_resume (sc);
2943 GNUNET_free_non_null (emsg);
2944 free_search_context (sc);
2945 GNUNET_free_non_null (uris);
2951 * Function called with a filename of serialized search operation
2954 * @param cls the `struct GNUNET_FS_Handle *`
2955 * @param filename complete filename (absolute path)
2956 * @return #GNUNET_OK (continue to iterate)
2959 deserialize_search_file (void *cls,
2960 const char *filename)
2962 struct GNUNET_FS_Handle *h = cls;
2965 struct GNUNET_BIO_ReadHandle *rh;
2966 struct GNUNET_FS_SearchContext *sc;
2969 if (0 != STAT (filename, &buf))
2971 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2974 if (S_ISDIR (buf.st_mode))
2975 return GNUNET_OK; /* skip directories */
2976 ser = get_serialization_short_name (filename);
2977 rh = GNUNET_BIO_read_open (filename);
2982 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2987 sc = deserialize_search (h, rh, NULL, ser);
2989 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2991 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2994 _("Failure while resuming search operation `%s': %s\n"),
3003 * Function called with a filename of serialized download operation
3006 * @param cls the `struct GNUNET_FS_Handle *`
3007 * @param filename complete filename (absolute path)
3008 * @return #GNUNET_OK (continue to iterate)
3011 deserialize_download_file (void *cls, const char *filename)
3013 struct GNUNET_FS_Handle *h = cls;
3016 struct GNUNET_BIO_ReadHandle *rh;
3018 ser = get_serialization_short_name (filename);
3019 rh = GNUNET_BIO_read_open (filename);
3022 if (0 != UNLINK (filename))
3023 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3027 deserialize_download (h, rh, NULL, NULL, ser);
3029 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3031 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3032 _("Failure while resuming download operation `%s': %s\n"),
3041 * Deserialize informatin about pending operations.
3043 * @param master_path which master directory should be scanned
3044 * @param proc function to call for each entry (will get @a h for 'cls')
3045 * @param h the `struct GNUNET_FS_Handle *`
3048 deserialization_master (const char *master_path,
3049 GNUNET_FileNameCallback proc,
3050 struct GNUNET_FS_Handle *h)
3054 dn = get_serialization_file_name (h, master_path, "");
3057 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3058 GNUNET_DISK_directory_scan (dn, proc, h);
3064 * Setup a connection to the file-sharing service.
3066 * @param cfg configuration to use
3067 * @param client_name unique identifier for this client
3068 * @param upcb function to call to notify about FS actions
3069 * @param upcb_cls closure for @a upcb
3070 * @param flags specific attributes for fs-operations
3071 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3072 * @return NULL on error
3074 struct GNUNET_FS_Handle *
3075 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3076 const char *client_name,
3077 GNUNET_FS_ProgressCallback upcb,
3079 enum GNUNET_FS_Flags flags, ...)
3081 struct GNUNET_FS_Handle *ret;
3082 enum GNUNET_FS_OPTIONS opt;
3085 ret = GNUNET_new (struct GNUNET_FS_Handle);
3087 ret->client_name = GNUNET_strdup (client_name);
3089 ret->upcb_cls = upcb_cls;
3091 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3092 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3093 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3094 va_start (ap, flags);
3095 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3099 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3100 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3103 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3104 ret->max_parallel_requests = va_arg (ap, unsigned int);
3109 GNUNET_free (ret->client_name);
3116 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3118 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3119 &deserialize_publish_file, ret);
3120 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3121 &deserialize_search_file, ret);
3122 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3123 &deserialize_download_file, ret);
3124 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3125 &deserialize_unindex_file, ret);
3132 * Close our connection with the file-sharing service.
3133 * The callback given to #GNUNET_FS_start() will no longer be
3134 * called after this function returns.
3135 * This function MUST NOT be called from within the
3138 * @param h handle that was returned from #GNUNET_FS_start()
3141 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3143 while (NULL != h->top_head)
3144 h->top_head->ssf (h->top_head->ssf_cls);
3145 if (NULL != h->queue_job)
3146 GNUNET_SCHEDULER_cancel (h->queue_job);
3147 GNUNET_free (h->client_name);
3152 /* end of fs_api.c */