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