diff --git a/src/ox/fs/CMakeLists.txt b/src/ox/fs/CMakeLists.txt index 408efe096..a11e144fc 100644 --- a/src/ox/fs/CMakeLists.txt +++ b/src/ox/fs/CMakeLists.txt @@ -7,6 +7,12 @@ add_library( inodemgr.cpp ) +add_executable( + oxfstool + oxfstool.cpp +) +target_link_libraries(oxfstool OxFS OxStd) + install( FILES filestore.hpp diff --git a/src/ox/fs/filestore.hpp b/src/ox/fs/filestore.hpp index 5f21aaa41..eee1175c8 100644 --- a/src/ox/fs/filestore.hpp +++ b/src/ox/fs/filestore.hpp @@ -12,13 +12,11 @@ namespace ox { namespace fs { -using namespace ox::std; - template class FileStore { public: - typedef uint16_t InodeId_t; + typedef ox::std::uint16_t InodeId_t; typedef FsT FsSize_t; struct StatInfo { @@ -34,7 +32,7 @@ class FileStore { // The following variables should not be assumed to exist FsSize_t dataLen; InodeId_t m_id; - uint8_t refs; + ox::std::uint8_t refs; FsSize_t left, right; FsSize_t size(); @@ -46,7 +44,7 @@ class FileStore { Inode() = default; }; - uint32_t m_fsType; + ox::std::uint32_t m_fsType; FsSize_t m_size; FsSize_t m_firstInode; FsSize_t m_rootInode; @@ -92,9 +90,9 @@ class FileStore { */ StatInfo stat(InodeId_t id); - static uint8_t version(); + static ox::std::uint8_t version(); - static uint8_t *format(uint8_t *buffer, FsSize_t size, uint32_t fsType = 0); + static ox::std::uint8_t *format(ox::std::uint8_t *buffer, FsSize_t size, ox::std::uint32_t fsType = 0); private: /** @@ -146,11 +144,11 @@ class FileStore { Inode *lastInode(); - uint8_t *begin() { - return (uint8_t*) this; + ox::std::uint8_t *begin() { + return (ox::std::uint8_t*) this; } - uint8_t *end() { + ox::std::uint8_t *end() { return begin() + this->m_size; } @@ -181,7 +179,7 @@ void FileStore::Inode::setId(InodeId_t id) { template void FileStore::Inode::setData(void *data, int size) { - memcpy(this->data(), data, size); + ox::std::memcpy(this->data(), data, size); dataLen = size; } @@ -286,7 +284,7 @@ int FileStore::read(InodeId_t id, void *data, FsSize_t *size) { if (size) { *size = inode->dataLen; } - memcpy(data, inode->data(), inode->dataLen); + ox::std::memcpy(data, inode->data(), inode->dataLen); retval = 0; } return retval; @@ -326,16 +324,16 @@ typename FileStore::Inode *FileStore::getInode(Inode *root, template void *FileStore::alloc(FsSize_t size) { - if ((lastInode()->next + size) > (uint64_t) end()) { + if ((lastInode()->next + size) > (ox::std::uint64_t) end()) { compress(); - if ((lastInode()->next + size) > (uint64_t) end()) { + if ((lastInode()->next + size) > (ox::std::uint64_t) end()) { return nullptr; } } const auto retval = lastInode()->next; const auto inode = ptr(retval); - memset(inode, 0, size); + ox::std::memset(inode, 0, size); inode->next = retval + size; ptr(m_firstInode)->prev = retval; return inode; @@ -351,7 +349,7 @@ void FileStore::compress() { current = ptr(current->next); current->prev = prev; if (prevEnd != current) { - memcpy(prevEnd, current, current->size()); + ox::std::memcpy(prevEnd, current, current->size()); current = prevEnd; } } @@ -387,7 +385,7 @@ FsSize_t FileStore::iterator() { template FsSize_t FileStore::ptr(void *ptr) { - return ((uint8_t*) ptr) - begin(); + return ((ox::std::uint8_t*) ptr) - begin(); } template @@ -401,13 +399,13 @@ typename FileStore::Inode *FileStore::lastInode() { } template -uint8_t FileStore::version() { +ox::std::uint8_t FileStore::version() { return 1; }; template -uint8_t *FileStore::format(uint8_t *buffer, FsSize_t size, uint32_t fsType) { - memset(buffer, 0, size); +ox::std::uint8_t *FileStore::format(ox::std::uint8_t *buffer, FsSize_t size, ox::std::uint32_t fsType) { + ox::std::memset(buffer, 0, size); auto *fs = (FileStore*) buffer; fs->m_fsType = fsType; @@ -417,12 +415,12 @@ uint8_t *FileStore::format(uint8_t *buffer, FsSize_t size, uint32_t fs fs->firstInode()->prev = fs->m_firstInode; fs->lastInode()->next = sizeof(FileStore); - return (uint8_t*) buffer; + return (ox::std::uint8_t*) buffer; } -typedef FileStore FileStore16; -typedef FileStore FileStore32; -typedef FileStore FileStore64; +typedef FileStore FileStore16; +typedef FileStore FileStore32; +typedef FileStore FileStore64; } } diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 91a7e3050..035970100 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -15,6 +15,12 @@ namespace fs { using namespace std; +enum FsType { + OxFS16 = 1, + OxFS32 = 2, + OxFS64 = 3 +}; + struct FileStat { uint64_t inode; uint64_t size; diff --git a/src/ox/fs/oxfstool.cpp b/src/ox/fs/oxfstool.cpp new file mode 100644 index 000000000..2ede19cec --- /dev/null +++ b/src/ox/fs/oxfstool.cpp @@ -0,0 +1,72 @@ +/* + * Copyright 2015 - 2016 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 + +using namespace ox::fs; + +int format(int argc, char **args) { + printf("Creating file system...\n"); + auto err = 1; + if (argc >= 5) { + auto type = ox::std::atoi(args[2]); + auto size = ox::std::atoi(args[3]); + auto path = args[4]; + auto buff = (ox::std::uint8_t*) malloc(size); + + printf("Size: %d bytes\n", size); + printf("Type: %d\n", type); + + // format + switch (type) { + case 16: + FileStore16::format(buff, size, ox::fs::OxFS16); + break; + case 32: + FileStore32::format(buff, size, ox::fs::OxFS32); + break; + case 64: + FileStore64::format(buff, size, ox::fs::OxFS64); + break; + } + + FILE *file = fopen(path, "w"); + if (file) { + fwrite(buff, size, 1, file); + err = fclose(file); + if (err) { + printf("Could not write to file: %s.\n", path); + } + } else { + printf("Could not open file: %s.\n", path); + } + + if (err == 0) { + printf("Created file system %s\n", path); + } + } + + return err; +} + +int main(int argc, char **args) { + auto err = 0; + if (argc > 1) { + auto cmd = args[1]; + if (ox::std::strcmp(cmd, "format") == 0) { + err = format(argc, args); + } else { + printf("Command '%s' not recognized.\n", cmd); + err = 1; + } + } + return err; +} diff --git a/src/ox/fs/test/filestore_format.cpp b/src/ox/fs/test/filestore_format.cpp index bc910d12f..4ebe87fba 100644 --- a/src/ox/fs/test/filestore_format.cpp +++ b/src/ox/fs/test/filestore_format.cpp @@ -11,8 +11,8 @@ using namespace ox::fs; int main() { const auto size = 65535; - uint8_t volume[size]; - uint32_t err = 0; + ox::std::uint8_t volume[size]; + ox::std::uint32_t err = 0; FileStore32::format(volume, size); return err; } diff --git a/src/ox/fs/test/filestoreio.cpp b/src/ox/fs/test/filestoreio.cpp index ff1930f85..80eb114b3 100644 --- a/src/ox/fs/test/filestoreio.cpp +++ b/src/ox/fs/test/filestoreio.cpp @@ -13,8 +13,8 @@ using namespace ox::std; template int test() { - const uint16_t size = ~0; - uint8_t volume[size]; + const ox::std::uint16_t size = ~0; + ox::std::uint8_t volume[size]; char out[6]; typename FileStore::FsSize_t outSize; FileStore::format(volume, size); diff --git a/src/ox/fs/test/filesystem_format.cpp b/src/ox/fs/test/filesystem_format.cpp index 8052cb5ed..547cbdef8 100644 --- a/src/ox/fs/test/filesystem_format.cpp +++ b/src/ox/fs/test/filesystem_format.cpp @@ -13,8 +13,8 @@ using namespace ox::std; template int test() { - const uint16_t size = ~0; - uint8_t volume[size]; + const ox::std::uint16_t size = ~0; + ox::std::uint8_t volume[size]; FileSystem::format(volume, size); return 0; } diff --git a/src/ox/std/strops.cpp b/src/ox/std/strops.cpp index a3bcbd476..6c8b46e33 100644 --- a/src/ox/std/strops.cpp +++ b/src/ox/std/strops.cpp @@ -30,6 +30,17 @@ int strlen(const char *str1) { return len; } -} +int atoi(const char *str) { + int total = 0; + int multiplier = 1; + + for (int i = strlen(str) - 1; i >= 0; i--) { + total += (str[i] - '0') * multiplier; + multiplier *= 10; + } + + return total; } +} +} diff --git a/src/ox/std/strops.hpp b/src/ox/std/strops.hpp index e72e22237..8e00b840b 100644 --- a/src/ox/std/strops.hpp +++ b/src/ox/std/strops.hpp @@ -14,5 +14,7 @@ int strcmp(const char *str1, const char *str2); int strlen(const char *str1); +int atoi(const char *str); + } }