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,
432 GNUNET_free (basename);
438 * Return a read handle for deserialization.
440 * @param h master context
441 * @param ext component of the path
442 * @param ent entity identifier (or emtpy string for the directory)
443 * @return NULL on error
445 static struct GNUNET_BIO_ReadHandle *
446 get_read_handle (struct GNUNET_FS_Handle *h,
451 struct GNUNET_BIO_ReadHandle *ret;
453 fn = get_serialization_file_name (h, ext, ent);
456 ret = GNUNET_BIO_read_open (fn);
463 * Return a write handle for serialization.
465 * @param h master context
466 * @param ext component of the path
467 * @param ent entity identifier (or emtpy string for the directory)
468 * @return NULL on error
470 static struct GNUNET_BIO_WriteHandle *
471 get_write_handle (struct GNUNET_FS_Handle *h,
476 struct GNUNET_BIO_WriteHandle *ret;
478 fn = get_serialization_file_name (h, ext, ent);
481 ret = GNUNET_BIO_write_open (fn);
488 * Remove serialization/deserialization file from disk.
490 * @param h master context
491 * @param ext component of the path
492 * @param ent entity identifier
495 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
501 if ( (NULL == ent) ||
502 (0 == strlen (ent)) )
507 filename = get_serialization_file_name (h, ext, ent);
508 if (0 != UNLINK (filename))
509 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
512 GNUNET_free (filename);
517 * Serialize a 'start_time'. Since we use start-times to
518 * calculate the duration of some operation, we actually
519 * do not serialize the absolute time but the (relative)
520 * duration since the start time. When we then
521 * deserialize the start time, we take the current time and
522 * subtract that duration so that we get again an absolute
523 * time stamp that will result in correct performance
526 * @param wh handle for writing
527 * @param timestamp time to serialize
528 * @return GNUNET_OK on success
531 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
532 struct GNUNET_TIME_Absolute timestamp)
534 struct GNUNET_TIME_Relative dur;
536 dur = GNUNET_TIME_absolute_get_duration (timestamp);
537 return GNUNET_BIO_write_int64 (wh, dur.value);
542 * Serialize a 'start_time'. Since we use start-times to
543 * calculate the duration of some operation, we actually
544 * do not serialize the absolute time but the (relative)
545 * duration since the start time. When we then
546 * deserialize the start time, we take the current time and
547 * subtract that duration so that we get again an absolute
548 * time stamp that will result in correct performance
551 * @param rh handle for reading
552 * @param timestamp where to write the deserialized timestamp
553 * @return GNUNET_OK on success
556 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
557 struct GNUNET_TIME_Absolute *timestamp)
559 struct GNUNET_TIME_Relative dur;
561 GNUNET_BIO_read_int64 (rh, &dur.value))
562 return GNUNET_SYSERR;
563 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
570 * Using the given serialization filename, try to deserialize
571 * the file-information tree associated with it.
573 * @param h master context
574 * @param filename name of the file (without directory) with
576 * @return NULL on error
578 static struct GNUNET_FS_FileInformation *
579 deserialize_file_information (struct GNUNET_FS_Handle *h,
580 const char *filename);
584 * Using the given serialization filename, try to deserialize
585 * the file-information tree associated with it.
587 * @param h master context
588 * @param fn name of the file (without directory) with
590 * @param rh handle for reading
591 * @return NULL on error
593 static struct GNUNET_FS_FileInformation *
594 deserialize_fi_node (struct GNUNET_FS_Handle *h,
596 struct GNUNET_BIO_ReadHandle *rh)
598 struct GNUNET_FS_FileInformation *ret;
599 struct GNUNET_FS_FileInformation *nxt;
607 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
612 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
617 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
619 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
622 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
624 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
626 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
629 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
631 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
633 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
635 read_start_time (rh, &ret->start_time)) ||
637 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
639 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
641 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
643 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
647 case 0: /* file-insert */
649 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
651 ret->is_directory = GNUNET_NO;
652 ret->data.file.do_index = GNUNET_NO;
653 ret->data.file.have_hash = GNUNET_NO;
654 ret->data.file.index_start_confirmed = GNUNET_NO;
655 if (GNUNET_NO == ret->is_published)
657 if (NULL == ret->filename)
659 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
660 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
661 if (ret->data.file.reader_cls == NULL)
664 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
669 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
670 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
674 case 1: /* file-index, no hash */
675 if (NULL == ret->filename)
678 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
680 ret->is_directory = GNUNET_NO;
681 ret->data.file.do_index = GNUNET_YES;
682 ret->data.file.have_hash = GNUNET_NO;
683 ret->data.file.index_start_confirmed = GNUNET_NO;
684 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
685 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
687 case 2: /* file-index-with-hash */
688 if (NULL == ret->filename)
691 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
693 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
695 ret->is_directory = GNUNET_NO;
696 ret->data.file.do_index = GNUNET_YES;
697 ret->data.file.have_hash = GNUNET_YES;
698 ret->data.file.index_start_confirmed = GNUNET_NO;
699 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
700 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
702 case 3: /* file-index-with-hash-confirmed */
703 if (NULL == ret->filename)
706 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
708 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
711 ret->is_directory = GNUNET_NO;
712 ret->data.file.do_index = GNUNET_YES;
713 ret->data.file.have_hash = GNUNET_YES;
714 ret->data.file.index_start_confirmed = GNUNET_YES;
715 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
716 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
718 case 4: /* directory */
720 GNUNET_BIO_read_int32 (rh, &dsize)) ||
721 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
723 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
725 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
727 ret->data.dir.dir_size = (uint32_t) dsize;
728 ret->is_directory = GNUNET_YES;
729 if (filename != NULL)
731 ret->data.dir.entries = deserialize_file_information (h, filename);
732 GNUNET_free (filename);
734 nxt = ret->data.dir.entries;
746 ret->serialization = GNUNET_strdup (fn);
748 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
750 if (filename != NULL)
752 ret->next = deserialize_file_information (h, filename);
753 GNUNET_free (filename);
756 GNUNET_free_non_null (ksks);
757 GNUNET_free_non_null (chks);
760 GNUNET_free_non_null (ksks);
761 GNUNET_free_non_null (chks);
762 GNUNET_free_non_null (filename);
763 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
769 * Using the given serialization filename, try to deserialize
770 * the file-information tree associated with it.
772 * @param h master context
773 * @param filename name of the file (without directory) with
775 * @return NULL on error
777 static struct GNUNET_FS_FileInformation *
778 deserialize_file_information (struct GNUNET_FS_Handle *h,
779 const char *filename)
781 struct GNUNET_FS_FileInformation *ret;
782 struct GNUNET_BIO_ReadHandle *rh;
785 rh = get_read_handle (h, "publish-fi", filename);
788 ret = deserialize_fi_node (h, filename, rh);
790 GNUNET_BIO_read_close (rh, &emsg))
792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
793 _("Failed to resume publishing information `%s': %s\n"),
803 * Given a serialization name (full absolute path), return the
804 * basename of the file (without the path), which must only
805 * consist of the 6 random characters.
807 * @param fullname name to extract the basename from
808 * @return copy of the basename, NULL on error
811 get_serialization_short_name (const char *fullname)
818 /* FIXME: we could do this faster since we know
819 the length of 'end'... */
822 if (DIR_SEPARATOR == *nxt)
826 if ( (end == NULL) ||
827 (strlen (end) == 0) )
832 GNUNET_break (6 == strlen (end));
833 return GNUNET_strdup (end);
838 * Create a new random name for serialization. Also checks if persistence
839 * is enabled and returns NULL if not.
841 * @param h master context
842 * @param ext component of the path
843 * @return NULL on errror
846 make_serialization_file_name (struct GNUNET_FS_Handle *h,
853 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
854 return NULL; /* persistence not requested */
855 dn = get_serialization_file_name (h, ext, "");
856 fn = GNUNET_DISK_mktemp (dn);
859 return NULL; /* epic fail */
860 ret = get_serialization_short_name (fn);
867 * Copy all of the data from the reader to the write handle.
869 * @param wh write handle
870 * @param fi file with reader
871 * @return GNUNET_OK on success
874 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
875 struct GNUNET_FS_FileInformation * fi)
884 while (off < fi->data.file.file_size)
886 ret = fi->data.file.reader (fi->data.file.reader_cls,
893 return GNUNET_SYSERR;
896 GNUNET_BIO_write (wh, buf, ret))
897 return GNUNET_SYSERR;
905 * Create a temporary file on disk to store the current
908 * @param fi file information to sync with disk
911 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
914 struct GNUNET_BIO_WriteHandle *wh;
919 if (NULL == fi->serialization)
920 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
921 if (NULL == fi->serialization)
923 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
926 GNUNET_free (fi->serialization);
927 fi->serialization = NULL;
930 if (GNUNET_YES == fi->is_directory)
932 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
934 else if (GNUNET_YES == fi->data.file.have_hash)
936 else if (GNUNET_YES == fi->data.file.do_index)
940 if (fi->keywords != NULL)
941 ksks = GNUNET_FS_uri_to_string (fi->keywords);
944 if (fi->chk_uri != NULL)
945 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
949 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
951 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
953 GNUNET_BIO_write_string (wh, ksks)) ||
955 GNUNET_BIO_write_string (wh, chks)) ||
957 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
959 write_start_time (wh, fi->start_time)) ||
961 GNUNET_BIO_write_string (wh, fi->emsg)) ||
963 GNUNET_BIO_write_string (wh, fi->filename)) ||
965 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
967 GNUNET_BIO_write_int32 (wh, fi->priority)) )
969 GNUNET_free_non_null (chks);
971 GNUNET_free_non_null (ksks);
976 case 0: /* file-insert */
978 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
980 if ( (GNUNET_NO == fi->is_published) &&
981 (NULL == fi->filename) )
983 copy_from_reader (wh, fi))
986 case 1: /* file-index, no hash */
987 if (NULL == fi->filename)
990 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
993 case 2: /* file-index-with-hash */
994 case 3: /* file-index-with-hash-confirmed */
995 if (NULL == fi->filename)
998 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1000 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
1003 case 4: /* directory */
1005 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1007 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
1009 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
1017 GNUNET_BIO_write_string (wh, fi->next->serialization))
1020 GNUNET_BIO_write_close (wh))
1023 (void) GNUNET_BIO_write_close (wh);
1024 GNUNET_free_non_null (chks);
1025 GNUNET_free_non_null (ksks);
1026 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
1027 if (0 != UNLINK (fn))
1028 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1030 GNUNET_free (fi->serialization);
1031 fi->serialization = NULL;
1037 * Find the entry in the file information struct where the
1038 * serialization filename matches the given name.
1040 * @param pos file information to search
1041 * @param srch filename to search for
1042 * @return NULL if srch was not found in this subtree
1044 static struct GNUNET_FS_FileInformation *
1045 find_file_position (struct GNUNET_FS_FileInformation *pos,
1048 struct GNUNET_FS_FileInformation *r;
1052 if (0 == strcmp (srch,
1053 pos->serialization))
1055 if (pos->is_directory)
1057 r = find_file_position (pos->data.dir.entries,
1069 * Signal the FS's progress function that we are resuming
1072 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1073 * @param fi the entry in the publish-structure
1074 * @param length length of the file or directory
1075 * @param meta metadata for the file or directory (can be modified)
1076 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1077 * @param anonymity pointer to selected anonymity level (can be modified)
1078 * @param priority pointer to selected priority (can be modified)
1079 * @param expirationTime pointer to selected expiration time (can be modified)
1080 * @param client_info pointer to client context set upon creation (can be modified)
1081 * @return GNUNET_OK to continue (always)
1084 fip_signal_resume(void *cls,
1085 struct GNUNET_FS_FileInformation *fi,
1087 struct GNUNET_CONTAINER_MetaData *meta,
1088 struct GNUNET_FS_Uri **uri,
1089 uint32_t *anonymity,
1091 struct GNUNET_TIME_Absolute *expirationTime,
1094 struct GNUNET_FS_PublishContext *sc = cls;
1095 struct GNUNET_FS_ProgressInfo pi;
1097 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1098 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1099 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1100 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1106 * Function called with a filename of serialized publishing operation
1109 * @param cls the 'struct GNUNET_FS_Handle*'
1110 * @param filename complete filename (absolute path)
1111 * @return GNUNET_OK (continue to iterate)
1114 deserialize_publish_file (void *cls,
1115 const char *filename)
1117 struct GNUNET_FS_Handle *h = cls;
1118 struct GNUNET_BIO_ReadHandle *rh;
1119 struct GNUNET_FS_PublishContext *pc;
1127 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1129 pc->serialization = get_serialization_short_name (filename);
1133 rh = GNUNET_BIO_read_open (filename);
1137 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1139 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1141 GNUNET_BIO_read_int32 (rh, &options)) ||
1143 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1145 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1147 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1149 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1151 pc->options = options;
1152 pc->all_done = all_done;
1153 pc->fi = deserialize_file_information (h, fi_root);
1158 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1159 if (pc->namespace == NULL)
1161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1162 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1169 pc->fi_pos = find_file_position (pc->fi,
1171 GNUNET_free (fi_pos);
1173 if (pc->fi_pos == NULL)
1175 /* failed to find position for resuming, outch! Will start from root! */
1177 if (pc->all_done != GNUNET_YES)
1178 pc->fi_pos = pc->fi;
1181 /* generate RESUME event(s) */
1182 GNUNET_FS_file_information_inspect (pc->fi,
1186 /* re-start publishing (if needed)... */
1187 if (pc->all_done != GNUNET_YES)
1189 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1190 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1191 &GNUNET_FS_publish_main_,
1194 GNUNET_BIO_read_close (rh, &emsg))
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 _("Failure while resuming publishing operation `%s': %s\n"),
1202 GNUNET_free_non_null (ns);
1205 GNUNET_free_non_null (pc->nid);
1206 GNUNET_free_non_null (pc->nuid);
1207 GNUNET_free_non_null (fi_root);
1208 GNUNET_free_non_null (fi_pos);
1209 GNUNET_free_non_null (ns);
1210 if ( (rh != NULL) &&
1212 GNUNET_BIO_read_close (rh, &emsg)) )
1214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1215 _("Failed to resume publishing operation `%s': %s\n"),
1221 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1222 if (pc->serialization != NULL)
1223 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1224 GNUNET_free_non_null (pc->serialization);
1231 * Synchronize this publishing struct with its mirror
1232 * on disk. Note that all internal FS-operations that change
1233 * publishing structs should already call "sync" internally,
1234 * so this function is likely not useful for clients.
1236 * @param pc the struct to sync
1239 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1241 struct GNUNET_BIO_WriteHandle *wh;
1243 if (NULL == pc->serialization)
1244 pc->serialization = make_serialization_file_name (pc->h,
1246 if (NULL == pc->serialization)
1250 if (NULL == pc->fi->serialization)
1255 wh = get_write_handle (pc->h, "publish", pc->serialization);
1257 GNUNET_BIO_write_string (wh, pc->nid)) ||
1259 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1261 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1263 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1265 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1267 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1269 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1271 (void) GNUNET_BIO_write_close (wh);
1272 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1273 GNUNET_free (pc->serialization);
1274 pc->serialization = NULL;
1278 GNUNET_BIO_write_close (wh))
1280 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1281 GNUNET_free (pc->serialization);
1282 pc->serialization = NULL;
1289 * Synchronize this unindex struct with its mirror
1290 * on disk. Note that all internal FS-operations that change
1291 * publishing structs should already call "sync" internally,
1292 * so this function is likely not useful for clients.
1294 * @param uc the struct to sync
1297 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1299 struct GNUNET_BIO_WriteHandle *wh;
1301 if (UNINDEX_STATE_ABORTED == uc->state)
1303 if (NULL == uc->serialization)
1304 uc->serialization = make_serialization_file_name (uc->h,
1306 if (NULL == uc->serialization)
1308 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1310 GNUNET_BIO_write_string (wh, uc->filename)) ||
1312 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1314 write_start_time (wh, uc->start_time)) ||
1316 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1317 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1319 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1320 ( (uc->state == UNINDEX_STATE_ERROR) &&
1322 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1324 (void) GNUNET_BIO_write_close (wh);
1325 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1326 GNUNET_free (uc->serialization);
1327 uc->serialization = NULL;
1331 GNUNET_BIO_write_close (wh))
1333 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1334 GNUNET_free (uc->serialization);
1335 uc->serialization = NULL;
1342 * Serialize an active or pending download request.
1344 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
1345 * @param key unused, can be NULL
1346 * @param value the 'struct DownloadRequest'
1347 * @return GNUNET_YES on success, GNUNET_NO on error
1350 write_download_request (void *cls,
1351 const GNUNET_HashCode *key,
1354 struct GNUNET_BIO_WriteHandle *wh = cls;
1355 struct DownloadRequest *dr = value;
1358 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) ||
1360 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1362 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1369 * Count active download requests.
1371 * @param cls the 'uint32_t*' counter
1372 * @param key unused, can be NULL
1373 * @param value the 'struct DownloadRequest'
1374 * @return GNUNET_YES (continue iteration)
1377 count_download_requests (void *cls,
1378 const GNUNET_HashCode *key,
1381 uint32_t *counter = cls;
1389 * Synchronize this download struct with its mirror
1390 * on disk. Note that all internal FS-operations that change
1391 * publishing structs should already call "sync" internally,
1392 * so this function is likely not useful for clients.
1394 * @param dc the struct to sync
1397 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1399 struct GNUNET_BIO_WriteHandle *wh;
1400 struct DownloadRequest *dr;
1402 const char *category;
1405 uint32_t num_pending;
1407 if (dc->parent != NULL)
1409 if (dc->parent->serialization == NULL)
1411 GNUNET_snprintf (pbuf,
1416 dc->parent->serialization);
1421 category = "download";
1423 if (NULL == dc->serialization)
1424 dc->serialization = make_serialization_file_name (dc->h,
1426 if (NULL == dc->serialization)
1428 wh = get_write_handle (dc->h, category, dc->serialization);
1431 GNUNET_free (dc->serialization);
1432 dc->serialization = NULL;
1435 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1436 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1437 uris = GNUNET_FS_uri_to_string (dc->uri);
1439 if (dc->emsg != NULL)
1447 (void) GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1448 &count_download_requests,
1451 GNUNET_assert ( (dc->length == dc->completed) ||
1452 (dc->emsg != NULL) ||
1453 (num_pending > 0) );
1455 GNUNET_BIO_write_string (wh, uris)) ||
1457 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1459 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1461 GNUNET_BIO_write_string (wh, dc->filename)) ||
1463 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1465 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1467 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1469 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1471 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1473 write_start_time (wh, dc->start_time)) ||
1475 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1477 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1479 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) ||
1481 GNUNET_BIO_write_int32 (wh, num_pending)) )
1487 write_download_request (wh, NULL, dr))
1491 if (GNUNET_SYSERR ==
1492 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1493 &write_download_request,
1496 while (0 < num_pending--)
1498 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1500 dr->is_pending = GNUNET_YES;
1501 dr->next = dc->pending;
1505 GNUNET_free_non_null (uris);
1507 GNUNET_BIO_write_close (wh))
1510 (void) GNUNET_BIO_write_close (wh);
1511 GNUNET_free_non_null (uris);
1512 fn = get_serialization_file_name (dc->h, category, dc->serialization);
1513 if (0 != UNLINK (fn))
1514 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1516 GNUNET_free (dc->serialization);
1517 dc->serialization = NULL;
1522 * Synchronize this search result with its mirror
1523 * on disk. Note that all internal FS-operations that change
1524 * publishing structs should already call "sync" internally,
1525 * so this function is likely not useful for clients.
1527 * @param key key for the search result
1528 * @param sr the struct to sync
1531 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1532 struct GNUNET_FS_SearchResult *sr)
1534 struct GNUNET_BIO_WriteHandle *wh;
1537 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1538 (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1540 if (NULL == sr->serialization)
1541 sr->serialization = make_serialization_file_name (sr->sc->h,
1543 if (NULL == sr->serialization)
1545 wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1546 uris = GNUNET_FS_uri_to_string (sr->uri);
1548 GNUNET_BIO_write_string (wh, uris)) ||
1550 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1552 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1554 GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1556 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1558 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1560 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1562 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1565 GNUNET_BIO_write_close (wh))
1570 GNUNET_free_non_null (uris);
1573 GNUNET_free_non_null (uris);
1575 (void) GNUNET_BIO_write_close (wh);
1576 GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1577 GNUNET_free (sr->serialization);
1578 sr->serialization = NULL;
1583 * Synchronize this search struct with its mirror
1584 * on disk. Note that all internal FS-operations that change
1585 * publishing structs should already call "sync" internally,
1586 * so this function is likely not useful for clients.
1588 * @param sc the struct to sync
1591 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1593 struct GNUNET_BIO_WriteHandle *wh;
1594 struct GNUNET_FS_SearchContext *scc;
1598 if (NULL == sc->serialization)
1599 sc->serialization = make_serialization_file_name (sc->h,
1601 if (NULL == sc->serialization)
1603 wh = get_write_handle (sc->h, "search", sc->serialization);
1604 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1605 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1606 uris = GNUNET_FS_uri_to_string (sc->uri);
1607 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1609 GNUNET_BIO_write_string (wh, uris)) ||
1611 write_start_time (wh, sc->start_time)) ||
1613 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1615 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1617 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1619 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1623 scc = sc->child_head;
1626 if (scc->serialization == NULL)
1629 GNUNET_BIO_write_string (wh, scc->serialization))
1633 GNUNET_BIO_write_string (wh, NULL);
1635 GNUNET_BIO_write_close (wh))
1643 (void) GNUNET_BIO_write_close (wh);
1644 GNUNET_free_non_null (uris);
1645 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1646 GNUNET_free (sc->serialization);
1647 sc->serialization = NULL;
1652 * Deserialize information about pending publish operations.
1654 * @param h master context
1657 deserialize_publish (struct GNUNET_FS_Handle *h)
1661 dn = get_serialization_file_name (h, "publish", "");
1664 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1670 * Function called with a filename of serialized unindexing operation
1673 * @param cls the 'struct GNUNET_FS_Handle*'
1674 * @param filename complete filename (absolute path)
1675 * @return GNUNET_OK (continue to iterate)
1678 deserialize_unindex_file (void *cls,
1679 const char *filename)
1681 struct GNUNET_FS_Handle *h = cls;
1682 struct GNUNET_BIO_ReadHandle *rh;
1683 struct GNUNET_FS_UnindexContext *uc;
1684 struct GNUNET_FS_ProgressInfo pi;
1688 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1690 uc->serialization = get_serialization_short_name (filename);
1691 rh = GNUNET_BIO_read_open (filename);
1695 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1697 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1699 read_start_time (rh, &uc->start_time)) ||
1701 GNUNET_BIO_read_int32 (rh, &state)) )
1703 uc->state = (enum UnindexState) state;
1706 case UNINDEX_STATE_HASHING:
1708 case UNINDEX_STATE_FS_NOTIFY:
1710 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1713 case UNINDEX_STATE_DS_REMOVE:
1715 case UNINDEX_STATE_COMPLETE:
1717 case UNINDEX_STATE_ERROR:
1719 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1722 case UNINDEX_STATE_ABORTED:
1729 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1730 pi.value.unindex.specifics.resume.message = uc->emsg;
1731 GNUNET_FS_unindex_make_status_ (&pi,
1733 (uc->state == UNINDEX_STATE_COMPLETE)
1738 case UNINDEX_STATE_HASHING:
1739 GNUNET_CRYPTO_hash_file (uc->h->sched,
1740 GNUNET_SCHEDULER_PRIORITY_IDLE,
1743 &GNUNET_FS_unindex_process_hash_,
1746 case UNINDEX_STATE_FS_NOTIFY:
1747 uc->state = UNINDEX_STATE_HASHING;
1748 GNUNET_FS_unindex_process_hash_ (uc,
1751 case UNINDEX_STATE_DS_REMOVE:
1752 GNUNET_FS_unindex_do_remove_ (uc);
1754 case UNINDEX_STATE_COMPLETE:
1755 case UNINDEX_STATE_ERROR:
1756 /* no need to resume any operation, we were done */
1762 GNUNET_BIO_read_close (rh, &emsg))
1764 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1765 _("Failure while resuming unindexing operation `%s': %s\n"),
1772 GNUNET_free_non_null (uc->filename);
1773 if ( (rh != NULL) &&
1775 GNUNET_BIO_read_close (rh, &emsg)) )
1777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1778 _("Failed to resume unindexing operation `%s': %s\n"),
1783 if (uc->serialization != NULL)
1784 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1785 GNUNET_free_non_null (uc->serialization);
1792 * Deserialize information about pending publish operations.
1794 * @param h master context
1797 deserialize_unindex (struct GNUNET_FS_Handle *h)
1801 dn = get_serialization_file_name (h, "unindex", "");
1804 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1810 * Deserialize a download.
1812 * @param h overall context
1813 * @param rh file to deserialize from
1814 * @param parent parent download
1815 * @param search associated search
1816 * @param serialization name under which the search was serialized
1819 deserialize_download (struct GNUNET_FS_Handle *h,
1820 struct GNUNET_BIO_ReadHandle *rh,
1821 struct GNUNET_FS_DownloadContext *parent,
1822 struct GNUNET_FS_SearchResult *search,
1823 const char *serialization);
1827 * Function called with a filename of serialized search result
1830 * @param cls the 'struct GNUNET_FS_SearchContext*'
1831 * @param filename complete filename (absolute path)
1832 * @return GNUNET_OK (continue to iterate)
1835 deserialize_search_result (void *cls,
1836 const char *filename)
1838 struct GNUNET_FS_SearchContext *sc = cls;
1844 struct GNUNET_BIO_ReadHandle *rh;
1845 struct GNUNET_BIO_ReadHandle *drh;
1846 struct GNUNET_FS_SearchResult *sr;
1847 GNUNET_HashCode key;
1849 ser = get_serialization_short_name (filename);
1850 rh = GNUNET_BIO_read_open (filename);
1855 GNUNET_snprintf (pbuf,
1861 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1869 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
1870 sr->serialization = ser;
1872 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1873 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1874 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1875 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1877 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
1879 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1881 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1883 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1885 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1887 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1889 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1892 if (download != NULL)
1894 drh = get_read_handle (sc->h,
1897 deserialize_download (sc->h,
1903 GNUNET_BIO_read_close (drh, &emsg))
1905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1906 _("Failed to resume sub-download `%s': %s\n"),
1911 GNUNET_free (download);
1913 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1919 GNUNET_free_non_null (download);
1920 GNUNET_free_non_null (emsg);
1921 GNUNET_free_non_null (uris);
1922 if (sr->uri != NULL)
1923 GNUNET_FS_uri_destroy (sr->uri);
1924 if (sr->meta != NULL)
1925 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1926 GNUNET_free (sr->serialization);
1933 * Send the 'resume' signal to the callback; also actually
1934 * resume the download (put it in the queue). Does this
1935 * recursively for the top-level download and all child
1938 * @param dc download to resume
1941 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
1943 struct GNUNET_FS_DownloadContext *dcc;
1944 struct GNUNET_FS_ProgressInfo pi;
1946 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1947 pi.value.download.specifics.resume.meta = dc->meta;
1948 pi.value.download.specifics.resume.message = dc->emsg;
1949 GNUNET_FS_download_make_status_ (&pi,
1951 dcc = dc->child_head;
1954 signal_download_resume (dcc);
1957 if (dc->pending != NULL)
1958 GNUNET_FS_download_start_downloading_ (dc);
1963 * Iterator over search results signaling resume to the client for
1966 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1967 * @param key current key code
1968 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
1969 * @return GNUNET_YES (we should continue to iterate)
1972 signal_result_resume (void *cls,
1973 const GNUNET_HashCode * key,
1976 struct GNUNET_FS_SearchContext *sc = cls;
1977 struct GNUNET_FS_ProgressInfo pi;
1978 struct GNUNET_FS_SearchResult *sr = value;
1980 if (0 == sr->mandatory_missing)
1982 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1983 pi.value.search.specifics.resume_result.meta = sr->meta;
1984 pi.value.search.specifics.resume_result.uri = sr->uri;
1985 pi.value.search.specifics.resume_result.result = sr;
1986 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1987 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1988 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1989 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1992 if (sr->download != NULL)
1994 signal_download_resume (sr->download);
1998 GNUNET_FS_search_start_probe_ (sr);
2005 * Iterator over search results freeing each.
2007 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2008 * @param key current key code
2009 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2010 * @return GNUNET_YES (we should continue to iterate)
2013 free_result (void *cls,
2014 const GNUNET_HashCode * key,
2017 struct GNUNET_FS_SearchResult *sr = value;
2019 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2020 GNUNET_FS_uri_destroy (sr->uri);
2027 * Free memory allocated by the search context and its children
2029 * @param sc search context to free
2032 free_search_context (struct GNUNET_FS_SearchContext *sc)
2034 struct GNUNET_FS_SearchContext *scc;
2036 while (NULL != (scc = sc->child_head))
2038 GNUNET_CONTAINER_DLL_remove (sc->child_head,
2041 free_search_context (scc);
2043 GNUNET_free_non_null (sc->emsg);
2044 if (sc->serialization != NULL)
2045 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
2046 /* FIXME: remove 'pbuf' directory with search results as well! */
2047 GNUNET_free_non_null (sc->serialization);
2048 if (sc->uri != NULL)
2049 GNUNET_FS_uri_destroy (sc->uri);
2050 if (sc->master_result_map != NULL)
2052 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2055 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2062 * Function called with a filename of serialized sub-download
2065 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2066 * @param filename complete filename (absolute path)
2067 * @return GNUNET_OK (continue to iterate)
2070 deserialize_subdownload (void *cls,
2071 const char *filename)
2073 struct GNUNET_FS_DownloadContext *parent = cls;
2076 struct GNUNET_BIO_ReadHandle *rh;
2078 ser = get_serialization_short_name (filename);
2079 rh = GNUNET_BIO_read_open (filename);
2080 deserialize_download (parent->h,
2086 GNUNET_BIO_read_close (rh, &emsg))
2088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2089 _("Failed to resume sub-download `%s': %s\n"),
2100 * Free this download context and all of its descendants.
2101 * (only works during deserialization since not all possible
2102 * state it taken care of).
2104 * @param dc context to free
2107 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2109 struct GNUNET_FS_DownloadContext *dcc;
2110 struct DownloadRequest *dr;
2111 if (dc->meta != NULL)
2112 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2113 if (dc->uri != NULL)
2114 GNUNET_FS_uri_destroy (dc->uri);
2115 GNUNET_free_non_null (dc->temp_filename);
2116 GNUNET_free_non_null (dc->emsg);
2117 GNUNET_free_non_null (dc->filename);
2118 while (NULL != (dcc = dc->child_head))
2120 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2123 free_download_context (dcc);
2125 while (NULL != (dr = dc->pending))
2127 dc->pending = dr->next;
2135 * Deserialize a download.
2137 * @param h overall context
2138 * @param rh file to deserialize from
2139 * @param parent parent download
2140 * @param search associated search
2141 * @param serialization name under which the search was serialized
2144 deserialize_download (struct GNUNET_FS_Handle *h,
2145 struct GNUNET_BIO_ReadHandle *rh,
2146 struct GNUNET_FS_DownloadContext *parent,
2147 struct GNUNET_FS_SearchResult *search,
2148 const char *serialization)
2150 struct GNUNET_FS_DownloadContext *dc;
2151 struct DownloadRequest *dr;
2158 uint32_t num_pending;
2163 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2164 dc->parent = parent;
2166 dc->serialization = GNUNET_strdup (serialization);
2168 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2169 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2170 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2171 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2173 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2175 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2177 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2179 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2181 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2183 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2185 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2187 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2189 read_start_time (rh, &dc->start_time)) ||
2191 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2193 GNUNET_BIO_read_int32 (rh, &options)) ||
2195 GNUNET_BIO_read_int32 (rh, &status)) ||
2197 GNUNET_BIO_read_int32 (rh, &num_pending)) )
2199 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2200 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
2201 dc->has_finished = (int) status;
2202 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2203 if (GNUNET_FS_uri_test_loc (dc->uri))
2204 GNUNET_assert (GNUNET_OK ==
2205 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2207 if ( (dc->length > dc->completed) &&
2208 (num_pending == 0) )
2210 while (0 < num_pending--)
2212 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
2214 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
2216 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
2218 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
2220 dr->is_pending = GNUNET_YES;
2221 dr->next = dc->pending;
2225 GNUNET_snprintf (pbuf,
2231 dn = get_serialization_file_name (h, pbuf, "");
2234 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2238 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2243 dc->search = search;
2244 search->download = dc;
2246 signal_download_resume (dc);
2250 GNUNET_free_non_null (uris);
2251 GNUNET_free_non_null (dr);
2252 free_download_context (dc);
2257 * Signal resuming of a search to our clients (for the
2258 * top level search and all sub-searches).
2260 * @param sc search being resumed
2263 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2265 struct GNUNET_FS_SearchContext *scc;
2266 struct GNUNET_FS_ProgressInfo pi;
2268 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2269 pi.value.search.specifics.resume.message = sc->emsg;
2270 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2271 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2273 scc = sc->child_head;
2276 signal_search_resume (scc);
2283 * Deserialize a search.
2285 * @param h overall context
2286 * @param rh file to deserialize from
2287 * @param parent parent search
2288 * @param serialization name under which the search was serialized
2290 static struct GNUNET_FS_SearchContext *
2291 deserialize_search (struct GNUNET_FS_Handle *h,
2292 struct GNUNET_BIO_ReadHandle *rh,
2293 struct GNUNET_FS_SearchContext *parent,
2294 const char *serialization)
2296 struct GNUNET_FS_SearchContext *sc;
2297 struct GNUNET_FS_SearchContext *scc;
2298 struct GNUNET_BIO_ReadHandle *rhc;
2309 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2310 sc->parent = parent;
2312 sc->serialization = GNUNET_strdup (serialization);
2314 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2315 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2316 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2317 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2319 read_start_time (rh, &sc->start_time)) ||
2321 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2323 GNUNET_BIO_read_int32 (rh, &options)) ||
2325 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2327 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2329 sc->options = (enum GNUNET_FS_SearchOptions) options;
2330 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2331 GNUNET_snprintf (pbuf,
2337 dn = get_serialization_file_name (h, pbuf, "");
2340 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2343 if ( ('\0' == in_pause) &&
2345 GNUNET_FS_search_start_searching_ (sc)) )
2347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2348 _("Could not resume running search, will resume as paused search\n"));
2353 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
2355 if (child_ser == NULL)
2357 rhc = get_read_handle (h, "search-children", child_ser);
2360 scc = deserialize_search (h, rhc, sc, child_ser);
2362 GNUNET_CONTAINER_DLL_insert (sc->child_head,
2367 GNUNET_BIO_read_close (rhc, &emsg))
2369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2370 _("Failed to resume sub-search `%s': %s\n"),
2376 GNUNET_free (child_ser);
2379 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2382 signal_search_resume (sc);
2383 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2384 &signal_result_resume,
2389 GNUNET_free_non_null (emsg);
2390 free_search_context (sc);
2391 GNUNET_free_non_null (uris);
2397 * Function called with a filename of serialized search operation
2400 * @param cls the 'struct GNUNET_FS_Handle*'
2401 * @param filename complete filename (absolute path)
2402 * @return GNUNET_OK (continue to iterate)
2405 deserialize_search_file (void *cls,
2406 const char *filename)
2408 struct GNUNET_FS_Handle *h = cls;
2411 struct GNUNET_BIO_ReadHandle *rh;
2412 struct GNUNET_FS_SearchContext *sc;
2414 ser = get_serialization_short_name (filename);
2415 rh = GNUNET_BIO_read_open (filename);
2420 GNUNET_FS_remove_sync_file_ (h, "search", ser);
2425 sc = deserialize_search (h, rh, NULL, ser);
2428 GNUNET_BIO_read_close (rh, &emsg))
2430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2431 _("Failure while resuming search operation `%s': %s\n"),
2441 * Deserialize information about pending search operations.
2443 * @param h master context
2446 deserialize_search_master (struct GNUNET_FS_Handle *h)
2450 dn = get_serialization_file_name (h, "search", "");
2453 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
2459 * Function called with a filename of serialized download operation
2462 * @param cls the 'struct GNUNET_FS_Handle*'
2463 * @param filename complete filename (absolute path)
2464 * @return GNUNET_OK (continue to iterate)
2467 deserialize_download_file (void *cls,
2468 const char *filename)
2470 struct GNUNET_FS_Handle *h = cls;
2473 struct GNUNET_BIO_ReadHandle *rh;
2475 ser = get_serialization_short_name (filename);
2476 rh = GNUNET_BIO_read_open (filename);
2481 GNUNET_FS_remove_sync_file_ (h, "download", ser);
2486 deserialize_download (h, rh, NULL, NULL, ser);
2489 GNUNET_BIO_read_close (rh, &emsg))
2491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2492 _("Failure while resuming download operation `%s': %s\n"),
2502 * Deserialize information about pending download operations.
2504 * @param h master context
2507 deserialize_download_master (struct GNUNET_FS_Handle *h)
2511 dn = get_serialization_file_name (h, "download", "");
2514 GNUNET_DISK_directory_scan (dn, &deserialize_download_file, h);
2520 * Setup a connection to the file-sharing service.
2522 * @param sched scheduler to use
2523 * @param cfg configuration to use
2524 * @param client_name unique identifier for this client
2525 * @param upcb function to call to notify about FS actions
2526 * @param upcb_cls closure for upcb
2527 * @param flags specific attributes for fs-operations
2528 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2529 * @return NULL on error
2531 struct GNUNET_FS_Handle *
2532 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2533 const struct GNUNET_CONFIGURATION_Handle *cfg,
2534 const char *client_name,
2535 GNUNET_FS_ProgressCallback upcb,
2537 enum GNUNET_FS_Flags flags,
2540 struct GNUNET_FS_Handle *ret;
2541 struct GNUNET_CLIENT_Connection *client;
2542 enum GNUNET_FS_OPTIONS opt;
2545 client = GNUNET_CLIENT_connect (sched,
2550 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2553 ret->client_name = GNUNET_strdup (client_name);
2555 ret->upcb_cls = upcb_cls;
2556 ret->client = client;
2558 ret->max_parallel_downloads = 1;
2559 ret->max_parallel_requests = 1;
2560 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2561 va_start (ap, flags);
2562 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2566 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2567 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2569 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2570 ret->max_parallel_requests = va_arg (ap, unsigned int);
2574 GNUNET_free (ret->client_name);
2581 // FIXME: setup receive-loop with client (do we need one?)
2582 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2584 /* FIXME: could write one generic deserialization
2585 function instead of these four... */
2586 deserialize_publish (ret);
2587 deserialize_search_master (ret);
2588 deserialize_download_master (ret);
2589 deserialize_unindex (ret);
2596 * Close our connection with the file-sharing service.
2597 * The callback given to GNUNET_FS_start will no longer be
2598 * called after this function returns.
2600 * @param h handle that was returned from GNUNET_FS_start
2603 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2605 /* generate SUSPEND events and clean up state */
2606 while (h->top_head != NULL)
2607 h->top_head->ssf (h->top_head->ssf_cls);
2608 // FIXME: terminate receive-loop with client (do we need one?)
2609 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2610 GNUNET_SCHEDULER_cancel (h->sched,
2612 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
2613 GNUNET_free (h->client_name);