diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 83cff48b..c3afc3ba 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -35,8 +35,8 @@ struct __attribute__((packed)) FileStoreItem: public ptrarith::Item { return sizeof(*this) + this->size(); } - ptrarith::Ptr data() { - return ptrarith::Ptr(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); + ptrarith::Ptr data() { + return ptrarith::Ptr(this, this->fullSize(), sizeof(*this), this->size()); } }; @@ -74,7 +74,7 @@ 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); + const ptrarith::Ptr read(InodeId_t id); /** * Reads the "file" at the given id. You are responsible for freeing @@ -100,6 +100,8 @@ class FileStoreTemplate: public FileStore { InodeId_t available(); private: + void compact(); + FileStoreData *fileStoreData(); /** @@ -201,14 +203,20 @@ Error FileStoreTemplate::write(InodeId_t id, void *data, FsSize_t dataSi // write the given data auto dest = m_buffer->malloc(dataSize); // if first malloc failed, compact and try again - dest = m_buffer->malloc(dataSize); + if (!dest.valid()) { + compact(); + dest = m_buffer->malloc(dataSize); + } if (dest.valid()) { new (dest) FileStoreItem(dataSize); dest->id = id; dest->fileType = fileType; auto destData = m_buffer->template dataOf(dest); if (destData.valid()) { - ox_memcpy(destData, data, dest->size()); + // write data if any was provided + if (data != nullptr) { + ox_memcpy(destData, data, dest->size()); + } auto fsData = fileStoreData(); if (fsData) { auto root = m_buffer->ptr(fsData->rootNode); @@ -312,8 +320,13 @@ int FileStoreTemplate::read(InodeId_t id, FsSize_t readStart, } template -ValErr FileStoreTemplate::read(InodeId_t id) { - return reinterpret_cast(find(id).get()); +const ptrarith::Ptr FileStoreTemplate::read(InodeId_t id) { + auto item = find(id); + if (item.valid()) { + return item->data(); + } else { + return nullptr; + } } template @@ -345,6 +358,10 @@ InodeId_t FileStoreTemplate::available() { return m_buffer->available(); } +template +void FileStoreTemplate::compact() { +} + template typename FileStoreTemplate::FileStoreData *FileStoreTemplate::fileStoreData() { auto first = m_buffer->firstItem();