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 * Opaque handle used to access files.
31 struct GNUNET_DISK_FileHandle;
34 * Handle used to manage a pipe.
36 struct GNUNET_DISK_PipeHandle;
41 GNUNET_DISK_FILE, GNUNET_PIPE
45 * Handle used to access files (and pipes).
47 struct GNUNET_DISK_FileHandle
52 * File handle under W32.
59 enum GNUNET_FILE_Type type;
62 * Structure for overlapped reading (for pipes)
64 OVERLAPPED *oOverlapRead;
67 * Structure for overlapped writing (for pipes)
69 OVERLAPPED *oOverlapWrite;
73 * File handle on other OSes.
82 /* we need size_t, and since it can be both unsigned int
83 or unsigned long long, this IS platform dependent;
84 but "stdlib.h" should be portable 'enough' to be
85 unconditionally available... */
87 #include "gnunet_configuration_lib.h"
88 #include "gnunet_scheduler_lib.h"
93 #if 0 /* keep Emacsens' auto-indent happy */
100 * Specifies how a file should be opened.
102 enum GNUNET_DISK_OpenFlags
106 * Open the file for reading
108 GNUNET_DISK_OPEN_READ = 1,
111 * Open the file for writing
113 GNUNET_DISK_OPEN_WRITE = 2,
116 * Open the file for both reading and writing
118 GNUNET_DISK_OPEN_READWRITE = 3,
121 * Fail if file already exists
123 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
126 * Truncate file if it exists
128 GNUNET_DISK_OPEN_TRUNCATE = 8,
131 * Create file if it doesn't exist
133 GNUNET_DISK_OPEN_CREATE = 16,
138 GNUNET_DISK_OPEN_APPEND = 32
142 * Specifies what type of memory map is desired.
144 enum GNUNET_DISK_MapType
147 * Read-only memory map.
149 GNUNET_DISK_MAP_TYPE_READ = 1,
152 * Write-able memory map.
154 GNUNET_DISK_MAP_TYPE_WRITE = 2,
156 * Read-write memory map.
158 GNUNET_DISK_MAP_TYPE_READWRITE = 3
163 * File access permissions, UNIX-style.
165 enum GNUNET_DISK_AccessPermissions
168 * Nobody is allowed to do anything to the file.
170 GNUNET_DISK_PERM_NONE = 0,
175 GNUNET_DISK_PERM_USER_READ = 1,
180 GNUNET_DISK_PERM_USER_WRITE = 2,
185 GNUNET_DISK_PERM_USER_EXEC = 4,
190 GNUNET_DISK_PERM_GROUP_READ = 8,
195 GNUNET_DISK_PERM_GROUP_WRITE = 16,
200 GNUNET_DISK_PERM_GROUP_EXEC = 32,
203 * Everybody can read.
205 GNUNET_DISK_PERM_OTHER_READ = 64,
208 * Everybody can write.
210 GNUNET_DISK_PERM_OTHER_WRITE = 128,
213 * Everybody can execute.
215 GNUNET_DISK_PERM_OTHER_EXEC = 256
220 * Constants for specifying how to seek.
222 enum GNUNET_DISK_Seek
225 * Seek an absolute position (from the start of the file).
227 GNUNET_DISK_SEEK_SET,
230 * Seek a relative position (from the current offset).
232 GNUNET_DISK_SEEK_CUR,
235 * Seek an absolute position from the end of the file.
242 * Enumeration identifying the two ends of a pipe.
244 enum GNUNET_DISK_PipeEnd
247 * The reading-end of a pipe.
249 GNUNET_DISK_PIPE_END_READ = 0,
252 * The writing-end of a pipe.
254 GNUNET_DISK_PIPE_END_WRITE = 1
259 * Get the number of blocks that are left on the partition that
260 * contains the given file (for normal users).
262 * @param part a file on the partition to check
263 * @return -1 on errors, otherwise the number of free blocks
266 GNUNET_DISK_get_blocks_available (const char *part);
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 the read/write pointer in a file
293 * @param h handle of an open file
294 * @param offset position to move to
295 * @param whence specification to which position the offset parameter relates to
296 * @return the new position on success, GNUNET_SYSERR otherwise
299 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
300 enum GNUNET_DISK_Seek whence);
304 * Get the size of the file (or directory)
305 * of the given file (in bytes).
307 * @param filename name of the file or directory
308 * @param size set to the size of the file (or,
309 * in the case of directories, the sum
310 * of all sizes of files in the directory)
311 * @param includeSymLinks should symbolic links be
313 * @return GNUNET_OK on success, GNUNET_SYSERR on error
316 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
317 int includeSymLinks);
321 * Obtain some unique identifiers for the given file
322 * that can be used to identify it in the local system.
323 * This function is used between GNUnet processes to
324 * quickly check if two files with the same absolute path
325 * are actually identical. The two processes represent
326 * the same peer but may communicate over the network
327 * (and the file may be on an NFS volume). This function
328 * may not be supported on all operating systems.
330 * @param filename name of the file
331 * @param dev set to the device ID
332 * @param ino set to the inode ID
333 * @return GNUNET_OK on success
336 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
341 * Create an (empty) temporary file on disk. If the given name is not
342 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
343 * 6 random characters will be appended to the name to create a unique
346 * @param t component to use for the name;
347 * does NOT contain "XXXXXX" or "/tmp/".
348 * @return NULL on error, otherwise name of fresh
349 * file on disk in directory for temporary files
352 GNUNET_DISK_mktemp (const char *t);
356 * Open a file. Note that the access permissions will only be
357 * used if a new file is created and if the underlying operating
358 * system supports the given permissions.
360 * @param fn file name to be opened
361 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
362 * @param perm permissions for the newly created file, use
363 * GNUNET_DISK_PERM_NONE if a file could not be created by this
364 * call (because of flags)
365 * @return IO handle on success, NULL on error
367 struct GNUNET_DISK_FileHandle *
368 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
369 enum GNUNET_DISK_AccessPermissions perm);
372 * Creates an interprocess channel
373 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
374 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
375 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
376 * @return handle to the new pipe, NULL on error
378 struct GNUNET_DISK_PipeHandle *
379 GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write);
383 * Closes an interprocess channel
385 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
388 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
391 * Closes one half of an interprocess channel
393 * @param p pipe to close end of
394 * @param end which end of the pipe to close
395 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
398 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
399 enum GNUNET_DISK_PipeEnd end);
402 * Close an open file.
404 * @param h file handle
405 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
408 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
412 * Get the handle to a particular pipe end
415 * @param n end to access
416 * @return handle for the respective end
418 const struct GNUNET_DISK_FileHandle *
419 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
420 enum GNUNET_DISK_PipeEnd n);
423 * Read the contents of a binary file into a buffer.
424 * @param h handle to an open file
425 * @param result the buffer to write the result to
426 * @param len the maximum number of bytes to read
427 * @return the number of bytes read on success, GNUNET_SYSERR on failure
430 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
435 * Read the contents of a binary file into a buffer.
437 * @param fn file name
438 * @param result the buffer to write the result to
439 * @param len the maximum number of bytes to read
440 * @return number of bytes read, GNUNET_SYSERR on failure
443 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
447 * Write a buffer to a file.
449 * @param h handle to open file
450 * @param buffer the data to write
451 * @param n number of bytes to write
452 * @return number of bytes written on success, GNUNET_SYSERR on error
455 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
456 const void *buffer, size_t n);
460 * Write a buffer to a file. If the file is longer than
461 * the given buffer size, it will be truncated.
463 * @param fn file name
464 * @param buffer the data to write
465 * @param n number of bytes to write
466 * @param mode file permissions
467 * @return number of bytes written on success, GNUNET_SYSERR on error
470 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
471 enum GNUNET_DISK_AccessPermissions mode);
477 * @param src file to copy
478 * @param dst destination file name
479 * @return GNUNET_OK on success, GNUNET_SYSERR on error
482 GNUNET_DISK_file_copy (const char *src, const char *dst);
486 * Scan a directory for files.
488 * @param dirName the name of the directory
489 * @param callback the method to call for each file
490 * @param callback_cls closure for callback
491 * @return the number of files found, -1 on error
494 GNUNET_DISK_directory_scan (const char *dirName,
495 GNUNET_FileNameCallback callback,
500 * Opaque handle used for iterating over a directory.
502 struct GNUNET_DISK_DirectoryIterator;
506 * Function called to iterate over a directory.
509 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
510 * get called on the next entry (or finish cleanly);
511 * NULL on error (will be the last call in that case)
512 * @param filename complete filename (absolute path)
513 * @param dirname directory name (absolute path)
515 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
517 GNUNET_DISK_DirectoryIterator
519 const char *filename,
520 const char *dirname);
524 * This function must be called during the DiskIteratorCallback
525 * (exactly once) to schedule the task to process the next
526 * filename in the directory (if there is one).
528 * @param iter opaque handle for the iterator
529 * @param can set to GNUNET_YES to terminate the iteration early
530 * @return GNUNET_YES if iteration will continue,
531 * GNUNET_NO if this was the last entry (and iteration is complete),
532 * GNUNET_SYSERR if "can" was YES
535 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
540 * Scan a directory for files using the scheduler to run a task for
541 * each entry. The name of the directory must be expanded first (!).
542 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
543 * may provide a simpler API.
545 * @param prio priority to use
546 * @param dirName the name of the directory
547 * @param callback the method to call for each file
548 * @param callback_cls closure for callback
551 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
553 GNUNET_DISK_DirectoryIteratorCallback
554 callback, void *callback_cls);
558 * Create the directory structure for storing
561 * @param filename name of a file in the directory
562 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
563 * GNUNET_NO if directory exists but is not writeable
566 GNUNET_DISK_directory_create_for_file (const char *filename);
570 * Test if "fil" is a directory that can be accessed.
571 * Will not print an error message if the directory
572 * does not exist. Will log errors if GNUNET_SYSERR is
575 * @param fil filename to test
576 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
577 * on any error and if exists but not directory
580 GNUNET_DISK_directory_test (const char *fil);
584 * Remove all files in a directory (rm -rf). Call with
587 * @param fileName the file to remove
588 * @return GNUNET_OK on success, GNUNET_SYSERR on error
591 GNUNET_DISK_directory_remove (const char *fileName);
595 * Implementation of "mkdir -p"
597 * @param dir the directory to create
598 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
601 GNUNET_DISK_directory_create (const char *dir);
605 * Lock a part of a file.
607 * @param fh file handle
608 * @param lockStart absolute position from where to lock
609 * @param lockEnd absolute position until where to lock
610 * @param excl GNUNET_YES for an exclusive lock
611 * @return GNUNET_OK on success, GNUNET_SYSERR on error
614 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
615 off_t lockEnd, int excl);
619 * Unlock a part of a file
620 * @param fh file handle
621 * @param unlockStart absolute position from where to unlock
622 * @param unlockEnd absolute position until where to unlock
623 * @return GNUNET_OK on success, GNUNET_SYSERR on error
626 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
631 * @brief Removes special characters as ':' from a filename.
632 * @param fn the filename to canonicalize
635 GNUNET_DISK_filename_canonicalize (char *fn);
639 * @brief Change owner of a file
640 * @param filename file to change
641 * @param user new owner of the file
642 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
645 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
649 * Construct full path to a file inside of the private
650 * directory used by GNUnet. Also creates the corresponding
651 * directory. If the resulting name is supposed to be
652 * a directory, end the last argument in '/' (or pass
653 * DIR_SEPARATOR_STR as the last argument before NULL).
655 * @param cfg configuration to use
656 * @param serviceName name of the service asking
657 * @param ... is NULL-terminated list of
658 * path components to append to the
659 * private directory name.
660 * @return the constructed filename
663 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
664 const char *serviceName, ...);
668 * Opaque handle for a memory-mapping operation.
670 struct GNUNET_DISK_MapHandle;
673 * Map a file into memory
674 * @param h open file handle
675 * @param m handle to the new mapping (will be set)
676 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
677 * @param len size of the mapping
678 * @return pointer to the mapped memory region, NULL on failure
681 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
682 struct GNUNET_DISK_MapHandle **m,
683 enum GNUNET_DISK_MapType access, size_t len);
688 * @param h mapping handle
689 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
692 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
695 * Write file changes to disk
696 * @param h handle to an open file
697 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
700 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
703 * Creates a named pipe/FIFO and opens it
704 * @param fn pointer to the name of the named pipe or to NULL
705 * @param flags open flags
706 * @param perm access permissions
707 * @return pipe handle on success, NULL on error
709 struct GNUNET_DISK_FileHandle *
710 GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
711 enum GNUNET_DISK_AccessPermissions perm);
714 * Opens already existing named pipe/FIFO
716 * @param fn name of an existing named pipe
717 * @param flags open flags
718 * @param perm access permissions
719 * @return pipe handle on success, NULL on error
721 struct GNUNET_DISK_FileHandle *
722 GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
723 enum GNUNET_DISK_AccessPermissions perm);
726 * Closes a named pipe/FIFO
727 * @param pipe named pipe
728 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
731 GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe);
733 #if 0 /* keep Emacsens' auto-indent happy */
741 /* ifndef GNUNET_DISK_LIB_H */
743 /* end of gnunet_disk_lib.h */