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_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, qe->h->pending_tail, qe);
69 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head, qe->h->running_tail,
70 qe->h->running_tail, qe);
75 * Stop the given job (send signal, remove from active queue, update
76 * counters and state).
78 * @param qe job to stop
81 stop_job (struct GNUNET_FS_QueueEntry *qe)
85 GNUNET_assert (0 < qe->h->active_downloads);
86 qe->h->active_downloads--;
87 qe->h->active_blocks -= qe->blocks;
89 GNUNET_TIME_relative_add (qe->run_time,
90 GNUNET_TIME_absolute_get_duration
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Stopping job %p (%u active)\n",
95 qe->h->active_downloads);
96 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
97 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
98 qe->h->pending_tail, qe);
103 * Process the jobs in the job queue, possibly starting some
104 * and stopping others.
106 * @param cls the `struct GNUNET_FS_Handle *`
107 * @param tc scheduler context
110 process_job_queue (void *cls,
111 const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct GNUNET_FS_Handle *h = cls;
114 struct GNUNET_FS_QueueEntry *qe;
115 struct GNUNET_FS_QueueEntry *next;
116 struct GNUNET_TIME_Relative run_time;
117 struct GNUNET_TIME_Relative restart_at;
118 struct GNUNET_TIME_Relative rst;
119 struct GNUNET_TIME_Absolute end_time;
120 unsigned int num_downloads_waiting;
121 unsigned int num_downloads_active;
122 unsigned int num_downloads_expired;
123 unsigned int num_probes_active;
124 unsigned int num_probes_waiting;
125 unsigned int num_probes_expired;
126 int num_probes_change;
127 int num_downloads_change;
130 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
131 /* restart_at will be set to the time when it makes sense to
132 re-evaluate the job queue (unless, of course, jobs complete
133 or are added, then we'll be triggered immediately */
134 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
135 /* first, calculate some basic statistics on pending jobs */
136 num_probes_waiting = 0;
137 num_downloads_waiting = 0;
138 for (qe = h->pending_head; NULL != qe; qe = qe->next)
140 switch (qe->priority)
142 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
143 num_probes_waiting++;
145 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
146 num_downloads_waiting++;
153 /* now, calculate some basic statistics on running jobs */
154 num_probes_active = 0;
155 num_probes_expired = 0;
156 num_downloads_active = 0;
157 num_downloads_expired = 0;
158 next = h->running_head;
159 while (NULL != (qe = next))
162 switch (qe->priority)
164 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
165 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
166 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
167 rst = GNUNET_TIME_absolute_get_remaining (end_time);
168 if (0 == rst.rel_value_us)
170 num_probes_expired++;
176 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
179 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
181 GNUNET_TIME_relative_multiply (h->avg_block_latency,
182 qe->blocks * qe->start_times);
183 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
184 rst = GNUNET_TIME_absolute_get_remaining (end_time);
185 if (0 == rst.rel_value_us)
187 num_downloads_expired++;
192 num_downloads_active++;
193 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
206 num_downloads_active,
207 num_downloads_expired,
208 num_downloads_waiting);
209 /* calculate start/stop decisions */
210 if (h->active_downloads + num_downloads_waiting > h->max_parallel_requests)
212 /* stop probes if possible */
213 num_probes_change = - num_probes_active;
214 num_downloads_change = h->max_parallel_requests - h->active_downloads;
218 /* start all downloads */
219 num_downloads_change = num_downloads_waiting;
220 /* start as many probes as we can */
221 num_probes_change = GNUNET_MIN (num_probes_waiting,
222 h->max_parallel_requests - (h->active_downloads + num_downloads_waiting));
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Changing %d probes and %d downloads\n",
228 num_downloads_change);
229 /* actually stop probes */
230 next = h->running_head;
231 while (NULL != (qe = next))
234 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
236 if (num_probes_change < 0)
240 if (0 == num_probes_change)
244 GNUNET_break (0 <= num_probes_change);
246 /* start some more tasks if we now have empty slots */
247 block_limit_hit = GNUNET_NO;
248 next = h->pending_head;
249 while ( (NULL != (qe = next)) &&
250 ( (num_probes_change > 0) ||
251 (num_downloads_change > 0) ) )
254 switch (qe->priority)
256 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
257 if (num_probes_change > 0)
261 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
262 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
265 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
266 if ( (num_downloads_change > 0) &&
267 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
268 ( (qe->blocks > h->max_parallel_requests) &&
269 (0 == h->active_downloads) ) ) )
272 num_downloads_change--;
274 else if (num_downloads_change > 0)
275 block_limit_hit = GNUNET_YES;
282 GNUNET_break ( (0 == num_downloads_change) || (GNUNET_YES == block_limit_hit) );
283 GNUNET_break (0 == num_probes_change);
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
288 h->max_parallel_requests,
290 num_downloads_change,
291 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
293 /* make sure we run again */
295 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
300 * Add a job to the queue.
302 * @param h handle to the overall FS state
303 * @param start function to call to begin the job
304 * @param stop function to call to pause the job, or on dequeue (if the job was running)
305 * @param cls closure for start and stop
306 * @param blocks number of blocks this jobs uses
307 * @param priority how important is this download
308 * @return queue handle
310 struct GNUNET_FS_QueueEntry *
311 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
312 GNUNET_FS_QueueStart start,
313 GNUNET_FS_QueueStop stop, void *cls,
315 enum GNUNET_FS_QueuePriority priority)
317 struct GNUNET_FS_QueueEntry *qe;
319 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
324 qe->queue_time = GNUNET_TIME_absolute_get ();
326 qe->priority = priority;
327 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
328 h->pending_tail, qe);
329 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
330 GNUNET_SCHEDULER_cancel (h->queue_job);
331 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 * Dequeue a job from the queue.
342 * @param qe handle for the job
345 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
347 struct GNUNET_FS_Handle *h;
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Dequeueing job %p\n",
353 if (NULL != qe->client)
355 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
357 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
358 GNUNET_SCHEDULER_cancel (h->queue_job);
359 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
364 * Create a top-level activity entry.
366 * @param h global fs handle
367 * @param ssf suspend signal function to use
368 * @param ssf_cls closure for @a ssf
369 * @return fresh top-level activity handle
371 struct TopLevelActivity *
372 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
373 SuspendSignalFunction ssf,
376 struct TopLevelActivity *ret;
378 ret = GNUNET_new (struct TopLevelActivity);
380 ret->ssf_cls = ssf_cls;
381 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
387 * Destroy a top-level activity entry.
389 * @param h global fs handle
390 * @param top top level activity entry
393 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
394 struct TopLevelActivity *top)
396 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
402 * Closure for #data_reader_file().
407 * Name of the file to read.
412 * File descriptor, NULL if it has not yet been opened.
414 struct GNUNET_DISK_FileHandle *fd;
419 * Function that provides data by reading from a file.
421 * @param cls closure with the `struct FileInfo *`
422 * @param offset offset to read from; it is possible
423 * that the caller might need to go backwards
424 * a bit at times; set to `UINT64_MAX` to tell
425 * the reader that we won't be reading for a while
426 * (used to close the file descriptor but NOT fully
427 * clean up the reader's state); in this case,
428 * a value of '0' for @a max should be ignored
429 * @param max maximum number of bytes that should be
430 * copied to @a buf; readers are not allowed
431 * to provide less data unless there is an error;
432 * a value of "0" will be used at the end to allow
433 * the reader to clean up its internal state
434 * @param buf where the reader should write the data
435 * @param emsg location for the reader to store an error message
436 * @return number of bytes written, usually @a max, 0 on error
439 GNUNET_FS_data_reader_file_ (void *cls,
445 struct FileInfo *fi = cls;
448 if (UINT64_MAX == offset)
452 GNUNET_DISK_file_close (fi->fd);
460 GNUNET_DISK_file_close (fi->fd);
461 GNUNET_free (fi->filename);
468 GNUNET_DISK_file_open (fi->filename,
469 GNUNET_DISK_OPEN_READ,
470 GNUNET_DISK_PERM_NONE);
473 GNUNET_asprintf (emsg,
474 _("Could not open file `%s': %s"),
480 if ( (GNUNET_SYSERR ==
481 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
482 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
484 GNUNET_asprintf (emsg,
485 _("Could not read file `%s': %s"),
492 GNUNET_asprintf (emsg,
493 _("Short read reading from file `%s'!"),
502 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
504 * @param filename file to read
505 * @return closure to use, NULL on error
508 GNUNET_FS_make_file_reader_context_ (const char *filename)
512 fi = GNUNET_new (struct FileInfo);
513 fi->filename = GNUNET_STRINGS_filename_expand (filename);
514 if (NULL == fi->filename)
524 * Function that provides data by copying from a buffer.
526 * @param cls closure (points to the buffer)
527 * @param offset offset to read from; it is possible
528 * that the caller might need to go backwards
529 * a bit at times; set to `UINT64_MAX` to tell
530 * the reader that we won't be reading for a while
531 * (used to close the file descriptor but NOT fully
532 * clean up the reader's state); in this case,
533 * a value of '0' for @a max should be ignored
534 * @param max maximum number of bytes that should be
535 * copied to @a buf; readers are not allowed
536 * to provide less data unless there is an error;
537 * a value of "0" will be used at the end to allow
538 * the reader to clean up its internal state
539 * @param buf where the reader should write the data
540 * @param emsg location for the reader to store an error message
541 * @return number of bytes written, usually @a max, 0 on error
544 GNUNET_FS_data_reader_copy_ (void *cls,
552 if (UINT64_MAX == offset)
556 GNUNET_free_non_null (data);
559 memcpy (buf, &data[offset], max);
565 * Return the full filename where we would store state information
566 * (for serialization/deserialization).
568 * @param h master context
569 * @param ext component of the path
570 * @param ent entity identifier (or emtpy string for the directory)
571 * @return NULL on error
574 get_serialization_file_name (struct GNUNET_FS_Handle *h,
581 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
582 return NULL; /* persistence not requested */
584 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
587 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
588 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
590 GNUNET_free (basename);
596 * Return the full filename where we would store state information
597 * (for serialization/deserialization) that is associated with a
600 * @param h master context
601 * @param ext component of the path
602 * @param uni name of the parent operation
603 * @param ent entity identifier (or emtpy string for the directory)
604 * @return NULL on error
607 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
615 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
616 return NULL; /* persistence not requested */
618 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
621 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
622 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
623 uni, DIR_SEPARATOR_STR, ent);
624 GNUNET_free (basename);
630 * Return a read handle for deserialization.
632 * @param h master context
633 * @param ext component of the path
634 * @param ent entity identifier (or emtpy string for the directory)
635 * @return NULL on error
637 static struct GNUNET_BIO_ReadHandle *
638 get_read_handle (struct GNUNET_FS_Handle *h,
643 struct GNUNET_BIO_ReadHandle *ret;
645 fn = get_serialization_file_name (h, ext, ent);
648 ret = GNUNET_BIO_read_open (fn);
655 * Return a write handle for serialization.
657 * @param h master context
658 * @param ext component of the path
659 * @param ent entity identifier (or emtpy string for the directory)
660 * @return NULL on error
662 static struct GNUNET_BIO_WriteHandle *
663 get_write_handle (struct GNUNET_FS_Handle *h,
668 struct GNUNET_BIO_WriteHandle *ret;
670 fn = get_serialization_file_name (h, ext, ent);
673 ret = GNUNET_BIO_write_open (fn);
674 GNUNET_break (NULL != ret);
681 * Return a write handle for serialization.
683 * @param h master context
684 * @param ext component of the path
685 * @param uni name of parent
686 * @param ent entity identifier (or emtpy string for the directory)
687 * @return NULL on error
689 static struct GNUNET_BIO_WriteHandle *
690 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
691 const char *uni, const char *ent)
694 struct GNUNET_BIO_WriteHandle *ret;
696 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
699 ret = GNUNET_BIO_write_open (fn);
706 * Remove serialization/deserialization file from disk.
708 * @param h master context
709 * @param ext component of the path
710 * @param ent entity identifier
713 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
719 if ((NULL == ent) || (0 == strlen (ent)))
724 filename = get_serialization_file_name (h, ext, ent);
725 if (NULL != filename)
727 if ( (0 != UNLINK (filename)) &&
729 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
730 GNUNET_free (filename);
736 * Remove serialization/deserialization file from disk.
738 * @param h master context
739 * @param ext component of the path
740 * @param uni parent name
741 * @param ent entity identifier
744 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
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,
780 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
783 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
784 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
785 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
791 * Serialize a start-time. Since we use start-times to
792 * calculate the duration of some operation, we actually
793 * do not serialize the absolute time but the (relative)
794 * duration since the start time. When we then
795 * deserialize the start time, we take the current time and
796 * subtract that duration so that we get again an absolute
797 * time stamp that will result in correct performance
800 * @param wh handle for writing
801 * @param timestamp time to serialize
802 * @return #GNUNET_OK on success
805 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
806 struct GNUNET_TIME_Absolute timestamp)
808 struct GNUNET_TIME_Relative dur;
810 dur = GNUNET_TIME_absolute_get_duration (timestamp);
811 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
816 * Deserialize a start-time. Since we use start-times to
817 * calculate the duration of some operation, we actually
818 * do not serialize the absolute time but the (relative)
819 * duration since the start time. Thus, when we then
820 * deserialize the start time, we take the current time and
821 * subtract that duration so that we get again an absolute
822 * time stamp that will result in correct performance
825 * @param rh handle for reading
826 * @param timestamp where to write the deserialized timestamp
827 * @return #GNUNET_OK on success
830 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
831 struct GNUNET_TIME_Absolute *timestamp)
833 struct GNUNET_TIME_Relative dur;
835 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
836 return GNUNET_SYSERR;
837 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
843 * Using the given serialization filename, try to deserialize
844 * the file-information tree associated with it.
846 * @param h master context
847 * @param filename name of the file (without directory) with
849 * @return NULL on error
851 static struct GNUNET_FS_FileInformation *
852 deserialize_file_information (struct GNUNET_FS_Handle *h,
853 const char *filename);
857 * Using the given serialization filename, try to deserialize
858 * the file-information tree associated with it.
860 * @param h master context
861 * @param fn name of the file (without directory) with
863 * @param rh handle for reading
864 * @return NULL on error
866 static struct GNUNET_FS_FileInformation *
867 deserialize_fi_node (struct GNUNET_FS_Handle *h,
869 struct GNUNET_BIO_ReadHandle *rh)
871 struct GNUNET_FS_FileInformation *ret;
872 struct GNUNET_FS_FileInformation *nxt;
879 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
884 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
889 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
890 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
892 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
893 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
894 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
896 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
897 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
898 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
899 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
901 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
903 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
904 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
905 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
906 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
913 case 0: /* file-insert */
914 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
919 ret->is_directory = GNUNET_NO;
920 ret->data.file.do_index = GNUNET_NO;
921 ret->data.file.have_hash = GNUNET_NO;
922 ret->data.file.index_start_confirmed = GNUNET_NO;
923 if (GNUNET_NO == ret->is_published)
925 if (NULL == ret->filename)
927 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
928 ret->data.file.reader_cls =
929 GNUNET_malloc_large (ret->data.file.file_size);
930 if (ret->data.file.reader_cls == NULL)
933 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
934 ret->data.file.file_size))
942 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
943 ret->data.file.reader_cls =
944 GNUNET_FS_make_file_reader_context_ (ret->filename);
948 case 1: /* file-index, no hash */
949 if (NULL == ret->filename)
954 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
959 ret->is_directory = GNUNET_NO;
960 ret->data.file.do_index = GNUNET_YES;
961 ret->data.file.have_hash = GNUNET_NO;
962 ret->data.file.index_start_confirmed = GNUNET_NO;
963 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
964 ret->data.file.reader_cls =
965 GNUNET_FS_make_file_reader_context_ (ret->filename);
967 case 2: /* file-index-with-hash */
968 if (NULL == ret->filename)
973 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
975 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
976 sizeof (struct GNUNET_HashCode))))
981 ret->is_directory = GNUNET_NO;
982 ret->data.file.do_index = GNUNET_YES;
983 ret->data.file.have_hash = GNUNET_YES;
984 ret->data.file.index_start_confirmed = GNUNET_NO;
985 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
986 ret->data.file.reader_cls =
987 GNUNET_FS_make_file_reader_context_ (ret->filename);
989 case 3: /* file-index-with-hash-confirmed */
990 if (NULL == ret->filename)
995 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
997 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
998 sizeof (struct GNUNET_HashCode))))
1003 ret->is_directory = GNUNET_NO;
1004 ret->data.file.do_index = GNUNET_YES;
1005 ret->data.file.have_hash = GNUNET_YES;
1006 ret->data.file.index_start_confirmed = GNUNET_YES;
1007 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1008 ret->data.file.reader_cls =
1009 GNUNET_FS_make_file_reader_context_ (ret->filename);
1011 case 4: /* directory */
1012 ret->is_directory = GNUNET_YES;
1013 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1014 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1015 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1016 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1018 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1020 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1025 ret->data.dir.dir_size = (uint32_t) dsize;
1026 if (NULL != filename)
1028 ret->data.dir.entries = deserialize_file_information (h, filename);
1029 GNUNET_free (filename);
1031 nxt = ret->data.dir.entries;
1043 ret->serialization = GNUNET_strdup (fn);
1045 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1050 if (NULL != filename)
1052 ret->next = deserialize_file_information (h, filename);
1053 GNUNET_free (filename);
1056 GNUNET_free_non_null (ksks);
1057 GNUNET_free_non_null (chks);
1060 GNUNET_free_non_null (ksks);
1061 GNUNET_free_non_null (chks);
1062 GNUNET_free_non_null (filename);
1063 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1069 * Using the given serialization filename, try to deserialize
1070 * the file-information tree associated with it.
1072 * @param h master context
1073 * @param filename name of the file (without directory) with
1075 * @return NULL on error
1077 static struct GNUNET_FS_FileInformation *
1078 deserialize_file_information (struct GNUNET_FS_Handle *h,
1079 const char *filename)
1081 struct GNUNET_FS_FileInformation *ret;
1082 struct GNUNET_BIO_ReadHandle *rh;
1086 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1089 ret = deserialize_fi_node (h, filename, rh);
1090 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1093 _("Failed to resume publishing information `%s': %s\n"),
1099 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1102 if (0 != UNLINK (fn))
1103 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1112 * Given a serialization name (full absolute path), return the
1113 * basename of the file (without the path), which must only
1114 * consist of the 6 random characters.
1116 * @param fullname name to extract the basename from
1117 * @return copy of the basename, NULL on error
1120 get_serialization_short_name (const char *fullname)
1127 /* FIXME: we could do this faster since we know
1128 * the length of 'end'... */
1129 while ('\0' != *nxt)
1131 if (DIR_SEPARATOR == *nxt)
1135 if ((NULL == end) || (0 == strlen (end)))
1140 GNUNET_break (6 == strlen (end));
1141 return GNUNET_strdup (end);
1146 * Create a new random name for serialization. Also checks if persistence
1147 * is enabled and returns NULL if not.
1149 * @param h master context
1150 * @param ext component of the path
1151 * @return NULL on errror
1154 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1161 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1162 return NULL; /* persistence not requested */
1163 dn = get_serialization_file_name (h, ext, "");
1166 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1171 fn = GNUNET_DISK_mktemp (dn);
1174 return NULL; /* epic fail */
1175 ret = get_serialization_short_name (fn);
1182 * Create a new random name for serialization. Also checks if persistence
1183 * is enabled and returns NULL if not.
1185 * @param h master context
1186 * @param ext component of the path
1187 * @param uni name of parent
1188 * @return NULL on errror
1191 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1199 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1200 return NULL; /* persistence not requested */
1201 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1204 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1209 fn = GNUNET_DISK_mktemp (dn);
1212 return NULL; /* epic fail */
1213 ret = get_serialization_short_name (fn);
1220 * Copy all of the data from the reader to the write handle.
1222 * @param wh write handle
1223 * @param fi file with reader
1224 * @return #GNUNET_OK on success
1227 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1228 struct GNUNET_FS_FileInformation *fi)
1230 char buf[32 * 1024];
1238 while (off < fi->data.file.file_size)
1240 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1242 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1246 return GNUNET_SYSERR;
1248 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1249 return GNUNET_SYSERR;
1257 * Create a temporary file on disk to store the current
1258 * state of @a fi in.
1260 * @param fi file information to sync with disk
1263 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1266 struct GNUNET_BIO_WriteHandle *wh;
1271 if (NULL == fi->serialization)
1273 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1274 if (NULL == fi->serialization)
1276 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1280 GNUNET_free (fi->serialization);
1281 fi->serialization = NULL;
1284 if (GNUNET_YES == fi->is_directory)
1286 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1288 else if (GNUNET_YES == fi->data.file.have_hash)
1290 else if (GNUNET_YES == fi->data.file.do_index)
1294 if (NULL != fi->keywords)
1295 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1298 if (NULL != fi->chk_uri)
1299 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1302 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1303 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1304 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1305 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1306 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1307 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1308 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1310 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1311 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1312 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1313 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1318 GNUNET_free_non_null (chks);
1320 GNUNET_free_non_null (ksks);
1325 case 0: /* file-insert */
1326 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1331 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1332 if (GNUNET_OK != copy_from_reader (wh, fi))
1338 case 1: /* file-index, no hash */
1339 if (NULL == fi->filename)
1344 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1350 case 2: /* file-index-with-hash */
1351 case 3: /* file-index-with-hash-confirmed */
1352 if (NULL == fi->filename)
1357 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1359 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1360 sizeof (struct GNUNET_HashCode))))
1366 case 4: /* directory */
1367 if ( (NULL != fi->data.dir.entries) &&
1368 (NULL == fi->data.dir.entries->serialization) )
1369 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1370 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1371 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1372 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1374 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1375 (uint32_t) fi->data.dir.dir_size)) ||
1377 GNUNET_BIO_write_string (wh,
1378 (fi->data.dir.entries ==
1379 NULL) ? NULL : fi->data.dir.
1380 entries->serialization)))
1390 if ( (NULL != fi->next) &&
1391 (NULL == fi->next->serialization) )
1392 GNUNET_FS_file_information_sync_ (fi->next);
1394 GNUNET_BIO_write_string (wh,
1396 NULL) ? fi->next->serialization : NULL))
1401 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1410 (void) GNUNET_BIO_write_close (wh);
1411 GNUNET_free_non_null (chks);
1412 GNUNET_free_non_null (ksks);
1413 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1417 if (0 != UNLINK (fn))
1418 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1421 GNUNET_free (fi->serialization);
1422 fi->serialization = NULL;
1428 * Find the entry in the file information struct where the
1429 * serialization filename matches the given name.
1431 * @param pos file information to search
1432 * @param srch filename to search for
1433 * @return NULL if srch was not found in this subtree
1435 static struct GNUNET_FS_FileInformation *
1436 find_file_position (struct GNUNET_FS_FileInformation *pos,
1439 struct GNUNET_FS_FileInformation *r;
1443 if (0 == strcmp (srch, pos->serialization))
1445 if ( (GNUNET_YES == pos->is_directory) &&
1446 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1455 * Signal the FS's progress function that we are resuming
1458 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1459 * @param fi the entry in the publish-structure
1460 * @param length length of the file or directory
1461 * @param meta metadata for the file or directory (can be modified)
1462 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1463 * @param bo block options (can be modified)
1464 * @param do_index should we index?
1465 * @param client_info pointer to client context set upon creation (can be modified)
1466 * @return #GNUNET_OK to continue (always)
1469 fip_signal_resume (void *cls,
1470 struct GNUNET_FS_FileInformation *fi,
1472 struct GNUNET_CONTAINER_MetaData *meta,
1473 struct GNUNET_FS_Uri **uri,
1474 struct GNUNET_FS_BlockOptions *bo,
1478 struct GNUNET_FS_PublishContext *pc = cls;
1479 struct GNUNET_FS_ProgressInfo pi;
1481 if (GNUNET_YES == pc->skip_next_fi_callback)
1483 pc->skip_next_fi_callback = GNUNET_NO;
1486 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1487 pi.value.publish.specifics.resume.message = fi->emsg;
1488 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1489 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1490 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1492 /* process entries in directory */
1493 pc->skip_next_fi_callback = GNUNET_YES;
1494 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1501 * Function called with a filename of serialized publishing operation
1504 * @param cls the `struct GNUNET_FS_Handle *`
1505 * @param filename complete filename (absolute path)
1506 * @return #GNUNET_OK (continue to iterate)
1509 deserialize_publish_file (void *cls,
1510 const char *filename)
1512 struct GNUNET_FS_Handle *h = cls;
1513 struct GNUNET_BIO_ReadHandle *rh;
1514 struct GNUNET_FS_PublishContext *pc;
1519 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1523 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1525 pc->serialization = get_serialization_short_name (filename);
1528 rh = GNUNET_BIO_read_open (filename);
1534 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1536 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1537 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1538 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1539 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1541 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1542 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1543 || ( (GNUNET_YES == have_ns) &&
1544 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1549 pc->options = options;
1550 pc->all_done = all_done;
1551 if (NULL == fi_root)
1556 pc->fi = deserialize_file_information (h, fi_root);
1562 if (GNUNET_YES == have_ns)
1564 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1567 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1568 (GNUNET_YES != pc->all_done))
1570 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1571 if (NULL == pc->dsh)
1576 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1577 GNUNET_free (fi_pos);
1579 if (NULL == pc->fi_pos)
1581 /* failed to find position for resuming, outch! Will start from root! */
1583 if (GNUNET_YES != pc->all_done)
1584 pc->fi_pos = pc->fi;
1587 GNUNET_free (fi_root);
1589 /* generate RESUME event(s) */
1590 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1592 /* re-start publishing (if needed)... */
1593 if (GNUNET_YES != pc->all_done)
1595 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1597 GNUNET_SCHEDULER_add_with_priority
1598 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1599 &GNUNET_FS_publish_main_, pc);
1601 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1604 _("Failure while resuming publishing operation `%s': %s\n"),
1608 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1611 GNUNET_free_non_null (pc->nid);
1612 GNUNET_free_non_null (pc->nuid);
1613 GNUNET_free_non_null (fi_root);
1614 GNUNET_free_non_null (fi_pos);
1615 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1618 _("Failed to resume publishing operation `%s': %s\n"), filename,
1623 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1624 if (0 != UNLINK (filename))
1625 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1626 GNUNET_free (pc->serialization);
1633 * Synchronize this publishing struct with its mirror
1634 * on disk. Note that all internal FS-operations that change
1635 * publishing structs should already call "sync" internally,
1636 * so this function is likely not useful for clients.
1638 * @param pc the struct to sync
1641 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1643 struct GNUNET_BIO_WriteHandle *wh;
1646 if (NULL == pc->serialization)
1648 make_serialization_file_name (pc->h,
1649 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1650 if (NULL == pc->serialization)
1654 if (NULL == pc->fi->serialization)
1659 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1666 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1667 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1668 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1669 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1670 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1671 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1672 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1674 GNUNET_BIO_write_string (wh,
1675 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1676 ( (NULL != pc->ns) &&
1677 (GNUNET_OK != GNUNET_BIO_write (wh,
1679 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1684 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1693 (void) GNUNET_BIO_write_close (wh);
1694 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1696 GNUNET_free (pc->serialization);
1697 pc->serialization = NULL;
1702 * Synchronize this unindex struct with its mirror
1703 * on disk. Note that all internal FS-operations that change
1704 * publishing structs should already call "sync" internally,
1705 * so this function is likely not useful for clients.
1707 * @param uc the struct to sync
1710 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1712 struct GNUNET_BIO_WriteHandle *wh;
1715 if (NULL == uc->serialization)
1717 make_serialization_file_name (uc->h,
1718 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1719 if (NULL == uc->serialization)
1721 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1728 if (NULL != uc->ksk_uri)
1729 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1732 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1733 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1734 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1735 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1737 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1738 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1739 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1740 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1742 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1743 ((uc->state == UNINDEX_STATE_ERROR) &&
1744 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1749 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1758 (void) GNUNET_BIO_write_close (wh);
1759 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1761 GNUNET_free (uc->serialization);
1762 uc->serialization = NULL;
1767 * Serialize a download request.
1769 * @param wh handle for writing the download request to disk
1770 * @param dr the the request to write to disk
1771 * @return #GNUNET_YES on success, #GNUNET_NO on error
1774 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1775 struct DownloadRequest *dr)
1779 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1780 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1781 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1782 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1784 if ((BRS_CHK_SET == dr->state) &&
1786 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1788 for (i = 0; i < dr->num_children; i++)
1789 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1796 * Read a download request tree.
1798 * @param rh cadet to read from
1799 * @return value the download request read from disk, NULL on error
1801 static struct DownloadRequest *
1802 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1804 struct DownloadRequest *dr;
1807 dr = GNUNET_new (struct DownloadRequest);
1808 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1809 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1810 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1811 (dr->num_children > CHK_PER_INODE) ||
1812 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1813 ( (0 == dr->depth) &&
1814 (dr->num_children > 0) ) ||
1815 ((dr->depth > 0) && (0 == dr->num_children)))
1818 dr->num_children = 0;
1821 if (dr->num_children > 0)
1823 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1827 case BRS_RECONSTRUCT_DOWN:
1828 case BRS_RECONSTRUCT_META_UP:
1829 case BRS_RECONSTRUCT_UP:
1833 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1836 case BRS_DOWNLOAD_DOWN:
1837 case BRS_DOWNLOAD_UP:
1844 for (i = 0; i < dr->num_children; i++)
1846 if (NULL == (dr->children[i] = read_download_request (rh)))
1848 dr->children[i]->parent = dr;
1852 GNUNET_FS_free_download_request_ (dr);
1858 * Compute the name of the sync file (or directory) for the given download
1861 * @param dc download context to compute for
1862 * @param uni unique filename to use, use "" for the directory name
1863 * @param ext extension to use, use ".dir" for our own subdirectory
1864 * @return the expanded file name, NULL for none
1867 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1874 if (dc->parent == NULL)
1875 return get_serialization_file_name (dc->h,
1876 (dc->search != NULL) ?
1877 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1878 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1880 if (NULL == dc->parent->serialization)
1882 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1885 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1892 * Synchronize this download struct with its mirror
1893 * on disk. Note that all internal FS-operations that change
1894 * publishing structs should already call "sync" internally,
1895 * so this function is likely not useful for clients.
1897 * @param dc the struct to sync
1900 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1902 struct GNUNET_BIO_WriteHandle *wh;
1907 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1908 return; /* we don't sync probes */
1909 if (NULL == dc->serialization)
1911 dir = get_download_sync_filename (dc, "", "");
1914 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1919 fn = GNUNET_DISK_mktemp (dir);
1923 dc->serialization = get_serialization_short_name (fn);
1927 fn = get_download_sync_filename (dc, dc->serialization, "");
1930 GNUNET_free (dc->serialization);
1931 dc->serialization = NULL;
1936 wh = GNUNET_BIO_write_open (fn);
1939 GNUNET_free (dc->serialization);
1940 dc->serialization = NULL;
1944 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1945 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1946 uris = GNUNET_FS_uri_to_string (dc->uri);
1947 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1948 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1949 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1950 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1951 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1952 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1953 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1954 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1955 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1956 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1957 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1958 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1959 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1964 if (NULL == dc->emsg)
1966 GNUNET_assert (dc->top_request != NULL);
1967 if (GNUNET_YES != write_download_request (wh, dc->top_request))
1973 GNUNET_free_non_null (uris);
1975 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1985 (void) GNUNET_BIO_write_close (wh);
1986 GNUNET_free_non_null (uris);
1987 if (0 != UNLINK (fn))
1988 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1990 GNUNET_free (dc->serialization);
1991 dc->serialization = NULL;
1996 * Synchronize this search result with its mirror
1997 * on disk. Note that all internal FS-operations that change
1998 * publishing structs should already call "sync" internally,
1999 * so this function is likely not useful for clients.
2001 * @param sr the struct to sync
2004 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2006 struct GNUNET_BIO_WriteHandle *wh;
2012 if (NULL == sr->serialization)
2014 make_serialization_file_name_in_dir (sr->h,
2015 (sr->sc->psearch_result ==
2017 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2018 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2019 sr->sc->serialization);
2020 if (NULL == sr->serialization)
2022 wh = get_write_handle_in_dir (sr->h,
2023 (sr->sc->psearch_result ==
2024 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2025 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2026 sr->sc->serialization, sr->serialization);
2032 uris = GNUNET_FS_uri_to_string (sr->uri);
2033 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2035 GNUNET_BIO_write_string (wh,
2037 NULL ? sr->download->serialization : NULL)) ||
2039 GNUNET_BIO_write_string (wh,
2040 sr->update_search !=
2041 NULL ? sr->update_search->serialization : NULL))
2042 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2043 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2044 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2045 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2046 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2047 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2052 if ( (NULL != sr->uri) &&
2053 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2054 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2055 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2060 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2066 GNUNET_free_non_null (uris);
2069 GNUNET_free_non_null (uris);
2071 (void) GNUNET_BIO_write_close (wh);
2072 remove_sync_file_in_dir (sr->h,
2073 (NULL == sr->sc->psearch_result)
2074 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2075 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2076 sr->sc->serialization, sr->serialization);
2077 GNUNET_free (sr->serialization);
2078 sr->serialization = NULL;
2083 * Synchronize this search struct with its mirror
2084 * on disk. Note that all internal FS-operations that change
2085 * publishing structs should already call "sync" internally,
2086 * so this function is likely not useful for clients.
2088 * @param sc the struct to sync
2091 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2093 struct GNUNET_BIO_WriteHandle *wh;
2096 const char *category;
2099 (NULL == sc->psearch_result)
2100 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2101 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2102 if (NULL == sc->serialization)
2103 sc->serialization = make_serialization_file_name (sc->h, category);
2104 if (NULL == sc->serialization)
2107 wh = get_write_handle (sc->h, category, sc->serialization);
2113 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2114 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2115 uris = GNUNET_FS_uri_to_string (sc->uri);
2116 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2117 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2118 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2119 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2120 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2121 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2122 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2129 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2138 (void) GNUNET_BIO_write_close (wh);
2139 GNUNET_free_non_null (uris);
2140 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2141 GNUNET_free (sc->serialization);
2142 sc->serialization = NULL;
2147 * Function called with a filename of serialized unindexing operation
2150 * @param cls the `struct GNUNET_FS_Handle *`
2151 * @param filename complete filename (absolute path)
2152 * @return #GNUNET_OK (continue to iterate)
2155 deserialize_unindex_file (void *cls,
2156 const char *filename)
2158 struct GNUNET_FS_Handle *h = cls;
2159 struct GNUNET_BIO_ReadHandle *rh;
2160 struct GNUNET_FS_UnindexContext *uc;
2161 struct GNUNET_FS_ProgressInfo pi;
2166 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2168 uc->serialization = get_serialization_short_name (filename);
2169 rh = GNUNET_BIO_read_open (filename);
2177 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2178 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2179 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2180 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2181 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2182 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2183 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2185 GNUNET_free_non_null (uris);
2191 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2193 if (NULL == uc->ksk_uri)
2196 GNUNET_free_non_null (emsg);
2200 if ( (uc->ksk_offset > 0) &&
2201 ( (NULL == uc->ksk_uri) ||
2202 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2207 uc->state = (enum UnindexState) state;
2210 case UNINDEX_STATE_HASHING:
2212 case UNINDEX_STATE_FS_NOTIFY:
2214 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2215 sizeof (struct GNUNET_HashCode)))
2221 case UNINDEX_STATE_DS_REMOVE:
2222 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2223 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2225 case UNINDEX_STATE_COMPLETE:
2227 case UNINDEX_STATE_ERROR:
2229 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2239 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2240 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2241 pi.value.unindex.specifics.resume.message = uc->emsg;
2242 GNUNET_FS_unindex_make_status_ (&pi, uc,
2244 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2247 case UNINDEX_STATE_HASHING:
2249 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2251 &GNUNET_FS_unindex_process_hash_, uc);
2253 case UNINDEX_STATE_FS_NOTIFY:
2254 uc->state = UNINDEX_STATE_HASHING;
2255 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2257 case UNINDEX_STATE_DS_REMOVE:
2258 GNUNET_FS_unindex_do_remove_ (uc);
2260 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2261 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2263 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2264 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2266 case UNINDEX_STATE_COMPLETE:
2267 case UNINDEX_STATE_ERROR:
2268 /* no need to resume any operation, we were done */
2273 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2276 _("Failure while resuming unindexing operation `%s': %s\n"),
2282 GNUNET_free_non_null (uc->filename);
2283 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2286 _("Failed to resume unindexing operation `%s': %s\n"),
2291 if (NULL != uc->serialization)
2292 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2294 GNUNET_free_non_null (uc->serialization);
2301 * Deserialize a download.
2303 * @param h overall context
2304 * @param rh file to deserialize from
2305 * @param parent parent download
2306 * @param search associated search
2307 * @param serialization name under which the search was serialized
2310 deserialize_download (struct GNUNET_FS_Handle *h,
2311 struct GNUNET_BIO_ReadHandle *rh,
2312 struct GNUNET_FS_DownloadContext *parent,
2313 struct GNUNET_FS_SearchResult *search,
2314 const char *serialization);
2318 * Deserialize a search.
2320 * @param h overall context
2321 * @param rh file to deserialize from
2322 * @param psearch_result parent search result
2323 * @param serialization name under which the search was serialized
2325 static struct GNUNET_FS_SearchContext *
2326 deserialize_search (struct GNUNET_FS_Handle *h,
2327 struct GNUNET_BIO_ReadHandle *rh,
2328 struct GNUNET_FS_SearchResult *psearch_result,
2329 const char *serialization);
2333 * Function called with a filename of serialized search result
2336 * @param cls the `struct GNUNET_FS_SearchContext *`
2337 * @param filename complete filename (absolute path)
2338 * @return #GNUNET_OK (continue to iterate)
2341 deserialize_search_result (void *cls,
2342 const char *filename)
2344 struct GNUNET_FS_SearchContext *sc = cls;
2350 struct GNUNET_BIO_ReadHandle *rh;
2351 struct GNUNET_BIO_ReadHandle *drh;
2352 struct GNUNET_FS_SearchResult *sr;
2354 ser = get_serialization_short_name (filename);
2355 rh = GNUNET_BIO_read_open (filename);
2360 remove_sync_file_in_dir (sc->h,
2361 (NULL == sc->psearch_result)
2362 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2363 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2364 sc->serialization, ser);
2373 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2376 sr->serialization = ser;
2377 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2378 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2379 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2381 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2382 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2384 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2385 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2386 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2387 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2388 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2393 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2395 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2396 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2398 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2405 if (NULL != download)
2407 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2410 deserialize_download (sc->h, drh, NULL, sr, download);
2411 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2414 _("Failed to resume sub-download `%s': %s\n"),
2420 GNUNET_free (download);
2422 if (NULL != update_srch)
2425 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2428 deserialize_search (sc->h, drh, sr, update_srch);
2429 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2432 _("Failed to resume sub-search `%s': %s\n"),
2438 GNUNET_free (update_srch);
2440 GNUNET_break (GNUNET_YES ==
2441 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2444 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2447 _("Failure while resuming search operation `%s': %s\n"),
2453 GNUNET_free_non_null (download);
2454 GNUNET_free_non_null (emsg);
2455 GNUNET_free_non_null (uris);
2456 GNUNET_free_non_null (update_srch);
2457 if (NULL != sr->uri)
2458 GNUNET_FS_uri_destroy (sr->uri);
2459 if (NULL != sr->meta)
2460 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2461 GNUNET_free (sr->serialization);
2463 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2466 _("Failure while resuming search operation `%s': %s\n"),
2475 * Send the 'resume' signal to the callback; also actually
2476 * resume the download (put it in the queue). Does this
2477 * recursively for the top-level download and all child
2480 * @param dc download to resume
2483 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2485 struct GNUNET_FS_DownloadContext *dcc;
2486 struct GNUNET_FS_ProgressInfo pi;
2488 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2489 pi.value.download.specifics.resume.meta = dc->meta;
2490 pi.value.download.specifics.resume.message = dc->emsg;
2491 GNUNET_FS_download_make_status_ (&pi, dc);
2492 dcc = dc->child_head;
2495 signal_download_resume (dcc);
2498 if (NULL != dc->pending_head)
2499 GNUNET_FS_download_start_downloading_ (dc);
2504 * Signal resuming of a search to our clients (for the
2505 * top level search and all sub-searches).
2507 * @param sc search being resumed
2510 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2514 * Iterator over search results signaling resume to the client for
2517 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2518 * @param key current key code
2519 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2520 * @return #GNUNET_YES (we should continue to iterate)
2523 signal_result_resume (void *cls,
2524 const struct GNUNET_HashCode *key,
2527 struct GNUNET_FS_SearchContext *sc = cls;
2528 struct GNUNET_FS_ProgressInfo pi;
2529 struct GNUNET_FS_SearchResult *sr = value;
2531 if (0 == sr->mandatory_missing)
2533 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2534 pi.value.search.specifics.resume_result.meta = sr->meta;
2535 pi.value.search.specifics.resume_result.uri = sr->uri;
2536 pi.value.search.specifics.resume_result.result = sr;
2537 pi.value.search.specifics.resume_result.availability_rank =
2538 2 * sr->availability_success - sr->availability_trials;
2539 pi.value.search.specifics.resume_result.availability_certainty =
2540 sr->availability_trials;
2541 pi.value.search.specifics.resume_result.applicability_rank =
2542 sr->optional_support;
2543 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2545 if (NULL != sr->download)
2547 signal_download_resume (sr->download);
2551 GNUNET_FS_search_start_probe_ (sr);
2553 if (NULL != sr->update_search)
2554 signal_search_resume (sr->update_search);
2560 * Free memory allocated by the search context and its children
2562 * @param sc search context to free
2565 free_search_context (struct GNUNET_FS_SearchContext *sc);
2569 * Iterator over search results freeing each.
2571 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2572 * @param key current key code
2573 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2574 * @return #GNUNET_YES (we should continue to iterate)
2577 free_result (void *cls,
2578 const struct GNUNET_HashCode *key,
2581 struct GNUNET_FS_SearchResult *sr = value;
2583 if (NULL != sr->update_search)
2585 free_search_context (sr->update_search);
2586 GNUNET_assert (NULL == sr->update_search);
2588 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2589 GNUNET_FS_uri_destroy (sr->uri);
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)
2603 if (NULL != sc->serialization)
2605 GNUNET_FS_remove_sync_file_ (sc->h,
2606 (sc->psearch_result ==
2607 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2608 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2610 GNUNET_FS_remove_sync_dir_ (sc->h,
2611 (sc->psearch_result ==
2612 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2613 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2616 GNUNET_free_non_null (sc->serialization);
2617 GNUNET_free_non_null (sc->emsg);
2618 if (NULL != sc->uri)
2619 GNUNET_FS_uri_destroy (sc->uri);
2620 if (NULL != sc->master_result_map)
2622 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2624 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2631 * Function called with a filename of serialized sub-download
2634 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2635 * @param filename complete filename (absolute path)
2636 * @return #GNUNET_OK (continue to iterate)
2639 deserialize_subdownload (void *cls,
2640 const char *filename)
2642 struct GNUNET_FS_DownloadContext *parent = cls;
2645 struct GNUNET_BIO_ReadHandle *rh;
2647 ser = get_serialization_short_name (filename);
2648 rh = GNUNET_BIO_read_open (filename);
2651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2652 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2658 deserialize_download (parent->h, rh, parent, NULL, ser);
2659 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2662 _("Failed to resume sub-download `%s': %s\n"),
2673 * Free this download context and all of its descendants.
2674 * (only works during deserialization since not all possible
2675 * state it taken care of).
2677 * @param dc context to free
2680 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2682 struct GNUNET_FS_DownloadContext *dcc;
2684 if (NULL != dc->meta)
2685 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2686 if (NULL != dc->uri)
2687 GNUNET_FS_uri_destroy (dc->uri);
2688 GNUNET_free_non_null (dc->temp_filename);
2689 GNUNET_free_non_null (dc->emsg);
2690 GNUNET_free_non_null (dc->filename);
2691 GNUNET_free_non_null (dc->serialization);
2692 while (NULL != (dcc = dc->child_head))
2694 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2697 free_download_context (dcc);
2699 GNUNET_FS_free_download_request_ (dc->top_request);
2700 if (NULL != dc->active)
2701 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2707 * Deserialize a download.
2709 * @param h overall context
2710 * @param rh file to deserialize from
2711 * @param parent parent download
2712 * @param search associated search
2713 * @param serialization name under which the search was serialized
2716 deserialize_download (struct GNUNET_FS_Handle *h,
2717 struct GNUNET_BIO_ReadHandle *rh,
2718 struct GNUNET_FS_DownloadContext *parent,
2719 struct GNUNET_FS_SearchResult *search,
2720 const char *serialization)
2722 struct GNUNET_FS_DownloadContext *dc;
2731 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2732 dc->parent = parent;
2734 dc->serialization = GNUNET_strdup (serialization);
2736 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2737 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2738 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2739 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2740 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2742 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2744 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2746 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2748 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2749 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2750 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2751 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2752 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2753 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2754 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2755 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2760 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2762 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2763 dc->has_finished = (int) status;
2765 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2766 if (GNUNET_FS_uri_test_loc (dc->uri))
2767 GNUNET_assert (GNUNET_OK ==
2768 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2769 if (NULL == dc->emsg)
2771 dc->top_request = read_download_request (rh);
2772 if (NULL == dc->top_request)
2778 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2781 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2782 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2787 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2791 dc->search = search;
2792 search->download = dc;
2794 if ((NULL == parent) && (NULL == search))
2797 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2798 signal_download_resume (dc);
2801 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2804 GNUNET_free_non_null (uris);
2805 GNUNET_free_non_null (emsg);
2806 free_download_context (dc);
2811 * Signal resuming of a search to our clients (for the
2812 * top level search and all sub-searches).
2814 * @param sc search being resumed
2817 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2819 struct GNUNET_FS_ProgressInfo pi;
2821 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2822 pi.value.search.specifics.resume.message = sc->emsg;
2823 pi.value.search.specifics.resume.is_paused =
2824 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2825 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2826 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2827 &signal_result_resume, sc);
2833 * Deserialize a search.
2835 * @param h overall context
2836 * @param rh file to deserialize from
2837 * @param psearch_result parent search result
2838 * @param serialization name under which the search was serialized
2840 static struct GNUNET_FS_SearchContext *
2841 deserialize_search (struct GNUNET_FS_Handle *h,
2842 struct GNUNET_BIO_ReadHandle *rh,
2843 struct GNUNET_FS_SearchResult *psearch_result,
2844 const char *serialization)
2846 struct GNUNET_FS_SearchContext *sc;
2853 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2860 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2861 if (NULL != psearch_result)
2863 sc->psearch_result = psearch_result;
2864 psearch_result->update_search = sc;
2867 sc->serialization = GNUNET_strdup (serialization);
2868 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024))
2869 || (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2870 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2871 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2872 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2874 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2875 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2877 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2878 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2883 sc->options = (enum GNUNET_FS_SearchOptions) options;
2884 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2885 dn = get_serialization_file_name_in_dir (h,
2886 (sc->psearch_result ==
2888 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2889 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2890 sc->serialization, "");
2893 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2894 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2897 if (('\0' == in_pause) &&
2898 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2901 _("Could not resume running search, will resume as paused search\n"));
2903 signal_search_resume (sc);
2907 GNUNET_free_non_null (emsg);
2908 free_search_context (sc);
2909 GNUNET_free_non_null (uris);
2915 * Function called with a filename of serialized search operation
2918 * @param cls the `struct GNUNET_FS_Handle *`
2919 * @param filename complete filename (absolute path)
2920 * @return #GNUNET_OK (continue to iterate)
2923 deserialize_search_file (void *cls,
2924 const char *filename)
2926 struct GNUNET_FS_Handle *h = cls;
2929 struct GNUNET_BIO_ReadHandle *rh;
2930 struct GNUNET_FS_SearchContext *sc;
2933 if (0 != STAT (filename, &buf))
2935 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2938 if (S_ISDIR (buf.st_mode))
2939 return GNUNET_OK; /* skip directories */
2940 ser = get_serialization_short_name (filename);
2941 rh = GNUNET_BIO_read_open (filename);
2946 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2951 sc = deserialize_search (h, rh, NULL, ser);
2953 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2955 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2958 _("Failure while resuming search operation `%s': %s\n"),
2967 * Function called with a filename of serialized download operation
2970 * @param cls the `struct GNUNET_FS_Handle *`
2971 * @param filename complete filename (absolute path)
2972 * @return #GNUNET_OK (continue to iterate)
2975 deserialize_download_file (void *cls, const char *filename)
2977 struct GNUNET_FS_Handle *h = cls;
2980 struct GNUNET_BIO_ReadHandle *rh;
2982 ser = get_serialization_short_name (filename);
2983 rh = GNUNET_BIO_read_open (filename);
2986 if (0 != UNLINK (filename))
2987 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
2991 deserialize_download (h, rh, NULL, NULL, ser);
2993 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2996 _("Failure while resuming download operation `%s': %s\n"),
3005 * Deserialize informatin about pending operations.
3007 * @param master_path which master directory should be scanned
3008 * @param proc function to call for each entry (will get 'h' for 'cls')
3009 * @param h the 'struct GNUNET_FS_Handle*'
3012 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
3013 struct GNUNET_FS_Handle *h)
3017 dn = get_serialization_file_name (h, master_path, "");
3020 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3021 GNUNET_DISK_directory_scan (dn, proc, h);
3027 * Setup a connection to the file-sharing service.
3029 * @param cfg configuration to use
3030 * @param client_name unique identifier for this client
3031 * @param upcb function to call to notify about FS actions
3032 * @param upcb_cls closure for @a upcb
3033 * @param flags specific attributes for fs-operations
3034 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3035 * @return NULL on error
3037 struct GNUNET_FS_Handle *
3038 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3039 const char *client_name,
3040 GNUNET_FS_ProgressCallback upcb,
3042 enum GNUNET_FS_Flags flags, ...)
3044 struct GNUNET_FS_Handle *ret;
3045 enum GNUNET_FS_OPTIONS opt;
3048 ret = GNUNET_new (struct GNUNET_FS_Handle);
3050 ret->client_name = GNUNET_strdup (client_name);
3052 ret->upcb_cls = upcb_cls;
3054 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3055 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3056 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3057 va_start (ap, flags);
3058 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3062 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3063 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3066 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3067 ret->max_parallel_requests = va_arg (ap, unsigned int);
3072 GNUNET_free (ret->client_name);
3079 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3081 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3082 &deserialize_publish_file, ret);
3083 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3084 &deserialize_search_file, ret);
3085 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3086 &deserialize_download_file, ret);
3087 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3088 &deserialize_unindex_file, ret);
3095 * Close our connection with the file-sharing service.
3096 * The callback given to GNUNET_FS_start will no longer be
3097 * called after this function returns.
3099 * @param h handle that was returned from #GNUNET_FS_start()
3102 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3104 while (h->top_head != NULL)
3105 h->top_head->ssf (h->top_head->ssf_cls);
3106 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3107 GNUNET_SCHEDULER_cancel (h->queue_job);
3108 GNUNET_free (h->client_name);
3113 /* end of fs_api.c */