better datastore API
[oweals/gnunet.git] / src / include / gnunet_disk_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 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
21 /**
22  * @file include/gnunet_disk_lib.h
23  * @brief disk IO apis
24  */
25 #ifndef GNUNET_DISK_LIB_H
26 #define GNUNET_DISK_LIB_H
27
28 /**
29  * Opaque handle used to access files.
30  */
31 struct GNUNET_DISK_FileHandle;
32
33 /**
34  * Opaque handle used to manage a pipe.
35  */
36 struct GNUNET_DISK_PipeHandle;
37
38
39 /* we need size_t, and since it can be both unsigned int
40    or unsigned long long, this IS platform dependent;
41    but "stdlib.h" should be portable 'enough' to be
42    unconditionally available... */
43 #include <stdlib.h>
44 #include "gnunet_configuration_lib.h"
45 #include "gnunet_scheduler_lib.h"
46
47 #ifdef __cplusplus
48 extern "C"
49 {
50 #if 0                           /* keep Emacsens' auto-indent happy */
51 }
52 #endif
53 #endif
54
55
56 /* Open the file for reading */
57 #define GNUNET_DISK_OPEN_READ           1
58 /* Open the file for writing */
59 #define GNUNET_DISK_OPEN_WRITE          2
60 /* Open the file for both reading and writing */
61 #define GNUNET_DISK_OPEN_READWRITE      3
62 /* Fail if file already exists */
63 #define GNUNET_DISK_OPEN_FAILIFEXISTS   4
64 /* Truncate file if it exists */
65 #define GNUNET_DISK_OPEN_TRUNCATE       8
66 /* Create file if it doesn't exist */
67 #define GNUNET_DISK_OPEN_CREATE         16
68 /* Append to the file */
69 #define GNUNET_DISK_OPEN_APPEND         32
70
71 #define GNUNET_DISK_MAP_READ    1
72 #define GNUNET_DISK_MAP_WRITE   2
73 #define GNUNET_DISK_MAP_READWRITE 3
74
75 #define GNUNET_DISK_PERM_USER_READ      1
76 #define GNUNET_DISK_PERM_USER_WRITE     2
77 #define GNUNET_DISK_PERM_USER_EXEC      4
78 #define GNUNET_DISK_PERM_GROUP_READ     8
79 #define GNUNET_DISK_PERM_GROUP_WRITE    16
80 #define GNUNET_DISK_PERM_GROUP_EXEC     32
81 #define GNUNET_DISK_PERM_OTHER_READ     64
82 #define GNUNET_DISK_PERM_OTHER_WRITE    128
83 #define GNUNET_DISK_PERM_OTHER_EXEC     256
84
85 enum GNUNET_DISK_Seek 
86   {
87     GNUNET_DISK_SEEK_SET, 
88     GNUNET_DISK_SEEK_CUR, 
89     GNUNET_DISK_SEEK_END
90   };
91
92 /**
93  * Get the number of blocks that are left on the partition that
94  * contains the given file (for normal users).
95  *
96  * @param part a file on the partition to check
97  * @return -1 on errors, otherwise the number of free blocks
98  */
99 long GNUNET_DISK_get_blocks_available (const char *part);
100
101
102 /**
103  * Checks whether a handle is invalid
104  * @param h handle to check
105  * @return GNUNET_YES if invalid, GNUNET_NO if valid
106  */
107 int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
108
109
110 /**
111  * Check that fil corresponds to a filename
112  * (of a file that exists and that is not a directory).
113  *
114  * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
115  * else (will print an error message in that case, too).
116  */
117 int GNUNET_DISK_file_test (const char *fil);
118
119
120 /**
121  * Move the read/write pointer in a file
122  * @param h handle of an open file
123  * @param offset position to move to
124  * @param whence specification to which position the offset parameter relates to
125  * @return the new position on success, GNUNET_SYSERR otherwise
126  */
127 off_t
128 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, 
129                        off_t offset,
130                        enum GNUNET_DISK_Seek whence);
131
132
133 /**
134  * Get the size of the file (or directory)
135  * of the given file (in bytes).
136  *
137  * @param filename name of the file or directory
138  * @param size set to the size of the file (or,
139  *             in the case of directories, the sum
140  *             of all sizes of files in the directory)
141  * @param includeSymLinks should symbolic links be
142  *        included?
143  *
144  * @return GNUNET_OK on success, GNUNET_SYSERR on error
145  */
146 int GNUNET_DISK_file_size (const char *filename,
147                            uint64_t *size, 
148                            int includeSymLinks);
149
150
151 /**
152  * Create an (empty) temporary file on disk.
153  * 
154  * @param template component to use for the name;
155  *        does NOT contain "XXXXXX" or "/tmp/".
156  * @return NULL on error, otherwise name of fresh
157  *         file on disk in directory for temporary files
158  */
159 char *
160 GNUNET_DISK_mktemp (const char *t);
161
162
163 /**
164  * Open a file
165  * @param fn file name to be opened
166  * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
167  * @param perm permissions for the newly created file
168  * @return IO handle on success, NULL on error
169  */
170 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
171
172 /**
173  * Creates an interprocess channel
174  * @param blocking creates an asynchronous pipe if set to GNUNET_NO
175  * @return handle to the new pipe, NULL on error
176  */
177 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking);
178
179 /**
180  * Closes an interprocess channel
181  * @param p pipe
182  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
183  */
184 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
185
186 /**
187  * Close an open file.
188  *
189  * @param h file handle
190  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
191  */
192 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
193
194 /**
195  * Get the handle to a particular pipe end
196  * @param p pipe
197  * @param n number of the end
198  */
199 const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
200                                                         GNUNET_DISK_PipeHandle
201                                                         *p, int n);
202
203 /**
204  * Read the contents of a binary file into a buffer.
205  * @param h handle to an open file
206  * @param result the buffer to write the result to
207  * @param len the maximum number of bytes to read
208  * @return the number of bytes read on success, GNUNET_SYSERR on failure
209  */
210 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result, 
211                                size_t len);
212
213
214 /**
215  * Read the contents of a binary file into a buffer.
216  * @param fn file name
217  * @param result the buffer to write the result to
218  * @param len the maximum number of bytes to read
219  * @return number of bytes read, GNUNET_SYSERR on failure
220  */
221 ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result, 
222                              size_t len);
223
224
225 /**
226  * Write a buffer to a file.
227  *
228  * @param h handle to open file
229  * @param buffer the data to write
230  * @param n number of bytes to write
231  * @return GNUNET_OK on success, GNUNET_SYSERR on error
232  */
233 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, 
234                                 const void *buffer,
235                                 size_t n);
236
237
238 /**
239  * Write a buffer to a file.  If the file is longer than
240  * the given buffer size, it will be truncated.
241  *
242  * @param fn file name
243  * @param buffer the data to write
244  * @param n number of bytes to write
245  * @return number of bytes written on success, GNUNET_SYSERR on error
246  */
247 ssize_t GNUNET_DISK_fn_write (const char * fn, 
248                               const void *buffer,
249                               size_t n, 
250                               int mode);
251
252
253 /**
254  * Copy a file.
255  * @return GNUNET_OK on success, GNUNET_SYSERR on error
256  */
257 int GNUNET_DISK_file_copy (const char *src, const char *dst);
258
259
260 /**
261  * Scan a directory for files. The name of the directory
262  * must be expanded first (!).
263  *
264  * @param dirName the name of the directory
265  * @param callback the method to call for each file
266  * @param data argument to pass to callback
267  * @return the number of files found, -1 on error
268  */
269 int GNUNET_DISK_directory_scan (const char *dirName,
270                                 GNUNET_FileNameCallback callback, 
271                                 void *data);
272
273
274 /**
275  * Opaque handle used for iterating over a directory.
276  */
277 struct GNUNET_DISK_DirectoryIterator;
278
279
280 /**
281  * Function called to iterate over a directory.
282  *
283  * @param cls closure
284  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
285  *           get called on the next entry (or finish cleanly)
286  * @param filename complete filename (absolute path)
287  * @param dirname directory name (absolute path)
288  */
289 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
290                                                        struct
291                                                        GNUNET_DISK_DirectoryIterator
292                                                        * di,
293                                                        const char *filename,
294                                                        const char *dirname);
295
296
297 /**
298  * This function must be called during the DiskIteratorCallback
299  * (exactly once) to schedule the task to process the next
300  * filename in the directory (if there is one).
301  *
302  * @param iter opaque handle for the iterator
303  * @param can set to GNUNET_YES to terminate the iteration early
304  * @return GNUNET_YES if iteration will continue,
305  *         GNUNET_NO if this was the last entry (and iteration is complete),
306  *         GNUNET_SYSERR if "can" was YES
307  */
308 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
309                                          *iter, int can);
310
311
312 /**
313  * Scan a directory for files using the scheduler to run a task for
314  * each entry.  The name of the directory must be expanded first (!).
315  * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
316  * may provide a simpler API.
317  *
318  * @param sched scheduler to use
319  * @param prio priority to use
320  * @param dirName the name of the directory
321  * @param callback the method to call for each file
322  * @param callback_cls closure for callback
323  */
324 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
325                                            *sched,
326                                            enum GNUNET_SCHEDULER_Priority
327                                            prio, const char *dirName,
328                                            GNUNET_DISK_DirectoryIteratorCallback
329                                            callback, void *callback_cls);
330
331
332 /**
333  * Create the directory structure for storing
334  * a file.
335  *
336  * @param filename name of a file in the directory
337  * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
338  *          GNUNET_NO if directory exists but is not writeable
339  */
340 int GNUNET_DISK_directory_create_for_file (const char *filename);
341
342
343 /**
344  * Test if fil is a directory that can be accessed.
345  * Will not print an error message if the directory
346  * does not exist.  Will log errors if GNUNET_SYSERR is
347  * returned.
348  *
349  * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
350  *   on any error and if exists but not directory
351  */
352 int GNUNET_DISK_directory_test (const char *fil);
353
354
355 /**
356  * Remove all files in a directory (rm -rf). Call with
357  * caution.
358  *
359  * @param fileName the file to remove
360  * @return GNUNET_OK on success, GNUNET_SYSERR on error
361  */
362 int GNUNET_DISK_directory_remove (const char *fileName);
363
364
365 /**
366  * Implementation of "mkdir -p"
367  *
368  * @param dir the directory to create
369  * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
370  */
371 int GNUNET_DISK_directory_create (const char *dir);
372
373
374 /**
375  * Lock a part of a file
376  * @param fh file handle
377  * @lockStart absolute position from where to lock
378  * @lockEnd absolute position until where to lock
379  * @excl GNUNET_YES for an exclusive lock
380  * @return GNUNET_OK on success, GNUNET_SYSERR on error
381  */
382 int
383 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
384     off_t lockEnd, int excl);
385
386 /**
387  * Unlock a part of a file
388  * @param fh file handle
389  * @lockStart absolute position from where to unlock
390  * @lockEnd absolute position until where to unlock
391  * @return GNUNET_OK on success, GNUNET_SYSERR on error
392  */
393 int
394 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
395     off_t unlockEnd);
396
397
398 /**
399  * @brief Removes special characters as ':' from a filename.
400  * @param fn the filename to canonicalize
401  */
402 void GNUNET_DISK_filename_canonicalize (char *fn);
403
404
405 /**
406  * @brief Change owner of a file
407  * @param filename file to change
408  * @param user new owner of the file
409  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
410  */
411 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
412
413
414 /**
415  * Construct full path to a file inside of the private
416  * directory used by GNUnet.  Also creates the corresponding
417  * directory.  If the resulting name is supposed to be
418  * a directory, end the last argument in '/' (or pass
419  * DIR_SEPARATOR_STR as the last argument before NULL).
420  *
421  * @param serviceName name of the service asking
422  * @param varargs is NULL-terminated list of
423  *                path components to append to the
424  *                private directory name.
425  * @return the constructed filename
426  */
427 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
428                                      const char *serviceName, ...);
429
430
431 /**
432  * Opaque handle for a memory-mapping operation.
433  */
434 struct GNUNET_DISK_MapHandle;
435
436 /**
437  * Map a file into memory
438  * @param h open file handle
439  * @param m handle to the new mapping (will be set)
440  * @param access access specification, GNUNET_DISK_MAP_xxx
441  * @param len size of the mapping
442  * @return pointer to the mapped memory region, NULL on failure
443  */
444 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 
445                             struct GNUNET_DISK_MapHandle **m,
446                             int access, size_t len);
447
448 /**
449  * Unmap a file
450  *
451  * @param h mapping handle
452  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
453  */
454 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
455
456 /**
457  * Write file changes to disk
458  * @param h handle to an open file
459  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
460  */
461 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
462
463 #if 0                           /* keep Emacsens' auto-indent happy */
464 {
465 #endif
466 #ifdef __cplusplus
467 }
468 #endif
469
470
471 /* ifndef GNUNET_DISK_LIB_H */
472 #endif
473 /* end of gnunet_disk_lib.h */