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