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