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 * Closure for "data_reader_file".
221 * Name of the file to read.
226 * File descriptor, NULL if it has not yet been opened.
228 struct GNUNET_DISK_FileHandle *fd;
233 * Function that provides data by reading from a file.
235 * @param cls closure (points to the file information)
236 * @param offset offset to read from; it is possible
237 * that the caller might need to go backwards
239 * @param max maximum number of bytes that should be
240 * copied to buf; readers are not allowed
241 * to provide less data unless there is an error;
242 * a value of "0" will be used at the end to allow
243 * the reader to clean up its internal state
244 * @param buf where the reader should write the data
245 * @param emsg location for the reader to store an error message
246 * @return number of bytes written, usually "max", 0 on error
249 GNUNET_FS_data_reader_file_(void *cls,
255 struct FileInfo *fi = cls;
261 GNUNET_DISK_file_close (fi->fd);
262 GNUNET_free (fi->filename);
268 fi->fd = GNUNET_DISK_file_open (fi->filename,
269 GNUNET_DISK_OPEN_READ,
270 GNUNET_DISK_PERM_NONE);
273 GNUNET_asprintf (emsg,
274 _("Could not open file `%s': %s"),
280 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
281 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
284 GNUNET_asprintf (emsg,
285 _("Could not read file `%s': %s"),
292 GNUNET_asprintf (emsg,
293 _("Short read reading from file `%s'!"),
302 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
304 * @param filename file to read
305 * @return closure to use, NULL on error
308 GNUNET_FS_make_file_reader_context_ (const char *filename)
312 fi = GNUNET_malloc (sizeof(struct FileInfo));
313 fi->filename = GNUNET_STRINGS_filename_expand (filename);
314 if (fi->filename == NULL)
324 * Function that provides data by copying from a buffer.
326 * @param cls closure (points to the buffer)
327 * @param offset offset to read from; it is possible
328 * that the caller might need to go backwards
330 * @param max maximum number of bytes that should be
331 * copied to buf; readers are not allowed
332 * to provide less data unless there is an error;
333 * a value of "0" will be used at the end to allow
334 * the reader to clean up its internal state
335 * @param buf where the reader should write the data
336 * @param emsg location for the reader to store an error message
337 * @return number of bytes written, usually "max", 0 on error
340 GNUNET_FS_data_reader_copy_ (void *cls,
350 GNUNET_free_non_null (data);
353 memcpy (buf, &data[offset], max);
360 * Return the full filename where we would store state information
361 * (for serialization/deserialization).
363 * @param h master context
364 * @param ext component of the path
365 * @param ent entity identifier (or emtpy string for the directory)
366 * @return NULL on error
369 get_serialization_file_name (struct GNUNET_FS_Handle *h,
376 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
381 GNUNET_asprintf (&ret,
389 GNUNET_free (basename);
395 * Return a read handle for deserialization.
397 * @param h master context
398 * @param ext component of the path
399 * @param ent entity identifier (or emtpy string for the directory)
400 * @return NULL on error
402 static struct GNUNET_BIO_ReadHandle *
403 get_read_handle (struct GNUNET_FS_Handle *h,
408 struct GNUNET_BIO_ReadHandle *ret;
410 fn = get_serialization_file_name (h, ext, ent);
413 ret = GNUNET_BIO_read_open (fn);
420 * Return a write handle for serialization.
422 * @param h master context
423 * @param ext component of the path
424 * @param ent entity identifier (or emtpy string for the directory)
425 * @return NULL on error
427 static struct GNUNET_BIO_WriteHandle *
428 get_write_handle (struct GNUNET_FS_Handle *h,
433 struct GNUNET_BIO_WriteHandle *ret;
435 fn = get_serialization_file_name (h, ext, ent);
438 ret = GNUNET_BIO_write_open (fn);
445 * Remove serialization/deserialization file from disk.
447 * @param h master context
448 * @param ext component of the path
449 * @param ent entity identifier
452 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
458 if ( (NULL == ent) ||
459 (0 == strlen (ent)) )
464 filename = get_serialization_file_name (h, ext, ent);
465 if (0 != UNLINK (filename))
466 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
469 GNUNET_free (filename);
475 * Using the given serialization filename, try to deserialize
476 * the file-information tree associated with it.
478 * @param h master context
479 * @param filename name of the file (without directory) with
481 * @return NULL on error
483 static struct GNUNET_FS_FileInformation *
484 deserialize_file_information (struct GNUNET_FS_Handle *h,
485 const char *filename);
489 * Using the given serialization filename, try to deserialize
490 * the file-information tree associated with it.
492 * @param h master context
493 * @param fn name of the file (without directory) with
495 * @param rh handle for reading
496 * @return NULL on error
498 static struct GNUNET_FS_FileInformation *
499 deserialize_fi_node (struct GNUNET_FS_Handle *h,
501 struct GNUNET_BIO_ReadHandle *rh)
503 struct GNUNET_FS_FileInformation *ret;
504 struct GNUNET_FS_FileInformation *nxt;
512 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
517 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
522 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
524 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
527 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
529 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
531 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
534 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
536 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
538 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
540 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
542 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
544 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
546 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
548 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
552 case 0: /* file-insert */
554 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
556 ret->is_directory = GNUNET_NO;
557 ret->data.file.do_index = GNUNET_NO;
558 ret->data.file.have_hash = GNUNET_NO;
559 ret->data.file.index_start_confirmed = GNUNET_NO;
560 if (GNUNET_NO == ret->is_published)
562 if (NULL == ret->filename)
564 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
565 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
566 if (ret->data.file.reader_cls == NULL)
569 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
574 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
575 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
579 case 1: /* file-index, no hash */
580 if (NULL == ret->filename)
583 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
585 ret->is_directory = GNUNET_NO;
586 ret->data.file.do_index = GNUNET_YES;
587 ret->data.file.have_hash = GNUNET_NO;
588 ret->data.file.index_start_confirmed = GNUNET_NO;
589 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
590 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
592 case 2: /* file-index-with-hash */
593 if (NULL == ret->filename)
596 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
598 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
600 ret->is_directory = GNUNET_NO;
601 ret->data.file.do_index = GNUNET_YES;
602 ret->data.file.have_hash = GNUNET_YES;
603 ret->data.file.index_start_confirmed = GNUNET_NO;
604 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
605 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
607 case 3: /* file-index-with-hash-confirmed */
608 if (NULL == ret->filename)
611 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
613 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
616 ret->is_directory = GNUNET_NO;
617 ret->data.file.do_index = GNUNET_YES;
618 ret->data.file.have_hash = GNUNET_YES;
619 ret->data.file.index_start_confirmed = GNUNET_YES;
620 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
621 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
623 case 4: /* directory */
625 GNUNET_BIO_read_int32 (rh, &dsize)) ||
626 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
628 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
630 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
632 ret->data.dir.dir_size = (uint32_t) dsize;
633 ret->is_directory = GNUNET_YES;
634 if (filename != NULL)
636 ret->data.dir.entries = deserialize_file_information (h, filename);
637 GNUNET_free (filename);
639 nxt = ret->data.dir.entries;
651 /* FIXME: adjust ret->start_time! */
652 ret->serialization = GNUNET_strdup (fn);
654 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
656 if (filename != NULL)
658 ret->next = deserialize_file_information (h, filename);
659 GNUNET_free (filename);
662 GNUNET_free_non_null (ksks);
663 GNUNET_free_non_null (chks);
666 GNUNET_free_non_null (ksks);
667 GNUNET_free_non_null (chks);
668 GNUNET_free_non_null (filename);
669 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
675 * Using the given serialization filename, try to deserialize
676 * the file-information tree associated with it.
678 * @param h master context
679 * @param filename name of the file (without directory) with
681 * @return NULL on error
683 static struct GNUNET_FS_FileInformation *
684 deserialize_file_information (struct GNUNET_FS_Handle *h,
685 const char *filename)
687 struct GNUNET_FS_FileInformation *ret;
688 struct GNUNET_BIO_ReadHandle *rh;
691 rh = get_read_handle (h, "publish-fi", filename);
694 ret = deserialize_fi_node (h, filename, rh);
696 GNUNET_BIO_read_close (rh, &emsg))
698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
699 _("Failed to resume publishing information `%s': %s\n"),
709 * Given a serialization name (full absolute path), return the
710 * basename of the file (without the path), which must only
711 * consist of the 6 random characters.
713 * @param fullname name to extract the basename from
714 * @return copy of the basename, NULL on error
717 get_serialization_short_name (const char *fullname)
724 /* FIXME: we could do this faster since we know
725 the length of 'end'... */
728 if (DIR_SEPARATOR == *nxt)
732 if ( (end == NULL) ||
733 (strlen (end) == 0) )
738 GNUNET_break (6 == strlen (end));
739 return GNUNET_strdup (end);
744 * Create a new random name for serialization. Also checks if persistence
745 * is enabled and returns NULL if not.
747 * @param h master context
748 * @param ext component of the path
749 * @return NULL on errror
752 make_serialization_file_name (struct GNUNET_FS_Handle *h,
759 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
760 return NULL; /* persistence not requested */
761 dn = get_serialization_file_name (h, ext, "");
762 fn = GNUNET_DISK_mktemp (dn);
765 return NULL; /* epic fail */
766 ret = get_serialization_short_name (fn);
773 * Copy all of the data from the reader to the write handle.
775 * @param wh write handle
776 * @param fi file with reader
777 * @return GNUNET_OK on success
780 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
781 struct GNUNET_FS_FileInformation * fi)
790 while (off < fi->data.file.file_size)
792 ret = fi->data.file.reader (fi->data.file.reader_cls,
799 return GNUNET_SYSERR;
802 GNUNET_BIO_write (wh, buf, ret))
803 return GNUNET_SYSERR;
811 * Create a temporary file on disk to store the current
814 * @param fi file information to sync with disk
817 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
820 struct GNUNET_BIO_WriteHandle *wh;
825 if (NULL == fi->serialization)
826 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
827 if (NULL == fi->serialization)
829 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
832 GNUNET_free (fi->serialization);
833 fi->serialization = NULL;
836 if (GNUNET_YES == fi->is_directory)
838 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
840 else if (GNUNET_YES == fi->data.file.have_hash)
842 else if (GNUNET_YES == fi->data.file.do_index)
846 if (fi->keywords != NULL)
847 ksks = GNUNET_FS_uri_to_string (fi->keywords);
850 if (fi->chk_uri != NULL)
851 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
855 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
857 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
859 GNUNET_BIO_write_string (wh, ksks)) ||
861 GNUNET_BIO_write_string (wh, chks)) ||
863 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
865 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
867 GNUNET_BIO_write_string (wh, fi->emsg)) ||
869 GNUNET_BIO_write_string (wh, fi->filename)) ||
871 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
873 GNUNET_BIO_write_int32 (wh, fi->priority)) )
875 GNUNET_free_non_null (chks);
877 GNUNET_free_non_null (ksks);
882 case 0: /* file-insert */
884 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
886 if ( (GNUNET_NO == fi->is_published) &&
887 (NULL == fi->filename) )
889 copy_from_reader (wh, fi))
892 case 1: /* file-index, no hash */
893 if (NULL == fi->filename)
896 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
899 case 2: /* file-index-with-hash */
900 case 3: /* file-index-with-hash-confirmed */
901 if (NULL == fi->filename)
904 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
906 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
909 case 4: /* directory */
911 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
913 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
915 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
923 GNUNET_BIO_write_string (wh, fi->next->serialization))
926 GNUNET_BIO_write_close (wh))
929 (void) GNUNET_BIO_write_close (wh);
930 GNUNET_free_non_null (chks);
931 GNUNET_free_non_null (ksks);
932 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
933 if (0 != UNLINK (fn))
934 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
936 GNUNET_free (fi->serialization);
937 fi->serialization = NULL;
943 * Find the entry in the file information struct where the
944 * serialization filename matches the given name.
946 * @param pos file information to search
947 * @param srch filename to search for
948 * @return NULL if srch was not found in this subtree
950 static struct GNUNET_FS_FileInformation *
951 find_file_position (struct GNUNET_FS_FileInformation *pos,
954 struct GNUNET_FS_FileInformation *r;
958 if (0 == strcmp (srch,
961 if (pos->is_directory)
963 r = find_file_position (pos->data.dir.entries,
975 * Signal the FS's progress function that we are resuming
978 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
979 * @param fi the entry in the publish-structure
980 * @param length length of the file or directory
981 * @param meta metadata for the file or directory (can be modified)
982 * @param uri pointer to the keywords that will be used for this entry (can be modified)
983 * @param anonymity pointer to selected anonymity level (can be modified)
984 * @param priority pointer to selected priority (can be modified)
985 * @param expirationTime pointer to selected expiration time (can be modified)
986 * @param client_info pointer to client context set upon creation (can be modified)
987 * @return GNUNET_OK to continue (always)
990 fip_signal_resume(void *cls,
991 struct GNUNET_FS_FileInformation *fi,
993 struct GNUNET_CONTAINER_MetaData *meta,
994 struct GNUNET_FS_Uri **uri,
997 struct GNUNET_TIME_Absolute *expirationTime,
1000 struct GNUNET_FS_PublishContext *sc = cls;
1001 struct GNUNET_FS_ProgressInfo pi;
1003 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1004 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1005 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1006 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1012 * Function called with a filename of serialized publishing operation
1015 * @param cls the 'struct GNUNET_FS_Handle*'
1016 * @param filename complete filename (absolute path)
1017 * @return GNUNET_OK (continue to iterate)
1020 deserialize_publish_file (void *cls,
1021 const char *filename)
1023 struct GNUNET_FS_Handle *h = cls;
1024 struct GNUNET_BIO_ReadHandle *rh;
1025 struct GNUNET_FS_PublishContext *pc;
1033 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1035 pc->serialization = get_serialization_short_name (filename);
1039 rh = GNUNET_BIO_read_open (filename);
1043 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1045 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1047 GNUNET_BIO_read_int32 (rh, &options)) ||
1049 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1051 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1053 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1055 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1057 pc->options = options;
1058 pc->all_done = all_done;
1059 pc->fi = deserialize_file_information (h, fi_root);
1064 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1065 if (pc->namespace == NULL)
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1068 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1075 pc->fi_pos = find_file_position (pc->fi,
1077 GNUNET_free (fi_pos);
1079 if (pc->fi_pos == NULL)
1081 /* failed to find position for resuming, outch! Will start from root! */
1083 if (pc->all_done != GNUNET_YES)
1084 pc->fi_pos = pc->fi;
1087 /* generate RESUME event(s) */
1088 GNUNET_FS_file_information_inspect (pc->fi,
1092 /* re-start publishing (if needed)... */
1093 if (pc->all_done != GNUNET_YES)
1095 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1096 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1097 &GNUNET_FS_publish_main_,
1100 GNUNET_BIO_read_close (rh, &emsg))
1102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1103 _("Failure while resuming publishing operation `%s': %s\n"),
1108 GNUNET_free_non_null (ns);
1111 GNUNET_free_non_null (pc->nid);
1112 GNUNET_free_non_null (pc->nuid);
1113 GNUNET_free_non_null (fi_root);
1114 GNUNET_free_non_null (fi_pos);
1115 GNUNET_free_non_null (ns);
1116 if ( (rh != NULL) &&
1118 GNUNET_BIO_read_close (rh, &emsg)) )
1120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1121 _("Failed to resume publishing operation `%s': %s\n"),
1127 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1128 if (pc->serialization != NULL)
1129 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1130 GNUNET_free_non_null (pc->serialization);
1137 * Synchronize this publishing struct with its mirror
1138 * on disk. Note that all internal FS-operations that change
1139 * publishing structs should already call "sync" internally,
1140 * so this function is likely not useful for clients.
1142 * @param pc the struct to sync
1145 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1147 struct GNUNET_BIO_WriteHandle *wh;
1149 if (NULL == pc->serialization)
1150 pc->serialization = make_serialization_file_name (pc->h,
1152 if (NULL == pc->serialization)
1156 if (NULL == pc->fi->serialization)
1161 wh = get_write_handle (pc->h, "publish", pc->serialization);
1163 GNUNET_BIO_write_string (wh, pc->nid)) ||
1165 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1167 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1169 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1171 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1173 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1175 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1177 (void) GNUNET_BIO_write_close (wh);
1178 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1179 GNUNET_free (pc->serialization);
1180 pc->serialization = NULL;
1184 GNUNET_BIO_write_close (wh))
1186 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1187 GNUNET_free (pc->serialization);
1188 pc->serialization = NULL;
1195 * Synchronize this unindex struct with its mirror
1196 * on disk. Note that all internal FS-operations that change
1197 * publishing structs should already call "sync" internally,
1198 * so this function is likely not useful for clients.
1200 * @param uc the struct to sync
1203 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1205 struct GNUNET_BIO_WriteHandle *wh;
1207 if (UNINDEX_STATE_ABORTED == uc->state)
1209 if (NULL == uc->serialization)
1210 uc->serialization = make_serialization_file_name (uc->h,
1212 if (NULL == uc->serialization)
1214 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1216 GNUNET_BIO_write_string (wh, uc->filename)) ||
1218 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1220 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1222 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1223 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1225 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1226 ( (uc->state == UNINDEX_STATE_ERROR) &&
1228 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1230 (void) GNUNET_BIO_write_close (wh);
1231 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1232 GNUNET_free (uc->serialization);
1233 uc->serialization = NULL;
1237 GNUNET_BIO_write_close (wh))
1239 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1240 GNUNET_free (uc->serialization);
1241 uc->serialization = NULL;
1248 * Synchronize this download struct with its mirror
1249 * on disk. Note that all internal FS-operations that change
1250 * publishing structs should already call "sync" internally,
1251 * so this function is likely not useful for clients.
1253 * @param dc the struct to sync
1256 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1263 * Synchronize this search result with its mirror
1264 * on disk. Note that all internal FS-operations that change
1265 * publishing structs should already call "sync" internally,
1266 * so this function is likely not useful for clients.
1268 * @param key key for the search result
1269 * @param sr the struct to sync
1272 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1273 struct SearchResult *sr)
1275 struct GNUNET_BIO_WriteHandle *wh;
1278 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1279 (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1281 if (NULL == sr->serialization)
1282 sr->serialization = make_serialization_file_name (sr->sc->h,
1284 if (NULL == sr->serialization)
1286 wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1287 uris = GNUNET_FS_uri_to_string (sr->uri);
1289 GNUNET_BIO_write_string (wh, uris)) ||
1291 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1293 GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1295 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1297 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1299 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1301 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1304 GNUNET_BIO_write_close (wh))
1309 GNUNET_free_non_null (uris);
1312 GNUNET_free_non_null (uris);
1314 (void) GNUNET_BIO_write_close (wh);
1315 GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1316 GNUNET_free (sr->serialization);
1317 sr->serialization = NULL;
1322 * Synchronize this search struct with its mirror
1323 * on disk. Note that all internal FS-operations that change
1324 * publishing structs should already call "sync" internally,
1325 * so this function is likely not useful for clients.
1327 * @param sc the struct to sync
1330 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1332 struct GNUNET_BIO_WriteHandle *wh;
1333 struct GNUNET_FS_SearchContext *scc;
1337 if (NULL == sc->serialization)
1338 sc->serialization = make_serialization_file_name (sc->h,
1340 if (NULL == sc->serialization)
1342 wh = get_write_handle (sc->h, "search", sc->serialization);
1343 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1344 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1345 uris = GNUNET_FS_uri_to_string (sc->uri);
1346 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1348 GNUNET_BIO_write_string (wh, uris)) ||
1350 GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1352 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1354 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1356 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1358 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1362 scc = sc->child_head;
1365 if (scc->serialization == NULL)
1368 GNUNET_BIO_write_string (wh, scc->serialization))
1372 GNUNET_BIO_write_string (wh, NULL);
1374 GNUNET_BIO_write_close (wh))
1382 (void) GNUNET_BIO_write_close (wh);
1383 GNUNET_free_non_null (uris);
1384 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1385 GNUNET_free (sc->serialization);
1386 sc->serialization = NULL;
1391 * Deserialize information about pending publish operations.
1393 * @param h master context
1396 deserialize_publish (struct GNUNET_FS_Handle *h)
1400 dn = get_serialization_file_name (h, "publish", "");
1403 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1409 * Function called with a filename of serialized unindexing operation
1412 * @param cls the 'struct GNUNET_FS_Handle*'
1413 * @param filename complete filename (absolute path)
1414 * @return GNUNET_OK (continue to iterate)
1417 deserialize_unindex_file (void *cls,
1418 const char *filename)
1420 struct GNUNET_FS_Handle *h = cls;
1421 struct GNUNET_BIO_ReadHandle *rh;
1422 struct GNUNET_FS_UnindexContext *uc;
1423 struct GNUNET_FS_ProgressInfo pi;
1427 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1429 uc->serialization = get_serialization_short_name (filename);
1430 rh = GNUNET_BIO_read_open (filename);
1434 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1436 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1438 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1440 GNUNET_BIO_read_int32 (rh, &state)) )
1442 uc->state = (enum UnindexState) state;
1445 case UNINDEX_STATE_HASHING:
1447 case UNINDEX_STATE_FS_NOTIFY:
1449 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1452 case UNINDEX_STATE_DS_REMOVE:
1454 case UNINDEX_STATE_COMPLETE:
1456 case UNINDEX_STATE_ERROR:
1458 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1461 case UNINDEX_STATE_ABORTED:
1468 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1469 pi.value.unindex.specifics.resume.message = uc->emsg;
1470 GNUNET_FS_unindex_make_status_ (&pi,
1472 (uc->state == UNINDEX_STATE_COMPLETE)
1477 case UNINDEX_STATE_HASHING:
1478 GNUNET_CRYPTO_hash_file (uc->h->sched,
1479 GNUNET_SCHEDULER_PRIORITY_IDLE,
1482 &GNUNET_FS_unindex_process_hash_,
1485 case UNINDEX_STATE_FS_NOTIFY:
1486 uc->state = UNINDEX_STATE_HASHING;
1487 GNUNET_FS_unindex_process_hash_ (uc,
1490 case UNINDEX_STATE_DS_REMOVE:
1491 GNUNET_FS_unindex_do_remove_ (uc);
1493 case UNINDEX_STATE_COMPLETE:
1494 case UNINDEX_STATE_ERROR:
1495 /* no need to resume any operation, we were done */
1501 GNUNET_BIO_read_close (rh, &emsg))
1503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1504 _("Failure while resuming unindexing operation `%s': %s\n"),
1511 GNUNET_free_non_null (uc->filename);
1512 if ( (rh != NULL) &&
1514 GNUNET_BIO_read_close (rh, &emsg)) )
1516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1517 _("Failed to resume unindexing operation `%s': %s\n"),
1522 if (uc->serialization != NULL)
1523 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1524 GNUNET_free_non_null (uc->serialization);
1531 * Deserialize information about pending publish operations.
1533 * @param h master context
1536 deserialize_unindex (struct GNUNET_FS_Handle *h)
1540 dn = get_serialization_file_name (h, "unindex", "");
1543 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1549 * Function called with a filename of serialized search result
1552 * @param cls the 'struct GNUNET_FS_SearchContext*'
1553 * @param filename complete filename (absolute path)
1554 * @return GNUNET_OK (continue to iterate)
1557 deserialize_search_result (void *cls,
1558 const char *filename)
1560 struct GNUNET_FS_SearchContext *sc = cls;
1565 struct GNUNET_BIO_ReadHandle *rh;
1566 struct SearchResult *sr;
1567 GNUNET_HashCode key;
1569 ser = get_serialization_short_name (filename);
1570 rh = GNUNET_BIO_read_open (filename);
1575 GNUNET_snprintf (pbuf,
1581 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1588 sr = GNUNET_malloc (sizeof (struct SearchResult));
1589 sr->serialization = ser;
1591 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1592 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1593 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1594 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1596 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1598 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1600 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1602 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1604 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1606 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1609 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1612 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1615 GNUNET_free_non_null (emsg);
1616 GNUNET_free_non_null (uris);
1617 if (sr->uri != NULL)
1618 GNUNET_FS_uri_destroy (sr->uri);
1619 if (sr->meta != NULL)
1620 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1621 GNUNET_free (sr->serialization);
1628 * Iterator over search results signaling resume to the client for
1631 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1632 * @param key current key code
1633 * @param value value in the hash map, the 'struct SearchResult'
1634 * @return GNUNET_YES (we should continue to iterate)
1637 signal_result_resume (void *cls,
1638 const GNUNET_HashCode * key,
1641 struct GNUNET_FS_SearchContext *sc = cls;
1642 struct GNUNET_FS_ProgressInfo pi;
1643 struct SearchResult *sr = value;
1645 if (0 == sr->mandatory_missing)
1647 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1648 pi.value.search.specifics.resume_result.meta = sr->meta;
1649 pi.value.search.specifics.resume_result.uri = sr->uri;
1650 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1651 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1652 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1653 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1656 GNUNET_FS_search_start_probe_ (sr);
1662 * Iterator over search results freeing each.
1664 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1665 * @param key current key code
1666 * @param value value in the hash map, the 'struct SearchResult'
1667 * @return GNUNET_YES (we should continue to iterate)
1670 free_result (void *cls,
1671 const GNUNET_HashCode * key,
1674 struct SearchResult *sr = value;
1676 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1677 GNUNET_FS_uri_destroy (sr->uri);
1684 * Free memory allocated by the search context and its children
1686 * @param sc search context to free
1689 free_search_context (struct GNUNET_FS_SearchContext *sc)
1691 struct GNUNET_FS_SearchContext *scc;
1693 while (NULL != (scc = sc->child_head))
1695 GNUNET_CONTAINER_DLL_remove (sc->child_head,
1698 free_search_context (scc);
1700 GNUNET_free_non_null (sc->emsg);
1701 if (sc->serialization != NULL)
1702 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1703 /* FIXME: remove 'pbuf' directory with search results as well! */
1704 GNUNET_free_non_null (sc->serialization);
1705 if (sc->uri != NULL)
1706 GNUNET_FS_uri_destroy (sc->uri);
1707 if (sc->master_result_map != NULL)
1709 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1712 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1719 * Deserialize a download.
1721 * @param h overall context
1722 * @param rh file to deserialize from
1723 * @param parent parent download
1724 * @param serialization name under which the search was serialized
1727 deserialize_download (struct GNUNET_FS_Handle *h,
1728 struct GNUNET_BIO_ReadHandle *rh,
1729 struct GNUNET_FS_DownloadContext *parent,
1730 const char *serialization);
1734 * Function called with a filename of serialized sub-download
1737 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
1738 * @param filename complete filename (absolute path)
1739 * @return GNUNET_OK (continue to iterate)
1742 deserialize_subdownload (void *cls,
1743 const char *filename)
1745 struct GNUNET_FS_DownloadContext *parent = cls;
1748 struct GNUNET_BIO_ReadHandle *rh;
1750 ser = get_serialization_short_name (filename);
1751 rh = GNUNET_BIO_read_open (filename);
1752 deserialize_download (parent->h,
1757 GNUNET_BIO_read_close (rh, &emsg))
1759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1760 _("Failed to resume sub-download `%s': %s\n"),
1771 * Send the 'resume' signal to the callback; also actually
1772 * resume the download (put it in the queue). Does this
1773 * recursively for the top-level download and all child
1776 * @param dc download to resume
1779 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
1781 struct GNUNET_FS_DownloadContext *dcc;
1782 struct GNUNET_FS_ProgressInfo pi;
1784 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1785 pi.value.download.specifics.resume.meta = dc->meta;
1786 pi.value.download.specifics.resume.message = dc->emsg;
1787 GNUNET_FS_download_make_status_ (&pi,
1789 dcc = dc->child_head;
1792 signal_download_resume (dcc);
1795 if (dc->pending != NULL)
1796 GNUNET_FS_download_start_downloading_ (dc);
1801 * Free this download context and all of its descendants.
1802 * (only works during deserialization since not all possible
1803 * state it taken care of).
1805 * @param dc context to free
1808 free_download_context (struct GNUNET_FS_DownloadContext *dc)
1810 struct GNUNET_FS_DownloadContext *dcc;
1811 struct DownloadRequest *dr;
1812 if (dc->meta != NULL)
1813 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1814 if (dc->uri != NULL)
1815 GNUNET_FS_uri_destroy (dc->uri);
1816 GNUNET_free_non_null (dc->temp_filename);
1817 GNUNET_free_non_null (dc->emsg);
1818 GNUNET_free_non_null (dc->filename);
1819 while (NULL != (dcc = dc->child_head))
1821 GNUNET_CONTAINER_DLL_remove (dc->child_head,
1824 free_download_context (dcc);
1826 while (NULL != (dr = dc->pending))
1828 dc->pending = dr->next;
1836 * Deserialize a download.
1838 * @param h overall context
1839 * @param rh file to deserialize from
1840 * @param parent parent download
1841 * @param serialization name under which the search was serialized
1844 deserialize_download (struct GNUNET_FS_Handle *h,
1845 struct GNUNET_BIO_ReadHandle *rh,
1846 struct GNUNET_FS_DownloadContext *parent,
1847 const char *serialization)
1849 struct GNUNET_FS_DownloadContext *dc;
1850 struct DownloadRequest *dr;
1857 uint32_t num_pending;
1862 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
1863 dc->parent = parent;
1865 dc->serialization = GNUNET_strdup (serialization);
1867 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
1868 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1869 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
1870 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
1872 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
1874 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
1876 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
1878 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
1880 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
1882 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
1884 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
1886 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
1888 GNUNET_BIO_read_int64 (rh, &dc->start_time.value)) ||
1890 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
1892 GNUNET_BIO_read_int32 (rh, &options)) ||
1894 GNUNET_BIO_read_int32 (rh, &status)) ||
1896 GNUNET_BIO_read_int32 (rh, &num_pending)) )
1898 /* FIXME: adjust start_time.value */
1899 dc->options = (enum GNUNET_FS_DownloadOptions) options;
1900 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
1901 dc->has_finished = (int) status;
1902 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
1903 if (GNUNET_FS_uri_test_loc (dc->uri))
1904 GNUNET_assert (GNUNET_OK ==
1905 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
1907 if ( (dc->length > dc->completed) &&
1908 (num_pending == 0) )
1910 while (0 < num_pending--)
1912 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1914 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
1916 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
1918 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
1920 dr->is_pending = GNUNET_YES;
1921 dr->next = dc->pending;
1925 GNUNET_snprintf (pbuf,
1931 dn = get_serialization_file_name (h, pbuf, "");
1934 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
1938 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1941 signal_download_resume (dc);
1945 GNUNET_free_non_null (uris);
1946 GNUNET_free_non_null (dr);
1947 free_download_context (dc);
1952 * Signal resuming of a search to our clients (for the
1953 * top level search and all sub-searches).
1955 * @param sc search being resumed
1958 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
1960 struct GNUNET_FS_SearchContext *scc;
1961 struct GNUNET_FS_ProgressInfo pi;
1963 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1964 pi.value.search.specifics.resume.message = sc->emsg;
1965 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
1966 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1968 scc = sc->child_head;
1971 signal_search_resume (scc);
1978 * Deserialize a search.
1980 * @param h overall context
1981 * @param rh file to deserialize from
1982 * @param parent parent search
1983 * @param serialization name under which the search was serialized
1985 static struct GNUNET_FS_SearchContext *
1986 deserialize_search (struct GNUNET_FS_Handle *h,
1987 struct GNUNET_BIO_ReadHandle *rh,
1988 struct GNUNET_FS_SearchContext *parent,
1989 const char *serialization)
1991 struct GNUNET_FS_SearchContext *sc;
1992 struct GNUNET_FS_SearchContext *scc;
1993 struct GNUNET_BIO_ReadHandle *rhc;
2004 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2005 sc->parent = parent;
2007 sc->serialization = GNUNET_strdup (serialization);
2009 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2010 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2011 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2012 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2014 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
2016 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2018 GNUNET_BIO_read_int32 (rh, &options)) ||
2020 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2022 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2024 /* FIXME: adjust start_time.value */
2025 sc->options = (enum GNUNET_FS_SearchOptions) options;
2026 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2027 GNUNET_snprintf (pbuf,
2033 dn = get_serialization_file_name (h, pbuf, "");
2036 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2039 if ( ('\0' == in_pause) &&
2041 GNUNET_FS_search_start_searching_ (sc)) )
2043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2044 _("Could not resume running search, will resume as paused search\n"));
2049 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
2051 if (child_ser == NULL)
2053 rhc = get_read_handle (h, "search-children", child_ser);
2056 scc = deserialize_search (h, rhc, sc, child_ser);
2058 GNUNET_CONTAINER_DLL_insert (sc->child_head,
2063 GNUNET_BIO_read_close (rhc, &emsg))
2065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2066 _("Failed to resume sub-search `%s': %s\n"),
2072 GNUNET_free (child_ser);
2075 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2078 signal_search_resume (sc);
2079 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2080 &signal_result_resume,
2085 GNUNET_free_non_null (emsg);
2086 free_search_context (sc);
2087 GNUNET_free_non_null (uris);
2093 * Function called with a filename of serialized search operation
2096 * @param cls the 'struct GNUNET_FS_Handle*'
2097 * @param filename complete filename (absolute path)
2098 * @return GNUNET_OK (continue to iterate)
2101 deserialize_search_file (void *cls,
2102 const char *filename)
2104 struct GNUNET_FS_Handle *h = cls;
2107 struct GNUNET_BIO_ReadHandle *rh;
2108 struct GNUNET_FS_SearchContext *sc;
2110 ser = get_serialization_short_name (filename);
2111 rh = GNUNET_BIO_read_open (filename);
2116 GNUNET_FS_remove_sync_file_ (h, "search", ser);
2121 sc = deserialize_search (h, rh, NULL, ser);
2124 GNUNET_BIO_read_close (rh, &emsg))
2126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2127 _("Failure while resuming search operation `%s': %s\n"),
2137 * Deserialize information about pending search operations.
2139 * @param h master context
2142 deserialize_search_master (struct GNUNET_FS_Handle *h)
2146 dn = get_serialization_file_name (h, "search", "");
2149 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
2155 * Function called with a filename of serialized download operation
2158 * @param cls the 'struct GNUNET_FS_Handle*'
2159 * @param filename complete filename (absolute path)
2160 * @return GNUNET_OK (continue to iterate)
2163 deserialize_download_file (void *cls,
2164 const char *filename)
2166 struct GNUNET_FS_Handle *h = cls;
2169 struct GNUNET_BIO_ReadHandle *rh;
2171 ser = get_serialization_short_name (filename);
2172 rh = GNUNET_BIO_read_open (filename);
2177 GNUNET_FS_remove_sync_file_ (h, "download", ser);
2182 deserialize_download (h, rh, NULL, ser);
2185 GNUNET_BIO_read_close (rh, &emsg))
2187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2188 _("Failure while resuming download operation `%s': %s\n"),
2198 * Deserialize information about pending download operations.
2200 * @param h master context
2203 deserialize_download_master (struct GNUNET_FS_Handle *h)
2207 dn = get_serialization_file_name (h, "download", "");
2210 GNUNET_DISK_directory_scan (dn, &deserialize_download_file, h);
2216 * Setup a connection to the file-sharing service.
2218 * @param sched scheduler to use
2219 * @param cfg configuration to use
2220 * @param client_name unique identifier for this client
2221 * @param upcb function to call to notify about FS actions
2222 * @param upcb_cls closure for upcb
2223 * @param flags specific attributes for fs-operations
2224 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2225 * @return NULL on error
2227 struct GNUNET_FS_Handle *
2228 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2229 const struct GNUNET_CONFIGURATION_Handle *cfg,
2230 const char *client_name,
2231 GNUNET_FS_ProgressCallback upcb,
2233 enum GNUNET_FS_Flags flags,
2236 struct GNUNET_FS_Handle *ret;
2237 struct GNUNET_CLIENT_Connection *client;
2238 enum GNUNET_FS_OPTIONS opt;
2241 client = GNUNET_CLIENT_connect (sched,
2246 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2249 ret->client_name = GNUNET_strdup (client_name);
2251 ret->upcb_cls = upcb_cls;
2252 ret->client = client;
2254 ret->max_parallel_downloads = 1;
2255 ret->max_parallel_requests = 1;
2256 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2257 va_start (ap, flags);
2258 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2262 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2263 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2265 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2266 ret->max_parallel_requests = va_arg (ap, unsigned int);
2270 GNUNET_free (ret->client_name);
2277 // FIXME: setup receive-loop with client (do we need one?)
2278 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2280 /* FIXME: could write one generic deserialization
2281 function instead of these four... */
2282 deserialize_publish (ret);
2283 deserialize_search_master (ret);
2284 deserialize_download_master (ret);
2285 deserialize_unindex (ret);
2292 * Close our connection with the file-sharing service.
2293 * The callback given to GNUNET_FS_start will no longer be
2294 * called after this function returns.
2296 * @param h handle that was returned from GNUNET_FS_start
2299 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2301 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
2303 // FIXME: generate SUSPEND events and clean up state!
2305 // FIXME: terminate receive-loop with client (do we need one?)
2306 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2307 GNUNET_SCHEDULER_cancel (h->sched,
2309 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
2310 GNUNET_free (h->client_name);