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