Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7fdf5751b1 | |||
| 0402fac389 | |||
| 9328113fcd | |||
| 9f3b338fed | |||
| 0052cccc14 | |||
| e25d4a67da | |||
| 78edb14d76 | |||
| 416883b354 | |||
| 8eb73298f5 |
+9
-7
@@ -1,9 +1,11 @@
|
|||||||
if(${CMAKE_FIND_ROOT_PATH})
|
if("${CMAKE_FIND_ROOT_PATH}" STREQUAL "")
|
||||||
set(Ox_INCLUDE_DIRS ${CMAKE_FIND_ROOT_PATH}/include/)
|
|
||||||
set(OxStd_LIBRARY ${CMAKE_FIND_ROOT_PATH}/lib/ox/libOxStd.a)
|
|
||||||
set(OxFs_LIBRARY ${CMAKE_FIND_ROOT_PATH}/lib/ox/libOxFs.a)
|
|
||||||
else(${CMAKE_FIND_ROOT_PATH})
|
|
||||||
set(Ox_INCLUDE_DIRS /usr/local/include/)
|
set(Ox_INCLUDE_DIRS /usr/local/include/)
|
||||||
set(OxStd_LIBRARY /usr/local/lib/ox/libOxStd.a)
|
set(OxStd_LIBRARY /usr/local/lib/ox/libOxStd.a)
|
||||||
set(OxFs_LIBRARY /usr/local/lib/ox/libOxFs.a)
|
set(OxFS_LIBRARY /usr/local/lib/ox/libOxFS.a)
|
||||||
endif(${CMAKE_FIND_ROOT_PATH})
|
set(OxClArgs_LIBRARY /usr/local/lib/ox/libOxClArgs.a)
|
||||||
|
else("${CMAKE_FIND_ROOT_PATH}" STREQUAL "")
|
||||||
|
set(Ox_INCLUDE_DIRS ${CMAKE_FIND_ROOT_PATH}/include/)
|
||||||
|
set(OxStd_LIBRARY ${CMAKE_FIND_ROOT_PATH}/lib/ox/libOxStd.a)
|
||||||
|
set(OxFS_LIBRARY ${CMAKE_FIND_ROOT_PATH}/lib/ox/libOxFS.a)
|
||||||
|
set(OxClArgs_LIBRARY ${CMAKE_FIND_ROOT_PATH}/lib/ox/libOxClArgs.a)
|
||||||
|
endif("${CMAKE_FIND_ROOT_PATH}" STREQUAL "")
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 2.8)
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
|
||||||
|
add_subdirectory(clargs)
|
||||||
add_subdirectory(fs)
|
add_subdirectory(fs)
|
||||||
add_subdirectory(std)
|
add_subdirectory(std)
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
|
||||||
|
add_library(
|
||||||
|
OxClArgs
|
||||||
|
clargs.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
install(
|
||||||
|
FILES
|
||||||
|
clargs.hpp
|
||||||
|
DESTINATION
|
||||||
|
include/ox/clargs
|
||||||
|
)
|
||||||
|
|
||||||
|
install(
|
||||||
|
TARGETS
|
||||||
|
OxClArgs
|
||||||
|
LIBRARY DESTINATION lib/ox
|
||||||
|
ARCHIVE DESTINATION lib/ox
|
||||||
|
)
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ox/std/strops.hpp>
|
||||||
|
#include "clargs.hpp"
|
||||||
|
|
||||||
|
namespace ox {
|
||||||
|
namespace clargs {
|
||||||
|
|
||||||
|
using ::std::string;
|
||||||
|
using namespace ::std;
|
||||||
|
|
||||||
|
ClArgs::ClArgs(int argc, const char **args) {
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
string arg = args[i];
|
||||||
|
if (arg[0] == '-') {
|
||||||
|
while (arg[0] == '-' && arg.size()) {
|
||||||
|
arg = arg.substr(1);
|
||||||
|
}
|
||||||
|
m_bools[arg.c_str()] = true;
|
||||||
|
|
||||||
|
// parse additional arguments
|
||||||
|
if (i < argc) {
|
||||||
|
string val = args[i + 1];
|
||||||
|
if (val[i] != '-') {
|
||||||
|
if (val == "false") {
|
||||||
|
m_bools[arg.c_str()] = false;
|
||||||
|
}
|
||||||
|
m_strings[arg.c_str()] = val.c_str();
|
||||||
|
m_ints[arg.c_str()] = ox_atoi(val.c_str());
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClArgs::getBool(const char *arg) {
|
||||||
|
return m_bools[arg];
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *ClArgs::getString(const char *arg) {
|
||||||
|
return m_strings[arg];
|
||||||
|
}
|
||||||
|
|
||||||
|
int ClArgs::getInt(const char *arg) {
|
||||||
|
return m_ints[arg];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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 <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ox {
|
||||||
|
namespace clargs {
|
||||||
|
|
||||||
|
class ClArgs {
|
||||||
|
private:
|
||||||
|
::std::map<::std::string, bool> m_bools;
|
||||||
|
::std::map<::std::string, const char*> m_strings;
|
||||||
|
::std::map<::std::string, int> m_ints;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ClArgs(int argc, const char **args);
|
||||||
|
|
||||||
|
bool getBool(const char *arg);
|
||||||
|
|
||||||
|
const char *getString(const char *arg);
|
||||||
|
|
||||||
|
int getInt(const char *arg);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-35
@@ -276,15 +276,9 @@ class FileStore {
|
|||||||
*/
|
*/
|
||||||
bool insert(Inode *root, Inode *insertValue);
|
bool insert(Inode *root, Inode *insertValue);
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the FsSize_t associated with the next Inode to be allocated.
|
|
||||||
* @retrun the FsSize_t associated with the next Inode to be allocated
|
|
||||||
*/
|
|
||||||
typename Header::FsSize_t iterator();
|
|
||||||
|
|
||||||
typename Header::FsSize_t firstInode();
|
typename Header::FsSize_t firstInode();
|
||||||
|
|
||||||
Inode *lastInode();
|
typename Header::FsSize_t lastInode();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the address of the inode in the tree.
|
* Updates the address of the inode in the tree.
|
||||||
@@ -326,7 +320,7 @@ void FileStore<Header>::Inode::setDataLen(typename Header::FsSize_t dataLen) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::Inode::getDataLen() {
|
typename Header::FsSize_t FileStore<Header>::Inode::getDataLen() {
|
||||||
return std::bigEndianAdapt(this->m_dataLen);
|
return std::bigEndianAdapt(m_dataLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -336,7 +330,7 @@ void FileStore<Header>::Inode::setPrev(typename Header::FsSize_t prev) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::Inode::getPrev() {
|
typename Header::FsSize_t FileStore<Header>::Inode::getPrev() {
|
||||||
return std::bigEndianAdapt(this->m_prev);
|
return std::bigEndianAdapt(m_prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -346,7 +340,7 @@ void FileStore<Header>::Inode::setNext(typename Header::FsSize_t next) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::Inode::getNext() {
|
typename Header::FsSize_t FileStore<Header>::Inode::getNext() {
|
||||||
return std::bigEndianAdapt(this->m_next);
|
return std::bigEndianAdapt(m_next);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -356,7 +350,7 @@ void FileStore<Header>::Inode::setId(InodeId_t id) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::InodeId_t FileStore<Header>::Inode::getId() {
|
typename Header::InodeId_t FileStore<Header>::Inode::getId() {
|
||||||
return std::bigEndianAdapt(this->m_id);
|
return std::bigEndianAdapt(m_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -366,7 +360,7 @@ void FileStore<Header>::Inode::setFileType(uint8_t fileType) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
uint8_t FileStore<Header>::Inode::getFileType() {
|
uint8_t FileStore<Header>::Inode::getFileType() {
|
||||||
return std::bigEndianAdapt(this->m_fileType);
|
return std::bigEndianAdapt(m_fileType);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -376,7 +370,7 @@ void FileStore<Header>::Inode::setLeft(typename Header::FsSize_t left) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::Inode::getLeft() {
|
typename Header::FsSize_t FileStore<Header>::Inode::getLeft() {
|
||||||
return std::bigEndianAdapt(this->m_left);
|
return std::bigEndianAdapt(m_left);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -386,12 +380,12 @@ void FileStore<Header>::Inode::setRight(typename Header::FsSize_t right) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::Inode::getRight() {
|
typename Header::FsSize_t FileStore<Header>::Inode::getRight() {
|
||||||
return std::bigEndianAdapt(this->m_right);
|
return std::bigEndianAdapt(m_right);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
void FileStore<Header>::Inode::setData(void *data, typename Header::FsSize_t size) {
|
void FileStore<Header>::Inode::setData(void *data, typename Header::FsSize_t size) {
|
||||||
ox_memcpy(this->getData(), data, size);
|
ox_memcpy(getData(), data, size);
|
||||||
setDataLen(size);
|
setDataLen(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -571,12 +565,7 @@ typename FileStore<Header>::StatInfo FileStore<Header>::stat(InodeId_t id) {
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::spaceNeeded(InodeId_t id, typename Header::FsSize_t size) {
|
typename Header::FsSize_t FileStore<Header>::spaceNeeded(InodeId_t id, typename Header::FsSize_t size) {
|
||||||
typename Header::FsSize_t needed = sizeof(Inode) + size;;
|
return sizeof(Inode) + size;
|
||||||
auto inode = getInode(ptr<Inode*>(m_header.getRootInode()), id);
|
|
||||||
if (inode) {
|
|
||||||
needed -= inode->size();
|
|
||||||
}
|
|
||||||
return needed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -635,17 +624,16 @@ typename FileStore<Header>::Inode *FileStore<Header>::getInodeParent(Inode *root
|
|||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::nextInodeAddr() {
|
typename Header::FsSize_t FileStore<Header>::nextInodeAddr() {
|
||||||
typename Header::FsSize_t next = ptr(lastInode()) + lastInode()->size();
|
return lastInode() + ptr<Inode*>(lastInode())->size();
|
||||||
return next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
void *FileStore<Header>::alloc(typename Header::FsSize_t size) {
|
void *FileStore<Header>::alloc(typename Header::FsSize_t size) {
|
||||||
typename Header::FsSize_t next = nextInodeAddr();
|
auto next = nextInodeAddr();
|
||||||
if ((next + size) > (uint64_t) end()) {
|
if ((next + size) > ptr(end())) {
|
||||||
compact();
|
compact();
|
||||||
next = nextInodeAddr();
|
next = nextInodeAddr();
|
||||||
if ((next + size) > (uint64_t) end()) {
|
if ((next + size) > ptr(end())) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -654,7 +642,7 @@ void *FileStore<Header>::alloc(typename Header::FsSize_t size) {
|
|||||||
const auto inode = ptr<Inode*>(retval);
|
const auto inode = ptr<Inode*>(retval);
|
||||||
ox_memset(inode, 0, size);
|
ox_memset(inode, 0, size);
|
||||||
inode->setPrev(ptr<Inode*>(firstInode())->getPrev());
|
inode->setPrev(ptr<Inode*>(firstInode())->getPrev());
|
||||||
inode->setNext(retval + size);
|
inode->setNext(firstInode());
|
||||||
m_header.setMemUsed(m_header.getMemUsed() + size);
|
m_header.setMemUsed(m_header.getMemUsed() + size);
|
||||||
ptr<Inode*>(firstInode())->setPrev(retval);
|
ptr<Inode*>(firstInode())->setPrev(retval);
|
||||||
return inode;
|
return inode;
|
||||||
@@ -702,11 +690,6 @@ bool FileStore<Header>::insert(Inode *root, Inode *insertValue) {
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
|
||||||
typename Header::FsSize_t FileStore<Header>::iterator() {
|
|
||||||
return ptr(lastInode()) + lastInode()->size();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename Header::FsSize_t FileStore<Header>::ptr(void *ptr) {
|
typename Header::FsSize_t FileStore<Header>::ptr(void *ptr) {
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@@ -724,8 +707,8 @@ typename Header::FsSize_t FileStore<Header>::firstInode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
typename FileStore<Header>::Inode *FileStore<Header>::lastInode() {
|
typename Header::FsSize_t FileStore<Header>::lastInode() {
|
||||||
return ptr<Inode*>(ptr<Inode*>(firstInode())->getPrev());
|
return ptr<Inode*>(firstInode())->getPrev();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Header>
|
template<typename Header>
|
||||||
@@ -749,7 +732,7 @@ uint8_t *FileStore<Header>::format(uint8_t *buffer, typename Header::FsSize_t si
|
|||||||
fs->m_header.setMemUsed(sizeof(FileStore<Header>) + sizeof(Inode));
|
fs->m_header.setMemUsed(sizeof(FileStore<Header>) + sizeof(Inode));
|
||||||
fs->m_header.setRootInode(sizeof(FileStore<Header>));
|
fs->m_header.setRootInode(sizeof(FileStore<Header>));
|
||||||
((Inode*) (fs + 1))->setPrev(sizeof(FileStore<Header>));
|
((Inode*) (fs + 1))->setPrev(sizeof(FileStore<Header>));
|
||||||
fs->lastInode()->setNext(sizeof(FileStore<Header>));
|
((Inode*) (fs + 1))->setNext(sizeof(FileStore<Header>));
|
||||||
|
|
||||||
return (uint8_t*) buffer;
|
return (uint8_t*) buffer;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/.
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
#include "filesystem.hpp"
|
#include "filesystem.hpp"
|
||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
@@ -36,5 +37,34 @@ FileSystem *createFileSystem(void *buff) {
|
|||||||
return fs;
|
return fs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileSystem *expandCopy(FileSystem *fs, size_t size) {
|
||||||
|
auto fsBuff = fs->buff();
|
||||||
|
FileSystem *retval = nullptr;
|
||||||
|
|
||||||
|
if (fs->size() <= size) {
|
||||||
|
auto cloneBuff = new uint8_t[size];
|
||||||
|
ox_memcpy(cloneBuff, fsBuff, fs->size());
|
||||||
|
|
||||||
|
fsBuff = cloneBuff;
|
||||||
|
retval = createFileSystem(fsBuff);
|
||||||
|
retval->resize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSystem *expandCopyCleanup(FileSystem *fs, size_t size) {
|
||||||
|
auto out = expandCopy(fs, size);
|
||||||
|
|
||||||
|
if (out) {
|
||||||
|
delete fs->buff();
|
||||||
|
delete fs;
|
||||||
|
} else {
|
||||||
|
out = fs;
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,10 +51,23 @@ class FileSystem {
|
|||||||
virtual uint64_t available() = 0;
|
virtual uint64_t available() = 0;
|
||||||
|
|
||||||
virtual uint64_t size() = 0;
|
virtual uint64_t size() = 0;
|
||||||
|
|
||||||
|
virtual uint8_t *buff() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
FileSystem *createFileSystem(void *buff);
|
FileSystem *createFileSystem(void *buff);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<typename FileStore, FsType FS_TYPE>
|
template<typename FileStore, FsType FS_TYPE>
|
||||||
class FileSystemTemplate: public FileSystem {
|
class FileSystemTemplate: public FileSystem {
|
||||||
|
|
||||||
@@ -117,6 +130,8 @@ class FileSystemTemplate: public FileSystem {
|
|||||||
|
|
||||||
uint64_t size() override;
|
uint64_t size() override;
|
||||||
|
|
||||||
|
uint8_t *buff() override;
|
||||||
|
|
||||||
static uint8_t *format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories);
|
static uint8_t *format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -233,6 +248,11 @@ uint64_t FileSystemTemplate<FileStore, FS_TYPE>::size() {
|
|||||||
return store->size();
|
return store->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename FileStore, FsType FS_TYPE>
|
||||||
|
uint8_t *FileSystemTemplate<FileStore, FS_TYPE>::buff() {
|
||||||
|
return (uint8_t*) store;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable:4244)
|
#pragma warning(disable:4244)
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+25
-27
@@ -30,13 +30,12 @@ const static auto usage = "usage:\n"
|
|||||||
"\toxfs compact <FS file>\n"
|
"\toxfs compact <FS file>\n"
|
||||||
"\toxfs version\n";
|
"\toxfs version\n";
|
||||||
|
|
||||||
char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
char *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) {
|
||||||
auto file = fopen(path, "rb");
|
|
||||||
if (file) {
|
if (file) {
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
const auto size = ftell(file);
|
const auto size = ftell(file);
|
||||||
rewind(file);
|
rewind(file);
|
||||||
auto buff = (char*) malloc(size);
|
auto buff = new char[size];
|
||||||
auto itemsRead = fread(buff, size, 1, file);
|
auto itemsRead = fread(buff, size, 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if (sizeOut) {
|
if (sizeOut) {
|
||||||
@@ -48,12 +47,17 @@ char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
||||||
|
return loadFileBuff(fopen(path, "rb"), sizeOut);
|
||||||
|
}
|
||||||
|
|
||||||
size_t bytes(const char *str) {
|
size_t bytes(const char *str) {
|
||||||
auto size = ::ox_strlen(str);
|
auto size = ::ox_strlen(str);
|
||||||
const auto lastChar = str[size-1];
|
const auto lastChar = str[size-1];
|
||||||
auto multiplier = 1;
|
auto multiplier = 1;
|
||||||
auto copy = new char[size];
|
char copy[size + 1];
|
||||||
ox_memcpy(copy, str, size);
|
ox_memcpy(copy, str, size + 1);
|
||||||
|
// parse size unit
|
||||||
if (lastChar < '0' || lastChar > '9') {
|
if (lastChar < '0' || lastChar > '9') {
|
||||||
copy[size-1] = 0;
|
copy[size-1] = 0;
|
||||||
switch (lastChar) {
|
switch (lastChar) {
|
||||||
@@ -73,9 +77,7 @@ size_t bytes(const char *str) {
|
|||||||
multiplier = -1;
|
multiplier = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto retval = ((size_t) ox_atoi(copy)) * multiplier;
|
return ox_atoi(copy) * multiplier;
|
||||||
delete []copy;
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int format(int argc, char **args) {
|
int format(int argc, char **args) {
|
||||||
@@ -83,13 +85,10 @@ int format(int argc, char **args) {
|
|||||||
auto err = 0;
|
auto err = 0;
|
||||||
if (argc >= 5) {
|
if (argc >= 5) {
|
||||||
auto type = ox_atoi(args[2]);
|
auto type = ox_atoi(args[2]);
|
||||||
cout << args[3] << endl;
|
|
||||||
auto size = bytes(args[3]);
|
auto size = bytes(args[3]);
|
||||||
auto path = args[4];
|
auto path = args[4];
|
||||||
auto buff = (uint8_t*) malloc(size);
|
auto buff = new uint8_t[size];
|
||||||
|
|
||||||
cout << "Size: " << size << " bytes\n";
|
|
||||||
cout << "Type: " << type << endl;
|
|
||||||
|
|
||||||
if (size < sizeof(FileStore64)) {
|
if (size < sizeof(FileStore64)) {
|
||||||
err = 1;
|
err = 1;
|
||||||
@@ -126,7 +125,7 @@ int format(int argc, char **args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buff);
|
delete []buff;
|
||||||
|
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
fprintf(stderr, "Created file system %s\n", path);
|
fprintf(stderr, "Created file system %s\n", path);
|
||||||
@@ -161,7 +160,7 @@ int read(int argc, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete fs;
|
delete fs;
|
||||||
free(fsBuff);
|
delete []fsBuff;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Invalid file system type: %d.\n", *(uint32_t*) fsBuff);
|
fprintf(stderr, "Invalid file system type: %d.\n", *(uint32_t*) fsBuff);
|
||||||
}
|
}
|
||||||
@@ -195,22 +194,21 @@ int write(int argc, char **args, bool expand) {
|
|||||||
if (itemsRead) {
|
if (itemsRead) {
|
||||||
auto srcBuff = loadFileBuff(srcPath, &srcSize);
|
auto srcBuff = loadFileBuff(srcPath, &srcSize);
|
||||||
if (srcBuff) {
|
if (srcBuff) {
|
||||||
|
auto expanded = false;
|
||||||
auto fs = createFileSystem(fsBuff);
|
auto fs = createFileSystem(fsBuff);
|
||||||
if (fs) {
|
if (fs) {
|
||||||
if (expand && fs->available() <= srcSize) {
|
if (expand && fs->available() <= srcSize) {
|
||||||
auto needed = fs->spaceNeeded(inode, srcSize);
|
auto needed = fs->size() + fs->spaceNeeded(inode, srcSize);
|
||||||
auto cloneBuff = new uint8_t[needed];
|
|
||||||
ox_memcpy(cloneBuff, fsBuff, fsSize);
|
|
||||||
|
|
||||||
delete fs;
|
|
||||||
delete []fsBuff;
|
|
||||||
|
|
||||||
fsBuff = cloneBuff;
|
|
||||||
fs = createFileSystem(fsBuff);
|
|
||||||
fsSize = needed;
|
fsSize = needed;
|
||||||
fs->resize(fsSize);
|
fs = expandCopyCleanup(fs, needed);
|
||||||
}
|
}
|
||||||
err |= fs->write(inode, srcBuff, srcSize);
|
err |= fs->write(inode, srcBuff, srcSize);
|
||||||
|
|
||||||
|
// compact the file system if it was expanded
|
||||||
|
if (expanded) {
|
||||||
|
fs->resize();
|
||||||
|
}
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr, "Could not write to file system.\n");
|
fprintf(stderr, "Could not write to file system.\n");
|
||||||
}
|
}
|
||||||
@@ -233,7 +231,7 @@ int write(int argc, char **args, bool expand) {
|
|||||||
err = 1;
|
err = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(srcBuff);
|
delete []fsBuff;
|
||||||
} else {
|
} else {
|
||||||
err = 1;
|
err = 1;
|
||||||
fprintf(stderr, "Could not load source file: %s.\n", srcPath);
|
fprintf(stderr, "Could not load source file: %s.\n", srcPath);
|
||||||
@@ -279,7 +277,7 @@ int compact(int argc, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete fs;
|
delete fs;
|
||||||
free(fsBuff);
|
delete []fsBuff;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Could not open file: %s\n", fsPath);
|
fprintf(stderr, "Could not open file: %s\n", fsPath);
|
||||||
}
|
}
|
||||||
@@ -322,7 +320,7 @@ int remove(int argc, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete fs;
|
delete fs;
|
||||||
free(fsBuff);
|
delete []fsBuff;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Could not open file: %s\n", fsPath);
|
fprintf(stderr, "Could not open file: %s\n", fsPath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <string>
|
||||||
#include <ox/std/std.hpp>
|
#include <ox/std/std.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|||||||
Reference in New Issue
Block a user