2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 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 2, 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 qe->client = GNUNET_CLIENT_connect (qe->h->sched, "fs", qe->h->cfg);
44 if (qe->client == NULL)
49 qe->start (qe->cls, qe->client);
51 qe->h->active_blocks += qe->blocks;
52 qe->start_time = GNUNET_TIME_absolute_get ();
53 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
56 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
64 * Stop the given job (send signal, remove from active queue, update
65 * counters and state).
67 * @param qe job to stop
70 stop_job (struct GNUNET_FS_QueueEntry *qe)
74 qe->h->active_downloads--;
75 qe->h->active_blocks -= qe->blocks;
76 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
77 GNUNET_TIME_absolute_get_duration (qe->start_time));
78 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
81 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
89 * Process the jobs in the job queue, possibly starting some
90 * and stopping others.
92 * @param cls the 'struct GNUNET_FS_Handle'
93 * @param tc scheduler context
96 process_job_queue (void *cls,
97 const struct GNUNET_SCHEDULER_TaskContext *tc)
99 struct GNUNET_FS_Handle *h = cls;
100 struct GNUNET_FS_QueueEntry *qe;
101 struct GNUNET_FS_QueueEntry *next;
102 struct GNUNET_TIME_Relative run_time;
103 struct GNUNET_TIME_Relative restart_at;
104 struct GNUNET_TIME_Relative rst;
105 struct GNUNET_TIME_Absolute end_time;
107 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
108 next = h->pending_head;
109 while (NULL != (qe = next))
112 if (h->running_head == NULL)
117 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
118 (h->active_downloads + 1 <= h->max_parallel_downloads) )
124 if (h->pending_head == NULL)
125 return; /* no need to stop anything */
126 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
127 next = h->running_head;
128 while (NULL != (qe = next))
131 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
132 qe->blocks * qe->start_times);
133 end_time = GNUNET_TIME_absolute_add (qe->start_time,
135 rst = GNUNET_TIME_absolute_get_remaining (end_time);
136 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
141 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
149 * Add a job to the queue.
151 * @param h handle to the overall FS state
152 * @param start function to call to begin the job
153 * @param stop function to call to pause the job, or on dequeue (if the job was running)
154 * @param cls closure for start and stop
155 * @param blocks number of blocks this jobs uses
156 * @return queue handle
158 struct GNUNET_FS_QueueEntry *
159 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
160 GNUNET_FS_QueueStart start,
161 GNUNET_FS_QueueStop stop,
165 struct GNUNET_FS_QueueEntry *qe;
167 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
172 qe->queue_time = GNUNET_TIME_absolute_get ();
174 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
178 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
179 GNUNET_SCHEDULER_cancel (h->sched,
182 = GNUNET_SCHEDULER_add_now (h->sched,
190 * Dequeue a job from the queue.
191 * @param qh handle for the job
194 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
196 struct GNUNET_FS_Handle *h;
199 if (qh->client != NULL)
201 GNUNET_CONTAINER_DLL_remove (h->pending_head,
205 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
206 GNUNET_SCHEDULER_cancel (h->sched,
209 = GNUNET_SCHEDULER_add_now (h->sched,
216 * Create a top-level activity entry.
218 * @param h global fs handle
219 * @param ssf suspend signal function to use
220 * @param ssf_cls closure for ssf
221 * @return fresh top-level activity handle
223 struct TopLevelActivity *
224 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
225 SuspendSignalFunction ssf,
228 struct TopLevelActivity *ret;
230 ret = GNUNET_malloc (sizeof (struct TopLevelActivity));
232 ret->ssf_cls = ssf_cls;
233 GNUNET_CONTAINER_DLL_insert (h->top_head,
241 * Destroy a top-level activity entry.
243 * @param h global fs handle
244 * @param top top level activity entry
247 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h,
248 struct TopLevelActivity *top)
250 GNUNET_CONTAINER_DLL_remove (h->top_head,
259 * Closure for "data_reader_file".
264 * Name of the file to read.
269 * File descriptor, NULL if it has not yet been opened.
271 struct GNUNET_DISK_FileHandle *fd;
276 * Function that provides data by reading from a file.
278 * @param cls closure (points to the file information)
279 * @param offset offset to read from; it is possible
280 * that the caller might need to go backwards
282 * @param max maximum number of bytes that should be
283 * copied to buf; readers are not allowed
284 * to provide less data unless there is an error;
285 * a value of "0" will be used at the end to allow
286 * the reader to clean up its internal state
287 * @param buf where the reader should write the data
288 * @param emsg location for the reader to store an error message
289 * @return number of bytes written, usually "max", 0 on error
292 GNUNET_FS_data_reader_file_(void *cls,
298 struct FileInfo *fi = cls;
304 GNUNET_DISK_file_close (fi->fd);
305 GNUNET_free (fi->filename);
311 fi->fd = GNUNET_DISK_file_open (fi->filename,
312 GNUNET_DISK_OPEN_READ,
313 GNUNET_DISK_PERM_NONE);
316 GNUNET_asprintf (emsg,
317 _("Could not open file `%s': %s"),
323 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
324 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
327 GNUNET_asprintf (emsg,
328 _("Could not read file `%s': %s"),
335 GNUNET_asprintf (emsg,
336 _("Short read reading from file `%s'!"),
345 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
347 * @param filename file to read
348 * @return closure to use, NULL on error
351 GNUNET_FS_make_file_reader_context_ (const char *filename)
355 fi = GNUNET_malloc (sizeof(struct FileInfo));
356 fi->filename = GNUNET_STRINGS_filename_expand (filename);
357 if (fi->filename == NULL)
367 * Function that provides data by copying from a buffer.
369 * @param cls closure (points to the buffer)
370 * @param offset offset to read from; it is possible
371 * that the caller might need to go backwards
373 * @param max maximum number of bytes that should be
374 * copied to buf; readers are not allowed
375 * to provide less data unless there is an error;
376 * a value of "0" will be used at the end to allow
377 * the reader to clean up its internal state
378 * @param buf where the reader should write the data
379 * @param emsg location for the reader to store an error message
380 * @return number of bytes written, usually "max", 0 on error
383 GNUNET_FS_data_reader_copy_ (void *cls,
393 GNUNET_free_non_null (data);
396 memcpy (buf, &data[offset], max);
403 * Return the full filename where we would store state information
404 * (for serialization/deserialization).
406 * @param h master context
407 * @param ext component of the path
408 * @param ent entity identifier (or emtpy string for the directory)
409 * @return NULL on error
412 get_serialization_file_name (struct GNUNET_FS_Handle *h,
419 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
424 GNUNET_asprintf (&ret,
433 GNUNET_free (basename);
439 * Return a read handle for deserialization.
441 * @param h master context
442 * @param ext component of the path
443 * @param ent entity identifier (or emtpy string for the directory)
444 * @return NULL on error
446 static struct GNUNET_BIO_ReadHandle *
447 get_read_handle (struct GNUNET_FS_Handle *h,
452 struct GNUNET_BIO_ReadHandle *ret;
454 fn = get_serialization_file_name (h, ext, ent);
457 ret = GNUNET_BIO_read_open (fn);
464 * Return a write handle for serialization.
466 * @param h master context
467 * @param ext component of the path
468 * @param ent entity identifier (or emtpy string for the directory)
469 * @return NULL on error
471 static struct GNUNET_BIO_WriteHandle *
472 get_write_handle (struct GNUNET_FS_Handle *h,
477 struct GNUNET_BIO_WriteHandle *ret;
479 fn = get_serialization_file_name (h, ext, ent);
482 ret = GNUNET_BIO_write_open (fn);
489 * Remove serialization/deserialization file from disk.
491 * @param h master context
492 * @param ext component of the path
493 * @param ent entity identifier
496 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
502 if ( (NULL == ent) ||
503 (0 == strlen (ent)) )
508 filename = get_serialization_file_name (h, ext, ent);
509 if (0 != UNLINK (filename))
510 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
513 GNUNET_free (filename);
518 * Remove serialization/deserialization directory from disk.
520 * @param h master context
521 * @param ext component of the path
522 * @param uni unique name of parent
525 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
534 GNUNET_snprintf (pbuf,
540 dn = get_serialization_file_name (h, ext, "");
543 if (GNUNET_OK != GNUNET_DISK_directory_remove (dn))
544 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
552 * Serialize a 'start_time'. Since we use start-times to
553 * calculate the duration of some operation, we actually
554 * do not serialize the absolute time but the (relative)
555 * duration since the start time. When we then
556 * deserialize the start time, we take the current time and
557 * subtract that duration so that we get again an absolute
558 * time stamp that will result in correct performance
561 * @param wh handle for writing
562 * @param timestamp time to serialize
563 * @return GNUNET_OK on success
566 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
567 struct GNUNET_TIME_Absolute timestamp)
569 struct GNUNET_TIME_Relative dur;
571 dur = GNUNET_TIME_absolute_get_duration (timestamp);
572 return GNUNET_BIO_write_int64 (wh, dur.value);
577 * Serialize a 'start_time'. Since we use start-times to
578 * calculate the duration of some operation, we actually
579 * do not serialize the absolute time but the (relative)
580 * duration since the start time. When we then
581 * deserialize the start time, we take the current time and
582 * subtract that duration so that we get again an absolute
583 * time stamp that will result in correct performance
586 * @param rh handle for reading
587 * @param timestamp where to write the deserialized timestamp
588 * @return GNUNET_OK on success
591 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
592 struct GNUNET_TIME_Absolute *timestamp)
594 struct GNUNET_TIME_Relative dur;
596 GNUNET_BIO_read_int64 (rh, &dur.value))
597 return GNUNET_SYSERR;
598 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
605 * Using the given serialization filename, try to deserialize
606 * the file-information tree associated with it.
608 * @param h master context
609 * @param filename name of the file (without directory) with
611 * @return NULL on error
613 static struct GNUNET_FS_FileInformation *
614 deserialize_file_information (struct GNUNET_FS_Handle *h,
615 const char *filename);
619 * Using the given serialization filename, try to deserialize
620 * the file-information tree associated with it.
622 * @param h master context
623 * @param fn name of the file (without directory) with
625 * @param rh handle for reading
626 * @return NULL on error
628 static struct GNUNET_FS_FileInformation *
629 deserialize_fi_node (struct GNUNET_FS_Handle *h,
631 struct GNUNET_BIO_ReadHandle *rh)
633 struct GNUNET_FS_FileInformation *ret;
634 struct GNUNET_FS_FileInformation *nxt;
642 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
647 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
652 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
654 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
657 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
659 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
661 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
664 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
666 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
668 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
670 read_start_time (rh, &ret->start_time)) ||
672 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
674 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
676 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
678 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
682 case 0: /* file-insert */
684 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
686 ret->is_directory = GNUNET_NO;
687 ret->data.file.do_index = GNUNET_NO;
688 ret->data.file.have_hash = GNUNET_NO;
689 ret->data.file.index_start_confirmed = GNUNET_NO;
690 if (GNUNET_NO == ret->is_published)
692 if (NULL == ret->filename)
694 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
695 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
696 if (ret->data.file.reader_cls == NULL)
699 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
704 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
705 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
709 case 1: /* file-index, no hash */
710 if (NULL == ret->filename)
713 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
715 ret->is_directory = GNUNET_NO;
716 ret->data.file.do_index = GNUNET_YES;
717 ret->data.file.have_hash = GNUNET_NO;
718 ret->data.file.index_start_confirmed = GNUNET_NO;
719 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
720 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
722 case 2: /* file-index-with-hash */
723 if (NULL == ret->filename)
726 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
728 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
730 ret->is_directory = GNUNET_NO;
731 ret->data.file.do_index = GNUNET_YES;
732 ret->data.file.have_hash = GNUNET_YES;
733 ret->data.file.index_start_confirmed = GNUNET_NO;
734 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
735 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
737 case 3: /* file-index-with-hash-confirmed */
738 if (NULL == ret->filename)
741 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
743 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
746 ret->is_directory = GNUNET_NO;
747 ret->data.file.do_index = GNUNET_YES;
748 ret->data.file.have_hash = GNUNET_YES;
749 ret->data.file.index_start_confirmed = GNUNET_YES;
750 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
751 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
753 case 4: /* directory */
755 GNUNET_BIO_read_int32 (rh, &dsize)) ||
756 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
758 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
760 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
762 ret->data.dir.dir_size = (uint32_t) dsize;
763 ret->is_directory = GNUNET_YES;
764 if (filename != NULL)
766 ret->data.dir.entries = deserialize_file_information (h, filename);
767 GNUNET_free (filename);
769 nxt = ret->data.dir.entries;
781 ret->serialization = GNUNET_strdup (fn);
783 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
785 if (filename != NULL)
787 ret->next = deserialize_file_information (h, filename);
788 GNUNET_free (filename);
791 GNUNET_free_non_null (ksks);
792 GNUNET_free_non_null (chks);
795 GNUNET_free_non_null (ksks);
796 GNUNET_free_non_null (chks);
797 GNUNET_free_non_null (filename);
798 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
804 * Using the given serialization filename, try to deserialize
805 * the file-information tree associated with it.
807 * @param h master context
808 * @param filename name of the file (without directory) with
810 * @return NULL on error
812 static struct GNUNET_FS_FileInformation *
813 deserialize_file_information (struct GNUNET_FS_Handle *h,
814 const char *filename)
816 struct GNUNET_FS_FileInformation *ret;
817 struct GNUNET_BIO_ReadHandle *rh;
820 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
823 ret = deserialize_fi_node (h, filename, rh);
825 GNUNET_BIO_read_close (rh, &emsg))
827 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
828 _("Failed to resume publishing information `%s': %s\n"),
838 * Given a serialization name (full absolute path), return the
839 * basename of the file (without the path), which must only
840 * consist of the 6 random characters.
842 * @param fullname name to extract the basename from
843 * @return copy of the basename, NULL on error
846 get_serialization_short_name (const char *fullname)
853 /* FIXME: we could do this faster since we know
854 the length of 'end'... */
857 if (DIR_SEPARATOR == *nxt)
861 if ( (end == NULL) ||
862 (strlen (end) == 0) )
867 GNUNET_break (6 == strlen (end));
868 return GNUNET_strdup (end);
873 * Create a new random name for serialization. Also checks if persistence
874 * is enabled and returns NULL if not.
876 * @param h master context
877 * @param ext component of the path
878 * @return NULL on errror
881 make_serialization_file_name (struct GNUNET_FS_Handle *h,
888 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
889 return NULL; /* persistence not requested */
890 dn = get_serialization_file_name (h, ext, "");
891 fn = GNUNET_DISK_mktemp (dn);
894 return NULL; /* epic fail */
895 ret = get_serialization_short_name (fn);
902 * Copy all of the data from the reader to the write handle.
904 * @param wh write handle
905 * @param fi file with reader
906 * @return GNUNET_OK on success
909 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
910 struct GNUNET_FS_FileInformation * fi)
919 while (off < fi->data.file.file_size)
921 ret = fi->data.file.reader (fi->data.file.reader_cls,
928 return GNUNET_SYSERR;
931 GNUNET_BIO_write (wh, buf, ret))
932 return GNUNET_SYSERR;
940 * Create a temporary file on disk to store the current
943 * @param fi file information to sync with disk
946 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
949 struct GNUNET_BIO_WriteHandle *wh;
954 if (NULL == fi->serialization)
955 fi->serialization = make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
956 if (NULL == fi->serialization)
958 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
961 GNUNET_free (fi->serialization);
962 fi->serialization = NULL;
965 if (GNUNET_YES == fi->is_directory)
967 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
969 else if (GNUNET_YES == fi->data.file.have_hash)
971 else if (GNUNET_YES == fi->data.file.do_index)
975 if (fi->keywords != NULL)
976 ksks = GNUNET_FS_uri_to_string (fi->keywords);
979 if (fi->chk_uri != NULL)
980 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
984 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
986 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
988 GNUNET_BIO_write_string (wh, ksks)) ||
990 GNUNET_BIO_write_string (wh, chks)) ||
992 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
994 write_start_time (wh, fi->start_time)) ||
996 GNUNET_BIO_write_string (wh, fi->emsg)) ||
998 GNUNET_BIO_write_string (wh, fi->filename)) ||
1000 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
1002 GNUNET_BIO_write_int32 (wh, fi->priority)) )
1004 GNUNET_free_non_null (chks);
1006 GNUNET_free_non_null (ksks);
1011 case 0: /* file-insert */
1013 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1015 if ( (GNUNET_NO == fi->is_published) &&
1016 (NULL == fi->filename) )
1018 copy_from_reader (wh, fi))
1021 case 1: /* file-index, no hash */
1022 if (NULL == fi->filename)
1025 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1028 case 2: /* file-index-with-hash */
1029 case 3: /* file-index-with-hash-confirmed */
1030 if (NULL == fi->filename)
1033 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1035 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
1038 case 4: /* directory */
1040 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1042 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
1044 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
1052 GNUNET_BIO_write_string (wh, fi->next->serialization))
1055 GNUNET_BIO_write_close (wh))
1058 (void) GNUNET_BIO_write_close (wh);
1059 GNUNET_free_non_null (chks);
1060 GNUNET_free_non_null (ksks);
1061 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1062 if (0 != UNLINK (fn))
1063 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1065 GNUNET_free (fi->serialization);
1066 fi->serialization = NULL;
1072 * Find the entry in the file information struct where the
1073 * serialization filename matches the given name.
1075 * @param pos file information to search
1076 * @param srch filename to search for
1077 * @return NULL if srch was not found in this subtree
1079 static struct GNUNET_FS_FileInformation *
1080 find_file_position (struct GNUNET_FS_FileInformation *pos,
1083 struct GNUNET_FS_FileInformation *r;
1087 if (0 == strcmp (srch,
1088 pos->serialization))
1090 if (pos->is_directory)
1092 r = find_file_position (pos->data.dir.entries,
1104 * Signal the FS's progress function that we are resuming
1107 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1108 * @param fi the entry in the publish-structure
1109 * @param length length of the file or directory
1110 * @param meta metadata for the file or directory (can be modified)
1111 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1112 * @param anonymity pointer to selected anonymity level (can be modified)
1113 * @param priority pointer to selected priority (can be modified)
1114 * @param expirationTime pointer to selected expiration time (can be modified)
1115 * @param client_info pointer to client context set upon creation (can be modified)
1116 * @return GNUNET_OK to continue (always)
1119 fip_signal_resume(void *cls,
1120 struct GNUNET_FS_FileInformation *fi,
1122 struct GNUNET_CONTAINER_MetaData *meta,
1123 struct GNUNET_FS_Uri **uri,
1124 uint32_t *anonymity,
1126 struct GNUNET_TIME_Absolute *expirationTime,
1129 struct GNUNET_FS_PublishContext *sc = cls;
1130 struct GNUNET_FS_ProgressInfo pi;
1132 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1133 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1134 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1135 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1141 * Function called with a filename of serialized publishing operation
1144 * @param cls the 'struct GNUNET_FS_Handle*'
1145 * @param filename complete filename (absolute path)
1146 * @return GNUNET_OK (continue to iterate)
1149 deserialize_publish_file (void *cls,
1150 const char *filename)
1152 struct GNUNET_FS_Handle *h = cls;
1153 struct GNUNET_BIO_ReadHandle *rh;
1154 struct GNUNET_FS_PublishContext *pc;
1162 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1164 pc->serialization = get_serialization_short_name (filename);
1168 rh = GNUNET_BIO_read_open (filename);
1172 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1174 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1176 GNUNET_BIO_read_int32 (rh, &options)) ||
1178 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1180 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1182 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1184 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1186 pc->options = options;
1187 pc->all_done = all_done;
1188 pc->fi = deserialize_file_information (h, fi_root);
1193 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1194 if (pc->namespace == NULL)
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1204 pc->fi_pos = find_file_position (pc->fi,
1206 GNUNET_free (fi_pos);
1208 if (pc->fi_pos == NULL)
1210 /* failed to find position for resuming, outch! Will start from root! */
1212 if (pc->all_done != GNUNET_YES)
1213 pc->fi_pos = pc->fi;
1216 /* generate RESUME event(s) */
1217 GNUNET_FS_file_information_inspect (pc->fi,
1221 /* re-start publishing (if needed)... */
1222 if (pc->all_done != GNUNET_YES)
1224 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1225 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1226 &GNUNET_FS_publish_main_,
1229 GNUNET_BIO_read_close (rh, &emsg))
1231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1232 _("Failure while resuming publishing operation `%s': %s\n"),
1237 GNUNET_free_non_null (ns);
1240 GNUNET_free_non_null (pc->nid);
1241 GNUNET_free_non_null (pc->nuid);
1242 GNUNET_free_non_null (fi_root);
1243 GNUNET_free_non_null (fi_pos);
1244 GNUNET_free_non_null (ns);
1245 if ( (rh != NULL) &&
1247 GNUNET_BIO_read_close (rh, &emsg)) )
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250 _("Failed to resume publishing operation `%s': %s\n"),
1256 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1257 if (0 != UNLINK (filename))
1258 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1259 GNUNET_free_non_null (pc->serialization);
1266 * Synchronize this publishing struct with its mirror
1267 * on disk. Note that all internal FS-operations that change
1268 * publishing structs should already call "sync" internally,
1269 * so this function is likely not useful for clients.
1271 * @param pc the struct to sync
1274 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1276 struct GNUNET_BIO_WriteHandle *wh;
1278 if (NULL == pc->serialization)
1279 pc->serialization = make_serialization_file_name (pc->h,
1280 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1281 if (NULL == pc->serialization)
1285 if (NULL == pc->fi->serialization)
1290 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1292 GNUNET_BIO_write_string (wh, pc->nid)) ||
1294 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1296 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1298 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1300 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1302 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1304 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1309 GNUNET_BIO_write_close (wh))
1317 (void) GNUNET_BIO_write_close (wh);
1318 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1319 GNUNET_free (pc->serialization);
1320 pc->serialization = NULL;
1325 * Synchronize this unindex struct with its mirror
1326 * on disk. Note that all internal FS-operations that change
1327 * publishing structs should already call "sync" internally,
1328 * so this function is likely not useful for clients.
1330 * @param uc the struct to sync
1333 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1335 struct GNUNET_BIO_WriteHandle *wh;
1337 if (UNINDEX_STATE_ABORTED == uc->state)
1339 if (NULL == uc->serialization)
1340 uc->serialization = make_serialization_file_name (uc->h,
1341 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1342 if (NULL == uc->serialization)
1344 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1346 GNUNET_BIO_write_string (wh, uc->filename)) ||
1348 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1350 write_start_time (wh, uc->start_time)) ||
1352 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1353 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1355 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1356 ( (uc->state == UNINDEX_STATE_ERROR) &&
1358 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1361 GNUNET_BIO_write_close (wh))
1369 (void) GNUNET_BIO_write_close (wh);
1370 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1371 GNUNET_free (uc->serialization);
1372 uc->serialization = NULL;
1377 * Serialize an active or pending download request.
1379 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
1380 * @param key unused, can be NULL
1381 * @param value the 'struct DownloadRequest'
1382 * @return GNUNET_YES on success, GNUNET_NO on error
1385 write_download_request (void *cls,
1386 const GNUNET_HashCode *key,
1389 struct GNUNET_BIO_WriteHandle *wh = cls;
1390 struct DownloadRequest *dr = value;
1393 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) ||
1395 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1397 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1404 * Count active download requests.
1406 * @param cls the 'uint32_t*' counter
1407 * @param key unused, can be NULL
1408 * @param value the 'struct DownloadRequest'
1409 * @return GNUNET_YES (continue iteration)
1412 count_download_requests (void *cls,
1413 const GNUNET_HashCode *key,
1416 uint32_t *counter = cls;
1424 * Synchronize this download struct with its mirror
1425 * on disk. Note that all internal FS-operations that change
1426 * publishing structs should already call "sync" internally,
1427 * so this function is likely not useful for clients.
1429 * @param dc the struct to sync
1432 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1434 struct GNUNET_BIO_WriteHandle *wh;
1436 const char *category;
1439 uint32_t num_pending;
1441 if (dc->parent != NULL)
1443 if (dc->parent->serialization == NULL)
1445 GNUNET_snprintf (pbuf,
1448 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1450 dc->parent->serialization);
1455 category = GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD;
1457 if (NULL == dc->serialization)
1458 dc->serialization = make_serialization_file_name (dc->h,
1460 if (NULL == dc->serialization)
1462 wh = get_write_handle (dc->h, category, dc->serialization);
1465 GNUNET_free (dc->serialization);
1466 dc->serialization = NULL;
1469 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1470 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1471 uris = GNUNET_FS_uri_to_string (dc->uri);
1473 if (dc->emsg != NULL)
1474 (void) GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1475 &count_download_requests,
1477 GNUNET_assert ( (dc->length == dc->completed) ||
1478 (dc->emsg != NULL) ||
1479 (num_pending > 0) );
1481 GNUNET_BIO_write_string (wh, uris)) ||
1483 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1485 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1487 GNUNET_BIO_write_string (wh, dc->filename)) ||
1489 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1491 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1493 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1495 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1497 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1499 write_start_time (wh, dc->start_time)) ||
1501 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1503 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1505 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) ||
1507 GNUNET_BIO_write_int32 (wh, num_pending)) )
1509 if (GNUNET_SYSERR ==
1510 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1511 &write_download_request,
1514 GNUNET_free_non_null (uris);
1516 GNUNET_BIO_write_close (wh))
1519 (void) GNUNET_BIO_write_close (wh);
1520 GNUNET_free_non_null (uris);
1521 fn = get_serialization_file_name (dc->h, category, dc->serialization);
1522 if (0 != UNLINK (fn))
1523 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1525 GNUNET_free (dc->serialization);
1526 dc->serialization = NULL;
1531 * Synchronize this search result with its mirror
1532 * on disk. Note that all internal FS-operations that change
1533 * publishing structs should already call "sync" internally,
1534 * so this function is likely not useful for clients.
1536 * @param sr the struct to sync
1539 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1541 struct GNUNET_BIO_WriteHandle *wh;
1545 if (NULL == sr->serialization)
1546 sr->serialization = make_serialization_file_name (sr->sc->h,
1547 GNUNET_FS_SYNC_PATH_SEARCH_RESULT);
1548 if (NULL == sr->serialization)
1550 wh = get_write_handle (sr->sc->h, GNUNET_FS_SYNC_PATH_SEARCH_RESULT, sr->serialization);
1551 uris = GNUNET_FS_uri_to_string (sr->uri);
1553 GNUNET_BIO_write_string (wh, uris)) ||
1555 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1557 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1559 GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode))) ||
1561 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1563 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1565 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1567 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1570 GNUNET_BIO_write_close (wh))
1575 GNUNET_free_non_null (uris);
1578 GNUNET_free_non_null (uris);
1580 (void) GNUNET_BIO_write_close (wh);
1581 GNUNET_FS_remove_sync_file_ (sr->sc->h, GNUNET_FS_SYNC_PATH_SEARCH_RESULT, sr->serialization);
1582 GNUNET_free (sr->serialization);
1583 sr->serialization = NULL;
1588 * Synchronize this search struct with its mirror
1589 * on disk. Note that all internal FS-operations that change
1590 * publishing structs should already call "sync" internally,
1591 * so this function is likely not useful for clients.
1593 * @param sc the struct to sync
1596 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1598 struct GNUNET_BIO_WriteHandle *wh;
1599 struct GNUNET_FS_SearchContext *scc;
1602 const char *category;
1605 category = (sc->parent == NULL) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
1606 if (NULL == sc->serialization)
1607 sc->serialization = make_serialization_file_name (sc->h,
1609 if (NULL == sc->serialization)
1611 wh = get_write_handle (sc->h, category, sc->serialization);
1612 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1613 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1614 uris = GNUNET_FS_uri_to_string (sc->uri);
1615 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1617 GNUNET_BIO_write_string (wh, uris)) ||
1619 write_start_time (wh, sc->start_time)) ||
1621 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1623 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1625 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1627 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1631 scc = sc->child_head;
1634 if (scc->serialization == NULL)
1637 GNUNET_BIO_write_string (wh, scc->serialization))
1641 GNUNET_BIO_write_string (wh, NULL);
1643 GNUNET_BIO_write_close (wh))
1651 (void) GNUNET_BIO_write_close (wh);
1652 GNUNET_free_non_null (uris);
1653 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
1654 GNUNET_free (sc->serialization);
1655 sc->serialization = NULL;
1660 * Function called with a filename of serialized unindexing operation
1663 * @param cls the 'struct GNUNET_FS_Handle*'
1664 * @param filename complete filename (absolute path)
1665 * @return GNUNET_OK (continue to iterate)
1668 deserialize_unindex_file (void *cls,
1669 const char *filename)
1671 struct GNUNET_FS_Handle *h = cls;
1672 struct GNUNET_BIO_ReadHandle *rh;
1673 struct GNUNET_FS_UnindexContext *uc;
1674 struct GNUNET_FS_ProgressInfo pi;
1678 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1680 uc->serialization = get_serialization_short_name (filename);
1681 rh = GNUNET_BIO_read_open (filename);
1685 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1687 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1689 read_start_time (rh, &uc->start_time)) ||
1691 GNUNET_BIO_read_int32 (rh, &state)) )
1693 uc->state = (enum UnindexState) state;
1696 case UNINDEX_STATE_HASHING:
1698 case UNINDEX_STATE_FS_NOTIFY:
1700 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1703 case UNINDEX_STATE_DS_REMOVE:
1705 case UNINDEX_STATE_COMPLETE:
1707 case UNINDEX_STATE_ERROR:
1709 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1712 case UNINDEX_STATE_ABORTED:
1719 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1720 pi.value.unindex.specifics.resume.message = uc->emsg;
1721 GNUNET_FS_unindex_make_status_ (&pi,
1723 (uc->state == UNINDEX_STATE_COMPLETE)
1728 case UNINDEX_STATE_HASHING:
1729 GNUNET_CRYPTO_hash_file (uc->h->sched,
1730 GNUNET_SCHEDULER_PRIORITY_IDLE,
1733 &GNUNET_FS_unindex_process_hash_,
1736 case UNINDEX_STATE_FS_NOTIFY:
1737 uc->state = UNINDEX_STATE_HASHING;
1738 GNUNET_FS_unindex_process_hash_ (uc,
1741 case UNINDEX_STATE_DS_REMOVE:
1742 GNUNET_FS_unindex_do_remove_ (uc);
1744 case UNINDEX_STATE_COMPLETE:
1745 case UNINDEX_STATE_ERROR:
1746 /* no need to resume any operation, we were done */
1752 GNUNET_BIO_read_close (rh, &emsg))
1754 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1755 _("Failure while resuming unindexing operation `%s': %s\n"),
1762 GNUNET_free_non_null (uc->filename);
1763 if ( (rh != NULL) &&
1765 GNUNET_BIO_read_close (rh, &emsg)) )
1767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1768 _("Failed to resume unindexing operation `%s': %s\n"),
1773 if (uc->serialization != NULL)
1774 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1775 GNUNET_free_non_null (uc->serialization);
1782 * Deserialize a download.
1784 * @param h overall context
1785 * @param rh file to deserialize from
1786 * @param parent parent download
1787 * @param search associated search
1788 * @param serialization name under which the search was serialized
1791 deserialize_download (struct GNUNET_FS_Handle *h,
1792 struct GNUNET_BIO_ReadHandle *rh,
1793 struct GNUNET_FS_DownloadContext *parent,
1794 struct GNUNET_FS_SearchResult *search,
1795 const char *serialization);
1799 * Function called with a filename of serialized search result
1802 * @param cls the 'struct GNUNET_FS_SearchContext*'
1803 * @param filename complete filename (absolute path)
1804 * @return GNUNET_OK (continue to iterate)
1807 deserialize_search_result (void *cls,
1808 const char *filename)
1810 struct GNUNET_FS_SearchContext *sc = cls;
1816 struct GNUNET_BIO_ReadHandle *rh;
1817 struct GNUNET_BIO_ReadHandle *drh;
1818 struct GNUNET_FS_SearchResult *sr;
1820 ser = get_serialization_short_name (filename);
1821 rh = GNUNET_BIO_read_open (filename);
1826 GNUNET_snprintf (pbuf,
1829 GNUNET_FS_SYNC_PATH_SEARCH_RESULT,
1832 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1840 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
1841 sr->serialization = ser;
1843 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1844 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1846 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
1848 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1850 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode))) ||
1852 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1854 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1856 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1858 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1861 if (download != NULL)
1863 drh = get_read_handle (sc->h,
1864 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
1866 deserialize_download (sc->h,
1872 GNUNET_BIO_read_close (drh, &emsg))
1874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1875 _("Failed to resume sub-download `%s': %s\n"),
1880 GNUNET_free (download);
1882 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1885 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1888 GNUNET_free_non_null (download);
1889 GNUNET_free_non_null (emsg);
1890 GNUNET_free_non_null (uris);
1891 if (sr->uri != NULL)
1892 GNUNET_FS_uri_destroy (sr->uri);
1893 if (sr->meta != NULL)
1894 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1895 GNUNET_free (sr->serialization);
1902 * Send the 'resume' signal to the callback; also actually
1903 * resume the download (put it in the queue). Does this
1904 * recursively for the top-level download and all child
1907 * @param dc download to resume
1910 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
1912 struct GNUNET_FS_DownloadContext *dcc;
1913 struct GNUNET_FS_ProgressInfo pi;
1915 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1916 pi.value.download.specifics.resume.meta = dc->meta;
1917 pi.value.download.specifics.resume.message = dc->emsg;
1918 GNUNET_FS_download_make_status_ (&pi,
1920 dcc = dc->child_head;
1923 signal_download_resume (dcc);
1926 if (dc->pending != NULL)
1927 GNUNET_FS_download_start_downloading_ (dc);
1932 * Iterator over search results signaling resume to the client for
1935 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1936 * @param key current key code
1937 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
1938 * @return GNUNET_YES (we should continue to iterate)
1941 signal_result_resume (void *cls,
1942 const GNUNET_HashCode * key,
1945 struct GNUNET_FS_SearchContext *sc = cls;
1946 struct GNUNET_FS_ProgressInfo pi;
1947 struct GNUNET_FS_SearchResult *sr = value;
1949 if (0 == sr->mandatory_missing)
1951 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1952 pi.value.search.specifics.resume_result.meta = sr->meta;
1953 pi.value.search.specifics.resume_result.uri = sr->uri;
1954 pi.value.search.specifics.resume_result.result = sr;
1955 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1956 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1957 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1958 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1961 if (sr->download != NULL)
1963 signal_download_resume (sr->download);
1967 GNUNET_FS_search_start_probe_ (sr);
1974 * Iterator over search results freeing each.
1976 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1977 * @param key current key code
1978 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
1979 * @return GNUNET_YES (we should continue to iterate)
1982 free_result (void *cls,
1983 const GNUNET_HashCode * key,
1986 struct GNUNET_FS_SearchResult *sr = value;
1988 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1989 GNUNET_FS_uri_destroy (sr->uri);
1996 * Free memory allocated by the search context and its children
1998 * @param sc search context to free
2001 free_search_context (struct GNUNET_FS_SearchContext *sc)
2003 struct GNUNET_FS_SearchContext *scc;
2005 while (NULL != (scc = sc->child_head))
2007 GNUNET_CONTAINER_DLL_remove (sc->child_head,
2010 free_search_context (scc);
2012 GNUNET_free_non_null (sc->emsg);
2013 if (sc->serialization != NULL)
2014 GNUNET_FS_remove_sync_file_ (sc->h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, sc->serialization);
2015 /* FIXME: remove 'pbuf' directory with search results as well! */
2016 GNUNET_free_non_null (sc->serialization);
2017 if (sc->uri != NULL)
2018 GNUNET_FS_uri_destroy (sc->uri);
2019 if (sc->master_result_map != NULL)
2021 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2024 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2031 * Function called with a filename of serialized sub-download
2034 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2035 * @param filename complete filename (absolute path)
2036 * @return GNUNET_OK (continue to iterate)
2039 deserialize_subdownload (void *cls,
2040 const char *filename)
2042 struct GNUNET_FS_DownloadContext *parent = cls;
2045 struct GNUNET_BIO_ReadHandle *rh;
2047 ser = get_serialization_short_name (filename);
2048 rh = GNUNET_BIO_read_open (filename);
2049 deserialize_download (parent->h,
2055 GNUNET_BIO_read_close (rh, &emsg))
2057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2058 _("Failed to resume sub-download `%s': %s\n"),
2069 * Free this download context and all of its descendants.
2070 * (only works during deserialization since not all possible
2071 * state it taken care of).
2073 * @param dc context to free
2076 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2078 struct GNUNET_FS_DownloadContext *dcc;
2079 struct DownloadRequest *dr;
2080 if (dc->meta != NULL)
2081 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2082 if (dc->uri != NULL)
2083 GNUNET_FS_uri_destroy (dc->uri);
2084 GNUNET_free_non_null (dc->temp_filename);
2085 GNUNET_free_non_null (dc->emsg);
2086 GNUNET_free_non_null (dc->filename);
2087 while (NULL != (dcc = dc->child_head))
2089 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2092 free_download_context (dcc);
2094 while (NULL != (dr = dc->pending))
2096 dc->pending = dr->next;
2104 * Deserialize a download.
2106 * @param h overall context
2107 * @param rh file to deserialize from
2108 * @param parent parent download
2109 * @param search associated search
2110 * @param serialization name under which the search was serialized
2113 deserialize_download (struct GNUNET_FS_Handle *h,
2114 struct GNUNET_BIO_ReadHandle *rh,
2115 struct GNUNET_FS_DownloadContext *parent,
2116 struct GNUNET_FS_SearchResult *search,
2117 const char *serialization)
2119 struct GNUNET_FS_DownloadContext *dc;
2120 struct DownloadRequest *dr;
2127 uint32_t num_pending;
2132 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2133 dc->parent = parent;
2135 dc->serialization = GNUNET_strdup (serialization);
2137 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2138 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2139 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2140 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2142 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2144 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2146 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2148 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2150 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2152 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2154 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2156 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2158 read_start_time (rh, &dc->start_time)) ||
2160 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2162 GNUNET_BIO_read_int32 (rh, &options)) ||
2164 GNUNET_BIO_read_int32 (rh, &status)) ||
2166 GNUNET_BIO_read_int32 (rh, &num_pending)) )
2168 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2169 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
2170 dc->has_finished = (int) status;
2171 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2172 if (GNUNET_FS_uri_test_loc (dc->uri))
2173 GNUNET_assert (GNUNET_OK ==
2174 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2176 if ( (dc->length > dc->completed) &&
2177 (num_pending == 0) )
2179 while (0 < num_pending--)
2181 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
2183 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
2185 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
2187 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
2189 dr->is_pending = GNUNET_YES;
2190 dr->next = dc->pending;
2194 GNUNET_snprintf (pbuf,
2197 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
2200 dn = get_serialization_file_name (h, pbuf, "");
2203 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2207 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2212 dc->search = search;
2213 search->download = dc;
2215 signal_download_resume (dc);
2219 GNUNET_free_non_null (uris);
2220 GNUNET_free_non_null (dr);
2221 free_download_context (dc);
2226 * Signal resuming of a search to our clients (for the
2227 * top level search and all sub-searches).
2229 * @param sc search being resumed
2232 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2234 struct GNUNET_FS_SearchContext *scc;
2235 struct GNUNET_FS_ProgressInfo pi;
2237 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2238 pi.value.search.specifics.resume.message = sc->emsg;
2239 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2240 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2242 scc = sc->child_head;
2245 signal_search_resume (scc);
2252 * Deserialize a search.
2254 * @param h overall context
2255 * @param rh file to deserialize from
2256 * @param parent parent search
2257 * @param serialization name under which the search was serialized
2259 static struct GNUNET_FS_SearchContext *
2260 deserialize_search (struct GNUNET_FS_Handle *h,
2261 struct GNUNET_BIO_ReadHandle *rh,
2262 struct GNUNET_FS_SearchContext *parent,
2263 const char *serialization)
2265 struct GNUNET_FS_SearchContext *sc;
2266 struct GNUNET_FS_SearchContext *scc;
2267 struct GNUNET_BIO_ReadHandle *rhc;
2278 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2279 sc->parent = parent;
2281 sc->serialization = GNUNET_strdup (serialization);
2283 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2284 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2285 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2286 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2288 read_start_time (rh, &sc->start_time)) ||
2290 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2292 GNUNET_BIO_read_int32 (rh, &options)) ||
2294 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2296 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2298 sc->options = (enum GNUNET_FS_SearchOptions) options;
2299 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2300 GNUNET_snprintf (pbuf,
2303 GNUNET_FS_SYNC_PATH_SEARCH_RESULT,
2306 dn = get_serialization_file_name (h, pbuf, "");
2309 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2312 if ( ('\0' == in_pause) &&
2314 GNUNET_FS_search_start_searching_ (sc)) )
2316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2317 _("Could not resume running search, will resume as paused search\n"));
2322 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
2324 if (child_ser == NULL)
2326 rhc = get_read_handle (h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, child_ser);
2329 scc = deserialize_search (h, rhc, sc, child_ser);
2331 GNUNET_CONTAINER_DLL_insert (sc->child_head,
2336 GNUNET_BIO_read_close (rhc, &emsg))
2338 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2339 _("Failed to resume sub-search `%s': %s\n"),
2345 GNUNET_free (child_ser);
2348 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2351 signal_search_resume (sc);
2352 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2353 &signal_result_resume,
2358 GNUNET_free_non_null (emsg);
2359 free_search_context (sc);
2360 GNUNET_free_non_null (uris);
2366 * Function called with a filename of serialized search operation
2369 * @param cls the 'struct GNUNET_FS_Handle*'
2370 * @param filename complete filename (absolute path)
2371 * @return GNUNET_OK (continue to iterate)
2374 deserialize_search_file (void *cls,
2375 const char *filename)
2377 struct GNUNET_FS_Handle *h = cls;
2380 struct GNUNET_BIO_ReadHandle *rh;
2381 struct GNUNET_FS_SearchContext *sc;
2383 ser = get_serialization_short_name (filename);
2384 rh = GNUNET_BIO_read_open (filename);
2389 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2394 sc = deserialize_search (h, rh, NULL, ser);
2397 GNUNET_BIO_read_close (rh, &emsg))
2399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2400 _("Failure while resuming search operation `%s': %s\n"),
2410 * Function called with a filename of serialized download operation
2413 * @param cls the 'struct GNUNET_FS_Handle*'
2414 * @param filename complete filename (absolute path)
2415 * @return GNUNET_OK (continue to iterate)
2418 deserialize_download_file (void *cls,
2419 const char *filename)
2421 struct GNUNET_FS_Handle *h = cls;
2424 struct GNUNET_BIO_ReadHandle *rh;
2426 ser = get_serialization_short_name (filename);
2427 rh = GNUNET_BIO_read_open (filename);
2430 if (filename != NULL)
2432 if (0 != UNLINK (filename))
2433 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2440 deserialize_download (h, rh, NULL, NULL, ser);
2443 GNUNET_BIO_read_close (rh, &emsg))
2445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2446 _("Failure while resuming download operation `%s': %s\n"),
2456 * Deserialize informatin about pending operations.
2458 * @param master_path which master directory should be scanned
2459 * @param proc function to call for each entry (will get 'h' for 'cls')
2460 * @param h the 'struct GNUNET_FS_Handle*'
2463 deserialization_master (const char *master_path,
2464 GNUNET_FileNameCallback proc,
2465 struct GNUNET_FS_Handle *h)
2469 dn = get_serialization_file_name (h, master_path, "");
2472 GNUNET_DISK_directory_scan (dn, proc, h);
2478 * Setup a connection to the file-sharing service.
2480 * @param sched scheduler to use
2481 * @param cfg configuration to use
2482 * @param client_name unique identifier for this client
2483 * @param upcb function to call to notify about FS actions
2484 * @param upcb_cls closure for upcb
2485 * @param flags specific attributes for fs-operations
2486 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2487 * @return NULL on error
2489 struct GNUNET_FS_Handle *
2490 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2491 const struct GNUNET_CONFIGURATION_Handle *cfg,
2492 const char *client_name,
2493 GNUNET_FS_ProgressCallback upcb,
2495 enum GNUNET_FS_Flags flags,
2498 struct GNUNET_FS_Handle *ret;
2499 enum GNUNET_FS_OPTIONS opt;
2502 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2505 ret->client_name = GNUNET_strdup (client_name);
2507 ret->upcb_cls = upcb_cls;
2509 ret->max_parallel_downloads = 1;
2510 ret->max_parallel_requests = 1;
2511 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2512 va_start (ap, flags);
2513 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2517 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2518 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2520 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2521 ret->max_parallel_requests = va_arg (ap, unsigned int);
2525 GNUNET_free (ret->client_name);
2532 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2534 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
2535 &deserialize_publish_file,
2537 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
2538 &deserialize_search_file,
2540 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2541 &deserialize_download_file,
2543 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2544 &deserialize_unindex_file,
2552 * Close our connection with the file-sharing service.
2553 * The callback given to GNUNET_FS_start will no longer be
2554 * called after this function returns.
2556 * @param h handle that was returned from GNUNET_FS_start
2559 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2561 /* generate SUSPEND events and clean up state */
2562 while (h->top_head != NULL)
2563 h->top_head->ssf (h->top_head->ssf_cls);
2564 /* FIXME: terminate receive-loop with client (do we need one?) */
2565 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2566 GNUNET_SCHEDULER_cancel (h->sched,
2568 GNUNET_free (h->client_name);