Commit 4c139017 authored by Tom Niget's avatar Tom Niget

Add mode support to open()

parent 51775850
......@@ -184,17 +184,55 @@ struct {
using PyFile = decltype(file)::type;
//}
typon::Task<typon::PyFile> open(const PyStr &path, std::string_view mode) {
typon::Task<PyFile> open(const PyStr &path, std::string_view mode) {
const char *path_c = path.c_str();
size_t len = 0;
struct statx statxbuf;
if (int err = co_await typon::io::statx(AT_FDCWD, path_c, 0, STATX_SIZE, &statxbuf)) {
system_error(-err, "statx()");
if (int err = co_await typon::io::statx(AT_FDCWD, path_c, 0, STATX_SIZE,
&statxbuf)) {
// new file
} else {
len = statxbuf.stx_size;
}
int fd = co_await typon::io::openat(AT_FDCWD, path_c, O_RDONLY, 0);
int flags = 0;
bool created = false, writable = false, readable = false;
if (mode.find('x') != std::string_view::npos) {
created = true;
writable = true;
flags = O_EXCL | O_CREAT;
} else if (mode.find('r') != std::string_view::npos) {
readable = true;
flags = 0;
} else if (mode.find('w') != std::string_view::npos) {
writable = true;
flags = O_CREAT | O_TRUNC;
} else if (mode.find('a') != std::string_view::npos) {
writable = true;
flags = O_APPEND | O_CREAT;
}
if (mode.find('+') != std::string_view::npos) {
readable = true;
writable = true;
}
if (readable && writable) {
flags |= O_RDWR;
} else if (readable) {
flags |= O_RDONLY;
} else {
flags |= O_WRONLY;
}
flags |= O_CLOEXEC;
int fd = co_await typon::io::openat(AT_FDCWD, path_c, flags, 0666);
if (fd < 0) {
std::cerr << path << "," << flags << std::endl;
system_error(-fd, "openat()");
}
co_return typon::PyFile(fd, statxbuf.stx_size);
co_return PyFile(fd, len);
}
#include "../typon/generator.hpp"
......
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