2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 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 (qe->client == NULL)
59 qe->start (qe->cls, qe->client);
61 qe->h->active_blocks += qe->blocks;
62 qe->start_time = GNUNET_TIME_absolute_get ();
63 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head, qe->h->pending_tail, qe);
64 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head, qe->h->running_tail,
65 qe->h->running_tail, qe);
70 * Stop the given job (send signal, remove from active queue, update
71 * counters and state).
73 * @param qe job to stop
76 stop_job (struct GNUNET_FS_QueueEntry *qe)
80 qe->h->active_downloads--;
81 qe->h->active_blocks -= qe->blocks;
83 GNUNET_TIME_relative_add (qe->run_time,
84 GNUNET_TIME_absolute_get_duration
86 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
87 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
88 qe->h->pending_tail, qe);
93 * Process the jobs in the job queue, possibly starting some
94 * and stopping others.
96 * @param cls the 'struct GNUNET_FS_Handle'
97 * @param tc scheduler context
100 process_job_queue (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 struct GNUNET_FS_Handle *h = cls;
103 struct GNUNET_FS_QueueEntry *qe;
104 struct GNUNET_FS_QueueEntry *next;
105 struct GNUNET_TIME_Relative run_time;
106 struct GNUNET_TIME_Relative restart_at;
107 struct GNUNET_TIME_Relative rst;
108 struct GNUNET_TIME_Absolute end_time;
110 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
111 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
112 /* first, see if we can start all the jobs */
113 next = h->pending_head;
114 while (NULL != (qe = next))
117 if (h->running_head == NULL)
122 if ((qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
123 (h->active_downloads < h->max_parallel_downloads))
129 if (h->pending_head == NULL)
130 return; /* no need to stop anything */
131 /* then, check if we should stop some jobs */
132 next = h->running_head;
133 while (NULL != (qe = next))
137 GNUNET_TIME_relative_multiply (h->avg_block_latency,
138 qe->blocks * qe->start_times);
139 switch (qe->priority)
141 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
142 /* run probes for at most 1s * number-of-restarts; note that
143 as the total runtime of a probe is limited to 2m, we don't
144 need to additionally limit the total time of a probe to
145 strictly limit its lifetime. */
146 run_time = GNUNET_TIME_relative_min (run_time,
147 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
148 1 + qe->start_times));
150 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
155 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
156 rst = GNUNET_TIME_absolute_get_remaining (end_time);
157 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
158 if (rst.rel_value > 0)
162 /* finally, start some more tasks if we now have empty slots */
163 next = h->pending_head;
164 while (NULL != (qe = next))
167 if ((qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
168 (h->active_downloads < h->max_parallel_downloads))
175 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
180 * Add a job to the queue.
182 * @param h handle to the overall FS state
183 * @param start function to call to begin the job
184 * @param stop function to call to pause the job, or on dequeue (if the job was running)
185 * @param cls closure for start and stop
186 * @param blocks number of blocks this jobs uses
187 * @param priority how important is this download
188 * @return queue handle
190 struct GNUNET_FS_QueueEntry *
191 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h, GNUNET_FS_QueueStart start,
192 GNUNET_FS_QueueStop stop, void *cls, unsigned int blocks,
193 enum GNUNET_FS_QueuePriority priority)
195 struct GNUNET_FS_QueueEntry *qe;
197 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
202 qe->queue_time = GNUNET_TIME_absolute_get ();
204 qe->priority = priority;
205 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
206 h->pending_tail, qe);
207 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
208 GNUNET_SCHEDULER_cancel (h->queue_job);
209 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
215 * Dequeue a job from the queue.
216 * @param qh handle for the job
219 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
221 struct GNUNET_FS_Handle *h;
224 if (qh->client != NULL)
226 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qh);
228 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
229 GNUNET_SCHEDULER_cancel (h->queue_job);
230 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
235 * Create a top-level activity entry.
237 * @param h global fs handle
238 * @param ssf suspend signal function to use
239 * @param ssf_cls closure for ssf
240 * @return fresh top-level activity handle
242 struct TopLevelActivity *
243 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h, SuspendSignalFunction ssf,
246 struct TopLevelActivity *ret;
248 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
250 ret->ssf_cls = ssf_cls;
251 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
257 * Destroy a top-level activity entry.
259 * @param h global fs handle
260 * @param top top level activity entry
263 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top)
265 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
272 * Closure for "data_reader_file".
277 * Name of the file to read.
282 * File descriptor, NULL if it has not yet been opened.
284 struct GNUNET_DISK_FileHandle *fd;
289 * Function that provides data by reading from a file.
291 * @param cls closure (points to the file information)
292 * @param offset offset to read from; it is possible
293 * that the caller might need to go backwards
295 * @param max maximum number of bytes that should be
296 * copied to buf; readers are not allowed
297 * to provide less data unless there is an error;
298 * a value of "0" will be used at the end to allow
299 * the reader to clean up its internal state
300 * @param buf where the reader should write the data
301 * @param emsg location for the reader to store an error message
302 * @return number of bytes written, usually "max", 0 on error
305 GNUNET_FS_data_reader_file_ (void *cls, uint64_t offset, size_t max, void *buf,
308 struct FileInfo *fi = cls;
314 GNUNET_DISK_file_close (fi->fd);
315 GNUNET_free (fi->filename);
322 GNUNET_DISK_file_open (fi->filename, GNUNET_DISK_OPEN_READ,
323 GNUNET_DISK_PERM_NONE);
326 GNUNET_asprintf (emsg, _("Could not open file `%s': %s"), fi->filename,
331 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
332 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
335 GNUNET_asprintf (emsg, _("Could not read file `%s': %s"), fi->filename,
341 GNUNET_asprintf (emsg, _("Short read reading from file `%s'!"),
350 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
352 * @param filename file to read
353 * @return closure to use, NULL on error
356 GNUNET_FS_make_file_reader_context_ (const char *filename)
360 fi = GNUNET_malloc (sizeof (struct FileInfo));
361 fi->filename = GNUNET_STRINGS_filename_expand (filename);
362 if (fi->filename == NULL)
372 * Function that provides data by copying from a buffer.
374 * @param cls closure (points to the buffer)
375 * @param offset offset to read from; it is possible
376 * that the caller might need to go backwards
378 * @param max maximum number of bytes that should be
379 * copied to buf; readers are not allowed
380 * to provide less data unless there is an error;
381 * a value of "0" will be used at the end to allow
382 * the reader to clean up its internal state
383 * @param buf where the reader should write the data
384 * @param emsg location for the reader to store an error message
385 * @return number of bytes written, usually "max", 0 on error
388 GNUNET_FS_data_reader_copy_ (void *cls, uint64_t offset, size_t max, void *buf,
395 GNUNET_free_non_null (data);
398 memcpy (buf, &data[offset], max);
404 * Return the full filename where we would store state information
405 * (for serialization/deserialization).
407 * @param h master context
408 * @param ext component of the path
409 * @param ent entity identifier (or emtpy string for the directory)
410 * @return NULL on error
413 get_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext,
419 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
420 return NULL; /* persistence not requested */
422 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
425 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
426 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
428 GNUNET_free (basename);
434 * Return the full filename where we would store state information
435 * (for serialization/deserialization) that is associated with a
438 * @param h master context
439 * @param ext component of the path
440 * @param uni name of the parent operation
441 * @param ent entity identifier (or emtpy string for the directory)
442 * @return NULL on error
445 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
446 const char *uni, const char *ent)
451 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
452 return NULL; /* persistence not requested */
454 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
457 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
458 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
459 uni, DIR_SEPARATOR_STR, ent);
460 GNUNET_free (basename);
466 * Return a read handle for deserialization.
468 * @param h master context
469 * @param ext component of the path
470 * @param ent entity identifier (or emtpy string for the directory)
471 * @return NULL on error
473 static struct GNUNET_BIO_ReadHandle *
474 get_read_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
477 struct GNUNET_BIO_ReadHandle *ret;
479 fn = get_serialization_file_name (h, ext, ent);
482 ret = GNUNET_BIO_read_open (fn);
489 * Return a write handle for serialization.
491 * @param h master context
492 * @param ext component of the path
493 * @param ent entity identifier (or emtpy string for the directory)
494 * @return NULL on error
496 static struct GNUNET_BIO_WriteHandle *
497 get_write_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
500 struct GNUNET_BIO_WriteHandle *ret;
502 fn = get_serialization_file_name (h, ext, ent);
507 ret = GNUNET_BIO_write_open (fn);
516 * Return a write handle for serialization.
518 * @param h master context
519 * @param ext component of the path
520 * @param uni name of parent
521 * @param ent entity identifier (or emtpy string for the directory)
522 * @return NULL on error
524 static struct GNUNET_BIO_WriteHandle *
525 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
526 const char *uni, const char *ent)
529 struct GNUNET_BIO_WriteHandle *ret;
531 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
534 ret = GNUNET_BIO_write_open (fn);
541 * Remove serialization/deserialization file from disk.
543 * @param h master context
544 * @param ext component of the path
545 * @param ent entity identifier
548 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h, const char *ext,
553 if ((NULL == ent) || (0 == strlen (ent)))
558 filename = get_serialization_file_name (h, ext, ent);
559 if (filename != NULL)
561 if (0 != UNLINK (filename))
562 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
563 GNUNET_free (filename);
569 * Remove serialization/deserialization file from disk.
571 * @param h master context
572 * @param ext component of the path
573 * @param uni parent name
574 * @param ent entity identifier
577 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
578 const char *uni, const char *ent)
582 if ((NULL == ent) || (0 == strlen (ent)))
587 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
588 if (filename != NULL)
590 if (0 != UNLINK (filename))
591 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
592 GNUNET_free (filename);
598 * Remove serialization/deserialization directory from disk.
600 * @param h master context
601 * @param ext component of the path
602 * @param uni unique name of parent
605 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h, const char *ext,
612 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
615 if ((GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
616 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
617 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
623 * Serialize a 'start_time'. Since we use start-times to
624 * calculate the duration of some operation, we actually
625 * do not serialize the absolute time but the (relative)
626 * duration since the start time. When we then
627 * deserialize the start time, we take the current time and
628 * subtract that duration so that we get again an absolute
629 * time stamp that will result in correct performance
632 * @param wh handle for writing
633 * @param timestamp time to serialize
634 * @return GNUNET_OK on success
637 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
638 struct GNUNET_TIME_Absolute timestamp)
640 struct GNUNET_TIME_Relative dur;
642 dur = GNUNET_TIME_absolute_get_duration (timestamp);
643 return GNUNET_BIO_write_int64 (wh, dur.rel_value);
648 * Serialize a 'start_time'. Since we use start-times to
649 * calculate the duration of some operation, we actually
650 * do not serialize the absolute time but the (relative)
651 * duration since the start time. When we then
652 * deserialize the start time, we take the current time and
653 * subtract that duration so that we get again an absolute
654 * time stamp that will result in correct performance
657 * @param rh handle for reading
658 * @param timestamp where to write the deserialized timestamp
659 * @return GNUNET_OK on success
662 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
663 struct GNUNET_TIME_Absolute *timestamp)
665 struct GNUNET_TIME_Relative dur;
667 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value))
668 return GNUNET_SYSERR;
669 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
675 * Using the given serialization filename, try to deserialize
676 * the file-information tree associated with it.
678 * @param h master context
679 * @param filename name of the file (without directory) with
681 * @return NULL on error
683 static struct GNUNET_FS_FileInformation *
684 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename);
688 * Using the given serialization filename, try to deserialize
689 * the file-information tree associated with it.
691 * @param h master context
692 * @param fn name of the file (without directory) with
694 * @param rh handle for reading
695 * @return NULL on error
697 static struct GNUNET_FS_FileInformation *
698 deserialize_fi_node (struct GNUNET_FS_Handle *h, const char *fn,
699 struct GNUNET_BIO_ReadHandle *rh)
701 struct GNUNET_FS_FileInformation *ret;
702 struct GNUNET_FS_FileInformation *nxt;
709 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
714 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
719 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
720 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
722 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
723 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
724 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
726 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
727 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
728 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
729 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
731 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
733 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value)) ||
734 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
735 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
736 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
743 case 0: /* file-insert */
744 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
749 ret->is_directory = GNUNET_NO;
750 ret->data.file.do_index = GNUNET_NO;
751 ret->data.file.have_hash = GNUNET_NO;
752 ret->data.file.index_start_confirmed = GNUNET_NO;
753 if (GNUNET_NO == ret->is_published)
755 if (NULL == ret->filename)
757 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
758 ret->data.file.reader_cls =
759 GNUNET_malloc_large (ret->data.file.file_size);
760 if (ret->data.file.reader_cls == NULL)
763 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
764 ret->data.file.file_size))
772 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
773 ret->data.file.reader_cls =
774 GNUNET_FS_make_file_reader_context_ (ret->filename);
778 case 1: /* file-index, no hash */
779 if (NULL == ret->filename)
784 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
789 ret->is_directory = GNUNET_NO;
790 ret->data.file.do_index = GNUNET_YES;
791 ret->data.file.have_hash = GNUNET_NO;
792 ret->data.file.index_start_confirmed = GNUNET_NO;
793 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
794 ret->data.file.reader_cls =
795 GNUNET_FS_make_file_reader_context_ (ret->filename);
797 case 2: /* file-index-with-hash */
798 if (NULL == ret->filename)
803 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
805 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
806 sizeof (GNUNET_HashCode))))
811 ret->is_directory = GNUNET_NO;
812 ret->data.file.do_index = GNUNET_YES;
813 ret->data.file.have_hash = GNUNET_YES;
814 ret->data.file.index_start_confirmed = GNUNET_NO;
815 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
816 ret->data.file.reader_cls =
817 GNUNET_FS_make_file_reader_context_ (ret->filename);
819 case 3: /* file-index-with-hash-confirmed */
820 if (NULL == ret->filename)
825 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
827 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
828 sizeof (GNUNET_HashCode))))
833 ret->is_directory = GNUNET_NO;
834 ret->data.file.do_index = GNUNET_YES;
835 ret->data.file.have_hash = GNUNET_YES;
836 ret->data.file.index_start_confirmed = GNUNET_YES;
837 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
838 ret->data.file.reader_cls =
839 GNUNET_FS_make_file_reader_context_ (ret->filename);
841 case 4: /* directory */
842 ret->is_directory = GNUNET_YES;
843 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
844 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
846 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
848 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
853 ret->data.dir.dir_size = (uint32_t) dsize;
854 if (filename != NULL)
856 ret->data.dir.entries = deserialize_file_information (h, filename);
857 GNUNET_free (filename);
859 nxt = ret->data.dir.entries;
871 ret->serialization = GNUNET_strdup (fn);
873 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
878 if (filename != NULL)
880 ret->next = deserialize_file_information (h, filename);
881 GNUNET_free (filename);
884 GNUNET_free_non_null (ksks);
885 GNUNET_free_non_null (chks);
888 GNUNET_free_non_null (ksks);
889 GNUNET_free_non_null (chks);
890 GNUNET_free_non_null (filename);
891 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
897 * Using the given serialization filename, try to deserialize
898 * the file-information tree associated with it.
900 * @param h master context
901 * @param filename name of the file (without directory) with
903 * @return NULL on error
905 static struct GNUNET_FS_FileInformation *
906 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename)
908 struct GNUNET_FS_FileInformation *ret;
909 struct GNUNET_BIO_ReadHandle *rh;
912 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
915 ret = deserialize_fi_node (h, filename, rh);
916 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
919 _("Failed to resume publishing information `%s': %s\n"),
925 if (0 != UNLINK (filename))
926 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
933 * Given a serialization name (full absolute path), return the
934 * basename of the file (without the path), which must only
935 * consist of the 6 random characters.
937 * @param fullname name to extract the basename from
938 * @return copy of the basename, NULL on error
941 get_serialization_short_name (const char *fullname)
948 /* FIXME: we could do this faster since we know
949 * the length of 'end'... */
952 if (DIR_SEPARATOR == *nxt)
956 if ((end == NULL) || (strlen (end) == 0))
961 GNUNET_break (6 == strlen (end));
962 return GNUNET_strdup (end);
967 * Create a new random name for serialization. Also checks if persistence
968 * is enabled and returns NULL if not.
970 * @param h master context
971 * @param ext component of the path
972 * @return NULL on errror
975 make_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext)
981 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
982 return NULL; /* persistence not requested */
983 dn = get_serialization_file_name (h, ext, "");
986 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
991 fn = GNUNET_DISK_mktemp (dn);
994 return NULL; /* epic fail */
995 ret = get_serialization_short_name (fn);
1002 * Create a new random name for serialization. Also checks if persistence
1003 * is enabled and returns NULL if not.
1005 * @param h master context
1006 * @param ext component of the path
1007 * @param uni name of parent
1008 * @return NULL on errror
1011 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1012 const char *ext, const char *uni)
1018 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1019 return NULL; /* persistence not requested */
1020 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1023 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1028 fn = GNUNET_DISK_mktemp (dn);
1031 return NULL; /* epic fail */
1032 ret = get_serialization_short_name (fn);
1039 * Copy all of the data from the reader to the write handle.
1041 * @param wh write handle
1042 * @param fi file with reader
1043 * @return GNUNET_OK on success
1046 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1047 struct GNUNET_FS_FileInformation *fi)
1049 char buf[32 * 1024];
1057 while (off < fi->data.file.file_size)
1059 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1061 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1065 return GNUNET_SYSERR;
1067 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1068 return GNUNET_SYSERR;
1076 * Create a temporary file on disk to store the current
1079 * @param fi file information to sync with disk
1082 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1085 struct GNUNET_BIO_WriteHandle *wh;
1090 if (NULL == fi->serialization)
1092 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1093 if (NULL == fi->serialization)
1095 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1099 GNUNET_free (fi->serialization);
1100 fi->serialization = NULL;
1103 if (GNUNET_YES == fi->is_directory)
1105 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1107 else if (GNUNET_YES == fi->data.file.have_hash)
1109 else if (GNUNET_YES == fi->data.file.do_index)
1113 if (fi->keywords != NULL)
1114 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1117 if (fi->chk_uri != NULL)
1118 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1121 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1122 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1123 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1124 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1125 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1126 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1127 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1129 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value)) ||
1130 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1131 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1132 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1137 GNUNET_free_non_null (chks);
1139 GNUNET_free_non_null (ksks);
1144 case 0: /* file-insert */
1145 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1150 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1151 if (GNUNET_OK != copy_from_reader (wh, fi))
1157 case 1: /* file-index, no hash */
1158 if (NULL == fi->filename)
1163 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1169 case 2: /* file-index-with-hash */
1170 case 3: /* file-index-with-hash-confirmed */
1171 if (NULL == fi->filename)
1176 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1178 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1179 sizeof (GNUNET_HashCode))))
1185 case 4: /* directory */
1186 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1188 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1189 (uint32_t) fi->data.dir.dir_size)) ||
1191 GNUNET_BIO_write_string (wh,
1192 (fi->data.dir.entries ==
1193 NULL) ? NULL : fi->data.dir.
1194 entries->serialization)))
1205 GNUNET_BIO_write_string (wh,
1207 NULL) ? fi->next->serialization : NULL))
1212 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1221 (void) GNUNET_BIO_write_close (wh);
1222 GNUNET_free_non_null (chks);
1223 GNUNET_free_non_null (ksks);
1224 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1228 if (0 != UNLINK (fn))
1229 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1232 GNUNET_free (fi->serialization);
1233 fi->serialization = NULL;
1239 * Find the entry in the file information struct where the
1240 * serialization filename matches the given name.
1242 * @param pos file information to search
1243 * @param srch filename to search for
1244 * @return NULL if srch was not found in this subtree
1246 static struct GNUNET_FS_FileInformation *
1247 find_file_position (struct GNUNET_FS_FileInformation *pos, const char *srch)
1249 struct GNUNET_FS_FileInformation *r;
1253 if (0 == strcmp (srch, pos->serialization))
1255 if (pos->is_directory == GNUNET_YES)
1257 r = find_file_position (pos->data.dir.entries, srch);
1268 * Signal the FS's progress function that we are resuming
1271 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1272 * @param fi the entry in the publish-structure
1273 * @param length length of the file or directory
1274 * @param meta metadata for the file or directory (can be modified)
1275 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1276 * @param bo block options (can be modified)
1277 * @param do_index should we index?
1278 * @param client_info pointer to client context set upon creation (can be modified)
1279 * @return GNUNET_OK to continue (always)
1282 fip_signal_resume (void *cls, struct GNUNET_FS_FileInformation *fi,
1283 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1284 struct GNUNET_FS_Uri **uri,
1285 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1288 struct GNUNET_FS_PublishContext *pc = cls;
1289 struct GNUNET_FS_ProgressInfo pi;
1291 if (GNUNET_YES == pc->skip_next_fi_callback)
1293 pc->skip_next_fi_callback = GNUNET_NO;
1296 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1297 pi.value.publish.specifics.resume.message = pc->fi->emsg;
1298 pi.value.publish.specifics.resume.chk_uri = pc->fi->chk_uri;
1299 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1300 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1302 /* process entries in directory */
1303 pc->skip_next_fi_callback = GNUNET_YES;
1304 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1311 * Function called with a filename of serialized publishing operation
1314 * @param cls the 'struct GNUNET_FS_Handle*'
1315 * @param filename complete filename (absolute path)
1316 * @return GNUNET_OK (continue to iterate)
1319 deserialize_publish_file (void *cls, const char *filename)
1321 struct GNUNET_FS_Handle *h = cls;
1322 struct GNUNET_BIO_ReadHandle *rh;
1323 struct GNUNET_FS_PublishContext *pc;
1331 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1333 pc->serialization = get_serialization_short_name (filename);
1337 rh = GNUNET_BIO_read_open (filename);
1343 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1345 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1346 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1347 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1349 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1350 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1351 || (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)))
1356 pc->options = options;
1357 pc->all_done = all_done;
1358 if (NULL == fi_root)
1363 pc->fi = deserialize_file_information (h, fi_root);
1371 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1372 if (pc->namespace == NULL)
1374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1376 ("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1381 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1382 (GNUNET_YES != pc->all_done))
1384 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1385 if (NULL == pc->dsh)
1390 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1391 GNUNET_free (fi_pos);
1393 if (pc->fi_pos == NULL)
1395 /* failed to find position for resuming, outch! Will start from root! */
1397 if (pc->all_done != GNUNET_YES)
1398 pc->fi_pos = pc->fi;
1401 GNUNET_free (fi_root);
1403 /* generate RESUME event(s) */
1404 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1406 /* re-start publishing (if needed)... */
1407 if (pc->all_done != GNUNET_YES)
1409 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1411 GNUNET_SCHEDULER_add_with_priority
1412 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1413 &GNUNET_FS_publish_main_, pc);
1415 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1418 _("Failure while resuming publishing operation `%s': %s\n"),
1422 GNUNET_free_non_null (ns);
1423 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1426 GNUNET_free_non_null (pc->nid);
1427 GNUNET_free_non_null (pc->nuid);
1428 GNUNET_free_non_null (fi_root);
1429 GNUNET_free_non_null (fi_pos);
1430 GNUNET_free_non_null (ns);
1431 if ((rh != NULL) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1434 _("Failed to resume publishing operation `%s': %s\n"), filename,
1439 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1440 if (0 != UNLINK (filename))
1441 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1442 GNUNET_free (pc->serialization);
1449 * Synchronize this publishing struct with its mirror
1450 * on disk. Note that all internal FS-operations that change
1451 * publishing structs should already call "sync" internally,
1452 * so this function is likely not useful for clients.
1454 * @param pc the struct to sync
1457 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1459 struct GNUNET_BIO_WriteHandle *wh;
1461 if (NULL == pc->serialization)
1463 make_serialization_file_name (pc->h,
1464 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1465 if (NULL == pc->serialization)
1469 if (NULL == pc->fi->serialization)
1474 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1481 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1482 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1483 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1484 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1485 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1487 GNUNET_BIO_write_string (wh,
1489 NULL) ? NULL : pc->fi_pos->serialization)) ||
1491 GNUNET_BIO_write_string (wh,
1493 NULL) ? NULL : pc->namespace->name)))
1498 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1507 (void) GNUNET_BIO_write_close (wh);
1508 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1510 GNUNET_free (pc->serialization);
1511 pc->serialization = NULL;
1516 * Synchronize this unindex struct with its mirror
1517 * on disk. Note that all internal FS-operations that change
1518 * publishing structs should already call "sync" internally,
1519 * so this function is likely not useful for clients.
1521 * @param uc the struct to sync
1524 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1526 struct GNUNET_BIO_WriteHandle *wh;
1529 if (NULL == uc->serialization)
1531 make_serialization_file_name (uc->h,
1532 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1533 if (NULL == uc->serialization)
1535 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1542 if (NULL != uc->ksk_uri)
1543 uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
1546 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1547 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1548 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1549 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1551 GNUNET_BIO_write (wh, &uc->chk, sizeof (struct ContentHashKey))) ||
1552 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1553 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->ksk_offset)) ||
1554 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1556 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode)))) ||
1557 ((uc->state == UNINDEX_STATE_ERROR) &&
1558 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1563 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1572 (void) GNUNET_BIO_write_close (wh);
1573 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1575 GNUNET_free (uc->serialization);
1576 uc->serialization = NULL;
1581 * Serialize a download request.
1583 * @param wh the 'struct GNUNET_BIO_WriteHandle*'
1584 * @param dr the 'struct DownloadRequest'
1585 * @return GNUNET_YES on success, GNUNET_NO on error
1588 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1589 struct DownloadRequest *dr)
1593 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1594 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1595 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1596 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1598 if ((dr->state == BRS_CHK_SET) &&
1600 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1602 for (i = 0; i < dr->num_children; i++)
1603 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1610 * Read a download request tree.
1612 * @param rh stream to read from
1613 * @return value the 'struct DownloadRequest', NULL on error
1615 static struct DownloadRequest *
1616 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1618 struct DownloadRequest *dr;
1621 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1623 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1624 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1625 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1626 (dr->num_children > CHK_PER_INODE) ||
1627 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) || ((dr->depth == 0)
1631 ((dr->depth > 0) && (dr->num_children == 0)))
1634 dr->num_children = 0;
1637 if (dr->num_children > 0)
1639 GNUNET_malloc (dr->num_children * sizeof (struct ContentHashKey));
1643 case BRS_RECONSTRUCT_DOWN:
1644 case BRS_RECONSTRUCT_META_UP:
1645 case BRS_RECONSTRUCT_UP:
1649 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1652 case BRS_DOWNLOAD_DOWN:
1653 case BRS_DOWNLOAD_UP:
1660 for (i = 0; i < dr->num_children; i++)
1662 if (NULL == (dr->children[i] = read_download_request (rh)))
1664 dr->children[i]->parent = dr;
1668 GNUNET_FS_free_download_request_ (dr);
1674 * Compute the name of the sync file (or directory) for the given download
1677 * @param dc download context to compute for
1678 * @param uni unique filename to use, use "" for the directory name
1679 * @param ext extension to use, use ".dir" for our own subdirectory
1680 * @return the expanded file name, NULL for none
1683 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1684 const char *uni, const char *ext)
1689 if (dc->parent == NULL)
1690 return get_serialization_file_name (dc->h,
1693 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1694 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1696 if (dc->parent->serialization == NULL)
1698 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1701 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1708 * Synchronize this download struct with its mirror
1709 * on disk. Note that all internal FS-operations that change
1710 * publishing structs should already call "sync" internally,
1711 * so this function is likely not useful for clients.
1713 * @param dc the struct to sync
1716 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1718 struct GNUNET_BIO_WriteHandle *wh;
1723 if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
1724 return; /* we don't sync probes */
1725 if (NULL == dc->serialization)
1727 dir = get_download_sync_filename (dc, "", "");
1730 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1735 fn = GNUNET_DISK_mktemp (dir);
1739 dc->serialization = get_serialization_short_name (fn);
1743 fn = get_download_sync_filename (dc, dc->serialization, "");
1746 GNUNET_free (dc->serialization);
1747 dc->serialization = NULL;
1752 wh = GNUNET_BIO_write_open (fn);
1755 GNUNET_free (dc->serialization);
1756 dc->serialization = NULL;
1760 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1761 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1762 uris = GNUNET_FS_uri_to_string (dc->uri);
1763 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1764 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1765 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1766 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1767 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1768 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1769 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1770 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1771 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1772 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1773 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1774 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1775 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1780 if (NULL == dc->emsg)
1782 GNUNET_assert (dc->top_request != NULL);
1783 if (GNUNET_YES != write_download_request (wh, dc->top_request))
1789 GNUNET_free_non_null (uris);
1791 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1801 (void) GNUNET_BIO_write_close (wh);
1802 GNUNET_free_non_null (uris);
1803 if (0 != UNLINK (fn))
1804 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1806 GNUNET_free (dc->serialization);
1807 dc->serialization = NULL;
1812 * Synchronize this search result with its mirror
1813 * on disk. Note that all internal FS-operations that change
1814 * publishing structs should already call "sync" internally,
1815 * so this function is likely not useful for clients.
1817 * @param sr the struct to sync
1820 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1822 struct GNUNET_BIO_WriteHandle *wh;
1826 if (NULL == sr->serialization)
1828 make_serialization_file_name_in_dir (sr->sc->h,
1829 (sr->sc->psearch_result ==
1831 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1832 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1833 sr->sc->serialization);
1834 if (NULL == sr->serialization)
1836 wh = get_write_handle_in_dir (sr->sc->h,
1837 (sr->sc->psearch_result ==
1838 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1839 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1840 sr->sc->serialization, sr->serialization);
1846 uris = GNUNET_FS_uri_to_string (sr->uri);
1847 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1849 GNUNET_BIO_write_string (wh,
1851 NULL ? sr->download->serialization : NULL)) ||
1853 GNUNET_BIO_write_string (wh,
1854 sr->update_search !=
1855 NULL ? sr->update_search->serialization : NULL))
1856 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1857 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode)))
1858 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1859 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1860 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1861 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1866 if ( (sr->uri != NULL) &&
1867 (sr->sc->uri->type == ksk) &&
1868 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
1869 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
1874 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1880 GNUNET_free_non_null (uris);
1883 GNUNET_free_non_null (uris);
1885 (void) GNUNET_BIO_write_close (wh);
1886 remove_sync_file_in_dir (sr->sc->h,
1887 (sr->sc->psearch_result ==
1888 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1889 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1890 sr->sc->serialization, sr->serialization);
1891 GNUNET_free (sr->serialization);
1892 sr->serialization = NULL;
1897 * Synchronize this search struct with its mirror
1898 * on disk. Note that all internal FS-operations that change
1899 * publishing structs should already call "sync" internally,
1900 * so this function is likely not useful for clients.
1902 * @param sc the struct to sync
1905 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1907 struct GNUNET_BIO_WriteHandle *wh;
1910 const char *category;
1913 (sc->psearch_result ==
1914 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1915 GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
1916 if (NULL == sc->serialization)
1917 sc->serialization = make_serialization_file_name (sc->h, category);
1918 if (NULL == sc->serialization)
1921 wh = get_write_handle (sc->h, category, sc->serialization);
1927 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1928 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
1929 uris = GNUNET_FS_uri_to_string (sc->uri);
1930 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1931 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1932 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
1933 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
1934 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1935 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1936 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
1943 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1952 (void) GNUNET_BIO_write_close (wh);
1953 GNUNET_free_non_null (uris);
1954 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
1955 GNUNET_free (sc->serialization);
1956 sc->serialization = NULL;
1961 * Function called with a filename of serialized unindexing operation
1964 * @param cls the 'struct GNUNET_FS_Handle*'
1965 * @param filename complete filename (absolute path)
1966 * @return GNUNET_OK (continue to iterate)
1969 deserialize_unindex_file (void *cls, const char *filename)
1971 struct GNUNET_FS_Handle *h = cls;
1972 struct GNUNET_BIO_ReadHandle *rh;
1973 struct GNUNET_FS_UnindexContext *uc;
1974 struct GNUNET_FS_ProgressInfo pi;
1979 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1981 uc->serialization = get_serialization_short_name (filename);
1982 rh = GNUNET_BIO_read_open (filename);
1989 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
1990 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1991 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
1992 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)) ||
1993 (GNUNET_OK != GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof (struct ContentHashKey))) ||
1994 (GNUNET_OK != GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
1995 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &uc->ksk_offset)) )
2002 uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
2004 if (NULL == uc->ksk_uri)
2010 if ( (uc->ksk_offset > 0) &&
2011 ( (NULL == uc->ksk_uri) ||
2012 (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount) ) )
2017 uc->state = (enum UnindexState) state;
2020 case UNINDEX_STATE_HASHING:
2022 case UNINDEX_STATE_FS_NOTIFY:
2024 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
2025 sizeof (GNUNET_HashCode)))
2031 case UNINDEX_STATE_DS_REMOVE:
2032 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2033 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2035 case UNINDEX_STATE_COMPLETE:
2037 case UNINDEX_STATE_ERROR:
2039 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2049 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2050 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2051 pi.value.unindex.specifics.resume.message = uc->emsg;
2052 GNUNET_FS_unindex_make_status_ (&pi, uc,
2054 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2057 case UNINDEX_STATE_HASHING:
2059 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2061 &GNUNET_FS_unindex_process_hash_, uc);
2063 case UNINDEX_STATE_FS_NOTIFY:
2064 uc->state = UNINDEX_STATE_HASHING;
2065 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2067 case UNINDEX_STATE_DS_REMOVE:
2068 GNUNET_FS_unindex_do_remove_ (uc);
2070 case UNINDEX_STATE_EXTRACT_KEYWORDS:
2071 GNUNET_FS_unindex_do_extract_keywords_ (uc);
2073 case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
2074 GNUNET_FS_unindex_do_remove_kblocks_ (uc);
2076 case UNINDEX_STATE_COMPLETE:
2077 case UNINDEX_STATE_ERROR:
2078 /* no need to resume any operation, we were done */
2083 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2086 _("Failure while resuming unindexing operation `%s': %s\n"),
2092 GNUNET_free_non_null (uc->filename);
2093 if ((rh != NULL) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2096 _("Failed to resume unindexing operation `%s': %s\n"), filename,
2100 if (uc->serialization != NULL)
2101 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2103 GNUNET_free_non_null (uc->serialization);
2110 * Deserialize a download.
2112 * @param h overall context
2113 * @param rh file to deserialize from
2114 * @param parent parent download
2115 * @param search associated search
2116 * @param serialization name under which the search was serialized
2119 deserialize_download (struct GNUNET_FS_Handle *h,
2120 struct GNUNET_BIO_ReadHandle *rh,
2121 struct GNUNET_FS_DownloadContext *parent,
2122 struct GNUNET_FS_SearchResult *search,
2123 const char *serialization);
2127 * Deserialize a search.
2129 * @param h overall context
2130 * @param rh file to deserialize from
2131 * @param psearch_result parent search result
2132 * @param serialization name under which the search was serialized
2134 static struct GNUNET_FS_SearchContext *
2135 deserialize_search (struct GNUNET_FS_Handle *h,
2136 struct GNUNET_BIO_ReadHandle *rh,
2137 struct GNUNET_FS_SearchResult *psearch_result,
2138 const char *serialization);
2142 * Function called with a filename of serialized search result
2145 * @param cls the 'struct GNUNET_FS_SearchContext*'
2146 * @param filename complete filename (absolute path)
2147 * @return GNUNET_OK (continue to iterate)
2150 deserialize_search_result (void *cls, const char *filename)
2152 struct GNUNET_FS_SearchContext *sc = cls;
2158 struct GNUNET_BIO_ReadHandle *rh;
2159 struct GNUNET_BIO_ReadHandle *drh;
2160 struct GNUNET_FS_SearchResult *sr;
2162 ser = get_serialization_short_name (filename);
2163 rh = GNUNET_BIO_read_open (filename);
2168 remove_sync_file_in_dir (sc->h,
2169 (sc->psearch_result ==
2170 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2171 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2172 sc->serialization, ser);
2181 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2183 sr->serialization = ser;
2184 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2185 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2186 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2188 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2189 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2191 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode)))
2192 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2193 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2194 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2195 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2200 if (sr->sc->uri->type == ksk)
2202 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2203 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2205 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2212 if (download != NULL)
2214 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2217 deserialize_download (sc->h, drh, NULL, sr, download);
2218 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2221 _("Failed to resume sub-download `%s': %s\n"), download,
2226 GNUNET_free (download);
2228 if (update_srch != NULL)
2231 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2234 deserialize_search (sc->h, drh, sr, update_srch);
2235 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2238 _("Failed to resume sub-search `%s': %s\n"), update_srch,
2243 GNUNET_free (update_srch);
2245 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &sr->key, sr,
2246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2247 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2250 _("Failure while resuming search operation `%s': %s\n"),
2256 GNUNET_free_non_null (download);
2257 GNUNET_free_non_null (emsg);
2258 GNUNET_free_non_null (uris);
2259 GNUNET_free_non_null (update_srch);
2260 if (sr->uri != NULL)
2261 GNUNET_FS_uri_destroy (sr->uri);
2262 if (sr->meta != NULL)
2263 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2264 GNUNET_free (sr->serialization);
2266 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2269 _("Failure while resuming search operation `%s': %s\n"),
2278 * Send the 'resume' signal to the callback; also actually
2279 * resume the download (put it in the queue). Does this
2280 * recursively for the top-level download and all child
2283 * @param dc download to resume
2286 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2288 struct GNUNET_FS_DownloadContext *dcc;
2289 struct GNUNET_FS_ProgressInfo pi;
2291 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2292 pi.value.download.specifics.resume.meta = dc->meta;
2293 pi.value.download.specifics.resume.message = dc->emsg;
2294 GNUNET_FS_download_make_status_ (&pi, dc);
2295 dcc = dc->child_head;
2298 signal_download_resume (dcc);
2301 if (dc->pending_head != NULL)
2302 GNUNET_FS_download_start_downloading_ (dc);
2307 * Signal resuming of a search to our clients (for the
2308 * top level search and all sub-searches).
2310 * @param sc search being resumed
2313 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2317 * Iterator over search results signaling resume to the client for
2320 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2321 * @param key current key code
2322 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2323 * @return GNUNET_YES (we should continue to iterate)
2326 signal_result_resume (void *cls, const GNUNET_HashCode * key, void *value)
2328 struct GNUNET_FS_SearchContext *sc = cls;
2329 struct GNUNET_FS_ProgressInfo pi;
2330 struct GNUNET_FS_SearchResult *sr = value;
2332 if (0 == sr->mandatory_missing)
2334 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2335 pi.value.search.specifics.resume_result.meta = sr->meta;
2336 pi.value.search.specifics.resume_result.uri = sr->uri;
2337 pi.value.search.specifics.resume_result.result = sr;
2338 pi.value.search.specifics.resume_result.availability_rank =
2339 2 * sr->availability_success - sr->availability_trials;
2340 pi.value.search.specifics.resume_result.availability_certainty =
2341 sr->availability_trials;
2342 pi.value.search.specifics.resume_result.applicability_rank =
2343 sr->optional_support;
2344 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2346 if (sr->download != NULL)
2348 signal_download_resume (sr->download);
2352 GNUNET_FS_search_start_probe_ (sr);
2354 if (sr->update_search != NULL)
2355 signal_search_resume (sr->update_search);
2361 * Free memory allocated by the search context and its children
2363 * @param sc search context to free
2366 free_search_context (struct GNUNET_FS_SearchContext *sc);
2370 * Iterator over search results freeing each.
2372 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2373 * @param key current key code
2374 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2375 * @return GNUNET_YES (we should continue to iterate)
2378 free_result (void *cls, const GNUNET_HashCode * key, void *value)
2380 struct GNUNET_FS_SearchResult *sr = value;
2382 if (sr->update_search != NULL)
2384 free_search_context (sr->update_search);
2385 GNUNET_assert (NULL == sr->update_search);
2387 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2388 GNUNET_FS_uri_destroy (sr->uri);
2395 * Free memory allocated by the search context and its children
2397 * @param sc search context to free
2400 free_search_context (struct GNUNET_FS_SearchContext *sc)
2402 if (sc->serialization != NULL)
2404 GNUNET_FS_remove_sync_file_ (sc->h,
2405 (sc->psearch_result ==
2406 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2407 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2409 GNUNET_FS_remove_sync_dir_ (sc->h,
2410 (sc->psearch_result ==
2411 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2412 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2415 GNUNET_free_non_null (sc->serialization);
2416 GNUNET_free_non_null (sc->emsg);
2417 if (sc->uri != NULL)
2418 GNUNET_FS_uri_destroy (sc->uri);
2419 if (sc->master_result_map != NULL)
2421 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2423 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2430 * Function called with a filename of serialized sub-download
2433 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2434 * @param filename complete filename (absolute path)
2435 * @return GNUNET_OK (continue to iterate)
2438 deserialize_subdownload (void *cls, const char *filename)
2440 struct GNUNET_FS_DownloadContext *parent = cls;
2443 struct GNUNET_BIO_ReadHandle *rh;
2445 ser = get_serialization_short_name (filename);
2446 rh = GNUNET_BIO_read_open (filename);
2449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2451 ("Failed to resume sub-download `%s': could not open file `%s'\n"),
2456 deserialize_download (parent->h, rh, parent, NULL, ser);
2457 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2460 _("Failed to resume sub-download `%s': %s\n"), ser, emsg);
2469 * Free this download context and all of its descendants.
2470 * (only works during deserialization since not all possible
2471 * state it taken care of).
2473 * @param dc context to free
2476 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2478 struct GNUNET_FS_DownloadContext *dcc;
2480 if (dc->meta != NULL)
2481 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2482 if (dc->uri != NULL)
2483 GNUNET_FS_uri_destroy (dc->uri);
2484 GNUNET_free_non_null (dc->temp_filename);
2485 GNUNET_free_non_null (dc->emsg);
2486 GNUNET_free_non_null (dc->filename);
2487 GNUNET_free_non_null (dc->serialization);
2488 while (NULL != (dcc = dc->child_head))
2490 GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
2491 free_download_context (dcc);
2493 GNUNET_FS_free_download_request_ (dc->top_request);
2494 if (NULL != dc->active)
2495 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2501 * Deserialize a download.
2503 * @param h overall context
2504 * @param rh file to deserialize from
2505 * @param parent parent download
2506 * @param search associated search
2507 * @param serialization name under which the search was serialized
2510 deserialize_download (struct GNUNET_FS_Handle *h,
2511 struct GNUNET_BIO_ReadHandle *rh,
2512 struct GNUNET_FS_DownloadContext *parent,
2513 struct GNUNET_FS_SearchResult *search,
2514 const char *serialization)
2516 struct GNUNET_FS_DownloadContext *dc;
2525 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2526 dc->parent = parent;
2528 dc->serialization = GNUNET_strdup (serialization);
2530 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2531 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2532 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2533 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2534 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2536 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2538 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2540 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2542 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2543 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2544 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2545 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2546 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2547 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2548 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2549 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2554 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2556 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE));
2557 dc->has_finished = (int) status;
2559 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2560 if (GNUNET_FS_uri_test_loc (dc->uri))
2561 GNUNET_assert (GNUNET_OK ==
2562 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2563 if (dc->emsg == NULL)
2565 dc->top_request = read_download_request (rh);
2566 if (dc->top_request == NULL)
2572 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2575 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2576 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2581 GNUNET_abort (); // for debugging for now - FIXME
2582 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2586 dc->search = search;
2587 search->download = dc;
2589 if ((parent == NULL) && (search == NULL))
2592 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2593 signal_download_resume (dc);
2596 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2599 GNUNET_free_non_null (uris);
2600 GNUNET_free_non_null (emsg);
2601 free_download_context (dc);
2606 * Signal resuming of a search to our clients (for the
2607 * top level search and all sub-searches).
2609 * @param sc search being resumed
2612 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2614 struct GNUNET_FS_ProgressInfo pi;
2616 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2617 pi.value.search.specifics.resume.message = sc->emsg;
2618 pi.value.search.specifics.resume.is_paused =
2619 (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2620 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2621 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2622 &signal_result_resume, sc);
2628 * Deserialize a search.
2630 * @param h overall context
2631 * @param rh file to deserialize from
2632 * @param psearch_result parent search result
2633 * @param serialization name under which the search was serialized
2635 static struct GNUNET_FS_SearchContext *
2636 deserialize_search (struct GNUNET_FS_Handle *h,
2637 struct GNUNET_BIO_ReadHandle *rh,
2638 struct GNUNET_FS_SearchResult *psearch_result,
2639 const char *serialization)
2641 struct GNUNET_FS_SearchContext *sc;
2648 if ((psearch_result != NULL) && (psearch_result->update_search != NULL))
2655 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2656 if (psearch_result != NULL)
2658 sc->psearch_result = psearch_result;
2659 psearch_result->update_search = sc;
2662 sc->serialization = GNUNET_strdup (serialization);
2663 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024))
2664 || (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2665 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2666 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2667 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2669 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2670 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2672 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2673 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2678 sc->options = (enum GNUNET_FS_SearchOptions) options;
2679 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2680 dn = get_serialization_file_name_in_dir (h,
2681 (sc->psearch_result ==
2683 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2684 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2685 sc->serialization, "");
2688 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2689 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2692 if (('\0' == in_pause) &&
2693 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2697 ("Could not resume running search, will resume as paused search\n"));
2699 signal_search_resume (sc);
2703 GNUNET_free_non_null (emsg);
2704 free_search_context (sc);
2705 GNUNET_free_non_null (uris);
2711 * Function called with a filename of serialized search operation
2714 * @param cls the 'struct GNUNET_FS_Handle*'
2715 * @param filename complete filename (absolute path)
2716 * @return GNUNET_OK (continue to iterate)
2719 deserialize_search_file (void *cls, const char *filename)
2721 struct GNUNET_FS_Handle *h = cls;
2724 struct GNUNET_BIO_ReadHandle *rh;
2725 struct GNUNET_FS_SearchContext *sc;
2728 if (0 != STAT (filename, &buf))
2730 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2733 if (S_ISDIR (buf.st_mode))
2734 return GNUNET_OK; /* skip directories */
2735 ser = get_serialization_short_name (filename);
2736 rh = GNUNET_BIO_read_open (filename);
2741 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2746 sc = deserialize_search (h, rh, NULL, ser);
2748 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2750 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2753 _("Failure while resuming search operation `%s': %s\n"),
2762 * Function called with a filename of serialized download operation
2765 * @param cls the 'struct GNUNET_FS_Handle*'
2766 * @param filename complete filename (absolute path)
2767 * @return GNUNET_OK (continue to iterate)
2770 deserialize_download_file (void *cls, const char *filename)
2772 struct GNUNET_FS_Handle *h = cls;
2775 struct GNUNET_BIO_ReadHandle *rh;
2777 ser = get_serialization_short_name (filename);
2778 rh = GNUNET_BIO_read_open (filename);
2781 if (0 != UNLINK (filename))
2782 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
2786 deserialize_download (h, rh, NULL, NULL, ser);
2788 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2791 _("Failure while resuming download operation `%s': %s\n"),
2800 * Deserialize informatin about pending operations.
2802 * @param master_path which master directory should be scanned
2803 * @param proc function to call for each entry (will get 'h' for 'cls')
2804 * @param h the 'struct GNUNET_FS_Handle*'
2807 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
2808 struct GNUNET_FS_Handle *h)
2812 dn = get_serialization_file_name (h, master_path, "");
2815 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2816 GNUNET_DISK_directory_scan (dn, proc, h);
2822 * Setup a connection to the file-sharing service.
2824 * @param cfg configuration to use
2825 * @param client_name unique identifier for this client
2826 * @param upcb function to call to notify about FS actions
2827 * @param upcb_cls closure for upcb
2828 * @param flags specific attributes for fs-operations
2829 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2830 * @return NULL on error
2832 struct GNUNET_FS_Handle *
2833 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2834 const char *client_name, GNUNET_FS_ProgressCallback upcb,
2835 void *upcb_cls, enum GNUNET_FS_Flags flags, ...)
2837 struct GNUNET_FS_Handle *ret;
2838 enum GNUNET_FS_OPTIONS opt;
2841 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2843 ret->client_name = GNUNET_strdup (client_name);
2845 ret->upcb_cls = upcb_cls;
2847 ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
2848 ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
2849 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2850 va_start (ap, flags);
2851 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2855 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2856 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2859 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2860 ret->max_parallel_requests = va_arg (ap, unsigned int);
2865 GNUNET_free (ret->client_name);
2872 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2874 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
2875 &deserialize_publish_file, ret);
2876 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
2877 &deserialize_search_file, ret);
2878 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2879 &deserialize_download_file, ret);
2880 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2881 &deserialize_unindex_file, ret);
2888 * Close our connection with the file-sharing service.
2889 * The callback given to GNUNET_FS_start will no longer be
2890 * called after this function returns.
2892 * @param h handle that was returned from GNUNET_FS_start
2895 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2897 while (h->top_head != NULL)
2898 h->top_head->ssf (h->top_head->ssf_cls);
2899 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2900 GNUNET_SCHEDULER_cancel (h->queue_job);
2901 GNUNET_free (h->client_name);