From 637c9f24c2a30a151b72c904043fe23f6fddbb03 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 2 Feb 2018 01:15:15 -0600 Subject: [PATCH] Breakup file system code into smaller files --- deps/ox/src/ox/fs/CMakeLists.txt | 14 +- deps/ox/src/ox/fs/filesystem/directory.hpp | 208 ++++++++++++ .../src/ox/fs/{ => filesystem}/filesystem.cpp | 2 +- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 101 ++++++ .../filesystemtemplate.hpp} | 295 +----------------- .../ox/fs/{ => filesystem}/pathiterator.cpp | 0 .../ox/fs/{ => filesystem}/pathiterator.hpp | 0 deps/ox/src/ox/fs/filesystem/types.hpp | 33 ++ deps/ox/src/ox/fs/fs.hpp | 2 +- deps/ox/src/ox/fs/oxfstool.cpp | 2 +- deps/ox/src/ox/fs/test/filesystem_format.cpp | 2 +- deps/ox/src/ox/fs/test/tests.cpp | 3 +- deps/ox/src/ox/std/types.hpp | 2 + 13 files changed, 360 insertions(+), 304 deletions(-) create mode 100644 deps/ox/src/ox/fs/filesystem/directory.hpp rename deps/ox/src/ox/fs/{ => filesystem}/filesystem.cpp (97%) create mode 100644 deps/ox/src/ox/fs/filesystem/filesystem.hpp rename deps/ox/src/ox/fs/{filesystem.hpp => filesystem/filesystemtemplate.hpp} (70%) rename deps/ox/src/ox/fs/{ => filesystem}/pathiterator.cpp (100%) rename deps/ox/src/ox/fs/{ => filesystem}/pathiterator.hpp (100%) create mode 100644 deps/ox/src/ox/fs/filesystem/types.hpp diff --git a/deps/ox/src/ox/fs/CMakeLists.txt b/deps/ox/src/ox/fs/CMakeLists.txt index 5c333217..0d5479d3 100644 --- a/deps/ox/src/ox/fs/CMakeLists.txt +++ b/deps/ox/src/ox/fs/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 2.8) add_library( OxFS - filesystem.cpp - pathiterator.cpp + filesystem/filesystem.cpp + filesystem/pathiterator.cpp ) set_property( @@ -31,12 +31,18 @@ endif() install( FILES filestore.hpp - filesystem.hpp - pathiterator.hpp DESTINATION include/ox/fs ) +install( + FILES + filesystem/filesystem.hpp + filesystem/pathiterator.hpp + DESTINATION + include/ox/fs/filesystem +) + install( TARGETS OxFS diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp new file mode 100644 index 00000000..39b0c533 --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -0,0 +1,208 @@ +/* + * 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/. + */ + +#pragma once + +#include + +#include "types.hpp" + +namespace ox { + +template +struct DirectoryListing { + String name; + FileStat stat; + + DirectoryListing() = default; + + DirectoryListing(const char *name) { + this->name = name; + } +}; + +template +bool operator<(const DirectoryListing &a, const DirectoryListing &b) { + return a.name < b.name; +} + + +template +struct __attribute__((packed)) DirectoryEntry { + + public: + InodeId_t inode = 0; + + private: + uint32_t m_nameLen = 0; + + public: + void *end() { + return ((uint8_t*) this) + size(); + } + + char *getName() { + return (char*) (this + 1); + } + + void setName(const char *name) { + auto data = getName(); + m_nameLen = ox_strlen(name); + ox_memcpy(data, name, m_nameLen); + data[m_nameLen] = 0; + } + + static uint64_t spaceNeeded(const char *fileName) { + return sizeof(DirectoryEntry) + ox_strlen(fileName) + 1; + } + + /** + * The size in bytes. + */ + uint64_t size() { + return sizeof(DirectoryEntry) + m_nameLen + 1; + } +}; + + +template +struct __attribute__((packed)) Directory { + /** + * Number of bytes after this Directory struct. + */ + FsSize_t size = 0; + FsSize_t children = 0; + + DirectoryEntry *files() { + return size ? (DirectoryEntry*) (this + 1) : nullptr; + } + + void *end(); + + uint64_t getFileInode(const char *name); + + int getChildrenInodes(InodeId_t *inodes, size_t inodesLen); + + int rmFile(const char *name); + + int copy(Directory *dirOut); + + template + int ls(List *list); +}; + +template +void *Directory::end() { + return ((int8_t*) this) + size; +} + +template +uint64_t Directory::getFileInode(const char *name) { + uint64_t inode = 0; + auto current = files(); + if (current) { + for (uint64_t i = 0; ox_strcmp(current->getName(), name) != 0;) { + i += current->size(); + if (i < this->size) { + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + } else { + current = nullptr; + break; + } + } + if (current) { + inode = current->inode; + } + } + return inode; +} + +template +int Directory::getChildrenInodes(InodeId_t *inodes, size_t inodesLen) { + if (inodesLen >= this->children) { + auto current = files(); + if (current) { + for (uint64_t i = 0; i < this->children; i++) { + if (ox_strcmp(current->getName(), ".") and ox_strcmp(current->getName(), "..")) { + inodes[i] = current->inode; + } + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + } + return 0; + } else { + return 1; + } + } else { + return 2; + } +} + +template +int Directory::rmFile(const char *name) { + int err = 1; + auto current = files(); + if (current) { + for (uint64_t i = 0; i < this->size;) { + i += current->size(); + if (ox_strcmp(current->getName(), name) == 0) { + auto dest = (uint8_t*) current; + auto src = dest + current->size(); + ox_memcpy(dest, src, this->size - i); + this->size -= current->size(); + this->children--; + err = 0; + break; + } + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + } + } + return err; +} + +template +int Directory::copy(Directory *dirOut) { + auto current = files(); + 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*) dirOutBuff; + if (this->end() < current->end()) { + return 1; + } + entry->inode = current->inode; + entry->setName(current->getName()); + + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + dirOutBuff += entry->size(); + } + } else { + return 2; + } + return 0; +} + +template +template +int Directory::ls(List *list) { + auto current = files(); + if (current) { + for (uint64_t i = 0; i < this->children; i++) { + list->push_back(current->getName()); + (*list)[i].stat.inode = current->inode; + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + } + return 0; + } else { + return 1; + } +} + +} \ No newline at end of file diff --git a/deps/ox/src/ox/fs/filesystem.cpp b/deps/ox/src/ox/fs/filesystem/filesystem.cpp similarity index 97% rename from deps/ox/src/ox/fs/filesystem.cpp rename to deps/ox/src/ox/fs/filesystem/filesystem.cpp index c3b786ce..fad00e59 100644 --- a/deps/ox/src/ox/fs/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.cpp @@ -6,7 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "filesystem.hpp" +#include "filesystemtemplate.hpp" namespace ox { diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp new file mode 100644 index 00000000..b604867b --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -0,0 +1,101 @@ +/* + * 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/. + */ + +#pragma once + +#include + +#include "directory.hpp" +#include "pathiterator.hpp" + +namespace ox { + +class FileSystem { + public: + virtual ~FileSystem() {}; + + virtual int stripDirectories() = 0; + + virtual int mkdir(const char *path, bool recursive = false) = 0; + + /** + * Moves an entry from one directory to another. + * @param src the path to the file + * @param dest the path of the destination directory + */ + virtual int move(const char *src, const char *dest) = 0; + + template + int ls(const char *path, List *list); + + virtual int read(const char *path, void *buffer, size_t buffSize) = 0; + + virtual int read(uint64_t inode, void *buffer, size_t size) = 0; + + virtual int read(uint64_t inode, size_t readStart, size_t readSize, void *buffer, size_t *size) = 0; + + virtual uint8_t *read(uint64_t inode, size_t *size) = 0; + + virtual int remove(uint64_t inode, bool recursive = false) = 0; + + virtual int remove(const char *path, bool recursive = false) = 0; + + virtual void resize(uint64_t size = 0) = 0; + + virtual int write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; + + virtual int write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; + + virtual FileStat stat(uint64_t inode) = 0; + + virtual FileStat stat(const char *path) = 0; + + virtual uint64_t spaceNeeded(uint64_t size) = 0; + + virtual uint64_t available() = 0; + + virtual uint64_t size() = 0; + + 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; +}; + +template +int FileSystem::ls(const char *path, List *list) { + typedef Directory Dir; + int err = 0; + auto s = stat(path); + if (s.fileType == FileType_Directory) { + uint8_t dirBuff[max(static_cast(s.size), sizeof(Dir)) * 4]; + auto dir = (Directory*) dirBuff; + err |= readDirectory(path, dir); + if (!err) { + err |= dir->ls(list); + } + } + return err; +} + +FileSystem *createFileSystem(uint8_t *buff, size_t buffSize, bool ownsBuff = false); + +/** + * 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); + +} diff --git a/deps/ox/src/ox/fs/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystemtemplate.hpp similarity index 70% rename from deps/ox/src/ox/fs/filesystem.hpp rename to deps/ox/src/ox/fs/filesystem/filesystemtemplate.hpp index 1f107515..54433cc9 100644 --- a/deps/ox/src/ox/fs/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystemtemplate.hpp @@ -8,303 +8,10 @@ #pragma once -#include -#include "pathiterator.hpp" -#include "filestore.hpp" +#include "filesystem.hpp" namespace ox { -enum FsType { - OxFS_16 = 1, - OxFS_32 = 2, - OxFS_64 = 3 -}; - -enum FileType { - FileType_NormalFile = 1, - FileType_Directory = 2 -}; - -struct FileStat { - uint64_t inode = 0; - uint64_t links = 0; - uint64_t size = 0; - uint8_t fileType = 0; -}; - -template -struct DirectoryListing { - String name; - FileStat stat; - - DirectoryListing() = default; - - DirectoryListing(const char *name) { - this->name = name; - } -}; - -template -bool operator<(const DirectoryListing &a, const DirectoryListing &b) { - return a.name < b.name; -} - -template -struct __attribute__((packed)) DirectoryEntry { - - public: - InodeId_t inode = 0; - - private: - uint32_t m_nameLen = 0; - - public: - void *end() { - return ((uint8_t*) this) + size(); - } - - char *getName() { - return (char*) (this + 1); - } - - void setName(const char *name) { - auto data = getName(); - m_nameLen = ox_strlen(name); - ox_memcpy(data, name, m_nameLen); - data[m_nameLen] = 0; - } - - static uint64_t spaceNeeded(const char *fileName) { - return sizeof(DirectoryEntry) + ox_strlen(fileName) + 1; - } - - /** - * The size in bytes. - */ - uint64_t size() { - return sizeof(DirectoryEntry) + m_nameLen + 1; - } -}; - -template -struct __attribute__((packed)) Directory { - /** - * Number of bytes after this Directory struct. - */ - FsSize_t size = 0; - FsSize_t children = 0; - - DirectoryEntry *files() { - return size ? (DirectoryEntry*) (this + 1) : nullptr; - } - - void *end(); - - uint64_t getFileInode(const char *name); - - int getChildrenInodes(InodeId_t *inodes, size_t inodesLen); - - int rmFile(const char *name); - - int copy(Directory *dirOut); - - template - int ls(List *list); -}; - -template -void *Directory::end() { - return ((int8_t*) this) + size; -} - -template -uint64_t Directory::getFileInode(const char *name) { - uint64_t inode = 0; - auto current = files(); - if (current) { - for (uint64_t i = 0; ox_strcmp(current->getName(), name) != 0;) { - i += current->size(); - if (i < this->size) { - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - } else { - current = nullptr; - break; - } - } - if (current) { - inode = current->inode; - } - } - return inode; -} - -template -int Directory::getChildrenInodes(InodeId_t *inodes, size_t inodesLen) { - if (inodesLen >= this->children) { - auto current = files(); - if (current) { - for (uint64_t i = 0; i < this->children; i++) { - if (ox_strcmp(current->getName(), ".") and ox_strcmp(current->getName(), "..")) { - inodes[i] = current->inode; - } - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - } - return 0; - } else { - return 1; - } - } else { - return 2; - } -} - -template -int Directory::rmFile(const char *name) { - int err = 1; - auto current = files(); - if (current) { - for (uint64_t i = 0; i < this->size;) { - i += current->size(); - if (ox_strcmp(current->getName(), name) == 0) { - auto dest = (uint8_t*) current; - auto src = dest + current->size(); - ox_memcpy(dest, src, this->size - i); - this->size -= current->size(); - this->children--; - err = 0; - break; - } - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - } - } - return err; -} - -template -int Directory::copy(Directory *dirOut) { - auto current = files(); - 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*) dirOutBuff; - if (this->end() < current->end()) { - return 1; - } - entry->inode = current->inode; - entry->setName(current->getName()); - - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - dirOutBuff += entry->size(); - } - } else { - return 2; - } - return 0; -} - -template -template -int Directory::ls(List *list) { - auto current = files(); - if (current) { - for (uint64_t i = 0; i < this->children; i++) { - list->push_back(current->getName()); - (*list)[i].stat.inode = current->inode; - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - } - return 0; - } else { - return 1; - } -} - - -class FileSystem { - public: - virtual ~FileSystem() {}; - - virtual int stripDirectories() = 0; - - virtual int mkdir(const char *path, bool recursive = false) = 0; - - /** - * Moves an entry from one directory to another. - * @param src the path to the file - * @param dest the path of the destination directory - */ - virtual int move(const char *src, const char *dest) = 0; - - template - int ls(const char *path, List *list); - - virtual int read(const char *path, void *buffer, size_t buffSize) = 0; - - virtual int read(uint64_t inode, void *buffer, size_t size) = 0; - - virtual int read(uint64_t inode, size_t readStart, size_t readSize, void *buffer, size_t *size) = 0; - - virtual uint8_t *read(uint64_t inode, size_t *size) = 0; - - virtual int remove(uint64_t inode, bool recursive = false) = 0; - - virtual int remove(const char *path, bool recursive = false) = 0; - - virtual void resize(uint64_t size = 0) = 0; - - virtual int write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; - - virtual int write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; - - virtual FileStat stat(uint64_t inode) = 0; - - virtual FileStat stat(const char *path) = 0; - - virtual uint64_t spaceNeeded(uint64_t size) = 0; - - virtual uint64_t available() = 0; - - virtual uint64_t size() = 0; - - 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; -}; - -template -int FileSystem::ls(const char *path, List *list) { - typedef Directory Dir; - int err = 0; - auto s = stat(path); - if (s.fileType == FileType_Directory) { - uint8_t dirBuff[max(static_cast(s.size), sizeof(Dir)) * 4]; - auto dir = (Directory*) dirBuff; - err |= readDirectory(path, dir); - if (!err) { - err |= dir->ls(list); - } - } - return err; -} - -FileSystem *createFileSystem(uint8_t *buff, size_t buffSize, bool ownsBuff = false); - -/** - * 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 { diff --git a/deps/ox/src/ox/fs/pathiterator.cpp b/deps/ox/src/ox/fs/filesystem/pathiterator.cpp similarity index 100% rename from deps/ox/src/ox/fs/pathiterator.cpp rename to deps/ox/src/ox/fs/filesystem/pathiterator.cpp diff --git a/deps/ox/src/ox/fs/pathiterator.hpp b/deps/ox/src/ox/fs/filesystem/pathiterator.hpp similarity index 100% rename from deps/ox/src/ox/fs/pathiterator.hpp rename to deps/ox/src/ox/fs/filesystem/pathiterator.hpp diff --git a/deps/ox/src/ox/fs/filesystem/types.hpp b/deps/ox/src/ox/fs/filesystem/types.hpp new file mode 100644 index 00000000..931efe43 --- /dev/null +++ b/deps/ox/src/ox/fs/filesystem/types.hpp @@ -0,0 +1,33 @@ +/* + * 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/. + */ + +#pragma once + +#include + +namespace ox { + +enum FsType { + OxFS_16 = 1, + OxFS_32 = 2, + OxFS_64 = 3 +}; + +enum FileType { + FileType_NormalFile = 1, + FileType_Directory = 2 +}; + +struct FileStat { + uint64_t inode = 0; + uint64_t links = 0; + uint64_t size = 0; + uint8_t fileType = 0; +}; + +} \ No newline at end of file diff --git a/deps/ox/src/ox/fs/fs.hpp b/deps/ox/src/ox/fs/fs.hpp index 8acc24ce..195120be 100644 --- a/deps/ox/src/ox/fs/fs.hpp +++ b/deps/ox/src/ox/fs/fs.hpp @@ -8,4 +8,4 @@ #pragma once -#include "filesystem.hpp" \ No newline at end of file +#include "filesystem/filesystemtemplate.hpp" \ No newline at end of file diff --git a/deps/ox/src/ox/fs/oxfstool.cpp b/deps/ox/src/ox/fs/oxfstool.cpp index b2a01a00..afc0e524 100644 --- a/deps/ox/src/ox/fs/oxfstool.cpp +++ b/deps/ox/src/ox/fs/oxfstool.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include "toollib.hpp" diff --git a/deps/ox/src/ox/fs/test/filesystem_format.cpp b/deps/ox/src/ox/fs/test/filesystem_format.cpp index 5e11585f..ff115652 100644 --- a/deps/ox/src/ox/fs/test/filesystem_format.cpp +++ b/deps/ox/src/ox/fs/test/filesystem_format.cpp @@ -6,7 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include +#include using namespace ox; diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index cd0de02a..3686d4c8 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -11,8 +11,7 @@ #include #include #include -#include -#include +#include #include using namespace std; diff --git a/deps/ox/src/ox/std/types.hpp b/deps/ox/src/ox/std/types.hpp index 4754c43e..4cce71a3 100644 --- a/deps/ox/src/ox/std/types.hpp +++ b/deps/ox/src/ox/std/types.hpp @@ -23,6 +23,8 @@ typedef long int64_t; typedef unsigned long uint64_t; #endif +typedef uint64_t uintmax_t; + namespace ox { typedef uint32_t Error;