Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e976fd3fe6 | |||
| 9308b5e59c | |||
| df1605d189 | |||
| e3ff37c6c9 | |||
| 6e690ee98d | |||
| 9b7c68efc9 | |||
| 58400b950b | |||
| aa1b3d0a74 | |||
| b7775d3d82 | |||
| bf110e5341 | |||
| 9183815634 | |||
| c6e33e5285 | |||
| 709cfbf750 | |||
| 5c02645036 | |||
| 75e4aaa3b4 | |||
| f2384e93c7 | |||
| 17f09ab84a | |||
| 4fde40ece9 | |||
| 5e80cc80b8 |
+3
-1
@@ -7,9 +7,11 @@ include(address_sanitizer)
|
||||
|
||||
set(OX_BUILD_EXEC "ON" CACHE STRING "Build executables (ON/OFF)")
|
||||
set(OX_RUN_TESTS "ON" CACHE STRING "Run tests (ON/OFF)")
|
||||
set(OX_USE_STDLIB "ON" CACHE STRING "Build libraries that need the std lib (ON/OFF)")
|
||||
|
||||
# can't run tests without building them
|
||||
if(OX_BUILD_EXEC STREQUAL "OFF")
|
||||
if(OX_BUILD_EXEC STREQUAL "OFF" OR OX_USE_STDLIB STREQUAL "OFF")
|
||||
set(OX_BUILD_EXEC "OFF")
|
||||
set(OX_RUN_TESTS "OFF")
|
||||
endif()
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ BUILD_TYPE=$2
|
||||
if [[ $TARGET == windows ]]; then
|
||||
toolchain="-DCMAKE_TOOLCHAIN_FILE=cmake/Modules/Mingw.cmake"
|
||||
elif [[ $TARGET == gba ]]; then
|
||||
toolchain="-DCMAKE_TOOLCHAIN_FILE=cmake/Modules/GBA.cmake -DOX_BUILD_EXEC=OFF"
|
||||
toolchain="-DCMAKE_TOOLCHAIN_FILE=cmake/Modules/GBA.cmake -DOX_USE_STDLIB=OFF -DCMAKE_INSTALL_PREFIX=$DEVKITARM"
|
||||
fi
|
||||
|
||||
if [[ $BUILD_TYPE == debug ]]; then
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
add_subdirectory(clargs)
|
||||
if(OX_USE_STDLIB STREQUAL "ON")
|
||||
add_subdirectory(clargs)
|
||||
endif(OX_USE_STDLIB STREQUAL "ON")
|
||||
add_subdirectory(fs)
|
||||
add_subdirectory(std)
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace ox {
|
||||
namespace clargs {
|
||||
|
||||
using ::std::string;
|
||||
using namespace ::std;
|
||||
|
||||
ClArgs::ClArgs(int argc, const char **args) {
|
||||
@@ -22,17 +21,17 @@ ClArgs::ClArgs(int argc, const char **args) {
|
||||
while (arg[0] == '-' && arg.size()) {
|
||||
arg = arg.substr(1);
|
||||
}
|
||||
m_bools[arg.c_str()] = true;
|
||||
m_bools[arg] = true;
|
||||
|
||||
// parse additional arguments
|
||||
if (i < argc) {
|
||||
if (i < argc && args[i + 1]) {
|
||||
string val = args[i + 1];
|
||||
if (val[i] != '-') {
|
||||
if (val.size() && val[i] != '-') {
|
||||
if (val == "false") {
|
||||
m_bools[arg.c_str()] = false;
|
||||
m_bools[arg] = false;
|
||||
}
|
||||
m_strings[arg.c_str()] = val.c_str();
|
||||
m_ints[arg.c_str()] = ox_atoi(val.c_str());
|
||||
m_strings[arg] = val;
|
||||
m_ints[arg] = ox_atoi(val.c_str());
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -44,7 +43,7 @@ bool ClArgs::getBool(const char *arg) {
|
||||
return m_bools[arg];
|
||||
}
|
||||
|
||||
const char *ClArgs::getString(const char *arg) {
|
||||
string ClArgs::getString(const char *arg) {
|
||||
return m_strings[arg];
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace clargs {
|
||||
class ClArgs {
|
||||
private:
|
||||
::std::map<::std::string, bool> m_bools;
|
||||
::std::map<::std::string, const char*> m_strings;
|
||||
::std::map<::std::string, ::std::string> m_strings;
|
||||
::std::map<::std::string, int> m_ints;
|
||||
|
||||
public:
|
||||
@@ -25,7 +25,7 @@ class ClArgs {
|
||||
|
||||
bool getBool(const char *arg);
|
||||
|
||||
const char *getString(const char *arg);
|
||||
::std::string getString(const char *arg);
|
||||
|
||||
int getInt(const char *arg);
|
||||
};
|
||||
|
||||
+92
-19
@@ -13,11 +13,11 @@ namespace ox {
|
||||
namespace fs {
|
||||
|
||||
template<typename FsT, typename InodeId>
|
||||
struct FileStoreHeader {
|
||||
struct __attribute__((packed)) FileStoreHeader {
|
||||
public:
|
||||
typedef InodeId InodeId_t;
|
||||
typedef FsT FsSize_t;
|
||||
const static auto VERSION = 4;
|
||||
const static auto VERSION = 5;
|
||||
|
||||
private:
|
||||
uint16_t m_version;
|
||||
@@ -103,20 +103,22 @@ class FileStore {
|
||||
|
||||
struct StatInfo {
|
||||
InodeId_t inodeId;
|
||||
typename Header::FsSize_t size;
|
||||
typename Header::FsSize_t size;
|
||||
uint8_t fileType;
|
||||
};
|
||||
|
||||
private:
|
||||
struct Inode {
|
||||
struct __attribute__((packed)) Inode {
|
||||
private:
|
||||
// the next Inode in memory
|
||||
typename Header::FsSize_t m_prev, m_next;
|
||||
typename Header::FsSize_t m_prev;
|
||||
typename Header::FsSize_t m_next;
|
||||
typename Header::FsSize_t m_dataLen;
|
||||
|
||||
InodeId_t m_id;
|
||||
uint8_t m_fileType;
|
||||
typename Header::FsSize_t m_left, m_right;
|
||||
typename Header::FsSize_t m_left;
|
||||
typename Header::FsSize_t m_right;
|
||||
|
||||
public:
|
||||
typename Header::FsSize_t size();
|
||||
@@ -143,7 +145,7 @@ class FileStore {
|
||||
typename Header::FsSize_t getRight();
|
||||
|
||||
void setData(void *data, typename Header::FsSize_t size);
|
||||
void *getData();
|
||||
uint8_t *getData();
|
||||
};
|
||||
|
||||
Header m_header;
|
||||
@@ -186,6 +188,35 @@ class FileStore {
|
||||
*/
|
||||
int read(InodeId_t id, void *data, typename Header::FsSize_t *size);
|
||||
|
||||
/**
|
||||
* Reads the "file" at the given id. You are responsible for freeing
|
||||
* the data when done with it.
|
||||
* @param id id of the "file"
|
||||
* @param readStart where in the data to start reading
|
||||
* @param readSize how much data to read
|
||||
* @param data pointer to the pointer where the data is stored
|
||||
* @param size pointer to a value that will be assigned the size of data
|
||||
* @return 0 if read is a success
|
||||
*/
|
||||
int read(InodeId_t id, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, void *data,
|
||||
typename Header::FsSize_t *size);
|
||||
|
||||
/**
|
||||
* Reads the "file" at the given id. You are responsible for freeing
|
||||
* the data when done with it.
|
||||
* @param id id of the "file"
|
||||
* @param readStart where in the data to start reading
|
||||
* @param readSize how much data to read
|
||||
* @param data pointer to the pointer where the data is stored
|
||||
* @param size pointer to a value that will be assigned the size of data
|
||||
* @return 0 if read is a success
|
||||
*/
|
||||
template<typename T>
|
||||
int read(InodeId_t id, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, T *data,
|
||||
typename Header::FsSize_t *size);
|
||||
|
||||
/**
|
||||
* Reads the stat information of the inode of the given inode id.
|
||||
* If the returned inode id is 0, then the requested inode was not found.
|
||||
@@ -200,7 +231,7 @@ class FileStore {
|
||||
* @param size the size of the data to insert
|
||||
* @return the space currently available in this file store.
|
||||
*/
|
||||
typename Header::FsSize_t spaceNeeded(InodeId_t id, typename Header::FsSize_t size);
|
||||
typename Header::FsSize_t spaceNeeded(typename Header::FsSize_t size);
|
||||
|
||||
/**
|
||||
* Returns the size of the file store.
|
||||
@@ -240,6 +271,21 @@ class FileStore {
|
||||
*/
|
||||
Inode *getInodeParent(Inode *root, InodeId_t id, typename Header::FsSize_t targetAddr);
|
||||
|
||||
/**
|
||||
* Reads the "file" at the given id. You are responsible for freeing
|
||||
* the data when done with it.
|
||||
* @param inode inode of the "file"
|
||||
* @param readStart where in the data to start reading
|
||||
* @param readSize how much data to read
|
||||
* @param data pointer to the pointer where the data is stored
|
||||
* @param size pointer to a value that will be assigned the size of data
|
||||
* @return 0 if read is a success
|
||||
*/
|
||||
template<typename T>
|
||||
int read(Inode *inode, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, T *data,
|
||||
typename Header::FsSize_t *size);
|
||||
|
||||
/**
|
||||
* Removes the inode of the given ID.
|
||||
* @param id the id of the file
|
||||
@@ -391,8 +437,8 @@ void FileStore<Header>::Inode::setData(void *data, typename Header::FsSize_t siz
|
||||
|
||||
|
||||
template<typename Header>
|
||||
void *FileStore<Header>::Inode::getData() {
|
||||
return this + 1;
|
||||
uint8_t *FileStore<Header>::Inode::getData() {
|
||||
return (uint8_t*) (this + 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -538,15 +584,42 @@ void FileStore<Header>::updateInodeAddress(InodeId_t id, typename Header::FsSize
|
||||
template<typename Header>
|
||||
int FileStore<Header>::read(InodeId_t id, void *data, typename Header::FsSize_t *size) {
|
||||
auto inode = getInode(ptr<Inode*>(m_header.getRootInode()), id);
|
||||
int retval = 1;
|
||||
if (inode) {
|
||||
if (size) {
|
||||
*size = inode->getDataLen();
|
||||
}
|
||||
ox_memcpy(data, inode->getData(), inode->getDataLen());
|
||||
retval = 0;
|
||||
return inode ? read(inode, 0, inode->getDataLen(), (uint8_t*) data, size) : 1;
|
||||
}
|
||||
|
||||
template<typename Header>
|
||||
int FileStore<Header>::read(InodeId_t id, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, void *data, typename Header::FsSize_t *size) {
|
||||
auto inode = getInode(ptr<Inode*>(m_header.getRootInode()), id);
|
||||
return inode ? read<uint8_t>(inode, readStart, readSize, (uint8_t*) data, size) : 1;
|
||||
}
|
||||
|
||||
template<typename Header>
|
||||
template<typename T>
|
||||
int FileStore<Header>::read(InodeId_t id, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, T *data, typename Header::FsSize_t *size) {
|
||||
auto inode = getInode(ptr<Inode*>(m_header.getRootInode()), id);
|
||||
return inode ? read(inode, readStart, readSize, data, size) : 1;
|
||||
}
|
||||
|
||||
template<typename Header>
|
||||
template<typename T>
|
||||
int FileStore<Header>::read(Inode *inode, typename Header::FsSize_t readStart,
|
||||
typename Header::FsSize_t readSize, T *data, typename Header::FsSize_t *size) {
|
||||
// be sure read size is not greater than what is available to read
|
||||
if (inode->getDataLen() - readStart < readSize) {
|
||||
readSize = inode->getDataLen() - readStart;
|
||||
}
|
||||
return retval;
|
||||
if (size) {
|
||||
*size = readSize;
|
||||
}
|
||||
|
||||
readSize /= sizeof(T);
|
||||
T *it = (T*) &(inode->getData()[readStart]);
|
||||
for (typename Header::FsSize_t i = 0; i < readSize; i++) {
|
||||
*(data++) = *(it++);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename Header>
|
||||
@@ -564,7 +637,7 @@ typename FileStore<Header>::StatInfo FileStore<Header>::stat(InodeId_t id) {
|
||||
}
|
||||
|
||||
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(typename Header::FsSize_t size) {
|
||||
return sizeof(Inode) + size;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +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/.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "filesystem.hpp"
|
||||
|
||||
namespace ox {
|
||||
namespace fs {
|
||||
|
||||
FileSystem *createFileSystem(void *buff) {
|
||||
FileSystem *createFileSystem(void *buff, size_t buffSize) {
|
||||
auto version = ((FileStore16*) buff)->version();
|
||||
auto type = ((FileStore16*) buff)->fsType();
|
||||
FileSystem *fs = nullptr;
|
||||
@@ -34,6 +33,11 @@ FileSystem *createFileSystem(void *buff) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (fs && fs->size() > buffSize) {
|
||||
delete fs;
|
||||
fs = nullptr;
|
||||
}
|
||||
|
||||
return fs;
|
||||
}
|
||||
|
||||
@@ -46,7 +50,7 @@ FileSystem *expandCopy(FileSystem *fs, size_t size) {
|
||||
ox_memcpy(cloneBuff, fsBuff, fs->size());
|
||||
|
||||
fsBuff = cloneBuff;
|
||||
retval = createFileSystem(fsBuff);
|
||||
retval = createFileSystem(fsBuff, size);
|
||||
retval->resize(size);
|
||||
}
|
||||
|
||||
@@ -57,7 +61,7 @@ FileSystem *expandCopyCleanup(FileSystem *fs, size_t size) {
|
||||
auto out = expandCopy(fs, size);
|
||||
|
||||
if (out) {
|
||||
delete fs->buff();
|
||||
delete[] fs->buff();
|
||||
delete fs;
|
||||
} else {
|
||||
out = fs;
|
||||
|
||||
+35
-14
@@ -36,6 +36,8 @@ class FileSystem {
|
||||
|
||||
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) = 0;
|
||||
@@ -46,7 +48,7 @@ class FileSystem {
|
||||
|
||||
virtual FileStat stat(uint64_t inode) = 0;
|
||||
|
||||
virtual uint64_t spaceNeeded(uint64_t id, uint64_t size) = 0;
|
||||
virtual uint64_t spaceNeeded(uint64_t size) = 0;
|
||||
|
||||
virtual uint64_t available() = 0;
|
||||
|
||||
@@ -55,7 +57,7 @@ class FileSystem {
|
||||
virtual uint8_t *buff() = 0;
|
||||
};
|
||||
|
||||
FileSystem *createFileSystem(void *buff);
|
||||
FileSystem *createFileSystem(void *buff, size_t buffSize);
|
||||
|
||||
/**
|
||||
* Creates a larger version of the given FileSystem.
|
||||
@@ -104,15 +106,17 @@ class FileSystemTemplate: public FileSystem {
|
||||
FileStore *store = nullptr;
|
||||
|
||||
public:
|
||||
FileSystemTemplate(void *buff);
|
||||
explicit FileSystemTemplate(void *buff);
|
||||
|
||||
int mkdir(const char *path);
|
||||
|
||||
int read(const char *path, void *buffer);
|
||||
|
||||
uint8_t *read(uint64_t inode, size_t *size) override;
|
||||
int read(uint64_t inode, void *buffer, size_t buffSize) override;
|
||||
|
||||
int read(uint64_t inode, void *buffer, size_t size) override;
|
||||
int read(uint64_t inode, size_t readStart, size_t readSize, void *buffer, size_t *size) override;
|
||||
|
||||
uint8_t *read(uint64_t inode, size_t *size) override;
|
||||
|
||||
void resize(uint64_t size = 0) override;
|
||||
|
||||
@@ -124,7 +128,7 @@ class FileSystemTemplate: public FileSystem {
|
||||
|
||||
FileStat stat(uint64_t inode) override;
|
||||
|
||||
uint64_t spaceNeeded(uint64_t id, uint64_t size) override;
|
||||
uint64_t spaceNeeded(uint64_t size) override;
|
||||
|
||||
uint64_t available() override;
|
||||
|
||||
@@ -174,18 +178,35 @@ FileStat FileSystemTemplate<FileStore, FS_TYPE>::stat(uint64_t inode) {
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
template<typename FileStore, FsType FS_TYPE>
|
||||
int FileSystemTemplate<FileStore, FS_TYPE>::read(uint64_t inode, void *buffer, size_t size) {
|
||||
auto err = 1;
|
||||
auto s = store->stat(inode);
|
||||
if (size == s.size) {
|
||||
err = store->read(inode, buffer, nullptr);
|
||||
int FileSystemTemplate<FileStore, FS_TYPE>::read(uint64_t inode, void *buffer, size_t buffSize) {
|
||||
auto stat = store->stat(inode);
|
||||
if (stat.size <= buffSize) {
|
||||
return store->read(inode, buffer, nullptr);
|
||||
}
|
||||
return err;
|
||||
return 0;
|
||||
;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(default:4244)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
template<typename FileStore, FsType FS_TYPE>
|
||||
int FileSystemTemplate<FileStore, FS_TYPE>::read(uint64_t inode, size_t readStart,
|
||||
size_t readSize, void *buffer,
|
||||
size_t *size) {
|
||||
if (size) {
|
||||
auto stat = store->stat(inode);
|
||||
*size = stat.size;
|
||||
}
|
||||
return store->read(inode, readStart, readSize, buffer, nullptr);
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
@@ -234,8 +255,8 @@ void FileSystemTemplate<FileStore, FS_TYPE>::resize(uint64_t size) {
|
||||
}
|
||||
|
||||
template<typename FileStore, FsType FS_TYPE>
|
||||
uint64_t FileSystemTemplate<FileStore, FS_TYPE>::spaceNeeded(uint64_t id, uint64_t size) {
|
||||
return store->spaceNeeded(id, size);
|
||||
uint64_t FileSystemTemplate<FileStore, FS_TYPE>::spaceNeeded(uint64_t size) {
|
||||
return store->spaceNeeded(size);
|
||||
}
|
||||
|
||||
template<typename FileStore, FsType FS_TYPE>
|
||||
|
||||
@@ -148,7 +148,7 @@ int read(int argc, char **args) {
|
||||
auto fsBuff = loadFileBuff(fsPath, &fsSize);
|
||||
|
||||
if (fsBuff) {
|
||||
auto fs = createFileSystem(fsBuff);
|
||||
auto fs = createFileSystem(fsBuff, fsSize);
|
||||
|
||||
if (fs) {
|
||||
auto output = fs->read(inode, &fileSize);
|
||||
@@ -195,12 +195,13 @@ int write(int argc, char **args, bool expand) {
|
||||
auto srcBuff = loadFileBuff(srcPath, &srcSize);
|
||||
if (srcBuff) {
|
||||
auto expanded = false;
|
||||
auto fs = createFileSystem(fsBuff);
|
||||
auto fs = createFileSystem(fsBuff, fsSize);
|
||||
if (fs) {
|
||||
if (expand && fs->available() <= srcSize) {
|
||||
auto needed = fs->size() + fs->spaceNeeded(inode, srcSize);
|
||||
auto needed = fs->size() + fs->spaceNeeded(srcSize);
|
||||
fsSize = needed;
|
||||
fs = expandCopyCleanup(fs, needed);
|
||||
fsBuff = fs->buff();
|
||||
}
|
||||
err |= fs->write(inode, srcBuff, srcSize);
|
||||
|
||||
@@ -231,7 +232,7 @@ int write(int argc, char **args, bool expand) {
|
||||
err = 1;
|
||||
}
|
||||
}
|
||||
delete []fsBuff;
|
||||
delete []srcBuff;
|
||||
} else {
|
||||
err = 1;
|
||||
fprintf(stderr, "Could not load source file: %s.\n", srcPath);
|
||||
@@ -256,7 +257,7 @@ int compact(int argc, char **args) {
|
||||
|
||||
auto fsBuff = loadFileBuff(fsPath, &fsSize);
|
||||
if (fsBuff) {
|
||||
auto fs = createFileSystem(fsBuff);
|
||||
auto fs = createFileSystem(fsBuff, fsSize);
|
||||
|
||||
if (fs) {
|
||||
fs->resize();
|
||||
@@ -296,7 +297,7 @@ int remove(int argc, char **args) {
|
||||
|
||||
auto fsBuff = loadFileBuff(fsPath, &fsSize);
|
||||
if (fsBuff) {
|
||||
auto fs = createFileSystem(fsBuff);
|
||||
auto fs = createFileSystem(fsBuff, fsSize);
|
||||
|
||||
if (fs) {
|
||||
err = fs->remove(inode);
|
||||
|
||||
@@ -23,11 +23,9 @@ typedef unsigned long uint64_t;
|
||||
#endif
|
||||
|
||||
namespace ox {
|
||||
namespace std {
|
||||
|
||||
typedef uint32_t Error;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_LP64) || defined(__ppc64__) || defined(__aarch64__)
|
||||
|
||||
Reference in New Issue
Block a user