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