From 247013089989bd8bfac2149cfb07a0157deb6069 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Fri, 27 May 2011 15:41:17 -0700 Subject: [PATCH] monitorstore: return error codes for consistency Signed-off-by: Colin McCabe --- src/mon/Monitor.cc | 11 +++++---- src/mon/MonitorStore.cc | 49 +++++++++++++++++------------------------ src/mon/MonitorStore.h | 18 ++++----------- 3 files changed, 29 insertions(+), 49 deletions(-) diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 676fef47fd641..256942846b941 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -43,6 +43,7 @@ #include "common/Timer.h" #include "common/Clock.h" #include "common/DoutStreambuf.h" +#include "common/errno.h" #include "include/color.h" #include "OSDMonitor.h" @@ -1006,13 +1007,11 @@ int Monitor::mkfs(bufferlist& osdmapbl) bufferlist magicbl; magicbl.append(CEPH_MON_ONDISK_MAGIC); magicbl.append("\n"); - try { - store->put_bl_ss(magicbl, "magic", 0); - } - catch (const MonitorStore::Error &e) { + int r = store->put_bl_ss(magicbl, "magic", 0); + if (r < 0) { dout(0) << TEXT_RED << "** ERROR: initializing cmon failed: couldn't " - << "initialize the monitor state machine: " - << e.what() << TEXT_NORMAL << dendl; + << "initialize the monitor state machine: " << cpp_strerror(r) + << TEXT_NORMAL << dendl; exit(1); } diff --git a/src/mon/MonitorStore.cc b/src/mon/MonitorStore.cc index 7316a7604e23f..95983dc3bd2ec 100644 --- a/src/mon/MonitorStore.cc +++ b/src/mon/MonitorStore.cc @@ -39,28 +39,6 @@ static ostream& _prefix(const string& dir) { #include #include -MonitorStore::Error MonitorStore::Error:: -FromErrno(const char *prefix, const char *prefix2, int errno_) -{ - char buf[128]; - const char *b2 = strerror_r(errno_, buf, sizeof(buf)); - ostringstream oss; - oss << prefix << prefix2 << ": " << b2 << " (" << errno_ << ")"; - return MonitorStore::Error(oss.str()); -} - -MonitorStore::Error:: -Error(const std::string &str_) : str(str_) { } - -MonitorStore::Error:: -~Error() throw () { } - -const char *MonitorStore::Error:: -what() const throw () -{ - return str.c_str(); -} - int MonitorStore::mount() { char t[1024]; @@ -311,7 +289,8 @@ int MonitorStore::get_bl_ss(bufferlist& bl, const char *a, const char *b) return len; } -int MonitorStore::write_bl_ss(bufferlist& bl, const char *a, const char *b, bool append, bool sync) +int MonitorStore:: +write_bl_ss_impl(bufferlist& bl, const char *a, const char *b, bool append, bool sync) { char fn[1024]; snprintf(fn, sizeof(fn), "%s/%s", dir.c_str(), a); @@ -328,13 +307,20 @@ int MonitorStore::write_bl_ss(bufferlist& bl, const char *a, const char *b, bool int fd; if (append) { fd = ::open(fn, O_WRONLY|O_CREAT|O_APPEND, 0644); - if (fd < 0) - throw Error::FromErrno("failed to open for append: ", fn, errno); + if (fd < 0) { + err = -errno; + derr << "failed to open " << fn << "for append: " + << cpp_strerror(err) << dendl; + return err; + } } else { snprintf(tfn, sizeof(tfn), "%s.new", fn); fd = ::open(tfn, O_WRONLY|O_CREAT, 0644); - if (fd < 0) - throw Error::FromErrno("failed to open: ", tfn, errno); + if (fd < 0) { + err = -errno; + derr << "failed to open " << tfn << ": " << cpp_strerror(err) << dendl; + return err; + } } err = bl.write_fd(fd); @@ -346,8 +332,13 @@ int MonitorStore::write_bl_ss(bufferlist& bl, const char *a, const char *b, bool ::rename(tfn, fn); } - assert(!err); // for now - return err; } +int MonitorStore:: +write_bl_ss(bufferlist& bl, const char *a, const char *b, bool append, bool sync) +{ + int err = write_bl_ss_impl(bl, a, b, append,sync); + assert(!err); // for now + return 0; +} diff --git a/src/mon/MonitorStore.h b/src/mon/MonitorStore.h index 01f27f48c9823..8e931e19e0794 100644 --- a/src/mon/MonitorStore.h +++ b/src/mon/MonitorStore.h @@ -25,21 +25,11 @@ class MonitorStore { string dir; int lock_fd; - int write_bl_ss(bufferlist& bl, const char *a, const char *b, bool append, bool sync=true); - + int write_bl_ss_impl(bufferlist& bl, const char *a, const char *b, + bool append, bool sync); + int write_bl_ss(bufferlist& bl, const char *a, const char *b, + bool append, bool sync=true); public: - class Error : public std::exception - { - public: - static Error FromErrno(const char *prefix, - const char *prefix2, int errno_); - Error(const std::string &str_); - virtual ~Error() throw (); - const char *what() const throw (); - private: - std::string str; - }; - MonitorStore(const std::string &d) : dir(d) { } ~MonitorStore() { } -- 2.47.3