Commit a11ba478 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent e1e09762
......@@ -17,14 +17,22 @@
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// XXX #define _GNU_SOURCE ?
#include "wcfs_misc.h"
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <memory>
// os::
namespace os {
tuple<File, error> open(const string &path, int flags, mode_t mode) {
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)
return make_tuple(f, f._errno("open"));
}
......@@ -32,7 +40,7 @@ tuple<File, error> open(const string &path, int flags, mode_t mode) {
error File::close() {
File *f = this;
int err = close(f->_fd);
int err = ::close(f->_fd);
if (err != 0)
return f->_errno("close");
}
......@@ -50,7 +58,9 @@ error File::stat(struct stat *st) {
// _errno returns error corresponding to op and errno.
error File::_errno(const char *op) {
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::
......@@ -60,7 +70,7 @@ error File::_errno(const char *op) {
namespace fmt {
string sprintf(const string &format, ...) {
// https://stackoverflow.com/a/26221725/9456786
// based on https://stackoverflow.com/a/26221725/9456786
va_list ap;
va_start(ap, format);
......@@ -74,4 +84,10 @@ string sprintf(const string &format, ...) {
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::
......@@ -35,7 +35,7 @@ const nullptr_t nil = nullptr;
// error mimics error from Go.
struct error {
string err;
string err; // XXX -> private + ,error() ?
error() {}
error(nullptr_t) {} // = nil
......@@ -61,7 +61,7 @@ class File {
string _path;
public:
friend open...
friend tuple<File, error> open(const string &path, int flags, mode_t mode);
// XXX empty ctor -> fd=-1, path=?
int fd();
string name();
......@@ -85,6 +85,7 @@ tuple<File, error> open(const string &path, int flags = O_RDONLY,
namespace fmt {
string sprintf(const string &format, ...);
error errorf (const string &format, ...);
} // 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