diff --git a/Makefile b/Makefile index 39060309..3c2b781f 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,9 @@ HOST_ENV=${OS}-$(shell uname -m) DEVENV=devenv$(shell pwd | sed 's/\//-/g') DEVENV_IMAGE=wombatant/devenv ifneq ($(shell which gmake),) - MAKE=gmake + MAKE=gmake -s else - MAKE=make + MAKE=make -s endif ifneq ($(shell which docker 2>&1),) ifeq ($(shell docker inspect --format="{{.State.Status}}" ${DEVENV} 2>&1),running) diff --git a/src/ox/fs/filesystem.cpp b/src/ox/fs/filesystem.cpp index 1f649c92..ceeaa5fb 100644 --- a/src/ox/fs/filesystem.cpp +++ b/src/ox/fs/filesystem.cpp @@ -10,7 +10,7 @@ namespace ox { -FileSystem *createFileSystem(void *buff, size_t buffSize) { +FileSystem *createFileSystem(uint8_t *buff, size_t buffSize, bool ownsBuff) { auto version = ((FileStore16*) buff)->version(); auto type = ((FileStore16*) buff)->fsType(); FileSystem *fs = nullptr; @@ -19,13 +19,13 @@ FileSystem *createFileSystem(void *buff, size_t buffSize) { case 5: switch (type) { case ox::OxFS_16: - fs = new FileSystem16(buff); + fs = new FileSystem16(buff, ownsBuff); break; case ox::OxFS_32: - fs = new FileSystem32(buff); + fs = new FileSystem32(buff, ownsBuff); break; case ox::OxFS_64: - fs = new FileSystem64(buff); + fs = new FileSystem64(buff, ownsBuff); break; } break; diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 3c0df7a1..0854456e 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -35,6 +35,8 @@ struct DirectoryListing { String name; FileStat stat; + DirectoryListing() = default; + DirectoryListing(const char *name) { this->name = name; } @@ -155,18 +157,18 @@ int Directory::rmFile(const char *name) { template int Directory::copy(Directory *dirOut) { auto current = files(); - auto dirBuff = (uint8_t*) dirOut; - dirBuff += sizeof(Directory); + auto dirOutBuff = (uint8_t*) dirOut; + dirOutBuff += sizeof(Directory); dirOut->size = this->size; dirOut->children = this->children; if (current) { for (uint64_t i = 0; i < this->children; i++) { - auto entry = (DirectoryEntry*) dirBuff; + auto entry = (DirectoryEntry*) dirOutBuff; entry->inode = current->inode; entry->setName(current->getName()); current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - dirBuff += entry->size(); + dirOutBuff += entry->size(); } return 0; } else { @@ -181,7 +183,7 @@ int Directory::ls(List *list) { if (current) { for (uint64_t i = 0; i < this->children; i++) { list->push_back(current->getName()); - list->at(i).stat.inode = current->inode; + (*list)[i].stat.inode = current->inode; current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); } return 0; @@ -242,11 +244,11 @@ int FileSystem::ls(const char *path, List *list) { uint8_t dirBuff[s.size * 4]; auto dir = (Directory*) dirBuff; auto err = readDirectory(path, dir); - dir->ls(list); + err |= dir->ls(list); return err; } -FileSystem *createFileSystem(void *buff, size_t buffSize); +FileSystem *createFileSystem(uint8_t *buff, size_t buffSize, bool ownsBuff = false); /** * Creates a larger version of the given FileSystem. @@ -264,6 +266,7 @@ class FileSystemTemplate: public FileSystem { private: FileStore *m_store = nullptr; + bool m_ownsBuff = false; public: // static members @@ -271,13 +274,12 @@ class FileSystemTemplate: public FileSystem { static typename FileStore::InodeId_t INODE_ROOT_DIR; static typename FileStore::InodeId_t INODE_RESERVED_END; - explicit FileSystemTemplate(void *buff); + explicit FileSystemTemplate(uint8_t *buff, bool ownsBuff = false); + + ~FileSystemTemplate(); int stripDirectories() override; - template - int ls(const char *path, List *list); - int mkdir(const char *path) override; int read(const char *path, void *buffer, size_t buffSize) override; @@ -324,7 +326,7 @@ class FileSystemTemplate: public FileSystem { */ int rmDirectoryEntry(const char *path); - static uint8_t *format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories); + static uint8_t *format(uint8_t *buffer, typename FileStore::FsSize_t size, bool useDirectories); protected: int readDirectory(const char *path, Directory *dirOut) override; @@ -333,11 +335,21 @@ class FileSystemTemplate: public FileSystem { uint64_t generateInodeId(); int insertDirectoryEntry(const char *dirPath, const char *fileName, uint64_t inode); + + void expand(uint64_t size); }; template -FileSystemTemplate::FileSystemTemplate(void *buff) { +FileSystemTemplate::FileSystemTemplate(uint8_t *buff, bool ownsBuff) { m_store = (FileStore*) buff; + m_ownsBuff = ownsBuff; +} + +template +FileSystemTemplate::~FileSystemTemplate() { + if (m_ownsBuff) { + delete[] (uint8_t*) m_store; + } } template @@ -354,30 +366,6 @@ int FileSystemTemplate::stripDirectories() { return m_store->removeAllType(FileType::FileType_Directory); } -template -template -int FileSystemTemplate::ls(const char *path, List *list) { - int err = 0; - auto inode = findInodeOf(path); - auto dirStat = stat(inode); - auto dirBuffLen = dirStat.size; - uint8_t dirBuff[dirBuffLen]; - auto dir = (Directory*) dirBuff; - - err = read(dirStat.inode, dirBuff, dirBuffLen); - if (!err) { - dir->ls(list); - - for (auto &i : *list) { - i.stat = stat(i.stat.inode); - } - - return 0; - } else { - return 1; - } -} - template int FileSystemTemplate::mkdir(const char *path) { Directory dir; @@ -572,6 +560,11 @@ int FileSystemTemplate::write(const char *path, void *buffer #endif template int FileSystemTemplate::write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType) { + if (m_ownsBuff) { + while (m_store->spaceNeeded(size) > m_store->available()) { + expand(this->size() * 2); + } + } return m_store->write(inode, buffer, size, fileType); } #ifdef _MSC_VER @@ -643,12 +636,12 @@ uint8_t *FileSystemTemplate::buff() { #pragma warning(disable:4244) #endif template -uint8_t *FileSystemTemplate::format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories) { - buffer = FileStore::format((uint8_t*) buffer, size, (uint16_t) FS_TYPE); +uint8_t *FileSystemTemplate::format(uint8_t *buffer, typename FileStore::FsSize_t size, bool useDirectories) { + buffer = FileStore::format(buffer, size, (uint16_t) FS_TYPE); if (buffer && useDirectories) { Directory dir; - FileSystemTemplate fs(buffer); + FileSystemTemplate fs((uint8_t*) buffer); fs.write(INODE_ROOT_DIR, &dir, sizeof(dir), FileType::FileType_Directory); } @@ -700,7 +693,7 @@ int FileSystemTemplate::insertDirectoryEntry(const char *dir auto entry = (DirectoryEntry*) &dirBuff[s.size]; entry->inode = inode; entry->setName(fileName); - return write(s.inode, dirBuff, dirBuffSize, FileType::FileType_Directory); + return write(s.inode, dirBuff, dirBuffSize, FileType_Directory); } else { return 1; } @@ -795,7 +788,7 @@ int FileSystemTemplate::readDirectory(const char *path, Dire auto dirStat = stat(inode); auto dirBuffLen = dirStat.size; uint8_t dirBuff[dirBuffLen]; - auto dir = (Directory*) dirBuff; + auto dir = (Directory*) dirBuff; err = read(dirStat.inode, dirBuff, dirBuffLen); if (!err) { @@ -805,6 +798,16 @@ int FileSystemTemplate::readDirectory(const char *path, Dire } } +template +void FileSystemTemplate::expand(uint64_t newSize) { + if (newSize > size()) { + auto newBuff = new uint8_t[newSize]; + ox_memcpy(newBuff, m_store, m_store->size()); + delete[] m_store; + m_store = (FileStore*) newBuff; + resize(newSize); + } +} typedef FileSystemTemplate FileSystem16; typedef FileSystemTemplate FileSystem32; diff --git a/src/ox/fs/oxfstool.cpp b/src/ox/fs/oxfstool.cpp index fd40e82a..ee241245 100644 --- a/src/ox/fs/oxfstool.cpp +++ b/src/ox/fs/oxfstool.cpp @@ -30,12 +30,12 @@ const static auto usage = "usage:\n" "\toxfs compact \n" "\toxfs version\n"; -char *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) { +uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) { if (file) { fseek(file, 0, SEEK_END); const auto size = ftell(file); rewind(file); - auto buff = new char[size]; + auto buff = new uint8_t[size]; auto itemsRead = fread(buff, size, 1, file); fclose(file); if (sizeOut) { @@ -47,7 +47,7 @@ char *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) { } } -char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) { +uint8_t *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) { return loadFileBuff(fopen(path, "rb"), sizeOut); }