glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / include / gnunet_disk_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @author Christian Grothoff
17  *
18  * @file
19  * Disk IO APIs
20  *
21  * @defgroup disk  Disk library
22  * Disk IO APIs
23  * @{
24  */
25 #ifndef GNUNET_DISK_LIB_H
26 #define GNUNET_DISK_LIB_H
27
28 /**
29  * Handle used to manage a pipe.
30  */
31 struct GNUNET_DISK_PipeHandle;
32
33 /**
34  * Type of a handle.
35  */
36 enum GNUNET_FILE_Type
37 {
38   /**
39    * Handle represents an event.
40    */
41   GNUNET_DISK_HANLDE_TYPE_EVENT,
42
43   /**
44    * Handle represents a file.
45    */
46   GNUNET_DISK_HANLDE_TYPE_FILE,
47
48   /**
49    * Handle represents a pipe.
50    */
51   GNUNET_DISK_HANLDE_TYPE_PIPE
52 };
53
54 /**
55  * Handle used to access files (and pipes).
56  */
57 struct GNUNET_DISK_FileHandle
58 {
59
60 #if WINDOWS
61   /**
62    * File handle under W32.
63    */
64   HANDLE h;
65
66   /**
67    * Type
68    */
69   enum GNUNET_FILE_Type type;
70
71   /**
72    * Structure for overlapped reading (for pipes)
73    */
74   OVERLAPPED *oOverlapRead;
75
76   /**
77    * Structure for overlapped writing (for pipes)
78    */
79   OVERLAPPED *oOverlapWrite;
80 #else
81
82   /**
83    * File handle on other OSes.
84    */
85   int fd;
86
87 #endif
88 };
89
90
91 /* we need size_t, and since it can be both unsigned int
92    or unsigned long long, this IS platform dependent;
93    but "stdlib.h" should be portable 'enough' to be
94    unconditionally available... */
95 #include <stdlib.h>
96 #include "gnunet_configuration_lib.h"
97 #include "gnunet_scheduler_lib.h"
98
99 #ifdef __cplusplus
100 extern "C"
101 {
102 #if 0                           /* keep Emacsens' auto-indent happy */
103 }
104 #endif
105 #endif
106
107
108 /**
109  * Specifies how a file should be opened.
110  */
111 enum GNUNET_DISK_OpenFlags
112 {
113
114   /**
115    * Open the file for reading
116    */
117   GNUNET_DISK_OPEN_READ = 1,
118
119   /**
120    * Open the file for writing
121    */
122   GNUNET_DISK_OPEN_WRITE = 2,
123
124   /**
125    * Open the file for both reading and writing
126    */
127   GNUNET_DISK_OPEN_READWRITE = 3,
128
129   /**
130    * Fail if file already exists
131    */
132   GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
133
134   /**
135    * Truncate file if it exists
136    */
137   GNUNET_DISK_OPEN_TRUNCATE = 8,
138
139   /**
140    * Create file if it doesn't exist
141    */
142   GNUNET_DISK_OPEN_CREATE = 16,
143
144   /**
145    * Append to the file
146    */
147   GNUNET_DISK_OPEN_APPEND = 32
148 };
149
150 /**
151  * Specifies what type of memory map is desired.
152  */
153 enum GNUNET_DISK_MapType
154 {
155   /**
156    * Read-only memory map.
157    */
158   GNUNET_DISK_MAP_TYPE_READ = 1,
159
160   /**
161    * Write-able memory map.
162    */
163   GNUNET_DISK_MAP_TYPE_WRITE = 2,
164
165   /**
166    * Read-write memory map.
167    */
168   GNUNET_DISK_MAP_TYPE_READWRITE = 3
169 };
170
171
172 /**
173  * File access permissions, UNIX-style.
174  */
175 enum GNUNET_DISK_AccessPermissions
176 {
177   /**
178    * Nobody is allowed to do anything to the file.
179    */
180   GNUNET_DISK_PERM_NONE = 0,
181
182   /**
183    * Owner can read.
184    */
185   GNUNET_DISK_PERM_USER_READ = 1,
186
187   /**
188    * Owner can write.
189    */
190   GNUNET_DISK_PERM_USER_WRITE = 2,
191
192   /**
193    * Owner can execute.
194    */
195   GNUNET_DISK_PERM_USER_EXEC = 4,
196
197   /**
198    * Group can read.
199    */
200   GNUNET_DISK_PERM_GROUP_READ = 8,
201
202   /**
203    * Group can write.
204    */
205   GNUNET_DISK_PERM_GROUP_WRITE = 16,
206
207   /**
208    * Group can execute.
209    */
210   GNUNET_DISK_PERM_GROUP_EXEC = 32,
211
212   /**
213    * Everybody can read.
214    */
215   GNUNET_DISK_PERM_OTHER_READ = 64,
216
217   /**
218    * Everybody can write.
219    */
220   GNUNET_DISK_PERM_OTHER_WRITE = 128,
221
222   /**
223    * Everybody can execute.
224    */
225   GNUNET_DISK_PERM_OTHER_EXEC = 256
226 };
227
228
229 /**
230  * Constants for specifying how to seek.  Do not change values or order,
231  * some of the code depends on the specific numeric values!
232  */
233 enum GNUNET_DISK_Seek
234 {
235   /**
236    * Seek an absolute position (from the start of the file).
237    */
238   GNUNET_DISK_SEEK_SET = 0,
239
240   /**
241    * Seek a relative position (from the current offset).
242    */
243   GNUNET_DISK_SEEK_CUR = 1,
244
245   /**
246    * Seek an absolute position from the end of the file.
247    */
248   GNUNET_DISK_SEEK_END = 2
249 };
250
251
252 /**
253  * Enumeration identifying the two ends of a pipe.
254  */
255 enum GNUNET_DISK_PipeEnd
256 {
257   /**
258    * The reading-end of a pipe.
259    */
260   GNUNET_DISK_PIPE_END_READ = 0,
261
262   /**
263    * The writing-end of a pipe.
264    */
265   GNUNET_DISK_PIPE_END_WRITE = 1
266 };
267
268
269 /**
270  * Checks whether a handle is invalid
271  *
272  * @param h handle to check
273  * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
274  */
275 int
276 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
277
278
279 /**
280  * Check that fil corresponds to a filename
281  * (of a file that exists and that is not a directory).
282  *
283  * @param fil filename to check
284  * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
285  * else (will print an error message in that case, too).
286  */
287 int
288 GNUNET_DISK_file_test (const char *fil);
289
290
291 /**
292  * Move a file out of the way (create a backup) by
293  * renaming it to "orig.NUM~" where NUM is the smallest
294  * number that is not used yet.
295  *
296  * @param fil name of the file to back up
297  */
298 void
299 GNUNET_DISK_file_backup (const char *fil);
300
301
302 /**
303  * Move the read/write pointer in a file
304  * @param h handle of an open file
305  * @param offset position to move to
306  * @param whence specification to which position the offset parameter relates to
307  * @return the new position on success, GNUNET_SYSERR otherwise
308  */
309 off_t
310 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
311                        enum GNUNET_DISK_Seek whence);
312
313
314 /**
315  * Get the size of the file (or directory) of the given file (in
316  * bytes).
317  *
318  * @param filename name of the file or directory
319  * @param size set to the size of the file (or,
320  *             in the case of directories, the sum
321  *             of all sizes of files in the directory)
322  * @param include_symbolic_links should symbolic links be
323  *        included?
324  * @param single_file_mode #GNUNET_YES to only get size of one file
325  *        and return #GNUNET_SYSERR for directories.
326  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
327  */
328 int
329 GNUNET_DISK_file_size (const char *filename,
330                        uint64_t *size,
331                        int include_symbolic_links,
332                        int single_file_mode);
333
334
335 /**
336  * Obtain some unique identifiers for the given file
337  * that can be used to identify it in the local system.
338  * This function is used between GNUnet processes to
339  * quickly check if two files with the same absolute path
340  * are actually identical.  The two processes represent
341  * the same peer but may communicate over the network
342  * (and the file may be on an NFS volume).  This function
343  * may not be supported on all operating systems.
344  *
345  * @param filename name of the file
346  * @param dev set to the device ID
347  * @param ino set to the inode ID
348  * @return #GNUNET_OK on success
349  */
350 int
351 GNUNET_DISK_file_get_identifiers (const char *filename,
352                                   uint64_t *dev,
353                                   uint64_t *ino);
354
355
356 /**
357  * Create an (empty) temporary file on disk.  If the given name is not
358  * an absolute path, the current 'TMPDIR' will be prepended.  In any case,
359  * 6 random characters will be appended to the name to create a unique
360  * filename.
361  *
362  * @param t component to use for the name;
363  *        does NOT contain "XXXXXX" or "/tmp/".
364  * @return NULL on error, otherwise name of fresh
365  *         file on disk in directory for temporary files
366  */
367 char *
368 GNUNET_DISK_mktemp (const char *t);
369
370
371 /**
372  * Create an (empty) temporary directory on disk.  If the given name is not an
373  * absolute path, the current 'TMPDIR' will be prepended.  In any case, 6
374  * random characters will be appended to the name to create a unique name.
375  *
376  * @param t component to use for the name;
377  *        does NOT contain "XXXXXX" or "/tmp/".
378  * @return NULL on error, otherwise name of freshly created directory
379  */
380 char *
381 GNUNET_DISK_mkdtemp (const char *t);
382
383
384 /**
385  * Open a file.  Note that the access permissions will only be
386  * used if a new file is created and if the underlying operating
387  * system supports the given permissions.
388  *
389  * @param fn file name to be opened
390  * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
391  * @param perm permissions for the newly created file, use
392  *             #GNUNET_DISK_PERM_NONE if a file could not be created by this
393  *             call (because of flags)
394  * @return IO handle on success, NULL on error
395  */
396 struct GNUNET_DISK_FileHandle *
397 GNUNET_DISK_file_open (const char *fn,
398                        enum GNUNET_DISK_OpenFlags flags,
399                        enum GNUNET_DISK_AccessPermissions perm);
400
401
402 /**
403  * Get the size of an open file.
404  *
405  * @param fh open file handle
406  * @param size where to write size of the file
407  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
408  */
409 int
410 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
411                               off_t *size);
412
413
414 /**
415  * Creates an interprocess channel
416  *
417  * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
418  * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
419  * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
420  * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
421  * @return handle to the new pipe, NULL on error
422  */
423 struct GNUNET_DISK_PipeHandle *
424 GNUNET_DISK_pipe (int blocking_read,
425                   int blocking_write,
426                   int inherit_read,
427                   int inherit_write);
428
429
430 /**
431  * Creates a pipe object from a couple of file descriptors.
432  * Useful for wrapping existing pipe FDs.
433  *
434  * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
435  * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
436  * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
437  *
438  * @return handle to the new pipe, NULL on error
439  */
440 struct GNUNET_DISK_PipeHandle *
441 GNUNET_DISK_pipe_from_fd (int blocking_read,
442                           int blocking_write,
443                           int fd[2]);
444
445
446 /**
447  * Closes an interprocess channel
448  * @param p pipe
449  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
450  */
451 int
452 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
453
454
455 /**
456  * Closes one half of an interprocess channel
457  *
458  * @param p pipe to close end of
459  * @param end which end of the pipe to close
460  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
461  */
462 int
463 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
464                             enum GNUNET_DISK_PipeEnd end);
465
466
467 /**
468  * Detaches one of the ends from the pipe.
469  * Detached end is a fully-functional FileHandle, it will
470  * not be affected by anything you do with the pipe afterwards.
471  * Each end of a pipe can only be detched from it once (i.e.
472  * it is not duplicated).
473  *
474  * @param p pipe to detach an end from
475  * @param end which end of the pipe to detach
476  * @return Detached end on success, NULL on failure
477  * (or if that end is not present or is closed).
478  */
479 struct GNUNET_DISK_FileHandle *
480 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
481                              enum GNUNET_DISK_PipeEnd end);
482
483 /**
484  * Close an open file.
485  *
486  * @param h file handle
487  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
488  */
489 int
490 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
491
492
493 /**
494  * Get the handle to a particular pipe end
495  *
496  * @param p pipe
497  * @param n end to access
498  * @return handle for the respective end
499  */
500 const struct GNUNET_DISK_FileHandle *
501 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
502                          enum GNUNET_DISK_PipeEnd n);
503
504
505 #if WINDOWS
506 /**
507  * Get a GNUnet file handle from a W32 handle (W32-only).
508  * Do not call on non-W32 platforms (returns NULL).
509  *
510  * @param handle native handle
511  * @return GNUnet file handle corresponding to the W32 handle
512  */
513 struct GNUNET_DISK_FileHandle *
514 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
515 #endif
516
517 /**
518  * Update POSIX permissions mask of a file on disk.  If both argumets
519  * are #GNUNET_NO, the file is made world-read-write-executable (777).
520  * Does nothing on W32.
521  *
522  * @param fn name of the file to update
523  * @param require_uid_match #GNUNET_YES means 700
524  * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
525  */
526 void
527 GNUNET_DISK_fix_permissions (const char *fn,
528                              int require_uid_match,
529                              int require_gid_match);
530
531
532 /**
533  * Get a handle from a native integer FD.
534  *
535  * @param fno native integer file descriptor
536  * @return file handle corresponding to the descriptor
537  */
538 struct GNUNET_DISK_FileHandle *
539 GNUNET_DISK_get_handle_from_int_fd (int fno);
540
541
542 /**
543  * Get a handle from a native FD.
544  *
545  * @param fd native file descriptor
546  * @return file handle corresponding to the descriptor
547  */
548 struct GNUNET_DISK_FileHandle *
549 GNUNET_DISK_get_handle_from_native (FILE *fd);
550
551
552 /**
553  * Read the contents of a binary file into a buffer.
554  *
555  * @param h handle to an open file
556  * @param result the buffer to write the result to
557  * @param len the maximum number of bytes to read
558  * @return the number of bytes read on success, #GNUNET_SYSERR on failure
559  */
560 ssize_t
561 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
562                        void *result,
563                        size_t len);
564
565
566 /**
567  * Read the contents of a binary file into a buffer.
568  * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
569  * when no data can be read).
570  *
571  * @param h handle to an open file
572  * @param result the buffer to write the result to
573  * @param len the maximum number of bytes to read
574  * @return the number of bytes read on success, #GNUNET_SYSERR on failure
575  */
576 ssize_t
577 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
578                                     void *result,
579                                     size_t len);
580
581
582 /**
583  * Read the contents of a binary file into a buffer.
584  *
585  * @param fn file name
586  * @param result the buffer to write the result to
587  * @param len the maximum number of bytes to read
588  * @return number of bytes read, #GNUNET_SYSERR on failure
589  */
590 ssize_t
591 GNUNET_DISK_fn_read (const char *fn,
592                      void *result,
593                      size_t len);
594
595
596 /**
597  * Write a buffer to a file.
598  *
599  * @param h handle to open file
600  * @param buffer the data to write
601  * @param n number of bytes to write
602  * @return number of bytes written on success, #GNUNET_SYSERR on error
603  */
604 ssize_t
605 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
606                         const void *buffer,
607                         size_t n);
608
609
610 /**
611  * Write a buffer to a file, blocking, if necessary.
612  *
613  * @param h handle to open file
614  * @param buffer the data to write
615  * @param n number of bytes to write
616  * @return number of bytes written on success, #GNUNET_SYSERR on error
617  */
618 ssize_t
619 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
620                                  const void *buffer,
621                                  size_t n);
622
623
624 /**
625  * Write a buffer to a file.  If the file is longer than
626  * the given buffer size, it will be truncated.
627  *
628  * @param fn file name
629  * @param buffer the data to write
630  * @param n number of bytes to write
631  * @param mode file permissions
632  * @return number of bytes written on success, #GNUNET_SYSERR on error
633  */
634 ssize_t
635 GNUNET_DISK_fn_write (const char *fn,
636                       const void *buffer,
637                       size_t n,
638                       enum GNUNET_DISK_AccessPermissions mode);
639
640
641 /**
642  * Copy a file.
643  *
644  * @param src file to copy
645  * @param dst destination file name
646  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
647  */
648 int
649 GNUNET_DISK_file_copy (const char *src,
650                        const char *dst);
651
652
653 /**
654  * Scan a directory for files.
655  *
656  * @param dir_name the name of the directory
657  * @param callback the method to call for each file
658  * @param callback_cls closure for @a callback
659  * @return the number of files found, -1 on error
660  */
661 int
662 GNUNET_DISK_directory_scan (const char *dir_name,
663                             GNUNET_FileNameCallback callback,
664                             void *callback_cls);
665
666
667 /**
668  * Create the directory structure for storing
669  * a file.
670  *
671  * @param filename name of a file in the directory
672  * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
673  *          #GNUNET_NO if directory exists but is not writeable
674  */
675 int
676 GNUNET_DISK_directory_create_for_file (const char *filename);
677
678
679 /**
680  * Test if @a fil is a directory and listable. Optionally, also check if the
681  * directory is readable.  Will not print an error message if the directory does
682  * not exist.  Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
683  * with the same name).
684  *
685  * @param fil filename to test
686  * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
687  *          #GNUNET_NO to disable this check
688  * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
689  *           does not exist or `stat`ed
690  */
691 int
692 GNUNET_DISK_directory_test (const char *fil, int is_readable);
693
694
695 /**
696  * Remove all files in a directory (rm -rf). Call with
697  * caution.
698  *
699  * @param filename the file to remove
700  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
701  */
702 int
703 GNUNET_DISK_directory_remove (const char *filename);
704
705
706 /**
707  * Remove the directory given under @a option in
708  * section [PATHS] in configuration under @a cfg_filename
709  *
710  * @param cfg_filename configuration file to parse
711  * @param option option with the dir name to purge
712  */
713 void
714 GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
715                            const char *option);
716
717
718 /**
719  * Implementation of "mkdir -p"
720  *
721  * @param dir the directory to create
722  * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
723  */
724 int
725 GNUNET_DISK_directory_create (const char *dir);
726
727
728 /**
729  * Lock a part of a file.
730  *
731  * @param fh file handle
732  * @param lock_start absolute position from where to lock
733  * @param lock_end absolute position until where to lock
734  * @param excl #GNUNET_YES for an exclusive lock
735  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
736  */
737 int
738 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
739                        off_t lock_start,
740                        off_t lock_end, int excl);
741
742
743 /**
744  * Unlock a part of a file.
745  *
746  * @param fh file handle
747  * @param unlock_start absolute position from where to unlock
748  * @param unlock_end absolute position until where to unlock
749  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
750  */
751 int
752 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
753                          off_t unlock_start,
754                          off_t unlock_end);
755
756
757 /**
758  * @brief Removes special characters as ':' from a filename.
759  * @param fn the filename to canonicalize
760  */
761 void
762 GNUNET_DISK_filename_canonicalize (char *fn);
763
764
765 /**
766  * @brief Change owner of a file
767  * @param filename file to change
768  * @param user new owner of the file
769  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
770  */
771 int
772 GNUNET_DISK_file_change_owner (const char *filename,
773                                const char *user);
774
775
776 /**
777  * Opaque handle for a memory-mapping operation.
778  */
779 struct GNUNET_DISK_MapHandle;
780
781 /**
782  * Map a file into memory
783  * @param h open file handle
784  * @param m handle to the new mapping (will be set)
785  * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
786  * @param len size of the mapping
787  * @return pointer to the mapped memory region, NULL on failure
788  */
789 void *
790 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
791                       struct GNUNET_DISK_MapHandle **m,
792                       enum GNUNET_DISK_MapType access,
793                       size_t len);
794
795
796 /**
797  * Unmap a file
798  *
799  * @param h mapping handle
800  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
801  */
802 int
803 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
804
805
806 /**
807  * Write file changes to disk
808  *
809  * @param h handle to an open file
810  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
811  */
812 int
813 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
814
815
816 #if 0                           /* keep Emacsens' auto-indent happy */
817 {
818 #endif
819 #ifdef __cplusplus
820 }
821 #endif
822
823 /* ifndef GNUNET_DISK_LIB_H */
824 #endif
825
826 /** @} */  /* end of group */
827
828 /* end of gnunet_disk_lib.h */