From 84533e557e0a2a20c10ef52983d10a5e93616363 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 9 Sep 2017 19:32:45 -0500 Subject: [PATCH] Add walk command. --- src/ox/fs/filestore.hpp | 13 +++++++++++++ src/ox/fs/filesystem.hpp | 10 ++++++++++ src/ox/fs/oxfstool.cpp | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/ox/fs/filestore.hpp b/src/ox/fs/filestore.hpp index cff4611b5..cedcf99c2 100644 --- a/src/ox/fs/filestore.hpp +++ b/src/ox/fs/filestore.hpp @@ -268,6 +268,8 @@ class FileStore { */ typename Header::FsSize_t available(); + void walk(int(*cb)(const char*, uint64_t start, uint64_t end)); + uint16_t fsType(); uint16_t version(); @@ -887,6 +889,17 @@ uint16_t FileStore
::version() { return m_header.getVersion(); }; +template +void FileStore
::walk(int(*cb)(const char*, uint64_t start, uint64_t end)) { + auto err = cb("Header", 0, sizeof(Header)); + auto inode = ptr(firstInode()); + while (!err && inode != (Inode*) begin()) { + inode = ptr(inode->getNext()); + auto start = ptr(inode); + err = cb("Inode", start, start + inode->size()); + } +} + template uint8_t *FileStore
::format(uint8_t *buffer, typename Header::FsSize_t size, uint16_t fsType) { ox_memset(buffer, 0, size); diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 988c116cc..d0a671cb1 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -244,6 +244,8 @@ class FileSystem { virtual uint8_t *buff() = 0; + virtual void walk(int(*cb)(const char*, uint64_t, uint64_t)) = 0; + protected: virtual int readDirectory(const char *path, Directory *dirOut) = 0; }; @@ -326,6 +328,7 @@ class FileSystemTemplate: public FileSystem { uint64_t size() override; uint8_t *buff() override; + int move(const char *src, const char *dest) override; /** @@ -333,6 +336,8 @@ class FileSystemTemplate: public FileSystem { */ int rmDirectoryEntry(const char *path); + void walk(int(*cb)(const char*, uint64_t, uint64_t)) override; + static uint8_t *format(uint8_t *buffer, typename FileStore::FsSize_t size, bool useDirectories); protected: @@ -849,6 +854,11 @@ void FileSystemTemplate::expand(uint64_t newSize) { } } +template +void FileSystemTemplate::walk(int(*cb)(const char*, uint64_t, uint64_t)) { + m_store->walk(cb); +} + typedef FileSystemTemplate FileSystem16; typedef FileSystemTemplate FileSystem32; typedef FileSystemTemplate FileSystem64; diff --git a/src/ox/fs/oxfstool.cpp b/src/ox/fs/oxfstool.cpp index ee241245d..4d52e5b32 100644 --- a/src/ox/fs/oxfstool.cpp +++ b/src/ox/fs/oxfstool.cpp @@ -20,7 +20,7 @@ using namespace ox; using namespace std; -const static auto oxfstoolVersion = "1.3.0"; +const static auto oxfstoolVersion = "1.4.0"; const static auto usage = "usage:\n" "\toxfs format [16,32,64] \n" "\toxfs read \n" @@ -28,6 +28,7 @@ const static auto usage = "usage:\n" "\toxfs write-expand \n" "\toxfs rm \n" "\toxfs compact \n" +"\toxfs walk \n" "\toxfs version\n"; uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) { @@ -148,7 +149,7 @@ int read(int argc, char **args) { size_t fileSize; auto fsBuff = loadFileBuff(fsPath, &fsSize); - + if (fsBuff) { auto fs = createFileSystem(fsBuff, fsSize); @@ -333,6 +334,31 @@ int remove(int argc, char **args) { return err; } +int walk(int argc, char **args) { + int err = 0; + size_t fsSize; + auto fsPath = args[2]; + auto fsBuff = loadFileBuff(fsPath, &fsSize); + if (fsBuff) { + auto fs = createFileSystem(fsBuff, fsSize); + if (fs) { + fs->walk([](const char *type, uint64_t start, uint64_t end) { + cout << "narf\n"; + cout << type << ", start: " << start << ", end: " << end << endl; + return 0; + }); + delete fs; + } else { + cerr << "Invalid file system.\n"; + err = 1; + } + delete []fsBuff; + } else { + err = 2; + } + return err; +} + int main(int argc, char **args) { auto err = 0; if (argc > 1) { @@ -349,6 +375,8 @@ int main(int argc, char **args) { err = compact(argc, args); } else if (ox_strcmp(cmd, "rm") == 0) { err = remove(argc, args); + } else if (ox_strcmp(cmd, "walk") == 0) { + err = walk(argc, args); } else if (ox_strcmp(cmd, "help") == 0) { printf("%s\n", usage); } else if (ox_strcmp(cmd, "version") == 0) {