2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 a file.
41 GNUNET_DISK_HANLDE_TYPE_FILE,
44 * Handle represents a pipe.
46 GNUNET_DISK_HANLDE_TYPE_PIPE
50 * Handle used to access files (and pipes).
52 struct GNUNET_DISK_FileHandle
57 * File handle under W32.
64 enum GNUNET_FILE_Type type;
67 * Structure for overlapped reading (for pipes)
69 OVERLAPPED *oOverlapRead;
72 * Structure for overlapped writing (for pipes)
74 OVERLAPPED *oOverlapWrite;
78 * File handle on other OSes.
86 /* we need size_t, and since it can be both unsigned int
87 or unsigned long long, this IS platform dependent;
88 but "stdlib.h" should be portable 'enough' to be
89 unconditionally available... */
91 #include "gnunet_configuration_lib.h"
92 #include "gnunet_scheduler_lib.h"
97 #if 0 /* keep Emacsens' auto-indent happy */
104 * Specifies how a file should be opened.
106 enum GNUNET_DISK_OpenFlags
110 * Open the file for reading
112 GNUNET_DISK_OPEN_READ = 1,
115 * Open the file for writing
117 GNUNET_DISK_OPEN_WRITE = 2,
120 * Open the file for both reading and writing
122 GNUNET_DISK_OPEN_READWRITE = 3,
125 * Fail if file already exists
127 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
130 * Truncate file if it exists
132 GNUNET_DISK_OPEN_TRUNCATE = 8,
135 * Create file if it doesn't exist
137 GNUNET_DISK_OPEN_CREATE = 16,
142 GNUNET_DISK_OPEN_APPEND = 32
146 * Specifies what type of memory map is desired.
148 enum GNUNET_DISK_MapType
151 * Read-only memory map.
153 GNUNET_DISK_MAP_TYPE_READ = 1,
156 * Write-able memory map.
158 GNUNET_DISK_MAP_TYPE_WRITE = 2,
161 * Read-write memory map.
163 GNUNET_DISK_MAP_TYPE_READWRITE = 3
168 * File access permissions, UNIX-style.
170 enum GNUNET_DISK_AccessPermissions
173 * Nobody is allowed to do anything to the file.
175 GNUNET_DISK_PERM_NONE = 0,
180 GNUNET_DISK_PERM_USER_READ = 1,
185 GNUNET_DISK_PERM_USER_WRITE = 2,
190 GNUNET_DISK_PERM_USER_EXEC = 4,
195 GNUNET_DISK_PERM_GROUP_READ = 8,
200 GNUNET_DISK_PERM_GROUP_WRITE = 16,
205 GNUNET_DISK_PERM_GROUP_EXEC = 32,
208 * Everybody can read.
210 GNUNET_DISK_PERM_OTHER_READ = 64,
213 * Everybody can write.
215 GNUNET_DISK_PERM_OTHER_WRITE = 128,
218 * Everybody can execute.
220 GNUNET_DISK_PERM_OTHER_EXEC = 256
225 * Constants for specifying how to seek. Do not change values or order,
226 * some of the code depends on the specific numeric values!
228 enum GNUNET_DISK_Seek
231 * Seek an absolute position (from the start of the file).
233 GNUNET_DISK_SEEK_SET = 0,
236 * Seek a relative position (from the current offset).
238 GNUNET_DISK_SEEK_CUR = 1,
241 * Seek an absolute position from the end of the file.
243 GNUNET_DISK_SEEK_END = 2
248 * Enumeration identifying the two ends of a pipe.
250 enum GNUNET_DISK_PipeEnd
253 * The reading-end of a pipe.
255 GNUNET_DISK_PIPE_END_READ = 0,
258 * The writing-end of a pipe.
260 GNUNET_DISK_PIPE_END_WRITE = 1
265 * Checks whether a handle is invalid
267 * @param h handle to check
268 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
271 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
275 * Check that fil corresponds to a filename
276 * (of a file that exists and that is not a directory).
278 * @param fil filename to check
279 * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something
280 * else (will print an error message in that case, too).
283 GNUNET_DISK_file_test (const char *fil);
287 * Move a file out of the way (create a backup) by
288 * renaming it to "orig.NUM~" where NUM is the smallest
289 * number that is not used yet.
291 * @param fil name of the file to back up
294 GNUNET_DISK_file_backup (const char *fil);
298 * Move the read/write pointer in a file
299 * @param h handle of an open file
300 * @param offset position to move to
301 * @param whence specification to which position the offset parameter relates to
302 * @return the new position on success, GNUNET_SYSERR otherwise
305 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
306 enum GNUNET_DISK_Seek whence);
310 * Get the size of the file (or directory) of the given file (in
313 * @param filename name of the file or directory
314 * @param size set to the size of the file (or,
315 * in the case of directories, the sum
316 * of all sizes of files in the directory)
317 * @param include_symbolic_links should symbolic links be
319 * @param single_file_mode #GNUNET_YES to only get size of one file
320 * and return #GNUNET_SYSERR for directories.
321 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
324 GNUNET_DISK_file_size (const char *filename, uint64_t *size,
325 int include_symbolic_links,
326 int single_file_mode);
330 * Obtain some unique identifiers for the given file
331 * that can be used to identify it in the local system.
332 * This function is used between GNUnet processes to
333 * quickly check if two files with the same absolute path
334 * are actually identical. The two processes represent
335 * the same peer but may communicate over the network
336 * (and the file may be on an NFS volume). This function
337 * may not be supported on all operating systems.
339 * @param filename name of the file
340 * @param dev set to the device ID
341 * @param ino set to the inode ID
342 * @return GNUNET_OK on success
345 GNUNET_DISK_file_get_identifiers (const char *filename,
351 * Create an (empty) temporary file on disk. If the given name is not
352 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
353 * 6 random characters will be appended to the name to create a unique
356 * @param t component to use for the name;
357 * does NOT contain "XXXXXX" or "/tmp/".
358 * @return NULL on error, otherwise name of fresh
359 * file on disk in directory for temporary files
362 GNUNET_DISK_mktemp (const char *t);
366 * Create an (empty) temporary directory on disk. If the given name is not an
367 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
368 * random characters will be appended to the name to create a unique name.
370 * @param t component to use for the name;
371 * does NOT contain "XXXXXX" or "/tmp/".
372 * @return NULL on error, otherwise name of freshly created directory
375 GNUNET_DISK_mkdtemp (const char *t);
379 * Open a file. Note that the access permissions will only be
380 * used if a new file is created and if the underlying operating
381 * system supports the given permissions.
383 * @param fn file name to be opened
384 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
385 * @param perm permissions for the newly created file, use
386 * #GNUNET_DISK_PERM_NONE if a file could not be created by this
387 * call (because of flags)
388 * @return IO handle on success, NULL on error
390 struct GNUNET_DISK_FileHandle *
391 GNUNET_DISK_file_open (const char *fn,
392 enum GNUNET_DISK_OpenFlags flags,
393 enum GNUNET_DISK_AccessPermissions perm);
397 * Get the size of an open file.
399 * @param fh open file handle
400 * @param size where to write size of the file
401 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
404 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
409 * Creates an interprocess channel
411 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
412 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
413 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
414 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
415 * @return handle to the new pipe, NULL on error
417 struct GNUNET_DISK_PipeHandle *
418 GNUNET_DISK_pipe (int blocking_read,
425 * Creates a pipe object from a couple of file descriptors.
426 * Useful for wrapping existing pipe FDs.
428 * @param blocking_read creates an asynchronous pipe for reading if set to #GNUNET_NO
429 * @param blocking_write creates an asynchronous pipe for writing if set to #GNUNET_NO
430 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
432 * @return handle to the new pipe, NULL on error
434 struct GNUNET_DISK_PipeHandle *
435 GNUNET_DISK_pipe_from_fd (int blocking_read,
441 * Closes an interprocess channel
443 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
446 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
450 * Closes one half of an interprocess channel
452 * @param p pipe to close end of
453 * @param end which end of the pipe to close
454 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
457 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
458 enum GNUNET_DISK_PipeEnd end);
462 * Detaches one of the ends from the pipe.
463 * Detached end is a fully-functional FileHandle, it will
464 * not be affected by anything you do with the pipe afterwards.
465 * Each end of a pipe can only be detched from it once (i.e.
466 * it is not duplicated).
468 * @param p pipe to detach an end from
469 * @param end which end of the pipe to detach
470 * @return Detached end on success, NULL on failure
471 * (or if that end is not present or is closed).
473 struct GNUNET_DISK_FileHandle *
474 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
475 enum GNUNET_DISK_PipeEnd end);
478 * Close an open file.
480 * @param h file handle
481 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
484 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
488 * Get the handle to a particular pipe end
491 * @param n end to access
492 * @return handle for the respective end
494 const struct GNUNET_DISK_FileHandle *
495 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
496 enum GNUNET_DISK_PipeEnd n);
501 * Get a GNUnet file handle from a W32 handle (W32-only).
502 * Do not call on non-W32 platforms (returns NULL).
504 * @param handle native handle
505 * @return GNUnet file handle corresponding to the W32 handle
507 struct GNUNET_DISK_FileHandle *
508 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
512 * Update POSIX permissions mask of a file on disk. If both argumets
513 * are #GNUNET_NO, the file is made world-read-write-executable (777).
515 * @param fn name of the file to update
516 * @param require_uid_match #GNUNET_YES means 700
517 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
520 GNUNET_DISK_fix_permissions (const char *fn,
521 int require_uid_match,
522 int require_gid_match);
528 * Get a handle from a native integer FD.
530 * @param fno native integer file descriptor
531 * @return file handle corresponding to the descriptor
533 struct GNUNET_DISK_FileHandle *
534 GNUNET_DISK_get_handle_from_int_fd (int fno);
538 * Get a handle from a native FD.
540 * @param fd native file descriptor
541 * @return file handle corresponding to the descriptor
543 struct GNUNET_DISK_FileHandle *
544 GNUNET_DISK_get_handle_from_native (FILE *fd);
548 * Read the contents of a binary file into a buffer.
550 * @param h handle to an open file
551 * @param result the buffer to write the result to
552 * @param len the maximum number of bytes to read
553 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
556 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
562 * Read the contents of a binary file into a buffer.
563 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
564 * when no data can be read).
566 * @param h handle to an open file
567 * @param result the buffer to write the result to
568 * @param len the maximum number of bytes to read
569 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
572 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
578 * Read the contents of a binary file into a buffer.
580 * @param fn file name
581 * @param result the buffer to write the result to
582 * @param len the maximum number of bytes to read
583 * @return number of bytes read, #GNUNET_SYSERR on failure
586 GNUNET_DISK_fn_read (const char *fn,
592 * Write a buffer to a file.
594 * @param h handle to open file
595 * @param buffer the data to write
596 * @param n number of bytes to write
597 * @return number of bytes written on success, #GNUNET_SYSERR on error
600 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
606 * Write a buffer to a file, blocking, if necessary.
608 * @param h handle to open file
609 * @param buffer the data to write
610 * @param n number of bytes to write
611 * @return number of bytes written on success, #GNUNET_SYSERR on error
614 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
620 * Write a buffer to a file. If the file is longer than
621 * the given buffer size, it will be truncated.
623 * @param fn file name
624 * @param buffer the data to write
625 * @param n number of bytes to write
626 * @param mode file permissions
627 * @return number of bytes written on success, #GNUNET_SYSERR on error
630 GNUNET_DISK_fn_write (const char *fn,
633 enum GNUNET_DISK_AccessPermissions mode);
639 * @param src file to copy
640 * @param dst destination file name
641 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
644 GNUNET_DISK_file_copy (const char *src, const char *dst);
648 * Scan a directory for files.
650 * @param dir_name the name of the directory
651 * @param callback the method to call for each file
652 * @param callback_cls closure for @a callback
653 * @return the number of files found, -1 on error
656 GNUNET_DISK_directory_scan (const char *dir_name,
657 GNUNET_FileNameCallback callback,
662 * Opaque handle used for iterating over a directory.
664 struct GNUNET_DISK_DirectoryIterator;
668 * Function called to iterate over a directory.
671 * @param di argument to pass to #GNUNET_DISK_directory_iterator_next to
672 * get called on the next entry (or finish cleanly);
673 * NULL on error (will be the last call in that case)
674 * @param filename complete filename (absolute path)
675 * @param dirname directory name (absolute path)
677 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
678 struct GNUNET_DISK_DirectoryIterator *di,
679 const char *filename,
680 const char *dirname);
684 * This function must be called during the DiskIteratorCallback
685 * (exactly once) to schedule the task to process the next
686 * filename in the directory (if there is one).
688 * @param iter opaque handle for the iterator
689 * @param can set to #GNUNET_YES to terminate the iteration early
690 * @return #GNUNET_YES if iteration will continue,
691 * #GNUNET_NO if this was the last entry (and iteration is complete),
692 * #GNUNET_SYSERR if @a can was #GNUNET_YES
695 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
700 * Scan a directory for files using the scheduler to run a task for
701 * each entry. The name of the directory must be expanded first (!).
702 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
703 * may provide a simpler API.
705 * @param prio priority to use
706 * @param dir_name the name of the directory
707 * @param callback the method to call for each file
708 * @param callback_cls closure for @a callback
709 * @return #GNUNET_YES if directory is not empty and @a callback
710 * will be called later, #GNUNET_NO otherwise, #GNUNET_SYSERR on error.
713 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
714 const char *dir_name,
715 GNUNET_DISK_DirectoryIteratorCallback
716 callback, void *callback_cls);
720 * Create the directory structure for storing
723 * @param filename name of a file in the directory
724 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
725 * #GNUNET_NO if directory exists but is not writeable
728 GNUNET_DISK_directory_create_for_file (const char *filename);
732 * Test if @a fil is a directory and listable. Optionally, also check if the
733 * directory is readable. Will not print an error message if the directory does
734 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
735 * with the same name).
737 * @param fil filename to test
738 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
739 * #GNUNET_NO to disable this check
740 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
741 * does not exist or `stat`ed
744 GNUNET_DISK_directory_test (const char *fil, int is_readable);
748 * Remove all files in a directory (rm -rf). Call with
751 * @param filename the file to remove
752 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
755 GNUNET_DISK_directory_remove (const char *filename);
759 * Implementation of "mkdir -p"
761 * @param dir the directory to create
762 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
765 GNUNET_DISK_directory_create (const char *dir);
769 * Lock a part of a file.
771 * @param fh file handle
772 * @param lock_start absolute position from where to lock
773 * @param lock_end absolute position until where to lock
774 * @param excl #GNUNET_YES for an exclusive lock
775 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
778 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
780 off_t lock_end, int excl);
784 * Unlock a part of a file.
786 * @param fh file handle
787 * @param unlock_start absolute position from where to unlock
788 * @param unlock_end absolute position until where to unlock
789 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
792 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
798 * @brief Removes special characters as ':' from a filename.
799 * @param fn the filename to canonicalize
802 GNUNET_DISK_filename_canonicalize (char *fn);
806 * @brief Change owner of a file
807 * @param filename file to change
808 * @param user new owner of the file
809 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
812 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
816 * Opaque handle for a memory-mapping operation.
818 struct GNUNET_DISK_MapHandle;
821 * Map a file into memory
822 * @param h open file handle
823 * @param m handle to the new mapping (will be set)
824 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
825 * @param len size of the mapping
826 * @return pointer to the mapped memory region, NULL on failure
829 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
830 struct GNUNET_DISK_MapHandle **m,
831 enum GNUNET_DISK_MapType access, size_t len);
836 * @param h mapping handle
837 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
840 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
843 * Write file changes to disk
844 * @param h handle to an open file
845 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
848 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
851 #if 0 /* keep Emacsens' auto-indent happy */
859 /* ifndef GNUNET_DISK_LIB_H */
861 /* end of gnunet_disk_lib.h */