diff options
-rw-r--r-- | src/fs/fs_file_information.c | 3 | ||||
-rw-r--r-- | src/fs/fs_unindex.c | 3 | ||||
-rw-r--r-- | src/fs/gnunet-directory.c | 3 | ||||
-rw-r--r-- | src/fs/gnunet-service-fs.c | 3 | ||||
-rw-r--r-- | src/include/gnunet_disk_lib.h | 133 | ||||
-rw-r--r-- | src/statistics/gnunet-service-statistics.c | 3 | ||||
-rw-r--r-- | src/util/bio.c | 3 | ||||
-rw-r--r-- | src/util/crypto_hash.c | 3 | ||||
-rw-r--r-- | src/util/crypto_rsa.c | 3 | ||||
-rw-r--r-- | src/util/disk.c | 326 | ||||
-rw-r--r-- | src/util/server.c | 11 | ||||
-rw-r--r-- | src/util/test_scheduler.c | 4 |
12 files changed, 343 insertions, 155 deletions
diff --git a/src/fs/fs_file_information.c b/src/fs/fs_file_information.c index 84b79866a..5afdc392f 100644 --- a/src/fs/fs_file_information.c +++ b/src/fs/fs_file_information.c | |||
@@ -139,7 +139,8 @@ data_reader_file(void *cls, | |||
139 | if (fi->fd == NULL) | 139 | if (fi->fd == NULL) |
140 | { | 140 | { |
141 | fi->fd = GNUNET_DISK_file_open (fi->filename, | 141 | fi->fd = GNUNET_DISK_file_open (fi->filename, |
142 | GNUNET_DISK_OPEN_READ); | 142 | GNUNET_DISK_OPEN_READ, |
143 | GNUNET_DISK_PERM_NONE); | ||
143 | if (fi->fd == NULL) | 144 | if (fi->fd == NULL) |
144 | { | 145 | { |
145 | GNUNET_asprintf (emsg, | 146 | GNUNET_asprintf (emsg, |
diff --git a/src/fs/fs_unindex.c b/src/fs/fs_unindex.c index 51d46ea37..53814e186 100644 --- a/src/fs/fs_unindex.c +++ b/src/fs/fs_unindex.c | |||
@@ -319,7 +319,8 @@ process_fs_response (void *cls, | |||
319 | return; | 319 | return; |
320 | } | 320 | } |
321 | uc->fh = GNUNET_DISK_file_open (uc->filename, | 321 | uc->fh = GNUNET_DISK_file_open (uc->filename, |
322 | GNUNET_DISK_OPEN_READ); | 322 | GNUNET_DISK_OPEN_READ, |
323 | GNUNET_DISK_PERM_NONE); | ||
323 | if (NULL == uc->fh) | 324 | if (NULL == uc->fh) |
324 | { | 325 | { |
325 | GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO); | 326 | GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO); |
diff --git a/src/fs/gnunet-directory.c b/src/fs/gnunet-directory.c index a5caaa53d..6c7bcdaf3 100644 --- a/src/fs/gnunet-directory.c +++ b/src/fs/gnunet-directory.c | |||
@@ -116,7 +116,8 @@ run (void *cls, | |||
116 | &size, | 116 | &size, |
117 | GNUNET_YES)) || | 117 | GNUNET_YES)) || |
118 | (NULL == (h = GNUNET_DISK_file_open (filename, | 118 | (NULL == (h = GNUNET_DISK_file_open (filename, |
119 | GNUNET_DISK_OPEN_READ))) ) | 119 | GNUNET_DISK_OPEN_READ, |
120 | GNUNET_DISK_PERM_NONE))) ) | ||
120 | { | 121 | { |
121 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 122 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
122 | _("Failed to read directory `%s'\n"), | 123 | _("Failed to read directory `%s'\n"), |
diff --git a/src/fs/gnunet-service-fs.c b/src/fs/gnunet-service-fs.c index 2a50cbee9..b01a4f29e 100644 --- a/src/fs/gnunet-service-fs.c +++ b/src/fs/gnunet-service-fs.c | |||
@@ -1430,7 +1430,8 @@ handle_on_demand_block (const GNUNET_HashCode * key, | |||
1430 | fh = NULL; | 1430 | fh = NULL; |
1431 | if ( (NULL == fn) || | 1431 | if ( (NULL == fn) || |
1432 | (NULL == (fh = GNUNET_DISK_file_open (fn, | 1432 | (NULL == (fh = GNUNET_DISK_file_open (fn, |
1433 | GNUNET_DISK_OPEN_READ))) || | 1433 | GNUNET_DISK_OPEN_READ, |
1434 | GNUNET_DISK_PERM_NONE))) || | ||
1434 | (off != | 1435 | (off != |
1435 | GNUNET_DISK_file_seek (fh, | 1436 | GNUNET_DISK_file_seek (fh, |
1436 | off, | 1437 | off, |
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h index ac20a61a3..38b5df9c6 100644 --- a/src/include/gnunet_disk_lib.h +++ b/src/include/gnunet_disk_lib.h | |||
@@ -31,7 +31,7 @@ | |||
31 | struct GNUNET_DISK_FileHandle; | 31 | struct GNUNET_DISK_FileHandle; |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * Opaque handle used to manage a pipe. | 34 | * Handle used to manage a pipe. |
35 | */ | 35 | */ |
36 | struct GNUNET_DISK_PipeHandle; | 36 | struct GNUNET_DISK_PipeHandle; |
37 | 37 | ||
@@ -116,16 +116,62 @@ enum GNUNET_DISK_MapType | |||
116 | }; | 116 | }; |
117 | 117 | ||
118 | 118 | ||
119 | // FIXME: use enum here! | 119 | /** |
120 | #define GNUNET_DISK_PERM_USER_READ 1 | 120 | * File access permissions, UNIX-style. |
121 | #define GNUNET_DISK_PERM_USER_WRITE 2 | 121 | */ |
122 | #define GNUNET_DISK_PERM_USER_EXEC 4 | 122 | enum GNUNET_DISK_AccessPermissions |
123 | #define GNUNET_DISK_PERM_GROUP_READ 8 | 123 | { |
124 | #define GNUNET_DISK_PERM_GROUP_WRITE 16 | 124 | /** |
125 | #define GNUNET_DISK_PERM_GROUP_EXEC 32 | 125 | * Nobody is allowed to do anything to the file. |
126 | #define GNUNET_DISK_PERM_OTHER_READ 64 | 126 | */ |
127 | #define GNUNET_DISK_PERM_OTHER_WRITE 128 | 127 | GNUNET_DISK_PERM_NONE = 0, |
128 | #define GNUNET_DISK_PERM_OTHER_EXEC 256 | 128 | |
129 | /** | ||
130 | * Owner can read. | ||
131 | */ | ||
132 | GNUNET_DISK_PERM_USER_READ = 1, | ||
133 | |||
134 | /** | ||
135 | * Owner can write. | ||
136 | */ | ||
137 | GNUNET_DISK_PERM_USER_WRITE = 2, | ||
138 | |||
139 | /** | ||
140 | * Owner can execute. | ||
141 | */ | ||
142 | GNUNET_DISK_PERM_USER_EXEC = 4, | ||
143 | |||
144 | /** | ||
145 | * Group can read. | ||
146 | */ | ||
147 | GNUNET_DISK_PERM_GROUP_READ = 8, | ||
148 | |||
149 | /** | ||
150 | * Group can write. | ||
151 | */ | ||
152 | GNUNET_DISK_PERM_GROUP_WRITE = 16, | ||
153 | |||
154 | /** | ||
155 | * Group can execute. | ||
156 | */ | ||
157 | GNUNET_DISK_PERM_GROUP_EXEC = 32, | ||
158 | |||
159 | /** | ||
160 | * Everybody can read. | ||
161 | */ | ||
162 | GNUNET_DISK_PERM_OTHER_READ = 64, | ||
163 | |||
164 | /** | ||
165 | * Everybody can write. | ||
166 | */ | ||
167 | GNUNET_DISK_PERM_OTHER_WRITE = 128, | ||
168 | |||
169 | /** | ||
170 | * Everybody can execute. | ||
171 | */ | ||
172 | GNUNET_DISK_PERM_OTHER_EXEC = 256 | ||
173 | }; | ||
174 | |||
129 | 175 | ||
130 | /** | 176 | /** |
131 | * Constants for specifying how to seek. | 177 | * Constants for specifying how to seek. |
@@ -148,6 +194,24 @@ enum GNUNET_DISK_Seek | |||
148 | GNUNET_DISK_SEEK_END | 194 | GNUNET_DISK_SEEK_END |
149 | }; | 195 | }; |
150 | 196 | ||
197 | |||
198 | /** | ||
199 | * Enumeration identifying the two ends of a pipe. | ||
200 | */ | ||
201 | enum GNUNET_DISK_PipeEnd | ||
202 | { | ||
203 | /** | ||
204 | * The reading-end of a pipe. | ||
205 | */ | ||
206 | GNUNET_DISK_PIPE_END_READ = 0, | ||
207 | |||
208 | /** | ||
209 | * The writing-end of a pipe. | ||
210 | */ | ||
211 | GNUNET_DISK_PIPE_END_WRITE = 1 | ||
212 | }; | ||
213 | |||
214 | |||
151 | /** | 215 | /** |
152 | * Get the number of blocks that are left on the partition that | 216 | * Get the number of blocks that are left on the partition that |
153 | * contains the given file (for normal users). | 217 | * contains the given file (for normal users). |
@@ -160,6 +224,7 @@ long GNUNET_DISK_get_blocks_available (const char *part); | |||
160 | 224 | ||
161 | /** | 225 | /** |
162 | * Checks whether a handle is invalid | 226 | * Checks whether a handle is invalid |
227 | * | ||
163 | * @param h handle to check | 228 | * @param h handle to check |
164 | * @return GNUNET_YES if invalid, GNUNET_NO if valid | 229 | * @return GNUNET_YES if invalid, GNUNET_NO if valid |
165 | */ | 230 | */ |
@@ -200,7 +265,6 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | |||
200 | * of all sizes of files in the directory) | 265 | * of all sizes of files in the directory) |
201 | * @param includeSymLinks should symbolic links be | 266 | * @param includeSymLinks should symbolic links be |
202 | * included? | 267 | * included? |
203 | * | ||
204 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 268 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
205 | */ | 269 | */ |
206 | int GNUNET_DISK_file_size (const char *filename, | 270 | int GNUNET_DISK_file_size (const char *filename, |
@@ -231,26 +295,30 @@ int GNUNET_DISK_file_get_identifiers (const char *filename, | |||
231 | /** | 295 | /** |
232 | * Create an (empty) temporary file on disk. | 296 | * Create an (empty) temporary file on disk. |
233 | * | 297 | * |
234 | * @param tmpl component to use for the name; | 298 | * @param t component to use for the name; |
235 | * does NOT contain "XXXXXX" or "/tmp/". | 299 | * does NOT contain "XXXXXX" or "/tmp/". |
236 | * @return NULL on error, otherwise name of fresh | 300 | * @return NULL on error, otherwise name of fresh |
237 | * file on disk in directory for temporary files | 301 | * file on disk in directory for temporary files |
238 | */ | 302 | */ |
239 | char * | 303 | char * |
240 | GNUNET_DISK_mktemp (const char *tmpl); | 304 | GNUNET_DISK_mktemp (const char *t); |
241 | 305 | ||
242 | 306 | ||
243 | /** | 307 | /** |
244 | * Open a file. | 308 | * Open a file. Note that the access permissions will only be |
309 | * used if a new file is created and if the underlying operating | ||
310 | * system supports the given permissions. | ||
245 | * | 311 | * |
246 | * @param fn file name to be opened | 312 | * @param fn file name to be opened |
247 | * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags | 313 | * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags |
248 | * @param ... permissions for the newly created file (only required if creation is possible) | 314 | * @param perm permissions for the newly created file, use |
315 | * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this | ||
316 | * call (because of flags) | ||
249 | * @return IO handle on success, NULL on error | 317 | * @return IO handle on success, NULL on error |
250 | */ | 318 | */ |
251 | struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, | 319 | struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, |
252 | enum GNUNET_DISK_OpenFlags flags, | 320 | enum GNUNET_DISK_OpenFlags flags, |
253 | ...); | 321 | enum GNUNET_DISK_AccessPermissions perm); |
254 | 322 | ||
255 | /** | 323 | /** |
256 | * Creates an interprocess channel | 324 | * Creates an interprocess channel |
@@ -277,12 +345,14 @@ int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h); | |||
277 | /** | 345 | /** |
278 | * Get the handle to a particular pipe end | 346 | * Get the handle to a particular pipe end |
279 | * @param p pipe | 347 | * @param p pipe |
280 | * @param n number of the end (0 or 1); FIXME: use enum here! | 348 | * @param n end to access |
281 | * @return handle for the respective end | 349 | * @return handle for the respective end |
282 | */ | 350 | */ |
283 | const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct | 351 | const struct GNUNET_DISK_FileHandle * |
284 | GNUNET_DISK_PipeHandle | 352 | GNUNET_DISK_pipe_handle (const struct |
285 | *p, int n); | 353 | GNUNET_DISK_PipeHandle |
354 | *p, | ||
355 | enum GNUNET_DISK_PipeEnd n); | ||
286 | 356 | ||
287 | /** | 357 | /** |
288 | * Read the contents of a binary file into a buffer. | 358 | * Read the contents of a binary file into a buffer. |
@@ -297,12 +367,14 @@ ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *res | |||
297 | 367 | ||
298 | /** | 368 | /** |
299 | * Read the contents of a binary file into a buffer. | 369 | * Read the contents of a binary file into a buffer. |
370 | * | ||
300 | * @param fn file name | 371 | * @param fn file name |
301 | * @param result the buffer to write the result to | 372 | * @param result the buffer to write the result to |
302 | * @param len the maximum number of bytes to read | 373 | * @param len the maximum number of bytes to read |
303 | * @return number of bytes read, GNUNET_SYSERR on failure | 374 | * @return number of bytes read, GNUNET_SYSERR on failure |
304 | */ | 375 | */ |
305 | ssize_t GNUNET_DISK_fn_read (const char * const fn, void *result, | 376 | ssize_t GNUNET_DISK_fn_read (const char *fn, |
377 | void *result, | ||
306 | size_t len); | 378 | size_t len); |
307 | 379 | ||
308 | 380 | ||
@@ -326,33 +398,36 @@ ssize_t GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, | |||
326 | * @param fn file name | 398 | * @param fn file name |
327 | * @param buffer the data to write | 399 | * @param buffer the data to write |
328 | * @param n number of bytes to write | 400 | * @param n number of bytes to write |
401 | * @param mode file permissions | ||
329 | * @return number of bytes written on success, GNUNET_SYSERR on error | 402 | * @return number of bytes written on success, GNUNET_SYSERR on error |
330 | */ | 403 | */ |
331 | ssize_t GNUNET_DISK_fn_write (const char * fn, | 404 | ssize_t GNUNET_DISK_fn_write (const char *fn, |
332 | const void *buffer, | 405 | const void *buffer, |
333 | size_t n, | 406 | size_t n, |
334 | int mode); | 407 | enum GNUNET_DISK_AccessPermissions mode); |
335 | 408 | ||
336 | 409 | ||
337 | /** | 410 | /** |
338 | * Copy a file. | 411 | * Copy a file. |
412 | * | ||
413 | * @param src file to copy | ||
414 | * @param dst destination file name | ||
339 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 415 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
340 | */ | 416 | */ |
341 | int GNUNET_DISK_file_copy (const char *src, const char *dst); | 417 | int GNUNET_DISK_file_copy (const char *src, const char *dst); |
342 | 418 | ||
343 | 419 | ||
344 | /** | 420 | /** |
345 | * Scan a directory for files. The name of the directory | 421 | * Scan a directory for files. |
346 | * must be expanded first (!). | ||
347 | * | 422 | * |
348 | * @param dirName the name of the directory | 423 | * @param dirName the name of the directory |
349 | * @param callback the method to call for each file | 424 | * @param callback the method to call for each file |
350 | * @param data argument to pass to callback | 425 | * @param callback_cls closure for callback |
351 | * @return the number of files found, -1 on error | 426 | * @return the number of files found, -1 on error |
352 | */ | 427 | */ |
353 | int GNUNET_DISK_directory_scan (const char *dirName, | 428 | int GNUNET_DISK_directory_scan (const char *dirName, |
354 | GNUNET_FileNameCallback callback, | 429 | GNUNET_FileNameCallback callback, |
355 | void *data); | 430 | void *callback_cls); |
356 | 431 | ||
357 | 432 | ||
358 | /** | 433 | /** |
@@ -425,7 +500,7 @@ int GNUNET_DISK_directory_create_for_file (const char *filename); | |||
425 | 500 | ||
426 | 501 | ||
427 | /** | 502 | /** |
428 | * Test if fil is a directory that can be accessed. | 503 | * Test if "fil" is a directory that can be accessed. |
429 | * Will not print an error message if the directory | 504 | * Will not print an error message if the directory |
430 | * does not exist. Will log errors if GNUNET_SYSERR is | 505 | * does not exist. Will log errors if GNUNET_SYSERR is |
431 | * returned. | 506 | * returned. |
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c index 20dd10586..3221fe2ae 100644 --- a/src/statistics/gnunet-service-statistics.c +++ b/src/statistics/gnunet-service-statistics.c | |||
@@ -118,7 +118,8 @@ load (struct GNUNET_SERVER_Handle *server, | |||
118 | GNUNET_free (fn); | 118 | GNUNET_free (fn); |
119 | return; | 119 | return; |
120 | } | 120 | } |
121 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); | 121 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, |
122 | GNUNET_DISK_PERM_NONE); | ||
122 | if (!fh) | 123 | if (!fh) |
123 | { | 124 | { |
124 | GNUNET_free (fn); | 125 | GNUNET_free (fn); |
diff --git a/src/util/bio.c b/src/util/bio.c index 8c3daed15..83afa34f3 100644 --- a/src/util/bio.c +++ b/src/util/bio.c | |||
@@ -56,7 +56,8 @@ GNUNET_BIO_read_open (const char *fn) | |||
56 | struct GNUNET_DISK_FileHandle *fd; | 56 | struct GNUNET_DISK_FileHandle *fd; |
57 | struct GNUNET_BIO_ReadHandle *h; | 57 | struct GNUNET_BIO_ReadHandle *h; |
58 | 58 | ||
59 | fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); | 59 | fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, |
60 | GNUNET_DISK_PERM_NONE); | ||
60 | if (NULL == fd) | 61 | if (NULL == fd) |
61 | return NULL; | 62 | return NULL; |
62 | h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); | 63 | h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); |
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index ec2a79602..c3fb9a84c 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -523,7 +523,8 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched, | |||
523 | } | 523 | } |
524 | fhc->run_on_shutdown = run_on_shutdown; | 524 | fhc->run_on_shutdown = run_on_shutdown; |
525 | fhc->fh = GNUNET_DISK_file_open (filename, | 525 | fhc->fh = GNUNET_DISK_file_open (filename, |
526 | GNUNET_DISK_OPEN_READ); | 526 | GNUNET_DISK_OPEN_READ, |
527 | GNUNET_DISK_PERM_NONE); | ||
527 | if (!fhc->fh) | 528 | if (!fhc->fh) |
528 | { | 529 | { |
529 | file_hash_finish (fhc, NULL); | 530 | file_hash_finish (fhc, NULL); |
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c index ef2e328e9..7bffa71ee 100644 --- a/src/util/crypto_rsa.c +++ b/src/util/crypto_rsa.c | |||
@@ -615,7 +615,8 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
615 | return ret; | 615 | return ret; |
616 | } | 616 | } |
617 | /* hostkey file exists already, read it! */ | 617 | /* hostkey file exists already, read it! */ |
618 | fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ); | 618 | fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, |
619 | GNUNET_DISK_PERM_NONE); | ||
619 | if (NULL == fd) | 620 | if (NULL == fd) |
620 | { | 621 | { |
621 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); | 622 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); |
diff --git a/src/util/disk.c b/src/util/disk.c index ee327e03f..46a71b48f 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -34,6 +34,13 @@ | |||
34 | #include "disk.h" | 34 | #include "disk.h" |
35 | 35 | ||
36 | 36 | ||
37 | /** | ||
38 | * Block size for IO for copying files. | ||
39 | */ | ||
40 | #define COPY_BLK_SIZE 65536 | ||
41 | |||
42 | |||
43 | |||
37 | #if LINUX || CYGWIN | 44 | #if LINUX || CYGWIN |
38 | #include <sys/vfs.h> | 45 | #include <sys/vfs.h> |
39 | #else | 46 | #else |
@@ -68,19 +75,53 @@ | |||
68 | #endif | 75 | #endif |
69 | #endif | 76 | #endif |
70 | #endif | 77 | #endif |
78 | #if LINUX | ||
79 | #include <sys/statvfs.h> | ||
80 | #endif | ||
81 | |||
71 | 82 | ||
72 | typedef struct | 83 | /** |
84 | * Handle used to manage a pipe. | ||
85 | */ | ||
86 | struct GNUNET_DISK_PipeHandle | ||
73 | { | 87 | { |
88 | /** | ||
89 | * File descriptors for the pipe. | ||
90 | */ | ||
91 | struct GNUNET_DISK_FileHandle * fd[2]; | ||
92 | }; | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Closure for the recursion to determine the file size | ||
97 | * of a directory. | ||
98 | */ | ||
99 | struct GetFileSizeData | ||
100 | { | ||
101 | /** | ||
102 | * Set to the total file size. | ||
103 | */ | ||
74 | uint64_t total; | 104 | uint64_t total; |
75 | int include_sym_links; | ||
76 | } GetFileSizeData; | ||
77 | 105 | ||
106 | /** | ||
107 | * GNUNET_YES if symbolic links should be included. | ||
108 | */ | ||
109 | int include_sym_links; | ||
110 | }; | ||
78 | 111 | ||
79 | 112 | ||
113 | /** | ||
114 | * Iterate over all files in the given directory and | ||
115 | * accumulate their size. | ||
116 | * | ||
117 | * @param cls closure of type "struct GetFileSizeData" | ||
118 | * @param fn current filename we are looking at | ||
119 | * @return GNUNET_SYSERR on serious errors, otherwise GNUNET_OK | ||
120 | */ | ||
80 | static int | 121 | static int |
81 | getSizeRec (void *ptr, const char *fn) | 122 | getSizeRec (void *cls, const char *fn) |
82 | { | 123 | { |
83 | GetFileSizeData *gfsd = ptr; | 124 | struct GetFileSizeData *gfsd = cls; |
84 | #ifdef HAVE_STAT64 | 125 | #ifdef HAVE_STAT64 |
85 | struct stat64 buf; | 126 | struct stat64 buf; |
86 | #else | 127 | #else |
@@ -112,8 +153,10 @@ getSizeRec (void *ptr, const char *fn) | |||
112 | return GNUNET_OK; | 153 | return GNUNET_OK; |
113 | } | 154 | } |
114 | 155 | ||
156 | |||
115 | /** | 157 | /** |
116 | * Checks whether a handle is invalid | 158 | * Checks whether a handle is invalid |
159 | * | ||
117 | * @param h handle to check | 160 | * @param h handle to check |
118 | * @return GNUNET_YES if invalid, GNUNET_NO if valid | 161 | * @return GNUNET_YES if invalid, GNUNET_NO if valid |
119 | */ | 162 | */ |
@@ -121,15 +164,16 @@ int | |||
121 | GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | 164 | GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) |
122 | { | 165 | { |
123 | #ifdef MINGW | 166 | #ifdef MINGW |
124 | return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO; | 167 | return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; |
125 | #else | 168 | #else |
126 | return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO; | 169 | return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; |
127 | #endif | 170 | #endif |
128 | } | 171 | } |
129 | 172 | ||
130 | 173 | ||
131 | /** | 174 | /** |
132 | * Move the read/write pointer in a file | 175 | * Move the read/write pointer in a file |
176 | * | ||
133 | * @param h handle of an open file | 177 | * @param h handle of an open file |
134 | * @param offset position to move to | 178 | * @param offset position to move to |
135 | * @param whence specification to which position the offset parameter relates to | 179 | * @param whence specification to which position the offset parameter relates to |
@@ -137,7 +181,7 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | |||
137 | */ | 181 | */ |
138 | off_t | 182 | off_t |
139 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset, | 183 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset, |
140 | enum GNUNET_DISK_Seek whence) | 184 | enum GNUNET_DISK_Seek whence) |
141 | { | 185 | { |
142 | if (h == NULL) | 186 | if (h == NULL) |
143 | { | 187 | { |
@@ -165,10 +209,17 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, off_t offset, | |||
165 | #endif | 209 | #endif |
166 | } | 210 | } |
167 | 211 | ||
212 | |||
168 | /** | 213 | /** |
169 | * Get the size of the file (or directory) | 214 | * Get the size of the file (or directory) of the given file (in |
170 | * of the given file (in bytes). | 215 | * bytes). |
171 | * | 216 | * |
217 | * @param filename name of the file or directory | ||
218 | * @param size set to the size of the file (or, | ||
219 | * in the case of directories, the sum | ||
220 | * of all sizes of files in the directory) | ||
221 | * @param includeSymLinks should symbolic links be | ||
222 | * included? | ||
172 | * @return GNUNET_SYSERR on error, GNUNET_OK on success | 223 | * @return GNUNET_SYSERR on error, GNUNET_OK on success |
173 | */ | 224 | */ |
174 | int | 225 | int |
@@ -176,7 +227,7 @@ GNUNET_DISK_file_size (const char *filename, | |||
176 | uint64_t *size, | 227 | uint64_t *size, |
177 | int includeSymLinks) | 228 | int includeSymLinks) |
178 | { | 229 | { |
179 | GetFileSizeData gfsd; | 230 | struct GetFileSizeData gfsd; |
180 | int ret; | 231 | int ret; |
181 | 232 | ||
182 | GNUNET_assert (size != NULL); | 233 | GNUNET_assert (size != NULL); |
@@ -188,15 +239,15 @@ GNUNET_DISK_file_size (const char *filename, | |||
188 | } | 239 | } |
189 | 240 | ||
190 | 241 | ||
191 | |||
192 | #if LINUX | ||
193 | #include <sys/statvfs.h> | ||
194 | #endif | ||
195 | |||
196 | |||
197 | |||
198 | /** | 242 | /** |
199 | * FIXME. | 243 | * Obtain some unique identifiers for the given file |
244 | * that can be used to identify it in the local system. | ||
245 | * This function is used between GNUnet processes to | ||
246 | * quickly check if two files with the same absolute path | ||
247 | * are actually identical. The two processes represent | ||
248 | * the same peer but may communicate over the network | ||
249 | * (and the file may be on an NFS volume). This function | ||
250 | * may not be supported on all operating systems. | ||
200 | * | 251 | * |
201 | * @param filename name of the file | 252 | * @param filename name of the file |
202 | * @param dev set to the device ID | 253 | * @param dev set to the device ID |
@@ -322,9 +373,14 @@ GNUNET_DISK_get_blocks_available (const char *part) | |||
322 | #endif | 373 | #endif |
323 | } | 374 | } |
324 | 375 | ||
376 | |||
325 | /** | 377 | /** |
326 | * Test if fil is a directory. | 378 | * Test if "fil" is a directory. |
379 | * Will not print an error message if the directory | ||
380 | * does not exist. Will log errors if GNUNET_SYSERR is | ||
381 | * returned (i.e., a file exists with the same name). | ||
327 | * | 382 | * |
383 | * @param fil filename to test | ||
328 | * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it | 384 | * @return GNUNET_YES if yes, GNUNET_NO if not, GNUNET_SYSERR if it |
329 | * does not exist | 385 | * does not exist |
330 | */ | 386 | */ |
@@ -357,7 +413,9 @@ GNUNET_DISK_directory_test (const char *fil) | |||
357 | /** | 413 | /** |
358 | * Check that fil corresponds to a filename | 414 | * Check that fil corresponds to a filename |
359 | * (of a file that exists and that is not a directory). | 415 | * (of a file that exists and that is not a directory). |
360 | * @returns GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something | 416 | * |
417 | * @param fil filename to check | ||
418 | * @return GNUNET_YES if yes, GNUNET_NO if not a file, GNUNET_SYSERR if something | ||
361 | * else (will print an error message in that case, too). | 419 | * else (will print an error message in that case, too). |
362 | */ | 420 | */ |
363 | int | 421 | int |
@@ -398,6 +456,7 @@ GNUNET_DISK_file_test (const char *fil) | |||
398 | return GNUNET_YES; | 456 | return GNUNET_YES; |
399 | } | 457 | } |
400 | 458 | ||
459 | |||
401 | /** | 460 | /** |
402 | * Implementation of "mkdir -p" | 461 | * Implementation of "mkdir -p" |
403 | * @param dir the directory to create | 462 | * @param dir the directory to create |
@@ -504,6 +563,7 @@ GNUNET_DISK_directory_create_for_file (const char *dir) | |||
504 | return ret; | 563 | return ret; |
505 | } | 564 | } |
506 | 565 | ||
566 | |||
507 | /** | 567 | /** |
508 | * Read the contents of a binary file into a buffer. | 568 | * Read the contents of a binary file into a buffer. |
509 | * @param h handle to an open file | 569 | * @param h handle to an open file |
@@ -545,14 +605,15 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, void *result, | |||
545 | * @return number of bytes read, GNUNET_SYSERR on failure | 605 | * @return number of bytes read, GNUNET_SYSERR on failure |
546 | */ | 606 | */ |
547 | ssize_t | 607 | ssize_t |
548 | GNUNET_DISK_fn_read (const char * const fn, | 608 | GNUNET_DISK_fn_read (const char * fn, |
549 | void *result, | 609 | void *result, |
550 | size_t len) | 610 | size_t len) |
551 | { | 611 | { |
552 | struct GNUNET_DISK_FileHandle *fh; | 612 | struct GNUNET_DISK_FileHandle *fh; |
553 | ssize_t ret; | 613 | ssize_t ret; |
554 | 614 | ||
555 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ); | 615 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, |
616 | GNUNET_DISK_PERM_NONE); | ||
556 | if (!fh) | 617 | if (!fh) |
557 | return GNUNET_SYSERR; | 618 | return GNUNET_SYSERR; |
558 | ret = GNUNET_DISK_file_read (fh, result, len); | 619 | ret = GNUNET_DISK_file_read (fh, result, len); |
@@ -595,16 +656,18 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, const void *buff | |||
595 | 656 | ||
596 | /** | 657 | /** |
597 | * Write a buffer to a file. If the file is longer than the | 658 | * Write a buffer to a file. If the file is longer than the |
598 | * number of bytes that will be written, iit will be truncated. | 659 | * number of bytes that will be written, it will be truncated. |
599 | * | 660 | * |
600 | * @param fn file name | 661 | * @param fn file name |
601 | * @param buffer the data to write | 662 | * @param buffer the data to write |
602 | * @param n number of bytes to write | 663 | * @param n number of bytes to write |
664 | * @param mode file permissions | ||
603 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 665 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
604 | */ | 666 | */ |
605 | ssize_t | 667 | ssize_t |
606 | GNUNET_DISK_fn_write (const char * const fn, const void *buffer, | 668 | GNUNET_DISK_fn_write (const char * fn, const void *buffer, |
607 | size_t n, int mode) | 669 | size_t n, |
670 | enum GNUNET_DISK_AccessPermissions mode) | ||
608 | { | 671 | { |
609 | struct GNUNET_DISK_FileHandle *fh; | 672 | struct GNUNET_DISK_FileHandle *fh; |
610 | int ret; | 673 | int ret; |
@@ -622,18 +685,19 @@ GNUNET_DISK_fn_write (const char * const fn, const void *buffer, | |||
622 | } | 685 | } |
623 | 686 | ||
624 | /** | 687 | /** |
625 | * Scan a directory for files. The name of the directory | 688 | * Scan a directory for files. |
626 | * must be expanded first (!). | 689 | * |
627 | * @param dirName the name of the directory | 690 | * @param dirName the name of the directory |
628 | * @param callback the method to call for each file, | 691 | * @param callback the method to call for each file, |
629 | * can be NULL, in that case, we only count | 692 | * can be NULL, in that case, we only count |
630 | * @param data argument to pass to callback | 693 | * @param callback_cls closure for callback |
631 | * @return the number of files found, GNUNET_SYSERR on error or | 694 | * @return the number of files found, GNUNET_SYSERR on error or |
632 | * ieration aborted by callback returning GNUNET_SYSERR | 695 | * ieration aborted by callback returning GNUNET_SYSERR |
633 | */ | 696 | */ |
634 | int | 697 | int |
635 | GNUNET_DISK_directory_scan (const char *dirName, | 698 | GNUNET_DISK_directory_scan (const char *dirName, |
636 | GNUNET_FileNameCallback callback, void *data) | 699 | GNUNET_FileNameCallback callback, |
700 | void *callback_cls) | ||
637 | { | 701 | { |
638 | DIR *dinfo; | 702 | DIR *dinfo; |
639 | struct dirent *finfo; | 703 | struct dirent *finfo; |
@@ -697,7 +761,7 @@ GNUNET_DISK_directory_scan (const char *dirName, | |||
697 | dname, | 761 | dname, |
698 | (strcmp (dname, DIR_SEPARATOR_STR) == | 762 | (strcmp (dname, DIR_SEPARATOR_STR) == |
699 | 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); | 763 | 0) ? "" : DIR_SEPARATOR_STR, finfo->d_name); |
700 | if (GNUNET_OK != callback (data, name)) | 764 | if (GNUNET_OK != callback (callback_cls, name)) |
701 | { | 765 | { |
702 | closedir (dinfo); | 766 | closedir (dinfo); |
703 | GNUNET_free (name); | 767 | GNUNET_free (name); |
@@ -856,6 +920,14 @@ GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched, | |||
856 | } | 920 | } |
857 | 921 | ||
858 | 922 | ||
923 | /** | ||
924 | * Function that removes the given directory by calling | ||
925 | * "GNUNET_DISK_directory_remove". | ||
926 | * | ||
927 | * @param unused not used | ||
928 | * @param fn directory to remove | ||
929 | * @return GNUNET_OK | ||
930 | */ | ||
859 | static int | 931 | static int |
860 | remove_helper (void *unused, const char *fn) | 932 | remove_helper (void *unused, const char *fn) |
861 | { | 933 | { |
@@ -863,6 +935,7 @@ remove_helper (void *unused, const char *fn) | |||
863 | return GNUNET_OK; | 935 | return GNUNET_OK; |
864 | } | 936 | } |
865 | 937 | ||
938 | |||
866 | /** | 939 | /** |
867 | * Remove all files in a directory (rm -rf). Call with | 940 | * Remove all files in a directory (rm -rf). Call with |
868 | * caution. | 941 | * caution. |
@@ -890,7 +963,9 @@ GNUNET_DISK_directory_remove (const char *fileName) | |||
890 | return GNUNET_SYSERR; | 963 | return GNUNET_SYSERR; |
891 | } | 964 | } |
892 | if (GNUNET_SYSERR == | 965 | if (GNUNET_SYSERR == |
893 | GNUNET_DISK_directory_scan (fileName, remove_helper, NULL)) | 966 | GNUNET_DISK_directory_scan (fileName, |
967 | &remove_helper, | ||
968 | NULL)) | ||
894 | return GNUNET_SYSERR; | 969 | return GNUNET_SYSERR; |
895 | if (0 != RMDIR (fileName)) | 970 | if (0 != RMDIR (fileName)) |
896 | { | 971 | { |
@@ -900,10 +975,12 @@ GNUNET_DISK_directory_remove (const char *fileName) | |||
900 | return GNUNET_OK; | 975 | return GNUNET_OK; |
901 | } | 976 | } |
902 | 977 | ||
903 | #define COPY_BLK_SIZE 65536 | ||
904 | 978 | ||
905 | /** | 979 | /** |
906 | * Copy a file. | 980 | * Copy a file. |
981 | * | ||
982 | * @param src file to copy | ||
983 | * @param dst destination file name | ||
907 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | 984 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
908 | */ | 985 | */ |
909 | int | 986 | int |
@@ -919,7 +996,8 @@ GNUNET_DISK_file_copy (const char *src, const char *dst) | |||
919 | if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES)) | 996 | if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES)) |
920 | return GNUNET_SYSERR; | 997 | return GNUNET_SYSERR; |
921 | pos = 0; | 998 | pos = 0; |
922 | in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ); | 999 | in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, |
1000 | GNUNET_DISK_PERM_NONE); | ||
923 | if (!in) | 1001 | if (!in) |
924 | return GNUNET_SYSERR; | 1002 | return GNUNET_SYSERR; |
925 | out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE | 1003 | out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE |
@@ -984,6 +1062,10 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
984 | 1062 | ||
985 | /** | 1063 | /** |
986 | * @brief Change owner of a file | 1064 | * @brief Change owner of a file |
1065 | * | ||
1066 | * @param filename name of file to change the owner of | ||
1067 | * @param user name of the new owner | ||
1068 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | ||
987 | */ | 1069 | */ |
988 | int | 1070 | int |
989 | GNUNET_DISK_file_change_owner (const char *filename, const char *user) | 1071 | GNUNET_DISK_file_change_owner (const char *filename, const char *user) |
@@ -1097,16 +1179,21 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, off_t unlockStart, | |||
1097 | 1179 | ||
1098 | 1180 | ||
1099 | /** | 1181 | /** |
1100 | * Open a file | 1182 | * Open a file. Note that the access permissions will only be |
1183 | * used if a new file is created and if the underlying operating | ||
1184 | * system supports the given permissions. | ||
1185 | * | ||
1101 | * @param fn file name to be opened | 1186 | * @param fn file name to be opened |
1102 | * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags | 1187 | * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags |
1103 | * @param ... permissions for the newly created file | 1188 | * @param perm permissions for the newly created file, use |
1189 | * GNUNET_DISK_PERM_USER_NONE if a file could not be created by this | ||
1190 | * call (because of flags) | ||
1104 | * @return IO handle on success, NULL on error | 1191 | * @return IO handle on success, NULL on error |
1105 | */ | 1192 | */ |
1106 | struct GNUNET_DISK_FileHandle * | 1193 | struct GNUNET_DISK_FileHandle * |
1107 | GNUNET_DISK_file_open (const char *fn, | 1194 | GNUNET_DISK_file_open (const char *fn, |
1108 | enum GNUNET_DISK_OpenFlags flags, | 1195 | enum GNUNET_DISK_OpenFlags flags, |
1109 | ...) | 1196 | enum GNUNET_DISK_AccessPermissions perm) |
1110 | { | 1197 | { |
1111 | char *expfn; | 1198 | char *expfn; |
1112 | struct GNUNET_DISK_FileHandle *ret; | 1199 | struct GNUNET_DISK_FileHandle *ret; |
@@ -1144,15 +1231,7 @@ GNUNET_DISK_file_open (const char *fn, | |||
1144 | oflags |= O_APPEND; | 1231 | oflags |= O_APPEND; |
1145 | if (flags & GNUNET_DISK_OPEN_CREATE) | 1232 | if (flags & GNUNET_DISK_OPEN_CREATE) |
1146 | { | 1233 | { |
1147 | int perm; | ||
1148 | |||
1149 | oflags |= O_CREAT; | 1234 | oflags |= O_CREAT; |
1150 | |||
1151 | va_list arg; | ||
1152 | va_start (arg, flags); | ||
1153 | perm = va_arg (arg, int); | ||
1154 | va_end (arg); | ||
1155 | |||
1156 | if (perm & GNUNET_DISK_PERM_USER_READ) | 1235 | if (perm & GNUNET_DISK_PERM_USER_READ) |
1157 | mode |= S_IRUSR; | 1236 | mode |= S_IRUSR; |
1158 | if (perm & GNUNET_DISK_PERM_USER_WRITE) | 1237 | if (perm & GNUNET_DISK_PERM_USER_WRITE) |
@@ -1243,6 +1322,7 @@ GNUNET_DISK_file_open (const char *fn, | |||
1243 | return ret; | 1322 | return ret; |
1244 | } | 1323 | } |
1245 | 1324 | ||
1325 | |||
1246 | /** | 1326 | /** |
1247 | * Close an open file | 1327 | * Close an open file |
1248 | * @param h file handle | 1328 | * @param h file handle |
@@ -1277,6 +1357,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) | |||
1277 | return GNUNET_OK; | 1357 | return GNUNET_OK; |
1278 | } | 1358 | } |
1279 | 1359 | ||
1360 | |||
1280 | /** | 1361 | /** |
1281 | * Construct full path to a file inside of the private | 1362 | * Construct full path to a file inside of the private |
1282 | * directory used by GNUnet. Also creates the corresponding | 1363 | * directory used by GNUnet. Also creates the corresponding |
@@ -1347,12 +1428,26 @@ GNUNET_DISK_get_home_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1347 | return ret; | 1428 | return ret; |
1348 | } | 1429 | } |
1349 | 1430 | ||
1431 | |||
1432 | /** | ||
1433 | * Handle for a memory-mapping operation. | ||
1434 | */ | ||
1350 | struct GNUNET_DISK_MapHandle | 1435 | struct GNUNET_DISK_MapHandle |
1351 | { | 1436 | { |
1352 | #ifdef MINGW | 1437 | #ifdef MINGW |
1438 | /** | ||
1439 | * Underlying OS handle. | ||
1440 | */ | ||
1353 | HANDLE h; | 1441 | HANDLE h; |
1354 | #else | 1442 | #else |
1443 | /** | ||
1444 | * Address where the map is in memory. | ||
1445 | */ | ||
1355 | void *addr; | 1446 | void *addr; |
1447 | |||
1448 | /** | ||
1449 | * Number of bytes mapped. | ||
1450 | */ | ||
1356 | size_t len; | 1451 | size_t len; |
1357 | #endif | 1452 | #endif |
1358 | }; | 1453 | }; |
@@ -1360,6 +1455,7 @@ struct GNUNET_DISK_MapHandle | |||
1360 | 1455 | ||
1361 | /** | 1456 | /** |
1362 | * Map a file into memory | 1457 | * Map a file into memory |
1458 | * | ||
1363 | * @param h open file handle | 1459 | * @param h open file handle |
1364 | * @param m handle to the new mapping | 1460 | * @param m handle to the new mapping |
1365 | * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx | 1461 | * @param access access specification, GNUNET_DISK_MAP_TYPE_xxx |
@@ -1505,11 +1601,14 @@ struct GNUNET_DISK_PipeHandle * | |||
1505 | GNUNET_DISK_pipe (int blocking) | 1601 | GNUNET_DISK_pipe (int blocking) |
1506 | { | 1602 | { |
1507 | struct GNUNET_DISK_PipeHandle *p; | 1603 | struct GNUNET_DISK_PipeHandle *p; |
1604 | struct GNUNET_DISK_FileHandle *fds; | ||
1508 | int err; | 1605 | int err; |
1509 | 1606 | ||
1510 | err = GNUNET_NO; | 1607 | err = GNUNET_NO; |
1511 | p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle)); | 1608 | p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle) + 2 * sizeof (struct GNUNET_DISK_FileHandle)); |
1512 | 1609 | fds = (struct GNUNET_DISK_FileHandle *) &p[1]; | |
1610 | p->fd[0] = &fds[0]; | ||
1611 | p->fd[1] = &fds[1]; | ||
1513 | #ifndef MINGW | 1612 | #ifndef MINGW |
1514 | int fd[2]; | 1613 | int fd[2]; |
1515 | int ret; | 1614 | int ret; |
@@ -1517,67 +1616,60 @@ GNUNET_DISK_pipe (int blocking) | |||
1517 | int eno; | 1616 | int eno; |
1518 | 1617 | ||
1519 | ret = pipe (fd); | 1618 | ret = pipe (fd); |
1520 | if (ret != -1) | 1619 | if (ret == -1) |
1521 | { | 1620 | { |
1522 | p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | 1621 | err = errno; |
1523 | p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | 1622 | GNUNET_free (p); |
1524 | p->fd[0]->fd = fd[0]; | 1623 | errno = err; |
1525 | p->fd[1]->fd = fd[1]; | 1624 | return NULL; |
1526 | 1625 | } | |
1527 | if (!blocking) | 1626 | p->fd[0]->fd = fd[0]; |
1528 | { | 1627 | p->fd[1]->fd = fd[1]; |
1529 | flags = fcntl (fd[0], F_GETFL); | 1628 | if (!blocking) |
1530 | flags |= O_NONBLOCK; | 1629 | { |
1531 | ret = fcntl (fd[0], F_SETFL, flags); | 1630 | flags = fcntl (fd[0], F_GETFL); |
1532 | if (ret != -1) | 1631 | flags |= O_NONBLOCK; |
1533 | { | 1632 | ret = fcntl (fd[0], F_SETFL, flags); |
1534 | flags = fcntl (fd[1], F_GETFL); | 1633 | if (ret != -1) |
1535 | flags |= O_NONBLOCK; | 1634 | { |
1536 | ret = fcntl (fd[1], F_SETFL, flags); | 1635 | flags = fcntl (fd[1], F_GETFL); |
1537 | } | 1636 | flags |= O_NONBLOCK; |
1538 | if (ret == -1) | 1637 | ret = fcntl (fd[1], F_SETFL, flags); |
1539 | { | 1638 | } |
1540 | eno = errno; | 1639 | if (ret == -1) |
1541 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl"); | 1640 | { |
1542 | GNUNET_DISK_file_close (p->fd[0]); | 1641 | eno = errno; |
1543 | GNUNET_DISK_file_close (p->fd[1]); | 1642 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl"); |
1544 | p->fd[0] = NULL; | 1643 | close (p->fd[0]->fd); |
1545 | p->fd[1] = NULL; | 1644 | close (p->fd[1]->fd); |
1546 | err = GNUNET_YES; | 1645 | err = GNUNET_YES; |
1547 | errno = eno; | 1646 | GNUNET_free (p); |
1548 | } | 1647 | errno = eno; |
1549 | } | 1648 | return NULL; |
1649 | } | ||
1550 | } | 1650 | } |
1551 | else | ||
1552 | err = GNUNET_YES; | ||
1553 | #else | 1651 | #else |
1554 | BOOL ret; | 1652 | BOOL ret; |
1555 | 1653 | ||
1556 | ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0); | 1654 | ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0); |
1557 | if (ret) | 1655 | if (! ret) |
1558 | { | 1656 | { |
1559 | if (!blocking) | 1657 | GNUNET_free (p); |
1560 | { | 1658 | SetErrnoFromWinError (GetLastError ()); |
1561 | DWORD mode; | 1659 | return NULL; |
1562 | |||
1563 | mode = PIPE_NOWAIT; | ||
1564 | p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | ||
1565 | p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | ||
1566 | SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); | ||
1567 | SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); | ||
1568 | /* this always fails on Windows 95, so we don't care about error handling */ | ||
1569 | } | ||
1570 | } | 1660 | } |
1571 | else | 1661 | if (!blocking) |
1572 | err = GNUNET_YES; | ||
1573 | #endif | ||
1574 | |||
1575 | if (GNUNET_YES == err) | ||
1576 | { | 1662 | { |
1577 | GNUNET_free (p); | 1663 | DWORD mode; |
1578 | p = NULL; | 1664 | |
1665 | mode = PIPE_NOWAIT; | ||
1666 | p->fd[0] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | ||
1667 | p->fd[1] = GNUNET_malloc(sizeof(struct GNUNET_DISK_FileHandle)); | ||
1668 | SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL); | ||
1669 | SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL); | ||
1670 | /* this always fails on Windows 95, so we don't care about error handling */ | ||
1579 | } | 1671 | } |
1580 | 1672 | #endif | |
1581 | return p; | 1673 | return p; |
1582 | } | 1674 | } |
1583 | 1675 | ||
@@ -1592,37 +1684,35 @@ int | |||
1592 | GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | 1684 | GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) |
1593 | { | 1685 | { |
1594 | int ret = GNUNET_OK; | 1686 | int ret = GNUNET_OK; |
1687 | int save; | ||
1688 | |||
1595 | #ifdef MINGW | 1689 | #ifdef MINGW |
1596 | if (!CloseHandle (p->fd[0]->h)) | 1690 | if (!CloseHandle (p->fd[0]->h)) |
1597 | { | 1691 | { |
1598 | SetErrnoFromWinError (GetLastError ()); | 1692 | SetErrnoFromWinError (GetLastError ()); |
1599 | ret = GNUNET_SYSERR; | 1693 | ret = GNUNET_SYSERR; |
1600 | } | 1694 | } |
1601 | |||
1602 | if (!CloseHandle (p->fd[1]->h)) | 1695 | if (!CloseHandle (p->fd[1]->h)) |
1603 | { | 1696 | { |
1604 | SetErrnoFromWinError (GetLastError ()); | 1697 | SetErrnoFromWinError (GetLastError ()); |
1605 | ret = GNUNET_SYSERR; | 1698 | ret = GNUNET_SYSERR; |
1606 | } | 1699 | } |
1700 | save = errno; | ||
1607 | #else | 1701 | #else |
1608 | int save; | 1702 | save = 0; |
1609 | |||
1610 | if (0 != close (p->fd[0]->fd)) | 1703 | if (0 != close (p->fd[0]->fd)) |
1611 | { | 1704 | { |
1612 | ret = GNUNET_SYSERR; | 1705 | ret = GNUNET_SYSERR; |
1613 | save = errno; | 1706 | save = errno; |
1614 | } | 1707 | } |
1615 | else | ||
1616 | save = 0; | ||
1617 | |||
1618 | if (0 != close (p->fd[1]->fd)) | 1708 | if (0 != close (p->fd[1]->fd)) |
1619 | ret = GNUNET_SYSERR; | 1709 | { |
1620 | else | 1710 | ret = GNUNET_SYSERR; |
1621 | errno = save; | 1711 | save = errno; |
1712 | } | ||
1622 | #endif | 1713 | #endif |
1623 | GNUNET_free (p->fd[0]); | ||
1624 | GNUNET_free (p->fd[1]); | ||
1625 | GNUNET_free (p); | 1714 | GNUNET_free (p); |
1715 | errno = save; | ||
1626 | return ret; | 1716 | return ret; |
1627 | } | 1717 | } |
1628 | 1718 | ||
@@ -1630,14 +1720,24 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | |||
1630 | /** | 1720 | /** |
1631 | * Get the handle to a particular pipe end | 1721 | * Get the handle to a particular pipe end |
1632 | * @param p pipe | 1722 | * @param p pipe |
1633 | * @param n number of the end | 1723 | * @param n end to access |
1634 | */ | 1724 | */ |
1635 | const struct GNUNET_DISK_FileHandle * | 1725 | const struct GNUNET_DISK_FileHandle * |
1636 | GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n) | 1726 | GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, |
1727 | enum GNUNET_DISK_PipeEnd n) | ||
1637 | { | 1728 | { |
1638 | return p->fd[n]; | 1729 | switch (n) |
1730 | { | ||
1731 | case GNUNET_DISK_PIPE_END_READ: | ||
1732 | case GNUNET_DISK_PIPE_END_WRITE: | ||
1733 | return p->fd[n]; | ||
1734 | default: | ||
1735 | GNUNET_break (0); | ||
1736 | return NULL; | ||
1737 | } | ||
1639 | } | 1738 | } |
1640 | 1739 | ||
1740 | |||
1641 | /** | 1741 | /** |
1642 | * Retrieve OS file handle | 1742 | * Retrieve OS file handle |
1643 | * @internal | 1743 | * @internal |
diff --git a/src/util/server.c b/src/util/server.c index a2d5551a4..7214b1939 100644 --- a/src/util/server.c +++ b/src/util/server.c | |||
@@ -324,7 +324,8 @@ process_listen_socket (void *cls, | |||
324 | destroy_server (server); | 324 | destroy_server (server); |
325 | return; | 325 | return; |
326 | } | 326 | } |
327 | shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, 0); | 327 | shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, |
328 | GNUNET_DISK_PIPE_END_READ); | ||
328 | GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket)); | 329 | GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket)); |
329 | GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe)); | 330 | GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe)); |
330 | sock = GNUNET_CONNECTION_create_from_accept (tc->sched, | 331 | sock = GNUNET_CONNECTION_create_from_accept (tc->sched, |
@@ -476,7 +477,8 @@ GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched, | |||
476 | { | 477 | { |
477 | r = GNUNET_NETWORK_fdset_create (); | 478 | r = GNUNET_NETWORK_fdset_create (); |
478 | GNUNET_NETWORK_fdset_set (r, ret->listen_socket); | 479 | GNUNET_NETWORK_fdset_set (r, ret->listen_socket); |
479 | GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, 0)); | 480 | GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, |
481 | GNUNET_DISK_PIPE_END_READ)); | ||
480 | GNUNET_SCHEDULER_add_select (sched, | 482 | GNUNET_SCHEDULER_add_select (sched, |
481 | GNUNET_YES, | 483 | GNUNET_YES, |
482 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 484 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
@@ -503,7 +505,10 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) | |||
503 | if (s->listen_socket == NULL) | 505 | if (s->listen_socket == NULL) |
504 | destroy_server (s); | 506 | destroy_server (s); |
505 | else | 507 | else |
506 | GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, 1), &c, 1)); | 508 | GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, |
509 | GNUNET_DISK_PIPE_END_WRITE), | ||
510 | &c, | ||
511 | sizeof(c))); | ||
507 | } | 512 | } |
508 | 513 | ||
509 | 514 | ||
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c index 811389953..b19ccff2d 100644 --- a/src/util/test_scheduler.c +++ b/src/util/test_scheduler.c | |||
@@ -116,8 +116,8 @@ task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
116 | (*ok) = 6; | 116 | (*ok) = 6; |
117 | p = GNUNET_DISK_pipe (GNUNET_NO); | 117 | p = GNUNET_DISK_pipe (GNUNET_NO); |
118 | GNUNET_assert (NULL != p); | 118 | GNUNET_assert (NULL != p); |
119 | fds[0] = GNUNET_DISK_pipe_handle (p, 0); | 119 | fds[0] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_READ); |
120 | fds[1] = GNUNET_DISK_pipe_handle (p, 1); | 120 | fds[1] = GNUNET_DISK_pipe_handle (p, GNUNET_DISK_PIPE_END_WRITE); |
121 | GNUNET_SCHEDULER_add_read_file (tc->sched, | 121 | GNUNET_SCHEDULER_add_read_file (tc->sched, |
122 | GNUNET_NO, | 122 | GNUNET_NO, |
123 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 123 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, |