[ox/fs] Add new version of ls and make direct read return const

This commit is contained in:
Gary Talent 2021-04-17 16:36:39 -05:00
parent fef0ff58c3
commit 772cc7d954
4 changed files with 40 additions and 12 deletions

View File

@ -10,7 +10,7 @@
namespace ox {
Result<uint8_t*> FileSystem::read(FileAddress addr) noexcept {
Result<const uint8_t*> FileSystem::read(FileAddress addr) noexcept {
switch (addr.type()) {
case FileAddressType::Inode:
return read(addr.getInode().value);

View File

@ -32,19 +32,21 @@ class FileSystem {
virtual Error read(const char *path, void *buffer, std::size_t buffSize) noexcept = 0;
virtual Result<uint8_t*> read(const char *path) noexcept = 0;
virtual Result<const uint8_t*> read(const char *path) noexcept = 0;
virtual Error read(uint64_t inode, void *buffer, std::size_t size) noexcept = 0;
virtual Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0;
virtual Result<uint8_t*> read(uint64_t inode) noexcept = 0;
virtual Result<const uint8_t*> read(uint64_t inode) noexcept = 0;
Error read(FileAddress addr, void *buffer, std::size_t size) noexcept;
Error read(FileAddress addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept;
Result<uint8_t*> read(FileAddress addr) noexcept;
Result<const uint8_t*> read(FileAddress addr) noexcept;
virtual Result<Vector<String>> ls(const char *dir) noexcept = 0;
virtual Error remove(const char *path, bool recursive = false) noexcept = 0;
@ -118,13 +120,15 @@ class FileSystemTemplate: public FileSystem {
Error read(const char *path, void *buffer, std::size_t buffSize) noexcept override;
Result<uint8_t*> read(const char*) noexcept override;
Result<const uint8_t*> read(const char*) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Result<uint8_t*> read(uint64_t) noexcept override;
Result<const uint8_t*> read(uint64_t) noexcept override;
Result<Vector<String>> ls(const char *dir) noexcept override;
template<typename F>
Error ls(const char *dir, F cb);
@ -241,7 +245,7 @@ Error FileSystemTemplate<FileStore, Directory>::read(const char *path, void *buf
}
template<typename FileStore, typename Directory>
Result<uint8_t*> FileSystemTemplate<FileStore, Directory>::read(const char *path) noexcept {
Result<const uint8_t*> FileSystemTemplate<FileStore, Directory>::read(const char *path) noexcept {
auto fd = fileSystemData();
oxReturnError(fd.error);
Directory rootDir(m_fs, fd.value.rootDirInode);
@ -261,7 +265,7 @@ Error FileSystemTemplate<FileStore, Directory>::read(uint64_t inode, std::size_t
}
template<typename FileStore, typename Directory>
Result<uint8_t*> FileSystemTemplate<FileStore, Directory>::read(uint64_t inode) noexcept {
Result<const uint8_t*> FileSystemTemplate<FileStore, Directory>::read(uint64_t inode) noexcept {
auto data = m_fs.read(inode);
if (!data.valid()) {
return OxError(1);
@ -269,6 +273,16 @@ Result<uint8_t*> FileSystemTemplate<FileStore, Directory>::read(uint64_t inode)
return data.get();
}
template<typename FileStore, typename Directory>
Result<Vector<String>> FileSystemTemplate<FileStore, Directory>::ls(const char *path) noexcept {
Vector<String> out;
oxReturnError(ls(path, [&out](const char *name, typename FileStore::InodeId_t) {
out.emplace_back(name);
return OxError(0);
}));
return ox::move(out);
}
template<typename FileStore, typename Directory>
template<typename F>
Error FileSystemTemplate<FileStore, Directory>::ls(const char *path, F cb) {

View File

@ -74,7 +74,7 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize)
return OxError(0);
}
Result<uint8_t*> PassThroughFS::read(const char*) noexcept {
Result<const uint8_t*> PassThroughFS::read(const char*) noexcept {
return OxError(1);
}
@ -88,10 +88,22 @@ Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t
return OxError(1);
}
Result<uint8_t*> PassThroughFS::read(uint64_t) noexcept {
Result<const uint8_t*> PassThroughFS::read(uint64_t) noexcept {
return OxError(1);
}
Result<Vector<String>> PassThroughFS::ls(const char *dir) noexcept {
Vector<String> out;
std::error_code ec;
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed"));
for (auto &p : di) {
auto u8p = p.path().filename().u8string();
out.emplace_back(ox::bit_cast<const char*>(u8p.c_str()));
}
return ox::move(out);
}
Error PassThroughFS::remove(const char *path, bool recursive) noexcept {
if (recursive) {
return OxError(std::filesystem::remove_all(m_path / stripSlash(path)) != 0);

View File

@ -43,13 +43,15 @@ class PassThroughFS: public FileSystem {
Error read(const char *path, void *buffer, std::size_t buffSize) noexcept override;
Result<uint8_t*> read(const char*) noexcept override;
Result<const uint8_t*> read(const char*) noexcept override;
Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override;
Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override;
Result<uint8_t*> read(uint64_t) noexcept override;
Result<const uint8_t*> read(uint64_t) noexcept override;
Result<Vector<String>> ls(const char *dir) noexcept override;
template<typename F>
Error ls(const char *dir, F cb) noexcept;