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 #define OFF_T uint64_t
35 * Handle used to manage a pipe.
37 struct GNUNET_DISK_PipeHandle;
45 * Handle represents a file.
47 GNUNET_DISK_HANLDE_TYPE_FILE,
50 * Handle represents a pipe.
52 GNUNET_DISK_HANLDE_TYPE_PIPE
56 * Handle used to access files (and pipes).
58 struct GNUNET_DISK_FileHandle
63 * File handle under W32.
70 enum GNUNET_FILE_Type type;
73 * Structure for overlapped reading (for pipes)
75 OVERLAPPED *oOverlapRead;
78 * Structure for overlapped writing (for pipes)
80 OVERLAPPED *oOverlapWrite;
84 * File handle on other OSes.
92 /* we need size_t, and since it can be both unsigned int
93 or unsigned long long, this IS platform dependent;
94 but "stdlib.h" should be portable 'enough' to be
95 unconditionally available... */
97 #include "gnunet_configuration_lib.h"
98 #include "gnunet_scheduler_lib.h"
103 #if 0 /* keep Emacsens' auto-indent happy */
110 * Specifies how a file should be opened.
112 enum GNUNET_DISK_OpenFlags
116 * Open the file for reading
118 GNUNET_DISK_OPEN_READ = 1,
121 * Open the file for writing
123 GNUNET_DISK_OPEN_WRITE = 2,
126 * Open the file for both reading and writing
128 GNUNET_DISK_OPEN_READWRITE = 3,
131 * Fail if file already exists
133 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
136 * Truncate file if it exists
138 GNUNET_DISK_OPEN_TRUNCATE = 8,
141 * Create file if it doesn't exist
143 GNUNET_DISK_OPEN_CREATE = 16,
148 GNUNET_DISK_OPEN_APPEND = 32
152 * Specifies what type of memory map is desired.
154 enum GNUNET_DISK_MapType
157 * Read-only memory map.
159 GNUNET_DISK_MAP_TYPE_READ = 1,
162 * Write-able memory map.
164 GNUNET_DISK_MAP_TYPE_WRITE = 2,
167 * Read-write memory map.
169 GNUNET_DISK_MAP_TYPE_READWRITE = 3
174 * File access permissions, UNIX-style.
176 enum GNUNET_DISK_AccessPermissions
179 * Nobody is allowed to do anything to the file.
181 GNUNET_DISK_PERM_NONE = 0,
186 GNUNET_DISK_PERM_USER_READ = 1,
191 GNUNET_DISK_PERM_USER_WRITE = 2,
196 GNUNET_DISK_PERM_USER_EXEC = 4,
201 GNUNET_DISK_PERM_GROUP_READ = 8,
206 GNUNET_DISK_PERM_GROUP_WRITE = 16,
211 GNUNET_DISK_PERM_GROUP_EXEC = 32,
214 * Everybody can read.
216 GNUNET_DISK_PERM_OTHER_READ = 64,
219 * Everybody can write.
221 GNUNET_DISK_PERM_OTHER_WRITE = 128,
224 * Everybody can execute.
226 GNUNET_DISK_PERM_OTHER_EXEC = 256
231 * Constants for specifying how to seek. Do not change values or order,
232 * some of the code depends on the specific numeric values!
234 enum GNUNET_DISK_Seek
237 * Seek an absolute position (from the start of the file).
239 GNUNET_DISK_SEEK_SET = 0,
242 * Seek a relative position (from the current offset).
244 GNUNET_DISK_SEEK_CUR = 1,
247 * Seek an absolute position from the end of the file.
249 GNUNET_DISK_SEEK_END = 2
254 * Enumeration identifying the two ends of a pipe.
256 enum GNUNET_DISK_PipeEnd
259 * The reading-end of a pipe.
261 GNUNET_DISK_PIPE_END_READ = 0,
264 * The writing-end of a pipe.
266 GNUNET_DISK_PIPE_END_WRITE = 1
271 * Checks whether a handle is invalid
273 * @param h handle to check
274 * @return GNUNET_YES if invalid, GNUNET_NO if valid
277 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
281 * Check that fil corresponds to a filename
282 * (of a file that exists and that is not a directory).
284 * @param fil filename to check
285 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
286 * else (will print an error message in that case, too).
289 GNUNET_DISK_file_test (const char *fil);
293 * Move a file out of the way (create a backup) by
294 * renaming it to "orig.NUM~" where NUM is the smallest
295 * number that is not used yet.
297 * @param fil name of the file to back up
300 GNUNET_DISK_file_backup (const char *fil);
304 * Move the read/write pointer in a file
305 * @param h handle of an open file
306 * @param offset position to move to
307 * @param whence specification to which position the offset parameter relates to
308 * @return the new position on success, GNUNET_SYSERR otherwise
311 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, OFF_T offset,
312 enum GNUNET_DISK_Seek whence);
316 * Get the size of the file (or directory) of the given file (in
319 * @param filename name of the file or directory
320 * @param size set to the size of the file (or,
321 * in the case of directories, the sum
322 * of all sizes of files in the directory)
323 * @param includeSymLinks should symbolic links be
325 * @param singleFileMode GNUNET_YES to only get size of one file
326 * and return GNUNET_SYSERR for directories.
327 * @return GNUNET_SYSERR on error, GNUNET_OK on success
330 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
331 int includeSymLinks, int singleFileMode);
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, uint64_t * dev,
355 * Create an (empty) temporary file on disk. If the given name is not
356 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
357 * 6 random characters will be appended to the name to create a unique
360 * @param t component to use for the name;
361 * does NOT contain "XXXXXX" or "/tmp/".
362 * @return NULL on error, otherwise name of fresh
363 * file on disk in directory for temporary files
366 GNUNET_DISK_mktemp (const char *t);
370 * Create an (empty) temporary directory on disk. If the given name is not an
371 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
372 * random characters will be appended to the name to create a unique name.
374 * @param t component to use for the name;
375 * does NOT contain "XXXXXX" or "/tmp/".
376 * @return NULL on error, otherwise name of freshly created directory
379 GNUNET_DISK_mkdtemp (const char *t);
383 * Open a file. Note that the access permissions will only be
384 * used if a new file is created and if the underlying operating
385 * system supports the given permissions.
387 * @param fn file name to be opened
388 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
389 * @param perm permissions for the newly created file, use
390 * GNUNET_DISK_PERM_NONE if a file could not be created by this
391 * call (because of flags)
392 * @return IO handle on success, NULL on error
394 struct GNUNET_DISK_FileHandle *
395 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
396 enum GNUNET_DISK_AccessPermissions perm);
400 * Get the size of an open file.
402 * @param fh open file handle
403 * @param size where to write size of the file
404 * @return GNUNET_OK on success, GNUNET_SYSERR on error
407 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
412 * Creates an interprocess channel
414 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
415 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
416 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
417 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
418 * @return handle to the new pipe, NULL on error
420 struct GNUNET_DISK_PipeHandle *
421 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write);
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, int blocking_write, int fd[2]);
439 * Closes an interprocess channel
441 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
444 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
448 * Closes one half of an interprocess channel
450 * @param p pipe to close end of
451 * @param end which end of the pipe to close
452 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
455 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
456 enum GNUNET_DISK_PipeEnd end);
459 * Detaches one of the ends from the pipe.
460 * Detached end is a fully-functional FileHandle, it will
461 * not be affected by anything you do with the pipe afterwards.
462 * Each end of a pipe can only be detched from it once (i.e.
463 * it is not duplicated).
465 * @param p pipe to detach an end from
466 * @param end which end of the pipe to detach
467 * @return Detached end on success, NULL on failure
468 * (or if that end is not present or is closed).
470 struct GNUNET_DISK_FileHandle *
471 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
472 enum GNUNET_DISK_PipeEnd end);
475 * Close an open file.
477 * @param h file handle
478 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
481 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
485 * Get the handle to a particular pipe end
488 * @param n end to access
489 * @return handle for the respective end
491 const struct GNUNET_DISK_FileHandle *
492 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
493 enum GNUNET_DISK_PipeEnd n);
498 * Get a GNUnet file handle from a W32 handle (W32-only).
499 * Do not call on non-W32 platforms (returns NULL).
501 * @param handle native handle
502 * @return GNUnet file handle corresponding to the W32 handle
504 struct GNUNET_DISK_FileHandle *
505 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
509 * Get a handle from a native integer FD.
511 * @param fno native integer file descriptor
512 * @return file handle corresponding to the descriptor
514 struct GNUNET_DISK_FileHandle *
515 GNUNET_DISK_get_handle_from_int_fd (int fno);
518 * Get a handle from a native FD.
520 * @param fd native file descriptor
521 * @return file handle corresponding to the descriptor
523 struct GNUNET_DISK_FileHandle *
524 GNUNET_DISK_get_handle_from_native (FILE *fd);
528 * Read the contents of a binary file into a buffer.
529 * @param h handle to an open file
530 * @param result the buffer to write the result to
531 * @param len the maximum number of bytes to read
532 * @return the number of bytes read on success, GNUNET_SYSERR on failure
535 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
540 * Read the contents of a binary file into a buffer.
541 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
542 * when no data can be read).
544 * @param h handle to an open file
545 * @param result the buffer to write the result to
546 * @param len the maximum number of bytes to read
547 * @return the number of bytes read on success, GNUNET_SYSERR on failure
550 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
551 void *result, size_t len);
555 * Read the contents of a binary file into a buffer.
557 * @param fn file name
558 * @param result the buffer to write the result to
559 * @param len the maximum number of bytes to read
560 * @return number of bytes read, GNUNET_SYSERR on failure
563 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
567 * Write a buffer to a file.
569 * @param h handle to open file
570 * @param buffer the data to write
571 * @param n number of bytes to write
572 * @return number of bytes written on success, GNUNET_SYSERR on error
575 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
576 const void *buffer, size_t n);
580 * Write a buffer to a file, blocking, if necessary.
581 * @param h handle to open file
582 * @param buffer the data to write
583 * @param n number of bytes to write
584 * @return number of bytes written on success, GNUNET_SYSERR on error
587 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
593 * Write a buffer to a file. If the file is longer than
594 * the given buffer size, it will be truncated.
596 * @param fn file name
597 * @param buffer the data to write
598 * @param n number of bytes to write
599 * @param mode file permissions
600 * @return number of bytes written on success, GNUNET_SYSERR on error
603 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
604 enum GNUNET_DISK_AccessPermissions mode);
610 * @param src file to copy
611 * @param dst destination file name
612 * @return GNUNET_OK on success, GNUNET_SYSERR on error
615 GNUNET_DISK_file_copy (const char *src, const char *dst);
619 * Scan a directory for files.
621 * @param dirName the name of the directory
622 * @param callback the method to call for each file
623 * @param callback_cls closure for callback
624 * @return the number of files found, -1 on error
627 GNUNET_DISK_directory_scan (const char *dirName,
628 GNUNET_FileNameCallback callback,
633 * Opaque handle used for iterating over a directory.
635 struct GNUNET_DISK_DirectoryIterator;
639 * Function called to iterate over a directory.
642 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
643 * get called on the next entry (or finish cleanly);
644 * NULL on error (will be the last call in that case)
645 * @param filename complete filename (absolute path)
646 * @param dirname directory name (absolute path)
648 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
650 GNUNET_DISK_DirectoryIterator
652 const char *filename,
653 const char *dirname);
657 * This function must be called during the DiskIteratorCallback
658 * (exactly once) to schedule the task to process the next
659 * filename in the directory (if there is one).
661 * @param iter opaque handle for the iterator
662 * @param can set to GNUNET_YES to terminate the iteration early
663 * @return GNUNET_YES if iteration will continue,
664 * GNUNET_NO if this was the last entry (and iteration is complete),
665 * GNUNET_SYSERR if "can" was YES
668 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
673 * Scan a directory for files using the scheduler to run a task for
674 * each entry. The name of the directory must be expanded first (!).
675 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
676 * may provide a simpler API.
678 * @param prio priority to use
679 * @param dirName the name of the directory
680 * @param callback the method to call for each file
681 * @param callback_cls closure for callback
682 * @return GNUNET_YES if directory is not empty and 'callback'
683 * will be called later, GNUNET_NO otherwise, GNUNET_SYSERR on error.
686 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
688 GNUNET_DISK_DirectoryIteratorCallback
689 callback, void *callback_cls);
693 * Create the directory structure for storing
696 * @param filename name of a file in the directory
697 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
698 * GNUNET_NO if directory exists but is not writeable
701 GNUNET_DISK_directory_create_for_file (const char *filename);
705 * Test if "fil" is a directory and listable. Optionally, also check if the
706 * directory is readable. Will not print an error message if the directory does
707 * not exist. Will log errors if GNUNET_SYSERR is returned (i.e., a file exists
708 * with the same name).
710 * @param fil filename to test
711 * @param is_readable GNUNET_YES to additionally check if "fil" is readable;
712 * GNUNET_NO to disable this check
713 * @return GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it
714 * does not exist or stat'ed
717 GNUNET_DISK_directory_test (const char *fil, int is_readable);
721 * Remove all files in a directory (rm -rf). Call with
724 * @param filename the file to remove
725 * @return GNUNET_OK on success, GNUNET_SYSERR on error
728 GNUNET_DISK_directory_remove (const char *filename);
732 * Implementation of "mkdir -p"
734 * @param dir the directory to create
735 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
738 GNUNET_DISK_directory_create (const char *dir);
742 * Lock a part of a file.
744 * @param fh file handle
745 * @param lockStart absolute position from where to lock
746 * @param lockEnd absolute position until where to lock
747 * @param excl GNUNET_YES for an exclusive lock
748 * @return GNUNET_OK on success, GNUNET_SYSERR on error
751 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
752 OFF_T lockEnd, int excl);
756 * Unlock a part of a file
757 * @param fh file handle
758 * @param unlockStart absolute position from where to unlock
759 * @param unlockEnd absolute position until where to unlock
760 * @return GNUNET_OK on success, GNUNET_SYSERR on error
763 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
768 * @brief Removes special characters as ':' from a filename.
769 * @param fn the filename to canonicalize
772 GNUNET_DISK_filename_canonicalize (char *fn);
776 * @brief Change owner of a file
777 * @param filename file to change
778 * @param user new owner of the file
779 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
782 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
786 * Construct full path to a file inside of the private
787 * directory used by GNUnet. Also creates the corresponding
788 * directory. If the resulting name is supposed to be
789 * a directory, end the last argument in '/' (or pass
790 * DIR_SEPARATOR_STR as the last argument before NULL).
792 * @param cfg configuration to use
793 * @param serviceName name of the service asking
794 * @param ... is NULL-terminated list of
795 * path components to append to the
796 * private directory name.
797 * @return the constructed filename
800 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
801 const char *serviceName, ...);
805 * Opaque handle for a memory-mapping operation.
807 struct GNUNET_DISK_MapHandle;
810 * Map a file into memory
811 * @param h open file handle
812 * @param m handle to the new mapping (will be set)
813 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
814 * @param len size of the mapping
815 * @return pointer to the mapped memory region, NULL on failure
818 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
819 struct GNUNET_DISK_MapHandle **m,
820 enum GNUNET_DISK_MapType access, size_t len);
825 * @param h mapping handle
826 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
829 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
832 * Write file changes to disk
833 * @param h handle to an open file
834 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
837 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
840 #if 0 /* keep Emacsens' auto-indent happy */
848 /* ifndef GNUNET_DISK_LIB_H */
850 /* end of gnunet_disk_lib.h */