-fix double free, linker issue
[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 #if WINDOWS
29 #define OFF_T uint64_t
30 #else
31 #define OFF_T off_t
32 #endif
33
34 /**
35  * Handle used to manage a pipe.
36  */
37 struct GNUNET_DISK_PipeHandle;
38
39 /**
40  * Type of a handle.
41  */
42 enum GNUNET_FILE_Type
43 {
44   /**
45    * Handle represents a file.
46    */ 
47   GNUNET_DISK_HANLDE_TYPE_FILE, 
48
49   /**
50    * Handle represents a pipe.
51    */
52   GNUNET_DISK_HANLDE_TYPE_PIPE
53 };
54
55 /**
56  * Handle used to access files (and pipes).
57  */
58 struct GNUNET_DISK_FileHandle
59 {
60
61 #if WINDOWS
62   /**
63    * File handle under W32.
64    */
65   HANDLE h;
66
67   /**
68    * Type
69    */
70   enum GNUNET_FILE_Type type;
71
72   /**
73    * Structure for overlapped reading (for pipes)
74    */
75   OVERLAPPED *oOverlapRead;
76
77   /**
78    * Structure for overlapped writing (for pipes)
79    */
80   OVERLAPPED *oOverlapWrite;
81 #else
82
83   /**
84    * File handle on other OSes.
85    */
86   int fd;
87
88 #endif                          /*
89                                  */
90 };
91
92
93 /* we need size_t, and since it can be both unsigned int
94    or unsigned long long, this IS platform dependent;
95    but "stdlib.h" should be portable 'enough' to be
96    unconditionally available... */
97 #include <stdlib.h>
98 #include "gnunet_configuration_lib.h"
99 #include "gnunet_scheduler_lib.h"
100
101 #ifdef __cplusplus
102 extern "C"
103 {
104 #if 0                           /* keep Emacsens' auto-indent happy */
105 }
106 #endif
107 #endif
108
109
110 /**
111  * Specifies how a file should be opened.
112  */
113 enum GNUNET_DISK_OpenFlags
114 {
115
116     /**
117      * Open the file for reading
118      */
119   GNUNET_DISK_OPEN_READ = 1,
120
121     /**
122      * Open the file for writing
123      */
124   GNUNET_DISK_OPEN_WRITE = 2,
125
126     /**
127      * Open the file for both reading and writing
128      */
129   GNUNET_DISK_OPEN_READWRITE = 3,
130
131     /**
132      * Fail if file already exists
133      */
134   GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
135
136     /**
137      * Truncate file if it exists
138      */
139   GNUNET_DISK_OPEN_TRUNCATE = 8,
140
141     /**
142      * Create file if it doesn't exist
143      */
144   GNUNET_DISK_OPEN_CREATE = 16,
145
146     /**
147      * Append to the file
148      */
149   GNUNET_DISK_OPEN_APPEND = 32
150 };
151
152 /**
153  * Specifies what type of memory map is desired.
154  */
155 enum GNUNET_DISK_MapType
156 {
157     /**
158      * Read-only memory map.
159      */
160   GNUNET_DISK_MAP_TYPE_READ = 1,
161
162     /**
163      * Write-able memory map.
164      */
165   GNUNET_DISK_MAP_TYPE_WRITE = 2,
166     /**
167      * Read-write memory map.
168      */
169   GNUNET_DISK_MAP_TYPE_READWRITE = 3
170 };
171
172
173 /**
174  * File access permissions, UNIX-style.
175  */
176 enum GNUNET_DISK_AccessPermissions
177 {
178     /**
179      * Nobody is allowed to do anything to the file.
180      */
181   GNUNET_DISK_PERM_NONE = 0,
182
183     /**
184      * Owner can read.
185      */
186   GNUNET_DISK_PERM_USER_READ = 1,
187
188     /**
189      * Owner can write.
190      */
191   GNUNET_DISK_PERM_USER_WRITE = 2,
192
193     /**
194      * Owner can execute.
195      */
196   GNUNET_DISK_PERM_USER_EXEC = 4,
197
198     /**
199      * Group can read.
200      */
201   GNUNET_DISK_PERM_GROUP_READ = 8,
202
203     /**
204      * Group can write.
205      */
206   GNUNET_DISK_PERM_GROUP_WRITE = 16,
207
208     /**
209      * Group can execute.
210      */
211   GNUNET_DISK_PERM_GROUP_EXEC = 32,
212
213     /**
214      * Everybody can read.
215      */
216   GNUNET_DISK_PERM_OTHER_READ = 64,
217
218     /**
219      * Everybody can write.
220      */
221   GNUNET_DISK_PERM_OTHER_WRITE = 128,
222
223     /**
224      * Everybody can execute.
225      */
226   GNUNET_DISK_PERM_OTHER_EXEC = 256
227 };
228
229
230 /**
231  * Constants for specifying how to seek.
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,
239
240     /**
241      * Seek a relative position (from the current offset).
242      */
243   GNUNET_DISK_SEEK_CUR,
244
245     /**
246      * Seek an absolute position from the end of the file.
247      */
248   GNUNET_DISK_SEEK_END
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  * Get the number of blocks that are left on the partition that
271  * contains the given file (for normal users).
272  *
273  * @param part a file on the partition to check
274  * @return -1 on errors, otherwise the number of free blocks
275  */
276 long
277 GNUNET_DISK_get_blocks_available (const char *part);
278
279
280 /**
281  * Checks whether a handle is invalid
282  *
283  * @param h handle to check
284  * @return GNUNET_YES if invalid, GNUNET_NO if valid
285  */
286 int
287 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
288
289
290 /**
291  * Check that fil corresponds to a filename
292  * (of a file that exists and that is not a directory).
293  *
294  * @param fil filename to check
295  * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
296  * else (will print an error message in that case, too).
297  */
298 int
299 GNUNET_DISK_file_test (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 includeSymLinks should symbolic links be
323  *        included?
324  * @param singleFileMode 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, uint64_t * size,
330                        int includeSymLinks, int singleFileMode);
331
332
333 /**
334  * Obtain some unique identifiers for the given file
335  * that can be used to identify it in the local system.
336  * This function is used between GNUnet processes to
337  * quickly check if two files with the same absolute path
338  * are actually identical.  The two processes represent
339  * the same peer but may communicate over the network
340  * (and the file may be on an NFS volume).  This function
341  * may not be supported on all operating systems.
342  *
343  * @param filename name of the file
344  * @param dev set to the device ID
345  * @param ino set to the inode ID
346  * @return GNUNET_OK on success
347  */
348 int
349 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
350                                   uint64_t * ino);
351
352
353 /**
354  * Create an (empty) temporary file on disk.  If the given name is not
355  * an absolute path, the current 'TMPDIR' will be prepended.  In any case,
356  * 6 random characters will be appended to the name to create a unique
357  * filename.
358  *
359  * @param t component to use for the name;
360  *        does NOT contain "XXXXXX" or "/tmp/".
361  * @return NULL on error, otherwise name of fresh
362  *         file on disk in directory for temporary files
363  */
364 char *
365 GNUNET_DISK_mktemp (const char *t);
366
367
368 /**
369  * Create an (empty) temporary directory on disk.  If the given name is not an
370  * absolute path, the current 'TMPDIR' will be prepended.  In any case, 6
371  * random characters will be appended to the name to create a unique name.
372  *
373  * @param t component to use for the name;
374  *        does NOT contain "XXXXXX" or "/tmp/".
375  * @return NULL on error, otherwise name of freshly created directory
376  */
377 char *
378 GNUNET_DISK_mkdtemp (const char *t);
379
380
381 /**
382  * Open a file.  Note that the access permissions will only be
383  * used if a new file is created and if the underlying operating
384  * system supports the given permissions.
385  *
386  * @param fn file name to be opened
387  * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
388  * @param perm permissions for the newly created file, use
389  *             GNUNET_DISK_PERM_NONE if a file could not be created by this
390  *             call (because of flags)
391  * @return IO handle on success, NULL on error
392  */
393 struct GNUNET_DISK_FileHandle *
394 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
395                        enum GNUNET_DISK_AccessPermissions perm);
396
397
398 /**
399  * Get the size of an open file.
400  *
401  * @param fh open file handle
402  * @param size where to write size of the file
403  * @return GNUNET_OK on success, GNUNET_SYSERR on error
404  */
405 int
406 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
407                               OFF_T *size);
408
409
410 /**
411  * Creates an interprocess channel
412  *
413  * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
414  * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
415  * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
416  * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
417  * @return handle to the new pipe, NULL on error
418  */
419 struct GNUNET_DISK_PipeHandle *
420 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write);
421
422
423 /**
424  * Creates a pipe object from a couple of file descriptors.
425  * Useful for wrapping existing pipe FDs.
426  *
427  * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
428  * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
429  * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
430  *
431  * @return handle to the new pipe, NULL on error
432  */
433 struct GNUNET_DISK_PipeHandle *
434 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]);
435
436
437 /**
438  * Closes an interprocess channel
439  * @param p pipe
440  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
441  */
442 int
443 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
444
445
446 /**
447  * Closes one half of an interprocess channel
448  *
449  * @param p pipe to close end of
450  * @param end which end of the pipe to close
451  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
452  */
453 int
454 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
455                             enum GNUNET_DISK_PipeEnd end);
456
457 /**
458  * Close an open file.
459  *
460  * @param h file handle
461  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
462  */
463 int
464 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
465
466
467 /**
468  * Get the handle to a particular pipe end
469  *
470  * @param p pipe
471  * @param n end to access
472  * @return handle for the respective end
473  */
474 const struct GNUNET_DISK_FileHandle *
475 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
476                          enum GNUNET_DISK_PipeEnd n);
477
478
479 /**
480  * Get a handle from a native FD.
481  *
482  * @param fd native file descriptor
483  * @return file handle corresponding to the descriptor
484  */
485 struct GNUNET_DISK_FileHandle *
486 GNUNET_DISK_get_handle_from_native (FILE *fd);
487
488
489 /**
490  * Read the contents of a binary file into a buffer.
491  * @param h handle to an open file
492  * @param result the buffer to write the result to
493  * @param len the maximum number of bytes to read
494  * @return the number of bytes read on success, GNUNET_SYSERR on failure
495  */
496 ssize_t
497 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
498                        size_t len);
499
500
501 /**
502  * Read the contents of a binary file into a buffer.
503  * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
504  * when no data can be read).
505  *
506  * @param h handle to an open file
507  * @param result the buffer to write the result to
508  * @param len the maximum number of bytes to read
509  * @return the number of bytes read on success, GNUNET_SYSERR on failure
510  */
511 ssize_t
512 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
513                                     void *result, size_t len);
514
515
516 /**
517  * Read the contents of a binary file into a buffer.
518  *
519  * @param fn file name
520  * @param result the buffer to write the result to
521  * @param len the maximum number of bytes to read
522  * @return number of bytes read, GNUNET_SYSERR on failure
523  */
524 ssize_t
525 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
526
527
528 /**
529  * Write a buffer to a file.
530  *
531  * @param h handle to open file
532  * @param buffer the data to write
533  * @param n number of bytes to write
534  * @return number of bytes written on success, GNUNET_SYSERR on error
535  */
536 ssize_t
537 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
538                         const void *buffer, size_t n);
539
540
541 /**
542  * Write a buffer to a file, blocking, if necessary.
543  * @param h handle to open file
544  * @param buffer the data to write
545  * @param n number of bytes to write
546  * @return number of bytes written on success, GNUNET_SYSERR on error
547  */
548 ssize_t
549 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
550     const void *buffer, size_t n);
551
552 /**
553  * Write a buffer to a file.  If the file is longer than
554  * the given buffer size, it will be truncated.
555  *
556  * @param fn file name
557  * @param buffer the data to write
558  * @param n number of bytes to write
559  * @param mode file permissions
560  * @return number of bytes written on success, GNUNET_SYSERR on error
561  */
562 ssize_t
563 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
564                       enum GNUNET_DISK_AccessPermissions mode);
565
566
567 /**
568  * Copy a file.
569  *
570  * @param src file to copy
571  * @param dst destination file name
572  * @return GNUNET_OK on success, GNUNET_SYSERR on error
573  */
574 int
575 GNUNET_DISK_file_copy (const char *src, const char *dst);
576
577
578 /**
579  * Scan a directory for files.
580  *
581  * @param dirName the name of the directory
582  * @param callback the method to call for each file
583  * @param callback_cls closure for callback
584  * @return the number of files found, -1 on error
585  */
586 int
587 GNUNET_DISK_directory_scan (const char *dirName,
588                             GNUNET_FileNameCallback callback,
589                             void *callback_cls);
590
591
592 /**
593  * Opaque handle used for iterating over a directory.
594  */
595 struct GNUNET_DISK_DirectoryIterator;
596
597
598 /**
599  * Function called to iterate over a directory.
600  *
601  * @param cls closure
602  * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
603  *           get called on the next entry (or finish cleanly);
604  *           NULL on error (will be the last call in that case)
605  * @param filename complete filename (absolute path)
606  * @param dirname directory name (absolute path)
607  */
608 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
609                                                        struct
610                                                        GNUNET_DISK_DirectoryIterator
611                                                        * di,
612                                                        const char *filename,
613                                                        const char *dirname);
614
615
616 /**
617  * This function must be called during the DiskIteratorCallback
618  * (exactly once) to schedule the task to process the next
619  * filename in the directory (if there is one).
620  *
621  * @param iter opaque handle for the iterator
622  * @param can set to GNUNET_YES to terminate the iteration early
623  * @return GNUNET_YES if iteration will continue,
624  *         GNUNET_NO if this was the last entry (and iteration is complete),
625  *         GNUNET_SYSERR if "can" was YES
626  */
627 int
628 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
629                                      int can);
630
631
632 /**
633  * Scan a directory for files using the scheduler to run a task for
634  * each entry.  The name of the directory must be expanded first (!).
635  * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
636  * may provide a simpler API.
637  *
638  * @param prio priority to use
639  * @param dirName the name of the directory
640  * @param callback the method to call for each file
641  * @param callback_cls closure for callback
642  * @return GNUNET_YES if directory is not empty and 'callback'
643  *         will be called later, GNUNET_NO otherwise, GNUNET_SYSERR on error.
644  */
645 int
646 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
647                                       const char *dirName,
648                                       GNUNET_DISK_DirectoryIteratorCallback
649                                       callback, void *callback_cls);
650
651
652 /**
653  * Create the directory structure for storing
654  * a file.
655  *
656  * @param filename name of a file in the directory
657  * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
658  *          GNUNET_NO if directory exists but is not writeable
659  */
660 int
661 GNUNET_DISK_directory_create_for_file (const char *filename);
662
663
664 /**
665  * Test if "fil" is a directory that can be accessed.
666  * Will not print an error message if the directory
667  * does not exist.  Will log errors if GNUNET_SYSERR is
668  * returned.
669  *
670  * @param fil filename to test
671  * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
672  *   on any error and if exists but not directory
673  */
674 int
675 GNUNET_DISK_directory_test (const char *fil);
676
677
678 /**
679  * Remove all files in a directory (rm -rf). Call with
680  * caution.
681  *
682  * @param filename the file to remove
683  * @return GNUNET_OK on success, GNUNET_SYSERR on error
684  */
685 int
686 GNUNET_DISK_directory_remove (const char *filename);
687
688
689 /**
690  * Implementation of "mkdir -p"
691  *
692  * @param dir the directory to create
693  * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
694  */
695 int
696 GNUNET_DISK_directory_create (const char *dir);
697
698
699 /**
700  * Lock a part of a file.
701  *
702  * @param fh file handle
703  * @param lockStart absolute position from where to lock
704  * @param lockEnd absolute position until where to lock
705  * @param excl GNUNET_YES for an exclusive lock
706  * @return GNUNET_OK on success, GNUNET_SYSERR on error
707  */
708 int
709 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
710                        OFF_T lockEnd, int excl);
711
712
713 /**
714  * Unlock a part of a file
715  * @param fh file handle
716  * @param unlockStart absolute position from where to unlock
717  * @param unlockEnd absolute position until where to unlock
718  * @return GNUNET_OK on success, GNUNET_SYSERR on error
719  */
720 int
721 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
722                          OFF_T unlockEnd);
723
724
725 /**
726  * @brief Removes special characters as ':' from a filename.
727  * @param fn the filename to canonicalize
728  */
729 void
730 GNUNET_DISK_filename_canonicalize (char *fn);
731
732
733 /**
734  * @brief Change owner of a file
735  * @param filename file to change
736  * @param user new owner of the file
737  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
738  */
739 int
740 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
741
742
743 /**
744  * Construct full path to a file inside of the private
745  * directory used by GNUnet.  Also creates the corresponding
746  * directory.  If the resulting name is supposed to be
747  * a directory, end the last argument in '/' (or pass
748  * DIR_SEPARATOR_STR as the last argument before NULL).
749  *
750  * @param cfg configuration to use
751  * @param serviceName name of the service asking
752  * @param ... is NULL-terminated list of
753  *                path components to append to the
754  *                private directory name.
755  * @return the constructed filename
756  */
757 char *
758 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
759                                const char *serviceName, ...);
760
761
762 /**
763  * Opaque handle for a memory-mapping operation.
764  */
765 struct GNUNET_DISK_MapHandle;
766
767 /**
768  * Map a file into memory
769  * @param h open file handle
770  * @param m handle to the new mapping (will be set)
771  * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
772  * @param len size of the mapping
773  * @return pointer to the mapped memory region, NULL on failure
774  */
775 void *
776 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
777                       struct GNUNET_DISK_MapHandle **m,
778                       enum GNUNET_DISK_MapType access, size_t len);
779
780 /**
781  * Unmap a file
782  *
783  * @param h mapping handle
784  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
785  */
786 int
787 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
788
789 /**
790  * Write file changes to disk
791  * @param h handle to an open file
792  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
793  */
794 int
795 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
796
797
798 #if 0                           /* keep Emacsens' auto-indent happy */
799 {
800 #endif
801 #ifdef __cplusplus
802 }
803 #endif
804
805
806 /* ifndef GNUNET_DISK_LIB_H */
807 #endif
808 /* end of gnunet_disk_lib.h */