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 * Opaque 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 */
56 /* Open the file for reading */
57 #define GNUNET_DISK_OPEN_READ 1
58 /* Open the file for writing */
59 #define GNUNET_DISK_OPEN_WRITE 2
60 /* Open the file for both reading and writing */
61 #define GNUNET_DISK_OPEN_READWRITE 3
62 /* Fail if file already exists */
63 #define GNUNET_DISK_OPEN_FAILIFEXISTS 4
64 /* Truncate file if it exists */
65 #define GNUNET_DISK_OPEN_TRUNCATE 8
66 /* Create file if it doesn't exist */
67 #define GNUNET_DISK_OPEN_CREATE 16
68 /* Append to the file */
69 #define GNUNET_DISK_OPEN_APPEND 32
71 #define GNUNET_DISK_MAP_READ 1
72 #define GNUNET_DISK_MAP_WRITE 2
73 #define GNUNET_DISK_MAP_READWRITE 3
75 #define GNUNET_DISK_PERM_USER_READ 1
76 #define GNUNET_DISK_PERM_USER_WRITE 2
77 #define GNUNET_DISK_PERM_USER_EXEC 4
78 #define GNUNET_DISK_PERM_GROUP_READ 8
79 #define GNUNET_DISK_PERM_GROUP_WRITE 16
80 #define GNUNET_DISK_PERM_GROUP_EXEC 32
81 #define GNUNET_DISK_PERM_OTHER_READ 64
82 #define GNUNET_DISK_PERM_OTHER_WRITE 128
83 #define GNUNET_DISK_PERM_OTHER_EXEC 256
93 * Get the number of blocks that are left on the partition that
94 * contains the given file (for normal users).
96 * @param part a file on the partition to check
97 * @return -1 on errors, otherwise the number of free blocks
99 long GNUNET_DISK_get_blocks_available (const char *part);
103 * Checks whether a handle is invalid
104 * @param h handle to check
105 * @return GNUNET_YES if invalid, GNUNET_NO if valid
107 int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
111 * Check that fil corresponds to a filename
112 * (of a file that exists and that is not a directory).
114 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
115 * else (will print an error message in that case, too).
117 int GNUNET_DISK_file_test (const char *fil);
121 * Move the read/write pointer in a file
122 * @param h handle of an open file
123 * @param offset position to move to
124 * @param whence specification to which position the offset parameter relates to
125 * @return the new position on success, GNUNET_SYSERR otherwise
128 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
130 enum GNUNET_DISK_Seek whence);
134 * Get the size of the file (or directory)
135 * of the given file (in bytes).
137 * @param filename name of the file or directory
138 * @param size set to the size of the file (or,
139 * in the case of directories, the sum
140 * of all sizes of files in the directory)
141 * @param includeSymLinks should symbolic links be
144 * @return GNUNET_OK on success, GNUNET_SYSERR on error
146 int GNUNET_DISK_file_size (const char *filename,
148 int includeSymLinks);
152 * Create an (empty) temporary file on disk.
154 * @param template component to use for the name;
155 * does NOT contain "XXXXXX" or "/tmp/".
156 * @return NULL on error, otherwise name of fresh
157 * file on disk in directory for temporary files
160 GNUNET_DISK_mktemp (const char *t);
165 * @param fn file name to be opened
166 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
167 * @param perm permissions for the newly created file
168 * @return IO handle on success, NULL on error
170 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
173 * Creates an interprocess channel
174 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
175 * @return handle to the new pipe, NULL on error
177 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking);
180 * Closes an interprocess channel
182 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
184 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
187 * Close an open file.
189 * @param h file handle
190 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
192 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
195 * Get the handle to a particular pipe end
197 * @param n number of the end
199 const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
200 GNUNET_DISK_PipeHandle
204 * Read the contents of a binary file into a buffer.
205 * @param h handle to an open file
206 * @param result the buffer to write the result to
207 * @param len the maximum number of bytes to read
208 * @return the number of bytes read on success, GNUNET_SYSERR on failure
210 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
215 * Read the contents of a binary file into a buffer.
216 * @param fn file name
217 * @param result the buffer to write the result to
218 * @param len the maximum number of bytes to read
219 * @return number of bytes read, GNUNET_SYSERR on failure
221 ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result,
226 * Write a buffer to a file.
228 * @param h handle to open file
229 * @param buffer the data to write
230 * @param n number of bytes to write
231 * @return GNUNET_OK on success, GNUNET_SYSERR on error
233 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
239 * Write a buffer to a file. If the file is longer than
240 * the given buffer size, it will be truncated.
242 * @param fn file name
243 * @param buffer the data to write
244 * @param n number of bytes to write
245 * @return number of bytes written on success, GNUNET_SYSERR on error
247 ssize_t GNUNET_DISK_fn_write (const char * fn,
255 * @return GNUNET_OK on success, GNUNET_SYSERR on error
257 int GNUNET_DISK_file_copy (const char *src, const char *dst);
261 * Scan a directory for files. The name of the directory
262 * must be expanded first (!).
264 * @param dirName the name of the directory
265 * @param callback the method to call for each file
266 * @param data argument to pass to callback
267 * @return the number of files found, -1 on error
269 int GNUNET_DISK_directory_scan (const char *dirName,
270 GNUNET_FileNameCallback callback,
275 * Opaque handle used for iterating over a directory.
277 struct GNUNET_DISK_DirectoryIterator;
281 * Function called to iterate over a directory.
284 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
285 * get called on the next entry (or finish cleanly)
286 * @param filename complete filename (absolute path)
287 * @param dirname directory name (absolute path)
289 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
291 GNUNET_DISK_DirectoryIterator
293 const char *filename,
294 const char *dirname);
298 * This function must be called during the DiskIteratorCallback
299 * (exactly once) to schedule the task to process the next
300 * filename in the directory (if there is one).
302 * @param iter opaque handle for the iterator
303 * @param can set to GNUNET_YES to terminate the iteration early
304 * @return GNUNET_YES if iteration will continue,
305 * GNUNET_NO if this was the last entry (and iteration is complete),
306 * GNUNET_SYSERR if "can" was YES
308 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
313 * Scan a directory for files using the scheduler to run a task for
314 * each entry. The name of the directory must be expanded first (!).
315 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
316 * may provide a simpler API.
318 * @param sched scheduler to use
319 * @param prio priority to use
320 * @param dirName the name of the directory
321 * @param callback the method to call for each file
322 * @param callback_cls closure for callback
324 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
326 enum GNUNET_SCHEDULER_Priority
327 prio, const char *dirName,
328 GNUNET_DISK_DirectoryIteratorCallback
329 callback, void *callback_cls);
333 * Create the directory structure for storing
336 * @param filename name of a file in the directory
337 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
338 * GNUNET_NO if directory exists but is not writeable
340 int GNUNET_DISK_directory_create_for_file (const char *filename);
344 * Test if fil is a directory that can be accessed.
345 * Will not print an error message if the directory
346 * does not exist. Will log errors if GNUNET_SYSERR is
349 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
350 * on any error and if exists but not directory
352 int GNUNET_DISK_directory_test (const char *fil);
356 * Remove all files in a directory (rm -rf). Call with
359 * @param fileName the file to remove
360 * @return GNUNET_OK on success, GNUNET_SYSERR on error
362 int GNUNET_DISK_directory_remove (const char *fileName);
366 * Implementation of "mkdir -p"
368 * @param dir the directory to create
369 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
371 int GNUNET_DISK_directory_create (const char *dir);
375 * Lock a part of a file
376 * @param fh file handle
377 * @lockStart absolute position from where to lock
378 * @lockEnd absolute position until where to lock
379 * @excl GNUNET_YES for an exclusive lock
380 * @return GNUNET_OK on success, GNUNET_SYSERR on error
383 GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
384 off_t lockEnd, int excl);
387 * Unlock a part of a file
388 * @param fh file handle
389 * @lockStart absolute position from where to unlock
390 * @lockEnd absolute position until where to unlock
391 * @return GNUNET_OK on success, GNUNET_SYSERR on error
394 GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart,
399 * @brief Removes special characters as ':' from a filename.
400 * @param fn the filename to canonicalize
402 void GNUNET_DISK_filename_canonicalize (char *fn);
406 * @brief Change owner of a file
407 * @param filename file to change
408 * @param user new owner of the file
409 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
411 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
415 * Construct full path to a file inside of the private
416 * directory used by GNUnet. Also creates the corresponding
417 * directory. If the resulting name is supposed to be
418 * a directory, end the last argument in '/' (or pass
419 * DIR_SEPARATOR_STR as the last argument before NULL).
421 * @param serviceName name of the service asking
422 * @param varargs is NULL-terminated list of
423 * path components to append to the
424 * private directory name.
425 * @return the constructed filename
427 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
428 const char *serviceName, ...);
432 * Opaque handle for a memory-mapping operation.
434 struct GNUNET_DISK_MapHandle;
437 * Map a file into memory
438 * @param h open file handle
439 * @param m handle to the new mapping (will be set)
440 * @param access access specification, GNUNET_DISK_MAP_xxx
441 * @param len size of the mapping
442 * @return pointer to the mapped memory region, NULL on failure
444 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
445 struct GNUNET_DISK_MapHandle **m,
446 int access, size_t len);
451 * @param h mapping handle
452 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
454 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
457 * Write file changes to disk
458 * @param h handle to an open file
459 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
461 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
463 #if 0 /* keep Emacsens' auto-indent happy */
471 /* ifndef GNUNET_DISK_LIB_H */
473 /* end of gnunet_disk_lib.h */