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