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
26 #ifndef GNUNET_DISK_LIB_H
27 #define GNUNET_DISK_LIB_H
29 #include "gnunet_configuration_lib.h"
30 #include "gnunet_scheduler_lib.h"
32 /* we need size_t, and since it can be both unsigned int
33 or unsigned long long, this IS platform dependent;
34 but "stdlib.h" should be portable 'enough' to be
35 unconditionally available... */
41 #if 0 /* keep Emacsens' auto-indent happy */
46 /* Open the file for reading */
47 #define GNUNET_DISK_OPEN_READ 1
48 /* Open the file for writing */
49 #define GNUNET_DISK_OPEN_WRITE 2
50 /* Open the file for both reading and writing */
51 #define GNUNET_DISK_OPEN_READWRITE 3
52 /* Fail if file already exists */
53 #define GNUNET_DISK_OPEN_FAILIFEXISTS 4
54 /* Truncate file if it exists */
55 #define GNUNET_DISK_OPEN_TRUNCATE 8
56 /* Create file if it doesn't exist */
57 #define GNUNET_DISK_OPEN_CREATE 16
58 /* Append to the file */
59 #define GNUNET_DISK_OPEN_APPEND 32
61 #define GNUNET_DISK_MAP_READ 1
62 #define GNUNET_DISK_MAP_WRITE 2
63 #define GNUNET_DISK_MAP_READWRITE 3
65 #define GNUNET_DISK_PERM_USER_READ 1
66 #define GNUNET_DISK_PERM_USER_WRITE 2
67 #define GNUNET_DISK_PERM_USER_EXEC 4
68 #define GNUNET_DISK_PERM_GROUP_READ 8
69 #define GNUNET_DISK_PERM_GROUP_WRITE 16
70 #define GNUNET_DISK_PERM_GROUP_EXEC 32
71 #define GNUNET_DISK_PERM_OTHER_READ 64
72 #define GNUNET_DISK_PERM_OTHER_WRITE 128
73 #define GNUNET_DISK_PERM_OTHER_EXEC 256
75 enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END};
77 struct GNUNET_DISK_FileHandle;
79 struct GNUNET_DISK_PipeHandle;
82 * Get the number of blocks that are left on the partition that
83 * contains the given file (for normal users).
85 * @param part a file on the partition to check
86 * @return -1 on errors, otherwise the number of free blocks
88 long GNUNET_DISK_get_blocks_available (const char *part);
92 * Checks whether a handle is invalid
93 * @param h handle to check
94 * @return GNUNET_YES if invalid, GNUNET_NO if valid
96 int GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h);
100 * Check that fil corresponds to a filename
101 * (of a file that exists and that is not a directory).
103 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
104 * else (will print an error message in that case, too).
106 int GNUNET_DISK_file_test (const char *fil);
110 * Move the read/write pointer in a file
111 * @param h handle of an open file
112 * @param offset position to move to
113 * @param whence specification to which position the offset parameter relates to
114 * @return the new position on success, GNUNET_SYSERR otherwise
117 GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset,
118 enum GNUNET_DISK_Seek whence);
122 * Get the size of the file (or directory)
123 * of the given file (in bytes).
125 * @param includeSymLinks should symbolic links be
128 * @return GNUNET_OK on success, GNUNET_SYSERR on error
130 int GNUNET_DISK_file_size (const char *filename,
131 unsigned long long *size, int includeSymLinks);
135 * Create an (empty) temporary file on disk.
137 * @param template component to use for the name;
138 * does NOT contain "XXXXXX" or "/tmp/".
139 * @return NULL on error, otherwise name of fresh
140 * file on disk in directory for temporary files
143 GNUNET_DISK_mktemp (const char *template);
148 * @param fn file name to be opened
149 * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
150 * @param perm permissions for the newly created file
151 * @return IO handle on success, NULL on error
153 struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...);
156 * Creates an interprocess channel
157 * @param blocking creates an asynchronous pipe if set to GNUNET_NO
158 * @return handle to the new pipe, NULL on error
160 struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking);
163 * Closes an interprocess channel
165 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
167 int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p);
170 * Close an open file.
172 * @param h file handle
173 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
175 int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h);
178 * Get the handle to a particular pipe end
180 * @param n number of the end
182 const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct
183 GNUNET_DISK_PipeHandle
187 * Read the contents of a binary file into a buffer.
188 * @param h handle to an open file
189 * @param result the buffer to write the result to
190 * @param len the maximum number of bytes to read
191 * @return the number of bytes read on success, GNUNET_SYSERR on failure
193 ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result,
198 * Read the contents of a binary file into a buffer.
199 * @param fn file name
200 * @param result the buffer to write the result to
201 * @param len the maximum number of bytes to read
202 * @return number of bytes read, GNUNET_SYSERR on failure
204 ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result,
209 * Write a buffer to a file.
211 * @param h handle to open file
212 * @param buffer the data to write
213 * @param n number of bytes to write
214 * @return GNUNET_OK on success, GNUNET_SYSERR on error
216 ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
222 * Write a buffer to a file. If the file is longer than
223 * the given buffer size, it will be truncated.
225 * @param fn file name
226 * @param buffer the data to write
227 * @param n number of bytes to write
228 * @return number of bytes written on success, GNUNET_SYSERR on error
230 ssize_t GNUNET_DISK_fn_write (const char * fn,
238 * @return GNUNET_OK on success, GNUNET_SYSERR on error
240 int GNUNET_DISK_file_copy (const char *src, const char *dst);
244 * Scan a directory for files. The name of the directory
245 * must be expanded first (!).
247 * @param dirName the name of the directory
248 * @param callback the method to call for each file
249 * @param data argument to pass to callback
250 * @return the number of files found, -1 on error
252 int GNUNET_DISK_directory_scan (const char *dirName,
253 GNUNET_FileNameCallback callback, void *data);
257 * Opaque handle used for iterating over a directory.
259 struct GNUNET_DISK_DirectoryIterator;
263 * Function called to iterate over a directory.
266 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
267 * get called on the next entry (or finish cleanly)
268 * @param filename complete filename (absolute path)
269 * @param dirname directory name (absolute path)
271 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
273 GNUNET_DISK_DirectoryIterator
275 const char *filename,
276 const char *dirname);
280 * This function must be called during the DiskIteratorCallback
281 * (exactly once) to schedule the task to process the next
282 * filename in the directory (if there is one).
284 * @param iter opaque handle for the iterator
285 * @param can set to GNUNET_YES to terminate the iteration early
286 * @return GNUNET_YES if iteration will continue,
287 * GNUNET_NO if this was the last entry (and iteration is complete),
288 * GNUNET_SYSERR if "can" was YES
290 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
295 * Scan a directory for files using the scheduler to run a task for
296 * each entry. The name of the directory must be expanded first (!).
297 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
298 * may provide a simpler API.
300 * @param sched scheduler to use
301 * @param prio priority to use
302 * @param dirName the name of the directory
303 * @param callback the method to call for each file
304 * @param callback_cls closure for callback
306 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
308 enum GNUNET_SCHEDULER_Priority
309 prio, const char *dirName,
310 GNUNET_DISK_DirectoryIteratorCallback
311 callback, void *callback_cls);
315 * Create the directory structure for storing
318 * @param filename name of a file in the directory
319 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
320 * GNUNET_NO if directory exists but is not writeable
322 int GNUNET_DISK_directory_create_for_file (const char *filename);
326 * Test if fil is a directory that can be accessed.
327 * Will not print an error message if the directory
328 * does not exist. Will log errors if GNUNET_SYSERR is
331 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
332 * on any error and if exists but not directory
334 int GNUNET_DISK_directory_test (const char *fil);
338 * Remove all files in a directory (rm -rf). Call with
341 * @param fileName the file to remove
342 * @return GNUNET_OK on success, GNUNET_SYSERR on error
344 int GNUNET_DISK_directory_remove (const char *fileName);
348 * Implementation of "mkdir -p"
350 * @param dir the directory to create
351 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
353 int GNUNET_DISK_directory_create (const char *dir);
357 * Lock a part of a file
358 * @param fh file handle
359 * @lockStart absolute position from where to lock
360 * @lockEnd absolute position until where to lock
361 * @return GNUNET_OK on success, GNUNET_SYSERR on error
364 GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, off_t lockStart,
369 * @brief Removes special characters as ':' from a filename.
370 * @param fn the filename to canonicalize
372 void GNUNET_DISK_filename_canonicalize (char *fn);
376 * @brief Change owner of a file
377 * @param filename file to change
378 * @param user new owner of the file
379 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
381 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
385 * Construct full path to a file inside of the private
386 * directory used by GNUnet. Also creates the corresponding
387 * directory. If the resulting name is supposed to be
388 * a directory, end the last argument in '/' (or pass
389 * DIR_SEPARATOR_STR as the last argument before NULL).
391 * @param serviceName name of the service asking
392 * @param varargs is NULL-terminated list of
393 * path components to append to the
394 * private directory name.
395 * @return the constructed filename
397 char *GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
398 const char *serviceName, ...);
402 * Opaque handle for a memory-mapping operation.
404 struct GNUNET_DISK_MapHandle;
407 * Map a file into memory
408 * @param h open file handle
409 * @param m handle to the new mapping (will be set)
410 * @param access access specification, GNUNET_DISK_MAP_xxx
411 * @param len size of the mapping
412 * @return pointer to the mapped memory region, NULL on failure
414 void *GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
415 struct GNUNET_DISK_MapHandle **m,
416 int access, size_t len);
421 * @param h mapping handle
422 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
424 int GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h);
427 * Write file changes to disk
428 * @param h handle to an open file
429 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
431 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
433 #if 0 /* keep Emacsens' auto-indent happy */
441 /* ifndef GNUNET_DISK_LIB_H */
443 /* end of gnunet_disk_lib.h */