From f4fa52d7633d9d67d4c41f76b0e317c6232eb907 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Dec 2022 12:39:26 +0000 Subject: Add a mini C filesystem wrapper library with mmap support --- lib/filesystem.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/filesystem.h | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 lib/filesystem.cpp create mode 100644 lib/filesystem.h (limited to 'lib') diff --git a/lib/filesystem.cpp b/lib/filesystem.cpp new file mode 100644 index 0000000..181fb07 --- /dev/null +++ b/lib/filesystem.cpp @@ -0,0 +1,64 @@ +#include "filesystem.h" +#include +#include +#include +#include +#include +#include + +namespace filesystem { + template + [[noreturn]] static void + throw_filesystem_error(std::string operation, int err, Args &&... args) + { + throw std::filesystem::filesystem_error { + std::move(operation), std::forward(args)..., std::error_code {err, std::system_category()}}; + } + + memmap::memmap(size_t length, int prot, int flags, int fd, off_t offset) : + addr {mmap(nullptr, length, prot, flags, fd, offset)}, length {length} + { + if (addr == MAP_FAILED) { + throw std::filesystem::filesystem_error {"mmap", std::error_code {errno, std::system_category()}}; + } + } + + memmap::~memmap() + { + ::munmap(addr, length); + } + + void + memmap::msync(int flags) const + { + if (::msync(addr, length, flags)) { + throw_filesystem_error("msync", errno); + } + } + + fh::fh(const char * path, int flags, int mode) : h {open(path, flags, mode)} + { + if (h == -1) { + throw_filesystem_error("open", errno, path); + } + } + + fh::~fh() + { + ::close(h); + } + + void + fh::truncate(size_t size) + { + if (::ftruncate(h, static_cast(size))) { + throw_filesystem_error("ftruncate", errno); + } + } + + memmap + fh::mmap(size_t length, size_t offset, int prot, int flags) + { + return memmap {length, prot, flags, h, static_cast(offset)}; + } +} diff --git a/lib/filesystem.h b/lib/filesystem.h new file mode 100644 index 0000000..0c44236 --- /dev/null +++ b/lib/filesystem.h @@ -0,0 +1,42 @@ +#pragma once + +#include "special_members.hpp" +#include +#include + +namespace filesystem { + class [[nodiscard]] memmap final { + public: + memmap(size_t length, int prot, int flags, int fd, off_t offset); + ~memmap(); + NO_MOVE(memmap); + NO_COPY(memmap); + + template + T * + get() + { + return static_cast(addr); + } + + void msync(int flags) const; + + private: + void * addr; + size_t length; + }; + + class [[nodiscard]] fh final { + public: + fh(const char * path, int flags, int mode); + ~fh(); + NO_MOVE(fh); + NO_COPY(fh); + + void truncate(size_t size); + memmap mmap(size_t length, size_t offset, int prot, int flags); + + private: + int h; + }; +} -- cgit v1.2.3