4d09d4c994d133fc5c98ccdc208c40af98520680
[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  * FIXME.
153  *
154  * @param filename name of the file
155  * @param dev set to the device ID
156  * @param ino set to the inode ID
157  * @return GNUNET_OK on success
158  */
159 int GNUNET_DISK_file_get_identifiers (const char *filename,
160                                       uint32_t *dev,
161                                       uint64_t *ino);
162  
163
164 /**
165  * Create an (empty) temporary file on disk.
166  * 
167  * @param template component to use for the name;
168  *        does NOT contain "XXXXXX" or "/tmp/".
169  * @return NULL on error, otherwise name of fresh
170  *         file on disk in directory for temporary files
171  */
172 char *
173 GNUNET_DISK_mktemp (const char *t);
174
175
176 /**
177  * Open a file
178  * @param fn file name to be opened
179  * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
180  * @param perm permissions for the newly created file
181  * @return IO handle on success, NULL on error
182  */
183 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
184
185 /**
186  * Creates an interprocess channel
187  * @param blocking creates an asynchronous pipe if set to GNUNET_NO
188  * @return handle to the new pipe, NULL on error
189  */
190 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking);
191
192 /**
193  * Closes an interprocess channel
194  * @param p pipe
195  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
196  */
197 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
198
199 /**
200  * Close an open file.
201  *
202  * @param h file handle
203  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
204  */
205 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
206
207 /**
208  * Get the handle to a particular pipe end
209  * @param p pipe
210  * @param n number of the end
211  */
212 const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
213                                                         GNUNET_DISK_PipeHandle
214                                                         *p, int n);
215
216 /**
217  * Read the contents of a binary file into a buffer.
218  * @param h handle to an open file
219  * @param result the buffer to write the result to
220  * @param len the maximum number of bytes to read
221  * @return the number of bytes read on success, GNUNET_SYSERR on failure
222  */
223 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result, 
224                                size_t len);
225
226
227 /**
228  * Read the contents of a binary file into a buffer.
229  * @param fn file name
230  * @param result the buffer to write the result to
231  * @param len the maximum number of bytes to read
232  * @return number of bytes read, GNUNET_SYSERR on failure
233  */
234 ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result, 
235                              size_t len);
236
237
238 /**
239  * Write a buffer to a file.
240  *
241  * @param h handle to open file
242  * @param buffer the data to write
243  * @param n number of bytes to write
244  * @return GNUNET_OK on success, GNUNET_SYSERR on error
245  */
246 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, 
247                                 const void *buffer,
248                                 size_t n);
249
250
251 /**
252  * Write a buffer to a file.  If the file is longer than
253  * the given buffer size, it will be truncated.
254  *
255  * @param fn file name
256  * @param buffer the data to write
257  * @param n number of bytes to write
258  * @return number of bytes written on success, GNUNET_SYSERR on error
259  */
260 ssize_t GNUNET_DISK_fn_write (const char * fn, 
261                               const void *buffer,
262                               size_t n, 
263                               int mode);
264
265
266 /**
267  * Copy a file.
268  * @return GNUNET_OK on success, GNUNET_SYSERR on error
269  */
270 int GNUNET_DISK_file_copy (const char *src, const char *dst);
271
272
273 /**
274  * Scan a directory for files. The name of the directory
275  * must be expanded first (!).
276  *
277  * @param dirName the name of the directory
278  * @param callback the method to call for each file
279  * @param data argument to pass to callback
280  * @return the number of files found, -1 on error
281  */
282 int GNUNET_DISK_directory_scan (const char *dirName,
283                                 GNUNET_FileNameCallback callback, 
284                                 void *data);
285
286
287 /**
288  * Opaque handle used for iterating over a directory.
289  */
290 struct GNUNET_DISK_DirectoryIterator;
291
292
293 /**
294  * Function called to iterate over a directory.
295  *
296  * @param cls closure
297  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
298  *           get called on the next entry (or finish cleanly)
299  * @param filename complete filename (absolute path)
300  * @param dirname directory name (absolute path)
301  */
302 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
303                                                        struct
304                                                        GNUNET_DISK_DirectoryIterator
305                                                        * di,
306                                                        const char *filename,
307                                                        const char *dirname);
308
309
310 /**
311  * This function must be called during the DiskIteratorCallback
312  * (exactly once) to schedule the task to process the next
313  * filename in the directory (if there is one).
314  *
315  * @param iter opaque handle for the iterator
316  * @param can set to GNUNET_YES to terminate the iteration early
317  * @return GNUNET_YES if iteration will continue,
318  *         GNUNET_NO if this was the last entry (and iteration is complete),
319  *         GNUNET_SYSERR if "can" was YES
320  */
321 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
322                                          *iter, int can);
323
324
325 /**
326  * Scan a directory for files using the scheduler to run a task for
327  * each entry.  The name of the directory must be expanded first (!).
328  * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
329  * may provide a simpler API.
330  *
331  * @param sched scheduler to use
332  * @param prio priority to use
333  * @param dirName the name of the directory
334  * @param callback the method to call for each file
335  * @param callback_cls closure for callback
336  */
337 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
338                                            *sched,
339                                            enum GNUNET_SCHEDULER_Priority
340                                            prio, const char *dirName,
341                                            GNUNET_DISK_DirectoryIteratorCallback
342                                            callback, void *callback_cls);
343
344
345 /**
346  * Create the directory structure for storing
347  * a file.
348  *
349  * @param filename name of a file in the directory
350  * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
351  *          GNUNET_NO if directory exists but is not writeable
352  */
353 int GNUNET_DISK_directory_create_for_file (const char *filename);
354
355
356 /**
357  * Test if fil is a directory that can be accessed.
358  * Will not print an error message if the directory
359  * does not exist.  Will log errors if GNUNET_SYSERR is
360  * returned.
361  *
362  * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
363  *   on any error and if exists but not directory
364  */
365 int GNUNET_DISK_directory_test (const char *fil);
366
367
368 /**
369  * Remove all files in a directory (rm -rf). Call with
370  * caution.
371  *
372  * @param fileName the file to remove
373  * @return GNUNET_OK on success, GNUNET_SYSERR on error
374  */
375 int GNUNET_DISK_directory_remove (const char *fileName);
376
377
378 /**
379  * Implementation of "mkdir -p"
380  *
381  * @param dir the directory to create
382  * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
383  */
384 int GNUNET_DISK_directory_create (const char *dir);
385
386
387 /**
388  * Lock a part of a file
389  * @param fh file handle
390  * @lockStart absolute position from where to lock
391  * @lockEnd absolute position until where to lock
392  * @excl GNUNET_YES for an exclusive lock
393  * @return GNUNET_OK on success, GNUNET_SYSERR on error
394  */
395 int
396 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
397     off_t lockEnd, int excl);
398
399 /**
400  * Unlock a part of a file
401  * @param fh file handle
402  * @lockStart absolute position from where to unlock
403  * @lockEnd absolute position until where to unlock
404  * @return GNUNET_OK on success, GNUNET_SYSERR on error
405  */
406 int
407 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
408     off_t unlockEnd);
409
410
411 /**
412  * @brief Removes special characters as ':' from a filename.
413  * @param fn the filename to canonicalize
414  */
415 void GNUNET_DISK_filename_canonicalize (char *fn);
416
417
418 /**
419  * @brief Change owner of a file
420  * @param filename file to change
421  * @param user new owner of the file
422  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
423  */
424 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
425
426
427 /**
428  * Construct full path to a file inside of the private
429  * directory used by GNUnet.  Also creates the corresponding
430  * directory.  If the resulting name is supposed to be
431  * a directory, end the last argument in '/' (or pass
432  * DIR_SEPARATOR_STR as the last argument before NULL).
433  *
434  * @param serviceName name of the service asking
435  * @param varargs is NULL-terminated list of
436  *                path components to append to the
437  *                private directory name.
438  * @return the constructed filename
439  */
440 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
441                                      const char *serviceName, ...);
442
443
444 /**
445  * Opaque handle for a memory-mapping operation.
446  */
447 struct GNUNET_DISK_MapHandle;
448
449 /**
450  * Map a file into memory
451  * @param h open file handle
452  * @param m handle to the new mapping (will be set)
453  * @param access access specification, GNUNET_DISK_MAP_xxx
454  * @param len size of the mapping
455  * @return pointer to the mapped memory region, NULL on failure
456  */
457 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 
458                             struct GNUNET_DISK_MapHandle **m,
459                             int access, size_t len);
460
461 /**
462  * Unmap a file
463  *
464  * @param h mapping handle
465  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
466  */
467 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
468
469 /**
470  * Write file changes to disk
471  * @param h handle to an open file
472  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
473  */
474 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
475
476 #if 0                           /* keep Emacsens' auto-indent happy */
477 {
478 #endif
479 #ifdef __cplusplus
480 }
481 #endif
482
483
484 /* ifndef GNUNET_DISK_LIB_H */
485 #endif
486 /* end of gnunet_disk_lib.h */