-options to play with
[oweals/gnunet.git] / src / fs / fs_api.h
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/fs_api.h
23  * @brief shared definitions for the FS library
24  * @author Igor Wronsky, Christian Grothoff
25  */
26 #ifndef FS_API_H
27 #define FS_API_H
28
29 #include "gnunet_constants.h"
30 #include "gnunet_datastore_service.h"
31 #include "gnunet_dht_service.h"
32 #include "gnunet_fs_service.h"
33 #include "gnunet_block_lib.h"
34 #include "block_fs.h"
35 #include "fs.h"
36
37 /**
38  * Size of the individual blocks used for file-sharing.
39  */
40 #define DBLOCK_SIZE (32*1024)
41
42 /**
43  * Pick a multiple of 2 here to achive 8-byte alignment!  We also
44  * probably want DBlocks to have (roughly) the same size as IBlocks.
45  * With SHA-512, the optimal value is 32768 byte / 128 byte = 256 (128
46  * byte = 2 * 512 bits).  DO NOT CHANGE!
47  */
48 #define CHK_PER_INODE 256
49
50 /**
51  * Maximum size for a file to be considered for inlining in a
52  * directory.
53  */
54 #define MAX_INLINE_SIZE 65536
55
56 /**
57  * Name of the directory with top-level searches.
58  */
59 #define GNUNET_FS_SYNC_PATH_MASTER_SEARCH "search"
60
61 /**
62  * Name of the directory with sub-searches (namespace-updates).
63  */
64 #define GNUNET_FS_SYNC_PATH_CHILD_SEARCH "search-child"
65
66 /**
67  * Name of the directory with master downloads (not associated
68  * with search or part of another download).
69  */
70 #define GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD "download"
71
72 /**
73  * Name of the directory with downloads that are part of another
74  * download or a search.
75  */
76 #define GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD "download-child"
77
78 /**
79  * Name of the directory with publishing operations.
80  */
81 #define GNUNET_FS_SYNC_PATH_MASTER_PUBLISH "publish"
82
83 /**
84  * Name of the directory with files that are being published
85  */
86 #define GNUNET_FS_SYNC_PATH_FILE_INFO "publish-file"
87
88 /**
89  * Name of the directory with unindex operations.
90  */
91 #define GNUNET_FS_SYNC_PATH_MASTER_UNINDEX "unindex"
92
93
94 /**
95  * @brief complete information needed
96  * to download a file.
97  */
98 struct FileIdentifier
99 {
100
101   /**
102    * Total size of the file in bytes. (network byte order (!))
103    */
104   uint64_t file_length;
105
106   /**
107    * Query and key of the top GNUNET_EC_IBlock.
108    */
109   struct ContentHashKey chk;
110
111 };
112
113
114 /**
115  * Information about a file and its location
116  * (peer claiming to share the file).
117  */
118 struct Location
119 {
120   /**
121    * Information about the shared file.
122    */
123   struct FileIdentifier fi;
124
125   /**
126    * Identity of the peer sharing the file.
127    */
128   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
129
130   /**
131    * Time when this location URI expires.
132    */
133   struct GNUNET_TIME_Absolute expirationTime;
134
135   /**
136    * RSA signature over the GNUNET_EC_FileIdentifier,
137    * GNUNET_hash of the peer and expiration time.
138    */
139   struct GNUNET_CRYPTO_RsaSignature contentSignature;
140
141 };
142
143 /**
144  * Types of URIs.
145  */
146 enum uri_types
147 {
148     /**
149      * Content-hash-key (simple file).
150      */
151   chk,
152
153     /**
154      * Signed key space (file in namespace).
155      */
156   sks,
157
158     /**
159      * Keyword search key (query with keywords).
160      */
161   ksk,
162
163     /**
164      * Location (chk with identity of hosting peer).
165      */
166   loc
167 };
168
169 /**
170  * A Universal Resource Identifier (URI), opaque.
171  */
172 struct GNUNET_FS_Uri
173 {
174   /**
175    * Type of the URI.
176    */
177   enum uri_types type;
178
179   union
180   {
181     struct
182     {
183       /**
184        * Keywords start with a '+' if they are
185        * mandatory (in which case the '+' is NOT
186        * part of the keyword) and with a
187        * simple space if they are optional
188        * (in which case the space is ALSO not
189        * part of the actual keyword).
190        *
191        * Double-quotes to protect spaces and
192        * %-encoding are NOT used internally
193        * (only in URI-strings).
194        */
195       char **keywords;
196
197       /**
198        * Size of the keywords array.
199        */
200       unsigned int keywordCount;
201     } ksk;
202
203     struct
204     {
205       /**
206        * Hash of the public key for the namespace.
207        */
208       struct GNUNET_HashCode ns;
209
210       /**
211        * Human-readable identifier chosen for this
212        * entry in the namespace.
213        */
214       char *identifier;
215     } sks;
216
217     /**
218      * Information needed to retrieve a file (content-hash-key
219      * plus file size).
220      */
221     struct FileIdentifier chk;
222
223     /**
224      * Information needed to retrieve a file including signed
225      * location (identity of a peer) of the content.
226      */
227     struct Location loc;
228   } data;
229
230 };
231
232
233 /**
234  * Information for a file or directory that is
235  * about to be published.
236  */
237 struct GNUNET_FS_FileInformation
238 {
239
240   /**
241    * Files in a directory are kept as a linked list.
242    */
243   struct GNUNET_FS_FileInformation *next;
244
245   /**
246    * If this is a file in a directory, "dir" refers to
247    * the directory; otherwise NULL.
248    */
249   struct GNUNET_FS_FileInformation *dir;
250
251   /**
252    * Handle to the master context.
253    */
254   struct GNUNET_FS_Handle *h;
255
256   /**
257    * Pointer kept for the client.
258    */
259   void *client_info;
260
261   /**
262    * Metadata to use for the file.
263    */
264   struct GNUNET_CONTAINER_MetaData *meta;
265
266   /**
267    * Keywords to use for KBlocks.
268    */
269   struct GNUNET_FS_Uri *keywords;
270
271   /**
272    * CHK for this file or directory. NULL if
273    * we have not yet computed it.
274    */
275   struct GNUNET_FS_Uri *chk_uri;
276
277   /**
278    * Block options for the file.
279    */
280   struct GNUNET_FS_BlockOptions bo;
281
282   /**
283    * At what time did we start this upload?
284    */
285   struct GNUNET_TIME_Absolute start_time;
286
287   /**
288    * Under what filename is this struct serialized
289    * (for operational persistence).  Should be determined
290    * using 'mktemp'.
291    */
292   char *serialization;
293
294   /**
295    * Encoder being used to publish this file.
296    */
297   struct GNUNET_FS_TreeEncoder *te;
298
299   /**
300    * Error message (non-NULL if this operation failed).
301    */
302   char *emsg;
303
304   /**
305    * Name of the file or directory (must be an absolute path).
306    */
307   char *filename;
308
309   /**
310    * Data describing either the file or the directory.
311    */
312   union
313   {
314
315     /**
316      * Data for a file.
317      */
318     struct
319     {
320
321       /**
322        * Function that can be used to read the data for the file.
323        */
324       GNUNET_FS_DataReader reader;
325
326       /**
327        * Closure for reader.
328        */
329       void *reader_cls;
330
331       /**
332        * If this file is being indexed, this value is set to the hash
333        * over the entire file (when the indexing process is started).
334        * Otherwise this field is not used.
335        */
336       struct GNUNET_HashCode file_id;
337
338       /**
339        * Size of the file (in bytes).
340        */
341       uint64_t file_size;
342
343       /**
344        * Should the file be indexed or inserted?
345        */
346       int do_index;
347
348       /**
349        * Is "file_id" already valid?  Set to GNUNET_YES once the hash
350        * has been calculated.
351        */
352       int have_hash;
353
354       /**
355        * Has the service confirmed our INDEX_START request?
356        * GNUNET_YES if this step has been completed.
357        */
358       int index_start_confirmed;
359
360     } file;
361
362     /**
363      * Data for a directory.
364      */
365     struct
366     {
367
368       /**
369        * Linked list of entries in the directory.
370        */
371       struct GNUNET_FS_FileInformation *entries;
372
373       /**
374        * Size of the directory itself (in bytes); 0 if the
375        * size has not yet been calculated.
376        */
377       size_t dir_size;
378
379       /**
380        * Pointer to the data for the directory (or NULL if not
381        * available).
382        */
383       void *dir_data;
384
385     } dir;
386
387   } data;
388
389   /**
390    * Is this struct for a file or directory?
391    */
392   int is_directory;
393
394   /**
395    * Are we done publishing this file?
396    */
397   int is_published;
398
399 };
400
401
402 /**
403  * The job is now ready to run and should use the given client
404  * handle to communicate with the FS service.
405  *
406  * @param cls closure
407  * @param client handle to use for FS communication
408  */
409 typedef void (*GNUNET_FS_QueueStart) (void *cls,
410                                       struct GNUNET_CLIENT_Connection * client);
411
412
413 /**
414  * The job must now stop to run and should destry the client handle as
415  * soon as possible (ideally prior to returning).
416  */
417 typedef void (*GNUNET_FS_QueueStop) (void *cls);
418
419
420
421 /**
422  * Priorities for the queue.
423  */ 
424 enum GNUNET_FS_QueuePriority
425   {
426     /**
427      * This is a probe (low priority).
428      */
429     GNUNET_FS_QUEUE_PRIORITY_PROBE,
430
431     /**
432      * Default priority.
433      */
434     GNUNET_FS_QUEUE_PRIORITY_NORMAL
435   };
436
437
438 /**
439  * Entry in the job queue.
440  */
441 struct GNUNET_FS_QueueEntry
442 {
443   /**
444    * This is a linked list.
445    */
446   struct GNUNET_FS_QueueEntry *next;
447
448   /**
449    * This is a linked list.
450    */
451   struct GNUNET_FS_QueueEntry *prev;
452
453   /**
454    * Function to call when the job is started.
455    */
456   GNUNET_FS_QueueStart start;
457
458   /**
459    * Function to call when the job needs to stop (or is done / dequeued).
460    */
461   GNUNET_FS_QueueStop stop;
462
463   /**
464    * Closure for start and stop.
465    */
466   void *cls;
467
468   /**
469    * Handle to FS primary context.
470    */
471   struct GNUNET_FS_Handle *h;
472
473   /**
474    * Client handle, or NULL if job is not running.
475    */
476   struct GNUNET_CLIENT_Connection *client;
477
478   /**
479    * Time the job was originally queued.
480    */
481   struct GNUNET_TIME_Absolute queue_time;
482
483   /**
484    * Time the job was started last.
485    */
486   struct GNUNET_TIME_Absolute start_time;
487
488   /**
489    * Total amount of time the job has been running (except for the
490    * current run).
491    */
492   struct GNUNET_TIME_Relative run_time;
493
494   /**
495    * How many blocks do the active downloads have?
496    */
497   unsigned int blocks;
498
499   /**
500    * How important is this download?
501    */
502   enum GNUNET_FS_QueuePriority priority;
503
504   /**
505    * How often have we (re)started this download?
506    */
507   unsigned int start_times;
508
509 };
510
511
512
513
514 /**
515  * Information we store for each search result.
516  */
517 struct GNUNET_FS_SearchResult
518 {
519
520   /**
521    * Search context this result belongs to.
522    */
523   struct GNUNET_FS_SearchContext *sc;
524
525   /**
526    * URI to which this search result refers to.
527    */
528   struct GNUNET_FS_Uri *uri;
529
530   /**
531    * Metadata for the search result.
532    */
533   struct GNUNET_CONTAINER_MetaData *meta;
534
535   /**
536    * Client info for this search result.
537    */
538   void *client_info;
539
540   /**
541    * ID of a job that is currently probing this results' availability
542    * (NULL if we are not currently probing).
543    */
544   struct GNUNET_FS_DownloadContext *probe_ctx;
545
546   /**
547    * ID of an associated download based on this search result (or
548    * NULL for none).
549    */
550   struct GNUNET_FS_DownloadContext *download;
551
552   /**
553    * If this search result triggered an update search, this field
554    * links to the update search.
555    */
556   struct GNUNET_FS_SearchContext *update_search;
557
558   /**
559    * Name under which this search result is stored on disk.
560    */
561   char *serialization;
562
563   /**
564    * Bitmap that specifies precisely which keywords have been matched already.
565    */
566   uint8_t *keyword_bitmap;
567
568   /**
569    * Key for the search result
570    */
571   struct GNUNET_HashCode key;
572
573   /**
574    * ID of the task that will clean up the probe_ctx should it not
575    * complete on time (and that will need to be cancelled if we clean
576    * up the search result before then).
577    */
578   GNUNET_SCHEDULER_TaskIdentifier probe_cancel_task;
579
580   /**
581    * Task we use to report periodically to the application that the
582    * probe is still running.
583    */
584   GNUNET_SCHEDULER_TaskIdentifier probe_ping_task;
585
586   /**
587    * When did the current probe become active?
588    */
589   struct GNUNET_TIME_Absolute probe_active_time;
590
591   /**
592    * How much longer should we run the current probe before giving up?
593    */
594   struct GNUNET_TIME_Relative remaining_probe_time;
595
596   /**
597    * Number of mandatory keywords for which we have NOT yet found the
598    * search result; when this value hits zero, the search result is
599    * given to the callback.
600    */
601   uint32_t mandatory_missing;
602
603   /**
604    * Number of optional keywords under which this result was also
605    * found.
606    */
607   uint32_t optional_support;
608
609   /**
610    * Number of availability tests that have succeeded for this result.
611    */
612   uint32_t availability_success;
613
614   /**
615    * Number of availability trials that we have performed for this
616    * search result.
617    */
618   uint32_t availability_trials;
619
620 };
621
622
623 /**
624  * Add a job to the queue.
625  *
626  * @param h handle to the overall FS state
627  * @param start function to call to begin the job
628  * @param stop function to call to pause the job, or on dequeue (if the job was running)
629  * @param cls closure for start and stop
630  * @param blocks number of blocks this download has
631  * @param priority how important is this download
632  * @return queue handle
633  */
634 struct GNUNET_FS_QueueEntry *
635 GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h, GNUNET_FS_QueueStart start,
636                   GNUNET_FS_QueueStop stop, void *cls, unsigned int blocks,
637                   enum GNUNET_FS_QueuePriority priority);
638
639
640 /**
641  * Dequeue a job from the queue.
642  *
643  * @param qe handle for the job
644  */
645 void
646 GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe);
647
648
649 /**
650  * Function that provides data by reading from a file.
651  *
652  * @param cls closure (points to the file information)
653  * @param offset offset to read from; it is possible
654  *            that the caller might need to go backwards
655  *            a bit at times
656  * @param max maximum number of bytes that should be
657  *            copied to buf; readers are not allowed
658  *            to provide less data unless there is an error;
659  *            a value of "0" will be used at the end to allow
660  *            the reader to clean up its internal state
661  * @param buf where the reader should write the data
662  * @param emsg location for the reader to store an error message
663  * @return number of bytes written, usually "max", 0 on error
664  */
665 size_t
666 GNUNET_FS_data_reader_file_ (void *cls, uint64_t offset, size_t max, void *buf,
667                              char **emsg);
668
669
670 /**
671  * Create the closure for the 'GNUNET_FS_data_reader_file_' callback.
672  *
673  * @param filename file to read
674  * @return closure to use
675  */
676 void *
677 GNUNET_FS_make_file_reader_context_ (const char *filename);
678
679
680
681 /**
682  * Function that provides data by copying from a buffer.
683  *
684  * @param cls closure (points to the buffer)
685  * @param offset offset to read from; it is possible
686  *            that the caller might need to go backwards
687  *            a bit at times
688  * @param max maximum number of bytes that should be
689  *            copied to buf; readers are not allowed
690  *            to provide less data unless there is an error;
691  *            a value of "0" will be used at the end to allow
692  *            the reader to clean up its internal state
693  * @param buf where the reader should write the data
694  * @param emsg location for the reader to store an error message
695  * @return number of bytes written, usually "max", 0 on error
696  */
697 size_t
698 GNUNET_FS_data_reader_copy_ (void *cls, uint64_t offset, size_t max, void *buf,
699                              char **emsg);
700
701 /**
702  * Notification of FS that a search probe has made progress.
703  * This function is used INSTEAD of the client's event handler
704  * for downloads where the GNUNET_FS_DOWNLOAD_IS_PROBE flag is set.
705  *
706  * @param cls closure, always NULL (!), actual closure
707  *        is in the client-context of the info struct
708  * @param info details about the event, specifying the event type
709  *        and various bits about the event
710  * @return client-context (for the next progress call
711  *         for this operation; should be set to NULL for
712  *         SUSPEND and STOPPED events).  The value returned
713  *         will be passed to future callbacks in the respective
714  *         field in the GNUNET_FS_ProgressInfo struct.
715  */
716 void *
717 GNUNET_FS_search_probe_progress_ (void *cls,
718                                   const struct GNUNET_FS_ProgressInfo *info);
719
720
721 /**
722  * Main function that performs the upload.
723  *
724  * @param cls "struct GNUNET_FS_PublishContext" identifies the upload
725  * @param tc task context
726  */
727 void
728 GNUNET_FS_publish_main_ (void *cls,
729                          const struct GNUNET_SCHEDULER_TaskContext *tc);
730
731
732 /**
733  * Function called once the hash of the file
734  * that is being unindexed has been computed.
735  *
736  * @param cls closure, unindex context
737  * @param file_id computed hash, NULL on error
738  */
739 void
740 GNUNET_FS_unindex_process_hash_ (void *cls, const struct GNUNET_HashCode * file_id);
741
742
743 /**
744  * Extract the keywords for KBlock removal
745  *
746  * @param uc context for the unindex operation.
747  */
748 void
749 GNUNET_FS_unindex_do_extract_keywords_ (struct GNUNET_FS_UnindexContext *uc);
750
751
752 /**
753  * If necessary, connect to the datastore and remove the KBlocks.
754  *
755  * @param uc context for the unindex operation.
756  */
757 void
758 GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc);
759
760
761 /**
762  * Fill in all of the generic fields for a publish event and call the
763  * callback.
764  *
765  * @param pi structure to fill in
766  * @param pc overall publishing context
767  * @param p file information for the file being published
768  * @param offset where in the file are we so far
769  * @return value returned from callback
770  */
771 void *
772 GNUNET_FS_publish_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
773                                 struct GNUNET_FS_PublishContext *pc,
774                                 const struct GNUNET_FS_FileInformation *p,
775                                 uint64_t offset);
776
777
778 /**
779  * Fill in all of the generic fields for a download event and call the
780  * callback.
781  *
782  * @param pi structure to fill in
783  * @param dc overall download context
784  */
785 void
786 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
787                                  struct GNUNET_FS_DownloadContext *dc);
788
789
790 /**
791  * Task that creates the initial (top-level) download
792  * request for the file.
793  *
794  * @param cls the 'struct GNUNET_FS_DownloadContext'
795  * @param tc scheduler context
796  */
797 void
798 GNUNET_FS_download_start_task_ (void *cls,
799                                 const struct GNUNET_SCHEDULER_TaskContext *tc);
800
801
802
803 /**
804  * Fill in all of the generic fields for
805  * an unindex event and call the callback.
806  *
807  * @param pi structure to fill in
808  * @param uc overall unindex context
809  * @param offset where we are in the file (for progress)
810  */
811 void
812 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
813                                 struct GNUNET_FS_UnindexContext *uc,
814                                 uint64_t offset);
815
816 /**
817  * Fill in all of the generic fields for a search event and
818  * call the callback.
819  *
820  * @param pi structure to fill in
821  * @param sc overall search context
822  * @return value returned by the callback
823  */
824 void *
825 GNUNET_FS_search_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
826                                struct GNUNET_FS_SearchContext *sc);
827
828
829 /**
830  * Connect to the datastore and remove the blocks.
831  *
832  * @param uc context for the unindex operation.
833  */
834 void
835 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc);
836
837 /**
838  * Build the request and actually initiate the search using the
839  * GNUnet FS service.
840  *
841  * @param sc search context
842  * @return GNUNET_OK on success, GNUNET_SYSERR on error
843  */
844 int
845 GNUNET_FS_search_start_searching_ (struct GNUNET_FS_SearchContext *sc);
846
847 /**
848  * Start the downloading process (by entering the queue).
849  *
850  * @param dc our download context
851  */
852 void
853 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc);
854
855
856 /**
857  * Start download probes for the given search result.
858  *
859  * @param sr the search result
860  */
861 void
862 GNUNET_FS_search_start_probe_ (struct GNUNET_FS_SearchResult *sr);
863
864 /**
865  * Remove serialization/deserialization file from disk.
866  *
867  * @param h master context
868  * @param ext component of the path
869  * @param ent entity identifier
870  */
871 void
872 GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h, const char *ext,
873                              const char *ent);
874
875
876 /**
877  * Remove serialization/deserialization directory from disk.
878  *
879  * @param h master context
880  * @param ext component of the path
881  * @param uni unique name of parent
882  */
883 void
884 GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h, const char *ext,
885                             const char *uni);
886
887
888 /**
889  * Synchronize this file-information struct with its mirror
890  * on disk.  Note that all internal FS-operations that change
891  * file information data should already call "sync" internally,
892  * so this function is likely not useful for clients.
893  *
894  * @param fi the struct to sync
895  */
896 void
897 GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *f);
898
899 /**
900  * Synchronize this publishing struct with its mirror
901  * on disk.  Note that all internal FS-operations that change
902  * publishing structs should already call "sync" internally,
903  * so this function is likely not useful for clients.
904  *
905  * @param pc the struct to sync
906  */
907 void
908 GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc);
909
910 /**
911  * Synchronize this unindex struct with its mirror
912  * on disk.  Note that all internal FS-operations that change
913  * publishing structs should already call "sync" internally,
914  * so this function is likely not useful for clients.
915  *
916  * @param uc the struct to sync
917  */
918 void
919 GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc);
920
921 /**
922  * Synchronize this search struct with its mirror
923  * on disk.  Note that all internal FS-operations that change
924  * publishing structs should already call "sync" internally,
925  * so this function is likely not useful for clients.
926  *
927  * @param sc the struct to sync
928  */
929 void
930 GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc);
931
932 /**
933  * Synchronize this search result with its mirror
934  * on disk.  Note that all internal FS-operations that change
935  * publishing structs should already call "sync" internally,
936  * so this function is likely not useful for clients.
937  *
938  * @param sr the struct to sync
939  */
940 void
941 GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr);
942
943 /**
944  * Synchronize this download struct with its mirror
945  * on disk.  Note that all internal FS-operations that change
946  * publishing structs should already call "sync" internally,
947  * so this function is likely not useful for clients.
948  *
949  * @param dc the struct to sync
950  */
951 void
952 GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc);
953
954 /**
955  * Create SUSPEND event for the given publish operation
956  * and then clean up our state (without stop signal).
957  *
958  * @param cls the 'struct GNUNET_FS_PublishContext' to signal for
959  */
960 void
961 GNUNET_FS_publish_signal_suspend_ (void *cls);
962
963 /**
964  * Create SUSPEND event for the given search operation
965  * and then clean up our state (without stop signal).
966  *
967  * @param cls the 'struct GNUNET_FS_SearchContext' to signal for
968  */
969 void
970 GNUNET_FS_search_signal_suspend_ (void *cls);
971
972 /**
973  * Create SUSPEND event for the given download operation
974  * and then clean up our state (without stop signal).
975  *
976  * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
977  */
978 void
979 GNUNET_FS_download_signal_suspend_ (void *cls);
980
981 /**
982  * Create SUSPEND event for the given unindex operation
983  * and then clean up our state (without stop signal).
984  *
985  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
986  */
987 void
988 GNUNET_FS_unindex_signal_suspend_ (void *cls);
989
990 /**
991  * Function signature of the functions that can be called
992  * to trigger suspend signals and clean-up for top-level
993  * activities.
994  *
995  * @param cls closure
996  */
997 typedef void (*SuspendSignalFunction) (void *cls);
998
999 /**
1000  * We track all of the top-level activities of FS
1001  * so that we can signal 'suspend' on shutdown.
1002  */
1003 struct TopLevelActivity
1004 {
1005   /**
1006    * This is a doubly-linked list.
1007    */
1008   struct TopLevelActivity *next;
1009
1010   /**
1011    * This is a doubly-linked list.
1012    */
1013   struct TopLevelActivity *prev;
1014
1015   /**
1016    * Function to call for suspend-signalling and clean up.
1017    */
1018   SuspendSignalFunction ssf;
1019
1020   /**
1021    * Closure for 'ssf' (some struct GNUNET_FS_XXXHandle*)
1022    */
1023   void *ssf_cls;
1024 };
1025
1026
1027 /**
1028  * Create a top-level activity entry.
1029  *
1030  * @param h global fs handle
1031  * @param ssf suspend signal function to use
1032  * @param ssf_cls closure for ssf
1033  * @return fresh top-level activity handle
1034  */
1035 struct TopLevelActivity *
1036 GNUNET_FS_make_top (struct GNUNET_FS_Handle *h, SuspendSignalFunction ssf,
1037                     void *ssf_cls);
1038
1039
1040 /**
1041  * Destroy a top-level activity entry.
1042  *
1043  * @param h global fs handle
1044  * @param top top level activity entry
1045  */
1046 void
1047 GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top);
1048
1049
1050
1051 /**
1052  * Master context for most FS operations.
1053  */
1054 struct GNUNET_FS_Handle
1055 {
1056   /**
1057    * Configuration to use.
1058    */
1059   const struct GNUNET_CONFIGURATION_Handle *cfg;
1060
1061   /**
1062    * Name of our client.
1063    */
1064   char *client_name;
1065
1066   /**
1067    * Function to call with updates on our progress.
1068    */
1069   GNUNET_FS_ProgressCallback upcb;
1070
1071   /**
1072    * Closure for upcb.
1073    */
1074   void *upcb_cls;
1075
1076   /**
1077    * Head of DLL of top-level activities.
1078    */
1079   struct TopLevelActivity *top_head;
1080
1081   /**
1082    * Tail of DLL of top-level activities.
1083    */
1084   struct TopLevelActivity *top_tail;
1085
1086   /**
1087    * Head of DLL of running jobs.
1088    */
1089   struct GNUNET_FS_QueueEntry *running_head;
1090
1091   /**
1092    * Tail of DLL of running jobs.
1093    */
1094   struct GNUNET_FS_QueueEntry *running_tail;
1095
1096   /**
1097    * Head of DLL of pending jobs.
1098    */
1099   struct GNUNET_FS_QueueEntry *pending_head;
1100
1101   /**
1102    * Tail of DLL of pending jobs.
1103    */
1104   struct GNUNET_FS_QueueEntry *pending_tail;
1105
1106   /**
1107    * Task that processes the jobs in the running and pending queues
1108    * (and moves jobs around as needed).
1109    */
1110   GNUNET_SCHEDULER_TaskIdentifier queue_job;
1111
1112   /**
1113    * Average time we take for a single request to be satisfied.
1114    * FIXME: not yet calcualted properly...
1115    */
1116   struct GNUNET_TIME_Relative avg_block_latency;
1117
1118   /**
1119    * How many actual downloads do we have running right now?
1120    */
1121   unsigned int active_downloads;
1122
1123   /**
1124    * How many blocks do the active downloads have?
1125    */
1126   unsigned int active_blocks;
1127
1128   /**
1129    * General flags.
1130    */
1131   enum GNUNET_FS_Flags flags;
1132
1133   /**
1134    * Maximum number of parallel downloads.
1135    */
1136   unsigned int max_parallel_downloads;
1137
1138   /**
1139    * Maximum number of parallel requests.
1140    */
1141   unsigned int max_parallel_requests;
1142
1143 };
1144
1145
1146 /**
1147  * Handle for controlling a publication process.
1148  */
1149 struct GNUNET_FS_PublishContext
1150 {
1151   /**
1152    * Handle to the global fs context.
1153    */
1154   struct GNUNET_FS_Handle *h;
1155
1156   /**
1157    * Our top-level activity entry (if we are top-level, otherwise NULL).
1158    */
1159   struct TopLevelActivity *top;
1160
1161   /**
1162    * File-structure that is being shared.
1163    */
1164   struct GNUNET_FS_FileInformation *fi;
1165
1166   /**
1167    * Namespace that we are publishing in, NULL if we have no namespace.
1168    */
1169   struct GNUNET_FS_Namespace *ns;
1170
1171   /**
1172    * ID of the content in the namespace, NULL if we have no namespace.
1173    */
1174   char *nid;
1175
1176   /**
1177    * ID for future updates, NULL if we have no namespace or no updates.
1178    */
1179   char *nuid;
1180
1181   /**
1182    * Filename used for serializing information about this operation
1183    * (should be determined using 'mktemp').
1184    */
1185   char *serialization;
1186
1187   /**
1188    * Our own client handle for the FS service; only briefly used when
1189    * we start to index a file, otherwise NULL.
1190    */
1191   struct GNUNET_CLIENT_Connection *client;
1192
1193   /**
1194    * Current position in the file-tree for the upload.
1195    */
1196   struct GNUNET_FS_FileInformation *fi_pos;
1197
1198   /**
1199    * Non-null if we are currently hashing a file.
1200    */
1201   struct GNUNET_CRYPTO_FileHashContext *fhc;
1202
1203   /**
1204    * Connection to the datastore service.
1205    */
1206   struct GNUNET_DATASTORE_Handle *dsh;
1207
1208   /**
1209    * Queue entry for reservation/unreservation.
1210    */
1211   struct GNUNET_DATASTORE_QueueEntry *qre;
1212
1213   /**
1214    * Context for SKS publishing operation that is part of this publishing operation
1215    * (NULL if not active).
1216    */
1217   struct GNUNET_FS_PublishSksContext *sks_pc;
1218
1219   /**
1220    * Context for KSK publishing operation that is part of this publishing operation
1221    * (NULL if not active).
1222    */
1223   struct GNUNET_FS_PublishKskContext *ksk_pc;
1224
1225   /**
1226    * ID of the task performing the upload. NO_TASK if the upload has
1227    * completed.
1228    */
1229   GNUNET_SCHEDULER_TaskIdentifier upload_task;
1230
1231   /**
1232    * Storage space to reserve for the operation.
1233    */
1234   uint64_t reserve_space;
1235
1236   /**
1237    * Overall number of entries to reserve for the
1238    * publish operation.
1239    */
1240   uint32_t reserve_entries;
1241
1242   /**
1243    * Options for publishing.
1244    */
1245   enum GNUNET_FS_PublishOptions options;
1246
1247   /**
1248    * Space reservation ID with datastore service
1249    * for this upload.
1250    */
1251   int rid;
1252
1253   /**
1254    * Set to GNUNET_YES if all processing has completed.
1255    */
1256   int all_done;
1257   
1258   /**
1259    * Flag set to GNUNET_YES if the next callback from
1260    * GNUNET_FS_file_information_inspect should be skipped because it
1261    * is for the directory which was already processed with the parent.
1262    */
1263   int skip_next_fi_callback;
1264 };
1265
1266
1267 /**
1268  * Phases of unindex processing (state machine).
1269  */
1270 enum UnindexState
1271 {
1272   /**
1273    * We're currently hashing the file.
1274    */
1275   UNINDEX_STATE_HASHING = 0,
1276
1277   /**
1278    * We're telling the datastore to delete
1279    * the respective DBlocks and IBlocks.
1280    */
1281   UNINDEX_STATE_DS_REMOVE = 1,
1282   
1283   /**
1284    * Find out which keywords apply.
1285    */
1286   UNINDEX_STATE_EXTRACT_KEYWORDS = 2,
1287
1288   /**
1289    * We're telling the datastore to remove KBlocks.
1290    */
1291   UNINDEX_STATE_DS_REMOVE_KBLOCKS = 3,
1292
1293   /**
1294    * We're notifying the FS service about
1295    * the unindexing.
1296    */
1297   UNINDEX_STATE_FS_NOTIFY = 4,
1298   
1299   /**
1300    * We're done.
1301    */
1302   UNINDEX_STATE_COMPLETE = 5,
1303   
1304   /**
1305    * We've encountered a fatal error.
1306    */
1307   UNINDEX_STATE_ERROR = 6
1308 };
1309
1310
1311 /**
1312  * Handle for controlling an unindexing operation.
1313  */
1314 struct GNUNET_FS_UnindexContext
1315 {
1316
1317   /**
1318    * The content hash key of the last block we processed, will in the
1319    * end be set to the CHK from the URI.  Used to remove the KBlocks.
1320    */
1321   struct ContentHashKey chk; 
1322
1323   /**
1324    * Global FS context.
1325    */
1326   struct GNUNET_FS_Handle *h;
1327
1328   /**
1329    * Our top-level activity entry.
1330    */
1331   struct TopLevelActivity *top;
1332
1333   /**
1334    * Directory scanner to find keywords (KBlock removal).
1335    */
1336   struct GNUNET_FS_DirScanner *dscan;
1337
1338   /**
1339    * Keywords found (telling us which KBlocks to remove).
1340    */
1341   struct GNUNET_FS_Uri *ksk_uri;
1342
1343   /**
1344    * Current offset in KSK removal.
1345    */
1346   uint32_t ksk_offset;
1347
1348   /**
1349    * Name of the file that we are unindexing.
1350    */
1351   char *filename;
1352
1353   /**
1354    * Short name under which we are serializing the state of this operation.
1355    */
1356   char *serialization;
1357
1358   /**
1359    * Connection to the FS service, only valid during the
1360    * UNINDEX_STATE_FS_NOTIFY phase.
1361    */
1362   struct GNUNET_CLIENT_Connection *client;
1363
1364   /**
1365    * Connection to the datastore service, only valid during the
1366    * UNINDEX_STATE_DS_NOTIFY phase.
1367    */
1368   struct GNUNET_DATASTORE_Handle *dsh;
1369
1370   /**
1371    * Pointer kept for the client.
1372    */
1373   void *client_info;
1374
1375   /**
1376    * Merkle-ish tree encoder context.
1377    */
1378   struct GNUNET_FS_TreeEncoder *tc;
1379
1380   /**
1381    * Handle used to read the file.
1382    */
1383   struct GNUNET_DISK_FileHandle *fh;
1384
1385   /**
1386    * Handle to datastore 'get_key' operation issued for
1387    * obtaining KBlocks.
1388    */
1389   struct GNUNET_DATASTORE_QueueEntry *dqe;
1390
1391   /**
1392    * Current key for decrypting KBLocks from 'get_key' operation.
1393    */
1394   struct GNUNET_HashCode key;
1395
1396   /**
1397    * Current query of 'get_key' operation.
1398    */
1399   struct GNUNET_HashCode query;
1400
1401   /**
1402    * First content UID, 0 for none.
1403    */
1404   uint64_t first_uid;
1405
1406   /**
1407    * Error message, NULL on success.
1408    */
1409   char *emsg;
1410
1411   /**
1412    * Context for hashing of the file.
1413    */
1414   struct GNUNET_CRYPTO_FileHashContext *fhc;
1415
1416   /**
1417    * Overall size of the file.
1418    */
1419   uint64_t file_size;
1420
1421   /**
1422    * Random offset given to 'GNUNET_DATASTORE_get_key'.
1423    */
1424   uint64_t roff;
1425
1426   /**
1427    * When did we start?
1428    */
1429   struct GNUNET_TIME_Absolute start_time;
1430
1431   /**
1432    * Hash of the file's contents (once computed).
1433    */
1434   struct GNUNET_HashCode file_id;
1435
1436   /**
1437    * Current operatinonal phase.
1438    */
1439   enum UnindexState state;
1440
1441 };
1442
1443
1444 /**
1445  * Information we keep for each keyword in
1446  * a keyword search.
1447  */
1448 struct SearchRequestEntry
1449 {
1450   /**
1451    * Hash of the original keyword, also known as the
1452    * key (for decrypting the KBlock).
1453    */
1454   struct GNUNET_HashCode key;
1455
1456   /**
1457    * Hash of the public key, also known as the query.
1458    */
1459   struct GNUNET_HashCode query;
1460
1461   /**
1462    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1463    * was found under this keyword.  Note that the entries will point
1464    * to the same locations as those in the master result map (in
1465    * "struct GNUNET_FS_SearchContext"), so they should not be freed.
1466    * The key for each entry is the XOR of the key and query in the CHK
1467    * URI (as a unique identifier for the search result).
1468    */
1469   struct GNUNET_CONTAINER_MultiHashMap *results;
1470
1471   /**
1472    * Is this keyword a mandatory keyword
1473    * (started with '+')?
1474    */
1475   int mandatory;
1476
1477 };
1478
1479
1480 /**
1481  * Handle for controlling a search.
1482  */
1483 struct GNUNET_FS_SearchContext
1484 {
1485   /**
1486    * Handle to the global FS context.
1487    */
1488   struct GNUNET_FS_Handle *h;
1489
1490   /**
1491    * Our top-level activity entry (if we are top-level, otherwise NULL).
1492    */
1493   struct TopLevelActivity *top;
1494
1495   /**
1496    * List of keywords that we're looking for.
1497    */
1498   struct GNUNET_FS_Uri *uri;
1499
1500   /**
1501    * For update-searches, link to the search result that triggered
1502    * the update search; otherwise NULL.
1503    */
1504   struct GNUNET_FS_SearchResult *psearch_result;
1505
1506   /**
1507    * Connection to the FS service.
1508    */
1509   struct GNUNET_CLIENT_Connection *client;
1510
1511   /**
1512    * Pointer we keep for the client.
1513    */
1514   void *client_info;
1515
1516   /**
1517    * Name of the file on disk we use for persistence.
1518    */
1519   char *serialization;
1520
1521   /**
1522    * Error message (non-NULL if this operation failed).
1523    */
1524   char *emsg;
1525
1526   /**
1527    * Map that contains a "struct GNUNET_FS_SearchResult" for each result that
1528    * was found in the search.  The key for each entry is the XOR of
1529    * the key and query in the CHK URI (as a unique identifier for the
1530    * search result).
1531    */
1532   struct GNUNET_CONTAINER_MultiHashMap *master_result_map;
1533
1534   /**
1535    * Per-keyword information for a keyword search.  This array will
1536    * have exactly as many entries as there were keywords.
1537    */
1538   struct SearchRequestEntry *requests;
1539
1540   /**
1541    * When did we start?
1542    */
1543   struct GNUNET_TIME_Absolute start_time;
1544
1545   /**
1546    * How long to wait before we try to reconnect to FS service?
1547    */
1548   struct GNUNET_TIME_Relative reconnect_backoff;
1549
1550   /**
1551    * ID of a task that is using this struct and that must be cancelled
1552    * when the search is being stopped (if not
1553    * GNUNET_SCHEDULER_NO_TASK).  Used for the task that adds some
1554    * artificial delay when trying to reconnect to the FS service.
1555    */
1556   GNUNET_SCHEDULER_TaskIdentifier task;
1557
1558   /**
1559    * How many of the entries in the search request
1560    * map have been passed to the service so far?
1561    */
1562   unsigned int search_request_map_offset;
1563
1564   /**
1565    * How many of the keywords in the KSK
1566    * map have been passed to the service so far?
1567    */
1568   unsigned int keyword_offset;
1569
1570   /**
1571    * Anonymity level for the search.
1572    */
1573   uint32_t anonymity;
1574
1575   /**
1576    * Number of mandatory keywords in this query.
1577    */
1578   uint32_t mandatory_count;
1579
1580   /**
1581    * Options for the search.
1582    */
1583   enum GNUNET_FS_SearchOptions options;
1584 };
1585
1586
1587 /**
1588  * FSM for possible states a block can go through.  The typical
1589  * order of progression is linear through the states, alternatives
1590  * are documented in the comments.
1591  */
1592 enum BlockRequestState
1593 {
1594     /**
1595      * Initial state, block has only been allocated (since it is
1596      * relevant to the overall download request).
1597      */
1598   BRS_INIT = 0,
1599
1600     /**
1601      * We've checked the block on the path down the tree, and the
1602      * content on disk did match the desired CHK, but not all
1603      * the way down, so at the bottom some blocks will still
1604      * need to be reconstructed).
1605      */
1606   BRS_RECONSTRUCT_DOWN = 1,
1607
1608     /**
1609      * We've calculated the CHK bottom-up based on the meta data.
1610      * This may work, but if it did we have to write the meta data to
1611      * disk at the end (and we still need to check against the
1612      * CHK set on top).
1613      */
1614   BRS_RECONSTRUCT_META_UP = 2,
1615
1616     /**
1617      * We've calculated the CHK bottom-up based on what we have on
1618      * disk, which may not be what the desired CHK is.  If the
1619      * reconstructed CHKs match whatever comes from above, we're
1620      * done with the respective subtree.
1621      */
1622   BRS_RECONSTRUCT_UP = 3,
1623
1624     /**
1625      * We've determined the real, desired CHK for this block
1626      * (full tree reconstruction failed), request is now pending.
1627      * If the CHK that bubbled up through reconstruction did match
1628      * the top-level request, the state machine for the subtree
1629      * would have moved to BRS_DOWNLOAD_UP.
1630      */
1631   BRS_CHK_SET = 4,
1632
1633     /**
1634      * We've successfully downloaded this block, but the children
1635      * still need to be either downloaded or verified (download
1636      * request propagates down).  If the download fails, the
1637      * state machine for this block may move to
1638      * BRS_DOWNLOAD_ERROR instead.
1639      */
1640   BRS_DOWNLOAD_DOWN = 5,
1641
1642     /**
1643      * This block and all of its children have been downloaded
1644      * successfully (full completion propagates up).
1645      */
1646   BRS_DOWNLOAD_UP = 6,
1647
1648     /**
1649      * We got a block back that matched the query but did not hash to
1650      * the key (malicious publisher or hash collision); this block
1651      * can never be downloaded (error propagates up).
1652      */
1653   BRS_ERROR = 7
1654 };
1655
1656
1657 /**
1658  * Information about an active download request.
1659  */
1660 struct DownloadRequest
1661 {
1662   /**
1663    * While pending, we keep all download requests in a doubly-linked list.
1664    */
1665   struct DownloadRequest *next;
1666
1667   /**
1668    * While pending, we keep all download requests in a doubly-linked list.
1669    */
1670   struct DownloadRequest *prev;
1671
1672   /**
1673    * Parent in the CHK-tree.
1674    */
1675   struct DownloadRequest *parent;
1676
1677   /**
1678    * Array (!) of child-requests, or NULL for the bottom of the tree.
1679    */
1680   struct DownloadRequest **children;
1681
1682   /**
1683    * CHK for the request for this block (set during reconstruction
1684    * to what we have on disk, later to what we want to have).
1685    */
1686   struct ContentHashKey chk;
1687
1688   /**
1689    * Offset of the corresponding block.  Specifically, first (!) byte of
1690    * the first DBLOCK in the subtree induced by block represented by
1691    * this request.
1692    */
1693   uint64_t offset;
1694
1695   /**
1696    * Number of entries in 'children' array.
1697    */
1698   unsigned int num_children;
1699
1700   /**
1701    * Depth of the corresponding block in the tree.  0==DBLOCKs.
1702    */
1703   unsigned int depth;
1704
1705   /**
1706    * Offset of the CHK for this block in the parent block
1707    */
1708   unsigned int chk_idx;
1709
1710   /**
1711    * State in the FSM.
1712    */
1713   enum BlockRequestState state;
1714
1715   /**
1716    * GNUNET_YES if this entry is in the pending list.
1717    */
1718   int is_pending;
1719
1720 };
1721
1722
1723 /**
1724  * (recursively) free download request structure
1725  *
1726  * @param dr request to free
1727  */
1728 void
1729 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr);
1730
1731
1732 /**
1733  * Context for controlling a download.
1734  */
1735 struct GNUNET_FS_DownloadContext
1736 {
1737
1738   /**
1739    * Global FS context.
1740    */
1741   struct GNUNET_FS_Handle *h;
1742
1743   /**
1744    * Our top-level activity entry (if we are top-level, otherwise NULL).
1745    */
1746   struct TopLevelActivity *top;
1747
1748   /**
1749    * Connection to the FS service.
1750    */
1751   struct GNUNET_CLIENT_Connection *client;
1752
1753   /**
1754    * Parent download (used when downloading files
1755    * in directories).
1756    */
1757   struct GNUNET_FS_DownloadContext *parent;
1758
1759   /**
1760    * Associated search (used when downloading files
1761    * based on search results), or NULL for none.
1762    */
1763   struct GNUNET_FS_SearchResult *search;
1764
1765   /**
1766    * Head of list of child downloads.
1767    */
1768   struct GNUNET_FS_DownloadContext *child_head;
1769
1770   /**
1771    * Tail of list of child downloads.
1772    */
1773   struct GNUNET_FS_DownloadContext *child_tail;
1774
1775   /**
1776    * Previous download belonging to the same parent.
1777    */
1778   struct GNUNET_FS_DownloadContext *prev;
1779
1780   /**
1781    * Next download belonging to the same parent.
1782    */
1783   struct GNUNET_FS_DownloadContext *next;
1784
1785   /**
1786    * Context kept for the client.
1787    */
1788   void *client_info;
1789
1790   /**
1791    * URI that identifies the file that we are downloading.
1792    */
1793   struct GNUNET_FS_Uri *uri;
1794
1795   /**
1796    * Known meta-data for the file (can be NULL).
1797    */
1798   struct GNUNET_CONTAINER_MetaData *meta;
1799
1800   /**
1801    * Error message, NULL if we're doing OK.
1802    */
1803   char *emsg;
1804
1805   /**
1806    * Random portion of filename we use for syncing state of this
1807    * download.
1808    */
1809   char *serialization;
1810
1811   /**
1812    * Where are we writing the data (name of the
1813    * file, can be NULL!).
1814    */
1815   char *filename;
1816
1817   /**
1818    * Where are we writing the data temporarily (name of the
1819    * file, can be NULL!); used if we do not have a permanent
1820    * name and we are a directory and we do a recursive download.
1821    */
1822   char *temp_filename;
1823
1824   /**
1825    * Our entry in the job queue.
1826    */
1827   struct GNUNET_FS_QueueEntry *job_queue;
1828
1829   /**
1830    * Non-NULL if we are currently having a request for
1831    * transmission pending with the client handle.
1832    */
1833   struct GNUNET_CLIENT_TransmitHandle *th;
1834
1835   /**
1836    * Tree encoder used for the reconstruction.
1837    */
1838   struct GNUNET_FS_TreeEncoder *te;
1839
1840   /**
1841    * File handle for reading data from an existing file
1842    * (to pass to tree encoder).
1843    */
1844   struct GNUNET_DISK_FileHandle *rfh;
1845
1846   /**
1847    * Map of active requests (those waiting for a response).  The key
1848    * is the hash of the encryped block (aka query).
1849    */
1850   struct GNUNET_CONTAINER_MultiHashMap *active;
1851
1852   /**
1853    * Head of linked list of pending requests.
1854    */
1855   struct DownloadRequest *pending_head;
1856
1857   /**
1858    * Head of linked list of pending requests.
1859    */
1860   struct DownloadRequest *pending_tail;
1861
1862   /**
1863    * Top-level download request.
1864    */
1865   struct DownloadRequest *top_request;
1866
1867   /**
1868    * Identity of the peer having the content, or all-zeros
1869    * if we don't know of such a peer.
1870    */
1871   struct GNUNET_PeerIdentity target;
1872
1873   /**
1874    * ID of a task that is using this struct and that must be cancelled
1875    * when the download is being stopped (if not
1876    * GNUNET_SCHEDULER_NO_TASK).  Used for the task that adds some
1877    * artificial delay when trying to reconnect to the FS service or
1878    * the task processing incrementally the data on disk, or the
1879    * task requesting blocks, etc.
1880    */
1881   GNUNET_SCHEDULER_TaskIdentifier task;
1882
1883   /**
1884    * What is the first offset that we're interested
1885    * in?
1886    */
1887   uint64_t offset;
1888
1889   /**
1890    * How many bytes starting from offset are desired?
1891    * This is NOT the overall length of the file!
1892    */
1893   uint64_t length;
1894
1895   /**
1896    * How many bytes have we already received within
1897    * the specified range (DBlocks only).
1898    */
1899   uint64_t completed;
1900
1901   /**
1902    * What was the size of the file on disk that we're downloading
1903    * before we started?  Used to detect if there is a point in
1904    * checking an existing block on disk for matching the desired
1905    * content.  0 if the file did not exist already.
1906    */
1907   uint64_t old_file_size;
1908
1909   /**
1910    * Time download was started.
1911    */
1912   struct GNUNET_TIME_Absolute start_time;
1913
1914   /**
1915    * How long to wait before we try to reconnect to FS service?
1916    */
1917   struct GNUNET_TIME_Relative reconnect_backoff;
1918
1919   /**
1920    * Desired level of anonymity.
1921    */
1922   uint32_t anonymity;
1923
1924   /**
1925    * The depth of the file-tree.
1926    */
1927   unsigned int treedepth;
1928
1929   /**
1930    * Options for the download.
1931    */
1932   enum GNUNET_FS_DownloadOptions options;
1933
1934   /**
1935    * Flag set upon transitive completion (includes child downloads).
1936    * This flag is only set to GNUNET_YES for directories where all
1937    * child-downloads have also completed (and signalled completion).
1938    */
1939   int has_finished;
1940
1941   /**
1942    * Have we started the receive continuation yet?
1943    */
1944   int in_receive;
1945
1946   /**
1947    * Are we ready to issue requests (reconstructions are finished)?
1948    */
1949   int issue_requests;
1950
1951 };
1952
1953
1954 /**
1955  * Information about an (updateable) node in the
1956  * namespace.
1957  */
1958 struct NamespaceUpdateNode
1959 {
1960   /**
1961    * Identifier for this node.
1962    */
1963   char *id;
1964
1965   /**
1966    * Identifier of children of this node.
1967    */
1968   char *update;
1969
1970   /**
1971    * Metadata for this entry.
1972    */
1973   struct GNUNET_CONTAINER_MetaData *md;
1974
1975   /**
1976    * URI of this entry in the namespace.
1977    */
1978   struct GNUNET_FS_Uri *uri;
1979
1980   /**
1981    * Namespace update generation ID.  Used to ensure
1982    * freshness of the tree_id.
1983    */
1984   unsigned int nug;
1985
1986   /**
1987    * TREE this entry belongs to (if nug is current).
1988    */
1989   unsigned int tree_id;
1990
1991 };
1992
1993
1994 struct GNUNET_FS_Namespace
1995 {
1996
1997   /**
1998    * Handle to the FS service context.
1999    */
2000   struct GNUNET_FS_Handle *h;
2001
2002   /**
2003    * Array with information about nodes in the namespace.
2004    */
2005   struct NamespaceUpdateNode **update_nodes;
2006
2007   /**
2008    * Private key for the namespace.
2009    */
2010   struct GNUNET_CRYPTO_RsaPrivateKey *key;
2011
2012   /**
2013    * Hash map mapping identifiers of update nodes
2014    * to the update nodes (initialized on-demand).
2015    */
2016   struct GNUNET_CONTAINER_MultiHashMap *update_map;
2017
2018   /**
2019    * Name of the file with the private key.
2020    */
2021   char *filename;
2022
2023   /**
2024    * Name of the namespace.
2025    */
2026   char *name;
2027
2028   /**
2029    * Size of the update nodes array.
2030    */
2031   unsigned int update_node_count;
2032
2033   /**
2034    * Reference counter.
2035    */
2036   unsigned int rc;
2037
2038   /**
2039    * Generator for unique nug numbers.
2040    */
2041   unsigned int nug_gen;
2042 };
2043
2044 #endif
2045
2046 /* end of fs_api.h */