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 #GNUNET_FS_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;
880 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
885 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
891 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
892 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
894 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
895 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
896 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
898 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
899 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
900 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
902 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
903 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
904 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
905 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
907 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
909 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
910 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
911 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
912 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
919 case 0: /* file-insert */
920 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
925 ret->is_directory = GNUNET_NO;
926 ret->data.file.do_index = GNUNET_NO;
927 ret->data.file.have_hash = GNUNET_NO;
928 ret->data.file.index_start_confirmed = GNUNET_NO;
929 if (GNUNET_NO == ret->is_published)
931 if (NULL == ret->filename)
933 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
934 ret->data.file.reader_cls =
935 GNUNET_malloc_large (ret->data.file.file_size);
936 if (ret->data.file.reader_cls == NULL)
939 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
940 ret->data.file.file_size))
948 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
949 ret->data.file.reader_cls =
950 GNUNET_FS_make_file_reader_context_ (ret->filename);
954 case 1: /* file-index, no hash */
955 if (NULL == ret->filename)
960 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
965 ret->is_directory = GNUNET_NO;
966 ret->data.file.do_index = GNUNET_YES;
967 ret->data.file.have_hash = GNUNET_NO;
968 ret->data.file.index_start_confirmed = GNUNET_NO;
969 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
970 ret->data.file.reader_cls =
971 GNUNET_FS_make_file_reader_context_ (ret->filename);
973 case 2: /* file-index-with-hash */
974 if (NULL == ret->filename)
979 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
981 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
982 sizeof (struct GNUNET_HashCode))))
987 ret->is_directory = GNUNET_NO;
988 ret->data.file.do_index = GNUNET_YES;
989 ret->data.file.have_hash = GNUNET_YES;
990 ret->data.file.index_start_confirmed = GNUNET_NO;
991 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
992 ret->data.file.reader_cls =
993 GNUNET_FS_make_file_reader_context_ (ret->filename);
995 case 3: /* file-index-with-hash-confirmed */
996 if (NULL == ret->filename)
1001 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1003 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1004 sizeof (struct GNUNET_HashCode))))
1009 ret->is_directory = GNUNET_NO;
1010 ret->data.file.do_index = GNUNET_YES;
1011 ret->data.file.have_hash = GNUNET_YES;
1012 ret->data.file.index_start_confirmed = GNUNET_YES;
1013 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1014 ret->data.file.reader_cls =
1015 GNUNET_FS_make_file_reader_context_ (ret->filename);
1017 case 4: /* directory */
1018 ret->is_directory = GNUNET_YES;
1019 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1020 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1021 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1022 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1024 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1026 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1031 ret->data.dir.dir_size = (uint32_t) dsize;
1032 if (NULL != filename)
1034 ret->data.dir.entries = deserialize_file_information (h, filename);
1035 GNUNET_free (filename);
1037 nxt = ret->data.dir.entries;
1049 ret->serialization = GNUNET_strdup (fn);
1051 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1056 if (NULL != filename)
1058 ret->next = deserialize_file_information (h, filename);
1059 GNUNET_free (filename);
1062 GNUNET_free_non_null (ksks);
1063 GNUNET_free_non_null (skss);
1064 GNUNET_free_non_null (chks);
1067 GNUNET_free_non_null (ksks);
1068 GNUNET_free_non_null (chks);
1069 GNUNET_free_non_null (skss);
1070 GNUNET_free_non_null (filename);
1071 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1077 * Using the given serialization filename, try to deserialize
1078 * the file-information tree associated with it.
1080 * @param h master context
1081 * @param filename name of the file (without directory) with
1083 * @return NULL on error
1085 static struct GNUNET_FS_FileInformation *
1086 deserialize_file_information (struct GNUNET_FS_Handle *h,
1087 const char *filename)
1089 struct GNUNET_FS_FileInformation *ret;
1090 struct GNUNET_BIO_ReadHandle *rh;
1094 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1097 ret = deserialize_fi_node (h, filename, rh);
1098 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1101 _("Failed to resume publishing information `%s': %s\n"),
1107 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1110 if (0 != UNLINK (fn))
1111 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1120 * Given a serialization name (full absolute path), return the
1121 * basename of the file (without the path), which must only
1122 * consist of the 6 random characters.
1124 * @param fullname name to extract the basename from
1125 * @return copy of the basename, NULL on error
1128 get_serialization_short_name (const char *fullname)
1135 /* FIXME: we could do this faster since we know
1136 * the length of 'end'... */
1137 while ('\0' != *nxt)
1139 if (DIR_SEPARATOR == *nxt)
1143 if ((NULL == end) || (0 == strlen (end)))
1148 GNUNET_break (6 == strlen (end));
1149 return GNUNET_strdup (end);
1154 * Create a new random name for serialization. Also checks if persistence
1155 * is enabled and returns NULL if not.
1157 * @param h master context
1158 * @param ext component of the path
1159 * @return NULL on errror
1162 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1169 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1170 return NULL; /* persistence not requested */
1171 dn = get_serialization_file_name (h, ext, "");
1174 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1179 fn = GNUNET_DISK_mktemp (dn);
1182 return NULL; /* epic fail */
1183 ret = get_serialization_short_name (fn);
1190 * Create a new random name for serialization. Also checks if persistence
1191 * is enabled and returns NULL if not.
1193 * @param h master context
1194 * @param ext component of the path
1195 * @param uni name of parent
1196 * @return NULL on errror
1199 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1207 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1208 return NULL; /* persistence not requested */
1209 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1212 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1217 fn = GNUNET_DISK_mktemp (dn);
1220 return NULL; /* epic fail */
1221 ret = get_serialization_short_name (fn);
1228 * Copy all of the data from the reader to the write handle.
1230 * @param wh write handle
1231 * @param fi file with reader
1232 * @return #GNUNET_OK on success
1235 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1236 struct GNUNET_FS_FileInformation *fi)
1238 char buf[32 * 1024];
1246 while (off < fi->data.file.file_size)
1248 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1250 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1254 return GNUNET_SYSERR;
1256 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1257 return GNUNET_SYSERR;
1265 * Create a temporary file on disk to store the current
1266 * state of @a fi in.
1268 * @param fi file information to sync with disk
1271 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1274 struct GNUNET_BIO_WriteHandle *wh;
1280 if (NULL == fi->serialization)
1282 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1283 if (NULL == fi->serialization)
1285 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1289 GNUNET_free (fi->serialization);
1290 fi->serialization = NULL;
1293 if (GNUNET_YES == fi->is_directory)
1295 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1297 else if (GNUNET_YES == fi->data.file.have_hash)
1299 else if (GNUNET_YES == fi->data.file.do_index)
1303 if (NULL != fi->keywords)
1304 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1307 if (NULL != fi->chk_uri)
1308 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1311 if (NULL != fi->sks_uri)
1312 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1315 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1316 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1317 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1318 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1319 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1320 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1321 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1322 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1324 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1325 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1326 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1327 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1332 GNUNET_free_non_null (chks);
1334 GNUNET_free_non_null (ksks);
1336 GNUNET_free_non_null (skss);
1341 case 0: /* file-insert */
1342 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1347 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1348 if (GNUNET_OK != copy_from_reader (wh, fi))
1354 case 1: /* file-index, no hash */
1355 if (NULL == fi->filename)
1360 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1366 case 2: /* file-index-with-hash */
1367 case 3: /* file-index-with-hash-confirmed */
1368 if (NULL == fi->filename)
1373 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1375 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1376 sizeof (struct GNUNET_HashCode))))
1382 case 4: /* directory */
1383 if ( (NULL != fi->data.dir.entries) &&
1384 (NULL == fi->data.dir.entries->serialization) )
1385 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1386 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1387 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1388 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1390 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1391 (uint32_t) fi->data.dir.dir_size)) ||
1393 GNUNET_BIO_write_string (wh,
1394 (fi->data.dir.entries ==
1395 NULL) ? NULL : fi->data.dir.
1396 entries->serialization)))
1406 if ( (NULL != fi->next) &&
1407 (NULL == fi->next->serialization) )
1408 GNUNET_FS_file_information_sync_ (fi->next);
1410 GNUNET_BIO_write_string (wh,
1412 NULL) ? fi->next->serialization : NULL))
1417 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1426 (void) GNUNET_BIO_write_close (wh);
1427 GNUNET_free_non_null (chks);
1428 GNUNET_free_non_null (ksks);
1429 GNUNET_free_non_null (skss);
1430 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1434 if (0 != UNLINK (fn))
1435 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1438 GNUNET_free (fi->serialization);
1439 fi->serialization = NULL;
1445 * Find the entry in the file information struct where the
1446 * serialization filename matches the given name.
1448 * @param pos file information to search
1449 * @param srch filename to search for
1450 * @return NULL if srch was not found in this subtree
1452 static struct GNUNET_FS_FileInformation *
1453 find_file_position (struct GNUNET_FS_FileInformation *pos,
1456 struct GNUNET_FS_FileInformation *r;
1460 if (0 == strcmp (srch, pos->serialization))
1462 if ( (GNUNET_YES == pos->is_directory) &&
1463 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1472 * Signal the FS's progress function that we are resuming
1475 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1476 * @param fi the entry in the publish-structure
1477 * @param length length of the file or directory
1478 * @param meta metadata for the file or directory (can be modified)
1479 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1480 * @param bo block options (can be modified)
1481 * @param do_index should we index?
1482 * @param client_info pointer to client context set upon creation (can be modified)
1483 * @return #GNUNET_OK to continue (always)
1486 fip_signal_resume (void *cls,
1487 struct GNUNET_FS_FileInformation *fi,
1489 struct GNUNET_CONTAINER_MetaData *meta,
1490 struct GNUNET_FS_Uri **uri,
1491 struct GNUNET_FS_BlockOptions *bo,
1495 struct GNUNET_FS_PublishContext *pc = cls;
1496 struct GNUNET_FS_ProgressInfo pi;
1498 if (GNUNET_YES == pc->skip_next_fi_callback)
1500 pc->skip_next_fi_callback = GNUNET_NO;
1503 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1504 pi.value.publish.specifics.resume.message = fi->emsg;
1505 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1506 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1507 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1509 /* process entries in directory */
1510 pc->skip_next_fi_callback = GNUNET_YES;
1511 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1518 * Function called with a filename of serialized publishing operation
1521 * @param cls the `struct GNUNET_FS_Handle *`
1522 * @param filename complete filename (absolute path)
1523 * @return #GNUNET_OK (continue to iterate)
1526 deserialize_publish_file (void *cls,
1527 const char *filename)
1529 struct GNUNET_FS_Handle *h = cls;
1530 struct GNUNET_BIO_ReadHandle *rh;
1531 struct GNUNET_FS_PublishContext *pc;
1536 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1540 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1542 pc->serialization = get_serialization_short_name (filename);
1545 rh = GNUNET_BIO_read_open (filename);
1551 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1553 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1554 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1555 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1556 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1558 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1559 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1560 || ( (GNUNET_YES == have_ns) &&
1561 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1566 pc->options = options;
1567 pc->all_done = all_done;
1568 if (NULL == fi_root)
1573 pc->fi = deserialize_file_information (h, fi_root);
1579 if (GNUNET_YES == have_ns)
1581 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1584 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1585 (GNUNET_YES != pc->all_done))
1587 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1588 if (NULL == pc->dsh)
1593 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1594 GNUNET_free (fi_pos);
1596 if (NULL == pc->fi_pos)
1598 /* failed to find position for resuming, outch! Will start from root! */
1600 if (GNUNET_YES != pc->all_done)
1601 pc->fi_pos = pc->fi;
1604 GNUNET_free (fi_root);
1606 /* generate RESUME event(s) */
1607 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1609 /* re-start publishing (if needed)... */
1610 if (GNUNET_YES != pc->all_done)
1612 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1614 GNUNET_SCHEDULER_add_with_priority
1615 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1616 &GNUNET_FS_publish_main_, pc);
1618 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1621 _("Failure while resuming publishing operation `%s': %s\n"),
1625 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1628 GNUNET_free_non_null (pc->nid);
1629 GNUNET_free_non_null (pc->nuid);
1630 GNUNET_free_non_null (fi_root);
1631 GNUNET_free_non_null (fi_pos);
1632 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1635 _("Failed to resume publishing operation `%s': %s\n"), filename,
1640 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1641 if (0 != UNLINK (filename))
1642 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1643 GNUNET_free (pc->serialization);
1650 * Synchronize this publishing struct with its mirror
1651 * on disk. Note that all internal FS-operations that change
1652 * publishing structs should already call "sync" internally,
1653 * so this function is likely not useful for clients.
1655 * @param pc the struct to sync
1658 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1660 struct GNUNET_BIO_WriteHandle *wh;
1663 if (NULL == pc->serialization)
1665 make_serialization_file_name (pc->h,
1666 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1667 if (NULL == pc->serialization)
1671 if (NULL == pc->fi->serialization)
1676 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1683 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1684 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1685 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1686 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1687 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1688 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1689 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1691 GNUNET_BIO_write_string (wh,
1692 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1693 ( (NULL != pc->ns) &&
1694 (GNUNET_OK != GNUNET_BIO_write (wh,
1696 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1701 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1710 (void) GNUNET_BIO_write_close (wh);
1711 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1713 GNUNET_free (pc->serialization);
1714 pc->serialization = NULL;
1719 * Synchronize this unindex struct with its mirror
1720 * on disk. Note that all internal FS-operations that change
1721 * publishing structs should already call "sync" internally,
1722 * so this function is likely not useful for clients.
1724 * @param uc the struct to sync
1727 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1729 struct GNUNET_BIO_WriteHandle *wh;
1732 if (NULL == uc->serialization)
1734 make_serialization_file_name (uc->h,
1735 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1736 if (NULL == uc->serialization)
1738 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1745 if (NULL != uc->ksk_uri)
1746 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1749 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1750 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1751 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1752 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1754 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1755 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1756 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1757 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1759 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1760 ((uc->state == UNINDEX_STATE_ERROR) &&
1761 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1766 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1775 (void) GNUNET_BIO_write_close (wh);
1776 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1778 GNUNET_free (uc->serialization);
1779 uc->serialization = NULL;
1784 * Serialize a download request.
1786 * @param wh handle for writing the download request to disk
1787 * @param dr the the request to write to disk
1788 * @return #GNUNET_YES on success, #GNUNET_NO on error
1791 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1792 struct DownloadRequest *dr)
1796 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1797 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1798 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1799 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1801 if ((BRS_CHK_SET == dr->state) &&
1803 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1805 for (i = 0; i < dr->num_children; i++)
1806 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1813 * Read a download request tree.
1815 * @param rh cadet to read from
1816 * @return value the download request read from disk, NULL on error
1818 static struct DownloadRequest *
1819 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1821 struct DownloadRequest *dr;
1824 dr = GNUNET_new (struct DownloadRequest);
1825 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1826 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1827 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1828 (dr->num_children > CHK_PER_INODE) ||
1829 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1830 ( (0 == dr->depth) &&
1831 (dr->num_children > 0) ) ||
1832 ((dr->depth > 0) && (0 == dr->num_children)))
1835 dr->num_children = 0;
1838 if (dr->num_children > 0)
1840 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1844 case BRS_RECONSTRUCT_DOWN:
1845 case BRS_RECONSTRUCT_META_UP:
1846 case BRS_RECONSTRUCT_UP:
1850 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1853 case BRS_DOWNLOAD_DOWN:
1854 case BRS_DOWNLOAD_UP:
1861 for (i = 0; i < dr->num_children; i++)
1863 if (NULL == (dr->children[i] = read_download_request (rh)))
1865 dr->children[i]->parent = dr;
1869 GNUNET_FS_free_download_request_ (dr);
1875 * Compute the name of the sync file (or directory) for the given download
1878 * @param dc download context to compute for
1879 * @param uni unique filename to use, use "" for the directory name
1880 * @param ext extension to use, use ".dir" for our own subdirectory
1881 * @return the expanded file name, NULL for none
1884 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1891 if (dc->parent == NULL)
1892 return get_serialization_file_name (dc->h,
1893 (dc->search != NULL) ?
1894 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1895 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1897 if (NULL == dc->parent->serialization)
1899 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1902 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1909 * Synchronize this download struct with its mirror
1910 * on disk. Note that all internal FS-operations that change
1911 * publishing structs should already call "sync" internally,
1912 * so this function is likely not useful for clients.
1914 * @param dc the struct to sync
1917 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1919 struct GNUNET_BIO_WriteHandle *wh;
1924 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1925 return; /* we don't sync probes */
1926 if (NULL == dc->serialization)
1928 dir = get_download_sync_filename (dc, "", "");
1931 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1936 fn = GNUNET_DISK_mktemp (dir);
1940 dc->serialization = get_serialization_short_name (fn);
1944 fn = get_download_sync_filename (dc, dc->serialization, "");
1947 GNUNET_free (dc->serialization);
1948 dc->serialization = NULL;
1953 wh = GNUNET_BIO_write_open (fn);
1956 GNUNET_free (dc->serialization);
1957 dc->serialization = NULL;
1961 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1962 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1963 uris = GNUNET_FS_uri_to_string (dc->uri);
1964 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1965 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1966 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1967 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1968 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1969 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1970 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1971 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1972 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1973 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1974 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1975 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1976 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1981 if (NULL == dc->emsg)
1983 GNUNET_assert (dc->top_request != NULL);
1984 if (GNUNET_YES != write_download_request (wh, dc->top_request))
1990 GNUNET_free_non_null (uris);
1992 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2002 (void) GNUNET_BIO_write_close (wh);
2003 GNUNET_free_non_null (uris);
2004 if (0 != UNLINK (fn))
2005 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2007 GNUNET_free (dc->serialization);
2008 dc->serialization = NULL;
2013 * Synchronize this search result with its mirror
2014 * on disk. Note that all internal FS-operations that change
2015 * publishing structs should already call "sync" internally,
2016 * so this function is likely not useful for clients.
2018 * @param sr the struct to sync
2021 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2023 struct GNUNET_BIO_WriteHandle *wh;
2029 if (NULL == sr->serialization)
2031 make_serialization_file_name_in_dir (sr->h,
2032 (sr->sc->psearch_result ==
2034 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2035 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2036 sr->sc->serialization);
2037 if (NULL == sr->serialization)
2039 wh = get_write_handle_in_dir (sr->h,
2040 (sr->sc->psearch_result ==
2041 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2042 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2043 sr->sc->serialization, sr->serialization);
2049 uris = GNUNET_FS_uri_to_string (sr->uri);
2050 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2052 GNUNET_BIO_write_string (wh,
2054 NULL ? sr->download->serialization : NULL)) ||
2056 GNUNET_BIO_write_string (wh,
2057 sr->update_search !=
2058 NULL ? sr->update_search->serialization : NULL))
2059 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2060 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2061 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2062 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2063 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2064 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2069 if ( (NULL != sr->uri) &&
2070 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2071 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2072 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2077 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2083 GNUNET_free_non_null (uris);
2086 GNUNET_free_non_null (uris);
2088 (void) GNUNET_BIO_write_close (wh);
2089 remove_sync_file_in_dir (sr->h,
2090 (NULL == sr->sc->psearch_result)
2091 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2092 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2093 sr->sc->serialization, sr->serialization);
2094 GNUNET_free (sr->serialization);
2095 sr->serialization = NULL;
2100 * Synchronize this search struct with its mirror
2101 * on disk. Note that all internal FS-operations that change
2102 * publishing structs should already call "sync" internally,
2103 * so this function is likely not useful for clients.
2105 * @param sc the struct to sync
2108 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2110 struct GNUNET_BIO_WriteHandle *wh;
2113 const char *category;
2116 (NULL == sc->psearch_result)
2117 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2118 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2119 if (NULL == sc->serialization)
2120 sc->serialization = make_serialization_file_name (sc->h, category);
2121 if (NULL == sc->serialization)
2124 wh = get_write_handle (sc->h, category, sc->serialization);
2130 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2131 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2132 uris = GNUNET_FS_uri_to_string (sc->uri);
2133 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
2134 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2135 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2136 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2137 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2138 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2139 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2146 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2155 (void) GNUNET_BIO_write_close (wh);
2156 GNUNET_free_non_null (uris);
2157 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2158 GNUNET_free (sc->serialization);
2159 sc->serialization = NULL;
2164 * Function called with a filename of serialized unindexing operation
2167 * @param cls the `struct GNUNET_FS_Handle *`
2168 * @param filename complete filename (absolute path)
2169 * @return #GNUNET_OK (continue to iterate)
2172 deserialize_unindex_file (void *cls,
2173 const char *filename)
2175 struct GNUNET_FS_Handle *h = cls;
2176 struct GNUNET_BIO_ReadHandle *rh;
2177 struct GNUNET_FS_UnindexContext *uc;
2178 struct GNUNET_FS_ProgressInfo pi;
2183 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2185 uc->serialization = get_serialization_short_name (filename);
2186 rh = GNUNET_BIO_read_open (filename);
2194 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2195 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2196 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2197 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2198 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2199 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2200 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2202 GNUNET_free_non_null (uris);
2208 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2210 if (NULL == uc->ksk_uri)
2213 GNUNET_free_non_null (emsg);
2217 if ( (uc->ksk_offset > 0) &&
2218 ( (NULL == uc->ksk_uri) ||
2219 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2224 uc->state = (enum UnindexState) state;
2227 case UNINDEX_STATE_HASHING:
2229 case UNINDEX_STATE_FS_NOTIFY:
2231 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2232 sizeof (struct GNUNET_HashCode)))
2238 case UNINDEX_STATE_DS_REMOVE:
2239 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2240 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2242 case UNINDEX_STATE_COMPLETE:
2244 case UNINDEX_STATE_ERROR:
2246 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2256 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2257 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2258 pi.value.unindex.specifics.resume.message = uc->emsg;
2259 GNUNET_FS_unindex_make_status_ (&pi, uc,
2261 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2264 case UNINDEX_STATE_HASHING:
2266 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2268 &GNUNET_FS_unindex_process_hash_, uc);
2270 case UNINDEX_STATE_FS_NOTIFY:
2271 uc->state = UNINDEX_STATE_HASHING;
2272 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2274 case UNINDEX_STATE_DS_REMOVE:
2275 GNUNET_FS_unindex_do_remove_ (uc);
2277 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2278 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2280 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2281 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2283 case UNINDEX_STATE_COMPLETE:
2284 case UNINDEX_STATE_ERROR:
2285 /* no need to resume any operation, we were done */
2290 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2293 _("Failure while resuming unindexing operation `%s': %s\n"),
2299 GNUNET_free_non_null (uc->filename);
2300 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2303 _("Failed to resume unindexing operation `%s': %s\n"),
2308 if (NULL != uc->serialization)
2309 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2311 GNUNET_free_non_null (uc->serialization);
2318 * Deserialize a download.
2320 * @param h overall context
2321 * @param rh file to deserialize from
2322 * @param parent parent download
2323 * @param search associated search
2324 * @param serialization name under which the search was serialized
2327 deserialize_download (struct GNUNET_FS_Handle *h,
2328 struct GNUNET_BIO_ReadHandle *rh,
2329 struct GNUNET_FS_DownloadContext *parent,
2330 struct GNUNET_FS_SearchResult *search,
2331 const char *serialization);
2335 * Deserialize a search.
2337 * @param h overall context
2338 * @param rh file to deserialize from
2339 * @param psearch_result parent search result
2340 * @param serialization name under which the search was serialized
2342 static struct GNUNET_FS_SearchContext *
2343 deserialize_search (struct GNUNET_FS_Handle *h,
2344 struct GNUNET_BIO_ReadHandle *rh,
2345 struct GNUNET_FS_SearchResult *psearch_result,
2346 const char *serialization);
2350 * Function called with a filename of serialized search result
2353 * @param cls the `struct GNUNET_FS_SearchContext *`
2354 * @param filename complete filename (absolute path)
2355 * @return #GNUNET_OK (continue to iterate)
2358 deserialize_search_result (void *cls,
2359 const char *filename)
2361 struct GNUNET_FS_SearchContext *sc = cls;
2367 struct GNUNET_BIO_ReadHandle *rh;
2368 struct GNUNET_BIO_ReadHandle *drh;
2369 struct GNUNET_FS_SearchResult *sr;
2371 ser = get_serialization_short_name (filename);
2372 rh = GNUNET_BIO_read_open (filename);
2377 remove_sync_file_in_dir (sc->h,
2378 (NULL == sc->psearch_result)
2379 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2380 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2381 sc->serialization, ser);
2390 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2393 sr->serialization = ser;
2394 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2395 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2396 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2398 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2399 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2401 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2402 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2403 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2404 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2405 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2410 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2412 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2413 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2415 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2422 if (NULL != download)
2424 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2427 deserialize_download (sc->h, drh, NULL, sr, download);
2428 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2431 _("Failed to resume sub-download `%s': %s\n"),
2437 GNUNET_free (download);
2439 if (NULL != update_srch)
2442 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2445 deserialize_search (sc->h, drh, sr, update_srch);
2446 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2449 _("Failed to resume sub-search `%s': %s\n"),
2455 GNUNET_free (update_srch);
2457 GNUNET_break (GNUNET_YES ==
2458 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2460 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2461 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2464 _("Failure while resuming search operation `%s': %s\n"),
2470 GNUNET_free_non_null (download);
2471 GNUNET_free_non_null (emsg);
2472 GNUNET_free_non_null (uris);
2473 GNUNET_free_non_null (update_srch);
2474 if (NULL != sr->uri)
2475 GNUNET_FS_uri_destroy (sr->uri);
2476 if (NULL != sr->meta)
2477 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2478 GNUNET_free (sr->serialization);
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"),
2492 * Send the 'resume' signal to the callback; also actually
2493 * resume the download (put it in the queue). Does this
2494 * recursively for the top-level download and all child
2497 * @param dc download to resume
2500 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2502 struct GNUNET_FS_DownloadContext *dcc;
2503 struct GNUNET_FS_ProgressInfo pi;
2505 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2506 pi.value.download.specifics.resume.meta = dc->meta;
2507 pi.value.download.specifics.resume.message = dc->emsg;
2508 GNUNET_FS_download_make_status_ (&pi, dc);
2509 dcc = dc->child_head;
2512 signal_download_resume (dcc);
2515 if (NULL != dc->pending_head)
2516 GNUNET_FS_download_start_downloading_ (dc);
2521 * Signal resuming of a search to our clients (for the
2522 * top level search and all sub-searches).
2524 * @param sc search being resumed
2527 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2531 * Iterator over search results signaling resume to the client for
2534 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2535 * @param key current key code
2536 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2537 * @return #GNUNET_YES (we should continue to iterate)
2540 signal_result_resume (void *cls,
2541 const struct GNUNET_HashCode *key,
2544 struct GNUNET_FS_SearchContext *sc = cls;
2545 struct GNUNET_FS_ProgressInfo pi;
2546 struct GNUNET_FS_SearchResult *sr = value;
2548 if (0 == sr->mandatory_missing)
2550 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2551 pi.value.search.specifics.resume_result.meta = sr->meta;
2552 pi.value.search.specifics.resume_result.uri = sr->uri;
2553 pi.value.search.specifics.resume_result.result = sr;
2554 pi.value.search.specifics.resume_result.availability_rank =
2555 2 * sr->availability_success - sr->availability_trials;
2556 pi.value.search.specifics.resume_result.availability_certainty =
2557 sr->availability_trials;
2558 pi.value.search.specifics.resume_result.applicability_rank =
2559 sr->optional_support;
2560 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2562 if (NULL != sr->download)
2564 signal_download_resume (sr->download);
2568 GNUNET_FS_search_start_probe_ (sr);
2570 if (NULL != sr->update_search)
2571 signal_search_resume (sr->update_search);
2577 * Free memory allocated by the search context and its children
2579 * @param sc search context to free
2582 free_search_context (struct GNUNET_FS_SearchContext *sc);
2586 * Iterator over search results freeing each.
2588 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2589 * @param key current key code
2590 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2591 * @return #GNUNET_YES (we should continue to iterate)
2594 free_result (void *cls,
2595 const struct GNUNET_HashCode *key,
2598 struct GNUNET_FS_SearchResult *sr = value;
2600 if (NULL != sr->update_search)
2602 free_search_context (sr->update_search);
2603 GNUNET_assert (NULL == sr->update_search);
2605 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2606 GNUNET_FS_uri_destroy (sr->uri);
2613 * Free memory allocated by the search context and its children
2615 * @param sc search context to free
2618 free_search_context (struct GNUNET_FS_SearchContext *sc)
2620 if (NULL != sc->serialization)
2622 GNUNET_FS_remove_sync_file_ (sc->h,
2623 (sc->psearch_result ==
2624 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2625 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2627 GNUNET_FS_remove_sync_dir_ (sc->h,
2628 (sc->psearch_result ==
2629 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2630 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2633 GNUNET_free_non_null (sc->serialization);
2634 GNUNET_free_non_null (sc->emsg);
2635 if (NULL != sc->uri)
2636 GNUNET_FS_uri_destroy (sc->uri);
2637 if (NULL != sc->master_result_map)
2639 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2641 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2648 * Function called with a filename of serialized sub-download
2651 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2652 * @param filename complete filename (absolute path)
2653 * @return #GNUNET_OK (continue to iterate)
2656 deserialize_subdownload (void *cls,
2657 const char *filename)
2659 struct GNUNET_FS_DownloadContext *parent = cls;
2662 struct GNUNET_BIO_ReadHandle *rh;
2664 ser = get_serialization_short_name (filename);
2665 rh = GNUNET_BIO_read_open (filename);
2668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2669 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2675 deserialize_download (parent->h, rh, parent, NULL, ser);
2676 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2679 _("Failed to resume sub-download `%s': %s\n"),
2690 * Free this download context and all of its descendants.
2691 * (only works during deserialization since not all possible
2692 * state it taken care of).
2694 * @param dc context to free
2697 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2699 struct GNUNET_FS_DownloadContext *dcc;
2701 if (NULL != dc->meta)
2702 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2703 if (NULL != dc->uri)
2704 GNUNET_FS_uri_destroy (dc->uri);
2705 GNUNET_free_non_null (dc->temp_filename);
2706 GNUNET_free_non_null (dc->emsg);
2707 GNUNET_free_non_null (dc->filename);
2708 GNUNET_free_non_null (dc->serialization);
2709 while (NULL != (dcc = dc->child_head))
2711 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2714 free_download_context (dcc);
2716 GNUNET_FS_free_download_request_ (dc->top_request);
2717 if (NULL != dc->active)
2718 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2724 * Deserialize a download.
2726 * @param h overall context
2727 * @param rh file to deserialize from
2728 * @param parent parent download
2729 * @param search associated search
2730 * @param serialization name under which the search was serialized
2733 deserialize_download (struct GNUNET_FS_Handle *h,
2734 struct GNUNET_BIO_ReadHandle *rh,
2735 struct GNUNET_FS_DownloadContext *parent,
2736 struct GNUNET_FS_SearchResult *search,
2737 const char *serialization)
2739 struct GNUNET_FS_DownloadContext *dc;
2748 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2749 dc->parent = parent;
2751 dc->serialization = GNUNET_strdup (serialization);
2753 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2754 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2755 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2756 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2757 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2759 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2761 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2763 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2765 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2766 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2767 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2768 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2769 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2770 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2771 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2772 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2777 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2779 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2780 dc->has_finished = (int) status;
2782 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2783 if (GNUNET_FS_uri_test_loc (dc->uri))
2784 GNUNET_assert (GNUNET_OK ==
2785 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2786 if (NULL == dc->emsg)
2788 dc->top_request = read_download_request (rh);
2789 if (NULL == dc->top_request)
2795 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2798 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2799 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2804 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2808 dc->search = search;
2809 search->download = dc;
2811 if ((NULL == parent) && (NULL == search))
2814 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2815 signal_download_resume (dc);
2818 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2821 GNUNET_free_non_null (uris);
2822 GNUNET_free_non_null (emsg);
2823 free_download_context (dc);
2828 * Signal resuming of a search to our clients (for the
2829 * top level search and all sub-searches).
2831 * @param sc search being resumed
2834 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2836 struct GNUNET_FS_ProgressInfo pi;
2838 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2839 pi.value.search.specifics.resume.message = sc->emsg;
2840 pi.value.search.specifics.resume.is_paused =
2841 (NULL == sc->client) ? GNUNET_YES : GNUNET_NO;
2842 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2843 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2844 &signal_result_resume, sc);
2850 * Deserialize a search.
2852 * @param h overall context
2853 * @param rh file to deserialize from
2854 * @param psearch_result parent search result
2855 * @param serialization name under which the search was serialized
2857 static struct GNUNET_FS_SearchContext *
2858 deserialize_search (struct GNUNET_FS_Handle *h,
2859 struct GNUNET_BIO_ReadHandle *rh,
2860 struct GNUNET_FS_SearchResult *psearch_result,
2861 const char *serialization)
2863 struct GNUNET_FS_SearchContext *sc;
2870 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2877 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2878 if (NULL != psearch_result)
2880 sc->psearch_result = psearch_result;
2881 psearch_result->update_search = sc;
2884 sc->serialization = GNUNET_strdup (serialization);
2886 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2887 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2888 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2889 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2890 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2892 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2893 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2895 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2896 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2901 sc->options = (enum GNUNET_FS_SearchOptions) options;
2902 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2903 dn = get_serialization_file_name_in_dir (h,
2904 (NULL == sc->psearch_result) ?
2905 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2906 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2907 sc->serialization, "");
2910 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2911 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2914 if (('\0' == in_pause) &&
2915 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2917 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2918 _("Could not resume running search, will resume as paused search\n"));
2920 signal_search_resume (sc);
2924 GNUNET_free_non_null (emsg);
2925 free_search_context (sc);
2926 GNUNET_free_non_null (uris);
2932 * Function called with a filename of serialized search operation
2935 * @param cls the `struct GNUNET_FS_Handle *`
2936 * @param filename complete filename (absolute path)
2937 * @return #GNUNET_OK (continue to iterate)
2940 deserialize_search_file (void *cls,
2941 const char *filename)
2943 struct GNUNET_FS_Handle *h = cls;
2946 struct GNUNET_BIO_ReadHandle *rh;
2947 struct GNUNET_FS_SearchContext *sc;
2950 if (0 != STAT (filename, &buf))
2952 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2955 if (S_ISDIR (buf.st_mode))
2956 return GNUNET_OK; /* skip directories */
2957 ser = get_serialization_short_name (filename);
2958 rh = GNUNET_BIO_read_open (filename);
2963 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2968 sc = deserialize_search (h, rh, NULL, ser);
2970 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2972 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2975 _("Failure while resuming search operation `%s': %s\n"),
2984 * Function called with a filename of serialized download operation
2987 * @param cls the `struct GNUNET_FS_Handle *`
2988 * @param filename complete filename (absolute path)
2989 * @return #GNUNET_OK (continue to iterate)
2992 deserialize_download_file (void *cls, const char *filename)
2994 struct GNUNET_FS_Handle *h = cls;
2997 struct GNUNET_BIO_ReadHandle *rh;
2999 ser = get_serialization_short_name (filename);
3000 rh = GNUNET_BIO_read_open (filename);
3003 if (0 != UNLINK (filename))
3004 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3008 deserialize_download (h, rh, NULL, NULL, ser);
3010 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3012 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3013 _("Failure while resuming download operation `%s': %s\n"),
3022 * Deserialize informatin about pending operations.
3024 * @param master_path which master directory should be scanned
3025 * @param proc function to call for each entry (will get 'h' for 'cls')
3026 * @param h the 'struct GNUNET_FS_Handle*'
3029 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
3030 struct GNUNET_FS_Handle *h)
3034 dn = get_serialization_file_name (h, master_path, "");
3037 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3038 GNUNET_DISK_directory_scan (dn, proc, h);
3044 * Setup a connection to the file-sharing service.
3046 * @param cfg configuration to use
3047 * @param client_name unique identifier for this client
3048 * @param upcb function to call to notify about FS actions
3049 * @param upcb_cls closure for @a upcb
3050 * @param flags specific attributes for fs-operations
3051 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3052 * @return NULL on error
3054 struct GNUNET_FS_Handle *
3055 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3056 const char *client_name,
3057 GNUNET_FS_ProgressCallback upcb,
3059 enum GNUNET_FS_Flags flags, ...)
3061 struct GNUNET_FS_Handle *ret;
3062 enum GNUNET_FS_OPTIONS opt;
3065 ret = GNUNET_new (struct GNUNET_FS_Handle);
3067 ret->client_name = GNUNET_strdup (client_name);
3069 ret->upcb_cls = upcb_cls;
3071 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3072 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3073 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3074 va_start (ap, flags);
3075 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3079 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3080 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3083 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3084 ret->max_parallel_requests = va_arg (ap, unsigned int);
3089 GNUNET_free (ret->client_name);
3096 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3098 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3099 &deserialize_publish_file, ret);
3100 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3101 &deserialize_search_file, ret);
3102 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3103 &deserialize_download_file, ret);
3104 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3105 &deserialize_unindex_file, ret);
3112 * Close our connection with the file-sharing service.
3113 * The callback given to GNUNET_FS_start will no longer be
3114 * called after this function returns.
3116 * @param h handle that was returned from #GNUNET_FS_start()
3119 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3121 while (h->top_head != NULL)
3122 h->top_head->ssf (h->top_head->ssf_cls);
3123 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
3124 GNUNET_SCHEDULER_cancel (h->queue_job);
3125 GNUNET_free (h->client_name);
3130 /* end of fs_api.c */