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 * Serialize an active or pending download request.
1250 * @param cls the 'struct GNUNET_BIO_WriteHandle*'
1251 * @param key unused, can be NULL
1252 * @param value the 'struct DownloadRequest'
1253 * @return GNUNET_YES on success, GNUNET_NO on error
1256 write_download_request (void *cls,
1257 const GNUNET_HashCode *key,
1260 struct GNUNET_BIO_WriteHandle *wh = cls;
1261 struct DownloadRequest *dr = value;
1264 GNUNET_BIO_write (wh, &dr->chk, sizeof (struct ContentHashKey))) ||
1266 GNUNET_BIO_write_int64 (wh, dr->offset)) ||
1268 GNUNET_BIO_write_int32 (wh, dr->depth)) )
1275 * Count active download requests.
1277 * @param cls the 'uint32_t*' counter
1278 * @param key unused, can be NULL
1279 * @param value the 'struct DownloadRequest'
1280 * @return GNUNET_YES (continue iteration)
1283 count_download_requests (void *cls,
1284 const GNUNET_HashCode *key,
1287 uint32_t *counter = cls;
1295 * Synchronize this download struct with its mirror
1296 * on disk. Note that all internal FS-operations that change
1297 * publishing structs should already call "sync" internally,
1298 * so this function is likely not useful for clients.
1300 * @param dc the struct to sync
1303 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
1305 struct GNUNET_BIO_WriteHandle *wh;
1306 struct DownloadRequest *dr;
1308 const char *category;
1311 uint32_t num_pending;
1313 if (dc->parent != NULL)
1315 if (dc->parent->serialization == NULL)
1317 GNUNET_snprintf (pbuf,
1322 dc->parent->serialization);
1327 category = "download";
1329 if (NULL == dc->serialization)
1330 dc->serialization = make_serialization_file_name (dc->h,
1332 if (NULL == dc->serialization)
1334 wh = get_write_handle (dc->h, category, dc->serialization);
1337 GNUNET_free (dc->serialization);
1338 dc->serialization = NULL;
1341 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
1342 (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)) );
1343 uris = GNUNET_FS_uri_to_string (dc->uri);
1345 if (dc->emsg != NULL)
1353 (void) GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1354 &count_download_requests,
1357 GNUNET_assert ( (dc->length == dc->completed) ||
1358 (dc->emsg != NULL) ||
1359 (num_pending > 0) );
1361 GNUNET_BIO_write_string (wh, uris)) ||
1363 GNUNET_BIO_write_meta_data (wh, dc->meta)) ||
1365 GNUNET_BIO_write_string (wh, dc->emsg)) ||
1367 GNUNET_BIO_write_string (wh, dc->filename)) ||
1369 GNUNET_BIO_write_string (wh, dc->temp_filename)) ||
1371 GNUNET_BIO_write_int64 (wh, dc->old_file_size)) ||
1373 GNUNET_BIO_write_int64 (wh, dc->offset)) ||
1375 GNUNET_BIO_write_int64 (wh, dc->length)) ||
1377 GNUNET_BIO_write_int64 (wh, dc->completed)) ||
1379 GNUNET_BIO_write_int64 (wh, dc->start_time.value)) ||
1381 GNUNET_BIO_write_int32 (wh, dc->anonymity)) ||
1383 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->options)) ||
1385 GNUNET_BIO_write_int32 (wh, (uint32_t) dc->has_finished)) ||
1387 GNUNET_BIO_write_int32 (wh, num_pending)) )
1393 write_download_request (wh, NULL, dr))
1397 if (GNUNET_SYSERR ==
1398 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
1399 &write_download_request,
1402 while (0 < num_pending--)
1404 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1406 dr->is_pending = GNUNET_YES;
1407 dr->next = dc->pending;
1411 GNUNET_free_non_null (uris);
1413 GNUNET_BIO_write_close (wh))
1416 (void) GNUNET_BIO_write_close (wh);
1417 GNUNET_free_non_null (uris);
1418 fn = get_serialization_file_name (dc->h, category, dc->serialization);
1419 if (0 != UNLINK (fn))
1420 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1422 GNUNET_free (dc->serialization);
1423 dc->serialization = NULL;
1428 * Synchronize this search result with its mirror
1429 * on disk. Note that all internal FS-operations that change
1430 * publishing structs should already call "sync" internally,
1431 * so this function is likely not useful for clients.
1433 * @param key key for the search result
1434 * @param sr the struct to sync
1437 GNUNET_FS_search_result_sync_ (const GNUNET_HashCode *key,
1438 struct SearchResult *sr)
1440 struct GNUNET_BIO_WriteHandle *wh;
1443 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_chk (sr->uri)) ||
1444 (GNUNET_YES == GNUNET_FS_uri_test_loc (sr->uri)) );
1446 if (NULL == sr->serialization)
1447 sr->serialization = make_serialization_file_name (sr->sc->h,
1449 if (NULL == sr->serialization)
1451 wh = get_write_handle (sr->sc->h, "search-results", sr->serialization);
1452 uris = GNUNET_FS_uri_to_string (sr->uri);
1454 GNUNET_BIO_write_string (wh, uris)) ||
1456 GNUNET_BIO_write_meta_data (wh, sr->meta)) ||
1458 GNUNET_BIO_write (wh, key, sizeof (GNUNET_HashCode))) ||
1460 GNUNET_BIO_write_int32 (wh, sr->mandatory_missing)) ||
1462 GNUNET_BIO_write_int32 (wh, sr->optional_support)) ||
1464 GNUNET_BIO_write_int32 (wh, sr->availability_success)) ||
1466 GNUNET_BIO_write_int32 (wh, sr->availability_trials)) )
1469 GNUNET_BIO_write_close (wh))
1474 GNUNET_free_non_null (uris);
1477 GNUNET_free_non_null (uris);
1479 (void) GNUNET_BIO_write_close (wh);
1480 GNUNET_FS_remove_sync_file_ (sr->sc->h, "search-results", sr->serialization);
1481 GNUNET_free (sr->serialization);
1482 sr->serialization = NULL;
1487 * Synchronize this search struct with its mirror
1488 * on disk. Note that all internal FS-operations that change
1489 * publishing structs should already call "sync" internally,
1490 * so this function is likely not useful for clients.
1492 * @param sc the struct to sync
1495 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1497 struct GNUNET_BIO_WriteHandle *wh;
1498 struct GNUNET_FS_SearchContext *scc;
1502 if (NULL == sc->serialization)
1503 sc->serialization = make_serialization_file_name (sc->h,
1505 if (NULL == sc->serialization)
1507 wh = get_write_handle (sc->h, "search", sc->serialization);
1508 GNUNET_assert ( (GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
1509 (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)) );
1510 uris = GNUNET_FS_uri_to_string (sc->uri);
1511 in_pause = (sc->task != GNUNET_SCHEDULER_NO_TASK) ? 'r' : '\0';
1513 GNUNET_BIO_write_string (wh, uris)) ||
1515 GNUNET_BIO_write_int64 (wh, sc->start_time.value)) ||
1517 GNUNET_BIO_write_string (wh, sc->emsg)) ||
1519 GNUNET_BIO_write_int32 (wh, (uint32_t) sc->options)) ||
1521 GNUNET_BIO_write (wh, &in_pause, sizeof (in_pause))) ||
1523 GNUNET_BIO_write_int32 (wh, sc->anonymity)) )
1527 scc = sc->child_head;
1530 if (scc->serialization == NULL)
1533 GNUNET_BIO_write_string (wh, scc->serialization))
1537 GNUNET_BIO_write_string (wh, NULL);
1539 GNUNET_BIO_write_close (wh))
1547 (void) GNUNET_BIO_write_close (wh);
1548 GNUNET_free_non_null (uris);
1549 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1550 GNUNET_free (sc->serialization);
1551 sc->serialization = NULL;
1556 * Deserialize information about pending publish operations.
1558 * @param h master context
1561 deserialize_publish (struct GNUNET_FS_Handle *h)
1565 dn = get_serialization_file_name (h, "publish", "");
1568 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1574 * Function called with a filename of serialized unindexing operation
1577 * @param cls the 'struct GNUNET_FS_Handle*'
1578 * @param filename complete filename (absolute path)
1579 * @return GNUNET_OK (continue to iterate)
1582 deserialize_unindex_file (void *cls,
1583 const char *filename)
1585 struct GNUNET_FS_Handle *h = cls;
1586 struct GNUNET_BIO_ReadHandle *rh;
1587 struct GNUNET_FS_UnindexContext *uc;
1588 struct GNUNET_FS_ProgressInfo pi;
1592 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1594 uc->serialization = get_serialization_short_name (filename);
1595 rh = GNUNET_BIO_read_open (filename);
1599 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1601 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1603 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1605 GNUNET_BIO_read_int32 (rh, &state)) )
1607 uc->state = (enum UnindexState) state;
1610 case UNINDEX_STATE_HASHING:
1612 case UNINDEX_STATE_FS_NOTIFY:
1614 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1617 case UNINDEX_STATE_DS_REMOVE:
1619 case UNINDEX_STATE_COMPLETE:
1621 case UNINDEX_STATE_ERROR:
1623 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1626 case UNINDEX_STATE_ABORTED:
1633 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1634 pi.value.unindex.specifics.resume.message = uc->emsg;
1635 GNUNET_FS_unindex_make_status_ (&pi,
1637 (uc->state == UNINDEX_STATE_COMPLETE)
1642 case UNINDEX_STATE_HASHING:
1643 GNUNET_CRYPTO_hash_file (uc->h->sched,
1644 GNUNET_SCHEDULER_PRIORITY_IDLE,
1647 &GNUNET_FS_unindex_process_hash_,
1650 case UNINDEX_STATE_FS_NOTIFY:
1651 uc->state = UNINDEX_STATE_HASHING;
1652 GNUNET_FS_unindex_process_hash_ (uc,
1655 case UNINDEX_STATE_DS_REMOVE:
1656 GNUNET_FS_unindex_do_remove_ (uc);
1658 case UNINDEX_STATE_COMPLETE:
1659 case UNINDEX_STATE_ERROR:
1660 /* no need to resume any operation, we were done */
1666 GNUNET_BIO_read_close (rh, &emsg))
1668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1669 _("Failure while resuming unindexing operation `%s': %s\n"),
1676 GNUNET_free_non_null (uc->filename);
1677 if ( (rh != NULL) &&
1679 GNUNET_BIO_read_close (rh, &emsg)) )
1681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1682 _("Failed to resume unindexing operation `%s': %s\n"),
1687 if (uc->serialization != NULL)
1688 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1689 GNUNET_free_non_null (uc->serialization);
1696 * Deserialize information about pending publish operations.
1698 * @param h master context
1701 deserialize_unindex (struct GNUNET_FS_Handle *h)
1705 dn = get_serialization_file_name (h, "unindex", "");
1708 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1714 * Function called with a filename of serialized search result
1717 * @param cls the 'struct GNUNET_FS_SearchContext*'
1718 * @param filename complete filename (absolute path)
1719 * @return GNUNET_OK (continue to iterate)
1722 deserialize_search_result (void *cls,
1723 const char *filename)
1725 struct GNUNET_FS_SearchContext *sc = cls;
1730 struct GNUNET_BIO_ReadHandle *rh;
1731 struct SearchResult *sr;
1732 GNUNET_HashCode key;
1734 ser = get_serialization_short_name (filename);
1735 rh = GNUNET_BIO_read_open (filename);
1740 GNUNET_snprintf (pbuf,
1746 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1753 sr = GNUNET_malloc (sizeof (struct SearchResult));
1754 sr->serialization = ser;
1756 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1757 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1758 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1759 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1761 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1763 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1765 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1767 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1769 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1771 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1774 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1777 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1780 GNUNET_free_non_null (emsg);
1781 GNUNET_free_non_null (uris);
1782 if (sr->uri != NULL)
1783 GNUNET_FS_uri_destroy (sr->uri);
1784 if (sr->meta != NULL)
1785 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1786 GNUNET_free (sr->serialization);
1793 * Iterator over search results signaling resume to the client for
1796 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1797 * @param key current key code
1798 * @param value value in the hash map, the 'struct SearchResult'
1799 * @return GNUNET_YES (we should continue to iterate)
1802 signal_result_resume (void *cls,
1803 const GNUNET_HashCode * key,
1806 struct GNUNET_FS_SearchContext *sc = cls;
1807 struct GNUNET_FS_ProgressInfo pi;
1808 struct SearchResult *sr = value;
1810 if (0 == sr->mandatory_missing)
1812 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1813 pi.value.search.specifics.resume_result.meta = sr->meta;
1814 pi.value.search.specifics.resume_result.uri = sr->uri;
1815 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1816 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1817 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1818 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1821 GNUNET_FS_search_start_probe_ (sr);
1827 * Iterator over search results freeing each.
1829 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1830 * @param key current key code
1831 * @param value value in the hash map, the 'struct SearchResult'
1832 * @return GNUNET_YES (we should continue to iterate)
1835 free_result (void *cls,
1836 const GNUNET_HashCode * key,
1839 struct SearchResult *sr = value;
1841 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1842 GNUNET_FS_uri_destroy (sr->uri);
1849 * Free memory allocated by the search context and its children
1851 * @param sc search context to free
1854 free_search_context (struct GNUNET_FS_SearchContext *sc)
1856 struct GNUNET_FS_SearchContext *scc;
1858 while (NULL != (scc = sc->child_head))
1860 GNUNET_CONTAINER_DLL_remove (sc->child_head,
1863 free_search_context (scc);
1865 GNUNET_free_non_null (sc->emsg);
1866 if (sc->serialization != NULL)
1867 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1868 /* FIXME: remove 'pbuf' directory with search results as well! */
1869 GNUNET_free_non_null (sc->serialization);
1870 if (sc->uri != NULL)
1871 GNUNET_FS_uri_destroy (sc->uri);
1872 if (sc->master_result_map != NULL)
1874 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1877 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1884 * Deserialize a download.
1886 * @param h overall context
1887 * @param rh file to deserialize from
1888 * @param parent parent download
1889 * @param serialization name under which the search was serialized
1892 deserialize_download (struct GNUNET_FS_Handle *h,
1893 struct GNUNET_BIO_ReadHandle *rh,
1894 struct GNUNET_FS_DownloadContext *parent,
1895 const char *serialization);
1899 * Function called with a filename of serialized sub-download
1902 * @param cls the 'struct GNUNET_FS_DownloadContext*' (parent)
1903 * @param filename complete filename (absolute path)
1904 * @return GNUNET_OK (continue to iterate)
1907 deserialize_subdownload (void *cls,
1908 const char *filename)
1910 struct GNUNET_FS_DownloadContext *parent = cls;
1913 struct GNUNET_BIO_ReadHandle *rh;
1915 ser = get_serialization_short_name (filename);
1916 rh = GNUNET_BIO_read_open (filename);
1917 deserialize_download (parent->h,
1922 GNUNET_BIO_read_close (rh, &emsg))
1924 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1925 _("Failed to resume sub-download `%s': %s\n"),
1936 * Send the 'resume' signal to the callback; also actually
1937 * resume the download (put it in the queue). Does this
1938 * recursively for the top-level download and all child
1941 * @param dc download to resume
1944 signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
1946 struct GNUNET_FS_DownloadContext *dcc;
1947 struct GNUNET_FS_ProgressInfo pi;
1949 pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
1950 pi.value.download.specifics.resume.meta = dc->meta;
1951 pi.value.download.specifics.resume.message = dc->emsg;
1952 GNUNET_FS_download_make_status_ (&pi,
1954 dcc = dc->child_head;
1957 signal_download_resume (dcc);
1960 if (dc->pending != NULL)
1961 GNUNET_FS_download_start_downloading_ (dc);
1966 * Free this download context and all of its descendants.
1967 * (only works during deserialization since not all possible
1968 * state it taken care of).
1970 * @param dc context to free
1973 free_download_context (struct GNUNET_FS_DownloadContext *dc)
1975 struct GNUNET_FS_DownloadContext *dcc;
1976 struct DownloadRequest *dr;
1977 if (dc->meta != NULL)
1978 GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1979 if (dc->uri != NULL)
1980 GNUNET_FS_uri_destroy (dc->uri);
1981 GNUNET_free_non_null (dc->temp_filename);
1982 GNUNET_free_non_null (dc->emsg);
1983 GNUNET_free_non_null (dc->filename);
1984 while (NULL != (dcc = dc->child_head))
1986 GNUNET_CONTAINER_DLL_remove (dc->child_head,
1989 free_download_context (dcc);
1991 while (NULL != (dr = dc->pending))
1993 dc->pending = dr->next;
2001 * Deserialize a download.
2003 * @param h overall context
2004 * @param rh file to deserialize from
2005 * @param parent parent download
2006 * @param serialization name under which the search was serialized
2009 deserialize_download (struct GNUNET_FS_Handle *h,
2010 struct GNUNET_BIO_ReadHandle *rh,
2011 struct GNUNET_FS_DownloadContext *parent,
2012 const char *serialization)
2014 struct GNUNET_FS_DownloadContext *dc;
2015 struct DownloadRequest *dr;
2022 uint32_t num_pending;
2027 dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2028 dc->parent = parent;
2030 dc->serialization = GNUNET_strdup (serialization);
2032 GNUNET_BIO_read_string (rh, "download-uri", &uris, 10*1024)) ||
2033 (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2034 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
2035 (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri)) ) ||
2037 GNUNET_BIO_read_meta_data (rh, "download-meta", &dc->meta)) ||
2039 GNUNET_BIO_read_string (rh, "download-emsg", &dc->emsg, 10*1024)) ||
2041 GNUNET_BIO_read_string (rh, "download-fn", &dc->filename, 10*1024)) ||
2043 GNUNET_BIO_read_string (rh, "download-tfn", &dc->temp_filename, 10*1024)) ||
2045 GNUNET_BIO_read_int64 (rh, &dc->old_file_size)) ||
2047 GNUNET_BIO_read_int64 (rh, &dc->offset)) ||
2049 GNUNET_BIO_read_int64 (rh, &dc->length)) ||
2051 GNUNET_BIO_read_int64 (rh, &dc->completed)) ||
2053 GNUNET_BIO_read_int64 (rh, &dc->start_time.value)) ||
2055 GNUNET_BIO_read_int32 (rh, &dc->anonymity)) ||
2057 GNUNET_BIO_read_int32 (rh, &options)) ||
2059 GNUNET_BIO_read_int32 (rh, &status)) ||
2061 GNUNET_BIO_read_int32 (rh, &num_pending)) )
2063 /* FIXME: adjust start_time.value */
2064 dc->options = (enum GNUNET_FS_DownloadOptions) options;
2065 dc->active = GNUNET_CONTAINER_multihashmap_create (16);
2066 dc->has_finished = (int) status;
2067 dc->treedepth = GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2068 if (GNUNET_FS_uri_test_loc (dc->uri))
2069 GNUNET_assert (GNUNET_OK ==
2070 GNUNET_FS_uri_loc_get_peer_identity (dc->uri,
2072 if ( (dc->length > dc->completed) &&
2073 (num_pending == 0) )
2075 while (0 < num_pending--)
2077 dr = GNUNET_malloc (sizeof (struct DownloadRequest));
2079 GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof (struct ContentHashKey))) ||
2081 GNUNET_BIO_read_int64 (rh, &dr->offset)) ||
2083 GNUNET_BIO_read_int32 (rh, &dr->depth)) )
2085 dr->is_pending = GNUNET_YES;
2086 dr->next = dc->pending;
2090 GNUNET_snprintf (pbuf,
2096 dn = get_serialization_file_name (h, pbuf, "");
2099 GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
2103 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2106 signal_download_resume (dc);
2110 GNUNET_free_non_null (uris);
2111 GNUNET_free_non_null (dr);
2112 free_download_context (dc);
2117 * Signal resuming of a search to our clients (for the
2118 * top level search and all sub-searches).
2120 * @param sc search being resumed
2123 signal_search_resume (struct GNUNET_FS_SearchContext *sc)
2125 struct GNUNET_FS_SearchContext *scc;
2126 struct GNUNET_FS_ProgressInfo pi;
2128 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
2129 pi.value.search.specifics.resume.message = sc->emsg;
2130 pi.value.search.specifics.resume.is_paused = (sc->client == NULL) ? GNUNET_YES : GNUNET_NO;
2131 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
2133 scc = sc->child_head;
2136 signal_search_resume (scc);
2143 * Deserialize a search.
2145 * @param h overall context
2146 * @param rh file to deserialize from
2147 * @param parent parent search
2148 * @param serialization name under which the search was serialized
2150 static struct GNUNET_FS_SearchContext *
2151 deserialize_search (struct GNUNET_FS_Handle *h,
2152 struct GNUNET_BIO_ReadHandle *rh,
2153 struct GNUNET_FS_SearchContext *parent,
2154 const char *serialization)
2156 struct GNUNET_FS_SearchContext *sc;
2157 struct GNUNET_FS_SearchContext *scc;
2158 struct GNUNET_BIO_ReadHandle *rhc;
2169 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
2170 sc->parent = parent;
2172 sc->serialization = GNUNET_strdup (serialization);
2174 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
2175 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
2176 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
2177 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
2179 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
2181 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
2183 GNUNET_BIO_read_int32 (rh, &options)) ||
2185 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
2187 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
2189 /* FIXME: adjust start_time.value */
2190 sc->options = (enum GNUNET_FS_SearchOptions) options;
2191 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
2192 GNUNET_snprintf (pbuf,
2198 dn = get_serialization_file_name (h, pbuf, "");
2201 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
2204 if ( ('\0' == in_pause) &&
2206 GNUNET_FS_search_start_searching_ (sc)) )
2208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2209 _("Could not resume running search, will resume as paused search\n"));
2214 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)))
2216 if (child_ser == NULL)
2218 rhc = get_read_handle (h, "search-children", child_ser);
2221 scc = deserialize_search (h, rhc, sc, child_ser);
2223 GNUNET_CONTAINER_DLL_insert (sc->child_head,
2228 GNUNET_BIO_read_close (rhc, &emsg))
2230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2231 _("Failed to resume sub-search `%s': %s\n"),
2237 GNUNET_free (child_ser);
2240 GNUNET_CONTAINER_DLL_insert (parent->child_head,
2243 signal_search_resume (sc);
2244 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
2245 &signal_result_resume,
2250 GNUNET_free_non_null (emsg);
2251 free_search_context (sc);
2252 GNUNET_free_non_null (uris);
2258 * Function called with a filename of serialized search operation
2261 * @param cls the 'struct GNUNET_FS_Handle*'
2262 * @param filename complete filename (absolute path)
2263 * @return GNUNET_OK (continue to iterate)
2266 deserialize_search_file (void *cls,
2267 const char *filename)
2269 struct GNUNET_FS_Handle *h = cls;
2272 struct GNUNET_BIO_ReadHandle *rh;
2273 struct GNUNET_FS_SearchContext *sc;
2275 ser = get_serialization_short_name (filename);
2276 rh = GNUNET_BIO_read_open (filename);
2281 GNUNET_FS_remove_sync_file_ (h, "search", ser);
2286 sc = deserialize_search (h, rh, NULL, ser);
2289 GNUNET_BIO_read_close (rh, &emsg))
2291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2292 _("Failure while resuming search operation `%s': %s\n"),
2302 * Deserialize information about pending search operations.
2304 * @param h master context
2307 deserialize_search_master (struct GNUNET_FS_Handle *h)
2311 dn = get_serialization_file_name (h, "search", "");
2314 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
2320 * Function called with a filename of serialized download operation
2323 * @param cls the 'struct GNUNET_FS_Handle*'
2324 * @param filename complete filename (absolute path)
2325 * @return GNUNET_OK (continue to iterate)
2328 deserialize_download_file (void *cls,
2329 const char *filename)
2331 struct GNUNET_FS_Handle *h = cls;
2334 struct GNUNET_BIO_ReadHandle *rh;
2336 ser = get_serialization_short_name (filename);
2337 rh = GNUNET_BIO_read_open (filename);
2342 GNUNET_FS_remove_sync_file_ (h, "download", ser);
2347 deserialize_download (h, rh, NULL, ser);
2350 GNUNET_BIO_read_close (rh, &emsg))
2352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2353 _("Failure while resuming download operation `%s': %s\n"),
2363 * Deserialize information about pending download operations.
2365 * @param h master context
2368 deserialize_download_master (struct GNUNET_FS_Handle *h)
2372 dn = get_serialization_file_name (h, "download", "");
2375 GNUNET_DISK_directory_scan (dn, &deserialize_download_file, h);
2381 * Setup a connection to the file-sharing service.
2383 * @param sched scheduler to use
2384 * @param cfg configuration to use
2385 * @param client_name unique identifier for this client
2386 * @param upcb function to call to notify about FS actions
2387 * @param upcb_cls closure for upcb
2388 * @param flags specific attributes for fs-operations
2389 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
2390 * @return NULL on error
2392 struct GNUNET_FS_Handle *
2393 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
2394 const struct GNUNET_CONFIGURATION_Handle *cfg,
2395 const char *client_name,
2396 GNUNET_FS_ProgressCallback upcb,
2398 enum GNUNET_FS_Flags flags,
2401 struct GNUNET_FS_Handle *ret;
2402 struct GNUNET_CLIENT_Connection *client;
2403 enum GNUNET_FS_OPTIONS opt;
2406 client = GNUNET_CLIENT_connect (sched,
2411 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
2414 ret->client_name = GNUNET_strdup (client_name);
2416 ret->upcb_cls = upcb_cls;
2417 ret->client = client;
2419 ret->max_parallel_downloads = 1;
2420 ret->max_parallel_requests = 1;
2421 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
2422 va_start (ap, flags);
2423 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
2427 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
2428 ret->max_parallel_downloads = va_arg (ap, unsigned int);
2430 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
2431 ret->max_parallel_requests = va_arg (ap, unsigned int);
2435 GNUNET_free (ret->client_name);
2442 // FIXME: setup receive-loop with client (do we need one?)
2443 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
2445 /* FIXME: could write one generic deserialization
2446 function instead of these four... */
2447 deserialize_publish (ret);
2448 deserialize_search_master (ret);
2449 deserialize_download_master (ret);
2450 deserialize_unindex (ret);
2457 * Close our connection with the file-sharing service.
2458 * The callback given to GNUNET_FS_start will no longer be
2459 * called after this function returns.
2461 * @param h handle that was returned from GNUNET_FS_start
2464 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
2466 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
2468 // FIXME: generate SUSPEND events and clean up state!
2470 // FIXME: terminate receive-loop with client (do we need one?)
2471 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
2472 GNUNET_SCHEDULER_cancel (h->sched,
2474 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
2475 GNUNET_free (h->client_name);