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 * Opaque handle used to access files.
37 struct GNUNET_DISK_FileHandle;
40 * Handle used to manage a pipe.
42 struct GNUNET_DISK_PipeHandle;
47 GNUNET_DISK_FILE, GNUNET_PIPE
51 * Handle used to access files (and pipes).
53 struct GNUNET_DISK_FileHandle
58 * File handle under W32.
65 enum GNUNET_FILE_Type type;
68 * Structure for overlapped reading (for pipes)
70 OVERLAPPED *oOverlapRead;
73 * Structure for overlapped writing (for pipes)
75 OVERLAPPED *oOverlapWrite;
79 * File handle on other OSes.
88 /* we need size_t, and since it can be both unsigned int
89 or unsigned long long, this IS platform dependent;
90 but "stdlib.h" should be portable 'enough' to be
91 unconditionally available... */
93 #include "gnunet_configuration_lib.h"
94 #include "gnunet_scheduler_lib.h"
99 #if 0 /* keep Emacsens' auto-indent happy */
106 * Specifies how a file should be opened.
108 enum GNUNET_DISK_OpenFlags
112 * Open the file for reading
114 GNUNET_DISK_OPEN_READ = 1,
117 * Open the file for writing
119 GNUNET_DISK_OPEN_WRITE = 2,
122 * Open the file for both reading and writing
124 GNUNET_DISK_OPEN_READWRITE = 3,
127 * Fail if file already exists
129 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
132 * Truncate file if it exists
134 GNUNET_DISK_OPEN_TRUNCATE = 8,
137 * Create file if it doesn't exist
139 GNUNET_DISK_OPEN_CREATE = 16,
144 GNUNET_DISK_OPEN_APPEND = 32
148 * Specifies what type of memory map is desired.
150 enum GNUNET_DISK_MapType
153 * Read-only memory map.
155 GNUNET_DISK_MAP_TYPE_READ = 1,
158 * Write-able memory map.
160 GNUNET_DISK_MAP_TYPE_WRITE = 2,
162 * Read-write memory map.
164 GNUNET_DISK_MAP_TYPE_READWRITE = 3
169 * File access permissions, UNIX-style.
171 enum GNUNET_DISK_AccessPermissions
174 * Nobody is allowed to do anything to the file.
176 GNUNET_DISK_PERM_NONE = 0,
181 GNUNET_DISK_PERM_USER_READ = 1,
186 GNUNET_DISK_PERM_USER_WRITE = 2,
191 GNUNET_DISK_PERM_USER_EXEC = 4,
196 GNUNET_DISK_PERM_GROUP_READ = 8,
201 GNUNET_DISK_PERM_GROUP_WRITE = 16,
206 GNUNET_DISK_PERM_GROUP_EXEC = 32,
209 * Everybody can read.
211 GNUNET_DISK_PERM_OTHER_READ = 64,
214 * Everybody can write.
216 GNUNET_DISK_PERM_OTHER_WRITE = 128,
219 * Everybody can execute.
221 GNUNET_DISK_PERM_OTHER_EXEC = 256
226 * Constants for specifying how to seek.
228 enum GNUNET_DISK_Seek
231 * Seek an absolute position (from the start of the file).
233 GNUNET_DISK_SEEK_SET,
236 * Seek a relative position (from the current offset).
238 GNUNET_DISK_SEEK_CUR,
241 * Seek an absolute position from the end of the file.
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 * Get the number of blocks that are left on the partition that
266 * contains the given file (for normal users).
268 * @param part a file on the partition to check
269 * @return -1 on errors, otherwise the number of free blocks
272 GNUNET_DISK_get_blocks_available (const char *part);
276 * Checks whether a handle is invalid
278 * @param h handle to check
279 * @return GNUNET_YES if invalid, GNUNET_NO if valid
282 GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
286 * Check that fil corresponds to a filename
287 * (of a file that exists and that is not a directory).
289 * @param fil filename to check
290 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
291 * else (will print an error message in that case, too).
294 GNUNET_DISK_file_test (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)
311 * of the given file (in bytes).
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 includeSymLinks should symbolic links be
319 * @return GNUNET_OK on success, GNUNET_SYSERR on error
322 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
323 int includeSymLinks);
327 * Obtain some unique identifiers for the given file
328 * that can be used to identify it in the local system.
329 * This function is used between GNUnet processes to
330 * quickly check if two files with the same absolute path
331 * are actually identical. The two processes represent
332 * the same peer but may communicate over the network
333 * (and the file may be on an NFS volume). This function
334 * may not be supported on all operating systems.
336 * @param filename name of the file
337 * @param dev set to the device ID
338 * @param ino set to the inode ID
339 * @return GNUNET_OK on success
342 GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev,
347 * Create an (empty) temporary file on disk. If the given name is not
348 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
349 * 6 random characters will be appended to the name to create a unique
352 * @param t component to use for the name;
353 * does NOT contain "XXXXXX" or "/tmp/".
354 * @return NULL on error, otherwise name of fresh
355 * file on disk in directory for temporary files
358 GNUNET_DISK_mktemp (const char *t);
362 * Open a file. Note that the access permissions will only be
363 * used if a new file is created and if the underlying operating
364 * system supports the given permissions.
366 * @param fn file name to be opened
367 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
368 * @param perm permissions for the newly created file, use
369 * GNUNET_DISK_PERM_NONE if a file could not be created by this
370 * call (because of flags)
371 * @return IO handle on success, NULL on error
373 struct GNUNET_DISK_FileHandle *
374 GNUNET_DISK_file_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
375 enum GNUNET_DISK_AccessPermissions perm);
379 * Get the size of an open file.
381 * @param fh open file handle
382 * @param size where to write size of the file
383 * @return GNUNET_OK on success, GNUNET_SYSERR on error
386 GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh,
391 * Creates an interprocess channel
392 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
393 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
394 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
395 * @return handle to the new pipe, NULL on error
397 struct GNUNET_DISK_PipeHandle *
398 GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write);
402 * Closes an interprocess channel
404 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
407 GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
410 * Closes one half of an interprocess channel
412 * @param p pipe to close end of
413 * @param end which end of the pipe to close
414 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
417 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
418 enum GNUNET_DISK_PipeEnd end);
421 * Close an open file.
423 * @param h file handle
424 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
427 GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
431 * Get the handle to a particular pipe end
434 * @param n end to access
435 * @return handle for the respective end
437 const struct GNUNET_DISK_FileHandle *
438 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
439 enum GNUNET_DISK_PipeEnd n);
442 * Read the contents of a binary file into a buffer.
443 * @param h handle to an open file
444 * @param result the buffer to write the result to
445 * @param len the maximum number of bytes to read
446 * @return the number of bytes read on success, GNUNET_SYSERR on failure
449 GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
454 * Read the contents of a binary file into a buffer.
456 * @param fn file name
457 * @param result the buffer to write the result to
458 * @param len the maximum number of bytes to read
459 * @return number of bytes read, GNUNET_SYSERR on failure
462 GNUNET_DISK_fn_read (const char *fn, void *result, size_t len);
466 * Write a buffer to a file.
468 * @param h handle to open file
469 * @param buffer the data to write
470 * @param n number of bytes to write
471 * @return number of bytes written on success, GNUNET_SYSERR on error
474 GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
475 const void *buffer, size_t n);
479 * Write a buffer to a file. If the file is longer than
480 * the given buffer size, it will be truncated.
482 * @param fn file name
483 * @param buffer the data to write
484 * @param n number of bytes to write
485 * @param mode file permissions
486 * @return number of bytes written on success, GNUNET_SYSERR on error
489 GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n,
490 enum GNUNET_DISK_AccessPermissions mode);
496 * @param src file to copy
497 * @param dst destination file name
498 * @return GNUNET_OK on success, GNUNET_SYSERR on error
501 GNUNET_DISK_file_copy (const char *src, const char *dst);
505 * Scan a directory for files.
507 * @param dirName the name of the directory
508 * @param callback the method to call for each file
509 * @param callback_cls closure for callback
510 * @return the number of files found, -1 on error
513 GNUNET_DISK_directory_scan (const char *dirName,
514 GNUNET_FileNameCallback callback,
519 * Opaque handle used for iterating over a directory.
521 struct GNUNET_DISK_DirectoryIterator;
525 * Function called to iterate over a directory.
528 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
529 * get called on the next entry (or finish cleanly);
530 * NULL on error (will be the last call in that case)
531 * @param filename complete filename (absolute path)
532 * @param dirname directory name (absolute path)
534 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
536 GNUNET_DISK_DirectoryIterator
538 const char *filename,
539 const char *dirname);
543 * This function must be called during the DiskIteratorCallback
544 * (exactly once) to schedule the task to process the next
545 * filename in the directory (if there is one).
547 * @param iter opaque handle for the iterator
548 * @param can set to GNUNET_YES to terminate the iteration early
549 * @return GNUNET_YES if iteration will continue,
550 * GNUNET_NO if this was the last entry (and iteration is complete),
551 * GNUNET_SYSERR if "can" was YES
554 GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator *iter,
559 * Scan a directory for files using the scheduler to run a task for
560 * each entry. The name of the directory must be expanded first (!).
561 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
562 * may provide a simpler API.
564 * @param prio priority to use
565 * @param dirName the name of the directory
566 * @param callback the method to call for each file
567 * @param callback_cls closure for callback
570 GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
572 GNUNET_DISK_DirectoryIteratorCallback
573 callback, void *callback_cls);
577 * Create the directory structure for storing
580 * @param filename name of a file in the directory
581 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
582 * GNUNET_NO if directory exists but is not writeable
585 GNUNET_DISK_directory_create_for_file (const char *filename);
589 * Test if "fil" is a directory that can be accessed.
590 * Will not print an error message if the directory
591 * does not exist. Will log errors if GNUNET_SYSERR is
594 * @param fil filename to test
595 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
596 * on any error and if exists but not directory
599 GNUNET_DISK_directory_test (const char *fil);
603 * Remove all files in a directory (rm -rf). Call with
606 * @param fileName the file to remove
607 * @return GNUNET_OK on success, GNUNET_SYSERR on error
610 GNUNET_DISK_directory_remove (const char *fileName);
614 * Implementation of "mkdir -p"
616 * @param dir the directory to create
617 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
620 GNUNET_DISK_directory_create (const char *dir);
624 * Lock a part of a file.
626 * @param fh file handle
627 * @param lockStart absolute position from where to lock
628 * @param lockEnd absolute position until where to lock
629 * @param excl GNUNET_YES for an exclusive lock
630 * @return GNUNET_OK on success, GNUNET_SYSERR on error
633 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, OFF_T lockStart,
634 OFF_T lockEnd, int excl);
638 * Unlock a part of a file
639 * @param fh file handle
640 * @param unlockStart absolute position from where to unlock
641 * @param unlockEnd absolute position until where to unlock
642 * @return GNUNET_OK on success, GNUNET_SYSERR on error
645 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, OFF_T unlockStart,
650 * @brief Removes special characters as ':' from a filename.
651 * @param fn the filename to canonicalize
654 GNUNET_DISK_filename_canonicalize (char *fn);
658 * @brief Change owner of a file
659 * @param filename file to change
660 * @param user new owner of the file
661 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
664 GNUNET_DISK_file_change_owner (const char *filename, const char *user);
668 * Construct full path to a file inside of the private
669 * directory used by GNUnet. Also creates the corresponding
670 * directory. If the resulting name is supposed to be
671 * a directory, end the last argument in '/' (or pass
672 * DIR_SEPARATOR_STR as the last argument before NULL).
674 * @param cfg configuration to use
675 * @param serviceName name of the service asking
676 * @param ... is NULL-terminated list of
677 * path components to append to the
678 * private directory name.
679 * @return the constructed filename
682 GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
683 const char *serviceName, ...);
687 * Opaque handle for a memory-mapping operation.
689 struct GNUNET_DISK_MapHandle;
692 * Map a file into memory
693 * @param h open file handle
694 * @param m handle to the new mapping (will be set)
695 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
696 * @param len size of the mapping
697 * @return pointer to the mapped memory region, NULL on failure
700 GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
701 struct GNUNET_DISK_MapHandle **m,
702 enum GNUNET_DISK_MapType access, size_t len);
707 * @param h mapping handle
708 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
711 GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
714 * Write file changes to disk
715 * @param h handle to an open file
716 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
719 GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
722 * Creates a named pipe/FIFO and opens it
723 * @param fn pointer to the name of the named pipe or to NULL
724 * @param flags open flags
725 * @param perm access permissions
726 * @return pipe handle on success, NULL on error
728 struct GNUNET_DISK_FileHandle *
729 GNUNET_DISK_npipe_create (char **fn, enum GNUNET_DISK_OpenFlags flags,
730 enum GNUNET_DISK_AccessPermissions perm);
733 * Opens already existing named pipe/FIFO
735 * @param fn name of an existing named pipe
736 * @param flags open flags
737 * @param perm access permissions
738 * @return pipe handle on success, NULL on error
740 struct GNUNET_DISK_FileHandle *
741 GNUNET_DISK_npipe_open (const char *fn, enum GNUNET_DISK_OpenFlags flags,
742 enum GNUNET_DISK_AccessPermissions perm);
745 * Closes a named pipe/FIFO
746 * @param pipe named pipe
747 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
750 GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe);
752 #if 0 /* keep Emacsens' auto-indent happy */
760 /* ifndef GNUNET_DISK_LIB_H */
762 /* end of gnunet_disk_lib.h */