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