2 This file is part of GNUnet.
3 Copyright (C) 2001--2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief main FS functions (master initialization, serialization, deserialization, shared code)
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fs_service.h"
32 * How many block requests can we have outstanding in parallel at a time by default?
34 #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
37 * How many downloads can we have outstanding in parallel at a time by default?
39 #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
42 * Start the given job (send signal, remove from pending queue, update
43 * counters and state).
45 * @param qe job to start
48 start_job (struct GNUNET_FS_QueueEntry *qe)
50 qe->active = GNUNET_YES;
53 qe->h->active_blocks += qe->blocks;
54 qe->h->active_downloads++;
55 qe->start_time = GNUNET_TIME_absolute_get ();
56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57 "Starting job %p (%u active)\n",
59 qe->h->active_downloads);
60 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
63 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
71 * Stop the given job (send signal, remove from active queue, update
72 * counters and state).
74 * @param qe job to stop
77 stop_job (struct GNUNET_FS_QueueEntry *qe)
79 qe->active = GNUNET_NO;
81 GNUNET_assert (0 < qe->h->active_downloads);
82 qe->h->active_downloads--;
83 qe->h->active_blocks -= qe->blocks;
85 GNUNET_TIME_relative_add (qe->run_time,
86 GNUNET_TIME_absolute_get_duration
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 "Stopping job %p (%u active)\n",
91 qe->h->active_downloads);
92 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
95 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
103 * Process the jobs in the job queue, possibly starting some
104 * and stopping others.
106 * @param cls the `struct GNUNET_FS_Handle *`
109 process_job_queue (void *cls)
111 struct GNUNET_FS_Handle *h = cls;
112 struct GNUNET_FS_QueueEntry *qe;
113 struct GNUNET_FS_QueueEntry *next;
114 struct GNUNET_TIME_Relative run_time;
115 struct GNUNET_TIME_Relative restart_at;
116 struct GNUNET_TIME_Relative rst;
117 struct GNUNET_TIME_Absolute end_time;
118 unsigned int num_downloads_waiting;
119 unsigned int num_downloads_active;
120 unsigned int num_downloads_expired;
121 unsigned int num_probes_active;
122 unsigned int num_probes_waiting;
123 unsigned int num_probes_expired;
124 int num_probes_change;
125 int num_downloads_change;
129 /* restart_at will be set to the time when it makes sense to
130 re-evaluate the job queue (unless, of course, jobs complete
131 or are added, then we'll be triggered immediately */
132 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
133 /* first, calculate some basic statistics on pending jobs */
134 num_probes_waiting = 0;
135 num_downloads_waiting = 0;
136 for (qe = h->pending_head; NULL != qe; qe = qe->next)
138 switch (qe->priority)
140 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
141 num_probes_waiting++;
143 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
144 num_downloads_waiting++;
151 /* now, calculate some basic statistics on running jobs */
152 num_probes_active = 0;
153 num_probes_expired = 0;
154 num_downloads_active = 0;
155 num_downloads_expired = 0;
156 next = h->running_head;
157 while (NULL != (qe = next))
160 switch (qe->priority)
162 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
163 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
164 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
165 rst = GNUNET_TIME_absolute_get_remaining (end_time);
166 if (0 == rst.rel_value_us)
168 num_probes_expired++;
174 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
177 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
179 GNUNET_TIME_relative_saturating_multiply (h->avg_block_latency,
180 qe->blocks * qe->start_times);
181 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
182 rst = GNUNET_TIME_absolute_get_remaining (end_time);
183 if (0 == rst.rel_value_us)
185 num_downloads_expired++;
190 num_downloads_active++;
191 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
199 GNUNET_break (h->active_downloads ==
200 num_downloads_active + num_probes_active);
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 GNUNET_break (h->active_downloads + num_probes_active <=
210 h->max_parallel_downloads);
211 /* calculate start/stop decisions */
212 if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
214 /* stop as many probes as there are downloads and probes */
215 num_probes_change = - GNUNET_MIN (num_probes_active,
216 num_downloads_waiting);
217 /* start as many downloads as there are free slots, including those
219 num_downloads_change = h->max_parallel_downloads - h->active_downloads - num_probes_change;
223 /* start all downloads (we can) */
224 num_downloads_change = num_downloads_waiting;
225 /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
226 if (1 + h->max_parallel_downloads / 4 >= (h->active_downloads + num_downloads_change))
227 num_probes_change = GNUNET_MIN (num_probes_waiting,
228 (1 + h->max_parallel_downloads / 4) - (h->active_downloads + num_downloads_change));
230 num_probes_change = 0;
232 GNUNET_break (num_downloads_change <= num_downloads_waiting);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Changing %d probes and %d/%u/%u downloads\n",
236 num_downloads_change,
237 (unsigned int) h->active_downloads,
238 (unsigned int) h->max_parallel_downloads);
239 /* actually stop probes */
240 next = h->running_head;
241 while (NULL != (qe = next))
244 if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
246 if (num_probes_change < 0)
250 if (0 == num_probes_change)
254 GNUNET_break (0 <= num_probes_change);
256 /* start some more tasks if we now have empty slots */
257 block_limit_hit = GNUNET_NO;
258 next = h->pending_head;
259 while ( (NULL != (qe = next)) &&
260 ( (num_probes_change > 0) ||
261 (num_downloads_change > 0) ) )
264 switch (qe->priority)
266 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
267 if (num_probes_change > 0)
271 run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
272 restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
275 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
276 if ( (num_downloads_change > 0) &&
277 ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
278 ( (qe->blocks > h->max_parallel_requests) &&
279 (0 == h->active_downloads) ) ) )
282 num_downloads_change--;
284 else if (num_downloads_change > 0)
285 block_limit_hit = GNUNET_YES;
292 GNUNET_break ( (0 == num_downloads_change) ||
293 (GNUNET_YES == block_limit_hit) );
294 GNUNET_break (0 == num_probes_change);
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
299 h->max_parallel_requests,
301 num_downloads_change,
302 GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
304 /* make sure we run again, callbacks might have
305 already re-scheduled the job, so cancel such
306 an operation (if it exists) */
307 if (NULL != h->queue_job)
308 GNUNET_SCHEDULER_cancel (h->queue_job);
310 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
315 * Add a job to the queue.
317 * @param h handle to the overall FS state
318 * @param start function to call to begin the job
319 * @param stop function to call to pause the job, or on dequeue (if the job was running)
320 * @param cls closure for start and stop
321 * @param blocks number of blocks this jobs uses
322 * @param priority how important is this download
323 * @return queue handle
325 struct GNUNET_FS_QueueEntry *
326 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
327 GNUNET_SCHEDULER_TaskCallback start,
328 GNUNET_SCHEDULER_TaskCallback stop,
331 enum GNUNET_FS_QueuePriority priority)
333 struct GNUNET_FS_QueueEntry *qe;
335 qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
340 qe->queue_time = GNUNET_TIME_absolute_get ();
342 qe->priority = priority;
343 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
344 h->pending_tail, qe);
345 if (NULL != h->queue_job)
346 GNUNET_SCHEDULER_cancel (h->queue_job);
347 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 * Dequeue a job from the queue.
358 * @param qe handle for the job
361 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
363 struct GNUNET_FS_Handle *h;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Dequeueing job %p\n",
369 if (GNUNET_YES == qe->active)
371 GNUNET_CONTAINER_DLL_remove (h->pending_head,
375 if (NULL != h->queue_job)
376 GNUNET_SCHEDULER_cancel (h->queue_job);
377 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue,
383 * Create a top-level activity entry.
385 * @param h global fs handle
386 * @param ssf suspend signal function to use
387 * @param ssf_cls closure for @a ssf
388 * @return fresh top-level activity handle
390 struct TopLevelActivity *
391 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
392 SuspendSignalFunction ssf,
395 struct TopLevelActivity *ret;
397 ret = GNUNET_new (struct TopLevelActivity);
399 ret->ssf_cls = ssf_cls;
400 GNUNET_CONTAINER_DLL_insert (h->top_head,
408 * Destroy a top-level activity entry.
410 * @param h global fs handle
411 * @param top top level activity entry
414 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
415 struct TopLevelActivity *top)
417 GNUNET_CONTAINER_DLL_remove (h->top_head,
425 * Closure for #GNUNET_FS_data_reader_file_().
430 * Name of the file to read.
435 * File descriptor, NULL if it has not yet been opened.
437 struct GNUNET_DISK_FileHandle *fd;
442 * Function that provides data by reading from a file.
444 * @param cls closure with the `struct FileInfo *`
445 * @param offset offset to read from; it is possible
446 * that the caller might need to go backwards
447 * a bit at times; set to `UINT64_MAX` to tell
448 * the reader that we won't be reading for a while
449 * (used to close the file descriptor but NOT fully
450 * clean up the reader's state); in this case,
451 * a value of '0' for @a max should be ignored
452 * @param max maximum number of bytes that should be
453 * copied to @a buf; readers are not allowed
454 * to provide less data unless there is an error;
455 * a value of "0" will be used at the end to allow
456 * the reader to clean up its internal state
457 * @param buf where the reader should write the data
458 * @param emsg location for the reader to store an error message
459 * @return number of bytes written, usually @a max, 0 on error
462 GNUNET_FS_data_reader_file_ (void *cls,
468 struct FileInfo *fi = cls;
471 if (UINT64_MAX == offset)
475 GNUNET_DISK_file_close (fi->fd);
483 GNUNET_DISK_file_close (fi->fd);
484 GNUNET_free (fi->filename);
491 GNUNET_DISK_file_open (fi->filename,
492 GNUNET_DISK_OPEN_READ,
493 GNUNET_DISK_PERM_NONE);
496 GNUNET_asprintf (emsg,
497 _("Could not open file `%s': %s"),
503 if ( (GNUNET_SYSERR ==
504 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
505 (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))) )
507 GNUNET_asprintf (emsg,
508 _("Could not read file `%s': %s"),
515 GNUNET_asprintf (emsg,
516 _("Short read reading from file `%s'!"),
525 * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
527 * @param filename file to read
528 * @return closure to use, NULL on error
531 GNUNET_FS_make_file_reader_context_ (const char *filename)
535 fi = GNUNET_new (struct FileInfo);
536 fi->filename = GNUNET_STRINGS_filename_expand (filename);
537 if (NULL == fi->filename)
547 * Function that provides data by copying from a buffer.
549 * @param cls closure (points to the buffer)
550 * @param offset offset to read from; it is possible
551 * that the caller might need to go backwards
552 * a bit at times; set to `UINT64_MAX` to tell
553 * the reader that we won't be reading for a while
554 * (used to close the file descriptor but NOT fully
555 * clean up the reader's state); in this case,
556 * a value of '0' for @a max should be ignored
557 * @param max maximum number of bytes that should be
558 * copied to @a buf; readers are not allowed
559 * to provide less data unless there is an error;
560 * a value of "0" will be used at the end to allow
561 * the reader to clean up its internal state
562 * @param buf where the reader should write the data
563 * @param emsg location for the reader to store an error message
564 * @return number of bytes written, usually @a max, 0 on error
567 GNUNET_FS_data_reader_copy_ (void *cls,
575 if (UINT64_MAX == offset)
579 GNUNET_free_non_null (data);
582 GNUNET_memcpy (buf, &data[offset], max);
588 * Return the full filename where we would store state information
589 * (for serialization/deserialization).
591 * @param h master context
592 * @param ext component of the path
593 * @param ent entity identifier (or emtpy string for the directory)
594 * @return NULL on error
597 get_serialization_file_name (struct GNUNET_FS_Handle *h,
604 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
605 return NULL; /* persistence not requested */
607 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
610 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
611 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
613 GNUNET_free (basename);
619 * Return the full filename where we would store state information
620 * (for serialization/deserialization) that is associated with a
623 * @param h master context
624 * @param ext component of the path
625 * @param uni name of the parent operation
626 * @param ent entity identifier (or emtpy string for the directory)
627 * @return NULL on error
630 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
638 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
639 return NULL; /* persistence not requested */
641 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
644 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
645 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
646 uni, DIR_SEPARATOR_STR, ent);
647 GNUNET_free (basename);
653 * Return a read handle for deserialization.
655 * @param h master context
656 * @param ext component of the path
657 * @param ent entity identifier (or emtpy string for the directory)
658 * @return NULL on error
660 static struct GNUNET_BIO_ReadHandle *
661 get_read_handle (struct GNUNET_FS_Handle *h,
666 struct GNUNET_BIO_ReadHandle *ret;
668 fn = get_serialization_file_name (h, ext, ent);
671 ret = GNUNET_BIO_read_open (fn);
678 * Return a write handle for serialization.
680 * @param h master context
681 * @param ext component of the path
682 * @param ent entity identifier (or emtpy string for the directory)
683 * @return NULL on error
685 static struct GNUNET_BIO_WriteHandle *
686 get_write_handle (struct GNUNET_FS_Handle *h,
691 struct GNUNET_BIO_WriteHandle *ret;
693 fn = get_serialization_file_name (h, ext, ent);
696 ret = GNUNET_BIO_write_open (fn);
697 GNUNET_break (NULL != ret);
704 * Return a write handle for serialization.
706 * @param h master context
707 * @param ext component of the path
708 * @param uni name of parent
709 * @param ent entity identifier (or emtpy string for the directory)
710 * @return NULL on error
712 static struct GNUNET_BIO_WriteHandle *
713 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
714 const char *uni, const char *ent)
717 struct GNUNET_BIO_WriteHandle *ret;
719 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
722 ret = GNUNET_BIO_write_open (fn);
729 * Remove serialization/deserialization file from disk.
731 * @param h master context
732 * @param ext component of the path
733 * @param ent entity identifier
736 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
742 if ((NULL == ent) || (0 == strlen (ent)))
747 filename = get_serialization_file_name (h, ext, ent);
748 if (NULL != filename)
750 if ( (0 != UNLINK (filename)) &&
752 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
753 GNUNET_free (filename);
759 * Remove serialization/deserialization file from disk.
761 * @param h master context
762 * @param ext component of the path
763 * @param uni parent name
764 * @param ent entity identifier
767 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
769 const char *uni, const char *ent)
773 if ((NULL == ent) || (0 == strlen (ent)))
778 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
779 if (NULL == filename)
781 if (0 != UNLINK (filename))
782 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
783 GNUNET_free (filename);
788 * Remove serialization/deserialization directory from disk.
790 * @param h master context
791 * @param ext component of the path
792 * @param uni unique name of parent
795 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
803 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
806 if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
807 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
808 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
814 * Serialize a start-time. Since we use start-times to
815 * calculate the duration of some operation, we actually
816 * do not serialize the absolute time but the (relative)
817 * duration since the start time. When we then
818 * deserialize the start time, we take the current time and
819 * subtract that duration so that we get again an absolute
820 * time stamp that will result in correct performance
823 * @param wh handle for writing
824 * @param timestamp time to serialize
825 * @return #GNUNET_OK on success
828 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
829 struct GNUNET_TIME_Absolute timestamp)
831 struct GNUNET_TIME_Relative dur;
833 dur = GNUNET_TIME_absolute_get_duration (timestamp);
834 return GNUNET_BIO_write_int64 (wh, dur.rel_value_us);
839 * Deserialize a start-time. Since we use start-times to
840 * calculate the duration of some operation, we actually
841 * do not serialize the absolute time but the (relative)
842 * duration since the start time. Thus, when we then
843 * deserialize the start time, we take the current time and
844 * subtract that duration so that we get again an absolute
845 * time stamp that will result in correct performance
848 * @param rh handle for reading
849 * @param timestamp where to write the deserialized timestamp
850 * @return #GNUNET_OK on success
853 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
854 struct GNUNET_TIME_Absolute *timestamp)
856 struct GNUNET_TIME_Relative dur;
858 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value_us))
859 return GNUNET_SYSERR;
860 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
866 * Using the given serialization filename, try to deserialize
867 * the file-information tree associated with it.
869 * @param h master context
870 * @param filename name of the file (without directory) with
872 * @return NULL on error
874 static struct GNUNET_FS_FileInformation *
875 deserialize_file_information (struct GNUNET_FS_Handle *h,
876 const char *filename);
880 * Using the given serialization filename, try to deserialize
881 * the file-information tree associated with it.
883 * @param h master context
884 * @param fn name of the file (without directory) with
886 * @param rh handle for reading
887 * @return NULL on error
889 static struct GNUNET_FS_FileInformation *
890 deserialize_fi_node (struct GNUNET_FS_Handle *h,
892 struct GNUNET_BIO_ReadHandle *rh)
894 struct GNUNET_FS_FileInformation *ret;
895 struct GNUNET_FS_FileInformation *nxt;
903 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
908 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
914 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
915 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
917 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
918 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
919 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
921 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
922 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
923 (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
925 ( (NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
926 (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri))) ) ||
927 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
928 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
930 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
932 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value_us)) ||
933 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
934 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
935 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
942 case 0: /* file-insert */
943 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
948 ret->is_directory = GNUNET_NO;
949 ret->data.file.do_index = GNUNET_NO;
950 ret->data.file.have_hash = GNUNET_NO;
951 ret->data.file.index_start_confirmed = GNUNET_NO;
952 if (GNUNET_NO == ret->is_published)
954 if (NULL == ret->filename)
956 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
957 ret->data.file.reader_cls =
958 GNUNET_malloc_large (ret->data.file.file_size);
959 if (ret->data.file.reader_cls == NULL)
962 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
963 ret->data.file.file_size))
971 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
972 ret->data.file.reader_cls =
973 GNUNET_FS_make_file_reader_context_ (ret->filename);
977 case 1: /* file-index, no hash */
978 if (NULL == ret->filename)
983 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
988 ret->is_directory = GNUNET_NO;
989 ret->data.file.do_index = GNUNET_YES;
990 ret->data.file.have_hash = GNUNET_NO;
991 ret->data.file.index_start_confirmed = GNUNET_NO;
992 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
993 ret->data.file.reader_cls =
994 GNUNET_FS_make_file_reader_context_ (ret->filename);
996 case 2: /* file-index-with-hash */
997 if (NULL == ret->filename)
1002 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1004 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1005 sizeof (struct GNUNET_HashCode))))
1010 ret->is_directory = GNUNET_NO;
1011 ret->data.file.do_index = GNUNET_YES;
1012 ret->data.file.have_hash = GNUNET_YES;
1013 ret->data.file.index_start_confirmed = GNUNET_NO;
1014 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1015 ret->data.file.reader_cls =
1016 GNUNET_FS_make_file_reader_context_ (ret->filename);
1018 case 3: /* file-index-with-hash-confirmed */
1019 if (NULL == ret->filename)
1024 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
1026 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
1027 sizeof (struct GNUNET_HashCode))))
1032 ret->is_directory = GNUNET_NO;
1033 ret->data.file.do_index = GNUNET_YES;
1034 ret->data.file.have_hash = GNUNET_YES;
1035 ret->data.file.index_start_confirmed = GNUNET_YES;
1036 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
1037 ret->data.file.reader_cls =
1038 GNUNET_FS_make_file_reader_context_ (ret->filename);
1040 case 4: /* directory */
1041 ret->is_directory = GNUNET_YES;
1042 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
1043 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_completed)) ||
1044 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.dir.contents_size)) ||
1045 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
1047 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
1049 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
1054 ret->data.dir.dir_size = (uint32_t) dsize;
1055 if (NULL != filename)
1057 ret->data.dir.entries = deserialize_file_information (h, filename);
1058 GNUNET_free (filename);
1060 nxt = ret->data.dir.entries;
1072 ret->serialization = GNUNET_strdup (fn);
1074 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
1079 if (NULL != filename)
1081 ret->next = deserialize_file_information (h, filename);
1082 GNUNET_free (filename);
1085 GNUNET_free_non_null (ksks);
1086 GNUNET_free_non_null (skss);
1087 GNUNET_free_non_null (chks);
1090 GNUNET_free_non_null (ksks);
1091 GNUNET_free_non_null (chks);
1092 GNUNET_free_non_null (skss);
1093 GNUNET_free_non_null (filename);
1094 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
1100 * Using the given serialization filename, try to deserialize
1101 * the file-information tree associated with it.
1103 * @param h master context
1104 * @param filename name of the file (without directory) with
1106 * @return NULL on error
1108 static struct GNUNET_FS_FileInformation *
1109 deserialize_file_information (struct GNUNET_FS_Handle *h,
1110 const char *filename)
1112 struct GNUNET_FS_FileInformation *ret;
1113 struct GNUNET_BIO_ReadHandle *rh;
1117 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1120 ret = deserialize_fi_node (h, filename, rh);
1121 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1124 _("Failed to resume publishing information `%s': %s\n"),
1130 fn = get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
1133 if (0 != UNLINK (fn))
1134 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1143 * Given a serialization name (full absolute path), return the
1144 * basename of the file (without the path), which must only
1145 * consist of the 6 random characters.
1147 * @param fullname name to extract the basename from
1148 * @return copy of the basename, NULL on error
1151 get_serialization_short_name (const char *fullname)
1158 /* FIXME: we could do this faster since we know
1159 * the length of 'end'... */
1160 while ('\0' != *nxt)
1162 if (DIR_SEPARATOR == *nxt)
1166 if ((NULL == end) || (0 == strlen (end)))
1171 GNUNET_break (6 == strlen (end));
1172 return GNUNET_strdup (end);
1177 * Create a new random name for serialization. Also checks if persistence
1178 * is enabled and returns NULL if not.
1180 * @param h master context
1181 * @param ext component of the path
1182 * @return NULL on errror
1185 make_serialization_file_name (struct GNUNET_FS_Handle *h,
1192 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1193 return NULL; /* persistence not requested */
1194 dn = get_serialization_file_name (h, ext, "");
1197 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1202 fn = GNUNET_DISK_mktemp (dn);
1205 return NULL; /* epic fail */
1206 ret = get_serialization_short_name (fn);
1213 * Create a new random name for serialization. Also checks if persistence
1214 * is enabled and returns NULL if not.
1216 * @param h master context
1217 * @param ext component of the path
1218 * @param uni name of parent
1219 * @return NULL on errror
1222 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1230 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1231 return NULL; /* persistence not requested */
1232 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1235 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1240 fn = GNUNET_DISK_mktemp (dn);
1243 return NULL; /* epic fail */
1244 ret = get_serialization_short_name (fn);
1251 * Copy all of the data from the reader to the write handle.
1253 * @param wh write handle
1254 * @param fi file with reader
1255 * @return #GNUNET_OK on success
1258 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1259 struct GNUNET_FS_FileInformation *fi)
1261 char buf[32 * 1024];
1269 while (off < fi->data.file.file_size)
1271 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1273 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1277 return GNUNET_SYSERR;
1279 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1280 return GNUNET_SYSERR;
1288 * Create a temporary file on disk to store the current
1289 * state of @a fi in.
1291 * @param fi file information to sync with disk
1294 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1297 struct GNUNET_BIO_WriteHandle *wh;
1303 if (NULL == fi->serialization)
1305 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1306 if (NULL == fi->serialization)
1308 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1312 GNUNET_free (fi->serialization);
1313 fi->serialization = NULL;
1316 if (GNUNET_YES == fi->is_directory)
1318 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1320 else if (GNUNET_YES == fi->data.file.have_hash)
1322 else if (GNUNET_YES == fi->data.file.do_index)
1326 if (NULL != fi->keywords)
1327 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1330 if (NULL != fi->chk_uri)
1331 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1334 if (NULL != fi->sks_uri)
1335 skss = GNUNET_FS_uri_to_string (fi->sks_uri);
1338 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1339 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1340 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1341 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1342 (GNUNET_OK != GNUNET_BIO_write_string (wh, skss)) ||
1343 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1344 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1345 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1347 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value_us)) ||
1348 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1349 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1350 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1355 GNUNET_free_non_null (chks);
1357 GNUNET_free_non_null (ksks);
1359 GNUNET_free_non_null (skss);
1364 case 0: /* file-insert */
1365 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1370 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1371 if (GNUNET_OK != copy_from_reader (wh, fi))
1377 case 1: /* file-index, no hash */
1378 if (NULL == fi->filename)
1383 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1389 case 2: /* file-index-with-hash */
1390 case 3: /* file-index-with-hash-confirmed */
1391 if (NULL == fi->filename)
1396 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1398 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1399 sizeof (struct GNUNET_HashCode))))
1405 case 4: /* directory */
1406 if ( (NULL != fi->data.dir.entries) &&
1407 (NULL == fi->data.dir.entries->serialization) )
1408 GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
1409 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1410 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_completed)) ||
1411 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.dir.contents_size)) ||
1413 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1414 (uint32_t) fi->data.dir.dir_size)) ||
1416 GNUNET_BIO_write_string (wh,
1417 (fi->data.dir.entries ==
1418 NULL) ? NULL : fi->data.dir.
1419 entries->serialization)))
1429 if ( (NULL != fi->next) &&
1430 (NULL == fi->next->serialization) )
1431 GNUNET_FS_file_information_sync_ (fi->next);
1433 GNUNET_BIO_write_string (wh,
1435 NULL) ? fi->next->serialization : NULL))
1440 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1449 (void) GNUNET_BIO_write_close (wh);
1450 GNUNET_free_non_null (chks);
1451 GNUNET_free_non_null (ksks);
1452 GNUNET_free_non_null (skss);
1453 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1457 if (0 != UNLINK (fn))
1458 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1461 GNUNET_free (fi->serialization);
1462 fi->serialization = NULL;
1468 * Find the entry in the file information struct where the
1469 * serialization filename matches the given name.
1471 * @param pos file information to search
1472 * @param srch filename to search for
1473 * @return NULL if srch was not found in this subtree
1475 static struct GNUNET_FS_FileInformation *
1476 find_file_position (struct GNUNET_FS_FileInformation *pos,
1479 struct GNUNET_FS_FileInformation *r;
1483 if (0 == strcmp (srch, pos->serialization))
1485 if ( (GNUNET_YES == pos->is_directory) &&
1486 (NULL != (r = find_file_position (pos->data.dir.entries, srch))) )
1495 * Signal the FS's progress function that we are resuming
1498 * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
1499 * @param fi the entry in the publish-structure
1500 * @param length length of the file or directory
1501 * @param meta metadata for the file or directory (can be modified)
1502 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1503 * @param bo block options (can be modified)
1504 * @param do_index should we index?
1505 * @param client_info pointer to client context set upon creation (can be modified)
1506 * @return #GNUNET_OK to continue (always)
1509 fip_signal_resume (void *cls,
1510 struct GNUNET_FS_FileInformation *fi,
1512 struct GNUNET_CONTAINER_MetaData *meta,
1513 struct GNUNET_FS_Uri **uri,
1514 struct GNUNET_FS_BlockOptions *bo,
1518 struct GNUNET_FS_PublishContext *pc = cls;
1519 struct GNUNET_FS_ProgressInfo pi;
1521 if (GNUNET_YES == pc->skip_next_fi_callback)
1523 pc->skip_next_fi_callback = GNUNET_NO;
1526 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1527 pi.value.publish.specifics.resume.message = fi->emsg;
1528 pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
1529 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1530 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1532 /* process entries in directory */
1533 pc->skip_next_fi_callback = GNUNET_YES;
1534 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1541 * Function called with a filename of serialized publishing operation
1544 * @param cls the `struct GNUNET_FS_Handle *`
1545 * @param filename complete filename (absolute path)
1546 * @return #GNUNET_OK (continue to iterate)
1549 deserialize_publish_file (void *cls,
1550 const char *filename)
1552 struct GNUNET_FS_Handle *h = cls;
1553 struct GNUNET_BIO_ReadHandle *rh;
1554 struct GNUNET_FS_PublishContext *pc;
1559 struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
1563 pc = GNUNET_new (struct GNUNET_FS_PublishContext);
1565 pc->serialization = get_serialization_short_name (filename);
1568 rh = GNUNET_BIO_read_open (filename);
1574 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1576 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1577 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1578 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1579 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &have_ns)) ||
1581 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1582 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1583 || ( (GNUNET_YES == have_ns) &&
1584 (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof (ns)))) )
1589 pc->options = options;
1590 pc->all_done = all_done;
1591 if (NULL == fi_root)
1596 pc->fi = deserialize_file_information (h, fi_root);
1602 if (GNUNET_YES == have_ns)
1604 pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1607 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1608 (GNUNET_YES != pc->all_done))
1610 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1611 if (NULL == pc->dsh)
1616 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1617 GNUNET_free (fi_pos);
1619 if (NULL == pc->fi_pos)
1621 /* failed to find position for resuming, outch! Will start from root! */
1623 if (GNUNET_YES != pc->all_done)
1624 pc->fi_pos = pc->fi;
1627 GNUNET_free (fi_root);
1629 /* generate RESUME event(s) */
1630 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1632 /* re-start publishing (if needed)... */
1633 if (GNUNET_YES != pc->all_done)
1635 GNUNET_assert (NULL == pc->upload_task);
1637 GNUNET_SCHEDULER_add_with_priority
1638 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1639 &GNUNET_FS_publish_main_, pc);
1641 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1644 _("Failure while resuming publishing operation `%s': %s\n"),
1648 pc->top = GNUNET_FS_make_top (h,
1649 &GNUNET_FS_publish_signal_suspend_,
1653 GNUNET_free_non_null (pc->nid);
1654 GNUNET_free_non_null (pc->nuid);
1655 GNUNET_free_non_null (fi_root);
1656 GNUNET_free_non_null (fi_pos);
1657 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1660 _("Failed to resume publishing operation `%s': %s\n"), filename,
1665 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1666 if (0 != UNLINK (filename))
1667 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1668 GNUNET_free (pc->serialization);
1675 * Synchronize this publishing struct with its mirror
1676 * on disk. Note that all internal FS-operations that change
1677 * publishing structs should already call "sync" internally,
1678 * so this function is likely not useful for clients.
1680 * @param pc the struct to sync
1683 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1685 struct GNUNET_BIO_WriteHandle *wh;
1688 if (NULL == pc->serialization)
1690 make_serialization_file_name (pc->h,
1691 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1692 if (NULL == pc->serialization)
1696 if (NULL == pc->fi->serialization)
1701 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1708 have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
1709 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1710 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1711 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1712 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1713 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, have_ns)) ||
1714 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1716 GNUNET_BIO_write_string (wh,
1717 (NULL == pc->fi_pos) ? NULL : pc->fi_pos->serialization)) ||
1718 ( (NULL != pc->ns) &&
1719 (GNUNET_OK != GNUNET_BIO_write (wh,
1721 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) ) ))
1726 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1735 (void) GNUNET_BIO_write_close (wh);
1736 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1738 GNUNET_free (pc->serialization);
1739 pc->serialization = NULL;
1744 * Synchronize this unindex struct with its mirror
1745 * on disk. Note that all internal FS-operations that change
1746 * publishing structs should already call "sync" internally,
1747 * so this function is likely not useful for clients.
1749 * @param uc the struct to sync
1752 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1754 struct GNUNET_BIO_WriteHandle *wh;
1757 if (NULL == uc->serialization)
1759 make_serialization_file_name (uc->h,
1760 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1761 if (NULL == uc->serialization)
1763 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1770 if (NULL != uc->ksk_uri)
1771 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1774 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1775 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1776 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1777 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1779 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1780 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1781 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1782 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1784 GNUNET_BIO_write (wh, &uc->file_id, sizeof (struct GNUNET_HashCode)))) ||
1785 ((uc->state == UNINDEX_STATE_ERROR) &&
1786 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1791 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1800 (void) GNUNET_BIO_write_close (wh);
1801 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1803 GNUNET_free (uc->serialization);
1804 uc->serialization = NULL;
1809 * Serialize a download request.
1811 * @param wh handle for writing the download request to disk
1812 * @param dr the the request to write to disk
1813 * @return #GNUNET_YES on success, #GNUNET_NO on error
1816 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1817 struct DownloadRequest *dr)
1821 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1822 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1823 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1824 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1826 if ((BRS_CHK_SET == dr->state) &&
1828 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1830 for (i = 0; i < dr->num_children; i++)
1831 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1838 * Read a download request tree.
1840 * @param rh cadet to read from
1841 * @return value the download request read from disk, NULL on error
1843 static struct DownloadRequest *
1844 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1846 struct DownloadRequest *dr;
1849 dr = GNUNET_new (struct DownloadRequest);
1850 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1851 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1852 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1853 (dr->num_children > CHK_PER_INODE) ||
1854 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) ||
1855 ( (0 == dr->depth) &&
1856 (dr->num_children > 0) ) ||
1857 ((dr->depth > 0) && (0 == dr->num_children)))
1860 dr->num_children = 0;
1863 if (dr->num_children > 0)
1865 GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1869 case BRS_RECONSTRUCT_DOWN:
1870 case BRS_RECONSTRUCT_META_UP:
1871 case BRS_RECONSTRUCT_UP:
1875 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1878 case BRS_DOWNLOAD_DOWN:
1879 case BRS_DOWNLOAD_UP:
1886 for (i = 0; i < dr->num_children; i++)
1888 if (NULL == (dr->children[i] = read_download_request (rh)))
1890 dr->children[i]->parent = dr;
1894 GNUNET_FS_free_download_request_ (dr);
1900 * Compute the name of the sync file (or directory) for the given download
1903 * @param dc download context to compute for
1904 * @param uni unique filename to use, use "" for the directory name
1905 * @param ext extension to use, use ".dir" for our own subdirectory
1906 * @return the expanded file name, NULL for none
1909 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1916 if (dc->parent == NULL)
1917 return get_serialization_file_name (dc->h,
1918 (dc->search != NULL) ?
1919 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1920 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1922 if (NULL == dc->parent->serialization)
1924 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1927 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1934 * Synchronize this download struct with its mirror
1935 * on disk. Note that all internal FS-operations that change
1936 * publishing structs should already call "sync" internally,
1937 * so this function is likely not useful for clients.
1939 * @param dc the struct to sync
1942 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1944 struct GNUNET_BIO_WriteHandle *wh;
1949 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1950 return; /* we don't sync probes */
1951 if (NULL == dc->serialization)
1953 dir = get_download_sync_filename (dc, "", "");
1956 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1961 fn = GNUNET_DISK_mktemp (dir);
1965 dc->serialization = get_serialization_short_name (fn);
1969 fn = get_download_sync_filename (dc, dc->serialization, "");
1972 GNUNET_free (dc->serialization);
1973 dc->serialization = NULL;
1978 wh = GNUNET_BIO_write_open (fn);
1981 GNUNET_free (dc->serialization);
1982 dc->serialization = NULL;
1986 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1987 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1988 uris = GNUNET_FS_uri_to_string (dc->uri);
1989 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1990 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1991 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1992 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1993 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1994 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1995 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1996 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1997 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1998 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1999 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
2000 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
2001 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
2006 if (NULL == dc->emsg)
2008 GNUNET_assert (dc->top_request != NULL);
2009 if (GNUNET_YES != write_download_request (wh, dc->top_request))
2015 GNUNET_free_non_null (uris);
2017 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2027 (void) GNUNET_BIO_write_close (wh);
2028 GNUNET_free_non_null (uris);
2029 if (0 != UNLINK (fn))
2030 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
2032 GNUNET_free (dc->serialization);
2033 dc->serialization = NULL;
2038 * Synchronize this search result with its mirror
2039 * on disk. Note that all internal FS-operations that change
2040 * publishing structs should already call "sync" internally,
2041 * so this function is likely not useful for clients.
2043 * @param sr the struct to sync
2046 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
2048 struct GNUNET_BIO_WriteHandle *wh;
2054 if (NULL == sr->serialization)
2056 make_serialization_file_name_in_dir (sr->h,
2057 (sr->sc->psearch_result ==
2059 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2060 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2061 sr->sc->serialization);
2062 if (NULL == sr->serialization)
2064 wh = get_write_handle_in_dir (sr->h,
2065 (sr->sc->psearch_result ==
2066 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2067 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2068 sr->sc->serialization, sr->serialization);
2074 uris = GNUNET_FS_uri_to_string (sr->uri);
2075 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2077 GNUNET_BIO_write_string (wh,
2079 NULL ? sr->download->serialization : NULL)) ||
2081 GNUNET_BIO_write_string (wh,
2082 sr->update_search !=
2083 NULL ? sr->update_search->serialization : NULL))
2084 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
2085 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (struct GNUNET_HashCode)))
2086 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
2087 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
2088 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
2089 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
2094 if ( (NULL != sr->uri) &&
2095 (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
2096 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
2097 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
2102 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2108 GNUNET_free_non_null (uris);
2111 GNUNET_free_non_null (uris);
2113 (void) GNUNET_BIO_write_close (wh);
2114 remove_sync_file_in_dir (sr->h,
2115 (NULL == sr->sc->psearch_result)
2116 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2117 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2118 sr->sc->serialization, sr->serialization);
2119 GNUNET_free (sr->serialization);
2120 sr->serialization = NULL;
2125 * Synchronize this search struct with its mirror
2126 * on disk. Note that all internal FS-operations that change
2127 * publishing structs should already call "sync" internally,
2128 * so this function is likely not useful for clients.
2130 * @param sc the struct to sync
2133 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
2135 struct GNUNET_BIO_WriteHandle *wh;
2138 const char *category;
2141 (NULL == sc->psearch_result)
2142 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2143 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
2144 if (NULL == sc->serialization)
2145 sc->serialization = make_serialization_file_name (sc->h, category);
2146 if (NULL == sc->serialization)
2149 wh = get_write_handle (sc->h, category, sc->serialization);
2155 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
2156 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
2157 uris = GNUNET_FS_uri_to_string (sc->uri);
2158 in_pause = (sc->task != NULL) ? 'r' : '\0';
2159 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
2160 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
2161 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
2162 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
2163 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
2164 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
2171 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
2180 (void) GNUNET_BIO_write_close (wh);
2181 GNUNET_free_non_null (uris);
2182 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
2183 GNUNET_free (sc->serialization);
2184 sc->serialization = NULL;
2189 * Function called with a filename of serialized unindexing operation
2192 * @param cls the `struct GNUNET_FS_Handle *`
2193 * @param filename complete filename (absolute path)
2194 * @return #GNUNET_OK (continue to iterate)
2197 deserialize_unindex_file (void *cls,
2198 const char *filename)
2200 struct GNUNET_FS_Handle *h = cls;
2201 struct GNUNET_BIO_ReadHandle *rh;
2202 struct GNUNET_FS_UnindexContext *uc;
2203 struct GNUNET_FS_ProgressInfo pi;
2208 uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
2210 uc->serialization = get_serialization_short_name (filename);
2211 rh = GNUNET_BIO_read_open (filename);
2219 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
2220 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
2221 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
2222 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
2223 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
2224 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
2225 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2227 GNUNET_free_non_null (uris);
2233 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2235 if (NULL == uc->ksk_uri)
2238 GNUNET_free_non_null (emsg);
2242 if ( (uc->ksk_offset > 0) &&
2243 ( (NULL == uc->ksk_uri) ||
2244 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2249 uc->state = (enum UnindexState) state;
2252 case UNINDEX_STATE_HASHING:
2254 case UNINDEX_STATE_FS_NOTIFY:
2256 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2257 sizeof (struct GNUNET_HashCode)))
2263 case UNINDEX_STATE_DS_REMOVE:
2264 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2265 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2267 case UNINDEX_STATE_COMPLETE:
2269 case UNINDEX_STATE_ERROR:
2271 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2281 uc->top = GNUNET_FS_make_top (h,
2282 &GNUNET_FS_unindex_signal_suspend_,
2284 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2285 pi.value.unindex.specifics.resume.message = uc->emsg;
2286 GNUNET_FS_unindex_make_status_ (&pi, uc,
2288 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2291 case UNINDEX_STATE_HASHING:
2293 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2295 &GNUNET_FS_unindex_process_hash_, uc);
2297 case UNINDEX_STATE_FS_NOTIFY:
2298 uc->state = UNINDEX_STATE_HASHING;
2299 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2301 case UNINDEX_STATE_DS_REMOVE:
2302 GNUNET_FS_unindex_do_remove_ (uc);
2304 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2305 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2307 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2308 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2310 case UNINDEX_STATE_COMPLETE:
2311 case UNINDEX_STATE_ERROR:
2312 /* no need to resume any operation, we were done */
2317 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2320 _("Failure while resuming unindexing operation `%s': %s\n"),
2326 GNUNET_free_non_null (uc->filename);
2327 if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2330 _("Failed to resume unindexing operation `%s': %s\n"),
2335 if (NULL != uc->serialization)
2336 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2338 GNUNET_free_non_null (uc->serialization);
2345 * Deserialize a download.
2347 * @param h overall context
2348 * @param rh file to deserialize from
2349 * @param parent parent download
2350 * @param search associated search
2351 * @param serialization name under which the search was serialized
2354 deserialize_download (struct GNUNET_FS_Handle *h,
2355 struct GNUNET_BIO_ReadHandle *rh,
2356 struct GNUNET_FS_DownloadContext *parent,
2357 struct GNUNET_FS_SearchResult *search,
2358 const char *serialization);
2362 * Deserialize a search.
2364 * @param h overall context
2365 * @param rh file to deserialize from
2366 * @param psearch_result parent search result
2367 * @param serialization name under which the search was serialized
2369 static struct GNUNET_FS_SearchContext *
2370 deserialize_search (struct GNUNET_FS_Handle *h,
2371 struct GNUNET_BIO_ReadHandle *rh,
2372 struct GNUNET_FS_SearchResult *psearch_result,
2373 const char *serialization);
2377 * Function called with a filename of serialized search result
2380 * @param cls the `struct GNUNET_FS_SearchContext *`
2381 * @param filename complete filename (absolute path)
2382 * @return #GNUNET_OK (continue to iterate)
2385 deserialize_search_result (void *cls,
2386 const char *filename)
2388 struct GNUNET_FS_SearchContext *sc = cls;
2394 struct GNUNET_BIO_ReadHandle *rh;
2395 struct GNUNET_BIO_ReadHandle *drh;
2396 struct GNUNET_FS_SearchResult *sr;
2398 ser = get_serialization_short_name (filename);
2399 rh = GNUNET_BIO_read_open (filename);
2404 remove_sync_file_in_dir (sc->h,
2405 (NULL == sc->psearch_result)
2406 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2407 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2408 sc->serialization, ser);
2417 sr = GNUNET_new (struct GNUNET_FS_SearchResult);
2420 sr->serialization = ser;
2421 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2422 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2423 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2425 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2426 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2428 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (struct GNUNET_HashCode)))
2429 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2430 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2431 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2432 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2437 if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
2439 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2440 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2442 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2449 if (NULL != download)
2451 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2454 deserialize_download (sc->h, drh, NULL, sr, download);
2455 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2458 _("Failed to resume sub-download `%s': %s\n"),
2464 GNUNET_free (download);
2466 if (NULL != update_srch)
2469 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2472 deserialize_search (sc->h, drh, sr, update_srch);
2473 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2476 _("Failed to resume sub-search `%s': %s\n"),
2482 GNUNET_free (update_srch);
2484 GNUNET_break (GNUNET_YES ==
2485 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2488 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2491 _("Failure while resuming search operation `%s': %s\n"),
2497 GNUNET_free_non_null (download);
2498 GNUNET_free_non_null (emsg);
2499 GNUNET_free_non_null (uris);
2500 GNUNET_free_non_null (update_srch);
2501 if (NULL != sr->uri)
2502 GNUNET_FS_uri_destroy (sr->uri);
2503 if (NULL != sr->meta)
2504 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2505 GNUNET_free (sr->serialization);
2507 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2510 _("Failure while resuming search operation `%s': %s\n"),
2519 * Send the 'resume' signal to the callback; also actually
2520 * resume the download (put it in the queue). Does this
2521 * recursively for the top-level download and all child
2524 * @param dc download to resume
2527 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2529 struct GNUNET_FS_DownloadContext *dcc;
2530 struct GNUNET_FS_ProgressInfo pi;
2532 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2533 pi.value.download.specifics.resume.meta = dc->meta;
2534 pi.value.download.specifics.resume.message = dc->emsg;
2535 GNUNET_FS_download_make_status_ (&pi, dc);
2536 dcc = dc->child_head;
2539 signal_download_resume (dcc);
2546 * Signal resuming of a search to our clients (for the
2547 * top level search and all sub-searches).
2549 * @param sc search being resumed
2552 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2556 * Iterator over search results signaling resume to the client for
2559 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2560 * @param key current key code
2561 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2562 * @return #GNUNET_YES (we should continue to iterate)
2565 signal_result_resume (void *cls,
2566 const struct GNUNET_HashCode *key,
2569 struct GNUNET_FS_SearchContext *sc = cls;
2570 struct GNUNET_FS_ProgressInfo pi;
2571 struct GNUNET_FS_SearchResult *sr = value;
2573 if (0 == sr->mandatory_missing)
2575 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2576 pi.value.search.specifics.resume_result.meta = sr->meta;
2577 pi.value.search.specifics.resume_result.uri = sr->uri;
2578 pi.value.search.specifics.resume_result.result = sr;
2579 pi.value.search.specifics.resume_result.availability_rank =
2580 2 * sr->availability_success - sr->availability_trials;
2581 pi.value.search.specifics.resume_result.availability_certainty =
2582 sr->availability_trials;
2583 pi.value.search.specifics.resume_result.applicability_rank =
2584 sr->optional_support;
2585 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2587 if (NULL != sr->download)
2589 signal_download_resume (sr->download);
2593 GNUNET_FS_search_start_probe_ (sr);
2595 if (NULL != sr->update_search)
2596 signal_search_resume (sr->update_search);
2602 * Free memory allocated by the search context and its children
2604 * @param sc search context to free
2607 free_search_context (struct GNUNET_FS_SearchContext *sc);
2611 * Iterator over search results freeing each.
2613 * @param cls closure, the `struct GNUNET_FS_SearchContext *`
2614 * @param key current key code
2615 * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
2616 * @return #GNUNET_YES (we should continue to iterate)
2619 free_result (void *cls,
2620 const struct GNUNET_HashCode *key,
2623 struct GNUNET_FS_SearchResult *sr = value;
2625 if (NULL != sr->update_search)
2627 free_search_context (sr->update_search);
2628 GNUNET_assert (NULL == sr->update_search);
2630 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2631 GNUNET_FS_uri_destroy (sr->uri);
2638 * Free memory allocated by the search context and its children
2640 * @param sc search context to free
2643 free_search_context (struct GNUNET_FS_SearchContext *sc)
2645 if (NULL != sc->serialization)
2647 GNUNET_FS_remove_sync_file_ (sc->h,
2648 (sc->psearch_result ==
2649 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2650 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2652 GNUNET_FS_remove_sync_dir_ (sc->h,
2653 (sc->psearch_result ==
2654 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2655 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2658 GNUNET_free_non_null (sc->serialization);
2659 GNUNET_free_non_null (sc->emsg);
2660 if (NULL != sc->uri)
2661 GNUNET_FS_uri_destroy (sc->uri);
2662 if (NULL != sc->master_result_map)
2664 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2666 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2673 * Function called with a filename of serialized sub-download
2676 * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
2677 * @param filename complete filename (absolute path)
2678 * @return #GNUNET_OK (continue to iterate)
2681 deserialize_subdownload (void *cls,
2682 const char *filename)
2684 struct GNUNET_FS_DownloadContext *parent = cls;
2687 struct GNUNET_BIO_ReadHandle *rh;
2689 ser = get_serialization_short_name (filename);
2690 rh = GNUNET_BIO_read_open (filename);
2693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2694 _("Failed to resume sub-download `%s': could not open file `%s'\n"),
2700 deserialize_download (parent->h, rh, parent, NULL, ser);
2701 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2704 _("Failed to resume sub-download `%s': %s\n"),
2715 * Free this download context and all of its descendants.
2716 * (only works during deserialization since not all possible
2717 * state it taken care of).
2719 * @param dc context to free
2722 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2724 struct GNUNET_FS_DownloadContext *dcc;
2726 if (NULL != dc->meta)
2727 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2728 if (NULL != dc->uri)
2729 GNUNET_FS_uri_destroy (dc->uri);
2730 GNUNET_free_non_null (dc->temp_filename);
2731 GNUNET_free_non_null (dc->emsg);
2732 GNUNET_free_non_null (dc->filename);
2733 GNUNET_free_non_null (dc->serialization);
2734 while (NULL != (dcc = dc->child_head))
2736 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2739 free_download_context (dcc);
2741 GNUNET_FS_free_download_request_ (dc->top_request);
2742 if (NULL != dc->active)
2743 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2749 * Deserialize a download.
2751 * @param h overall context
2752 * @param rh file to deserialize from
2753 * @param parent parent download
2754 * @param search associated search
2755 * @param serialization name under which the search was serialized
2758 deserialize_download (struct GNUNET_FS_Handle *h,
2759 struct GNUNET_BIO_ReadHandle *rh,
2760 struct GNUNET_FS_DownloadContext *parent,
2761 struct GNUNET_FS_SearchResult *search,
2762 const char *serialization)
2764 struct GNUNET_FS_DownloadContext *dc;
2773 dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
2774 dc->parent = parent;
2776 dc->serialization = GNUNET_strdup (serialization);
2778 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2779 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2780 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2781 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2782 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2784 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2786 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2788 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2790 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2791 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2792 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2793 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2794 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2795 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2796 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2797 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2802 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2804 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE), GNUNET_NO);
2805 dc->has_finished = (int) status;
2807 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2808 if (GNUNET_FS_uri_test_loc (dc->uri))
2809 GNUNET_assert (GNUNET_OK ==
2810 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2812 if (NULL == dc->emsg)
2814 dc->top_request = read_download_request (rh);
2815 if (NULL == dc->top_request)
2821 dn = get_download_sync_filename (dc,
2827 GNUNET_DISK_directory_test (dn,
2829 GNUNET_DISK_directory_scan (dn,
2830 &deserialize_subdownload,
2836 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2842 dc->search = search;
2843 search->download = dc;
2845 if ( (NULL == parent) &&
2849 = GNUNET_FS_make_top (dc->h,
2850 &GNUNET_FS_download_signal_suspend_,
2852 signal_download_resume (dc);
2855 GNUNET_assert (NULL == dc->job_queue);
2856 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_,
2860 GNUNET_free_non_null (uris);
2861 GNUNET_free_non_null (emsg);
2862 free_download_context (dc);
2867 * Signal resuming of a search to our clients (for the
2868 * top level search and all sub-searches).
2870 * @param sc search being resumed
2873 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2875 struct GNUNET_FS_ProgressInfo pi;
2877 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2878 pi.value.search.specifics.resume.message = sc->emsg;
2879 pi.value.search.specifics.resume.is_paused =
2880 (NULL == sc->mq) ? GNUNET_YES : GNUNET_NO;
2881 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
2882 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2883 &signal_result_resume, sc);
2889 * Deserialize a search.
2891 * @param h overall context
2892 * @param rh file to deserialize from
2893 * @param psearch_result parent search result
2894 * @param serialization name under which the search was serialized
2896 static struct GNUNET_FS_SearchContext *
2897 deserialize_search (struct GNUNET_FS_Handle *h,
2898 struct GNUNET_BIO_ReadHandle *rh,
2899 struct GNUNET_FS_SearchResult *psearch_result,
2900 const char *serialization)
2902 struct GNUNET_FS_SearchContext *sc;
2909 if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
2916 sc = GNUNET_new (struct GNUNET_FS_SearchContext);
2917 if (NULL != psearch_result)
2919 sc->psearch_result = psearch_result;
2920 psearch_result->update_search = sc;
2923 sc->serialization = GNUNET_strdup (serialization);
2925 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
2926 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2927 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2928 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2929 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2931 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2932 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2934 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2935 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2940 sc->options = (enum GNUNET_FS_SearchOptions) options;
2941 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
2942 dn = get_serialization_file_name_in_dir (h,
2943 (NULL == sc->psearch_result) ?
2944 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2945 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2946 sc->serialization, "");
2949 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
2950 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2953 if (('\0' == in_pause) &&
2954 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2956 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2957 _("Could not resume running search, will resume as paused search\n"));
2959 signal_search_resume (sc);
2963 GNUNET_free_non_null (emsg);
2964 free_search_context (sc);
2965 GNUNET_free_non_null (uris);
2971 * Function called with a filename of serialized search operation
2974 * @param cls the `struct GNUNET_FS_Handle *`
2975 * @param filename complete filename (absolute path)
2976 * @return #GNUNET_OK (continue to iterate)
2979 deserialize_search_file (void *cls,
2980 const char *filename)
2982 struct GNUNET_FS_Handle *h = cls;
2985 struct GNUNET_BIO_ReadHandle *rh;
2986 struct GNUNET_FS_SearchContext *sc;
2989 if (0 != STAT (filename, &buf))
2991 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2994 if (S_ISDIR (buf.st_mode))
2995 return GNUNET_OK; /* skip directories */
2996 ser = get_serialization_short_name (filename);
2997 rh = GNUNET_BIO_read_open (filename);
3002 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
3007 sc = deserialize_search (h, rh, NULL, ser);
3009 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
3011 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3013 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3014 _("Failure while resuming search operation `%s': %s\n"),
3023 * Function called with a filename of serialized download operation
3026 * @param cls the `struct GNUNET_FS_Handle *`
3027 * @param filename complete filename (absolute path)
3028 * @return #GNUNET_OK (continue to iterate)
3031 deserialize_download_file (void *cls, const char *filename)
3033 struct GNUNET_FS_Handle *h = cls;
3036 struct GNUNET_BIO_ReadHandle *rh;
3038 ser = get_serialization_short_name (filename);
3039 rh = GNUNET_BIO_read_open (filename);
3042 if (0 != UNLINK (filename))
3043 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
3047 deserialize_download (h, rh, NULL, NULL, ser);
3049 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
3051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3052 _("Failure while resuming download operation `%s': %s\n"),
3061 * Deserialize informatin about pending operations.
3063 * @param master_path which master directory should be scanned
3064 * @param proc function to call for each entry (will get @a h for 'cls')
3065 * @param h the `struct GNUNET_FS_Handle *`
3068 deserialization_master (const char *master_path,
3069 GNUNET_FileNameCallback proc,
3070 struct GNUNET_FS_Handle *h)
3074 dn = get_serialization_file_name (h, master_path, "");
3077 if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
3078 GNUNET_DISK_directory_scan (dn, proc, h);
3084 * Setup a connection to the file-sharing service.
3086 * @param cfg configuration to use
3087 * @param client_name unique identifier for this client
3088 * @param upcb function to call to notify about FS actions
3089 * @param upcb_cls closure for @a upcb
3090 * @param flags specific attributes for fs-operations
3091 * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
3092 * @return NULL on error
3094 struct GNUNET_FS_Handle *
3095 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
3096 const char *client_name,
3097 GNUNET_FS_ProgressCallback upcb,
3099 enum GNUNET_FS_Flags flags, ...)
3101 struct GNUNET_FS_Handle *ret;
3102 enum GNUNET_FS_OPTIONS opt;
3105 ret = GNUNET_new (struct GNUNET_FS_Handle);
3107 ret->client_name = GNUNET_strdup (client_name);
3109 ret->upcb_cls = upcb_cls;
3111 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
3112 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
3113 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
3114 va_start (ap, flags);
3115 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
3119 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
3120 ret->max_parallel_downloads = va_arg (ap, unsigned int);
3123 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
3124 ret->max_parallel_requests = va_arg (ap, unsigned int);
3129 GNUNET_free (ret->client_name);
3136 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
3138 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
3139 &deserialize_publish_file, ret);
3140 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
3141 &deserialize_search_file, ret);
3142 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
3143 &deserialize_download_file, ret);
3144 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
3145 &deserialize_unindex_file, ret);
3152 * Close our connection with the file-sharing service.
3153 * The callback given to #GNUNET_FS_start() will no longer be
3154 * called after this function returns.
3155 * This function MUST NOT be called from within the
3158 * @param h handle that was returned from #GNUNET_FS_start()
3161 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
3163 while (NULL != h->top_head)
3164 h->top_head->ssf (h->top_head->ssf_cls);
3165 if (NULL != h->queue_job)
3166 GNUNET_SCHEDULER_cancel (h->queue_job);
3167 GNUNET_free (h->client_name);
3172 /* end of fs_api.c */