Introduce error_string() as a portable replacement for strerror_r()

This commit is contained in:
Joe Thornber 2015-01-16 12:54:09 +00:00
parent f1130198e1
commit 25b4b526f4
5 changed files with 17 additions and 13 deletions

View File

@ -29,6 +29,7 @@ SOURCE=\
base/base64.cc \ base/base64.cc \
base/endian_utils.cc \ base/endian_utils.cc \
base/error_state.cc \ base/error_state.cc \
base/error_string.cc \
base/progress_monitor.cc \ base/progress_monitor.cc \
base/xml_utils.cc \ base/xml_utils.cc \
block-cache/block_cache.cc \ block-cache/block_cache.cc \
@ -98,6 +99,7 @@ CFLAGS+=-g -Wall -O3
CXXFLAGS+=-g -Wall -fno-strict-aliasing CXXFLAGS+=-g -Wall -fno-strict-aliasing
CXXFLAGS+=@CXXOPTIMISE_FLAG@ CXXFLAGS+=@CXXOPTIMISE_FLAG@
CXXFLAGS+=@CXXDEBUG_FLAG@ CXXFLAGS+=@CXXDEBUG_FLAG@
CXXFLAGS+=@CXX_STRERROR_FLAG@
INCLUDES+=-I$(TOP_BUILDDIR) -I$(TOP_DIR) -I$(TOP_DIR)/thin-provisioning INCLUDES+=-I$(TOP_BUILDDIR) -I$(TOP_DIR) -I$(TOP_DIR)/thin-provisioning
LIBS:=-lstdc++ -laio -lexpat LIBS:=-lstdc++ -laio -lexpat
INSTALL:=@INSTALL@ INSTALL:=@INSTALL@

View File

@ -13,6 +13,7 @@
#include <unistd.h> #include <unistd.h>
#include "base/error_state.h" #include "base/error_state.h"
#include "base/error_string.h"
#include "base/nested_output.h" #include "base/nested_output.h"
#include "caching/commands.h" #include "caching/commands.h"
#include "caching/metadata.h" #include "caching/metadata.h"
@ -202,10 +203,7 @@ namespace {
int r = ::stat(path.c_str(), &info); int r = ::stat(path.c_str(), &info);
if (r) { if (r) {
ostringstream msg; ostringstream msg;
char buffer[128], *ptr; msg << path << ": " << error_string(errno);
ptr = ::strerror_r(errno, buffer, sizeof(buffer));
msg << path << ": " << ptr;
throw runtime_error(msg.str()); throw runtime_error(msg.str());
} }

View File

@ -42,6 +42,13 @@ AC_PROG_MAKE_SET
AC_PROG_MKDIR_P AC_PROG_MKDIR_P
AC_PROG_INSTALL AC_PROG_INSTALL
################################################################
dnl -- Checks for functions.
AC_FUNC_STRERROR_R
if test x$ac_cv_func_strerror_r_char_p = xyes; then
CXX_STRERROR_FLAG="-DSTRERROR_R_CHAR_P"
fi
################################################################################ ################################################################################
dnl -- Prefix is /usr by default, the exec_prefix default is setup later dnl -- Prefix is /usr by default, the exec_prefix default is setup later
AC_PREFIX_DEFAULT(/usr) AC_PREFIX_DEFAULT(/usr)
@ -136,6 +143,7 @@ VERSION_PATCHLEVEL=`echo "$VER" | $AWK -F '[[(.]]' '{print $3}'`
################################################################ ################################################################
AC_SUBST(CXXDEBUG_FLAG) AC_SUBST(CXXDEBUG_FLAG)
AC_SUBST(CXXOPTIMISE_FLAG) AC_SUBST(CXXOPTIMISE_FLAG)
AC_SUBST(CXX_STRERROR_FLAG)
AC_SUBST(INSTALL) AC_SUBST(INSTALL)
AC_SUBST(prefix) AC_SUBST(prefix)
AC_SUBST(RELEASE_DATE) AC_SUBST(RELEASE_DATE)

View File

@ -13,6 +13,7 @@
#include <unistd.h> #include <unistd.h>
#include "base/error_state.h" #include "base/error_state.h"
#include "base/error_string.h"
#include "base/nested_output.h" #include "base/nested_output.h"
#include "era/commands.h" #include "era/commands.h"
#include "era/writeset_tree.h" #include "era/writeset_tree.h"
@ -186,10 +187,7 @@ namespace {
int r = ::stat(path.c_str(), &info); int r = ::stat(path.c_str(), &info);
if (r) { if (r) {
ostringstream msg; ostringstream msg;
char buffer[128], *ptr; msg << path << ": " << error_string(errno);;
ptr = ::strerror_r(errno, buffer, sizeof(buffer));
msg << path << ": " << ptr;
throw runtime_error(msg.str()); throw runtime_error(msg.str());
} }

View File

@ -18,13 +18,14 @@
#include "block.h" #include "block.h"
#include "base/error_string.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <stdexcept> #include <stdexcept>
@ -44,11 +45,8 @@ namespace {
// FIXME: introduce a new exception for this, or at least lift this // FIXME: introduce a new exception for this, or at least lift this
// to exception.h // to exception.h
void syscall_failed(char const *call) { void syscall_failed(char const *call) {
char buffer[128];
char *msg = strerror_r(errno, buffer, sizeof(buffer));
ostringstream out; ostringstream out;
out << "syscall '" << call << "' failed: " << msg; out << "syscall '" << call << "' failed: " << base::error_string(errno);;
throw runtime_error(out.str()); throw runtime_error(out.str());
} }