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);
513 * Get a handle from a native integer FD.
515 * @param fno native integer file descriptor
516 * @return file handle corresponding to the descriptor
518 struct GNUNET_DISK_FileHandle *
519 GNUNET_DISK_get_handle_from_int_fd (int fno);
523 * Get a handle from a native FD.
525 * @param fd native file descriptor
526 * @return file handle corresponding to the descriptor
528 struct GNUNET_DISK_FileHandle *
529 GNUNET_DISK_get_handle_from_native (FILE *fd);
533 * Read the contents of a binary file into a buffer.
535 * @param h handle to an open file
536 * @param result the buffer to write the result to
537 * @param len the maximum number of bytes to read
538 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
541 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
547 * Read the contents of a binary file into a buffer.
548 * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN
549 * when no data can be read).
551 * @param h handle to an open file
552 * @param result the buffer to write the result to
553 * @param len the maximum number of bytes to read
554 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
557 GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h,
563 * Read the contents of a binary file into a buffer.
565 * @param fn file name
566 * @param result the buffer to write the result to
567 * @param len the maximum number of bytes to read
568 * @return number of bytes read, #GNUNET_SYSERR on failure
571 GNUNET_DISK_fn_read (const char *fn,
577 * Write a buffer to a file.
579 * @param h handle to open file
580 * @param buffer the data to write
581 * @param n number of bytes to write
582 * @return number of bytes written on success, #GNUNET_SYSERR on error
585 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
591 * Write a buffer to a file, blocking, if necessary.
593 * @param h handle to open file
594 * @param buffer the data to write
595 * @param n number of bytes to write
596 * @return number of bytes written on success, #GNUNET_SYSERR on error
599 GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
605 * Write a buffer to a file. If the file is longer than
606 * the given buffer size, it will be truncated.
608 * @param fn file name
609 * @param buffer the data to write
610 * @param n number of bytes to write
611 * @param mode file permissions
612 * @return number of bytes written on success, #GNUNET_SYSERR on error
615 GNUNET_DISK_fn_write (const char *fn,
618 enum GNUNET_DISK_AccessPermissions mode);
624 * @param src file to copy
625 * @param dst destination file name
626 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
629 GNUNET_DISK_file_copy (const char *src, const char *dst);
633 * Scan a directory for files.
635 * @param dir_name the name of the directory
636 * @param callback the method to call for each file
637 * @param callback_cls closure for @a callback
638 * @return the number of files found, -1 on error
641 GNUNET_DISK_directory_scan (const char *dir_name,
642 GNUNET_FileNameCallback callback,
647 * Opaque handle used for iterating over a directory.
649 struct GNUNET_DISK_DirectoryIterator;
653 * Function called to iterate over a directory.
656 * @param di argument to pass to #GNUNET_DISK_directory_iterator_next to
657 * get called on the next entry (or finish cleanly);
658 * NULL on error (will be the last call in that case)
659 * @param filename complete filename (absolute path)
660 * @param dirname directory name (absolute path)
662 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
663 struct GNUNET_DISK_DirectoryIterator *di,
664 const char *filename,
665 const char *dirname);
669 * This function must be called during the DiskIteratorCallback
670 * (exactly once) to schedule the task to process the next
671 * filename in the directory (if there is one).
673 * @param iter opaque handle for the iterator
674 * @param can set to #GNUNET_YES to terminate the iteration early
675 * @return #GNUNET_YES if iteration will continue,
676 * #GNUNET_NO if this was the last entry (and iteration is complete),
677 * #GNUNET_SYSERR if @a can was #GNUNET_YES
680 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
685 * Scan a directory for files using the scheduler to run a task for
686 * each entry. The name of the directory must be expanded first (!).
687 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
688 * may provide a simpler API.
690 * @param prio priority to use
691 * @param dir_name the name of the directory
692 * @param callback the method to call for each file
693 * @param callback_cls closure for @a callback
694 * @return #GNUNET_YES if directory is not empty and @a callback
695 * will be called later, #GNUNET_NO otherwise, #GNUNET_SYSERR on error.
698 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
699 const char *dir_name,
700 GNUNET_DISK_DirectoryIteratorCallback
701 callback, void *callback_cls);
705 * Create the directory structure for storing
708 * @param filename name of a file in the directory
709 * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure,
710 * #GNUNET_NO if directory exists but is not writeable
713 GNUNET_DISK_directory_create_for_file (const char *filename);
717 * Test if @a fil is a directory and listable. Optionally, also check if the
718 * directory is readable. Will not print an error message if the directory does
719 * not exist. Will log errors if #GNUNET_SYSERR is returned (i.e., a file exists
720 * with the same name).
722 * @param fil filename to test
723 * @param is_readable #GNUNET_YES to additionally check if @a fil is readable;
724 * #GNUNET_NO to disable this check
725 * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it
726 * does not exist or `stat`ed
729 GNUNET_DISK_directory_test (const char *fil, int is_readable);
733 * Remove all files in a directory (rm -rf). Call with
736 * @param filename the file to remove
737 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
740 GNUNET_DISK_directory_remove (const char *filename);
744 * Implementation of "mkdir -p"
746 * @param dir the directory to create
747 * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise
750 GNUNET_DISK_directory_create (const char *dir);
754 * Lock a part of a file.
756 * @param fh file handle
757 * @param lock_start absolute position from where to lock
758 * @param lock_end absolute position until where to lock
759 * @param excl #GNUNET_YES for an exclusive lock
760 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
763 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
765 off_t lock_end, int excl);
769 * Unlock a part of a file.
771 * @param fh file handle
772 * @param unlock_start absolute position from where to unlock
773 * @param unlock_end absolute position until where to unlock
774 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
777 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
783 * @brief Removes special characters as ':' from a filename.
784 * @param fn the filename to canonicalize
787 GNUNET_DISK_filename_canonicalize (char *fn);
791 * @brief Change owner of a file
792 * @param filename file to change
793 * @param user new owner of the file
794 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
797 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
801 * Construct full path to a file inside of the private
802 * directory used by GNUnet. Also creates the corresponding
803 * directory. If the resulting name is supposed to be
804 * a directory, end the last argument in '/' (or pass
805 * DIR_SEPARATOR_STR as the last argument before NULL).
807 * @param cfg configuration to use
808 * @param service_name name of the service asking
809 * @param ... is NULL-terminated list of
810 * path components to append to the
811 * private directory name.
812 * @return the constructed filename
815 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
816 const char *service_name, ...);
820 * Opaque handle for a memory-mapping operation.
822 struct GNUNET_DISK_MapHandle;
825 * Map a file into memory
826 * @param h open file handle
827 * @param m handle to the new mapping (will be set)
828 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
829 * @param len size of the mapping
830 * @return pointer to the mapped memory region, NULL on failure
833 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
834 struct GNUNET_DISK_MapHandle **m,
835 enum GNUNET_DISK_MapType access, size_t len);
840 * @param h mapping handle
841 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
844 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
847 * Write file changes to disk
848 * @param h handle to an open file
849 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
852 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
855 #if 0 /* keep Emacsens' auto-indent happy */
863 /* ifndef GNUNET_DISK_LIB_H */
865 /* end of gnunet_disk_lib.h */