4a31659ca878dfc74210a1793c94d9b6f60fdf02
[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 2, 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 - review:
21 * directory creation/inspection API
22 * unindex start/stop API
23 * resume notifications 
24 * ProgressCallback: struct/union instead of tons of args?
25 * download options (no temporary files -- what about no files at all?)
26
27 /**
28  * @file include/gnunet_fs_service.h
29  * @brief support for file-sharing via GNUnet 
30  * @author Christian Grothoff
31  */
32
33 #ifndef GNUNET_FS_LIB_H
34 #define GNUNET_FS_LIB_H
35
36 #include "gnunet_util_lib.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 /**
47  * Version number of the implementation.
48  * History:
49  *
50  * 1.x.x: initial version with triple GNUNET_hash and merkle tree
51  * 2.x.x: root node with mime-type, filename and version number
52  * 2.1.x: combined GNUNET_EC_ContentHashKey/3HASH encoding with 25:1 super-nodes
53  * 2.2.x: with directories
54  * 3.0.x: with namespaces
55  * 3.1.x: with namespace meta-data
56  * 3.2.x: with collections
57  * 4.0.x: with expiration, variable meta-data, kblocks
58  * 4.1.x: with new error and configuration handling
59  * 5.0.x: with location URIs
60  * 6.0.0: with support for OR in KSKs
61  * 6.1.x: with simplified namespace support
62  * 9.0.0: CPS-style integrated API
63  */
64 #define GNUNET_FS_VERSION 0x00090000
65
66 #define GNUNET_DIRECTORY_MIME  "application/gnunet-directory"
67 #define GNUNET_DIRECTORY_MAGIC "\211GND\r\n\032\n"
68 #define GNUNET_DIRECTORY_EXT   ".gnd"
69
70 /* URI API */ 
71
72 #define GNUNET_FS_URI_PREFIX      "gnunet://fs/"
73 #define GNUNET_FS_SEARCH_INFIX    "ksk/"
74 #define GNUNET_FS_SUBSPACE_INFIX  "sks/"
75 #define GNUNET_FS_FILE_INFIX      "chk/"
76 #define GNUNET_FS_LOCATION_INFIX  "loc/"
77
78 /**
79  * Iterator over keywords
80  *
81  * @param cls closure
82  * @param keyword the keyword
83  * @param is_mandatory is the keyword mandatory (in a search)
84  * @return GNUNET_OK to continue to iterate, GNUNET_SYSERR to abort
85  */
86 typedef int (*GNUNET_FS_KeywordIterator) (void *cls,
87                                           const char *keyword,
88                                           int is_mandatory);
89
90
91 /**
92  * Does the meta-data claim that this is a directory?
93  * Checks if the mime-type is that of a GNUnet directory.
94  *
95  * @return GNUNET_YES if it is, GNUNET_NO if it is not, GNUNET_SYSERR if
96  *  we have no mime-type information (treat as 'GNUNET_NO')
97  */
98 int 
99 GNUNET_FS_meta_data_test_for_directory (const struct GNUNET_CONTAINER_MetaData *md);
100
101
102 /**
103  * A URI (in internal representation).
104  */
105 struct GNUNET_FS_Uri;
106
107
108 /**
109  * Get a unique key from a URI.  This is for putting URIs
110  * into HashMaps.  The key may change between FS implementations.
111  *
112  * @param uri uri to convert to a unique key
113  * @param key wherer to store the unique key
114  */
115 void 
116 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
117                       GNUNET_HashCode * key);
118
119 /**
120  * Convert a URI to a UTF-8 String.
121  *
122  * @param uri uri to convert to a string
123  * @return the UTF-8 string
124  */
125 char *
126 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri);
127
128 /**
129  * Convert keyword URI to a human readable format
130  * (i.e. the search query that was used in the first place)
131  *
132  * @param uri ksk uri to convert to a string 
133  * @return string with the keywords
134  */
135 char *
136 GNUNET_FS_ksk_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri);
137
138 /**
139  * Convert a UTF-8 String to a URI.
140  *
141  * @param uri string to parse
142  * @param emsg where to store the parser error message (if any)
143  * @return NULL on error
144  */
145 struct GNUNET_FS_Uri *
146 GNUNET_FS_uri_parse (const char *uri,
147                      char **emsg);
148
149 /**
150  * Free URI.
151  *
152  * @param uri uri to free
153  */
154 void 
155 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri);
156
157
158 /**
159  * How many keywords are ANDed in this keyword URI?
160  *
161  * @param uri ksk uri to get the number of keywords from
162  * @return 0 if this is not a keyword URI
163  */
164 unsigned int 
165 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri);
166
167
168 /**
169  * Iterate over all keywords in this keyword URI.
170  *
171  * @param uri ksk uri to get the keywords from
172  * @param iterator function to call on each keyword
173  * @param iterator_cls closure for iterator
174  * @return -1 if this is not a keyword URI, otherwise number of
175  *   keywords iterated over until iterator aborted
176  */
177 int 
178 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
179                                 GNUNET_FS_KeywordIterator iterator, 
180                                 void *iterator_cls);
181
182
183 /**
184  * Obtain the identity of the peer offering the data
185  *
186  * @param uri the location URI to inspect
187  * @param peer where to store the identify of the peer (presumably) offering the content
188  * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
189  */
190 int
191 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
192                                      struct GNUNET_PeerIdentity * peer);
193
194
195 /**
196  * Obtain the URI of the content itself.
197  *
198  * @param uri location URI to get the content URI from
199  * @return NULL if argument is not a location URI
200  */
201 struct GNUNET_FS_Uri *
202 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri);
203
204
205 /**
206  * Construct a location URI (this peer will be used for the location).
207  *
208  * @param baseURI content offered by the sender
209  * @param cfg configuration information (used to find our hostkey)
210  * @param expiration_time how long will the content be offered?
211  * @return the location URI, NULL on error
212  */
213 struct GNUNET_FS_Uri *
214 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
215                           struct GNUNET_CONFIGURATION_Handle *cfg,
216                           struct GNUNET_TIME_Absolute expiration_time);
217
218
219 /**
220  * Duplicate URI.
221  *
222  * @param uri the URI to duplicate
223  * @return copy of the URI
224  */
225 struct GNUNET_FS_Uri *
226 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri);
227
228
229 /**
230  * Create an FS URI from a single user-supplied string of keywords.
231  * The string is broken up at spaces into individual keywords.
232  * Keywords that start with "+" are mandatory.  Double-quotes can
233  * be used to prevent breaking up strings at spaces (and also
234  * to specify non-mandatory keywords starting with "+").
235  *
236  * Keywords must contain a balanced number of double quotes and
237  * double quotes can not be used in the actual keywords (for
238  * example, the string '""foo bar""' will be turned into two
239  * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
240  *
241  * @param keywords the keyword string
242  * @return an FS URI for the given keywords, NULL
243  *  if keywords is not legal (i.e. empty).
244  */
245 struct GNUNET_FS_Uri *
246 GNUNET_FS_uri_ksk_create (const char *keywords);
247
248
249 /**
250  * Create an FS URI from a user-supplied command line of keywords.
251  * Arguments should start with "+" to indicate mandatory
252  * keywords.
253  *
254  * @param argc number of keywords
255  * @param argv keywords (double quotes are not required for
256  *             keywords containing spaces; however, double
257  *             quotes are required for keywords starting with
258  *             "+"); there is no mechanism for having double
259  *             quotes in the actual keywords (if the user
260  *             did specifically specify double quotes, the
261  *             caller should convert each double quote
262  *             into two single quotes).
263  * @return an FS URI for the given keywords, NULL
264  *  if keywords is not legal (i.e. empty).
265  */
266 struct GNUNET_FS_Uri *
267 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
268                                     const char **argv);
269
270
271 /**
272  * Test if two URIs are equal.
273  *
274  * @param u1 one of the URIs
275  * @param u2 the other URI
276  * @return GNUNET_YES if the URIs are equal
277  */
278 int 
279 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
280                           const struct GNUNET_FS_Uri *u2);
281
282
283 /**
284  * Is this a namespace URI?
285  *
286  * @param uri the uri to check
287  * @return GNUNET_YES if this is an SKS uri
288  */
289 int
290 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri);
291
292
293 /**
294  * Get the ID of a namespace from the given
295  * namespace URI.
296  *
297  * @param uri the uri to get the namespace ID from
298  * @param nsid where to store the ID of the namespace
299  * @return GNUNET_OK on success
300  */
301 int 
302 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
303                                  GNUNET_HashCode * nsid);
304
305
306 /**
307  * Get the content identifier of an SKS URI.
308  *
309  * @param uri the sks uri
310  * @return NULL on error (not a valid SKS URI)
311  */
312 char *
313 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri);
314
315
316 /**
317  * Convert namespace URI to a human readable format
318  * (using the namespace description, if available).
319  *
320  * @param cfg configuration to use
321  * @param uri SKS uri to convert
322  * @return NULL on error (not an SKS URI)
323  */
324 char *
325 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
326                                    const struct GNUNET_FS_Uri *uri);
327
328
329 /**
330  * Is this a keyword URI?
331  *
332  * @param uri the uri
333  * @return GNUNET_YES if this is a KSK uri
334  */
335 int 
336 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri);
337
338
339 /**
340  * Is this a file (or directory) URI?
341  *
342  * @param uri the uri to check
343  * @return GNUNET_YES if this is a CHK uri
344  */
345 int 
346 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri);
347
348
349 /**
350  * What is the size of the file that this URI
351  * refers to?
352  *
353  * @param uri the CHK URI to inspect
354  * @return size of the file as specified in the CHK URI
355  */
356 uint64_t 
357 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri);
358
359
360 /**
361  * Is this a location URI?
362  *
363  * @param uri the uri to check
364  * @return GNUNET_YES if this is a LOC uri
365  */
366 int 
367 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri);
368
369
370 /**
371  * Construct a keyword-URI from meta-data (take all entries
372  * in the meta-data and construct one large keyword URI
373  * that lists all keywords that can be found in the meta-data).
374  * @deprecated
375  */
376 struct GNUNET_FS_Uri *
377 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_MetaData *md);
378
379
380 /**
381  * Command-line option parser function that allows the user
382  * to specify one or more '-k' options with keywords.  Each
383  * specified keyword will be added to the URI.  A pointer to
384  * the URI must be passed as the "scls" argument.
385  *
386  * @param ctx command line processor context
387  * @param scls must be of type "struct GNUNET_FS_Uri **"
388  * @param option name of the option (typically 'k')
389  * @param value command line argument given
390  * @return GNUNET_OK on success
391  */
392 int
393 GNUNET_FS_getopt_configure_set_keywords (GNUNET_GETOPT_CommandLineProcessorContext* ctx, 
394                                          void *scls,
395                                          const char *option,
396                                          const char *value);
397
398
399 /**
400  * Command-line option parser function that allows the user to specify
401  * one or more '-m' options with metadata.  Each specified entry of
402  * the form "type=value" will be added to the metadata.  A pointer to
403  * the metadata must be passed as the "scls" argument.
404  *
405  * @param ctx command line processor context
406  * @param scls must be of type "struct GNUNET_MetaData **"
407  * @param option name of the option (typically 'k')
408  * @param value command line argument given
409  * @return GNUNET_OK on success
410  */
411 int
412 GNUNET_FS_getopt_configure_set_metadata (GNUNET_GETOPT_CommandLineProcessorContext* ctx, 
413                                          void *scls,
414                                          const char *option,
415                                          const char *value);
416
417
418
419 /* ************************* sharing API ***************** */
420
421
422 /**
423  * Possible status codes used in the callback for the 
424  * various file-sharing operations.  On each file (or search),
425  * the callback is guaranteed to be called once with "START"
426  * and once with STOPPED; calls with PROGRESS, ERROR or COMPLETED
427  * are optional and depend on the circumstances; parent operations
428  * will be STARTED before child-operations and STOPPED after
429  * their respective child-operations.  START and STOP signals 
430  * are typically generated either due to explicit client requests
431  * or because of suspend/resume operations.
432  */
433 enum GNUNET_FS_Status
434 {
435   GNUNET_FS_STATUS_SHARE_START,
436   GNUNET_FS_STATUS_SHARE_RESUME,
437   GNUNET_FS_STATUS_SHARE_SUSPEND,
438   GNUNET_FS_STATUS_SHARE_PROGRESS,
439   GNUNET_FS_STATUS_SHARE_ERROR,
440   GNUNET_FS_STATUS_SHARE_COMPLETED,
441   GNUNET_FS_STATUS_SHARE_STOPPED,
442   GNUNET_FS_STATUS_DOWNLOAD_START,
443   GNUNET_FS_STATUS_DOWNLOAD_RESUME,
444   GNUNET_FS_STATUS_DOWNLOAD_SUSPEND,
445   GNUNET_FS_STATUS_DOWNLOAD_PROGRESS,
446   GNUNET_FS_STATUS_DOWNLOAD_ERROR,
447   GNUNET_FS_STATUS_DOWNLOAD_COMPLETED,
448   GNUNET_FS_STATUS_DOWNLOAD_STOPPED,
449   GNUNET_FS_STATUS_SEARCH_START,
450   GNUNET_FS_STATUS_SEARCH_RESUME,
451   GNUNET_FS_STATUS_SEARCH_RESUME_RESULT,
452   GNUNET_FS_STATUS_SEARCH_SUSPEND,
453   GNUNET_FS_STATUS_SEARCH_RESULT,
454   GNUNET_FS_STATUS_SEARCH_UPDATE,
455   GNUNET_FS_STATUS_SEARCH_ERROR,
456   GNUNET_FS_STATUS_SEARCH_STOPPED,
457   GNUNET_FS_STATUS_UNINDEX_START,
458   GNUNET_FS_STATUS_UNINDEX_RESUME,
459   GNUNET_FS_STATUS_UNINDEX_SUSPEND,
460   GNUNET_FS_STATUS_UNINDEX_PROGRESS,
461   GNUNET_FS_STATUS_UNINDEX_ERROR,
462   GNUNET_FS_STATUS_UNINDEX_STOPPED,
463   GNUNET_FS_STATUS_NAMESPACE_DISCOVERED
464 };
465
466
467 /**
468  * Notification of FS to a client about the progress of an 
469  * operation.  Callbacks of this type will be used for uploads,
470  * downloads and searches.  Some of the arguments depend a bit 
471  * in their meaning on the context in which the callback is used.
472  *
473  * @param cls closure
474  * @param cctx client-context (for the next progress call
475  *        for this operation; should be set to NULL for
476  *        SUSPEND and STOPPED events)
477  * @param ctx location where the callback can store a context pointer
478  *        to keep track of things for this specific operation
479  * @param pctx context pointer set by the callback for the parent operation
480  *        (NULL if there is no parent operation); for a search result,
481  *        the actual search is the parent and the individual search results
482  *        are the children (multiple calls for the same search result can
483  *        be used whenever availability/certainty or metadata values change)
484  * @param filename name of the file that this update is about, NULL for 
485  *        searches
486  * @param availability value between 0 and 100 indicating how likely
487  *        we think it is that this search result is actually available
488  *        in the network (or, in the case of a download, that the download 
489  *        will complete); always 100 for uploads; percentage of blocks
490  *        that could be unindexed so far for unindexing operations
491  *        (indicates how many blocks in the indexed file changed in 
492  *        the meantime)
493  * @param certainty how certain are we that the availability value is
494  *        actually correct?  certainty is also between 0 and 100.
495  * @param fsize number of bytes that will need to be processed (for this file)
496  * @param completed number of bytes that have been processed (for this file)
497  * @param offset offset of the data of buffer in the file
498  * @param eta absolute estimated time for the completion of the operation
499  * @param uri pointer to CHK URI for search results and downloads; pointer
500  *        to KSK uri for uploads; client can modify KSK uri to change the
501  *        set of keywords that will be used
502  * @param meta metadata for search results and downloads (NULL for downloads
503  *        if no metadata is available); can be modified for uploads to change
504  *        metadata that will be used
505  * @param bsize number of bytes in the buffer
506  * @param buffer pointer to the last bytes processed; will be a plaintext
507  *        buffer for files (with content downloaded or uploaded) and 
508  *        NULL when searching; points to an error message of bsize bytes
509  *        if this callback is used to signal an error
510  * @return GNUNET_SYSERR to abort the overall operation; GNUNET_NO to
511  *        stop this specific operation (do not share this file or skip
512  *        this download; GNUNET_NO has no meaning for search results);
513  *        GNUNET_YES to continue processing as usual
514  * @deprecated (use 2-arg function getting union argument instead)
515  */
516 typedef int (*GNUNET_FS_ProgressCallback)
517   (void *cls,
518    void **cctx,
519    const struct GNUNET_FS_ProgressInfo *info);
520
521
522    void **ctx,
523    void *pctx,
524    const char *filename,
525    enum GNUNET_FS_Status status,
526    float availability,
527    float certainty,
528    uint64_t fsize,
529    uint64_t completed, 
530    uint64_t offset, struct GNUNET_TIME_Absolute eta,
531    struct GNUNET_FS_Uri **uri,
532    struct GNUNET_CONTAINER_MetaData *meta,
533    size_t bsize, const void *buffer);
534
535
536
537 /**
538  * Handle to one of our namespaces.
539  */
540 struct GNUNET_FS_Namespace;
541
542
543 /**
544  * Handle for controlling an upload.
545  */
546 struct GNUNET_FS_ShareContext;
547
548
549 /**
550  * Handle for controlling an unindexing operation.
551  */
552 struct GNUNET_FS_UnindexContext;
553
554
555 /**
556  * Handle for controlling a search.
557  */
558 struct GNUNET_FS_SearchContext;
559
560
561 /**
562  * Context for controlling a download.
563  */
564 struct GNUNET_FS_DownloadContext;
565
566
567 /**
568  * Handle for detail information about a file that is being shared.
569  * Specifies metadata, keywords, how to get the contents of the file
570  * (i.e. data-buffer in memory, filename on disk) and other options.
571  */
572 struct GNUNET_FS_FileInformation;
573
574
575 /**
576  * Argument given to the progress callback with
577  * information about what is going on.
578  */
579 struct GNUNET_FS_ProgressInfo
580 {  
581
582   /**
583    * Values that depend on the event type.
584    */
585   union {
586     
587     /**
588      * Values for all "GNUNET_FS_STATUS_SHARE_*" events.
589      */
590     struct {
591
592       /**
593        * Context for controlling the upload.
594        */
595       struct GNUNET_FS_ShareContext *sc;
596
597       /**
598        * Information about the file that is being shared.
599        */
600       struct GNUNET_FS_FileInformation *fi;
601
602       /**
603        * Client context pointer (set the last time
604        * by the client for this operation; initially
605        * NULL on START/RESUME events).
606        */
607       void *cctx;
608
609       /**
610        * Client context pointer for the parent operation
611        * (if this is a file in a directory or a subdirectory).
612        */
613       void *pctx;
614       
615       /**
616        * How large is the file overall?  For directories,
617        * this is only the size of the directory itself,
618        * not of the other files contained within the 
619        * directory.
620        */
621       uint64_t size;
622
623       /**
624        * At what time do we expect to finish the upload?
625        * (will be a value in the past for completed
626        * uploads).
627        */ 
628       struct GNUNET_TIME_Absolute eta;
629
630       /**
631        * How many bytes have we completed?
632        */
633       uint64_t completed;
634
635       /**
636        * Additional values for specific events.
637        */
638       union {
639
640         /**
641          * These values are only valid for
642          * GNUNET_FS_STATUS_SHARE_PROGRESS events.
643          */
644         struct {
645           
646           /**
647            * Data block we just published.
648            */
649           const void *data;
650           
651           /**
652            * At what offset in the file is "data"?
653            */
654           uint64_t offset;
655           
656           /**
657            * Length of the data block.
658            */
659           uint64_t data_len;
660
661         } progress;
662
663         /**
664          * These values are only valid for
665          * GNUNET_FS_STATUS_SHARE_ERROR events.
666          */
667         struct {
668           
669           /**
670            * Error message.
671            */
672           const char *message;
673
674         } error;
675
676       } specifics;
677
678     } share;
679
680     
681     /**
682      * Values for all "GNUNET_FS_STATUS_DOWNLOAD_*" events.
683      */
684     struct {
685
686       /**
687        * Context for controlling the download.
688        */
689       struct GNUNET_FS_DownloadContext *dc;
690
691       /**
692        * Client context pointer (set the last time
693        * by the client for this operation; initially
694        * NULL on START/RESUME events).
695        */
696       void *cctx;
697
698       /**
699        * Client context pointer for the parent operation
700        * (if this is a file in a directory or a subdirectory).
701        */
702       void *pctx;
703       
704       /**
705        * How large is the file overall?  For directories,
706        * this is only the size of the directory itself,
707        * not of the other files contained within the 
708        * directory.
709        */
710       uint64_t size;
711
712       /**
713        * At what time do we expect to finish the upload?
714        * (will be a value in the past for completed
715        * uploads).
716        */ 
717       struct GNUNET_TIME_Absolute eta;
718
719       /**
720        * How many bytes have we completed?
721        */
722       uint64_t completed;
723
724       /**
725        * Additional values for specific events.
726        */
727       union {
728         
729         /**
730          * These values are only valid for
731          * GNUNET_FS_STATUS_DOWNLOAD_PROGRESS events.
732          */
733         struct {
734   
735           /**
736            * Data block we just obtained.
737            */
738           const void *data;
739           
740           /**
741            * At what offset in the file is "data"?
742            */
743           uint64_t offset;
744           
745           /**
746            * Length of the data block.
747            */
748           uint64_t data_len;
749
750           /**
751            * Amount of trust we offered to get the block.
752            */
753           unsigned int trust_offered;     
754
755         } progress;
756
757         /**
758          * These values are only valid for
759          * GNUNET_FS_STATUS_DOWNLOAD_ERROR events.
760          */
761         struct {
762
763           /**
764            * Error message.
765            */
766           const char *message;
767
768         } error;
769
770       } specifics;
771
772     } download;
773
774     /**
775      * Values for all "GNUNET_FS_STATUS_SEARCH_*" events.
776      */
777     struct {
778
779       /**
780        * Context for controlling the search.
781        */
782       struct GNUNET_FS_SearchContext *sc;
783
784       /**
785        * Client context pointer (set the last time by the client for
786        * this operation; initially NULL on START/RESUME events).  Note
787        * that this value can only be set on START/RESUME; setting
788        * "cctx" on RESULT/RESUME_RESULT will actually update the
789        * private context for "UPDATE" events.
790        */
791       void *cctx;
792
793       /**
794        * Additional values for specific events.
795        */
796       union {
797         
798         /**
799          * These values are only valid for
800          * GNUNET_FS_STATUS_SEARCH_RESULT events.
801          */
802         struct {
803           
804           /**
805            * Metadata for the search result.
806            */
807           struct GNUNET_MetaData *meta;
808           // FIXME...
809
810         } result;
811         
812         /**
813          * These values are only valid for
814          * GNUNET_FS_STATUS_SEARCH_RESUME_RESULT events.
815          */
816         struct {
817           
818           /**
819            * Metadata for the search result.
820            */
821           struct GNUNET_MetaData *meta;
822           // FIXME...
823           
824         } resume_result;
825         
826         /**
827          * These values are only valid for
828          * GNUNET_FS_STATUS_SEARCH_UPDATE events.
829          */
830         struct {
831
832           /**
833            * Private context set for for this result
834            * during the "RESULT" event.
835            */
836           void *cctx;
837           // FIXME...
838
839         } update;
840
841         /**
842          * These values are only valid for
843          * GNUNET_FS_STATUS_SEARCH_ERROR events.
844          */
845         struct {
846
847           /**
848            * Error message.
849            */
850           const char *message;
851
852         } error;
853
854       } specifics;
855
856     } search;
857
858     /**
859      * Values for all "GNUNET_FS_STATUS_UNINDEX_*" events.
860      */
861     struct {
862
863       /**
864        * Context for controlling the unindexing.
865        */
866       struct GNUNET_FS_UnindexContext *uc;
867
868       /**
869        * Client context pointer (set the last time
870        * by the client for this operation; initially
871        * NULL on START/RESUME events).
872        */
873       void *cctx;
874
875       /**
876        * How large is the file overall?
877        */
878       uint64_t size;
879
880       /**
881        * At what time do we expect to finish unindexing?
882        * (will be a value in the past for completed
883        * unindexing opeations).
884        */ 
885       struct GNUNET_TIME_Absolute eta;
886
887       /**
888        * How many bytes have we completed?
889        */
890       uint64_t completed;
891
892       /**
893        * Additional values for specific events.
894        */
895       union {
896
897         /**
898          * These values are only valid for
899          * GNUNET_FS_STATUS_UNINDEX_PROGRESS events.
900          */
901         struct {
902   
903           /**
904            * Data block we just unindexed.
905            */
906           const void *data;
907           
908           /**
909            * At what offset in the file is "data"?
910            */
911           uint64_t offset;
912           
913           /**
914            * Length of the data block.
915            */
916           uint64_t data_len;
917
918         } progress;
919
920         /**
921          * These values are only valid for
922          * GNUNET_FS_STATUS_UNINDEX_ERROR events.
923          */
924         struct {
925
926           /**
927            * Error message.
928            */
929           const char *message;
930
931         } error;
932
933       } specifics;
934
935     } unindex;
936
937     
938     /**
939      * Values for all "GNUNET_FS_STATUS_NAMESPACE_*" events.
940      */
941     struct {
942       /**
943        * Handle to the namespace (NULL if it is not a local
944        * namespace).
945        */
946       struct GNUNET_FS_Namespace *ns;
947
948       /**
949        * Short, human-readable name of the namespace.
950        */
951       const char *name;
952
953       /**
954        * Root identifier for the namespace, can be NULL.
955        */
956       const char *root;
957
958       /**
959        * Metadata for the namespace.
960        */
961       struct GNUNET_CONTAINER_MetaData *meta;
962
963       /**
964        * Hash-identifier for the namespace.
965        */
966       struct GNUNET_HashCode id;      
967
968     } namespace;
969
970   } value;
971
972   /**
973    * Specific status code (determines the event type).
974    */  
975   enum GNUNET_FS_Status status;
976
977 };
978
979
980 /**
981  * Handle to the file-sharing service.
982  */
983 struct GNUNET_FS_Handle;
984
985
986 /**
987  * Setup a connection to the file-sharing service.
988  *
989  * @param sched scheduler to use
990  * @param cfg configuration to use
991  * @param client_name unique identifier for this client 
992  * @param upcb function to call to notify about FS actions
993  * @param upcb_cls closure for upcb
994  */
995 struct GNUNET_FS_Handle *
996 GNUNET_FS_start (struct GNUNET_SCHEDULER_Handle *sched,
997                  struct GNUNET_CONFIGURATION_Handle *cfg,
998                  const char *client_name,
999                  GNUNET_FS_ProgressCallback upcb,
1000                  void *upcb_cls);
1001
1002
1003 /**
1004  * Close our connection with the file-sharing service.
1005  * The callback given to GNUNET_FS_start will no longer be
1006  * called after this function returns.
1007  *
1008  * @param h handle that was returned from GNUNET_FS_start
1009  */                    
1010 void 
1011 GNUNET_FS_stop (struct GNUNET_FS_Handle *h); 
1012
1013
1014 /**
1015  * Share a file or directory.
1016  *
1017  * @param h handle to the file sharing subsystem
1018  * @param ctx initial value to use for the '*ctx' in the callback
1019  * @param filename name of the file or directory to share
1020  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
1021  *                GNUNET_SYSERR for simulation
1022  * @param anonymity what is the desired anonymity level for sharing?
1023  * @param priority what is the priority for OUR node to
1024  *   keep this file available?  Use 0 for maximum anonymity and
1025  *   minimum reliability...
1026  * @param expirationTime when should this content expire?
1027  * @param namespace namespace to share the file in, NULL for no namespace
1028  * @param nid identifier to use for the shared content in the namespace
1029  *        (can be NULL, must be NULL if namespace is NULL)
1030  * @param nuid update-identifier that will be used for future updates 
1031  *        (can be NULL, must be NULL if namespace or nid is NULL)
1032  * @deprecated API not powerful enough to share complex directory structures
1033  *         with metadata in general (need to pre-build tree)
1034  */
1035 struct GNUNET_FS_ShareContext *
1036 GNUNET_FS_share_start (struct GNUNET_FS_Handle *h,
1037                        void *ctx,
1038                        const char *filename,
1039                        int do_index,
1040                        unsigned int anonymity,
1041                        unsigned int priority,
1042                        struct GNUNET_TIME_Absolute expirationTime,
1043                        struct GNUNET_FS_Namespace *namespace
1044                        const char *nid,
1045                        const char *nuid);
1046
1047
1048 /**
1049  * Stop an upload.  Will abort incomplete uploads (but 
1050  * not remove blocks that have already been shared) or
1051  * simply clean up the state for completed uploads.
1052  *
1053  * @param sc context for the upload to stop
1054  */
1055 void 
1056 GNUNET_FS_share_stop (struct GNUNET_FS_ShareContext *sc);
1057
1058
1059 /**
1060  * Type of a function called by "GNUNET_FS_get_indexed_files".
1061  *
1062  * @param cls closure
1063  * @param filename the name of the file
1064  * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1065  */
1066 typedef int (*GNUNET_FS_FileProcessor) (void *cls,
1067                                         const char *filename);
1068
1069
1070 /**
1071  * Iterate over all indexed files.
1072  *
1073  * @param h handle to the file sharing subsystem
1074  * @param iterator function to call on each indexed file
1075  * @param iterator_cls closure for iterator
1076  */
1077 void 
1078 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
1079                              GNUNET_FS_FileProcessor iterator,
1080                              void *iterator_cls);
1081
1082
1083
1084
1085
1086 /**
1087  * Unindex a file.
1088  *
1089  * @param h handle to the file sharing subsystem
1090  * @param filename file to unindex
1091  * @return NULL on error, otherwise handle 
1092  */
1093 struct GNUNET_FS_UnindexContext *
1094 GNUNET_FS_unindex (struct GNUNET_FS_Handle *h,
1095                    const char *filename);
1096
1097
1098 /**
1099  * Clean up after completion of an unindex operation.
1100  *
1101  * @param uc handle
1102  */
1103 void
1104 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc);
1105
1106
1107
1108 /**
1109  * Publish an advertismement for a namespace.  
1110  *
1111  * @param h handle to the file sharing subsystem
1112  * @param namespace handle for the namespace that should be advertised
1113  * @param meta meta-data for the namespace advertisement
1114  * @param anonymity for the namespace advertismement
1115  * @param priority for the namespace advertisement
1116  * @param expiration for the namespace advertisement
1117  * @param advertisementURI the keyword (!) URI to advertise the
1118  *        namespace under (we will create a GNUNET_EC_KNBlock)
1119  * @param rootEntry name of the root entry in the namespace (for
1120  *        the namespace advertisement)
1121  *
1122  * @return uri of the advertisement
1123  */
1124 struct GNUNET_FS_Uri *
1125 GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
1126                                struct GNUNET_FS_Namespace *namespace,
1127                                const struct GNUNET_MetaData *meta,
1128                                unsigned int anonymity,
1129                                unsigned int priority,
1130                                struct GNUNET_TIME_Absolute expiration,
1131                                const struct GNUNET_FS_Uri *advertisementURI,
1132                                const char *rootEntry);
1133
1134
1135 /**
1136  * Create a namespace with the given name; if one already
1137  * exists, return a handle to the existing namespace.
1138  *
1139  * @param h handle to the file sharing subsystem
1140  * @param name name to use for the namespace
1141  * @return handle to the namespace, NULL on error
1142  */
1143 struct GNUNET_FS_Namespace *
1144 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h,
1145                             const char *name);
1146
1147
1148 /**
1149  * Delete a namespace handle.  Can be used for a clean shutdown (free
1150  * memory) or also to freeze the namespace to prevent further
1151  * insertions by anyone.
1152  *
1153  * @param namespace handle to the namespace that should be deleted / freed
1154  * @param freeze prevents future insertions; creating a namespace
1155  *        with the same name again will create a fresh namespace instead
1156  *
1157  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1158  */
1159 int 
1160 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace,
1161                             int freeze);
1162
1163
1164 /**
1165  * Callback with information about local (!) namespaces.
1166  * Contains the names of the local namespace and the global
1167  * ID.
1168  *
1169  * @param cls closure
1170  * @param name human-readable identifier of the namespace
1171  * @param id hash identifier for the namespace
1172  */
1173 typedef void (*GNUNET_FS_NamespaceInfoProcessor) (void *cls,
1174                                                   const char *name,
1175                                                   const GNUNET_HashCode *id);
1176
1177
1178 /**
1179  * Build a list of all available local (!) namespaces The returned
1180  * names are only the nicknames since we only iterate over the local
1181  * namespaces.
1182  *
1183  * @param h handle to the file sharing subsystem
1184  * @param cb function to call on each known namespace
1185  * @param cb_cls closure for cb
1186  * @return GNUNET_SYSERR on error, otherwise the number of pseudonyms in list
1187  */
1188 int 
1189 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
1190                           GNUNET_FS_NamespaceProcessor cb,
1191                           void *cb_cls);
1192
1193
1194 /**
1195  * Function called on updateable identifiers.
1196  *
1197  * @param cls closure
1198  * @param last_id last identifier 
1199  * @param last_uri uri used for the content published under the last_id
1200  * @param last_meta metadata associated with last_uri
1201  * @param next_id identifier that should be used for updates
1202  */
1203 typedef void 
1204 (*GNUNET_FS_IdentifierProcessor)(void *cls,
1205                                  const char *last_id, 
1206                                  const struct GNUNET_FS_Uri *last_uri,
1207                                  const struct GNUNET_CONTAINER_MetaData *last_meta,
1208                                  const char *next_id);
1209
1210
1211 /**
1212  * List all of the identifiers in the namespace for 
1213  * which we could produce an update.
1214  *
1215  * @param namespace namespace to inspect for updateable content
1216  * @param ip function to call on each updateable identifier
1217  * @param ip_cls closure for ip
1218  */
1219 void
1220 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
1221                                      GNUNET_FS_IdentifierProcessor ip, 
1222                                      void *ip_cls);
1223
1224
1225 /**
1226  * Start search for content.
1227  *
1228  * @param h handle to the file sharing subsystem
1229  * @param uri specifies the search parameters; can be
1230  *        a KSK URI or an SKS URI.
1231  * @param anonymity desired level of anonymity
1232  * @return context that can be used to control the search
1233  */
1234 struct GNUNET_FS_SearchContext *
1235 GNUNET_FS_search_start (struct GNUNET_FS_Handle *h,
1236                         const struct GNUNET_FS_Uri *uri,
1237                         unsigned int anonymity);
1238
1239
1240 /**
1241  * Pause search.  
1242  *
1243  * @param sc context for the search that should be paused
1244  */
1245 void 
1246 GNUNET_FS_search_pause (struct GNUNET_FS_SearchContext *sc);
1247
1248
1249 /**
1250  * Resume paused search.
1251  *
1252  * @param sc context for the search that should be resumed
1253  */
1254 void 
1255 GNUNET_FS_search_resume (struct GNUNET_FS_SearchContext *sc);
1256
1257
1258 /**
1259  * Stop search for content.
1260  *
1261  * @param sc context for the search that should be stopped
1262  */
1263 void 
1264 GNUNET_FS_search_stop (struct GNUNET_FS_SearchContext *sc);
1265
1266
1267 /**
1268  * Download parts of a file.  Note that this will store
1269  * the blocks at the respective offset in the given file.  Also, the
1270  * download is still using the blocking of the underlying FS
1271  * encoding.  As a result, the download may *write* outside of the
1272  * given boundaries (if offset and length do not match the 32k FS
1273  * block boundaries). <p>
1274  *
1275  * This function should be used to focus a download towards a
1276  * particular portion of the file (optimization), not to strictly
1277  * limit the download to exactly those bytes.
1278  *
1279  * @param h handle to the file sharing subsystem
1280  * @param uri the URI of the file (determines what to download); CHK or LOC URI
1281  * @param filename where to store the file, maybe NULL (then no file is
1282  *        created on disk and data must be grabbed from the callbacks)
1283  * @param offset at what offset should we start the download (typically 0)
1284  * @param length how many bytes should be downloaded starting at offset
1285  * @param anonymity anonymity level to use for the download
1286  * @param no_temporaries set to GNUNET_YES to disallow generation of temporary files
1287  * @param recursive should this be a recursive download (useful for directories
1288  *        to automatically trigger download of files in the directories)
1289  * @param parent parent download to associate this download with (use NULL
1290  *        for top-level downloads; useful for manually-triggered recursive downloads)
1291  * @return context that can be used to control this download
1292  */
1293 struct GNUNET_FS_DownloadContext *
1294 GNUNET_FS_file_download_start (struct GNUNET_FS_Handle *h,
1295                                const struct GNUNET_FS_Uri *uri,
1296                                const char *filename,
1297                                unsigned long long offset,
1298                                unsigned long long length,
1299                                unsigned int anonymity,
1300                                int no_temporaries,      
1301                                int recursive,
1302                                struct GNUNET_FS_DownloadContext *parent);
1303
1304
1305 /**
1306  * Stop a download (aborts if download is incomplete).
1307  *
1308  * @param rm handle for the download
1309  * @param do_delete delete files of incomplete downloads
1310  */
1311 void
1312 GNUNET_FS_file_download_stop (struct GNUNET_FS_DownloadContext *rm,
1313                               int do_delete);
1314
1315
1316 /**
1317  * Iterate over all entries in a directory.  Note that directories
1318  * are structured such that it is possible to iterate over the
1319  * individual blocks as well as over the entire directory.  Thus
1320  * a client can call this function on the buffer in the
1321  * GNUNET_FS_ProgressCallback.
1322  *
1323  * @param size number of bytes in data
1324  * @param data pointer to the beginning of the directory
1325  * @param offset offset of data in the directory
1326  * @param spcb function to call on each entry
1327  * @param spcb_cls closure for spcb
1328  */
1329 void 
1330 GNUNET_FS_directory_list_contents (size_t size,
1331                                    const void *data,
1332                                    uint64_t offset,
1333                                    GNUNET_FS_SearchResultProcessor spcb, 
1334                                    void *spcb_cls);
1335
1336
1337 /**
1338  * Create a directory.
1339  *
1340  * @param data pointer set to the beginning of the directory
1341  * @param len set to number of bytes in data
1342  * @param count number of entries in uris and metaDatas
1343
1344  * @param uris URIs of the files in the directory
1345  * @param metaDatas meta-data for the files (must match
1346  *        respective values at same offset in in uris)
1347  * @param meta meta-data for the directory.  The meta entry
1348  *        is extended with the mime-type for a GNUnet directory.
1349
1350  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1351  * @deprecated (not powerful enough?)
1352  */
1353 int 
1354 GNUNET_FS_directory_create (char **data,
1355                             uint64_t *len,
1356                             unsigned int count,
1357                             const GNUNET_FS_FileInfo * fis,
1358                             struct GNUNET_MetaData *meta);
1359
1360
1361 /**
1362  * Initialize collection.
1363  *
1364  * @param h handle to the file sharing subsystem
1365  * @param namespace namespace to use for the collection
1366  * @return GNUNET_OK on success, GNUNET_SYSERR if another
1367  *         namespace is already set for our collection
1368  */
1369 int 
1370 GNUNET_FS_collection_start (struct GNUNET_FS_Handle *h,
1371                             struct GNUNET_FS_Namespace *namespace);
1372
1373
1374 /**
1375  * Stop collection.
1376  *
1377  * @param h handle to the file sharing subsystem
1378  * @return GNUNET_OK on success, GNUNET_SYSERR if no collection is active
1379  */
1380 int 
1381 GNUNET_CO_collection_stop (struct GNUNET_FS_Handle *h);
1382
1383
1384 /**
1385  * Are we using a collection?
1386  *
1387  * @param h handle to the file sharing subsystem
1388  * @return NULL if there is no collection,
1389  */
1390 struct GNUNET_FS_Namespace *
1391 GNUNET_FS_collection_get(struct GNUNET_FS_Handle *h);
1392
1393
1394 /**
1395  * Publish an update of the current collection information to the
1396  * network now.  The function has no effect if the collection has not
1397  * changed since the last publication.  If we are currently not
1398  * collecting, this function does nothing.
1399  *
1400  * @param h handle to the file sharing subsystem
1401  */
1402 void GNUNET_FS_collection_publish (struct GNUNET_FS_Handle *h);
1403
1404
1405 /**
1406  * If we are currently building a collection, publish the given file
1407  * information in that collection.  If we are currently not
1408  * collecting, this function does nothing.
1409  *
1410  * @param h handle to the file sharing subsystem
1411  * @param uri uri to add to the collection
1412  * @param meta metadata for the uri
1413  */
1414 void GNUNET_FS_collection_add (const struct GNUNET_FS_Handle *h,
1415                                const struct GNUNET_FS_Uri *uri,
1416                                const struct GNUNET_CONTAINER_MetaData *meta);
1417
1418
1419
1420 #if 0                           /* keep Emacsens' auto-indent happy */
1421 {
1422 #endif
1423 #ifdef __cplusplus
1424 }
1425 #endif
1426
1427
1428 #endif