diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-14 23:26:53 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-14 23:26:53 +0000 |
commit | 72724deec7f8d42fb33d02034bd24cd9e95eac7a (patch) | |
tree | 07cc1988a6305bc00f7fced6802ef39b02d3d0d2 /src | |
parent | 2e9b3b16b6e8471da0f8f816490dba47d87c646d (diff) | |
download | gnunet-72724deec7f8d42fb33d02034bd24cd9e95eac7a.tar.gz gnunet-72724deec7f8d42fb33d02034bd24cd9e95eac7a.zip |
-implement blocking write / non-blocking read for non-W32 systems
Diffstat (limited to 'src')
-rw-r--r-- | src/util/disk.c | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/util/disk.c b/src/util/disk.c index a5190d587..1454a71b1 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -115,7 +115,7 @@ struct GetFileSizeData | |||
115 | }; | 115 | }; |
116 | 116 | ||
117 | 117 | ||
118 | int | 118 | static int |
119 | translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm) | 119 | translate_unix_perms (enum GNUNET_DISK_AccessPermissions perm) |
120 | { | 120 | { |
121 | int mode; | 121 | int mode; |
@@ -420,6 +420,7 @@ GNUNET_DISK_mktemp (const char *t) | |||
420 | fn = tmpl; | 420 | fn = tmpl; |
421 | #endif | 421 | #endif |
422 | /* FIXME: why is this not MKSTEMP()? This function is implemented in plibc. | 422 | /* FIXME: why is this not MKSTEMP()? This function is implemented in plibc. |
423 | * CG: really? If I put MKSTEMP here, I get a compilation error... | ||
423 | * It will assume that fn is UTF-8-encoded, if compiled with UTF-8 support. | 424 | * It will assume that fn is UTF-8-encoded, if compiled with UTF-8 support. |
424 | */ | 425 | */ |
425 | fd = mkstemp (fn); | 426 | fd = mkstemp (fn); |
@@ -531,6 +532,7 @@ GNUNET_DISK_directory_test (const char *fil) | |||
531 | return GNUNET_YES; | 532 | return GNUNET_YES; |
532 | } | 533 | } |
533 | 534 | ||
535 | |||
534 | /** | 536 | /** |
535 | * Check that fil corresponds to a filename | 537 | * Check that fil corresponds to a filename |
536 | * (of a file that exists and that is not a directory). | 538 | * (of a file that exists and that is not a directory). |
@@ -746,6 +748,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, | |||
746 | #endif | 748 | #endif |
747 | } | 749 | } |
748 | 750 | ||
751 | |||
749 | /** | 752 | /** |
750 | * Read the contents of a binary file into a buffer. | 753 | * Read the contents of a binary file into a buffer. |
751 | * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN | 754 | * Guarantees not to block (returns GNUNET_SYSERR and sets errno to EAGAIN |
@@ -809,8 +812,17 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle * h, | |||
809 | } | 812 | } |
810 | return bytesRead; | 813 | return bytesRead; |
811 | #else | 814 | #else |
812 | /* FIXME: set to non-blocking (fcntl?), read, then set back? */ | 815 | int flags; |
813 | return read (h->fd, result, len); | 816 | ssize_t ret; |
817 | |||
818 | /* set to non-blocking, read, then set back */ | ||
819 | flags = fcntl (h->fd, F_GETFL); | ||
820 | if (0 == (flags & O_NONBLOCK)) | ||
821 | fcntl (h->fd, F_SETFL, flags | O_NONBLOCK); | ||
822 | ret = read (h->fd, result, len); | ||
823 | if (0 == (flags & O_NONBLOCK)) | ||
824 | fcntl (h->fd, F_SETFL, flags); | ||
825 | return ret; | ||
814 | #endif | 826 | #endif |
815 | } | 827 | } |
816 | 828 | ||
@@ -938,6 +950,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, | |||
938 | #endif | 950 | #endif |
939 | } | 951 | } |
940 | 952 | ||
953 | |||
941 | /** | 954 | /** |
942 | * Write a buffer to a file, blocking, if necessary. | 955 | * Write a buffer to a file, blocking, if necessary. |
943 | * @param h handle to open file | 956 | * @param h handle to open file |
@@ -991,11 +1004,21 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle * h, | |||
991 | #endif | 1004 | #endif |
992 | return bytesWritten; | 1005 | return bytesWritten; |
993 | #else | 1006 | #else |
994 | /* FIXME: switch to blocking mode (fcntl?), write, then switch back? */ | 1007 | int flags; |
995 | return write (h->fd, buffer, n); | 1008 | ssize_t ret; |
1009 | |||
1010 | /* set to blocking, write, then set back */ | ||
1011 | flags = fcntl (h->fd, F_GETFL); | ||
1012 | if (0 != (flags & O_NONBLOCK)) | ||
1013 | fcntl (h->fd, F_SETFL, flags - O_NONBLOCK); | ||
1014 | ret = write (h->fd, buffer, n); | ||
1015 | if (0 == (flags & O_NONBLOCK)) | ||
1016 | fcntl (h->fd, F_SETFL, flags); | ||
1017 | return ret; | ||
996 | #endif | 1018 | #endif |
997 | } | 1019 | } |
998 | 1020 | ||
1021 | |||
999 | /** | 1022 | /** |
1000 | * Write a buffer to a file. If the file is longer than the | 1023 | * Write a buffer to a file. If the file is longer than the |
1001 | * number of bytes that will be written, it will be truncated. | 1024 | * number of bytes that will be written, it will be truncated. |