[ox/fs] Strip leading slashes in PassThroughFS paths

This commit is contained in:
Gary Talent 2019-01-15 22:01:30 -06:00
parent ec6326001d
commit 791c076b51
3 changed files with 42 additions and 15 deletions

View File

@ -23,21 +23,23 @@ PassThroughFS::~PassThroughFS() {
Error PassThroughFS::mkdir(const char *path, bool recursive) {
bool success = false;
const auto p = m_path / stripSlash(path);
oxTrace("ox::fs::PassThroughFS::mkdir") << p.c_str();
if (recursive) {
success = std::filesystem::create_directories(m_path / path);
success = std::filesystem::create_directories(p);
} else {
success = std::filesystem::create_directory(m_path / path);
success = std::filesystem::create_directory(p);
}
return OxError(success ? 0 : 1);
}
Error PassThroughFS::move(const char *src, const char *dest) {
std::filesystem::rename(m_path / src, m_path / dest);
std::filesystem::rename(m_path / stripSlash(src), m_path / stripSlash(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");
auto file = fopen((m_path / stripSlash(path)).c_str(), "r");
if (file) {
fseek(file, 0, SEEK_END);
const std::size_t size = ftell(file);
@ -63,9 +65,9 @@ Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t
Error PassThroughFS::remove(const char *path, bool recursive) {
if (recursive) {
return OxError(std::filesystem::remove_all(m_path / path) != 0);
return OxError(std::filesystem::remove_all(m_path / stripSlash(path)) != 0);
} else {
return OxError(std::filesystem::remove(m_path / path) != 0);
return OxError(std::filesystem::remove(m_path / stripSlash(path)) != 0);
}
}
@ -74,8 +76,9 @@ void PassThroughFS::resize(uint64_t, void*) {
}
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);
auto p = (m_path / stripSlash(path));
auto f = fopen(p.c_str(), "w");
auto err = OxError(fwrite(buffer, size, 1, f) == 1 ? 0 : 1);
fclose(f);
return err;
}
@ -92,8 +95,14 @@ ValErr<FileStat> PassThroughFS::stat(uint64_t) {
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())};
const auto p = m_path / stripSlash(path);
uint8_t type = std::filesystem::is_directory(p, ec) ?
FileType_Directory : FileType_NormalFile;
oxTrace("PassThroughFS::stat") << ec.message().c_str() << path;
uint64_t size = type == FileType_Directory ? 0 : std::filesystem::file_size(p, ec);
oxTrace("PassThroughFS::stat") << ec.message().c_str() << path;
oxTrace("PassThroughFS::stat::size") << path << size;
return {{.size = size, .fileType = type}, OxError(ec.value())};
}
uint64_t PassThroughFS::spaceNeeded(uint64_t size) {
@ -102,13 +111,13 @@ uint64_t PassThroughFS::spaceNeeded(uint64_t size) {
uint64_t PassThroughFS::available() {
std::error_code ec;
auto s = std::filesystem::space(path, ec);
auto s = std::filesystem::space(m_path, ec);
return s.available;
}
uint64_t PassThroughFS::size() const {
std::error_code ec;
auto s = std::filesystem::space(path, ec);
auto s = std::filesystem::space(m_path, ec);
return s.capacity;
}
@ -124,6 +133,14 @@ bool PassThroughFS::valid() const {
return std::filesystem::is_directory(m_path);
}
const char *PassThroughFS::stripSlash(const char *path) {
auto pathLen = ox_strlen(path);
for (decltype(pathLen) i = 0; i < pathLen && path[0] == '/'; i++) {
path++;
}
return path;
}
}
#endif

View File

@ -24,8 +24,6 @@ class PassThroughFS: public FileSystem {
std::filesystem::path m_path;
public:
PassThroughFS() = default;
PassThroughFS(const char *dirPath);
~PassThroughFS();
@ -67,11 +65,22 @@ class PassThroughFS: public FileSystem {
bool valid() const override;
private:
/**
* Strips the leading slashes from a string.
*/
const char *stripSlash(const char *path);
};
template<typename F>
Error PassThroughFS::ls(const char *dir, F cb) {
return OxError(1);
for (auto &p : std::filesystem::directory_iterator(m_path / stripSlash(dir))) {
if (auto err = cb(p.path().filename().c_str(), 0); err) {
return err;
}
}
return OxError(0);
}
}

View File

@ -10,4 +10,5 @@
#include "filestore/filestoretemplate.hpp"
#include "filesystem/filesystem.hpp"
#include "filesystem/passthroughfs.hpp"
#include "filesystem/directory.hpp"