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