From 080ca9df86f339e841cd447b32dadf4970ee8094 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 14 Mar 2018 00:39:08 -0500 Subject: [PATCH] Fix new FileStore to correctly initialize the first item --- .../src/ox/fs/filestore/filestoretemplate.hpp | 27 ++++++++++++------- deps/ox/src/ox/fs/filestore/nodebuffer.hpp | 26 +++++++++--------- deps/ox/src/ox/fs/test/tests.cpp | 6 ++--- deps/ox/src/ox/trace/trace.hpp | 2 +- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index b4d1bf96..a1d9bf4c 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -24,6 +24,13 @@ struct __attribute__((packed)) FileStoreItem: public Item { explicit FileStoreItem(size_t size): Item(size) { } + /** + * @return the size of the data + the size of the Item type + */ + size_t fullSize() const { + return sizeof(*this) + this->size(); + } + ox::fs::Ptr data() { return Ptr(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); } @@ -169,22 +176,24 @@ Error FileStoreTemplate::write(InodeId_t id, void *data, FsSize_t dataSi new (dest) FileStoreItem(dataSize); dest->id = id; dest->fileType = fileType; - ox_memcpy(dest->data(), data, dest->size()); - auto root = rootInode(); - if (root.valid()) { - return placeItem(root, dest); - } else { + auto destData = dest->data(); + if (destData.valid()) { + ox_memcpy(destData, data, dest->size()); auto fsData = fileStoreData(); if (fsData) { - fsData->rootNode = dest; - return 0; + auto root = m_buffer->ptr(fsData->rootNode); + if (root.valid()) { + return placeItem(root, dest); + } else { + fsData->rootNode = dest; + return 0; + } } else { - m_buffer->free(dest); oxTrace("ox::fs::FileStoreTemplate::write::fail") << "Could not place item due to absence of FileStore header."; - return 1; } } } + m_buffer->free(dest); } return 1; } diff --git a/deps/ox/src/ox/fs/filestore/nodebuffer.hpp b/deps/ox/src/ox/fs/filestore/nodebuffer.hpp index e3b2c2b9..81b89fd7 100644 --- a/deps/ox/src/ox/fs/filestore/nodebuffer.hpp +++ b/deps/ox/src/ox/fs/filestore/nodebuffer.hpp @@ -132,12 +132,21 @@ typename NodeBuffer::ItemPtr NodeBuffer::ptr(size_t template typename NodeBuffer::ItemPtr NodeBuffer::malloc(size_t size) { - size += sizeof(Item); - if (m_header.size - m_header.bytesUsed >= size) { - if (!m_header.firstItem) { - m_header.firstItem = sizeof(m_header); + auto fullSize = size + sizeof(Item); + if (m_header.size - m_header.bytesUsed >= fullSize) { + auto last = lastItem(); + size_t addr = 0; + if (last.valid()) { + addr = last.offset() + last.size(); + } else { + // there is no first item, so this may be the first item + if (!m_header.firstItem) { + oxTrace("ox::fs::NodeBuffer::malloc") << "No first item."; + m_header.firstItem = sizeof(m_header); + addr = m_header.firstItem; + } } - auto out = ItemPtr(this, m_header.size, m_header.firstItem, size); + auto out = ItemPtr(this, m_header.size, addr, fullSize); if (out.valid()) { new (out) Item(size); @@ -261,13 +270,6 @@ struct __attribute__((packed)) Item { size_t size() const { return m_size; } - - /** - * @return the size of the data + the size of the Item type - */ - size_t fullSize() const { - return sizeof(*this) + m_size; - } }; } diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index eb6f1fcd..eab37f5f 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -340,9 +340,9 @@ map tests = { uint8_t buff[buffLen]; auto list = new (buff) ox::fs::NodeBuffer>(buffLen); err |= !(list->malloc(50).valid()); - //auto first = list->firstItem(); - //err |= !(first.valid()); - //err |= !(first->size() == 50); + auto first = list->firstItem(); + ox_assert(first.valid(), "NodeBuffer::insert: Could not access first item"); + ox_assert(first->size() == 50, "NodeBuffer::insert: First item size invalid"); return err; } }, diff --git a/deps/ox/src/ox/trace/trace.hpp b/deps/ox/src/ox/trace/trace.hpp index e2e6f5dc..827c6665 100644 --- a/deps/ox/src/ox/trace/trace.hpp +++ b/deps/ox/src/ox/trace/trace.hpp @@ -93,4 +93,4 @@ class NullStream { } -#define oxTrace(ch) ox::trace::StdOutStream(__FILE__, __LINE__, ch) +#define oxTrace(ch) ox::trace::NullStream(__FILE__, __LINE__, ch)