[ox/fs] Add PassthroughFS

This commit is contained in:
Gary Talent 2019-01-11 19:39:20 -06:00
parent 55119253da
commit 59ee1ada56
6 changed files with 261 additions and 1 deletions

View File

@ -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(

View File

@ -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;
}
}
}

View File

@ -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 <ox/std/std.hpp>
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();
};
}

View File

@ -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(<filesystem>)
#include <stdio.h>
#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<FileStat> PassThroughFS::stat(uint64_t) {
// unsupported
return {{}, OxError(1)};
}
ValErr<FileStat> 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

View File

@ -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(<filesystem>)
#include <filesystem>
#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<typename F>
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<FileStat> stat(uint64_t inode) override;
ValErr<FileStat> 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<typename F>
Error PassThroughFS::ls(const char *dir, F cb) {
return OxError(1);
}
}
#endif

View File

@ -10,7 +10,7 @@
#include "bitops.hpp"
#if OX_USE_STDLIB
#if __has_include(<cstdint>)
#include <cstdint>