f8a20c75b01d85f5f3e6af0f954351c4247bb28e
[oweals/gnunet.git] / src / include / gnunet_fs_service.h
1 /*
2      This file is part of GNUnet
3      (C) 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  * @file include/gnunet_fs_service.h
22  * @brief API for file-sharing via GNUnet
23  * @author Christian Grothoff
24  */
25 #ifndef GNUNET_FS_LIB_H
26 #define GNUNET_FS_LIB_H
27
28 #include "gnunet_util_lib.h"
29 #include "gnunet_scheduler_lib.h"
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 /**
40  * Version number of the implementation.
41  * History:
42  *
43  * 1.x.x: initial version with triple GNUNET_hash and merkle tree
44  * 2.x.x: root node with mime-type, filename and version number
45  * 2.1.x: combined GNUNET_EC_ContentHashKey/3HASH encoding with 25:1 super-nodes
46  * 2.2.x: with directories
47  * 3.0.x: with namespaces
48  * 3.1.x: with namespace meta-data
49  * 3.2.x: with collections
50  * 4.0.x: with expiration, variable meta-data, kblocks
51  * 4.1.x: with new error and configuration handling
52  * 5.0.x: with location URIs
53  * 6.0.0: with support for OR in KSKs
54  * 6.1.x: with simplified namespace support
55  * 9.0.0: CPS-style integrated API
56  * 9.1.1: asynchronous directory scanning
57  */
58 #define GNUNET_FS_VERSION 0x00090101
59
60
61 /* ******************** URI API *********************** */
62
63 #define GNUNET_FS_URI_PREFIX "gnunet://fs/"
64 #define GNUNET_FS_URI_KSK_INFIX "ksk/"
65 #define GNUNET_FS_URI_SKS_INFIX "sks/"
66 #define GNUNET_FS_URI_CHK_INFIX "chk/"
67 #define GNUNET_FS_URI_LOC_INFIX "loc/"
68
69
70 /**
71  * A Universal Resource Identifier (URI), opaque.
72  */
73 struct GNUNET_FS_Uri;
74
75 struct GNUNET_FS_DirScanner;
76
77 /**
78  * Iterator over keywords
79  *
80  * @param cls closure
81  * @param keyword the keyword
82  * @param is_mandatory is the keyword mandatory (in a search)
83  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
84  */
85 typedef int (*GNUNET_FS_KeywordIterator) (void *cls, const char *keyword,
86                                           int is_mandatory);
87
88 /**
89  * Get a unique key from a URI.  This is for putting URIs
90  * into HashMaps.  The key may change between FS implementations.
91  *
92  * @param uri uri to convert to a unique key
93  * @param key wherer to store the unique key
94  */
95 void
96 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri, GNUNET_HashCode * key);
97
98 /**
99  * Convert a URI to a UTF-8 String.
100  *
101  * @param uri uri to convert to a string
102  * @return the UTF-8 string
103  */
104 char *
105 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri);
106
107 /**
108  * Convert keyword URI to a human readable format
109  * (i.e. the search query that was used in the first place)
110  *
111  * @param uri ksk uri to convert to a string
112  * @return string with the keywords
113  */
114 char *
115 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri);
116
117
118 /**
119  * Add the given keyword to the set of keywords represented by the URI.
120  * Does nothing if the keyword is already present.
121  *
122  * @param uri ksk uri to modify
123  * @param keyword keyword to add
124  * @param is_mandatory is this keyword mandatory?
125  */
126 void
127 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri, const char *keyword,
128                                int is_mandatory);
129
130
131 /**
132  * Remove the given keyword from the set of keywords represented by the URI.
133  * Does nothing if the keyword is not present.
134  *
135  * @param uri ksk uri to modify
136  * @param keyword keyword to add
137  */
138 void
139 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
140                                   const char *keyword);
141
142
143 /**
144  * Convert a UTF-8 String to a URI.
145  *
146  * @param uri string to parse
147  * @param emsg where to store the parser error message (if any)
148  * @return NULL on error
149  */
150 struct GNUNET_FS_Uri *
151 GNUNET_FS_uri_parse (const char *uri, char **emsg);
152
153 /**
154  * Free URI.
155  *
156  * @param uri uri to free
157  */
158 void
159 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri);
160
161
162 /**
163  * How many keywords are ANDed in this keyword URI?
164  *
165  * @param uri ksk uri to get the number of keywords from
166  * @return 0 if this is not a keyword URI
167  */
168 unsigned int
169 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri);
170
171
172 /**
173  * Iterate over all keywords in this keyword URI.
174  *
175  * @param uri ksk uri to get the keywords from
176  * @param iterator function to call on each keyword
177  * @param iterator_cls closure for iterator
178  * @return -1 if this is not a keyword URI, otherwise number of
179  *   keywords iterated over until iterator aborted
180  */
181 int
182 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
183                                 GNUNET_FS_KeywordIterator iterator,
184                                 void *iterator_cls);
185
186
187 /**
188  * Obtain the identity of the peer offering the data
189  *
190  * @param uri the location URI to inspect
191  * @param peer where to store the identify of the peer (presumably) offering the content
192  * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
193  */
194 int
195 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
196                                      struct GNUNET_PeerIdentity *peer);
197
198
199 /**
200  * Obtain the URI of the content itself.
201  *
202  * @param uri location URI to get the content URI from
203  * @return NULL if argument is not a location URI
204  */
205 struct GNUNET_FS_Uri *
206 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri);
207
208
209 /**
210  * Obtain the expiration of the LOC URI.
211  *
212  * @param uri location URI to get the expiration from
213  * @return expiration time of the URI
214  */
215 struct GNUNET_TIME_Absolute
216 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri);
217
218
219 /**
220  * Construct a location URI (this peer will be used for the location).
221  *
222  * @param baseUri content offered by the sender
223  * @param cfg configuration information (used to find our hostkey)
224  * @param expiration_time how long will the content be offered?
225  * @return the location URI, NULL on error
226  */
227 struct GNUNET_FS_Uri *
228 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
229                           const struct GNUNET_CONFIGURATION_Handle *cfg,
230                           struct GNUNET_TIME_Absolute expiration_time);
231
232
233 /**
234  * Merge the sets of keywords from two KSK URIs.
235  *
236  * @param u1 first uri
237  * @param u2 second uri
238  * @return merged URI, NULL on error
239  */
240 struct GNUNET_FS_Uri *
241 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
242                          const struct GNUNET_FS_Uri *u2);
243
244
245 /**
246  * Duplicate URI.
247  *
248  * @param uri the URI to duplicate
249  * @return copy of the URI
250  */
251 struct GNUNET_FS_Uri *
252 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri);
253
254
255 /**
256  * Create an FS URI from a single user-supplied string of keywords.
257  * The string is broken up at spaces into individual keywords.
258  * Keywords that start with "+" are mandatory.  Double-quotes can
259  * be used to prevent breaking up strings at spaces (and also
260  * to specify non-mandatory keywords starting with "+").
261  *
262  * Keywords must contain a balanced number of double quotes and
263  * double quotes can not be used in the actual keywords (for
264  * example, the string '""foo bar""' will be turned into two
265  * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
266  *
267  * @param keywords the keyword string
268  * @param emsg where to store an error message
269  * @return an FS URI for the given keywords, NULL
270  *  if keywords is not legal (i.e. empty).
271  */
272 struct GNUNET_FS_Uri *
273 GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg);
274
275
276 /**
277  * Create an FS URI from a user-supplied command line of keywords.
278  * Arguments should start with "+" to indicate mandatory
279  * keywords.
280  *
281  * @param argc number of keywords
282  * @param argv keywords (double quotes are not required for
283  *             keywords containing spaces; however, double
284  *             quotes are required for keywords starting with
285  *             "+"); there is no mechanism for having double
286  *             quotes in the actual keywords (if the user
287  *             did specifically specify double quotes, the
288  *             caller should convert each double quote
289  *             into two single quotes).
290  * @return an FS URI for the given keywords, NULL
291  *  if keywords is not legal (i.e. empty).
292  */
293 struct GNUNET_FS_Uri *
294 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv);
295
296
297 /**
298  * Test if two URIs are equal.
299  *
300  * @param u1 one of the URIs
301  * @param u2 the other URI
302  * @return GNUNET_YES if the URIs are equal
303  */
304 int
305 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
306                           const struct GNUNET_FS_Uri *u2);
307
308
309 /**
310  * Is this a namespace URI?
311  *
312  * @param uri the uri to check
313  * @return GNUNET_YES if this is an SKS uri
314  */
315 int
316 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri);
317
318
319 /**
320  * Handle to one of our namespaces.
321  */
322 struct GNUNET_FS_Namespace;
323
324
325 /**
326  * Create an SKS URI from a namespace and an identifier.
327  *
328  * @param ns namespace
329  * @param id identifier
330  * @param emsg where to store an error message
331  * @return an FS URI for the given namespace and identifier
332  */
333 struct GNUNET_FS_Uri *
334 GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns, const char *id,
335                           char **emsg);
336
337
338 /**
339  * Create an SKS URI from a namespace ID and an identifier.
340  *
341  * @param nsid namespace ID
342  * @param id identifier
343  * @return an FS URI for the given namespace and identifier
344  */
345 struct GNUNET_FS_Uri *
346 GNUNET_FS_uri_sks_create_from_nsid (GNUNET_HashCode * nsid, const char *id);
347
348
349 /**
350  * Get the ID of a namespace from the given
351  * namespace URI.
352  *
353  * @param uri the uri to get the namespace ID from
354  * @param nsid where to store the ID of the namespace
355  * @return GNUNET_OK on success
356  */
357 int
358 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
359                                  GNUNET_HashCode * nsid);
360
361
362 /**
363  * Get the content identifier of an SKS URI.
364  *
365  * @param uri the sks uri
366  * @return NULL on error (not a valid SKS URI)
367  */
368 char *
369 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri);
370
371
372 /**
373  * Convert namespace URI to a human readable format
374  * (using the namespace description, if available).
375  *
376  * @param cfg configuration to use
377  * @param uri SKS uri to convert
378  * @return NULL on error (not an SKS URI)
379  */
380 char *
381 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
382                                    const struct GNUNET_FS_Uri *uri);
383
384
385 /**
386  * Is this a keyword URI?
387  *
388  * @param uri the uri
389  * @return GNUNET_YES if this is a KSK uri
390  */
391 int
392 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri);
393
394
395 /**
396  * Is this a file (or directory) URI?
397  *
398  * @param uri the uri to check
399  * @return GNUNET_YES if this is a CHK uri
400  */
401 int
402 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri);
403
404
405 /**
406  * What is the size of the file that this URI
407  * refers to?
408  *
409  * @param uri the CHK (or LOC) URI to inspect
410  * @return size of the file as specified in the CHK URI
411  */
412 uint64_t
413 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri);
414
415
416 /**
417  * Is this a location URI?
418  *
419  * @param uri the uri to check
420  * @return GNUNET_YES if this is a LOC uri
421  */
422 int
423 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri);
424
425
426 /**
427  * Construct a keyword-URI from meta-data (take all entries
428  * in the meta-data and construct one large keyword URI
429  * that lists all keywords that can be found in the meta-data).
430  *
431  * @param md metadata to use
432  * @return NULL on error, otherwise a KSK URI
433  */
434 struct GNUNET_FS_Uri *
435 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
436                                          *md);
437
438
439 /* ******************** command-line option parsing API *********************** */
440
441 /**
442  * Command-line option parser function that allows the user
443  * to specify one or more '-k' options with keywords.  Each
444  * specified keyword will be added to the URI.  A pointer to
445  * the URI must be passed as the "scls" argument.
446  *
447  * @param ctx command line processor context
448  * @param scls must be of type "struct GNUNET_FS_Uri **"
449  * @param option name of the option (typically 'k')
450  * @param value command line argument given
451  * @return GNUNET_OK on success
452  */
453 int
454 GNUNET_FS_getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext
455                                *ctx, void *scls, const char *option,
456                                const char *value);
457
458
459 /**
460  * Command-line option parser function that allows the user to specify
461  * one or more '-m' options with metadata.  Each specified entry of
462  * the form "type=value" will be added to the metadata.  A pointer to
463  * the metadata must be passed as the "scls" argument.
464  *
465  * @param ctx command line processor context
466  * @param scls must be of type "struct GNUNET_CONTAINER_MetaData **"
467  * @param option name of the option (typically 'k')
468  * @param value command line argument given
469  * @return GNUNET_OK on success
470  */
471 int
472 GNUNET_FS_getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext
473                                *ctx, void *scls, const char *option,
474                                const char *value);
475
476
477
478 /* ************************* sharing API ***************** */
479
480
481 /**
482  * Possible status codes used in the callback for the
483  * various file-sharing operations.  On each file (or search),
484  * the callback is guaranteed to be called once with "START"
485  * and once with STOPPED; calls with PROGRESS, ERROR or COMPLETED
486  * are optional and depend on the circumstances; parent operations
487  * will be STARTED before child-operations and STOPPED after
488  * their respective child-operations.  START and STOP signals
489  * are typically generated either due to explicit client requests
490  * or because of suspend/resume operations.
491  */
492 enum GNUNET_FS_Status
493 {
494   /**
495    * Notification that we have started to publish a file structure.
496    */
497   GNUNET_FS_STATUS_PUBLISH_START = 0,
498
499   /**
500    * Notification that we have resumed sharing a file structure.
501    */
502   GNUNET_FS_STATUS_PUBLISH_RESUME = 1,
503
504   /**
505    * Notification that we have suspended sharing a file structure.
506    */
507   GNUNET_FS_STATUS_PUBLISH_SUSPEND = 2,
508
509   /**
510    * Notification that we are making progress sharing a file structure.
511    */
512   GNUNET_FS_STATUS_PUBLISH_PROGRESS = 3,
513
514   /**
515    * Notification that an error was encountered  sharing a file structure.
516    * The application will continue to receive resume/suspend events for
517    * this structure until "GNUNET_FS_publish_stop" is called.
518    */
519   GNUNET_FS_STATUS_PUBLISH_ERROR = 4,
520
521   /**
522    * Notification that we completed sharing a file structure.
523    * The application will continue to receive resume/suspend events for
524    * this structure until "GNUNET_FS_publish_stop" is called.
525    */
526   GNUNET_FS_STATUS_PUBLISH_COMPLETED = 5,
527
528   /**
529    * Notification that we have stopped
530    * the process of uploading a file structure; no
531    * futher events will be generated for this action.
532    */
533   GNUNET_FS_STATUS_PUBLISH_STOPPED = 6,
534
535   /**
536    * Notification that we have started this download.
537    */
538   GNUNET_FS_STATUS_DOWNLOAD_START = 7,
539
540   /**
541    * Notification that this download is being resumed.
542    */
543   GNUNET_FS_STATUS_DOWNLOAD_RESUME = 8,
544
545   /**
546    * Notification that this download was suspended.
547    */
548   GNUNET_FS_STATUS_DOWNLOAD_SUSPEND = 9,
549
550   /**
551    * Notification about progress with this download.
552    */
553   GNUNET_FS_STATUS_DOWNLOAD_PROGRESS = 10,
554
555   /**
556    * Notification that this download encountered an error.
557    */
558   GNUNET_FS_STATUS_DOWNLOAD_ERROR = 11,
559
560   /**
561    * Notification that this download completed.  Note that for
562    * directories, completion does not imply completion of all files in
563    * the directory.
564    */
565   GNUNET_FS_STATUS_DOWNLOAD_COMPLETED = 12,
566
567   /**
568    * Notification that this download was stopped
569    * (final event with respect to this action).
570    */
571   GNUNET_FS_STATUS_DOWNLOAD_STOPPED = 13,
572
573   /**
574    * Notification that this download is now actively being
575    * pursued (as opposed to waiting in the queue).
576    */
577   GNUNET_FS_STATUS_DOWNLOAD_ACTIVE = 14,
578
579   /**
580    * Notification that this download is no longer actively
581    * being pursued (back in the queue).
582    */
583   GNUNET_FS_STATUS_DOWNLOAD_INACTIVE = 15,
584
585   /**
586    * Notification that this download is no longer part of a
587    * recursive download or search but now a 'stand-alone'
588    * download (and may thus need to be moved in the GUI
589    * into a different category).
590    */
591   GNUNET_FS_STATUS_DOWNLOAD_LOST_PARENT = 16,
592
593   /**
594    * First event generated when a client requests
595    * a search to begin or when a namespace result
596    * automatically triggers the search for updates.
597    */
598   GNUNET_FS_STATUS_SEARCH_START = 17,
599
600   /**
601    * Last event when a search is being resumed;
602    * note that "GNUNET_FS_SEARCH_START" will not
603    * be generated in this case.
604    */
605   GNUNET_FS_STATUS_SEARCH_RESUME = 18,
606
607   /**
608    * Event generated for each search result
609    * when the respective search is resumed.
610    */
611   GNUNET_FS_STATUS_SEARCH_RESUME_RESULT = 19,
612
613   /**
614    * Last event when a search is being suspended;
615    * note that "GNUNET_FS_SEARCH_STOPPED" will not
616    * be generated in this case.
617    */
618   GNUNET_FS_STATUS_SEARCH_SUSPEND = 20,
619
620   /**
621    * This search has yielded a result.
622    */
623   GNUNET_FS_STATUS_SEARCH_RESULT = 21,
624
625   /**
626    * We have discovered a new namespace.
627    */
628   GNUNET_FS_STATUS_SEARCH_RESULT_NAMESPACE = 22,
629
630   /**
631    * We have additional data about the quality
632    * or availability of a search result.
633    */
634   GNUNET_FS_STATUS_SEARCH_UPDATE = 23,
635
636   /**
637    * Signals a problem with this search.
638    */
639   GNUNET_FS_STATUS_SEARCH_ERROR = 24,
640
641   /**
642    * Signals that this search was paused.
643    */
644   GNUNET_FS_STATUS_SEARCH_PAUSED = 25,
645
646   /**
647    * Signals that this search was continued (unpaused).
648    */
649   GNUNET_FS_STATUS_SEARCH_CONTINUED = 26,
650
651   /**
652    * Event generated for each search result
653    * when the respective search is stopped.
654    */
655   GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED = 27,
656
657   /**
658    * Event generated for each search result
659    * when the respective search is suspended.
660    */
661   GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND = 28,
662
663   /**
664    * Last message from a search; this signals
665    * that there will be no further events associated
666    * with this search.
667    */
668   GNUNET_FS_STATUS_SEARCH_STOPPED = 29,
669
670   /**
671    * Notification that we started to unindex a file.
672    */
673   GNUNET_FS_STATUS_UNINDEX_START = 30,
674
675   /**
676    * Notification that we resumed unindexing of a file.
677    */
678   GNUNET_FS_STATUS_UNINDEX_RESUME = 31,
679
680   /**
681    * Notification that we suspended unindexing a file.
682    */
683   GNUNET_FS_STATUS_UNINDEX_SUSPEND = 32,
684
685   /**
686    * Notification that we made progress unindexing a file.
687    */
688   GNUNET_FS_STATUS_UNINDEX_PROGRESS = 33,
689
690   /**
691    * Notification that we encountered an error unindexing
692    * a file.
693    */
694   GNUNET_FS_STATUS_UNINDEX_ERROR = 34,
695
696   /**
697    * Notification that the unindexing of this file
698    * was completed.
699    */
700   GNUNET_FS_STATUS_UNINDEX_COMPLETED = 35,
701
702   /**
703    * Notification that the unindexing of this file
704    * was stopped (final event for this action).
705    */
706   GNUNET_FS_STATUS_UNINDEX_STOPPED = 36
707 };
708
709
710 /**
711  * Handle for controlling an upload.
712  */
713 struct GNUNET_FS_PublishContext;
714
715
716 /**
717  * Handle for controlling an unindexing operation.
718  */
719 struct GNUNET_FS_UnindexContext;
720
721
722 /**
723  * Handle for controlling a search.
724  */
725 struct GNUNET_FS_SearchContext;
726
727
728 /**
729  * Result from a search.  Opaque handle to refer to the search
730  * (typically used when starting a download associated with the search
731  * result).
732  */
733 struct GNUNET_FS_SearchResult;
734
735
736 /**
737  * Context for controlling a download.
738  */
739 struct GNUNET_FS_DownloadContext;
740
741
742 /**
743  * Handle for detail information about a file that is being publishd.
744  * Specifies metadata, keywords, how to get the contents of the file
745  * (i.e. data-buffer in memory, filename on disk) and other options.
746  */
747 struct GNUNET_FS_FileInformation;
748
749
750 /**
751  * Argument given to the progress callback with
752  * information about what is going on.
753  */
754 struct GNUNET_FS_ProgressInfo
755 {
756
757   /**
758    * Values that depend on the event type.
759    */
760   union
761   {
762
763     /**
764      * Values for all "GNUNET_FS_STATUS_PUBLISH_*" events.
765      */
766     struct
767     {
768
769       /**
770        * Context for controlling the upload.
771        */
772       struct GNUNET_FS_PublishContext *pc;
773
774       /**
775        * Information about the file that is being publishd.
776        */
777       const struct GNUNET_FS_FileInformation *fi;
778
779       /**
780        * Client context pointer (set the last time by the client for
781        * this operation; initially NULL on START/RESUME events).
782        */
783       void *cctx;
784
785       /**
786        * Client context pointer for the parent operation
787        * (if this is a file in a directory or a subdirectory).
788        */
789       void *pctx;
790
791       /**
792        * Name of the file being published; can be NULL.
793        */
794       const char *filename;
795
796       /**
797        * How large is the file overall?  For directories,
798        * this is only the size of the directory itself,
799        * not of the other files contained within the
800        * directory.
801        */
802       uint64_t size;
803
804       /**
805        * At what time do we expect to finish the upload?
806        * (will be a value in the past for completed
807        * uploads).
808        */
809       struct GNUNET_TIME_Relative eta;
810
811       /**
812        * How long has this upload been actively running
813        * (excludes times where the upload was suspended).
814        */
815       struct GNUNET_TIME_Relative duration;
816
817       /**
818        * How many bytes have we completed?
819        */
820       uint64_t completed;
821
822       /**
823        * What anonymity level is used for this upload?
824        */
825       uint32_t anonymity;
826
827       /**
828        * Additional values for specific events.
829        */
830       union
831       {
832
833         /**
834          * These values are only valid for
835          * GNUNET_FS_STATUS_PUBLISH_PROGRESS events.
836          */
837         struct
838         {
839
840           /**
841            * Data block we just published.
842            */
843           const void *data;
844
845           /**
846            * At what offset in the file is "data"?
847            */
848           uint64_t offset;
849
850           /**
851            * Length of the data block.
852            */
853           uint64_t data_len;
854
855           /**
856            * Depth of the given block in the tree;
857            * 0 would be the lowest level (DBLOCKs).
858            */
859           unsigned int depth;
860
861         } progress;
862
863         /**
864          * These values are only valid for
865          * GNUNET_FS_STATUS_PUBLISH_RESUME events.
866          */
867         struct
868         {
869
870           /**
871            * Error message, NULL if no error was encountered so far.
872            */
873           const char *message;
874
875           /**
876            * URI of the file (if the download had been completed)
877            */
878           const struct GNUNET_FS_Uri *chk_uri;
879
880         } resume;
881
882         /**
883          * These values are only valid for
884          * GNUNET_FS_STATUS_PUBLISH_COMPLETED events.
885          */
886         struct
887         {
888
889           /**
890            * URI of the file.
891            */
892           const struct GNUNET_FS_Uri *chk_uri;
893
894         } completed;
895
896         /**
897          * These values are only valid for
898          * GNUNET_FS_STATUS_PUBLISH_ERROR events.
899          */
900         struct
901         {
902
903           /**
904            * Error message, never NULL.
905            */
906           const char *message;
907
908         } error;
909
910       } specifics;
911
912     } publish;
913
914
915     /**
916      * Values for all "GNUNET_FS_STATUS_DOWNLOAD_*" events.
917      */
918     struct
919     {
920
921       /**
922        * Context for controlling the download.
923        */
924       struct GNUNET_FS_DownloadContext *dc;
925
926       /**
927        * Client context pointer (set the last time
928        * by the client for this operation; initially
929        * NULL on START/RESUME events).
930        */
931       void *cctx;
932
933       /**
934        * Client context pointer for the parent operation
935        * (if this is a file in a directory or a subdirectory).
936        */
937       void *pctx;
938
939       /**
940        * Client context pointer for the associated search operation
941        * (specifically, context pointer for the specific search
942        * result, not the overall search); only set if this
943        * download was started from a search result.
944        */
945       void *sctx;
946
947       /**
948        * URI used for this download.
949        */
950       const struct GNUNET_FS_Uri *uri;
951
952       /**
953        * Name of the file that we are downloading.
954        */
955       const char *filename;
956
957       /**
958        * How large is the download overall?  This
959        * is NOT necessarily the size from the
960        * URI since we may be doing a partial download.
961        */
962       uint64_t size;
963
964       /**
965        * At what time do we expect to finish the download?
966        * (will be a value in the past for completed
967        * uploads).
968        */
969       struct GNUNET_TIME_Relative eta;
970
971       /**
972        * How long has this download been active?
973        */
974       struct GNUNET_TIME_Relative duration;
975
976       /**
977        * How many bytes have we completed?
978        */
979       uint64_t completed;
980
981       /**
982        * What anonymity level is used for this download?
983        */
984       uint32_t anonymity;
985
986       /**
987        * Is the download currently active.
988        */
989       int is_active;
990
991       /**
992        * Additional values for specific events.
993        */
994       union
995       {
996
997         /**
998          * These values are only valid for
999          * GNUNET_FS_STATUS_DOWNLOAD_PROGRESS events.
1000          */
1001         struct
1002         {
1003
1004           /**
1005            * Data block we just obtained, can be NULL (even if
1006            * data_len > 0) if we found the entire block 'intact' on
1007            * disk.  In this case, it is also possible for 'data_len'
1008            * to be larger than an individual (32k) block.
1009            */
1010           const void *data;
1011
1012           /**
1013            * At what offset in the file is "data"?
1014            */
1015           uint64_t offset;
1016
1017           /**
1018            * Length of the data block.
1019            */
1020           uint64_t data_len;
1021
1022           /**
1023            * Depth of the given block in the tree;
1024            * 0 would be the lowest level (DBLOCKS).
1025            */
1026           unsigned int depth;
1027
1028           /**
1029            * How much trust did we offer for downloading this block?
1030            */
1031           unsigned int trust_offered;
1032
1033           /**
1034            * How much time passed between us asking for this block and
1035            * actually getting it? GNUNET_TIME_UNIT_FOREVER_REL if unknown.
1036            */
1037           struct GNUNET_TIME_Relative block_download_duration;
1038
1039         } progress;
1040
1041         /**
1042          * These values are only valid for
1043          * GNUNET_FS_STATUS_DOWNLOAD_START events.
1044          */
1045         struct
1046         {
1047
1048           /**
1049            * Known metadata for the download.
1050            */
1051           const struct GNUNET_CONTAINER_MetaData *meta;
1052
1053         } start;
1054
1055         /**
1056          * These values are only valid for
1057          * GNUNET_FS_STATUS_DOWNLOAD_RESUME events.
1058          */
1059         struct
1060         {
1061
1062           /**
1063            * Known metadata for the download.
1064            */
1065           const struct GNUNET_CONTAINER_MetaData *meta;
1066
1067           /**
1068            * Error message, NULL if we have not encountered any error yet.
1069            */
1070           const char *message;
1071
1072         } resume;
1073
1074         /**
1075          * These values are only valid for
1076          * GNUNET_FS_STATUS_DOWNLOAD_ERROR events.
1077          */
1078         struct
1079         {
1080
1081           /**
1082            * Error message.
1083            */
1084           const char *message;
1085
1086         } error;
1087
1088       } specifics;
1089
1090     } download;
1091
1092     /**
1093      * Values for all "GNUNET_FS_STATUS_SEARCH_*" events.
1094      */
1095     struct
1096     {
1097
1098       /**
1099        * Context for controlling the search, NULL for
1100        * searches that were not explicitly triggered
1101        * by the client (i.e., searches for updates in
1102        * namespaces).
1103        */
1104       struct GNUNET_FS_SearchContext *sc;
1105
1106       /**
1107        * Client context pointer (set the last time by the client for
1108        * this operation; initially NULL on START/RESUME events).  Note
1109        * that this value can only be set on START/RESUME; returning
1110        * non-NULL on RESULT/RESUME_RESULT will actually update the
1111        * private context for "UPDATE" events.
1112        */
1113       void *cctx;
1114
1115       /**
1116        * Client parent-context pointer; NULL for top-level searches,
1117        * refers to the client context of the associated search result
1118        * for automatically triggered searches for updates in
1119        * namespaces.  In this case, 'presult' refers to that search
1120        * result.
1121        */
1122       void *pctx;
1123
1124       /**
1125        * What query is used for this search
1126        * (list of keywords or SKS identifier).
1127        */
1128       const struct GNUNET_FS_Uri *query;
1129
1130       /**
1131        * How long has this search been actively running
1132        * (excludes times where the search was paused or
1133        * suspended).
1134        */
1135       struct GNUNET_TIME_Relative duration;
1136
1137       /**
1138        * What anonymity level is used for this search?
1139        */
1140       uint32_t anonymity;
1141
1142       /**
1143        * Additional values for specific events.
1144        */
1145       union
1146       {
1147
1148         /**
1149          * These values are only valid for
1150          * GNUNET_FS_STATUS_SEARCH_RESULT events.
1151          */
1152         struct
1153         {
1154
1155           /**
1156            * Metadata for the search result.
1157            */
1158           const struct GNUNET_CONTAINER_MetaData *meta;
1159
1160           /**
1161            * URI for the search result.
1162            */
1163           const struct GNUNET_FS_Uri *uri;
1164
1165           /**
1166            * Handle to the result (for starting downloads).
1167            */
1168           struct GNUNET_FS_SearchResult *result;
1169
1170           /**
1171            * Applicability rank (the larger, the better the result
1172            * fits the search criteria).
1173            */
1174           uint32_t applicability_rank;
1175
1176         } result;
1177
1178         /**
1179          * These values are only valid for
1180          * GNUNET_FS_STATUS_SEARCH_RESUME_RESULT events.
1181          */
1182         struct
1183         {
1184
1185           /**
1186            * Metadata for the search result.
1187            */
1188           const struct GNUNET_CONTAINER_MetaData *meta;
1189
1190           /**
1191            * URI for the search result.
1192            */
1193           const struct GNUNET_FS_Uri *uri;
1194
1195           /**
1196            * Handle to the result (for starting downloads).
1197            */
1198           struct GNUNET_FS_SearchResult *result;
1199
1200           /**
1201            * Current availability rank (negative:
1202            * unavailable, positive: available)
1203            */
1204           int32_t availability_rank;
1205
1206           /**
1207            * On how many total queries is the given
1208            * availability_rank based?
1209            */
1210           uint32_t availability_certainty;
1211
1212           /**
1213            * Updated applicability rank (the larger,
1214            * the better the result fits the search
1215            * criteria).
1216            */
1217           uint32_t applicability_rank;
1218
1219         } resume_result;
1220
1221         /**
1222          * These values are only valid for
1223          * GNUNET_FS_STATUS_SEARCH_UPDATE events.
1224          */
1225         struct
1226         {
1227
1228           /**
1229            * Private context set for for this result
1230            * during the "RESULT" event.
1231            */
1232           void *cctx;
1233
1234           /**
1235            * Metadata for the search result.
1236            */
1237           const struct GNUNET_CONTAINER_MetaData *meta;
1238
1239           /**
1240            * URI for the search result.
1241            */
1242           const struct GNUNET_FS_Uri *uri;
1243
1244           /**
1245            * Current availability rank (negative:
1246            * unavailable, positive: available)
1247            */
1248           int32_t availability_rank;
1249
1250           /**
1251            * On how many total queries is the given
1252            * availability_rank based?
1253            */
1254           uint32_t availability_certainty;
1255
1256           /**
1257            * Updated applicability rank (the larger,
1258            * the better the result fits the search
1259            * criteria).
1260            */
1261           uint32_t applicability_rank;
1262
1263         } update;
1264
1265         /**
1266          * These values are only valid for
1267          * GNUNET_FS_STATUS_SEARCH_RESULT_SUSPEND events.
1268          * These events are automatically triggered for
1269          * each search result before the
1270          * GNUNET_FS_STATUS_SEARCH_SUSPEND event.  This
1271          * happens primarily to give the client a chance
1272          * to clean up the "cctx" (if needed).
1273          */
1274         struct
1275         {
1276
1277           /**
1278            * Private context set for for this result
1279            * during the "RESULT" event.
1280            */
1281           void *cctx;
1282
1283           /**
1284            * Metadata for the search result.
1285            */
1286           const struct GNUNET_CONTAINER_MetaData *meta;
1287
1288           /**
1289            * URI for the search result.
1290            */
1291           const struct GNUNET_FS_Uri *uri;
1292
1293         } result_suspend;
1294
1295         /**
1296          * These values are only valid for
1297          * GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED events.
1298          * These events are automatically triggered for
1299          * each search result before the
1300          * GNUNET_FS_STATUS_SEARCH_STOPPED event.  This
1301          * happens primarily to give the client a chance
1302          * to clean up the "cctx" (if needed).
1303          */
1304         struct
1305         {
1306
1307           /**
1308            * Private context set for for this result
1309            * during the "RESULT" event.
1310            */
1311           void *cctx;
1312
1313           /**
1314            * Metadata for the search result.
1315            */
1316           const struct GNUNET_CONTAINER_MetaData *meta;
1317
1318           /**
1319            * URI for the search result.
1320            */
1321           const struct GNUNET_FS_Uri *uri;
1322
1323         } result_stopped;
1324
1325         /**
1326          * These values are only valid for
1327          * GNUNET_FS_STATUS_SEARCH_RESUME events.
1328          */
1329         struct
1330         {
1331
1332           /**
1333            * Error message, NULL if we have not encountered any error yet.
1334            */
1335           const char *message;
1336
1337           /**
1338            * Is this search currently paused?
1339            */
1340           int is_paused;
1341
1342         } resume;
1343
1344         /**
1345          * These values are only valid for
1346          * GNUNET_FS_STATUS_SEARCH_ERROR events.
1347          */
1348         struct
1349         {
1350
1351           /**
1352            * Error message.
1353            */
1354           const char *message;
1355
1356         } error;
1357
1358         /**
1359          * Values for all "GNUNET_FS_STATUS_SEARCH_RESULT_NAMESPACE" events.
1360          */
1361         struct
1362         {
1363
1364           /**
1365            * Handle to the namespace (NULL if it is not a local
1366            * namespace).
1367            */
1368           struct GNUNET_FS_Namespace *ns;
1369
1370           /**
1371            * Short, human-readable name of the namespace.
1372            */
1373           const char *name;
1374
1375           /**
1376            * Root identifier for the namespace, can be NULL.
1377            */
1378           const char *root;
1379
1380           /**
1381            * Metadata for the namespace.
1382            */
1383           const struct GNUNET_CONTAINER_MetaData *meta;
1384
1385           /**
1386            * Hash-identifier for the namespace.
1387            */
1388           GNUNET_HashCode id;
1389
1390         } namespace;
1391
1392       } specifics;
1393
1394     } search;
1395
1396     /**
1397      * Values for all "GNUNET_FS_STATUS_UNINDEX_*" events.
1398      */
1399     struct
1400     {
1401
1402       /**
1403        * Context for controlling the unindexing.
1404        */
1405       struct GNUNET_FS_UnindexContext *uc;
1406
1407       /**
1408        * Client context pointer (set the last time
1409        * by the client for this operation; initially
1410        * NULL on START/RESUME events).
1411        */
1412       void *cctx;
1413
1414       /**
1415        * Name of the file that is being unindexed.
1416        */
1417       const char *filename;
1418
1419       /**
1420        * How large is the file overall?
1421        */
1422       uint64_t size;
1423
1424       /**
1425        * At what time do we expect to finish unindexing?
1426        * (will be a value in the past for completed
1427        * unindexing opeations).
1428        */
1429       struct GNUNET_TIME_Relative eta;
1430
1431       /**
1432        * How long has this upload been actively running
1433        * (excludes times where the upload was suspended).
1434        */
1435       struct GNUNET_TIME_Relative duration;
1436
1437       /**
1438        * How many bytes have we completed?
1439        */
1440       uint64_t completed;
1441
1442       /**
1443        * Additional values for specific events.
1444        */
1445       union
1446       {
1447
1448         /**
1449          * These values are only valid for
1450          * GNUNET_FS_STATUS_UNINDEX_PROGRESS events.
1451          */
1452         struct
1453         {
1454
1455           /**
1456            * Data block we just unindexed.
1457            */
1458           const void *data;
1459
1460           /**
1461            * At what offset in the file is "data"?
1462            */
1463           uint64_t offset;
1464
1465           /**
1466            * Length of the data block.
1467            */
1468           uint64_t data_len;
1469
1470           /**
1471            * Depth of the given block in the tree;
1472            * 0 would be the lowest level (DBLOCKS).
1473            */
1474           unsigned int depth;
1475
1476         } progress;
1477
1478         /**
1479          * These values are only valid for
1480          * GNUNET_FS_STATUS_UNINDEX_RESUME events.
1481          */
1482         struct
1483         {
1484
1485           /**
1486            * Error message, NULL if we have not encountered any error yet.
1487            */
1488           const char *message;
1489
1490         } resume;
1491
1492         /**
1493          * These values are only valid for
1494          * GNUNET_FS_STATUS_UNINDEX_ERROR events.
1495          */
1496         struct
1497         {
1498
1499           /**
1500            * Error message.
1501            */
1502           const char *message;
1503
1504         } error;
1505
1506       } specifics;
1507
1508     } unindex;
1509
1510   } value;
1511
1512   /**
1513    * Specific status code (determines the event type).
1514    */
1515   enum GNUNET_FS_Status status;
1516
1517 };
1518
1519
1520 /**
1521  * Notification of FS to a client about the progress of an
1522  * operation.  Callbacks of this type will be used for uploads,
1523  * downloads and searches.  Some of the arguments depend a bit
1524  * in their meaning on the context in which the callback is used.
1525  *
1526  * @param cls closure
1527  * @param info details about the event, specifying the event type
1528  *        and various bits about the event
1529  * @return client-context (for the next progress call
1530  *         for this operation; should be set to NULL for
1531  *         SUSPEND and STOPPED events).  The value returned
1532  *         will be passed to future callbacks in the respective
1533  *         field in the GNUNET_FS_ProgressInfo struct.
1534  */
1535 typedef void *(*GNUNET_FS_ProgressCallback) (void *cls,
1536                                              const struct GNUNET_FS_ProgressInfo
1537                                              * info);
1538
1539
1540 /**
1541  * General (global) option flags for file-sharing.
1542  */
1543 enum GNUNET_FS_Flags
1544 {
1545     /**
1546      * No special flags set.
1547      */
1548   GNUNET_FS_FLAGS_NONE = 0,
1549
1550     /**
1551      * Is persistence of operations desired?
1552      * (will create SUSPEND/RESUME events).
1553      */
1554   GNUNET_FS_FLAGS_PERSISTENCE = 1,
1555
1556     /**
1557      * Should we automatically trigger probes for search results
1558      * to determine availability?
1559      * (will create GNUNET_FS_STATUS_SEARCH_UPDATE events).
1560      */
1561   GNUNET_FS_FLAGS_DO_PROBES = 2
1562 };
1563
1564 /**
1565  * Options specified in the VARARGs portion of GNUNET_FS_start.
1566  */
1567 enum GNUNET_FS_OPTIONS
1568 {
1569
1570     /**
1571      * Last option in the VARARG list.
1572      */
1573   GNUNET_FS_OPTIONS_END = 0,
1574
1575     /**
1576      * Select the desired amount of parallelism (this option should be
1577      * followed by an "unsigned int" giving the desired maximum number
1578      * of parallel downloads).
1579      */
1580   GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM = 1,
1581
1582     /**
1583      * Maximum number of requests that should be pending at a given
1584      * point in time (invidivual downloads may go above this, but
1585      * if we are above this threshold, we should not activate any
1586      * additional downloads.
1587      */
1588   GNUNET_FS_OPTIONS_REQUEST_PARALLELISM = 2
1589 };
1590
1591
1592 /**
1593  * Settings for publishing a block (which may of course also
1594  * apply to an entire directory or file).
1595  */
1596 struct GNUNET_FS_BlockOptions
1597 {
1598
1599   /**
1600    * At what time should the block expire?  Data blocks (DBLOCKS and
1601    * IBLOCKS) may still be used even if they are expired (however,
1602    * they'd be removed quickly from the datastore if we are short on
1603    * space), all other types of blocks will no longer be returned
1604    * after they expire.
1605    */
1606   struct GNUNET_TIME_Absolute expiration_time;
1607
1608   /**
1609    * At which anonymity level should the block be shared?
1610    * (0: no anonymity, 1: normal GAP, >1: with cover traffic).
1611    */
1612   uint32_t anonymity_level;
1613
1614   /**
1615    * How important is it for us to store the block?  If we run
1616    * out of space, the highest-priority, non-expired blocks will
1617    * be kept.
1618    */
1619   uint32_t content_priority;
1620
1621   /**
1622    * How often should we try to migrate the block to other peers?
1623    * Only used if "CONTENT_PUSHING" is set to YES, in which case we
1624    * first push each block to other peers according to their
1625    * replication levels.  Once each block has been pushed that many
1626    * times to other peers, blocks are chosen for migration at random.
1627    * Naturally, there is no guarantee that the other peers will keep
1628    * these blocks for any period of time (since they won't have any
1629    * priority or might be too busy to even store the block in the
1630    * first place).
1631    */
1632   uint32_t replication_level;
1633
1634 };
1635
1636
1637 /**
1638  * Return the current year (i.e. '2011').
1639  */
1640 unsigned int
1641 GNUNET_FS_get_current_year (void);
1642
1643
1644 /**
1645  * Convert a year to an expiration time of January 1st of that year.
1646  *
1647  * @param year a year (after 1970, please ;-)).
1648  * @return absolute time for January 1st of that year.
1649  */
1650 struct GNUNET_TIME_Absolute
1651 GNUNET_FS_year_to_time (unsigned int year);
1652
1653
1654 /**
1655  * Convert an expiration time to the respective year (rounds)
1656  *
1657  * @param at absolute time 
1658  * @return year a year (after 1970), 0 on error
1659  */
1660 unsigned int 
1661 GNUNET_FS_time_to_year (struct GNUNET_TIME_Absolute at);
1662
1663
1664 /**
1665  * Handle to the file-sharing service.
1666  */
1667 struct GNUNET_FS_Handle;
1668
1669
1670 /**
1671  * Setup a connection to the file-sharing service.
1672  *
1673  * @param cfg configuration to use
1674  * @param client_name unique identifier for this client
1675  * @param upcb function to call to notify about FS actions
1676  * @param upcb_cls closure for upcb
1677  * @param flags specific attributes for fs-operations
1678  * @param ... list of optional options, terminated with GNUNET_FS_OPTIONS_END
1679  * @return NULL on error
1680  */
1681 struct GNUNET_FS_Handle *
1682 GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1683                  const char *client_name, GNUNET_FS_ProgressCallback upcb,
1684                  void *upcb_cls, enum GNUNET_FS_Flags flags, ...);
1685
1686
1687 /**
1688  * Close our connection with the file-sharing service.
1689  * The callback given to GNUNET_FS_start will no longer be
1690  * called after this function returns.
1691  *
1692  * @param h handle that was returned from GNUNET_FS_start
1693  */
1694 void
1695 GNUNET_FS_stop (struct GNUNET_FS_Handle *h);
1696
1697
1698 /**
1699  * Extract meta-data from a file.
1700  *
1701  * @param md metadata to set
1702  * @param filename name of file to inspect
1703  * @param extractors plugins to use
1704  * @return GNUNET_SYSERR on error, otherwise the number
1705  *   of meta-data items obtained
1706  */
1707 int
1708 GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData *md,
1709                                        const char *filename,
1710                                        struct EXTRACTOR_PluginList *extractors);
1711
1712
1713 /**
1714  * Function called on entries in a GNUNET_FS_FileInformation publish-structure.
1715  *
1716  * @param cls closure
1717  * @param fi the entry in the publish-structure
1718  * @param length length of the file or directory
1719  * @param meta metadata for the file or directory (can be modified)
1720  * @param uri pointer to the keywords that will be used for this entry (can be modified)
1721  * @param bo block options (can be modified)
1722  * @param do_index should we index (can be modified)
1723  * @param client_info pointer to client context set upon creation (can be modified)
1724  * @return GNUNET_OK to continue, GNUNET_NO to remove
1725  *         this entry from the directory, GNUNET_SYSERR
1726  *         to abort the iteration
1727  */
1728 typedef int (*GNUNET_FS_FileInformationProcessor) (void *cls,
1729                                                    struct
1730                                                    GNUNET_FS_FileInformation *
1731                                                    fi, uint64_t length,
1732                                                    struct
1733                                                    GNUNET_CONTAINER_MetaData *
1734                                                    meta,
1735                                                    struct GNUNET_FS_Uri ** uri,
1736                                                    struct GNUNET_FS_BlockOptions
1737                                                    * bo, int *do_index,
1738                                                    void **client_info);
1739
1740
1741 /**
1742  * Obtain the name under which this file information
1743  * structure is stored on disk.  Only works for top-level
1744  * file information structures.
1745  *
1746  * @param s structure to get the filename for
1747  * @return NULL on error, otherwise filename that
1748  *         can be passed to "GNUNET_FS_file_information_recover"
1749  *         to read this fi-struct from disk.
1750  */
1751 const char *
1752 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s);
1753
1754
1755 /**
1756  * Obtain the filename from the file information structure.
1757  *
1758  * @param s structure to get the filename for
1759  * @return "filename" field of the structure (can be NULL)
1760  */
1761 const char *
1762 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s);
1763
1764
1765 /**
1766  * Set the filename in the file information structure.
1767  * If filename was already set, frees it before setting the new one.
1768  * Makes a copy of the argument.
1769  *
1770  * @param s structure to get the filename for
1771  * @param filename filename to set
1772  */
1773 void
1774 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
1775                                          const char *filename);
1776
1777
1778 /**
1779  * Create an entry for a file in a publish-structure.
1780  *
1781  * @param h handle to the file sharing subsystem
1782  * @param client_info initial client-info value for this entry
1783  * @param filename name of the file or directory to publish
1784  * @param keywords under which keywords should this file be available
1785  *         directly; can be NULL
1786  * @param meta metadata for the file
1787  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
1788  *                GNUNET_SYSERR for simulation
1789  * @param bo block options
1790  * @return publish structure entry for the file
1791  */
1792 struct GNUNET_FS_FileInformation *
1793 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
1794                                              void *client_info,
1795                                              const char *filename,
1796                                              const struct GNUNET_FS_Uri
1797                                              *keywords,
1798                                              const struct
1799                                              GNUNET_CONTAINER_MetaData *meta,
1800                                              int do_index,
1801                                              const struct GNUNET_FS_BlockOptions
1802                                              *bo);
1803
1804
1805 /**
1806  * Create an entry for a file in a publish-structure.
1807  *
1808  * @param h handle to the file sharing subsystem
1809  * @param client_info initial client-info value for this entry
1810  * @param length length of the file
1811  * @param data data for the file (should not be used afterwards by
1812  *        the caller; callee will "free")
1813  * @param keywords under which keywords should this file be available
1814  *         directly; can be NULL
1815  * @param meta metadata for the file
1816  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
1817  *                GNUNET_SYSERR for simulation
1818  * @param bo block options
1819  * @return publish structure entry for the file
1820  */
1821 struct GNUNET_FS_FileInformation *
1822 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
1823                                              void *client_info, uint64_t length,
1824                                              void *data,
1825                                              const struct GNUNET_FS_Uri
1826                                              *keywords,
1827                                              const struct
1828                                              GNUNET_CONTAINER_MetaData *meta,
1829                                              int do_index,
1830                                              const struct GNUNET_FS_BlockOptions
1831                                              *bo);
1832
1833
1834 /**
1835  * Function that provides data.
1836  *
1837  * @param cls closure
1838  * @param offset offset to read from; it is possible
1839  *            that the caller might need to go backwards
1840  *            a bit at times
1841  * @param max maximum number of bytes that should be
1842  *            copied to buf; readers are not allowed
1843  *            to provide less data unless there is an error;
1844  *            a value of "0" will be used at the end to allow
1845  *            the reader to clean up its internal state
1846  * @param buf where the reader should write the data
1847  * @param emsg location for the reader to store an error message
1848  * @return number of bytes written, usually "max", 0 on error
1849  */
1850 typedef size_t (*GNUNET_FS_DataReader) (void *cls, uint64_t offset, size_t max,
1851                                         void *buf, char **emsg);
1852
1853
1854 /**
1855  * Create an entry for a file in a publish-structure.
1856  *
1857  * @param h handle to the file sharing subsystem
1858  * @param client_info initial client-info value for this entry
1859  * @param length length of the file
1860  * @param reader function that can be used to obtain the data for the file
1861  * @param reader_cls closure for "reader"
1862  * @param keywords under which keywords should this file be available
1863  *         directly; can be NULL
1864  * @param meta metadata for the file
1865  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
1866  *                GNUNET_SYSERR for simulation
1867  * @param bo block options
1868  * @return publish structure entry for the file
1869  */
1870 struct GNUNET_FS_FileInformation *
1871 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
1872                                                void *client_info,
1873                                                uint64_t length,
1874                                                GNUNET_FS_DataReader reader,
1875                                                void *reader_cls,
1876                                                const struct GNUNET_FS_Uri
1877                                                *keywords,
1878                                                const struct
1879                                                GNUNET_CONTAINER_MetaData *meta,
1880                                                int do_index,
1881                                                const struct
1882                                                GNUNET_FS_BlockOptions *bo);
1883
1884
1885 /**
1886  * Create an entry for an empty directory in a publish-structure.
1887  * This function should be used by applications for which the
1888  * use of "GNUNET_FS_file_information_create_from_directory"
1889  * is not appropriate.
1890  *
1891  * @param h handle to the file sharing subsystem
1892  * @param client_info initial client-info value for this entry
1893  * @param keywords under which keywords should this directory be available
1894  *         directly; can be NULL
1895  * @param meta metadata for the directory
1896  * @param bo block options
1897  * @param filename name of the directory; can be NULL
1898  * @return publish structure entry for the directory , NULL on error
1899  */
1900 struct GNUNET_FS_FileInformation *
1901 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
1902                                                    void *client_info,
1903                                                    const struct GNUNET_FS_Uri
1904                                                    *keywords,
1905                                                    const struct
1906                                                    GNUNET_CONTAINER_MetaData
1907                                                    *meta,
1908                                                    const struct
1909                                                    GNUNET_FS_BlockOptions *bo,
1910                                                    const char *filename);
1911
1912
1913 /**
1914  * Test if a given entry represents a directory.
1915  *
1916  * @param ent check if this FI represents a directory
1917  * @return GNUNET_YES if so, GNUNET_NO if not
1918  */
1919 int
1920 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
1921                                          *ent);
1922
1923
1924 /**
1925  * Add an entry to a directory in a publish-structure.  Clients
1926  * should never modify publish structures that were passed to
1927  * "GNUNET_FS_publish_start" already.
1928  *
1929  * @param dir the directory
1930  * @param ent the entry to add; the entry must not have been
1931  *            added to any other directory at this point and
1932  *            must not include "dir" in its structure
1933  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1934  */
1935 int
1936 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
1937                                 struct GNUNET_FS_FileInformation *ent);
1938
1939
1940 /**
1941  * Inspect a file or directory in a publish-structure.  Clients
1942  * should never modify publish structures that were passed to
1943  * "GNUNET_FS_publish_start" already.  When called on a directory,
1944  * this function will FIRST call "proc" with information about
1945  * the directory itself and then for each of the files in the
1946  * directory (but not for files in subdirectories).  When called
1947  * on a file, "proc" will be called exactly once (with information
1948  * about the specific file).
1949  *
1950  * @param dir the directory
1951  * @param proc function to call on each entry
1952  * @param proc_cls closure for proc
1953  */
1954 void
1955 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
1956                                     GNUNET_FS_FileInformationProcessor proc,
1957                                     void *proc_cls);
1958
1959
1960 /**
1961  * Destroy publish-structure.  Clients should never destroy publish
1962  * structures that were passed to "GNUNET_FS_publish_start" already.
1963  *
1964  * @param fi structure to destroy
1965  * @param cleaner function to call on each entry in the structure
1966  *        (useful to clean up client_info); can be NULL; return
1967  *        values are ignored
1968  * @param cleaner_cls closure for cleaner
1969  */
1970 void
1971 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
1972                                     GNUNET_FS_FileInformationProcessor cleaner,
1973                                     void *cleaner_cls);
1974
1975
1976 /**
1977  * Options for publishing.  Compatible options
1978  * can be OR'ed together.
1979  */
1980 enum GNUNET_FS_PublishOptions
1981 {
1982     /**
1983      * No options (use defaults for everything).
1984      */
1985   GNUNET_FS_PUBLISH_OPTION_NONE = 0,
1986
1987     /**
1988      * Simulate publishing.  With this option, no data will be stored
1989      * in the datastore.  Useful for computing URIs from files.
1990      */
1991   GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY = 1
1992 };
1993
1994 /**
1995  * Publish a file or directory.
1996  *
1997  * @param h handle to the file sharing subsystem
1998  * @param fi information about the file or directory structure to publish
1999  * @param namespace namespace to publish the file in, NULL for no namespace
2000  * @param nid identifier to use for the publishd content in the namespace
2001  *        (can be NULL, must be NULL if namespace is NULL)
2002  * @param nuid update-identifier that will be used for future updates
2003  *        (can be NULL, must be NULL if namespace or nid is NULL)
2004  * @param options options for the publication
2005  * @return context that can be used to control the publish operation
2006  */
2007 struct GNUNET_FS_PublishContext *
2008 GNUNET_FS_publish_start (struct GNUNET_FS_Handle *h,
2009                          struct GNUNET_FS_FileInformation *fi,
2010                          struct GNUNET_FS_Namespace *namespace, const char *nid,
2011                          const char *nuid,
2012                          enum GNUNET_FS_PublishOptions options);
2013
2014
2015 /**
2016  * Stop a publication.  Will abort incomplete publications (but
2017  * not remove blocks that have already been published) or
2018  * simply clean up the state for completed publications.
2019  * Must NOT be called from within the event callback!
2020  *
2021  * @param pc context for the publication to stop
2022  */
2023 void
2024 GNUNET_FS_publish_stop (struct GNUNET_FS_PublishContext *pc);
2025
2026
2027 /**
2028  * Signature of a function called as the continuation of a KBlock or
2029  * SBlock publication.
2030  *
2031  * @param cls closure
2032  * @param uri URI under which the block is now available, NULL on error
2033  * @param emsg error message, NULL on success
2034  */
2035 typedef void (*GNUNET_FS_PublishContinuation) (void *cls,
2036                                                const struct GNUNET_FS_Uri * uri,
2037                                                const char *emsg);
2038
2039
2040 /**
2041  * Publish a KBlock on GNUnet.
2042  *
2043  * @param h handle to the file sharing subsystem
2044  * @param ksk_uri keywords to use
2045  * @param meta metadata to use
2046  * @param uri URI to refer to in the KBlock
2047  * @param bo block options
2048  * @param options publication options
2049  * @param cont continuation
2050  * @param cont_cls closure for cont
2051  */
2052 void
2053 GNUNET_FS_publish_ksk (struct GNUNET_FS_Handle *h,
2054                        const struct GNUNET_FS_Uri *ksk_uri,
2055                        const struct GNUNET_CONTAINER_MetaData *meta,
2056                        const struct GNUNET_FS_Uri *uri,
2057                        const struct GNUNET_FS_BlockOptions *bo,
2058                        enum GNUNET_FS_PublishOptions options,
2059                        GNUNET_FS_PublishContinuation cont, void *cont_cls);
2060
2061
2062 /**
2063  * Publish an SBlock on GNUnet.
2064  *
2065  * @param h handle to the file sharing subsystem
2066  * @param namespace namespace to publish in
2067  * @param identifier identifier to use
2068  * @param update update identifier to use
2069  * @param meta metadata to use
2070  * @param uri URI to refer to in the SBlock
2071  * @param bo block options
2072  * @param options publication options
2073  * @param cont continuation
2074  * @param cont_cls closure for cont
2075  */
2076 void
2077 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
2078                        struct GNUNET_FS_Namespace *namespace,
2079                        const char *identifier, const char *update,
2080                        const struct GNUNET_CONTAINER_MetaData *meta,
2081                        const struct GNUNET_FS_Uri *uri,
2082                        const struct GNUNET_FS_BlockOptions *bo,
2083                        enum GNUNET_FS_PublishOptions options,
2084                        GNUNET_FS_PublishContinuation cont, void *cont_cls);
2085
2086
2087 /**
2088  * Type of a function called by "GNUNET_FS_get_indexed_files".
2089  *
2090  * @param cls closure
2091  * @param filename the name of the file
2092  * @param file_id hash of the contents of the indexed file
2093  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
2094  */
2095 typedef int (*GNUNET_FS_IndexedFileProcessor) (void *cls, const char *filename,
2096                                                const GNUNET_HashCode * file_id);
2097
2098
2099 /**
2100  * Iterate over all indexed files.
2101  *
2102  * @param h handle to the file sharing subsystem
2103  * @param iterator function to call on each indexed file
2104  * @param iterator_cls closure for iterator
2105  * @param cont continuation to call when done;
2106  *             reason should be "TIMEOUT" (on
2107  *             error) or  "PREREQ_DONE" (on success)
2108  * @param cont_cls closure for cont
2109  */
2110 void
2111 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
2112                              GNUNET_FS_IndexedFileProcessor iterator,
2113                              void *iterator_cls, GNUNET_SCHEDULER_Task cont,
2114                              void *cont_cls);
2115
2116
2117 /**
2118  * Unindex a file.
2119  *
2120  * @param h handle to the file sharing subsystem
2121  * @param filename file to unindex
2122  * @param cctx initial value for the client context
2123  * @return NULL on error, otherwise handle
2124  */
2125 struct GNUNET_FS_UnindexContext *
2126 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h, const char *filename,
2127                          void *cctx);
2128
2129
2130 /**
2131  * Clean up after completion of an unindex operation.
2132  *
2133  * @param uc handle
2134  */
2135 void
2136 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc);
2137
2138
2139 /**
2140  * Publish an advertismement for a namespace.
2141  *
2142  * @param h handle to the file sharing subsystem
2143  * @param ksk_uri keywords to use for advertisment
2144  * @param namespace handle for the namespace that should be advertised
2145  * @param meta meta-data for the namespace advertisement
2146  * @param bo block options
2147  * @param rootEntry name of the root of the namespace
2148  * @param cont continuation
2149  * @param cont_cls closure for cont
2150  */
2151 void
2152 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
2153                                struct GNUNET_FS_Uri *ksk_uri,
2154                                struct GNUNET_FS_Namespace *namespace,
2155                                const struct GNUNET_CONTAINER_MetaData *meta,
2156                                const struct GNUNET_FS_BlockOptions *bo,
2157                                const char *rootEntry,
2158                                GNUNET_FS_PublishContinuation cont,
2159                                void *cont_cls);
2160
2161
2162 /**
2163  * Create a namespace with the given name; if one already
2164  * exists, return a handle to the existing namespace.
2165  *
2166  * @param h handle to the file sharing subsystem
2167  * @param name name to use for the namespace
2168  * @return handle to the namespace, NULL on error
2169  */
2170 struct GNUNET_FS_Namespace *
2171 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name);
2172
2173
2174 /**
2175  * Delete a namespace handle.  Can be used for a clean shutdown (free
2176  * memory) or also to freeze the namespace to prevent further
2177  * insertions by anyone.
2178  *
2179  * @param namespace handle to the namespace that should be deleted / freed
2180  * @param freeze prevents future insertions; creating a namespace
2181  *        with the same name again will create a fresh namespace instead
2182  *
2183  * @return GNUNET_OK on success, GNUNET_SYSERR on error
2184  */
2185 int
2186 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze);
2187
2188
2189 /**
2190  * Callback with information about local (!) namespaces.
2191  * Contains the names of the local namespace and the global
2192  * ID.
2193  *
2194  * @param cls closure
2195  * @param name human-readable identifier of the namespace
2196  * @param id hash identifier for the namespace
2197  */
2198 typedef void (*GNUNET_FS_NamespaceInfoProcessor) (void *cls, const char *name,
2199                                                   const GNUNET_HashCode * id);
2200
2201
2202 /**
2203  * Build a list of all available local (!) namespaces The returned
2204  * names are only the nicknames since we only iterate over the local
2205  * namespaces.
2206  *
2207  * @param h handle to the file sharing subsystem
2208  * @param cb function to call on each known namespace
2209  * @param cb_cls closure for cb
2210  */
2211 void
2212 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
2213                           GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls);
2214
2215
2216 /**
2217  * Function called on updateable identifiers.
2218  *
2219  * @param cls closure
2220  * @param last_id last identifier
2221  * @param last_uri uri used for the content published under the last_id
2222  * @param last_meta metadata associated with last_uri
2223  * @param next_id identifier that should be used for updates
2224  */
2225 typedef void (*GNUNET_FS_IdentifierProcessor) (void *cls, const char *last_id,
2226                                                const struct GNUNET_FS_Uri *
2227                                                last_uri,
2228                                                const struct
2229                                                GNUNET_CONTAINER_MetaData *
2230                                                last_meta, const char *next_id);
2231
2232
2233 /**
2234  * List all of the identifiers in the namespace for which we could
2235  * produce an update.  Namespace updates form a graph where each node
2236  * has a name.  Each node can have any number of URI/meta-data entries
2237  * which can each be linked to other nodes.  Cycles are possible.
2238  *
2239  * Calling this function with "next_id" NULL will cause the library to
2240  * call "ip" with a root for each strongly connected component of the
2241  * graph (a root being a node from which all other nodes in the Scc
2242  * are reachable).
2243  *
2244  * Calling this function with "next_id" being the name of a node will
2245  * cause the library to call "ip" with all children of the node.  Note
2246  * that cycles within an SCC are possible (including self-loops).
2247  *
2248  * @param namespace namespace to inspect for updateable content
2249  * @param next_id ID to look for; use NULL to look for SCC roots
2250  * @param ip function to call on each updateable identifier
2251  * @param ip_cls closure for ip
2252  */
2253 void
2254 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
2255                                      const char *next_id,
2256                                      GNUNET_FS_IdentifierProcessor ip,
2257                                      void *ip_cls);
2258
2259
2260 /**
2261  * Options for searching.  Compatible options
2262  * can be OR'ed together.
2263  */
2264 enum GNUNET_FS_SearchOptions
2265 {
2266     /**
2267      * No options (use defaults for everything).
2268      */
2269   GNUNET_FS_SEARCH_OPTION_NONE = 0,
2270
2271     /**
2272      * Only search the local host, do not search remote systems (no P2P)
2273      */
2274   GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY = 1
2275 };
2276
2277
2278 /**
2279  * Start search for content.
2280  *
2281  * @param h handle to the file sharing subsystem
2282  * @param uri specifies the search parameters; can be
2283  *        a KSK URI or an SKS URI.
2284  * @param anonymity desired level of anonymity
2285  * @param options options for the search
2286  * @param cctx initial value for the client context
2287  * @return context that can be used to control the search
2288  */
2289 struct GNUNET_FS_SearchContext *
2290 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
2291                         const struct GNUNET_FS_Uri *uri, uint32_t anonymity,
2292                         enum GNUNET_FS_SearchOptions options, void *cctx);
2293
2294
2295 /**
2296  * Pause search.
2297  *
2298  * @param sc context for the search that should be paused
2299  */
2300 void
2301 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc);
2302
2303
2304 /**
2305  * Continue paused search.
2306  *
2307  * @param sc context for the search that should be resumed
2308  */
2309 void
2310 GNUNET_FS_search_continue (struct GNUNET_FS_SearchContext *sc);
2311
2312
2313 /**
2314  * Stop search for content.
2315  *
2316  * @param sc context for the search that should be stopped
2317  */
2318 void
2319 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc);
2320
2321
2322
2323
2324 /**
2325  * Options for downloading.  Compatible options
2326  * can be OR'ed together.
2327  */
2328 enum GNUNET_FS_DownloadOptions
2329 {
2330     /**
2331      * No options (use defaults for everything).
2332      */
2333   GNUNET_FS_DOWNLOAD_OPTION_NONE = 0,
2334
2335     /**
2336      * Only download from the local host, do not access remote systems (no P2P)
2337      */
2338   GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY = 1,
2339
2340     /**
2341      * Do a recursive download (that is, automatically trigger the
2342      * download of files in directories).
2343      */
2344   GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE = 2,
2345
2346     /**
2347      * Do not append temporary data to
2348      * the target file (for the IBlocks).
2349      */
2350   GNUNET_FS_DOWNLOAD_NO_TEMPORARIES = 4,
2351
2352     /**
2353      * Internal option used to flag this download as a 'probe' for a
2354      * search result.  Impacts the priority with which the download is
2355      * run and causes signalling callbacks to be done differently.
2356      * Also, probe downloads are not serialized on suspension.  Normal
2357      * clients should not use this!
2358      */
2359   GNUNET_FS_DOWNLOAD_IS_PROBE = (1 << 31)
2360 };
2361
2362
2363
2364 /**
2365  * Download parts of a file.  Note that this will store
2366  * the blocks at the respective offset in the given file.  Also, the
2367  * download is still using the blocking of the underlying FS
2368  * encoding.  As a result, the download may *write* outside of the
2369  * given boundaries (if offset and length do not match the 32k FS
2370  * block boundaries). <p>
2371  *
2372  * The given range can be used to focus a download towards a
2373  * particular portion of the file (optimization), not to strictly
2374  * limit the download to exactly those bytes.
2375  *
2376  * @param h handle to the file sharing subsystem
2377  * @param uri the URI of the file (determines what to download); CHK or LOC URI
2378  * @param meta known metadata for the file (can be NULL)
2379  * @param filename where to store the file, maybe NULL (then no file is
2380  *        created on disk and data must be grabbed from the callbacks)
2381  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2382  *        can be NULL (in which case we will pick a name if needed); the temporary file
2383  *        may already exist, in which case we will try to use the data that is there and
2384  *        if it is not what is desired, will overwrite it
2385  * @param offset at what offset should we start the download (typically 0)
2386  * @param length how many bytes should be downloaded starting at offset
2387  * @param anonymity anonymity level to use for the download
2388  * @param options various download options
2389  * @param cctx initial value for the client context for this download
2390  * @param parent parent download to associate this download with (use NULL
2391  *        for top-level downloads; useful for manually-triggered recursive downloads)
2392  * @return context that can be used to control this download
2393  */
2394 struct GNUNET_FS_DownloadContext *
2395 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2396                           const struct GNUNET_FS_Uri *uri,
2397                           const struct GNUNET_CONTAINER_MetaData *meta,
2398                           const char *filename, const char *tempname,
2399                           uint64_t offset, uint64_t length, uint32_t anonymity,
2400                           enum GNUNET_FS_DownloadOptions options, void *cctx,
2401                           struct GNUNET_FS_DownloadContext *parent);
2402
2403
2404 /**
2405  * Download parts of a file based on a search result.  The download
2406  * will be associated with the search result (and the association
2407  * will be preserved when serializing/deserializing the state).
2408  * If the search is stopped, the download will not be aborted but
2409  * be 'promoted' to a stand-alone download.
2410  *
2411  * As with the other download function, this will store
2412  * the blocks at the respective offset in the given file.  Also, the
2413  * download is still using the blocking of the underlying FS
2414  * encoding.  As a result, the download may *write* outside of the
2415  * given boundaries (if offset and length do not match the 32k FS
2416  * block boundaries). <p>
2417  *
2418  * The given range can be used to focus a download towards a
2419  * particular portion of the file (optimization), not to strictly
2420  * limit the download to exactly those bytes.
2421  *
2422  * @param h handle to the file sharing subsystem
2423  * @param sr the search result to use for the download (determines uri and
2424  *        meta data and associations)
2425  * @param filename where to store the file, maybe NULL (then no file is
2426  *        created on disk and data must be grabbed from the callbacks)
2427  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2428  *        can be NULL (in which case we will pick a name if needed); the temporary file
2429  *        may already exist, in which case we will try to use the data that is there and
2430  *        if it is not what is desired, will overwrite it
2431  * @param offset at what offset should we start the download (typically 0)
2432  * @param length how many bytes should be downloaded starting at offset
2433  * @param anonymity anonymity level to use for the download
2434  * @param options various download options
2435  * @param cctx initial value for the client context for this download
2436  * @return context that can be used to control this download
2437  */
2438 struct GNUNET_FS_DownloadContext *
2439 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2440                                       struct GNUNET_FS_SearchResult *sr,
2441                                       const char *filename,
2442                                       const char *tempname, uint64_t offset,
2443                                       uint64_t length, uint32_t anonymity,
2444                                       enum GNUNET_FS_DownloadOptions options,
2445                                       void *cctx);
2446
2447
2448 /**
2449  * Stop a download (aborts if download is incomplete).
2450  *
2451  * @param dc handle for the download
2452  * @param do_delete delete files of incomplete downloads
2453  */
2454 void
2455 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc, int do_delete);
2456
2457
2458
2459 /* ******************** Directory API *********************** */
2460
2461
2462 #define GNUNET_FS_DIRECTORY_MIME  "application/gnunet-directory"
2463 #define GNUNET_FS_DIRECTORY_MAGIC "\211GND\r\n\032\n"
2464 #define GNUNET_FS_DIRECTORY_EXT   ".gnd"
2465
2466 /**
2467  * Does the meta-data claim that this is a directory?
2468  * Checks if the mime-type is that of a GNUnet directory.
2469  *
2470  * @return GNUNET_YES if it is, GNUNET_NO if it is not, GNUNET_SYSERR if
2471  *  we have no mime-type information (treat as 'GNUNET_NO')
2472  */
2473 int
2474 GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData
2475                                         *md);
2476
2477
2478 /**
2479  * Set the MIMETYPE information for the given
2480  * metadata to "application/gnunet-directory".
2481  *
2482  * @param md metadata to add mimetype to
2483  */
2484 void
2485 GNUNET_FS_meta_data_make_directory (struct GNUNET_CONTAINER_MetaData *md);
2486
2487
2488 /**
2489  * Suggest a filename based on given metadata.
2490  *
2491  * @param md given meta data
2492  * @return NULL if meta data is useless for suggesting a filename
2493  */
2494 char *
2495 GNUNET_FS_meta_data_suggest_filename (const struct GNUNET_CONTAINER_MetaData
2496                                       *md);
2497
2498
2499 /**
2500  * Function used to process entries in a directory.
2501  *
2502  * @param cls closure
2503  * @param filename name of the file in the directory
2504  * @param uri URI of the file
2505  * @param metadata metadata for the file; metadata for
2506  *        the directory if everything else is NULL/zero
2507  * @param length length of the available data for the file
2508  *           (of type size_t since data must certainly fit
2509  *            into memory; if files are larger than size_t
2510  *            permits, then they will certainly not be
2511  *            embedded with the directory itself).
2512  * @param data data available for the file (length bytes)
2513  */
2514 typedef void (*GNUNET_FS_DirectoryEntryProcessor) (void *cls,
2515                                                    const char *filename,
2516                                                    const struct GNUNET_FS_Uri *
2517                                                    uri,
2518                                                    const struct
2519                                                    GNUNET_CONTAINER_MetaData *
2520                                                    meta, size_t length,
2521                                                    const void *data);
2522
2523
2524 /**
2525  * Iterate over all entries in a directory.  Note that directories
2526  * are structured such that it is possible to iterate over the
2527  * individual blocks as well as over the entire directory.  Thus
2528  * a client can call this function on the buffer in the
2529  * GNUNET_FS_ProgressCallback.  Also, directories can optionally
2530  * include the contents of (small) files embedded in the directory
2531  * itself; for those files, the processor may be given the
2532  * contents of the file directly by this function.
2533  *
2534  * @param size number of bytes in data
2535  * @param data pointer to the beginning of the directory
2536  * @param offset offset of data in the directory
2537  * @param dep function to call on each entry
2538  * @param dep_cls closure for dep
2539  * @return GNUNET_OK if this could be a block in a directory,
2540  *         GNUNET_NO if this could be part of a directory (but not 100% OK)
2541  *         GNUNET_SYSERR if 'data' does not represent a directory
2542  */
2543 int
2544 GNUNET_FS_directory_list_contents (size_t size, const void *data,
2545                                    uint64_t offset,
2546                                    GNUNET_FS_DirectoryEntryProcessor dep,
2547                                    void *dep_cls);
2548
2549
2550 /**
2551  * Opaque handle to a directory builder.
2552  */
2553 struct GNUNET_FS_DirectoryBuilder;
2554
2555 /**
2556  * Create a directory builder.
2557  *
2558  * @param mdir metadata for the directory
2559  */
2560 struct GNUNET_FS_DirectoryBuilder *
2561 GNUNET_FS_directory_builder_create (const struct GNUNET_CONTAINER_MetaData
2562                                     *mdir);
2563
2564
2565 /**
2566  * Add an entry to a directory.
2567  *
2568  * @param bld directory to extend
2569  * @param uri uri of the entry (must not be a KSK)
2570  * @param md metadata of the entry
2571  * @param data raw data of the entry, can be NULL, otherwise
2572  *        data must point to exactly the number of bytes specified
2573  *        by the uri
2574  */
2575 void
2576 GNUNET_FS_directory_builder_add (struct GNUNET_FS_DirectoryBuilder *bld,
2577                                  const struct GNUNET_FS_Uri *uri,
2578                                  const struct GNUNET_CONTAINER_MetaData *md,
2579                                  const void *data);
2580
2581
2582 /**
2583  * Finish building the directory.  Frees the
2584  * builder context and returns the directory
2585  * in-memory.
2586  *
2587  * @param bld directory to finish
2588  * @param rsize set to the number of bytes needed
2589  * @param rdata set to the encoded directory
2590  * @return GNUNET_OK on success
2591  */
2592 int
2593 GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld,
2594                                     size_t * rsize, void **rdata);
2595
2596
2597 /* ******************** DirScanner API *********************** */
2598
2599 /**
2600  * Progress reasons of the directory scanner.
2601  */
2602 enum GNUNET_FS_DirScannerProgressUpdateReason
2603 {
2604
2605   /**
2606    * We've started processing a file or directory.
2607    */
2608   GNUNET_FS_DIRSCANNER_FILE_START = 0,
2609
2610   /**
2611    * We've finished processing a subtree in the pre-pass.
2612    */
2613   GNUNET_FS_DIRSCANNER_SUBTREE_COUNTED,
2614
2615   /**
2616    * We've found all files (in the pre-pass).
2617    */
2618   GNUNET_FS_DIRSCANNER_ALL_COUNTED,
2619
2620   /**
2621    * We've finished extracting meta data from a file.
2622    */
2623   GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED,
2624
2625   /**
2626    * Last call to the progress function: we have finished scanning
2627    * the directory.
2628    */
2629   GNUNET_FS_DIRSCANNER_FINISHED,
2630
2631   /**
2632    * We're having trouble accessing a file (soft-error); it will
2633    * be ignored.
2634    */
2635   GNUNET_FS_DIRSCANNER_DOES_NOT_EXIST,
2636
2637   /**
2638    * There was an internal error.  Application should abort the scan.
2639    */
2640   GNUNET_FS_DIRSCANNER_INTERNAL_ERROR
2641
2642 };
2643
2644
2645 /**
2646  * Function called over time as the directory scanner makes
2647  * progress on the job at hand.
2648  *
2649  * @param cls closure
2650  * @param ds handle to the directory scanner (NEEDED!?)
2651  * @param filename which file we are making progress on
2652  * @param is_directory GNUNET_YES if this is a directory,
2653  *                     GNUNET_NO if this is a file
2654  *                     GNUNET_SYSERR if it is neither (or unknown)
2655  * @param reason kind of progress we are making
2656  */
2657 typedef void (*GNUNET_FS_DirScannerProgressCallback) (void *cls, 
2658                                                       struct GNUNET_FS_DirScanner *ds, 
2659                                                       const char *filename,
2660                                                       int is_directory, 
2661                                                       enum GNUNET_FS_DirScannerProgressUpdateReason reason);
2662
2663
2664 /**
2665  * A node of a directory tree (produced by dirscanner)
2666  */
2667 struct GNUNET_FS_ShareTreeItem
2668 {
2669   /**
2670    * This is a doubly-linked list
2671    */
2672   struct GNUNET_FS_ShareTreeItem *prev;
2673
2674   /**
2675    * This is a doubly-linked list
2676    */
2677   struct GNUNET_FS_ShareTreeItem *next;
2678
2679   /**
2680    * This is a doubly-linked tree
2681    * NULL for top-level entries.
2682    */
2683   struct GNUNET_FS_ShareTreeItem *parent;
2684
2685   /**
2686    * This is a doubly-linked tree
2687    * NULL for files and empty directories
2688    */
2689   struct GNUNET_FS_ShareTreeItem *children_head;
2690
2691   /**
2692    * This is a doubly-linked tree
2693    * NULL for files and empty directories
2694    */
2695   struct GNUNET_FS_ShareTreeItem *children_tail;
2696
2697   /**
2698    * Metadata for this file or directory
2699    */
2700   struct GNUNET_CONTAINER_MetaData *meta;
2701
2702   /**
2703    * Keywords for this file or directory (derived from metadata).
2704    */
2705   struct GNUNET_FS_Uri *ksk_uri;
2706
2707   /**
2708    * Name of the file/directory
2709    */
2710   char *filename;
2711
2712   /**
2713    * Base name of the file/directory
2714    */
2715   char *short_filename;
2716
2717   /**
2718    * Size of the file (if it is a file), in bytes
2719    */
2720   uint64_t file_size;
2721
2722   /**
2723    * GNUNET_YES if this is a directory
2724    */
2725   int is_directory;
2726
2727 };
2728
2729
2730 /**
2731  * Opaqe handle to an asynchronous directory scanning activity.
2732  */
2733 struct GNUNET_FS_DirScanner;
2734
2735
2736 /**
2737  * Start a directory scanner.
2738  *
2739  * @param filename name of the directory to scan
2740  * @param GNUNET_YES to not to run libextractor on files (only build a tree)
2741  * @param ex if not NULL, must be a list of extra plugins for extractor
2742  * @param cb the callback to call when there are scanning progress messages
2743  * @param cb_cls closure for 'cb'
2744  * @return directory scanner object to be used for controlling the scanner
2745  */
2746 struct GNUNET_FS_DirScanner *
2747 GNUNET_FS_directory_scan_start (const char *filename,
2748                                 int disable_extractor, 
2749                                 const char *ex,
2750                                 GNUNET_FS_DirScannerProgressCallback cb, 
2751                                 void *cb_cls);
2752
2753
2754 /**
2755  * Abort the scan.
2756  *
2757  * @param ds directory scanner structure
2758  */
2759 void
2760 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds);
2761
2762
2763 /**
2764  * Obtain the result of the scan after the scan has signalled
2765  * completion.  Must not be called prior to completion.  The 'ds' is
2766  * freed as part of this call.
2767  *
2768  * @param ds directory scanner structure
2769  * @return the results of the scan (a directory tree)
2770  */
2771 struct GNUNET_FS_ShareTreeItem *
2772 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds);
2773
2774
2775 /**
2776  * Process a share item tree, moving frequent keywords up and
2777  * copying frequent metadata up.
2778  *
2779  * @param toplevel toplevel directory in the tree, returned by the scanner
2780  */
2781 void
2782 GNUNET_FS_share_tree_trim (struct GNUNET_FS_ShareTreeItem *toplevel);
2783
2784
2785 /**
2786  * Release memory of a share item tree.
2787  *
2788  * @param toplevel toplevel of the tree to be freed
2789  */
2790 void
2791 GNUNET_FS_share_tree_free (struct GNUNET_FS_ShareTreeItem *toplevel);
2792
2793
2794 #if 0                           /* keep Emacsens' auto-indent happy */
2795 {
2796 #endif
2797 #ifdef __cplusplus
2798 }
2799 #endif
2800
2801
2802 #endif