diff options
Diffstat (limited to 'src/util/disk.c')
-rw-r--r-- | src/util/disk.c | 589 |
1 files changed, 289 insertions, 300 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index f395a375e..c1f24e4c8 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -28,11 +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) GNUNET_log_from_strerror (kind, "util-disk", syscall) | 33 | #define LOG_STRERROR(kind, syscall) \ |
34 | GNUNET_log_from_strerror (kind, "util-disk", syscall) | ||
34 | 35 | ||
35 | #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename) | 36 | #define LOG_STRERROR_FILE(kind, syscall, filename) \ |
37 | GNUNET_log_from_strerror_file (kind, "util-disk", syscall, filename) | ||
36 | 38 | ||
37 | /** | 39 | /** |
38 | * Block size for IO for copying files. | 40 | * Block size for IO for copying files. |
@@ -54,9 +56,9 @@ | |||
54 | #endif | 56 | #endif |
55 | 57 | ||
56 | #ifndef S_ISLNK | 58 | #ifndef S_ISLNK |
57 | #define _IFMT 0170000 /* type of file */ | 59 | #define _IFMT 0170000 /* type of file */ |
58 | #define _IFLNK 0120000 /* symbolic link */ | 60 | #define _IFLNK 0120000 /* symbolic link */ |
59 | #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) | 61 | #define S_ISLNK(m) (((m) &_IFMT) == _IFLNK) |
60 | #endif | 62 | #endif |
61 | 63 | ||
62 | 64 | ||
@@ -147,7 +149,8 @@ getSizeRec (void *cls, const char *fn) | |||
147 | { | 149 | { |
148 | struct GetFileSizeData *gfsd = cls; | 150 | struct GetFileSizeData *gfsd = cls; |
149 | 151 | ||
150 | #if defined (HAVE_STAT64) && !(defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) | 152 | #if defined(HAVE_STAT64) && \ |
153 | ! (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) | ||
151 | STRUCT_STAT64 buf; | 154 | STRUCT_STAT64 buf; |
152 | 155 | ||
153 | if (0 != STAT64 (fn, &buf)) | 156 | if (0 != STAT64 (fn, &buf)) |
@@ -158,7 +161,7 @@ getSizeRec (void *cls, const char *fn) | |||
158 | #else | 161 | #else |
159 | struct stat buf; | 162 | struct stat buf; |
160 | 163 | ||
161 | if (0 != STAT (fn, &buf)) | 164 | if (0 != stat (fn, &buf)) |
162 | { | 165 | { |
163 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn); | 166 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_DEBUG, "stat", fn); |
164 | return GNUNET_SYSERR; | 167 | return GNUNET_SYSERR; |
@@ -169,10 +172,10 @@ getSizeRec (void *cls, const char *fn) | |||
169 | errno = EISDIR; | 172 | errno = EISDIR; |
170 | return GNUNET_SYSERR; | 173 | return GNUNET_SYSERR; |
171 | } | 174 | } |
172 | if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) | 175 | if ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES)) |
173 | gfsd->total += buf.st_size; | 176 | gfsd->total += buf.st_size; |
174 | if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) && | 177 | if ((S_ISDIR (buf.st_mode)) && (0 == access (fn, X_OK)) && |
175 | ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) | 178 | ((! S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))) |
176 | { | 179 | { |
177 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) | 180 | if (GNUNET_SYSERR == GNUNET_DISK_directory_scan (fn, &getSizeRec, gfsd)) |
178 | return GNUNET_SYSERR; | 181 | return GNUNET_SYSERR; |
@@ -191,9 +194,9 @@ int | |||
191 | GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | 194 | GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) |
192 | { | 195 | { |
193 | #ifdef MINGW | 196 | #ifdef MINGW |
194 | return ((!h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; | 197 | return ((! h) || (h->h == INVALID_HANDLE_VALUE)) ? GNUNET_YES : GNUNET_NO; |
195 | #else | 198 | #else |
196 | return ((!h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; | 199 | return ((! h) || (h->fd == -1)) ? GNUNET_YES : GNUNET_NO; |
197 | #endif | 200 | #endif |
198 | } | 201 | } |
199 | 202 | ||
@@ -205,14 +208,13 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h) | |||
205 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 208 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
206 | */ | 209 | */ |
207 | int | 210 | int |
208 | GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, | 211 | GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size) |
209 | off_t *size) | ||
210 | { | 212 | { |
211 | #if WINDOWS | 213 | #if WINDOWS |
212 | BOOL b; | 214 | BOOL b; |
213 | LARGE_INTEGER li; | 215 | LARGE_INTEGER li; |
214 | b = GetFileSizeEx (fh->h, &li); | 216 | b = GetFileSizeEx (fh->h, &li); |
215 | if (!b) | 217 | if (! b) |
216 | { | 218 | { |
217 | SetErrnoFromWinError (GetLastError ()); | 219 | SetErrnoFromWinError (GetLastError ()); |
218 | return GNUNET_SYSERR; | 220 | return GNUNET_SYSERR; |
@@ -221,7 +223,7 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, | |||
221 | #else | 223 | #else |
222 | struct stat sbuf; | 224 | struct stat sbuf; |
223 | 225 | ||
224 | if (0 != FSTAT (fh->fd, &sbuf)) | 226 | if (0 != fstat (fh->fd, &sbuf)) |
225 | return GNUNET_SYSERR; | 227 | return GNUNET_SYSERR; |
226 | *size = sbuf.st_size; | 228 | *size = sbuf.st_size; |
227 | #endif | 229 | #endif |
@@ -239,7 +241,7 @@ GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, | |||
239 | */ | 241 | */ |
240 | off_t | 242 | off_t |
241 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | 243 | GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, |
242 | off_t offset, | 244 | off_t offset, |
243 | enum GNUNET_DISK_Seek whence) | 245 | enum GNUNET_DISK_Seek whence) |
244 | { | 246 | { |
245 | if (h == NULL) | 247 | if (h == NULL) |
@@ -253,7 +255,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | |||
253 | LARGE_INTEGER new_pos; | 255 | LARGE_INTEGER new_pos; |
254 | BOOL b; | 256 | BOOL b; |
255 | 257 | ||
256 | static DWORD t[] = { FILE_BEGIN, FILE_CURRENT, FILE_END }; | 258 | static DWORD t[] = {FILE_BEGIN, FILE_CURRENT, FILE_END}; |
257 | li.QuadPart = offset; | 259 | li.QuadPart = offset; |
258 | 260 | ||
259 | b = SetFilePointerEx (h->h, li, &new_pos, t[whence]); | 261 | b = SetFilePointerEx (h->h, li, &new_pos, t[whence]); |
@@ -264,7 +266,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | |||
264 | } | 266 | } |
265 | return (off_t) new_pos.QuadPart; | 267 | return (off_t) new_pos.QuadPart; |
266 | #else | 268 | #else |
267 | static int t[] = { SEEK_SET, SEEK_CUR, SEEK_END }; | 269 | static int t[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
268 | 270 | ||
269 | return lseek (h->fd, offset, t[whence]); | 271 | return lseek (h->fd, offset, t[whence]); |
270 | #endif | 272 | #endif |
@@ -287,9 +289,9 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, | |||
287 | */ | 289 | */ |
288 | int | 290 | int |
289 | GNUNET_DISK_file_size (const char *filename, | 291 | GNUNET_DISK_file_size (const char *filename, |
290 | uint64_t * size, | 292 | uint64_t *size, |
291 | int include_symbolic_links, | 293 | int include_symbolic_links, |
292 | int single_file_mode) | 294 | int single_file_mode) |
293 | { | 295 | { |
294 | struct GetFileSizeData gfsd; | 296 | struct GetFileSizeData gfsd; |
295 | int ret; | 297 | int ret; |
@@ -320,8 +322,9 @@ GNUNET_DISK_file_size (const char *filename, | |||
320 | * @return #GNUNET_OK on success | 322 | * @return #GNUNET_OK on success |
321 | */ | 323 | */ |
322 | int | 324 | int |
323 | GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, | 325 | GNUNET_DISK_file_get_identifiers (const char *filename, |
324 | uint64_t * ino) | 326 | uint64_t *dev, |
327 | uint64_t *ino) | ||
325 | { | 328 | { |
326 | #if WINDOWS | 329 | #if WINDOWS |
327 | { | 330 | { |
@@ -337,12 +340,13 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, | |||
337 | return GNUNET_SYSERR; | 340 | return GNUNET_SYSERR; |
338 | succ = GetFileInformationByHandle (fh->h, &info); | 341 | succ = GetFileInformationByHandle (fh->h, &info); |
339 | GNUNET_DISK_file_close (fh); | 342 | GNUNET_DISK_file_close (fh); |
340 | if (!succ) | 343 | if (! succ) |
341 | { | 344 | { |
342 | return GNUNET_SYSERR; | 345 | return GNUNET_SYSERR; |
343 | } | 346 | } |
344 | *dev = info.dwVolumeSerialNumber; | 347 | *dev = info.dwVolumeSerialNumber; |
345 | *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | info.nFileIndexLow); | 348 | *ino = ((((uint64_t) info.nFileIndexHigh) << (sizeof (DWORD) * 8)) | |
349 | info.nFileIndexLow); | ||
346 | } | 350 | } |
347 | #else /* !WINDOWS */ | 351 | #else /* !WINDOWS */ |
348 | #if HAVE_STAT | 352 | #if HAVE_STAT |
@@ -376,8 +380,8 @@ GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t * dev, | |||
376 | { | 380 | { |
377 | return GNUNET_SYSERR; | 381 | return GNUNET_SYSERR; |
378 | } | 382 | } |
379 | *dev = ((uint64_t) fbuf.f_fsid.val[0]) << 32 || | 383 | *dev = |
380 | ((uint64_t) fbuf.f_fsid.val[1]); | 384 | ((uint64_t) fbuf.f_fsid.val[0]) << 32 || ((uint64_t) fbuf.f_fsid.val[1]); |
381 | } | 385 | } |
382 | #else | 386 | #else |
383 | *dev = 0; | 387 | *dev = 0; |
@@ -402,9 +406,9 @@ mktemp_name (const char *t) | |||
402 | 406 | ||
403 | if ((t[0] != '/') && (t[0] != '\\') | 407 | if ((t[0] != '/') && (t[0] != '\\') |
404 | #if WINDOWS | 408 | #if WINDOWS |
405 | && !(isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) | 409 | && ! (isalpha ((int) t[0]) && (t[0] != '\0') && (t[1] == ':')) |
406 | #endif | 410 | #endif |
407 | ) | 411 | ) |
408 | { | 412 | { |
409 | /* FIXME: This uses system codepage on W32, not UTF-8 */ | 413 | /* FIXME: This uses system codepage on W32, not UTF-8 */ |
410 | tmpdir = getenv ("TMPDIR"); | 414 | tmpdir = getenv ("TMPDIR"); |
@@ -506,11 +510,10 @@ GNUNET_DISK_fix_permissions (const char *fn, | |||
506 | else if (GNUNET_YES == require_gid_match) | 510 | else if (GNUNET_YES == require_gid_match) |
507 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP; | 511 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP; |
508 | else | 512 | else |
509 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; | 513 | mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | |
514 | S_IWOTH | S_IXOTH; | ||
510 | if (0 != chmod (fn, mode)) | 515 | if (0 != chmod (fn, mode)) |
511 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 516 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn); |
512 | "chmod", | ||
513 | fn); | ||
514 | } | 517 | } |
515 | 518 | ||
516 | #endif | 519 | #endif |
@@ -565,15 +568,10 @@ GNUNET_DISK_file_backup (const char *fil) | |||
565 | num = 0; | 568 | num = 0; |
566 | do | 569 | do |
567 | { | 570 | { |
568 | GNUNET_snprintf (target, slen, | 571 | GNUNET_snprintf (target, slen, "%s.%u~", fil, num++); |
569 | "%s.%u~", | ||
570 | fil, | ||
571 | num++); | ||
572 | } while (0 == access (target, F_OK)); | 572 | } while (0 == access (target, F_OK)); |
573 | if (0 != rename (fil, target)) | 573 | if (0 != rename (fil, target)) |
574 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | 574 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "rename", fil); |
575 | "rename", | ||
576 | fil); | ||
577 | GNUNET_free (target); | 575 | GNUNET_free (target); |
578 | } | 576 | } |
579 | 577 | ||
@@ -606,7 +604,7 @@ GNUNET_DISK_mktemp (const char *t) | |||
606 | return NULL; | 604 | return NULL; |
607 | } | 605 | } |
608 | umask (omask); | 606 | umask (omask); |
609 | if (0 != CLOSE (fd)) | 607 | if (0 != close (fd)) |
610 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); | 608 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "close", fn); |
611 | return fn; | 609 | return fn; |
612 | } | 610 | } |
@@ -625,29 +623,29 @@ GNUNET_DISK_mktemp (const char *t) | |||
625 | * does not exist or stat'ed | 623 | * does not exist or stat'ed |
626 | */ | 624 | */ |
627 | int | 625 | int |
628 | GNUNET_DISK_directory_test (const char *fil, | 626 | GNUNET_DISK_directory_test (const char *fil, int is_readable) |
629 | int is_readable) | ||
630 | { | 627 | { |
631 | struct stat filestat; | 628 | struct stat filestat; |
632 | int ret; | 629 | int ret; |
633 | 630 | ||
634 | ret = STAT (fil, &filestat); | 631 | ret = stat (fil, &filestat); |
635 | if (ret != 0) | 632 | if (ret != 0) |
636 | { | 633 | { |
637 | if (errno != ENOENT) | 634 | if (errno != ENOENT) |
638 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); | 635 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", fil); |
639 | return GNUNET_SYSERR; | 636 | return GNUNET_SYSERR; |
640 | } | 637 | } |
641 | if (!S_ISDIR (filestat.st_mode)) | 638 | if (! S_ISDIR (filestat.st_mode)) |
642 | { | 639 | { |
643 | LOG (GNUNET_ERROR_TYPE_INFO, | 640 | LOG (GNUNET_ERROR_TYPE_INFO, |
644 | "A file already exits with the same name %s\n", fil); | 641 | "A file already exits with the same name %s\n", |
642 | fil); | ||
645 | return GNUNET_NO; | 643 | return GNUNET_NO; |
646 | } | 644 | } |
647 | if (GNUNET_YES == is_readable) | 645 | if (GNUNET_YES == is_readable) |
648 | ret = ACCESS (fil, R_OK | X_OK); | 646 | ret = access (fil, R_OK | X_OK); |
649 | else | 647 | else |
650 | ret = ACCESS (fil, X_OK); | 648 | ret = access (fil, X_OK); |
651 | if (ret < 0) | 649 | if (ret < 0) |
652 | { | 650 | { |
653 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); | 651 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", fil); |
@@ -676,7 +674,7 @@ GNUNET_DISK_file_test (const char *fil) | |||
676 | if (rdir == NULL) | 674 | if (rdir == NULL) |
677 | return GNUNET_SYSERR; | 675 | return GNUNET_SYSERR; |
678 | 676 | ||
679 | ret = STAT (rdir, &filestat); | 677 | ret = stat (rdir, &filestat); |
680 | if (ret != 0) | 678 | if (ret != 0) |
681 | { | 679 | { |
682 | if (errno != ENOENT) | 680 | if (errno != ENOENT) |
@@ -688,12 +686,12 @@ GNUNET_DISK_file_test (const char *fil) | |||
688 | GNUNET_free (rdir); | 686 | GNUNET_free (rdir); |
689 | return GNUNET_NO; | 687 | return GNUNET_NO; |
690 | } | 688 | } |
691 | if (!S_ISREG (filestat.st_mode)) | 689 | if (! S_ISREG (filestat.st_mode)) |
692 | { | 690 | { |
693 | GNUNET_free (rdir); | 691 | GNUNET_free (rdir); |
694 | return GNUNET_NO; | 692 | return GNUNET_NO; |
695 | } | 693 | } |
696 | if (ACCESS (rdir, F_OK) < 0) | 694 | if (access (rdir, F_OK) < 0) |
697 | { | 695 | { |
698 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); | 696 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "access", rdir); |
699 | GNUNET_free (rdir); | 697 | GNUNET_free (rdir); |
@@ -728,7 +726,7 @@ GNUNET_DISK_directory_create (const char *dir) | |||
728 | 726 | ||
729 | len = strlen (rdir); | 727 | len = strlen (rdir); |
730 | #ifndef MINGW | 728 | #ifndef MINGW |
731 | pos = 1; /* skip heading '/' */ | 729 | pos = 1; /* skip heading '/' */ |
732 | #else | 730 | #else |
733 | /* Local or Network path? */ | 731 | /* Local or Network path? */ |
734 | if (strncmp (rdir, "\\\\", 2) == 0) | 732 | if (strncmp (rdir, "\\\\", 2) == 0) |
@@ -746,7 +744,7 @@ GNUNET_DISK_directory_create (const char *dir) | |||
746 | } | 744 | } |
747 | else | 745 | else |
748 | { | 746 | { |
749 | pos = 3; /* strlen("C:\\") */ | 747 | pos = 3; /* strlen("C:\\") */ |
750 | } | 748 | } |
751 | #endif | 749 | #endif |
752 | /* Check which low level directories already exist */ | 750 | /* Check which low level directories already exist */ |
@@ -796,11 +794,13 @@ GNUNET_DISK_directory_create (const char *dir) | |||
796 | if (GNUNET_SYSERR == ret) | 794 | if (GNUNET_SYSERR == ret) |
797 | { | 795 | { |
798 | #ifndef MINGW | 796 | #ifndef MINGW |
799 | ret = mkdir (rdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); /* 755 */ | 797 | ret = mkdir (rdir, |
798 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | | ||
799 | S_IXOTH); /* 755 */ | ||
800 | #else | 800 | #else |
801 | wchar_t wrdir[MAX_PATH + 1]; | 801 | wchar_t wrdir[MAX_PATH + 1]; |
802 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(rdir, wrdir)) | 802 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (rdir, wrdir)) |
803 | ret = !CreateDirectoryW (wrdir, NULL); | 803 | ret = ! CreateDirectoryW (wrdir, NULL); |
804 | else | 804 | else |
805 | ret = 1; | 805 | ret = 1; |
806 | #endif | 806 | #endif |
@@ -843,7 +843,7 @@ GNUNET_DISK_directory_create_for_file (const char *filename) | |||
843 | errno = EINVAL; | 843 | errno = EINVAL; |
844 | return GNUNET_SYSERR; | 844 | return GNUNET_SYSERR; |
845 | } | 845 | } |
846 | if (0 == ACCESS (rdir, W_OK)) | 846 | if (0 == access (rdir, W_OK)) |
847 | { | 847 | { |
848 | GNUNET_free (rdir); | 848 | GNUNET_free (rdir); |
849 | return GNUNET_OK; | 849 | return GNUNET_OK; |
@@ -860,7 +860,7 @@ GNUNET_DISK_directory_create_for_file (const char *filename) | |||
860 | rdir = GNUNET_strdup ("/"); | 860 | rdir = GNUNET_strdup ("/"); |
861 | } | 861 | } |
862 | ret = GNUNET_DISK_directory_create (rdir); | 862 | ret = GNUNET_DISK_directory_create (rdir); |
863 | if ((GNUNET_OK == ret) && (0 != ACCESS (rdir, W_OK))) | 863 | if ((GNUNET_OK == ret) && (0 != access (rdir, W_OK))) |
864 | ret = GNUNET_NO; | 864 | ret = GNUNET_NO; |
865 | eno = errno; | 865 | eno = errno; |
866 | GNUNET_free (rdir); | 866 | GNUNET_free (rdir); |
@@ -893,7 +893,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, | |||
893 | 893 | ||
894 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 894 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
895 | { | 895 | { |
896 | if (!ReadFile (h->h, result, len, &bytes_read, NULL)) | 896 | if (! ReadFile (h->h, result, len, &bytes_read, NULL)) |
897 | { | 897 | { |
898 | SetErrnoFromWinError (GetLastError ()); | 898 | SetErrnoFromWinError (GetLastError ()); |
899 | return GNUNET_SYSERR; | 899 | return GNUNET_SYSERR; |
@@ -901,7 +901,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, | |||
901 | } | 901 | } |
902 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 902 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
903 | { | 903 | { |
904 | if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) | 904 | if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) |
905 | { | 905 | { |
906 | if (GetLastError () != ERROR_IO_PENDING) | 906 | if (GetLastError () != ERROR_IO_PENDING) |
907 | { | 907 | { |
@@ -939,8 +939,8 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, | |||
939 | */ | 939 | */ |
940 | ssize_t | 940 | ssize_t |
941 | GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | 941 | GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, |
942 | void *result, | 942 | void *result, |
943 | size_t len) | 943 | size_t len) |
944 | { | 944 | { |
945 | if (NULL == h) | 945 | if (NULL == h) |
946 | { | 946 | { |
@@ -953,7 +953,7 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
953 | 953 | ||
954 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 954 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
955 | { | 955 | { |
956 | if (!ReadFile (h->h, result, len, &bytes_read, NULL)) | 956 | if (! ReadFile (h->h, result, len, &bytes_read, NULL)) |
957 | { | 957 | { |
958 | SetErrnoFromWinError (GetLastError ()); | 958 | SetErrnoFromWinError (GetLastError ()); |
959 | return GNUNET_SYSERR; | 959 | return GNUNET_SYSERR; |
@@ -961,26 +961,25 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
961 | } | 961 | } |
962 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 962 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
963 | { | 963 | { |
964 | if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) | 964 | if (! ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) |
965 | { | 965 | { |
966 | if (GetLastError () != ERROR_IO_PENDING) | 966 | if (GetLastError () != ERROR_IO_PENDING) |
967 | { | 967 | { |
968 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); | 968 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
969 | "Error reading from pipe: %u\n", | ||
970 | GetLastError ()); | ||
969 | SetErrnoFromWinError (GetLastError ()); | 971 | SetErrnoFromWinError (GetLastError ()); |
970 | return GNUNET_SYSERR; | 972 | return GNUNET_SYSERR; |
971 | } | 973 | } |
972 | else | 974 | else |
973 | { | 975 | { |
974 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 976 | LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadFile() queued a read, cancelling\n"); |
975 | "ReadFile() queued a read, cancelling\n"); | ||
976 | CancelIo (h->h); | 977 | CancelIo (h->h); |
977 | errno = EAGAIN; | 978 | errno = EAGAIN; |
978 | return GNUNET_SYSERR; | 979 | return GNUNET_SYSERR; |
979 | } | 980 | } |
980 | } | 981 | } |
981 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 982 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytes_read); |
982 | "Read %u bytes\n", | ||
983 | bytes_read); | ||
984 | } | 983 | } |
985 | else | 984 | else |
986 | { | 985 | { |
@@ -997,11 +996,11 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
997 | (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK); | 996 | (void) fcntl (h->fd, F_SETFL, flags | O_NONBLOCK); |
998 | ret = read (h->fd, result, len); | 997 | ret = read (h->fd, result, len); |
999 | if (0 == (flags & O_NONBLOCK)) | 998 | if (0 == (flags & O_NONBLOCK)) |
1000 | { | 999 | { |
1001 | int eno = errno; | 1000 | int eno = errno; |
1002 | (void) fcntl (h->fd, F_SETFL, flags); | 1001 | (void) fcntl (h->fd, F_SETFL, flags); |
1003 | errno = eno; | 1002 | errno = eno; |
1004 | } | 1003 | } |
1005 | return ret; | 1004 | return ret; |
1006 | #endif | 1005 | #endif |
1007 | } | 1006 | } |
@@ -1016,17 +1015,13 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, | |||
1016 | * @return number of bytes read, #GNUNET_SYSERR on failure | 1015 | * @return number of bytes read, #GNUNET_SYSERR on failure |
1017 | */ | 1016 | */ |
1018 | ssize_t | 1017 | ssize_t |
1019 | GNUNET_DISK_fn_read (const char *fn, | 1018 | GNUNET_DISK_fn_read (const char *fn, void *result, size_t len) |
1020 | void *result, | ||
1021 | size_t len) | ||
1022 | { | 1019 | { |
1023 | struct GNUNET_DISK_FileHandle *fh; | 1020 | struct GNUNET_DISK_FileHandle *fh; |
1024 | ssize_t ret; | 1021 | ssize_t ret; |
1025 | int eno; | 1022 | int eno; |
1026 | 1023 | ||
1027 | fh = GNUNET_DISK_file_open (fn, | 1024 | fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
1028 | GNUNET_DISK_OPEN_READ, | ||
1029 | GNUNET_DISK_PERM_NONE); | ||
1030 | if (NULL == fh) | 1025 | if (NULL == fh) |
1031 | return GNUNET_SYSERR; | 1026 | return GNUNET_SYSERR; |
1032 | ret = GNUNET_DISK_file_read (fh, result, len); | 1027 | ret = GNUNET_DISK_file_read (fh, result, len); |
@@ -1046,9 +1041,9 @@ GNUNET_DISK_fn_read (const char *fn, | |||
1046 | * @return number of bytes written on success, #GNUNET_SYSERR on error | 1041 | * @return number of bytes written on success, #GNUNET_SYSERR on error |
1047 | */ | 1042 | */ |
1048 | ssize_t | 1043 | ssize_t |
1049 | GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | 1044 | GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle *h, |
1050 | const void *buffer, | 1045 | const void *buffer, |
1051 | size_t n) | 1046 | size_t n) |
1052 | { | 1047 | { |
1053 | if (NULL == h) | 1048 | if (NULL == h) |
1054 | { | 1049 | { |
@@ -1061,7 +1056,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
1061 | 1056 | ||
1062 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) | 1057 | if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) |
1063 | { | 1058 | { |
1064 | if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) | 1059 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) |
1065 | { | 1060 | { |
1066 | SetErrnoFromWinError (GetLastError ()); | 1061 | SetErrnoFromWinError (GetLastError ()); |
1067 | return GNUNET_SYSERR; | 1062 | return GNUNET_SYSERR; |
@@ -1070,46 +1065,50 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
1070 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) | 1065 | else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) |
1071 | { | 1066 | { |
1072 | LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); | 1067 | LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); |
1073 | if (!WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite)) | 1068 | if (! WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite)) |
1074 | { | 1069 | { |
1075 | if (GetLastError () != ERROR_IO_PENDING) | 1070 | if (GetLastError () != ERROR_IO_PENDING) |
1076 | { | 1071 | { |
1077 | SetErrnoFromWinError (GetLastError ()); | 1072 | SetErrnoFromWinError (GetLastError ()); |
1078 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", | 1073 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1079 | GetLastError ()); | 1074 | "Error writing to pipe: %u\n", |
1075 | GetLastError ()); | ||
1080 | return GNUNET_SYSERR; | 1076 | return GNUNET_SYSERR; |
1081 | } | 1077 | } |
1082 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); | 1078 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); |
1083 | if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE)) | 1079 | if (! GetOverlappedResult (h->h, h->oOverlapWrite, &bytes_written, TRUE)) |
1084 | { | 1080 | { |
1085 | SetErrnoFromWinError (GetLastError ()); | 1081 | SetErrnoFromWinError (GetLastError ()); |
1086 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1082 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1087 | "Error getting overlapped result while writing to pipe: %u\n", | 1083 | "Error getting overlapped result while writing to pipe: %u\n", |
1088 | GetLastError ()); | 1084 | GetLastError ()); |
1089 | return GNUNET_SYSERR; | 1085 | return GNUNET_SYSERR; |
1090 | } | 1086 | } |
1091 | } | 1087 | } |
1092 | else | 1088 | else |
1093 | { | 1089 | { |
1094 | DWORD ovr; | 1090 | DWORD ovr; |
1095 | if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) | 1091 | if (! GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) |
1096 | { | 1092 | { |
1097 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1093 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1098 | "Error getting control overlapped result while writing to pipe: %u\n", | 1094 | "Error getting control overlapped result while writing to pipe: %u\n", |
1099 | GetLastError ()); | 1095 | GetLastError ()); |
1100 | } | 1096 | } |
1101 | else | 1097 | else |
1102 | { | 1098 | { |
1103 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1099 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1104 | "Wrote %u bytes (ovr says %u), picking the greatest\n", | 1100 | "Wrote %u bytes (ovr says %u), picking the greatest\n", |
1105 | bytes_written, ovr); | 1101 | bytes_written, |
1102 | ovr); | ||
1106 | } | 1103 | } |
1107 | } | 1104 | } |
1108 | if (bytes_written == 0) | 1105 | if (bytes_written == 0) |
1109 | { | 1106 | { |
1110 | if (n > 0) | 1107 | if (n > 0) |
1111 | { | 1108 | { |
1112 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytes_written); | 1109 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1110 | "Wrote %u bytes, returning -1 with EAGAIN\n", | ||
1111 | bytes_written); | ||
1113 | errno = EAGAIN; | 1112 | errno = EAGAIN; |
1114 | return GNUNET_SYSERR; | 1113 | return GNUNET_SYSERR; |
1115 | } | 1114 | } |
@@ -1136,7 +1135,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
1136 | * @return number of bytes written on success, #GNUNET_SYSERR on error | 1135 | * @return number of bytes written on success, #GNUNET_SYSERR on error |
1137 | */ | 1136 | */ |
1138 | ssize_t | 1137 | ssize_t |
1139 | GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h, | 1138 | GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h, |
1140 | const void *buffer, | 1139 | const void *buffer, |
1141 | size_t n) | 1140 | size_t n) |
1142 | { | 1141 | { |
@@ -1150,28 +1149,28 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h, | |||
1150 | DWORD bytes_written; | 1149 | DWORD bytes_written; |
1151 | /* We do a non-overlapped write, which is as blocking as it gets */ | 1150 | /* We do a non-overlapped write, which is as blocking as it gets */ |
1152 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); | 1151 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing %u bytes\n", n); |
1153 | if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) | 1152 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) |
1154 | { | 1153 | { |
1155 | SetErrnoFromWinError (GetLastError ()); | 1154 | SetErrnoFromWinError (GetLastError ()); |
1156 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", | 1155 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1157 | GetLastError ()); | 1156 | "Error writing to pipe: %u\n", |
1157 | GetLastError ()); | ||
1158 | return GNUNET_SYSERR; | 1158 | return GNUNET_SYSERR; |
1159 | } | 1159 | } |
1160 | if (bytes_written == 0 && n > 0) | 1160 | if (bytes_written == 0 && n > 0) |
1161 | { | 1161 | { |
1162 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); | 1162 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for pipe to clean\n"); |
1163 | WaitForSingleObject (h->h, INFINITE); | 1163 | WaitForSingleObject (h->h, INFINITE); |
1164 | if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) | 1164 | if (! WriteFile (h->h, buffer, n, &bytes_written, NULL)) |
1165 | { | 1165 | { |
1166 | SetErrnoFromWinError (GetLastError ()); | 1166 | SetErrnoFromWinError (GetLastError ()); |
1167 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", | 1167 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1168 | GetLastError ()); | 1168 | "Error writing to pipe: %u\n", |
1169 | GetLastError ()); | ||
1169 | return GNUNET_SYSERR; | 1170 | return GNUNET_SYSERR; |
1170 | } | 1171 | } |
1171 | } | 1172 | } |
1172 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1173 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); |
1173 | "Wrote %u bytes\n", | ||
1174 | bytes_written); | ||
1175 | return bytes_written; | 1174 | return bytes_written; |
1176 | #else | 1175 | #else |
1177 | int flags; | 1176 | int flags; |
@@ -1208,9 +1207,11 @@ GNUNET_DISK_fn_write (const char *fn, | |||
1208 | struct GNUNET_DISK_FileHandle *fh; | 1207 | struct GNUNET_DISK_FileHandle *fh; |
1209 | ssize_t ret; | 1208 | ssize_t ret; |
1210 | 1209 | ||
1211 | fh = GNUNET_DISK_file_open (fn, | 1210 | fh = |
1212 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | 1211 | GNUNET_DISK_file_open (fn, |
1213 | | GNUNET_DISK_OPEN_CREATE, mode); | 1212 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | |
1213 | GNUNET_DISK_OPEN_CREATE, | ||
1214 | mode); | ||
1214 | if (! fh) | 1215 | if (! fh) |
1215 | return GNUNET_SYSERR; | 1216 | return GNUNET_SYSERR; |
1216 | ret = GNUNET_DISK_file_write (fh, buffer, n); | 1217 | ret = GNUNET_DISK_file_write (fh, buffer, n); |
@@ -1248,45 +1249,39 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1248 | dname = GNUNET_STRINGS_filename_expand (dir_name); | 1249 | dname = GNUNET_STRINGS_filename_expand (dir_name); |
1249 | if (NULL == dname) | 1250 | if (NULL == dname) |
1250 | return GNUNET_SYSERR; | 1251 | return GNUNET_SYSERR; |
1251 | while ( (strlen (dname) > 0) && | 1252 | while ((strlen (dname) > 0) && (dname[strlen (dname) - 1] == DIR_SEPARATOR)) |
1252 | (dname[strlen (dname) - 1] == DIR_SEPARATOR) ) | ||
1253 | dname[strlen (dname) - 1] = '\0'; | 1253 | dname[strlen (dname) - 1] = '\0'; |
1254 | if (0 != STAT (dname, &istat)) | 1254 | if (0 != stat (dname, &istat)) |
1255 | { | 1255 | { |
1256 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, | 1256 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", dname); |
1257 | "stat", | ||
1258 | dname); | ||
1259 | GNUNET_free (dname); | 1257 | GNUNET_free (dname); |
1260 | return GNUNET_SYSERR; | 1258 | return GNUNET_SYSERR; |
1261 | } | 1259 | } |
1262 | if (! S_ISDIR (istat.st_mode)) | 1260 | if (! S_ISDIR (istat.st_mode)) |
1263 | { | 1261 | { |
1264 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1262 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1265 | _("Expected `%s' to be a directory!\n"), | 1263 | _ ("Expected `%s' to be a directory!\n"), |
1266 | dir_name); | 1264 | dir_name); |
1267 | GNUNET_free (dname); | 1265 | GNUNET_free (dname); |
1268 | return GNUNET_SYSERR; | 1266 | return GNUNET_SYSERR; |
1269 | } | 1267 | } |
1270 | errno = 0; | 1268 | errno = 0; |
1271 | dinfo = OPENDIR (dname); | 1269 | dinfo = opendir (dname); |
1272 | if ( (EACCES == errno) || | 1270 | if ((EACCES == errno) || (NULL == dinfo)) |
1273 | (NULL == dinfo) ) | ||
1274 | { | 1271 | { |
1275 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, | 1272 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "opendir", dname); |
1276 | "opendir", | ||
1277 | dname); | ||
1278 | if (NULL != dinfo) | 1273 | if (NULL != dinfo) |
1279 | CLOSEDIR (dinfo); | 1274 | closedir (dinfo); |
1280 | GNUNET_free (dname); | 1275 | GNUNET_free (dname); |
1281 | return GNUNET_SYSERR; | 1276 | return GNUNET_SYSERR; |
1282 | } | 1277 | } |
1283 | name_len = 256; | 1278 | name_len = 256; |
1284 | n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1; | 1279 | n_size = strlen (dname) + name_len + strlen (DIR_SEPARATOR_STR) + 1; |
1285 | name = GNUNET_malloc (n_size); | 1280 | name = GNUNET_malloc (n_size); |
1286 | while (NULL != (finfo = READDIR (dinfo))) | 1281 | while (NULL != (finfo = readdir (dinfo))) |
1287 | { | 1282 | { |
1288 | if ( (0 == strcmp (finfo->d_name, ".")) || | 1283 | if ((0 == strcmp (finfo->d_name, ".")) || |
1289 | (0 == strcmp (finfo->d_name, "..")) ) | 1284 | (0 == strcmp (finfo->d_name, ".."))) |
1290 | continue; | 1285 | continue; |
1291 | if (NULL != callback) | 1286 | if (NULL != callback) |
1292 | { | 1287 | { |
@@ -1304,16 +1299,14 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1304 | n_size, | 1299 | n_size, |
1305 | "%s%s%s", | 1300 | "%s%s%s", |
1306 | dname, | 1301 | dname, |
1307 | (0 == strcmp (dname, | 1302 | (0 == strcmp (dname, DIR_SEPARATOR_STR)) |
1308 | DIR_SEPARATOR_STR)) | 1303 | ? "" |
1309 | ? "" | 1304 | : DIR_SEPARATOR_STR, |
1310 | : DIR_SEPARATOR_STR, | ||
1311 | finfo->d_name); | 1305 | finfo->d_name); |
1312 | ret = callback (callback_cls, | 1306 | ret = callback (callback_cls, name); |
1313 | name); | ||
1314 | if (GNUNET_OK != ret) | 1307 | if (GNUNET_OK != ret) |
1315 | { | 1308 | { |
1316 | CLOSEDIR (dinfo); | 1309 | closedir (dinfo); |
1317 | GNUNET_free (name); | 1310 | GNUNET_free (name); |
1318 | GNUNET_free (dname); | 1311 | GNUNET_free (dname); |
1319 | if (GNUNET_NO == ret) | 1312 | if (GNUNET_NO == ret) |
@@ -1323,7 +1316,7 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1323 | } | 1316 | } |
1324 | count++; | 1317 | count++; |
1325 | } | 1318 | } |
1326 | CLOSEDIR (dinfo); | 1319 | closedir (dinfo); |
1327 | GNUNET_free (name); | 1320 | GNUNET_free (name); |
1328 | GNUNET_free (dname); | 1321 | GNUNET_free (dname); |
1329 | return count; | 1322 | return count; |
@@ -1339,8 +1332,7 @@ GNUNET_DISK_directory_scan (const char *dir_name, | |||
1339 | * @return #GNUNET_OK | 1332 | * @return #GNUNET_OK |
1340 | */ | 1333 | */ |
1341 | static int | 1334 | static int |
1342 | remove_helper (void *unused, | 1335 | remove_helper (void *unused, const char *fn) |
1343 | const char *fn) | ||
1344 | { | 1336 | { |
1345 | (void) unused; | 1337 | (void) unused; |
1346 | (void) GNUNET_DISK_directory_remove (fn); | 1338 | (void) GNUNET_DISK_directory_remove (fn); |
@@ -1365,35 +1357,26 @@ GNUNET_DISK_directory_remove (const char *filename) | |||
1365 | GNUNET_break (0); | 1357 | GNUNET_break (0); |
1366 | return GNUNET_SYSERR; | 1358 | return GNUNET_SYSERR; |
1367 | } | 1359 | } |
1368 | if (0 != LSTAT (filename, &istat)) | 1360 | if (0 != lstat (filename, &istat)) |
1369 | return GNUNET_NO; /* file may not exist... */ | 1361 | return GNUNET_NO; /* file may not exist... */ |
1370 | (void) CHMOD (filename, | 1362 | (void) chmod (filename, S_IWUSR | S_IRUSR | S_IXUSR); |
1371 | S_IWUSR | S_IRUSR | S_IXUSR); | 1363 | if (0 == unlink (filename)) |
1372 | if (0 == UNLINK (filename)) | ||
1373 | return GNUNET_OK; | 1364 | return GNUNET_OK; |
1374 | if ( (errno != EISDIR) && | 1365 | if ((errno != EISDIR) && |
1375 | /* EISDIR is not sufficient in all cases, e.g. | 1366 | /* EISDIR is not sufficient in all cases, e.g. |
1376 | * sticky /tmp directory may result in EPERM on BSD. | 1367 | * sticky /tmp directory may result in EPERM on BSD. |
1377 | * So we also explicitly check "isDirectory" */ | 1368 | * So we also explicitly check "isDirectory" */ |
1378 | (GNUNET_YES != | 1369 | (GNUNET_YES != GNUNET_DISK_directory_test (filename, GNUNET_YES))) |
1379 | GNUNET_DISK_directory_test (filename, | ||
1380 | GNUNET_YES)) ) | ||
1381 | { | 1370 | { |
1382 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, | 1371 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); |
1383 | "rmdir", | ||
1384 | filename); | ||
1385 | return GNUNET_SYSERR; | 1372 | return GNUNET_SYSERR; |
1386 | } | 1373 | } |
1387 | if (GNUNET_SYSERR == | 1374 | if (GNUNET_SYSERR == |
1388 | GNUNET_DISK_directory_scan (filename, | 1375 | GNUNET_DISK_directory_scan (filename, &remove_helper, NULL)) |
1389 | &remove_helper, | ||
1390 | NULL)) | ||
1391 | return GNUNET_SYSERR; | 1376 | return GNUNET_SYSERR; |
1392 | if (0 != RMDIR (filename)) | 1377 | if (0 != rmdir (filename)) |
1393 | { | 1378 | { |
1394 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, | 1379 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "rmdir", filename); |
1395 | "rmdir", | ||
1396 | filename); | ||
1397 | return GNUNET_SYSERR; | 1380 | return GNUNET_SYSERR; |
1398 | } | 1381 | } |
1399 | return GNUNET_OK; | 1382 | return GNUNET_OK; |
@@ -1408,8 +1391,7 @@ GNUNET_DISK_directory_remove (const char *filename) | |||
1408 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 1391 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
1409 | */ | 1392 | */ |
1410 | int | 1393 | int |
1411 | GNUNET_DISK_file_copy (const char *src, | 1394 | GNUNET_DISK_file_copy (const char *src, const char *dst) |
1412 | const char *dst) | ||
1413 | { | 1395 | { |
1414 | char *buf; | 1396 | char *buf; |
1415 | uint64_t pos; | 1397 | uint64_t pos; |
@@ -1419,41 +1401,30 @@ GNUNET_DISK_file_copy (const char *src, | |||
1419 | struct GNUNET_DISK_FileHandle *in; | 1401 | struct GNUNET_DISK_FileHandle *in; |
1420 | struct GNUNET_DISK_FileHandle *out; | 1402 | struct GNUNET_DISK_FileHandle *out; |
1421 | 1403 | ||
1422 | if (GNUNET_OK != | 1404 | if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES)) |
1423 | GNUNET_DISK_file_size (src, | 1405 | { |
1424 | &size, | 1406 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", src); |
1425 | GNUNET_YES, | ||
1426 | GNUNET_YES)) | ||
1427 | { | ||
1428 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
1429 | "stat", | ||
1430 | src); | ||
1431 | return GNUNET_SYSERR; | 1407 | return GNUNET_SYSERR; |
1432 | } | 1408 | } |
1433 | pos = 0; | 1409 | pos = 0; |
1434 | in = GNUNET_DISK_file_open (src, | 1410 | in = |
1435 | GNUNET_DISK_OPEN_READ, | 1411 | GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
1436 | GNUNET_DISK_PERM_NONE); | ||
1437 | if (! in) | 1412 | if (! in) |
1438 | { | 1413 | { |
1439 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | 1414 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", src); |
1440 | "open", | ||
1441 | src); | ||
1442 | return GNUNET_SYSERR; | 1415 | return GNUNET_SYSERR; |
1443 | } | 1416 | } |
1444 | out = | 1417 | out = |
1445 | GNUNET_DISK_file_open (dst, | 1418 | GNUNET_DISK_file_open (dst, |
1446 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | | 1419 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | |
1447 | GNUNET_DISK_OPEN_FAILIFEXISTS, | 1420 | GNUNET_DISK_OPEN_FAILIFEXISTS, |
1448 | GNUNET_DISK_PERM_USER_READ | | 1421 | GNUNET_DISK_PERM_USER_READ | |
1449 | GNUNET_DISK_PERM_USER_WRITE | | 1422 | GNUNET_DISK_PERM_USER_WRITE | |
1450 | GNUNET_DISK_PERM_GROUP_READ | | 1423 | GNUNET_DISK_PERM_GROUP_READ | |
1451 | GNUNET_DISK_PERM_GROUP_WRITE); | 1424 | GNUNET_DISK_PERM_GROUP_WRITE); |
1452 | if (!out) | 1425 | if (! out) |
1453 | { | 1426 | { |
1454 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | 1427 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", dst); |
1455 | "open", | ||
1456 | dst); | ||
1457 | GNUNET_DISK_file_close (in); | 1428 | GNUNET_DISK_file_close (in); |
1458 | return GNUNET_SYSERR; | 1429 | return GNUNET_SYSERR; |
1459 | } | 1430 | } |
@@ -1463,17 +1434,11 @@ GNUNET_DISK_file_copy (const char *src, | |||
1463 | len = COPY_BLK_SIZE; | 1434 | len = COPY_BLK_SIZE; |
1464 | if (len > size - pos) | 1435 | if (len > size - pos) |
1465 | len = size - pos; | 1436 | len = size - pos; |
1466 | sret = GNUNET_DISK_file_read (in, | 1437 | sret = GNUNET_DISK_file_read (in, buf, len); |
1467 | buf, | 1438 | if ((sret < 0) || (len != (size_t) sret)) |
1468 | len); | ||
1469 | if ( (sret < 0) || | ||
1470 | (len != (size_t) sret) ) | ||
1471 | goto FAIL; | 1439 | goto FAIL; |
1472 | sret = GNUNET_DISK_file_write (out, | 1440 | sret = GNUNET_DISK_file_write (out, buf, len); |
1473 | buf, | 1441 | if ((sret < 0) || (len != (size_t) sret)) |
1474 | len); | ||
1475 | if ( (sret < 0) || | ||
1476 | (len != (size_t) sret) ) | ||
1477 | goto FAIL; | 1442 | goto FAIL; |
1478 | pos += len; | 1443 | pos += len; |
1479 | } | 1444 | } |
@@ -1503,8 +1468,7 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
1503 | { | 1468 | { |
1504 | c = *idx; | 1469 | c = *idx; |
1505 | 1470 | ||
1506 | if (c == '/' || c == '\\' || c == ':' || | 1471 | if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || |
1507 | c == '*' || c == '?' || c == '"' || | ||
1508 | c == '<' || c == '>' || c == '|') | 1472 | c == '<' || c == '>' || c == '|') |
1509 | { | 1473 | { |
1510 | *idx = '_'; | 1474 | *idx = '_'; |
@@ -1513,7 +1477,6 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
1513 | } | 1477 | } |
1514 | 1478 | ||
1515 | 1479 | ||
1516 | |||
1517 | /** | 1480 | /** |
1518 | * @brief Change owner of a file | 1481 | * @brief Change owner of a file |
1519 | * | 1482 | * |
@@ -1522,8 +1485,7 @@ GNUNET_DISK_filename_canonicalize (char *fn) | |||
1522 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 1485 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
1523 | */ | 1486 | */ |
1524 | int | 1487 | int |
1525 | GNUNET_DISK_file_change_owner (const char *filename, | 1488 | GNUNET_DISK_file_change_owner (const char *filename, const char *user) |
1526 | const char *user) | ||
1527 | { | 1489 | { |
1528 | #ifndef MINGW | 1490 | #ifndef MINGW |
1529 | struct passwd *pws; | 1491 | struct passwd *pws; |
@@ -1532,18 +1494,14 @@ GNUNET_DISK_file_change_owner (const char *filename, | |||
1532 | if (NULL == pws) | 1494 | if (NULL == pws) |
1533 | { | 1495 | { |
1534 | LOG (GNUNET_ERROR_TYPE_ERROR, | 1496 | LOG (GNUNET_ERROR_TYPE_ERROR, |
1535 | _("Cannot obtain information about user `%s': %s\n"), | 1497 | _ ("Cannot obtain information about user `%s': %s\n"), |
1536 | user, | 1498 | user, |
1537 | STRERROR (errno)); | 1499 | strerror (errno)); |
1538 | return GNUNET_SYSERR; | 1500 | return GNUNET_SYSERR; |
1539 | } | 1501 | } |
1540 | if (0 != chown (filename, | 1502 | if (0 != chown (filename, pws->pw_uid, pws->pw_gid)) |
1541 | pws->pw_uid, | ||
1542 | pws->pw_gid)) | ||
1543 | { | 1503 | { |
1544 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, | 1504 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "chown", filename); |
1545 | "chown", | ||
1546 | filename); | ||
1547 | return GNUNET_SYSERR; | 1505 | return GNUNET_SYSERR; |
1548 | } | 1506 | } |
1549 | #endif | 1507 | #endif |
@@ -1590,12 +1548,18 @@ GNUNET_DISK_file_lock (struct GNUNET_DISK_FileHandle *fh, | |||
1590 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1548 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); |
1591 | 1549 | ||
1592 | memset (&o, 0, sizeof (OVERLAPPED)); | 1550 | memset (&o, 0, sizeof (OVERLAPPED)); |
1593 | o.Offset = (DWORD) (lock_start & 0xFFFFFFFF);; | 1551 | o.Offset = (DWORD) (lock_start & 0xFFFFFFFF); |
1594 | o.OffsetHigh = (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1552 | ; |
1595 | 1553 | o.OffsetHigh = | |
1596 | if (!LockFileEx | 1554 | (DWORD) (((lock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); |
1597 | (fh->h, (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | LOCKFILE_FAIL_IMMEDIATELY, | 1555 | |
1598 | 0, diff_low, diff_high, &o)) | 1556 | if (! LockFileEx (fh->h, |
1557 | (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) | | ||
1558 | LOCKFILE_FAIL_IMMEDIATELY, | ||
1559 | 0, | ||
1560 | diff_low, | ||
1561 | diff_high, | ||
1562 | &o)) | ||
1599 | { | 1563 | { |
1600 | SetErrnoFromWinError (GetLastError ()); | 1564 | SetErrnoFromWinError (GetLastError ()); |
1601 | return GNUNET_SYSERR; | 1565 | return GNUNET_SYSERR; |
@@ -1643,10 +1607,12 @@ GNUNET_DISK_file_unlock (struct GNUNET_DISK_FileHandle *fh, | |||
1643 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1607 | diff_high = (DWORD) ((diff >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); |
1644 | 1608 | ||
1645 | memset (&o, 0, sizeof (OVERLAPPED)); | 1609 | memset (&o, 0, sizeof (OVERLAPPED)); |
1646 | o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF);; | 1610 | o.Offset = (DWORD) (unlock_start & 0xFFFFFFFF); |
1647 | o.OffsetHigh = (DWORD) (((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | 1611 | ; |
1612 | o.OffsetHigh = (DWORD) ( | ||
1613 | ((unlock_start & ~0xFFFFFFFF) >> (sizeof (DWORD) * 8)) & 0xFFFFFFFF); | ||
1648 | 1614 | ||
1649 | if (!UnlockFileEx (fh->h, 0, diff_low, diff_high, &o)) | 1615 | if (! UnlockFileEx (fh->h, 0, diff_low, diff_high, &o)) |
1650 | { | 1616 | { |
1651 | SetErrnoFromWinError (GetLastError ()); | 1617 | SetErrnoFromWinError (GetLastError ()); |
1652 | return GNUNET_SYSERR; | 1618 | return GNUNET_SYSERR; |
@@ -1694,7 +1660,7 @@ GNUNET_DISK_file_open (const char *fn, | |||
1694 | #ifndef MINGW | 1660 | #ifndef MINGW |
1695 | mode = 0; | 1661 | mode = 0; |
1696 | if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) | 1662 | if (GNUNET_DISK_OPEN_READWRITE == (flags & GNUNET_DISK_OPEN_READWRITE)) |
1697 | oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ | 1663 | oflags = O_RDWR; /* note: O_RDWR is NOT always O_RDONLY | O_WRONLY */ |
1698 | else if (flags & GNUNET_DISK_OPEN_READ) | 1664 | else if (flags & GNUNET_DISK_OPEN_READ) |
1699 | oflags = O_RDONLY; | 1665 | oflags = O_RDONLY; |
1700 | else if (flags & GNUNET_DISK_OPEN_WRITE) | 1666 | else if (flags & GNUNET_DISK_OPEN_WRITE) |
@@ -1706,26 +1672,28 @@ GNUNET_DISK_file_open (const char *fn, | |||
1706 | return NULL; | 1672 | return NULL; |
1707 | } | 1673 | } |
1708 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) | 1674 | if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS) |
1709 | oflags |= (O_CREAT | O_EXCL); | 1675 | oflags |= (O_CREAT | O_EXCL); |
1710 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) | 1676 | if (flags & GNUNET_DISK_OPEN_TRUNCATE) |
1711 | oflags |= O_TRUNC; | 1677 | oflags |= O_TRUNC; |
1712 | if (flags & GNUNET_DISK_OPEN_APPEND) | 1678 | if (flags & GNUNET_DISK_OPEN_APPEND) |
1713 | oflags |= O_APPEND; | 1679 | oflags |= O_APPEND; |
1714 | if(GNUNET_NO == GNUNET_DISK_file_test(fn)) | 1680 | if (GNUNET_NO == GNUNET_DISK_file_test (fn)) |
1715 | { | 1681 | { |
1716 | if (flags & GNUNET_DISK_OPEN_CREATE ) | 1682 | if (flags & GNUNET_DISK_OPEN_CREATE) |
1717 | { | 1683 | { |
1718 | (void) GNUNET_DISK_directory_create_for_file (expfn); | 1684 | (void) GNUNET_DISK_directory_create_for_file (expfn); |
1719 | oflags |= O_CREAT; | 1685 | oflags |= O_CREAT; |
1720 | mode = translate_unix_perms (perm); | 1686 | mode = translate_unix_perms (perm); |
1721 | } | 1687 | } |
1722 | } | 1688 | } |
1723 | 1689 | ||
1724 | fd = open (expfn, oflags | 1690 | fd = open (expfn, |
1691 | oflags | ||
1725 | #if O_CLOEXEC | 1692 | #if O_CLOEXEC |
1726 | | O_CLOEXEC | 1693 | | O_CLOEXEC |
1727 | #endif | 1694 | #endif |
1728 | | O_LARGEFILE, mode); | 1695 | | O_LARGEFILE, |
1696 | mode); | ||
1729 | if (fd == -1) | 1697 | if (fd == -1) |
1730 | { | 1698 | { |
1731 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) | 1699 | if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)) |
@@ -1767,10 +1735,14 @@ GNUNET_DISK_file_open (const char *fn, | |||
1767 | disp = OPEN_EXISTING; | 1735 | disp = OPEN_EXISTING; |
1768 | } | 1736 | } |
1769 | 1737 | ||
1770 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv(expfn, wexpfn)) | 1738 | if (ERROR_SUCCESS == plibc_conv_to_win_pathwconv (expfn, wexpfn)) |
1771 | h = CreateFileW (wexpfn, access, | 1739 | h = CreateFileW (wexpfn, |
1772 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 1740 | access, |
1773 | disp, FILE_ATTRIBUTE_NORMAL, NULL); | 1741 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, |
1742 | NULL, | ||
1743 | disp, | ||
1744 | FILE_ATTRIBUTE_NORMAL, | ||
1745 | NULL); | ||
1774 | else | 1746 | else |
1775 | h = INVALID_HANDLE_VALUE; | 1747 | h = INVALID_HANDLE_VALUE; |
1776 | if (h == INVALID_HANDLE_VALUE) | 1748 | if (h == INVALID_HANDLE_VALUE) |
@@ -1844,7 +1816,7 @@ GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h) | |||
1844 | } | 1816 | } |
1845 | if (h->oOverlapWrite) | 1817 | if (h->oOverlapWrite) |
1846 | { | 1818 | { |
1847 | if (!CloseHandle (h->oOverlapWrite->hEvent)) | 1819 | if (! CloseHandle (h->oOverlapWrite->hEvent)) |
1848 | { | 1820 | { |
1849 | SetErrnoFromWinError (GetLastError ()); | 1821 | SetErrnoFromWinError (GetLastError ()); |
1850 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); | 1822 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "close"); |
@@ -1888,8 +1860,8 @@ GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh) | |||
1888 | ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; | 1860 | ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; |
1889 | break; | 1861 | break; |
1890 | case FILE_TYPE_UNKNOWN: | 1862 | case FILE_TYPE_UNKNOWN: |
1891 | if ( (GetLastError () == NO_ERROR) || | 1863 | if ((GetLastError () == NO_ERROR) || |
1892 | (GetLastError () == ERROR_INVALID_HANDLE) ) | 1864 | (GetLastError () == ERROR_INVALID_HANDLE)) |
1893 | { | 1865 | { |
1894 | if (0 != ResetEvent (osfh)) | 1866 | if (0 != ResetEvent (osfh)) |
1895 | ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; | 1867 | ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; |
@@ -1938,8 +1910,7 @@ GNUNET_DISK_get_handle_from_int_fd (int fno) | |||
1938 | { | 1910 | { |
1939 | struct GNUNET_DISK_FileHandle *fh; | 1911 | struct GNUNET_DISK_FileHandle *fh; |
1940 | 1912 | ||
1941 | if ( (((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && | 1913 | if ((((off_t) -1) == lseek (fno, 0, SEEK_CUR)) && (EBADF == errno)) |
1942 | (EBADF == errno) ) | ||
1943 | return NULL; /* invalid FD */ | 1914 | return NULL; /* invalid FD */ |
1944 | 1915 | ||
1945 | #ifndef WINDOWS | 1916 | #ifndef WINDOWS |
@@ -2019,7 +1990,8 @@ struct GNUNET_DISK_MapHandle | |||
2019 | void * | 1990 | void * |
2020 | GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | 1991 | GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, |
2021 | struct GNUNET_DISK_MapHandle **m, | 1992 | struct GNUNET_DISK_MapHandle **m, |
2022 | enum GNUNET_DISK_MapType access, size_t len) | 1993 | enum GNUNET_DISK_MapType access, |
1994 | size_t len) | ||
2023 | { | 1995 | { |
2024 | if (NULL == h) | 1996 | if (NULL == h) |
2025 | { | 1997 | { |
@@ -2062,7 +2034,7 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, | |||
2062 | } | 2034 | } |
2063 | 2035 | ||
2064 | (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); | 2036 | (*m)->addr = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len); |
2065 | if (!(*m)->addr) | 2037 | if (! (*m)->addr) |
2066 | { | 2038 | { |
2067 | SetErrnoFromWinError (GetLastError ()); | 2039 | SetErrnoFromWinError (GetLastError ()); |
2068 | CloseHandle ((*m)->h); | 2040 | CloseHandle ((*m)->h); |
@@ -2111,7 +2083,7 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h) | |||
2111 | ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; | 2083 | ret = UnmapViewOfFile (h->addr) ? GNUNET_OK : GNUNET_SYSERR; |
2112 | if (ret != GNUNET_OK) | 2084 | if (ret != GNUNET_OK) |
2113 | SetErrnoFromWinError (GetLastError ()); | 2085 | SetErrnoFromWinError (GetLastError ()); |
2114 | if (!CloseHandle (h->h) && (ret == GNUNET_OK)) | 2086 | if (! CloseHandle (h->h) && (ret == GNUNET_OK)) |
2115 | { | 2087 | { |
2116 | ret = GNUNET_SYSERR; | 2088 | ret = GNUNET_SYSERR; |
2117 | SetErrnoFromWinError (GetLastError ()); | 2089 | SetErrnoFromWinError (GetLastError ()); |
@@ -2155,7 +2127,7 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | |||
2155 | 2127 | ||
2156 | #if WINDOWS | 2128 | #if WINDOWS |
2157 | #ifndef PIPE_BUF | 2129 | #ifndef PIPE_BUF |
2158 | #define PIPE_BUF 512 | 2130 | #define PIPE_BUF 512 |
2159 | #endif | 2131 | #endif |
2160 | /* Copyright Bob Byrnes <byrnes <at> curl.com> | 2132 | /* Copyright Bob Byrnes <byrnes <at> curl.com> |
2161 | http://permalink.gmane.org/gmane.os.cygwin.patches/2121 | 2133 | http://permalink.gmane.org/gmane.os.cygwin.patches/2121 |
@@ -2168,9 +2140,12 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | |||
2168 | Note that the return value is either NO_ERROR or GetLastError, | 2140 | Note that the return value is either NO_ERROR or GetLastError, |
2169 | unlike CreatePipe, which returns a bool for success or failure. */ | 2141 | unlike CreatePipe, which returns a bool for success or failure. */ |
2170 | static int | 2142 | static int |
2171 | create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | 2143 | create_selectable_pipe (PHANDLE read_pipe_ptr, |
2172 | LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize, | 2144 | PHANDLE write_pipe_ptr, |
2173 | DWORD dwReadMode, DWORD dwWriteMode) | 2145 | LPSECURITY_ATTRIBUTES sa_ptr, |
2146 | DWORD psize, | ||
2147 | DWORD dwReadMode, | ||
2148 | DWORD dwWriteMode) | ||
2174 | { | 2149 | { |
2175 | /* Default to error. */ | 2150 | /* Default to error. */ |
2176 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; | 2151 | *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE; |
@@ -2191,10 +2166,15 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | |||
2191 | { | 2166 | { |
2192 | static volatile LONG pipe_unique_id; | 2167 | static volatile LONG pipe_unique_id; |
2193 | 2168 | ||
2194 | snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld", | 2169 | snprintf (pipename, |
2195 | getpid (), InterlockedIncrement ((LONG *) & pipe_unique_id)); | 2170 | sizeof pipename, |
2196 | LOG (GNUNET_ERROR_TYPE_DEBUG, "CreateNamedPipe: name = %s, size = %lu\n", | 2171 | "\\\\.\\pipe\\gnunet-%d-%ld", |
2197 | pipename, psize); | 2172 | getpid (), |
2173 | InterlockedIncrement ((LONG *) &pipe_unique_id)); | ||
2174 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2175 | "CreateNamedPipe: name = %s, size = %lu\n", | ||
2176 | pipename, | ||
2177 | psize); | ||
2198 | /* Use CreateNamedPipe instead of CreatePipe, because the latter | 2178 | /* Use CreateNamedPipe instead of CreatePipe, because the latter |
2199 | * returns a write handle that does not permit FILE_READ_ATTRIBUTES | 2179 | * returns a write handle that does not permit FILE_READ_ATTRIBUTES |
2200 | * access, on versions of win32 earlier than WinXP SP2. | 2180 | * access, on versions of win32 earlier than WinXP SP2. |
@@ -2203,10 +2183,15 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | |||
2203 | * It's important to only allow a single instance, to ensure that | 2183 | * It's important to only allow a single instance, to ensure that |
2204 | * the pipe was not created earlier by some other process, even if | 2184 | * the pipe was not created earlier by some other process, even if |
2205 | * the pid has been reused. */ | 2185 | * the pid has been reused. */ |
2206 | read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, /* max instances */ | 2186 | read_pipe = CreateNamedPipeA (pipename, |
2207 | psize, /* output buffer size */ | 2187 | PIPE_ACCESS_INBOUND | |
2208 | psize, /* input buffer size */ | 2188 | FILE_FLAG_FIRST_PIPE_INSTANCE | dwReadMode, |
2209 | NMPWAIT_USE_DEFAULT_WAIT, sa_ptr); | 2189 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
2190 | 1, /* max instances */ | ||
2191 | psize, /* output buffer size */ | ||
2192 | psize, /* input buffer size */ | ||
2193 | NMPWAIT_USE_DEFAULT_WAIT, | ||
2194 | sa_ptr); | ||
2210 | 2195 | ||
2211 | if (read_pipe != INVALID_HANDLE_VALUE) | 2196 | if (read_pipe != INVALID_HANDLE_VALUE) |
2212 | { | 2197 | { |
@@ -2233,10 +2218,12 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | |||
2233 | * Return an anonymous pipe as the best approximation. */ | 2218 | * Return an anonymous pipe as the best approximation. */ |
2234 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 2219 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
2235 | "CreateNamedPipe not implemented, resorting to " | 2220 | "CreateNamedPipe not implemented, resorting to " |
2236 | "CreatePipe: size = %lu\n", psize); | 2221 | "CreatePipe: size = %lu\n", |
2222 | psize); | ||
2237 | if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) | 2223 | if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize)) |
2238 | { | 2224 | { |
2239 | LOG (GNUNET_ERROR_TYPE_DEBUG, "pipe read handle = %p, write handle = %p\n", | 2225 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
2226 | "pipe read handle = %p, write handle = %p\n", | ||
2240 | *read_pipe_ptr, | 2227 | *read_pipe_ptr, |
2241 | *write_pipe_ptr); | 2228 | *write_pipe_ptr); |
2242 | return GNUNET_OK; | 2229 | return GNUNET_OK; |
@@ -2254,8 +2241,12 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | |||
2254 | 2241 | ||
2255 | /* Open the named pipe for writing. | 2242 | /* Open the named pipe for writing. |
2256 | * Be sure to permit FILE_READ_ATTRIBUTES access. */ | 2243 | * Be sure to permit FILE_READ_ATTRIBUTES access. */ |
2257 | write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, /* share mode */ | 2244 | write_pipe = CreateFileA (pipename, |
2258 | sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */ | 2245 | GENERIC_WRITE | FILE_READ_ATTRIBUTES, |
2246 | 0, /* share mode */ | ||
2247 | sa_ptr, | ||
2248 | OPEN_EXISTING, | ||
2249 | dwWriteMode, /* flags and attributes */ | ||
2259 | 0); /* handle to template file */ | 2250 | 0); /* handle to template file */ |
2260 | 2251 | ||
2261 | if (write_pipe == INVALID_HANDLE_VALUE) | 2252 | if (write_pipe == INVALID_HANDLE_VALUE) |
@@ -2287,9 +2278,9 @@ create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr, | |||
2287 | */ | 2278 | */ |
2288 | struct GNUNET_DISK_PipeHandle * | 2279 | struct GNUNET_DISK_PipeHandle * |
2289 | GNUNET_DISK_pipe (int blocking_read, | 2280 | GNUNET_DISK_pipe (int blocking_read, |
2290 | int blocking_write, | 2281 | int blocking_write, |
2291 | int inherit_read, | 2282 | int inherit_read, |
2292 | int inherit_write) | 2283 | int inherit_write) |
2293 | { | 2284 | { |
2294 | #ifndef MINGW | 2285 | #ifndef MINGW |
2295 | int fd[2]; | 2286 | int fd[2]; |
@@ -2302,14 +2293,11 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2302 | if (ret == -1) | 2293 | if (ret == -1) |
2303 | { | 2294 | { |
2304 | eno = errno; | 2295 | eno = errno; |
2305 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, | 2296 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe"); |
2306 | "pipe"); | ||
2307 | errno = eno; | 2297 | errno = eno; |
2308 | return NULL; | 2298 | return NULL; |
2309 | } | 2299 | } |
2310 | return GNUNET_DISK_pipe_from_fd (blocking_read, | 2300 | return GNUNET_DISK_pipe_from_fd (blocking_read, blocking_write, fd); |
2311 | blocking_write, | ||
2312 | fd); | ||
2313 | #else | 2301 | #else |
2314 | struct GNUNET_DISK_PipeHandle *p; | 2302 | struct GNUNET_DISK_PipeHandle *p; |
2315 | BOOL ret; | 2303 | BOOL ret; |
@@ -2329,11 +2317,13 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2329 | * Pipes are not seekable, and need no offsets, which is | 2317 | * Pipes are not seekable, and need no offsets, which is |
2330 | * probably why it works for them. | 2318 | * probably why it works for them. |
2331 | */ | 2319 | */ |
2332 | ret = | 2320 | ret = create_selectable_pipe (&p->fd[0]->h, |
2333 | create_selectable_pipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0, | 2321 | &p->fd[1]->h, |
2334 | FILE_FLAG_OVERLAPPED, | 2322 | NULL, |
2335 | FILE_FLAG_OVERLAPPED); | 2323 | 0, |
2336 | if (!ret) | 2324 | FILE_FLAG_OVERLAPPED, |
2325 | FILE_FLAG_OVERLAPPED); | ||
2326 | if (! ret) | ||
2337 | { | 2327 | { |
2338 | SetErrnoFromWinError (GetLastError ()); | 2328 | SetErrnoFromWinError (GetLastError ()); |
2339 | save_errno = errno; | 2329 | save_errno = errno; |
@@ -2343,9 +2333,13 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2343 | errno = save_errno; | 2333 | errno = save_errno; |
2344 | return NULL; | 2334 | return NULL; |
2345 | } | 2335 | } |
2346 | if (!DuplicateHandle | 2336 | if (! DuplicateHandle (GetCurrentProcess (), |
2347 | (GetCurrentProcess (), p->fd[0]->h, GetCurrentProcess (), &tmp_handle, 0, | 2337 | p->fd[0]->h, |
2348 | inherit_read == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) | 2338 | GetCurrentProcess (), |
2339 | &tmp_handle, | ||
2340 | 0, | ||
2341 | inherit_read == GNUNET_YES ? TRUE : FALSE, | ||
2342 | DUPLICATE_SAME_ACCESS)) | ||
2349 | { | 2343 | { |
2350 | SetErrnoFromWinError (GetLastError ()); | 2344 | SetErrnoFromWinError (GetLastError ()); |
2351 | save_errno = errno; | 2345 | save_errno = errno; |
@@ -2360,9 +2354,13 @@ GNUNET_DISK_pipe (int blocking_read, | |||
2360 | CloseHandle (p->fd[0]->h); | 2354 | CloseHandle (p->fd[0]->h); |
2361 | p->fd[0]->h = tmp_handle; | 2355 | p->fd[0]->h = tmp_handle; |
2362 | 2356 | ||
2363 | if (!DuplicateHandle | 2357 | if (! DuplicateHandle (GetCurrentProcess (), |
2364 | (GetCurrentProcess (), p->fd[1]->h, GetCurrentProcess (), &tmp_handle, 0, | 2358 | p->fd[1]->h, |
2365 | inherit_write == GNUNET_YES ? TRUE : FALSE, DUPLICATE_SAME_ACCESS)) | 2359 | GetCurrentProcess (), |
2360 | &tmp_handle, | ||
2361 | 0, | ||
2362 | inherit_write == GNUNET_YES ? TRUE : FALSE, | ||
2363 | DUPLICATE_SAME_ACCESS)) | ||
2366 | { | 2364 | { |
2367 | SetErrnoFromWinError (GetLastError ()); | 2365 | SetErrnoFromWinError (GetLastError ()); |
2368 | save_errno = errno; | 2366 | save_errno = errno; |
@@ -2423,14 +2421,14 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]) | |||
2423 | { | 2421 | { |
2424 | p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); | 2422 | p->fd[0] = GNUNET_new (struct GNUNET_DISK_FileHandle); |
2425 | p->fd[0]->fd = fd[0]; | 2423 | p->fd[0]->fd = fd[0]; |
2426 | if (!blocking_read) | 2424 | if (! blocking_read) |
2427 | { | 2425 | { |
2428 | flags = fcntl (fd[0], F_GETFL); | 2426 | flags = fcntl (fd[0], F_GETFL); |
2429 | flags |= O_NONBLOCK; | 2427 | flags |= O_NONBLOCK; |
2430 | if (0 > fcntl (fd[0], F_SETFL, flags)) | 2428 | if (0 > fcntl (fd[0], F_SETFL, flags)) |
2431 | { | 2429 | { |
2432 | ret = -1; | 2430 | ret = -1; |
2433 | eno = errno; | 2431 | eno = errno; |
2434 | } | 2432 | } |
2435 | } | 2433 | } |
2436 | flags = fcntl (fd[0], F_GETFD); | 2434 | flags = fcntl (fd[0], F_GETFD); |
@@ -2446,14 +2444,14 @@ GNUNET_DISK_pipe_from_fd (int blocking_read, int blocking_write, int fd[2]) | |||
2446 | { | 2444 | { |
2447 | p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); | 2445 | p->fd[1] = GNUNET_new (struct GNUNET_DISK_FileHandle); |
2448 | p->fd[1]->fd = fd[1]; | 2446 | p->fd[1]->fd = fd[1]; |
2449 | if (!blocking_write) | 2447 | if (! blocking_write) |
2450 | { | 2448 | { |
2451 | flags = fcntl (fd[1], F_GETFL); | 2449 | flags = fcntl (fd[1], F_GETFL); |
2452 | flags |= O_NONBLOCK; | 2450 | flags |= O_NONBLOCK; |
2453 | if (0 > fcntl (fd[1], F_SETFL, flags)) | 2451 | if (0 > fcntl (fd[1], F_SETFL, flags)) |
2454 | { | 2452 | { |
2455 | ret = -1; | 2453 | ret = -1; |
2456 | eno = errno; | 2454 | eno = errno; |
2457 | } | 2455 | } |
2458 | } | 2456 | } |
2459 | flags = fcntl (fd[1], F_GETFD); | 2457 | flags = fcntl (fd[1], F_GETFD); |
@@ -2663,7 +2661,8 @@ GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, | |||
2663 | */ | 2661 | */ |
2664 | int | 2662 | int |
2665 | GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, | 2663 | GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, |
2666 | void *dst, size_t dst_len) | 2664 | void *dst, |
2665 | size_t dst_len) | ||
2667 | { | 2666 | { |
2668 | if (NULL == fh) | 2667 | if (NULL == fh) |
2669 | return GNUNET_SYSERR; | 2668 | return GNUNET_SYSERR; |
@@ -2689,29 +2688,20 @@ GNUNET_DISK_internal_file_handle_ (const struct GNUNET_DISK_FileHandle *fh, | |||
2689 | * @return #GNUNET_OK on success | 2688 | * @return #GNUNET_OK on success |
2690 | */ | 2689 | */ |
2691 | static int | 2690 | static int |
2692 | purge_cfg_dir (void *cls, | 2691 | purge_cfg_dir (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) |
2693 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
2694 | { | 2692 | { |
2695 | const char *option = cls; | 2693 | const char *option = cls; |
2696 | char *tmpname; | 2694 | char *tmpname; |
2697 | 2695 | ||
2698 | if (GNUNET_OK != | 2696 | if (GNUNET_OK != |
2699 | GNUNET_CONFIGURATION_get_value_filename (cfg, | 2697 | GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", option, &tmpname)) |
2700 | "PATHS", | 2698 | { |
2701 | option, | 2699 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PATHS", option); |
2702 | &tmpname)) | ||
2703 | { | ||
2704 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
2705 | "PATHS", | ||
2706 | option); | ||
2707 | return GNUNET_NO; | 2700 | return GNUNET_NO; |
2708 | } | 2701 | } |
2709 | if (GNUNET_SYSERR == | 2702 | if (GNUNET_SYSERR == GNUNET_DISK_directory_remove (tmpname)) |
2710 | GNUNET_DISK_directory_remove (tmpname)) | ||
2711 | { | 2703 | { |
2712 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | 2704 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "remove", tmpname); |
2713 | "remove", | ||
2714 | tmpname); | ||
2715 | GNUNET_free (tmpname); | 2705 | GNUNET_free (tmpname); |
2716 | return GNUNET_OK; | 2706 | return GNUNET_OK; |
2717 | } | 2707 | } |
@@ -2728,13 +2718,12 @@ purge_cfg_dir (void *cls, | |||
2728 | * @param option option with the dir name to purge | 2718 | * @param option option with the dir name to purge |
2729 | */ | 2719 | */ |
2730 | void | 2720 | void |
2731 | GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, | 2721 | GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, const char *option) |
2732 | const char *option) | ||
2733 | { | 2722 | { |
2734 | GNUNET_break (GNUNET_OK == | 2723 | GNUNET_break (GNUNET_OK == |
2735 | GNUNET_CONFIGURATION_parse_and_run (cfg_filename, | 2724 | GNUNET_CONFIGURATION_parse_and_run (cfg_filename, |
2736 | &purge_cfg_dir, | 2725 | &purge_cfg_dir, |
2737 | (void *) option)); | 2726 | (void *) option)); |
2738 | } | 2727 | } |
2739 | 2728 | ||
2740 | 2729 | ||