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