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/>.
19 * @author Christian Grothoff
24 * @defgroup disk Disk library
28 #ifndef GNUNET_DISK_LIB_H
29 #define GNUNET_DISK_LIB_H
32 * Handle used to manage a pipe.
34 struct GNUNET_DISK_PipeHandle;
42 * Handle represents an event.
44 GNUNET_DISK_HANLDE_TYPE_EVENT,
47 * Handle represents a file.
49 GNUNET_DISK_HANLDE_TYPE_FILE,
52 * Handle represents a pipe.
54 GNUNET_DISK_HANLDE_TYPE_PIPE
58 * Handle used to access files (and pipes).
60 struct GNUNET_DISK_FileHandle
65 * File handle under W32.
72 enum GNUNET_FILE_Type type;
75 * Structure for overlapped reading (for pipes)
77 OVERLAPPED *oOverlapRead;
80 * Structure for overlapped writing (for pipes)
82 OVERLAPPED *oOverlapWrite;
86 * File handle on other OSes.
94 /* we need size_t, and since it can be both unsigned int
95 or unsigned long long, this IS platform dependent;
96 but "stdlib.h" should be portable 'enough' to be
97 unconditionally available... */
99 #include "gnunet_configuration_lib.h"
100 #include "gnunet_scheduler_lib.h"
105 #if 0 /* keep Emacsens' auto-indent happy */
112 * Specifies how a file should be opened.
114 enum GNUNET_DISK_OpenFlags
118 * Open the file for reading
120 GNUNET_DISK_OPEN_READ = 1,
123 * Open the file for writing
125 GNUNET_DISK_OPEN_WRITE = 2,
128 * Open the file for both reading and writing
130 GNUNET_DISK_OPEN_READWRITE = 3,
133 * Fail if file already exists
135 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
138 * Truncate file if it exists
140 GNUNET_DISK_OPEN_TRUNCATE = 8,
143 * Create file if it doesn't exist
145 GNUNET_DISK_OPEN_CREATE = 16,
150 GNUNET_DISK_OPEN_APPEND = 32
154 * Specifies what type of memory map is desired.
156 enum GNUNET_DISK_MapType
159 * Read-only memory map.
161 GNUNET_DISK_MAP_TYPE_READ = 1,
164 * Write-able memory map.
166 GNUNET_DISK_MAP_TYPE_WRITE = 2,
169 * Read-write memory map.
171 GNUNET_DISK_MAP_TYPE_READWRITE = 3
176 * File access permissions, UNIX-style.
178 enum GNUNET_DISK_AccessPermissions
181 * Nobody is allowed to do anything to the file.
183 GNUNET_DISK_PERM_NONE = 0,
188 GNUNET_DISK_PERM_USER_READ = 1,
193 GNUNET_DISK_PERM_USER_WRITE = 2,
198 GNUNET_DISK_PERM_USER_EXEC = 4,
203 GNUNET_DISK_PERM_GROUP_READ = 8,
208 GNUNET_DISK_PERM_GROUP_WRITE = 16,
213 GNUNET_DISK_PERM_GROUP_EXEC = 32,
216 * Everybody can read.
218 GNUNET_DISK_PERM_OTHER_READ = 64,
221 * Everybody can write.
223 GNUNET_DISK_PERM_OTHER_WRITE = 128,
226 * Everybody can execute.
228 GNUNET_DISK_PERM_OTHER_EXEC = 256
233 * Constants for specifying how to seek. Do not change values or order,
234 * some of the code depends on the specific numeric values!
236 enum GNUNET_DISK_Seek
239 * Seek an absolute position (from the start of the file).
241 GNUNET_DISK_SEEK_SET = 0,
244 * Seek a relative position (from the current offset).
246 GNUNET_DISK_SEEK_CUR = 1,
249 * Seek an absolute position from the end of the file.
251 GNUNET_DISK_SEEK_END = 2
256 * Enumeration identifying the two ends of a pipe.
258 enum GNUNET_DISK_PipeEnd
261 * The reading-end of a pipe.
263 GNUNET_DISK_PIPE_END_READ = 0,
266 * The writing-end of a pipe.
268 GNUNET_DISK_PIPE_END_WRITE = 1
273 * Checks whether a handle is invalid
275 * @param h handle to check
276 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
279 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
283 * Check that fil corresponds to a filename
284 * (of a file that exists and that is not a directory).
286 * @param fil filename to check
287 * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
288 * else (will print an error message in that case, too).
291 GNUNET_DISK_file_test (const char *fil);
295 * Move a file out of the way (create a backup) by
296 * renaming it to "orig.NUM~" where NUM is the smallest
297 * number that is not used yet.
299 * @param fil name of the file to back up
302 GNUNET_DISK_file_backup (const char *fil);
306 * Move the read/write pointer in a file
307 * @param h handle of an open file
308 * @param offset position to move to
309 * @param whence specification to which position the offset parameter relates to
310 * @return the new position on success, GNUNET_SYSERR otherwise
313 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
314 enum GNUNET_DISK_Seek whence);
318 * Get the size of the file (or directory) of the given file (in
321 * @param filename name of the file or directory
322 * @param size set to the size of the file (or,
323 * in the case of directories, the sum
324 * of all sizes of files in the directory)
325 * @param include_symbolic_links should symbolic links be
327 * @param single_file_mode #GNUNET_YES to only get size of one file
328 * and return #GNUNET_SYSERR for directories.
329 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
332 GNUNET_DISK_file_size (const char *filename,
334 int include_symbolic_links,
335 int single_file_mode);
339 * Obtain some unique identifiers for the given file
340 * that can be used to identify it in the local system.
341 * This function is used between GNUnet processes to
342 * quickly check if two files with the same absolute path
343 * are actually identical. The two processes represent
344 * the same peer but may communicate over the network
345 * (and the file may be on an NFS volume). This function
346 * may not be supported on all operating systems.
348 * @param filename name of the file
349 * @param dev set to the device ID
350 * @param ino set to the inode ID
351 * @return #GNUNET_OK on success
354 GNUNET_DISK_file_get_identifiers (const char *filename,
360 * Create an (empty) temporary file on disk. If the given name is not
361 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
362 * 6 random characters will be appended to the name to create a unique
365 * @param t component to use for the name;
366 * does NOT contain "XXXXXX" or "/tmp/".
367 * @return NULL on error, otherwise name of fresh
368 * file on disk in directory for temporary files
371 GNUNET_DISK_mktemp (const char *t);
375 * Create an (empty) temporary directory on disk. If the given name is not an
376 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
377 * random characters will be appended to the name to create a unique name.
379 * @param t component to use for the name;
380 * does NOT contain "XXXXXX" or "/tmp/".
381 * @return NULL on error, otherwise name of freshly created directory
384 GNUNET_DISK_mkdtemp (const char *t);
388 * Open a file. Note that the access permissions will only be
389 * used if a new file is created and if the underlying operating
390 * system supports the given permissions.
392 * @param fn file name to be opened
393 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
394 * @param perm permissions for the newly created file, use
395 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
396 * call (because of flags)
397 * @return IO handle on success, NULL on error
399 struct GNUNET_DISK_FileHandle *
400 GNUNET_DISK_file_open (const char *fn,
401 enum GNUNET_DISK_OpenFlags flags,
402 enum GNUNET_DISK_AccessPermissions perm);
406 * Get the size of an open file.
408 * @param fh open file handle
409 * @param size where to write size of the file
410 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
413 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
418 * Creates an interprocess channel
420 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
421 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
422 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
423 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
424 * @return handle to the new pipe, NULL on error
426 struct GNUNET_DISK_PipeHandle *
427 GNUNET_DISK_pipe (int blocking_read,
434 * Creates a pipe object from a couple of file descriptors.
435 * Useful for wrapping existing pipe FDs.
437 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
438 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
439 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
441 * @return handle to the new pipe, NULL on error
443 struct GNUNET_DISK_PipeHandle *
444 GNUNET_DISK_pipe_from_fd (int blocking_read,
450 * Closes an interprocess channel
452 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
455 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
459 * Closes one half of an interprocess channel
461 * @param p pipe to close end of
462 * @param end which end of the pipe to close
463 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
466 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
467 enum GNUNET_DISK_PipeEnd end);
471 * Detaches one of the ends from the pipe.
472 * Detached end is a fully-functional FileHandle, it will
473 * not be affected by anything you do with the pipe afterwards.
474 * Each end of a pipe can only be detched from it once (i.e.
475 * it is not duplicated).
477 * @param p pipe to detach an end from
478 * @param end which end of the pipe to detach
479 * @return Detached end on success, NULL on failure
480 * (or if that end is not present or is closed).
482 struct GNUNET_DISK_FileHandle *
483 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
484 enum GNUNET_DISK_PipeEnd end);
487 * Close an open file.
489 * @param h file handle
490 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
493 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
497 * Get the handle to a particular pipe end
500 * @param n end to access
501 * @return handle for the respective end
503 const struct GNUNET_DISK_FileHandle *
504 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
505 enum GNUNET_DISK_PipeEnd n);
510 * Get a GNUnet file handle from a W32 handle (W32-only).
511 * Do not call on non-W32 platforms (returns NULL).
513 * @param handle native handle
514 * @return GNUnet file handle corresponding to the W32 handle
516 struct GNUNET_DISK_FileHandle *
517 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
521 * Update POSIX permissions mask of a file on disk. If both argumets
522 * are #GNUNET_NO, the file is made world-read-write-executable (777).
523 * Does nothing on W32.
525 * @param fn name of the file to update
526 * @param require_uid_match #GNUNET_YES means 700
527 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
530 GNUNET_DISK_fix_permissions (const char *fn,
531 int require_uid_match,
532 int require_gid_match);
536 * Get a handle from a native integer FD.
538 * @param fno native integer file descriptor
539 * @return file handle corresponding to the descriptor
541 struct GNUNET_DISK_FileHandle *
542 GNUNET_DISK_get_handle_from_int_fd (int fno);
546 * Get a handle from a native FD.
548 * @param fd native file descriptor
549 * @return file handle corresponding to the descriptor
551 struct GNUNET_DISK_FileHandle *
552 GNUNET_DISK_get_handle_from_native (FILE *fd);
556 * Read the contents of a binary file into a buffer.
558 * @param h handle to an open file
559 * @param result the buffer to write the result to
560 * @param len the maximum number of bytes to read
561 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
564 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
570 * Read the contents of a binary file into a buffer.
571 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
572 * when no data can be read).
574 * @param h handle to an open file
575 * @param result the buffer to write the result to
576 * @param len the maximum number of bytes to read
577 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
580 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
586 * Read the contents of a binary file into a buffer.
588 * @param fn file name
589 * @param result the buffer to write the result to
590 * @param len the maximum number of bytes to read
591 * @return number of bytes read, #GNUNET_SYSERR on failure
594 GNUNET_DISK_fn_read (const char *fn,
600 * Write a buffer to a file.
602 * @param h handle to open file
603 * @param buffer the data to write
604 * @param n number of bytes to write
605 * @return number of bytes written on success, #GNUNET_SYSERR on error
608 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
614 * Write a buffer to a file, blocking, if necessary.
616 * @param h handle to open file
617 * @param buffer the data to write
618 * @param n number of bytes to write
619 * @return number of bytes written on success, #GNUNET_SYSERR on error
622 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
628 * Write a buffer to a file. If the file is longer than
629 * the given buffer size, it will be truncated.
631 * @param fn file name
632 * @param buffer the data to write
633 * @param n number of bytes to write
634 * @param mode file permissions
635 * @return number of bytes written on success, #GNUNET_SYSERR on error
638 GNUNET_DISK_fn_write (const char *fn,
641 enum GNUNET_DISK_AccessPermissions mode);
647 * @param src file to copy
648 * @param dst destination file name
649 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
652 GNUNET_DISK_file_copy (const char *src,
657 * Scan a directory for files.
659 * @param dir_name the name of the directory
660 * @param callback the method to call for each file
661 * @param callback_cls closure for @a callback
662 * @return the number of files found, -1 on error
665 GNUNET_DISK_directory_scan (const char *dir_name,
666 GNUNET_FileNameCallback callback,
671 * Create the directory structure for storing
674 * @param filename name of a file in the directory
675 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
676 * #GNUNET_NO if directory exists but is not writeable
679 GNUNET_DISK_directory_create_for_file (const char *filename);
683 * Test if @a fil is a directory and listable. Optionally, also check if the
684 * directory is readable. Will not print an error message if the directory does
685 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
686 * with the same name).
688 * @param fil filename to test
689 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
690 * #GNUNET_NO to disable this check
691 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
692 * does not exist or `stat`ed
695 GNUNET_DISK_directory_test (const char *fil, int is_readable);
699 * Remove all files in a directory (rm -rf). Call with
702 * @param filename the file to remove
703 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
706 GNUNET_DISK_directory_remove (const char *filename);
710 * Remove the directory given under @a option in
711 * section [PATHS] in configuration under @a cfg_filename
713 * @param cfg_filename configuration file to parse
714 * @param option option with the dir name to purge
717 GNUNET_DISK_purge_cfg_dir (const char *cfg_filename,
722 * Implementation of "mkdir -p"
724 * @param dir the directory to create
725 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
728 GNUNET_DISK_directory_create (const char *dir);
732 * Lock a part of a file.
734 * @param fh file handle
735 * @param lock_start absolute position from where to lock
736 * @param lock_end absolute position until where to lock
737 * @param excl #GNUNET_YES for an exclusive lock
738 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
741 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
743 off_t lock_end, int excl);
747 * Unlock a part of a file.
749 * @param fh file handle
750 * @param unlock_start absolute position from where to unlock
751 * @param unlock_end absolute position until where to unlock
752 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
755 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
761 * @brief Removes special characters as ':' from a filename.
762 * @param fn the filename to canonicalize
765 GNUNET_DISK_filename_canonicalize (char *fn);
769 * @brief Change owner of a file
770 * @param filename file to change
771 * @param user new owner of the file
772 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
775 GNUNET_DISK_file_change_owner (const char *filename,
780 * Opaque handle for a memory-mapping operation.
782 struct GNUNET_DISK_MapHandle;
785 * Map a file into memory
786 * @param h open file handle
787 * @param m handle to the new mapping (will be set)
788 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
789 * @param len size of the mapping
790 * @return pointer to the mapped memory region, NULL on failure
793 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
794 struct GNUNET_DISK_MapHandle **m,
795 enum GNUNET_DISK_MapType access,
802 * @param h mapping handle
803 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
806 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
810 * Write file changes to disk
812 * @param h handle to an open file
813 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
816 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
819 #if 0 /* keep Emacsens' auto-indent happy */
826 /* ifndef GNUNET_DISK_LIB_H */
829 /** @} */ /* end of group */
831 /* end of gnunet_disk_lib.h */