From 1d600186cd6f877f9530a5c4bb8290518a866056 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 26 Nov 2019 23:18:38 -0600 Subject: [PATCH] [ox/fs] Cleanup, Fix FileLocation to make copy of non-const strings --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 4 +- deps/ox/src/ox/fs/oxfstool.cpp | 397 ------------------ deps/ox/src/ox/fs/toollib.cpp | 33 -- deps/ox/src/ox/fs/toollib.hpp | 14 - 4 files changed, 3 insertions(+), 445 deletions(-) delete mode 100644 deps/ox/src/ox/fs/oxfstool.cpp delete mode 100644 deps/ox/src/ox/fs/toollib.cpp delete mode 100644 deps/ox/src/ox/fs/toollib.hpp diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index e3d82d60..e39df0f5 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -28,7 +28,9 @@ FileAddress::FileAddress(uint64_t inode) { } FileAddress::FileAddress(char *path) { - m_data.path = path; + auto pathSize = ox_strlen(path) + 1; + m_data.path = new char[pathSize]; + memcpy(m_data.path, path, pathSize); m_type = FileAddressType::Path; } diff --git a/deps/ox/src/ox/fs/oxfstool.cpp b/deps/ox/src/ox/fs/oxfstool.cpp deleted file mode 100644 index c510a5f1..00000000 --- a/deps/ox/src/ox/fs/oxfstool.cpp +++ /dev/null @@ -1,397 +0,0 @@ -/* - * Copyright 2015 - 2018 gtalent2@gmail.com - * - * This Source Code Form is subject to the terms of the Mozilla Public - * 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 -#include -#include -#include -#include -#include -#include - -#include "toollib.hpp" - -// suppress warnings about using fopen -#ifdef _MSC_VER -#pragma warning(disable:4996) -#endif - -using namespace ox; -using namespace std; - -const static auto oxfstoolVersion = "1.4.0"; -const static auto usage = "usage:\n" -"\toxfs format [16,32,64] \n" -"\toxfs read \n" -"\toxfs write \n" -"\toxfs write-expand \n" -"\toxfs rm \n" -"\toxfs compact \n" -"\toxfs walk \n" -"\toxfs version\n"; - -size_t bytes(const char *str) { - auto size = ::ox_strlen(str); - const auto lastChar = str[size-1]; - auto multiplier = 1; - char copy[size + 1]; - ox_memcpy(copy, str, size + 1); - // parse size unit - if (lastChar < '0' || lastChar > '9') { - copy[size-1] = 0; - switch (lastChar) { - case 'k': - case 'K': - multiplier = 1024; - break; - case 'm': - case 'M': - multiplier = 1024 * 1024; - break; - case 'g': - case 'G': - multiplier = 1024 * 1024 * 1024; - break; - default: - multiplier = -1; - } - } - return ox_atoi(copy) * multiplier; -} - -int format(int argc, char **args) { - printf("Creating file system...\n"); - auto err = 0; - if (argc >= 5) { - auto type = ox_atoi(args[2]); - auto size = bytes(args[3]); - auto path = args[4]; - auto buff = new uint8_t[size]; - - - if (size < sizeof(FileStore64)) { - err = 1; - cerr << "File system size " << size << " too small, must be at least " << sizeof(FileStore64) << endl; - } - - if (!err) { - // format - switch (type) { - case 16: - FileSystem16::format(buff, (FileStore16::FsSize_t) size, true); - break; - case 32: - FileSystem32::format(buff, (FileStore32::FsSize_t) size, true); - break; - case 64: - FileSystem64::format(buff, size, true); - break; - default: - err = 1; - } - - if (!err) { - auto file = fopen(path, "wb"); - if (file) { - err = fwrite(buff, size, 1, file) != 1; - err |= fclose(file); - if (err) { - fprintf(stderr, "Could not write to file: %s.\n", path); - } - } else { - fprintf(stderr, "Could not open file: %s.\n", path); - } - } - } - - delete []buff; - - if (err == 0) { - cerr << "Created file system " << path << endl; - cerr << " type " << type << endl; - cerr << " wrote " << size << " bytes\n"; - } - } else { - fprintf(stderr, "Insufficient arguments\n"); - } - - return err; -} - -int read(int argc, char **args) { - auto err = 1; - if (argc >= 4) { - auto fsPath = args[2]; - auto inode = ox_atoi(args[3]); - size_t fsSize; - size_t fileSize; - - auto fsBuff = loadFileBuff(fsPath, &fsSize); - - if (fsBuff) { - auto fs = createFileSystem(fsBuff, fsSize); - - if (fs) { - auto output = fs->read(inode, &fileSize); - - if (output) { - fwrite(output, fileSize, 1, stdout); - delete []output; - err = 0; - } - - delete fs; - delete []fsBuff; - } else { - fprintf(stderr, "Invalid file system type: %d.\n", *(uint32_t*) fsBuff); - } - } else { - fprintf(stderr, "Could not open file: %s\n", fsPath); - } - } else { - fprintf(stderr, "Insufficient arguments\n"); - } - return err; -} - -int write(int argc, char **args, bool expand) { - auto err = 0; - if (argc >= 5) { - auto fsPath = args[2]; - auto inode = ox_atoi(args[3]); - auto srcPath = args[4]; - size_t srcSize; - - auto fsFile = fopen(fsPath, "rb"); - if (fsFile) { - fseek(fsFile, 0, SEEK_END); - - auto fsSize = (size_t) ftell(fsFile); - rewind(fsFile); - auto fsBuff = new uint8_t[fsSize]; - auto itemsRead = fread(fsBuff, fsSize, 1, fsFile); - fclose(fsFile); - - if (itemsRead) { - auto srcBuff = loadFileBuff(srcPath, &srcSize); - if (srcBuff) { - auto expanded = false; - auto fs = createFileSystem(fsBuff, fsSize); - if (fs) { - if (expand && fs->available() <= srcSize) { - auto needed = fs->size() + fs->spaceNeeded(srcSize); - fsSize = needed; - fs = expandCopyCleanup(fs, needed); - fsBuff = fs->buff(); - } - err |= fs->write(inode, srcBuff, srcSize); - - // compact the file system if it was expanded - if (expanded) { - fs->resize(); - } - - if (err) { - fprintf(stderr, "Could not write to file system.\n"); - } - delete fs; - } else { - fprintf(stderr, "Invalid file system type: %d.\n", *(uint32_t*) fsBuff); - err = 1; - } - - if (!err) { - fsFile = fopen(fsPath, "wb"); - - if (fsFile) { - err = fwrite(fsBuff, fsSize, 1, fsFile) != 1; - err |= fclose(fsFile); - if (err) { - fprintf(stderr, "Could not write to file system file.\n"); - } - } else { - err = 1; - } - } - delete []srcBuff; - } else { - err = 1; - fprintf(stderr, "Could not load source file: %s.\n", srcPath); - } - } - - delete []fsBuff; - } else { - fprintf(stderr, "Could not open file system\n"); - } - } else { - fprintf(stderr, "Insufficient arguments\n"); - } - return err; -} - -int compact(int argc, char **args) { - auto err = 1; - if (argc >= 2) { - auto fsPath = args[2]; - size_t fsSize; - - auto fsBuff = loadFileBuff(fsPath, &fsSize); - if (fsBuff) { - auto fs = createFileSystem(fsBuff, fsSize); - - if (fs) { - fs->resize(); - - // write back to file - auto fsFile = fopen(fsPath, "wb"); - if (fsFile) { - err = fwrite(fsBuff, fs->size(), 1, fsFile) != 1; - err |= fclose(fsFile); - if (err) { - fprintf(stderr, "Could not write to file system file.\n"); - } - } else { - err = 1; - } - - delete fs; - } else { - fprintf(stderr, "Invalid file system.\n"); - } - delete []fsBuff; - } else { - fprintf(stderr, "Could not open file: %s\n", fsPath); - } - } else { - fprintf(stderr, "Insufficient arguments\n"); - } - return err; -} - -int remove(int argc, char **args) { - auto err = 1; - if (argc >= 4) { - auto fsPath = args[2]; - auto inode = ox_atoi(args[3]); - size_t fsSize; - - auto fsBuff = loadFileBuff(fsPath, &fsSize); - if (fsBuff) { - auto fs = createFileSystem(fsBuff, fsSize); - - if (fs) { - err = fs->remove(inode); - } else { - fprintf(stderr, "Invalid file system.\n"); - } - - if (err) { - fprintf(stderr, "Could not write to file system.\n"); - } else { - auto fsFile = fopen(fsPath, "wb"); - if (fsFile) { - err = fwrite(fsBuff, fsSize, 1, fsFile) != 1; - err |= fclose(fsFile); - if (err) { - fprintf(stderr, "Could not write to file system file.\n"); - } - } else { - err = 1; - } - } - - delete fs; - delete []fsBuff; - } else { - fprintf(stderr, "Could not open file: %s\n", fsPath); - } - } else { - fprintf(stderr, "Insufficient arguments\n"); - } - return err; -} - -int walk(int argc, char **args) { - int err = 0; - size_t fsSize; - if (argc >= 2) { - auto fsPath = args[2]; - auto fsBuff = loadFileBuff(fsPath, &fsSize); - if (fsBuff) { - auto fs = createFileSystem(fsBuff, fsSize); - if (fs) { - cout << setw(9) << "Type |"; - cout << setw(10) << "Start |"; - cout << setw(10) << "End |"; - cout << setw(8) << "Size"; - cout << endl; - cout << "-------------------------------------"; - cout << endl; - fs->walk([](const char *type, uint64_t start, uint64_t end) { - cout << setw(7) << type << " |"; - cout << setw(8) << start << " |"; - cout << setw(8) << end << " |"; - cout << setw(8) << (end - start); - cout << endl; - return 0; - }); - delete fs; - } else { - cerr << "Invalid file system.\n"; - err = 1; - } - delete []fsBuff; - } else { - err = 2; - } - } - return err; -} - -int help(int, char**) { - cout << usage << endl; - return 0; -} - -int version(int, char**) { - cout << "oxfstool version " << oxfstoolVersion << endl; - cout << "oxfs format version " << FileStore16::VERSION << endl; - return 0; -} - -int main(int argc, char **args) { - auto err = 0; - map cmdMap = { - { "format", format }, - { "read", read }, - { "write", [](int argc, char **args) { return write(argc, args, false); } }, - { "write-expand", [](int argc, char **args) { return write(argc, args, true); } }, - { "compact", compact }, - { "rm", remove }, - { "walk", walk }, - { "help", help }, - { "version", version }, - }; - - if (argc > 1) { - auto cmd = args[1]; - auto f = cmdMap[cmd]; - if (f) { - err = f(argc, args); - } else { - cout << "Command '" << cmd << "' not recognized." << endl; - err = 1; - } - } else { - help(argc, args); - } - - return err; -} diff --git a/deps/ox/src/ox/fs/toollib.cpp b/deps/ox/src/ox/fs/toollib.cpp deleted file mode 100644 index 7399975f..00000000 --- a/deps/ox/src/ox/fs/toollib.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2015 - 2018 gtalent2@gmail.com - * - * This Source Code Form is subject to the terms of the Mozilla Public - * 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 - -#include "toollib.hpp" - -uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut) { - if (file) { - fseek(file, 0, SEEK_END); - const auto size = ftell(file); - rewind(file); - auto buff = new uint8_t[size]; - auto itemsRead = fread(buff, size, 1, file); - fclose(file); - if (sizeOut) { - *sizeOut = itemsRead ? size : 0; - } - return buff; - } else { - return nullptr; - } -} - -uint8_t *loadFileBuff(const char *path, ::size_t *sizeOut) { - return loadFileBuff(fopen(path, "rb"), sizeOut); -} diff --git a/deps/ox/src/ox/fs/toollib.hpp b/deps/ox/src/ox/fs/toollib.hpp deleted file mode 100644 index d2e7c739..00000000 --- a/deps/ox/src/ox/fs/toollib.hpp +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright 2015 - 2018 gtalent2@gmail.com - * - * This Source Code Form is subject to the terms of the Mozilla Public - * 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 - -uint8_t *loadFileBuff(FILE *file, size_t *sizeOut = nullptr); - -uint8_t *loadFileBuff(const char *path, size_t *sizeOut = nullptr);