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