From 361ee79c414a093827b845d35475fb1e266c7348 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 8 Apr 2017 18:50:24 -0500 Subject: [PATCH] Fix write-expand to factor in the space needed for the inode header --- CMakeLists.txt | 2 +- src/ox/fs/filestore.hpp | 18 ++++++++++++++++++ src/ox/fs/filesystem.hpp | 9 +++++++++ src/ox/fs/oxfstool.cpp | 4 ++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d093272fb..67c55d7b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ endif() if(NOT MSVC) add_definitions( - -std=c++11 + -std=c++14 -Wall -nostdlib -fno-exceptions diff --git a/src/ox/fs/filestore.hpp b/src/ox/fs/filestore.hpp index dd82a4f0c..5b0fc32b0 100644 --- a/src/ox/fs/filestore.hpp +++ b/src/ox/fs/filestore.hpp @@ -94,6 +94,14 @@ class FileStore { */ StatInfo stat(InodeId_t id); + /** + * Returns the space needed for this data at the given inode address. + * @param id the target inode id + * @param size the size of the data to insert + * @return the space currently available in this file store. + */ + FsSize_t spaceNeeded(InodeId_t id, FsSize_t size); + /** * Returns the size of the file store. * @return the size of the file store. @@ -396,6 +404,16 @@ typename FileStore::StatInfo FileStore::stat(InodeId_t id) { return stat; } +template +FsSize_t FileStore::spaceNeeded(InodeId_t id, FsSize_t size) { + FsSize_t needed = sizeof(Inode) + size;; + auto inode = getInode(ptr(m_rootInode), id); + if (inode) { + needed -= inode->size(); + } + return needed; +} + template FsSize_t FileStore::size() { return m_size; diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 2f676a708..8cd1f135d 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -46,6 +46,8 @@ class FileSystem { virtual FileStat stat(uint64_t inode) = 0; + virtual uint64_t spaceNeeded(uint64_t id, uint64_t size) = 0; + virtual uint64_t available() = 0; virtual uint64_t size() = 0; @@ -109,6 +111,8 @@ class FileSystemTemplate: public FileSystem { FileStat stat(uint64_t inode) override; + uint64_t spaceNeeded(uint64_t id, uint64_t size) override; + uint64_t available() override; uint64_t size() override; @@ -214,6 +218,11 @@ void FileSystemTemplate::resize(uint64_t size) { return store->resize(size); } +template +uint64_t FileSystemTemplate::spaceNeeded(uint64_t id, uint64_t size) { + return store->spaceNeeded(id, size); +} + template uint64_t FileSystemTemplate::available() { return store->available(); diff --git a/src/ox/fs/oxfstool.cpp b/src/ox/fs/oxfstool.cpp index e533f7df3..972efa39e 100644 --- a/src/ox/fs/oxfstool.cpp +++ b/src/ox/fs/oxfstool.cpp @@ -20,7 +20,7 @@ using namespace ox::fs; using namespace std; -const static auto oxfstoolVersion = "1.1.0"; +const static auto oxfstoolVersion = "1.1.1"; const static auto usage = "usage:\n" "\toxfs format [16,32,64] \n" "\toxfs read \n" @@ -198,7 +198,7 @@ int write(int argc, char **args, bool expand) { auto fs = createFileSystem(fsBuff); if (fs) { if (expand && fs->available() <= srcSize) { - auto needed = (fs->size() - fs->available()) + fsSize; + auto needed = fs->spaceNeeded(inode, srcSize); auto cloneBuff = new uint8_t[needed]; ox_memcpy(cloneBuff, fsBuff, fsSize);