diff --git a/deps/ox/src/ox/fs/filesystem2/directory.hpp b/deps/ox/src/ox/fs/filesystem2/directory.hpp index 17cd29c2..9f9f16ad 100644 --- a/deps/ox/src/ox/fs/filesystem2/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem2/directory.hpp @@ -94,6 +94,8 @@ class Directory { FileStore *m_fs = nullptr; public: + Directory() = default; + Directory(FileStore *fs, InodeId_t inode); /** diff --git a/deps/ox/src/ox/fs/filesystem2/filesystem.hpp b/deps/ox/src/ox/fs/filesystem2/filesystem.hpp index ec6bc28d..7cf6873e 100644 --- a/deps/ox/src/ox/fs/filesystem2/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem2/filesystem.hpp @@ -15,6 +15,12 @@ namespace ox::fs { +/** + * FileSystemTemplate used to create file system that wraps around a FileStore, + * taking an inode size and a directory type as parameters. + * + * Note: Directory parameter must have a default constructor. + */ template class FileSystemTemplate { private: @@ -73,12 +79,14 @@ class FileSystemTemplate { bool valid() const; private: - ValErr fileSystemData(); + ValErr fileSystemData() const noexcept; /** * Finds the inode ID at the given path. */ - ValErr find(const char *path); + ValErr find(const char *path) const noexcept; + + ValErr rootDir() const noexcept; }; @@ -186,13 +194,18 @@ void FileSystemTemplate::resize(uint64_t size, void *buffe template Error FileSystemTemplate::write(const char *path, void *buffer, uint64_t size, uint8_t fileType) { auto inode = find(path); - oxReturnError(inode.error); - return write(inode, buffer, size, fileType); + if (inode.error) { + inode = m_fs->generateInodeId(); + } + auto rootDir = this->rootDir(); + oxReturnError(rootDir.error); + oxReturnError(rootDir.value.write(path, inode)); + oxReturnError(write(inode, buffer, size, fileType)); + return 0; } template Error FileSystemTemplate::write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType) { - // TODO: directory insert return m_fs->write(inode, buffer, size, fileType); } @@ -242,7 +255,7 @@ Error FileSystemTemplate::walk(Error(*cb)(uint8_t, uint64_ } template -ValErr::FileSystemData> FileSystemTemplate::fileSystemData() { +ValErr::FileSystemData> FileSystemTemplate::fileSystemData() const noexcept { FileSystemData fd; auto err = m_fs->read(InodeFsData, &fd, sizeof(fd)); if (err != 0) { @@ -252,15 +265,28 @@ ValErr::FileSystemData> FileSy } template -ValErr FileSystemTemplate::find(const char *path) { +ValErr FileSystemTemplate::find(const char *path) const noexcept { auto fd = fileSystemData(); - oxReturnError(fd.error); + if (fd.error) { + return {0, fd.error}; + } Directory rootDir(m_fs, fd.value.rootDirInode); auto inode = rootDir.find(path); - oxReturnError(inode.error); + if (inode.error) { + return {0, inode.error}; + } return inode.value; } +template +ValErr FileSystemTemplate::rootDir() const noexcept { + auto fd = fileSystemData(); + if (fd.error) { + return {{}, fd.error}; + } + return Directory(m_fs, fd.value.rootDirInode); +} + extern template class Directory; extern template class Directory;