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