diff --git a/src/ox/fs/filesystem.cpp b/src/ox/fs/filesystem.cpp index b0e5de159..280a46247 100644 --- a/src/ox/fs/filesystem.cpp +++ b/src/ox/fs/filesystem.cpp @@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include #include "filesystem.hpp" namespace ox { @@ -36,5 +37,34 @@ FileSystem *createFileSystem(void *buff) { return fs; } +FileSystem *expandCopy(FileSystem *fs, size_t size) { + auto fsBuff = fs->buff(); + FileSystem *retval = nullptr; + + if (fs->size() <= size) { + auto cloneBuff = new uint8_t[size]; + ox_memcpy(cloneBuff, fsBuff, fs->size()); + + fsBuff = cloneBuff; + retval = createFileSystem(fsBuff); + retval->resize(size); + } + + return retval; +} + +FileSystem *expandCopyCleanup(FileSystem *fs, size_t size) { + auto out = expandCopy(fs, size); + + if (out) { + delete fs->buff(); + delete fs; + } else { + out = fs; + } + + return out; +} + } } diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 8cd1f135d..cac8f7c4e 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -51,10 +51,23 @@ class FileSystem { virtual uint64_t available() = 0; virtual uint64_t size() = 0; + + virtual uint8_t *buff() = 0; }; FileSystem *createFileSystem(void *buff); +/** + * Creates a larger version of the given FileSystem. + */ +FileSystem *expandCopy(FileSystem *src); + +/** + * Calls expandCopy and deletes the original FileSystem and buff a resize was + * performed. + */ +FileSystem *expandCopyCleanup(FileSystem *fs, size_t size); + template class FileSystemTemplate: public FileSystem { @@ -117,6 +130,8 @@ class FileSystemTemplate: public FileSystem { uint64_t size() override; + uint8_t *buff() override; + static uint8_t *format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories); }; @@ -233,6 +248,11 @@ uint64_t FileSystemTemplate::size() { return store->size(); } +template +uint8_t *FileSystemTemplate::buff() { + return (uint8_t*) store; +} + #ifdef _MSC_VER #pragma warning(disable:4244) #endif diff --git a/src/ox/fs/oxfstool.cpp b/src/ox/fs/oxfstool.cpp index 273958e17..7078f0db7 100644 --- a/src/ox/fs/oxfstool.cpp +++ b/src/ox/fs/oxfstool.cpp @@ -35,7 +35,7 @@ char *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) { fseek(file, 0, SEEK_END); const auto size = ftell(file); rewind(file); - auto buff = (char*) malloc(size); + auto buff = new char[size]; auto itemsRead = fread(buff, size, 1, file); fclose(file); if (sizeOut) { @@ -87,7 +87,7 @@ int format(int argc, char **args) { auto type = ox_atoi(args[2]); auto size = bytes(args[3]); auto path = args[4]; - auto buff = (uint8_t*) malloc(size); + auto buff = new uint8_t[size]; if (size < sizeof(FileStore64)) { @@ -125,7 +125,7 @@ int format(int argc, char **args) { } } - free(buff); + delete []buff; if (err == 0) { fprintf(stderr, "Created file system %s\n", path); @@ -160,7 +160,7 @@ int read(int argc, char **args) { } delete fs; - free(fsBuff); + delete []fsBuff; } else { fprintf(stderr, "Invalid file system type: %d.\n", *(uint32_t*) fsBuff); } @@ -199,16 +199,8 @@ int write(int argc, char **args, bool expand) { if (fs) { if (expand && fs->available() <= srcSize) { auto needed = fs->size() + fs->spaceNeeded(inode, srcSize); - auto cloneBuff = new uint8_t[needed]; - ox_memcpy(cloneBuff, fsBuff, fsSize); - - delete fs; - delete []fsBuff; - - fsBuff = cloneBuff; - fs = createFileSystem(fsBuff); fsSize = needed; - fs->resize(fsSize); + fs = expandCopyCleanup(fs, needed); } err |= fs->write(inode, srcBuff, srcSize); @@ -239,7 +231,7 @@ int write(int argc, char **args, bool expand) { err = 1; } } - free(srcBuff); + delete []fsBuff; } else { err = 1; fprintf(stderr, "Could not load source file: %s.\n", srcPath); @@ -285,7 +277,7 @@ int compact(int argc, char **args) { } delete fs; - free(fsBuff); + delete []fsBuff; } else { fprintf(stderr, "Could not open file: %s\n", fsPath); } @@ -328,7 +320,7 @@ int remove(int argc, char **args) { } delete fs; - free(fsBuff); + delete []fsBuff; } else { fprintf(stderr, "Could not open file: %s\n", fsPath); }