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