diff --git a/deps/ox/src/ox/fs/filestore/filestore.hpp b/deps/ox/src/ox/fs/filestore/filestore.hpp index 946f78dd..45f2ee53 100644 --- a/deps/ox/src/ox/fs/filestore/filestore.hpp +++ b/deps/ox/src/ox/fs/filestore/filestore.hpp @@ -12,16 +12,16 @@ namespace ox::fs { -using InodeId_t = uintptr_t; -using FsSize_t = uintptr_t; +using InodeId_t = uint64_t; +using FsSize_t = std::size_t; class FileStore { public: struct StatInfo { - InodeId_t inodeId = 0; + InodeId_t inode = 0; InodeId_t links = 0; - InodeId_t size = 0; + FsSize_t size = 0; uint8_t fileType = 0; }; @@ -40,7 +40,7 @@ class FileStore { */ virtual Error remove(InodeId_t id) = 0; - virtual Error read(InodeId_t id, void *data, FsSize_t dataSize, FsSize_t *size = nullptr) = 0; + virtual Error read(InodeId_t id, void *data, FsSize_t dataSize, std::size_t *size = nullptr) = 0; virtual Error read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size = nullptr) = 0; @@ -59,6 +59,14 @@ class FileStore { virtual InodeId_t available() = 0; + /** + * @return a pointer to the buffer of the file system, or null if not + * applicable + */ + virtual uint8_t *buff() = 0; + + virtual Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) = 0; + virtual ValErr generateInodeId() = 0; }; diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 6ad970d0..d022e559 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -63,23 +63,23 @@ class FileStoreTemplate: public FileStore { public: FileStoreTemplate(void *buff, size_t buffSize); - Error format(); + Error format() override; - Error setSize(InodeId_t buffSize); + Error setSize(InodeId_t buffSize) override; - Error incLinks(InodeId_t id); + Error incLinks(InodeId_t id) override; - Error decLinks(InodeId_t id); + Error decLinks(InodeId_t id) override; - Error write(InodeId_t id, void *data, FsSize_t dataLen, uint8_t fileType = 0); + Error write(InodeId_t id, void *data, FsSize_t dataLen, uint8_t fileType = 0) override; - Error remove(InodeId_t id); + Error remove(InodeId_t id) override; - Error read(InodeId_t id, void *data, FsSize_t dataSize, FsSize_t *size); + Error read(InodeId_t id, void *data, FsSize_t dataSize, FsSize_t *size) override; - Error read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size); + Error read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size) override; - const ptrarith::Ptr read(InodeId_t id); + const ptrarith::Ptr read(InodeId_t id) override; /** * Reads the "file" at the given id. You are responsible for freeing @@ -96,17 +96,21 @@ class FileStoreTemplate: public FileStore { FsSize_t readSize, T *data, FsSize_t *size); - ValErr stat(InodeId_t id); + ValErr stat(InodeId_t id) override; - Error resize(std::size_t size, void *newBuff = nullptr); + Error resize(std::size_t size, void *newBuff = nullptr) override; - InodeId_t spaceNeeded(FsSize_t size); + InodeId_t spaceNeeded(FsSize_t size) override; - InodeId_t size(); + InodeId_t size() override; - InodeId_t available(); + InodeId_t available() override; - ValErr generateInodeId(); + uint8_t *buff() override; + + Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) override; + + ValErr generateInodeId() override; private: void compact(); @@ -376,6 +380,10 @@ const ptrarith::Ptr FileStoreTemplate::read(InodeI template Error FileStoreTemplate::resize(std::size_t size, void *newBuff) { + m_buffSize = size; + if (newBuff) { + m_buffer = reinterpret_cast(newBuff); + } return OxError(0); } @@ -384,7 +392,7 @@ ValErr::StatInfo> FileStoreTemplate:: auto inode = find(id); if (inode.valid()) { return ValErr({ - .inodeId = id, + .inode = id, .links = inode->links, .size = inode->size(), .fileType = inode->fileType, @@ -408,6 +416,19 @@ InodeId_t FileStoreTemplate::available() { return m_buffer->available(); } +template +uint8_t *FileStoreTemplate::buff() { + return reinterpret_cast(m_buffer); +} + +template +Error FileStoreTemplate::walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) { + for (auto i = m_buffer->iterator(); i.valid(); i.next()) { + oxReturnError(cb(i->fileType, i.ptr().offset(), i.ptr().end())); + } + return OxError(0); +} + template ValErr FileStoreTemplate::generateInodeId() { auto fsData = fileStoreData(); diff --git a/deps/ox/src/ox/fs/filesystem/types.hpp b/deps/ox/src/ox/fs/filesystem/types.hpp index 931efe43..c6bab0b6 100644 --- a/deps/ox/src/ox/fs/filesystem/types.hpp +++ b/deps/ox/src/ox/fs/filesystem/types.hpp @@ -30,4 +30,4 @@ struct FileStat { uint8_t fileType = 0; }; -} \ No newline at end of file +} diff --git a/deps/ox/src/ox/fs/filesystem2/filesystem.hpp b/deps/ox/src/ox/fs/filesystem2/filesystem.hpp index 7a615b50..8219e58e 100644 --- a/deps/ox/src/ox/fs/filesystem2/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem2/filesystem.hpp @@ -50,15 +50,15 @@ class FileSystemTemplate { Error remove(const char *path, bool recursive = false); - void resize(uint64_t size = 0); + void resize(uint64_t size, void *buffer = nullptr); Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile); Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile); - FileStat stat(uint64_t inode); + ValErr stat(uint64_t inode); - FileStat stat(const char *path); + ValErr stat(const char *path); uint64_t spaceNeeded(uint64_t size); @@ -68,13 +68,18 @@ class FileSystemTemplate { uint8_t *buff(); - void walk(int(*cb)(const char*, uint64_t, uint64_t)); + Error walk(Error(*cb)(uint8_t, uint64_t, uint64_t)); bool valid() const; private: ValErr fileSystemData(); + /** + * Finds the inode ID at the given path. + */ + ValErr find(const char *path); + }; template @@ -122,29 +127,23 @@ Error FileSystemTemplate::mkdir(const char *path, bool recursive) { template Error FileSystemTemplate::move(const char *src, const char *dest) { auto fd = fileSystemData(); - if (fd.ok()) { - auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); - auto inode = rootDir->find(src); - oxReturnError(inode.error); - oxReturnError(rootDir->write(dest, inode)); - oxReturnError(rootDir->remove(src)); - return OxError(0); - } else { - return fd.error; - } + oxReturnError(fd.error); + auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); + auto inode = rootDir->find(src); + oxReturnError(inode.error); + oxReturnError(rootDir->write(dest, inode)); + oxReturnError(rootDir->remove(src)); + return OxError(0); } template Error FileSystemTemplate::read(const char *path, void *buffer, std::size_t buffSize) { auto fd = fileSystemData(); - if (fd.ok()) { - auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); - auto inode = rootDir->find(path); - oxReturnError(inode.error); - return read(inode, buffer, buffSize); - } else { - return fd.error; - } + oxReturnError(fd.error); + auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); + auto inode = rootDir->find(path); + oxReturnError(inode.error); + return read(inode, buffer, buffSize); } template @@ -160,67 +159,79 @@ Error FileSystemTemplate::read(uint64_t inode, std::size_t readStart, template Error FileSystemTemplate::remove(const char *path, bool recursive) { auto fd = fileSystemData(); - if (fd.ok()) { - auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); - auto inode = rootDir->find(path); - oxReturnError(inode.error); - if (auto err = rootDir->remove(path)) { - // removal failed, try putting the index back - oxLogError(rootDir->write(path, inode)); - return err; - } - return OxError(0); - } else { - return fd.error; + oxReturnError(fd.error); + auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); + auto inode = rootDir->find(path); + oxReturnError(inode.error); + if (auto err = rootDir->remove(path)) { + // removal failed, try putting the index back + oxLogError(rootDir->write(path, inode)); + return err; } + return OxError(0); } template -void FileSystemTemplate::resize(uint64_t size) { +void FileSystemTemplate::resize(uint64_t size, void *buffer) { + m_fs->resize(size, buffer); } template Error FileSystemTemplate::write(const char *path, void *buffer, uint64_t size, uint8_t fileType) { - return OxError(0); + auto inode = find(path); + oxReturnError(inode.error); + return write(inode, buffer, size, fileType); } template Error FileSystemTemplate::write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType) { - return OxError(0); + // TODO: directory insert + return m_fs->write(inode, buffer, size, fileType); } template -FileStat FileSystemTemplate::stat(uint64_t inode) { - return {}; +ValErr FileSystemTemplate::stat(uint64_t inode) { + auto s = m_fs->stat(inode); + FileStat out; + out.inode = s.value.inode; + out.links = s.value.links; + out.size = s.value.size; + out.fileType = s.value.fileType; + return {out, s.error}; } template -FileStat FileSystemTemplate::stat(const char *path) { - return {}; +ValErr FileSystemTemplate::stat(const char *path) { + auto inode = find(path); + if (inode.error) { + return {{}, inode.error}; + } + return stat(inode.value); } template uint64_t FileSystemTemplate::spaceNeeded(uint64_t size) { - return 0; + return m_fs->spaceNeeded(size); } template uint64_t FileSystemTemplate::available() { - return 0; + return m_fs->available(); } template uint64_t FileSystemTemplate::size() { - return 0; + return m_fs->size(); } template uint8_t *FileSystemTemplate::buff() { - return nullptr; + return m_fs->buff(); } template -void FileSystemTemplate::walk(int(*cb)(const char*, uint64_t, uint64_t)) { +Error FileSystemTemplate::walk(Error(*cb)(uint8_t, uint64_t, uint64_t)) { + return m_fs->walk(cb); } template @@ -233,6 +244,16 @@ ValErr::FileSystemData> FileSystemTemplat return fd; } +template +ValErr FileSystemTemplate::find(const char *path) { + auto fd = fileSystemData(); + oxReturnError(fd.error); + auto rootDir = ox_malloca(sizeof(ox::fs::Directory), ox::fs::Directory, m_fs, fd.value.rootDirInode); + auto inode = rootDir->find(path); + oxReturnError(inode.error); + return inode.value; +} + extern template class Directory; extern template class Directory; diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index db32f1d0..ea10833d 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -416,7 +416,8 @@ map tests = { oxAssert(fs.format(), "FileSystem format failed"); oxTrace("ox::fs::test::FileSystem") << "mkdir"; - oxAssert(fs.mkdir("/l1d1", true) == 0, "mkdir failed"); + oxAssert(fs.mkdir("/l1d1/l2d1/l3d1", true), "mkdir failed"); + oxAssert(fs.mkdir("/l1d1/l2d2", true), "mkdir failed"); return 0; } diff --git a/deps/ox/src/ox/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/ptrarith/nodebuffer.hpp index 6510412f..7621a76d 100644 --- a/deps/ox/src/ox/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/ptrarith/nodebuffer.hpp @@ -43,6 +43,10 @@ class __attribute__((packed)) NodeBuffer { return m_current; } + ItemPtr ptr() { + return m_current; + } + Item *get() { return m_current; }