Add oxfstool with format option.

This commit is contained in:
2016-07-02 22:31:17 -05:00
parent 2d2d334f50
commit d5a1e5e00b
9 changed files with 126 additions and 31 deletions
+6
View File
@@ -7,6 +7,12 @@ add_library(
inodemgr.cpp
)
add_executable(
oxfstool
oxfstool.cpp
)
target_link_libraries(oxfstool OxFS OxStd)
install(
FILES
filestore.hpp
+22 -24
View File
@@ -12,13 +12,11 @@
namespace ox {
namespace fs {
using namespace ox::std;
template<typename FsT>
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<FsSize_t>::Inode::setId(InodeId_t id) {
template<typename FsSize_t>
void FileStore<FsSize_t>::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<FsSize_t>::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<FsSize_t>::Inode *FileStore<FsSize_t>::getInode(Inode *root,
template<typename FsSize_t>
void *FileStore<FsSize_t>::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<Inode*>(retval);
memset(inode, 0, size);
ox::std::memset(inode, 0, size);
inode->next = retval + size;
ptr<Inode*>(m_firstInode)->prev = retval;
return inode;
@@ -351,7 +349,7 @@ void FileStore<FsSize_t>::compress() {
current = ptr<Inode*>(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<FsSize_t>::iterator() {
template<typename FsSize_t>
FsSize_t FileStore<FsSize_t>::ptr(void *ptr) {
return ((uint8_t*) ptr) - begin();
return ((ox::std::uint8_t*) ptr) - begin();
}
template<typename FsSize_t>
@@ -401,13 +399,13 @@ typename FileStore<FsSize_t>::Inode *FileStore<FsSize_t>::lastInode() {
}
template<typename FsSize_t>
uint8_t FileStore<FsSize_t>::version() {
ox::std::uint8_t FileStore<FsSize_t>::version() {
return 1;
};
template<typename FsSize_t>
uint8_t *FileStore<FsSize_t>::format(uint8_t *buffer, FsSize_t size, uint32_t fsType) {
memset(buffer, 0, size);
ox::std::uint8_t *FileStore<FsSize_t>::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<FsSize_t>::format(uint8_t *buffer, FsSize_t size, uint32_t fs
fs->firstInode()->prev = fs->m_firstInode;
fs->lastInode()->next = sizeof(FileStore<FsSize_t>);
return (uint8_t*) buffer;
return (ox::std::uint8_t*) buffer;
}
typedef FileStore<uint16_t> FileStore16;
typedef FileStore<uint32_t> FileStore32;
typedef FileStore<uint64_t> FileStore64;
typedef FileStore<ox::std::uint16_t> FileStore16;
typedef FileStore<ox::std::uint32_t> FileStore32;
typedef FileStore<ox::std::uint64_t> FileStore64;
}
}
+6
View File
@@ -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;
+72
View File
@@ -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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ox/std/strops.hpp>
#include <ox/fs/filesystem.hpp>
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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -13,8 +13,8 @@ using namespace ox::std;
template<typename FileStore>
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);
+2 -2
View File
@@ -13,8 +13,8 @@ using namespace ox::std;
template<typename FileSystem>
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;
}
+12 -1
View File
@@ -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;
}
}
}
+2
View File
@@ -14,5 +14,7 @@ int strcmp(const char *str1, const char *str2);
int strlen(const char *str1);
int atoi(const char *str);
}
}