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;
39 /* we need size_t, and since it can be both unsigned int
40 or unsigned long long, this IS platform dependent;
41 but "stdlib.h" should be portable 'enough' to be
42 unconditionally available... */
44 #include "gnunet_configuration_lib.h"
45 #include "gnunet_scheduler_lib.h"
50 #if 0 /* keep Emacsens' auto-indent happy */
57 * Specifies how a file should be opened.
59 enum GNUNET_DISK_OpenFlags
63 * Open the file for reading
65 GNUNET_DISK_OPEN_READ = 1,
68 * Open the file for writing
70 GNUNET_DISK_OPEN_WRITE = 2,
73 * Open the file for both reading and writing
75 GNUNET_DISK_OPEN_READWRITE = 3,
78 * Fail if file already exists
80 GNUNET_DISK_OPEN_FAILIFEXISTS = 4,
83 * Truncate file if it exists
85 GNUNET_DISK_OPEN_TRUNCATE = 8,
88 * Create file if it doesn't exist
90 GNUNET_DISK_OPEN_CREATE = 16,
95 GNUNET_DISK_OPEN_APPEND = 32
99 * Specifies what type of memory map is desired.
101 enum GNUNET_DISK_MapType
104 * Read-only memory map.
106 GNUNET_DISK_MAP_TYPE_READ = 1,
109 * Write-able memory map.
111 GNUNET_DISK_MAP_TYPE_WRITE = 2,
113 * Read-write memory map.
115 GNUNET_DISK_MAP_TYPE_READWRITE = 3
120 * File access permissions, UNIX-style.
122 enum GNUNET_DISK_AccessPermissions
125 * Nobody is allowed to do anything to the file.
127 GNUNET_DISK_PERM_NONE = 0,
132 GNUNET_DISK_PERM_USER_READ = 1,
137 GNUNET_DISK_PERM_USER_WRITE = 2,
142 GNUNET_DISK_PERM_USER_EXEC = 4,
147 GNUNET_DISK_PERM_GROUP_READ = 8,
152 GNUNET_DISK_PERM_GROUP_WRITE = 16,
157 GNUNET_DISK_PERM_GROUP_EXEC = 32,
160 * Everybody can read.
162 GNUNET_DISK_PERM_OTHER_READ = 64,
165 * Everybody can write.
167 GNUNET_DISK_PERM_OTHER_WRITE = 128,
170 * Everybody can execute.
172 GNUNET_DISK_PERM_OTHER_EXEC = 256
177 * Constants for specifying how to seek.
179 enum GNUNET_DISK_Seek
182 * Seek an absolute position (from the start of the file).
184 GNUNET_DISK_SEEK_SET,
187 * Seek a relative position (from the current offset).
189 GNUNET_DISK_SEEK_CUR,
192 * Seek an absolute position from the end of the file.
199 * Enumeration identifying the two ends of a pipe.
201 enum GNUNET_DISK_PipeEnd
204 * The reading-end of a pipe.
206 GNUNET_DISK_PIPE_END_READ = 0,
209 * The writing-end of a pipe.
211 GNUNET_DISK_PIPE_END_WRITE = 1
216 * Get the number of blocks that are left on the partition that
217 * contains the given file (for normal users).
219 * @param part a file on the partition to check
220 * @return -1 on errors, otherwise the number of free blocks
222 long GNUNET_DISK_get_blocks_available (const char *part);
226 * Checks whether a handle is invalid
228 * @param h handle to check
229 * @return GNUNET_YES if invalid, GNUNET_NO if valid
231 int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
235 * Check that fil corresponds to a filename
236 * (of a file that exists and that is not a directory).
238 * @param fil filename to check
239 * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
240 * else (will print an error message in that case, too).
242 int GNUNET_DISK_file_test (const char *fil);
246 * Move the read/write pointer in a file
247 * @param h handle of an open file
248 * @param offset position to move to
249 * @param whence specification to which position the offset parameter relates to
250 * @return the new position on success, GNUNET_SYSERR otherwise
253 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
255 enum GNUNET_DISK_Seek whence);
259 * Get the size of the file (or directory)
260 * of the given file (in bytes).
262 * @param filename name of the file or directory
263 * @param size set to the size of the file (or,
264 * in the case of directories, the sum
265 * of all sizes of files in the directory)
266 * @param includeSymLinks should symbolic links be
268 * @return GNUNET_OK on success, GNUNET_SYSERR on error
270 int GNUNET_DISK_file_size (const char *filename,
272 int includeSymLinks);
276 * Obtain some unique identifiers for the given file
277 * that can be used to identify it in the local system.
278 * This function is used between GNUnet processes to
279 * quickly check if two files with the same absolute path
280 * are actually identical. The two processes represent
281 * the same peer but may communicate over the network
282 * (and the file may be on an NFS volume). This function
283 * may not be supported on all operating systems.
285 * @param filename name of the file
286 * @param dev set to the device ID
287 * @param ino set to the inode ID
288 * @return GNUNET_OK on success
290 int GNUNET_DISK_file_get_identifiers (const char *filename,
296 * Create an (empty) temporary file on disk. If the given name is not
297 * an absolute path, the current 'TMPDIR' will be prepended. In any case,
298 * 6 random characters will be appended to the name to create a unique
301 * @param t component to use for the name;
302 * does NOT contain "XXXXXX" or "/tmp/".
303 * @return NULL on error, otherwise name of fresh
304 * file on disk in directory for temporary files
307 GNUNET_DISK_mktemp (const char *t);
311 * Open a file. Note that the access permissions will only be
312 * used if a new file is created and if the underlying operating
313 * system supports the given permissions.
315 * @param fn file name to be opened
316 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
317 * @param perm permissions for the newly created file, use
318 * GNUNET_DISK_PERM_NONE if a file could not be created by this
319 * call (because of flags)
320 * @return IO handle on success, NULL on error
322 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn,
323 enum GNUNET_DISK_OpenFlags flags,
324 enum GNUNET_DISK_AccessPermissions perm);
327 * Creates an interprocess channel
328 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
329 * @param inherit_read 1 to make read handle inheritable, 0 otherwise (NT only)
330 * @param inherit_write 1 to make write handle inheritable, 0 otherwise (NT only)
331 * @return handle to the new pipe, NULL on error
333 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking,
339 * Closes an interprocess channel
341 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
343 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
346 * Closes one half of an interprocess channel
348 * @param p pipe to close end of
349 * @param end which end of the pipe to close
350 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
353 GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
354 enum GNUNET_DISK_PipeEnd end);
357 * Close an open file.
359 * @param h file handle
360 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
362 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
366 * Get the handle to a particular pipe end
369 * @param n end to access
370 * @return handle for the respective end
372 const struct GNUNET_DISK_FileHandle *
373 GNUNET_DISK_pipe_handle (const struct
374 GNUNET_DISK_PipeHandle
376 enum GNUNET_DISK_PipeEnd n);
379 * Read the contents of a binary file into a buffer.
380 * @param h handle to an open file
381 * @param result the buffer to write the result to
382 * @param len the maximum number of bytes to read
383 * @return the number of bytes read on success, GNUNET_SYSERR on failure
385 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
390 * Read the contents of a binary file into a buffer.
392 * @param fn file name
393 * @param result the buffer to write the result to
394 * @param len the maximum number of bytes to read
395 * @return number of bytes read, GNUNET_SYSERR on failure
397 ssize_t GNUNET_DISK_fn_read (const char *fn,
403 * Write a buffer to a file.
405 * @param h handle to open file
406 * @param buffer the data to write
407 * @param n number of bytes to write
408 * @return number of bytes written on success, GNUNET_SYSERR on error
410 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
416 * Write a buffer to a file. If the file is longer than
417 * the given buffer size, it will be truncated.
419 * @param fn file name
420 * @param buffer the data to write
421 * @param n number of bytes to write
422 * @param mode file permissions
423 * @return number of bytes written on success, GNUNET_SYSERR on error
425 ssize_t GNUNET_DISK_fn_write (const char *fn,
428 enum GNUNET_DISK_AccessPermissions mode);
434 * @param src file to copy
435 * @param dst destination file name
436 * @return GNUNET_OK on success, GNUNET_SYSERR on error
438 int GNUNET_DISK_file_copy (const char *src, const char *dst);
442 * Scan a directory for files.
444 * @param dirName the name of the directory
445 * @param callback the method to call for each file
446 * @param callback_cls closure for callback
447 * @return the number of files found, -1 on error
449 int GNUNET_DISK_directory_scan (const char *dirName,
450 GNUNET_FileNameCallback callback,
455 * Opaque handle used for iterating over a directory.
457 struct GNUNET_DISK_DirectoryIterator;
461 * Function called to iterate over a directory.
464 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
465 * get called on the next entry (or finish cleanly);
466 * NULL on error (will be the last call in that case)
467 * @param filename complete filename (absolute path)
468 * @param dirname directory name (absolute path)
470 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
472 GNUNET_DISK_DirectoryIterator
474 const char *filename,
475 const char *dirname);
479 * This function must be called during the DiskIteratorCallback
480 * (exactly once) to schedule the task to process the next
481 * filename in the directory (if there is one).
483 * @param iter opaque handle for the iterator
484 * @param can set to GNUNET_YES to terminate the iteration early
485 * @return GNUNET_YES if iteration will continue,
486 * GNUNET_NO if this was the last entry (and iteration is complete),
487 * GNUNET_SYSERR if "can" was YES
489 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
494 * Scan a directory for files using the scheduler to run a task for
495 * each entry. The name of the directory must be expanded first (!).
496 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
497 * may provide a simpler API.
499 * @param prio priority to use
500 * @param dirName the name of the directory
501 * @param callback the method to call for each file
502 * @param callback_cls closure for callback
504 void GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority
505 prio, const char *dirName,
506 GNUNET_DISK_DirectoryIteratorCallback
507 callback, void *callback_cls);
511 * Create the directory structure for storing
514 * @param filename name of a file in the directory
515 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
516 * GNUNET_NO if directory exists but is not writeable
518 int GNUNET_DISK_directory_create_for_file (const char *filename);
522 * Test if "fil" is a directory that can be accessed.
523 * Will not print an error message if the directory
524 * does not exist. Will log errors if GNUNET_SYSERR is
527 * @param fil filename to test
528 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
529 * on any error and if exists but not directory
531 int GNUNET_DISK_directory_test (const char *fil);
535 * Remove all files in a directory (rm -rf). Call with
538 * @param fileName the file to remove
539 * @return GNUNET_OK on success, GNUNET_SYSERR on error
541 int GNUNET_DISK_directory_remove (const char *fileName);
545 * Implementation of "mkdir -p"
547 * @param dir the directory to create
548 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
550 int GNUNET_DISK_directory_create (const char *dir);
554 * Lock a part of a file.
556 * @param fh file handle
557 * @param lockStart absolute position from where to lock
558 * @param lockEnd absolute position until where to lock
559 * @param excl GNUNET_YES for an exclusive lock
560 * @return GNUNET_OK on success, GNUNET_SYSERR on error
563 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
564 off_t lockEnd, int excl);
568 * Unlock a part of a file
569 * @param fh file handle
570 * @param unlockStart absolute position from where to unlock
571 * @param unlockEnd absolute position until where to unlock
572 * @return GNUNET_OK on success, GNUNET_SYSERR on error
575 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
580 * @brief Removes special characters as ':' from a filename.
581 * @param fn the filename to canonicalize
583 void GNUNET_DISK_filename_canonicalize (char *fn);
587 * @brief Change owner of a file
588 * @param filename file to change
589 * @param user new owner of the file
590 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
592 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
596 * Construct full path to a file inside of the private
597 * directory used by GNUnet. Also creates the corresponding
598 * directory. If the resulting name is supposed to be
599 * a directory, end the last argument in '/' (or pass
600 * DIR_SEPARATOR_STR as the last argument before NULL).
602 * @param cfg configuration to use
603 * @param serviceName name of the service asking
604 * @param ... is NULL-terminated list of
605 * path components to append to the
606 * private directory name.
607 * @return the constructed filename
609 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
610 const char *serviceName, ...);
614 * Opaque handle for a memory-mapping operation.
616 struct GNUNET_DISK_MapHandle;
619 * Map a file into memory
620 * @param h open file handle
621 * @param m handle to the new mapping (will be set)
622 * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx
623 * @param len size of the mapping
624 * @return pointer to the mapped memory region, NULL on failure
626 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
627 struct GNUNET_DISK_MapHandle **m,
628 enum GNUNET_DISK_MapType access, size_t len);
633 * @param h mapping handle
634 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
636 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
639 * Write file changes to disk
640 * @param h handle to an open file
641 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
643 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
646 * Creates a named pipe/FIFO
647 * @param fn name of the named pipe
648 * @param flags open flags
649 * @param perm access permissions
650 * @return pipe handle on success, NULL on error
652 struct GNUNET_DISK_FileHandle *GNUNET_DISK_npipe_open (const char *fn,
653 enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm);
656 * Closes a named pipe/FIFO
657 * @param pipe named pipe
658 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
660 int GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe);
662 #if 0 /* keep Emacsens' auto-indent happy */
670 /* ifndef GNUNET_DISK_LIB_H */
672 /* end of gnunet_disk_lib.h */