summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gfx/models/texture.cpp18
-rw-r--r--lib/filesystem.cpp64
-rw-r--r--lib/filesystem.h42
3 files changed, 113 insertions, 11 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index 5b8d376..6019bec 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -3,6 +3,7 @@
#include <GL/glew.h>
#include <cache.h>
#include <fcntl.h>
+#include <filesystem.h>
#include <gfx/image.h>
#include <glm/geometric.hpp>
#include <resource.h>
@@ -50,18 +51,13 @@ Texture::save(const glTexture & texture, GLenum format, const glm::ivec2 & size,
size_t dataSize = (static_cast<size_t>(size.x * size.y * channels));
size_t fileSize = dataSize + sizeof(TGAHead);
- auto out = open(path, O_RDWR | O_CREAT, 0660);
- std::ignore = ftruncate(out, static_cast<off_t>(fileSize));
- TGAHead * tga = static_cast<TGAHead *>(mmap(nullptr, fileSize, PROT_WRITE, MAP_SHARED, out, 0));
- close(out);
- if (tga == MAP_FAILED) {
- return;
- }
- *tga = {0, tgaFormat, 0, 0, 0, 0, static_cast<short>(size.x), static_cast<short>(size.y),
+ filesystem::fh out {path, O_RDWR | O_CREAT, 0660};
+ out.truncate(fileSize);
+ auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED);
+ *tga.get<TGAHead>() = {0, tgaFormat, 0, 0, 0, 0, static_cast<short>(size.x), static_cast<short>(size.y),
static_cast<short>(8 * channels)};
- glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast<GLsizei>(dataSize), tga + 1);
- msync(tga, fileSize, MS_ASYNC);
- munmap(tga, fileSize);
+ glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast<GLsizei>(dataSize), tga.get<TGAHead>() + 1);
+ tga.msync(MS_ASYNC);
}
void
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 <fcntl.h>
+#include <filesystem>
+#include <string>
+#include <sys/mman.h>
+#include <system_error>
+#include <unistd.h>
+
+namespace filesystem {
+ template<typename... Args>
+ [[noreturn]] static void
+ throw_filesystem_error(std::string operation, int err, Args &&... args)
+ {
+ throw std::filesystem::filesystem_error {
+ std::move(operation), std::forward<Args>(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<off_t>(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<off_t>(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 <cstddef>
+#include <sys/types.h>
+
+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<typename T>
+ T *
+ get()
+ {
+ return static_cast<T *>(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;
+ };
+}