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 2, 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 * Get the number of blocks that are left on the partition that
272 * contains the given file (for normal users).
274 * @param part a file on the partition to check
275 * @return -1 on errors, otherwise the number of free blocks
278 GNUNET_DISK_get_blocks_available (const char *part);
282 * Checks whether a handle is invalid
284 * @param h handle to check
285 * @return GNUNET_YES if invalid, GNUNET_NO if valid
288 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
292 * Check that fil corresponds to a filename
293 * (of a file that exists and that is not a directory).
295 * @param fil filename to check
296 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
297 * else (will print an error message in that case, too).
300 GNUNET_DISK_file_test (const char *fil);
304 * Move a file out of the way (create a backup) by
305 * renaming it to "orig.NUM~" where NUM is the smallest
306 * number that is not used yet.
308 * @param fil name of the file to back up
311 GNUNET_DISK_file_backup (const char *fil);
315 * Move the read/write pointer in a file
316 * @param h handle of an open file
317 * @param offset position to move to
318 * @param whence specification to which position the offset parameter relates to
319 * @return the new position on success, GNUNET_SYSERR otherwise
322 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, OFF_T offset,
323 enum GNUNET_DISK_Seek whence);
327 * Get the size of the file (or directory) of the given file (in
330 * @param filename name of the file or directory
331 * @param size set to the size of the file (or,
332 * in the case of directories, the sum
333 * of all sizes of files in the directory)
334 * @param includeSymLinks should symbolic links be
336 * @param singleFileMode GNUNET_YES to only get size of one file
337 * and return GNUNET_SYSERR for directories.
338 * @return GNUNET_SYSERR on error, GNUNET_OK on success
341 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
342 int includeSymLinks, int singleFileMode);
346 * Obtain some unique identifiers for the given file
347 * that can be used to identify it in the local system.
348 * This function is used between GNUnet processes to
349 * quickly check if two files with the same absolute path
350 * are actually identical. The two processes represent
351 * the same peer but may communicate over the network
352 * (and the file may be on an NFS volume). This function
353 * may not be supported on all operating systems.
355 * @param filename name of the file
356 * @param dev set to the device ID
357 * @param ino set to the inode ID
358 * @return GNUNET_OK on success
361 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
366 * Create an (empty) temporary file on disk. If the given name is not
367 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
368 * 6 random characters will be appended to the name to create a unique
371 * @param t component to use for the name;
372 * does NOT contain "XXXXXX" or "/tmp/".
373 * @return NULL on error, otherwise name of fresh
374 * file on disk in directory for temporary files
377 GNUNET_DISK_mktemp (const char *t);
381 * Create an (empty) temporary directory on disk. If the given name is not an
382 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
383 * random characters will be appended to the name to create a unique name.
385 * @param t component to use for the name;
386 * does NOT contain "XXXXXX" or "/tmp/".
387 * @return NULL on error, otherwise name of freshly created directory
390 GNUNET_DISK_mkdtemp (const char *t);
394 * Open a file. Note that the access permissions will only be
395 * used if a new file is created and if the underlying operating
396 * system supports the given permissions.
398 * @param fn file name to be opened
399 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
400 * @param perm permissions for the newly created file, use
401 * GNUNET_DISK_PERM_NONE if a file could not be created by this
402 * call (because of flags)
403 * @return IO handle on success, NULL on error
405 struct GNUNET_DISK_FileHandle *
406 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
407 enum GNUNET_DISK_AccessPermissions perm);
411 * Get the size of an open file.
413 * @param fh open file handle
414 * @param size where to write size of the file
415 * @return GNUNET_OK on success, GNUNET_SYSERR on error
418 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
423 * Creates an interprocess channel
425 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
426 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
427 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
428 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
429 * @return handle to the new pipe, NULL on error
431 struct GNUNET_DISK_PipeHandle *
432 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write);
436 * Creates a pipe object from a couple of file descriptors.
437 * Useful for wrapping existing pipe FDs.
439 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
440 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
441 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
443 * @return handle to the new pipe, NULL on error
445 struct GNUNET_DISK_PipeHandle *
446 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]);
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);
470 * Detaches one of the ends from the pipe.
471 * Detached end is a fully-functional FileHandle, it will
472 * not be affected by anything you do with the pipe afterwards.
473 * Each end of a pipe can only be detched from it once (i.e.
474 * it is not duplicated).
476 * @param p pipe to detach an end from
477 * @param end which end of the pipe to detach
478 * @return Detached end on success, NULL on failure
479 * (or if that end is not present or is closed).
481 struct GNUNET_DISK_FileHandle *
482 GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
483 enum GNUNET_DISK_PipeEnd end);
486 * Close an open file.
488 * @param h file handle
489 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
492 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
496 * Get the handle to a particular pipe end
499 * @param n end to access
500 * @return handle for the respective end
502 const struct GNUNET_DISK_FileHandle *
503 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
504 enum GNUNET_DISK_PipeEnd n);
509 * Get a GNUnet file handle from a W32 handle (W32-only).
510 * Do not call on non-W32 platforms (returns NULL).
512 * @param handle native handle
513 * @return GNUnet file handle corresponding to the W32 handle
515 struct GNUNET_DISK_FileHandle *
516 GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
520 * Get a handle from a native integer FD.
522 * @param fno native integer file descriptor
523 * @return file handle corresponding to the descriptor
525 struct GNUNET_DISK_FileHandle *
526 GNUNET_DISK_get_handle_from_int_fd (int fno);
529 * Get a handle from a native FD.
531 * @param fd native file descriptor
532 * @return file handle corresponding to the descriptor
534 struct GNUNET_DISK_FileHandle *
535 GNUNET_DISK_get_handle_from_native (FILE *fd);
539 * Read the contents of a binary file into a buffer.
540 * @param h handle to an open file
541 * @param result the buffer to write the result to
542 * @param len the maximum number of bytes to read
543 * @return the number of bytes read on success, GNUNET_SYSERR on failure
546 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
551 * Read the contents of a binary file into a buffer.
552 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
553 * when no data can be read).
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_non_blocking (const struct GNUNET_DISK_FileHandle * h,
562 void *result, size_t len);
566 * Read the contents of a binary file into a buffer.
568 * @param fn file name
569 * @param result the buffer to write the result to
570 * @param len the maximum number of bytes to read
571 * @return number of bytes read, GNUNET_SYSERR on failure
574 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
578 * Write a buffer to a file.
580 * @param h handle to open file
581 * @param buffer the data to write
582 * @param n number of bytes to write
583 * @return number of bytes written on success, GNUNET_SYSERR on error
586 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
587 const void *buffer, size_t n);
591 * Write a buffer to a file, blocking, if necessary.
592 * @param h handle to open file
593 * @param buffer the data to write
594 * @param n number of bytes to write
595 * @return number of bytes written on success, GNUNET_SYSERR on error
598 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
599 const void *buffer, size_t n);
602 * Write a buffer to a file. If the file is longer than
603 * the given buffer size, it will be truncated.
605 * @param fn file name
606 * @param buffer the data to write
607 * @param n number of bytes to write
608 * @param mode file permissions
609 * @return number of bytes written on success, GNUNET_SYSERR on error
612 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
613 enum GNUNET_DISK_AccessPermissions mode);
619 * @param src file to copy
620 * @param dst destination file name
621 * @return GNUNET_OK on success, GNUNET_SYSERR on error
624 GNUNET_DISK_file_copy (const char *src, const char *dst);
628 * Scan a directory for files.
630 * @param dirName the name of the directory
631 * @param callback the method to call for each file
632 * @param callback_cls closure for callback
633 * @return the number of files found, -1 on error
636 GNUNET_DISK_directory_scan (const char *dirName,
637 GNUNET_FileNameCallback callback,
642 * Opaque handle used for iterating over a directory.
644 struct GNUNET_DISK_DirectoryIterator;
648 * Function called to iterate over a directory.
651 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
652 * get called on the next entry (or finish cleanly);
653 * NULL on error (will be the last call in that case)
654 * @param filename complete filename (absolute path)
655 * @param dirname directory name (absolute path)
657 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
659 GNUNET_DISK_DirectoryIterator
661 const char *filename,
662 const char *dirname);
666 * This function must be called during the DiskIteratorCallback
667 * (exactly once) to schedule the task to process the next
668 * filename in the directory (if there is one).
670 * @param iter opaque handle for the iterator
671 * @param can set to GNUNET_YES to terminate the iteration early
672 * @return GNUNET_YES if iteration will continue,
673 * GNUNET_NO if this was the last entry (and iteration is complete),
674 * GNUNET_SYSERR if "can" was YES
677 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
682 * Scan a directory for files using the scheduler to run a task for
683 * each entry. The name of the directory must be expanded first (!).
684 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
685 * may provide a simpler API.
687 * @param prio priority to use
688 * @param dirName the name of the directory
689 * @param callback the method to call for each file
690 * @param callback_cls closure for callback
691 * @return GNUNET_YES if directory is not empty and 'callback'
692 * will be called later, GNUNET_NO otherwise, GNUNET_SYSERR on error.
695 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
697 GNUNET_DISK_DirectoryIteratorCallback
698 callback, void *callback_cls);
702 * Create the directory structure for storing
705 * @param filename name of a file in the directory
706 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
707 * GNUNET_NO if directory exists but is not writeable
710 GNUNET_DISK_directory_create_for_file (const char *filename);
714 * Test if "fil" is a directory and listable. Optionally, also check if the
715 * directory is readable. Will not print an error message if the directory does
716 * not exist. Will log errors if GNUNET_SYSERR is returned (i.e., a file exists
717 * with the same name).
719 * @param fil filename to test
720 * @param is_readable GNUNET_YES to additionally check if "fil" is readable;
721 * GNUNET_NO to disable this check
722 * @return GNUNET_YES if yes, GNUNET_NO if not; GNUNET_SYSERR if it
723 * does not exist or stat'ed
726 GNUNET_DISK_directory_test (const char *fil, int is_readable);
730 * Remove all files in a directory (rm -rf). Call with
733 * @param filename the file to remove
734 * @return GNUNET_OK on success, GNUNET_SYSERR on error
737 GNUNET_DISK_directory_remove (const char *filename);
741 * Implementation of "mkdir -p"
743 * @param dir the directory to create
744 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
747 GNUNET_DISK_directory_create (const char *dir);
751 * Lock a part of a file.
753 * @param fh file handle
754 * @param lockStart absolute position from where to lock
755 * @param lockEnd absolute position until where to lock
756 * @param excl GNUNET_YES for an exclusive lock
757 * @return GNUNET_OK on success, GNUNET_SYSERR on error
760 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
761 OFF_T lockEnd, int excl);
765 * Unlock a part of a file
766 * @param fh file handle
767 * @param unlockStart absolute position from where to unlock
768 * @param unlockEnd absolute position until where to unlock
769 * @return GNUNET_OK on success, GNUNET_SYSERR on error
772 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
777 * @brief Removes special characters as ':' from a filename.
778 * @param fn the filename to canonicalize
781 GNUNET_DISK_filename_canonicalize (char *fn);
785 * @brief Change owner of a file
786 * @param filename file to change
787 * @param user new owner of the file
788 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
791 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
795 * Construct full path to a file inside of the private
796 * directory used by GNUnet. Also creates the corresponding
797 * directory. If the resulting name is supposed to be
798 * a directory, end the last argument in '/' (or pass
799 * DIR_SEPARATOR_STR as the last argument before NULL).
801 * @param cfg configuration to use
802 * @param serviceName name of the service asking
803 * @param ... is NULL-terminated list of
804 * path components to append to the
805 * private directory name.
806 * @return the constructed filename
809 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
810 const char *serviceName, ...);
814 * Opaque handle for a memory-mapping operation.
816 struct GNUNET_DISK_MapHandle;
819 * Map a file into memory
820 * @param h open file handle
821 * @param m handle to the new mapping (will be set)
822 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
823 * @param len size of the mapping
824 * @return pointer to the mapped memory region, NULL on failure
827 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
828 struct GNUNET_DISK_MapHandle **m,
829 enum GNUNET_DISK_MapType access, size_t len);
834 * @param h mapping handle
835 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
838 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
841 * Write file changes to disk
842 * @param h handle to an open file
843 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
846 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
849 #if 0 /* keep Emacsens' auto-indent happy */
857 /* ifndef GNUNET_DISK_LIB_H */
859 /* end of gnunet_disk_lib.h */