Commit a11ba478 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent e1e09762
...@@ -17,14 +17,22 @@ ...@@ -17,14 +17,22 @@
// See COPYING file for full licensing terms. // See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options. // See https://www.nexedi.com/licensing for rationale and options.
// XXX #define _GNU_SOURCE ?
#include "wcfs_misc.h" #include "wcfs_misc.h"
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <memory>
// os:: // os::
namespace os { namespace os {
tuple<File, error> open(const string &path, int flags, mode_t mode) { tuple<File, error> open(const string &path, int flags, mode_t mode) {
File f = {._fd = -1, .path = path}; File f = {._fd = -1, .path = path};
int err = std::open(path.c_str(), flags, mode); int err = ::open(path.c_str(), flags, mode);
if (err != 0) if (err != 0)
return make_tuple(f, f._errno("open")); return make_tuple(f, f._errno("open"));
} }
...@@ -32,7 +40,7 @@ tuple<File, error> open(const string &path, int flags, mode_t mode) { ...@@ -32,7 +40,7 @@ tuple<File, error> open(const string &path, int flags, mode_t mode) {
error File::close() { error File::close() {
File *f = this; File *f = this;
int err = close(f->_fd); int err = ::close(f->_fd);
if (err != 0) if (err != 0)
return f->_errno("close"); return f->_errno("close");
} }
...@@ -50,7 +58,9 @@ error File::stat(struct stat *st) { ...@@ -50,7 +58,9 @@ error File::stat(struct stat *st) {
// _errno returns error corresponding to op and errno. // _errno returns error corresponding to op and errno.
error File::_errno(const char *op) { error File::_errno(const char *op) {
File *f = this; File *f = this;
return errorf("%s %s: %s", op, f->path, strerror_r(errno)); char ebuf[128];
char *estr = strerror_r(errno, ebuf, sizeof(ebuf));
return fmt::errorf("%s %s: %s", op, f->_path, estr);
} }
} // os:: } // os::
...@@ -60,7 +70,7 @@ error File::_errno(const char *op) { ...@@ -60,7 +70,7 @@ error File::_errno(const char *op) {
namespace fmt { namespace fmt {
string sprintf(const string &format, ...) { string sprintf(const string &format, ...) {
// https://stackoverflow.com/a/26221725/9456786 // based on https://stackoverflow.com/a/26221725/9456786
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
...@@ -74,4 +84,10 @@ string sprintf(const string &format, ...) { ...@@ -74,4 +84,10 @@ string sprintf(const string &format, ...) {
return string(buf.get(), buf.get() + size - 1 ); // without trailing '\0' return string(buf.get(), buf.get() + size - 1 ); // without trailing '\0'
} }
error errorf(const string &format, ...) {
error err;
err.err = fmt::sprintf(format, ...); // XXX
return err;
}
} // fmt:: } // fmt::
...@@ -35,7 +35,7 @@ const nullptr_t nil = nullptr; ...@@ -35,7 +35,7 @@ const nullptr_t nil = nullptr;
// error mimics error from Go. // error mimics error from Go.
struct error { struct error {
string err; string err; // XXX -> private + ,error() ?
error() {} error() {}
error(nullptr_t) {} // = nil error(nullptr_t) {} // = nil
...@@ -61,7 +61,7 @@ class File { ...@@ -61,7 +61,7 @@ class File {
string _path; string _path;
public: public:
friend open... friend tuple<File, error> open(const string &path, int flags, mode_t mode);
// XXX empty ctor -> fd=-1, path=? // XXX empty ctor -> fd=-1, path=?
int fd(); int fd();
string name(); string name();
...@@ -85,6 +85,7 @@ tuple<File, error> open(const string &path, int flags = O_RDONLY, ...@@ -85,6 +85,7 @@ tuple<File, error> open(const string &path, int flags = O_RDONLY,
namespace fmt { namespace fmt {
string sprintf(const string &format, ...); string sprintf(const string &format, ...);
error errorf (const string &format, ...);
} // fmt:: } // fmt::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment