2 This file is part of GNUnet.
3 Copyright (C) 2001-2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
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, uint64_t *size,
335 int include_symbolic_links,
336 int single_file_mode);
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.
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
355 GNUNET_DISK_file_get_identifiers (const char *filename,
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
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
372 GNUNET_DISK_mktemp (const char *t);
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.
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
385 GNUNET_DISK_mkdtemp (const char *t);
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.
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
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);
407 * Get the size of an open file.
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
414 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
419 * Creates an interprocess channel
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
427 struct GNUNET_DISK_PipeHandle *
428 GNUNET_DISK_pipe (int blocking_read,
435 * Creates a pipe object from a couple of file descriptors.
436 * Useful for wrapping existing pipe FDs.
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
442 * @return handle to the new pipe, NULL on error
444 struct GNUNET_DISK_PipeHandle *
445 GNUNET_DISK_pipe_from_fd (int blocking_read,
451 * Closes an interprocess channel
453 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
456 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
460 * Closes one half of an interprocess channel
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
467 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
468 enum GNUNET_DISK_PipeEnd end);
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).
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).
483 struct GNUNET_DISK_FileHandle *
484 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
485 enum GNUNET_DISK_PipeEnd end);
488 * Close an open file.
490 * @param h file handle
491 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
494 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
498 * Get the handle to a particular pipe end
501 * @param n end to access
502 * @return handle for the respective end
504 const struct GNUNET_DISK_FileHandle *
505 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
506 enum GNUNET_DISK_PipeEnd n);
511 * Get a GNUnet file handle from a W32 handle (W32-only).
512 * Do not call on non-W32 platforms (returns NULL).
514 * @param handle native handle
515 * @return GNUnet file handle corresponding to the W32 handle
517 struct GNUNET_DISK_FileHandle *
518 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
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.
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
531 GNUNET_DISK_fix_permissions (const char *fn,
532 int require_uid_match,
533 int require_gid_match);
537 * Get a handle from a native integer FD.
539 * @param fno native integer file descriptor
540 * @return file handle corresponding to the descriptor
542 struct GNUNET_DISK_FileHandle *
543 GNUNET_DISK_get_handle_from_int_fd (int fno);
547 * Get a handle from a native FD.
549 * @param fd native file descriptor
550 * @return file handle corresponding to the descriptor
552 struct GNUNET_DISK_FileHandle *
553 GNUNET_DISK_get_handle_from_native (FILE *fd);
557 * Read the contents of a binary file into a buffer.
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
565 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
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).
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
581 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
587 * Read the contents of a binary file into a buffer.
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
595 GNUNET_DISK_fn_read (const char *fn,
601 * Write a buffer to a file.
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
609 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
615 * Write a buffer to a file, blocking, if necessary.
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
623 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
629 * Write a buffer to a file. If the file is longer than
630 * the given buffer size, it will be truncated.
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
639 GNUNET_DISK_fn_write (const char *fn,
642 enum GNUNET_DISK_AccessPermissions mode);
648 * @param src file to copy
649 * @param dst destination file name
650 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
653 GNUNET_DISK_file_copy (const char *src,
658 * Scan a directory for files.
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
666 GNUNET_DISK_directory_scan (const char *dir_name,
667 GNUNET_FileNameCallback callback,
672 * Opaque handle used for iterating over a directory.
674 struct GNUNET_DISK_DirectoryIterator;
678 * Function called to iterate over a directory.
681 * @param di argument to pass to #GNUNET_DISK_directory_iterator_next to
682 * get called on the next entry (or finish cleanly);
683 * NULL on error (will be the last call in that case)
684 * @param filename complete filename (absolute path)
685 * @param dirname directory name (absolute path)
688 (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
689 struct GNUNET_DISK_DirectoryIterator *di,
690 const char *filename,
691 const char *dirname);
695 * This function must be called during the DiskIteratorCallback
696 * (exactly once) to schedule the task to process the next
697 * filename in the directory (if there is one).
699 * @param iter opaque handle for the iterator
700 * @param can set to #GNUNET_YES to terminate the iteration early
701 * @return #GNUNET_YES if iteration will continue,
702 * #GNUNET_NO if this was the last entry (and iteration is complete),
703 * #GNUNET_SYSERR if @a can was #GNUNET_YES
706 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
711 * Scan a directory for files using the scheduler to run a task for
712 * each entry. The name of the directory must be expanded first (!).
713 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
714 * may provide a simpler API.
716 * @param prio priority to use
717 * @param dir_name the name of the directory
718 * @param callback the method to call for each file
719 * @param callback_cls closure for @a callback
720 * @return #GNUNET_YES if directory is not empty and @a callback
721 * will be called later, #GNUNET_NO otherwise, #GNUNET_SYSERR on error.
724 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
725 const char *dir_name,
726 GNUNET_DISK_DirectoryIteratorCallback
727 callback, void *callback_cls);
731 * Create the directory structure for storing
734 * @param filename name of a file in the directory
735 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
736 * #GNUNET_NO if directory exists but is not writeable
739 GNUNET_DISK_directory_create_for_file (const char *filename);
743 * Test if @a fil is a directory and listable. Optionally, also check if the
744 * directory is readable. Will not print an error message if the directory does
745 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
746 * with the same name).
748 * @param fil filename to test
749 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
750 * #GNUNET_NO to disable this check
751 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
752 * does not exist or `stat`ed
755 GNUNET_DISK_directory_test (const char *fil, int is_readable);
759 * Remove all files in a directory (rm -rf). Call with
762 * @param filename the file to remove
763 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
766 GNUNET_DISK_directory_remove (const char *filename);
770 * Implementation of "mkdir -p"
772 * @param dir the directory to create
773 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
776 GNUNET_DISK_directory_create (const char *dir);
780 * Lock a part of a file.
782 * @param fh file handle
783 * @param lock_start absolute position from where to lock
784 * @param lock_end absolute position until where to lock
785 * @param excl #GNUNET_YES for an exclusive lock
786 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
789 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
791 off_t lock_end, int excl);
795 * Unlock a part of a file.
797 * @param fh file handle
798 * @param unlock_start absolute position from where to unlock
799 * @param unlock_end absolute position until where to unlock
800 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
803 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
809 * @brief Removes special characters as ':' from a filename.
810 * @param fn the filename to canonicalize
813 GNUNET_DISK_filename_canonicalize (char *fn);
817 * @brief Change owner of a file
818 * @param filename file to change
819 * @param user new owner of the file
820 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
823 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
827 * Opaque handle for a memory-mapping operation.
829 struct GNUNET_DISK_MapHandle;
832 * Map a file into memory
833 * @param h open file handle
834 * @param m handle to the new mapping (will be set)
835 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
836 * @param len size of the mapping
837 * @return pointer to the mapped memory region, NULL on failure
840 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
841 struct GNUNET_DISK_MapHandle **m,
842 enum GNUNET_DISK_MapType access, size_t len);
847 * @param h mapping handle
848 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
851 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
854 * Write file changes to disk
855 * @param h handle to an open file
856 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
859 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
862 #if 0 /* keep Emacsens' auto-indent happy */
869 /* ifndef GNUNET_DISK_LIB_H */
872 /** @} */ /* end of group */
874 /* end of gnunet_disk_lib.h */