From 423f575386e8bf84cdf513e63828545f46f84033 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 4 May 2018 00:21:50 -0500 Subject: [PATCH] [ox/fs] Cleanup --- deps/ox/src/ox/fs/filesystem2/directory.hpp | 30 ++++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem2/directory.hpp b/deps/ox/src/ox/fs/filesystem2/directory.hpp index 868108f8..74fed0c6 100644 --- a/deps/ox/src/ox/fs/filesystem2/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem2/directory.hpp @@ -21,7 +21,7 @@ struct __attribute__((packed)) DirectoryEntry { struct __attribute__((packed)) DirectoryEntryData { // DirectoryEntry fields LittleEndian inode = 0; - BString name; + char name[MaxFileNameLength]; }; // NodeBuffer fields @@ -30,11 +30,11 @@ struct __attribute__((packed)) DirectoryEntry { DirectoryEntry() = default; - explicit DirectoryEntry(InodeId_t inode, const char *name) { + DirectoryEntry(InodeId_t inode, const char *name) { auto d = data(); if (d.valid()) { d->inode = inode; - d->name = name; + ox_strncpy(d->name, name, MaxFileNameLength); } } @@ -42,17 +42,13 @@ struct __attribute__((packed)) DirectoryEntry { return ptrarith::Ptr(this, this->fullSize(), sizeof(*this), this->size()); } - const ptrarith::Ptr data() const { - return ptrarith::Ptr(const_cast(this), this->fullSize(), sizeof(*this), this->size()); - } - /** * @return the size of the data + the size of the Item type */ InodeId_t fullSize() const { - auto d = data(); + const auto d = const_cast(this)->data(); if (d.valid()) { - return sizeof(*this) + offsetof(DirectoryEntryData, name) + d->name.size(); + return sizeof(*this) + offsetof(DirectoryEntryData, name) + ox_strnlen(d->name, MaxFileNameLength); } return 0; } @@ -84,7 +80,7 @@ class Directory { FileStore *m_fs = nullptr; public: - Directory(fs::FileStore *fs, InodeId_t inode); + Directory(FileStore *fs, InodeId_t inode); /** * Initializes Directory. @@ -100,7 +96,7 @@ class Directory { }; template -Directory::Directory(fs::FileStore *fs, InodeId_t id) { +Directory::Directory(FileStore *fs, InodeId_t id) { m_fs = fs; m_inodeId = id; auto buff = fs->read(id).template to(); @@ -136,11 +132,13 @@ Error Directory::write(PathIterator path, InodeId_t inode) noexcept { if (old.valid()) { const auto newSize = m_size + DirectoryEntry::spaceNeeded(name.size()); auto cpy = ox_malloca(newSize, Buffer, old); - cpy->setSize(newSize); - auto val = cpy->malloc(name.size()); - if (val.valid()) { - new (val) DirectoryEntry(inode, name.data()); - err = m_fs->write(m_inodeId, cpy, cpy->size()); + if (cpy != nullptr) { + cpy->setSize(newSize); + auto val = cpy->malloc(name.size()); + if (val.valid()) { + new (val) DirectoryEntry(inode, name.data()); + err = m_fs->write(m_inodeId, cpy, cpy->size()); + } } } return err;