3886be7c9f20d55c9b85d3539978121ffeb5951b
[oweals/gnunet.git] / src / include / gnunet_disk_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_disk_lib.h
23  * @brief disk IO apis
24  */
25
26 #ifndef GNUNET_DISK_LIB_H
27 #define GNUNET_DISK_LIB_H
28
29 #include "gnunet_configuration_lib.h"
30 #include "gnunet_scheduler_lib.h"
31
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... */
36 #include <stdlib.h>
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 /**
47  * Get the number of blocks that are left on the partition that
48  * contains the given file (for normal users).
49  *
50  * @param part a file on the partition to check
51  * @return -1 on errors, otherwise the number of free blocks
52  */
53 long GNUNET_DISK_get_blocks_available (const char *part);
54
55
56 /**
57  * Check that fil corresponds to a filename
58  * (of a file that exists and that is not a directory).
59  *
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).
62  */
63 int GNUNET_DISK_file_test (const char *fil);
64
65
66 /**
67  * Get the size of the file (or directory)
68  * of the given file (in bytes).
69  *
70  * @param includeSymLinks should symbolic links be
71  *        included?
72  *
73  * @return GNUNET_OK on success, GNUNET_SYSERR on error
74  */
75 int GNUNET_DISK_file_size (const char *filename,
76                            unsigned long long *size, int includeSymLinks);
77
78
79 /**
80  * Wrapper around "open()".  Opens a file.
81  *
82  * @return file handle, -1 on error
83  */
84 int GNUNET_DISK_file_open (const char *filename, int oflag, ...);
85
86
87 /**
88  * Wrapper around "close()".  Closes a file.
89  */
90 void GNUNET_DISK_file_close (const char *filename, int fd);
91
92
93 /**
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
100  */
101 int GNUNET_DISK_file_read (const char *fileName, int len, void *result);
102
103
104 /**
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
111  */
112 int GNUNET_DISK_file_write (const char *fileName,
113                             const void *buffer, unsigned int n,
114                             const char *mode);
115
116
117 /**
118  * Copy a file.
119  * @return GNUNET_OK on success, GNUNET_SYSERR on error
120  */
121 int GNUNET_DISK_file_copy (const char *src, const char *dst);
122
123
124 /**
125  * Scan a directory for files. The name of the directory
126  * must be expanded first (!).
127  *
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
132  */
133 int GNUNET_DISK_directory_scan (const char *dirName,
134                                 GNUNET_FileNameCallback callback, void *data);
135
136
137 /**
138  * Opaque handle used for iterating over a directory.
139  */
140 struct GNUNET_DISK_DirectoryIterator;
141
142
143 /**
144  * Function called to iterate over a directory.
145  *
146  * @param cls closure
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)
151  */
152 typedef void (*GNUNET_DISK_DirectoryIteratorCallback) (void *cls,
153                                                        struct
154                                                        GNUNET_DISK_DirectoryIterator
155                                                        * di,
156                                                        const char *filename,
157                                                        const char *dirname);
158
159
160 /**
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).
164  *
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
170  */
171 int GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
172                                          *iter, int can);
173
174
175 /**
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.
180  *
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
186  */
187 void GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle
188                                            *sched,
189                                            enum GNUNET_SCHEDULER_Priority
190                                            prio, const char *dirName,
191                                            GNUNET_DISK_DirectoryIteratorCallback
192                                            callback, void *callback_cls);
193
194
195 /**
196  * Create the directory structure for storing
197  * a file.
198  *
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
202  */
203 int GNUNET_DISK_directory_create_for_file (const char *filename);
204
205
206 /**
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
210  * returned.
211  *
212  * @return GNUNET_YES if yes, GNUNET_NO if does not exist, GNUNET_SYSERR
213  *   on any error and if exists but not directory
214  */
215 int GNUNET_DISK_directory_test (const char *fil);
216
217
218 /**
219  * Remove all files in a directory (rm -rf). Call with
220  * caution.
221  *
222  * @param fileName the file to remove
223  * @return GNUNET_OK on success, GNUNET_SYSERR on error
224  */
225 int GNUNET_DISK_directory_remove (const char *fileName);
226
227
228 /**
229  * Implementation of "mkdir -p"
230  *
231  * @param dir the directory to create
232  * @returns GNUNET_SYSERR on failure, GNUNET_OK otherwise
233  */
234 int GNUNET_DISK_directory_create (const char *dir);
235
236
237 /**
238  * @brief Removes special characters as ':' from a filename.
239  * @param fn the filename to canonicalize
240  */
241 void GNUNET_DISK_filename_canonicalize (char *fn);
242
243
244 /**
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
249  */
250 int GNUNET_DISK_file_change_owner (const char *filename, const char *user);
251
252
253 /**
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).
259  *
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
265  */
266 char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
267                                      const char *serviceName, ...);
268
269 #if 0                           /* keep Emacsens' auto-indent happy */
270 {
271 #endif
272 #ifdef __cplusplus
273 }
274 #endif
275
276
277 /* ifndef GNUNET_DISK_LIB_H */
278 #endif
279 /* end of gnunet_disk_lib.h */