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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Christian Grothoff
26 * @defgroup disk Disk library
30 #ifndef GNUNET_DISK_LIB_H
31 #define GNUNET_DISK_LIB_H
34 * Handle used to manage a pipe.
36 struct GNUNET_DISK_PipeHandle;
44 * Handle represents an event.
46 GNUNET_DISK_HANLDE_TYPE_EVENT,
49 * Handle represents a file.
51 GNUNET_DISK_HANLDE_TYPE_FILE,
54 * Handle represents a pipe.
56 GNUNET_DISK_HANLDE_TYPE_PIPE
60 * Handle used to access files (and pipes).
62 struct GNUNET_DISK_FileHandle
67 * File handle under W32.
74 enum GNUNET_FILE_Type type;
77 * Structure for overlapped reading (for pipes)
79 OVERLAPPED *oOverlapRead;
82 * Structure for overlapped writing (for pipes)
84 OVERLAPPED *oOverlapWrite;
88 * File handle on other OSes.
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... */
101 #include "gnunet_configuration_lib.h"
102 #include "gnunet_scheduler_lib.h"
107 #if 0 /* keep Emacsens' auto-indent happy */
114 * Specifies how a file should be opened.
116 enum GNUNET_DISK_OpenFlags
120 * Open the file for reading
122 GNUNET_DISK_OPEN_READ = 1,
125 * Open the file for writing
127 GNUNET_DISK_OPEN_WRITE = 2,
130 * Open the file for both reading and writing
132 GNUNET_DISK_OPEN_READWRITE = 3,
135 * Fail if file already exists
137 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
140 * Truncate file if it exists
142 GNUNET_DISK_OPEN_TRUNCATE = 8,
145 * Create file if it doesn't exist
147 GNUNET_DISK_OPEN_CREATE = 16,
152 GNUNET_DISK_OPEN_APPEND = 32
156 * Specifies what type of memory map is desired.
158 enum GNUNET_DISK_MapType
161 * Read-only memory map.
163 GNUNET_DISK_MAP_TYPE_READ = 1,
166 * Write-able memory map.
168 GNUNET_DISK_MAP_TYPE_WRITE = 2,
171 * Read-write memory map.
173 GNUNET_DISK_MAP_TYPE_READWRITE = 3
178 * File access permissions, UNIX-style.
180 enum GNUNET_DISK_AccessPermissions
183 * Nobody is allowed to do anything to the file.
185 GNUNET_DISK_PERM_NONE = 0,
190 GNUNET_DISK_PERM_USER_READ = 1,
195 GNUNET_DISK_PERM_USER_WRITE = 2,
200 GNUNET_DISK_PERM_USER_EXEC = 4,
205 GNUNET_DISK_PERM_GROUP_READ = 8,
210 GNUNET_DISK_PERM_GROUP_WRITE = 16,
215 GNUNET_DISK_PERM_GROUP_EXEC = 32,
218 * Everybody can read.
220 GNUNET_DISK_PERM_OTHER_READ = 64,
223 * Everybody can write.
225 GNUNET_DISK_PERM_OTHER_WRITE = 128,
228 * Everybody can execute.
230 GNUNET_DISK_PERM_OTHER_EXEC = 256
235 * Constants for specifying how to seek. Do not change values or order,
236 * some of the code depends on the specific numeric values!
238 enum GNUNET_DISK_Seek
241 * Seek an absolute position (from the start of the file).
243 GNUNET_DISK_SEEK_SET = 0,
246 * Seek a relative position (from the current offset).
248 GNUNET_DISK_SEEK_CUR = 1,
251 * Seek an absolute position from the end of the file.
253 GNUNET_DISK_SEEK_END = 2
258 * Enumeration identifying the two ends of a pipe.
260 enum GNUNET_DISK_PipeEnd
263 * The reading-end of a pipe.
265 GNUNET_DISK_PIPE_END_READ = 0,
268 * The writing-end of a pipe.
270 GNUNET_DISK_PIPE_END_WRITE = 1
275 * Checks whether a handle is invalid
277 * @param h handle to check
278 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
281 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
285 * Check that fil corresponds to a filename
286 * (of a file that exists and that is not a directory).
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).
293 GNUNET_DISK_file_test (const char *fil);
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.
301 * @param fil name of the file to back up
304 GNUNET_DISK_file_backup (const char *fil);
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
315 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
316 enum GNUNET_DISK_Seek whence);
320 * Get the size of the file (or directory) of the given file (in
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
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
334 GNUNET_DISK_file_size (const char *filename,
336 int include_symbolic_links,
337 int single_file_mode);
341 * Obtain some unique identifiers for the given file
342 * that can be used to identify it in the local system.
343 * This function is used between GNUnet processes to
344 * quickly check if two files with the same absolute path
345 * are actually identical. The two processes represent
346 * the same peer but may communicate over the network
347 * (and the file may be on an NFS volume). This function
348 * may not be supported on all operating systems.
350 * @param filename name of the file
351 * @param dev set to the device ID
352 * @param ino set to the inode ID
353 * @return #GNUNET_OK on success
356 GNUNET_DISK_file_get_identifiers (const char *filename,
362 * Create an (empty) temporary file on disk. If the given name is not
363 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
364 * 6 random characters will be appended to the name to create a unique
367 * @param t component to use for the name;
368 * does NOT contain "XXXXXX" or "/tmp/".
369 * @return NULL on error, otherwise name of fresh
370 * file on disk in directory for temporary files
373 GNUNET_DISK_mktemp (const char *t);
377 * Create an (empty) temporary directory on disk. If the given name is not an
378 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
379 * random characters will be appended to the name to create a unique name.
381 * @param t component to use for the name;
382 * does NOT contain "XXXXXX" or "/tmp/".
383 * @return NULL on error, otherwise name of freshly created directory
386 GNUNET_DISK_mkdtemp (const char *t);
390 * Open a file. Note that the access permissions will only be
391 * used if a new file is created and if the underlying operating
392 * system supports the given permissions.
394 * @param fn file name to be opened
395 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
396 * @param perm permissions for the newly created file, use
397 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
398 * call (because of flags)
399 * @return IO handle on success, NULL on error
401 struct GNUNET_DISK_FileHandle *
402 GNUNET_DISK_file_open (const char *fn,
403 enum GNUNET_DISK_OpenFlags flags,
404 enum GNUNET_DISK_AccessPermissions perm);
408 * Get the size of an open file.
410 * @param fh open file handle
411 * @param size where to write size of the file
412 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
415 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
420 * Creates an interprocess channel
422 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
423 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
424 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
425 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
426 * @return handle to the new pipe, NULL on error
428 struct GNUNET_DISK_PipeHandle *
429 GNUNET_DISK_pipe (int blocking_read,
436 * Creates a pipe object from a couple of file descriptors.
437 * Useful for wrapping existing pipe FDs.
439 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
440 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
441 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
443 * @return handle to the new pipe, NULL on error
445 struct GNUNET_DISK_PipeHandle *
446 GNUNET_DISK_pipe_from_fd (int blocking_read,
452 * Closes an interprocess channel
454 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
457 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
461 * Closes one half of an interprocess channel
463 * @param p pipe to close end of
464 * @param end which end of the pipe to close
465 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
468 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
469 enum GNUNET_DISK_PipeEnd end);
473 * Detaches one of the ends from the pipe.
474 * Detached end is a fully-functional FileHandle, it will
475 * not be affected by anything you do with the pipe afterwards.
476 * Each end of a pipe can only be detched from it once (i.e.
477 * it is not duplicated).
479 * @param p pipe to detach an end from
480 * @param end which end of the pipe to detach
481 * @return Detached end on success, NULL on failure
482 * (or if that end is not present or is closed).
484 struct GNUNET_DISK_FileHandle *
485 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
486 enum GNUNET_DISK_PipeEnd end);
489 * Close an open file.
491 * @param h file handle
492 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
495 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
499 * Get the handle to a particular pipe end
502 * @param n end to access
503 * @return handle for the respective end
505 const struct GNUNET_DISK_FileHandle *
506 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
507 enum GNUNET_DISK_PipeEnd n);
512 * Get a GNUnet file handle from a W32 handle (W32-only).
513 * Do not call on non-W32 platforms (returns NULL).
515 * @param handle native handle
516 * @return GNUnet file handle corresponding to the W32 handle
518 struct GNUNET_DISK_FileHandle *
519 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
523 * Update POSIX permissions mask of a file on disk. If both argumets
524 * are #GNUNET_NO, the file is made world-read-write-executable (777).
525 * Does nothing on W32.
527 * @param fn name of the file to update
528 * @param require_uid_match #GNUNET_YES means 700
529 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
532 GNUNET_DISK_fix_permissions (const char *fn,
533 int require_uid_match,
534 int require_gid_match);
538 * Get a handle from a native integer FD.
540 * @param fno native integer file descriptor
541 * @return file handle corresponding to the descriptor
543 struct GNUNET_DISK_FileHandle *
544 GNUNET_DISK_get_handle_from_int_fd (int fno);
548 * Get a handle from a native FD.
550 * @param fd native file descriptor
551 * @return file handle corresponding to the descriptor
553 struct GNUNET_DISK_FileHandle *
554 GNUNET_DISK_get_handle_from_native (FILE *fd);
558 * Read the contents of a binary file into a buffer.
560 * @param h handle to an open file
561 * @param result the buffer to write the result to
562 * @param len the maximum number of bytes to read
563 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
566 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
572 * Read the contents of a binary file into a buffer.
573 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
574 * when no data can be read).
576 * @param h handle to an open file
577 * @param result the buffer to write the result to
578 * @param len the maximum number of bytes to read
579 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
582 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
588 * Read the contents of a binary file into a buffer.
590 * @param fn file name
591 * @param result the buffer to write the result to
592 * @param len the maximum number of bytes to read
593 * @return number of bytes read, #GNUNET_SYSERR on failure
596 GNUNET_DISK_fn_read (const char *fn,
602 * Write a buffer to a file.
604 * @param h handle to open file
605 * @param buffer the data to write
606 * @param n number of bytes to write
607 * @return number of bytes written on success, #GNUNET_SYSERR on error
610 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
616 * Write a buffer to a file, blocking, if necessary.
618 * @param h handle to open file
619 * @param buffer the data to write
620 * @param n number of bytes to write
621 * @return number of bytes written on success, #GNUNET_SYSERR on error
624 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
630 * Write a buffer to a file. If the file is longer than
631 * the given buffer size, it will be truncated.
633 * @param fn file name
634 * @param buffer the data to write
635 * @param n number of bytes to write
636 * @param mode file permissions
637 * @return number of bytes written on success, #GNUNET_SYSERR on error
640 GNUNET_DISK_fn_write (const char *fn,
643 enum GNUNET_DISK_AccessPermissions mode);
649 * @param src file to copy
650 * @param dst destination file name
651 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
654 GNUNET_DISK_file_copy (const char *src,
659 * Scan a directory for files.
661 * @param dir_name the name of the directory
662 * @param callback the method to call for each file
663 * @param callback_cls closure for @a callback
664 * @return the number of files found, -1 on error
667 GNUNET_DISK_directory_scan (const char *dir_name,
668 GNUNET_FileNameCallback callback,
673 * Create the directory structure for storing
676 * @param filename name of a file in the directory
677 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
678 * #GNUNET_NO if directory exists but is not writeable
681 GNUNET_DISK_directory_create_for_file (const char *filename);
685 * Test if @a fil is a directory and listable. Optionally, also check if the
686 * directory is readable. Will not print an error message if the directory does
687 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
688 * with the same name).
690 * @param fil filename to test
691 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
692 * #GNUNET_NO to disable this check
693 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
694 * does not exist or `stat`ed
697 GNUNET_DISK_directory_test (const char *fil, int is_readable);
701 * Remove all files in a directory (rm -rf). Call with
704 * @param filename the file to remove
705 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
708 GNUNET_DISK_directory_remove (const char *filename);
712 * Remove the directory given under @a option in
713 * section [PATHS] in configuration under @a cfg_filename
715 * @param cfg_filename configuration file to parse
716 * @param option option with the dir name to purge
719 GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
724 * Implementation of "mkdir -p"
726 * @param dir the directory to create
727 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
730 GNUNET_DISK_directory_create (const char *dir);
734 * Lock a part of a file.
736 * @param fh file handle
737 * @param lock_start absolute position from where to lock
738 * @param lock_end absolute position until where to lock
739 * @param excl #GNUNET_YES for an exclusive lock
740 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
743 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
745 off_t lock_end, int excl);
749 * Unlock a part of a file.
751 * @param fh file handle
752 * @param unlock_start absolute position from where to unlock
753 * @param unlock_end absolute position until where to unlock
754 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
757 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
763 * @brief Removes special characters as ':' from a filename.
764 * @param fn the filename to canonicalize
767 GNUNET_DISK_filename_canonicalize (char *fn);
771 * @brief Change owner of a file
772 * @param filename file to change
773 * @param user new owner of the file
774 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
777 GNUNET_DISK_file_change_owner (const char *filename,
782 * Opaque handle for a memory-mapping operation.
784 struct GNUNET_DISK_MapHandle;
788 * Map a file into memory
789 * @param h open file handle
790 * @param m handle to the new mapping (will be set)
791 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
792 * @param len size of the mapping
793 * @return pointer to the mapped memory region, NULL on failure
796 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
797 struct GNUNET_DISK_MapHandle **m,
798 enum GNUNET_DISK_MapType access,
805 * @param h mapping handle
806 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
809 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
813 * Write file changes to disk
815 * @param h handle to an open file
816 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
819 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
822 #if 0 /* keep Emacsens' auto-indent happy */
829 /* ifndef GNUNET_DISK_LIB_H */
832 /** @} */ /* end of group */
834 /* end of gnunet_disk_lib.h */