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