diff --git a/deps/ox/.liccor.yml b/deps/ox/.liccor.yml index 550251462..66001a998 100644 --- a/deps/ox/.liccor.yml +++ b/deps/ox/.liccor.yml @@ -2,7 +2,7 @@ source: - src copyright_notice: |- - Copyright 2015 - 2017 gtalent2@gmail.com + 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 diff --git a/deps/ox/src/ox/fs/filestore.hpp b/deps/ox/src/ox/fs/filestore.hpp index 2ec0bcbef..c31f703eb 100644 --- a/deps/ox/src/ox/fs/filestore.hpp +++ b/deps/ox/src/ox/fs/filestore.hpp @@ -10,6 +10,10 @@ #include +#include +using std::cout; +using std::endl; + namespace ox { template @@ -801,6 +805,7 @@ void *FileStore
::alloc(typename Header::FsSize_t size) { compact(); next = nextInodeAddr(); if ((next + size) > ptr(end())) { + cout << "alloc returning nullptr\n"; return nullptr; } } @@ -818,6 +823,7 @@ void *FileStore
::alloc(typename Header::FsSize_t size) { template void FileStore
::compact() { + cout << "FileStore::compact\n"; auto dest = ptr(firstInode()); auto current = ptr(firstInode()); while (current->getNext() > firstInode() && current->getNext() < ptr(end())) { diff --git a/deps/ox/src/ox/fs/filesystem.cpp b/deps/ox/src/ox/fs/filesystem.cpp index 46c6908ae..c3b786cef 100644 --- a/deps/ox/src/ox/fs/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem.cpp @@ -60,10 +60,7 @@ FileSystem *expandCopy(FileSystem *fs, size_t size) { FileSystem *expandCopyCleanup(FileSystem *fs, size_t size) { auto out = expandCopy(fs, size); - if (out) { - delete[] fs->buff(); - delete fs; - } else { + if (!out) { out = fs; } diff --git a/deps/ox/src/ox/fs/filesystem.hpp b/deps/ox/src/ox/fs/filesystem.hpp index 4ea069ff5..1f1075151 100644 --- a/deps/ox/src/ox/fs/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem.hpp @@ -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/. */ + #pragma once #include @@ -25,10 +26,10 @@ enum FileType { }; struct FileStat { - uint64_t inode; - uint64_t links; - uint64_t size; - uint8_t fileType; + uint64_t inode = 0; + uint64_t links = 0; + uint64_t size = 0; + uint8_t fileType = 0; }; template @@ -50,29 +51,39 @@ bool operator<(const DirectoryListing &a, const DirectoryListing template struct __attribute__((packed)) DirectoryEntry { - InodeId_t inode; - char *getName() { - return (char*) (this + 1); - } + public: + InodeId_t inode = 0; - void setName(const char *name) { - auto data = getName(); - auto nameLen = ox_strlen(name); - ox_memcpy(data, name, nameLen); - data[nameLen] = 0; - } + private: + uint32_t m_nameLen = 0; - static uint64_t spaceNeeded(const char *fileName) { - return sizeof(DirectoryEntry) + ox_strlen(fileName) + 1; - } + public: + void *end() { + return ((uint8_t*) this) + size(); + } - /** - * The size in bytes. - */ - uint64_t size() { - return spaceNeeded(getName()); - } + 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 @@ -87,6 +98,8 @@ struct __attribute__((packed)) Directory { return size ? (DirectoryEntry*) (this + 1) : nullptr; } + void *end(); + uint64_t getFileInode(const char *name); int getChildrenInodes(InodeId_t *inodes, size_t inodesLen); @@ -99,6 +112,11 @@ struct __attribute__((packed)) Directory { 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; @@ -172,16 +190,19 @@ int Directory::copy(Directory *dirOut) 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(); } - return 0; } else { - return 1; + return 2; } + return 0; } template @@ -207,7 +228,7 @@ class FileSystem { virtual int stripDirectories() = 0; - virtual int mkdir(const char *path) = 0; + virtual int mkdir(const char *path, bool recursive = false) = 0; /** * Moves an entry from one directory to another. @@ -257,13 +278,16 @@ class FileSystem { 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[s.size * 4]; + uint8_t dirBuff[max(static_cast(s.size), sizeof(Dir)) * 4]; auto dir = (Directory*) dirBuff; - err = readDirectory(path, dir); - err |= dir->ls(list); + err |= readDirectory(path, dir); + if (!err) { + err |= dir->ls(list); + } } return err; } @@ -300,7 +324,7 @@ class FileSystemTemplate: public FileSystem { int stripDirectories() override; - int mkdir(const char *path) override; + int mkdir(const char *path, bool recursive = false) override; int read(const char *path, void *buffer, size_t buffSize) override; @@ -384,20 +408,35 @@ int FileSystemTemplate::stripDirectories() { } template -int FileSystemTemplate::mkdir(const char *pathIn) { +int FileSystemTemplate::mkdir(const char *pathIn, bool recursive) { if (!findInodeOf(pathIn)) { auto pathLen = ox_strlen(pathIn); char path[pathLen + 1]; ox_memcpy(path, pathIn, pathLen + 1); - // make sure last character does not end with / if (pathLen >= 1 && path[pathLen - 1] == '/') { path[pathLen - 1] = 0; pathLen--; } + char dirPath[pathLen]; + PathIterator pathReader(path, pathLen); + auto err = pathReader.dirPath(dirPath, pathLen); + + if (err) { + return err; + } + + // make sure parent directory exists if recursive + if (recursive and !findInodeOf(dirPath)) { + err |= mkdir(dirPath, recursive); + if (err) { + return err; + } + } + Directory dir; - auto err = write(path, &dir, sizeof(dir), FileType::FileType_Directory); + err |= write(path, &dir, sizeof(dir), FileType::FileType_Directory); if (err) { return err; } @@ -411,10 +450,7 @@ int FileSystemTemplate::mkdir(const char *pathIn) { } // add .. entry for parent - char dirPath[pathLen]; - PathIterator pathReader(path, pathLen); - err |= pathReader.dirPath(dirPath, pathLen); - err = insertDirectoryEntry(path, "..", findInodeOf(dirPath)); + err |= insertDirectoryEntry(path, "..", findInodeOf(dirPath)); if (err) { remove(inode); return err; @@ -846,14 +882,14 @@ int FileSystemTemplate::rmDirectoryEntry(const char *path) { template int FileSystemTemplate::readDirectory(const char *path, Directory *dirOut) { + typedef Directory Dir; int err = 0; - auto inode = findInodeOf(path); - auto dirStat = stat(inode); - auto dirBuffLen = dirStat.size; + auto dirStat = stat(path); + auto dirBuffLen = ox::max(static_cast(dirStat.size), sizeof(Dir)); uint8_t dirBuff[dirBuffLen]; - auto dir = (Directory*) dirBuff; + auto dir = (Dir*) dirBuff; - err = read(dirStat.inode, dirBuff, dirBuffLen); + err |= read(dirStat.inode, dirBuff, dirBuffLen); if (!err) { return dir->copy(dirOut); } else { diff --git a/deps/ox/src/ox/fs/fs.hpp b/deps/ox/src/ox/fs/fs.hpp new file mode 100644 index 000000000..8acc24cee --- /dev/null +++ b/deps/ox/src/ox/fs/fs.hpp @@ -0,0 +1,11 @@ +/* + * Copyright 2015 - 2017 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 "filesystem.hpp" \ No newline at end of file diff --git a/deps/ox/src/ox/std/math.hpp b/deps/ox/src/ox/std/math.hpp new file mode 100644 index 000000000..1e0920013 --- /dev/null +++ b/deps/ox/src/ox/std/math.hpp @@ -0,0 +1,23 @@ +/* + * Copyright 2015 - 2017 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 + +namespace ox { + +template +inline const T &min(const T &a, const T &b) { + return a < b ? a : b; +} + +template +inline const T &max(const T &a, const T &b) { + return a > b ? a : b; +} + +} diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 657d5f242..9613abab7 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -5,10 +5,12 @@ * 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 "bitops.hpp" #include "byteswap.hpp" +#include "math.hpp" #include "memops.hpp" #include "random.hpp" #include "strops.hpp"