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);
668 GNUNET_free_non_null (ksks);
669 GNUNET_free_non_null (chks);
670 GNUNET_free_non_null (filename);
671 GNUNET_FS_file_information_destroy (ret, NULL, NULL);
677 * Using the given serialization filename, try to deserialize
678 * the file-information tree associated with it.
680 * @param h master context
681 * @param filename name of the file (without directory) with
683 * @return NULL on error
685 static struct GNUNET_FS_FileInformation *
686 deserialize_file_information (struct GNUNET_FS_Handle *h,
687 const char *filename)
689 struct GNUNET_FS_FileInformation *ret;
690 struct GNUNET_BIO_ReadHandle *rh;
693 rh = get_read_handle (h, "publish-fi", filename);
696 ret = deserialize_fi_node (h, filename, rh);
698 GNUNET_BIO_read_close (rh, &emsg))
700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701 _("Failed to resume publishing information `%s': %s\n"),
711 * Given a serialization name (full absolute path), return the
712 * basename of the file (without the path), which must only
713 * consist of the 6 random characters.
715 * @param fullname name to extract the basename from
716 * @return copy of the basename, NULL on error
719 get_serialization_short_name (const char *fullname)
726 /* FIXME: we could do this faster since we know
727 the length of 'end'... */
730 if (DIR_SEPARATOR == *nxt)
734 if ( (end == NULL) ||
735 (strlen (end) == 0) )
740 GNUNET_break (6 == strlen (end));
741 return GNUNET_strdup (end);
746 * Create a new random name for serialization. Also checks if persistence
747 * is enabled and returns NULL if not.
749 * @param h master context
750 * @param ext component of the path
751 * @return NULL on errror
754 make_serialization_file_name (struct GNUNET_FS_Handle *h,
761 if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
762 return NULL; /* persistence not requested */
763 dn = get_serialization_file_name (h, ext, "");
764 fn = GNUNET_DISK_mktemp (dn);
767 return NULL; /* epic fail */
768 ret = get_serialization_short_name (fn);
775 * Copy all of the data from the reader to the write handle.
777 * @param wh write handle
778 * @param fi file with reader
779 * @return GNUNET_OK on success
782 copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
783 struct GNUNET_FS_FileInformation * fi)
792 while (off < fi->data.file.file_size)
794 ret = fi->data.file.reader (fi->data.file.reader_cls,
801 return GNUNET_SYSERR;
804 GNUNET_BIO_write (wh, buf, ret))
805 return GNUNET_SYSERR;
813 * Create a temporary file on disk to store the current
816 * @param fi file information to sync with disk
819 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
822 struct GNUNET_BIO_WriteHandle *wh;
827 if (NULL == fi->serialization)
828 fi->serialization = make_serialization_file_name (fi->h, "publish-fi");
829 if (NULL == fi->serialization)
831 wh = get_write_handle (fi->h, "publish-fi", fi->serialization);
834 GNUNET_free (fi->serialization);
835 fi->serialization = NULL;
838 if (GNUNET_YES == fi->is_directory)
840 else if (GNUNET_YES == fi->data.file.index_start_confirmed)
842 else if (GNUNET_YES == fi->data.file.have_hash)
844 else if (GNUNET_YES == fi->data.file.do_index)
848 if (fi->keywords != NULL)
849 ksks = GNUNET_FS_uri_to_string (fi->keywords);
852 if (fi->chk_uri != NULL)
853 chks = GNUNET_FS_uri_to_string (fi->chk_uri);
857 GNUNET_BIO_write (wh, &b, sizeof (b))) ||
859 GNUNET_BIO_write_meta_data (wh, fi->meta)) ||
861 GNUNET_BIO_write_string (wh, ksks)) ||
863 GNUNET_BIO_write_string (wh, chks)) ||
865 GNUNET_BIO_write_int64 (wh, fi->expirationTime.value)) ||
867 GNUNET_BIO_write_int64 (wh, fi->start_time.value)) ||
869 GNUNET_BIO_write_string (wh, fi->emsg)) ||
871 GNUNET_BIO_write_string (wh, fi->filename)) ||
873 GNUNET_BIO_write_int32 (wh, fi->anonymity)) ||
875 GNUNET_BIO_write_int32 (wh, fi->priority)) )
877 GNUNET_free_non_null (chks);
879 GNUNET_free_non_null (ksks);
884 case 0: /* file-insert */
886 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
888 if ( (GNUNET_NO == fi->is_published) &&
889 (NULL == fi->filename) )
891 copy_from_reader (wh, fi))
894 case 1: /* file-index, no hash */
895 if (NULL == fi->filename)
898 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size))
901 case 2: /* file-index-with-hash */
902 case 3: /* file-index-with-hash-confirmed */
903 if (NULL == fi->filename)
906 GNUNET_BIO_write_int64 (wh, fi->data.file.file_size)) ||
908 GNUNET_BIO_write (wh, &fi->data.file.file_id, sizeof (GNUNET_HashCode))) )
911 case 4: /* directory */
913 GNUNET_BIO_write_int32 (wh, fi->data.dir.dir_size)) ||
915 GNUNET_BIO_write (wh, fi->data.dir.dir_data, (uint32_t) fi->data.dir.dir_size)) ||
917 GNUNET_BIO_write_string (wh, fi->data.dir.entries->serialization)) )
925 GNUNET_BIO_write_string (wh, fi->next->serialization))
928 GNUNET_BIO_write_close (wh))
931 (void) GNUNET_BIO_write_close (wh);
932 GNUNET_free_non_null (chks);
933 GNUNET_free_non_null (ksks);
934 fn = get_serialization_file_name (fi->h, "publish-fi", fi->serialization);
935 if (0 != UNLINK (fn))
936 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
938 GNUNET_free (fi->serialization);
939 fi->serialization = NULL;
945 * Find the entry in the file information struct where the
946 * serialization filename matches the given name.
948 * @param pos file information to search
949 * @param srch filename to search for
950 * @return NULL if srch was not found in this subtree
952 static struct GNUNET_FS_FileInformation *
953 find_file_position (struct GNUNET_FS_FileInformation *pos,
956 struct GNUNET_FS_FileInformation *r;
960 if (0 == strcmp (srch,
963 if (pos->is_directory)
965 r = find_file_position (pos->data.dir.entries,
977 * Signal the FS's progress function that we are resuming
980 * @param cls closure (of type "struct GNUNET_FS_PublishContext*")
981 * @param fi the entry in the publish-structure
982 * @param length length of the file or directory
983 * @param meta metadata for the file or directory (can be modified)
984 * @param uri pointer to the keywords that will be used for this entry (can be modified)
985 * @param anonymity pointer to selected anonymity level (can be modified)
986 * @param priority pointer to selected priority (can be modified)
987 * @param expirationTime pointer to selected expiration time (can be modified)
988 * @param client_info pointer to client context set upon creation (can be modified)
989 * @return GNUNET_OK to continue (always)
992 fip_signal_resume(void *cls,
993 struct GNUNET_FS_FileInformation *fi,
995 struct GNUNET_CONTAINER_MetaData *meta,
996 struct GNUNET_FS_Uri **uri,
999 struct GNUNET_TIME_Absolute *expirationTime,
1002 struct GNUNET_FS_PublishContext *sc = cls;
1003 struct GNUNET_FS_ProgressInfo pi;
1005 pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
1006 pi.value.publish.specifics.resume.message = sc->fi->emsg;
1007 pi.value.publish.specifics.resume.chk_uri = sc->fi->chk_uri;
1008 *client_info = GNUNET_FS_publish_make_status_ (&pi, sc, fi, 0);
1014 * Function called with a filename of serialized publishing operation
1017 * @param cls the 'struct GNUNET_FS_Handle*'
1018 * @param filename complete filename (absolute path)
1019 * @return GNUNET_OK (continue to iterate)
1022 deserialize_publish_file (void *cls,
1023 const char *filename)
1025 struct GNUNET_FS_Handle *h = cls;
1026 struct GNUNET_BIO_ReadHandle *rh;
1027 struct GNUNET_FS_PublishContext *pc;
1035 pc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishContext));
1037 pc->serialization = get_serialization_short_name (filename);
1041 rh = GNUNET_BIO_read_open (filename);
1045 GNUNET_BIO_read_string (rh, "publish-nid", &pc->nid, 1024)) ||
1047 GNUNET_BIO_read_string (rh, "publish-nuid", &pc->nuid, 1024)) ||
1049 GNUNET_BIO_read_int32 (rh, &options)) ||
1051 GNUNET_BIO_read_int32 (rh, &all_done)) ||
1053 GNUNET_BIO_read_string (rh, "publish-firoot", &fi_root, 128)) ||
1055 GNUNET_BIO_read_string (rh, "publish-fipos", &fi_pos, 128)) ||
1057 GNUNET_BIO_read_string (rh, "publish-ns", &ns, 1024)) )
1059 pc->options = options;
1060 pc->all_done = all_done;
1061 pc->fi = deserialize_file_information (h, fi_root);
1066 pc->namespace = GNUNET_FS_namespace_create (h, ns);
1067 if (pc->namespace == NULL)
1069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1070 _("Failed to recover namespace `%s', cannot resume publishing operation.\n"),
1077 pc->fi_pos = find_file_position (pc->fi,
1079 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"),
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 (ns);
1115 if ( (rh != NULL) &&
1117 GNUNET_BIO_read_close (rh, &emsg)) )
1119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1120 _("Failed to resume publishing operation `%s': %s\n"),
1126 GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
1127 if (pc->serialization != NULL)
1128 GNUNET_FS_remove_sync_file_ (h, "publish", pc->serialization);
1129 GNUNET_free_non_null (pc->serialization);
1136 * Synchronize this publishing struct with its mirror
1137 * on disk. Note that all internal FS-operations that change
1138 * publishing structs should already call "sync" internally,
1139 * so this function is likely not useful for clients.
1141 * @param pc the struct to sync
1144 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
1146 struct GNUNET_BIO_WriteHandle *wh;
1148 if (NULL == pc->serialization)
1149 pc->serialization = make_serialization_file_name (pc->h,
1151 if (NULL == pc->serialization)
1155 if (NULL == pc->fi->serialization)
1160 wh = get_write_handle (pc->h, "publish", pc->serialization);
1162 GNUNET_BIO_write_string (wh, pc->nid)) ||
1164 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1166 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1168 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1170 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1172 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1174 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1176 (void) GNUNET_BIO_write_close (wh);
1177 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1178 GNUNET_free (pc->serialization);
1179 pc->serialization = NULL;
1183 GNUNET_BIO_write_close (wh))
1185 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1186 GNUNET_free (pc->serialization);
1187 pc->serialization = NULL;
1194 * Synchronize this unindex struct with its mirror
1195 * on disk. Note that all internal FS-operations that change
1196 * publishing structs should already call "sync" internally,
1197 * so this function is likely not useful for clients.
1199 * @param uc the struct to sync
1202 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
1204 struct GNUNET_BIO_WriteHandle *wh;
1206 if (UNINDEX_STATE_ABORTED == uc->state)
1208 if (NULL == uc->serialization)
1209 uc->serialization = make_serialization_file_name (uc->h,
1211 if (NULL == uc->serialization)
1213 wh = get_write_handle (uc->h, "unindex", uc->serialization);
1215 GNUNET_BIO_write_string (wh, uc->filename)) ||
1217 GNUNET_BIO_write_int64 (wh, uc->file_size)) ||
1219 GNUNET_BIO_write_int64 (wh, uc->start_time.value)) ||
1221 GNUNET_BIO_write_int32 (wh, (uint32_t) uc->state)) ||
1222 ( (uc->state == UNINDEX_STATE_FS_NOTIFY) &&
1224 GNUNET_BIO_write (wh, &uc->file_id, sizeof (GNUNET_HashCode))) ) ||
1225 ( (uc->state == UNINDEX_STATE_ERROR) &&
1227 GNUNET_BIO_write_string (wh, uc->emsg)) ) )
1229 (void) GNUNET_BIO_write_close (wh);
1230 GNUNET_FS_remove_sync_file_ (uc->h, "publish", uc->serialization);
1231 GNUNET_free (uc->serialization);
1232 uc->serialization = NULL;
1236 GNUNET_BIO_write_close (wh))
1238 GNUNET_FS_remove_sync_file_ (uc->h, "unindex", uc->serialization);
1239 GNUNET_free (uc->serialization);
1240 uc->serialization = NULL;
1247 * Synchronize this search struct with its mirror
1248 * on disk. Note that all internal FS-operations that change
1249 * publishing structs should already call "sync" internally,
1250 * so this function is likely not useful for clients.
1252 * @param sc the struct to sync
1255 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
1257 struct GNUNET_BIO_WriteHandle *wh;
1259 if (NULL == sc->serialization)
1260 sc->serialization = make_serialization_file_name (sc->h,
1262 if (NULL == sc->serialization)
1264 wh = get_write_handle (sc->h, "search", sc->serialization);
1267 GNUNET_BIO_write_string (wh, pc->nid)) ||
1269 GNUNET_BIO_write_string (wh, pc->nuid)) ||
1271 GNUNET_BIO_write_int32 (wh, pc->options)) ||
1273 GNUNET_BIO_write_int32 (wh, pc->all_done)) ||
1275 GNUNET_BIO_write_string (wh, pc->fi->serialization)) ||
1277 GNUNET_BIO_write_string (wh, (pc->fi_pos == NULL) ? NULL : pc->fi_pos->serialization)) ||
1279 GNUNET_BIO_write_string (wh, (pc->namespace == NULL) ? NULL : pc->namespace->name)) )
1281 (void) GNUNET_BIO_write_close (wh);
1282 GNUNET_FS_remove_sync_file_ (pc->h, "publish", pc->serialization);
1283 GNUNET_free (pc->serialization);
1284 pc->serialization = NULL;
1288 /* FIXME: do search-specific deserialization here! */
1290 GNUNET_BIO_write_close (wh))
1292 GNUNET_FS_remove_sync_file_ (sc->h, "search", sc->serialization);
1293 GNUNET_free (sc->serialization);
1294 sc->serialization = NULL;
1301 * Deserialize information about pending publish operations.
1303 * @param h master context
1306 deserialize_publish (struct GNUNET_FS_Handle *h)
1310 dn = get_serialization_file_name (h, "publish", "");
1313 GNUNET_DISK_directory_scan (dn, &deserialize_publish_file, h);
1321 * Function called with a filename of serialized unindexing operation
1324 * @param cls the 'struct GNUNET_FS_Handle*'
1325 * @param filename complete filename (absolute path)
1326 * @return GNUNET_OK (continue to iterate)
1329 deserialize_unindex_file (void *cls,
1330 const char *filename)
1332 struct GNUNET_FS_Handle *h = cls;
1333 struct GNUNET_BIO_ReadHandle *rh;
1334 struct GNUNET_FS_UnindexContext *uc;
1335 struct GNUNET_FS_ProgressInfo pi;
1339 uc = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
1341 uc->serialization = get_serialization_short_name (filename);
1342 rh = GNUNET_BIO_read_open (filename);
1346 GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10*1024)) ||
1348 GNUNET_BIO_read_int64 (rh, &uc->file_size)) ||
1350 GNUNET_BIO_read_int64 (rh, &uc->start_time.value)) ||
1352 GNUNET_BIO_read_int32 (rh, &state)) )
1354 uc->state = (enum UnindexState) state;
1357 case UNINDEX_STATE_HASHING:
1359 case UNINDEX_STATE_FS_NOTIFY:
1361 GNUNET_BIO_read (rh, "unindex-hash", &uc->file_id, sizeof (GNUNET_HashCode)))
1364 case UNINDEX_STATE_DS_REMOVE:
1366 case UNINDEX_STATE_COMPLETE:
1368 case UNINDEX_STATE_ERROR:
1370 GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10*1024))
1373 case UNINDEX_STATE_ABORTED:
1380 pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
1381 pi.value.unindex.specifics.resume.message = uc->emsg;
1382 GNUNET_FS_unindex_make_status_ (&pi,
1384 (uc->state == UNINDEX_STATE_COMPLETE)
1389 case UNINDEX_STATE_HASHING:
1390 GNUNET_CRYPTO_hash_file (uc->h->sched,
1391 GNUNET_SCHEDULER_PRIORITY_IDLE,
1394 &GNUNET_FS_unindex_process_hash_,
1397 case UNINDEX_STATE_FS_NOTIFY:
1398 uc->state = UNINDEX_STATE_HASHING;
1399 GNUNET_FS_unindex_process_hash_ (uc,
1402 case UNINDEX_STATE_DS_REMOVE:
1403 GNUNET_FS_unindex_do_remove_ (uc);
1405 case UNINDEX_STATE_COMPLETE:
1406 case UNINDEX_STATE_ERROR:
1407 /* no need to resume any operation, we were done */
1413 GNUNET_BIO_read_close (rh, &emsg))
1415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1416 _("Failure while resuming unindexing operation `%s': %s\n"),
1423 GNUNET_free_non_null (uc->filename);
1424 if ( (rh != NULL) &&
1426 GNUNET_BIO_read_close (rh, &emsg)) )
1428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1429 _("Failed to resume unindexing operation `%s': %s\n"),
1434 if (uc->serialization != NULL)
1435 GNUNET_FS_remove_sync_file_ (h, "unindex", uc->serialization);
1436 GNUNET_free_non_null (uc->serialization);
1443 * Deserialize information about pending publish operations.
1445 * @param h master context
1448 deserialize_unindex (struct GNUNET_FS_Handle *h)
1452 dn = get_serialization_file_name (h, "unindex", "");
1455 GNUNET_DISK_directory_scan (dn, &deserialize_unindex_file, h);
1462 * Function called with a filename of serialized search operation
1465 * @param cls the 'struct GNUNET_FS_Handle*'
1466 * @param filename complete filename (absolute path)
1467 * @return GNUNET_OK (continue to iterate)
1470 deserialize_search_file (void *cls,
1471 const char *filename)
1474 // Deserialize Search:
1475 // * for each query, read file with search results
1476 // * for each search result with active download, deserialize download
1477 // * for each directory search result, check for active downloads of contents
1483 * Deserialize information about pending search operations.
1485 * @param h master context
1488 deserialize_search (struct GNUNET_FS_Handle *h)
1492 dn = get_serialization_file_name (h, "search", "");
1495 GNUNET_DISK_directory_scan (dn, &deserialize_search_file, h);
1501 * Setup a connection to the file-sharing service.
1503 * @param sched scheduler to use
1504 * @param cfg configuration to use
1505 * @param client_name unique identifier for this client
1506 * @param upcb function to call to notify about FS actions
1507 * @param upcb_cls closure for upcb
1508 * @param flags specific attributes for fs-operations
1509 * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1510 * @return NULL on error
1512 struct GNUNET_FS_Handle *
1513 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
1514 const struct GNUNET_CONFIGURATION_Handle *cfg,
1515 const char *client_name,
1516 GNUNET_FS_ProgressCallback upcb,
1518 enum GNUNET_FS_Flags flags,
1521 struct GNUNET_FS_Handle *ret;
1522 struct GNUNET_CLIENT_Connection *client;
1523 enum GNUNET_FS_OPTIONS opt;
1526 client = GNUNET_CLIENT_connect (sched,
1531 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Handle));
1534 ret->client_name = GNUNET_strdup (client_name);
1536 ret->upcb_cls = upcb_cls;
1537 ret->client = client;
1539 ret->max_parallel_downloads = 1;
1540 ret->max_parallel_requests = 1;
1541 ret->avg_block_latency = GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
1542 va_start (ap, flags);
1543 while (GNUNET_FS_OPTIONS_END != (opt = va_arg (ap, enum GNUNET_FS_OPTIONS)))
1547 case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
1548 ret->max_parallel_downloads = va_arg (ap, unsigned int);
1550 case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
1551 ret->max_parallel_requests = va_arg (ap, unsigned int);
1555 GNUNET_free (ret->client_name);
1562 // FIXME: setup receive-loop with client (do we need one?)
1563 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
1565 /* FIXME: could write one generic deserialization
1566 function instead of these four... */
1567 deserialize_publish (ret);
1568 deserialize_search (ret);
1569 /* FIXME: deserialize downloads that are NOT part of searches */
1570 deserialize_unindex (ret);
1577 * Close our connection with the file-sharing service.
1578 * The callback given to GNUNET_FS_start will no longer be
1579 * called after this function returns.
1581 * @param h handle that was returned from GNUNET_FS_start
1584 GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
1586 if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & h->flags))
1588 // FIXME: generate SUSPEND events and clean up state!
1590 // FIXME: terminate receive-loop with client (do we need one?)
1591 if (h->queue_job != GNUNET_SCHEDULER_NO_TASK)
1592 GNUNET_SCHEDULER_cancel (h->sched,
1594 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1595 GNUNET_free (h->client_name);