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);
402 * Return the full filename where we would store state information
403 * (for serialization/deserialization).
405 * @param h master context
406 * @param ext component of the path
407 * @param ent entity identifier (or emtpy string for the directory)
408 * @return NULL on error
411 get_serialization_file_name (struct GNUNET_FS_Handle *h,
418 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
419 return NULL; /* persistence not requested */
421 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
426 GNUNET_asprintf (&ret,
435 GNUNET_free (basename);
441 * Return the full filename where we would store state information
442 * (for serialization/deserialization) that is associated with a
445 * @param h master context
446 * @param ext component of the path
447 * @param uni name of the parent operation
448 * @param ent entity identifier (or emtpy string for the directory)
449 * @return NULL on error
452 get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
460 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
461 return NULL; /* persistence not requested */
463 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
468 GNUNET_asprintf (&ret,
479 GNUNET_free (basename);
485 * Return a read handle for deserialization.
487 * @param h master context
488 * @param ext component of the path
489 * @param ent entity identifier (or emtpy string for the directory)
490 * @return NULL on error
492 static struct GNUNET_BIO_ReadHandle *
493 get_read_handle (struct GNUNET_FS_Handle *h,
498 struct GNUNET_BIO_ReadHandle *ret;
500 fn = get_serialization_file_name (h, ext, ent);
503 ret = GNUNET_BIO_read_open (fn);
510 * Return a write handle for serialization.
512 * @param h master context
513 * @param ext component of the path
514 * @param ent entity identifier (or emtpy string for the directory)
515 * @return NULL on error
517 static struct GNUNET_BIO_WriteHandle *
518 get_write_handle (struct GNUNET_FS_Handle *h,
523 struct GNUNET_BIO_WriteHandle *ret;
525 fn = get_serialization_file_name (h, ext, ent);
528 ret = GNUNET_BIO_write_open (fn);
535 * Return a write handle for serialization.
537 * @param h master context
538 * @param ext component of the path
539 * @param uni name of parent
540 * @param ent entity identifier (or emtpy string for the directory)
541 * @return NULL on error
543 static struct GNUNET_BIO_WriteHandle *
544 get_write_handle_in_dir (struct GNUNET_FS_Handle *h,
550 struct GNUNET_BIO_WriteHandle *ret;
552 fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
555 ret = GNUNET_BIO_write_open (fn);
562 * Remove serialization/deserialization file from disk.
564 * @param h master context
565 * @param ext component of the path
566 * @param ent entity identifier
569 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
575 if ( (NULL == ent) ||
576 (0 == strlen (ent)) )
581 filename = get_serialization_file_name (h, ext, ent);
582 if (0 != UNLINK (filename))
583 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
586 GNUNET_free (filename);
591 * Remove serialization/deserialization file from disk.
593 * @param h master context
594 * @param ext component of the path
595 * @param uni parent name
596 * @param ent entity identifier
599 remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
606 if ( (NULL == ent) ||
607 (0 == strlen (ent)) )
612 filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
613 if (0 != UNLINK (filename))
614 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
617 GNUNET_free (filename);
622 * Remove serialization/deserialization directory from disk.
624 * @param h master context
625 * @param ext component of the path
626 * @param uni unique name of parent
629 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
637 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
640 if ( (GNUNET_OK == GNUNET_DISK_directory_test (dn)) &&
641 (GNUNET_OK != GNUNET_DISK_directory_remove (dn)) )
642 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
650 * Serialize a 'start_time'. Since we use start-times to
651 * calculate the duration of some operation, we actually
652 * do not serialize the absolute time but the (relative)
653 * duration since the start time. When we then
654 * deserialize the start time, we take the current time and
655 * subtract that duration so that we get again an absolute
656 * time stamp that will result in correct performance
659 * @param wh handle for writing
660 * @param timestamp time to serialize
661 * @return GNUNET_OK on success
664 write_start_time (struct GNUNET_BIO_WriteHandle *wh,
665 struct GNUNET_TIME_Absolute timestamp)
667 struct GNUNET_TIME_Relative dur;
669 dur = GNUNET_TIME_absolute_get_duration (timestamp);
670 return GNUNET_BIO_write_int64 (wh, dur.value);
675 * Serialize a 'start_time'. Since we use start-times to
676 * calculate the duration of some operation, we actually
677 * do not serialize the absolute time but the (relative)
678 * duration since the start time. When we then
679 * deserialize the start time, we take the current time and
680 * subtract that duration so that we get again an absolute
681 * time stamp that will result in correct performance
684 * @param rh handle for reading
685 * @param timestamp where to write the deserialized timestamp
686 * @return GNUNET_OK on success
689 read_start_time (struct GNUNET_BIO_ReadHandle *rh,
690 struct GNUNET_TIME_Absolute *timestamp)
692 struct GNUNET_TIME_Relative dur;
694 GNUNET_BIO_read_int64 (rh, &dur.value))
695 return GNUNET_SYSERR;
696 *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
703 * Using the given serialization filename, try to deserialize
704 * the file-information tree associated with it.
706 * @param h master context
707 * @param filename name of the file (without directory) with
709 * @return NULL on error
711 static struct GNUNET_FS_FileInformation *
712 deserialize_file_information (struct GNUNET_FS_Handle *h,
713 const char *filename);
717 * Using the given serialization filename, try to deserialize
718 * the file-information tree associated with it.
720 * @param h master context
721 * @param fn name of the file (without directory) with
723 * @param rh handle for reading
724 * @return NULL on error
726 static struct GNUNET_FS_FileInformation *
727 deserialize_fi_node (struct GNUNET_FS_Handle *h,
729 struct GNUNET_BIO_ReadHandle *rh)
731 struct GNUNET_FS_FileInformation *ret;
732 struct GNUNET_FS_FileInformation *nxt;
740 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
745 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
750 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
752 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
755 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
757 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
759 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
762 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
764 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
766 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
768 read_start_time (rh, &ret->start_time)) ||
770 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
772 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
774 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
776 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
780 case 0: /* file-insert */
782 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
784 ret->is_directory = GNUNET_NO;
785 ret->data.file.do_index = GNUNET_NO;
786 ret->data.file.have_hash = GNUNET_NO;
787 ret->data.file.index_start_confirmed = GNUNET_NO;
788 if (GNUNET_NO == ret->is_published)
790 if (NULL == ret->filename)
792 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
793 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
794 if (ret->data.file.reader_cls == NULL)
797 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
802 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
803 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
807 case 1: /* file-index, no hash */
808 if (NULL == ret->filename)
811 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
813 ret->is_directory = GNUNET_NO;
814 ret->data.file.do_index = GNUNET_YES;
815 ret->data.file.have_hash = GNUNET_NO;
816 ret->data.file.index_start_confirmed = GNUNET_NO;
817 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
818 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
820 case 2: /* file-index-with-hash */
821 if (NULL == ret->filename)
824 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
826 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
828 ret->is_directory = GNUNET_NO;
829 ret->data.file.do_index = GNUNET_YES;
830 ret->data.file.have_hash = GNUNET_YES;
831 ret->data.file.index_start_confirmed = GNUNET_NO;
832 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
833 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
835 case 3: /* file-index-with-hash-confirmed */
836 if (NULL == ret->filename)
839 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
841 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
844 ret->is_directory = GNUNET_NO;
845 ret->data.file.do_index = GNUNET_YES;
846 ret->data.file.have_hash = GNUNET_YES;
847 ret->data.file.index_start_confirmed = GNUNET_YES;
848 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
849 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
851 case 4: /* directory */
853 GNUNET_BIO_read_int32 (rh, &dsize)) ||
854 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
856 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
858 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
860 ret->data.dir.dir_size = (uint32_t) dsize;
861 ret->is_directory = GNUNET_YES;
862 if (filename != NULL)
864 ret->data.dir.entries = deserialize_file_information (h, filename);
865 GNUNET_free (filename);
867 nxt = ret->data.dir.entries;
879 ret->serialization = GNUNET_strdup (fn);
881 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
883 if (filename != NULL)
885 ret->next = deserialize_file_information (h, filename);
886 GNUNET_free (filename);
889 GNUNET_free_non_null (ksks);
890 GNUNET_free_non_null (chks);
893 GNUNET_free_non_null (ksks);
894 GNUNET_free_non_null (chks);
895 GNUNET_free_non_null (filename);
896 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
902 * Using the given serialization filename, try to deserialize
903 * the file-information tree associated with it.
905 * @param h master context
906 * @param filename name of the file (without directory) with
908 * @return NULL on error
910 static struct GNUNET_FS_FileInformation *
911 deserialize_file_information (struct GNUNET_FS_Handle *h,
912 const char *filename)
914 struct GNUNET_FS_FileInformation *ret;
915 struct GNUNET_BIO_ReadHandle *rh;
918 rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
921 ret = deserialize_fi_node (h, filename, rh);
923 GNUNET_BIO_read_close (rh, &emsg))
925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
926 _("Failed to resume publishing information `%s': %s\n"),
936 * Given a serialization name (full absolute path), return the
937 * basename of the file (without the path), which must only
938 * consist of the 6 random characters.
940 * @param fullname name to extract the basename from
941 * @return copy of the basename, NULL on error
944 get_serialization_short_name (const char *fullname)
951 /* FIXME: we could do this faster since we know
952 the length of 'end'... */
955 if (DIR_SEPARATOR == *nxt)
959 if ( (end == NULL) ||
960 (strlen (end) == 0) )
965 GNUNET_break (6 == strlen (end));
966 return GNUNET_strdup (end);
971 * Create a new random name for serialization. Also checks if persistence
972 * is enabled and returns NULL if not.
974 * @param h master context
975 * @param ext component of the path
976 * @return NULL on errror
979 make_serialization_file_name (struct GNUNET_FS_Handle *h,
986 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
987 return NULL; /* persistence not requested */
988 dn = get_serialization_file_name (h, ext, "");
989 fn = GNUNET_DISK_mktemp (dn);
992 return NULL; /* epic fail */
993 ret = get_serialization_short_name (fn);
1000 * Create a new random name for serialization. Also checks if persistence
1001 * is enabled and returns NULL if not.
1003 * @param h master context
1004 * @param ext component of the path
1005 * @param uni name of parent
1006 * @return NULL on errror
1009 make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
1017 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
1018 return NULL; /* persistence not requested */
1019 dn = get_serialization_file_name_in_dir (h, ext, uni, "");
1020 fn = GNUNET_DISK_mktemp (dn);
1023 return NULL; /* epic fail */
1024 ret = get_serialization_short_name (fn);
1031 * Copy all of the data from the reader to the write handle.
1033 * @param wh write handle
1034 * @param fi file with reader
1035 * @return GNUNET_OK on success
1038 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
1039 struct GNUNET_FS_FileInformation * fi)
1041 char buf[32 * 1024];
1048 while (off < fi->data.file.file_size)
1050 ret = fi->data.file.reader (fi->data.file.reader_cls,
1057 return GNUNET_SYSERR;
1060 GNUNET_BIO_write (wh, buf, ret))
1061 return GNUNET_SYSERR;
1069 * Create a temporary file on disk to store the current
1072 * @param fi file information to sync with disk
1075 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
1078 struct GNUNET_BIO_WriteHandle *wh;
1083 if (NULL == fi->serialization)
1084 fi->serialization = make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
1085 if (NULL == fi->serialization)
1087 wh = get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1090 GNUNET_free (fi->serialization);
1091 fi->serialization = NULL;
1094 if (GNUNET_YES == fi->is_directory)
1096 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
1098 else if (GNUNET_YES == fi->data.file.have_hash)
1100 else if (GNUNET_YES == fi->data.file.do_index)
1104 if (fi->keywords != NULL)
1105 ksks = GNUNET_FS_uri_to_string (fi->keywords);
1108 if (fi->chk_uri != NULL)
1109 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
1113 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
1115 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
1117 GNUNET_BIO_write_string (wh, ksks)) ||
1119 GNUNET_BIO_write_string (wh, chks)) ||
1121 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
1123 write_start_time (wh, fi->start_time)) ||
1125 GNUNET_BIO_write_string (wh, fi->emsg)) ||
1127 GNUNET_BIO_write_string (wh, fi->filename)) ||
1129 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
1131 GNUNET_BIO_write_int32 (wh, fi->priority)) )
1133 GNUNET_free_non_null (chks);
1135 GNUNET_free_non_null (ksks);
1140 case 0: /* file-insert */
1142 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1144 if ( (GNUNET_NO == fi->is_published) &&
1145 (NULL == fi->filename) )
1147 copy_from_reader (wh, fi))
1150 case 1: /* file-index, no hash */
1151 if (NULL == fi->filename)
1154 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
1157 case 2: /* file-index-with-hash */
1158 case 3: /* file-index-with-hash-confirmed */
1159 if (NULL == fi->filename)
1162 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
1164 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
1167 case 4: /* directory */
1169 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
1171 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
1173 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
1181 GNUNET_BIO_write_string (wh, fi->next->serialization))
1184 GNUNET_BIO_write_close (wh))
1187 (void) GNUNET_BIO_write_close (wh);
1188 GNUNET_free_non_null (chks);
1189 GNUNET_free_non_null (ksks);
1190 fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
1191 if (0 != UNLINK (fn))
1192 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1194 GNUNET_free (fi->serialization);
1195 fi->serialization = NULL;
1201 * Find the entry in the file information struct where the
1202 * serialization filename matches the given name.
1204 * @param pos file information to search
1205 * @param srch filename to search for
1206 * @return NULL if srch was not found in this subtree
1208 static struct GNUNET_FS_FileInformation *
1209 find_file_position (struct GNUNET_FS_FileInformation *pos,
1212 struct GNUNET_FS_FileInformation *r;
1216 if (0 == strcmp (srch,
1217 pos->serialization))
1219 if (pos->is_directory)
1221 r = find_file_position (pos->data.dir.entries,
1233 * Signal the FS's progress function that we are resuming
1236 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
1237 * @param fi the entry in the publish-structure
1238 * @param length length of the file or directory
1239 * @param meta metadata for the file or directory (can be modified)
1240 * @param uri pointer to the keywords that will be used for this entry (can be modified)
1241 * @param anonymity pointer to selected anonymity level (can be modified)
1242 * @param priority pointer to selected priority (can be modified)
1243 * @param expirationTime pointer to selected expiration time (can be modified)
1244 * @param client_info pointer to client context set upon creation (can be modified)
1245 * @return GNUNET_OK to continue (always)
1248 fip_signal_resume(void *cls,
1249 struct GNUNET_FS_FileInformation *fi,
1251 struct GNUNET_CONTAINER_MetaData *meta,
1252 struct GNUNET_FS_Uri **uri,
1253 uint32_t *anonymity,
1255 struct GNUNET_TIME_Absolute *expirationTime,
1258 struct GNUNET_FS_PublishContext *sc = cls;
1259 struct GNUNET_FS_ProgressInfo pi;
1261 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1262 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1263 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1264 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1270 * Function called with a filename of serialized publishing operation
1273 * @param cls the 'struct GNUNET_FS_Handle*'
1274 * @param filename complete filename (absolute path)
1275 * @return GNUNET_OK (continue to iterate)
1278 deserialize_publish_file (void *cls,
1279 const char *filename)
1281 struct GNUNET_FS_Handle *h = cls;
1282 struct GNUNET_BIO_ReadHandle *rh;
1283 struct GNUNET_FS_PublishContext *pc;
1291 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1293 pc->serialization = get_serialization_short_name (filename);
1297 rh = GNUNET_BIO_read_open (filename);
1301 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1303 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1305 GNUNET_BIO_read_int32 (rh, &options)) ||
1307 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1309 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1311 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1313 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1315 pc->options = options;
1316 pc->all_done = all_done;
1317 pc->fi = deserialize_file_information (h, fi_root);
1322 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1323 if (pc->namespace == NULL)
1325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1326 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1333 pc->fi_pos = find_file_position (pc->fi,
1335 GNUNET_free (fi_pos);
1337 if (pc->fi_pos == NULL)
1339 /* failed to find position for resuming, outch! Will start from root! */
1341 if (pc->all_done != GNUNET_YES)
1342 pc->fi_pos = pc->fi;
1345 /* generate RESUME event(s) */
1346 GNUNET_FS_file_information_inspect (pc->fi,
1350 /* re-start publishing (if needed)... */
1351 if (pc->all_done != GNUNET_YES)
1353 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1354 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1355 &GNUNET_FS_publish_main_,
1358 GNUNET_BIO_read_close (rh, &emsg))
1360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1361 _("Failure while resuming publishing operation `%s': %s\n"),
1366 GNUNET_free_non_null (ns);
1369 GNUNET_free_non_null (pc->nid);
1370 GNUNET_free_non_null (pc->nuid);
1371 GNUNET_free_non_null (fi_root);
1372 GNUNET_free_non_null (fi_pos);
1373 GNUNET_free_non_null (ns);
1374 if ( (rh != NULL) &&
1376 GNUNET_BIO_read_close (rh, &emsg)) )
1378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1379 _("Failed to resume publishing operation `%s': %s\n"),
1385 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1386 if (0 != UNLINK (filename))
1387 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
1388 GNUNET_free_non_null (pc->serialization);
1395 * Synchronize this publishing struct with its mirror
1396 * on disk. Note that all internal FS-operations that change
1397 * publishing structs should already call "sync" internally,
1398 * so this function is likely not useful for clients.
1400 * @param pc the struct to sync
1403 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1405 struct GNUNET_BIO_WriteHandle *wh;
1407 if (NULL == pc->serialization)
1408 pc->serialization = make_serialization_file_name (pc->h,
1409 GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
1410 if (NULL == pc->serialization)
1414 if (NULL == pc->fi->serialization)
1419 wh = get_write_handle (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1421 GNUNET_BIO_write_string (wh, pc->nid)) ||
1423 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1425 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1427 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1429 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1431 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1433 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1438 GNUNET_BIO_write_close (wh))
1446 (void) GNUNET_BIO_write_close (wh);
1447 GNUNET_FS_remove_sync_file_ (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH, pc->serialization);
1448 GNUNET_free (pc->serialization);
1449 pc->serialization = NULL;
1454 * Synchronize this unindex struct with its mirror
1455 * on disk. Note that all internal FS-operations that change
1456 * publishing structs should already call "sync" internally,
1457 * so this function is likely not useful for clients.
1459 * @param uc the struct to sync
1462 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1464 struct GNUNET_BIO_WriteHandle *wh;
1466 if (UNINDEX_STATE_ABORTED == uc->state)
1468 if (NULL == uc->serialization)
1469 uc->serialization = make_serialization_file_name (uc->h,
1470 GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
1471 if (NULL == uc->serialization)
1473 wh = get_write_handle (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1475 GNUNET_BIO_write_string (wh, uc->filename)) ||
1477 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1479 write_start_time (wh, uc->start_time)) ||
1481 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1482 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1484 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1485 ( (uc->state == UNINDEX_STATE_ERROR) &&
1487 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1490 GNUNET_BIO_write_close (wh))
1498 (void) GNUNET_BIO_write_close (wh);
1499 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1500 GNUNET_free (uc->serialization);
1501 uc->serialization = NULL;
1506 * Serialize an active or pending download request.
1508 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
1509 * @param key unused, can be NULL
1510 * @param value the 'struct DownloadRequest'
1511 * @return GNUNET_YES on success, GNUNET_NO on error
1514 write_download_request (void *cls,
1515 const GNUNET_HashCode *key,
1518 struct GNUNET_BIO_WriteHandle *wh = cls;
1519 struct DownloadRequest *dr = value;
1522 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) ||
1524 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1526 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1533 * Count active download requests.
1535 * @param cls the 'uint32_t*' counter
1536 * @param key unused, can be NULL
1537 * @param value the 'struct DownloadRequest'
1538 * @return GNUNET_YES (continue iteration)
1541 count_download_requests (void *cls,
1542 const GNUNET_HashCode *key,
1545 uint32_t *counter = cls;
1553 * Compute the name of the sync file (or directory) for the given download
1556 * @param dc download context to compute for
1557 * @param uni unique filename to use, use "" for the directory name
1558 * @return the expanded file name, NULL for none
1561 get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
1567 if (dc->parent == NULL)
1568 return get_serialization_file_name (dc->h,
1569 (dc->search != NULL) ?
1570 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
1571 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
1573 if (dc->parent->serialization == NULL)
1575 par = get_download_sync_filename (dc->parent, dc->parent->serialization);
1578 GNUNET_asprintf (&epar,
1589 * Synchronize this download struct with its mirror
1590 * on disk. Note that all internal FS-operations that change
1591 * publishing structs should already call "sync" internally,
1592 * so this function is likely not useful for clients.
1594 * @param dc the struct to sync
1597 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1599 struct GNUNET_BIO_WriteHandle *wh;
1603 uint32_t num_pending;
1605 if (NULL == dc->serialization)
1607 dir = get_download_sync_filename (dc, "");
1610 fn = GNUNET_DISK_mktemp (dir);
1612 dc->serialization = get_serialization_short_name (fn);
1616 fn = get_download_sync_filename (dc, dc->serialization);
1618 wh = GNUNET_BIO_write_open (fn);
1621 GNUNET_free (dc->serialization);
1622 dc->serialization = NULL;
1626 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1627 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1628 uris = GNUNET_FS_uri_to_string (dc->uri);
1630 if (dc->emsg != NULL)
1631 (void) GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1632 &count_download_requests,
1634 GNUNET_assert ( (dc->length == dc->completed) ||
1635 (dc->emsg != NULL) ||
1636 (num_pending > 0) );
1638 GNUNET_BIO_write_string (wh, uris)) ||
1640 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1642 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1644 GNUNET_BIO_write_string (wh, dc->filename)) ||
1646 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1648 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1650 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1652 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1654 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1656 write_start_time (wh, dc->start_time)) ||
1658 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1660 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1662 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) ||
1664 GNUNET_BIO_write_int32 (wh, num_pending)) )
1666 if (GNUNET_SYSERR ==
1667 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1668 &write_download_request,
1671 GNUNET_free_non_null (uris);
1673 GNUNET_BIO_write_close (wh))
1682 (void) GNUNET_BIO_write_close (wh);
1683 GNUNET_free_non_null (uris);
1684 if (0 != UNLINK (fn))
1685 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1687 GNUNET_free (dc->serialization);
1688 dc->serialization = NULL;
1693 * Synchronize this search result with its mirror
1694 * on disk. Note that all internal FS-operations that change
1695 * publishing structs should already call "sync" internally,
1696 * so this function is likely not useful for clients.
1698 * @param sr the struct to sync
1701 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
1703 struct GNUNET_BIO_WriteHandle *wh;
1707 if (NULL == sr->serialization)
1708 sr->serialization = make_serialization_file_name_in_dir (sr->sc->h,
1709 (sr->sc->psearch_result == NULL)
1710 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1711 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1712 sr->sc->serialization);
1713 if (NULL == sr->serialization)
1715 wh = get_write_handle_in_dir (sr->sc->h,
1716 (sr->sc->psearch_result == NULL)
1717 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1718 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1719 sr->sc->serialization,
1721 uris = GNUNET_FS_uri_to_string (sr->uri);
1723 GNUNET_BIO_write_string (wh, uris)) ||
1725 GNUNET_BIO_write_string (wh, sr->download != NULL ? sr->download->serialization : NULL)) ||
1727 GNUNET_BIO_write_string (wh, sr->update_search != NULL ? sr->update_search->serialization : NULL)) ||
1729 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1731 GNUNET_BIO_write (wh, &sr->key, sizeof (GNUNET_HashCode))) ||
1733 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1735 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1737 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1739 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1742 GNUNET_BIO_write_close (wh))
1747 GNUNET_free_non_null (uris);
1750 GNUNET_free_non_null (uris);
1752 (void) GNUNET_BIO_write_close (wh);
1753 remove_sync_file_in_dir (sr->sc->h,
1754 (sr->sc->psearch_result == NULL)
1755 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1756 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
1757 sr->sc->serialization,
1759 GNUNET_free (sr->serialization);
1760 sr->serialization = NULL;
1765 * Synchronize this search struct with its mirror
1766 * on disk. Note that all internal FS-operations that change
1767 * publishing structs should already call "sync" internally,
1768 * so this function is likely not useful for clients.
1770 * @param sc the struct to sync
1773 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1775 struct GNUNET_BIO_WriteHandle *wh;
1778 const char *category;
1780 category = (sc->psearch_result == NULL)
1781 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
1782 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
1783 if (NULL == sc->serialization)
1784 sc->serialization = make_serialization_file_name (sc->h,
1786 if (NULL == sc->serialization)
1788 wh = get_write_handle (sc->h, category, sc->serialization);
1789 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1790 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1791 uris = GNUNET_FS_uri_to_string (sc->uri);
1792 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1794 GNUNET_BIO_write_string (wh, uris)) ||
1796 write_start_time (wh, sc->start_time)) ||
1798 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1800 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1802 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1804 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1809 GNUNET_BIO_write_close (wh))
1817 (void) GNUNET_BIO_write_close (wh);
1818 GNUNET_free_non_null (uris);
1819 GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
1820 GNUNET_free (sc->serialization);
1821 sc->serialization = NULL;
1826 * Function called with a filename of serialized unindexing operation
1829 * @param cls the 'struct GNUNET_FS_Handle*'
1830 * @param filename complete filename (absolute path)
1831 * @return GNUNET_OK (continue to iterate)
1834 deserialize_unindex_file (void *cls,
1835 const char *filename)
1837 struct GNUNET_FS_Handle *h = cls;
1838 struct GNUNET_BIO_ReadHandle *rh;
1839 struct GNUNET_FS_UnindexContext *uc;
1840 struct GNUNET_FS_ProgressInfo pi;
1844 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1846 uc->serialization = get_serialization_short_name (filename);
1847 rh = GNUNET_BIO_read_open (filename);
1851 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1853 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1855 read_start_time (rh, &uc->start_time)) ||
1857 GNUNET_BIO_read_int32 (rh, &state)) )
1859 uc->state = (enum UnindexState) state;
1862 case UNINDEX_STATE_HASHING:
1864 case UNINDEX_STATE_FS_NOTIFY:
1866 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1869 case UNINDEX_STATE_DS_REMOVE:
1871 case UNINDEX_STATE_COMPLETE:
1873 case UNINDEX_STATE_ERROR:
1875 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1878 case UNINDEX_STATE_ABORTED:
1885 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1886 pi.value.unindex.specifics.resume.message = uc->emsg;
1887 GNUNET_FS_unindex_make_status_ (&pi,
1889 (uc->state == UNINDEX_STATE_COMPLETE)
1894 case UNINDEX_STATE_HASHING:
1895 GNUNET_CRYPTO_hash_file (uc->h->sched,
1896 GNUNET_SCHEDULER_PRIORITY_IDLE,
1899 &GNUNET_FS_unindex_process_hash_,
1902 case UNINDEX_STATE_FS_NOTIFY:
1903 uc->state = UNINDEX_STATE_HASHING;
1904 GNUNET_FS_unindex_process_hash_ (uc,
1907 case UNINDEX_STATE_DS_REMOVE:
1908 GNUNET_FS_unindex_do_remove_ (uc);
1910 case UNINDEX_STATE_COMPLETE:
1911 case UNINDEX_STATE_ERROR:
1912 /* no need to resume any operation, we were done */
1918 GNUNET_BIO_read_close (rh, &emsg))
1920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1921 _("Failure while resuming unindexing operation `%s': %s\n"),
1928 GNUNET_free_non_null (uc->filename);
1929 if ( (rh != NULL) &&
1931 GNUNET_BIO_read_close (rh, &emsg)) )
1933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1934 _("Failed to resume unindexing operation `%s': %s\n"),
1939 if (uc->serialization != NULL)
1940 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
1941 GNUNET_free_non_null (uc->serialization);
1948 * Deserialize a download.
1950 * @param h overall context
1951 * @param rh file to deserialize from
1952 * @param parent parent download
1953 * @param search associated search
1954 * @param serialization name under which the search was serialized
1957 deserialize_download (struct GNUNET_FS_Handle *h,
1958 struct GNUNET_BIO_ReadHandle *rh,
1959 struct GNUNET_FS_DownloadContext *parent,
1960 struct GNUNET_FS_SearchResult *search,
1961 const char *serialization);
1965 * Deserialize a search.
1967 * @param h overall context
1968 * @param rh file to deserialize from
1969 * @param psearch_result parent search result
1970 * @param serialization name under which the search was serialized
1972 static struct GNUNET_FS_SearchContext *
1973 deserialize_search (struct GNUNET_FS_Handle *h,
1974 struct GNUNET_BIO_ReadHandle *rh,
1975 struct GNUNET_FS_SearchResult *psearch_result,
1976 const char *serialization);
1980 * Function called with a filename of serialized search result
1983 * @param cls the 'struct GNUNET_FS_SearchContext*'
1984 * @param filename complete filename (absolute path)
1985 * @return GNUNET_OK (continue to iterate)
1988 deserialize_search_result (void *cls,
1989 const char *filename)
1991 struct GNUNET_FS_SearchContext *sc = cls;
1997 struct GNUNET_BIO_ReadHandle *rh;
1998 struct GNUNET_BIO_ReadHandle *drh;
1999 struct GNUNET_FS_SearchResult *sr;
2001 ser = get_serialization_short_name (filename);
2002 rh = GNUNET_BIO_read_open (filename);
2007 remove_sync_file_in_dir (sc->h,
2008 (sc->psearch_result == NULL)
2009 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2010 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2020 sr = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchResult));
2021 sr->serialization = ser;
2023 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
2024 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2026 GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
2028 GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
2030 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
2032 GNUNET_BIO_read (rh, "result-key", &sr->key, sizeof (GNUNET_HashCode))) ||
2034 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
2036 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
2038 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
2040 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
2043 if (download != NULL)
2045 drh = get_read_handle (sc->h,
2046 GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
2048 deserialize_download (sc->h,
2054 GNUNET_BIO_read_close (drh, &emsg))
2056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2057 _("Failed to resume sub-download `%s': %s\n"),
2062 GNUNET_free (download);
2064 if (update_srch != NULL)
2066 drh = get_read_handle (sc->h,
2067 GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2069 deserialize_search (sc->h,
2074 GNUNET_BIO_read_close (drh, &emsg))
2076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2077 _("Failed to resume sub-search `%s': %s\n"),
2082 GNUNET_free (update_srch);
2084 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
2087 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2090 GNUNET_free_non_null (download);
2091 GNUNET_free_non_null (emsg);
2092 GNUNET_free_non_null (uris);
2093 if (sr->uri != NULL)
2094 GNUNET_FS_uri_destroy (sr->uri);
2095 if (sr->meta != NULL)
2096 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2097 GNUNET_free (sr->serialization);
2104 * Send the 'resume' signal to the callback; also actually
2105 * resume the download (put it in the queue). Does this
2106 * recursively for the top-level download and all child
2109 * @param dc download to resume
2112 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
2114 struct GNUNET_FS_DownloadContext *dcc;
2115 struct GNUNET_FS_ProgressInfo pi;
2117 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
2118 pi.value.download.specifics.resume.meta = dc->meta;
2119 pi.value.download.specifics.resume.message = dc->emsg;
2120 GNUNET_FS_download_make_status_ (&pi,
2122 dcc = dc->child_head;
2125 signal_download_resume (dcc);
2128 if (dc->pending != NULL)
2129 GNUNET_FS_download_start_downloading_ (dc);
2134 * Signal resuming of a search to our clients (for the
2135 * top level search and all sub-searches).
2137 * @param sc search being resumed
2140 signal_search_resume (struct GNUNET_FS_SearchContext *sc);
2144 * Iterator over search results signaling resume to the client for
2147 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2148 * @param key current key code
2149 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2150 * @return GNUNET_YES (we should continue to iterate)
2153 signal_result_resume (void *cls,
2154 const GNUNET_HashCode * key,
2157 struct GNUNET_FS_SearchContext *sc = cls;
2158 struct GNUNET_FS_ProgressInfo pi;
2159 struct GNUNET_FS_SearchResult *sr = value;
2161 if (0 == sr->mandatory_missing)
2163 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
2164 pi.value.search.specifics.resume_result.meta = sr->meta;
2165 pi.value.search.specifics.resume_result.uri = sr->uri;
2166 pi.value.search.specifics.resume_result.result = sr;
2167 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
2168 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
2169 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
2170 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
2173 if (sr->download != NULL)
2175 signal_download_resume (sr->download);
2179 GNUNET_FS_search_start_probe_ (sr);
2181 if (sr->update_search != NULL)
2182 signal_search_resume (sr->update_search);
2188 * Free memory allocated by the search context and its children
2190 * @param sc search context to free
2193 free_search_context (struct GNUNET_FS_SearchContext *sc);
2197 * Iterator over search results freeing each.
2199 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
2200 * @param key current key code
2201 * @param value value in the hash map, the 'struct GNUNET_FS_SearchResult'
2202 * @return GNUNET_YES (we should continue to iterate)
2205 free_result (void *cls,
2206 const GNUNET_HashCode * key,
2209 struct GNUNET_FS_SearchResult *sr = value;
2211 if (sr->update_search != NULL)
2213 free_search_context (sr->update_search);
2214 GNUNET_assert (NULL == sr->update_search);
2216 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
2217 GNUNET_FS_uri_destroy (sr->uri);
2224 * Free memory allocated by the search context and its children
2226 * @param sc search context to free
2229 free_search_context (struct GNUNET_FS_SearchContext *sc)
2231 if (sc->serialization != NULL)
2233 GNUNET_FS_remove_sync_file_ (sc->h,
2234 (sc->psearch_result == NULL)
2235 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2236 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2238 GNUNET_FS_remove_sync_dir_ (sc->h,
2239 (sc->psearch_result == NULL)
2240 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2241 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2244 GNUNET_free_non_null (sc->serialization);
2245 GNUNET_free_non_null (sc->emsg);
2246 if (sc->uri != NULL)
2247 GNUNET_FS_uri_destroy (sc->uri);
2248 if (sc->master_result_map != NULL)
2250 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2253 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
2260 * Function called with a filename of serialized sub-download
2263 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
2264 * @param filename complete filename (absolute path)
2265 * @return GNUNET_OK (continue to iterate)
2268 deserialize_subdownload (void *cls,
2269 const char *filename)
2271 struct GNUNET_FS_DownloadContext *parent = cls;
2274 struct GNUNET_BIO_ReadHandle *rh;
2276 ser = get_serialization_short_name (filename);
2277 rh = GNUNET_BIO_read_open (filename);
2278 deserialize_download (parent->h,
2284 GNUNET_BIO_read_close (rh, &emsg))
2286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2287 _("Failed to resume sub-download `%s': %s\n"),
2298 * Free this download context and all of its descendants.
2299 * (only works during deserialization since not all possible
2300 * state it taken care of).
2302 * @param dc context to free
2305 free_download_context (struct GNUNET_FS_DownloadContext *dc)
2307 struct GNUNET_FS_DownloadContext *dcc;
2308 struct DownloadRequest *dr;
2309 if (dc->meta != NULL)
2310 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2311 if (dc->uri != NULL)
2312 GNUNET_FS_uri_destroy (dc->uri);
2313 GNUNET_free_non_null (dc->temp_filename);
2314 GNUNET_free_non_null (dc->emsg);
2315 GNUNET_free_non_null (dc->filename);
2316 while (NULL != (dcc = dc->child_head))
2318 GNUNET_CONTAINER_DLL_remove (dc->child_head,
2321 free_download_context (dcc);
2323 while (NULL != (dr = dc->pending))
2325 dc->pending = dr->next;
2333 * Deserialize a download.
2335 * @param h overall context
2336 * @param rh file to deserialize from
2337 * @param parent parent download
2338 * @param search associated search
2339 * @param serialization name under which the search was serialized
2342 deserialize_download (struct GNUNET_FS_Handle *h,
2343 struct GNUNET_BIO_ReadHandle *rh,
2344 struct GNUNET_FS_DownloadContext *parent,
2345 struct GNUNET_FS_SearchResult *search,
2346 const char *serialization)
2348 struct GNUNET_FS_DownloadContext *dc;
2349 struct DownloadRequest *dr;
2355 uint32_t num_pending;
2360 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2361 dc->parent = parent;
2363 dc->serialization = GNUNET_strdup (serialization);
2365 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2366 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2367 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2368 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2370 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2372 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2374 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2376 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2378 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2380 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2382 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2384 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2386 read_start_time (rh, &dc->start_time)) ||
2388 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2390 GNUNET_BIO_read_int32 (rh, &options)) ||
2392 GNUNET_BIO_read_int32 (rh, &status)) ||
2394 GNUNET_BIO_read_int32 (rh, &num_pending)) )
2396 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2397 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
2398 dc->has_finished = (int) status;
2399 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2400 if (GNUNET_FS_uri_test_loc (dc->uri))
2401 GNUNET_assert (GNUNET_OK ==
2402 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2404 if ( (dc->length > dc->completed) &&
2405 (num_pending == 0) )
2407 while (0 < num_pending--)
2409 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
2411 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
2413 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
2415 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
2417 dr->is_pending = GNUNET_YES;
2418 dr->next = dc->pending;
2422 dn = get_download_sync_filename (dc, "");
2425 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2429 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2434 dc->search = search;
2435 search->download = dc;
2437 signal_download_resume (dc);
2441 GNUNET_free_non_null (uris);
2442 GNUNET_free_non_null (dr);
2443 GNUNET_free_non_null (emsg);
2444 free_download_context (dc);
2449 * Signal resuming of a search to our clients (for the
2450 * top level search and all sub-searches).
2452 * @param sc search being resumed
2455 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2457 struct GNUNET_FS_ProgressInfo pi;
2459 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2460 pi.value.search.specifics.resume.message = sc->emsg;
2461 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2462 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2464 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2465 &signal_result_resume,
2472 * Deserialize a search.
2474 * @param h overall context
2475 * @param rh file to deserialize from
2476 * @param psearch_result parent search result
2477 * @param serialization name under which the search was serialized
2479 static struct GNUNET_FS_SearchContext *
2480 deserialize_search (struct GNUNET_FS_Handle *h,
2481 struct GNUNET_BIO_ReadHandle *rh,
2482 struct GNUNET_FS_SearchResult *psearch_result,
2483 const char *serialization)
2485 struct GNUNET_FS_SearchContext *sc;
2492 if ( (psearch_result != NULL) &&
2493 (psearch_result->update_search != NULL) )
2500 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2501 if (psearch_result != NULL)
2503 sc->psearch_result = psearch_result;
2504 psearch_result->update_search = sc;
2507 sc->serialization = GNUNET_strdup (serialization);
2509 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2510 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2511 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2512 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2514 read_start_time (rh, &sc->start_time)) ||
2516 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2518 GNUNET_BIO_read_int32 (rh, &options)) ||
2520 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2522 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2524 sc->options = (enum GNUNET_FS_SearchOptions) options;
2525 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2526 dn = get_serialization_file_name_in_dir (h,
2527 (sc->psearch_result == NULL)
2528 ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
2529 : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
2534 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2537 if ( ('\0' == in_pause) &&
2539 GNUNET_FS_search_start_searching_ (sc)) )
2541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2542 _("Could not resume running search, will resume as paused search\n"));
2544 signal_search_resume (sc);
2548 GNUNET_free_non_null (emsg);
2549 free_search_context (sc);
2550 GNUNET_free_non_null (uris);
2556 * Function called with a filename of serialized search operation
2559 * @param cls the 'struct GNUNET_FS_Handle*'
2560 * @param filename complete filename (absolute path)
2561 * @return GNUNET_OK (continue to iterate)
2564 deserialize_search_file (void *cls,
2565 const char *filename)
2567 struct GNUNET_FS_Handle *h = cls;
2570 struct GNUNET_BIO_ReadHandle *rh;
2572 ser = get_serialization_short_name (filename);
2573 rh = GNUNET_BIO_read_open (filename);
2578 GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
2583 (void) deserialize_search (h, rh, NULL, ser);
2586 GNUNET_BIO_read_close (rh, &emsg))
2588 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2589 _("Failure while resuming search operation `%s': %s\n"),
2599 * Function called with a filename of serialized download operation
2602 * @param cls the 'struct GNUNET_FS_Handle*'
2603 * @param filename complete filename (absolute path)
2604 * @return GNUNET_OK (continue to iterate)
2607 deserialize_download_file (void *cls,
2608 const char *filename)
2610 struct GNUNET_FS_Handle *h = cls;
2613 struct GNUNET_BIO_ReadHandle *rh;
2615 ser = get_serialization_short_name (filename);
2616 rh = GNUNET_BIO_read_open (filename);
2619 if (filename != NULL)
2621 if (0 != UNLINK (filename))
2622 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
2629 deserialize_download (h, rh, NULL, NULL, ser);
2632 GNUNET_BIO_read_close (rh, &emsg))
2634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2635 _("Failure while resuming download operation `%s': %s\n"),
2645 * Deserialize informatin about pending operations.
2647 * @param master_path which master directory should be scanned
2648 * @param proc function to call for each entry (will get 'h' for 'cls')
2649 * @param h the 'struct GNUNET_FS_Handle*'
2652 deserialization_master (const char *master_path,
2653 GNUNET_FileNameCallback proc,
2654 struct GNUNET_FS_Handle *h)
2658 dn = get_serialization_file_name (h, master_path, "");
2661 GNUNET_DISK_directory_scan (dn, proc, h);
2667 * Setup a connection to the file-sharing service.
2669 * @param sched scheduler to use
2670 * @param cfg configuration to use
2671 * @param client_name unique identifier for this client
2672 * @param upcb function to call to notify about FS actions
2673 * @param upcb_cls closure for upcb
2674 * @param flags specific attributes for fs-operations
2675 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2676 * @return NULL on error
2678 struct GNUNET_FS_Handle *
2679 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2680 const struct GNUNET_CONFIGURATION_Handle *cfg,
2681 const char *client_name,
2682 GNUNET_FS_ProgressCallback upcb,
2684 enum GNUNET_FS_Flags flags,
2687 struct GNUNET_FS_Handle *ret;
2688 enum GNUNET_FS_OPTIONS opt;
2691 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2694 ret->client_name = GNUNET_strdup (client_name);
2696 ret->upcb_cls = upcb_cls;
2698 ret->max_parallel_downloads = 1;
2699 ret->max_parallel_requests = 1;
2700 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2701 va_start (ap, flags);
2702 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2706 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2707 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2709 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2710 ret->max_parallel_requests = va_arg (ap, unsigned int);
2714 GNUNET_free (ret->client_name);
2721 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2723 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
2724 &deserialize_publish_file,
2726 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
2727 &deserialize_search_file,
2729 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2730 &deserialize_download_file,
2732 deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
2733 &deserialize_unindex_file,
2741 * Close our connection with the file-sharing service.
2742 * The callback given to GNUNET_FS_start will no longer be
2743 * called after this function returns.
2745 * @param h handle that was returned from GNUNET_FS_start
2748 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2750 while (h->top_head != NULL)
2751 h->top_head->ssf (h->top_head->ssf_cls);
2752 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2753 GNUNET_SCHEDULER_cancel (h->sched,
2755 GNUNET_free (h->client_name);