2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief main FS functions (master initialization, serialization, deserialization, shared code)
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
34 * Start the given job (send signal, remove from pending queue, update
35 * counters and state).
37 * @param qe job to start
40 start_job (struct GNUNET_FS_QueueEntry *qe)
42 qe->client = GNUNET_CLIENT_connect (qe->h->sched, "fs", qe->h->cfg);
43 if (qe->client == NULL)
48 qe->start (qe->cls, qe->client);
50 qe->h->active_blocks += qe->blocks;
51 qe->start_time = GNUNET_TIME_absolute_get ();
52 GNUNET_CONTAINER_DLL_remove (qe->h->pending_head,
55 GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
63 * Stop the given job (send signal, remove from active queue, update
64 * counters and state).
66 * @param qe job to stop
69 stop_job (struct GNUNET_FS_QueueEntry *qe)
73 qe->h->active_downloads--;
74 qe->h->active_blocks -= qe->blocks;
75 qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
76 GNUNET_TIME_absolute_get_duration (qe->start_time));
77 GNUNET_CONTAINER_DLL_remove (qe->h->running_head,
80 GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
88 * Process the jobs in the job queue, possibly starting some
89 * and stopping others.
91 * @param cls the 'struct GNUNET_FS_Handle'
92 * @param tc scheduler context
95 process_job_queue (void *cls,
96 const struct GNUNET_SCHEDULER_TaskContext *tc)
98 struct GNUNET_FS_Handle *h = cls;
99 struct GNUNET_FS_QueueEntry *qe;
100 struct GNUNET_FS_QueueEntry *next;
101 struct GNUNET_TIME_Relative run_time;
102 struct GNUNET_TIME_Relative restart_at;
103 struct GNUNET_TIME_Relative rst;
104 struct GNUNET_TIME_Absolute end_time;
106 h->queue_job = GNUNET_SCHEDULER_NO_TASK;
107 next = h->pending_head;
108 while (NULL != (qe = next))
111 if (h->running_head == NULL)
116 if ( (qe->blocks + h->active_blocks <= h->max_parallel_requests) &&
117 (h->active_downloads + 1 <= h->max_parallel_downloads) )
123 if (h->pending_head == NULL)
124 return; /* no need to stop anything */
125 restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
126 next = h->running_head;
127 while (NULL != (qe = next))
130 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
131 qe->blocks * qe->start_times);
132 end_time = GNUNET_TIME_absolute_add (qe->start_time,
134 rst = GNUNET_TIME_absolute_get_remaining (end_time);
135 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
140 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
148 * Add a job to the queue.
150 * @param h handle to the overall FS state
151 * @param start function to call to begin the job
152 * @param stop function to call to pause the job, or on dequeue (if the job was running)
153 * @param cls closure for start and stop
154 * @param blocks number of blocks this jobs uses
155 * @return queue handle
157 struct GNUNET_FS_QueueEntry *
158 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
159 GNUNET_FS_QueueStart start,
160 GNUNET_FS_QueueStop stop,
164 struct GNUNET_FS_QueueEntry *qe;
166 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
171 qe->queue_time = GNUNET_TIME_absolute_get ();
173 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
177 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
178 GNUNET_SCHEDULER_cancel (h->sched,
181 = GNUNET_SCHEDULER_add_now (h->sched,
189 * Dequeue a job from the queue.
190 * @param qh handle for the job
193 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
195 struct GNUNET_FS_Handle *h;
198 if (qh->client != NULL)
200 GNUNET_CONTAINER_DLL_remove (h->pending_head,
204 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
205 GNUNET_SCHEDULER_cancel (h->sched,
208 = GNUNET_SCHEDULER_add_now (h->sched,
217 * Closure for "data_reader_file".
222 * Name of the file to read.
227 * File descriptor, NULL if it has not yet been opened.
229 struct GNUNET_DISK_FileHandle *fd;
234 * Function that provides data by reading from a file.
236 * @param cls closure (points to the file information)
237 * @param offset offset to read from; it is possible
238 * that the caller might need to go backwards
240 * @param max maximum number of bytes that should be
241 * copied to buf; readers are not allowed
242 * to provide less data unless there is an error;
243 * a value of "0" will be used at the end to allow
244 * the reader to clean up its internal state
245 * @param buf where the reader should write the data
246 * @param emsg location for the reader to store an error message
247 * @return number of bytes written, usually "max", 0 on error
250 GNUNET_FS_data_reader_file_(void *cls,
256 struct FileInfo *fi = cls;
262 GNUNET_DISK_file_close (fi->fd);
263 GNUNET_free (fi->filename);
269 fi->fd = GNUNET_DISK_file_open (fi->filename,
270 GNUNET_DISK_OPEN_READ,
271 GNUNET_DISK_PERM_NONE);
274 GNUNET_asprintf (emsg,
275 _("Could not open file `%s': %s"),
281 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
282 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
285 GNUNET_asprintf (emsg,
286 _("Could not read file `%s': %s"),
293 GNUNET_asprintf (emsg,
294 _("Short read reading from file `%s'!"),
303 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
305 * @param filename file to read
306 * @return closure to use, NULL on error
309 GNUNET_FS_make_file_reader_context_ (const char *filename)
313 fi = GNUNET_malloc (sizeof(struct FileInfo));
314 fi->filename = GNUNET_STRINGS_filename_expand (filename);
315 if (fi->filename == NULL)
325 * Function that provides data by copying from a buffer.
327 * @param cls closure (points to the buffer)
328 * @param offset offset to read from; it is possible
329 * that the caller might need to go backwards
331 * @param max maximum number of bytes that should be
332 * copied to buf; readers are not allowed
333 * to provide less data unless there is an error;
334 * a value of "0" will be used at the end to allow
335 * the reader to clean up its internal state
336 * @param buf where the reader should write the data
337 * @param emsg location for the reader to store an error message
338 * @return number of bytes written, usually "max", 0 on error
341 GNUNET_FS_data_reader_copy_ (void *cls,
351 GNUNET_free_non_null (data);
354 memcpy (buf, &data[offset], max);
361 * Return the full filename where we would store state information
362 * (for serialization/deserialization).
364 * @param h master context
365 * @param ext component of the path
366 * @param ent entity identifier (or emtpy string for the directory)
367 * @return NULL on error
370 get_serialization_file_name (struct GNUNET_FS_Handle *h,
377 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
382 GNUNET_asprintf (&ret,
390 GNUNET_free (basename);
396 * Return a read handle for deserialization.
398 * @param h master context
399 * @param ext component of the path
400 * @param ent entity identifier (or emtpy string for the directory)
401 * @return NULL on error
403 static struct GNUNET_BIO_ReadHandle *
404 get_read_handle (struct GNUNET_FS_Handle *h,
409 struct GNUNET_BIO_ReadHandle *ret;
411 fn = get_serialization_file_name (h, ext, ent);
414 ret = GNUNET_BIO_read_open (fn);
421 * Return a write handle for serialization.
423 * @param h master context
424 * @param ext component of the path
425 * @param ent entity identifier (or emtpy string for the directory)
426 * @return NULL on error
428 static struct GNUNET_BIO_WriteHandle *
429 get_write_handle (struct GNUNET_FS_Handle *h,
434 struct GNUNET_BIO_WriteHandle *ret;
436 fn = get_serialization_file_name (h, ext, ent);
439 ret = GNUNET_BIO_write_open (fn);
446 * Remove serialization/deserialization file from disk.
448 * @param h master context
449 * @param ext component of the path
450 * @param ent entity identifier
453 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
459 if ( (NULL == ent) ||
460 (0 == strlen (ent)) )
465 filename = get_serialization_file_name (h, ext, ent);
466 if (0 != UNLINK (filename))
467 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
470 GNUNET_free (filename);
476 * Using the given serialization filename, try to deserialize
477 * the file-information tree associated with it.
479 * @param h master context
480 * @param filename name of the file (without directory) with
482 * @return NULL on error
484 static struct GNUNET_FS_FileInformation *
485 deserialize_file_information (struct GNUNET_FS_Handle *h,
486 const char *filename);
490 * Using the given serialization filename, try to deserialize
491 * the file-information tree associated with it.
493 * @param h master context
494 * @param fn name of the file (without directory) with
496 * @param rh handle for reading
497 * @return NULL on error
499 static struct GNUNET_FS_FileInformation *
500 deserialize_fi_node (struct GNUNET_FS_Handle *h,
502 struct GNUNET_BIO_ReadHandle *rh)
504 struct GNUNET_FS_FileInformation *ret;
505 struct GNUNET_FS_FileInformation *nxt;
513 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
518 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
523 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
525 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
528 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
530 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
532 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
535 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
537 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
539 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
541 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
543 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
545 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
547 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
549 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
553 case 0: /* file-insert */
555 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
557 ret->is_directory = GNUNET_NO;
558 ret->data.file.do_index = GNUNET_NO;
559 ret->data.file.have_hash = GNUNET_NO;
560 ret->data.file.index_start_confirmed = GNUNET_NO;
561 if (GNUNET_NO == ret->is_published)
563 if (NULL == ret->filename)
565 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
566 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
567 if (ret->data.file.reader_cls == NULL)
570 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
575 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
576 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
580 case 1: /* file-index, no hash */
581 if (NULL == ret->filename)
584 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
586 ret->is_directory = GNUNET_NO;
587 ret->data.file.do_index = GNUNET_YES;
588 ret->data.file.have_hash = GNUNET_NO;
589 ret->data.file.index_start_confirmed = GNUNET_NO;
590 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
591 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
593 case 2: /* file-index-with-hash */
594 if (NULL == ret->filename)
597 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
599 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
601 ret->is_directory = GNUNET_NO;
602 ret->data.file.do_index = GNUNET_YES;
603 ret->data.file.have_hash = GNUNET_YES;
604 ret->data.file.index_start_confirmed = GNUNET_NO;
605 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
606 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
608 case 3: /* file-index-with-hash-confirmed */
609 if (NULL == ret->filename)
612 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
614 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
617 ret->is_directory = GNUNET_NO;
618 ret->data.file.do_index = GNUNET_YES;
619 ret->data.file.have_hash = GNUNET_YES;
620 ret->data.file.index_start_confirmed = GNUNET_YES;
621 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
622 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
624 case 4: /* directory */
626 GNUNET_BIO_read_int32 (rh, &dsize)) ||
627 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
629 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
631 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
633 ret->data.dir.dir_size = (uint32_t) dsize;
634 ret->is_directory = GNUNET_YES;
635 if (filename != NULL)
637 ret->data.dir.entries = deserialize_file_information (h, filename);
638 GNUNET_free (filename);
640 nxt = ret->data.dir.entries;
652 /* FIXME: adjust ret->start_time! */
653 ret->serialization = GNUNET_strdup (fn);
655 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
657 if (filename != NULL)
659 ret->next = deserialize_file_information (h, filename);
660 GNUNET_free (filename);
663 GNUNET_free_non_null (ksks);
664 GNUNET_free_non_null (chks);
667 GNUNET_free_non_null (ksks);
668 GNUNET_free_non_null (chks);
669 GNUNET_free_non_null (filename);
670 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
676 * Using the given serialization filename, try to deserialize
677 * the file-information tree associated with it.
679 * @param h master context
680 * @param filename name of the file (without directory) with
682 * @return NULL on error
684 static struct GNUNET_FS_FileInformation *
685 deserialize_file_information (struct GNUNET_FS_Handle *h,
686 const char *filename)
688 struct GNUNET_FS_FileInformation *ret;
689 struct GNUNET_BIO_ReadHandle *rh;
692 rh = get_read_handle (h, "publish-fi", filename);
695 ret = deserialize_fi_node (h, filename, rh);
697 GNUNET_BIO_read_close (rh, &emsg))
699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 _("Failed to resume publishing information `%s': %s\n"),
710 * Given a serialization name (full absolute path), return the
711 * basename of the file (without the path), which must only
712 * consist of the 6 random characters.
714 * @param fullname name to extract the basename from
715 * @return copy of the basename, NULL on error
718 get_serialization_short_name (const char *fullname)
725 /* FIXME: we could do this faster since we know
726 the length of 'end'... */
729 if (DIR_SEPARATOR == *nxt)
733 if ( (end == NULL) ||
734 (strlen (end) == 0) )
739 GNUNET_break (6 == strlen (end));
740 return GNUNET_strdup (end);
745 * Create a new random name for serialization. Also checks if persistence
746 * is enabled and returns NULL if not.
748 * @param h master context
749 * @param ext component of the path
750 * @return NULL on errror
753 make_serialization_file_name (struct GNUNET_FS_Handle *h,
760 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
761 return NULL; /* persistence not requested */
762 dn = get_serialization_file_name (h, ext, "");
763 fn = GNUNET_DISK_mktemp (dn);
766 return NULL; /* epic fail */
767 ret = get_serialization_short_name (fn);
774 * Copy all of the data from the reader to the write handle.
776 * @param wh write handle
777 * @param fi file with reader
778 * @return GNUNET_OK on success
781 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
782 struct GNUNET_FS_FileInformation * fi)
791 while (off < fi->data.file.file_size)
793 ret = fi->data.file.reader (fi->data.file.reader_cls,
800 return GNUNET_SYSERR;
803 GNUNET_BIO_write (wh, buf, ret))
804 return GNUNET_SYSERR;
812 * Create a temporary file on disk to store the current
815 * @param fi file information to sync with disk
818 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
821 struct GNUNET_BIO_WriteHandle *wh;
826 if (NULL == fi->serialization)
827 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
828 if (NULL == fi->serialization)
830 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
833 GNUNET_free (fi->serialization);
834 fi->serialization = NULL;
837 if (GNUNET_YES == fi->is_directory)
839 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
841 else if (GNUNET_YES == fi->data.file.have_hash)
843 else if (GNUNET_YES == fi->data.file.do_index)
847 if (fi->keywords != NULL)
848 ksks = GNUNET_FS_uri_to_string (fi->keywords);
851 if (fi->chk_uri != NULL)
852 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
856 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
858 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
860 GNUNET_BIO_write_string (wh, ksks)) ||
862 GNUNET_BIO_write_string (wh, chks)) ||
864 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
866 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
868 GNUNET_BIO_write_string (wh, fi->emsg)) ||
870 GNUNET_BIO_write_string (wh, fi->filename)) ||
872 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
874 GNUNET_BIO_write_int32 (wh, fi->priority)) )
876 GNUNET_free_non_null (chks);
878 GNUNET_free_non_null (ksks);
883 case 0: /* file-insert */
885 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
887 if ( (GNUNET_NO == fi->is_published) &&
888 (NULL == fi->filename) )
890 copy_from_reader (wh, fi))
893 case 1: /* file-index, no hash */
894 if (NULL == fi->filename)
897 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
900 case 2: /* file-index-with-hash */
901 case 3: /* file-index-with-hash-confirmed */
902 if (NULL == fi->filename)
905 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
907 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
910 case 4: /* directory */
912 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
914 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
916 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
924 GNUNET_BIO_write_string (wh, fi->next->serialization))
927 GNUNET_BIO_write_close (wh))
930 (void) GNUNET_BIO_write_close (wh);
931 GNUNET_free_non_null (chks);
932 GNUNET_free_non_null (ksks);
933 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
934 if (0 != UNLINK (fn))
935 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
937 GNUNET_free (fi->serialization);
938 fi->serialization = NULL;
944 * Find the entry in the file information struct where the
945 * serialization filename matches the given name.
947 * @param pos file information to search
948 * @param srch filename to search for
949 * @return NULL if srch was not found in this subtree
951 static struct GNUNET_FS_FileInformation *
952 find_file_position (struct GNUNET_FS_FileInformation *pos,
955 struct GNUNET_FS_FileInformation *r;
959 if (0 == strcmp (srch,
962 if (pos->is_directory)
964 r = find_file_position (pos->data.dir.entries,
976 * Signal the FS's progress function that we are resuming
979 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
980 * @param fi the entry in the publish-structure
981 * @param length length of the file or directory
982 * @param meta metadata for the file or directory (can be modified)
983 * @param uri pointer to the keywords that will be used for this entry (can be modified)
984 * @param anonymity pointer to selected anonymity level (can be modified)
985 * @param priority pointer to selected priority (can be modified)
986 * @param expirationTime pointer to selected expiration time (can be modified)
987 * @param client_info pointer to client context set upon creation (can be modified)
988 * @return GNUNET_OK to continue (always)
991 fip_signal_resume(void *cls,
992 struct GNUNET_FS_FileInformation *fi,
994 struct GNUNET_CONTAINER_MetaData *meta,
995 struct GNUNET_FS_Uri **uri,
998 struct GNUNET_TIME_Absolute *expirationTime,
1001 struct GNUNET_FS_PublishContext *sc = cls;
1002 struct GNUNET_FS_ProgressInfo pi;
1004 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1005 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1006 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1007 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1013 * Function called with a filename of serialized publishing operation
1016 * @param cls the 'struct GNUNET_FS_Handle*'
1017 * @param filename complete filename (absolute path)
1018 * @return GNUNET_OK (continue to iterate)
1021 deserialize_publish_file (void *cls,
1022 const char *filename)
1024 struct GNUNET_FS_Handle *h = cls;
1025 struct GNUNET_BIO_ReadHandle *rh;
1026 struct GNUNET_FS_PublishContext *pc;
1034 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1036 pc->serialization = get_serialization_short_name (filename);
1040 rh = GNUNET_BIO_read_open (filename);
1044 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1046 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1048 GNUNET_BIO_read_int32 (rh, &options)) ||
1050 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1052 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1054 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1056 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1058 pc->options = options;
1059 pc->all_done = all_done;
1060 pc->fi = deserialize_file_information (h, fi_root);
1065 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1066 if (pc->namespace == NULL)
1068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1069 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1076 pc->fi_pos = find_file_position (pc->fi,
1078 GNUNET_free (fi_pos);
1080 if (pc->fi_pos == NULL)
1082 /* failed to find position for resuming, outch! Will start from root! */
1084 if (pc->all_done != GNUNET_YES)
1085 pc->fi_pos = pc->fi;
1088 /* generate RESUME event(s) */
1089 GNUNET_FS_file_information_inspect (pc->fi,
1093 /* re-start publishing (if needed)... */
1094 if (pc->all_done != GNUNET_YES)
1096 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1097 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1098 &GNUNET_FS_publish_main_,
1101 GNUNET_BIO_read_close (rh, &emsg))
1103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1104 _("Failure while resuming publishing operation `%s': %s\n"),
1109 GNUNET_free_non_null (ns);
1112 GNUNET_free_non_null (pc->nid);
1113 GNUNET_free_non_null (pc->nuid);
1114 GNUNET_free_non_null (fi_root);
1115 GNUNET_free_non_null (fi_pos);
1116 GNUNET_free_non_null (ns);
1117 if ( (rh != NULL) &&
1119 GNUNET_BIO_read_close (rh, &emsg)) )
1121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1122 _("Failed to resume publishing operation `%s': %s\n"),
1128 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1129 if (pc->serialization != NULL)
1130 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1131 GNUNET_free_non_null (pc->serialization);
1138 * Synchronize this publishing struct with its mirror
1139 * on disk. Note that all internal FS-operations that change
1140 * publishing structs should already call "sync" internally,
1141 * so this function is likely not useful for clients.
1143 * @param pc the struct to sync
1146 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1148 struct GNUNET_BIO_WriteHandle *wh;
1150 if (NULL == pc->serialization)
1151 pc->serialization = make_serialization_file_name (pc->h,
1153 if (NULL == pc->serialization)
1157 if (NULL == pc->fi->serialization)
1162 wh = get_write_handle (pc->h, "publish", pc->serialization);
1164 GNUNET_BIO_write_string (wh, pc->nid)) ||
1166 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1168 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1170 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1172 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1174 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1176 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1178 (void) GNUNET_BIO_write_close (wh);
1179 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1180 GNUNET_free (pc->serialization);
1181 pc->serialization = NULL;
1185 GNUNET_BIO_write_close (wh))
1187 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1188 GNUNET_free (pc->serialization);
1189 pc->serialization = NULL;
1196 * Synchronize this unindex struct with its mirror
1197 * on disk. Note that all internal FS-operations that change
1198 * publishing structs should already call "sync" internally,
1199 * so this function is likely not useful for clients.
1201 * @param uc the struct to sync
1204 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1206 struct GNUNET_BIO_WriteHandle *wh;
1208 if (UNINDEX_STATE_ABORTED == uc->state)
1210 if (NULL == uc->serialization)
1211 uc->serialization = make_serialization_file_name (uc->h,
1213 if (NULL == uc->serialization)
1215 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1217 GNUNET_BIO_write_string (wh, uc->filename)) ||
1219 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1221 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1223 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1224 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1226 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1227 ( (uc->state == UNINDEX_STATE_ERROR) &&
1229 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1231 (void) GNUNET_BIO_write_close (wh);
1232 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1233 GNUNET_free (uc->serialization);
1234 uc->serialization = NULL;
1238 GNUNET_BIO_write_close (wh))
1240 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1241 GNUNET_free (uc->serialization);
1242 uc->serialization = NULL;
1249 * Synchronize this search result with its mirror
1250 * on disk. Note that all internal FS-operations that change
1251 * publishing structs should already call "sync" internally,
1252 * so this function is likely not useful for clients.
1254 * @param sc the struct to sync
1257 GNUNET_FS_search_result_sync_ (struct SearchResult *sr)
1264 * Synchronize this search struct with its mirror
1265 * on disk. Note that all internal FS-operations that change
1266 * publishing structs should already call "sync" internally,
1267 * so this function is likely not useful for clients.
1269 * @param sc the struct to sync
1272 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1274 struct GNUNET_BIO_WriteHandle *wh;
1276 if (NULL == sc->serialization)
1277 sc->serialization = make_serialization_file_name (sc->h,
1279 if (NULL == sc->serialization)
1281 wh = get_write_handle (sc->h, "search", sc->serialization);
1284 GNUNET_BIO_write_string (wh, pc->nid)) ||
1286 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1288 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1290 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1292 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1294 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1296 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1298 (void) GNUNET_BIO_write_close (wh);
1299 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1300 GNUNET_free (pc->serialization);
1301 pc->serialization = NULL;
1305 /* FIXME: do search-specific serialization here! */
1307 GNUNET_BIO_write_close (wh))
1309 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1310 GNUNET_free (sc->serialization);
1311 sc->serialization = NULL;
1318 * Deserialize information about pending publish operations.
1320 * @param h master context
1323 deserialize_publish (struct GNUNET_FS_Handle *h)
1327 dn = get_serialization_file_name (h, "publish", "");
1330 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1338 * Function called with a filename of serialized unindexing operation
1341 * @param cls the 'struct GNUNET_FS_Handle*'
1342 * @param filename complete filename (absolute path)
1343 * @return GNUNET_OK (continue to iterate)
1346 deserialize_unindex_file (void *cls,
1347 const char *filename)
1349 struct GNUNET_FS_Handle *h = cls;
1350 struct GNUNET_BIO_ReadHandle *rh;
1351 struct GNUNET_FS_UnindexContext *uc;
1352 struct GNUNET_FS_ProgressInfo pi;
1356 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1358 uc->serialization = get_serialization_short_name (filename);
1359 rh = GNUNET_BIO_read_open (filename);
1363 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1365 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1367 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1369 GNUNET_BIO_read_int32 (rh, &state)) )
1371 uc->state = (enum UnindexState) state;
1374 case UNINDEX_STATE_HASHING:
1376 case UNINDEX_STATE_FS_NOTIFY:
1378 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1381 case UNINDEX_STATE_DS_REMOVE:
1383 case UNINDEX_STATE_COMPLETE:
1385 case UNINDEX_STATE_ERROR:
1387 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1390 case UNINDEX_STATE_ABORTED:
1397 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1398 pi.value.unindex.specifics.resume.message = uc->emsg;
1399 GNUNET_FS_unindex_make_status_ (&pi,
1401 (uc->state == UNINDEX_STATE_COMPLETE)
1406 case UNINDEX_STATE_HASHING:
1407 GNUNET_CRYPTO_hash_file (uc->h->sched,
1408 GNUNET_SCHEDULER_PRIORITY_IDLE,
1411 &GNUNET_FS_unindex_process_hash_,
1414 case UNINDEX_STATE_FS_NOTIFY:
1415 uc->state = UNINDEX_STATE_HASHING;
1416 GNUNET_FS_unindex_process_hash_ (uc,
1419 case UNINDEX_STATE_DS_REMOVE:
1420 GNUNET_FS_unindex_do_remove_ (uc);
1422 case UNINDEX_STATE_COMPLETE:
1423 case UNINDEX_STATE_ERROR:
1424 /* no need to resume any operation, we were done */
1430 GNUNET_BIO_read_close (rh, &emsg))
1432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1433 _("Failure while resuming unindexing operation `%s': %s\n"),
1440 GNUNET_free_non_null (uc->filename);
1441 if ( (rh != NULL) &&
1443 GNUNET_BIO_read_close (rh, &emsg)) )
1445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1446 _("Failed to resume unindexing operation `%s': %s\n"),
1451 if (uc->serialization != NULL)
1452 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1453 GNUNET_free_non_null (uc->serialization);
1460 * Deserialize information about pending publish operations.
1462 * @param h master context
1465 deserialize_unindex (struct GNUNET_FS_Handle *h)
1469 dn = get_serialization_file_name (h, "unindex", "");
1472 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1478 * Function called with a filename of serialized search result
1481 * @param cls the 'struct GNUNET_FS_SearchContext*'
1482 * @param filename complete filename (absolute path)
1483 * @return GNUNET_OK (continue to iterate)
1486 deserialize_search_result (void *cls,
1487 const char *filename)
1489 struct GNUNET_FS_SearchContext *sc = cls;
1494 struct GNUNET_BIO_ReadHandle *rh;
1495 struct SearchResult *sr;
1496 GNUNET_HashCode key;
1498 ser = get_serialization_short_name (filename);
1499 rh = GNUNET_BIO_read_open (filename);
1504 GNUNET_snprintf (pbuf,
1510 GNUNET_FS_remove_sync_file_ (sc->h, pbuf, ser);
1517 sr = GNUNET_malloc (sizeof (struct SearchResult));
1518 sr->serialization = ser;
1520 GNUNET_BIO_read_string (rh, "result-uri", &uris, 10*1024)) ||
1521 (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1522 ( (GNUNET_YES != GNUNET_FS_uri_test_chk (sr->uri)) &&
1523 (GNUNET_YES != GNUNET_FS_uri_test_loc (sr->uri)) ) ||
1525 GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
1527 GNUNET_BIO_read (rh, "result-key", &key, sizeof (key))) ||
1529 GNUNET_BIO_read_int32 (rh, &sr->mandatory_missing)) ||
1531 GNUNET_BIO_read_int32 (rh, &sr->optional_support)) ||
1533 GNUNET_BIO_read_int32 (rh, &sr->availability_success)) ||
1535 GNUNET_BIO_read_int32 (rh, &sr->availability_trials)) )
1538 GNUNET_CONTAINER_multihashmap_put (sc->master_result_map,
1541 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1544 GNUNET_free_non_null (emsg);
1545 GNUNET_free_non_null (uris);
1546 if (sr->uri != NULL)
1547 GNUNET_FS_uri_destroy (sr->uri);
1548 if (sr->meta != NULL)
1549 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1550 GNUNET_free (sr->serialization);
1557 * Iterator over search results signaling resume to the client for
1560 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1561 * @param key current key code
1562 * @param value value in the hash map, the 'struct SearchResult'
1563 * @return GNUNET_YES (we should continue to iterate)
1566 signal_result_resume (void *cls,
1567 const GNUNET_HashCode * key,
1570 struct GNUNET_FS_SearchContext *sc = cls;
1571 struct GNUNET_FS_ProgressInfo pi;
1572 struct SearchResult *sr = value;
1574 if (0 == sr->mandatory_missing)
1576 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
1577 pi.value.search.specifics.resume_result.meta = sr->meta;
1578 pi.value.search.specifics.resume_result.uri = sr->uri;
1579 pi.value.search.specifics.resume_result.availability_rank = 2*sr->availability_success - sr->availability_trials;
1580 pi.value.search.specifics.resume_result.availability_certainty = sr->availability_trials;
1581 pi.value.search.specifics.resume_result.applicability_rank = sr->optional_support;
1582 sr->client_info = GNUNET_FS_search_make_status_ (&pi,
1585 GNUNET_FS_search_start_probe_ (sr);
1591 * Iterator over search results freeing each.
1593 * @param cls closure, the 'struct GNUNET_FS_SearchContext'
1594 * @param key current key code
1595 * @param value value in the hash map, the 'struct SearchResult'
1596 * @return GNUNET_YES (we should continue to iterate)
1599 free_result (void *cls,
1600 const GNUNET_HashCode * key,
1603 struct SearchResult *sr = value;
1605 GNUNET_CONTAINER_meta_data_destroy (sr->meta);
1606 GNUNET_FS_uri_destroy (sr->uri);
1613 * Deserialize a search.
1615 * @param h overall context
1616 * @param rh file to deserialize from
1617 * @param parent parent search
1618 * @param serialization name under which the search was serialized
1620 static struct GNUNET_FS_SearchContext *
1621 deserialize_search (struct GNUNET_FS_Handle *h,
1622 struct GNUNET_BIO_ReadHandle *rh,
1623 struct GNUNET_FS_SearchContext *parent,
1624 const char *serialization)
1626 struct GNUNET_FS_SearchContext *sc;
1628 struct GNUNET_FS_ProgressInfo pi;
1639 sc = GNUNET_malloc (sizeof (struct GNUNET_FS_SearchContext));
1640 sc->parent = parent;
1642 sc->serialization = GNUNET_strdup (serialization);
1644 GNUNET_BIO_read_string (rh, "search-uri", &uris, 10*1024)) ||
1645 (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
1646 ( (GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
1647 (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri)) ) ||
1649 GNUNET_BIO_read_int64 (rh, &sc->start_time.value)) ||
1651 GNUNET_BIO_read_string (rh, "child-serialization", &child_ser, 32)) ||
1653 GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10*1024)) ||
1655 GNUNET_BIO_read_int32 (rh, &options)) ||
1657 GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof (in_pause))) ||
1659 GNUNET_BIO_read_int32 (rh, &sc->anonymity)) )
1661 /* FIXME: adjust start_time.value */
1662 sc->options = (enum GNUNET_FS_SearchOptions) options;
1663 sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16);
1664 GNUNET_snprintf (pbuf,
1670 dn = get_serialization_file_name (h, pbuf, "");
1673 GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
1676 if ('\0' == in_pause)
1679 GNUNET_FS_search_start_searching_ (sc))
1682 if (child_ser != NULL)
1684 /* FIXME: deserialize child-search! */
1687 GNUNET_CONTAINER_DLL_insert (parent->child_head,
1690 pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
1691 pi.value.search.specifics.resume.message = sc->emsg;
1692 pi.value.search.specifics.resume.is_paused = ('\0' == in_pause) ? GNUNET_NO : GNUNET_YES;
1693 sc->client_info = GNUNET_FS_search_make_status_ (&pi,
1695 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1696 &signal_result_resume,
1700 GNUNET_free_non_null (child_ser);
1701 GNUNET_free_non_null (sc->emsg);
1702 GNUNET_free_non_null (emsg);
1703 if (sc->serialization != NULL)
1704 GNUNET_FS_remove_sync_file_ (h, "search", sc->serialization);
1705 /* FIXME: remove 'pbuf' directory with search results as well! */
1706 GNUNET_free_non_null (sc->serialization);
1707 if (sc->uri != NULL)
1708 GNUNET_FS_uri_destroy (sc->uri);
1709 if (sc->master_result_map != NULL)
1711 GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
1714 GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
1717 GNUNET_free_non_null (uris);
1723 * Function called with a filename of serialized search operation
1726 * @param cls the 'struct GNUNET_FS_Handle*'
1727 * @param filename complete filename (absolute path)
1728 * @return GNUNET_OK (continue to iterate)
1731 deserialize_search_file (void *cls,
1732 const char *filename)
1734 struct GNUNET_FS_Handle *h = cls;
1737 struct GNUNET_BIO_ReadHandle *rh;
1738 struct GNUNET_FS_SearchContext *sc;
1740 ser = get_serialization_short_name (filename);
1741 rh = GNUNET_BIO_read_open (filename);
1746 GNUNET_FS_remove_sync_file_ (h, "search", ser);
1751 sc = deserialize_search (h, rh, NULL, ser);
1754 GNUNET_BIO_read_close (rh, &emsg))
1756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1757 _("Failure while resuming unindexing operation `%s': %s\n"),
1767 * Deserialize information about pending search operations.
1769 * @param h master context
1772 deserialize_search_master (struct GNUNET_FS_Handle *h)
1776 dn = get_serialization_file_name (h, "search", "");
1779 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
1785 * Setup a connection to the file-sharing service.
1787 * @param sched scheduler to use
1788 * @param cfg configuration to use
1789 * @param client_name unique identifier for this client
1790 * @param upcb function to call to notify about FS actions
1791 * @param upcb_cls closure for upcb
1792 * @param flags specific attributes for fs-operations
1793 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1794 * @return NULL on error
1796 struct GNUNET_FS_Handle *
1797 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1798 const struct GNUNET_CONFIGURATION_Handle *cfg,
1799 const char *client_name,
1800 GNUNET_FS_ProgressCallback upcb,
1802 enum GNUNET_FS_Flags flags,
1805 struct GNUNET_FS_Handle *ret;
1806 struct GNUNET_CLIENT_Connection *client;
1807 enum GNUNET_FS_OPTIONS opt;
1810 client = GNUNET_CLIENT_connect (sched,
1815 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1818 ret->client_name = GNUNET_strdup (client_name);
1820 ret->upcb_cls = upcb_cls;
1821 ret->client = client;
1823 ret->max_parallel_downloads = 1;
1824 ret->max_parallel_requests = 1;
1825 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1826 va_start (ap, flags);
1827 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1831 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1832 ret->max_parallel_downloads = va_arg (ap, unsigned int);
1834 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1835 ret->max_parallel_requests = va_arg (ap, unsigned int);
1839 GNUNET_free (ret->client_name);
1846 // FIXME: setup receive-loop with client (do we need one?)
1847 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1849 /* FIXME: could write one generic deserialization
1850 function instead of these four... */
1851 deserialize_publish (ret);
1852 deserialize_search_master (ret);
1853 /* FIXME: deserialize downloads that are NOT part of searches */
1854 deserialize_unindex (ret);
1861 * Close our connection with the file-sharing service.
1862 * The callback given to GNUNET_FS_start will no longer be
1863 * called after this function returns.
1865 * @param h handle that was returned from GNUNET_FS_start
1868 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1870 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1872 // FIXME: generate SUSPEND events and clean up state!
1874 // FIXME: terminate receive-loop with client (do we need one?)
1875 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1876 GNUNET_SCHEDULER_cancel (h->sched,
1878 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1879 GNUNET_free (h->client_name);