summaryrefslogtreecommitdiff
path: root/src/util/disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/disk.c')
-rw-r--r--src/util/disk.c1373
1 files changed, 689 insertions, 684 deletions
diff --git a/src/util/disk.c b/src/util/disk.c
index 8b474542b..32667d554 100644
--- a/src/util/disk.c
+++ b/src/util/disk.c
@@ -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.
@@ -65,7 +65,8 @@
65/** 65/**
66 * Handle used to manage a pipe. 66 * Handle used to manage a pipe.
67 */ 67 */
68struct GNUNET_DISK_PipeHandle { 68struct GNUNET_DISK_PipeHandle
69{
69 /** 70 /**
70 * File descriptors for the pipe. 71 * File descriptors for the pipe.
71 * One or both of them could be NULL. 72 * One or both of them could be NULL.
@@ -78,7 +79,8 @@ struct GNUNET_DISK_PipeHandle {
78 * Closure for the recursion to determine the file size 79 * Closure for the recursion to determine the file size
79 * of a directory. 80 * of a directory.
80 */ 81 */
81struct GetFileSizeData { 82struct GetFileSizeData
83{
82 /** 84 /**
83 * Set to the total file size. 85 * Set to the total file size.
84 */ 86 */
@@ -104,7 +106,7 @@ struct GetFileSizeData {
104 * @return file permissions, UNIX style 106 * @return file permissions, UNIX style
105 */ 107 */
106static int 108static int
107translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm) 109translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm)
108{ 110{
109 int mode; 111 int mode;
110 112
@@ -141,41 +143,41 @@ translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
141 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK 143 * @return #GNUNET_SYSERR on serious errors, otherwise #GNUNET_OK
142 */ 144 */
143static int 145static int
144getSizeRec(void *cls, const char *fn) 146getSizeRec (void *cls, const char *fn)
145{ 147{
146 struct GetFileSizeData *gfsd = cls; 148 struct GetFileSizeData *gfsd = cls;
147 149
148#if defined(HAVE_STAT64) && \ 150#if defined(HAVE_STAT64) && \
149 !(defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) 151 ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
150 struct stat64 buf; 152 struct stat64 buf;
151 153
152 if (0 != stat64(fn, &buf)) 154 if (0 != stat64 (fn, &buf))
153 { 155 {
154 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat64", fn); 156 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat64", fn);
155 return GNUNET_SYSERR; 157 return GNUNET_SYSERR;
156 } 158 }
157#else 159#else
158 struct stat buf; 160 struct stat buf;
159 161
160 if (0 != stat(fn, &buf)) 162 if (0 != stat (fn, &buf))
161 { 163 {
162 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "stat", fn); 164 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn);
163 return GNUNET_SYSERR; 165 return GNUNET_SYSERR;
164 } 166 }
165#endif 167#endif
166 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))
167 { 169 {
168 errno = EISDIR; 170 errno = EISDIR;
169 return GNUNET_SYSERR; 171 return GNUNET_SYSERR;
170 } 172 }
171 if ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) 173 if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
172 gfsd->total += buf.st_size; 174 gfsd->total += buf.st_size;
173 if ((S_ISDIR(buf.st_mode)) && (0 == access(fn, X_OK)) && 175 if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) &&
174 ((!S_ISLNK(buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) 176 ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)))
175 { 177 {
176 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan(fn, &getSizeRec, gfsd)) 178 if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd))
177 return GNUNET_SYSERR; 179 return GNUNET_SYSERR;
178 } 180 }
179 return GNUNET_OK; 181 return GNUNET_OK;
180} 182}
181 183
@@ -187,9 +189,9 @@ getSizeRec(void *cls, const char *fn)
187 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid 189 * @return #GNUNET_YES if invalid, #GNUNET_NO if valid
188 */ 190 */
189int 191int
190GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h) 192GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h)
191{ 193{
192 return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; 194 return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO;
193} 195}
194 196
195/** 197/**
@@ -200,11 +202,11 @@ GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
200 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
201 */ 203 */
202int 204int
203GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size) 205GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size)
204{ 206{
205 struct stat sbuf; 207 struct stat sbuf;
206 208
207 if (0 != fstat(fh->fd, &sbuf)) 209 if (0 != fstat (fh->fd, &sbuf))
208 return GNUNET_SYSERR; 210 return GNUNET_SYSERR;
209 *size = sbuf.st_size; 211 *size = sbuf.st_size;
210 return GNUNET_OK; 212 return GNUNET_OK;
@@ -220,19 +222,19 @@ GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
220 * @return the new position on success, #GNUNET_SYSERR otherwise 222 * @return the new position on success, #GNUNET_SYSERR otherwise
221 */ 223 */
222off_t 224off_t
223GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, 225GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h,
224 off_t offset, 226 off_t offset,
225 enum GNUNET_DISK_Seek whence) 227 enum GNUNET_DISK_Seek whence)
226{ 228{
227 if (h == NULL) 229 if (h == NULL)
228 { 230 {
229 errno = EINVAL; 231 errno = EINVAL;
230 return GNUNET_SYSERR; 232 return GNUNET_SYSERR;
231 } 233 }
232 234
233 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; 235 static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END };
234 236
235 return lseek(h->fd, offset, t[whence]); 237 return lseek (h->fd, offset, t[whence]);
236} 238}
237 239
238 240
@@ -251,19 +253,19 @@ GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h,
251 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 253 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
252 */ 254 */
253int 255int
254GNUNET_DISK_file_size(const char *filename, 256GNUNET_DISK_file_size (const char *filename,
255 uint64_t *size, 257 uint64_t *size,
256 int include_symbolic_links, 258 int include_symbolic_links,
257 int single_file_mode) 259 int single_file_mode)
258{ 260{
259 struct GetFileSizeData gfsd; 261 struct GetFileSizeData gfsd;
260 int ret; 262 int ret;
261 263
262 GNUNET_assert(size != NULL); 264 GNUNET_assert (size != NULL);
263 gfsd.total = 0; 265 gfsd.total = 0;
264 gfsd.include_sym_links = include_symbolic_links; 266 gfsd.include_sym_links = include_symbolic_links;
265 gfsd.single_file_mode = single_file_mode; 267 gfsd.single_file_mode = single_file_mode;
266 ret = getSizeRec(&gfsd, filename); 268 ret = getSizeRec (&gfsd, filename);
267 *size = gfsd.total; 269 *size = gfsd.total;
268 return ret; 270 return ret;
269} 271}
@@ -285,19 +287,19 @@ GNUNET_DISK_file_size(const char *filename,
285 * @return #GNUNET_OK on success 287 * @return #GNUNET_OK on success
286 */ 288 */
287int 289int
288GNUNET_DISK_file_get_identifiers(const char *filename, 290GNUNET_DISK_file_get_identifiers (const char *filename,
289 uint64_t *dev, 291 uint64_t *dev,
290 uint64_t *ino) 292 uint64_t *ino)
291{ 293{
292#if HAVE_STAT 294#if HAVE_STAT
293 { 295 {
294 struct stat sbuf; 296 struct stat sbuf;
295 297
296 if (0 != stat(filename, &sbuf)) 298 if (0 != stat (filename, &sbuf))
297 { 299 {
298 return GNUNET_SYSERR; 300 return GNUNET_SYSERR;
299 } 301 }
300 *ino = (uint64_t)sbuf.st_ino; 302 *ino = (uint64_t) sbuf.st_ino;
301 } 303 }
302#else 304#else
303 *ino = 0; 305 *ino = 0;
@@ -306,22 +308,22 @@ GNUNET_DISK_file_get_identifiers(const char *filename,
306 { 308 {
307 struct statvfs fbuf; 309 struct statvfs fbuf;
308 310
309 if (0 != statvfs(filename, &fbuf)) 311 if (0 != statvfs (filename, &fbuf))
310 { 312 {
311 return GNUNET_SYSERR; 313 return GNUNET_SYSERR;
312 } 314 }
313 *dev = (uint64_t)fbuf.f_fsid; 315 *dev = (uint64_t) fbuf.f_fsid;
314 } 316 }
315#elif HAVE_STATFS 317#elif HAVE_STATFS
316 { 318 {
317 struct statfs fbuf; 319 struct statfs fbuf;
318 320
319 if (0 != statfs(filename, &fbuf)) 321 if (0 != statfs (filename, &fbuf))
320 { 322 {
321 return GNUNET_SYSERR; 323 return GNUNET_SYSERR;
322 } 324 }
323 *dev = 325 *dev =
324 ((uint64_t)fbuf.f_fsid.val[0]) << 32 || ((uint64_t)fbuf.f_fsid.val[1]); 326 ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]);
325 } 327 }
326#else 328#else
327 *dev = 0; 329 *dev = 0;
@@ -337,28 +339,28 @@ GNUNET_DISK_file_get_identifiers(const char *filename,
337 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error 339 * @return name ready for passing to 'mktemp' or 'mkdtemp', NULL on error
338 */ 340 */
339static char * 341static char *
340mktemp_name(const char *t) 342mktemp_name (const char *t)
341{ 343{
342 const char *tmpdir; 344 const char *tmpdir;
343 char *tmpl; 345 char *tmpl;
344 char *fn; 346 char *fn;
345 347
346 if ((t[0] != '/') && (t[0] != '\\')) 348 if ((t[0] != '/') && (t[0] != '\\'))
347 { 349 {
348 /* FIXME: This uses system codepage on W32, not UTF-8 */ 350 /* FIXME: This uses system codepage on W32, not UTF-8 */
349 tmpdir = getenv("TMPDIR"); 351 tmpdir = getenv ("TMPDIR");
350 if (NULL == tmpdir) 352 if (NULL == tmpdir)
351 tmpdir = getenv("TMP"); 353 tmpdir = getenv ("TMP");
352 if (NULL == tmpdir) 354 if (NULL == tmpdir)
353 tmpdir = getenv("TEMP"); 355 tmpdir = getenv ("TEMP");
354 if (NULL == tmpdir) 356 if (NULL == tmpdir)
355 tmpdir = "/tmp"; 357 tmpdir = "/tmp";
356 GNUNET_asprintf(&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX"); 358 GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
357 } 359 }
358 else 360 else
359 { 361 {
360 GNUNET_asprintf(&tmpl, "%s%s", t, "XXXXXX"); 362 GNUNET_asprintf (&tmpl, "%s%s", t, "XXXXXX");
361 } 363 }
362 fn = tmpl; 364 fn = tmpl;
363 return fn; 365 return fn;
364} 366}
@@ -373,9 +375,9 @@ mktemp_name(const char *t)
373 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set 375 * @param require_gid_match #GNUNET_YES means 770 unless @a require_uid_match is set
374 */ 376 */
375void 377void
376GNUNET_DISK_fix_permissions(const char *fn, 378GNUNET_DISK_fix_permissions (const char *fn,
377 int require_uid_match, 379 int require_uid_match,
378 int require_gid_match) 380 int require_gid_match)
379{ 381{
380 mode_t mode; 382 mode_t mode;
381 383
@@ -384,10 +386,10 @@ GNUNET_DISK_fix_permissions(const char *fn,
384 else if (GNUNET_YES == require_gid_match) 386 else if (GNUNET_YES == require_gid_match)
385 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP; 387 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
386 else 388 else
387 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | 389 mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH
388 S_IWOTH | S_IXOTH; 390 | S_IWOTH | S_IXOTH;
389 if (0 != chmod(fn, mode)) 391 if (0 != chmod (fn, mode))
390 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "chmod", fn); 392 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn);
391} 393}
392 394
393 395
@@ -403,21 +405,21 @@ GNUNET_DISK_fix_permissions(const char *fn,
403 * file on disk in directory for temporary files 405 * file on disk in directory for temporary files
404 */ 406 */
405char * 407char *
406GNUNET_DISK_mkdtemp(const char *t) 408GNUNET_DISK_mkdtemp (const char *t)
407{ 409{
408 char *fn; 410 char *fn;
409 mode_t omask; 411 mode_t omask;
410 412
411 omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); 413 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
412 fn = mktemp_name(t); 414 fn = mktemp_name (t);
413 if (fn != mkdtemp(fn)) 415 if (fn != mkdtemp (fn))
414 { 416 {
415 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn); 417 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdtemp", fn);
416 GNUNET_free(fn); 418 GNUNET_free (fn);
417 umask(omask); 419 umask (omask);
418 return NULL; 420 return NULL;
419 } 421 }
420 umask(omask); 422 umask (omask);
421 return fn; 423 return fn;
422} 424}
423 425
@@ -430,23 +432,23 @@ GNUNET_DISK_mkdtemp(const char *t)
430 * @param fil name of the file to back up 432 * @param fil name of the file to back up
431 */ 433 */
432void 434void
433GNUNET_DISK_file_backup(const char *fil) 435GNUNET_DISK_file_backup (const char *fil)
434{ 436{
435 size_t slen; 437 size_t slen;
436 char *target; 438 char *target;
437 unsigned int num; 439 unsigned int num;
438 440
439 slen = strlen(fil) + 20; 441 slen = strlen (fil) + 20;
440 target = GNUNET_malloc(slen); 442 target = GNUNET_malloc (slen);
441 num = 0; 443 num = 0;
442 do 444 do
443 { 445 {
444 GNUNET_snprintf(target, slen, "%s.%u~", fil, num++); 446 GNUNET_snprintf (target, slen, "%s.%u~", fil, num++);
445 } 447 }
446 while (0 == access(target, F_OK)); 448 while (0 == access (target, F_OK));
447 if (0 != rename(fil, target)) 449 if (0 != rename (fil, target))
448 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "rename", fil); 450 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil);
449 GNUNET_free(target); 451 GNUNET_free (target);
450} 452}
451 453
452 454
@@ -462,24 +464,24 @@ GNUNET_DISK_file_backup(const char *fil)
462 * file on disk in directory for temporary files 464 * file on disk in directory for temporary files
463 */ 465 */
464char * 466char *
465GNUNET_DISK_mktemp(const char *t) 467GNUNET_DISK_mktemp (const char *t)
466{ 468{
467 int fd; 469 int fd;
468 char *fn; 470 char *fn;
469 mode_t omask; 471 mode_t omask;
470 472
471 omask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); 473 omask = umask (S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH);
472 fn = mktemp_name(t); 474 fn = mktemp_name (t);
473 if (-1 == (fd = mkstemp(fn))) 475 if (-1 == (fd = mkstemp (fn)))
474 { 476 {
475 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn); 477 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkstemp", fn);
476 GNUNET_free(fn); 478 GNUNET_free (fn);
477 umask(omask); 479 umask (omask);
478 return NULL; 480 return NULL;
479 } 481 }
480 umask(omask); 482 umask (omask);
481 if (0 != close(fd)) 483 if (0 != close (fd))
482 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "close", fn); 484 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn);
483 return fn; 485 return fn;
484} 486}
485 487
@@ -497,34 +499,34 @@ GNUNET_DISK_mktemp(const char *t)
497 * does not exist or stat'ed 499 * does not exist or stat'ed
498 */ 500 */
499int 501int
500GNUNET_DISK_directory_test(const char *fil, int is_readable) 502GNUNET_DISK_directory_test (const char *fil, int is_readable)
501{ 503{
502 struct stat filestat; 504 struct stat filestat;
503 int ret; 505 int ret;
504 506
505 ret = stat(fil, &filestat); 507 ret = stat (fil, &filestat);
506 if (ret != 0) 508 if (ret != 0)
507 { 509 {
508 if (errno != ENOENT) 510 if (errno != ENOENT)
509 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", fil); 511 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil);
510 return GNUNET_SYSERR; 512 return GNUNET_SYSERR;
511 } 513 }
512 if (!S_ISDIR(filestat.st_mode)) 514 if (! S_ISDIR (filestat.st_mode))
513 { 515 {
514 LOG(GNUNET_ERROR_TYPE_INFO, 516 LOG (GNUNET_ERROR_TYPE_INFO,
515 "A file already exits with the same name %s\n", 517 "A file already exits with the same name %s\n",
516 fil); 518 fil);
517 return GNUNET_NO; 519 return GNUNET_NO;
518 } 520 }
519 if (GNUNET_YES == is_readable) 521 if (GNUNET_YES == is_readable)
520 ret = access(fil, R_OK | X_OK); 522 ret = access (fil, R_OK | X_OK);
521 else 523 else
522 ret = access(fil, X_OK); 524 ret = access (fil, X_OK);
523 if (ret < 0) 525 if (ret < 0)
524 { 526 {
525 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", fil); 527 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil);
526 return GNUNET_NO; 528 return GNUNET_NO;
527 } 529 }
528 return GNUNET_YES; 530 return GNUNET_YES;
529} 531}
530 532
@@ -538,40 +540,40 @@ GNUNET_DISK_directory_test(const char *fil, int is_readable)
538 * else (will print an error message in that case, too). 540 * else (will print an error message in that case, too).
539 */ 541 */
540int 542int
541GNUNET_DISK_file_test(const char *fil) 543GNUNET_DISK_file_test (const char *fil)
542{ 544{
543 struct stat filestat; 545 struct stat filestat;
544 int ret; 546 int ret;
545 char *rdir; 547 char *rdir;
546 548
547 rdir = GNUNET_STRINGS_filename_expand(fil); 549 rdir = GNUNET_STRINGS_filename_expand (fil);
548 if (rdir == NULL) 550 if (rdir == NULL)
549 return GNUNET_SYSERR; 551 return GNUNET_SYSERR;
550 552
551 ret = stat(rdir, &filestat); 553 ret = stat (rdir, &filestat);
552 if (ret != 0) 554 if (ret != 0)
555 {
556 if (errno != ENOENT)
553 { 557 {
554 if (errno != ENOENT) 558 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
555 { 559 GNUNET_free (rdir);
556 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", rdir);
557 GNUNET_free(rdir);
558 return GNUNET_SYSERR;
559 }
560 GNUNET_free(rdir);
561 return GNUNET_NO;
562 }
563 if (!S_ISREG(filestat.st_mode))
564 {
565 GNUNET_free(rdir);
566 return GNUNET_NO;
567 }
568 if (access(rdir, F_OK) < 0)
569 {
570 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "access", rdir);
571 GNUNET_free(rdir);
572 return GNUNET_SYSERR; 560 return GNUNET_SYSERR;
573 } 561 }
574 GNUNET_free(rdir); 562 GNUNET_free (rdir);
563 return GNUNET_NO;
564 }
565 if (! S_ISREG (filestat.st_mode))
566 {
567 GNUNET_free (rdir);
568 return GNUNET_NO;
569 }
570 if (access (rdir, F_OK) < 0)
571 {
572 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir);
573 GNUNET_free (rdir);
574 return GNUNET_SYSERR;
575 }
576 GNUNET_free (rdir);
575 return GNUNET_YES; 577 return GNUNET_YES;
576} 578}
577 579
@@ -583,7 +585,7 @@ GNUNET_DISK_file_test(const char *fil)
583 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 585 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
584 */ 586 */
585int 587int
586GNUNET_DISK_directory_create(const char *dir) 588GNUNET_DISK_directory_create (const char *dir)
587{ 589{
588 char *rdir; 590 char *rdir;
589 unsigned int len; 591 unsigned int len;
@@ -591,14 +593,14 @@ GNUNET_DISK_directory_create(const char *dir)
591 unsigned int pos2; 593 unsigned int pos2;
592 int ret = GNUNET_OK; 594 int ret = GNUNET_OK;
593 595
594 rdir = GNUNET_STRINGS_filename_expand(dir); 596 rdir = GNUNET_STRINGS_filename_expand (dir);
595 if (rdir == NULL) 597 if (rdir == NULL)
596 { 598 {
597 GNUNET_break(0); 599 GNUNET_break (0);
598 return GNUNET_SYSERR; 600 return GNUNET_SYSERR;
599 } 601 }
600 602
601 len = strlen(rdir); 603 len = strlen (rdir);
602 604
603 pos = 1; /* skip heading '/' */ 605 pos = 1; /* skip heading '/' */
604 606
@@ -606,64 +608,64 @@ GNUNET_DISK_directory_create(const char *dir)
606 pos2 = len; 608 pos2 = len;
607 rdir[len] = DIR_SEPARATOR; 609 rdir[len] = DIR_SEPARATOR;
608 while (pos <= pos2) 610 while (pos <= pos2)
611 {
612 if (DIR_SEPARATOR == rdir[pos2])
609 { 613 {
610 if (DIR_SEPARATOR == rdir[pos2]) 614 rdir[pos2] = '\0';
611 { 615 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
612 rdir[pos2] = '\0'; 616 if (GNUNET_NO == ret)
613 ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); 617 {
614 if (GNUNET_NO == ret) 618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
615 { 619 "Creating directory `%s' failed",
616 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 620 rdir);
617 "Creating directory `%s' failed", 621 GNUNET_free (rdir);
618 rdir); 622 return GNUNET_SYSERR;
619 GNUNET_free(rdir); 623 }
620 return GNUNET_SYSERR; 624 rdir[pos2] = DIR_SEPARATOR;
621 } 625 if (GNUNET_YES == ret)
622 rdir[pos2] = DIR_SEPARATOR; 626 {
623 if (GNUNET_YES == ret) 627 pos2++;
624 { 628 break;
625 pos2++; 629 }
626 break;
627 }
628 }
629 pos2--;
630 } 630 }
631 pos2--;
632 }
631 rdir[len] = '\0'; 633 rdir[len] = '\0';
632 if (pos < pos2) 634 if (pos < pos2)
633 pos = pos2; 635 pos = pos2;
634 /* Start creating directories */ 636 /* Start creating directories */
635 while (pos <= len) 637 while (pos <= len)
638 {
639 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len))
636 { 640 {
637 if ((rdir[pos] == DIR_SEPARATOR) || (pos == len)) 641 rdir[pos] = '\0';
642 ret = GNUNET_DISK_directory_test (rdir, GNUNET_NO);
643 if (GNUNET_NO == ret)
644 {
645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
646 "Creating directory `%s' failed",
647 rdir);
648 GNUNET_free (rdir);
649 return GNUNET_SYSERR;
650 }
651 if (GNUNET_SYSERR == ret)
652 {
653 ret = mkdir (rdir,
654 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
655 | S_IXOTH); /* 755 */
656
657 if ((ret != 0) && (errno != EEXIST))
638 { 658 {
639 rdir[pos] = '\0'; 659 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
640 ret = GNUNET_DISK_directory_test(rdir, GNUNET_NO); 660 GNUNET_free (rdir);
641 if (GNUNET_NO == ret) 661 return GNUNET_SYSERR;
642 {
643 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
644 "Creating directory `%s' failed",
645 rdir);
646 GNUNET_free(rdir);
647 return GNUNET_SYSERR;
648 }
649 if (GNUNET_SYSERR == ret)
650 {
651 ret = mkdir(rdir,
652 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH |
653 S_IXOTH); /* 755 */
654
655 if ((ret != 0) && (errno != EEXIST))
656 {
657 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "mkdir", rdir);
658 GNUNET_free(rdir);
659 return GNUNET_SYSERR;
660 }
661 }
662 rdir[pos] = DIR_SEPARATOR;
663 } 662 }
664 pos++; 663 }
664 rdir[pos] = DIR_SEPARATOR;
665 } 665 }
666 GNUNET_free(rdir); 666 pos++;
667 }
668 GNUNET_free (rdir);
667 return GNUNET_OK; 669 return GNUNET_OK;
668} 670}
669 671
@@ -678,40 +680,40 @@ GNUNET_DISK_directory_create(const char *dir)
678 * exists but is not writeable for us 680 * exists but is not writeable for us
679 */ 681 */
680int 682int
681GNUNET_DISK_directory_create_for_file(const char *filename) 683GNUNET_DISK_directory_create_for_file (const char *filename)
682{ 684{
683 char *rdir; 685 char *rdir;
684 size_t len; 686 size_t len;
685 int ret; 687 int ret;
686 int eno; 688 int eno;
687 689
688 rdir = GNUNET_STRINGS_filename_expand(filename); 690 rdir = GNUNET_STRINGS_filename_expand (filename);
689 if (NULL == rdir) 691 if (NULL == rdir)
690 { 692 {
691 errno = EINVAL; 693 errno = EINVAL;
692 return GNUNET_SYSERR; 694 return GNUNET_SYSERR;
693 } 695 }
694 if (0 == access(rdir, W_OK)) 696 if (0 == access (rdir, W_OK))
695 { 697 {
696 GNUNET_free(rdir); 698 GNUNET_free (rdir);
697 return GNUNET_OK; 699 return GNUNET_OK;
698 } 700 }
699 701
700 len = strlen(rdir); 702 len = strlen (rdir);
701 while ((len > 0) && (rdir[len] != DIR_SEPARATOR)) 703 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
702 len--; 704 len--;
703 rdir[len] = '\0'; 705 rdir[len] = '\0';
704 /* The empty path is invalid and in this case refers to / */ 706 /* The empty path is invalid and in this case refers to / */
705 if (0 == len) 707 if (0 == len)
706 { 708 {
707 GNUNET_free(rdir); 709 GNUNET_free (rdir);
708 rdir = GNUNET_strdup("/"); 710 rdir = GNUNET_strdup ("/");
709 } 711 }
710 ret = GNUNET_DISK_directory_create(rdir); 712 ret = GNUNET_DISK_directory_create (rdir);
711 if ((GNUNET_OK == ret) && (0 != access(rdir, W_OK))) 713 if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK)))
712 ret = GNUNET_NO; 714 ret = GNUNET_NO;
713 eno = errno; 715 eno = errno;
714 GNUNET_free(rdir); 716 GNUNET_free (rdir);
715 errno = eno; 717 errno = eno;
716 return ret; 718 return ret;
717} 719}
@@ -726,17 +728,17 @@ GNUNET_DISK_directory_create_for_file(const char *filename)
726 * @return the number of bytes read on success, #GNUNET_SYSERR on failure 728 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
727 */ 729 */
728ssize_t 730ssize_t
729GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, 731GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h,
730 void *result, 732 void *result,
731 size_t len) 733 size_t len)
732{ 734{
733 if (NULL == h) 735 if (NULL == h)
734 { 736 {
735 errno = EINVAL; 737 errno = EINVAL;
736 return GNUNET_SYSERR; 738 return GNUNET_SYSERR;
737 } 739 }
738 740
739 return read(h->fd, result, len); 741 return read (h->fd, result, len);
740} 742}
741 743
742 744
@@ -751,30 +753,30 @@ GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h,
751 * @return the number of bytes read on success, #GNUNET_SYSERR on failure 753 * @return the number of bytes read on success, #GNUNET_SYSERR on failure
752 */ 754 */
753ssize_t 755ssize_t
754GNUNET_DISK_file_read_non_blocking(const struct GNUNET_DISK_FileHandle *h, 756GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h,
755 void *result, 757 void *result,
756 size_t len) 758 size_t len)
757{ 759{
758 if (NULL == h) 760 if (NULL == h)
759 { 761 {
760 errno = EINVAL; 762 errno = EINVAL;
761 return GNUNET_SYSERR; 763 return GNUNET_SYSERR;
762 } 764 }
763 765
764 int flags; 766 int flags;
765 ssize_t ret; 767 ssize_t ret;
766 768
767 /* set to non-blocking, read, then set back */ 769 /* set to non-blocking, read, then set back */
768 flags = fcntl(h->fd, F_GETFL); 770 flags = fcntl (h->fd, F_GETFL);
769 if (0 == (flags & O_NONBLOCK)) 771 if (0 == (flags & O_NONBLOCK))
770 (void)fcntl(h->fd, F_SETFL, flags | O_NONBLOCK); 772 (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK);
771 ret = read(h->fd, result, len); 773 ret = read (h->fd, result, len);
772 if (0 == (flags & O_NONBLOCK)) 774 if (0 == (flags & O_NONBLOCK))
773 { 775 {
774 int eno = errno; 776 int eno = errno;
775 (void)fcntl(h->fd, F_SETFL, flags); 777 (void) fcntl (h->fd, F_SETFL, flags);
776 errno = eno; 778 errno = eno;
777 } 779 }
778 return ret; 780 return ret;
779} 781}
780 782
@@ -788,18 +790,18 @@ GNUNET_DISK_file_read_non_blocking(const struct GNUNET_DISK_FileHandle *h,
788 * @return number of bytes read, #GNUNET_SYSERR on failure 790 * @return number of bytes read, #GNUNET_SYSERR on failure
789 */ 791 */
790ssize_t 792ssize_t
791GNUNET_DISK_fn_read(const char *fn, void *result, size_t len) 793GNUNET_DISK_fn_read (const char *fn, void *result, size_t len)
792{ 794{
793 struct GNUNET_DISK_FileHandle *fh; 795 struct GNUNET_DISK_FileHandle *fh;
794 ssize_t ret; 796 ssize_t ret;
795 int eno; 797 int eno;
796 798
797 fh = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 799 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
798 if (NULL == fh) 800 if (NULL == fh)
799 return GNUNET_SYSERR; 801 return GNUNET_SYSERR;
800 ret = GNUNET_DISK_file_read(fh, result, len); 802 ret = GNUNET_DISK_file_read (fh, result, len);
801 eno = errno; 803 eno = errno;
802 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 804 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
803 errno = eno; 805 errno = eno;
804 return ret; 806 return ret;
805} 807}
@@ -814,18 +816,18 @@ GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
814 * @return number of bytes written on success, #GNUNET_SYSERR on error 816 * @return number of bytes written on success, #GNUNET_SYSERR on error
815 */ 817 */
816ssize_t 818ssize_t
817GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, 819GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h,
818 const void *buffer, 820 const void *buffer,
819 size_t n) 821 size_t n)
820{ 822{
821 if (NULL == h) 823 if (NULL == h)
822 { 824 {
823 errno = EINVAL; 825 errno = EINVAL;
824 return GNUNET_SYSERR; 826 return GNUNET_SYSERR;
825 } 827 }
826 828
827 829
828 return write(h->fd, buffer, n); 830 return write (h->fd, buffer, n);
829} 831}
830 832
831 833
@@ -838,27 +840,27 @@ GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h,
838 * @return number of bytes written on success, #GNUNET_SYSERR on error 840 * @return number of bytes written on success, #GNUNET_SYSERR on error
839 */ 841 */
840ssize_t 842ssize_t
841GNUNET_DISK_file_write_blocking(const struct GNUNET_DISK_FileHandle *h, 843GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h,
842 const void *buffer, 844 const void *buffer,
843 size_t n) 845 size_t n)
844{ 846{
845 if (NULL == h) 847 if (NULL == h)
846 { 848 {
847 errno = EINVAL; 849 errno = EINVAL;
848 return GNUNET_SYSERR; 850 return GNUNET_SYSERR;
849 } 851 }
850 852
851 853
852 int flags; 854 int flags;
853 ssize_t ret; 855 ssize_t ret;
854 856
855 /* set to blocking, write, then set back */ 857 /* set to blocking, write, then set back */
856 flags = fcntl(h->fd, F_GETFL); 858 flags = fcntl (h->fd, F_GETFL);
857 if (0 != (flags & O_NONBLOCK)) 859 if (0 != (flags & O_NONBLOCK))
858 (void)fcntl(h->fd, F_SETFL, flags - O_NONBLOCK); 860 (void) fcntl (h->fd, F_SETFL, flags - O_NONBLOCK);
859 ret = write(h->fd, buffer, n); 861 ret = write (h->fd, buffer, n);
860 if (0 == (flags & O_NONBLOCK)) 862 if (0 == (flags & O_NONBLOCK))
861 (void)fcntl(h->fd, F_SETFL, flags); 863 (void) fcntl (h->fd, F_SETFL, flags);
862 return ret; 864 return ret;
863} 865}
864 866
@@ -874,23 +876,23 @@ GNUNET_DISK_file_write_blocking(const struct GNUNET_DISK_FileHandle *h,
874 * @return number of bytes written on success, #GNUNET_SYSERR on error 876 * @return number of bytes written on success, #GNUNET_SYSERR on error
875 */ 877 */
876ssize_t 878ssize_t
877GNUNET_DISK_fn_write(const char *fn, 879GNUNET_DISK_fn_write (const char *fn,
878 const void *buffer, 880 const void *buffer,
879 size_t n, 881 size_t n,
880 enum GNUNET_DISK_AccessPermissions mode) 882 enum GNUNET_DISK_AccessPermissions mode)
881{ 883{
882 struct GNUNET_DISK_FileHandle *fh; 884 struct GNUNET_DISK_FileHandle *fh;
883 ssize_t ret; 885 ssize_t ret;
884 886
885 fh = 887 fh =
886 GNUNET_DISK_file_open(fn, 888 GNUNET_DISK_file_open (fn,
887 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | 889 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
888 GNUNET_DISK_OPEN_CREATE, 890 | GNUNET_DISK_OPEN_CREATE,
889 mode); 891 mode);
890 if (!fh) 892 if (! fh)
891 return GNUNET_SYSERR; 893 return GNUNET_SYSERR;
892 ret = GNUNET_DISK_file_write(fh, buffer, n); 894 ret = GNUNET_DISK_file_write (fh, buffer, n);
893 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh)); 895 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
894 return ret; 896 return ret;
895} 897}
896 898
@@ -906,9 +908,9 @@ GNUNET_DISK_fn_write(const char *fn,
906 * ieration aborted by callback returning #GNUNET_SYSERR 908 * ieration aborted by callback returning #GNUNET_SYSERR
907 */ 909 */
908int 910int
909GNUNET_DISK_directory_scan(const char *dir_name, 911GNUNET_DISK_directory_scan (const char *dir_name,
910 GNUNET_FileNameCallback callback, 912 GNUNET_FileNameCallback callback,
911 void *callback_cls) 913 void *callback_cls)
912{ 914{
913 DIR *dinfo; 915 DIR *dinfo;
914 struct dirent *finfo; 916 struct dirent *finfo;
@@ -920,80 +922,80 @@ GNUNET_DISK_directory_scan(const char *dir_name,
920 unsigned int name_len; 922 unsigned int name_len;
921 unsigned int n_size; 923 unsigned int n_size;
922 924
923 GNUNET_assert(NULL != dir_name); 925 GNUNET_assert (NULL != dir_name);
924 dname = GNUNET_STRINGS_filename_expand(dir_name); 926 dname = GNUNET_STRINGS_filename_expand (dir_name);
925 if (NULL == dname) 927 if (NULL == dname)
926 return GNUNET_SYSERR; 928 return GNUNET_SYSERR;
927 while ((strlen(dname) > 0) && (dname[strlen(dname) - 1] == DIR_SEPARATOR)) 929 while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR))
928 dname[strlen(dname) - 1] = '\0'; 930 dname[strlen (dname) - 1] = '\0';
929 if (0 != stat(dname, &istat)) 931 if (0 != stat (dname, &istat))
930 { 932 {
931 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", dname); 933 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname);
932 GNUNET_free(dname); 934 GNUNET_free (dname);
933 return GNUNET_SYSERR; 935 return GNUNET_SYSERR;
934 } 936 }
935 if (!S_ISDIR(istat.st_mode)) 937 if (! S_ISDIR (istat.st_mode))
936 { 938 {
937 LOG(GNUNET_ERROR_TYPE_WARNING, 939 LOG (GNUNET_ERROR_TYPE_WARNING,
938 _("Expected `%s' to be a directory!\n"), 940 _ ("Expected `%s' to be a directory!\n"),
939 dir_name); 941 dir_name);
940 GNUNET_free(dname); 942 GNUNET_free (dname);
941 return GNUNET_SYSERR; 943 return GNUNET_SYSERR;
942 } 944 }
943 errno = 0; 945 errno = 0;
944 dinfo = opendir(dname); 946 dinfo = opendir (dname);
945 if ((EACCES == errno) || (NULL == dinfo)) 947 if ((EACCES == errno) || (NULL == dinfo))
946 { 948 {
947 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "opendir", dname); 949 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname);
948 if (NULL != dinfo) 950 if (NULL != dinfo)
949 closedir(dinfo); 951 closedir (dinfo);
950 GNUNET_free(dname); 952 GNUNET_free (dname);
951 return GNUNET_SYSERR; 953 return GNUNET_SYSERR;
952 } 954 }
953 name_len = 256; 955 name_len = 256;
954 n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; 956 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
955 name = GNUNET_malloc(n_size); 957 name = GNUNET_malloc (n_size);
956 while (NULL != (finfo = readdir(dinfo))) 958 while (NULL != (finfo = readdir (dinfo)))
959 {
960 if ((0 == strcmp (finfo->d_name, ".")) ||
961 (0 == strcmp (finfo->d_name, "..")))
962 continue;
963 if (NULL != callback)
957 { 964 {
958 if ((0 == strcmp(finfo->d_name, ".")) || 965 if (name_len < strlen (finfo->d_name))
959 (0 == strcmp(finfo->d_name, ".."))) 966 {
960 continue; 967 GNUNET_free (name);
961 if (NULL != callback) 968 name_len = strlen (finfo->d_name);
962 { 969 n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1;
963 if (name_len < strlen(finfo->d_name)) 970 name = GNUNET_malloc (n_size);
964 { 971 }
965 GNUNET_free(name); 972 /* dname can end in "/" only if dname == "/";
966 name_len = strlen(finfo->d_name); 973 * if dname does not end in "/", we need to add
967 n_size = strlen(dname) + name_len + strlen(DIR_SEPARATOR_STR) + 1; 974 * a "/" (otherwise, we must not!) */
968 name = GNUNET_malloc(n_size); 975 GNUNET_snprintf (name,
969 } 976 n_size,
970 /* dname can end in "/" only if dname == "/"; 977 "%s%s%s",
971 * if dname does not end in "/", we need to add 978 dname,
972 * a "/" (otherwise, we must not!) */ 979 (0 == strcmp (dname, DIR_SEPARATOR_STR))
973 GNUNET_snprintf(name, 980 ? ""
974 n_size, 981 : DIR_SEPARATOR_STR,
975 "%s%s%s", 982 finfo->d_name);
976 dname, 983 ret = callback (callback_cls, name);
977 (0 == strcmp(dname, DIR_SEPARATOR_STR)) 984 if (GNUNET_OK != ret)
978 ? "" 985 {
979 : DIR_SEPARATOR_STR, 986 closedir (dinfo);
980 finfo->d_name); 987 GNUNET_free (name);
981 ret = callback(callback_cls, name); 988 GNUNET_free (dname);
982 if (GNUNET_OK != ret) 989 if (GNUNET_NO == ret)
983 { 990 return count;
984 closedir(dinfo); 991 return GNUNET_SYSERR;
985 GNUNET_free(name); 992 }
986 GNUNET_free(dname);
987 if (GNUNET_NO == ret)
988 return count;
989 return GNUNET_SYSERR;
990 }
991 }
992 count++;
993 } 993 }
994 closedir(dinfo); 994 count++;
995 GNUNET_free(name); 995 }
996 GNUNET_free(dname); 996 closedir (dinfo);
997 GNUNET_free (name);
998 GNUNET_free (dname);
997 return count; 999 return count;
998} 1000}
999 1001
@@ -1007,10 +1009,10 @@ GNUNET_DISK_directory_scan(const char *dir_name,
1007 * @return #GNUNET_OK 1009 * @return #GNUNET_OK
1008 */ 1010 */
1009static int 1011static int
1010remove_helper(void *unused, const char *fn) 1012remove_helper (void *unused, const char *fn)
1011{ 1013{
1012 (void)unused; 1014 (void) unused;
1013 (void)GNUNET_DISK_directory_remove(fn); 1015 (void) GNUNET_DISK_directory_remove (fn);
1014 return GNUNET_OK; 1016 return GNUNET_OK;
1015} 1017}
1016 1018
@@ -1023,37 +1025,37 @@ remove_helper(void *unused, const char *fn)
1023 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1025 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1024 */ 1026 */
1025int 1027int
1026GNUNET_DISK_directory_remove(const char *filename) 1028GNUNET_DISK_directory_remove (const char *filename)
1027{ 1029{
1028 struct stat istat; 1030 struct stat istat;
1029 1031
1030 if (NULL == filename) 1032 if (NULL == filename)
1031 { 1033 {
1032 GNUNET_break(0); 1034 GNUNET_break (0);
1033 return GNUNET_SYSERR; 1035 return GNUNET_SYSERR;
1034 } 1036 }
1035 if (0 != lstat(filename, &istat)) 1037 if (0 != lstat (filename, &istat))
1036 return GNUNET_NO; /* file may not exist... */ 1038 return GNUNET_NO; /* file may not exist... */
1037 (void)chmod(filename, S_IWUSR | S_IRUSR | S_IXUSR); 1039 (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR);
1038 if (0 == unlink(filename)) 1040 if (0 == unlink (filename))
1039 return GNUNET_OK; 1041 return GNUNET_OK;
1040 if ((errno != EISDIR) && 1042 if ((errno != EISDIR) &&
1041 /* EISDIR is not sufficient in all cases, e.g. 1043 /* EISDIR is not sufficient in all cases, e.g.
1042 * sticky /tmp directory may result in EPERM on BSD. 1044 * sticky /tmp directory may result in EPERM on BSD.
1043 * So we also explicitly check "isDirectory" */ 1045 * So we also explicitly check "isDirectory" */
1044 (GNUNET_YES != GNUNET_DISK_directory_test(filename, GNUNET_YES))) 1046 (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES)))
1045 { 1047 {
1046 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1048 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1047 return GNUNET_SYSERR; 1049 return GNUNET_SYSERR;
1048 } 1050 }
1049 if (GNUNET_SYSERR == 1051 if (GNUNET_SYSERR ==
1050 GNUNET_DISK_directory_scan(filename, &remove_helper, NULL)) 1052 GNUNET_DISK_directory_scan (filename, &remove_helper, NULL))
1051 return GNUNET_SYSERR; 1053 return GNUNET_SYSERR;
1052 if (0 != rmdir(filename)) 1054 if (0 != rmdir (filename))
1053 { 1055 {
1054 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); 1056 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename);
1055 return GNUNET_SYSERR; 1057 return GNUNET_SYSERR;
1056 } 1058 }
1057 return GNUNET_OK; 1059 return GNUNET_OK;
1058} 1060}
1059 1061
@@ -1066,7 +1068,7 @@ GNUNET_DISK_directory_remove(const char *filename)
1066 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1068 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1067 */ 1069 */
1068int 1070int
1069GNUNET_DISK_file_copy(const char *src, const char *dst) 1071GNUNET_DISK_file_copy (const char *src, const char *dst)
1070{ 1072{
1071 char *buf; 1073 char *buf;
1072 uint64_t pos; 1074 uint64_t pos;
@@ -1076,55 +1078,55 @@ GNUNET_DISK_file_copy(const char *src, const char *dst)
1076 struct GNUNET_DISK_FileHandle *in; 1078 struct GNUNET_DISK_FileHandle *in;
1077 struct GNUNET_DISK_FileHandle *out; 1079 struct GNUNET_DISK_FileHandle *out;
1078 1080
1079 if (GNUNET_OK != GNUNET_DISK_file_size(src, &size, GNUNET_YES, GNUNET_YES)) 1081 if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
1080 { 1082 {
1081 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "stat", src); 1083 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src);
1082 return GNUNET_SYSERR; 1084 return GNUNET_SYSERR;
1083 } 1085 }
1084 pos = 0; 1086 pos = 0;
1085 in = 1087 in =
1086 GNUNET_DISK_file_open(src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); 1088 GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
1087 if (!in) 1089 if (! in)
1088 { 1090 {
1089 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", src); 1091 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src);
1090 return GNUNET_SYSERR; 1092 return GNUNET_SYSERR;
1091 } 1093 }
1092 out = 1094 out =
1093 GNUNET_DISK_file_open(dst, 1095 GNUNET_DISK_file_open (dst,
1094 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 1096 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
1095 GNUNET_DISK_OPEN_FAILIFEXISTS, 1097 | GNUNET_DISK_OPEN_FAILIFEXISTS,
1096 GNUNET_DISK_PERM_USER_READ | 1098 GNUNET_DISK_PERM_USER_READ
1097 GNUNET_DISK_PERM_USER_WRITE | 1099 | GNUNET_DISK_PERM_USER_WRITE
1098 GNUNET_DISK_PERM_GROUP_READ | 1100 | GNUNET_DISK_PERM_GROUP_READ
1099 GNUNET_DISK_PERM_GROUP_WRITE); 1101 | GNUNET_DISK_PERM_GROUP_WRITE);
1100 if (!out) 1102 if (! out)
1101 { 1103 {
1102 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", dst); 1104 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst);
1103 GNUNET_DISK_file_close(in); 1105 GNUNET_DISK_file_close (in);
1104 return GNUNET_SYSERR; 1106 return GNUNET_SYSERR;
1105 } 1107 }
1106 buf = GNUNET_malloc(COPY_BLK_SIZE); 1108 buf = GNUNET_malloc (COPY_BLK_SIZE);
1107 while (pos < size) 1109 while (pos < size)
1108 { 1110 {
1109 len = COPY_BLK_SIZE; 1111 len = COPY_BLK_SIZE;
1110 if (len > size - pos) 1112 if (len > size - pos)
1111 len = size - pos; 1113 len = size - pos;
1112 sret = GNUNET_DISK_file_read(in, buf, len); 1114 sret = GNUNET_DISK_file_read (in, buf, len);
1113 if ((sret < 0) || (len != (size_t)sret)) 1115 if ((sret < 0) || (len != (size_t) sret))
1114 goto FAIL; 1116 goto FAIL;
1115 sret = GNUNET_DISK_file_write(out, buf, len); 1117 sret = GNUNET_DISK_file_write (out, buf, len);
1116 if ((sret < 0) || (len != (size_t)sret)) 1118 if ((sret < 0) || (len != (size_t) sret))
1117 goto FAIL; 1119 goto FAIL;
1118 pos += len; 1120 pos += len;
1119 } 1121 }
1120 GNUNET_free(buf); 1122 GNUNET_free (buf);
1121 GNUNET_DISK_file_close(in); 1123 GNUNET_DISK_file_close (in);
1122 GNUNET_DISK_file_close(out); 1124 GNUNET_DISK_file_close (out);
1123 return GNUNET_OK; 1125 return GNUNET_OK;
1124FAIL: 1126FAIL:
1125 GNUNET_free(buf); 1127 GNUNET_free (buf);
1126 GNUNET_DISK_file_close(in); 1128 GNUNET_DISK_file_close (in);
1127 GNUNET_DISK_file_close(out); 1129 GNUNET_DISK_file_close (out);
1128 return GNUNET_SYSERR; 1130 return GNUNET_SYSERR;
1129} 1131}
1130 1132
@@ -1134,21 +1136,23 @@ FAIL:
1134 * @param fn the filename to canonicalize 1136 * @param fn the filename to canonicalize
1135 */ 1137 */
1136void 1138void
1137GNUNET_DISK_filename_canonicalize(char *fn) 1139GNUNET_DISK_filename_canonicalize (char *fn)
1138{ 1140{
1139 char *idx; 1141 char *idx;
1140 char c; 1142 char c;
1141 1143
1142 for (idx = fn; *idx; idx++) 1144 for (idx = fn; *idx; idx++)
1143 { 1145 {
1144 c = *idx; 1146 c = *idx;
1145 1147
1146 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || 1148 if ((c == '/') ||(c == '\\') ||(c == ':') ||(c == '*') ||(c == '?') ||(c ==
1147 c == '<' || c == '>' || c == '|') 1149 '"')
1148 { 1150 ||
1149 *idx = '_'; 1151 (c == '<') ||(c == '>') ||(c == '|') )
1150 } 1152 {
1153 *idx = '_';
1151 } 1154 }
1155 }
1152} 1156}
1153 1157
1154 1158
@@ -1160,24 +1164,24 @@ GNUNET_DISK_filename_canonicalize(char *fn)
1160 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 1164 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1161 */ 1165 */
1162int 1166int
1163GNUNET_DISK_file_change_owner(const char *filename, const char *user) 1167GNUNET_DISK_file_change_owner (const char *filename, const char *user)
1164{ 1168{
1165 struct passwd *pws; 1169 struct passwd *pws;
1166 1170
1167 pws = getpwnam(user); 1171 pws = getpwnam (user);
1168 if (NULL == pws) 1172 if (NULL == pws)
1169 { 1173 {
1170 LOG(GNUNET_ERROR_TYPE_ERROR, 1174 LOG (GNUNET_ERROR_TYPE_ERROR,
1171 _("Cannot obtain information about user `%s': %s\n"), 1175 _ ("Cannot obtain information about user `%s': %s\n"),
1172 user, 1176 user,
1173 strerror(errno)); 1177 strerror (errno));
1174 return GNUNET_SYSERR; 1178 return GNUNET_SYSERR;
1175 } 1179 }
1176 if (0 != chown(filename, pws->pw_uid, pws->pw_gid)) 1180 if (0 != chown (filename, pws->pw_uid, pws->pw_gid))
1177 { 1181 {
1178 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "chown", filename); 1182 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename);
1179 return GNUNET_SYSERR; 1183 return GNUNET_SYSERR;
1180 } 1184 }
1181 return GNUNET_OK; 1185 return GNUNET_OK;
1182} 1186}
1183 1187
@@ -1192,26 +1196,26 @@ GNUNET_DISK_file_change_owner(const char *filename, const char *user)
1192 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1196 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1193 */ 1197 */
1194int 1198int
1195GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh, 1199GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh,
1196 off_t lock_start, 1200 off_t lock_start,
1197 off_t lock_end, 1201 off_t lock_end,
1198 int excl) 1202 int excl)
1199{ 1203{
1200 if (fh == NULL) 1204 if (fh == NULL)
1201 { 1205 {
1202 errno = EINVAL; 1206 errno = EINVAL;
1203 return GNUNET_SYSERR; 1207 return GNUNET_SYSERR;
1204 } 1208 }
1205 1209
1206 struct flock fl; 1210 struct flock fl;
1207 1211
1208 memset(&fl, 0, sizeof(struct flock)); 1212 memset (&fl, 0, sizeof(struct flock));
1209 fl.l_type = excl ? F_WRLCK : F_RDLCK; 1213 fl.l_type = excl ? F_WRLCK : F_RDLCK;
1210 fl.l_whence = SEEK_SET; 1214 fl.l_whence = SEEK_SET;
1211 fl.l_start = lock_start; 1215 fl.l_start = lock_start;
1212 fl.l_len = lock_end; 1216 fl.l_len = lock_end;
1213 1217
1214 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; 1218 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1215} 1219}
1216 1220
1217 1221
@@ -1224,25 +1228,25 @@ GNUNET_DISK_file_lock(struct GNUNET_DISK_FileHandle *fh,
1224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1228 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1225 */ 1229 */
1226int 1230int
1227GNUNET_DISK_file_unlock(struct GNUNET_DISK_FileHandle *fh, 1231GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh,
1228 off_t unlock_start, 1232 off_t unlock_start,
1229 off_t unlock_end) 1233 off_t unlock_end)
1230{ 1234{
1231 if (fh == NULL) 1235 if (fh == NULL)
1232 { 1236 {
1233 errno = EINVAL; 1237 errno = EINVAL;
1234 return GNUNET_SYSERR; 1238 return GNUNET_SYSERR;
1235 } 1239 }
1236 1240
1237 struct flock fl; 1241 struct flock fl;
1238 1242
1239 memset(&fl, 0, sizeof(struct flock)); 1243 memset (&fl, 0, sizeof(struct flock));
1240 fl.l_type = F_UNLCK; 1244 fl.l_type = F_UNLCK;
1241 fl.l_whence = SEEK_SET; 1245 fl.l_whence = SEEK_SET;
1242 fl.l_start = unlock_start; 1246 fl.l_start = unlock_start;
1243 fl.l_len = unlock_end; 1247 fl.l_len = unlock_end;
1244 1248
1245 return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK; 1249 return fcntl (fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
1246} 1250}
1247 1251
1248 1252
@@ -1259,9 +1263,9 @@ GNUNET_DISK_file_unlock(struct GNUNET_DISK_FileHandle *fh,
1259 * @return IO handle on success, NULL on error 1263 * @return IO handle on success, NULL on error
1260 */ 1264 */
1261struct GNUNET_DISK_FileHandle * 1265struct GNUNET_DISK_FileHandle *
1262GNUNET_DISK_file_open(const char *fn, 1266GNUNET_DISK_file_open (const char *fn,
1263 enum GNUNET_DISK_OpenFlags flags, 1267 enum GNUNET_DISK_OpenFlags flags,
1264 enum GNUNET_DISK_AccessPermissions perm) 1268 enum GNUNET_DISK_AccessPermissions perm)
1265{ 1269{
1266 char *expfn; 1270 char *expfn;
1267 struct GNUNET_DISK_FileHandle *ret; 1271 struct GNUNET_DISK_FileHandle *ret;
@@ -1270,7 +1274,7 @@ GNUNET_DISK_file_open(const char *fn,
1270 int mode; 1274 int mode;
1271 int fd; 1275 int fd;
1272 1276
1273 expfn = GNUNET_STRINGS_filename_expand(fn); 1277 expfn = GNUNET_STRINGS_filename_expand (fn);
1274 if (NULL == expfn) 1278 if (NULL == expfn)
1275 return NULL; 1279 return NULL;
1276 1280
@@ -1282,49 +1286,49 @@ GNUNET_DISK_file_open(const char *fn,
1282 else if (flags & GNUNET_DISK_OPEN_WRITE) 1286 else if (flags & GNUNET_DISK_OPEN_WRITE)
1283 oflags = O_WRONLY; 1287 oflags = O_WRONLY;
1284 else 1288 else
1285 { 1289 {
1286 GNUNET_break(0); 1290 GNUNET_break (0);
1287 GNUNET_free(expfn); 1291 GNUNET_free (expfn);
1288 return NULL; 1292 return NULL;
1289 } 1293 }
1290 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) 1294 if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
1291 oflags |= (O_CREAT | O_EXCL); 1295 oflags |= (O_CREAT | O_EXCL);
1292 if (flags & GNUNET_DISK_OPEN_TRUNCATE) 1296 if (flags & GNUNET_DISK_OPEN_TRUNCATE)
1293 oflags |= O_TRUNC; 1297 oflags |= O_TRUNC;
1294 if (flags & GNUNET_DISK_OPEN_APPEND) 1298 if (flags & GNUNET_DISK_OPEN_APPEND)
1295 oflags |= O_APPEND; 1299 oflags |= O_APPEND;
1296 if (GNUNET_NO == GNUNET_DISK_file_test(fn)) 1300 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
1301 {
1302 if (flags & GNUNET_DISK_OPEN_CREATE)
1297 { 1303 {
1298 if (flags & GNUNET_DISK_OPEN_CREATE) 1304 (void) GNUNET_DISK_directory_create_for_file (expfn);
1299 { 1305 oflags |= O_CREAT;
1300 (void)GNUNET_DISK_directory_create_for_file(expfn); 1306 mode = translate_unix_perms (perm);
1301 oflags |= O_CREAT;
1302 mode = translate_unix_perms(perm);
1303 }
1304 } 1307 }
1308 }
1305 1309
1306 fd = open(expfn, 1310 fd = open (expfn,
1307 oflags 1311 oflags
1308#if O_CLOEXEC 1312#if O_CLOEXEC
1309 | O_CLOEXEC 1313 | O_CLOEXEC
1310#endif 1314#endif
1311 | O_LARGEFILE, 1315 | O_LARGEFILE,
1312 mode); 1316 mode);
1313 if (fd == -1) 1317 if (fd == -1)
1314 { 1318 {
1315 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) 1319 if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
1316 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "open", expfn); 1320 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
1317 else 1321 else
1318 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_DEBUG, "open", expfn); 1322 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
1319 GNUNET_free(expfn); 1323 GNUNET_free (expfn);
1320 return NULL; 1324 return NULL;
1321 } 1325 }
1322 1326
1323 ret = GNUNET_new(struct GNUNET_DISK_FileHandle); 1327 ret = GNUNET_new (struct GNUNET_DISK_FileHandle);
1324 1328
1325 ret->fd = fd; 1329 ret->fd = fd;
1326 1330
1327 GNUNET_free(expfn); 1331 GNUNET_free (expfn);
1328 return ret; 1332 return ret;
1329} 1333}
1330 1334
@@ -1336,25 +1340,25 @@ GNUNET_DISK_file_open(const char *fn,
1336 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1340 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1337 */ 1341 */
1338int 1342int
1339GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h) 1343GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h)
1340{ 1344{
1341 int ret; 1345 int ret;
1342 1346
1343 if (h == NULL) 1347 if (h == NULL)
1344 { 1348 {
1345 errno = EINVAL; 1349 errno = EINVAL;
1346 return GNUNET_SYSERR; 1350 return GNUNET_SYSERR;
1347 } 1351 }
1348 1352
1349 ret = GNUNET_OK; 1353 ret = GNUNET_OK;
1350 1354
1351 if (close(h->fd) != 0) 1355 if (close (h->fd) != 0)
1352 { 1356 {
1353 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "close"); 1357 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close");
1354 ret = GNUNET_SYSERR; 1358 ret = GNUNET_SYSERR;
1355 } 1359 }
1356 1360
1357 GNUNET_free(h); 1361 GNUNET_free (h);
1358 return ret; 1362 return ret;
1359} 1363}
1360 1364
@@ -1366,14 +1370,14 @@ GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
1366 * @return file handle corresponding to the descriptor, NULL on error 1370 * @return file handle corresponding to the descriptor, NULL on error
1367 */ 1371 */
1368struct GNUNET_DISK_FileHandle * 1372struct GNUNET_DISK_FileHandle *
1369GNUNET_DISK_get_handle_from_int_fd(int fno) 1373GNUNET_DISK_get_handle_from_int_fd (int fno)
1370{ 1374{
1371 struct GNUNET_DISK_FileHandle *fh; 1375 struct GNUNET_DISK_FileHandle *fh;
1372 1376
1373 if ((((off_t)-1) == lseek(fno, 0, SEEK_CUR)) && (EBADF == errno)) 1377 if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno))
1374 return NULL; /* invalid FD */ 1378 return NULL; /* invalid FD */
1375 1379
1376 fh = GNUNET_new(struct GNUNET_DISK_FileHandle); 1380 fh = GNUNET_new (struct GNUNET_DISK_FileHandle);
1377 1381
1378 fh->fd = fno; 1382 fh->fd = fno;
1379 1383
@@ -1388,22 +1392,23 @@ GNUNET_DISK_get_handle_from_int_fd(int fno)
1388 * @return file handle corresponding to the descriptor 1392 * @return file handle corresponding to the descriptor
1389 */ 1393 */
1390struct GNUNET_DISK_FileHandle * 1394struct GNUNET_DISK_FileHandle *
1391GNUNET_DISK_get_handle_from_native(FILE *fd) 1395GNUNET_DISK_get_handle_from_native (FILE *fd)
1392{ 1396{
1393 int fno; 1397 int fno;
1394 1398
1395 fno = fileno(fd); 1399 fno = fileno (fd);
1396 if (-1 == fno) 1400 if (-1 == fno)
1397 return NULL; 1401 return NULL;
1398 1402
1399 return GNUNET_DISK_get_handle_from_int_fd(fno); 1403 return GNUNET_DISK_get_handle_from_int_fd (fno);
1400} 1404}
1401 1405
1402 1406
1403/** 1407/**
1404 * Handle for a memory-mapping operation. 1408 * Handle for a memory-mapping operation.
1405 */ 1409 */
1406struct GNUNET_DISK_MapHandle { 1410struct GNUNET_DISK_MapHandle
1411{
1407 /** 1412 /**
1408 * Address where the map is in memory. 1413 * Address where the map is in memory.
1409 */ 1414 */
@@ -1417,7 +1422,7 @@ struct GNUNET_DISK_MapHandle {
1417 1422
1418 1423
1419#ifndef MAP_FAILED 1424#ifndef MAP_FAILED
1420#define MAP_FAILED ((void *)-1) 1425#define MAP_FAILED ((void *) -1)
1421#endif 1426#endif
1422 1427
1423/** 1428/**
@@ -1430,16 +1435,16 @@ struct GNUNET_DISK_MapHandle {
1430 * @return pointer to the mapped memory region, NULL on failure 1435 * @return pointer to the mapped memory region, NULL on failure
1431 */ 1436 */
1432void * 1437void *
1433GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, 1438GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
1434 struct GNUNET_DISK_MapHandle **m, 1439 struct GNUNET_DISK_MapHandle **m,
1435 enum GNUNET_DISK_MapType access, 1440 enum GNUNET_DISK_MapType access,
1436 size_t len) 1441 size_t len)
1437{ 1442{
1438 if (NULL == h) 1443 if (NULL == h)
1439 { 1444 {
1440 errno = EINVAL; 1445 errno = EINVAL;
1441 return NULL; 1446 return NULL;
1442 } 1447 }
1443 1448
1444 int prot; 1449 int prot;
1445 1450
@@ -1448,14 +1453,14 @@ GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h,
1448 prot = PROT_READ; 1453 prot = PROT_READ;
1449 if (access & GNUNET_DISK_MAP_TYPE_WRITE) 1454 if (access & GNUNET_DISK_MAP_TYPE_WRITE)
1450 prot |= PROT_WRITE; 1455 prot |= PROT_WRITE;
1451 *m = GNUNET_new(struct GNUNET_DISK_MapHandle); 1456 *m = GNUNET_new (struct GNUNET_DISK_MapHandle);
1452 (*m)->addr = mmap(NULL, len, prot, MAP_SHARED, h->fd, 0); 1457 (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
1453 GNUNET_assert(NULL != (*m)->addr); 1458 GNUNET_assert (NULL != (*m)->addr);
1454 if (MAP_FAILED == (*m)->addr) 1459 if (MAP_FAILED == (*m)->addr)
1455 { 1460 {
1456 GNUNET_free(*m); 1461 GNUNET_free (*m);
1457 return NULL; 1462 return NULL;
1458 } 1463 }
1459 (*m)->len = len; 1464 (*m)->len = len;
1460 return (*m)->addr; 1465 return (*m)->addr;
1461} 1466}
@@ -1466,19 +1471,19 @@ GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h,
1466 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1471 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1467 */ 1472 */
1468int 1473int
1469GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h) 1474GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h)
1470{ 1475{
1471 int ret; 1476 int ret;
1472 1477
1473 if (h == NULL) 1478 if (h == NULL)
1474 { 1479 {
1475 errno = EINVAL; 1480 errno = EINVAL;
1476 return GNUNET_SYSERR; 1481 return GNUNET_SYSERR;
1477 } 1482 }
1478 1483
1479 ret = munmap(h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR; 1484 ret = munmap (h->addr, h->len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
1480 1485
1481 GNUNET_free(h); 1486 GNUNET_free (h);
1482 return ret; 1487 return ret;
1483} 1488}
1484 1489
@@ -1489,18 +1494,18 @@ GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
1489 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1494 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1490 */ 1495 */
1491int 1496int
1492GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h) 1497GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
1493{ 1498{
1494 if (h == NULL) 1499 if (h == NULL)
1495 { 1500 {
1496 errno = EINVAL; 1501 errno = EINVAL;
1497 return GNUNET_SYSERR; 1502 return GNUNET_SYSERR;
1498 } 1503 }
1499 1504
1500#if defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN) 1505#if defined(FREEBSD) || defined(OPENBSD) || defined(DARWIN)
1501 return fsync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; 1506 return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1502#else 1507#else
1503 return fdatasync(h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK; 1508 return fdatasync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
1504#endif 1509#endif
1505} 1510}
1506 1511
@@ -1515,26 +1520,26 @@ GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
1515 * @return handle to the new pipe, NULL on error 1520 * @return handle to the new pipe, NULL on error
1516 */ 1521 */
1517struct GNUNET_DISK_PipeHandle * 1522struct GNUNET_DISK_PipeHandle *
1518GNUNET_DISK_pipe(int blocking_read, 1523GNUNET_DISK_pipe (int blocking_read,
1519 int blocking_write, 1524 int blocking_write,
1520 int inherit_read, 1525 int inherit_read,
1521 int inherit_write) 1526 int inherit_write)
1522{ 1527{
1523 int fd[2]; 1528 int fd[2];
1524 int ret; 1529 int ret;
1525 int eno; 1530 int eno;
1526 1531
1527 (void)inherit_read; 1532 (void) inherit_read;
1528 (void)inherit_write; 1533 (void) inherit_write;
1529 ret = pipe(fd); 1534 ret = pipe (fd);
1530 if (ret == -1) 1535 if (ret == -1)
1531 { 1536 {
1532 eno = errno; 1537 eno = errno;
1533 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "pipe"); 1538 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1534 errno = eno; 1539 errno = eno;
1535 return NULL; 1540 return NULL;
1536 } 1541 }
1537 return GNUNET_DISK_pipe_from_fd(blocking_read, blocking_write, fd); 1542 return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd);
1538} 1543}
1539 1544
1540 1545
@@ -1549,11 +1554,11 @@ GNUNET_DISK_pipe(int blocking_read,
1549 * @return handle to the new pipe, NULL on error 1554 * @return handle to the new pipe, NULL on error
1550 */ 1555 */
1551struct GNUNET_DISK_PipeHandle * 1556struct GNUNET_DISK_PipeHandle *
1552GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2]) 1557GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2])
1553{ 1558{
1554 struct GNUNET_DISK_PipeHandle *p; 1559 struct GNUNET_DISK_PipeHandle *p;
1555 1560
1556 p = GNUNET_new(struct GNUNET_DISK_PipeHandle); 1561 p = GNUNET_new (struct GNUNET_DISK_PipeHandle);
1557 1562
1558 int ret; 1563 int ret;
1559 int flags; 1564 int flags;
@@ -1561,64 +1566,64 @@ GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2])
1561 1566
1562 ret = 0; 1567 ret = 0;
1563 if (fd[0] >= 0) 1568 if (fd[0] >= 0)
1569 {
1570 p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1571 p->fd[0]->fd = fd[0];
1572 if (! blocking_read)
1564 { 1573 {
1565 p->fd[0] = GNUNET_new(struct GNUNET_DISK_FileHandle); 1574 flags = fcntl (fd[0], F_GETFL);
1566 p->fd[0]->fd = fd[0]; 1575 flags |= O_NONBLOCK;
1567 if (!blocking_read) 1576 if (0 > fcntl (fd[0], F_SETFL, flags))
1568 { 1577 {
1569 flags = fcntl(fd[0], F_GETFL); 1578 ret = -1;
1570 flags |= O_NONBLOCK; 1579 eno = errno;
1571 if (0 > fcntl(fd[0], F_SETFL, flags)) 1580 }
1572 { 1581 }
1573 ret = -1; 1582 flags = fcntl (fd[0], F_GETFD);
1574 eno = errno; 1583 flags |= FD_CLOEXEC;
1575 } 1584 if (0 > fcntl (fd[0], F_SETFD, flags))
1576 } 1585 {
1577 flags = fcntl(fd[0], F_GETFD); 1586 ret = -1;
1578 flags |= FD_CLOEXEC; 1587 eno = errno;
1579 if (0 > fcntl(fd[0], F_SETFD, flags))
1580 {
1581 ret = -1;
1582 eno = errno;
1583 }
1584 } 1588 }
1589 }
1585 1590
1586 if (fd[1] >= 0) 1591 if (fd[1] >= 0)
1592 {
1593 p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle);
1594 p->fd[1]->fd = fd[1];
1595 if (! blocking_write)
1587 { 1596 {
1588 p->fd[1] = GNUNET_new(struct GNUNET_DISK_FileHandle); 1597 flags = fcntl (fd[1], F_GETFL);
1589 p->fd[1]->fd = fd[1]; 1598 flags |= O_NONBLOCK;
1590 if (!blocking_write) 1599 if (0 > fcntl (fd[1], F_SETFL, flags))
1591 { 1600 {
1592 flags = fcntl(fd[1], F_GETFL); 1601 ret = -1;
1593 flags |= O_NONBLOCK; 1602 eno = errno;
1594 if (0 > fcntl(fd[1], F_SETFL, flags)) 1603 }
1595 {
1596 ret = -1;
1597 eno = errno;
1598 }
1599 }
1600 flags = fcntl(fd[1], F_GETFD);
1601 flags |= FD_CLOEXEC;
1602 if (0 > fcntl(fd[1], F_SETFD, flags))
1603 {
1604 ret = -1;
1605 eno = errno;
1606 }
1607 } 1604 }
1608 if (ret == -1) 1605 flags = fcntl (fd[1], F_GETFD);
1606 flags |= FD_CLOEXEC;
1607 if (0 > fcntl (fd[1], F_SETFD, flags))
1609 { 1608 {
1610 errno = eno; 1609 ret = -1;
1611 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "fcntl"); 1610 eno = errno;
1612 if (p->fd[0]->fd >= 0)
1613 GNUNET_break(0 == close(p->fd[0]->fd));
1614 if (p->fd[1]->fd >= 0)
1615 GNUNET_break(0 == close(p->fd[1]->fd));
1616 GNUNET_free_non_null(p->fd[0]);
1617 GNUNET_free_non_null(p->fd[1]);
1618 GNUNET_free(p);
1619 errno = eno;
1620 return NULL;
1621 } 1611 }
1612 }
1613 if (ret == -1)
1614 {
1615 errno = eno;
1616 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fcntl");
1617 if (p->fd[0]->fd >= 0)
1618 GNUNET_break (0 == close (p->fd[0]->fd));
1619 if (p->fd[1]->fd >= 0)
1620 GNUNET_break (0 == close (p->fd[1]->fd));
1621 GNUNET_free_non_null (p->fd[0]);
1622 GNUNET_free_non_null (p->fd[1]);
1623 GNUNET_free (p);
1624 errno = eno;
1625 return NULL;
1626 }
1622 1627
1623 return p; 1628 return p;
1624} 1629}
@@ -1632,27 +1637,27 @@ GNUNET_DISK_pipe_from_fd(int blocking_read, int blocking_write, int fd[2])
1632 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1637 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1633 */ 1638 */
1634int 1639int
1635GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, 1640GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
1636 enum GNUNET_DISK_PipeEnd end) 1641 enum GNUNET_DISK_PipeEnd end)
1637{ 1642{
1638 int ret = GNUNET_OK; 1643 int ret = GNUNET_OK;
1639 1644
1640 if (end == GNUNET_DISK_PIPE_END_READ) 1645 if (end == GNUNET_DISK_PIPE_END_READ)
1646 {
1647 if (p->fd[0])
1641 { 1648 {
1642 if (p->fd[0]) 1649 ret = GNUNET_DISK_file_close (p->fd[0]);
1643 { 1650 p->fd[0] = NULL;
1644 ret = GNUNET_DISK_file_close(p->fd[0]);
1645 p->fd[0] = NULL;
1646 }
1647 } 1651 }
1652 }
1648 else if (end == GNUNET_DISK_PIPE_END_WRITE) 1653 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1654 {
1655 if (p->fd[1])
1649 { 1656 {
1650 if (p->fd[1]) 1657 ret = GNUNET_DISK_file_close (p->fd[1]);
1651 { 1658 p->fd[1] = NULL;
1652 ret = GNUNET_DISK_file_close(p->fd[1]);
1653 p->fd[1] = NULL;
1654 }
1655 } 1659 }
1660 }
1656 1661
1657 return ret; 1662 return ret;
1658} 1663}
@@ -1670,27 +1675,27 @@ GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p,
1670 * (or if that end is not present or is closed). 1675 * (or if that end is not present or is closed).
1671 */ 1676 */
1672struct GNUNET_DISK_FileHandle * 1677struct GNUNET_DISK_FileHandle *
1673GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, 1678GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p,
1674 enum GNUNET_DISK_PipeEnd end) 1679 enum GNUNET_DISK_PipeEnd end)
1675{ 1680{
1676 struct GNUNET_DISK_FileHandle *ret = NULL; 1681 struct GNUNET_DISK_FileHandle *ret = NULL;
1677 1682
1678 if (end == GNUNET_DISK_PIPE_END_READ) 1683 if (end == GNUNET_DISK_PIPE_END_READ)
1684 {
1685 if (p->fd[0])
1679 { 1686 {
1680 if (p->fd[0]) 1687 ret = p->fd[0];
1681 { 1688 p->fd[0] = NULL;
1682 ret = p->fd[0];
1683 p->fd[0] = NULL;
1684 }
1685 } 1689 }
1690 }
1686 else if (end == GNUNET_DISK_PIPE_END_WRITE) 1691 else if (end == GNUNET_DISK_PIPE_END_WRITE)
1692 {
1693 if (p->fd[1])
1687 { 1694 {
1688 if (p->fd[1]) 1695 ret = p->fd[1];
1689 { 1696 p->fd[1] = NULL;
1690 ret = p->fd[1];
1691 p->fd[1] = NULL;
1692 }
1693 } 1697 }
1698 }
1694 1699
1695 return ret; 1700 return ret;
1696} 1701}
@@ -1703,7 +1708,7 @@ GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p,
1703 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 1708 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1704 */ 1709 */
1705int 1710int
1706GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p) 1711GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
1707{ 1712{
1708 int ret = GNUNET_OK; 1713 int ret = GNUNET_OK;
1709 1714
@@ -1712,22 +1717,22 @@ GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
1712 int read_end_close_errno; 1717 int read_end_close_errno;
1713 int write_end_close_errno; 1718 int write_end_close_errno;
1714 1719
1715 read_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_READ); 1720 read_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_READ);
1716 read_end_close_errno = errno; 1721 read_end_close_errno = errno;
1717 write_end_close = GNUNET_DISK_pipe_close_end(p, GNUNET_DISK_PIPE_END_WRITE); 1722 write_end_close = GNUNET_DISK_pipe_close_end (p, GNUNET_DISK_PIPE_END_WRITE);
1718 write_end_close_errno = errno; 1723 write_end_close_errno = errno;
1719 GNUNET_free(p); 1724 GNUNET_free (p);
1720 1725
1721 if (GNUNET_OK != read_end_close) 1726 if (GNUNET_OK != read_end_close)
1722 { 1727 {
1723 errno = read_end_close_errno; 1728 errno = read_end_close_errno;
1724 ret = read_end_close; 1729 ret = read_end_close;
1725 } 1730 }
1726 else if (GNUNET_OK != write_end_close) 1731 else if (GNUNET_OK != write_end_close)
1727 { 1732 {
1728 errno = write_end_close_errno; 1733 errno = write_end_close_errno;
1729 ret = write_end_close; 1734 ret = write_end_close;
1730 } 1735 }
1731 1736
1732 return ret; 1737 return ret;
1733} 1738}
@@ -1741,19 +1746,19 @@ GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
1741 * @return handle for the respective end 1746 * @return handle for the respective end
1742 */ 1747 */
1743const struct GNUNET_DISK_FileHandle * 1748const struct GNUNET_DISK_FileHandle *
1744GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, 1749GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,
1745 enum GNUNET_DISK_PipeEnd n) 1750 enum GNUNET_DISK_PipeEnd n)
1746{ 1751{
1747 switch (n) 1752 switch (n)
1748 { 1753 {
1749 case GNUNET_DISK_PIPE_END_READ: 1754 case GNUNET_DISK_PIPE_END_READ:
1750 case GNUNET_DISK_PIPE_END_WRITE: 1755 case GNUNET_DISK_PIPE_END_WRITE:
1751 return p->fd[n]; 1756 return p->fd[n];
1752 1757
1753 default: 1758 default:
1754 GNUNET_break(0); 1759 GNUNET_break (0);
1755 return NULL; 1760 return NULL;
1756 } 1761 }
1757} 1762}
1758 1763
1759 1764
@@ -1766,16 +1771,16 @@ GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p,
1766 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1771 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1767 */ 1772 */
1768int 1773int
1769GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, 1774GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh,
1770 void *dst, 1775 void *dst,
1771 size_t dst_len) 1776 size_t dst_len)
1772{ 1777{
1773 if (NULL == fh) 1778 if (NULL == fh)
1774 return GNUNET_SYSERR; 1779 return GNUNET_SYSERR;
1775 1780
1776 if (dst_len < sizeof(int)) 1781 if (dst_len < sizeof(int))
1777 return GNUNET_SYSERR; 1782 return GNUNET_SYSERR;
1778 *((int *)dst) = fh->fd; 1783 *((int *) dst) = fh->fd;
1779 1784
1780 return GNUNET_OK; 1785 return GNUNET_OK;
1781} 1786}
@@ -1789,24 +1794,24 @@ GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh,
1789 * @return #GNUNET_OK on success 1794 * @return #GNUNET_OK on success
1790 */ 1795 */
1791static int 1796static int
1792purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) 1797purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1793{ 1798{
1794 const char *option = cls; 1799 const char *option = cls;
1795 char *tmpname; 1800 char *tmpname;
1796 1801
1797 if (GNUNET_OK != 1802 if (GNUNET_OK !=
1798 GNUNET_CONFIGURATION_get_value_filename(cfg, "PATHS", option, &tmpname)) 1803 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname))
1799 { 1804 {
1800 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "PATHS", option); 1805 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option);
1801 return GNUNET_NO; 1806 return GNUNET_NO;
1802 } 1807 }
1803 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove(tmpname)) 1808 if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname))
1804 { 1809 {
1805 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "remove", tmpname); 1810 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname);
1806 GNUNET_free(tmpname); 1811 GNUNET_free (tmpname);
1807 return GNUNET_OK; 1812 return GNUNET_OK;
1808 } 1813 }
1809 GNUNET_free(tmpname); 1814 GNUNET_free (tmpname);
1810 return GNUNET_OK; 1815 return GNUNET_OK;
1811} 1816}
1812 1817
@@ -1819,12 +1824,12 @@ purge_cfg_dir(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1819 * @param option option with the dir name to purge 1824 * @param option option with the dir name to purge
1820 */ 1825 */
1821void 1826void
1822GNUNET_DISK_purge_cfg_dir(const char *cfg_filename, const char *option) 1827GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option)
1823{ 1828{
1824 GNUNET_break(GNUNET_OK == 1829 GNUNET_break (GNUNET_OK ==
1825 GNUNET_CONFIGURATION_parse_and_run(cfg_filename, 1830 GNUNET_CONFIGURATION_parse_and_run (cfg_filename,
1826 &purge_cfg_dir, 1831 &purge_cfg_dir,
1827 (void *)option)); 1832 (void *) option));
1828} 1833}
1829 1834
1830 1835