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"
35 * Start the given job (send signal, remove from pending queue, update
36 * counters and state).
38 * @param qe job to start
41 start_job (struct GNUNET_FS_QueueEntry *qe)
43 GNUNET_assert (NULL == qe->client);
44 qe->client = GNUNET_CLIENT_connect ("fs", qe->h->cfg);
45 if (qe->client == NULL)
50 qe->start (qe->cls, qe->client);
52 qe->h->active_blocks += qe->blocks;
53 qe->start_time = GNUNET_TIME_absolute_get ();
54 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head, qe->h->pending_tail, qe);
55 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head, qe->h->running_tail,
56 qe->h->running_tail, qe);
61 * Stop the given job (send signal, remove from active queue, update
62 * counters and state).
64 * @param qe job to stop
67 stop_job (struct GNUNET_FS_QueueEntry *qe)
71 qe->h->active_downloads--;
72 qe->h->active_blocks -= qe->blocks;
74 GNUNET_TIME_relative_add (qe->run_time,
75 GNUNET_TIME_absolute_get_duration
77 GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
78 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head, qe->h->pending_tail,
79 qe->h->pending_tail, qe);
84 * Process the jobs in the job queue, possibly starting some
85 * and stopping others.
87 * @param cls the 'struct GNUNET_FS_Handle'
88 * @param tc scheduler context
91 process_job_queue (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 struct GNUNET_FS_Handle *h = cls;
94 struct GNUNET_FS_QueueEntry *qe;
95 struct GNUNET_FS_QueueEntry *next;
96 struct GNUNET_TIME_Relative run_time;
97 struct GNUNET_TIME_Relative restart_at;
98 struct GNUNET_TIME_Relative rst;
99 struct GNUNET_TIME_Absolute end_time;
101 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
102 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
103 /* first, see if we can start all the jobs */
104 next = h->pending_head;
105 while (NULL != (qe = next))
108 if (h->running_head == NULL)
113 if ((qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
114 (h->active_downloads < h->max_parallel_downloads))
120 if (h->pending_head == NULL)
121 return; /* no need to stop anything */
122 /* then, check if we should stop some jobs */
123 next = h->running_head;
124 while (NULL != (qe = next))
128 GNUNET_TIME_relative_multiply (h->avg_block_latency,
129 qe->blocks * qe->start_times);
130 switch (qe->priority)
132 case GNUNET_FS_QUEUE_PRIORITY_PROBE:
133 /* run probes for at most 1s * number-of-restarts; note that
134 as the total runtime of a probe is limited to 2m, we don't
135 need to additionally limit the total time of a probe to
136 strictly limit its lifetime. */
137 run_time = GNUNET_TIME_relative_min (run_time,
138 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
139 1 + qe->start_times));
141 case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
146 end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
147 rst = GNUNET_TIME_absolute_get_remaining (end_time);
148 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
149 if (rst.rel_value > 0)
153 /* finally, start some more tasks if we now have empty slots */
154 next = h->pending_head;
155 while (NULL != (qe = next))
158 if ((qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
159 (h->active_downloads < h->max_parallel_downloads))
166 GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
171 * Add a job to the queue.
173 * @param h handle to the overall FS state
174 * @param start function to call to begin the job
175 * @param stop function to call to pause the job, or on dequeue (if the job was running)
176 * @param cls closure for start and stop
177 * @param blocks number of blocks this jobs uses
178 * @param priority how important is this download
179 * @return queue handle
181 struct GNUNET_FS_QueueEntry *
182 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h, GNUNET_FS_QueueStart start,
183 GNUNET_FS_QueueStop stop, void *cls, unsigned int blocks,
184 enum GNUNET_FS_QueuePriority priority)
186 struct GNUNET_FS_QueueEntry *qe;
188 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
193 qe->queue_time = GNUNET_TIME_absolute_get ();
195 qe->priority = priority;
196 GNUNET_CONTAINER_DLL_insert_after (h->pending_head, h->pending_tail,
197 h->pending_tail, qe);
198 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
199 GNUNET_SCHEDULER_cancel (h->queue_job);
200 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
206 * Dequeue a job from the queue.
207 * @param qh handle for the job
210 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
212 struct GNUNET_FS_Handle *h;
215 if (qh->client != NULL)
217 GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qh);
219 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
220 GNUNET_SCHEDULER_cancel (h->queue_job);
221 h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
226 * Create a top-level activity entry.
228 * @param h global fs handle
229 * @param ssf suspend signal function to use
230 * @param ssf_cls closure for ssf
231 * @return fresh top-level activity handle
233 struct TopLevelActivity *
234 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h, SuspendSignalFunction ssf,
237 struct TopLevelActivity *ret;
239 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
241 ret->ssf_cls = ssf_cls;
242 GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
248 * Destroy a top-level activity entry.
250 * @param h global fs handle
251 * @param top top level activity entry
254 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top)
256 GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
263 * Closure for "data_reader_file".
268 * Name of the file to read.
273 * File descriptor, NULL if it has not yet been opened.
275 struct GNUNET_DISK_FileHandle *fd;
280 * Function that provides data by reading from a file.
282 * @param cls closure (points to the file information)
283 * @param offset offset to read from; it is possible
284 * that the caller might need to go backwards
286 * @param max maximum number of bytes that should be
287 * copied to buf; readers are not allowed
288 * to provide less data unless there is an error;
289 * a value of "0" will be used at the end to allow
290 * the reader to clean up its internal state
291 * @param buf where the reader should write the data
292 * @param emsg location for the reader to store an error message
293 * @return number of bytes written, usually "max", 0 on error
296 GNUNET_FS_data_reader_file_ (void *cls, uint64_t offset, size_t max, void *buf,
299 struct FileInfo *fi = cls;
305 GNUNET_DISK_file_close (fi->fd);
306 GNUNET_free (fi->filename);
313 GNUNET_DISK_file_open (fi->filename, GNUNET_DISK_OPEN_READ,
314 GNUNET_DISK_PERM_NONE);
317 GNUNET_asprintf (emsg, _("Could not open file `%s': %s"), fi->filename,
322 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
323 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
326 GNUNET_asprintf (emsg, _("Could not read file `%s': %s"), fi->filename,
332 GNUNET_asprintf (emsg, _("Short read reading from file `%s'!"),
341 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
343 * @param filename file to read
344 * @return closure to use, NULL on error
347 GNUNET_FS_make_file_reader_context_ (const char *filename)
351 fi = GNUNET_malloc (sizeof (struct FileInfo));
352 fi->filename = GNUNET_STRINGS_filename_expand (filename);
353 if (fi->filename == NULL)
363 * Function that provides data by copying from a buffer.
365 * @param cls closure (points to the buffer)
366 * @param offset offset to read from; it is possible
367 * that the caller might need to go backwards
369 * @param max maximum number of bytes that should be
370 * copied to buf; readers are not allowed
371 * to provide less data unless there is an error;
372 * a value of "0" will be used at the end to allow
373 * the reader to clean up its internal state
374 * @param buf where the reader should write the data
375 * @param emsg location for the reader to store an error message
376 * @return number of bytes written, usually "max", 0 on error
379 GNUNET_FS_data_reader_copy_ (void *cls, uint64_t offset, size_t max, void *buf,
386 GNUNET_free_non_null (data);
389 memcpy (buf, &data[offset], max);
395 * Return the full filename where we would store state information
396 * (for serialization/deserialization).
398 * @param h master context
399 * @param ext component of the path
400 * @param ent entity identifier (or emtpy string for the directory)
401 * @return NULL on error
404 get_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext,
410 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
411 return NULL; /* persistence not requested */
413 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
416 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s", basename, DIR_SEPARATOR_STR,
417 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
419 GNUNET_free (basename);
425 * Return the full filename where we would store state information
426 * (for serialization/deserialization) that is associated with a
429 * @param h master context
430 * @param ext component of the path
431 * @param uni name of the parent operation
432 * @param ent entity identifier (or emtpy string for the directory)
433 * @return NULL on error
436 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
437 const char *uni, const char *ent)
442 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
443 return NULL; /* persistence not requested */
445 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "fs", "STATE_DIR",
448 GNUNET_asprintf (&ret, "%s%s%s%s%s%s%s.dir%s%s", basename, DIR_SEPARATOR_STR,
449 h->client_name, DIR_SEPARATOR_STR, ext, DIR_SEPARATOR_STR,
450 uni, DIR_SEPARATOR_STR, ent);
451 GNUNET_free (basename);
457 * Return a read handle for deserialization.
459 * @param h master context
460 * @param ext component of the path
461 * @param ent entity identifier (or emtpy string for the directory)
462 * @return NULL on error
464 static struct GNUNET_BIO_ReadHandle *
465 get_read_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
468 struct GNUNET_BIO_ReadHandle *ret;
470 fn = get_serialization_file_name (h, ext, ent);
473 ret = GNUNET_BIO_read_open (fn);
480 * Return a write handle for serialization.
482 * @param h master context
483 * @param ext component of the path
484 * @param ent entity identifier (or emtpy string for the directory)
485 * @return NULL on error
487 static struct GNUNET_BIO_WriteHandle *
488 get_write_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
491 struct GNUNET_BIO_WriteHandle *ret;
493 fn = get_serialization_file_name (h, ext, ent);
498 ret = GNUNET_BIO_write_open (fn);
507 * Return a write handle for serialization.
509 * @param h master context
510 * @param ext component of the path
511 * @param uni name of parent
512 * @param ent entity identifier (or emtpy string for the directory)
513 * @return NULL on error
515 static struct GNUNET_BIO_WriteHandle *
516 get_write_handle_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
517 const char *uni, const char *ent)
520 struct GNUNET_BIO_WriteHandle *ret;
522 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
525 ret = GNUNET_BIO_write_open (fn);
532 * Remove serialization/deserialization file from disk.
534 * @param h master context
535 * @param ext component of the path
536 * @param ent entity identifier
539 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h, const char *ext,
544 if ((NULL == ent) || (0 == strlen (ent)))
549 filename = get_serialization_file_name (h, ext, ent);
550 if (filename != NULL)
552 if (0 != UNLINK (filename))
553 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
554 GNUNET_free (filename);
560 * Remove serialization/deserialization file from disk.
562 * @param h master context
563 * @param ext component of the path
564 * @param uni parent name
565 * @param ent entity identifier
568 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h, const char *ext,
569 const char *uni, const char *ent)
573 if ((NULL == ent) || (0 == strlen (ent)))
578 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
579 if (filename != NULL)
581 if (0 != UNLINK (filename))
582 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
583 GNUNET_free (filename);
589 * Remove serialization/deserialization directory from disk.
591 * @param h master context
592 * @param ext component of the path
593 * @param uni unique name of parent
596 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h, const char *ext,
603 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
606 if ((GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
607 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
608 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
614 * Serialize a 'start_time'. Since we use start-times to
615 * calculate the duration of some operation, we actually
616 * do not serialize the absolute time but the (relative)
617 * duration since the start time. When we then
618 * deserialize the start time, we take the current time and
619 * subtract that duration so that we get again an absolute
620 * time stamp that will result in correct performance
623 * @param wh handle for writing
624 * @param timestamp time to serialize
625 * @return GNUNET_OK on success
628 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
629 struct GNUNET_TIME_Absolute timestamp)
631 struct GNUNET_TIME_Relative dur;
633 dur = GNUNET_TIME_absolute_get_duration (timestamp);
634 return GNUNET_BIO_write_int64 (wh, dur.rel_value);
639 * Serialize a 'start_time'. Since we use start-times to
640 * calculate the duration of some operation, we actually
641 * do not serialize the absolute time but the (relative)
642 * duration since the start time. When we then
643 * deserialize the start time, we take the current time and
644 * subtract that duration so that we get again an absolute
645 * time stamp that will result in correct performance
648 * @param rh handle for reading
649 * @param timestamp where to write the deserialized timestamp
650 * @return GNUNET_OK on success
653 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
654 struct GNUNET_TIME_Absolute *timestamp)
656 struct GNUNET_TIME_Relative dur;
658 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dur.rel_value))
659 return GNUNET_SYSERR;
660 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
666 * Using the given serialization filename, try to deserialize
667 * the file-information tree associated with it.
669 * @param h master context
670 * @param filename name of the file (without directory) with
672 * @return NULL on error
674 static struct GNUNET_FS_FileInformation *
675 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename);
679 * Using the given serialization filename, try to deserialize
680 * the file-information tree associated with it.
682 * @param h master context
683 * @param fn name of the file (without directory) with
685 * @param rh handle for reading
686 * @return NULL on error
688 static struct GNUNET_FS_FileInformation *
689 deserialize_fi_node (struct GNUNET_FS_Handle *h, const char *fn,
690 struct GNUNET_BIO_ReadHandle *rh)
692 struct GNUNET_FS_FileInformation *ret;
693 struct GNUNET_FS_FileInformation *nxt;
700 if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof (b)))
705 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
710 if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
711 (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
713 ( (NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
714 (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)) ) ) ||
715 (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
717 ( (NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
718 (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri))) ) ||
719 (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
720 (GNUNET_OK != GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024))
722 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
724 GNUNET_BIO_read_int64 (rh, &ret->bo.expiration_time.abs_value)) ||
725 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.anonymity_level)) ||
726 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.content_priority)) ||
727 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &ret->bo.replication_level)))
734 case 0: /* file-insert */
735 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
740 ret->is_directory = GNUNET_NO;
741 ret->data.file.do_index = GNUNET_NO;
742 ret->data.file.have_hash = GNUNET_NO;
743 ret->data.file.index_start_confirmed = GNUNET_NO;
744 if (GNUNET_NO == ret->is_published)
746 if (NULL == ret->filename)
748 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
749 ret->data.file.reader_cls =
750 GNUNET_malloc_large (ret->data.file.file_size);
751 if (ret->data.file.reader_cls == NULL)
754 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls,
755 ret->data.file.file_size))
763 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
764 ret->data.file.reader_cls =
765 GNUNET_FS_make_file_reader_context_ (ret->filename);
769 case 1: /* file-index, no hash */
770 if (NULL == ret->filename)
775 if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
780 ret->is_directory = GNUNET_NO;
781 ret->data.file.do_index = GNUNET_YES;
782 ret->data.file.have_hash = GNUNET_NO;
783 ret->data.file.index_start_confirmed = GNUNET_NO;
784 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
785 ret->data.file.reader_cls =
786 GNUNET_FS_make_file_reader_context_ (ret->filename);
788 case 2: /* file-index-with-hash */
789 if (NULL == ret->filename)
794 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
796 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
797 sizeof (GNUNET_HashCode))))
802 ret->is_directory = GNUNET_NO;
803 ret->data.file.do_index = GNUNET_YES;
804 ret->data.file.have_hash = GNUNET_YES;
805 ret->data.file.index_start_confirmed = GNUNET_NO;
806 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
807 ret->data.file.reader_cls =
808 GNUNET_FS_make_file_reader_context_ (ret->filename);
810 case 3: /* file-index-with-hash-confirmed */
811 if (NULL == ret->filename)
816 if ((GNUNET_OK != GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
818 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id,
819 sizeof (GNUNET_HashCode))))
824 ret->is_directory = GNUNET_NO;
825 ret->data.file.do_index = GNUNET_YES;
826 ret->data.file.have_hash = GNUNET_YES;
827 ret->data.file.index_start_confirmed = GNUNET_YES;
828 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
829 ret->data.file.reader_cls =
830 GNUNET_FS_make_file_reader_context_ (ret->filename);
832 case 4: /* directory */
833 ret->is_directory = GNUNET_YES;
834 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dsize)) ||
835 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
837 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
839 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
844 ret->data.dir.dir_size = (uint32_t) dsize;
845 if (filename != NULL)
847 ret->data.dir.entries = deserialize_file_information (h, filename);
848 GNUNET_free (filename);
850 nxt = ret->data.dir.entries;
862 ret->serialization = GNUNET_strdup (fn);
864 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
869 if (filename != NULL)
871 ret->next = deserialize_file_information (h, filename);
872 GNUNET_free (filename);
875 GNUNET_free_non_null (ksks);
876 GNUNET_free_non_null (chks);
879 GNUNET_free_non_null (ksks);
880 GNUNET_free_non_null (chks);
881 GNUNET_free_non_null (filename);
882 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
888 * Using the given serialization filename, try to deserialize
889 * the file-information tree associated with it.
891 * @param h master context
892 * @param filename name of the file (without directory) with
894 * @return NULL on error
896 static struct GNUNET_FS_FileInformation *
897 deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename)
899 struct GNUNET_FS_FileInformation *ret;
900 struct GNUNET_BIO_ReadHandle *rh;
903 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
906 ret = deserialize_fi_node (h, filename, rh);
907 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
909 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
910 _("Failed to resume publishing information `%s': %s\n"),
916 if (0 != UNLINK (filename))
917 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
924 * Given a serialization name (full absolute path), return the
925 * basename of the file (without the path), which must only
926 * consist of the 6 random characters.
928 * @param fullname name to extract the basename from
929 * @return copy of the basename, NULL on error
932 get_serialization_short_name (const char *fullname)
939 /* FIXME: we could do this faster since we know
940 * the length of 'end'... */
943 if (DIR_SEPARATOR == *nxt)
947 if ((end == NULL) || (strlen (end) == 0))
952 GNUNET_break (6 == strlen (end));
953 return GNUNET_strdup (end);
958 * Create a new random name for serialization. Also checks if persistence
959 * is enabled and returns NULL if not.
961 * @param h master context
962 * @param ext component of the path
963 * @return NULL on errror
966 make_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext)
972 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
973 return NULL; /* persistence not requested */
974 dn = get_serialization_file_name (h, ext, "");
977 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
982 fn = GNUNET_DISK_mktemp (dn);
985 return NULL; /* epic fail */
986 ret = get_serialization_short_name (fn);
993 * Create a new random name for serialization. Also checks if persistence
994 * is enabled and returns NULL if not.
996 * @param h master context
997 * @param ext component of the path
998 * @param uni name of parent
999 * @return NULL on errror
1002 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1003 const char *ext, const char *uni)
1009 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1010 return NULL; /* persistence not requested */
1011 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1014 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
1019 fn = GNUNET_DISK_mktemp (dn);
1022 return NULL; /* epic fail */
1023 ret = get_serialization_short_name (fn);
1030 * Copy all of the data from the reader to the write handle.
1032 * @param wh write handle
1033 * @param fi file with reader
1034 * @return GNUNET_OK on success
1037 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1038 struct GNUNET_FS_FileInformation *fi)
1040 char buf[32 * 1024];
1048 while (off < fi->data.file.file_size)
1050 left = GNUNET_MIN (sizeof (buf), fi->data.file.file_size - off);
1052 fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
1056 return GNUNET_SYSERR;
1058 if (GNUNET_OK != GNUNET_BIO_write (wh, buf, ret))
1059 return GNUNET_SYSERR;
1067 * Create a temporary file on disk to store the current
1070 * @param fi file information to sync with disk
1073 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
1076 struct GNUNET_BIO_WriteHandle *wh;
1081 if (NULL == fi->serialization)
1083 make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1084 if (NULL == fi->serialization)
1086 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1090 GNUNET_free (fi->serialization);
1091 fi->serialization = NULL;
1094 if (GNUNET_YES == fi->is_directory)
1096 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1098 else if (GNUNET_YES == fi->data.file.have_hash)
1100 else if (GNUNET_YES == fi->data.file.do_index)
1104 if (fi->keywords != NULL)
1105 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1108 if (fi->chk_uri != NULL)
1109 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1112 if ((GNUNET_OK != GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1113 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1114 (GNUNET_OK != GNUNET_BIO_write_string (wh, ksks)) ||
1115 (GNUNET_OK != GNUNET_BIO_write_string (wh, chks)) ||
1116 (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
1117 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->emsg)) ||
1118 (GNUNET_OK != GNUNET_BIO_write_string (wh, fi->filename)) ||
1120 GNUNET_BIO_write_int64 (wh, fi->bo.expiration_time.abs_value)) ||
1121 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.anonymity_level)) ||
1122 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.content_priority)) ||
1123 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->bo.replication_level)))
1128 GNUNET_free_non_null (chks);
1130 GNUNET_free_non_null (ksks);
1135 case 0: /* file-insert */
1136 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1141 if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
1142 if (GNUNET_OK != copy_from_reader (wh, fi))
1148 case 1: /* file-index, no hash */
1149 if (NULL == fi->filename)
1154 if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1160 case 2: /* file-index-with-hash */
1161 case 3: /* file-index-with-hash-confirmed */
1162 if (NULL == fi->filename)
1167 if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1169 GNUNET_BIO_write (wh, &fi->data.file.file_id,
1170 sizeof (GNUNET_HashCode))))
1176 case 4: /* directory */
1177 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1179 GNUNET_BIO_write (wh, fi->data.dir.dir_data,
1180 (uint32_t) fi->data.dir.dir_size)) ||
1182 GNUNET_BIO_write_string (wh,
1183 (fi->data.dir.entries ==
1184 NULL) ? NULL : fi->data.dir.
1185 entries->serialization)))
1196 GNUNET_BIO_write_string (wh,
1198 NULL) ? fi->next->serialization : NULL))
1203 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1212 (void) GNUNET_BIO_write_close (wh);
1213 GNUNET_free_non_null (chks);
1214 GNUNET_free_non_null (ksks);
1215 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO,
1219 if (0 != UNLINK (fn))
1220 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1223 GNUNET_free (fi->serialization);
1224 fi->serialization = NULL;
1230 * Find the entry in the file information struct where the
1231 * serialization filename matches the given name.
1233 * @param pos file information to search
1234 * @param srch filename to search for
1235 * @return NULL if srch was not found in this subtree
1237 static struct GNUNET_FS_FileInformation *
1238 find_file_position (struct GNUNET_FS_FileInformation *pos, const char *srch)
1240 struct GNUNET_FS_FileInformation *r;
1244 if (0 == strcmp (srch, pos->serialization))
1246 if (pos->is_directory == GNUNET_YES)
1248 r = find_file_position (pos->data.dir.entries, srch);
1259 * Signal the FS's progress function that we are resuming
1262 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1263 * @param fi the entry in the publish-structure
1264 * @param length length of the file or directory
1265 * @param meta metadata for the file or directory (can be modified)
1266 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1267 * @param bo block options (can be modified)
1268 * @param do_index should we index?
1269 * @param client_info pointer to client context set upon creation (can be modified)
1270 * @return GNUNET_OK to continue (always)
1273 fip_signal_resume (void *cls, struct GNUNET_FS_FileInformation *fi,
1274 uint64_t length, struct GNUNET_CONTAINER_MetaData *meta,
1275 struct GNUNET_FS_Uri **uri,
1276 struct GNUNET_FS_BlockOptions *bo, int *do_index,
1279 struct GNUNET_FS_PublishContext *pc = cls;
1280 struct GNUNET_FS_ProgressInfo pi;
1282 if (GNUNET_YES == pc->skip_next_fi_callback)
1284 pc->skip_next_fi_callback = GNUNET_NO;
1287 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1288 pi.value.publish.specifics.resume.message = pc->fi->emsg;
1289 pi.value.publish.specifics.resume.chk_uri = pc->fi->chk_uri;
1290 *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
1291 if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
1293 /* process entries in directory */
1294 pc->skip_next_fi_callback = GNUNET_YES;
1295 GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
1302 * Function called with a filename of serialized publishing operation
1305 * @param cls the 'struct GNUNET_FS_Handle*'
1306 * @param filename complete filename (absolute path)
1307 * @return GNUNET_OK (continue to iterate)
1310 deserialize_publish_file (void *cls, const char *filename)
1312 struct GNUNET_FS_Handle *h = cls;
1313 struct GNUNET_BIO_ReadHandle *rh;
1314 struct GNUNET_FS_PublishContext *pc;
1322 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1324 pc->serialization = get_serialization_short_name (filename);
1328 rh = GNUNET_BIO_read_open (filename);
1334 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024))
1336 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1337 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
1338 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &all_done)) ||
1340 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1341 (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128))
1342 || (GNUNET_OK != GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)))
1347 pc->options = options;
1348 pc->all_done = all_done;
1349 if (NULL == fi_root)
1354 pc->fi = deserialize_file_information (h, fi_root);
1362 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1363 if (pc->namespace == NULL)
1365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1367 ("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1372 if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
1373 (GNUNET_YES != pc->all_done))
1375 pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
1376 if (NULL == pc->dsh)
1381 pc->fi_pos = find_file_position (pc->fi, fi_pos);
1382 GNUNET_free (fi_pos);
1384 if (pc->fi_pos == NULL)
1386 /* failed to find position for resuming, outch! Will start from root! */
1388 if (pc->all_done != GNUNET_YES)
1389 pc->fi_pos = pc->fi;
1392 GNUNET_free (fi_root);
1394 /* generate RESUME event(s) */
1395 GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
1397 /* re-start publishing (if needed)... */
1398 if (pc->all_done != GNUNET_YES)
1400 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pc->upload_task);
1402 GNUNET_SCHEDULER_add_with_priority
1403 (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1404 &GNUNET_FS_publish_main_, pc);
1406 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
1408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1409 _("Failure while resuming publishing operation `%s': %s\n"),
1413 GNUNET_free_non_null (ns);
1414 pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
1417 GNUNET_free_non_null (pc->nid);
1418 GNUNET_free_non_null (pc->nuid);
1419 GNUNET_free_non_null (fi_root);
1420 GNUNET_free_non_null (fi_pos);
1421 GNUNET_free_non_null (ns);
1422 if ((rh != NULL) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
1424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1425 _("Failed to resume publishing operation `%s': %s\n"), filename,
1430 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1431 if (0 != UNLINK (filename))
1432 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1433 GNUNET_free (pc->serialization);
1440 * Synchronize this publishing struct with its mirror
1441 * on disk. Note that all internal FS-operations that change
1442 * publishing structs should already call "sync" internally,
1443 * so this function is likely not useful for clients.
1445 * @param pc the struct to sync
1448 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1450 struct GNUNET_BIO_WriteHandle *wh;
1452 if (NULL == pc->serialization)
1454 make_serialization_file_name (pc->h,
1455 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1456 if (NULL == pc->serialization)
1460 if (NULL == pc->fi->serialization)
1465 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1472 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nid)) ||
1473 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->nuid)) ||
1474 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->options)) ||
1475 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1476 (GNUNET_OK != GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1478 GNUNET_BIO_write_string (wh,
1480 NULL) ? NULL : pc->fi_pos->serialization)) ||
1482 GNUNET_BIO_write_string (wh,
1484 NULL) ? NULL : pc->namespace->name)))
1489 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1498 (void) GNUNET_BIO_write_close (wh);
1499 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
1501 GNUNET_free (pc->serialization);
1502 pc->serialization = NULL;
1507 * Synchronize this unindex struct with its mirror
1508 * on disk. Note that all internal FS-operations that change
1509 * publishing structs should already call "sync" internally,
1510 * so this function is likely not useful for clients.
1512 * @param uc the struct to sync
1515 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1517 struct GNUNET_BIO_WriteHandle *wh;
1519 if (NULL == uc->serialization)
1521 make_serialization_file_name (uc->h,
1522 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1523 if (NULL == uc->serialization)
1525 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1532 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uc->filename)) ||
1533 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1534 (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
1535 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1536 ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1538 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode)))) ||
1539 ((uc->state == UNINDEX_STATE_ERROR) &&
1540 (GNUNET_OK != GNUNET_BIO_write_string (wh, uc->emsg))))
1545 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1554 (void) GNUNET_BIO_write_close (wh);
1555 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
1557 GNUNET_free (uc->serialization);
1558 uc->serialization = NULL;
1563 * Serialize a download request.
1565 * @param wh the 'struct GNUNET_BIO_WriteHandle*'
1566 * @param dr the 'struct DownloadRequest'
1567 * @return GNUNET_YES on success, GNUNET_NO on error
1570 write_download_request (struct GNUNET_BIO_WriteHandle *wh,
1571 struct DownloadRequest *dr)
1575 if ((GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->state)) ||
1576 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1577 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->num_children)) ||
1578 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dr->depth)))
1580 if ((dr->state == BRS_CHK_SET) &&
1582 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))))
1584 for (i = 0; i < dr->num_children; i++)
1585 if (GNUNET_NO == write_download_request (wh, dr->children[i]))
1592 * Read a download request tree.
1594 * @param rh stream to read from
1595 * @return value the 'struct DownloadRequest', NULL on error
1597 static struct DownloadRequest *
1598 read_download_request (struct GNUNET_BIO_ReadHandle *rh)
1600 struct DownloadRequest *dr;
1603 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1605 if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->state)) ||
1606 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1607 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->num_children)) ||
1608 (dr->num_children > CHK_PER_INODE) ||
1609 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dr->depth)) || ((dr->depth == 0)
1613 ((dr->depth > 0) && (dr->num_children == 0)))
1616 dr->num_children = 0;
1619 if (dr->num_children > 0)
1621 GNUNET_malloc (dr->num_children * sizeof (struct ContentHashKey));
1625 case BRS_RECONSTRUCT_DOWN:
1626 case BRS_RECONSTRUCT_META_UP:
1627 case BRS_RECONSTRUCT_UP:
1631 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey)))
1634 case BRS_DOWNLOAD_DOWN:
1635 case BRS_DOWNLOAD_UP:
1642 for (i = 0; i < dr->num_children; i++)
1644 if (NULL == (dr->children[i] = read_download_request (rh)))
1646 dr->children[i]->parent = dr;
1650 GNUNET_FS_free_download_request_ (dr);
1656 * Compute the name of the sync file (or directory) for the given download
1659 * @param dc download context to compute for
1660 * @param uni unique filename to use, use "" for the directory name
1661 * @param ext extension to use, use ".dir" for our own subdirectory
1662 * @return the expanded file name, NULL for none
1665 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1666 const char *uni, const char *ext)
1671 if (dc->parent == NULL)
1672 return get_serialization_file_name (dc->h,
1675 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1676 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1678 if (dc->parent->serialization == NULL)
1680 par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
1683 GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
1690 * Synchronize this download struct with its mirror
1691 * on disk. Note that all internal FS-operations that change
1692 * publishing structs should already call "sync" internally,
1693 * so this function is likely not useful for clients.
1695 * @param dc the struct to sync
1698 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1700 struct GNUNET_BIO_WriteHandle *wh;
1705 if (NULL == dc->serialization)
1707 dir = get_download_sync_filename (dc, "", "");
1710 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
1715 fn = GNUNET_DISK_mktemp (dir);
1719 dc->serialization = get_serialization_short_name (fn);
1723 fn = get_download_sync_filename (dc, dc->serialization, "");
1726 GNUNET_free (dc->serialization);
1727 dc->serialization = NULL;
1732 wh = GNUNET_BIO_write_open (fn);
1735 GNUNET_free (dc->serialization);
1736 dc->serialization = NULL;
1740 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1741 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
1742 uris = GNUNET_FS_uri_to_string (dc->uri);
1743 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1744 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1745 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->emsg)) ||
1746 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->filename)) ||
1747 (GNUNET_OK != GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1748 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1749 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1750 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->length)) ||
1751 (GNUNET_OK != GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1752 (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
1753 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1754 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1755 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)))
1760 if (NULL == dc->emsg)
1762 GNUNET_assert (dc->top_request != NULL);
1763 if (GNUNET_YES != write_download_request (wh, dc->top_request))
1769 GNUNET_free_non_null (uris);
1771 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1781 (void) GNUNET_BIO_write_close (wh);
1782 GNUNET_free_non_null (uris);
1783 if (0 != UNLINK (fn))
1784 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1786 GNUNET_free (dc->serialization);
1787 dc->serialization = NULL;
1792 * Synchronize this search result with its mirror
1793 * on disk. Note that all internal FS-operations that change
1794 * publishing structs should already call "sync" internally,
1795 * so this function is likely not useful for clients.
1797 * @param sr the struct to sync
1800 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1802 struct GNUNET_BIO_WriteHandle *wh;
1806 if (NULL == sr->serialization)
1808 make_serialization_file_name_in_dir (sr->sc->h,
1809 (sr->sc->psearch_result ==
1811 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1812 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1813 sr->sc->serialization);
1814 if (NULL == sr->serialization)
1816 wh = get_write_handle_in_dir (sr->sc->h,
1817 (sr->sc->psearch_result ==
1818 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1819 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1820 sr->sc->serialization, sr->serialization);
1826 uris = GNUNET_FS_uri_to_string (sr->uri);
1827 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1829 GNUNET_BIO_write_string (wh,
1831 NULL ? sr->download->serialization : NULL)) ||
1833 GNUNET_BIO_write_string (wh,
1834 sr->update_search !=
1835 NULL ? sr->update_search->serialization : NULL))
1836 || (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1837 (GNUNET_OK != GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode)))
1838 || (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1839 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1840 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1841 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1846 if ( (sr->uri != NULL) &&
1847 (sr->sc->uri->type == ksk) &&
1848 (GNUNET_OK != GNUNET_BIO_write (wh, sr->keyword_bitmap,
1849 (sr->sc->uri->data.ksk.keywordCount + 7) / 8)) )
1854 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1860 GNUNET_free_non_null (uris);
1863 GNUNET_free_non_null (uris);
1865 (void) GNUNET_BIO_write_close (wh);
1866 remove_sync_file_in_dir (sr->sc->h,
1867 (sr->sc->psearch_result ==
1868 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1869 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1870 sr->sc->serialization, sr->serialization);
1871 GNUNET_free (sr->serialization);
1872 sr->serialization = NULL;
1877 * Synchronize this search struct with its mirror
1878 * on disk. Note that all internal FS-operations that change
1879 * publishing structs should already call "sync" internally,
1880 * so this function is likely not useful for clients.
1882 * @param sc the struct to sync
1885 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1887 struct GNUNET_BIO_WriteHandle *wh;
1890 const char *category;
1893 (sc->psearch_result ==
1894 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
1895 GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
1896 if (NULL == sc->serialization)
1897 sc->serialization = make_serialization_file_name (sc->h, category);
1898 if (NULL == sc->serialization)
1901 wh = get_write_handle (sc->h, category, sc->serialization);
1907 GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1908 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
1909 uris = GNUNET_FS_uri_to_string (sc->uri);
1910 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1911 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, uris)) ||
1912 (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
1913 (GNUNET_OK != GNUNET_BIO_write_string (wh, sc->emsg)) ||
1914 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1915 (GNUNET_OK != GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1916 (GNUNET_OK != GNUNET_BIO_write_int32 (wh, sc->anonymity)))
1923 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
1932 (void) GNUNET_BIO_write_close (wh);
1933 GNUNET_free_non_null (uris);
1934 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
1935 GNUNET_free (sc->serialization);
1936 sc->serialization = NULL;
1941 * Function called with a filename of serialized unindexing operation
1944 * @param cls the 'struct GNUNET_FS_Handle*'
1945 * @param filename complete filename (absolute path)
1946 * @return GNUNET_OK (continue to iterate)
1949 deserialize_unindex_file (void *cls, const char *filename)
1951 struct GNUNET_FS_Handle *h = cls;
1952 struct GNUNET_BIO_ReadHandle *rh;
1953 struct GNUNET_FS_UnindexContext *uc;
1954 struct GNUNET_FS_ProgressInfo pi;
1958 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1960 uc->serialization = get_serialization_short_name (filename);
1961 rh = GNUNET_BIO_read_open (filename);
1968 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
1969 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1970 (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
1971 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &state)))
1976 uc->state = (enum UnindexState) state;
1979 case UNINDEX_STATE_HASHING:
1981 case UNINDEX_STATE_FS_NOTIFY:
1983 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id,
1984 sizeof (GNUNET_HashCode)))
1990 case UNINDEX_STATE_DS_REMOVE:
1992 case UNINDEX_STATE_COMPLETE:
1994 case UNINDEX_STATE_ERROR:
1996 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
2006 uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
2007 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
2008 pi.value.unindex.specifics.resume.message = uc->emsg;
2009 GNUNET_FS_unindex_make_status_ (&pi, uc,
2011 UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
2014 case UNINDEX_STATE_HASHING:
2016 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, uc->filename,
2018 &GNUNET_FS_unindex_process_hash_, uc);
2020 case UNINDEX_STATE_FS_NOTIFY:
2021 uc->state = UNINDEX_STATE_HASHING;
2022 GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
2024 case UNINDEX_STATE_DS_REMOVE:
2025 GNUNET_FS_unindex_do_remove_ (uc);
2027 case UNINDEX_STATE_COMPLETE:
2028 case UNINDEX_STATE_ERROR:
2029 /* no need to resume any operation, we were done */
2034 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2037 _("Failure while resuming unindexing operation `%s': %s\n"),
2043 GNUNET_free_non_null (uc->filename);
2044 if ((rh != NULL) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
2046 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2047 _("Failed to resume unindexing operation `%s': %s\n"), filename,
2051 if (uc->serialization != NULL)
2052 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2054 GNUNET_free_non_null (uc->serialization);
2061 * Deserialize a download.
2063 * @param h overall context
2064 * @param rh file to deserialize from
2065 * @param parent parent download
2066 * @param search associated search
2067 * @param serialization name under which the search was serialized
2070 deserialize_download (struct GNUNET_FS_Handle *h,
2071 struct GNUNET_BIO_ReadHandle *rh,
2072 struct GNUNET_FS_DownloadContext *parent,
2073 struct GNUNET_FS_SearchResult *search,
2074 const char *serialization);
2078 * Deserialize a search.
2080 * @param h overall context
2081 * @param rh file to deserialize from
2082 * @param psearch_result parent search result
2083 * @param serialization name under which the search was serialized
2085 static struct GNUNET_FS_SearchContext *
2086 deserialize_search (struct GNUNET_FS_Handle *h,
2087 struct GNUNET_BIO_ReadHandle *rh,
2088 struct GNUNET_FS_SearchResult *psearch_result,
2089 const char *serialization);
2093 * Function called with a filename of serialized search result
2096 * @param cls the 'struct GNUNET_FS_SearchContext*'
2097 * @param filename complete filename (absolute path)
2098 * @return GNUNET_OK (continue to iterate)
2101 deserialize_search_result (void *cls, const char *filename)
2103 struct GNUNET_FS_SearchContext *sc = cls;
2109 struct GNUNET_BIO_ReadHandle *rh;
2110 struct GNUNET_BIO_ReadHandle *drh;
2111 struct GNUNET_FS_SearchResult *sr;
2113 ser = get_serialization_short_name (filename);
2114 rh = GNUNET_BIO_read_open (filename);
2119 remove_sync_file_in_dir (sc->h,
2120 (sc->psearch_result ==
2121 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2122 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2123 sc->serialization, ser);
2132 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2134 sr->serialization = ser;
2135 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024))
2136 || (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2137 (GNUNET_OK != GNUNET_BIO_read_string (rh, "download-lnk", &download, 16))
2139 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2140 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2142 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode)))
2143 || (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2144 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2145 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2146 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sr->availability_trials)))
2151 if (sr->sc->uri->type == ksk)
2153 sr->keyword_bitmap = GNUNET_malloc ((sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
2154 if (GNUNET_OK != GNUNET_BIO_read (rh, "keyword-bitmap",
2156 (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
2163 if (download != NULL)
2165 drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
2168 deserialize_download (sc->h, drh, NULL, sr, download);
2169 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2172 _("Failed to resume sub-download `%s': %s\n"), download,
2177 GNUNET_free (download);
2179 if (update_srch != NULL)
2182 get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
2185 deserialize_search (sc->h, drh, sr, update_srch);
2186 if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
2188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2189 _("Failed to resume sub-search `%s': %s\n"), update_srch,
2194 GNUNET_free (update_srch);
2196 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map, &sr->key, sr,
2197 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2198 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2201 _("Failure while resuming search operation `%s': %s\n"),
2207 GNUNET_free_non_null (download);
2208 GNUNET_free_non_null (emsg);
2209 GNUNET_free_non_null (uris);
2210 GNUNET_free_non_null (update_srch);
2211 if (sr->uri != NULL)
2212 GNUNET_FS_uri_destroy (sr->uri);
2213 if (sr->meta != NULL)
2214 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2215 GNUNET_free (sr->serialization);
2217 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2220 _("Failure while resuming search operation `%s': %s\n"),
2229 * Send the 'resume' signal to the callback; also actually
2230 * resume the download (put it in the queue). Does this
2231 * recursively for the top-level download and all child
2234 * @param dc download to resume
2237 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2239 struct GNUNET_FS_DownloadContext *dcc;
2240 struct GNUNET_FS_ProgressInfo pi;
2242 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2243 pi.value.download.specifics.resume.meta = dc->meta;
2244 pi.value.download.specifics.resume.message = dc->emsg;
2245 GNUNET_FS_download_make_status_ (&pi, dc);
2246 dcc = dc->child_head;
2249 signal_download_resume (dcc);
2252 if (dc->pending_head != NULL)
2253 GNUNET_FS_download_start_downloading_ (dc);
2258 * Signal resuming of a search to our clients (for the
2259 * top level search and all sub-searches).
2261 * @param sc search being resumed
2264 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2268 * Iterator over search results signaling resume to the client for
2271 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2272 * @param key current key code
2273 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2274 * @return GNUNET_YES (we should continue to iterate)
2277 signal_result_resume (void *cls, const GNUNET_HashCode * key, void *value)
2279 struct GNUNET_FS_SearchContext *sc = cls;
2280 struct GNUNET_FS_ProgressInfo pi;
2281 struct GNUNET_FS_SearchResult *sr = value;
2283 if (0 == sr->mandatory_missing)
2285 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2286 pi.value.search.specifics.resume_result.meta = sr->meta;
2287 pi.value.search.specifics.resume_result.uri = sr->uri;
2288 pi.value.search.specifics.resume_result.result = sr;
2289 pi.value.search.specifics.resume_result.availability_rank =
2290 2 * sr->availability_success - sr->availability_trials;
2291 pi.value.search.specifics.resume_result.availability_certainty =
2292 sr->availability_trials;
2293 pi.value.search.specifics.resume_result.applicability_rank =
2294 sr->optional_support;
2295 sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2297 if (sr->download != NULL)
2299 signal_download_resume (sr->download);
2303 GNUNET_FS_search_start_probe_ (sr);
2305 if (sr->update_search != NULL)
2306 signal_search_resume (sr->update_search);
2312 * Free memory allocated by the search context and its children
2314 * @param sc search context to free
2317 free_search_context (struct GNUNET_FS_SearchContext *sc);
2321 * Iterator over search results freeing each.
2323 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2324 * @param key current key code
2325 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2326 * @return GNUNET_YES (we should continue to iterate)
2329 free_result (void *cls, const GNUNET_HashCode * key, void *value)
2331 struct GNUNET_FS_SearchResult *sr = value;
2333 if (sr->update_search != NULL)
2335 free_search_context (sr->update_search);
2336 GNUNET_assert (NULL == sr->update_search);
2338 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2339 GNUNET_FS_uri_destroy (sr->uri);
2346 * Free memory allocated by the search context and its children
2348 * @param sc search context to free
2351 free_search_context (struct GNUNET_FS_SearchContext *sc)
2353 if (sc->serialization != NULL)
2355 GNUNET_FS_remove_sync_file_ (sc->h,
2356 (sc->psearch_result ==
2357 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2358 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2360 GNUNET_FS_remove_sync_dir_ (sc->h,
2361 (sc->psearch_result ==
2362 NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2363 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2366 GNUNET_free_non_null (sc->serialization);
2367 GNUNET_free_non_null (sc->emsg);
2368 if (sc->uri != NULL)
2369 GNUNET_FS_uri_destroy (sc->uri);
2370 if (sc->master_result_map != NULL)
2372 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map, &free_result,
2374 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2381 * Function called with a filename of serialized sub-download
2384 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2385 * @param filename complete filename (absolute path)
2386 * @return GNUNET_OK (continue to iterate)
2389 deserialize_subdownload (void *cls, const char *filename)
2391 struct GNUNET_FS_DownloadContext *parent = cls;
2394 struct GNUNET_BIO_ReadHandle *rh;
2396 ser = get_serialization_short_name (filename);
2397 rh = GNUNET_BIO_read_open (filename);
2400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2402 ("Failed to resume sub-download `%s': could not open file `%s'\n"),
2407 deserialize_download (parent->h, rh, parent, NULL, ser);
2408 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2411 _("Failed to resume sub-download `%s': %s\n"), ser, emsg);
2420 * Free this download context and all of its descendants.
2421 * (only works during deserialization since not all possible
2422 * state it taken care of).
2424 * @param dc context to free
2427 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2429 struct GNUNET_FS_DownloadContext *dcc;
2431 if (dc->meta != NULL)
2432 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2433 if (dc->uri != NULL)
2434 GNUNET_FS_uri_destroy (dc->uri);
2435 GNUNET_free_non_null (dc->temp_filename);
2436 GNUNET_free_non_null (dc->emsg);
2437 GNUNET_free_non_null (dc->filename);
2438 GNUNET_free_non_null (dc->serialization);
2439 while (NULL != (dcc = dc->child_head))
2441 GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
2442 free_download_context (dcc);
2444 GNUNET_FS_free_download_request_ (dc->top_request);
2445 if (NULL != dc->active)
2446 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2452 * Deserialize a download.
2454 * @param h overall context
2455 * @param rh file to deserialize from
2456 * @param parent parent download
2457 * @param search associated search
2458 * @param serialization name under which the search was serialized
2461 deserialize_download (struct GNUNET_FS_Handle *h,
2462 struct GNUNET_BIO_ReadHandle *rh,
2463 struct GNUNET_FS_DownloadContext *parent,
2464 struct GNUNET_FS_SearchResult *search,
2465 const char *serialization)
2467 struct GNUNET_FS_DownloadContext *dc;
2476 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2477 dc->parent = parent;
2479 dc->serialization = GNUNET_strdup (serialization);
2481 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
2482 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2483 ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2484 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
2485 (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta))
2487 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10 * 1024)) ||
2489 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10 * 1024)) ||
2491 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename,
2493 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2494 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2495 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2496 (GNUNET_OK != GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2497 (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
2498 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2499 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2500 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &status)))
2505 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2507 GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE));
2508 dc->has_finished = (int) status;
2510 GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2511 if (GNUNET_FS_uri_test_loc (dc->uri))
2512 GNUNET_assert (GNUNET_OK ==
2513 GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2514 if (dc->emsg == NULL)
2516 dc->top_request = read_download_request (rh);
2517 if (dc->top_request == NULL)
2523 dn = get_download_sync_filename (dc, dc->serialization, ".dir");
2526 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2527 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2532 GNUNET_abort (); // for debugging for now - FIXME
2533 GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2537 dc->search = search;
2538 search->download = dc;
2540 if ((parent == NULL) && (search == NULL))
2543 GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2544 signal_download_resume (dc);
2547 dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2550 GNUNET_free_non_null (uris);
2551 GNUNET_free_non_null (emsg);
2552 free_download_context (dc);
2557 * Signal resuming of a search to our clients (for the
2558 * top level search and all sub-searches).
2560 * @param sc search being resumed
2563 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2565 struct GNUNET_FS_ProgressInfo pi;
2567 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2568 pi.value.search.specifics.resume.message = sc->emsg;
2569 pi.value.search.specifics.resume.is_paused =
2570 (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2571 sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc);
2572 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2573 &signal_result_resume, sc);
2579 * Deserialize a search.
2581 * @param h overall context
2582 * @param rh file to deserialize from
2583 * @param psearch_result parent search result
2584 * @param serialization name under which the search was serialized
2586 static struct GNUNET_FS_SearchContext *
2587 deserialize_search (struct GNUNET_FS_Handle *h,
2588 struct GNUNET_BIO_ReadHandle *rh,
2589 struct GNUNET_FS_SearchResult *psearch_result,
2590 const char *serialization)
2592 struct GNUNET_FS_SearchContext *sc;
2599 if ((psearch_result != NULL) && (psearch_result->update_search != NULL))
2606 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2607 if (psearch_result != NULL)
2609 sc->psearch_result = psearch_result;
2610 psearch_result->update_search = sc;
2613 sc->serialization = GNUNET_strdup (serialization);
2614 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024))
2615 || (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2616 ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2617 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
2618 (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
2620 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
2621 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &options)) ||
2623 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2624 (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &sc->anonymity)))
2629 sc->options = (enum GNUNET_FS_SearchOptions) options;
2630 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2631 dn = get_serialization_file_name_in_dir (h,
2632 (sc->psearch_result ==
2634 GNUNET_FS_SYNC_PATH_MASTER_SEARCH :
2635 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2636 sc->serialization, "");
2639 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2640 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2643 if (('\0' == in_pause) &&
2644 (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
2646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2648 ("Could not resume running search, will resume as paused search\n"));
2650 signal_search_resume (sc);
2654 GNUNET_free_non_null (emsg);
2655 free_search_context (sc);
2656 GNUNET_free_non_null (uris);
2662 * Function called with a filename of serialized search operation
2665 * @param cls the 'struct GNUNET_FS_Handle*'
2666 * @param filename complete filename (absolute path)
2667 * @return GNUNET_OK (continue to iterate)
2670 deserialize_search_file (void *cls, const char *filename)
2672 struct GNUNET_FS_Handle *h = cls;
2675 struct GNUNET_BIO_ReadHandle *rh;
2676 struct GNUNET_FS_SearchContext *sc;
2679 if (0 != STAT (filename, &buf))
2681 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
2684 if (S_ISDIR (buf.st_mode))
2685 return GNUNET_OK; /* skip directories */
2686 ser = get_serialization_short_name (filename);
2687 rh = GNUNET_BIO_read_open (filename);
2692 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2697 sc = deserialize_search (h, rh, NULL, ser);
2699 sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
2701 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2704 _("Failure while resuming search operation `%s': %s\n"),
2713 * Function called with a filename of serialized download operation
2716 * @param cls the 'struct GNUNET_FS_Handle*'
2717 * @param filename complete filename (absolute path)
2718 * @return GNUNET_OK (continue to iterate)
2721 deserialize_download_file (void *cls, const char *filename)
2723 struct GNUNET_FS_Handle *h = cls;
2726 struct GNUNET_BIO_ReadHandle *rh;
2728 ser = get_serialization_short_name (filename);
2729 rh = GNUNET_BIO_read_open (filename);
2732 if (0 != UNLINK (filename))
2733 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
2737 deserialize_download (h, rh, NULL, NULL, ser);
2739 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
2741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2742 _("Failure while resuming download operation `%s': %s\n"),
2751 * Deserialize informatin about pending operations.
2753 * @param master_path which master directory should be scanned
2754 * @param proc function to call for each entry (will get 'h' for 'cls')
2755 * @param h the 'struct GNUNET_FS_Handle*'
2758 deserialization_master (const char *master_path, GNUNET_FileNameCallback proc,
2759 struct GNUNET_FS_Handle *h)
2763 dn = get_serialization_file_name (h, master_path, "");
2766 if (GNUNET_YES == GNUNET_DISK_directory_test (dn))
2767 GNUNET_DISK_directory_scan (dn, proc, h);
2773 * Setup a connection to the file-sharing service.
2775 * @param cfg configuration to use
2776 * @param client_name unique identifier for this client
2777 * @param upcb function to call to notify about FS actions
2778 * @param upcb_cls closure for upcb
2779 * @param flags specific attributes for fs-operations
2780 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2781 * @return NULL on error
2783 struct GNUNET_FS_Handle *
2784 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2785 const char *client_name, GNUNET_FS_ProgressCallback upcb,
2786 void *upcb_cls, enum GNUNET_FS_Flags flags, ...)
2788 struct GNUNET_FS_Handle *ret;
2789 enum GNUNET_FS_OPTIONS opt;
2792 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2794 ret->client_name = GNUNET_strdup (client_name);
2796 ret->upcb_cls = upcb_cls;
2798 ret->max_parallel_downloads = 1;
2799 ret->max_parallel_requests = 1;
2800 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2801 va_start (ap, flags);
2802 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2806 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2807 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2810 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2811 ret->max_parallel_requests = va_arg (ap, unsigned int);
2816 GNUNET_free (ret->client_name);
2823 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2825 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
2826 &deserialize_publish_file, ret);
2827 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
2828 &deserialize_search_file, ret);
2829 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2830 &deserialize_download_file, ret);
2831 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2832 &deserialize_unindex_file, ret);
2839 * Close our connection with the file-sharing service.
2840 * The callback given to GNUNET_FS_start will no longer be
2841 * called after this function returns.
2843 * @param h handle that was returned from GNUNET_FS_start
2846 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2848 while (h->top_head != NULL)
2849 h->top_head->ssf (h->top_head->ssf_cls);
2850 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2851 GNUNET_SCHEDULER_cancel (h->queue_job);
2852 GNUNET_free (h->client_name);