From 24fbb8ba86a513693020d4bbb8fc245df6fcc871 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 26 Apr 2018 21:11:48 -0500 Subject: [PATCH] [ox/fs] Add new FileStore write --- deps/ox/src/ox/fs/filestore/filestore.hpp | 2 + .../src/ox/fs/filestore/filestoretemplate.hpp | 14 ++++- deps/ox/src/ox/fs/filesystem/pathiterator.cpp | 7 +++ deps/ox/src/ox/fs/filesystem/pathiterator.hpp | 10 +++- deps/ox/src/ox/fs/filesystem2/directory.hpp | 51 ++++++++++++------- deps/ox/src/ox/fs/test/tests.cpp | 15 +++--- deps/ox/src/ox/ptrarith/nodebuffer.hpp | 11 ++-- deps/ox/src/ox/std/string.hpp | 2 +- deps/ox/src/ox/std/types.hpp | 4 +- 9 files changed, 81 insertions(+), 35 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestore.hpp b/deps/ox/src/ox/fs/filestore/filestore.hpp index 393c2b7f..b9627b12 100644 --- a/deps/ox/src/ox/fs/filestore/filestore.hpp +++ b/deps/ox/src/ox/fs/filestore/filestore.hpp @@ -39,6 +39,8 @@ class FileStore { virtual Error read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size) = 0; + virtual ValErr read(InodeId_t id) = 0; + virtual StatInfo stat(InodeId_t id) = 0; virtual InodeId_t spaceNeeded(FsSize_t size) = 0; diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 667faaa0..83cff48b 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -22,7 +22,10 @@ struct __attribute__((packed)) FileStoreItem: public ptrarith::Item { ox::LittleEndian left = 0; ox::LittleEndian right = 0; - explicit FileStoreItem(size_t size): ptrarith::Item(size) { + FileStoreItem() = default; + + explicit FileStoreItem(size_t size) { + this->setSize(size); } /** @@ -35,6 +38,7 @@ struct __attribute__((packed)) FileStoreItem: public ptrarith::Item { ptrarith::Ptr data() { return ptrarith::Ptr(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); } + }; @@ -42,6 +46,7 @@ template class FileStoreTemplate: public FileStore { private: + using Item = FileStoreItem; using ItemPtr = typename ptrarith::NodeBuffer>::ItemPtr; using Buffer = ptrarith::NodeBuffer>; @@ -69,6 +74,8 @@ class FileStoreTemplate: public FileStore { Error read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size); + ValErr read(InodeId_t id); + /** * Reads the "file" at the given id. You are responsible for freeing * the data when done with it. @@ -304,6 +311,11 @@ int FileStoreTemplate::read(InodeId_t id, FsSize_t readStart, return 1; } +template +ValErr FileStoreTemplate::read(InodeId_t id) { + return reinterpret_cast(find(id).get()); +} + template typename FileStoreTemplate::StatInfo FileStoreTemplate::stat(InodeId_t id) { auto inode = find(id); diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp index f5d070e4..f63b1010 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.cpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp @@ -89,6 +89,13 @@ int PathIterator::next(char *pathOut, std::size_t pathOutSize) { return retval; } +/** + * @return 0 if no error + */ +int PathIterator::next(BString *fileName) { + return next(fileName->data(), fileName->cap()); +} + ValErr PathIterator::nextSize() const { std::size_t size = 0; Error retval = 1; diff --git a/deps/ox/src/ox/fs/filesystem/pathiterator.hpp b/deps/ox/src/ox/fs/filesystem/pathiterator.hpp index 8ca6523e..cb2e7861 100644 --- a/deps/ox/src/ox/fs/filesystem/pathiterator.hpp +++ b/deps/ox/src/ox/fs/filesystem/pathiterator.hpp @@ -8,11 +8,12 @@ #pragma once -#include -#include +#include namespace ox { +constexpr auto MaxFileNameLength = 255; + class PathIterator { private: const char *m_path = nullptr; @@ -39,6 +40,11 @@ class PathIterator { */ int next(char *pathOut, std::size_t pathOutSize); + /** + * @return 0 if no error + */ + int next(BString *fileName); + /** * @return 0 if no error */ diff --git a/deps/ox/src/ox/fs/filesystem2/directory.hpp b/deps/ox/src/ox/fs/filesystem2/directory.hpp index b285ac8c..dfaf14fc 100644 --- a/deps/ox/src/ox/fs/filesystem2/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem2/directory.hpp @@ -9,7 +9,7 @@ #pragma once #include -#include +#include #include #include @@ -19,12 +19,14 @@ template struct __attribute__((packed)) DirectoryEntry { // NodeBuffer fields - ox::LittleEndian prev = 0; - ox::LittleEndian next = 0; + LittleEndian prev = 0; + LittleEndian next = 0; // DirectoryEntry fields - ox::LittleEndian inode = 0; - BString<255> name; + LittleEndian inode = 0; + BString name; + + DirectoryEntry() = default; explicit DirectoryEntry(const char *name) { this->name = name; @@ -41,6 +43,10 @@ struct __attribute__((packed)) DirectoryEntry { return fullSize() - offsetof(DirectoryEntry, inode); } + void setSize(InodeId_t) { + // ignore set value + } + ptrarith::Ptr data() { return ptrarith::Ptr(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); } @@ -55,9 +61,10 @@ class Directory { using Buffer = ptrarith::NodeBuffer>; std::size_t m_size = 0; Buffer *m_buff = nullptr; + FileStore *m_fs = nullptr; public: - Directory(uint8_t *buff, std::size_t size); + Directory(fs::FileStore *fs, InodeId_t inode); /** * Initializes Directory. @@ -73,9 +80,10 @@ class Directory { }; template -Directory::Directory(uint8_t *buff, std::size_t size) { - m_size = size; - m_buff = reinterpret_cast(buff); +Directory::Directory(fs::FileStore *fs, InodeId_t) { + m_fs = fs; + //m_size = size; + //m_buff = reinterpret_cast(buff); } template @@ -88,16 +96,25 @@ Error Directory::init() noexcept { } template -Error Directory::write(PathIterator it, InodeId_t inode) noexcept { - if (it.hasNext()) { - return write(it + 1, inode); - } else { - auto current = find(it); - if (current.ok()) { - } else { +Error Directory::write(PathIterator path, InodeId_t inode) noexcept { + // find existing entry and update if exists + + if (!path.hasNext()) { + BString name; + path.next(&name); + + auto val = m_buff->malloc(name.size()); + if (val.valid()) { + new (val) DirectoryEntry(name.data()); + val->name = name; + val->inode = inode; + return 0; } + return 1; } - return 1; + + // TODO: get sub directory and call its write instead of recursing on this directory + return write(path + 1, inode); } template diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index d6646909..fa78312d 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -386,13 +386,16 @@ map tests = { { "Directory", [](string) { - std::array buff; - ox::fs::Directory32 dir(buff.data(), buff.size()); - dir.init(); - dir.write("/file1", 1); + //std::array fsBuff; + //std::array dirBuff; + //ox::fs::FileStore32 fileStore(fsBuff.data(), fsBuff.size()); + //fileStore.format(); + //ox::fs::Directory32 dir(&fileStore, dirBuff.data(), dirBuff.size()); + //dir.init(); + //dir.write("/file1", 1); //oxAssert(dir.find("/file1") == 1, "Could not find /file1"); - dir.write("/file3", 3); - dir.write("/file2", 2); + //dir.write("/file3", 3); + //dir.write("/file2", 2); return 0; } }, diff --git a/deps/ox/src/ox/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/ptrarith/nodebuffer.hpp index 8ce43138..920e1618 100644 --- a/deps/ox/src/ox/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/ptrarith/nodebuffer.hpp @@ -211,7 +211,8 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size } auto out = ItemPtr(this, m_header.size, addr, fullSize); if (out.valid()) { - new (out) Item(size); + new (out) Item; + out->setSize(size); auto first = firstItem(); out->next = first.offset(); @@ -329,12 +330,12 @@ struct __attribute__((packed)) Item { ox::LittleEndian m_size = sizeof(Item); public: - explicit Item(size_t size) { - this->m_size = size; + size_t size() const { + return m_size; } - virtual size_t size() const { - return m_size; + void setSize(size_t size) { + m_size = size; } }; diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index a036ed5c..15fb1782 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -18,7 +18,7 @@ namespace ox { template class BString { private: - uint8_t m_buff[buffLen]; + uint8_t m_buff[buffLen + 1]; public: BString() noexcept; diff --git a/deps/ox/src/ox/std/types.hpp b/deps/ox/src/ox/std/types.hpp index 301fff2c..0bff8d03 100644 --- a/deps/ox/src/ox/std/types.hpp +++ b/deps/ox/src/ox/std/types.hpp @@ -61,9 +61,7 @@ struct ValErr { inline constexpr ValErr() = default; - inline constexpr ValErr(T value, Error error = 0) { - this->value = value; - this->error = error; + inline constexpr ValErr(T value, Error error = 0): value(value), error(error) { } inline constexpr operator const T&() const {