Merge branch 'master' of github.com:wombatant/ox

This commit is contained in:
2017-04-22 02:46:51 -05:00
14 changed files with 262 additions and 12 deletions
+7
View File
@@ -5,6 +5,13 @@ add_library(
clargs.cpp
)
set_property(
TARGET
OxClArgs
PROPERTY
POSITION_INDEPENDENT_CODE ON
)
install(
FILES
clargs.hpp
+9
View File
@@ -3,6 +3,14 @@ cmake_minimum_required(VERSION 2.8)
add_library(
OxFS
filesystem.cpp
pathiterator.cpp
)
set_property(
TARGET
OxFS
PROPERTY
POSITION_INDEPENDENT_CODE ON
)
if(OX_BUILD_EXEC STREQUAL "ON")
@@ -19,6 +27,7 @@ install(
filestore.hpp
filesystem.hpp
inodemgr.hpp
pathiterator.hpp
DESTINATION
include/ox/fs
)
+35 -5
View File
@@ -17,7 +17,7 @@ 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;
@@ -202,6 +202,21 @@ class FileStore {
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.
@@ -266,8 +281,9 @@ class FileStore {
* @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, void *data,
typename Header::FsSize_t readSize, T *data,
typename Header::FsSize_t *size);
/**
@@ -568,19 +584,28 @@ 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);
return inode ? read(inode, 0, inode->getDataLen(), data, size) : 1;
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, void *data, typename Header::FsSize_t *size) {
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;
@@ -588,7 +613,12 @@ int FileStore<Header>::read(Inode *inode, typename Header::FsSize_t readStart,
if (size) {
*size = readSize;
}
ox_memcpy(data, inode->getData() + readStart, readSize);
readSize /= sizeof(T);
T *it = (T*) &(inode->getData()[readStart]);
for (typename Header::FsSize_t i = 0; i < readSize; i++) {
*(data++) = *(it++);
}
return 0;
}
+1 -1
View File
@@ -16,7 +16,7 @@ FileSystem *createFileSystem(void *buff, size_t buffSize) {
FileSystem *fs = nullptr;
switch (version) {
case 4:
case 5:
switch (type) {
case ox::fs::OxFS_16:
fs = new FileSystem16(buff);
+15 -4
View File
@@ -74,7 +74,7 @@ template<typename FileStore, FsType FS_TYPE>
class FileSystemTemplate: public FileSystem {
private:
struct DirectoryEntry {
struct __attribute__((packed)) DirectoryEntry {
typename FileStore::InodeId_t inode;
char *getName() {
@@ -89,7 +89,7 @@ class FileSystemTemplate: public FileSystem {
}
};
struct Directory {
struct __attribute__((packed)) Directory {
/**
* Number of files in this directory.
*/
@@ -106,11 +106,11 @@ 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);
int read(const char *path, void *buffer, size_t buffSize);
int read(uint64_t inode, void *buffer, size_t buffSize) override;
@@ -174,6 +174,17 @@ FileStat FileSystemTemplate<FileStore, FS_TYPE>::stat(uint64_t inode) {
#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(const char *path, void *buffer, size_t buffSize) {
return 0;
}
#ifdef _MSC_VER
#pragma warning(default:4244)
#endif
#ifdef _MSC_VER
#pragma warning(disable:4244)
#endif
+39
View File
@@ -0,0 +1,39 @@
/*
* 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/memops.hpp>
#include <ox/std/strops.hpp>
#include "pathiterator.hpp"
namespace ox {
namespace fs {
PathIterator::PathIterator(const char *path, size_t maxSize) {
m_path = path;
m_maxSize = maxSize;
}
int PathIterator::next(char *pathOut, size_t pathOutSize) {
int size = 0;
const char *substr = ox_strchar(m_path + m_iterator, '/', m_maxSize - m_iterator);
m_iterator = (substr - m_path) + 1;
if (substr && m_iterator < m_maxSize) {
int start = m_iterator;
int end = (ox_strchar(m_path + start, '/', m_maxSize - start) - m_path);
if (end < 0) {
end = m_maxSize;
}
size = end - start;
ox_memcpy(pathOut, &m_path[start], size);
}
pathOut[size] = 0; // end with null terminator
return 0;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
/*
* 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 <ox/std/types.hpp>
namespace ox {
namespace fs {
class PathIterator {
private:
const char *m_path = nullptr;
int m_iterator = 0;
int m_maxSize = 0;
public:
PathIterator(const char *path, size_t maxSize);
int next(char *pathOut, size_t pathOutSize);
};
}
}
+9
View File
@@ -15,10 +15,19 @@ add_executable(
filestoreio.cpp
)
add_executable(
FSTests
tests.cpp
)
target_link_libraries(FileStoreFormat OxFS OxStd)
target_link_libraries(FileSystemFormat OxFS OxStd)
target_link_libraries(FileStoreIO OxFS OxStd)
target_link_libraries(FSTests OxFS OxStd)
add_test("FileStoreFormat" FileStoreFormat)
add_test("FileSystemFormat" FileSystemFormat)
add_test("FileStoreIO" FileStoreIO)
add_test("Test\\ PathIterator1" FSTests PathIterator1)
add_test("Test\\ PathIterator2" FSTests PathIterator2)
add_test("Test\\ PathIterator3" FSTests PathIterator3)
+80
View File
@@ -0,0 +1,80 @@
/*
* 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 <assert.h>
#include <map>
#include <string>
#include <ox/fs/filesystem.hpp>
#include <ox/fs/pathiterator.hpp>
#include <ox/std/std.hpp>
using namespace std;
using namespace ox::fs;
using namespace ox::std;
map<string, int(*)(string)> tests = {
{
{
"PathIterator1",
[](string) {
int retval = 0;
string path = "/usr/share/charset.gbag";
PathIterator it(path.c_str(), path.size());
const auto buffSize = 1024;
char buff[buffSize];
assert(buffSize >= path.size());
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "usr") == 0);
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "share") == 0);
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "charset.gbag") == 0);
return retval;
}
},
{
"PathIterator2",
[](string) {
int retval = 0;
string path = "/usr/share/";
PathIterator it(path.c_str(), path.size());
const auto buffSize = 1024;
char buff[buffSize];
assert(buffSize >= path.size());
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "usr") == 0);
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "share") == 0);
return retval;
}
},
{
"PathIterator3",
[](string) {
int retval = 0;
string path = "/";
PathIterator it(path.c_str(), path.size());
const auto buffSize = 1024;
char buff[buffSize];
assert(buffSize >= path.size());
retval |= !(it.next(buff, path.size()) == 0 && ox_strcmp(buff, "\0") == 0);
return retval;
}
},
},
};
int main(int argc, const char **args) {
int retval = -1;
if (argc > 1) {
auto testName = args[1];
string testArg = "";
if (args[2]) {
testArg = args[2];
}
if (tests.find(testName) != tests.end()) {
retval = tests[testName](testArg);
}
}
return retval;
}
+7
View File
@@ -7,6 +7,13 @@ add_library(
strops.cpp
)
set_property(
TARGET
OxStd
PROPERTY
POSITION_INDEPENDENT_CODE ON
)
install(
FILES
byteswap.hpp
+20 -2
View File
@@ -11,7 +11,7 @@
int ox_strcmp(const char *str1, const char *str2) {
auto retval = 0;
auto i = 0;
do {
while (str1[i] || str2[i]) {
if (str1[i] < str2[i]) {
retval = -1;
break;
@@ -20,7 +20,7 @@ int ox_strcmp(const char *str1, const char *str2) {
break;
}
i++;
} while (str1[i] || str2[i]);
}
return retval;
}
@@ -30,6 +30,24 @@ int ox_strlen(const char *str1) {
return len;
}
const char *ox_strchar(const char *str, int character, size_t maxLen) {
for (size_t i = 0; i < maxLen && str[i]; i++) {
if (str[i] == character) {
return &str[i];
}
}
return nullptr;
}
char *ox_strchar(char *str, int character, size_t maxLen) {
for (size_t i = 0; i < maxLen && str[i]; i++) {
if (str[i] == character) {
return &str[i];
}
}
return nullptr;
}
int ox_atoi(const char *str) {
int total = 0;
int multiplier = 1;
+4
View File
@@ -13,4 +13,8 @@ int ox_strcmp(const char *str1, const char *str2);
int ox_strlen(const char *str1);
const char *ox_strchar(const char *str, int character, size_t maxLen = 0xFFFFFFFF);
char *ox_strchar(char *str, int character, size_t maxLen = 0xFFFFFFFF);
int ox_atoi(const char *str);
+1
View File
@@ -28,6 +28,7 @@ add_test("Test\\ ox_strcmp\\ hijk\\ !=\\ asdf" StrOpsTest "hijk > asdf")
add_test("Test\\ ox_strcmp\\ read\\ !=\\ resize" StrOpsTest "read < resize")
add_test("Test\\ ox_strcmp\\ resize\\ !=\\ read" StrOpsTest "resize > read")
add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest "resize == resize")
add_test("Test\\ ox_strcmp\\ resize\\ ==\\ resize" StrOpsTest " == ")
################################################################################
+6
View File
@@ -43,6 +43,12 @@ map<string, function<int()>> tests = {
return !(ox_strcmp("resize", "resize") == 0);
}
},
{
" == ",
[]() {
return !(ox_strcmp("", "") == 0);
}
},
};
int main(int argc, const char **args) {