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