#include #include #include #include namespace os { File::File(Badge) { } File::~File() { if (m_fd >= 0) close(m_fd); } Result> File::construct(StringView path, int flags, mode_t mode) { auto file = TRY(adopt_shared(new (std::nothrow) File({}))); long rc = syscall(SYS_open, path.chars(), flags, mode); int fd = TRY(Result::from_syscall(rc)); file->m_fd = fd; return file; } Result> File::open(StringView path, OpenMode flags) { return construct(path, (int)flags, 0); } Result> File::open_or_create(StringView path, OpenMode flags, mode_t mode) { return construct(path, (int)flags | O_CREAT, mode); } Result> File::create(StringView path, OpenMode flags, mode_t mode) { return construct(path, (int)flags | (O_CREAT | O_EXCL), mode); } Result File::raw_read(u8* buf, usize length) { long rc = syscall(SYS_read, m_fd, buf, length); return Result::from_syscall(rc); } Result File::raw_write(const u8* buf, usize length) { long rc = syscall(SYS_write, m_fd, buf, length); return Result::from_syscall(rc); } Result File::write(StringView str) { TRY(raw_write((const u8*)str.chars(), str.length())); return {}; } void File::set_close_on_exec() { fcntl(m_fd, F_SETFD, FD_CLOEXEC); } }