[ox/fs] Add FileAddress support to FileSystem

This commit is contained in:
Gary Talent 2019-10-12 11:56:49 -05:00
parent ce7c416fb7
commit 54ac86fce7
6 changed files with 142 additions and 9 deletions

View File

@ -10,8 +10,23 @@
namespace ox { namespace ox {
FileLocation::~FileLocation() { FileAddress::FileAddress(uint64_t inode) {
if (m_type == Path) { m_data.inode = inode;
m_type = FileAddressType::Inode;
}
FileAddress::FileAddress(char *path) {
m_data.path = path;
m_type = FileAddressType::Path;
}
FileAddress::FileAddress(const char *path) {
m_data.constPath = path;
m_type = FileAddressType::ConstPath;
}
FileAddress::~FileAddress() {
if (m_type == FileAddressType::Path) {
delete m_data.path; delete m_data.path;
m_data.path = nullptr; m_data.path = nullptr;
} }

View File

@ -6,24 +6,66 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#pragma once
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
namespace ox { namespace ox {
class FileLocation { enum class FileAddressType {
None = -1,
Path,
ConstPath,
Inode,
};
class FileAddress {
private: private:
enum { FileAddressType m_type = FileAddressType::None;
None = -1,
Path = 0,
Inode = 1,
} m_type = None;
union { union {
char *path; char *path;
const char *constPath;
uint64_t inode; uint64_t inode;
} m_data; } m_data;
public: public:
~FileLocation(); FileAddress(uint64_t inode);
FileAddress(char *path);
FileAddress(const char *path);
~FileAddress();
[[nodiscard]] constexpr FileAddressType type() const noexcept {
switch (m_type) {
case FileAddressType::Path:
case FileAddressType::ConstPath:
return FileAddressType::Path;
default:
return m_type;
}
}
[[nodiscard]] constexpr ValErr<uint64_t> getInode() const noexcept {
switch (m_type) {
case FileAddressType::Inode:
return m_data.inode;
default:
return OxError(1);
}
}
[[nodiscard]] constexpr ValErr<const char*> getPath() const noexcept {
switch (m_type) {
case FileAddressType::Path:
return m_data.path;
case FileAddressType::ConstPath:
return m_data.constPath;
default:
return OxError(1);
}
}
}; };

View File

@ -10,6 +10,66 @@
namespace ox { namespace ox {
[[nodiscard]] ox::Error FileSystem::read(FileAddress addr, void *buffer, std::size_t size) {
switch (addr.type()) {
case FileAddressType::Inode:
return read(addr.getInode().value, buffer, size);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return read(addr.getPath().value, buffer, size);
default:
return OxError(1);
}
}
[[nodiscard]] ox::Error FileSystem::read(FileAddress addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) {
switch (addr.type()) {
case FileAddressType::Inode:
return read(addr.getInode().value, readStart, readSize, buffer, size);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return read(addr.getPath().value, readStart, readSize, buffer, size);
default:
return OxError(1);
}
}
[[nodiscard]] ox::Error FileSystem::remove(FileAddress addr, bool recursive) {
switch (addr.type()) {
case FileAddressType::Inode:
return remove(addr.getInode().value, recursive);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return remove(addr.getPath().value, recursive);
default:
return OxError(1);
}
}
ox::Error FileSystem::write(FileAddress addr, void *buffer, uint64_t size, uint8_t fileType) {
switch (addr.type()) {
case FileAddressType::Inode:
return write(addr.getInode().value, buffer, size, fileType);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return write(addr.getPath().value, buffer, size, fileType);
default:
return OxError(1);
}
}
ox::ValErr<FileStat> FileSystem::stat(FileAddress addr) {
switch (addr.type()) {
case FileAddressType::Inode:
return stat(addr.getInode().value);
case FileAddressType::ConstPath:
case FileAddressType::Path:
return stat(addr.getPath().value);
default:
return OxError(1);
}
}
template class FileSystemTemplate<FileStore16, Directory16>; template class FileSystemTemplate<FileStore16, Directory16>;
template class FileSystemTemplate<FileStore32, Directory32>; template class FileSystemTemplate<FileStore32, Directory32>;

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <ox/fs/filestore/filestoretemplate.hpp> #include <ox/fs/filestore/filestoretemplate.hpp>
#include <ox/fs/filesystem/filelocation.hpp>
#include <ox/fs/filesystem/types.hpp> #include <ox/fs/filesystem/types.hpp>
#include "directory.hpp" #include "directory.hpp"
@ -35,18 +36,28 @@ class FileSystem {
virtual ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) = 0; virtual ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) = 0;
[[nodiscard]] ox::Error read(FileAddress addr, void *buffer, std::size_t size);
[[nodiscard]] ox::Error read(FileAddress addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size);
virtual ox::Error remove(const char *path, bool recursive = false) = 0; virtual ox::Error remove(const char *path, bool recursive = false) = 0;
[[nodiscard]] ox::Error remove(FileAddress addr, bool recursive = false);
virtual ox::Error resize(uint64_t size, void *buffer = nullptr) = 0; virtual ox::Error resize(uint64_t size, void *buffer = nullptr) = 0;
virtual ox::Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; virtual ox::Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0;
virtual ox::Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; virtual ox::Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0;
[[nodiscard]] ox::Error write(FileAddress addr, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile);
virtual ox::ValErr<FileStat> stat(uint64_t inode) = 0; virtual ox::ValErr<FileStat> stat(uint64_t inode) = 0;
virtual ox::ValErr<FileStat> stat(const char *path) = 0; virtual ox::ValErr<FileStat> stat(const char *path) = 0;
[[nodiscard]] ox::ValErr<FileStat> stat(FileAddress addr);
virtual uint64_t spaceNeeded(uint64_t size) = 0; virtual uint64_t spaceNeeded(uint64_t size) = 0;
virtual uint64_t available() = 0; virtual uint64_t available() = 0;

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include "filestore/filestoretemplate.hpp" #include "filestore/filestoretemplate.hpp"
#include "filesystem/filelocation.hpp"
#include "filesystem/filesystem.hpp" #include "filesystem/filesystem.hpp"
#include "filesystem/passthroughfs.hpp" #include "filesystem/passthroughfs.hpp"
#include "filesystem/directory.hpp" #include "filesystem/directory.hpp"

View File

@ -64,6 +64,10 @@ struct ValErr {
constexpr ValErr() noexcept: error(0) { constexpr ValErr() noexcept: error(0) {
} }
constexpr ValErr(Error error) noexcept: value(ox::move(value)), error(error) {
this->error = error;
}
constexpr ValErr(T value, Error error = OxError(0)) noexcept: value(ox::move(value)), error(error) { constexpr ValErr(T value, Error error = OxError(0)) noexcept: value(ox::move(value)), error(error) {
} }