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