ac20a61a301dc670c8553093e4b0717e2334803d
[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 /**
57  * Specifies how a file should be opened.
58  */
59 enum GNUNET_DISK_OpenFlags
60   {
61
62     /**
63      * Open the file for reading 
64      */
65     GNUNET_DISK_OPEN_READ = 1,
66     
67     /**
68      * Open the file for writing 
69      */
70     GNUNET_DISK_OPEN_WRITE = 2,
71     
72     /**
73      * Open the file for both reading and writing 
74      */
75     GNUNET_DISK_OPEN_READWRITE = 3,
76     
77     /**
78      * Fail if file already exists 
79      */
80     GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
81     
82     /**
83      * Truncate file if it exists 
84      */
85     GNUNET_DISK_OPEN_TRUNCATE = 8,
86     
87     /**
88      * Create file if it doesn't exist 
89      */
90     GNUNET_DISK_OPEN_CREATE = 16,
91
92     /**
93      * Append to the file 
94      */
95     GNUNET_DISK_OPEN_APPEND = 32
96   };
97
98 /**
99  * Specifies what type of memory map is desired.
100  */
101 enum GNUNET_DISK_MapType
102   {
103     /**
104      * Read-only memory map.
105      */
106     GNUNET_DISK_MAP_TYPE_READ = 1,
107
108     /**
109      * Write-able memory map.
110      */
111     GNUNET_DISK_MAP_TYPE_WRITE = 2,
112     /**
113      * Read-write memory map.
114      */
115     GNUNET_DISK_MAP_TYPE_READWRITE = 3
116   };
117
118
119 // FIXME: use enum here!
120 #define GNUNET_DISK_PERM_USER_READ      1
121 #define GNUNET_DISK_PERM_USER_WRITE     2
122 #define GNUNET_DISK_PERM_USER_EXEC      4
123 #define GNUNET_DISK_PERM_GROUP_READ     8
124 #define GNUNET_DISK_PERM_GROUP_WRITE    16
125 #define GNUNET_DISK_PERM_GROUP_EXEC     32
126 #define GNUNET_DISK_PERM_OTHER_READ     64
127 #define GNUNET_DISK_PERM_OTHER_WRITE    128
128 #define GNUNET_DISK_PERM_OTHER_EXEC     256
129
130 /**
131  * Constants for specifying how to seek.
132  */
133 enum GNUNET_DISK_Seek 
134   {
135     /**
136      * Seek an absolute position (from the start of the file).
137      */
138     GNUNET_DISK_SEEK_SET, 
139
140     /**
141      * Seek a relative position (from the current offset).
142      */
143     GNUNET_DISK_SEEK_CUR, 
144     
145     /**
146      * Seek an absolute position from the end of the file.
147      */
148     GNUNET_DISK_SEEK_END
149   };
150
151 /**
152  * Get the number of blocks that are left on the partition that
153  * contains the given file (for normal users).
154  *
155  * @param part a file on the partition to check
156  * @return -1 on errors, otherwise the number of free blocks
157  */
158 long GNUNET_DISK_get_blocks_available (const char *part);
159
160
161 /**
162  * Checks whether a handle is invalid
163  * @param h handle to check
164  * @return GNUNET_YES if invalid, GNUNET_NO if valid
165  */
166 int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
167
168
169 /**
170  * Check that fil corresponds to a filename
171  * (of a file that exists and that is not a directory).
172  *
173  * @param fil filename to check
174  * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
175  * else (will print an error message in that case, too).
176  */
177 int GNUNET_DISK_file_test (const char *fil);
178
179
180 /**
181  * Move the read/write pointer in a file
182  * @param h handle of an open file
183  * @param offset position to move to
184  * @param whence specification to which position the offset parameter relates to
185  * @return the new position on success, GNUNET_SYSERR otherwise
186  */
187 off_t
188 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, 
189                        off_t offset,
190                        enum GNUNET_DISK_Seek whence);
191
192
193 /**
194  * Get the size of the file (or directory)
195  * of the given file (in bytes).
196  *
197  * @param filename name of the file or directory
198  * @param size set to the size of the file (or,
199  *             in the case of directories, the sum
200  *             of all sizes of files in the directory)
201  * @param includeSymLinks should symbolic links be
202  *        included?
203  *
204  * @return GNUNET_OK on success, GNUNET_SYSERR on error
205  */
206 int GNUNET_DISK_file_size (const char *filename,
207                            uint64_t *size, 
208                            int includeSymLinks);
209
210
211 /**
212  * Obtain some unique identifiers for the given file
213  * that can be used to identify it in the local system.
214  * This function is used between GNUnet processes to
215  * quickly check if two files with the same absolute path
216  * are actually identical.  The two processes represent
217  * the same peer but may communicate over the network
218  * (and the file may be on an NFS volume).  This function
219  * may not be supported on all operating systems.
220  *
221  * @param filename name of the file
222  * @param dev set to the device ID
223  * @param ino set to the inode ID
224  * @return GNUNET_OK on success
225  */
226 int GNUNET_DISK_file_get_identifiers (const char *filename,
227                                       uint32_t *dev,
228                                       uint64_t *ino);
229  
230
231 /**
232  * Create an (empty) temporary file on disk.
233  * 
234  * @param tmpl component to use for the name;
235  *        does NOT contain "XXXXXX" or "/tmp/".
236  * @return NULL on error, otherwise name of fresh
237  *         file on disk in directory for temporary files
238  */
239 char *
240 GNUNET_DISK_mktemp (const char *tmpl);
241
242
243 /**
244  * Open a file.
245  *
246  * @param fn file name to be opened
247  * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
248  * @param ... permissions for the newly created file (only required if creation is possible)
249  * @return IO handle on success, NULL on error
250  */
251 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn,
252                                                       enum GNUNET_DISK_OpenFlags flags,
253                                                       ...);
254
255 /**
256  * Creates an interprocess channel
257  * @param blocking creates an asynchronous pipe if set to GNUNET_NO
258  * @return handle to the new pipe, NULL on error
259  */
260 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking);
261
262 /**
263  * Closes an interprocess channel
264  * @param p pipe
265  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
266  */
267 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
268
269 /**
270  * Close an open file.
271  *
272  * @param h file handle
273  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
274  */
275 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
276
277 /**
278  * Get the handle to a particular pipe end
279  * @param p pipe
280  * @param n number of the end (0 or 1); FIXME: use enum here!
281  * @return handle for the respective end
282  */
283 const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
284                                                         GNUNET_DISK_PipeHandle
285                                                         *p, int n);
286
287 /**
288  * Read the contents of a binary file into a buffer.
289  * @param h handle to an open file
290  * @param result the buffer to write the result to
291  * @param len the maximum number of bytes to read
292  * @return the number of bytes read on success, GNUNET_SYSERR on failure
293  */
294 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result, 
295                                size_t len);
296
297
298 /**
299  * Read the contents of a binary file into a buffer.
300  * @param fn file name
301  * @param result the buffer to write the result to
302  * @param len the maximum number of bytes to read
303  * @return number of bytes read, GNUNET_SYSERR on failure
304  */
305 ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result, 
306                              size_t len);
307
308
309 /**
310  * Write a buffer to a file.
311  *
312  * @param h handle to open file
313  * @param buffer the data to write
314  * @param n number of bytes to write
315  * @return GNUNET_OK on success, GNUNET_SYSERR on error
316  */
317 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, 
318                                 const void *buffer,
319                                 size_t n);
320
321
322 /**
323  * Write a buffer to a file.  If the file is longer than
324  * the given buffer size, it will be truncated.
325  *
326  * @param fn file name
327  * @param buffer the data to write
328  * @param n number of bytes to write
329  * @return number of bytes written on success, GNUNET_SYSERR on error
330  */
331 ssize_t GNUNET_DISK_fn_write (const char * fn, 
332                               const void *buffer,
333                               size_t n, 
334                               int mode);
335
336
337 /**
338  * Copy a file.
339  * @return GNUNET_OK on success, GNUNET_SYSERR on error
340  */
341 int GNUNET_DISK_file_copy (const char *src, const char *dst);
342
343
344 /**
345  * Scan a directory for files. The name of the directory
346  * must be expanded first (!).
347  *
348  * @param dirName the name of the directory
349  * @param callback the method to call for each file
350  * @param data argument to pass to callback
351  * @return the number of files found, -1 on error
352  */
353 int GNUNET_DISK_directory_scan (const char *dirName,
354                                 GNUNET_FileNameCallback callback, 
355                                 void *data);
356
357
358 /**
359  * Opaque handle used for iterating over a directory.
360  */
361 struct GNUNET_DISK_DirectoryIterator;
362
363
364 /**
365  * Function called to iterate over a directory.
366  *
367  * @param cls closure
368  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
369  *           get called on the next entry (or finish cleanly)
370  * @param filename complete filename (absolute path)
371  * @param dirname directory name (absolute path)
372  */
373 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
374                                                        struct
375                                                        GNUNET_DISK_DirectoryIterator
376                                                        * di,
377                                                        const char *filename,
378                                                        const char *dirname);
379
380
381 /**
382  * This function must be called during the DiskIteratorCallback
383  * (exactly once) to schedule the task to process the next
384  * filename in the directory (if there is one).
385  *
386  * @param iter opaque handle for the iterator
387  * @param can set to GNUNET_YES to terminate the iteration early
388  * @return GNUNET_YES if iteration will continue,
389  *         GNUNET_NO if this was the last entry (and iteration is complete),
390  *         GNUNET_SYSERR if "can" was YES
391  */
392 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
393                                          *iter, int can);
394
395
396 /**
397  * Scan a directory for files using the scheduler to run a task for
398  * each entry.  The name of the directory must be expanded first (!).
399  * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
400  * may provide a simpler API.
401  *
402  * @param sched scheduler to use
403  * @param prio priority to use
404  * @param dirName the name of the directory
405  * @param callback the method to call for each file
406  * @param callback_cls closure for callback
407  */
408 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
409                                            *sched,
410                                            enum GNUNET_SCHEDULER_Priority
411                                            prio, const char *dirName,
412                                            GNUNET_DISK_DirectoryIteratorCallback
413                                            callback, void *callback_cls);
414
415
416 /**
417  * Create the directory structure for storing
418  * a file.
419  *
420  * @param filename name of a file in the directory
421  * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
422  *          GNUNET_NO if directory exists but is not writeable
423  */
424 int GNUNET_DISK_directory_create_for_file (const char *filename);
425
426
427 /**
428  * Test if fil is a directory that can be accessed.
429  * Will not print an error message if the directory
430  * does not exist.  Will log errors if GNUNET_SYSERR is
431  * returned.
432  *
433  * @param fil filename to test
434  * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
435  *   on any error and if exists but not directory
436  */
437 int GNUNET_DISK_directory_test (const char *fil);
438
439
440 /**
441  * Remove all files in a directory (rm -rf). Call with
442  * caution.
443  *
444  * @param fileName the file to remove
445  * @return GNUNET_OK on success, GNUNET_SYSERR on error
446  */
447 int GNUNET_DISK_directory_remove (const char *fileName);
448
449
450 /**
451  * Implementation of "mkdir -p"
452  *
453  * @param dir the directory to create
454  * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
455  */
456 int GNUNET_DISK_directory_create (const char *dir);
457
458
459 /**
460  * Lock a part of a file.
461  *
462  * @param fh file handle
463  * @param lockStart absolute position from where to lock
464  * @param lockEnd absolute position until where to lock
465  * @param excl GNUNET_YES for an exclusive lock
466  * @return GNUNET_OK on success, GNUNET_SYSERR on error
467  */
468 int
469 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
470     off_t lockEnd, int excl);
471
472
473 /**
474  * Unlock a part of a file
475  * @param fh file handle
476  * @param lockStart absolute position from where to unlock
477  * @param lockEnd absolute position until where to unlock
478  * @return GNUNET_OK on success, GNUNET_SYSERR on error
479  */
480 int
481 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
482     off_t unlockEnd);
483
484
485 /**
486  * @brief Removes special characters as ':' from a filename.
487  * @param fn the filename to canonicalize
488  */
489 void GNUNET_DISK_filename_canonicalize (char *fn);
490
491
492 /**
493  * @brief Change owner of a file
494  * @param filename file to change
495  * @param user new owner of the file
496  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
497  */
498 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
499
500
501 /**
502  * Construct full path to a file inside of the private
503  * directory used by GNUnet.  Also creates the corresponding
504  * directory.  If the resulting name is supposed to be
505  * a directory, end the last argument in '/' (or pass
506  * DIR_SEPARATOR_STR as the last argument before NULL).
507  *
508  * @param cfg configuration to use
509  * @param serviceName name of the service asking
510  * @param varargs is NULL-terminated list of
511  *                path components to append to the
512  *                private directory name.
513  * @return the constructed filename
514  */
515 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
516                                      const char *serviceName, ...);
517
518
519 /**
520  * Opaque handle for a memory-mapping operation.
521  */
522 struct GNUNET_DISK_MapHandle;
523
524 /**
525  * Map a file into memory
526  * @param h open file handle
527  * @param m handle to the new mapping (will be set)
528  * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
529  * @param len size of the mapping
530  * @return pointer to the mapped memory region, NULL on failure
531  */
532 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, 
533                             struct GNUNET_DISK_MapHandle **m,
534                             enum GNUNET_DISK_MapType access, size_t len);
535
536 /**
537  * Unmap a file
538  *
539  * @param h mapping handle
540  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
541  */
542 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
543
544 /**
545  * Write file changes to disk
546  * @param h handle to an open file
547  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
548  */
549 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
550
551 #if 0                           /* keep Emacsens' auto-indent happy */
552 {
553 #endif
554 #ifdef __cplusplus
555 }
556 #endif
557
558
559 /* ifndef GNUNET_DISK_LIB_H */
560 #endif
561 /* end of gnunet_disk_lib.h */