Re: [devel] [PATCH 1/2] base: Add utility function osaf_fdatasync for syncing to disk [#2451]

2017-10-24 Thread Hans Nordebäck

ack, code review only. Minor comments below/Thanks HansN


On 10/23/2017 03:38 PM, Anders Widell wrote:

---
  src/base/Makefile.am|  3 +++
  src/base/osaf_unistd.cc | 62 +
  src/base/osaf_unistd.h  | 51 
  3 files changed, 116 insertions(+)
  create mode 100644 src/base/osaf_unistd.cc
  create mode 100644 src/base/osaf_unistd.h

diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index d0d67bbf0..be6f757c7 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -54,11 +54,13 @@ lib_libopensaf_core_la_SOURCES += \
src/base/ncsdlib.c \
src/base/os_defs.c \
src/base/osaf_extended_name.c \
+   src/base/osaf_gcov.c \
src/base/osaf_poll.c \
src/base/osaf_secutil.c \
src/base/osaf_time.c \
src/base/osaf_timerfd.c \
src/base/osaf_unicode.c \
+   src/base/osaf_unistd.cc \
src/base/osaf_utility.c \
src/base/patricia.c \
src/base/process.cc \
@@ -132,6 +134,7 @@ noinst_HEADERS += \
src/base/osaf_time.h \
src/base/osaf_timerfd.h \
src/base/osaf_unicode.h \
+   src/base/osaf_unistd.h \
src/base/osaf_utility.h \
src/base/process.h \
src/base/saf_def.h \
diff --git a/src/base/osaf_unistd.cc b/src/base/osaf_unistd.cc
new file mode 100644
index 0..a08b62ed2
--- /dev/null
+++ b/src/base/osaf_unistd.cc
@@ -0,0 +1,62 @@
+/*  -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2017 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+#include "base/osaf_unistd.h"
+#include 
+#include 
+#include 
+#include 
+#include "base/osaf_utility.h"
+

[HansN] perhaps use const std::string & ?

+int osaf_fdatasync(const char *pathname) {
+  int fd;
+
+  for (;;) {
+fd = open(pathname, O_RDONLY);
+if (fd >= 0) break;
+int e = errno;
+
+if (e == EFAULT) osaf_abort(e);
+if (e != EINTR) return -1;
+  }
+  for (;;) {
+if (fdatasync(fd) >= 0) break;
+int e = errno;
+
+if (e == EBADF) osaf_abort(e);
+if (e != EINTR) {
+  close(fd);
+  errno = e;
+  return -1;
+}
+  }
+  int result = close(fd);
+
+  if (result < 0 && errno == EINTR) result = 0;
+  return result;
+}
+
+int osaf_fdatasync(int fd) {
+  for (;;) {
+if (fdatasync(fd) >= 0) break;
+int e = errno;
+
+if (e == EBADF) osaf_abort(e);
+if (e != EINTR) {
+  return -1;
+}
+  }
+  return 0;
+}
diff --git a/src/base/osaf_unistd.h b/src/base/osaf_unistd.h
new file mode 100644
index 0..734548df4
--- /dev/null
+++ b/src/base/osaf_unistd.h
@@ -0,0 +1,51 @@
+/*  -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2017 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+/** @file
+ *
+ * This file contains convenience functions similar to the ones declared in the
+ * system header file unistd.h
+ */
+
+#ifndef BASE_OSAF_UNISTD_H_
+#define BASE_OSAF_UNISTD_H_
+
+/**
+ * @brief Sync a file or a directory to disk.
+ *
+ * This is a convenience function that calls open(2) and fdatasync(2) to sync
+ * the file or the directory specified by @a pathname. It handles EINTR
+ * internally with a loop. For other errors, this function will return -1 and
+ * set errno apropriately.

[HansN] or osaf_abort at other faults ?

+ *
+ * Please be aware that in order to sync a file that has been written, you
+ * probably also need to sync the directory containing the file.
+ */
+int osaf_fdatasync(const char* pathname);
+
+/**
+ * @brief Sync a file or a directory to disk.
+ *
+ * This is a convenience function that calls fdatasync(2) to sync the file or
+ * the directory specified by the open file descriptor @a fd. It handles EINTR
+ * internally with a loop. For other errors, this function will return -1 and
+ * set errno apropriately.

[HansN] or osaf_abort at other faults ?

+ *
+ * Please be aware that in order to sync a file that has been written, you
+ * probably also need to sync 

[devel] [PATCH 1/2] base: Add utility function osaf_fdatasync for syncing to disk [#2451]

2017-10-23 Thread Anders Widell
---
 src/base/Makefile.am|  3 +++
 src/base/osaf_unistd.cc | 62 +
 src/base/osaf_unistd.h  | 51 
 3 files changed, 116 insertions(+)
 create mode 100644 src/base/osaf_unistd.cc
 create mode 100644 src/base/osaf_unistd.h

diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index d0d67bbf0..be6f757c7 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -54,11 +54,13 @@ lib_libopensaf_core_la_SOURCES += \
src/base/ncsdlib.c \
src/base/os_defs.c \
src/base/osaf_extended_name.c \
+   src/base/osaf_gcov.c \
src/base/osaf_poll.c \
src/base/osaf_secutil.c \
src/base/osaf_time.c \
src/base/osaf_timerfd.c \
src/base/osaf_unicode.c \
+   src/base/osaf_unistd.cc \
src/base/osaf_utility.c \
src/base/patricia.c \
src/base/process.cc \
@@ -132,6 +134,7 @@ noinst_HEADERS += \
src/base/osaf_time.h \
src/base/osaf_timerfd.h \
src/base/osaf_unicode.h \
+   src/base/osaf_unistd.h \
src/base/osaf_utility.h \
src/base/process.h \
src/base/saf_def.h \
diff --git a/src/base/osaf_unistd.cc b/src/base/osaf_unistd.cc
new file mode 100644
index 0..a08b62ed2
--- /dev/null
+++ b/src/base/osaf_unistd.cc
@@ -0,0 +1,62 @@
+/*  -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2017 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+#include "base/osaf_unistd.h"
+#include 
+#include 
+#include 
+#include 
+#include "base/osaf_utility.h"
+
+int osaf_fdatasync(const char *pathname) {
+  int fd;
+
+  for (;;) {
+fd = open(pathname, O_RDONLY);
+if (fd >= 0) break;
+int e = errno;
+
+if (e == EFAULT) osaf_abort(e);
+if (e != EINTR) return -1;
+  }
+  for (;;) {
+if (fdatasync(fd) >= 0) break;
+int e = errno;
+
+if (e == EBADF) osaf_abort(e);
+if (e != EINTR) {
+  close(fd);
+  errno = e;
+  return -1;
+}
+  }
+  int result = close(fd);
+
+  if (result < 0 && errno == EINTR) result = 0;
+  return result;
+}
+
+int osaf_fdatasync(int fd) {
+  for (;;) {
+if (fdatasync(fd) >= 0) break;
+int e = errno;
+
+if (e == EBADF) osaf_abort(e);
+if (e != EINTR) {
+  return -1;
+}
+  }
+  return 0;
+}
diff --git a/src/base/osaf_unistd.h b/src/base/osaf_unistd.h
new file mode 100644
index 0..734548df4
--- /dev/null
+++ b/src/base/osaf_unistd.h
@@ -0,0 +1,51 @@
+/*  -*- OpenSAF  -*-
+ *
+ * Copyright Ericsson AB 2017 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+/** @file
+ *
+ * This file contains convenience functions similar to the ones declared in the
+ * system header file unistd.h
+ */
+
+#ifndef BASE_OSAF_UNISTD_H_
+#define BASE_OSAF_UNISTD_H_
+
+/**
+ * @brief Sync a file or a directory to disk.
+ *
+ * This is a convenience function that calls open(2) and fdatasync(2) to sync
+ * the file or the directory specified by @a pathname. It handles EINTR
+ * internally with a loop. For other errors, this function will return -1 and
+ * set errno apropriately.
+ *
+ * Please be aware that in order to sync a file that has been written, you
+ * probably also need to sync the directory containing the file.
+ */
+int osaf_fdatasync(const char* pathname);
+
+/**
+ * @brief Sync a file or a directory to disk.
+ *
+ * This is a convenience function that calls fdatasync(2) to sync the file or
+ * the directory specified by the open file descriptor @a fd. It handles EINTR
+ * internally with a loop. For other errors, this function will return -1 and
+ * set errno apropriately.
+ *
+ * Please be aware that in order to sync a file that has been written, you
+ * probably also need to sync the directory containing the file.
+ */
+int osaf_fdatasync(int fd);
+
+#endif  // BASE_OSAF_UNISTD_H_
-- 
2.13.3


--
Check out the vibrant tech community on one of