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 * @file include/gnunet_disk_lib.h
23 * @author Christian Grothoff
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, uint64_t *size,
330 int include_symbolic_links,
331 int single_file_mode);
335 * Obtain some unique identifiers for the given file
336 * that can be used to identify it in the local system.
337 * This function is used between GNUnet processes to
338 * quickly check if two files with the same absolute path
339 * are actually identical. The two processes represent
340 * the same peer but may communicate over the network
341 * (and the file may be on an NFS volume). This function
342 * may not be supported on all operating systems.
344 * @param filename name of the file
345 * @param dev set to the device ID
346 * @param ino set to the inode ID
347 * @return GNUNET_OK on success
350 GNUNET_DISK_file_get_identifiers (const char *filename,
356 * Create an (empty) temporary file on disk. If the given name is not
357 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
358 * 6 random characters will be appended to the name to create a unique
361 * @param t component to use for the name;
362 * does NOT contain "XXXXXX" or "/tmp/".
363 * @return NULL on error, otherwise name of fresh
364 * file on disk in directory for temporary files
367 GNUNET_DISK_mktemp (const char *t);
371 * Create an (empty) temporary directory on disk. If the given name is not an
372 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
373 * random characters will be appended to the name to create a unique name.
375 * @param t component to use for the name;
376 * does NOT contain "XXXXXX" or "/tmp/".
377 * @return NULL on error, otherwise name of freshly created directory
380 GNUNET_DISK_mkdtemp (const char *t);
384 * Open a file. Note that the access permissions will only be
385 * used if a new file is created and if the underlying operating
386 * system supports the given permissions.
388 * @param fn file name to be opened
389 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
390 * @param perm permissions for the newly created file, use
391 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
392 * call (because of flags)
393 * @return IO handle on success, NULL on error
395 struct GNUNET_DISK_FileHandle *
396 GNUNET_DISK_file_open (const char *fn,
397 enum GNUNET_DISK_OpenFlags flags,
398 enum GNUNET_DISK_AccessPermissions perm);
402 * Get the size of an open file.
404 * @param fh open file handle
405 * @param size where to write size of the file
406 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
409 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
414 * Creates an interprocess channel
416 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
417 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
418 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
419 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
420 * @return handle to the new pipe, NULL on error
422 struct GNUNET_DISK_PipeHandle *
423 GNUNET_DISK_pipe (int blocking_read,
430 * Creates a pipe object from a couple of file descriptors.
431 * Useful for wrapping existing pipe FDs.
433 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
434 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
435 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
437 * @return handle to the new pipe, NULL on error
439 struct GNUNET_DISK_PipeHandle *
440 GNUNET_DISK_pipe_from_fd (int blocking_read,
446 * Closes an interprocess channel
448 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
451 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
455 * Closes one half of an interprocess channel
457 * @param p pipe to close end of
458 * @param end which end of the pipe to close
459 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
462 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
463 enum GNUNET_DISK_PipeEnd end);
467 * Detaches one of the ends from the pipe.
468 * Detached end is a fully-functional FileHandle, it will
469 * not be affected by anything you do with the pipe afterwards.
470 * Each end of a pipe can only be detched from it once (i.e.
471 * it is not duplicated).
473 * @param p pipe to detach an end from
474 * @param end which end of the pipe to detach
475 * @return Detached end on success, NULL on failure
476 * (or if that end is not present or is closed).
478 struct GNUNET_DISK_FileHandle *
479 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
480 enum GNUNET_DISK_PipeEnd end);
483 * Close an open file.
485 * @param h file handle
486 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
489 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
493 * Get the handle to a particular pipe end
496 * @param n end to access
497 * @return handle for the respective end
499 const struct GNUNET_DISK_FileHandle *
500 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
501 enum GNUNET_DISK_PipeEnd n);
506 * Get a GNUnet file handle from a W32 handle (W32-only).
507 * Do not call on non-W32 platforms (returns NULL).
509 * @param handle native handle
510 * @return GNUnet file handle corresponding to the W32 handle
512 struct GNUNET_DISK_FileHandle *
513 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
517 * Update POSIX permissions mask of a file on disk. If both argumets
518 * are #GNUNET_NO, the file is made world-read-write-executable (777).
519 * Does nothing on W32.
521 * @param fn name of the file to update
522 * @param require_uid_match #GNUNET_YES means 700
523 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
526 GNUNET_DISK_fix_permissions (const char *fn,
527 int require_uid_match,
528 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, const char *dst);
653 * Scan a directory for files.
655 * @param dir_name the name of the directory
656 * @param callback the method to call for each file
657 * @param callback_cls closure for @a callback
658 * @return the number of files found, -1 on error
661 GNUNET_DISK_directory_scan (const char *dir_name,
662 GNUNET_FileNameCallback callback,
667 * Opaque handle used for iterating over a directory.
669 struct GNUNET_DISK_DirectoryIterator;
673 * Function called to iterate over a directory.
676 * @param di argument to pass to #GNUNET_DISK_directory_iterator_next to
677 * get called on the next entry (or finish cleanly);
678 * NULL on error (will be the last call in that case)
679 * @param filename complete filename (absolute path)
680 * @param dirname directory name (absolute path)
682 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
683 struct GNUNET_DISK_DirectoryIterator *di,
684 const char *filename,
685 const char *dirname);
689 * This function must be called during the DiskIteratorCallback
690 * (exactly once) to schedule the task to process the next
691 * filename in the directory (if there is one).
693 * @param iter opaque handle for the iterator
694 * @param can set to #GNUNET_YES to terminate the iteration early
695 * @return #GNUNET_YES if iteration will continue,
696 * #GNUNET_NO if this was the last entry (and iteration is complete),
697 * #GNUNET_SYSERR if @a can was #GNUNET_YES
700 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
705 * Scan a directory for files using the scheduler to run a task for
706 * each entry. The name of the directory must be expanded first (!).
707 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
708 * may provide a simpler API.
710 * @param prio priority to use
711 * @param dir_name the name of the directory
712 * @param callback the method to call for each file
713 * @param callback_cls closure for @a callback
714 * @return #GNUNET_YES if directory is not empty and @a callback
715 * will be called later, #GNUNET_NO otherwise, #GNUNET_SYSERR on error.
718 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
719 const char *dir_name,
720 GNUNET_DISK_DirectoryIteratorCallback
721 callback, void *callback_cls);
725 * Create the directory structure for storing
728 * @param filename name of a file in the directory
729 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
730 * #GNUNET_NO if directory exists but is not writeable
733 GNUNET_DISK_directory_create_for_file (const char *filename);
737 * Test if @a fil is a directory and listable. Optionally, also check if the
738 * directory is readable. Will not print an error message if the directory does
739 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
740 * with the same name).
742 * @param fil filename to test
743 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
744 * #GNUNET_NO to disable this check
745 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
746 * does not exist or `stat`ed
749 GNUNET_DISK_directory_test (const char *fil, int is_readable);
753 * Remove all files in a directory (rm -rf). Call with
756 * @param filename the file to remove
757 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
760 GNUNET_DISK_directory_remove (const char *filename);
764 * Implementation of "mkdir -p"
766 * @param dir the directory to create
767 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
770 GNUNET_DISK_directory_create (const char *dir);
774 * Lock a part of a file.
776 * @param fh file handle
777 * @param lock_start absolute position from where to lock
778 * @param lock_end absolute position until where to lock
779 * @param excl #GNUNET_YES for an exclusive lock
780 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
783 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
785 off_t lock_end, int excl);
789 * Unlock a part of a file.
791 * @param fh file handle
792 * @param unlock_start absolute position from where to unlock
793 * @param unlock_end absolute position until where to unlock
794 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
797 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
803 * @brief Removes special characters as ':' from a filename.
804 * @param fn the filename to canonicalize
807 GNUNET_DISK_filename_canonicalize (char *fn);
811 * @brief Change owner of a file
812 * @param filename file to change
813 * @param user new owner of the file
814 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
817 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
821 * Opaque handle for a memory-mapping operation.
823 struct GNUNET_DISK_MapHandle;
826 * Map a file into memory
827 * @param h open file handle
828 * @param m handle to the new mapping (will be set)
829 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
830 * @param len size of the mapping
831 * @return pointer to the mapped memory region, NULL on failure
834 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
835 struct GNUNET_DISK_MapHandle **m,
836 enum GNUNET_DISK_MapType access, size_t len);
841 * @param h mapping handle
842 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
845 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
848 * Write file changes to disk
849 * @param h handle to an open file
850 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
853 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
856 #if 0 /* keep Emacsens' auto-indent happy */
864 /* ifndef GNUNET_DISK_LIB_H */
866 /* end of gnunet_disk_lib.h */