diff options
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 2507 |
1 files changed, 1256 insertions, 1251 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index 959cb62cf..450a19ab1 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file util/disk.c | 21 | * @file util/disk.c |
22 | * @brief disk IO convenience methods | 22 | * @brief disk IO convenience methods |
@@ -28,13 +28,13 @@ | |||
28 | #include "gnunet_strings_lib.h" | 28 | #include "gnunet_strings_lib.h" |
29 | #include "gnunet_disk_lib.h" | 29 | #include "gnunet_disk_lib.h" |
30 | 30 | ||
31 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-disk", __VA_ARGS__) | 31 | #define LOG(kind, ...) GNUNET_log_from(kind, "util-disk", __VA_ARGS__) |
32 | 32 | ||
33 | #define LOG_STRERROR(kind, syscall) \ | 33 | #define LOG_STRERROR(kind, syscall) \ |
34 | GNUNET_log_from_strerror (kind, "util-disk", syscall) | 34 | GNUNET_log_from_strerror(kind, "util-disk", syscall) |
35 | 35 | ||
36 | #define LOG_STRERROR_FILE(kind, syscall, filename) \ | 36 | #define LOG_STRERROR_FILE(kind, syscall, filename) \ |
37 | GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename) | 37 | GNUNET_log_from_strerror_file(kind, "util-disk", syscall, filename) |
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Block size for IO for copying files. | 40 | * Block size for IO for copying files. |
@@ -58,15 +58,14 @@ | |||
58 | #ifndef S_ISLNK | 58 | #ifndef S_ISLNK |
59 | #define _IFMT 0170000 /* type of file */ | 59 | #define _IFMT 0170000 /* type of file */ |
60 | #define _IFLNK 0120000 /* symbolic link */ | 60 | #define _IFLNK 0120000 /* symbolic link */ |
61 | #define S_ISLNK(m) (((m) &_IFMT) == _IFLNK) | 61 | #define S_ISLNK(m) (((m) & _IFMT) == _IFLNK) |
62 | #endif | 62 | #endif |
63 | 63 | ||
64 | 64 | ||
65 | /** | 65 | /** |
66 | * Handle used to manage a pipe. | 66 | * Handle used to manage a pipe. |
67 | */ | 67 | */ |
68 | struct GNUNET_DISK_PipeHandle | 68 | struct GNUNET_DISK_PipeHandle { |
69 | { | ||
70 | /** | 69 | /** |
71 | * File descriptors for the pipe. | 70 | * File descriptors for the pipe. |
72 | * One or both of them could be NULL. | 71 | * One or both of them could be NULL. |
@@ -79,8 +78,7 @@ struct GNUNET_DISK_PipeHandle | |||
79 | * Closure for the recursion to determine the file size | 78 | * Closure for the recursion to determine the file size |
80 | * of a directory. | 79 | * of a directory. |
81 | */ | 80 | */ |
82 | struct GetFileSizeData | 81 | struct GetFileSizeData { |
83 | { | ||
84 | /** | 82 | /** |
85 | * Set to the total file size. | 83 | * Set to the total file size. |
86 | */ | 84 | */ |
@@ -107,7 +105,7 @@ struct GetFileSizeData | |||
107 | * @return file permissions, UNIX style | 105 | * @return file permissions, UNIX style |
108 | */ | 106 | */ |
109 | static int | 107 | static int |
110 | translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm) | 108 | translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm) |
111 | { | 109 | { |
112 | int mode; | 110 | int mode; |
113 | 111 | ||
@@ -145,41 +143,41 @@ translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm) | |||
145 | * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK | 143 | * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK |
146 | */ | 144 | */ |
147 | static int | 145 | static int |
148 | getSizeRec (void *cls, const char *fn) | 146 | getSizeRec(void *cls, const char *fn) |
149 | { | 147 | { |
150 | struct GetFileSizeData *gfsd = cls; | 148 | struct GetFileSizeData *gfsd = cls; |
151 | 149 | ||
152 | #if defined(HAVE_STAT64) && \ | 150 | #if defined(HAVE_STAT64) && \ |
153 | ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) | 151 | !(defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) |
154 | STRUCT_STAT64 buf; | 152 | STRUCT_STAT64 buf; |
155 | 153 | ||
156 | if (0 != stat64 (fn, &buf)) | 154 | if (0 != stat64(fn, &buf)) |
157 | { | 155 | { |
158 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn); | 156 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat64", fn); |
159 | return GNUNET_SYSERR; | 157 | return GNUNET_SYSERR; |
160 | } | 158 | } |
161 | #else | 159 | #else |
162 | struct stat buf; | 160 | struct stat buf; |
163 | 161 | ||
164 | if (0 != stat (fn, &buf)) | 162 | if (0 != stat(fn, &buf)) |
165 | { | 163 | { |
166 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn); | 164 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat", fn); |
167 | return GNUNET_SYSERR; | 165 | return GNUNET_SYSERR; |
168 | } | 166 | } |
169 | #endif | 167 | #endif |
170 | if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES)) | 168 | if ((S_ISDIR(buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES)) |
171 | { | 169 | { |
172 | errno = EISDIR; | 170 | errno = EISDIR; |
173 | return GNUNET_SYSERR; | ||
174 | } | ||
175 | if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) | ||
176 | gfsd->total += buf.st_size; | ||
177 | if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) && | ||
178 | ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) | ||
179 | { | ||
180 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) | ||
181 | return GNUNET_SYSERR; | 171 | return GNUNET_SYSERR; |
182 | } | 172 | } |
173 | if ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) | ||
174 | gfsd->total += buf.st_size; | ||
175 | if ((S_ISDIR(buf.st_mode)) && (0 == access(fn, X_OK)) && | ||
176 | ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) | ||
177 | { | ||
178 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan(fn, &getSizeRec, gfsd)) | ||
179 | return GNUNET_SYSERR; | ||
180 | } | ||
183 | return GNUNET_OK; | 181 | return GNUNET_OK; |
184 | } | 182 | } |
185 | 183 | ||
@@ -191,12 +189,12 @@ getSizeRec (void *cls, const char *fn) | |||
191 | * @return #GNUNET_YES if invalid, #GNUNET_NO if valid | 189 | * @return #GNUNET_YES if invalid, #GNUNET_NO if valid |
192 | */ | 190 | */ |
193 | int | 191 | int |
194 | GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | 192 | GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h) |
195 | { | 193 | { |
196 | #ifdef MINGW | 194 | #ifdef MINGW |
197 | return ((! h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; | 195 | return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; |
198 | #else | 196 | #else |
199 | return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; | 197 | return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; |
200 | #endif | 198 | #endif |
201 | } | 199 | } |
202 | 200 | ||
@@ -208,22 +206,22 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | |||
208 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 206 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
209 | */ | 207 | */ |
210 | int | 208 | int |
211 | GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size) | 209 | GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size) |
212 | { | 210 | { |
213 | #if WINDOWS | 211 | #if WINDOWS |
214 | BOOL b; | 212 | BOOL b; |
215 | LARGE_INTEGER li; | 213 | LARGE_INTEGER li; |
216 | b = GetFileSizeEx (fh->h, &li); | 214 | b = GetFileSizeEx(fh->h, &li); |
217 | if (! b) | 215 | if (!b) |
218 | { | 216 | { |
219 | SetErrnoFromWinError (GetLastError ()); | 217 | SetErrnoFromWinError(GetLastError()); |
220 | return GNUNET_SYSERR; | 218 | return GNUNET_SYSERR; |
221 | } | 219 | } |
222 | *size = (off_t) li.QuadPart; | 220 | *size = (off_t)li.QuadPart; |
223 | #else | 221 | #else |
224 | struct stat sbuf; | 222 | struct stat sbuf; |
225 | 223 | ||
226 | if (0 != fstat (fh->fd, &sbuf)) | 224 | if (0 != fstat(fh->fd, &sbuf)) |
227 | return GNUNET_SYSERR; | 225 | return GNUNET_SYSERR; |
228 | *size = sbuf.st_size; | 226 | *size = sbuf.st_size; |
229 | #endif | 227 | #endif |
@@ -240,35 +238,35 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size) | |||
240 | * @return the new position on success, #GNUNET_SYSERR otherwise | 238 | * @return the new position on success, #GNUNET_SYSERR otherwise |
241 | */ | 239 | */ |
242 | off_t | 240 | off_t |
243 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | 241 | GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, |
244 | off_t offset, | 242 | off_t offset, |
245 | enum GNUNET_DISK_Seek whence) | 243 | enum GNUNET_DISK_Seek whence) |
246 | { | 244 | { |
247 | if (h == NULL) | 245 | if (h == NULL) |
248 | { | 246 | { |
249 | errno = EINVAL; | 247 | errno = EINVAL; |
250 | return GNUNET_SYSERR; | 248 | return GNUNET_SYSERR; |
251 | } | 249 | } |
252 | 250 | ||
253 | #ifdef MINGW | 251 | #ifdef MINGW |
254 | LARGE_INTEGER li; | 252 | LARGE_INTEGER li; |
255 | LARGE_INTEGER new_pos; | 253 | LARGE_INTEGER new_pos; |
256 | BOOL b; | 254 | BOOL b; |
257 | 255 | ||
258 | static DWORD t[] = {FILE_BEGIN, FILE_CURRENT, FILE_END}; | 256 | static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END }; |
259 | li.QuadPart = offset; | 257 | li.QuadPart = offset; |
260 | 258 | ||
261 | b = SetFilePointerEx (h->h, li, &new_pos, t[whence]); | 259 | b = SetFilePointerEx(h->h, li, &new_pos, t[whence]); |
262 | if (b == 0) | 260 | if (b == 0) |
263 | { | 261 | { |
264 | SetErrnoFromWinError (GetLastError ()); | 262 | SetErrnoFromWinError(GetLastError()); |
265 | return GNUNET_SYSERR; | 263 | return GNUNET_SYSERR; |
266 | } | 264 | } |
267 | return (off_t) new_pos.QuadPart; | 265 | return (off_t)new_pos.QuadPart; |
268 | #else | 266 | #else |
269 | static int t[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 267 | static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; |
270 | 268 | ||
271 | return lseek (h->fd, offset, t[whence]); | 269 | return lseek(h->fd, offset, t[whence]); |
272 | #endif | 270 | #endif |
273 | } | 271 | } |
274 | 272 | ||
@@ -288,19 +286,19 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | |||
288 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | 286 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success |
289 | */ | 287 | */ |
290 | int | 288 | int |
291 | GNUNET_DISK_file_size (const char *filename, | 289 | GNUNET_DISK_file_size(const char *filename, |
292 | uint64_t *size, | 290 | uint64_t *size, |
293 | int include_symbolic_links, | 291 | int include_symbolic_links, |
294 | int single_file_mode) | 292 | int single_file_mode) |
295 | { | 293 | { |
296 | struct GetFileSizeData gfsd; | 294 | struct GetFileSizeData gfsd; |
297 | int ret; | 295 | int ret; |
298 | 296 | ||
299 | GNUNET_assert (size != NULL); | 297 | GNUNET_assert(size != NULL); |
300 | gfsd.total = 0; | 298 | gfsd.total = 0; |
301 | gfsd.include_sym_links = include_symbolic_links; | 299 | gfsd.include_sym_links = include_symbolic_links; |
302 | gfsd.single_file_mode = single_file_mode; | 300 | gfsd.single_file_mode = single_file_mode; |
303 | ret = getSizeRec (&gfsd, filename); | 301 | ret = getSizeRec(&gfsd, filename); |
304 | *size = gfsd.total; | 302 | *size = gfsd.total; |
305 | return ret; | 303 | return ret; |
306 | } | 304 | } |
@@ -322,9 +320,9 @@ GNUNET_DISK_file_size (const char *filename, | |||
322 | * @return #GNUNET_OK on success | 320 | * @return #GNUNET_OK on success |
323 | */ | 321 | */ |
324 | int | 322 | int |
325 | GNUNET_DISK_file_get_identifiers (const char *filename, | 323 | GNUNET_DISK_file_get_identifiers(const char *filename, |
326 | uint64_t *dev, | 324 | uint64_t *dev, |
327 | uint64_t *ino) | 325 | uint64_t *ino) |
328 | { | 326 | { |
329 | #if WINDOWS | 327 | #if WINDOWS |
330 | { | 328 | { |
@@ -333,19 +331,19 @@ GNUNET_DISK_file_get_identifiers (const char *filename, | |||
333 | BY_HANDLE_FILE_INFORMATION info; | 331 | BY_HANDLE_FILE_INFORMATION info; |
334 | int succ; | 332 | int succ; |
335 | 333 | ||
336 | fh = GNUNET_DISK_file_open (filename, | 334 | fh = GNUNET_DISK_file_open(filename, |
337 | GNUNET_DISK_OPEN_READ, | 335 | GNUNET_DISK_OPEN_READ, |
338 | GNUNET_DISK_PERM_NONE); | 336 | GNUNET_DISK_PERM_NONE); |
339 | if (NULL == fh) | 337 | if (NULL == fh) |
340 | return GNUNET_SYSERR; | 338 | return GNUNET_SYSERR; |
341 | succ = GetFileInformationByHandle (fh->h, &info); | 339 | succ = GetFileInformationByHandle(fh->h, &info); |
342 | GNUNET_DISK_file_close (fh); | 340 | GNUNET_DISK_file_close(fh); |
343 | if (! succ) | 341 | if (!succ) |
344 | { | 342 | { |
345 | return GNUNET_SYSERR; | 343 | return GNUNET_SYSERR; |
346 | } | 344 | } |
347 | *dev = info.dwVolumeSerialNumber; | 345 | *dev = info.dwVolumeSerialNumber; |
348 | *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | | 346 | *ino = ((((uint64_t)info.nFileIndexHigh) << (sizeof(DWORD) * 8)) | |
349 | info.nFileIndexLow); | 347 | info.nFileIndexLow); |
350 | } | 348 | } |
351 | #else /* !WINDOWS */ | 349 | #else /* !WINDOWS */ |
@@ -353,11 +351,11 @@ GNUNET_DISK_file_get_identifiers (const char *filename, | |||
353 | { | 351 | { |
354 | struct stat sbuf; | 352 | struct stat sbuf; |
355 | 353 | ||
356 | if (0 != stat (filename, &sbuf)) | 354 | if (0 != stat(filename, &sbuf)) |
357 | { | 355 | { |
358 | return GNUNET_SYSERR; | 356 | return GNUNET_SYSERR; |
359 | } | 357 | } |
360 | *ino = (uint64_t) sbuf.st_ino; | 358 | *ino = (uint64_t)sbuf.st_ino; |
361 | } | 359 | } |
362 | #else | 360 | #else |
363 | *ino = 0; | 361 | *ino = 0; |
@@ -366,22 +364,22 @@ GNUNET_DISK_file_get_identifiers (const char *filename, | |||
366 | { | 364 | { |
367 | struct statvfs fbuf; | 365 | struct statvfs fbuf; |
368 | 366 | ||
369 | if (0 != statvfs (filename, &fbuf)) | 367 | if (0 != statvfs(filename, &fbuf)) |
370 | { | 368 | { |
371 | return GNUNET_SYSERR; | 369 | return GNUNET_SYSERR; |
372 | } | 370 | } |
373 | *dev = (uint64_t) fbuf.f_fsid; | 371 | *dev = (uint64_t)fbuf.f_fsid; |
374 | } | 372 | } |
375 | #elif HAVE_STATFS | 373 | #elif HAVE_STATFS |
376 | { | 374 | { |
377 | struct statfs fbuf; | 375 | struct statfs fbuf; |
378 | 376 | ||
379 | if (0 != statfs (filename, &fbuf)) | 377 | if (0 != statfs(filename, &fbuf)) |
380 | { | 378 | { |
381 | return GNUNET_SYSERR; | 379 | return GNUNET_SYSERR; |
382 | } | 380 | } |
383 | *dev = | 381 | *dev = |
384 | ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]); | 382 | ((uint64_t)fbuf.f_fsid.val[0]) << 32 || ((uint64_t)fbuf.f_fsid.val[1]); |
385 | } | 383 | } |
386 | #else | 384 | #else |
387 | *dev = 0; | 385 | *dev = 0; |
@@ -398,7 +396,7 @@ GNUNET_DISK_file_get_identifiers (const char *filename, | |||
398 | * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error | 396 | * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error |
399 | */ | 397 | */ |
400 | static char * | 398 | static char * |
401 | mktemp_name (const char *t) | 399 | mktemp_name(const char *t) |
402 | { | 400 | { |
403 | const char *tmpdir; | 401 | const char *tmpdir; |
404 | char *tmpl; | 402 | char *tmpl; |
@@ -406,33 +404,33 @@ mktemp_name (const char *t) | |||
406 | 404 | ||
407 | if ((t[0] != '/') && (t[0] != '\\') | 405 | if ((t[0] != '/') && (t[0] != '\\') |
408 | #if WINDOWS | 406 | #if WINDOWS |
409 | && ! (isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) | 407 | && !(isalpha((int)t[0]) && (t[0] != '\0') && (t[1] == ':')) |
410 | #endif | 408 | #endif |
411 | ) | 409 | ) |
412 | { | 410 | { |
413 | /* FIXME: This uses system codepage on W32, not UTF-8 */ | 411 | /* FIXME: This uses system codepage on W32, not UTF-8 */ |
414 | tmpdir = getenv ("TMPDIR"); | 412 | tmpdir = getenv("TMPDIR"); |
415 | if (NULL == tmpdir) | 413 | if (NULL == tmpdir) |
416 | tmpdir = getenv ("TMP"); | 414 | tmpdir = getenv("TMP"); |
417 | if (NULL == tmpdir) | 415 | if (NULL == tmpdir) |
418 | tmpdir = getenv ("TEMP"); | 416 | tmpdir = getenv("TEMP"); |
419 | if (NULL == tmpdir) | 417 | if (NULL == tmpdir) |
420 | tmpdir = "/tmp"; | 418 | tmpdir = "/tmp"; |
421 | GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); | 419 | GNUNET_asprintf(&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); |
422 | } | 420 | } |
423 | else | 421 | else |
424 | { | 422 | { |
425 | GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX"); | 423 | GNUNET_asprintf(&tmpl, "%s%s", t, "XXXXXX"); |
426 | } | 424 | } |
427 | #ifdef MINGW | 425 | #ifdef MINGW |
428 | fn = (char *) GNUNET_malloc (MAX_PATH + 1); | 426 | fn = (char *)GNUNET_malloc(MAX_PATH + 1); |
429 | if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn)) | 427 | if (ERROR_SUCCESS != plibc_conv_to_win_path(tmpl, fn)) |
430 | { | 428 | { |
431 | GNUNET_free (fn); | 429 | GNUNET_free(fn); |
432 | GNUNET_free (tmpl); | 430 | GNUNET_free(tmpl); |
433 | return NULL; | 431 | return NULL; |
434 | } | 432 | } |
435 | GNUNET_free (tmpl); | 433 | GNUNET_free(tmpl); |
436 | #else | 434 | #else |
437 | fn = tmpl; | 435 | fn = tmpl; |
438 | #endif | 436 | #endif |
@@ -442,32 +440,32 @@ mktemp_name (const char *t) | |||
442 | 440 | ||
443 | #if WINDOWS | 441 | #if WINDOWS |
444 | static char * | 442 | static char * |
445 | mkdtemp (char *fn) | 443 | mkdtemp(char *fn) |
446 | { | 444 | { |
447 | char *random_fn; | 445 | char *random_fn; |
448 | char *tfn; | 446 | char *tfn; |
449 | 447 | ||
450 | while (1) | 448 | while (1) |
451 | { | ||
452 | tfn = GNUNET_strdup (fn); | ||
453 | random_fn = _mktemp (tfn); | ||
454 | if (NULL == random_fn) | ||
455 | { | ||
456 | GNUNET_free (tfn); | ||
457 | return NULL; | ||
458 | } | ||
459 | /* FIXME: assume fn to be UTF-8-encoded and do the right thing */ | ||
460 | if (0 == CreateDirectoryA (tfn, NULL)) | ||
461 | { | 449 | { |
462 | DWORD error = GetLastError (); | 450 | tfn = GNUNET_strdup(fn); |
463 | GNUNET_free (tfn); | 451 | random_fn = _mktemp(tfn); |
464 | if (ERROR_ALREADY_EXISTS == error) | 452 | if (NULL == random_fn) |
465 | continue; | 453 | { |
466 | return NULL; | 454 | GNUNET_free(tfn); |
455 | return NULL; | ||
456 | } | ||
457 | /* FIXME: assume fn to be UTF-8-encoded and do the right thing */ | ||
458 | if (0 == CreateDirectoryA(tfn, NULL)) | ||
459 | { | ||
460 | DWORD error = GetLastError(); | ||
461 | GNUNET_free(tfn); | ||
462 | if (ERROR_ALREADY_EXISTS == error) | ||
463 | continue; | ||
464 | return NULL; | ||
465 | } | ||
466 | break; | ||
467 | } | 467 | } |
468 | break; | 468 | strcpy(fn, tfn); |
469 | } | ||
470 | strcpy (fn, tfn); | ||
471 | return fn; | 469 | return fn; |
472 | } | 470 | } |
473 | 471 | ||
@@ -481,9 +479,9 @@ mkdtemp (char *fn) | |||
481 | * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set | 479 | * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set |
482 | */ | 480 | */ |
483 | void | 481 | void |
484 | GNUNET_DISK_fix_permissions (const char *fn, | 482 | GNUNET_DISK_fix_permissions(const char *fn, |
485 | int require_uid_match, | 483 | int require_uid_match, |
486 | int require_gid_match) | 484 | int require_gid_match) |
487 | { | 485 | { |
488 | /* nothing on W32 */ | 486 | /* nothing on W32 */ |
489 | } | 487 | } |
@@ -499,9 +497,9 @@ GNUNET_DISK_fix_permissions (const char *fn, | |||
499 | * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set | 497 | * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set |
500 | */ | 498 | */ |
501 | void | 499 | void |
502 | GNUNET_DISK_fix_permissions (const char *fn, | 500 | GNUNET_DISK_fix_permissions(const char *fn, |
503 | int require_uid_match, | 501 | int require_uid_match, |
504 | int require_gid_match) | 502 | int require_gid_match) |
505 | { | 503 | { |
506 | mode_t mode; | 504 | mode_t mode; |
507 | 505 | ||
@@ -512,8 +510,8 @@ GNUNET_DISK_fix_permissions (const char *fn, | |||
512 | else | 510 | else |
513 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | | 511 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | |
514 | S_IWOTH | S_IXOTH; | 512 | S_IWOTH | S_IXOTH; |
515 | if (0 != chmod (fn, mode)) | 513 | if (0 != chmod(fn, mode)) |
516 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn); | 514 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "chmod", fn); |
517 | } | 515 | } |
518 | 516 | ||
519 | #endif | 517 | #endif |
@@ -530,21 +528,21 @@ GNUNET_DISK_fix_permissions (const char *fn, | |||
530 | * file on disk in directory for temporary files | 528 | * file on disk in directory for temporary files |
531 | */ | 529 | */ |
532 | char * | 530 | char * |
533 | GNUNET_DISK_mkdtemp (const char *t) | 531 | GNUNET_DISK_mkdtemp(const char *t) |
534 | { | 532 | { |
535 | char *fn; | 533 | char *fn; |
536 | mode_t omask; | 534 | mode_t omask; |
537 | 535 | ||
538 | omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); | 536 | omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); |
539 | fn = mktemp_name (t); | 537 | fn = mktemp_name(t); |
540 | if (fn != mkdtemp (fn)) | 538 | if (fn != mkdtemp(fn)) |
541 | { | 539 | { |
542 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn); | 540 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn); |
543 | GNUNET_free (fn); | 541 | GNUNET_free(fn); |
544 | umask (omask); | 542 | umask(omask); |
545 | return NULL; | 543 | return NULL; |
546 | } | 544 | } |
547 | umask (omask); | 545 | umask(omask); |
548 | return fn; | 546 | return fn; |
549 | } | 547 | } |
550 | 548 | ||
@@ -557,22 +555,23 @@ GNUNET_DISK_mkdtemp (const char *t) | |||
557 | * @param fil name of the file to back up | 555 | * @param fil name of the file to back up |
558 | */ | 556 | */ |
559 | void | 557 | void |
560 | GNUNET_DISK_file_backup (const char *fil) | 558 | GNUNET_DISK_file_backup(const char *fil) |
561 | { | 559 | { |
562 | size_t slen; | 560 | size_t slen; |
563 | char *target; | 561 | char *target; |
564 | unsigned int num; | 562 | unsigned int num; |
565 | 563 | ||
566 | slen = strlen (fil) + 20; | 564 | slen = strlen(fil) + 20; |
567 | target = GNUNET_malloc (slen); | 565 | target = GNUNET_malloc(slen); |
568 | num = 0; | 566 | num = 0; |
569 | do | 567 | do |
570 | { | 568 | { |
571 | GNUNET_snprintf (target, slen, "%s.%u~", fil, num++); | 569 | GNUNET_snprintf(target, slen, "%s.%u~", fil, num++); |
572 | } while (0 == access (target, F_OK)); | 570 | } |
573 | if (0 != rename (fil, target)) | 571 | while (0 == access(target, F_OK)); |
574 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil); | 572 | if (0 != rename(fil, target)) |
575 | GNUNET_free (target); | 573 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "rename", fil); |
574 | GNUNET_free(target); | ||
576 | } | 575 | } |
577 | 576 | ||
578 | 577 | ||
@@ -588,24 +587,24 @@ GNUNET_DISK_file_backup (const char *fil) | |||
588 | * file on disk in directory for temporary files | 587 | * file on disk in directory for temporary files |
589 | */ | 588 | */ |
590 | char * | 589 | char * |
591 | GNUNET_DISK_mktemp (const char *t) | 590 | GNUNET_DISK_mktemp(const char *t) |
592 | { | 591 | { |
593 | int fd; | 592 | int fd; |
594 | char *fn; | 593 | char *fn; |
595 | mode_t omask; | 594 | mode_t omask; |
596 | 595 | ||
597 | omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); | 596 | omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); |
598 | fn = mktemp_name (t); | 597 | fn = mktemp_name(t); |
599 | if (-1 == (fd = mkstemp (fn))) | 598 | if (-1 == (fd = mkstemp(fn))) |
600 | { | 599 | { |
601 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); | 600 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); |
602 | GNUNET_free (fn); | 601 | GNUNET_free(fn); |
603 | umask (omask); | 602 | umask(omask); |
604 | return NULL; | 603 | return NULL; |
605 | } | 604 | } |
606 | umask (omask); | 605 | umask(omask); |
607 | if (0 != close (fd)) | 606 | if (0 != close(fd)) |
608 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); | 607 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "close", fn); |
609 | return fn; | 608 | return fn; |
610 | } | 609 | } |
611 | 610 | ||
@@ -623,34 +622,34 @@ GNUNET_DISK_mktemp (const char *t) | |||
623 | * does not exist or stat'ed | 622 | * does not exist or stat'ed |
624 | */ | 623 | */ |
625 | int | 624 | int |
626 | GNUNET_DISK_directory_test (const char *fil, int is_readable) | 625 | GNUNET_DISK_directory_test(const char *fil, int is_readable) |
627 | { | 626 | { |
628 | struct stat filestat; | 627 | struct stat filestat; |
629 | int ret; | 628 | int ret; |
630 | 629 | ||
631 | ret = stat (fil, &filestat); | 630 | ret = stat(fil, &filestat); |
632 | if (ret != 0) | 631 | if (ret != 0) |
633 | { | 632 | { |
634 | if (errno != ENOENT) | 633 | if (errno != ENOENT) |
635 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); | 634 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", fil); |
636 | return GNUNET_SYSERR; | 635 | return GNUNET_SYSERR; |
637 | } | 636 | } |
638 | if (! S_ISDIR (filestat.st_mode)) | 637 | if (!S_ISDIR(filestat.st_mode)) |
639 | { | 638 | { |
640 | LOG (GNUNET_ERROR_TYPE_INFO, | 639 | LOG(GNUNET_ERROR_TYPE_INFO, |
641 | "A file already exits with the same name %s\n", | 640 | "A file already exits with the same name %s\n", |
642 | fil); | 641 | fil); |
643 | return GNUNET_NO; | 642 | return GNUNET_NO; |
644 | } | 643 | } |
645 | if (GNUNET_YES == is_readable) | 644 | if (GNUNET_YES == is_readable) |
646 | ret = access (fil, R_OK | X_OK); | 645 | ret = access(fil, R_OK | X_OK); |
647 | else | 646 | else |
648 | ret = access (fil, X_OK); | 647 | ret = access(fil, X_OK); |
649 | if (ret < 0) | 648 | if (ret < 0) |
650 | { | 649 | { |
651 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); | 650 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", fil); |
652 | return GNUNET_NO; | 651 | return GNUNET_NO; |
653 | } | 652 | } |
654 | return GNUNET_YES; | 653 | return GNUNET_YES; |
655 | } | 654 | } |
656 | 655 | ||
@@ -664,40 +663,40 @@ GNUNET_DISK_directory_test (const char *fil, int is_readable) | |||
664 | * else (will print an error message in that case, too). | 663 | * else (will print an error message in that case, too). |
665 | */ | 664 | */ |
666 | int | 665 | int |
667 | GNUNET_DISK_file_test (const char *fil) | 666 | GNUNET_DISK_file_test(const char *fil) |
668 | { | 667 | { |
669 | struct stat filestat; | 668 | struct stat filestat; |
670 | int ret; | 669 | int ret; |
671 | char *rdir; | 670 | char *rdir; |
672 | 671 | ||
673 | rdir = GNUNET_STRINGS_filename_expand (fil); | 672 | rdir = GNUNET_STRINGS_filename_expand(fil); |
674 | if (rdir == NULL) | 673 | if (rdir == NULL) |
675 | return GNUNET_SYSERR; | 674 | return GNUNET_SYSERR; |
676 | 675 | ||
677 | ret = stat (rdir, &filestat); | 676 | ret = stat(rdir, &filestat); |
678 | if (ret != 0) | 677 | if (ret != 0) |
679 | { | ||
680 | if (errno != ENOENT) | ||
681 | { | 678 | { |
682 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir); | 679 | if (errno != ENOENT) |
683 | GNUNET_free (rdir); | 680 | { |
681 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", rdir); | ||
682 | GNUNET_free(rdir); | ||
683 | return GNUNET_SYSERR; | ||
684 | } | ||
685 | GNUNET_free(rdir); | ||
686 | return GNUNET_NO; | ||
687 | } | ||
688 | if (!S_ISREG(filestat.st_mode)) | ||
689 | { | ||
690 | GNUNET_free(rdir); | ||
691 | return GNUNET_NO; | ||
692 | } | ||
693 | if (access(rdir, F_OK) < 0) | ||
694 | { | ||
695 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", rdir); | ||
696 | GNUNET_free(rdir); | ||
684 | return GNUNET_SYSERR; | 697 | return GNUNET_SYSERR; |
685 | } | 698 | } |
686 | GNUNET_free (rdir); | 699 | GNUNET_free(rdir); |
687 | return GNUNET_NO; | ||
688 | } | ||
689 | if (! S_ISREG (filestat.st_mode)) | ||
690 | { | ||
691 | GNUNET_free (rdir); | ||
692 | return GNUNET_NO; | ||
693 | } | ||
694 | if (access (rdir, F_OK) < 0) | ||
695 | { | ||
696 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); | ||
697 | GNUNET_free (rdir); | ||
698 | return GNUNET_SYSERR; | ||
699 | } | ||
700 | GNUNET_free (rdir); | ||
701 | return GNUNET_YES; | 700 | return GNUNET_YES; |
702 | } | 701 | } |
703 | 702 | ||
@@ -709,7 +708,7 @@ GNUNET_DISK_file_test (const char *fil) | |||
709 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 708 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
710 | */ | 709 | */ |
711 | int | 710 | int |
712 | GNUNET_DISK_directory_create (const char *dir) | 711 | GNUNET_DISK_directory_create(const char *dir) |
713 | { | 712 | { |
714 | char *rdir; | 713 | char *rdir; |
715 | unsigned int len; | 714 | unsigned int len; |
@@ -717,105 +716,105 @@ GNUNET_DISK_directory_create (const char *dir) | |||
717 | unsigned int pos2; | 716 | unsigned int pos2; |
718 | int ret = GNUNET_OK; | 717 | int ret = GNUNET_OK; |
719 | 718 | ||
720 | rdir = GNUNET_STRINGS_filename_expand (dir); | 719 | rdir = GNUNET_STRINGS_filename_expand(dir); |
721 | if (rdir == NULL) | 720 | if (rdir == NULL) |
722 | { | 721 | { |
723 | GNUNET_break (0); | 722 | GNUNET_break(0); |
724 | return GNUNET_SYSERR; | 723 | return GNUNET_SYSERR; |
725 | } | 724 | } |
726 | 725 | ||
727 | len = strlen (rdir); | 726 | len = strlen(rdir); |
728 | #ifndef MINGW | 727 | #ifndef MINGW |
729 | pos = 1; /* skip heading '/' */ | 728 | pos = 1; /* skip heading '/' */ |
730 | #else | 729 | #else |
731 | /* Local or Network path? */ | 730 | /* Local or Network path? */ |
732 | if (strncmp (rdir, "\\\\", 2) == 0) | 731 | if (strncmp(rdir, "\\\\", 2) == 0) |
733 | { | ||
734 | pos = 2; | ||
735 | while (rdir[pos]) | ||
736 | { | 732 | { |
737 | if (rdir[pos] == '\\') | 733 | pos = 2; |
738 | { | 734 | while (rdir[pos]) |
739 | pos++; | 735 | { |
740 | break; | 736 | if (rdir[pos] == '\\') |
741 | } | 737 | { |
742 | pos++; | 738 | pos++; |
739 | break; | ||
740 | } | ||
741 | pos++; | ||
742 | } | ||
743 | } | 743 | } |
744 | } | ||
745 | else | 744 | else |
746 | { | 745 | { |
747 | pos = 3; /* strlen("C:\\") */ | 746 | pos = 3; /* strlen("C:\\") */ |
748 | } | 747 | } |
749 | #endif | 748 | #endif |
750 | /* Check which low level directories already exist */ | 749 | /* Check which low level directories already exist */ |
751 | pos2 = len; | 750 | pos2 = len; |
752 | rdir[len] = DIR_SEPARATOR; | 751 | rdir[len] = DIR_SEPARATOR; |
753 | while (pos <= pos2) | 752 | while (pos <= pos2) |
754 | { | ||
755 | if (DIR_SEPARATOR == rdir[pos2]) | ||
756 | { | 753 | { |
757 | rdir[pos2] = '\0'; | 754 | if (DIR_SEPARATOR == rdir[pos2]) |
758 | ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO); | 755 | { |
759 | if (GNUNET_NO == ret) | 756 | rdir[pos2] = '\0'; |
760 | { | 757 | ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); |
761 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 758 | if (GNUNET_NO == ret) |
762 | "Creating directory `%s' failed", | 759 | { |
763 | rdir); | 760 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
764 | GNUNET_free (rdir); | 761 | "Creating directory `%s' failed", |
765 | return GNUNET_SYSERR; | 762 | rdir); |
766 | } | 763 | GNUNET_free(rdir); |
767 | rdir[pos2] = DIR_SEPARATOR; | 764 | return GNUNET_SYSERR; |
768 | if (GNUNET_YES == ret) | 765 | } |
769 | { | 766 | rdir[pos2] = DIR_SEPARATOR; |
770 | pos2++; | 767 | if (GNUNET_YES == ret) |
771 | break; | 768 | { |
772 | } | 769 | pos2++; |
770 | break; | ||
771 | } | ||
772 | } | ||
773 | pos2--; | ||
773 | } | 774 | } |
774 | pos2--; | ||
775 | } | ||
776 | rdir[len] = '\0'; | 775 | rdir[len] = '\0'; |
777 | if (pos < pos2) | 776 | if (pos < pos2) |
778 | pos = pos2; | 777 | pos = pos2; |
779 | /* Start creating directories */ | 778 | /* Start creating directories */ |
780 | while (pos <= len) | 779 | while (pos <= len) |
781 | { | ||
782 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) | ||
783 | { | 780 | { |
784 | rdir[pos] = '\0'; | 781 | if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) |
785 | ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO); | 782 | { |
786 | if (GNUNET_NO == ret) | 783 | rdir[pos] = '\0'; |
787 | { | 784 | ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); |
788 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 785 | if (GNUNET_NO == ret) |
789 | "Creating directory `%s' failed", | 786 | { |
790 | rdir); | 787 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
791 | GNUNET_free (rdir); | 788 | "Creating directory `%s' failed", |
792 | return GNUNET_SYSERR; | 789 | rdir); |
793 | } | 790 | GNUNET_free(rdir); |
794 | if (GNUNET_SYSERR == ret) | 791 | return GNUNET_SYSERR; |
795 | { | 792 | } |
793 | if (GNUNET_SYSERR == ret) | ||
794 | { | ||
796 | #ifndef MINGW | 795 | #ifndef MINGW |
797 | ret = mkdir (rdir, | 796 | ret = mkdir(rdir, |
798 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | | 797 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | |
799 | S_IXOTH); /* 755 */ | 798 | S_IXOTH); /* 755 */ |
800 | #else | 799 | #else |
801 | wchar_t wrdir[MAX_PATH + 1]; | 800 | wchar_t wrdir[MAX_PATH + 1]; |
802 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (rdir, wrdir)) | 801 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(rdir, wrdir)) |
803 | ret = ! CreateDirectoryW (wrdir, NULL); | 802 | ret = !CreateDirectoryW(wrdir, NULL); |
804 | else | 803 | else |
805 | ret = 1; | 804 | ret = 1; |
806 | #endif | 805 | #endif |
807 | if ((ret != 0) && (errno != EEXIST)) | 806 | if ((ret != 0) && (errno != EEXIST)) |
808 | { | 807 | { |
809 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); | 808 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir); |
810 | GNUNET_free (rdir); | 809 | GNUNET_free(rdir); |
811 | return GNUNET_SYSERR; | 810 | return GNUNET_SYSERR; |
811 | } | ||
812 | } | ||
813 | rdir[pos] = DIR_SEPARATOR; | ||
812 | } | 814 | } |
813 | } | 815 | pos++; |
814 | rdir[pos] = DIR_SEPARATOR; | ||
815 | } | 816 | } |
816 | pos++; | 817 | GNUNET_free(rdir); |
817 | } | ||
818 | GNUNET_free (rdir); | ||
819 | return GNUNET_OK; | 818 | return GNUNET_OK; |
820 | } | 819 | } |
821 | 820 | ||
@@ -830,40 +829,40 @@ GNUNET_DISK_directory_create (const char *dir) | |||
830 | * exists but is not writeable for us | 829 | * exists but is not writeable for us |
831 | */ | 830 | */ |
832 | int | 831 | int |
833 | GNUNET_DISK_directory_create_for_file (const char *filename) | 832 | GNUNET_DISK_directory_create_for_file(const char *filename) |
834 | { | 833 | { |
835 | char *rdir; | 834 | char *rdir; |
836 | size_t len; | 835 | size_t len; |
837 | int ret; | 836 | int ret; |
838 | int eno; | 837 | int eno; |
839 | 838 | ||
840 | rdir = GNUNET_STRINGS_filename_expand (filename); | 839 | rdir = GNUNET_STRINGS_filename_expand(filename); |
841 | if (NULL == rdir) | 840 | if (NULL == rdir) |
842 | { | 841 | { |
843 | errno = EINVAL; | 842 | errno = EINVAL; |
844 | return GNUNET_SYSERR; | 843 | return GNUNET_SYSERR; |
845 | } | 844 | } |
846 | if (0 == access (rdir, W_OK)) | 845 | if (0 == access(rdir, W_OK)) |
847 | { | 846 | { |
848 | GNUNET_free (rdir); | 847 | GNUNET_free(rdir); |
849 | return GNUNET_OK; | 848 | return GNUNET_OK; |
850 | } | 849 | } |
851 | 850 | ||
852 | len = strlen (rdir); | 851 | len = strlen(rdir); |
853 | while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) | 852 | while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) |
854 | len--; | 853 | len--; |
855 | rdir[len] = '\0'; | 854 | rdir[len] = '\0'; |
856 | /* The empty path is invalid and in this case refers to / */ | 855 | /* The empty path is invalid and in this case refers to / */ |
857 | if (0 == len) | 856 | if (0 == len) |
858 | { | 857 | { |
859 | GNUNET_free (rdir); | 858 | GNUNET_free(rdir); |
860 | rdir = GNUNET_strdup ("/"); | 859 | rdir = GNUNET_strdup("/"); |
861 | } | 860 | } |
862 | ret = GNUNET_DISK_directory_create (rdir); | 861 | ret = GNUNET_DISK_directory_create(rdir); |
863 | if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK))) | 862 | if ((GNUNET_OK == ret) && (0 != access(rdir, W_OK))) |
864 | ret = GNUNET_NO; | 863 | ret = GNUNET_NO; |
865 | eno = errno; | 864 | eno = errno; |
866 | GNUNET_free (rdir); | 865 | GNUNET_free(rdir); |
867 | errno = eno; | 866 | errno = eno; |
868 | return ret; | 867 | return ret; |
869 | } | 868 | } |
@@ -878,51 +877,51 @@ GNUNET_DISK_directory_create_for_file (const char *filename) | |||
878 | * @return the number of bytes read on success, #GNUNET_SYSERR on failure | 877 | * @return the number of bytes read on success, #GNUNET_SYSERR on failure |
879 | */ | 878 | */ |
880 | ssize_t | 879 | ssize_t |
881 | GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, | 880 | GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, |
882 | void *result, | 881 | void *result, |
883 | size_t len) | 882 | size_t len) |
884 | { | 883 | { |
885 | if (NULL == h) | 884 | if (NULL == h) |
886 | { | 885 | { |
887 | errno = EINVAL; | 886 | errno = EINVAL; |
888 | return GNUNET_SYSERR; | 887 | return GNUNET_SYSERR; |
889 | } | 888 | } |
890 | 889 | ||
891 | #ifdef MINGW | 890 | #ifdef MINGW |
892 | DWORD bytes_read; | 891 | DWORD bytes_read; |
893 | 892 | ||
894 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 893 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
895 | { | ||
896 | if (! ReadFile (h->h, result, len, &bytes_read, NULL)) | ||
897 | { | 894 | { |
898 | SetErrnoFromWinError (GetLastError ()); | 895 | if (!ReadFile(h->h, result, len, &bytes_read, NULL)) |
899 | return GNUNET_SYSERR; | 896 | { |
897 | SetErrnoFromWinError(GetLastError()); | ||
898 | return GNUNET_SYSERR; | ||
899 | } | ||
900 | } | 900 | } |
901 | } | ||
902 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 901 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
903 | { | ||
904 | if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) | ||
905 | { | 902 | { |
906 | if (GetLastError () != ERROR_IO_PENDING) | 903 | if (!ReadFile(h->h, result, len, &bytes_read, h->oOverlapRead)) |
907 | { | 904 | { |
908 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 905 | if (GetLastError() != ERROR_IO_PENDING) |
909 | "Error reading from pipe: %u\n", | 906 | { |
910 | GetLastError ()); | 907 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
911 | SetErrnoFromWinError (GetLastError ()); | 908 | "Error reading from pipe: %u\n", |
912 | return GNUNET_SYSERR; | 909 | GetLastError()); |
913 | } | 910 | SetErrnoFromWinError(GetLastError()); |
914 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | 911 | return GNUNET_SYSERR; |
915 | GetOverlappedResult (h->h, h->oOverlapRead, &bytes_read, TRUE); | 912 | } |
913 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | ||
914 | GetOverlappedResult(h->h, h->oOverlapRead, &bytes_read, TRUE); | ||
915 | } | ||
916 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytes_read); | ||
916 | } | 917 | } |
917 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytes_read); | ||
918 | } | ||
919 | else | 918 | else |
920 | { | 919 | { |
921 | bytes_read = 0; | 920 | bytes_read = 0; |
922 | } | 921 | } |
923 | return bytes_read; | 922 | return bytes_read; |
924 | #else | 923 | #else |
925 | return read (h->fd, result, len); | 924 | return read(h->fd, result, len); |
926 | #endif | 925 | #endif |
927 | } | 926 | } |
928 | 927 | ||
@@ -938,69 +937,69 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, | |||
938 | * @return the number of bytes read on success, #GNUNET_SYSERR on failure | 937 | * @return the number of bytes read on success, #GNUNET_SYSERR on failure |
939 | */ | 938 | */ |
940 | ssize_t | 939 | ssize_t |
941 | GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | 940 | GNUNET_DISK_file_read_non_blocking(const struct GNUNET_DISK_FileHandle *h, |
942 | void *result, | 941 | void *result, |
943 | size_t len) | 942 | size_t len) |
944 | { | 943 | { |
945 | if (NULL == h) | 944 | if (NULL == h) |
946 | { | 945 | { |
947 | errno = EINVAL; | 946 | errno = EINVAL; |
948 | return GNUNET_SYSERR; | 947 | return GNUNET_SYSERR; |
949 | } | 948 | } |
950 | 949 | ||
951 | #ifdef MINGW | 950 | #ifdef MINGW |
952 | DWORD bytes_read; | 951 | DWORD bytes_read; |
953 | 952 | ||
954 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 953 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
955 | { | ||
956 | if (! ReadFile (h->h, result, len, &bytes_read, NULL)) | ||
957 | { | 954 | { |
958 | SetErrnoFromWinError (GetLastError ()); | 955 | if (!ReadFile(h->h, result, len, &bytes_read, NULL)) |
959 | return GNUNET_SYSERR; | 956 | { |
957 | SetErrnoFromWinError(GetLastError()); | ||
958 | return GNUNET_SYSERR; | ||
959 | } | ||
960 | } | 960 | } |
961 | } | ||
962 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 961 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
963 | { | ||
964 | if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) | ||
965 | { | 962 | { |
966 | if (GetLastError () != ERROR_IO_PENDING) | 963 | if (!ReadFile(h->h, result, len, &bytes_read, h->oOverlapRead)) |
967 | { | 964 | { |
968 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 965 | if (GetLastError() != ERROR_IO_PENDING) |
969 | "Error reading from pipe: %u\n", | 966 | { |
970 | GetLastError ()); | 967 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
971 | SetErrnoFromWinError (GetLastError ()); | 968 | "Error reading from pipe: %u\n", |
972 | return GNUNET_SYSERR; | 969 | GetLastError()); |
973 | } | 970 | SetErrnoFromWinError(GetLastError()); |
974 | else | 971 | return GNUNET_SYSERR; |
975 | { | 972 | } |
976 | LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadFile() queued a read, cancelling\n"); | 973 | else |
977 | CancelIo (h->h); | 974 | { |
978 | errno = EAGAIN; | 975 | LOG(GNUNET_ERROR_TYPE_DEBUG, "ReadFile() queued a read, cancelling\n"); |
979 | return GNUNET_SYSERR; | 976 | CancelIo(h->h); |
980 | } | 977 | errno = EAGAIN; |
978 | return GNUNET_SYSERR; | ||
979 | } | ||
980 | } | ||
981 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytes_read); | ||
981 | } | 982 | } |
982 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytes_read); | ||
983 | } | ||
984 | else | 983 | else |
985 | { | 984 | { |
986 | bytes_read = 0; | 985 | bytes_read = 0; |
987 | } | 986 | } |
988 | return bytes_read; | 987 | return bytes_read; |
989 | #else | 988 | #else |
990 | int flags; | 989 | int flags; |
991 | ssize_t ret; | 990 | ssize_t ret; |
992 | 991 | ||
993 | /* set to non-blocking, read, then set back */ | 992 | /* set to non-blocking, read, then set back */ |
994 | flags = fcntl (h->fd, F_GETFL); | 993 | flags = fcntl(h->fd, F_GETFL); |
995 | if (0 == (flags & O_NONBLOCK)) | 994 | if (0 == (flags & O_NONBLOCK)) |
996 | (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK); | 995 | (void)fcntl(h->fd, F_SETFL, flags | O_NONBLOCK); |
997 | ret = read (h->fd, result, len); | 996 | ret = read(h->fd, result, len); |
998 | if (0 == (flags & O_NONBLOCK)) | 997 | if (0 == (flags & O_NONBLOCK)) |
999 | { | 998 | { |
1000 | int eno = errno; | 999 | int eno = errno; |
1001 | (void) fcntl (h->fd, F_SETFL, flags); | 1000 | (void)fcntl(h->fd, F_SETFL, flags); |
1002 | errno = eno; | 1001 | errno = eno; |
1003 | } | 1002 | } |
1004 | return ret; | 1003 | return ret; |
1005 | #endif | 1004 | #endif |
1006 | } | 1005 | } |
@@ -1015,18 +1014,18 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
1015 | * @return number of bytes read, #GNUNET_SYSERR on failure | 1014 | * @return number of bytes read, #GNUNET_SYSERR on failure |
1016 | */ | 1015 | */ |
1017 | ssize_t | 1016 | ssize_t |
1018 | GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) | 1017 | GNUNET_DISK_fn_read(const char *fn, void *result, size_t len) |
1019 | { | 1018 | { |
1020 | struct GNUNET_DISK_FileHandle *fh; | 1019 | struct GNUNET_DISK_FileHandle *fh; |
1021 | ssize_t ret; | 1020 | ssize_t ret; |
1022 | int eno; | 1021 | int eno; |
1023 | 1022 | ||
1024 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); | 1023 | fh = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
1025 | if (NULL == fh) | 1024 | if (NULL == fh) |
1026 | return GNUNET_SYSERR; | 1025 | return GNUNET_SYSERR; |
1027 | ret = GNUNET_DISK_file_read (fh, result, len); | 1026 | ret = GNUNET_DISK_file_read(fh, result, len); |
1028 | eno = errno; | 1027 | eno = errno; |
1029 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); | 1028 | GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); |
1030 | errno = eno; | 1029 | errno = eno; |
1031 | return ret; | 1030 | return ret; |
1032 | } | 1031 | } |
@@ -1041,87 +1040,87 @@ GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) | |||
1041 | * @return number of bytes written on success, #GNUNET_SYSERR on error | 1040 | * @return number of bytes written on success, #GNUNET_SYSERR on error |
1042 | */ | 1041 | */ |
1043 | ssize_t | 1042 | ssize_t |
1044 | GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, | 1043 | GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, |
1045 | const void *buffer, | 1044 | const void *buffer, |
1046 | size_t n) | 1045 | size_t n) |
1047 | { | 1046 | { |
1048 | if (NULL == h) | 1047 | if (NULL == h) |
1049 | { | 1048 | { |
1050 | errno = EINVAL; | 1049 | errno = EINVAL; |
1051 | return GNUNET_SYSERR; | 1050 | return GNUNET_SYSERR; |
1052 | } | 1051 | } |
1053 | 1052 | ||
1054 | #ifdef MINGW | 1053 | #ifdef MINGW |
1055 | DWORD bytes_written; | 1054 | DWORD bytes_written; |
1056 | 1055 | ||
1057 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 1056 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
1058 | { | ||
1059 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) | ||
1060 | { | 1057 | { |
1061 | SetErrnoFromWinError (GetLastError ()); | 1058 | if (!WriteFile(h->h, buffer, n, &bytes_written, NULL)) |
1062 | return GNUNET_SYSERR; | 1059 | { |
1060 | SetErrnoFromWinError(GetLastError()); | ||
1061 | return GNUNET_SYSERR; | ||
1062 | } | ||
1063 | } | 1063 | } |
1064 | } | ||
1065 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 1064 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
1066 | { | ||
1067 | LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); | ||
1068 | if (! WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite)) | ||
1069 | { | ||
1070 | if (GetLastError () != ERROR_IO_PENDING) | ||
1071 | { | ||
1072 | SetErrnoFromWinError (GetLastError ()); | ||
1073 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1074 | "Error writing to pipe: %u\n", | ||
1075 | GetLastError ()); | ||
1076 | return GNUNET_SYSERR; | ||
1077 | } | ||
1078 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | ||
1079 | if (! GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE)) | ||
1080 | { | ||
1081 | SetErrnoFromWinError (GetLastError ()); | ||
1082 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1083 | "Error getting overlapped result while writing to pipe: %u\n", | ||
1084 | GetLastError ()); | ||
1085 | return GNUNET_SYSERR; | ||
1086 | } | ||
1087 | } | ||
1088 | else | ||
1089 | { | 1065 | { |
1090 | DWORD ovr; | 1066 | LOG(GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); |
1091 | if (! GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) | 1067 | if (!WriteFile(h->h, buffer, n, &bytes_written, h->oOverlapWrite)) |
1092 | { | 1068 | { |
1093 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1069 | if (GetLastError() != ERROR_IO_PENDING) |
1094 | "Error getting control overlapped result while writing to pipe: %u\n", | 1070 | { |
1095 | GetLastError ()); | 1071 | SetErrnoFromWinError(GetLastError()); |
1096 | } | 1072 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
1073 | "Error writing to pipe: %u\n", | ||
1074 | GetLastError()); | ||
1075 | return GNUNET_SYSERR; | ||
1076 | } | ||
1077 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | ||
1078 | if (!GetOverlappedResult(h->h, h->oOverlapWrite, &bytes_written, TRUE)) | ||
1079 | { | ||
1080 | SetErrnoFromWinError(GetLastError()); | ||
1081 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
1082 | "Error getting overlapped result while writing to pipe: %u\n", | ||
1083 | GetLastError()); | ||
1084 | return GNUNET_SYSERR; | ||
1085 | } | ||
1086 | } | ||
1097 | else | 1087 | else |
1098 | { | 1088 | { |
1099 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1089 | DWORD ovr; |
1100 | "Wrote %u bytes (ovr says %u), picking the greatest\n", | 1090 | if (!GetOverlappedResult(h->h, h->oOverlapWrite, &ovr, TRUE)) |
1101 | bytes_written, | 1091 | { |
1102 | ovr); | 1092 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
1103 | } | 1093 | "Error getting control overlapped result while writing to pipe: %u\n", |
1094 | GetLastError()); | ||
1095 | } | ||
1096 | else | ||
1097 | { | ||
1098 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
1099 | "Wrote %u bytes (ovr says %u), picking the greatest\n", | ||
1100 | bytes_written, | ||
1101 | ovr); | ||
1102 | } | ||
1103 | } | ||
1104 | if (bytes_written == 0) | ||
1105 | { | ||
1106 | if (n > 0) | ||
1107 | { | ||
1108 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
1109 | "Wrote %u bytes, returning -1 with EAGAIN\n", | ||
1110 | bytes_written); | ||
1111 | errno = EAGAIN; | ||
1112 | return GNUNET_SYSERR; | ||
1113 | } | ||
1114 | } | ||
1115 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); | ||
1104 | } | 1116 | } |
1105 | if (bytes_written == 0) | 1117 | else |
1106 | { | 1118 | { |
1107 | if (n > 0) | 1119 | bytes_written = 0; |
1108 | { | ||
1109 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1110 | "Wrote %u bytes, returning -1 with EAGAIN\n", | ||
1111 | bytes_written); | ||
1112 | errno = EAGAIN; | ||
1113 | return GNUNET_SYSERR; | ||
1114 | } | ||
1115 | } | 1120 | } |
1116 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); | ||
1117 | } | ||
1118 | else | ||
1119 | { | ||
1120 | bytes_written = 0; | ||
1121 | } | ||
1122 | return bytes_written; | 1121 | return bytes_written; |
1123 | #else | 1122 | #else |
1124 | return write (h->fd, buffer, n); | 1123 | return write(h->fd, buffer, n); |
1125 | #endif | 1124 | #endif |
1126 | } | 1125 | } |
1127 | 1126 | ||
@@ -1135,54 +1134,54 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, | |||
1135 | * @return number of bytes written on success, #GNUNET_SYSERR on error | 1134 | * @return number of bytes written on success, #GNUNET_SYSERR on error |
1136 | */ | 1135 | */ |
1137 | ssize_t | 1136 | ssize_t |
1138 | GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h, | 1137 | GNUNET_DISK_file_write_blocking(const struct GNUNET_DISK_FileHandle *h, |
1139 | const void *buffer, | 1138 | const void *buffer, |
1140 | size_t n) | 1139 | size_t n) |
1141 | { | 1140 | { |
1142 | if (NULL == h) | 1141 | if (NULL == h) |
1143 | { | 1142 | { |
1144 | errno = EINVAL; | 1143 | errno = EINVAL; |
1145 | return GNUNET_SYSERR; | 1144 | return GNUNET_SYSERR; |
1146 | } | 1145 | } |
1147 | 1146 | ||
1148 | #ifdef MINGW | 1147 | #ifdef MINGW |
1149 | DWORD bytes_written; | 1148 | DWORD bytes_written; |
1150 | /* We do a non-overlapped write, which is as blocking as it gets */ | 1149 | /* We do a non-overlapped write, which is as blocking as it gets */ |
1151 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); | 1150 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); |
1152 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) | 1151 | if (!WriteFile(h->h, buffer, n, &bytes_written, NULL)) |
1153 | { | 1152 | { |
1154 | SetErrnoFromWinError (GetLastError ()); | 1153 | SetErrnoFromWinError(GetLastError()); |
1155 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1154 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
1156 | "Error writing to pipe: %u\n", | 1155 | "Error writing to pipe: %u\n", |
1157 | GetLastError ()); | 1156 | GetLastError()); |
1158 | return GNUNET_SYSERR; | ||
1159 | } | ||
1160 | if (bytes_written == 0 && n > 0) | ||
1161 | { | ||
1162 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); | ||
1163 | WaitForSingleObject (h->h, INFINITE); | ||
1164 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) | ||
1165 | { | ||
1166 | SetErrnoFromWinError (GetLastError ()); | ||
1167 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1168 | "Error writing to pipe: %u\n", | ||
1169 | GetLastError ()); | ||
1170 | return GNUNET_SYSERR; | 1157 | return GNUNET_SYSERR; |
1171 | } | 1158 | } |
1172 | } | 1159 | if (bytes_written == 0 && n > 0) |
1173 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); | 1160 | { |
1161 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); | ||
1162 | WaitForSingleObject(h->h, INFINITE); | ||
1163 | if (!WriteFile(h->h, buffer, n, &bytes_written, NULL)) | ||
1164 | { | ||
1165 | SetErrnoFromWinError(GetLastError()); | ||
1166 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
1167 | "Error writing to pipe: %u\n", | ||
1168 | GetLastError()); | ||
1169 | return GNUNET_SYSERR; | ||
1170 | } | ||
1171 | } | ||
1172 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); | ||
1174 | return bytes_written; | 1173 | return bytes_written; |
1175 | #else | 1174 | #else |
1176 | int flags; | 1175 | int flags; |
1177 | ssize_t ret; | 1176 | ssize_t ret; |
1178 | 1177 | ||
1179 | /* set to blocking, write, then set back */ | 1178 | /* set to blocking, write, then set back */ |
1180 | flags = fcntl (h->fd, F_GETFL); | 1179 | flags = fcntl(h->fd, F_GETFL); |
1181 | if (0 != (flags & O_NONBLOCK)) | 1180 | if (0 != (flags & O_NONBLOCK)) |
1182 | (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK); | 1181 | (void)fcntl(h->fd, F_SETFL, flags - O_NONBLOCK); |
1183 | ret = write (h->fd, buffer, n); | 1182 | ret = write(h->fd, buffer, n); |
1184 | if (0 == (flags & O_NONBLOCK)) | 1183 | if (0 == (flags & O_NONBLOCK)) |
1185 | (void) fcntl (h->fd, F_SETFL, flags); | 1184 | (void)fcntl(h->fd, F_SETFL, flags); |
1186 | return ret; | 1185 | return ret; |
1187 | #endif | 1186 | #endif |
1188 | } | 1187 | } |
@@ -1199,23 +1198,23 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
1199 | * @return number of bytes written on success, #GNUNET_SYSERR on error | 1198 | * @return number of bytes written on success, #GNUNET_SYSERR on error |
1200 | */ | 1199 | */ |
1201 | ssize_t | 1200 | ssize_t |
1202 | GNUNET_DISK_fn_write (const char *fn, | 1201 | GNUNET_DISK_fn_write(const char *fn, |
1203 | const void *buffer, | 1202 | const void *buffer, |
1204 | size_t n, | 1203 | size_t n, |
1205 | enum GNUNET_DISK_AccessPermissions mode) | 1204 | enum GNUNET_DISK_AccessPermissions mode) |
1206 | { | 1205 | { |
1207 | struct GNUNET_DISK_FileHandle *fh; | 1206 | struct GNUNET_DISK_FileHandle *fh; |
1208 | ssize_t ret; | 1207 | ssize_t ret; |
1209 | 1208 | ||
1210 | fh = | 1209 | fh = |
1211 | GNUNET_DISK_file_open (fn, | 1210 | GNUNET_DISK_file_open(fn, |
1212 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | | 1211 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | |
1213 | GNUNET_DISK_OPEN_CREATE, | 1212 | GNUNET_DISK_OPEN_CREATE, |
1214 | mode); | 1213 | mode); |
1215 | if (! fh) | 1214 | if (!fh) |
1216 | return GNUNET_SYSERR; | 1215 | return GNUNET_SYSERR; |
1217 | ret = GNUNET_DISK_file_write (fh, buffer, n); | 1216 | ret = GNUNET_DISK_file_write(fh, buffer, n); |
1218 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); | 1217 | GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); |
1219 | return ret; | 1218 | return ret; |
1220 | } | 1219 | } |
1221 | 1220 | ||
@@ -1231,9 +1230,9 @@ GNUNET_DISK_fn_write (const char *fn, | |||
1231 | * ieration aborted by callback returning #GNUNET_SYSERR | 1230 | * ieration aborted by callback returning #GNUNET_SYSERR |
1232 | */ | 1231 | */ |
1233 | int | 1232 | int |
1234 | GNUNET_DISK_directory_scan (const char *dir_name, | 1233 | GNUNET_DISK_directory_scan(const char *dir_name, |
1235 | GNUNET_FileNameCallback callback, | 1234 | GNUNET_FileNameCallback callback, |
1236 | void *callback_cls) | 1235 | void *callback_cls) |
1237 | { | 1236 | { |
1238 | DIR *dinfo; | 1237 | DIR *dinfo; |
1239 | struct dirent *finfo; | 1238 | struct dirent *finfo; |
@@ -1245,80 +1244,80 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1245 | unsigned int name_len; | 1244 | unsigned int name_len; |
1246 | unsigned int n_size; | 1245 | unsigned int n_size; |
1247 | 1246 | ||
1248 | GNUNET_assert (NULL != dir_name); | 1247 | GNUNET_assert(NULL != dir_name); |
1249 | dname = GNUNET_STRINGS_filename_expand (dir_name); | 1248 | dname = GNUNET_STRINGS_filename_expand(dir_name); |
1250 | if (NULL == dname) | 1249 | if (NULL == dname) |
1251 | return GNUNET_SYSERR; | 1250 | return GNUNET_SYSERR; |
1252 | while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) | 1251 | while ((strlen(dname) > 0) && (dname[strlen(dname) - 1] == DIR_SEPARATOR)) |
1253 | dname[strlen (dname) - 1] = '\0'; | 1252 | dname[strlen(dname) - 1] = '\0'; |
1254 | if (0 != stat (dname, &istat)) | 1253 | if (0 != stat(dname, &istat)) |
1255 | { | 1254 | { |
1256 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname); | 1255 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", dname); |
1257 | GNUNET_free (dname); | 1256 | GNUNET_free(dname); |
1258 | return GNUNET_SYSERR; | 1257 | return GNUNET_SYSERR; |
1259 | } | 1258 | } |
1260 | if (! S_ISDIR (istat.st_mode)) | 1259 | if (!S_ISDIR(istat.st_mode)) |
1261 | { | 1260 | { |
1262 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1261 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1263 | _ ("Expected `%s' to be a directory!\n"), | 1262 | _("Expected `%s' to be a directory!\n"), |
1264 | dir_name); | 1263 | dir_name); |
1265 | GNUNET_free (dname); | 1264 | GNUNET_free(dname); |
1266 | return GNUNET_SYSERR; | 1265 | return GNUNET_SYSERR; |
1267 | } | 1266 | } |
1268 | errno = 0; | 1267 | errno = 0; |
1269 | dinfo = opendir (dname); | 1268 | dinfo = opendir(dname); |
1270 | if ((EACCES == errno) || (NULL == dinfo)) | 1269 | if ((EACCES == errno) || (NULL == dinfo)) |
1271 | { | 1270 | { |
1272 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); | 1271 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "opendir", dname); |
1273 | if (NULL != dinfo) | 1272 | if (NULL != dinfo) |
1274 | closedir (dinfo); | 1273 | closedir(dinfo); |
1275 | GNUNET_free (dname); | 1274 | GNUNET_free(dname); |
1276 | return GNUNET_SYSERR; | 1275 | return GNUNET_SYSERR; |
1277 | } | 1276 | } |
1278 | name_len = 256; | 1277 | name_len = 256; |
1279 | n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1; | 1278 | n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; |
1280 | name = GNUNET_malloc (n_size); | 1279 | name = GNUNET_malloc(n_size); |
1281 | while (NULL != (finfo = readdir (dinfo))) | 1280 | while (NULL != (finfo = readdir(dinfo))) |
1282 | { | ||
1283 | if ((0 == strcmp (finfo->d_name, ".")) || | ||
1284 | (0 == strcmp (finfo->d_name, ".."))) | ||
1285 | continue; | ||
1286 | if (NULL != callback) | ||
1287 | { | 1281 | { |
1288 | if (name_len < strlen (finfo->d_name)) | 1282 | if ((0 == strcmp(finfo->d_name, ".")) || |
1289 | { | 1283 | (0 == strcmp(finfo->d_name, ".."))) |
1290 | GNUNET_free (name); | 1284 | continue; |
1291 | name_len = strlen (finfo->d_name); | 1285 | if (NULL != callback) |
1292 | n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1; | 1286 | { |
1293 | name = GNUNET_malloc (n_size); | 1287 | if (name_len < strlen(finfo->d_name)) |
1294 | } | 1288 | { |
1295 | /* dname can end in "/" only if dname == "/"; | 1289 | GNUNET_free(name); |
1296 | * if dname does not end in "/", we need to add | 1290 | name_len = strlen(finfo->d_name); |
1297 | * a "/" (otherwise, we must not!) */ | 1291 | n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; |
1298 | GNUNET_snprintf (name, | 1292 | name = GNUNET_malloc(n_size); |
1299 | n_size, | 1293 | } |
1300 | "%s%s%s", | 1294 | /* dname can end in "/" only if dname == "/"; |
1301 | dname, | 1295 | * if dname does not end in "/", we need to add |
1302 | (0 == strcmp (dname, DIR_SEPARATOR_STR)) | 1296 | * a "/" (otherwise, we must not!) */ |
1303 | ? "" | 1297 | GNUNET_snprintf(name, |
1304 | : DIR_SEPARATOR_STR, | 1298 | n_size, |
1305 | finfo->d_name); | 1299 | "%s%s%s", |
1306 | ret = callback (callback_cls, name); | 1300 | dname, |
1307 | if (GNUNET_OK != ret) | 1301 | (0 == strcmp(dname, DIR_SEPARATOR_STR)) |
1308 | { | 1302 | ? "" |
1309 | closedir (dinfo); | 1303 | : DIR_SEPARATOR_STR, |
1310 | GNUNET_free (name); | 1304 | finfo->d_name); |
1311 | GNUNET_free (dname); | 1305 | ret = callback(callback_cls, name); |
1312 | if (GNUNET_NO == ret) | 1306 | if (GNUNET_OK != ret) |
1313 | return count; | 1307 | { |
1314 | return GNUNET_SYSERR; | 1308 | closedir(dinfo); |
1315 | } | 1309 | GNUNET_free(name); |
1310 | GNUNET_free(dname); | ||
1311 | if (GNUNET_NO == ret) | ||
1312 | return count; | ||
1313 | return GNUNET_SYSERR; | ||
1314 | } | ||
1315 | } | ||
1316 | count++; | ||
1316 | } | 1317 | } |
1317 | count++; | 1318 | closedir(dinfo); |
1318 | } | 1319 | GNUNET_free(name); |
1319 | closedir (dinfo); | 1320 | GNUNET_free(dname); |
1320 | GNUNET_free (name); | ||
1321 | GNUNET_free (dname); | ||
1322 | return count; | 1321 | return count; |
1323 | } | 1322 | } |
1324 | 1323 | ||
@@ -1332,10 +1331,10 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1332 | * @return #GNUNET_OK | 1331 | * @return #GNUNET_OK |
1333 | */ | 1332 | */ |
1334 | static int | 1333 | static int |
1335 | remove_helper (void *unused, const char *fn) | 1334 | remove_helper(void *unused, const char *fn) |
1336 | { | 1335 | { |
1337 | (void) unused; | 1336 | (void)unused; |
1338 | (void) GNUNET_DISK_directory_remove (fn); | 1337 | (void)GNUNET_DISK_directory_remove(fn); |
1339 | return GNUNET_OK; | 1338 | return GNUNET_OK; |
1340 | } | 1339 | } |
1341 | 1340 | ||
@@ -1348,37 +1347,37 @@ remove_helper (void *unused, const char *fn) | |||
1348 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 1347 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
1349 | */ | 1348 | */ |
1350 | int | 1349 | int |
1351 | GNUNET_DISK_directory_remove (const char *filename) | 1350 | GNUNET_DISK_directory_remove(const char *filename) |
1352 | { | 1351 | { |
1353 | struct stat istat; | 1352 | struct stat istat; |
1354 | 1353 | ||
1355 | if (NULL == filename) | 1354 | if (NULL == filename) |
1356 | { | 1355 | { |
1357 | GNUNET_break (0); | 1356 | GNUNET_break(0); |
1358 | return GNUNET_SYSERR; | 1357 | return GNUNET_SYSERR; |
1359 | } | 1358 | } |
1360 | if (0 != lstat (filename, &istat)) | 1359 | if (0 != lstat(filename, &istat)) |
1361 | return GNUNET_NO; /* file may not exist... */ | 1360 | return GNUNET_NO; /* file may not exist... */ |
1362 | (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR); | 1361 | (void)chmod(filename, S_IWUSR | S_IRUSR | S_IXUSR); |
1363 | if (0 == unlink (filename)) | 1362 | if (0 == unlink(filename)) |
1364 | return GNUNET_OK; | 1363 | return GNUNET_OK; |
1365 | if ((errno != EISDIR) && | 1364 | if ((errno != EISDIR) && |
1366 | /* EISDIR is not sufficient in all cases, e.g. | 1365 | /* EISDIR is not sufficient in all cases, e.g. |
1367 | * sticky /tmp directory may result in EPERM on BSD. | 1366 | * sticky /tmp directory may result in EPERM on BSD. |
1368 | * So we also explicitly check "isDirectory" */ | 1367 | * So we also explicitly check "isDirectory" */ |
1369 | (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES))) | 1368 | (GNUNET_YES != GNUNET_DISK_directory_test(filename, GNUNET_YES))) |
1370 | { | 1369 | { |
1371 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); | 1370 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); |
1372 | return GNUNET_SYSERR; | 1371 | return GNUNET_SYSERR; |
1373 | } | 1372 | } |
1374 | if (GNUNET_SYSERR == | 1373 | if (GNUNET_SYSERR == |
1375 | GNUNET_DISK_directory_scan (filename, &remove_helper, NULL)) | 1374 | GNUNET_DISK_directory_scan(filename, &remove_helper, NULL)) |
1376 | return GNUNET_SYSERR; | 1375 | return GNUNET_SYSERR; |
1377 | if (0 != rmdir (filename)) | 1376 | if (0 != rmdir(filename)) |
1378 | { | 1377 | { |
1379 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); | 1378 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); |
1380 | return GNUNET_SYSERR; | 1379 | return GNUNET_SYSERR; |
1381 | } | 1380 | } |
1382 | return GNUNET_OK; | 1381 | return GNUNET_OK; |
1383 | } | 1382 | } |
1384 | 1383 | ||
@@ -1391,7 +1390,7 @@ GNUNET_DISK_directory_remove (const char *filename) | |||
1391 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 1390 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
1392 | */ | 1391 | */ |
1393 | int | 1392 | int |
1394 | GNUNET_DISK_file_copy (const char *src, const char *dst) | 1393 | GNUNET_DISK_file_copy(const char *src, const char *dst) |
1395 | { | 1394 | { |
1396 | char *buf; | 1395 | char *buf; |
1397 | uint64_t pos; | 1396 | uint64_t pos; |
@@ -1401,55 +1400,55 @@ GNUNET_DISK_file_copy (const char *src, const char *dst) | |||
1401 | struct GNUNET_DISK_FileHandle *in; | 1400 | struct GNUNET_DISK_FileHandle *in; |
1402 | struct GNUNET_DISK_FileHandle *out; | 1401 | struct GNUNET_DISK_FileHandle *out; |
1403 | 1402 | ||
1404 | if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES)) | 1403 | if (GNUNET_OK != GNUNET_DISK_file_size(src, &size, GNUNET_YES, GNUNET_YES)) |
1405 | { | 1404 | { |
1406 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src); | 1405 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "stat", src); |
1407 | return GNUNET_SYSERR; | 1406 | return GNUNET_SYSERR; |
1408 | } | 1407 | } |
1409 | pos = 0; | 1408 | pos = 0; |
1410 | in = | 1409 | in = |
1411 | GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); | 1410 | GNUNET_DISK_file_open(src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
1412 | if (! in) | 1411 | if (!in) |
1413 | { | 1412 | { |
1414 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src); | 1413 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", src); |
1415 | return GNUNET_SYSERR; | 1414 | return GNUNET_SYSERR; |
1416 | } | 1415 | } |
1417 | out = | 1416 | out = |
1418 | GNUNET_DISK_file_open (dst, | 1417 | GNUNET_DISK_file_open(dst, |
1419 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | | 1418 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | |
1420 | GNUNET_DISK_OPEN_FAILIFEXISTS, | 1419 | GNUNET_DISK_OPEN_FAILIFEXISTS, |
1421 | GNUNET_DISK_PERM_USER_READ | | 1420 | GNUNET_DISK_PERM_USER_READ | |
1422 | GNUNET_DISK_PERM_USER_WRITE | | 1421 | GNUNET_DISK_PERM_USER_WRITE | |
1423 | GNUNET_DISK_PERM_GROUP_READ | | 1422 | GNUNET_DISK_PERM_GROUP_READ | |
1424 | GNUNET_DISK_PERM_GROUP_WRITE); | 1423 | GNUNET_DISK_PERM_GROUP_WRITE); |
1425 | if (! out) | 1424 | if (!out) |
1426 | { | 1425 | { |
1427 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst); | 1426 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", dst); |
1428 | GNUNET_DISK_file_close (in); | 1427 | GNUNET_DISK_file_close(in); |
1429 | return GNUNET_SYSERR; | 1428 | return GNUNET_SYSERR; |
1430 | } | 1429 | } |
1431 | buf = GNUNET_malloc (COPY_BLK_SIZE); | 1430 | buf = GNUNET_malloc(COPY_BLK_SIZE); |
1432 | while (pos < size) | 1431 | while (pos < size) |
1433 | { | 1432 | { |
1434 | len = COPY_BLK_SIZE; | 1433 | len = COPY_BLK_SIZE; |
1435 | if (len > size - pos) | 1434 | if (len > size - pos) |
1436 | len = size - pos; | 1435 | len = size - pos; |
1437 | sret = GNUNET_DISK_file_read (in, buf, len); | 1436 | sret = GNUNET_DISK_file_read(in, buf, len); |
1438 | if ((sret < 0) || (len != (size_t) sret)) | 1437 | if ((sret < 0) || (len != (size_t)sret)) |
1439 | goto FAIL; | 1438 | goto FAIL; |
1440 | sret = GNUNET_DISK_file_write (out, buf, len); | 1439 | sret = GNUNET_DISK_file_write(out, buf, len); |
1441 | if ((sret < 0) || (len != (size_t) sret)) | 1440 | if ((sret < 0) || (len != (size_t)sret)) |
1442 | goto FAIL; | 1441 | goto FAIL; |
1443 | pos += len; | 1442 | pos += len; |
1444 | } | 1443 | } |
1445 | GNUNET_free (buf); | 1444 | GNUNET_free(buf); |
1446 | GNUNET_DISK_file_close (in); | 1445 | GNUNET_DISK_file_close(in); |
1447 | GNUNET_DISK_file_close (out); | 1446 | GNUNET_DISK_file_close(out); |
1448 | return GNUNET_OK; | 1447 | return GNUNET_OK; |
1449 | FAIL: | 1448 | FAIL: |
1450 | GNUNET_free (buf); | 1449 | GNUNET_free(buf); |
1451 | GNUNET_DISK_file_close (in); | 1450 | GNUNET_DISK_file_close(in); |
1452 | GNUNET_DISK_file_close (out); | 1451 | GNUNET_DISK_file_close(out); |
1453 | return GNUNET_SYSERR; | 1452 | return GNUNET_SYSERR; |
1454 | } | 1453 | } |
1455 | 1454 | ||
@@ -1459,21 +1458,21 @@ FAIL: | |||
1459 | * @param fn the filename to canonicalize | 1458 | * @param fn the filename to canonicalize |
1460 | */ | 1459 | */ |
1461 | void | 1460 | void |
1462 | GNUNET_DISK_filename_canonicalize (char *fn) | 1461 | GNUNET_DISK_filename_canonicalize(char *fn) |
1463 | { | 1462 | { |
1464 | char *idx; | 1463 | char *idx; |
1465 | char c; | 1464 | char c; |
1466 | 1465 | ||
1467 | for (idx = fn; *idx; idx++) | 1466 | for (idx = fn; *idx; idx++) |
1468 | { | ||
1469 | c = *idx; | ||
1470 | |||
1471 | if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || | ||
1472 | c == '<' || c == '>' || c == '|') | ||
1473 | { | 1467 | { |
1474 | *idx = '_'; | 1468 | c = *idx; |
1469 | |||
1470 | if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || | ||
1471 | c == '<' || c == '>' || c == '|') | ||
1472 | { | ||
1473 | *idx = '_'; | ||
1474 | } | ||
1475 | } | 1475 | } |
1476 | } | ||
1477 | } | 1476 | } |
1478 | 1477 | ||
1479 | 1478 | ||
@@ -1485,25 +1484,25 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
1485 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 1484 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
1486 | */ | 1485 | */ |
1487 | int | 1486 | int |
1488 | GNUNET_DISK_file_change_owner (const char *filename, const char *user) | 1487 | GNUNET_DISK_file_change_owner(const char *filename, const char *user) |
1489 | { | 1488 | { |
1490 | #ifndef MINGW | 1489 | #ifndef MINGW |
1491 | struct passwd *pws; | 1490 | struct passwd *pws; |
1492 | 1491 | ||
1493 | pws = getpwnam (user); | 1492 | pws = getpwnam(user); |
1494 | if (NULL == pws) | 1493 | if (NULL == pws) |
1495 | { | 1494 | { |
1496 | LOG (GNUNET_ERROR_TYPE_ERROR, | 1495 | LOG(GNUNET_ERROR_TYPE_ERROR, |
1497 | _ ("Cannot obtain information about user `%s': %s\n"), | 1496 | _("Cannot obtain information about user `%s': %s\n"), |
1498 | user, | 1497 | user, |
1499 | strerror (errno)); | 1498 | strerror(errno)); |
1500 | return GNUNET_SYSERR; | 1499 | return GNUNET_SYSERR; |
1501 | } | 1500 | } |
1502 | if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) | 1501 | if (0 != chown(filename, pws->pw_uid, pws->pw_gid)) |
1503 | { | 1502 | { |
1504 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); | 1503 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "chown", filename); |
1505 | return GNUNET_SYSERR; | 1504 | return GNUNET_SYSERR; |
1506 | } | 1505 | } |
1507 | #endif | 1506 | #endif |
1508 | return GNUNET_OK; | 1507 | return GNUNET_OK; |
1509 | } | 1508 | } |
@@ -1519,51 +1518,51 @@ GNUNET_DISK_file_change_owner (const char *filename, const char *user) | |||
1519 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 1518 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
1520 | */ | 1519 | */ |
1521 | int | 1520 | int |
1522 | GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, | 1521 | GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, |
1523 | off_t lock_start, | 1522 | off_t lock_start, |
1524 | off_t lock_end, | 1523 | off_t lock_end, |
1525 | int excl) | 1524 | int excl) |
1526 | { | 1525 | { |
1527 | if (fh == NULL) | 1526 | if (fh == NULL) |
1528 | { | 1527 | { |
1529 | errno = EINVAL; | 1528 | errno = EINVAL; |
1530 | return GNUNET_SYSERR; | 1529 | return GNUNET_SYSERR; |
1531 | } | 1530 | } |
1532 | 1531 | ||
1533 | #ifndef MINGW | 1532 | #ifndef MINGW |
1534 | struct flock fl; | 1533 | struct flock fl; |
1535 | 1534 | ||
1536 | memset (&fl, 0, sizeof (struct flock)); | 1535 | memset(&fl, 0, sizeof(struct flock)); |
1537 | fl.l_type = excl ? F_WRLCK : F_RDLCK; | 1536 | fl.l_type = excl ? F_WRLCK : F_RDLCK; |
1538 | fl.l_whence = SEEK_SET; | 1537 | fl.l_whence = SEEK_SET; |
1539 | fl.l_start = lock_start; | 1538 | fl.l_start = lock_start; |
1540 | fl.l_len = lock_end; | 1539 | fl.l_len = lock_end; |
1541 | 1540 | ||
1542 | return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; | 1541 | return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; |
1543 | #else | 1542 | #else |
1544 | OVERLAPPED o; | 1543 | OVERLAPPED o; |
1545 | off_t diff = lock_end - lock_start; | 1544 | off_t diff = lock_end - lock_start; |
1546 | DWORD diff_low, diff_high; | 1545 | DWORD diff_low, diff_high; |
1547 | diff_low = (DWORD) (diff & 0xFFFFFFFF); | 1546 | diff_low = (DWORD)(diff & 0xFFFFFFFF); |
1548 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1547 | diff_high = (DWORD)((diff >> (sizeof(DWORD) * 8)) & 0xFFFFFFFF); |
1549 | 1548 | ||
1550 | memset (&o, 0, sizeof (OVERLAPPED)); | 1549 | memset(&o, 0, sizeof(OVERLAPPED)); |
1551 | o.Offset = (DWORD) (lock_start & 0xFFFFFFFF); | 1550 | o.Offset = (DWORD)(lock_start & 0xFFFFFFFF); |
1552 | ; | 1551 | ; |
1553 | o.OffsetHigh = | 1552 | o.OffsetHigh = |
1554 | (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1553 | (DWORD)(((lock_start & ~0xFFFFFFFF) >> (sizeof(DWORD) * 8)) & 0xFFFFFFFF); |
1555 | 1554 | ||
1556 | if (! LockFileEx (fh->h, | 1555 | if (!LockFileEx(fh->h, |
1557 | (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | | 1556 | (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | |
1558 | LOCKFILE_FAIL_IMMEDIATELY, | 1557 | LOCKFILE_FAIL_IMMEDIATELY, |
1559 | 0, | 1558 | 0, |
1560 | diff_low, | 1559 | diff_low, |
1561 | diff_high, | 1560 | diff_high, |
1562 | &o)) | 1561 | &o)) |
1563 | { | 1562 | { |
1564 | SetErrnoFromWinError (GetLastError ()); | 1563 | SetErrnoFromWinError(GetLastError()); |
1565 | return GNUNET_SYSERR; | 1564 | return GNUNET_SYSERR; |
1566 | } | 1565 | } |
1567 | 1566 | ||
1568 | return GNUNET_OK; | 1567 | return GNUNET_OK; |
1569 | #endif | 1568 | #endif |
@@ -1579,44 +1578,44 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, | |||
1579 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 1578 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
1580 | */ | 1579 | */ |
1581 | int | 1580 | int |
1582 | GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, | 1581 | GNUNET_DISK_file_unlock(struct GNUNET_DISK_FileHandle *fh, |
1583 | off_t unlock_start, | 1582 | off_t unlock_start, |
1584 | off_t unlock_end) | 1583 | off_t unlock_end) |
1585 | { | 1584 | { |
1586 | if (fh == NULL) | 1585 | if (fh == NULL) |
1587 | { | 1586 | { |
1588 | errno = EINVAL; | 1587 | errno = EINVAL; |
1589 | return GNUNET_SYSERR; | 1588 | return GNUNET_SYSERR; |
1590 | } | 1589 | } |
1591 | 1590 | ||
1592 | #ifndef MINGW | 1591 | #ifndef MINGW |
1593 | struct flock fl; | 1592 | struct flock fl; |
1594 | 1593 | ||
1595 | memset (&fl, 0, sizeof (struct flock)); | 1594 | memset(&fl, 0, sizeof(struct flock)); |
1596 | fl.l_type = F_UNLCK; | 1595 | fl.l_type = F_UNLCK; |
1597 | fl.l_whence = SEEK_SET; | 1596 | fl.l_whence = SEEK_SET; |
1598 | fl.l_start = unlock_start; | 1597 | fl.l_start = unlock_start; |
1599 | fl.l_len = unlock_end; | 1598 | fl.l_len = unlock_end; |
1600 | 1599 | ||
1601 | return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; | 1600 | return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; |
1602 | #else | 1601 | #else |
1603 | OVERLAPPED o; | 1602 | OVERLAPPED o; |
1604 | off_t diff = unlock_end - unlock_start; | 1603 | off_t diff = unlock_end - unlock_start; |
1605 | DWORD diff_low, diff_high; | 1604 | DWORD diff_low, diff_high; |
1606 | diff_low = (DWORD) (diff & 0xFFFFFFFF); | 1605 | diff_low = (DWORD)(diff & 0xFFFFFFFF); |
1607 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1606 | diff_high = (DWORD)((diff >> (sizeof(DWORD) * 8)) & 0xFFFFFFFF); |
1608 | 1607 | ||
1609 | memset (&o, 0, sizeof (OVERLAPPED)); | 1608 | memset(&o, 0, sizeof(OVERLAPPED)); |
1610 | o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF); | 1609 | o.Offset = (DWORD)(unlock_start & 0xFFFFFFFF); |
1611 | ; | 1610 | ; |
1612 | o.OffsetHigh = (DWORD) ( | 1611 | o.OffsetHigh = (DWORD)( |
1613 | ((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1612 | ((unlock_start & ~0xFFFFFFFF) >> (sizeof(DWORD) * 8)) & 0xFFFFFFFF); |
1614 | 1613 | ||
1615 | if (! UnlockFileEx (fh->h, 0, diff_low, diff_high, &o)) | 1614 | if (!UnlockFileEx(fh->h, 0, diff_low, diff_high, &o)) |
1616 | { | 1615 | { |
1617 | SetErrnoFromWinError (GetLastError ()); | 1616 | SetErrnoFromWinError(GetLastError()); |
1618 | return GNUNET_SYSERR; | 1617 | return GNUNET_SYSERR; |
1619 | } | 1618 | } |
1620 | 1619 | ||
1621 | return GNUNET_OK; | 1620 | return GNUNET_OK; |
1622 | #endif | 1621 | #endif |
@@ -1636,9 +1635,9 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, | |||
1636 | * @return IO handle on success, NULL on error | 1635 | * @return IO handle on success, NULL on error |
1637 | */ | 1636 | */ |
1638 | struct GNUNET_DISK_FileHandle * | 1637 | struct GNUNET_DISK_FileHandle * |
1639 | GNUNET_DISK_file_open (const char *fn, | 1638 | GNUNET_DISK_file_open(const char *fn, |
1640 | enum GNUNET_DISK_OpenFlags flags, | 1639 | enum GNUNET_DISK_OpenFlags flags, |
1641 | enum GNUNET_DISK_AccessPermissions perm) | 1640 | enum GNUNET_DISK_AccessPermissions perm) |
1642 | { | 1641 | { |
1643 | char *expfn; | 1642 | char *expfn; |
1644 | struct GNUNET_DISK_FileHandle *ret; | 1643 | struct GNUNET_DISK_FileHandle *ret; |
@@ -1654,7 +1653,7 @@ GNUNET_DISK_file_open (const char *fn, | |||
1654 | int fd; | 1653 | int fd; |
1655 | #endif | 1654 | #endif |
1656 | 1655 | ||
1657 | expfn = GNUNET_STRINGS_filename_expand (fn); | 1656 | expfn = GNUNET_STRINGS_filename_expand(fn); |
1658 | if (NULL == expfn) | 1657 | if (NULL == expfn) |
1659 | return NULL; | 1658 | return NULL; |
1660 | #ifndef MINGW | 1659 | #ifndef MINGW |
@@ -1666,43 +1665,43 @@ GNUNET_DISK_file_open (const char *fn, | |||
1666 | else if (flags & GNUNET_DISK_OPEN_WRITE) | 1665 | else if (flags & GNUNET_DISK_OPEN_WRITE) |
1667 | oflags = O_WRONLY; | 1666 | oflags = O_WRONLY; |
1668 | else | 1667 | else |
1669 | { | 1668 | { |
1670 | GNUNET_break (0); | 1669 | GNUNET_break(0); |
1671 | GNUNET_free (expfn); | 1670 | GNUNET_free(expfn); |
1672 | return NULL; | 1671 | return NULL; |
1673 | } | 1672 | } |
1674 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1673 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1675 | oflags |= (O_CREAT | O_EXCL); | 1674 | oflags |= (O_CREAT | O_EXCL); |
1676 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1675 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1677 | oflags |= O_TRUNC; | 1676 | oflags |= O_TRUNC; |
1678 | if (flags & GNUNET_DISK_OPEN_APPEND) | 1677 | if (flags & GNUNET_DISK_OPEN_APPEND) |
1679 | oflags |= O_APPEND; | 1678 | oflags |= O_APPEND; |
1680 | if (GNUNET_NO == GNUNET_DISK_file_test (fn)) | 1679 | if (GNUNET_NO == GNUNET_DISK_file_test(fn)) |
1681 | { | ||
1682 | if (flags & GNUNET_DISK_OPEN_CREATE) | ||
1683 | { | 1680 | { |
1684 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1681 | if (flags & GNUNET_DISK_OPEN_CREATE) |
1685 | oflags |= O_CREAT; | 1682 | { |
1686 | mode = translate_unix_perms (perm); | 1683 | (void)GNUNET_DISK_directory_create_for_file(expfn); |
1684 | oflags |= O_CREAT; | ||
1685 | mode = translate_unix_perms(perm); | ||
1686 | } | ||
1687 | } | 1687 | } |
1688 | } | ||
1689 | 1688 | ||
1690 | fd = open (expfn, | 1689 | fd = open(expfn, |
1691 | oflags | 1690 | oflags |
1692 | #if O_CLOEXEC | 1691 | #if O_CLOEXEC |
1693 | | O_CLOEXEC | 1692 | | O_CLOEXEC |
1694 | #endif | 1693 | #endif |
1695 | | O_LARGEFILE, | 1694 | | O_LARGEFILE, |
1696 | mode); | 1695 | mode); |
1697 | if (fd == -1) | 1696 | if (fd == -1) |
1698 | { | 1697 | { |
1699 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) | 1698 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) |
1700 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn); | 1699 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "open", expfn); |
1701 | else | 1700 | else |
1702 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn); | 1701 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "open", expfn); |
1703 | GNUNET_free (expfn); | 1702 | GNUNET_free(expfn); |
1704 | return NULL; | 1703 | return NULL; |
1705 | } | 1704 | } |
1706 | #else | 1705 | #else |
1707 | access = 0; | 1706 | access = 0; |
1708 | disp = OPEN_ALWAYS; | 1707 | disp = OPEN_ALWAYS; |
@@ -1715,66 +1714,66 @@ GNUNET_DISK_file_open (const char *fn, | |||
1715 | access = FILE_WRITE_DATA; | 1714 | access = FILE_WRITE_DATA; |
1716 | 1715 | ||
1717 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1716 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1718 | { | 1717 | { |
1719 | disp = CREATE_NEW; | 1718 | disp = CREATE_NEW; |
1720 | } | 1719 | } |
1721 | else if (flags & GNUNET_DISK_OPEN_CREATE) | 1720 | else if (flags & GNUNET_DISK_OPEN_CREATE) |
1722 | { | 1721 | { |
1723 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1722 | (void)GNUNET_DISK_directory_create_for_file(expfn); |
1724 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1723 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1725 | disp = CREATE_ALWAYS; | 1724 | disp = CREATE_ALWAYS; |
1726 | else | 1725 | else |
1727 | disp = OPEN_ALWAYS; | 1726 | disp = OPEN_ALWAYS; |
1728 | } | 1727 | } |
1729 | else if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1728 | else if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1730 | { | 1729 | { |
1731 | disp = TRUNCATE_EXISTING; | 1730 | disp = TRUNCATE_EXISTING; |
1732 | } | 1731 | } |
1733 | else | 1732 | else |
1734 | { | 1733 | { |
1735 | disp = OPEN_EXISTING; | 1734 | disp = OPEN_EXISTING; |
1736 | } | 1735 | } |
1737 | 1736 | ||
1738 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (expfn, wexpfn)) | 1737 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn)) |
1739 | h = CreateFileW (wexpfn, | 1738 | h = CreateFileW(wexpfn, |
1740 | access, | 1739 | access, |
1741 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, | 1740 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
1742 | NULL, | 1741 | NULL, |
1743 | disp, | 1742 | disp, |
1744 | FILE_ATTRIBUTE_NORMAL, | 1743 | FILE_ATTRIBUTE_NORMAL, |
1745 | NULL); | 1744 | NULL); |
1746 | else | 1745 | else |
1747 | h = INVALID_HANDLE_VALUE; | 1746 | h = INVALID_HANDLE_VALUE; |
1748 | if (h == INVALID_HANDLE_VALUE) | 1747 | if (h == INVALID_HANDLE_VALUE) |
1749 | { | ||
1750 | int err; | ||
1751 | SetErrnoFromWinError (GetLastError ()); | ||
1752 | err = errno; | ||
1753 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_INFO, "open", expfn); | ||
1754 | GNUNET_free (expfn); | ||
1755 | errno = err; | ||
1756 | return NULL; | ||
1757 | } | ||
1758 | |||
1759 | if (flags & GNUNET_DISK_OPEN_APPEND) | ||
1760 | if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) | ||
1761 | { | 1748 | { |
1762 | SetErrnoFromWinError (GetLastError ()); | 1749 | int err; |
1763 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn); | 1750 | SetErrnoFromWinError(GetLastError()); |
1764 | CloseHandle (h); | 1751 | err = errno; |
1765 | GNUNET_free (expfn); | 1752 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_INFO, "open", expfn); |
1753 | GNUNET_free(expfn); | ||
1754 | errno = err; | ||
1766 | return NULL; | 1755 | return NULL; |
1767 | } | 1756 | } |
1757 | |||
1758 | if (flags & GNUNET_DISK_OPEN_APPEND) | ||
1759 | if (SetFilePointer(h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER) | ||
1760 | { | ||
1761 | SetErrnoFromWinError(GetLastError()); | ||
1762 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", expfn); | ||
1763 | CloseHandle(h); | ||
1764 | GNUNET_free(expfn); | ||
1765 | return NULL; | ||
1766 | } | ||
1768 | #endif | 1767 | #endif |
1769 | 1768 | ||
1770 | ret = GNUNET_new (struct GNUNET_DISK_FileHandle); | 1769 | ret = GNUNET_new(struct GNUNET_DISK_FileHandle); |
1771 | #ifdef MINGW | 1770 | #ifdef MINGW |
1772 | ret->h = h; | 1771 | ret->h = h; |
1773 | ret->type = GNUNET_DISK_HANLDE_TYPE_FILE; | 1772 | ret->type = GNUNET_DISK_HANLDE_TYPE_FILE; |
1774 | #else | 1773 | #else |
1775 | ret->fd = fd; | 1774 | ret->fd = fd; |
1776 | #endif | 1775 | #endif |
1777 | GNUNET_free (expfn); | 1776 | GNUNET_free(expfn); |
1778 | return ret; | 1777 | return ret; |
1779 | } | 1778 | } |
1780 | 1779 | ||
@@ -1786,52 +1785,53 @@ GNUNET_DISK_file_open (const char *fn, | |||
1786 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise | 1785 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise |
1787 | */ | 1786 | */ |
1788 | int | 1787 | int |
1789 | GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) | 1788 | GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h) |
1790 | { | 1789 | { |
1791 | int ret; | 1790 | int ret; |
1791 | |||
1792 | if (h == NULL) | 1792 | if (h == NULL) |
1793 | { | 1793 | { |
1794 | errno = EINVAL; | 1794 | errno = EINVAL; |
1795 | return GNUNET_SYSERR; | 1795 | return GNUNET_SYSERR; |
1796 | } | 1796 | } |
1797 | 1797 | ||
1798 | ret = GNUNET_OK; | 1798 | ret = GNUNET_OK; |
1799 | 1799 | ||
1800 | #if MINGW | 1800 | #if MINGW |
1801 | if (! CloseHandle (h->h)) | 1801 | if (!CloseHandle(h->h)) |
1802 | { | ||
1803 | SetErrnoFromWinError (GetLastError ()); | ||
1804 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); | ||
1805 | ret = GNUNET_SYSERR; | ||
1806 | } | ||
1807 | if (h->oOverlapRead) | ||
1808 | { | ||
1809 | if (! CloseHandle (h->oOverlapRead->hEvent)) | ||
1810 | { | 1802 | { |
1811 | SetErrnoFromWinError (GetLastError ()); | 1803 | SetErrnoFromWinError(GetLastError()); |
1812 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); | 1804 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); |
1813 | ret = GNUNET_SYSERR; | 1805 | ret = GNUNET_SYSERR; |
1814 | } | 1806 | } |
1815 | GNUNET_free (h->oOverlapRead); | 1807 | if (h->oOverlapRead) |
1816 | } | 1808 | { |
1809 | if (!CloseHandle(h->oOverlapRead->hEvent)) | ||
1810 | { | ||
1811 | SetErrnoFromWinError(GetLastError()); | ||
1812 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); | ||
1813 | ret = GNUNET_SYSERR; | ||
1814 | } | ||
1815 | GNUNET_free(h->oOverlapRead); | ||
1816 | } | ||
1817 | if (h->oOverlapWrite) | 1817 | if (h->oOverlapWrite) |
1818 | { | ||
1819 | if (! CloseHandle (h->oOverlapWrite->hEvent)) | ||
1820 | { | 1818 | { |
1821 | SetErrnoFromWinError (GetLastError ()); | 1819 | if (!CloseHandle(h->oOverlapWrite->hEvent)) |
1822 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); | 1820 | { |
1823 | ret = GNUNET_SYSERR; | 1821 | SetErrnoFromWinError(GetLastError()); |
1822 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); | ||
1823 | ret = GNUNET_SYSERR; | ||
1824 | } | ||
1825 | GNUNET_free(h->oOverlapWrite); | ||
1824 | } | 1826 | } |
1825 | GNUNET_free (h->oOverlapWrite); | ||
1826 | } | ||
1827 | #else | 1827 | #else |
1828 | if (close (h->fd) != 0) | 1828 | if (close(h->fd) != 0) |
1829 | { | 1829 | { |
1830 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); | 1830 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); |
1831 | ret = GNUNET_SYSERR; | 1831 | ret = GNUNET_SYSERR; |
1832 | } | 1832 | } |
1833 | #endif | 1833 | #endif |
1834 | GNUNET_free (h); | 1834 | GNUNET_free(h); |
1835 | return ret; | 1835 | return ret; |
1836 | } | 1836 | } |
1837 | 1837 | ||
@@ -1844,56 +1844,59 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) | |||
1844 | * @return GNUnet file handle corresponding to the W32 handle | 1844 | * @return GNUnet file handle corresponding to the W32 handle |
1845 | */ | 1845 | */ |
1846 | struct GNUNET_DISK_FileHandle * | 1846 | struct GNUNET_DISK_FileHandle * |
1847 | GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh) | 1847 | GNUNET_DISK_get_handle_from_w32_handle(HANDLE osfh) |
1848 | { | 1848 | { |
1849 | struct GNUNET_DISK_FileHandle *fh; | 1849 | struct GNUNET_DISK_FileHandle *fh; |
1850 | DWORD dwret; | 1850 | DWORD dwret; |
1851 | enum GNUNET_FILE_Type ftype; | 1851 | enum GNUNET_FILE_Type ftype; |
1852 | 1852 | ||
1853 | dwret = GetFileType (osfh); | 1853 | dwret = GetFileType(osfh); |
1854 | switch (dwret) | 1854 | switch (dwret) |
1855 | { | 1855 | { |
1856 | case FILE_TYPE_DISK: | 1856 | case FILE_TYPE_DISK: |
1857 | ftype = GNUNET_DISK_HANLDE_TYPE_FILE; | 1857 | ftype = GNUNET_DISK_HANLDE_TYPE_FILE; |
1858 | break; | 1858 | break; |
1859 | case FILE_TYPE_PIPE: | 1859 | |
1860 | ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; | 1860 | case FILE_TYPE_PIPE: |
1861 | break; | 1861 | ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; |
1862 | case FILE_TYPE_UNKNOWN: | 1862 | break; |
1863 | if ((GetLastError () == NO_ERROR) || | 1863 | |
1864 | (GetLastError () == ERROR_INVALID_HANDLE)) | 1864 | case FILE_TYPE_UNKNOWN: |
1865 | { | 1865 | if ((GetLastError() == NO_ERROR) || |
1866 | if (0 != ResetEvent (osfh)) | 1866 | (GetLastError() == ERROR_INVALID_HANDLE)) |
1867 | ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; | 1867 | { |
1868 | if (0 != ResetEvent(osfh)) | ||
1869 | ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; | ||
1870 | else | ||
1871 | return NULL; | ||
1872 | } | ||
1868 | else | 1873 | else |
1869 | return NULL; | 1874 | return NULL; |
1870 | } | 1875 | break; |
1871 | else | 1876 | |
1877 | default: | ||
1872 | return NULL; | 1878 | return NULL; |
1873 | break; | 1879 | } |
1874 | default: | ||
1875 | return NULL; | ||
1876 | } | ||
1877 | 1880 | ||
1878 | fh = GNUNET_new (struct GNUNET_DISK_FileHandle); | 1881 | fh = GNUNET_new(struct GNUNET_DISK_FileHandle); |
1879 | 1882 | ||
1880 | fh->h = osfh; | 1883 | fh->h = osfh; |
1881 | fh->type = ftype; | 1884 | fh->type = ftype; |
1882 | if (ftype == GNUNET_DISK_HANLDE_TYPE_PIPE) | 1885 | if (ftype == GNUNET_DISK_HANLDE_TYPE_PIPE) |
1883 | { | 1886 | { |
1884 | /** | 1887 | /** |
1885 | * Note that we can't make it overlapped if it isn't already. | 1888 | * Note that we can't make it overlapped if it isn't already. |
1886 | * (ReOpenFile() is only available in 2003/Vista). | 1889 | * (ReOpenFile() is only available in 2003/Vista). |
1887 | * The process that opened this file in the first place (usually a parent | 1890 | * The process that opened this file in the first place (usually a parent |
1888 | * process, if this is stdin/stdout/stderr) must make it overlapped, | 1891 | * process, if this is stdin/stdout/stderr) must make it overlapped, |
1889 | * otherwise we're screwed, as selecting on non-overlapped handle | 1892 | * otherwise we're screwed, as selecting on non-overlapped handle |
1890 | * will block. | 1893 | * will block. |
1891 | */ | 1894 | */ |
1892 | fh->oOverlapRead = GNUNET_new (OVERLAPPED); | 1895 | fh->oOverlapRead = GNUNET_new(OVERLAPPED); |
1893 | fh->oOverlapWrite = GNUNET_new (OVERLAPPED); | 1896 | fh->oOverlapWrite = GNUNET_new(OVERLAPPED); |
1894 | fh->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 1897 | fh->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
1895 | fh->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 1898 | fh->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
1896 | } | 1899 | } |
1897 | 1900 | ||
1898 | return fh; | 1901 | return fh; |
1899 | } | 1902 | } |
@@ -1906,25 +1909,25 @@ GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh) | |||
1906 | * @return file handle corresponding to the descriptor, NULL on error | 1909 | * @return file handle corresponding to the descriptor, NULL on error |
1907 | */ | 1910 | */ |
1908 | struct GNUNET_DISK_FileHandle * | 1911 | struct GNUNET_DISK_FileHandle * |
1909 | GNUNET_DISK_get_handle_from_int_fd (int fno) | 1912 | GNUNET_DISK_get_handle_from_int_fd(int fno) |
1910 | { | 1913 | { |
1911 | struct GNUNET_DISK_FileHandle *fh; | 1914 | struct GNUNET_DISK_FileHandle *fh; |
1912 | 1915 | ||
1913 | if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno)) | 1916 | if ((((off_t)-1) == lseek(fno, 0, SEEK_CUR)) && (EBADF == errno)) |
1914 | return NULL; /* invalid FD */ | 1917 | return NULL; /* invalid FD */ |
1915 | 1918 | ||
1916 | #ifndef WINDOWS | 1919 | #ifndef WINDOWS |
1917 | fh = GNUNET_new (struct GNUNET_DISK_FileHandle); | 1920 | fh = GNUNET_new(struct GNUNET_DISK_FileHandle); |
1918 | 1921 | ||
1919 | fh->fd = fno; | 1922 | fh->fd = fno; |
1920 | #else | 1923 | #else |
1921 | intptr_t osfh; | 1924 | intptr_t osfh; |
1922 | 1925 | ||
1923 | osfh = _get_osfhandle (fno); | 1926 | osfh = _get_osfhandle(fno); |
1924 | if (INVALID_HANDLE_VALUE == (HANDLE) osfh) | 1927 | if (INVALID_HANDLE_VALUE == (HANDLE)osfh) |
1925 | return NULL; | 1928 | return NULL; |
1926 | 1929 | ||
1927 | fh = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) osfh); | 1930 | fh = GNUNET_DISK_get_handle_from_w32_handle((HANDLE)osfh); |
1928 | #endif | 1931 | #endif |
1929 | 1932 | ||
1930 | return fh; | 1933 | return fh; |
@@ -1938,23 +1941,22 @@ GNUNET_DISK_get_handle_from_int_fd (int fno) | |||
1938 | * @return file handle corresponding to the descriptor | 1941 | * @return file handle corresponding to the descriptor |
1939 | */ | 1942 | */ |
1940 | struct GNUNET_DISK_FileHandle * | 1943 | struct GNUNET_DISK_FileHandle * |
1941 | GNUNET_DISK_get_handle_from_native (FILE *fd) | 1944 | GNUNET_DISK_get_handle_from_native(FILE *fd) |
1942 | { | 1945 | { |
1943 | int fno; | 1946 | int fno; |
1944 | 1947 | ||
1945 | fno = fileno (fd); | 1948 | fno = fileno(fd); |
1946 | if (-1 == fno) | 1949 | if (-1 == fno) |
1947 | return NULL; | 1950 | return NULL; |
1948 | 1951 | ||
1949 | return GNUNET_DISK_get_handle_from_int_fd (fno); | 1952 | return GNUNET_DISK_get_handle_from_int_fd(fno); |
1950 | } | 1953 | } |
1951 | 1954 | ||
1952 | 1955 | ||
1953 | /** | 1956 | /** |
1954 | * Handle for a memory-mapping operation. | 1957 | * Handle for a memory-mapping operation. |
1955 | */ | 1958 | */ |
1956 | struct GNUNET_DISK_MapHandle | 1959 | struct GNUNET_DISK_MapHandle { |
1957 | { | ||
1958 | /** | 1960 | /** |
1959 | * Address where the map is in memory. | 1961 | * Address where the map is in memory. |
1960 | */ | 1962 | */ |
@@ -1975,7 +1977,7 @@ struct GNUNET_DISK_MapHandle | |||
1975 | 1977 | ||
1976 | 1978 | ||
1977 | #ifndef MAP_FAILED | 1979 | #ifndef MAP_FAILED |
1978 | #define MAP_FAILED ((void *) -1) | 1980 | #define MAP_FAILED ((void *)-1) |
1979 | #endif | 1981 | #endif |
1980 | 1982 | ||
1981 | /** | 1983 | /** |
@@ -1988,58 +1990,58 @@ struct GNUNET_DISK_MapHandle | |||
1988 | * @return pointer to the mapped memory region, NULL on failure | 1990 | * @return pointer to the mapped memory region, NULL on failure |
1989 | */ | 1991 | */ |
1990 | void * | 1992 | void * |
1991 | GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | 1993 | GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, |
1992 | struct GNUNET_DISK_MapHandle **m, | 1994 | struct GNUNET_DISK_MapHandle **m, |
1993 | enum GNUNET_DISK_MapType access, | 1995 | enum GNUNET_DISK_MapType access, |
1994 | size_t len) | 1996 | size_t len) |
1995 | { | 1997 | { |
1996 | if (NULL == h) | 1998 | if (NULL == h) |
1997 | { | 1999 | { |
1998 | errno = EINVAL; | 2000 | errno = EINVAL; |
1999 | return NULL; | 2001 | return NULL; |
2000 | } | 2002 | } |
2001 | 2003 | ||
2002 | #ifdef MINGW | 2004 | #ifdef MINGW |
2003 | DWORD mapAccess, protect; | 2005 | DWORD mapAccess, protect; |
2004 | 2006 | ||
2005 | if ((access & GNUNET_DISK_MAP_TYPE_READ) && | 2007 | if ((access & GNUNET_DISK_MAP_TYPE_READ) && |
2006 | (access & GNUNET_DISK_MAP_TYPE_WRITE)) | 2008 | (access & GNUNET_DISK_MAP_TYPE_WRITE)) |
2007 | { | 2009 | { |
2008 | protect = PAGE_READWRITE; | 2010 | protect = PAGE_READWRITE; |
2009 | mapAccess = FILE_MAP_ALL_ACCESS; | 2011 | mapAccess = FILE_MAP_ALL_ACCESS; |
2010 | } | 2012 | } |
2011 | else if (access & GNUNET_DISK_MAP_TYPE_READ) | 2013 | else if (access & GNUNET_DISK_MAP_TYPE_READ) |
2012 | { | 2014 | { |
2013 | protect = PAGE_READONLY; | 2015 | protect = PAGE_READONLY; |
2014 | mapAccess = FILE_MAP_READ; | 2016 | mapAccess = FILE_MAP_READ; |
2015 | } | 2017 | } |
2016 | else if (access & GNUNET_DISK_MAP_TYPE_WRITE) | 2018 | else if (access & GNUNET_DISK_MAP_TYPE_WRITE) |
2017 | { | 2019 | { |
2018 | protect = PAGE_READWRITE; | 2020 | protect = PAGE_READWRITE; |
2019 | mapAccess = FILE_MAP_WRITE; | 2021 | mapAccess = FILE_MAP_WRITE; |
2020 | } | 2022 | } |
2021 | else | 2023 | else |
2022 | { | 2024 | { |
2023 | GNUNET_break (0); | 2025 | GNUNET_break(0); |
2024 | return NULL; | 2026 | return NULL; |
2025 | } | 2027 | } |
2026 | 2028 | ||
2027 | *m = GNUNET_new (struct GNUNET_DISK_MapHandle); | 2029 | *m = GNUNET_new(struct GNUNET_DISK_MapHandle); |
2028 | (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL); | 2030 | (*m)->h = CreateFileMapping(h->h, NULL, protect, 0, 0, NULL); |
2029 | if ((*m)->h == INVALID_HANDLE_VALUE) | 2031 | if ((*m)->h == INVALID_HANDLE_VALUE) |
2030 | { | 2032 | { |
2031 | SetErrnoFromWinError (GetLastError ()); | 2033 | SetErrnoFromWinError(GetLastError()); |
2032 | GNUNET_free (*m); | 2034 | GNUNET_free(*m); |
2033 | return NULL; | 2035 | return NULL; |
2034 | } | 2036 | } |
2035 | 2037 | ||
2036 | (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); | 2038 | (*m)->addr = MapViewOfFile((*m)->h, mapAccess, 0, 0, len); |
2037 | if (! (*m)->addr) | 2039 | if (!(*m)->addr) |
2038 | { | 2040 | { |
2039 | SetErrnoFromWinError (GetLastError ()); | 2041 | SetErrnoFromWinError(GetLastError()); |
2040 | CloseHandle ((*m)->h); | 2042 | CloseHandle((*m)->h); |
2041 | GNUNET_free (*m); | 2043 | GNUNET_free(*m); |
2042 | } | 2044 | } |
2043 | 2045 | ||
2044 | return (*m)->addr; | 2046 | return (*m)->addr; |
2045 | #else | 2047 | #else |
@@ -2050,14 +2052,14 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | |||
2050 | prot = PROT_READ; | 2052 | prot = PROT_READ; |
2051 | if (access & GNUNET_DISK_MAP_TYPE_WRITE) | 2053 | if (access & GNUNET_DISK_MAP_TYPE_WRITE) |
2052 | prot |= PROT_WRITE; | 2054 | prot |= PROT_WRITE; |
2053 | *m = GNUNET_new (struct GNUNET_DISK_MapHandle); | 2055 | *m = GNUNET_new(struct GNUNET_DISK_MapHandle); |
2054 | (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0); | 2056 | (*m)->addr = mmap(NULL, len, prot, MAP_SHARED, h->fd, 0); |
2055 | GNUNET_assert (NULL != (*m)->addr); | 2057 | GNUNET_assert(NULL != (*m)->addr); |
2056 | if (MAP_FAILED == (*m)->addr) | 2058 | if (MAP_FAILED == (*m)->addr) |
2057 | { | 2059 | { |
2058 | GNUNET_free (*m); | 2060 | GNUNET_free(*m); |
2059 | return NULL; | 2061 | return NULL; |
2060 | } | 2062 | } |
2061 | (*m)->len = len; | 2063 | (*m)->len = len; |
2062 | return (*m)->addr; | 2064 | return (*m)->addr; |
2063 | #endif | 2065 | #endif |
@@ -2069,29 +2071,29 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | |||
2069 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | 2071 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise |
2070 | */ | 2072 | */ |
2071 | int | 2073 | int |
2072 | GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) | 2074 | GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h) |
2073 | { | 2075 | { |
2074 | int ret; | 2076 | int ret; |
2075 | 2077 | ||
2076 | if (h == NULL) | 2078 | if (h == NULL) |
2077 | { | 2079 | { |
2078 | errno = EINVAL; | 2080 | errno = EINVAL; |
2079 | return GNUNET_SYSERR; | 2081 | return GNUNET_SYSERR; |
2080 | } | 2082 | } |
2081 | 2083 | ||
2082 | #ifdef MINGW | 2084 | #ifdef MINGW |
2083 | ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; | 2085 | ret = UnmapViewOfFile(h->addr) ? GNUNET_OK : GNUNET_SYSERR; |
2084 | if (ret != GNUNET_OK) | 2086 | if (ret != GNUNET_OK) |
2085 | SetErrnoFromWinError (GetLastError ()); | 2087 | SetErrnoFromWinError(GetLastError()); |
2086 | if (! CloseHandle (h->h) && (ret == GNUNET_OK)) | 2088 | if (!CloseHandle(h->h) && (ret == GNUNET_OK)) |
2087 | { | 2089 | { |
2088 | ret = GNUNET_SYSERR; | 2090 | ret = GNUNET_SYSERR; |
2089 | SetErrnoFromWinError (GetLastError ()); | 2091 | SetErrnoFromWinError(GetLastError()); |
2090 | } | 2092 | } |
2091 | #else | 2093 | #else |
2092 | ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; | 2094 | ret = munmap(h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; |
2093 | #endif | 2095 | #endif |
2094 | GNUNET_free (h); | 2096 | GNUNET_free(h); |
2095 | return ret; | 2097 | return ret; |
2096 | } | 2098 | } |
2097 | 2099 | ||
@@ -2102,25 +2104,25 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) | |||
2102 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | 2104 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise |
2103 | */ | 2105 | */ |
2104 | int | 2106 | int |
2105 | GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | 2107 | GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h) |
2106 | { | 2108 | { |
2107 | if (h == NULL) | 2109 | if (h == NULL) |
2108 | { | 2110 | { |
2109 | errno = EINVAL; | 2111 | errno = EINVAL; |
2110 | return GNUNET_SYSERR; | 2112 | return GNUNET_SYSERR; |
2111 | } | 2113 | } |
2112 | 2114 | ||
2113 | #ifdef MINGW | 2115 | #ifdef MINGW |
2114 | int ret; | 2116 | int ret; |
2115 | 2117 | ||
2116 | ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR; | 2118 | ret = FlushFileBuffers(h->h) ? GNUNET_OK : GNUNET_SYSERR; |
2117 | if (ret != GNUNET_OK) | 2119 | if (ret != GNUNET_OK) |
2118 | SetErrnoFromWinError (GetLastError ()); | 2120 | SetErrnoFromWinError(GetLastError()); |
2119 | return ret; | 2121 | return ret; |
2120 | #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN) | 2122 | #elif defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN) |
2121 | return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; | 2123 | return fsync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; |
2122 | #else | 2124 | #else |
2123 | return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; | 2125 | return fdatasync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; |
2124 | #endif | 2126 | #endif |
2125 | } | 2127 | } |
2126 | 2128 | ||
@@ -2131,7 +2133,7 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | |||
2131 | #endif | 2133 | #endif |
2132 | /* Copyright Bob Byrnes <byrnes <at> curl.com> | 2134 | /* Copyright Bob Byrnes <byrnes <at> curl.com> |
2133 | http://permalink.gmane.org/gmane.os.cygwin.patches/2121 | 2135 | http://permalink.gmane.org/gmane.os.cygwin.patches/2121 |
2134 | */ | 2136 | */ |
2135 | /* Create a pipe, and return handles to the read and write ends, | 2137 | /* Create a pipe, and return handles to the read and write ends, |
2136 | just like CreatePipe, but ensure that the write end permits | 2138 | just like CreatePipe, but ensure that the write end permits |
2137 | FILE_READ_ATTRIBUTES access, on later versions of win32 where | 2139 | FILE_READ_ATTRIBUTES access, on later versions of win32 where |
@@ -2140,12 +2142,12 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | |||
2140 | Note that the return value is either NO_ERROR or GetLastError, | 2142 | Note that the return value is either NO_ERROR or GetLastError, |
2141 | unlike CreatePipe, which returns a bool for success or failure. */ | 2143 | unlike CreatePipe, which returns a bool for success or failure. */ |
2142 | static int | 2144 | static int |
2143 | create_selectable_pipe (PHANDLE read_pipe_ptr, | 2145 | create_selectable_pipe(PHANDLE read_pipe_ptr, |
2144 | PHANDLE write_pipe_ptr, | 2146 | PHANDLE write_pipe_ptr, |
2145 | LPSECURITY_ATTRIBUTES sa_ptr, | 2147 | LPSECURITY_ATTRIBUTES sa_ptr, |
2146 | DWORD psize, | 2148 | DWORD psize, |
2147 | DWORD dwReadMode, | 2149 | DWORD dwReadMode, |
2148 | DWORD dwWriteMode) | 2150 | DWORD dwWriteMode) |
2149 | { | 2151 | { |
2150 | /* Default to error. */ | 2152 | /* Default to error. */ |
2151 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; | 2153 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; |
@@ -2163,102 +2165,105 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, | |||
2163 | * Retrying will probably never be necessary, but we want | 2165 | * Retrying will probably never be necessary, but we want |
2164 | * to be as robust as possible. */ | 2166 | * to be as robust as possible. */ |
2165 | while (1) | 2167 | while (1) |
2166 | { | 2168 | { |
2167 | static volatile LONG pipe_unique_id; | 2169 | static volatile LONG pipe_unique_id; |
2168 | 2170 | ||
2169 | snprintf (pipename, | 2171 | snprintf(pipename, |
2170 | sizeof pipename, | 2172 | sizeof pipename, |
2171 | "\\\\.\\pipe\\gnunet-%d-%ld", | 2173 | "\\\\.\\pipe\\gnunet-%d-%ld", |
2172 | getpid (), | 2174 | getpid(), |
2173 | InterlockedIncrement ((LONG *) &pipe_unique_id)); | 2175 | InterlockedIncrement((LONG *)&pipe_unique_id)); |
2174 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 2176 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
2175 | "CreateNamedPipe: name = %s, size = %lu\n", | 2177 | "CreateNamedPipe: name = %s, size = %lu\n", |
2176 | pipename, | 2178 | pipename, |
2177 | psize); | 2179 | psize); |
2178 | /* Use CreateNamedPipe instead of CreatePipe, because the latter | 2180 | /* Use CreateNamedPipe instead of CreatePipe, because the latter |
2179 | * returns a write handle that does not permit FILE_READ_ATTRIBUTES | 2181 | * returns a write handle that does not permit FILE_READ_ATTRIBUTES |
2180 | * access, on versions of win32 earlier than WinXP SP2. | 2182 | * access, on versions of win32 earlier than WinXP SP2. |
2181 | * CreatePipe also stupidly creates a full duplex pipe, which is | 2183 | * CreatePipe also stupidly creates a full duplex pipe, which is |
2182 | * a waste, since only a single direction is actually used. | 2184 | * a waste, since only a single direction is actually used. |
2183 | * It's important to only allow a single instance, to ensure that | 2185 | * It's important to only allow a single instance, to ensure that |
2184 | * the pipe was not created earlier by some other process, even if | 2186 | * the pipe was not created earlier by some other process, even if |
2185 | * the pid has been reused. */ | 2187 | * the pid has been reused. */ |
2186 | read_pipe = CreateNamedPipeA (pipename, | 2188 | read_pipe = CreateNamedPipeA(pipename, |
2187 | PIPE_ACCESS_INBOUND | | 2189 | PIPE_ACCESS_INBOUND | |
2188 | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, | 2190 | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, |
2189 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | 2191 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
2190 | 1, /* max instances */ | 2192 | 1, /* max instances */ |
2191 | psize, /* output buffer size */ | 2193 | psize, /* output buffer size */ |
2192 | psize, /* input buffer size */ | 2194 | psize, /* input buffer size */ |
2193 | NMPWAIT_USE_DEFAULT_WAIT, | 2195 | NMPWAIT_USE_DEFAULT_WAIT, |
2194 | sa_ptr); | 2196 | sa_ptr); |
2195 | 2197 | ||
2196 | if (read_pipe != INVALID_HANDLE_VALUE) | 2198 | if (read_pipe != INVALID_HANDLE_VALUE) |
2197 | { | 2199 | { |
2198 | LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); | 2200 | LOG(GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p\n", read_pipe); |
2199 | break; | 2201 | break; |
2200 | } | 2202 | } |
2201 | 2203 | ||
2202 | DWORD err = GetLastError (); | 2204 | DWORD err = GetLastError(); |
2203 | 2205 | ||
2204 | switch (err) | 2206 | switch (err) |
2205 | { | 2207 | { |
2206 | case ERROR_PIPE_BUSY: | 2208 | case ERROR_PIPE_BUSY: |
2207 | /* The pipe is already open with compatible parameters. | 2209 | /* The pipe is already open with compatible parameters. |
2208 | * Pick a new name and retry. */ | 2210 | * Pick a new name and retry. */ |
2209 | LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); | 2211 | LOG(GNUNET_ERROR_TYPE_DEBUG, "pipe busy, retrying\n"); |
2210 | continue; | 2212 | continue; |
2211 | case ERROR_ACCESS_DENIED: | 2213 | |
2212 | /* The pipe is already open with incompatible parameters. | 2214 | case ERROR_ACCESS_DENIED: |
2213 | * Pick a new name and retry. */ | 2215 | /* The pipe is already open with incompatible parameters. |
2214 | LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); | 2216 | * Pick a new name and retry. */ |
2215 | continue; | 2217 | LOG(GNUNET_ERROR_TYPE_DEBUG, "pipe access denied, retrying\n"); |
2216 | case ERROR_CALL_NOT_IMPLEMENTED: | 2218 | continue; |
2217 | /* We are on an older Win9x platform without named pipes. | 2219 | |
2218 | * Return an anonymous pipe as the best approximation. */ | 2220 | case ERROR_CALL_NOT_IMPLEMENTED: |
2219 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 2221 | /* We are on an older Win9x platform without named pipes. |
2220 | "CreateNamedPipe not implemented, resorting to " | 2222 | * Return an anonymous pipe as the best approximation. */ |
2221 | "CreatePipe: size = %lu\n", | 2223 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
2222 | psize); | 2224 | "CreateNamedPipe not implemented, resorting to " |
2223 | if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) | 2225 | "CreatePipe: size = %lu\n", |
2224 | { | 2226 | psize); |
2225 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 2227 | if (CreatePipe(read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) |
2226 | "pipe read handle = %p, write handle = %p\n", | 2228 | { |
2227 | *read_pipe_ptr, | 2229 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
2228 | *write_pipe_ptr); | 2230 | "pipe read handle = %p, write handle = %p\n", |
2229 | return GNUNET_OK; | 2231 | *read_pipe_ptr, |
2230 | } | 2232 | *write_pipe_ptr); |
2231 | err = GetLastError (); | 2233 | return GNUNET_OK; |
2232 | LOG (GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); | 2234 | } |
2233 | return err; | 2235 | err = GetLastError(); |
2234 | default: | 2236 | LOG(GNUNET_ERROR_TYPE_ERROR, "CreatePipe failed: %d\n", err); |
2235 | LOG (GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); | 2237 | return err; |
2236 | return err; | 2238 | |
2239 | default: | ||
2240 | LOG(GNUNET_ERROR_TYPE_ERROR, "CreateNamedPipe failed: %d\n", err); | ||
2241 | return err; | ||
2242 | } | ||
2243 | /* NOTREACHED */ | ||
2237 | } | 2244 | } |
2238 | /* NOTREACHED */ | 2245 | LOG(GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); |
2239 | } | ||
2240 | LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile: name = %s\n", pipename); | ||
2241 | 2246 | ||
2242 | /* Open the named pipe for writing. | 2247 | /* Open the named pipe for writing. |
2243 | * Be sure to permit FILE_READ_ATTRIBUTES access. */ | 2248 | * Be sure to permit FILE_READ_ATTRIBUTES access. */ |
2244 | write_pipe = CreateFileA (pipename, | 2249 | write_pipe = CreateFileA(pipename, |
2245 | GENERIC_WRITE | FILE_READ_ATTRIBUTES, | 2250 | GENERIC_WRITE | FILE_READ_ATTRIBUTES, |
2246 | 0, /* share mode */ | 2251 | 0, /* share mode */ |
2247 | sa_ptr, | 2252 | sa_ptr, |
2248 | OPEN_EXISTING, | 2253 | OPEN_EXISTING, |
2249 | dwWriteMode, /* flags and attributes */ | 2254 | dwWriteMode, /* flags and attributes */ |
2250 | 0); /* handle to template file */ | 2255 | 0); /* handle to template file */ |
2251 | 2256 | ||
2252 | if (write_pipe == INVALID_HANDLE_VALUE) | 2257 | if (write_pipe == INVALID_HANDLE_VALUE) |
2253 | { | 2258 | { |
2254 | /* Failure. */ | 2259 | /* Failure. */ |
2255 | DWORD err = GetLastError (); | 2260 | DWORD err = GetLastError(); |
2256 | 2261 | ||
2257 | LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); | 2262 | LOG(GNUNET_ERROR_TYPE_DEBUG, "CreateFile failed: %d\n", err); |
2258 | CloseHandle (read_pipe); | 2263 | CloseHandle(read_pipe); |
2259 | return err; | 2264 | return err; |
2260 | } | 2265 | } |
2261 | LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); | 2266 | LOG(GNUNET_ERROR_TYPE_DEBUG, "pipe write handle = %p\n", write_pipe); |
2262 | /* Success. */ | 2267 | /* Success. */ |
2263 | *read_pipe_ptr = read_pipe; | 2268 | *read_pipe_ptr = read_pipe; |
2264 | *write_pipe_ptr = write_pipe; | 2269 | *write_pipe_ptr = write_pipe; |
@@ -2277,36 +2282,36 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, | |||
2277 | * @return handle to the new pipe, NULL on error | 2282 | * @return handle to the new pipe, NULL on error |
2278 | */ | 2283 | */ |
2279 | struct GNUNET_DISK_PipeHandle * | 2284 | struct GNUNET_DISK_PipeHandle * |
2280 | GNUNET_DISK_pipe (int blocking_read, | 2285 | GNUNET_DISK_pipe(int blocking_read, |
2281 | int blocking_write, | 2286 | int blocking_write, |
2282 | int inherit_read, | 2287 | int inherit_read, |
2283 | int inherit_write) | 2288 | int inherit_write) |
2284 | { | 2289 | { |
2285 | #ifndef MINGW | 2290 | #ifndef MINGW |
2286 | int fd[2]; | 2291 | int fd[2]; |
2287 | int ret; | 2292 | int ret; |
2288 | int eno; | 2293 | int eno; |
2289 | 2294 | ||
2290 | (void) inherit_read; | 2295 | (void)inherit_read; |
2291 | (void) inherit_write; | 2296 | (void)inherit_write; |
2292 | ret = pipe (fd); | 2297 | ret = pipe(fd); |
2293 | if (ret == -1) | 2298 | if (ret == -1) |
2294 | { | 2299 | { |
2295 | eno = errno; | 2300 | eno = errno; |
2296 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); | 2301 | LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "pipe"); |
2297 | errno = eno; | 2302 | errno = eno; |
2298 | return NULL; | 2303 | return NULL; |
2299 | } | 2304 | } |
2300 | return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd); | 2305 | return GNUNET_DISK_pipe_from_fd(blocking_read, blocking_write, fd); |
2301 | #else | 2306 | #else |
2302 | struct GNUNET_DISK_PipeHandle *p; | 2307 | struct GNUNET_DISK_PipeHandle *p; |
2303 | BOOL ret; | 2308 | BOOL ret; |
2304 | HANDLE tmp_handle; | 2309 | HANDLE tmp_handle; |
2305 | int save_errno; | 2310 | int save_errno; |
2306 | 2311 | ||
2307 | p = GNUNET_new (struct GNUNET_DISK_PipeHandle); | 2312 | p = GNUNET_new(struct GNUNET_DISK_PipeHandle); |
2308 | p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); | 2313 | p->fd[0] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2309 | p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); | 2314 | p->fd[1] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2310 | 2315 | ||
2311 | /* All pipes are overlapped. If you want them to block - just | 2316 | /* All pipes are overlapped. If you want them to block - just |
2312 | * call WriteFile() and ReadFile() with NULL overlapped pointer. | 2317 | * call WriteFile() and ReadFile() with NULL overlapped pointer. |
@@ -2317,77 +2322,77 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2317 | * Pipes are not seekable, and need no offsets, which is | 2322 | * Pipes are not seekable, and need no offsets, which is |
2318 | * probably why it works for them. | 2323 | * probably why it works for them. |
2319 | */ | 2324 | */ |
2320 | ret = create_selectable_pipe (&p->fd[0]->h, | 2325 | ret = create_selectable_pipe(&p->fd[0]->h, |
2321 | &p->fd[1]->h, | 2326 | &p->fd[1]->h, |
2322 | NULL, | 2327 | NULL, |
2323 | 0, | 2328 | 0, |
2324 | FILE_FLAG_OVERLAPPED, | 2329 | FILE_FLAG_OVERLAPPED, |
2325 | FILE_FLAG_OVERLAPPED); | 2330 | FILE_FLAG_OVERLAPPED); |
2326 | if (! ret) | 2331 | if (!ret) |
2327 | { | 2332 | { |
2328 | SetErrnoFromWinError (GetLastError ()); | 2333 | SetErrnoFromWinError(GetLastError()); |
2329 | save_errno = errno; | 2334 | save_errno = errno; |
2330 | GNUNET_free (p->fd[0]); | 2335 | GNUNET_free(p->fd[0]); |
2331 | GNUNET_free (p->fd[1]); | 2336 | GNUNET_free(p->fd[1]); |
2332 | GNUNET_free (p); | 2337 | GNUNET_free(p); |
2333 | errno = save_errno; | 2338 | errno = save_errno; |
2334 | return NULL; | 2339 | return NULL; |
2335 | } | 2340 | } |
2336 | if (! DuplicateHandle (GetCurrentProcess (), | 2341 | if (!DuplicateHandle(GetCurrentProcess(), |
2337 | p->fd[0]->h, | 2342 | p->fd[0]->h, |
2338 | GetCurrentProcess (), | 2343 | GetCurrentProcess(), |
2339 | &tmp_handle, | 2344 | &tmp_handle, |
2340 | 0, | 2345 | 0, |
2341 | inherit_read == GNUNET_YES ? TRUE : FALSE, | 2346 | inherit_read == GNUNET_YES ? TRUE : FALSE, |
2342 | DUPLICATE_SAME_ACCESS)) | 2347 | DUPLICATE_SAME_ACCESS)) |
2343 | { | 2348 | { |
2344 | SetErrnoFromWinError (GetLastError ()); | 2349 | SetErrnoFromWinError(GetLastError()); |
2345 | save_errno = errno; | 2350 | save_errno = errno; |
2346 | CloseHandle (p->fd[0]->h); | 2351 | CloseHandle(p->fd[0]->h); |
2347 | CloseHandle (p->fd[1]->h); | 2352 | CloseHandle(p->fd[1]->h); |
2348 | GNUNET_free (p->fd[0]); | 2353 | GNUNET_free(p->fd[0]); |
2349 | GNUNET_free (p->fd[1]); | 2354 | GNUNET_free(p->fd[1]); |
2350 | GNUNET_free (p); | 2355 | GNUNET_free(p); |
2351 | errno = save_errno; | 2356 | errno = save_errno; |
2352 | return NULL; | 2357 | return NULL; |
2353 | } | 2358 | } |
2354 | CloseHandle (p->fd[0]->h); | 2359 | CloseHandle(p->fd[0]->h); |
2355 | p->fd[0]->h = tmp_handle; | 2360 | p->fd[0]->h = tmp_handle; |
2356 | 2361 | ||
2357 | if (! DuplicateHandle (GetCurrentProcess (), | 2362 | if (!DuplicateHandle(GetCurrentProcess(), |
2358 | p->fd[1]->h, | 2363 | p->fd[1]->h, |
2359 | GetCurrentProcess (), | 2364 | GetCurrentProcess(), |
2360 | &tmp_handle, | 2365 | &tmp_handle, |
2361 | 0, | 2366 | 0, |
2362 | inherit_write == GNUNET_YES ? TRUE : FALSE, | 2367 | inherit_write == GNUNET_YES ? TRUE : FALSE, |
2363 | DUPLICATE_SAME_ACCESS)) | 2368 | DUPLICATE_SAME_ACCESS)) |
2364 | { | 2369 | { |
2365 | SetErrnoFromWinError (GetLastError ()); | 2370 | SetErrnoFromWinError(GetLastError()); |
2366 | save_errno = errno; | 2371 | save_errno = errno; |
2367 | CloseHandle (p->fd[0]->h); | 2372 | CloseHandle(p->fd[0]->h); |
2368 | CloseHandle (p->fd[1]->h); | 2373 | CloseHandle(p->fd[1]->h); |
2369 | GNUNET_free (p->fd[0]); | 2374 | GNUNET_free(p->fd[0]); |
2370 | GNUNET_free (p->fd[1]); | 2375 | GNUNET_free(p->fd[1]); |
2371 | GNUNET_free (p); | 2376 | GNUNET_free(p); |
2372 | errno = save_errno; | 2377 | errno = save_errno; |
2373 | return NULL; | 2378 | return NULL; |
2374 | } | 2379 | } |
2375 | CloseHandle (p->fd[1]->h); | 2380 | CloseHandle(p->fd[1]->h); |
2376 | p->fd[1]->h = tmp_handle; | 2381 | p->fd[1]->h = tmp_handle; |
2377 | 2382 | ||
2378 | p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | 2383 | p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; |
2379 | p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | 2384 | p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; |
2380 | 2385 | ||
2381 | p->fd[0]->oOverlapRead = GNUNET_new (OVERLAPPED); | 2386 | p->fd[0]->oOverlapRead = GNUNET_new(OVERLAPPED); |
2382 | p->fd[0]->oOverlapWrite = GNUNET_new (OVERLAPPED); | 2387 | p->fd[0]->oOverlapWrite = GNUNET_new(OVERLAPPED); |
2383 | p->fd[1]->oOverlapRead = GNUNET_new (OVERLAPPED); | 2388 | p->fd[1]->oOverlapRead = GNUNET_new(OVERLAPPED); |
2384 | p->fd[1]->oOverlapWrite = GNUNET_new (OVERLAPPED); | 2389 | p->fd[1]->oOverlapWrite = GNUNET_new(OVERLAPPED); |
2385 | 2390 | ||
2386 | p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 2391 | p->fd[0]->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
2387 | p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 2392 | p->fd[0]->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
2388 | 2393 | ||
2389 | p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 2394 | p->fd[1]->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
2390 | p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | 2395 | p->fd[1]->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
2391 | 2396 | ||
2392 | return p; | 2397 | return p; |
2393 | #endif | 2398 | #endif |
@@ -2405,11 +2410,11 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2405 | * @return handle to the new pipe, NULL on error | 2410 | * @return handle to the new pipe, NULL on error |
2406 | */ | 2411 | */ |
2407 | struct GNUNET_DISK_PipeHandle * | 2412 | struct GNUNET_DISK_PipeHandle * |
2408 | GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]) | 2413 | GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2]) |
2409 | { | 2414 | { |
2410 | struct GNUNET_DISK_PipeHandle *p; | 2415 | struct GNUNET_DISK_PipeHandle *p; |
2411 | 2416 | ||
2412 | p = GNUNET_new (struct GNUNET_DISK_PipeHandle); | 2417 | p = GNUNET_new(struct GNUNET_DISK_PipeHandle); |
2413 | 2418 | ||
2414 | #ifndef MINGW | 2419 | #ifndef MINGW |
2415 | int ret; | 2420 | int ret; |
@@ -2418,102 +2423,101 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]) | |||
2418 | 2423 | ||
2419 | ret = 0; | 2424 | ret = 0; |
2420 | if (fd[0] >= 0) | 2425 | if (fd[0] >= 0) |
2421 | { | ||
2422 | p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); | ||
2423 | p->fd[0]->fd = fd[0]; | ||
2424 | if (! blocking_read) | ||
2425 | { | 2426 | { |
2426 | flags = fcntl (fd[0], F_GETFL); | 2427 | p->fd[0] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2427 | flags |= O_NONBLOCK; | 2428 | p->fd[0]->fd = fd[0]; |
2428 | if (0 > fcntl (fd[0], F_SETFL, flags)) | 2429 | if (!blocking_read) |
2429 | { | 2430 | { |
2430 | ret = -1; | 2431 | flags = fcntl(fd[0], F_GETFL); |
2431 | eno = errno; | 2432 | flags |= O_NONBLOCK; |
2432 | } | 2433 | if (0 > fcntl(fd[0], F_SETFL, flags)) |
2433 | } | 2434 | { |
2434 | flags = fcntl (fd[0], F_GETFD); | 2435 | ret = -1; |
2435 | flags |= FD_CLOEXEC; | 2436 | eno = errno; |
2436 | if (0 > fcntl (fd[0], F_SETFD, flags)) | 2437 | } |
2437 | { | 2438 | } |
2438 | ret = -1; | 2439 | flags = fcntl(fd[0], F_GETFD); |
2439 | eno = errno; | 2440 | flags |= FD_CLOEXEC; |
2441 | if (0 > fcntl(fd[0], F_SETFD, flags)) | ||
2442 | { | ||
2443 | ret = -1; | ||
2444 | eno = errno; | ||
2445 | } | ||
2440 | } | 2446 | } |
2441 | } | ||
2442 | 2447 | ||
2443 | if (fd[1] >= 0) | 2448 | if (fd[1] >= 0) |
2444 | { | ||
2445 | p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); | ||
2446 | p->fd[1]->fd = fd[1]; | ||
2447 | if (! blocking_write) | ||
2448 | { | 2449 | { |
2449 | flags = fcntl (fd[1], F_GETFL); | 2450 | p->fd[1] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2450 | flags |= O_NONBLOCK; | 2451 | p->fd[1]->fd = fd[1]; |
2451 | if (0 > fcntl (fd[1], F_SETFL, flags)) | 2452 | if (!blocking_write) |
2452 | { | 2453 | { |
2453 | ret = -1; | 2454 | flags = fcntl(fd[1], F_GETFL); |
2454 | eno = errno; | 2455 | flags |= O_NONBLOCK; |
2455 | } | 2456 | if (0 > fcntl(fd[1], F_SETFL, flags)) |
2457 | { | ||
2458 | ret = -1; | ||
2459 | eno = errno; | ||
2460 | } | ||
2461 | } | ||
2462 | flags = fcntl(fd[1], F_GETFD); | ||
2463 | flags |= FD_CLOEXEC; | ||
2464 | if (0 > fcntl(fd[1], F_SETFD, flags)) | ||
2465 | { | ||
2466 | ret = -1; | ||
2467 | eno = errno; | ||
2468 | } | ||
2456 | } | 2469 | } |
2457 | flags = fcntl (fd[1], F_GETFD); | 2470 | if (ret == -1) |
2458 | flags |= FD_CLOEXEC; | ||
2459 | if (0 > fcntl (fd[1], F_SETFD, flags)) | ||
2460 | { | 2471 | { |
2461 | ret = -1; | 2472 | errno = eno; |
2462 | eno = errno; | 2473 | LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "fcntl"); |
2474 | if (p->fd[0]->fd >= 0) | ||
2475 | GNUNET_break(0 == close(p->fd[0]->fd)); | ||
2476 | if (p->fd[1]->fd >= 0) | ||
2477 | GNUNET_break(0 == close(p->fd[1]->fd)); | ||
2478 | GNUNET_free_non_null(p->fd[0]); | ||
2479 | GNUNET_free_non_null(p->fd[1]); | ||
2480 | GNUNET_free(p); | ||
2481 | errno = eno; | ||
2482 | return NULL; | ||
2463 | } | 2483 | } |
2464 | } | ||
2465 | if (ret == -1) | ||
2466 | { | ||
2467 | errno = eno; | ||
2468 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl"); | ||
2469 | if (p->fd[0]->fd >= 0) | ||
2470 | GNUNET_break (0 == close (p->fd[0]->fd)); | ||
2471 | if (p->fd[1]->fd >= 0) | ||
2472 | GNUNET_break (0 == close (p->fd[1]->fd)); | ||
2473 | GNUNET_free_non_null (p->fd[0]); | ||
2474 | GNUNET_free_non_null (p->fd[1]); | ||
2475 | GNUNET_free (p); | ||
2476 | errno = eno; | ||
2477 | return NULL; | ||
2478 | } | ||
2479 | #else | 2484 | #else |
2480 | if (fd[0] >= 0) | 2485 | if (fd[0] >= 0) |
2481 | { | ||
2482 | p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); | ||
2483 | p->fd[0]->h = (HANDLE) _get_osfhandle (fd[0]); | ||
2484 | if (p->fd[0]->h != INVALID_HANDLE_VALUE) | ||
2485 | { | ||
2486 | p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | ||
2487 | p->fd[0]->oOverlapRead = GNUNET_new (OVERLAPPED); | ||
2488 | p->fd[0]->oOverlapWrite = GNUNET_new (OVERLAPPED); | ||
2489 | p->fd[0]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | ||
2490 | p->fd[0]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | ||
2491 | } | ||
2492 | else | ||
2493 | { | 2486 | { |
2494 | GNUNET_free (p->fd[0]); | 2487 | p->fd[0] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2495 | p->fd[0] = NULL; | 2488 | p->fd[0]->h = (HANDLE)_get_osfhandle(fd[0]); |
2489 | if (p->fd[0]->h != INVALID_HANDLE_VALUE) | ||
2490 | { | ||
2491 | p->fd[0]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | ||
2492 | p->fd[0]->oOverlapRead = GNUNET_new(OVERLAPPED); | ||
2493 | p->fd[0]->oOverlapWrite = GNUNET_new(OVERLAPPED); | ||
2494 | p->fd[0]->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
2495 | p->fd[0]->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
2496 | } | ||
2497 | else | ||
2498 | { | ||
2499 | GNUNET_free(p->fd[0]); | ||
2500 | p->fd[0] = NULL; | ||
2501 | } | ||
2496 | } | 2502 | } |
2497 | } | ||
2498 | if (fd[1] >= 0) | 2503 | if (fd[1] >= 0) |
2499 | { | ||
2500 | p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); | ||
2501 | p->fd[1]->h = (HANDLE) _get_osfhandle (fd[1]); | ||
2502 | if (p->fd[1]->h != INVALID_HANDLE_VALUE) | ||
2503 | { | ||
2504 | p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | ||
2505 | p->fd[1]->oOverlapRead = GNUNET_new (OVERLAPPED); | ||
2506 | p->fd[1]->oOverlapWrite = GNUNET_new (OVERLAPPED); | ||
2507 | p->fd[1]->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | ||
2508 | p->fd[1]->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); | ||
2509 | } | ||
2510 | else | ||
2511 | { | 2504 | { |
2512 | GNUNET_free (p->fd[1]); | 2505 | p->fd[1] = GNUNET_new(struct GNUNET_DISK_FileHandle); |
2513 | p->fd[1] = NULL; | 2506 | p->fd[1]->h = (HANDLE)_get_osfhandle(fd[1]); |
2507 | if (p->fd[1]->h != INVALID_HANDLE_VALUE) | ||
2508 | { | ||
2509 | p->fd[1]->type = GNUNET_DISK_HANLDE_TYPE_PIPE; | ||
2510 | p->fd[1]->oOverlapRead = GNUNET_new(OVERLAPPED); | ||
2511 | p->fd[1]->oOverlapWrite = GNUNET_new(OVERLAPPED); | ||
2512 | p->fd[1]->oOverlapRead->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
2513 | p->fd[1]->oOverlapWrite->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
2514 | } | ||
2515 | else | ||
2516 | { | ||
2517 | GNUNET_free(p->fd[1]); | ||
2518 | p->fd[1] = NULL; | ||
2519 | } | ||
2514 | } | 2520 | } |
2515 | } | ||
2516 | |||
2517 | #endif | 2521 | #endif |
2518 | return p; | 2522 | return p; |
2519 | } | 2523 | } |
@@ -2527,27 +2531,27 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]) | |||
2527 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | 2531 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise |
2528 | */ | 2532 | */ |
2529 | int | 2533 | int |
2530 | GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, | 2534 | GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, |
2531 | enum GNUNET_DISK_PipeEnd end) | 2535 | enum GNUNET_DISK_PipeEnd end) |
2532 | { | 2536 | { |
2533 | int ret = GNUNET_OK; | 2537 | int ret = GNUNET_OK; |
2534 | 2538 | ||
2535 | if (end == GNUNET_DISK_PIPE_END_READ) | 2539 | if (end == GNUNET_DISK_PIPE_END_READ) |
2536 | { | ||
2537 | if (p->fd[0]) | ||
2538 | { | 2540 | { |
2539 | ret = GNUNET_DISK_file_close (p->fd[0]); | 2541 | if (p->fd[0]) |
2540 | p->fd[0] = NULL; | 2542 | { |
2543 | ret = GNUNET_DISK_file_close(p->fd[0]); | ||
2544 | p->fd[0] = NULL; | ||
2545 | } | ||
2541 | } | 2546 | } |
2542 | } | ||
2543 | else if (end == GNUNET_DISK_PIPE_END_WRITE) | 2547 | else if (end == GNUNET_DISK_PIPE_END_WRITE) |
2544 | { | ||
2545 | if (p->fd[1]) | ||
2546 | { | 2548 | { |
2547 | ret = GNUNET_DISK_file_close (p->fd[1]); | 2549 | if (p->fd[1]) |
2548 | p->fd[1] = NULL; | 2550 | { |
2551 | ret = GNUNET_DISK_file_close(p->fd[1]); | ||
2552 | p->fd[1] = NULL; | ||
2553 | } | ||
2549 | } | 2554 | } |
2550 | } | ||
2551 | 2555 | ||
2552 | return ret; | 2556 | return ret; |
2553 | } | 2557 | } |
@@ -2565,27 +2569,27 @@ GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, | |||
2565 | * (or if that end is not present or is closed). | 2569 | * (or if that end is not present or is closed). |
2566 | */ | 2570 | */ |
2567 | struct GNUNET_DISK_FileHandle * | 2571 | struct GNUNET_DISK_FileHandle * |
2568 | GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p, | 2572 | GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, |
2569 | enum GNUNET_DISK_PipeEnd end) | 2573 | enum GNUNET_DISK_PipeEnd end) |
2570 | { | 2574 | { |
2571 | struct GNUNET_DISK_FileHandle *ret = NULL; | 2575 | struct GNUNET_DISK_FileHandle *ret = NULL; |
2572 | 2576 | ||
2573 | if (end == GNUNET_DISK_PIPE_END_READ) | 2577 | if (end == GNUNET_DISK_PIPE_END_READ) |
2574 | { | ||
2575 | if (p->fd[0]) | ||
2576 | { | 2578 | { |
2577 | ret = p->fd[0]; | 2579 | if (p->fd[0]) |
2578 | p->fd[0] = NULL; | 2580 | { |
2581 | ret = p->fd[0]; | ||
2582 | p->fd[0] = NULL; | ||
2583 | } | ||
2579 | } | 2584 | } |
2580 | } | ||
2581 | else if (end == GNUNET_DISK_PIPE_END_WRITE) | 2585 | else if (end == GNUNET_DISK_PIPE_END_WRITE) |
2582 | { | ||
2583 | if (p->fd[1]) | ||
2584 | { | 2586 | { |
2585 | ret = p->fd[1]; | 2587 | if (p->fd[1]) |
2586 | p->fd[1] = NULL; | 2588 | { |
2589 | ret = p->fd[1]; | ||
2590 | p->fd[1] = NULL; | ||
2591 | } | ||
2587 | } | 2592 | } |
2588 | } | ||
2589 | 2593 | ||
2590 | return ret; | 2594 | return ret; |
2591 | } | 2595 | } |
@@ -2598,7 +2602,7 @@ GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p, | |||
2598 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | 2602 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise |
2599 | */ | 2603 | */ |
2600 | int | 2604 | int |
2601 | GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | 2605 | GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p) |
2602 | { | 2606 | { |
2603 | int ret = GNUNET_OK; | 2607 | int ret = GNUNET_OK; |
2604 | 2608 | ||
@@ -2607,22 +2611,22 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | |||
2607 | int read_end_close_errno; | 2611 | int read_end_close_errno; |
2608 | int write_end_close_errno; | 2612 | int write_end_close_errno; |
2609 | 2613 | ||
2610 | read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ); | 2614 | read_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_READ); |
2611 | read_end_close_errno = errno; | 2615 | read_end_close_errno = errno; |
2612 | write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE); | 2616 | write_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_WRITE); |
2613 | write_end_close_errno = errno; | 2617 | write_end_close_errno = errno; |
2614 | GNUNET_free (p); | 2618 | GNUNET_free(p); |
2615 | 2619 | ||
2616 | if (GNUNET_OK != read_end_close) | 2620 | if (GNUNET_OK != read_end_close) |
2617 | { | 2621 | { |
2618 | errno = read_end_close_errno; | 2622 | errno = read_end_close_errno; |
2619 | ret = read_end_close; | 2623 | ret = read_end_close; |
2620 | } | 2624 | } |
2621 | else if (GNUNET_OK != write_end_close) | 2625 | else if (GNUNET_OK != write_end_close) |
2622 | { | 2626 | { |
2623 | errno = write_end_close_errno; | 2627 | errno = write_end_close_errno; |
2624 | ret = write_end_close; | 2628 | ret = write_end_close; |
2625 | } | 2629 | } |
2626 | 2630 | ||
2627 | return ret; | 2631 | return ret; |
2628 | } | 2632 | } |
@@ -2636,18 +2640,19 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | |||
2636 | * @return handle for the respective end | 2640 | * @return handle for the respective end |
2637 | */ | 2641 | */ |
2638 | const struct GNUNET_DISK_FileHandle * | 2642 | const struct GNUNET_DISK_FileHandle * |
2639 | GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, | 2643 | GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, |
2640 | enum GNUNET_DISK_PipeEnd n) | 2644 | enum GNUNET_DISK_PipeEnd n) |
2641 | { | 2645 | { |
2642 | switch (n) | 2646 | switch (n) |
2643 | { | 2647 | { |
2644 | case GNUNET_DISK_PIPE_END_READ: | 2648 | case GNUNET_DISK_PIPE_END_READ: |
2645 | case GNUNET_DISK_PIPE_END_WRITE: | 2649 | case GNUNET_DISK_PIPE_END_WRITE: |
2646 | return p->fd[n]; | 2650 | return p->fd[n]; |
2647 | default: | 2651 | |
2648 | GNUNET_break (0); | 2652 | default: |
2649 | return NULL; | 2653 | GNUNET_break(0); |
2650 | } | 2654 | return NULL; |
2655 | } | ||
2651 | } | 2656 | } |
2652 | 2657 | ||
2653 | 2658 | ||
@@ -2660,20 +2665,20 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, | |||
2660 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise | 2665 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise |
2661 | */ | 2666 | */ |
2662 | int | 2667 | int |
2663 | GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, | 2668 | GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, |
2664 | void *dst, | 2669 | void *dst, |
2665 | size_t dst_len) | 2670 | size_t dst_len) |
2666 | { | 2671 | { |
2667 | if (NULL == fh) | 2672 | if (NULL == fh) |
2668 | return GNUNET_SYSERR; | 2673 | return GNUNET_SYSERR; |
2669 | #ifdef MINGW | 2674 | #ifdef MINGW |
2670 | if (dst_len < sizeof (HANDLE)) | 2675 | if (dst_len < sizeof(HANDLE)) |
2671 | return GNUNET_SYSERR; | 2676 | return GNUNET_SYSERR; |
2672 | *((HANDLE *) dst) = fh->h; | 2677 | *((HANDLE *)dst) = fh->h; |
2673 | #else | 2678 | #else |
2674 | if (dst_len < sizeof (int)) | 2679 | if (dst_len < sizeof(int)) |
2675 | return GNUNET_SYSERR; | 2680 | return GNUNET_SYSERR; |
2676 | *((int *) dst) = fh->fd; | 2681 | *((int *)dst) = fh->fd; |
2677 | #endif | 2682 | #endif |
2678 | 2683 | ||
2679 | return GNUNET_OK; | 2684 | return GNUNET_OK; |
@@ -2688,24 +2693,24 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, | |||
2688 | * @return #GNUNET_OK on success | 2693 | * @return #GNUNET_OK on success |
2689 | */ | 2694 | */ |
2690 | static int | 2695 | static int |
2691 | purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) | 2696 | purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) |
2692 | { | 2697 | { |
2693 | const char *option = cls; | 2698 | const char *option = cls; |
2694 | char *tmpname; | 2699 | char *tmpname; |
2695 | 2700 | ||
2696 | if (GNUNET_OK != | 2701 | if (GNUNET_OK != |
2697 | GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname)) | 2702 | GNUNET_CONFIGURATION_get_value_filename(cfg, "PATHS", option, &tmpname)) |
2698 | { | 2703 | { |
2699 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option); | 2704 | GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "PATHS", option); |
2700 | return GNUNET_NO; | 2705 | return GNUNET_NO; |
2701 | } | 2706 | } |
2702 | if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname)) | 2707 | if (GNUNET_SYSERR == GNUNET_DISK_directory_remove(tmpname)) |
2703 | { | 2708 | { |
2704 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname); | 2709 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "remove", tmpname); |
2705 | GNUNET_free (tmpname); | 2710 | GNUNET_free(tmpname); |
2706 | return GNUNET_OK; | 2711 | return GNUNET_OK; |
2707 | } | 2712 | } |
2708 | GNUNET_free (tmpname); | 2713 | GNUNET_free(tmpname); |
2709 | return GNUNET_OK; | 2714 | return GNUNET_OK; |
2710 | } | 2715 | } |
2711 | 2716 | ||
@@ -2718,12 +2723,12 @@ purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
2718 | * @param option option with the dir name to purge | 2723 | * @param option option with the dir name to purge |
2719 | */ | 2724 | */ |
2720 | void | 2725 | void |
2721 | GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option) | 2726 | GNUNET_DISK_purge_cfg_dir(const char *cfg_filename, const char *option) |
2722 | { | 2727 | { |
2723 | GNUNET_break (GNUNET_OK == | 2728 | GNUNET_break(GNUNET_OK == |
2724 | GNUNET_CONFIGURATION_parse_and_run (cfg_filename, | 2729 | GNUNET_CONFIGURATION_parse_and_run(cfg_filename, |
2725 | &purge_cfg_dir, | 2730 | &purge_cfg_dir, |
2726 | (void *) option)); | 2731 | (void *)option)); |
2727 | } | 2732 | } |
2728 | 2733 | ||
2729 | 2734 | ||