2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 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.
22 * @file include/gnunet_disk_lib.h
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;
42 GNUNET_DISK_FILE, GNUNET_PIPE
46 * Handle used to access files (and pipes).
48 struct GNUNET_DISK_FileHandle
53 * File handle under W32.
60 enum GNUNET_FILE_Type type;
63 * Structure for overlapped reading (for pipes)
65 OVERLAPPED *oOverlapRead;
68 * Structure for overlapped writing (for pipes)
70 OVERLAPPED *oOverlapWrite;
74 * File handle on other OSes.
83 /* we need size_t, and since it can be both unsigned int
84 or unsigned long long, this IS platform dependent;
85 but "stdlib.h" should be portable 'enough' to be
86 unconditionally available... */
88 #include "gnunet_configuration_lib.h"
89 #include "gnunet_scheduler_lib.h"
94 #if 0 /* keep Emacsens' auto-indent happy */
101 * Specifies how a file should be opened.
103 enum GNUNET_DISK_OpenFlags
107 * Open the file for reading
109 GNUNET_DISK_OPEN_READ = 1,
112 * Open the file for writing
114 GNUNET_DISK_OPEN_WRITE = 2,
117 * Open the file for both reading and writing
119 GNUNET_DISK_OPEN_READWRITE = 3,
122 * Fail if file already exists
124 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
127 * Truncate file if it exists
129 GNUNET_DISK_OPEN_TRUNCATE = 8,
132 * Create file if it doesn't exist
134 GNUNET_DISK_OPEN_CREATE = 16,
139 GNUNET_DISK_OPEN_APPEND = 32
143 * Specifies what type of memory map is desired.
145 enum GNUNET_DISK_MapType
148 * Read-only memory map.
150 GNUNET_DISK_MAP_TYPE_READ = 1,
153 * Write-able memory map.
155 GNUNET_DISK_MAP_TYPE_WRITE = 2,
157 * Read-write memory map.
159 GNUNET_DISK_MAP_TYPE_READWRITE = 3
164 * File access permissions, UNIX-style.
166 enum GNUNET_DISK_AccessPermissions
169 * Nobody is allowed to do anything to the file.
171 GNUNET_DISK_PERM_NONE = 0,
176 GNUNET_DISK_PERM_USER_READ = 1,
181 GNUNET_DISK_PERM_USER_WRITE = 2,
186 GNUNET_DISK_PERM_USER_EXEC = 4,
191 GNUNET_DISK_PERM_GROUP_READ = 8,
196 GNUNET_DISK_PERM_GROUP_WRITE = 16,
201 GNUNET_DISK_PERM_GROUP_EXEC = 32,
204 * Everybody can read.
206 GNUNET_DISK_PERM_OTHER_READ = 64,
209 * Everybody can write.
211 GNUNET_DISK_PERM_OTHER_WRITE = 128,
214 * Everybody can execute.
216 GNUNET_DISK_PERM_OTHER_EXEC = 256
221 * Constants for specifying how to seek.
223 enum GNUNET_DISK_Seek
226 * Seek an absolute position (from the start of the file).
228 GNUNET_DISK_SEEK_SET,
231 * Seek a relative position (from the current offset).
233 GNUNET_DISK_SEEK_CUR,
236 * Seek an absolute position from the end of the file.
243 * Enumeration identifying the two ends of a pipe.
245 enum GNUNET_DISK_PipeEnd
248 * The reading-end of a pipe.
250 GNUNET_DISK_PIPE_END_READ = 0,
253 * The writing-end of a pipe.
255 GNUNET_DISK_PIPE_END_WRITE = 1
260 * Get the number of blocks that are left on the partition that
261 * contains the given file (for normal users).
263 * @param part a file on the partition to check
264 * @return -1 on errors, otherwise the number of free blocks
267 GNUNET_DISK_get_blocks_available (const char *part);
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 the read/write pointer in a file
294 * @param h handle of an open file
295 * @param offset position to move to
296 * @param whence specification to which position the offset parameter relates to
297 * @return the new position on success, GNUNET_SYSERR otherwise
300 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, OFF_T offset,
301 enum GNUNET_DISK_Seek whence);
305 * Get the size of the file (or directory) of the given file (in
308 * @param filename name of the file or directory
309 * @param size set to the size of the file (or,
310 * in the case of directories, the sum
311 * of all sizes of files in the directory)
312 * @param includeSymLinks should symbolic links be
314 * @param singleFileMode GNUNET_YES to only get size of one file
315 * and return GNUNET_SYSERR for directories.
316 * @return GNUNET_SYSERR on error, GNUNET_OK on success
319 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
320 int includeSymLinks, int singleFileMode);
324 * Obtain some unique identifiers for the given file
325 * that can be used to identify it in the local system.
326 * This function is used between GNUnet processes to
327 * quickly check if two files with the same absolute path
328 * are actually identical. The two processes represent
329 * the same peer but may communicate over the network
330 * (and the file may be on an NFS volume). This function
331 * may not be supported on all operating systems.
333 * @param filename name of the file
334 * @param dev set to the device ID
335 * @param ino set to the inode ID
336 * @return GNUNET_OK on success
339 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
344 * Create an (empty) temporary file on disk. If the given name is not
345 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
346 * 6 random characters will be appended to the name to create a unique
349 * @param t component to use for the name;
350 * does NOT contain "XXXXXX" or "/tmp/".
351 * @return NULL on error, otherwise name of fresh
352 * file on disk in directory for temporary files
355 GNUNET_DISK_mktemp (const char *t);
359 * Create an (empty) temporary directory on disk. If the given name is not an
360 * absolute path, the current 'TMPDIR' will be prepended. In any case, 6
361 * random characters will be appended to the name to create a unique name.
363 * @param t component to use for the name;
364 * does NOT contain "XXXXXX" or "/tmp/".
365 * @return NULL on error, otherwise name of freshly created directory
368 GNUNET_DISK_mkdtemp (const char *t);
372 * Open a file. Note that the access permissions will only be
373 * used if a new file is created and if the underlying operating
374 * system supports the given permissions.
376 * @param fn file name to be opened
377 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
378 * @param perm permissions for the newly created file, use
379 * GNUNET_DISK_PERM_NONE if a file could not be created by this
380 * call (because of flags)
381 * @return IO handle on success, NULL on error
383 struct GNUNET_DISK_FileHandle *
384 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
385 enum GNUNET_DISK_AccessPermissions perm);
389 * Get the size of an open file.
391 * @param fh open file handle
392 * @param size where to write size of the file
393 * @return GNUNET_OK on success, GNUNET_SYSERR on error
396 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
401 * Creates an interprocess channel
403 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
404 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
405 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
406 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
407 * @return handle to the new pipe, NULL on error
409 struct GNUNET_DISK_PipeHandle *
410 GNUNET_DISK_pipe (int blocking_read, int blocking_write, int inherit_read, int inherit_write);
414 * Creates a pipe object from a couple of file descriptors.
415 * Useful for wrapping existing pipe FDs.
417 * @param blocking_read creates an asynchronous pipe for reading if set to GNUNET_NO
418 * @param blocking_write creates an asynchronous pipe for writing if set to GNUNET_NO
419 * @param fd an array of two fd values. One of them may be -1 for read-only or write-only pipes
421 * @return handle to the new pipe, NULL on error
423 struct GNUNET_DISK_PipeHandle *
424 GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]);
428 * Closes an interprocess channel
430 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
433 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
437 * Closes one half of an interprocess channel
439 * @param p pipe to close end of
440 * @param end which end of the pipe to close
441 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
444 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
445 enum GNUNET_DISK_PipeEnd end);
448 * Close an open file.
450 * @param h file handle
451 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
454 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
458 * Get the handle to a particular pipe end
461 * @param n end to access
462 * @return handle for the respective end
464 const struct GNUNET_DISK_FileHandle *
465 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
466 enum GNUNET_DISK_PipeEnd n);
470 * Get a handle from a native FD.
472 * @param fd native file descriptor
473 * @return file handle corresponding to the descriptor
475 struct GNUNET_DISK_FileHandle *
476 GNUNET_DISK_get_handle_from_native (FILE *fd);
480 * Read the contents of a binary file into a buffer.
481 * @param h handle to an open file
482 * @param result the buffer to write the result to
483 * @param len the maximum number of bytes to read
484 * @return the number of bytes read on success, GNUNET_SYSERR on failure
487 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
492 * Read the contents of a binary file into a buffer.
493 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
494 * when no data can be read).
496 * @param h handle to an open file
497 * @param result the buffer to write the result to
498 * @param len the maximum number of bytes to read
499 * @return the number of bytes read on success, GNUNET_SYSERR on failure
502 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
503 void *result, size_t len);
507 * Read the contents of a binary file into a buffer.
509 * @param fn file name
510 * @param result the buffer to write the result to
511 * @param len the maximum number of bytes to read
512 * @return number of bytes read, GNUNET_SYSERR on failure
515 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
519 * Write a buffer to a file.
521 * @param h handle to open file
522 * @param buffer the data to write
523 * @param n number of bytes to write
524 * @return number of bytes written on success, GNUNET_SYSERR on error
527 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
528 const void *buffer, size_t n);
532 * Write a buffer to a file, blocking, if necessary.
533 * @param h handle to open file
534 * @param buffer the data to write
535 * @param n number of bytes to write
536 * @return number of bytes written on success, GNUNET_SYSERR on error
539 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h,
540 const void *buffer, size_t n);
543 * Write a buffer to a file. If the file is longer than
544 * the given buffer size, it will be truncated.
546 * @param fn file name
547 * @param buffer the data to write
548 * @param n number of bytes to write
549 * @param mode file permissions
550 * @return number of bytes written on success, GNUNET_SYSERR on error
553 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
554 enum GNUNET_DISK_AccessPermissions mode);
560 * @param src file to copy
561 * @param dst destination file name
562 * @return GNUNET_OK on success, GNUNET_SYSERR on error
565 GNUNET_DISK_file_copy (const char *src, const char *dst);
569 * Scan a directory for files.
571 * @param dirName the name of the directory
572 * @param callback the method to call for each file
573 * @param callback_cls closure for callback
574 * @return the number of files found, -1 on error
577 GNUNET_DISK_directory_scan (const char *dirName,
578 GNUNET_FileNameCallback callback,
583 * Opaque handle used for iterating over a directory.
585 struct GNUNET_DISK_DirectoryIterator;
589 * Function called to iterate over a directory.
592 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
593 * get called on the next entry (or finish cleanly);
594 * NULL on error (will be the last call in that case)
595 * @param filename complete filename (absolute path)
596 * @param dirname directory name (absolute path)
598 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
600 GNUNET_DISK_DirectoryIterator
602 const char *filename,
603 const char *dirname);
607 * This function must be called during the DiskIteratorCallback
608 * (exactly once) to schedule the task to process the next
609 * filename in the directory (if there is one).
611 * @param iter opaque handle for the iterator
612 * @param can set to GNUNET_YES to terminate the iteration early
613 * @return GNUNET_YES if iteration will continue,
614 * GNUNET_NO if this was the last entry (and iteration is complete),
615 * GNUNET_SYSERR if "can" was YES
618 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
623 * Scan a directory for files using the scheduler to run a task for
624 * each entry. The name of the directory must be expanded first (!).
625 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
626 * may provide a simpler API.
628 * @param prio priority to use
629 * @param dirName the name of the directory
630 * @param callback the method to call for each file
631 * @param callback_cls closure for callback
632 * @return GNUNET_YES if directory is not empty and 'callback'
633 * will be called later, GNUNET_NO otherwise, GNUNET_SYSERR on error.
636 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
638 GNUNET_DISK_DirectoryIteratorCallback
639 callback, void *callback_cls);
643 * Create the directory structure for storing
646 * @param filename name of a file in the directory
647 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
648 * GNUNET_NO if directory exists but is not writeable
651 GNUNET_DISK_directory_create_for_file (const char *filename);
655 * Test if "fil" is a directory that can be accessed.
656 * Will not print an error message if the directory
657 * does not exist. Will log errors if GNUNET_SYSERR is
660 * @param fil filename to test
661 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
662 * on any error and if exists but not directory
665 GNUNET_DISK_directory_test (const char *fil);
669 * Remove all files in a directory (rm -rf). Call with
672 * @param fileName the file to remove
673 * @return GNUNET_OK on success, GNUNET_SYSERR on error
676 GNUNET_DISK_directory_remove (const char *fileName);
680 * Implementation of "mkdir -p"
682 * @param dir the directory to create
683 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
686 GNUNET_DISK_directory_create (const char *dir);
690 * Lock a part of a file.
692 * @param fh file handle
693 * @param lockStart absolute position from where to lock
694 * @param lockEnd absolute position until where to lock
695 * @param excl GNUNET_YES for an exclusive lock
696 * @return GNUNET_OK on success, GNUNET_SYSERR on error
699 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
700 OFF_T lockEnd, int excl);
704 * Unlock a part of a file
705 * @param fh file handle
706 * @param unlockStart absolute position from where to unlock
707 * @param unlockEnd absolute position until where to unlock
708 * @return GNUNET_OK on success, GNUNET_SYSERR on error
711 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
716 * @brief Removes special characters as ':' from a filename.
717 * @param fn the filename to canonicalize
720 GNUNET_DISK_filename_canonicalize (char *fn);
724 * @brief Change owner of a file
725 * @param filename file to change
726 * @param user new owner of the file
727 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
730 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
734 * Construct full path to a file inside of the private
735 * directory used by GNUnet. Also creates the corresponding
736 * directory. If the resulting name is supposed to be
737 * a directory, end the last argument in '/' (or pass
738 * DIR_SEPARATOR_STR as the last argument before NULL).
740 * @param cfg configuration to use
741 * @param serviceName name of the service asking
742 * @param ... is NULL-terminated list of
743 * path components to append to the
744 * private directory name.
745 * @return the constructed filename
748 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
749 const char *serviceName, ...);
753 * Opaque handle for a memory-mapping operation.
755 struct GNUNET_DISK_MapHandle;
758 * Map a file into memory
759 * @param h open file handle
760 * @param m handle to the new mapping (will be set)
761 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
762 * @param len size of the mapping
763 * @return pointer to the mapped memory region, NULL on failure
766 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
767 struct GNUNET_DISK_MapHandle **m,
768 enum GNUNET_DISK_MapType access, size_t len);
773 * @param h mapping handle
774 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
777 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
780 * Write file changes to disk
781 * @param h handle to an open file
782 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
785 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
788 #if 0 /* keep Emacsens' auto-indent happy */
796 /* ifndef GNUNET_DISK_LIB_H */
798 /* end of gnunet_disk_lib.h */