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"
34 * Start the given job (send signal, remove from pending queue, update
35 * counters and state).
37 * @param qe job to start
40 start_job (struct GNUNET_FS_QueueEntry *qe)
42 qe->client = GNUNET_CLIENT_connect (qe->h->sched, "fs", qe->h->cfg);
43 if (qe->client == NULL)
48 qe->start (qe->cls, qe->client);
50 qe->h->active_blocks += qe->blocks;
51 qe->start_time = GNUNET_TIME_absolute_get ();
52 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
55 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
63 * Stop the given job (send signal, remove from active queue, update
64 * counters and state).
66 * @param qe job to stop
69 stop_job (struct GNUNET_FS_QueueEntry *qe)
73 qe->h->active_downloads--;
74 qe->h->active_blocks -= qe->blocks;
75 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
76 GNUNET_TIME_absolute_get_duration (qe->start_time));
77 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
80 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
88 * Process the jobs in the job queue, possibly starting some
89 * and stopping others.
91 * @param cls the 'struct GNUNET_FS_Handle'
92 * @param tc scheduler context
95 process_job_queue (void *cls,
96 const struct GNUNET_SCHEDULER_TaskContext *tc)
98 struct GNUNET_FS_Handle *h = cls;
99 struct GNUNET_FS_QueueEntry *qe;
100 struct GNUNET_FS_QueueEntry *next;
101 struct GNUNET_TIME_Relative run_time;
102 struct GNUNET_TIME_Relative restart_at;
103 struct GNUNET_TIME_Relative rst;
104 struct GNUNET_TIME_Absolute end_time;
106 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
107 next = h->pending_head;
108 while (NULL != (qe = next))
111 if (h->running_head == NULL)
116 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
117 (h->active_downloads + 1 <= h->max_parallel_downloads) )
123 if (h->pending_head == NULL)
124 return; /* no need to stop anything */
125 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
126 next = h->running_head;
127 while (NULL != (qe = next))
130 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
131 qe->blocks * qe->start_times);
132 end_time = GNUNET_TIME_absolute_add (qe->start_time,
134 rst = GNUNET_TIME_absolute_get_remaining (end_time);
135 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
140 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
148 * Add a job to the queue.
150 * @param h handle to the overall FS state
151 * @param start function to call to begin the job
152 * @param stop function to call to pause the job, or on dequeue (if the job was running)
153 * @param cls closure for start and stop
154 * @param blocks number of blocks this jobs uses
155 * @return queue handle
157 struct GNUNET_FS_QueueEntry *
158 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
159 GNUNET_FS_QueueStart start,
160 GNUNET_FS_QueueStop stop,
164 struct GNUNET_FS_QueueEntry *qe;
166 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
171 qe->queue_time = GNUNET_TIME_absolute_get ();
173 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
177 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
178 GNUNET_SCHEDULER_cancel (h->sched,
181 = GNUNET_SCHEDULER_add_now (h->sched,
189 * Dequeue a job from the queue.
190 * @param qh handle for the job
193 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
195 struct GNUNET_FS_Handle *h;
198 if (qh->client != NULL)
200 GNUNET_CONTAINER_DLL_remove (h->pending_head,
204 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
205 GNUNET_SCHEDULER_cancel (h->sched,
208 = GNUNET_SCHEDULER_add_now (h->sched,
215 * Closure for "data_reader_file".
220 * Name of the file to read.
225 * File descriptor, NULL if it has not yet been opened.
227 struct GNUNET_DISK_FileHandle *fd;
232 * Function that provides data by reading from a file.
234 * @param cls closure (points to the file information)
235 * @param offset offset to read from; it is possible
236 * that the caller might need to go backwards
238 * @param max maximum number of bytes that should be
239 * copied to buf; readers are not allowed
240 * to provide less data unless there is an error;
241 * a value of "0" will be used at the end to allow
242 * the reader to clean up its internal state
243 * @param buf where the reader should write the data
244 * @param emsg location for the reader to store an error message
245 * @return number of bytes written, usually "max", 0 on error
248 GNUNET_FS_data_reader_file_(void *cls,
254 struct FileInfo *fi = cls;
260 GNUNET_DISK_file_close (fi->fd);
261 GNUNET_free (fi->filename);
267 fi->fd = GNUNET_DISK_file_open (fi->filename,
268 GNUNET_DISK_OPEN_READ,
269 GNUNET_DISK_PERM_NONE);
272 GNUNET_asprintf (emsg,
273 _("Could not open file `%s': %s"),
279 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
280 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
283 GNUNET_asprintf (emsg,
284 _("Could not read file `%s': %s"),
291 GNUNET_asprintf (emsg,
292 _("Short read reading from file `%s'!"),
301 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
303 * @param filename file to read
304 * @return closure to use, NULL on error
307 GNUNET_FS_make_file_reader_context_ (const char *filename)
311 fi = GNUNET_malloc (sizeof(struct FileInfo));
312 fi->filename = GNUNET_STRINGS_filename_expand (filename);
313 if (fi->filename == NULL)
323 * Function that provides data by copying from a buffer.
325 * @param cls closure (points to the buffer)
326 * @param offset offset to read from; it is possible
327 * that the caller might need to go backwards
329 * @param max maximum number of bytes that should be
330 * copied to buf; readers are not allowed
331 * to provide less data unless there is an error;
332 * a value of "0" will be used at the end to allow
333 * the reader to clean up its internal state
334 * @param buf where the reader should write the data
335 * @param emsg location for the reader to store an error message
336 * @return number of bytes written, usually "max", 0 on error
339 GNUNET_FS_data_reader_copy_ (void *cls,
349 GNUNET_free_non_null (data);
352 memcpy (buf, &data[offset], max);
359 * Return the full filename where we would store state information
360 * (for serialization/deserialization).
362 * @param h master context
363 * @param ext component of the path
364 * @param ent entity identifier (or emtpy string for the directory)
365 * @return NULL on error
368 get_serialization_file_name (struct GNUNET_FS_Handle *h,
375 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
380 GNUNET_asprintf (&ret,
388 GNUNET_free (basename);
394 * Return a read handle for deserialization.
396 * @param h master context
397 * @param ext component of the path
398 * @param ent entity identifier (or emtpy string for the directory)
399 * @return NULL on error
401 static struct GNUNET_BIO_ReadHandle *
402 get_read_handle (struct GNUNET_FS_Handle *h,
407 struct GNUNET_BIO_ReadHandle *ret;
409 fn = get_serialization_file_name (h, ext, ent);
412 ret = GNUNET_BIO_read_open (fn);
419 * Return a write handle for serialization.
421 * @param h master context
422 * @param ext component of the path
423 * @param ent entity identifier (or emtpy string for the directory)
424 * @return NULL on error
426 static struct GNUNET_BIO_WriteHandle *
427 get_write_handle (struct GNUNET_FS_Handle *h,
432 struct GNUNET_BIO_WriteHandle *ret;
434 fn = get_serialization_file_name (h, ext, ent);
437 ret = GNUNET_BIO_write_open (fn);
444 * Remove serialization/deserialization file from disk.
446 * @param h master context
447 * @param ext component of the path
448 * @param ent entity identifier
451 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
457 if ( (NULL == ent) ||
458 (0 == strlen (ent)) )
463 filename = get_serialization_file_name (h, ext, ent);
464 if (0 != UNLINK (filename))
465 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
468 GNUNET_free (filename);
474 * Using the given serialization filename, try to deserialize
475 * the file-information tree associated with it.
477 * @param h master context
478 * @param filename name of the file (without directory) with
480 * @return NULL on error
482 static struct GNUNET_FS_FileInformation *
483 deserialize_file_information (struct GNUNET_FS_Handle *h,
484 const char *filename);
488 * Using the given serialization filename, try to deserialize
489 * the file-information tree associated with it.
491 * @param h master context
492 * @param fn name of the file (without directory) with
494 * @param rh handle for reading
495 * @return NULL on error
497 static struct GNUNET_FS_FileInformation *
498 deserialize_fi_node (struct GNUNET_FS_Handle *h,
500 struct GNUNET_BIO_ReadHandle *rh)
502 struct GNUNET_FS_FileInformation *ret;
503 struct GNUNET_FS_FileInformation *nxt;
511 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
516 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
521 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
523 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
526 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
528 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
530 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
533 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
535 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
537 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
539 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
541 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
543 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
545 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
547 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
551 case 0: /* file-insert */
553 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
555 ret->is_directory = GNUNET_NO;
556 ret->data.file.do_index = GNUNET_NO;
557 ret->data.file.have_hash = GNUNET_NO;
558 ret->data.file.index_start_confirmed = GNUNET_NO;
559 if (GNUNET_NO == ret->is_published)
561 if (NULL == ret->filename)
563 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
564 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
565 if (ret->data.file.reader_cls == NULL)
568 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
573 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
574 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
578 case 1: /* file-index, no hash */
579 if (NULL == ret->filename)
582 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
584 ret->is_directory = GNUNET_NO;
585 ret->data.file.do_index = GNUNET_YES;
586 ret->data.file.have_hash = GNUNET_NO;
587 ret->data.file.index_start_confirmed = GNUNET_NO;
588 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
589 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
591 case 2: /* file-index-with-hash */
592 if (NULL == ret->filename)
595 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
597 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
599 ret->is_directory = GNUNET_NO;
600 ret->data.file.do_index = GNUNET_YES;
601 ret->data.file.have_hash = GNUNET_YES;
602 ret->data.file.index_start_confirmed = GNUNET_NO;
603 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
604 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
606 case 3: /* file-index-with-hash-confirmed */
607 if (NULL == ret->filename)
610 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
612 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
615 ret->is_directory = GNUNET_NO;
616 ret->data.file.do_index = GNUNET_YES;
617 ret->data.file.have_hash = GNUNET_YES;
618 ret->data.file.index_start_confirmed = GNUNET_YES;
619 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
620 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
622 case 4: /* directory */
624 GNUNET_BIO_read_int32 (rh, &dsize)) ||
625 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
627 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
629 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
631 ret->data.dir.dir_size = (uint32_t) dsize;
632 ret->is_directory = GNUNET_YES;
633 if (filename != NULL)
635 ret->data.dir.entries = deserialize_file_information (h, filename);
636 GNUNET_free (filename);
638 nxt = ret->data.dir.entries;
650 /* FIXME: adjust ret->start_time! */
651 ret->serialization = GNUNET_strdup (fn);
653 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
655 if (filename != NULL)
657 ret->next = deserialize_file_information (h, filename);
658 GNUNET_free (filename);
661 GNUNET_free_non_null (ksks);
662 GNUNET_free_non_null (chks);
665 GNUNET_free_non_null (ksks);
666 GNUNET_free_non_null (chks);
667 GNUNET_free_non_null (filename);
668 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
674 * Using the given serialization filename, try to deserialize
675 * the file-information tree associated with it.
677 * @param h master context
678 * @param filename name of the file (without directory) with
680 * @return NULL on error
682 static struct GNUNET_FS_FileInformation *
683 deserialize_file_information (struct GNUNET_FS_Handle *h,
684 const char *filename)
686 struct GNUNET_FS_FileInformation *ret;
687 struct GNUNET_BIO_ReadHandle *rh;
690 rh = get_read_handle (h, "publish-fi", filename);
693 ret = deserialize_fi_node (h, filename, rh);
695 GNUNET_BIO_read_close (rh, &emsg))
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698 _("Failed to resume publishing information `%s': %s\n"),
708 * Given a serialization name (full absolute path), return the
709 * basename of the file (without the path), which must only
710 * consist of the 6 random characters.
712 * @param fullname name to extract the basename from
713 * @return copy of the basename, NULL on error
716 get_serialization_short_name (const char *fullname)
723 /* FIXME: we could do this faster since we know
724 the length of 'end'... */
727 if (DIR_SEPARATOR == *nxt)
731 if ( (end == NULL) ||
732 (strlen (end) == 0) )
737 GNUNET_break (6 == strlen (end));
738 return GNUNET_strdup (end);
743 * Create a new random name for serialization. Also checks if persistence
744 * is enabled and returns NULL if not.
746 * @param h master context
747 * @param ext component of the path
748 * @return NULL on errror
751 make_serialization_file_name (struct GNUNET_FS_Handle *h,
758 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
759 return NULL; /* persistence not requested */
760 dn = get_serialization_file_name (h, ext, "");
761 fn = GNUNET_DISK_mktemp (dn);
764 return NULL; /* epic fail */
765 ret = get_serialization_short_name (fn);
772 * Copy all of the data from the reader to the write handle.
774 * @param wh write handle
775 * @param fi file with reader
776 * @return GNUNET_OK on success
779 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
780 struct GNUNET_FS_FileInformation * fi)
789 while (off < fi->data.file.file_size)
791 ret = fi->data.file.reader (fi->data.file.reader_cls,
798 return GNUNET_SYSERR;
801 GNUNET_BIO_write (wh, buf, ret))
802 return GNUNET_SYSERR;
810 * Create a temporary file on disk to store the current
813 * @param fi file information to sync with disk
816 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
819 struct GNUNET_BIO_WriteHandle *wh;
824 if (NULL == fi->serialization)
825 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
826 if (NULL == fi->serialization)
828 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
831 GNUNET_free (fi->serialization);
832 fi->serialization = NULL;
835 if (GNUNET_YES == fi->is_directory)
837 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
839 else if (GNUNET_YES == fi->data.file.have_hash)
841 else if (GNUNET_YES == fi->data.file.do_index)
845 if (fi->keywords != NULL)
846 ksks = GNUNET_FS_uri_to_string (fi->keywords);
849 if (fi->chk_uri != NULL)
850 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
854 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
856 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
858 GNUNET_BIO_write_string (wh, ksks)) ||
860 GNUNET_BIO_write_string (wh, chks)) ||
862 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
864 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
866 GNUNET_BIO_write_string (wh, fi->emsg)) ||
868 GNUNET_BIO_write_string (wh, fi->filename)) ||
870 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
872 GNUNET_BIO_write_int32 (wh, fi->priority)) )
874 GNUNET_free_non_null (chks);
876 GNUNET_free_non_null (ksks);
881 case 0: /* file-insert */
883 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
885 if ( (GNUNET_NO == fi->is_published) &&
886 (NULL == fi->filename) )
888 copy_from_reader (wh, fi))
891 case 1: /* file-index, no hash */
892 if (NULL == fi->filename)
895 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
898 case 2: /* file-index-with-hash */
899 case 3: /* file-index-with-hash-confirmed */
900 if (NULL == fi->filename)
903 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
905 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
908 case 4: /* directory */
910 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
912 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
914 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
922 GNUNET_BIO_write_string (wh, fi->next->serialization))
925 GNUNET_BIO_write_close (wh))
928 (void) GNUNET_BIO_write_close (wh);
929 GNUNET_free_non_null (chks);
930 GNUNET_free_non_null (ksks);
931 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
932 if (0 != UNLINK (fn))
933 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
935 GNUNET_free (fi->serialization);
936 fi->serialization = NULL;
942 * Find the entry in the file information struct where the
943 * serialization filename matches the given name.
945 * @param pos file information to search
946 * @param srch filename to search for
947 * @return NULL if srch was not found in this subtree
949 static struct GNUNET_FS_FileInformation *
950 find_file_position (struct GNUNET_FS_FileInformation *pos,
953 struct GNUNET_FS_FileInformation *r;
957 if (0 == strcmp (srch,
960 if (pos->is_directory)
962 r = find_file_position (pos->data.dir.entries,
974 * Signal the FS's progress function that we are resuming
977 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
978 * @param fi the entry in the publish-structure
979 * @param length length of the file or directory
980 * @param meta metadata for the file or directory (can be modified)
981 * @param uri pointer to the keywords that will be used for this entry (can be modified)
982 * @param anonymity pointer to selected anonymity level (can be modified)
983 * @param priority pointer to selected priority (can be modified)
984 * @param expirationTime pointer to selected expiration time (can be modified)
985 * @param client_info pointer to client context set upon creation (can be modified)
986 * @return GNUNET_OK to continue (always)
989 fip_signal_resume(void *cls,
990 struct GNUNET_FS_FileInformation *fi,
992 struct GNUNET_CONTAINER_MetaData *meta,
993 struct GNUNET_FS_Uri **uri,
996 struct GNUNET_TIME_Absolute *expirationTime,
999 struct GNUNET_FS_PublishContext *sc = cls;
1000 struct GNUNET_FS_ProgressInfo pi;
1002 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1003 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1004 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1005 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1011 * Function called with a filename of serialized publishing operation
1014 * @param cls the 'struct GNUNET_FS_Handle*'
1015 * @param filename complete filename (absolute path)
1016 * @return GNUNET_OK (continue to iterate)
1019 deserialize_publish_file (void *cls,
1020 const char *filename)
1022 struct GNUNET_FS_Handle *h = cls;
1023 struct GNUNET_BIO_ReadHandle *rh;
1024 struct GNUNET_FS_PublishContext *pc;
1032 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1034 pc->serialization = get_serialization_short_name (filename);
1038 rh = GNUNET_BIO_read_open (filename);
1042 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1044 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1046 GNUNET_BIO_read_int32 (rh, &options)) ||
1048 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1050 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1052 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1054 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1056 pc->options = options;
1057 pc->all_done = all_done;
1058 pc->fi = deserialize_file_information (h, fi_root);
1063 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1064 if (pc->namespace == NULL)
1066 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1067 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1074 pc->fi_pos = find_file_position (pc->fi,
1076 GNUNET_free (fi_pos);
1078 if (pc->fi_pos == NULL)
1080 /* failed to find position for resuming, outch! Will start from root! */
1082 if (pc->all_done != GNUNET_YES)
1083 pc->fi_pos = pc->fi;
1086 /* generate RESUME event(s) */
1087 GNUNET_FS_file_information_inspect (pc->fi,
1091 /* re-start publishing (if needed)... */
1092 if (pc->all_done != GNUNET_YES)
1094 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1095 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1096 &GNUNET_FS_publish_main_,
1099 GNUNET_BIO_read_close (rh, &emsg))
1101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1102 _("Failure while resuming publishing operation `%s': %s\n"),
1107 GNUNET_free_non_null (ns);
1110 GNUNET_free_non_null (pc->nid);
1111 GNUNET_free_non_null (pc->nuid);
1112 GNUNET_free_non_null (fi_root);
1113 GNUNET_free_non_null (fi_pos);
1114 GNUNET_free_non_null (ns);
1115 if ( (rh != NULL) &&
1117 GNUNET_BIO_read_close (rh, &emsg)) )
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Failed to resume publishing operation `%s': %s\n"),
1126 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1127 if (pc->serialization != NULL)
1128 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1129 GNUNET_free_non_null (pc->serialization);
1136 * Synchronize this publishing struct with its mirror
1137 * on disk. Note that all internal FS-operations that change
1138 * publishing structs should already call "sync" internally,
1139 * so this function is likely not useful for clients.
1141 * @param pc the struct to sync
1144 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1146 struct GNUNET_BIO_WriteHandle *wh;
1148 if (NULL == pc->serialization)
1149 pc->serialization = make_serialization_file_name (pc->h,
1151 if (NULL == pc->serialization)
1155 if (NULL == pc->fi->serialization)
1160 wh = get_write_handle (pc->h, "publish", pc->serialization);
1162 GNUNET_BIO_write_string (wh, pc->nid)) ||
1164 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1166 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1168 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1170 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1172 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1174 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1176 (void) GNUNET_BIO_write_close (wh);
1177 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1178 GNUNET_free (pc->serialization);
1179 pc->serialization = NULL;
1183 GNUNET_BIO_write_close (wh))
1185 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1186 GNUNET_free (pc->serialization);
1187 pc->serialization = NULL;
1194 * Synchronize this unindex struct with its mirror
1195 * on disk. Note that all internal FS-operations that change
1196 * publishing structs should already call "sync" internally,
1197 * so this function is likely not useful for clients.
1199 * @param uc the struct to sync
1202 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1204 struct GNUNET_BIO_WriteHandle *wh;
1206 if (UNINDEX_STATE_ABORTED == uc->state)
1208 if (NULL == uc->serialization)
1209 uc->serialization = make_serialization_file_name (uc->h,
1211 if (NULL == uc->serialization)
1213 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1215 GNUNET_BIO_write_string (wh, uc->filename)) ||
1217 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1219 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1221 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1222 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1224 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1225 ( (uc->state == UNINDEX_STATE_ERROR) &&
1227 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1229 (void) GNUNET_BIO_write_close (wh);
1230 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1231 GNUNET_free (uc->serialization);
1232 uc->serialization = NULL;
1236 GNUNET_BIO_write_close (wh))
1238 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1239 GNUNET_free (uc->serialization);
1240 uc->serialization = NULL;
1247 * Synchronize this download struct with its mirror
1248 * on disk. Note that all internal FS-operations that change
1249 * publishing structs should already call "sync" internally,
1250 * so this function is likely not useful for clients.
1252 * @param dc the struct to sync
1255 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1262 * Synchronize this search result with its mirror
1263 * on disk. Note that all internal FS-operations that change
1264 * publishing structs should already call "sync" internally,
1265 * so this function is likely not useful for clients.
1267 * @param key key for the search result
1268 * @param sr the struct to sync
1271 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1272 struct SearchResult *sr)
1274 struct GNUNET_BIO_WriteHandle *wh;
1277 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1278 (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1280 if (NULL == sr->serialization)
1281 sr->serialization = make_serialization_file_name (sr->sc->h,
1283 if (NULL == sr->serialization)
1285 wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1286 uris = GNUNET_FS_uri_to_string (sr->uri);
1288 GNUNET_BIO_write_string (wh, uris)) ||
1290 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1292 GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1294 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1296 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1298 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1300 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1303 GNUNET_BIO_write_close (wh))
1308 GNUNET_free_non_null (uris);
1311 GNUNET_free_non_null (uris);
1313 (void) GNUNET_BIO_write_close (wh);
1314 GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1315 GNUNET_free (sr->serialization);
1316 sr->serialization = NULL;
1321 * Synchronize this search struct with its mirror
1322 * on disk. Note that all internal FS-operations that change
1323 * publishing structs should already call "sync" internally,
1324 * so this function is likely not useful for clients.
1326 * @param sc the struct to sync
1329 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1331 struct GNUNET_BIO_WriteHandle *wh;
1332 struct GNUNET_FS_SearchContext *scc;
1336 if (NULL == sc->serialization)
1337 sc->serialization = make_serialization_file_name (sc->h,
1339 if (NULL == sc->serialization)
1341 wh = get_write_handle (sc->h, "search", sc->serialization);
1342 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1343 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1344 uris = GNUNET_FS_uri_to_string (sc->uri);
1345 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1347 GNUNET_BIO_write_string (wh, uris)) ||
1349 GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1351 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1353 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1355 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1357 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1361 scc = sc->child_head;
1364 if (scc->serialization == NULL)
1367 GNUNET_BIO_write_string (wh, scc->serialization))
1371 GNUNET_BIO_write_string (wh, NULL);
1373 GNUNET_BIO_write_close (wh))
1381 (void) GNUNET_BIO_write_close (wh);
1382 GNUNET_free_non_null (uris);
1383 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1384 GNUNET_free (sc->serialization);
1385 sc->serialization = NULL;
1390 * Deserialize information about pending publish operations.
1392 * @param h master context
1395 deserialize_publish (struct GNUNET_FS_Handle *h)
1399 dn = get_serialization_file_name (h, "publish", "");
1402 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1408 * Function called with a filename of serialized unindexing operation
1411 * @param cls the 'struct GNUNET_FS_Handle*'
1412 * @param filename complete filename (absolute path)
1413 * @return GNUNET_OK (continue to iterate)
1416 deserialize_unindex_file (void *cls,
1417 const char *filename)
1419 struct GNUNET_FS_Handle *h = cls;
1420 struct GNUNET_BIO_ReadHandle *rh;
1421 struct GNUNET_FS_UnindexContext *uc;
1422 struct GNUNET_FS_ProgressInfo pi;
1426 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1428 uc->serialization = get_serialization_short_name (filename);
1429 rh = GNUNET_BIO_read_open (filename);
1433 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1435 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1437 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1439 GNUNET_BIO_read_int32 (rh, &state)) )
1441 uc->state = (enum UnindexState) state;
1444 case UNINDEX_STATE_HASHING:
1446 case UNINDEX_STATE_FS_NOTIFY:
1448 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1451 case UNINDEX_STATE_DS_REMOVE:
1453 case UNINDEX_STATE_COMPLETE:
1455 case UNINDEX_STATE_ERROR:
1457 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1460 case UNINDEX_STATE_ABORTED:
1467 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1468 pi.value.unindex.specifics.resume.message = uc->emsg;
1469 GNUNET_FS_unindex_make_status_ (&pi,
1471 (uc->state == UNINDEX_STATE_COMPLETE)
1476 case UNINDEX_STATE_HASHING:
1477 GNUNET_CRYPTO_hash_file (uc->h->sched,
1478 GNUNET_SCHEDULER_PRIORITY_IDLE,
1481 &GNUNET_FS_unindex_process_hash_,
1484 case UNINDEX_STATE_FS_NOTIFY:
1485 uc->state = UNINDEX_STATE_HASHING;
1486 GNUNET_FS_unindex_process_hash_ (uc,
1489 case UNINDEX_STATE_DS_REMOVE:
1490 GNUNET_FS_unindex_do_remove_ (uc);
1492 case UNINDEX_STATE_COMPLETE:
1493 case UNINDEX_STATE_ERROR:
1494 /* no need to resume any operation, we were done */
1500 GNUNET_BIO_read_close (rh, &emsg))
1502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1503 _("Failure while resuming unindexing operation `%s': %s\n"),
1510 GNUNET_free_non_null (uc->filename);
1511 if ( (rh != NULL) &&
1513 GNUNET_BIO_read_close (rh, &emsg)) )
1515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1516 _("Failed to resume unindexing operation `%s': %s\n"),
1521 if (uc->serialization != NULL)
1522 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1523 GNUNET_free_non_null (uc->serialization);
1530 * Deserialize information about pending publish operations.
1532 * @param h master context
1535 deserialize_unindex (struct GNUNET_FS_Handle *h)
1539 dn = get_serialization_file_name (h, "unindex", "");
1542 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1548 * Function called with a filename of serialized search result
1551 * @param cls the 'struct GNUNET_FS_SearchContext*'
1552 * @param filename complete filename (absolute path)
1553 * @return GNUNET_OK (continue to iterate)
1556 deserialize_search_result (void *cls,
1557 const char *filename)
1559 struct GNUNET_FS_SearchContext *sc = cls;
1564 struct GNUNET_BIO_ReadHandle *rh;
1565 struct SearchResult *sr;
1566 GNUNET_HashCode key;
1568 ser = get_serialization_short_name (filename);
1569 rh = GNUNET_BIO_read_open (filename);
1574 GNUNET_snprintf (pbuf,
1580 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1587 sr = GNUNET_malloc (sizeof (struct SearchResult));
1588 sr->serialization = ser;
1590 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1591 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1592 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1593 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1595 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1597 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1599 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1601 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1603 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1605 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1608 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1611 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1614 GNUNET_free_non_null (emsg);
1615 GNUNET_free_non_null (uris);
1616 if (sr->uri != NULL)
1617 GNUNET_FS_uri_destroy (sr->uri);
1618 if (sr->meta != NULL)
1619 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1620 GNUNET_free (sr->serialization);
1627 * Iterator over search results signaling resume to the client for
1630 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1631 * @param key current key code
1632 * @param value value in the hash map, the 'struct SearchResult'
1633 * @return GNUNET_YES (we should continue to iterate)
1636 signal_result_resume (void *cls,
1637 const GNUNET_HashCode * key,
1640 struct GNUNET_FS_SearchContext *sc = cls;
1641 struct GNUNET_FS_ProgressInfo pi;
1642 struct SearchResult *sr = value;
1644 if (0 == sr->mandatory_missing)
1646 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1647 pi.value.search.specifics.resume_result.meta = sr->meta;
1648 pi.value.search.specifics.resume_result.uri = sr->uri;
1649 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1650 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1651 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1652 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1655 GNUNET_FS_search_start_probe_ (sr);
1661 * Iterator over search results freeing each.
1663 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1664 * @param key current key code
1665 * @param value value in the hash map, the 'struct SearchResult'
1666 * @return GNUNET_YES (we should continue to iterate)
1669 free_result (void *cls,
1670 const GNUNET_HashCode * key,
1673 struct SearchResult *sr = value;
1675 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1676 GNUNET_FS_uri_destroy (sr->uri);
1683 * Free memory allocated by the search context and its children
1685 * @param sc search context to free
1688 free_search_context (struct GNUNET_FS_SearchContext *sc)
1690 struct GNUNET_FS_SearchContext *scc;
1692 while (NULL != (scc = sc->child_head))
1694 GNUNET_CONTAINER_DLL_remove (sc->child_head,
1697 free_search_context (scc);
1699 GNUNET_free_non_null (sc->emsg);
1700 if (sc->serialization != NULL)
1701 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1702 /* FIXME: remove 'pbuf' directory with search results as well! */
1703 GNUNET_free_non_null (sc->serialization);
1704 if (sc->uri != NULL)
1705 GNUNET_FS_uri_destroy (sc->uri);
1706 if (sc->master_result_map != NULL)
1708 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1711 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1718 * Deserialize a download.
1720 * @param h overall context
1721 * @param rh file to deserialize from
1722 * @param parent parent download
1723 * @param serialization name under which the search was serialized
1726 deserialize_download (struct GNUNET_FS_Handle *h,
1727 struct GNUNET_BIO_ReadHandle *rh,
1728 struct GNUNET_FS_DownloadContext *parent,
1729 const char *serialization);
1733 * Function called with a filename of serialized sub-download
1736 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
1737 * @param filename complete filename (absolute path)
1738 * @return GNUNET_OK (continue to iterate)
1741 deserialize_subdownload (void *cls,
1742 const char *filename)
1744 struct GNUNET_FS_DownloadContext *parent = cls;
1747 struct GNUNET_BIO_ReadHandle *rh;
1749 ser = get_serialization_short_name (filename);
1750 rh = GNUNET_BIO_read_open (filename);
1751 deserialize_download (parent->h,
1756 GNUNET_BIO_read_close (rh, &emsg))
1758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1759 _("Failed to resume sub-download `%s': %s\n"),
1770 * Deserialize a download.
1772 * @param h overall context
1773 * @param rh file to deserialize from
1774 * @param parent parent download
1775 * @param serialization name under which the search was serialized
1778 deserialize_download (struct GNUNET_FS_Handle *h,
1779 struct GNUNET_BIO_ReadHandle *rh,
1780 struct GNUNET_FS_DownloadContext *parent,
1781 const char *serialization)
1783 struct GNUNET_FS_DownloadContext *dc;
1784 struct GNUNET_FS_DownloadContext *dcc;
1786 struct GNUNET_FS_ProgressInfo pi;
1793 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
1794 dc->parent = parent;
1796 dc->serialization = GNUNET_strdup (serialization);
1800 GNUNET_BIO_read_string (rh, "-uri", &uris, 10*1024)) ||
1801 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1802 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1803 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1805 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1807 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1809 GNUNET_BIO_read_int32 (rh, &options)) ||
1811 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1813 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1815 /* FIXME: adjust start_time.value */
1816 sc->options = (enum GNUNET_FS_SearchOptions) options;
1817 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1819 GNUNET_snprintf (pbuf,
1825 dn = get_serialization_file_name (h, pbuf, "");
1828 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
1832 if ('\0' == in_pause)
1835 GNUNET_FS_search_start_searching_ (sc))
1842 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1845 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1847 pi.value.search.specifics.resume.message = sc->emsg;
1848 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1850 GNUNET_FS_download_make_status_ (&pi,
1852 dcc = dc->child_head;
1855 /* FIXME: wrong, need recursion! */
1856 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1858 pi.value.search.specifics.resume.message = scc->emsg;
1859 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1861 GNUNET_FS_download_make_status_ (&pi,
1871 GNUNET_free_non_null (emsg);
1872 free_search_context (sc);
1874 GNUNET_free_non_null (uris);
1879 * Deserialize a search.
1881 * @param h overall context
1882 * @param rh file to deserialize from
1883 * @param parent parent search
1884 * @param serialization name under which the search was serialized
1886 static struct GNUNET_FS_SearchContext *
1887 deserialize_search (struct GNUNET_FS_Handle *h,
1888 struct GNUNET_BIO_ReadHandle *rh,
1889 struct GNUNET_FS_SearchContext *parent,
1890 const char *serialization)
1892 struct GNUNET_FS_SearchContext *sc;
1893 struct GNUNET_FS_SearchContext *scc;
1894 struct GNUNET_BIO_ReadHandle *rhc;
1896 struct GNUNET_FS_ProgressInfo pi;
1906 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1907 sc->parent = parent;
1909 sc->serialization = GNUNET_strdup (serialization);
1911 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
1912 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1913 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1914 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1916 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1918 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1920 GNUNET_BIO_read_int32 (rh, &options)) ||
1922 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1924 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1926 /* FIXME: adjust start_time.value */
1927 sc->options = (enum GNUNET_FS_SearchOptions) options;
1928 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1929 GNUNET_snprintf (pbuf,
1935 dn = get_serialization_file_name (h, pbuf, "");
1938 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
1941 if ('\0' == in_pause)
1944 GNUNET_FS_search_start_searching_ (sc))
1950 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
1952 if (child_ser == NULL)
1954 rhc = get_read_handle (h, "search-children", child_ser);
1957 scc = deserialize_search (h, rhc, sc, child_ser);
1959 GNUNET_CONTAINER_DLL_insert (sc->child_head,
1964 GNUNET_BIO_read_close (rhc, &emsg))
1966 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1967 _("Failed to resume sub-search `%s': %s\n"),
1973 GNUNET_free (child_ser);
1976 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1979 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1980 pi.value.search.specifics.resume.message = sc->emsg;
1981 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1982 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1984 scc = sc->child_head;
1987 /* FIXME: wrong, need recursion! */
1988 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1989 pi.value.search.specifics.resume.message = scc->emsg;
1990 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1991 scc->client_info = GNUNET_FS_search_make_status_ (&pi,
1996 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1997 &signal_result_resume,
2002 GNUNET_free_non_null (emsg);
2003 free_search_context (sc);
2004 GNUNET_free_non_null (uris);
2010 * Function called with a filename of serialized search operation
2013 * @param cls the 'struct GNUNET_FS_Handle*'
2014 * @param filename complete filename (absolute path)
2015 * @return GNUNET_OK (continue to iterate)
2018 deserialize_search_file (void *cls,
2019 const char *filename)
2021 struct GNUNET_FS_Handle *h = cls;
2024 struct GNUNET_BIO_ReadHandle *rh;
2025 struct GNUNET_FS_SearchContext *sc;
2027 ser = get_serialization_short_name (filename);
2028 rh = GNUNET_BIO_read_open (filename);
2033 GNUNET_FS_remove_sync_file_ (h, "search", ser);
2038 sc = deserialize_search (h, rh, NULL, ser);
2041 GNUNET_BIO_read_close (rh, &emsg))
2043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2044 _("Failure while resuming search operation `%s': %s\n"),
2054 * Deserialize information about pending search operations.
2056 * @param h master context
2059 deserialize_search_master (struct GNUNET_FS_Handle *h)
2063 dn = get_serialization_file_name (h, "search", "");
2066 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
2072 * Function called with a filename of serialized download operation
2075 * @param cls the 'struct GNUNET_FS_Handle*'
2076 * @param filename complete filename (absolute path)
2077 * @return GNUNET_OK (continue to iterate)
2080 deserialize_download_file (void *cls,
2081 const char *filename)
2083 struct GNUNET_FS_Handle *h = cls;
2086 struct GNUNET_BIO_ReadHandle *rh;
2088 ser = get_serialization_short_name (filename);
2089 rh = GNUNET_BIO_read_open (filename);
2094 GNUNET_FS_remove_sync_file_ (h, "download", ser);
2099 deserialize_download (h, rh, NULL, ser);
2102 GNUNET_BIO_read_close (rh, &emsg))
2104 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2105 _("Failure while resuming download operation `%s': %s\n"),
2115 * Deserialize information about pending download operations.
2117 * @param h master context
2120 deserialize_download_master (struct GNUNET_FS_Handle *h)
2124 dn = get_serialization_file_name (h, "download", "");
2127 GNUNET_DISK_directory_scan (dn, &deserialize_download_file, h);
2133 * Setup a connection to the file-sharing service.
2135 * @param sched scheduler to use
2136 * @param cfg configuration to use
2137 * @param client_name unique identifier for this client
2138 * @param upcb function to call to notify about FS actions
2139 * @param upcb_cls closure for upcb
2140 * @param flags specific attributes for fs-operations
2141 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2142 * @return NULL on error
2144 struct GNUNET_FS_Handle *
2145 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2146 const struct GNUNET_CONFIGURATION_Handle *cfg,
2147 const char *client_name,
2148 GNUNET_FS_ProgressCallback upcb,
2150 enum GNUNET_FS_Flags flags,
2153 struct GNUNET_FS_Handle *ret;
2154 struct GNUNET_CLIENT_Connection *client;
2155 enum GNUNET_FS_OPTIONS opt;
2158 client = GNUNET_CLIENT_connect (sched,
2163 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2166 ret->client_name = GNUNET_strdup (client_name);
2168 ret->upcb_cls = upcb_cls;
2169 ret->client = client;
2171 ret->max_parallel_downloads = 1;
2172 ret->max_parallel_requests = 1;
2173 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2174 va_start (ap, flags);
2175 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2179 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2180 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2182 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2183 ret->max_parallel_requests = va_arg (ap, unsigned int);
2187 GNUNET_free (ret->client_name);
2194 // FIXME: setup receive-loop with client (do we need one?)
2195 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2197 /* FIXME: could write one generic deserialization
2198 function instead of these four... */
2199 deserialize_publish (ret);
2200 deserialize_search_master (ret);
2201 deserialize_download_master (ret);
2202 deserialize_unindex (ret);
2209 * Close our connection with the file-sharing service.
2210 * The callback given to GNUNET_FS_start will no longer be
2211 * called after this function returns.
2213 * @param h handle that was returned from GNUNET_FS_start
2216 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2218 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
2220 // FIXME: generate SUSPEND events and clean up state!
2222 // FIXME: terminate receive-loop with client (do we need one?)
2223 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2224 GNUNET_SCHEDULER_cancel (h->sched,
2226 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
2227 GNUNET_free (h->client_name);