2 This file is part of GNUnet.
3 Copyright (C) 2001-2012 GNUnet e.V.
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.
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.
16 * @author Christian Grothoff
21 * @defgroup disk Disk library
25 #ifndef GNUNET_DISK_LIB_H
26 #define GNUNET_DISK_LIB_H
29 * Handle used to manage a pipe.
31 struct GNUNET_DISK_PipeHandle;
39 * Handle represents an event.
41 GNUNET_DISK_HANLDE_TYPE_EVENT,
44 * Handle represents a file.
46 GNUNET_DISK_HANLDE_TYPE_FILE,
49 * Handle represents a pipe.
51 GNUNET_DISK_HANLDE_TYPE_PIPE
55 * Handle used to access files (and pipes).
57 struct GNUNET_DISK_FileHandle
62 * File handle under W32.
69 enum GNUNET_FILE_Type type;
72 * Structure for overlapped reading (for pipes)
74 OVERLAPPED *oOverlapRead;
77 * Structure for overlapped writing (for pipes)
79 OVERLAPPED *oOverlapWrite;
83 * File handle on other OSes.
91 /* we need size_t, and since it can be both unsigned int
92 or unsigned long long, this IS platform dependent;
93 but "stdlib.h" should be portable 'enough' to be
94 unconditionally available... */
96 #include "gnunet_configuration_lib.h"
97 #include "gnunet_scheduler_lib.h"
102 #if 0 /* keep Emacsens' auto-indent happy */
109 * Specifies how a file should be opened.
111 enum GNUNET_DISK_OpenFlags
115 * Open the file for reading
117 GNUNET_DISK_OPEN_READ = 1,
120 * Open the file for writing
122 GNUNET_DISK_OPEN_WRITE = 2,
125 * Open the file for both reading and writing
127 GNUNET_DISK_OPEN_READWRITE = 3,
130 * Fail if file already exists
132 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
135 * Truncate file if it exists
137 GNUNET_DISK_OPEN_TRUNCATE = 8,
140 * Create file if it doesn't exist
142 GNUNET_DISK_OPEN_CREATE = 16,
147 GNUNET_DISK_OPEN_APPEND = 32
151 * Specifies what type of memory map is desired.
153 enum GNUNET_DISK_MapType
156 * Read-only memory map.
158 GNUNET_DISK_MAP_TYPE_READ = 1,
161 * Write-able memory map.
163 GNUNET_DISK_MAP_TYPE_WRITE = 2,
166 * Read-write memory map.
168 GNUNET_DISK_MAP_TYPE_READWRITE = 3
173 * File access permissions, UNIX-style.
175 enum GNUNET_DISK_AccessPermissions
178 * Nobody is allowed to do anything to the file.
180 GNUNET_DISK_PERM_NONE = 0,
185 GNUNET_DISK_PERM_USER_READ = 1,
190 GNUNET_DISK_PERM_USER_WRITE = 2,
195 GNUNET_DISK_PERM_USER_EXEC = 4,
200 GNUNET_DISK_PERM_GROUP_READ = 8,
205 GNUNET_DISK_PERM_GROUP_WRITE = 16,
210 GNUNET_DISK_PERM_GROUP_EXEC = 32,
213 * Everybody can read.
215 GNUNET_DISK_PERM_OTHER_READ = 64,
218 * Everybody can write.
220 GNUNET_DISK_PERM_OTHER_WRITE = 128,
223 * Everybody can execute.
225 GNUNET_DISK_PERM_OTHER_EXEC = 256
230 * Constants for specifying how to seek. Do not change values or order,
231 * some of the code depends on the specific numeric values!
233 enum GNUNET_DISK_Seek
236 * Seek an absolute position (from the start of the file).
238 GNUNET_DISK_SEEK_SET = 0,
241 * Seek a relative position (from the current offset).
243 GNUNET_DISK_SEEK_CUR = 1,
246 * Seek an absolute position from the end of the file.
248 GNUNET_DISK_SEEK_END = 2
253 * Enumeration identifying the two ends of a pipe.
255 enum GNUNET_DISK_PipeEnd
258 * The reading-end of a pipe.
260 GNUNET_DISK_PIPE_END_READ = 0,
263 * The writing-end of a pipe.
265 GNUNET_DISK_PIPE_END_WRITE = 1
270 * Checks whether a handle is invalid
272 * @param h handle to check
273 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
276 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
280 * Check that fil corresponds to a filename
281 * (of a file that exists and that is not a directory).
283 * @param fil filename to check
284 * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
285 * else (will print an error message in that case, too).
288 GNUNET_DISK_file_test (const char *fil);
292 * Move a file out of the way (create a backup) by
293 * renaming it to "orig.NUM~" where NUM is the smallest
294 * number that is not used yet.
296 * @param fil name of the file to back up
299 GNUNET_DISK_file_backup (const char *fil);
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
310 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
311 enum GNUNET_DISK_Seek whence);
315 * Get the size of the file (or directory) of the given file (in
318 * @param filename name of the file or directory
319 * @param size set to the size of the file (or,
320 * in the case of directories, the sum
321 * of all sizes of files in the directory)
322 * @param include_symbolic_links should symbolic links be
324 * @param single_file_mode #GNUNET_YES to only get size of one file
325 * and return #GNUNET_SYSERR for directories.
326 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
329 GNUNET_DISK_file_size (const char *filename,
331 int include_symbolic_links,
332 int single_file_mode);
336 * Obtain some unique identifiers for the given file
337 * that can be used to identify it in the local system.
338 * This function is used between GNUnet processes to
339 * quickly check if two files with the same absolute path
340 * are actually identical. The two processes represent
341 * the same peer but may communicate over the network
342 * (and the file may be on an NFS volume). This function
343 * may not be supported on all operating systems.
345 * @param filename name of the file
346 * @param dev set to the device ID
347 * @param ino set to the inode ID
348 * @return #GNUNET_OK on success
351 GNUNET_DISK_file_get_identifiers (const char *filename,
357 * Create an (empty) temporary file on disk. If the given name is not
358 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
359 * 6 random characters will be appended to the name to create a unique
362 * @param t component to use for the name;
363 * does NOT contain "XXXXXX" or "/tmp/".
364 * @return NULL on error, otherwise name of fresh
365 * file on disk in directory for temporary files
368 GNUNET_DISK_mktemp (const char *t);
372 * Create an (empty) temporary directory on disk. If the given name is not an
373 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
374 * random characters will be appended to the name to create a unique name.
376 * @param t component to use for the name;
377 * does NOT contain "XXXXXX" or "/tmp/".
378 * @return NULL on error, otherwise name of freshly created directory
381 GNUNET_DISK_mkdtemp (const char *t);
385 * Open a file. Note that the access permissions will only be
386 * used if a new file is created and if the underlying operating
387 * system supports the given permissions.
389 * @param fn file name to be opened
390 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
391 * @param perm permissions for the newly created file, use
392 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
393 * call (because of flags)
394 * @return IO handle on success, NULL on error
396 struct GNUNET_DISK_FileHandle *
397 GNUNET_DISK_file_open (const char *fn,
398 enum GNUNET_DISK_OpenFlags flags,
399 enum GNUNET_DISK_AccessPermissions perm);
403 * Get the size of an open file.
405 * @param fh open file handle
406 * @param size where to write size of the file
407 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
410 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
415 * Creates an interprocess channel
417 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
418 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
419 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
420 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
421 * @return handle to the new pipe, NULL on error
423 struct GNUNET_DISK_PipeHandle *
424 GNUNET_DISK_pipe (int blocking_read,
431 * Creates a pipe object from a couple of file descriptors.
432 * Useful for wrapping existing pipe FDs.
434 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
435 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
436 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
438 * @return handle to the new pipe, NULL on error
440 struct GNUNET_DISK_PipeHandle *
441 GNUNET_DISK_pipe_from_fd (int blocking_read,
447 * Closes an interprocess channel
449 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
452 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
456 * Closes one half of an interprocess channel
458 * @param p pipe to close end of
459 * @param end which end of the pipe to close
460 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
463 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
464 enum GNUNET_DISK_PipeEnd end);
468 * Detaches one of the ends from the pipe.
469 * Detached end is a fully-functional FileHandle, it will
470 * not be affected by anything you do with the pipe afterwards.
471 * Each end of a pipe can only be detched from it once (i.e.
472 * it is not duplicated).
474 * @param p pipe to detach an end from
475 * @param end which end of the pipe to detach
476 * @return Detached end on success, NULL on failure
477 * (or if that end is not present or is closed).
479 struct GNUNET_DISK_FileHandle *
480 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
481 enum GNUNET_DISK_PipeEnd end);
484 * Close an open file.
486 * @param h file handle
487 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
490 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
494 * Get the handle to a particular pipe end
497 * @param n end to access
498 * @return handle for the respective end
500 const struct GNUNET_DISK_FileHandle *
501 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
502 enum GNUNET_DISK_PipeEnd n);
507 * Get a GNUnet file handle from a W32 handle (W32-only).
508 * Do not call on non-W32 platforms (returns NULL).
510 * @param handle native handle
511 * @return GNUnet file handle corresponding to the W32 handle
513 struct GNUNET_DISK_FileHandle *
514 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
518 * Update POSIX permissions mask of a file on disk. If both argumets
519 * are #GNUNET_NO, the file is made world-read-write-executable (777).
520 * Does nothing on W32.
522 * @param fn name of the file to update
523 * @param require_uid_match #GNUNET_YES means 700
524 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
527 GNUNET_DISK_fix_permissions (const char *fn,
528 int require_uid_match,
529 int require_gid_match);
533 * Get a handle from a native integer FD.
535 * @param fno native integer file descriptor
536 * @return file handle corresponding to the descriptor
538 struct GNUNET_DISK_FileHandle *
539 GNUNET_DISK_get_handle_from_int_fd (int fno);
543 * Get a handle from a native FD.
545 * @param fd native file descriptor
546 * @return file handle corresponding to the descriptor
548 struct GNUNET_DISK_FileHandle *
549 GNUNET_DISK_get_handle_from_native (FILE *fd);
553 * Read the contents of a binary file into a buffer.
555 * @param h handle to an open file
556 * @param result the buffer to write the result to
557 * @param len the maximum number of bytes to read
558 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
561 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
567 * Read the contents of a binary file into a buffer.
568 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
569 * when no data can be read).
571 * @param h handle to an open file
572 * @param result the buffer to write the result to
573 * @param len the maximum number of bytes to read
574 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
577 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
583 * Read the contents of a binary file into a buffer.
585 * @param fn file name
586 * @param result the buffer to write the result to
587 * @param len the maximum number of bytes to read
588 * @return number of bytes read, #GNUNET_SYSERR on failure
591 GNUNET_DISK_fn_read (const char *fn,
597 * Write a buffer to a file.
599 * @param h handle to open file
600 * @param buffer the data to write
601 * @param n number of bytes to write
602 * @return number of bytes written on success, #GNUNET_SYSERR on error
605 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
611 * Write a buffer to a file, blocking, if necessary.
613 * @param h handle to open file
614 * @param buffer the data to write
615 * @param n number of bytes to write
616 * @return number of bytes written on success, #GNUNET_SYSERR on error
619 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
625 * Write a buffer to a file. If the file is longer than
626 * the given buffer size, it will be truncated.
628 * @param fn file name
629 * @param buffer the data to write
630 * @param n number of bytes to write
631 * @param mode file permissions
632 * @return number of bytes written on success, #GNUNET_SYSERR on error
635 GNUNET_DISK_fn_write (const char *fn,
638 enum GNUNET_DISK_AccessPermissions mode);
644 * @param src file to copy
645 * @param dst destination file name
646 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
649 GNUNET_DISK_file_copy (const char *src,
654 * Scan a directory for files.
656 * @param dir_name the name of the directory
657 * @param callback the method to call for each file
658 * @param callback_cls closure for @a callback
659 * @return the number of files found, -1 on error
662 GNUNET_DISK_directory_scan (const char *dir_name,
663 GNUNET_FileNameCallback callback,
668 * Create the directory structure for storing
671 * @param filename name of a file in the directory
672 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
673 * #GNUNET_NO if directory exists but is not writeable
676 GNUNET_DISK_directory_create_for_file (const char *filename);
680 * Test if @a fil is a directory and listable. Optionally, also check if the
681 * directory is readable. Will not print an error message if the directory does
682 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
683 * with the same name).
685 * @param fil filename to test
686 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
687 * #GNUNET_NO to disable this check
688 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
689 * does not exist or `stat`ed
692 GNUNET_DISK_directory_test (const char *fil, int is_readable);
696 * Remove all files in a directory (rm -rf). Call with
699 * @param filename the file to remove
700 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
703 GNUNET_DISK_directory_remove (const char *filename);
707 * Remove the directory given under @a option in
708 * section [PATHS] in configuration under @a cfg_filename
710 * @param cfg_filename configuration file to parse
711 * @param option option with the dir name to purge
714 GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
719 * Implementation of "mkdir -p"
721 * @param dir the directory to create
722 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
725 GNUNET_DISK_directory_create (const char *dir);
729 * Lock a part of a file.
731 * @param fh file handle
732 * @param lock_start absolute position from where to lock
733 * @param lock_end absolute position until where to lock
734 * @param excl #GNUNET_YES for an exclusive lock
735 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
738 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
740 off_t lock_end, int excl);
744 * Unlock a part of a file.
746 * @param fh file handle
747 * @param unlock_start absolute position from where to unlock
748 * @param unlock_end absolute position until where to unlock
749 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
752 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
758 * @brief Removes special characters as ':' from a filename.
759 * @param fn the filename to canonicalize
762 GNUNET_DISK_filename_canonicalize (char *fn);
766 * @brief Change owner of a file
767 * @param filename file to change
768 * @param user new owner of the file
769 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
772 GNUNET_DISK_file_change_owner (const char *filename,
777 * Opaque handle for a memory-mapping operation.
779 struct GNUNET_DISK_MapHandle;
782 * Map a file into memory
783 * @param h open file handle
784 * @param m handle to the new mapping (will be set)
785 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
786 * @param len size of the mapping
787 * @return pointer to the mapped memory region, NULL on failure
790 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
791 struct GNUNET_DISK_MapHandle **m,
792 enum GNUNET_DISK_MapType access,
799 * @param h mapping handle
800 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
803 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
807 * Write file changes to disk
809 * @param h handle to an open file
810 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
813 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
816 #if 0 /* keep Emacsens' auto-indent happy */
823 /* ifndef GNUNET_DISK_LIB_H */
826 /** @} */ /* end of group */
828 /* end of gnunet_disk_lib.h */