diff --git a/deps/ox/src/ox/fs/CMakeLists.txt b/deps/ox/src/ox/fs/CMakeLists.txt index 9ab6e198..1efe83c3 100644 --- a/deps/ox/src/ox/fs/CMakeLists.txt +++ b/deps/ox/src/ox/fs/CMakeLists.txt @@ -2,9 +2,11 @@ add_library( OxFS filestore/filestoretemplate.cpp + filesystem/filelocation.cpp filesystem/pathiterator.cpp filesystem/directory.cpp filesystem/filesystem.cpp + filesystem/passthroughfs.cpp ) set_property( diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp new file mode 100644 index 00000000..097f7f9c --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -0,0 +1,20 @@ +/* + * Copyright 2015 - 2018 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "filelocation.hpp" + +namespace ox { + +FileLocation::~FileLocation() { + if (m_type == Path && m_data.path) { + delete m_data.path; + m_data.path = nullptr; + } +} + +} diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp new file mode 100644 index 00000000..3d093889 --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -0,0 +1,30 @@ +/* + * Copyright 2015 - 2018 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +namespace ox { + +class FileLocation { + private: + enum { + None = -1, + Path = 0, + Inode = 1, + } m_type = None; + union { + char *path; + uint64_t inode; + } m_data; + + public: + ~FileLocation(); + +}; + +} diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp new file mode 100644 index 00000000..b6f0ad1d --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -0,0 +1,129 @@ +/* + * Copyright 2015 - 2018 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#if __has_include() + +#include + +#include "passthroughfs.hpp" + +namespace ox { + +PassThroughFS::PassThroughFS(const char *dirPath) { + m_path = dirPath; +} + +PassThroughFS::~PassThroughFS() { +} + +Error PassThroughFS::mkdir(const char *path, bool recursive) { + bool success = false; + if (recursive) { + success = std::filesystem::create_directories(m_path / path); + } else { + success = std::filesystem::create_directory(m_path / path); + } + return OxError(success ? 0 : 1); +} + +Error PassThroughFS::move(const char *src, const char *dest) { + std::filesystem::rename(m_path / src, m_path / dest); + return OxError(0); +} + +Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) { + auto file = fopen((m_path / path).c_str(), "r"); + if (file) { + fseek(file, 0, SEEK_END); + const std::size_t size = ftell(file); + if (size <= buffSize) { + rewind(file); + auto itemsRead = fread(buffer, buffSize, 1, file); + fclose(file); + return OxError(itemsRead == 1 ? 0 : 1); + } + } + return OxError(1); +} + +Error PassThroughFS::read(uint64_t, void*, std::size_t) { + // unsupported + return OxError(1); +} + +Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) { + // unsupported + return OxError(1); +} + +Error PassThroughFS::remove(const char *path, bool recursive) { + if (recursive) { + return OxError(std::filesystem::remove_all(m_path / path) != 0); + } else { + return OxError(std::filesystem::remove(m_path / path) != 0); + } +} + +void PassThroughFS::resize(uint64_t, void*) { + // unsupported +} + +Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) { + auto f = fopen(path, "w"); + auto err = OxError(fwrite(buffer, size, 1, f) == size ? 0 : 1); + fclose(f); + return err; +} + +Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) { + // unsupported + return OxError(1); +} + +ValErr PassThroughFS::stat(uint64_t) { + // unsupported + return {{}, OxError(1)}; +} + +ValErr PassThroughFS::stat(const char *path) { + std::error_code ec; + auto size = std::filesystem::file_size(path, ec); + return {{.size = size}, OxError(ec.value())}; +} + +uint64_t PassThroughFS::spaceNeeded(uint64_t size) { + return size; +} + +uint64_t PassThroughFS::available() { + std::error_code ec; + auto s = std::filesystem::space(path, ec); + return s.available; +} + +uint64_t PassThroughFS::size() const { + std::error_code ec; + auto s = std::filesystem::space(path, ec); + return s.capacity; +} + +uint8_t *PassThroughFS::buff() { + return nullptr; +} + +Error PassThroughFS::walk(Error(*)(uint8_t, uint64_t, uint64_t)) { + return OxError(1); +} + +bool PassThroughFS::valid() const { + return std::filesystem::is_directory(m_path); +} + +} + +#endif diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp new file mode 100644 index 00000000..e0e68de0 --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -0,0 +1,79 @@ +/* + * Copyright 2015 - 2018 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#if __has_include() + +#include + +#include "filesystem.hpp" + +namespace ox { + +/** + * + */ +class PassThroughFS: public FileSystem { + private: + std::filesystem::path m_path; + + public: + PassThroughFS() = default; + + PassThroughFS(const char *dirPath); + + ~PassThroughFS(); + + Error mkdir(const char *path, bool recursive = false) override; + + Error move(const char *src, const char *dest) override; + + Error read(const char *path, void *buffer, std::size_t buffSize) override; + + Error read(uint64_t inode, void *buffer, std::size_t size) override; + + Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) override; + + template + Error ls(const char *dir, F cb); + + Error remove(const char *path, bool recursive = false) override; + + void resize(uint64_t size, void *buffer = nullptr) override; + + Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) override; + + Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) override; + + ValErr stat(uint64_t inode) override; + + ValErr stat(const char *path) override; + + uint64_t spaceNeeded(uint64_t size) override; + + uint64_t available() override; + + uint64_t size() const override; + + uint8_t *buff() override; + + Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) override; + + bool valid() const override; + +}; + +template +Error PassThroughFS::ls(const char *dir, F cb) { + return OxError(1); +} + +} + +#endif diff --git a/deps/ox/src/ox/std/types.hpp b/deps/ox/src/ox/std/types.hpp index 44fb987d..cfedecf0 100644 --- a/deps/ox/src/ox/std/types.hpp +++ b/deps/ox/src/ox/std/types.hpp @@ -10,7 +10,7 @@ #include "bitops.hpp" -#if OX_USE_STDLIB +#if __has_include() #include