diff options
author | Nils Durner <durner@gnunet.org> | 2009-06-15 20:11:51 +0000 |
---|---|---|
committer | Nils Durner <durner@gnunet.org> | 2009-06-15 20:11:51 +0000 |
commit | 1088905b5a89a24706d2c1877d289589e3f3c222 (patch) | |
tree | 38ec7cdc1b80502aa950c68ebefe1e3f42efa14b /src/include | |
parent | 7e3df72d6b8a32b15b02110b54c77f77e28928a2 (diff) | |
download | gnunet-1088905b5a89a24706d2c1877d289589e3f3c222.tar.gz gnunet-1088905b5a89a24706d2c1877d289589e3f3c222.zip |
more complete DISK API
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/Makefile.am | 1 | ||||
-rw-r--r-- | src/include/gnunet_disk_lib.h | 137 | ||||
-rw-r--r-- | src/include/gnunet_io_lib.h | 46 | ||||
-rw-r--r-- | src/include/gnunet_util_lib.h | 1 |
4 files changed, 169 insertions, 16 deletions
diff --git a/src/include/Makefile.am b/src/include/Makefile.am index bc832c444..0fb7aa0fd 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am | |||
@@ -24,6 +24,7 @@ gnunetinclude_HEADERS = \ | |||
24 | gnunet_fragmentation_lib.h \ | 24 | gnunet_fragmentation_lib.h \ |
25 | gnunet_getopt_lib.h \ | 25 | gnunet_getopt_lib.h \ |
26 | gnunet_hello_lib.h \ | 26 | gnunet_hello_lib.h \ |
27 | gnunet_io_lib.h \ | ||
27 | gnunet_network_lib.h \ | 28 | gnunet_network_lib.h \ |
28 | gnunet_peerinfo_service.h \ | 29 | gnunet_peerinfo_service.h \ |
29 | gnunet_program_lib.h \ | 30 | gnunet_program_lib.h \ |
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h index 3886be7c9..23a4789fa 100644 --- a/src/include/gnunet_disk_lib.h +++ b/src/include/gnunet_disk_lib.h | |||
@@ -28,6 +28,7 @@ | |||
28 | 28 | ||
29 | #include "gnunet_configuration_lib.h" | 29 | #include "gnunet_configuration_lib.h" |
30 | #include "gnunet_scheduler_lib.h" | 30 | #include "gnunet_scheduler_lib.h" |
31 | #include "gnunet_io_lib.h" | ||
31 | 32 | ||
32 | /* we need size_t, and since it can be both unsigned int | 33 | /* we need size_t, and since it can be both unsigned int |
33 | or unsigned long long, this IS platform dependent; | 34 | or unsigned long long, this IS platform dependent; |
@@ -43,6 +44,37 @@ extern "C" | |||
43 | #endif | 44 | #endif |
44 | #endif | 45 | #endif |
45 | 46 | ||
47 | /* Open the file for reading */ | ||
48 | #define GNUNET_DISK_OPEN_READ 1 | ||
49 | /* Open the file for writing */ | ||
50 | #define GNUNET_DISK_OPEN_WRITE 2 | ||
51 | /* Open the file for both reading and writing */ | ||
52 | #define GNUNET_DISK_OPEN_READWRITE 3 | ||
53 | /* Fail if file already exists */ | ||
54 | #define GNUNET_DISK_OPEN_FAILIFEXISTS 4 | ||
55 | /* Truncate file if it exists */ | ||
56 | #define GNUNET_DISK_OPEN_TRUNCATE 8 | ||
57 | /* Create file if it doesn't exist */ | ||
58 | #define GNUNET_DISK_OPEN_CREATE 16 | ||
59 | /* Append to the file */ | ||
60 | #define GNUNET_DISK_OPEN_APPEND 32 | ||
61 | |||
62 | #define GNUNET_DISK_MAP_READ 1 | ||
63 | #define GNUNET_DISK_MAP_WRITE 2 | ||
64 | #define GNUNET_DISK_MAP_READWRITE 3 | ||
65 | |||
66 | #define GNUNET_DISK_PERM_USER_READ 1 | ||
67 | #define GNUNET_DISK_PERM_USER_WRITE 2 | ||
68 | #define GNUNET_DISK_PERM_USER_EXEC 4 | ||
69 | #define GNUNET_DISK_PERM_GROUP_READ 8 | ||
70 | #define GNUNET_DISK_PERM_GROUP_WRITE 16 | ||
71 | #define GNUNET_DISK_PERM_GROUP_EXEC 32 | ||
72 | #define GNUNET_DISK_PERM_OTHER_READ 64 | ||
73 | #define GNUNET_DISK_PERM_OTHER_WRITE 128 | ||
74 | #define GNUNET_DISK_PERM_OTHER_EXEC 256 | ||
75 | |||
76 | enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END}; | ||
77 | |||
46 | /** | 78 | /** |
47 | * Get the number of blocks that are left on the partition that | 79 | * Get the number of blocks that are left on the partition that |
48 | * contains the given file (for normal users). | 80 | * contains the given file (for normal users). |
@@ -64,6 +96,18 @@ int GNUNET_DISK_file_test (const char *fil); | |||
64 | 96 | ||
65 | 97 | ||
66 | /** | 98 | /** |
99 | * Move the read/write pointer in a file | ||
100 | * @param h handle of an open file | ||
101 | * @param offset position to move to | ||
102 | * @param whence specification to which position the offset parameter relates to | ||
103 | * @return the new position on success, GNUNET_SYSERR otherwise | ||
104 | */ | ||
105 | off_t | ||
106 | GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset, | ||
107 | enum GNUNET_DISK_Seek whence); | ||
108 | |||
109 | |||
110 | /** | ||
67 | * Get the size of the file (or directory) | 111 | * Get the size of the file (or directory) |
68 | * of the given file (in bytes). | 112 | * of the given file (in bytes). |
69 | * | 113 | * |
@@ -77,41 +121,63 @@ int GNUNET_DISK_file_size (const char *filename, | |||
77 | 121 | ||
78 | 122 | ||
79 | /** | 123 | /** |
80 | * Wrapper around "open()". Opens a file. | 124 | * Open a file |
81 | * | 125 | * @param fn file name to be opened |
82 | * @return file handle, -1 on error | 126 | * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags |
127 | * @param perm permissions for the newly created file | ||
128 | * @return IO handle on success, NULL on error | ||
83 | */ | 129 | */ |
84 | int GNUNET_DISK_file_open (const char *filename, int oflag, ...); | 130 | struct GNUNET_IO_Handle *GNUNET_DISK_file_open (const char *fn, int flags, ...); |
85 | 131 | ||
86 | 132 | ||
87 | /** | 133 | /** |
88 | * Wrapper around "close()". Closes a file. | 134 | * Close an open file |
135 | * @param h file handle | ||
136 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
89 | */ | 137 | */ |
90 | void GNUNET_DISK_file_close (const char *filename, int fd); | 138 | int GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h); |
91 | 139 | ||
92 | 140 | ||
93 | /** | 141 | /** |
94 | * Read the contents of a binary file into a buffer. | 142 | * Read the contents of a binary file into a buffer. |
95 | * @param fileName the name of the file, not freed, | 143 | * @param h handle to an open file |
96 | * must already be expanded! | 144 | * @param result the buffer to write the result to |
97 | * @param len the maximum number of bytes to read | 145 | * @param len the maximum number of bytes to read |
146 | * @return the number of bytes read on success, GNUNET_SYSERR on failure | ||
147 | */ | ||
148 | int GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len); | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Read the contents of a binary file into a buffer. | ||
153 | * @param fn file name | ||
98 | * @param result the buffer to write the result to | 154 | * @param result the buffer to write the result to |
99 | * @return the number of bytes read on success, -1 on failure | 155 | * @param len the maximum number of bytes to read |
156 | * @return the number of bytes read on success, GNUNET_SYSERR on failure | ||
100 | */ | 157 | */ |
101 | int GNUNET_DISK_file_read (const char *fileName, int len, void *result); | 158 | int GNUNET_DISK_fn_read (const char * const fn, void *result, int len); |
102 | 159 | ||
103 | 160 | ||
104 | /** | 161 | /** |
105 | * Write a buffer to a file. | 162 | * Write a buffer to a file. |
106 | * @param fileName the name of the file, NOT freed! | 163 | * @param h handle to open file |
107 | * @param buffer the data to write | 164 | * @param buffer the data to write |
108 | * @param n number of bytes to write | 165 | * @param n number of bytes to write |
109 | * @param mode the mode for file permissions | 166 | * @return number of bytes written on success, GNUNET_SYSERR on error |
110 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
111 | */ | 167 | */ |
112 | int GNUNET_DISK_file_write (const char *fileName, | 168 | int GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer, |
113 | const void *buffer, unsigned int n, | 169 | unsigned int n); |
114 | const char *mode); | 170 | |
171 | |||
172 | /** | ||
173 | * Write a buffer to a file. | ||
174 | * @param fn file name | ||
175 | * @param buffer the data to write | ||
176 | * @param n number of bytes to write | ||
177 | * @return number of bytes written on success, GNUNET_SYSERR on error | ||
178 | */ | ||
179 | int GNUNET_DISK_fn_write (const char * const fn, const void *buffer, | ||
180 | unsigned int n, int mode); | ||
115 | 181 | ||
116 | 182 | ||
117 | /** | 183 | /** |
@@ -235,6 +301,18 @@ int GNUNET_DISK_directory_create (const char *dir); | |||
235 | 301 | ||
236 | 302 | ||
237 | /** | 303 | /** |
304 | * Lock a part of a file | ||
305 | * @param fh file handle | ||
306 | * @lockStart absolute position from where to lock | ||
307 | * @lockEnd absolute position until where to lock | ||
308 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
309 | */ | ||
310 | int | ||
311 | GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart, | ||
312 | off_t lockEnd); | ||
313 | |||
314 | |||
315 | /** | ||
238 | * @brief Removes special characters as ':' from a filename. | 316 | * @brief Removes special characters as ':' from a filename. |
239 | * @param fn the filename to canonicalize | 317 | * @param fn the filename to canonicalize |
240 | */ | 318 | */ |
@@ -266,6 +344,33 @@ int GNUNET_DISK_file_change_owner (const char *filename, const char *user); | |||
266 | char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg, | 344 | char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg, |
267 | const char *serviceName, ...); | 345 | const char *serviceName, ...); |
268 | 346 | ||
347 | /** | ||
348 | * Map a file into memory | ||
349 | * @param h open file handle | ||
350 | * @param m handle to the new mapping | ||
351 | * @param access access specification, GNUNET_DISK_MAP_xxx | ||
352 | * @param len size of the mapping | ||
353 | * @return pointer to the mapped memory region, NULL on failure | ||
354 | */ | ||
355 | void *GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle **m, | ||
356 | int access, size_t len); | ||
357 | |||
358 | /** | ||
359 | * Unmap a file | ||
360 | * @param h mapping handle | ||
361 | * @param addr pointer to the mapped memory region | ||
362 | * @param len size of the mapping | ||
363 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
364 | */ | ||
365 | int GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len); | ||
366 | |||
367 | /** | ||
368 | * Write file changes to disk | ||
369 | * @param h handle to an open file | ||
370 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
371 | */ | ||
372 | int GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h); | ||
373 | |||
269 | #if 0 /* keep Emacsens' auto-indent happy */ | 374 | #if 0 /* keep Emacsens' auto-indent happy */ |
270 | { | 375 | { |
271 | #endif | 376 | #endif |
diff --git a/src/include/gnunet_io_lib.h b/src/include/gnunet_io_lib.h new file mode 100644 index 000000000..3d04a2317 --- /dev/null +++ b/src/include/gnunet_io_lib.h | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 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_io_lib.h | ||
23 | * @brief Helper functions for abstract IO handles | ||
24 | * @author Nils Durner | ||
25 | */ | ||
26 | |||
27 | #ifndef IO_HANDLE_H_ | ||
28 | #define IO_HANDLE_H_ | ||
29 | |||
30 | struct GNUNET_IO_Handle; | ||
31 | |||
32 | /** | ||
33 | * Checks whether a handle is invalid | ||
34 | * @param h handle to check | ||
35 | * @return GNUNET_YES if invalid, GNUNET_NO if valid | ||
36 | */ | ||
37 | int GNUNET_IO_handle_invalid (const struct GNUNET_IO_Handle *h); | ||
38 | |||
39 | /** | ||
40 | * Mark a handle as invalid | ||
41 | * @param h file handle | ||
42 | */ | ||
43 | void GNUNET_IO_handle_invalidate (struct GNUNET_IO_Handle *h); | ||
44 | |||
45 | |||
46 | #endif /* IO_HANDLE_H_ */ | ||
diff --git a/src/include/gnunet_util_lib.h b/src/include/gnunet_util_lib.h index 6354e28fa..58621caf5 100644 --- a/src/include/gnunet_util_lib.h +++ b/src/include/gnunet_util_lib.h | |||
@@ -43,6 +43,7 @@ extern "C" | |||
43 | #include "gnunet_crypto_lib.h" | 43 | #include "gnunet_crypto_lib.h" |
44 | #include "gnunet_disk_lib.h" | 44 | #include "gnunet_disk_lib.h" |
45 | #include "gnunet_getopt_lib.h" | 45 | #include "gnunet_getopt_lib.h" |
46 | #include "gnunet_io_lib.h" | ||
46 | #include "gnunet_network_lib.h" | 47 | #include "gnunet_network_lib.h" |
47 | #include "gnunet_plugin_lib.h" | 48 | #include "gnunet_plugin_lib.h" |
48 | #include "gnunet_program_lib.h" | 49 | #include "gnunet_program_lib.h" |