2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006 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 */
47 * Get the number of blocks that are left on the partition that
48 * contains the given file (for normal users).
50 * @param part a file on the partition to check
51 * @return -1 on errors, otherwise the number of free blocks
53 long GNUNET_DISK_get_blocks_available (const char *part);
57 * Check that fil corresponds to a filename
58 * (of a file that exists and that is not a directory).
60 * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something
61 * else (will print an error message in that case, too).
63 int GNUNET_DISK_file_test (const char *fil);
67 * Get the size of the file (or directory)
68 * of the given file (in bytes).
70 * @param includeSymLinks should symbolic links be
73 * @return GNUNET_OK on success, GNUNET_SYSERR on error
75 int GNUNET_DISK_file_size (const char *filename,
76 unsigned long long *size, int includeSymLinks);
80 * Wrapper around "open()". Opens a file.
82 * @return file handle, -1 on error
84 int GNUNET_DISK_file_open (const char *filename, int oflag, ...);
88 * Wrapper around "close()". Closes a file.
90 void GNUNET_DISK_file_close (const char *filename, int fd);
94 * Read the contents of a binary file into a buffer.
95 * @param fileName the name of the file, not freed,
96 * must already be expanded!
97 * @param len the maximum number of bytes to read
98 * @param result the buffer to write the result to
99 * @return the number of bytes read on success, -1 on failure
101 int GNUNET_DISK_file_read (const char *fileName, int len, void *result);
105 * Write a buffer to a file.
106 * @param fileName the name of the file, NOT freed!
107 * @param buffer the data to write
108 * @param n number of bytes to write
109 * @param mode the mode for file permissions
110 * @return GNUNET_OK on success, GNUNET_SYSERR on error
112 int GNUNET_DISK_file_write (const char *fileName,
113 const void *buffer, unsigned int n,
119 * @return GNUNET_OK on success, GNUNET_SYSERR on error
121 int GNUNET_DISK_file_copy (const char *src, const char *dst);
125 * Scan a directory for files. The name of the directory
126 * must be expanded first (!).
128 * @param dirName the name of the directory
129 * @param callback the method to call for each file
130 * @param data argument to pass to callback
131 * @return the number of files found, -1 on error
133 int GNUNET_DISK_directory_scan (const char *dirName,
134 GNUNET_FileNameCallback callback, void *data);
138 * Opaque handle used for iterating over a directory.
140 struct GNUNET_DISK_DirectoryIterator;
144 * Function called to iterate over a directory.
147 * @param di argument to pass to "GNUNET_DISK_directory_iterator_next" to
148 * get called on the next entry (or finish cleanly)
149 * @param filename complete filename (absolute path)
150 * @param dirname directory name (absolute path)
152 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
154 GNUNET_DISK_DirectoryIterator
156 const char *filename,
157 const char *dirname);
161 * This function must be called during the DiskIteratorCallback
162 * (exactly once) to schedule the task to process the next
163 * filename in the directory (if there is one).
165 * @param iter opaque handle for the iterator
166 * @param can set to GNUNET_YES to terminate the iteration early
167 * @return GNUNET_YES if iteration will continue,
168 * GNUNET_NO if this was the last entry (and iteration is complete),
169 * GNUNET_SYSERR if "can" was YES
171 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
176 * Scan a directory for files using the scheduler to run a task for
177 * each entry. The name of the directory must be expanded first (!).
178 * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
179 * may provide a simpler API.
181 * @param sched scheduler to use
182 * @param prio priority to use
183 * @param dirName the name of the directory
184 * @param callback the method to call for each file
185 * @param callback_cls closure for callback
187 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
189 enum GNUNET_SCHEDULER_Priority
190 prio, const char *dirName,
191 GNUNET_DISK_DirectoryIteratorCallback
192 callback, void *callback_cls);
196 * Create the directory structure for storing
199 * @param filename name of a file in the directory
200 * @returns GNUNET_OK on success, GNUNET_SYSERR on failure,
201 * GNUNET_NO if directory exists but is not writeable
203 int GNUNET_DISK_directory_create_for_file (const char *filename);
207 * Test if fil is a directory that can be accessed.
208 * Will not print an error message if the directory
209 * does not exist. Will log errors if GNUNET_SYSERR is
212 * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
213 * on any error and if exists but not directory
215 int GNUNET_DISK_directory_test (const char *fil);
219 * Remove all files in a directory (rm -rf). Call with
222 * @param fileName the file to remove
223 * @return GNUNET_OK on success, GNUNET_SYSERR on error
225 int GNUNET_DISK_directory_remove (const char *fileName);
229 * Implementation of "mkdir -p"
231 * @param dir the directory to create
232 * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
234 int GNUNET_DISK_directory_create (const char *dir);
238 * @brief Removes special characters as ':' from a filename.
239 * @param fn the filename to canonicalize
241 void GNUNET_DISK_filename_canonicalize (char *fn);
245 * @brief Change owner of a file
246 * @param filename file to change
247 * @param user new owner of the file
248 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
250 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
254 * Construct full path to a file inside of the private
255 * directory used by GNUnet. Also creates the corresponding
256 * directory. If the resulting name is supposed to be
257 * a directory, end the last argument in '/' (or pass
258 * DIR_SEPARATOR_STR as the last argument before NULL).
260 * @param serviceName name of the service asking
261 * @param varargs is NULL-terminated list of
262 * path components to append to the
263 * private directory name.
264 * @return the constructed filename
266 char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
267 const char *serviceName, ...);
269 #if 0 /* keep Emacsens' auto-indent happy */
277 /* ifndef GNUNET_DISK_LIB_H */
279 /* end of gnunet_disk_lib.h */