]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
safe_io: move to C
authorSage Weil <sage.weil@dreamhost.com>
Mon, 7 Feb 2011 20:12:51 +0000 (12:12 -0800)
committerSage Weil <sage.weil@dreamhost.com>
Mon, 7 Feb 2011 20:12:51 +0000 (12:12 -0800)
Fixes unistd vs XOPEN_SOURCE define weirdness.

Signed-off-by: Sage Weil <sage.weil@dreamhost.com>
src/Makefile.am
src/common/safe_io.c [new file with mode: 0644]
src/common/safe_io.h

index 37f4235e87c96d08d9904591b8450ae6eb68492b..a1050db94647fd8277c801d38deeaf2476f06c38 100644 (file)
@@ -484,6 +484,7 @@ libcommon_files = \
        common/ConfUtils.cc \
        common/MemoryModel.cc \
        common/armor.c \
+       common/safe_io.c \
        common/str_list.cc \
        common/errno.cc \
        mon/MonMap.cc \
diff --git a/src/common/safe_io.c b/src/common/safe_io.c
new file mode 100644 (file)
index 0000000..455265e
--- /dev/null
@@ -0,0 +1,75 @@
+
+#define _XOPEN_SOURCE 500
+
+#include <unistd.h>
+#include <errno.h>
+
+ssize_t safe_read(int fd, void *buf, size_t count)
+{
+       int r;
+
+       while (count > 0) {
+               r = read(fd, buf, count);
+               if (r < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -errno;
+               }
+               count -= r;
+               buf = (char *)buf + r;
+       }
+       return 0;
+}
+ssize_t safe_write(int fd, const void *buf, size_t count)
+{
+       int r;
+
+       while (count > 0) {
+               r = write(fd, buf, count);
+               if (r < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -errno;
+               }
+               count -= r;
+               buf = (char *)buf + r;
+       }
+       return 0;
+}
+
+ssize_t safe_pread(int fd, void *buf, size_t count, off_t offset)
+{
+       int r;
+
+       while (count > 0) {
+               r = pread(fd, buf, count, offset);
+               if (r < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -errno;
+               }
+               count -= r;
+               buf = (char *)buf + r;
+               offset += r;
+       }
+       return 0;
+}
+
+ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset)
+{
+       int r;
+
+       while (count > 0) {
+               r = pwrite(fd, buf, count, offset);
+               if (r < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -errno;
+               }
+               count -= r;
+               buf = (char *)buf + r;
+               offset += r;
+       }
+       return 0;
+}
index f24d4175f2f9eaee7a17ed77e62ea12397cd86c9..30856ff29761ddad6f204f8c053bc80e7f7bf22d 100644 (file)
@@ -4,84 +4,14 @@
 /*
  * - mask EINTR
  * - mask short reads/writes
+ * - on error return -errno (instead of returning -1 and setting errno)
  */
 
-#ifndef _XOPEN_SOURCE
-#define _XOPEN_SOURCE 500
-#endif
-
-#include <unistd.h>
-#include <errno.h>
-
-
-static inline ssize_t safe_read(int fd, void *buf, size_t count)
-{
-       int r;
-
-       while (count > 0) {
-               r = read(fd, buf, count);
-               if (r < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       return -errno;
-               }
-               count -= r;
-               buf = (char *)buf + r;
-       }
-       return 0;
-}
-static inline ssize_t safe_write(int fd, const void *buf, size_t count)
-{
-       int r;
-
-       while (count > 0) {
-               r = write(fd, buf, count);
-               if (r < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       return -errno;
-               }
-               count -= r;
-               buf = (char *)buf + r;
-       }
-       return 0;
-}
-
-static inline ssize_t safe_pread(int fd, void *buf, size_t count, off_t offset)
-{
-       int r;
-
-       while (count > 0) {
-               r = pread(fd, buf, count, offset);
-               if (r < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       return -errno;
-               }
-               count -= r;
-               buf = (char *)buf + r;
-               offset += r;
-       }
-       return 0;
-}
-
-static inline ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset)
-{
-       int r;
-
-       while (count > 0) {
-               r = pwrite(fd, buf, count, offset);
-               if (r < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       return -errno;
-               }
-               count -= r;
-               buf = (char *)buf + r;
-               offset += r;
-       }
-       return 0;
+extern "C" {
+  ssize_t safe_read(int fd, void *buf, size_t count);
+  ssize_t safe_write(int fd, const void *buf, size_t count);
+  ssize_t safe_pread(int fd, void *buf, size_t count, off_t offset);
+  ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset);
 }
 
 #endif