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 /* FIXME: might be faster/simpler to do this calculation only once
131 when we start a job (OTOH, this would allow us to dynamically
132 and easily adjust qe->blocks over time, given the right API...) */
133 run_time = GNUNET_TIME_relative_multiply (h->avg_block_latency,
134 qe->blocks * qe->start_times);
135 end_time = GNUNET_TIME_absolute_add (qe->start_time,
137 rst = GNUNET_TIME_absolute_get_remaining (end_time);
138 restart_at = GNUNET_TIME_relative_min (rst, restart_at);
143 h->queue_job = GNUNET_SCHEDULER_add_delayed (h->sched,
151 * Add a job to the queue.
153 * @param h handle to the overall FS state
154 * @param start function to call to begin the job
155 * @param stop function to call to pause the job, or on dequeue (if the job was running)
156 * @param cls closure for start and stop
157 * @param blocks number of blocks this jobs uses
158 * @return queue handle
160 struct GNUNET_FS_QueueEntry *
161 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
162 GNUNET_FS_QueueStart start,
163 GNUNET_FS_QueueStop stop,
167 struct GNUNET_FS_QueueEntry *qe;
169 qe = GNUNET_malloc (sizeof (struct GNUNET_FS_QueueEntry));
174 qe->queue_time = GNUNET_TIME_absolute_get ();
176 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
180 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
181 GNUNET_SCHEDULER_cancel (h->sched,
184 = GNUNET_SCHEDULER_add_now (h->sched,
192 * Dequeue a job from the queue.
193 * @param qh handle for the job
196 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qh)
198 struct GNUNET_FS_Handle *h;
201 if (qh->client != NULL)
203 GNUNET_CONTAINER_DLL_remove (h->pending_head,
207 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
208 GNUNET_SCHEDULER_cancel (h->sched,
211 = GNUNET_SCHEDULER_add_now (h->sched,
220 * Closure for "data_reader_file".
225 * Name of the file to read.
230 * File descriptor, NULL if it has not yet been opened.
232 struct GNUNET_DISK_FileHandle *fd;
237 * Function that provides data by reading from a file.
239 * @param cls closure (points to the file information)
240 * @param offset offset to read from; it is possible
241 * that the caller might need to go backwards
243 * @param max maximum number of bytes that should be
244 * copied to buf; readers are not allowed
245 * to provide less data unless there is an error;
246 * a value of "0" will be used at the end to allow
247 * the reader to clean up its internal state
248 * @param buf where the reader should write the data
249 * @param emsg location for the reader to store an error message
250 * @return number of bytes written, usually "max", 0 on error
253 GNUNET_FS_data_reader_file_(void *cls,
259 struct FileInfo *fi = cls;
265 GNUNET_DISK_file_close (fi->fd);
266 GNUNET_free (fi->filename);
272 fi->fd = GNUNET_DISK_file_open (fi->filename,
273 GNUNET_DISK_OPEN_READ,
274 GNUNET_DISK_PERM_NONE);
277 GNUNET_asprintf (emsg,
278 _("Could not open file `%s': %s"),
284 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
285 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
288 GNUNET_asprintf (emsg,
289 _("Could not read file `%s': %s"),
296 GNUNET_asprintf (emsg,
297 _("Short read reading from file `%s'!"),
306 * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
308 * @param filename file to read
309 * @return closure to use, NULL on error
312 GNUNET_FS_make_file_reader_context_ (const char *filename)
316 fi = GNUNET_malloc (sizeof(struct FileInfo));
317 fi->filename = GNUNET_STRINGS_filename_expand (filename);
318 if (fi->filename == NULL)
328 * Function that provides data by copying from a buffer.
330 * @param cls closure (points to the buffer)
331 * @param offset offset to read from; it is possible
332 * that the caller might need to go backwards
334 * @param max maximum number of bytes that should be
335 * copied to buf; readers are not allowed
336 * to provide less data unless there is an error;
337 * a value of "0" will be used at the end to allow
338 * the reader to clean up its internal state
339 * @param buf where the reader should write the data
340 * @param emsg location for the reader to store an error message
341 * @return number of bytes written, usually "max", 0 on error
344 GNUNET_FS_data_reader_copy_ (void *cls,
354 GNUNET_free_non_null (data);
357 memcpy (buf, &data[offset], max);
364 * Return the full filename where we would store state information
365 * (for serialization/deserialization).
367 * @param h master context
368 * @param ext component of the path
369 * @param ent entity identifier (or emtpy string for the directory)
370 * @return NULL on error
373 get_serialization_file_name (struct GNUNET_FS_Handle *h,
380 GNUNET_CONFIGURATION_get_value_filename (h->cfg,
385 GNUNET_asprintf (&ret,
393 GNUNET_free (basename);
399 * Return a read handle for deserialization.
401 * @param h master context
402 * @param ext component of the path
403 * @param ent entity identifier (or emtpy string for the directory)
404 * @return NULL on error
406 static struct GNUNET_BIO_ReadHandle *
407 get_read_handle (struct GNUNET_FS_Handle *h,
412 struct GNUNET_BIO_ReadHandle *ret;
414 fn = get_serialization_file_name (h, ext, ent);
417 ret = GNUNET_BIO_read_open (fn);
424 * Return a write handle for serialization.
426 * @param h master context
427 * @param ext component of the path
428 * @param ent entity identifier (or emtpy string for the directory)
429 * @return NULL on error
431 static struct GNUNET_BIO_WriteHandle *
432 get_write_handle (struct GNUNET_FS_Handle *h,
437 struct GNUNET_BIO_WriteHandle *ret;
439 fn = get_serialization_file_name (h, ext, ent);
442 ret = GNUNET_BIO_write_open (fn);
449 * Remove serialization/deserialization file from disk.
451 * @param h master context
452 * @param ext component of the path
453 * @param ent entity identifier
456 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
462 if ( (NULL == ent) ||
463 (0 == strlen (ent)) )
468 filename = get_serialization_file_name (h, ext, ent);
469 if (0 != UNLINK (filename))
470 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
473 GNUNET_free (filename);
479 * Using the given serialization filename, try to deserialize
480 * the file-information tree associated with it.
482 * @param h master context
483 * @param filename name of the file (without directory) with
485 * @return NULL on error
487 static struct GNUNET_FS_FileInformation *
488 deserialize_file_information (struct GNUNET_FS_Handle *h,
489 const char *filename);
493 * Using the given serialization filename, try to deserialize
494 * the file-information tree associated with it.
496 * @param h master context
497 * @param fn name of the file (without directory) with
499 * @param rh handle for reading
500 * @return NULL on error
502 static struct GNUNET_FS_FileInformation *
503 deserialize_fi_node (struct GNUNET_FS_Handle *h,
505 struct GNUNET_BIO_ReadHandle *rh)
507 struct GNUNET_FS_FileInformation *ret;
508 struct GNUNET_FS_FileInformation *nxt;
516 GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
521 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
526 GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
528 GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32*1024)) ||
531 (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ) ||
533 GNUNET_FS_uri_test_ksk (ret->keywords)) ||
535 GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
538 (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
540 GNUNET_FS_uri_test_chk (ret->chk_uri)) ) ) ||
542 GNUNET_BIO_read_int64 (rh, &ret->expirationTime.value)) ||
544 GNUNET_BIO_read_int64 (rh, &ret->start_time.value)) ||
546 GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16*1024)) ||
548 GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16*1024)) ||
550 GNUNET_BIO_read_int32 (rh, &ret->anonymity)) ||
552 GNUNET_BIO_read_int32 (rh, &ret->priority)) )
556 case 0: /* file-insert */
558 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
560 ret->is_directory = GNUNET_NO;
561 ret->data.file.do_index = GNUNET_NO;
562 ret->data.file.have_hash = GNUNET_NO;
563 ret->data.file.index_start_confirmed = GNUNET_NO;
564 if (GNUNET_NO == ret->is_published)
566 if (NULL == ret->filename)
568 ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
569 ret->data.file.reader_cls = GNUNET_malloc_large (ret->data.file.file_size);
570 if (ret->data.file.reader_cls == NULL)
573 GNUNET_BIO_read (rh, "file-data", ret->data.file.reader_cls, ret->data.file.file_size))
578 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
579 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
583 case 1: /* file-index, no hash */
584 if (NULL == ret->filename)
587 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size))
589 ret->is_directory = GNUNET_NO;
590 ret->data.file.do_index = GNUNET_YES;
591 ret->data.file.have_hash = GNUNET_NO;
592 ret->data.file.index_start_confirmed = GNUNET_NO;
593 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
594 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
596 case 2: /* file-index-with-hash */
597 if (NULL == ret->filename)
600 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
602 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
604 ret->is_directory = GNUNET_NO;
605 ret->data.file.do_index = GNUNET_YES;
606 ret->data.file.have_hash = GNUNET_YES;
607 ret->data.file.index_start_confirmed = GNUNET_NO;
608 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
609 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
611 case 3: /* file-index-with-hash-confirmed */
612 if (NULL == ret->filename)
615 GNUNET_BIO_read_int64 (rh, &ret->data.file.file_size)) ||
617 GNUNET_BIO_read (rh, "fileid", &ret->data.file.file_id, sizeof (GNUNET_HashCode))) )
620 ret->is_directory = GNUNET_NO;
621 ret->data.file.do_index = GNUNET_YES;
622 ret->data.file.have_hash = GNUNET_YES;
623 ret->data.file.index_start_confirmed = GNUNET_YES;
624 ret->data.file.reader = &GNUNET_FS_data_reader_file_;
625 ret->data.file.reader_cls = GNUNET_FS_make_file_reader_context_ (ret->filename);
627 case 4: /* directory */
629 GNUNET_BIO_read_int32 (rh, &dsize)) ||
630 (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
632 GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
634 GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16*1024)) )
636 ret->data.dir.dir_size = (uint32_t) dsize;
637 ret->is_directory = GNUNET_YES;
638 if (filename != NULL)
640 ret->data.dir.entries = deserialize_file_information (h, filename);
641 GNUNET_free (filename);
643 nxt = ret->data.dir.entries;
655 /* FIXME: adjust ret->start_time! */
656 ret->serialization = GNUNET_strdup (fn);
658 GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16*1024))
660 if (filename != NULL)
662 ret->next = deserialize_file_information (h, filename);
663 GNUNET_free (filename);
666 GNUNET_free_non_null (ksks);
667 GNUNET_free_non_null (chks);
670 GNUNET_free_non_null (ksks);
671 GNUNET_free_non_null (chks);
672 GNUNET_free_non_null (filename);
673 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
679 * Using the given serialization filename, try to deserialize
680 * the file-information tree associated with it.
682 * @param h master context
683 * @param filename name of the file (without directory) with
685 * @return NULL on error
687 static struct GNUNET_FS_FileInformation *
688 deserialize_file_information (struct GNUNET_FS_Handle *h,
689 const char *filename)
691 struct GNUNET_FS_FileInformation *ret;
692 struct GNUNET_BIO_ReadHandle *rh;
695 rh = get_read_handle (h, "publish-fi", filename);
698 ret = deserialize_fi_node (h, filename, rh);
700 GNUNET_BIO_read_close (rh, &emsg))
702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
703 _("Failed to resume publishing information `%s': %s\n"),
713 * Given a serialization name (full absolute path), return the
714 * basename of the file (without the path), which must only
715 * consist of the 6 random characters.
717 * @param fullname name to extract the basename from
718 * @return copy of the basename, NULL on error
721 get_serialization_short_name (const char *fullname)
728 /* FIXME: we could do this faster since we know
729 the length of 'end'... */
732 if (DIR_SEPARATOR == *nxt)
736 if ( (end == NULL) ||
737 (strlen (end) == 0) )
742 GNUNET_break (6 == strlen (end));
743 return GNUNET_strdup (end);
748 * Create a new random name for serialization. Also checks if persistence
749 * is enabled and returns NULL if not.
751 * @param h master context
752 * @param ext component of the path
753 * @return NULL on errror
756 make_serialization_file_name (struct GNUNET_FS_Handle *h,
763 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
764 return NULL; /* persistence not requested */
765 dn = get_serialization_file_name (h, ext, "");
766 fn = GNUNET_DISK_mktemp (dn);
769 return NULL; /* epic fail */
770 ret = get_serialization_short_name (fn);
777 * Copy all of the data from the reader to the write handle.
779 * @param wh write handle
780 * @param fi file with reader
781 * @return GNUNET_OK on success
784 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
785 struct GNUNET_FS_FileInformation * fi)
794 while (off < fi->data.file.file_size)
796 ret = fi->data.file.reader (fi->data.file.reader_cls,
803 return GNUNET_SYSERR;
806 GNUNET_BIO_write (wh, buf, ret))
807 return GNUNET_SYSERR;
815 * Create a temporary file on disk to store the current
818 * @param fi file information to sync with disk
821 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
824 struct GNUNET_BIO_WriteHandle *wh;
829 if (NULL == fi->serialization)
830 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
831 if (NULL == fi->serialization)
833 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
836 GNUNET_free (fi->serialization);
837 fi->serialization = NULL;
840 if (GNUNET_YES == fi->is_directory)
842 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
844 else if (GNUNET_YES == fi->data.file.have_hash)
846 else if (GNUNET_YES == fi->data.file.do_index)
850 if (fi->keywords != NULL)
851 ksks = GNUNET_FS_uri_to_string (fi->keywords);
854 if (fi->chk_uri != NULL)
855 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
859 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
861 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
863 GNUNET_BIO_write_string (wh, ksks)) ||
865 GNUNET_BIO_write_string (wh, chks)) ||
867 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
869 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
871 GNUNET_BIO_write_string (wh, fi->emsg)) ||
873 GNUNET_BIO_write_string (wh, fi->filename)) ||
875 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
877 GNUNET_BIO_write_int32 (wh, fi->priority)) )
879 GNUNET_free_non_null (chks);
881 GNUNET_free_non_null (ksks);
886 case 0: /* file-insert */
888 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
890 if ( (GNUNET_NO == fi->is_published) &&
891 (NULL == fi->filename) )
893 copy_from_reader (wh, fi))
896 case 1: /* file-index, no hash */
897 if (NULL == fi->filename)
900 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
903 case 2: /* file-index-with-hash */
904 case 3: /* file-index-with-hash-confirmed */
905 if (NULL == fi->filename)
908 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
910 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
913 case 4: /* directory */
915 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
917 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
919 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
927 GNUNET_BIO_write_string (wh, fi->next->serialization))
930 GNUNET_BIO_write_close (wh))
933 (void) GNUNET_BIO_write_close (wh);
934 GNUNET_free_non_null (chks);
935 GNUNET_free_non_null (ksks);
936 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
937 if (0 != UNLINK (fn))
938 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
940 GNUNET_free (fi->serialization);
941 fi->serialization = NULL;
947 * Find the entry in the file information struct where the
948 * serialization filename matches the given name.
950 * @param pos file information to search
951 * @param srch filename to search for
952 * @return NULL if srch was not found in this subtree
954 static struct GNUNET_FS_FileInformation *
955 find_file_position (struct GNUNET_FS_FileInformation *pos,
958 struct GNUNET_FS_FileInformation *r;
962 if (0 == strcmp (srch,
965 if (pos->is_directory)
967 r = find_file_position (pos->data.dir.entries,
979 * Signal the FS's progress function that we are resuming
982 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
983 * @param fi the entry in the publish-structure
984 * @param length length of the file or directory
985 * @param meta metadata for the file or directory (can be modified)
986 * @param uri pointer to the keywords that will be used for this entry (can be modified)
987 * @param anonymity pointer to selected anonymity level (can be modified)
988 * @param priority pointer to selected priority (can be modified)
989 * @param expirationTime pointer to selected expiration time (can be modified)
990 * @param client_info pointer to client context set upon creation (can be modified)
991 * @return GNUNET_OK to continue (always)
994 fip_signal_resume(void *cls,
995 struct GNUNET_FS_FileInformation *fi,
997 struct GNUNET_CONTAINER_MetaData *meta,
998 struct GNUNET_FS_Uri **uri,
1001 struct GNUNET_TIME_Absolute *expirationTime,
1004 struct GNUNET_FS_PublishContext *sc = cls;
1005 struct GNUNET_FS_ProgressInfo pi;
1007 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1008 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1009 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1010 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1016 * Function called with a filename of serialized publishing operation
1019 * @param cls the 'struct GNUNET_FS_Handle*'
1020 * @param filename complete filename (absolute path)
1021 * @return GNUNET_OK (continue to iterate)
1024 deserialize_publish_file (void *cls,
1025 const char *filename)
1027 struct GNUNET_FS_Handle *h = cls;
1028 struct GNUNET_BIO_ReadHandle *rh;
1029 struct GNUNET_FS_PublishContext *pc;
1037 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1039 pc->serialization = get_serialization_short_name (filename);
1043 rh = GNUNET_BIO_read_open (filename);
1047 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1049 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1051 GNUNET_BIO_read_int32 (rh, &options)) ||
1053 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1055 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1057 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1059 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1061 pc->options = options;
1062 pc->all_done = all_done;
1063 pc->fi = deserialize_file_information (h, fi_root);
1068 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1069 if (pc->namespace == NULL)
1071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1072 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1079 pc->fi_pos = find_file_position (pc->fi,
1081 GNUNET_free (fi_pos);
1083 if (pc->fi_pos == NULL)
1085 /* failed to find position for resuming, outch! Will start from root! */
1087 if (pc->all_done != GNUNET_YES)
1088 pc->fi_pos = pc->fi;
1091 /* generate RESUME event(s) */
1092 GNUNET_FS_file_information_inspect (pc->fi,
1096 /* re-start publishing (if needed)... */
1097 if (pc->all_done != GNUNET_YES)
1099 = GNUNET_SCHEDULER_add_with_priority (h->sched,
1100 GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
1101 &GNUNET_FS_publish_main_,
1104 GNUNET_BIO_read_close (rh, &emsg))
1106 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1107 _("Failure while resuming publishing operation `%s': %s\n"),
1112 GNUNET_free_non_null (ns);
1115 GNUNET_free_non_null (pc->nid);
1116 GNUNET_free_non_null (pc->nuid);
1117 GNUNET_free_non_null (fi_root);
1118 GNUNET_free_non_null (fi_pos);
1119 GNUNET_free_non_null (ns);
1120 if ( (rh != NULL) &&
1122 GNUNET_BIO_read_close (rh, &emsg)) )
1124 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1125 _("Failed to resume publishing operation `%s': %s\n"),
1131 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1132 if (pc->serialization != NULL)
1133 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1134 GNUNET_free_non_null (pc->serialization);
1141 * Synchronize this publishing struct with its mirror
1142 * on disk. Note that all internal FS-operations that change
1143 * publishing structs should already call "sync" internally,
1144 * so this function is likely not useful for clients.
1146 * @param pc the struct to sync
1149 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1151 struct GNUNET_BIO_WriteHandle *wh;
1153 if (NULL == pc->serialization)
1154 pc->serialization = make_serialization_file_name (pc->h,
1156 if (NULL == pc->serialization)
1160 if (NULL == pc->fi->serialization)
1165 wh = get_write_handle (pc->h, "publish", pc->serialization);
1167 GNUNET_BIO_write_string (wh, pc->nid)) ||
1169 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1171 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1173 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1175 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1177 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1179 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1181 (void) GNUNET_BIO_write_close (wh);
1182 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1183 GNUNET_free (pc->serialization);
1184 pc->serialization = NULL;
1188 GNUNET_BIO_write_close (wh))
1190 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1191 GNUNET_free (pc->serialization);
1192 pc->serialization = NULL;
1199 * Synchronize this unindex struct with its mirror
1200 * on disk. Note that all internal FS-operations that change
1201 * publishing structs should already call "sync" internally,
1202 * so this function is likely not useful for clients.
1204 * @param uc the struct to sync
1207 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1209 struct GNUNET_BIO_WriteHandle *wh;
1211 if (UNINDEX_STATE_ABORTED == uc->state)
1213 if (NULL == uc->serialization)
1214 uc->serialization = make_serialization_file_name (uc->h,
1216 if (NULL == uc->serialization)
1218 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1220 GNUNET_BIO_write_string (wh, uc->filename)) ||
1222 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1224 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1226 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1227 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1229 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1230 ( (uc->state == UNINDEX_STATE_ERROR) &&
1232 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1234 (void) GNUNET_BIO_write_close (wh);
1235 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1236 GNUNET_free (uc->serialization);
1237 uc->serialization = NULL;
1241 GNUNET_BIO_write_close (wh))
1243 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1244 GNUNET_free (uc->serialization);
1245 uc->serialization = NULL;
1252 * Synchronize this search struct with its mirror
1253 * on disk. Note that all internal FS-operations that change
1254 * publishing structs should already call "sync" internally,
1255 * so this function is likely not useful for clients.
1257 * @param sc the struct to sync
1260 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1262 struct GNUNET_BIO_WriteHandle *wh;
1264 if (NULL == sc->serialization)
1265 sc->serialization = make_serialization_file_name (sc->h,
1267 if (NULL == sc->serialization)
1269 wh = get_write_handle (sc->h, "search", sc->serialization);
1272 GNUNET_BIO_write_string (wh, pc->nid)) ||
1274 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1276 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1278 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1280 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1282 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1284 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1286 (void) GNUNET_BIO_write_close (wh);
1287 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1288 GNUNET_free (pc->serialization);
1289 pc->serialization = NULL;
1293 /* FIXME: do search-specific deserialization here! */
1295 GNUNET_BIO_write_close (wh))
1297 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1298 GNUNET_free (sc->serialization);
1299 sc->serialization = NULL;
1306 * Deserialize information about pending publish operations.
1308 * @param h master context
1311 deserialize_publish (struct GNUNET_FS_Handle *h)
1315 dn = get_serialization_file_name (h, "publish", "");
1318 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1326 * Function called with a filename of serialized unindexing operation
1329 * @param cls the 'struct GNUNET_FS_Handle*'
1330 * @param filename complete filename (absolute path)
1331 * @return GNUNET_OK (continue to iterate)
1334 deserialize_unindex_file (void *cls,
1335 const char *filename)
1337 struct GNUNET_FS_Handle *h = cls;
1338 struct GNUNET_BIO_ReadHandle *rh;
1339 struct GNUNET_FS_UnindexContext *uc;
1340 struct GNUNET_FS_ProgressInfo pi;
1344 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1346 uc->serialization = get_serialization_short_name (filename);
1347 rh = GNUNET_BIO_read_open (filename);
1351 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1353 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1355 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1357 GNUNET_BIO_read_int32 (rh, &state)) )
1359 uc->state = (enum UnindexState) state;
1362 case UNINDEX_STATE_HASHING:
1364 case UNINDEX_STATE_FS_NOTIFY:
1366 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1369 case UNINDEX_STATE_DS_REMOVE:
1371 case UNINDEX_STATE_COMPLETE:
1373 case UNINDEX_STATE_ERROR:
1375 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1378 case UNINDEX_STATE_ABORTED:
1385 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1386 pi.value.unindex.specifics.resume.message = uc->emsg;
1387 GNUNET_FS_unindex_make_status_ (&pi,
1389 (uc->state == UNINDEX_STATE_COMPLETE)
1394 case UNINDEX_STATE_HASHING:
1395 GNUNET_CRYPTO_hash_file (uc->h->sched,
1396 GNUNET_SCHEDULER_PRIORITY_IDLE,
1399 &GNUNET_FS_unindex_process_hash_,
1402 case UNINDEX_STATE_FS_NOTIFY:
1403 uc->state = UNINDEX_STATE_HASHING;
1404 GNUNET_FS_unindex_process_hash_ (uc,
1407 case UNINDEX_STATE_DS_REMOVE:
1408 GNUNET_FS_unindex_do_remove_ (uc);
1410 case UNINDEX_STATE_COMPLETE:
1411 case UNINDEX_STATE_ERROR:
1412 /* no need to resume any operation, we were done */
1418 GNUNET_BIO_read_close (rh, &emsg))
1420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1421 _("Failure while resuming unindexing operation `%s': %s\n"),
1428 GNUNET_free_non_null (uc->filename);
1429 if ( (rh != NULL) &&
1431 GNUNET_BIO_read_close (rh, &emsg)) )
1433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1434 _("Failed to resume unindexing operation `%s': %s\n"),
1439 if (uc->serialization != NULL)
1440 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1441 GNUNET_free_non_null (uc->serialization);
1448 * Deserialize information about pending publish operations.
1450 * @param h master context
1453 deserialize_unindex (struct GNUNET_FS_Handle *h)
1457 dn = get_serialization_file_name (h, "unindex", "");
1460 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1467 * Function called with a filename of serialized search operation
1470 * @param cls the 'struct GNUNET_FS_Handle*'
1471 * @param filename complete filename (absolute path)
1472 * @return GNUNET_OK (continue to iterate)
1475 deserialize_search_file (void *cls,
1476 const char *filename)
1479 // Deserialize Search:
1480 // * for each query, read file with search results
1481 // * for each search result with active download, deserialize download
1482 // * for each directory search result, check for active downloads of contents
1488 * Deserialize information about pending search operations.
1490 * @param h master context
1493 deserialize_search (struct GNUNET_FS_Handle *h)
1497 dn = get_serialization_file_name (h, "search", "");
1500 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
1506 * Setup a connection to the file-sharing service.
1508 * @param sched scheduler to use
1509 * @param cfg configuration to use
1510 * @param client_name unique identifier for this client
1511 * @param upcb function to call to notify about FS actions
1512 * @param upcb_cls closure for upcb
1513 * @param flags specific attributes for fs-operations
1514 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1515 * @return NULL on error
1517 struct GNUNET_FS_Handle *
1518 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1519 const struct GNUNET_CONFIGURATION_Handle *cfg,
1520 const char *client_name,
1521 GNUNET_FS_ProgressCallback upcb,
1523 enum GNUNET_FS_Flags flags,
1526 struct GNUNET_FS_Handle *ret;
1527 struct GNUNET_CLIENT_Connection *client;
1528 enum GNUNET_FS_OPTIONS opt;
1531 client = GNUNET_CLIENT_connect (sched,
1536 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1539 ret->client_name = GNUNET_strdup (client_name);
1541 ret->upcb_cls = upcb_cls;
1542 ret->client = client;
1544 ret->max_parallel_downloads = 1;
1545 ret->max_parallel_requests = 1;
1546 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1547 va_start (ap, flags);
1548 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1552 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1553 ret->max_parallel_downloads = va_arg (ap, unsigned int);
1555 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1556 ret->max_parallel_requests = va_arg (ap, unsigned int);
1560 GNUNET_free (ret->client_name);
1567 // FIXME: setup receive-loop with client (do we need one?)
1568 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1570 /* FIXME: could write one generic deserialization
1571 function instead of these four... */
1572 deserialize_publish (ret);
1573 deserialize_search (ret);
1574 /* FIXME: deserialize downloads that are NOT part of searches */
1575 deserialize_unindex (ret);
1582 * Close our connection with the file-sharing service.
1583 * The callback given to GNUNET_FS_start will no longer be
1584 * called after this function returns.
1586 * @param h handle that was returned from GNUNET_FS_start
1589 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1591 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1593 // FIXME: generate SUSPEND events and clean up state!
1595 // FIXME: terminate receive-loop with client (do we need one?)
1596 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1597 GNUNET_SCHEDULER_cancel (h->sched,
1599 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1600 GNUNET_free (h->client_name);