Start on new FileStore and add test
This commit is contained in:
parent
cd38c961a3
commit
e1305a240e
51
deps/ox/src/ox/fs/filestore/filestore.hpp
vendored
Normal file
51
deps/ox/src/ox/fs/filestore/filestore.hpp
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2015 - 2018 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 "linkedlist.hpp"
|
||||
|
||||
namespace ox::fs {
|
||||
|
||||
using InodeId_t = uintptr_t;
|
||||
|
||||
class FileStore {
|
||||
|
||||
public:
|
||||
struct StatInfo {
|
||||
InodeId_t inodeId;
|
||||
InodeId_t links;
|
||||
InodeId_t size;
|
||||
uint8_t fileType;
|
||||
};
|
||||
|
||||
virtual bool valid(InodeId_t size) = 0;
|
||||
|
||||
virtual void resize(InodeId_t size = 0) = 0;
|
||||
|
||||
virtual Error write(InodeId_t id, void *data, InodeId_t dataLen, uint8_t fileType = 0) = 0;
|
||||
|
||||
virtual Error incLinks(InodeId_t id) = 0;
|
||||
|
||||
virtual Error decLinks(InodeId_t id) = 0;
|
||||
|
||||
virtual Error read(InodeId_t id, void *data, InodeId_t *size) = 0;
|
||||
|
||||
virtual Error read(InodeId_t id, InodeId_t readStart, InodeId_t readSize, void *data, InodeId_t *size) = 0;
|
||||
|
||||
virtual StatInfo stat(InodeId_t id) = 0;
|
||||
|
||||
virtual InodeId_t spaceNeeded(InodeId_t size) = 0;
|
||||
|
||||
virtual InodeId_t size() = 0;
|
||||
|
||||
virtual InodeId_t available() = 0;
|
||||
|
||||
};
|
||||
|
||||
}
|
98
deps/ox/src/ox/fs/filestore/filestoretemplate.hpp
vendored
Normal file
98
deps/ox/src/ox/fs/filestore/filestoretemplate.hpp
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2015 - 2018 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 "filestore.hpp"
|
||||
#include "linkedlist.hpp"
|
||||
|
||||
namespace ox::fs {
|
||||
|
||||
template<typename size_t>
|
||||
class FileStoreTemplate: public FileStore {
|
||||
|
||||
private:
|
||||
ox::fs::LinkedList<size_t> *m_linkedList = nullptr;
|
||||
|
||||
public:
|
||||
FileStoreTemplate(void *buff);
|
||||
|
||||
bool valid(InodeId_t size);
|
||||
|
||||
void resize(InodeId_t size = 0);
|
||||
|
||||
Error write(InodeId_t id, void *data, InodeId_t dataLen, uint8_t fileType = 0);
|
||||
|
||||
Error incLinks(InodeId_t id);
|
||||
|
||||
Error decLinks(InodeId_t id);
|
||||
|
||||
Error read(InodeId_t id, void *data, InodeId_t *size);
|
||||
|
||||
Error read(InodeId_t id, InodeId_t readStart, InodeId_t readSize, void *data, InodeId_t *size);
|
||||
|
||||
StatInfo stat(InodeId_t id);
|
||||
|
||||
InodeId_t spaceNeeded(InodeId_t size);
|
||||
|
||||
InodeId_t size();
|
||||
|
||||
InodeId_t available();
|
||||
|
||||
};
|
||||
|
||||
template<typename size_t>
|
||||
FileStoreTemplate<size_t>::FileStoreTemplate(void *buff) {
|
||||
m_linkedList = static_cast<ox::fs::LinkedList<size_t>*>(buff);
|
||||
}
|
||||
|
||||
template<typename size_t>
|
||||
bool FileStoreTemplate<size_t>::valid(size_t size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void FileStoreTemplate<size_t>::resize(InodeId_t size = 0) {
|
||||
}
|
||||
|
||||
Error FileStoreTemplate<size_t>::write(InodeId_t id, void *data, InodeId_t dataLen, uint8_t fileType = 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Error FileStoreTemplate<size_t>::incLinks(InodeId_t id) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Error FileStoreTemplate<size_t>::decLinks(InodeId_t id) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Error FileStoreTemplate<size_t>::read(InodeId_t id, void *data, InodeId_t *size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Error FileStoreTemplate<size_t>::read(InodeId_t id, InodeId_t readStart, InodeId_t readSize, void *data, InodeId_t *size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
StatInfo FileStoreTemplate<size_t>::stat(InodeId_t id) {
|
||||
return {};
|
||||
}
|
||||
|
||||
InodeId_t FileStoreTemplate<size_t>::spaceNeeded(InodeId_t size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
InodeId_t FileStoreTemplate<size_t>::size() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
InodeId_t FileStoreTemplate<size_t>::available() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
75
deps/ox/src/ox/fs/filestore/linkedlist.hpp
vendored
75
deps/ox/src/ox/fs/filestore/linkedlist.hpp
vendored
@ -10,40 +10,46 @@
|
||||
|
||||
#include "ptr.hpp"
|
||||
|
||||
namespace ox {
|
||||
namespace fs {
|
||||
namespace ox::fs {
|
||||
|
||||
template<typename size_t>
|
||||
class LinkedList {
|
||||
class __attribute__((packed)) LinkedList {
|
||||
|
||||
public:
|
||||
struct __attribute__((packed)) Item {
|
||||
friend LinkedList;
|
||||
|
||||
public:
|
||||
ox::LittleEndian<size_t> size = sizeof(Item);
|
||||
|
||||
protected:
|
||||
ox::LittleEndian<size_t> prev = 0;
|
||||
ox::LittleEndian<size_t> next = 0;
|
||||
|
||||
explicit Item(size_t size) {
|
||||
this->size = size;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
struct Header {
|
||||
struct __attribute__((packed)) Header {
|
||||
ox::LittleEndian<size_t> size = sizeof(Header);
|
||||
ox::LittleEndian<size_t> firstItem = 0;
|
||||
};
|
||||
|
||||
struct Item {
|
||||
ox::LittleEndian<size_t> size = sizeof(Item);
|
||||
ox::LittleEndian<size_t> prev = 0;
|
||||
ox::LittleEndian<size_t> next = 0;
|
||||
|
||||
Item(size_t size) {
|
||||
this->size = size;
|
||||
}
|
||||
};
|
||||
|
||||
struct ItemPtr: public ox::fs::Ptr<Item, size_t> {
|
||||
inline ItemPtr(void *dataStart, void *dataEnd, size_t itemOffset, size_t size):
|
||||
Ptr<Item, size_t>(dataStart, dataEnd, itemOffset, size) {}
|
||||
inline ItemPtr(void *dataStart, size_t dataSize, size_t itemOffset, size_t size):
|
||||
Ptr<Item, size_t>(dataStart, dataSize, itemOffset, size) {
|
||||
}
|
||||
|
||||
inline ItemPtr(void *dataStart, void *dataEnd, size_t itemOffset) {
|
||||
inline ItemPtr(void *dataStart, size_t dataSize, size_t itemOffset) {
|
||||
// make sure this can be read as an Item, and then use Item::size for the size
|
||||
uint8_t *itemStart = static_cast<uint8_t*>(dataStart) + itemOffset;
|
||||
if (static_cast<uint8_t*>(dataEnd) - itemStart >= static_cast<size_t>(sizeof(Item))) {
|
||||
auto item = (Item*) (static_cast<uint8_t*>(dataStart) + itemOffset);
|
||||
ItemPtr(dataStart, dataEnd, itemOffset, item->size);
|
||||
auto itemSpace = dataSize - itemOffset;
|
||||
auto item = reinterpret_cast<Item*>(static_cast<uint8_t*>(dataStart) + itemOffset);
|
||||
if (itemSpace >= static_cast<size_t>(sizeof(Item)) and itemSpace >= item->size) {
|
||||
this->init(dataStart, dataSize, itemOffset, item->size);
|
||||
} else {
|
||||
ItemPtr(dataStart, dataEnd, 0, 0);
|
||||
this->init(dataStart, dataSize, 0, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -51,6 +57,10 @@ class LinkedList {
|
||||
Header m_header;
|
||||
|
||||
public:
|
||||
LinkedList() = default;
|
||||
|
||||
explicit LinkedList(size_t size);
|
||||
|
||||
ItemPtr firstItem();
|
||||
|
||||
ItemPtr prev(Item *item);
|
||||
@ -61,15 +71,22 @@ class LinkedList {
|
||||
|
||||
ItemPtr ptr(uint8_t *item);
|
||||
|
||||
//private:
|
||||
ItemPtr malloc(size_t size);
|
||||
|
||||
void free(ItemPtr item);
|
||||
|
||||
private:
|
||||
void compact(void (*cb)(ItemPtr itemMoved));
|
||||
|
||||
uint8_t *data();
|
||||
|
||||
};
|
||||
|
||||
template<typename size_t>
|
||||
LinkedList<size_t>::LinkedList(size_t size) {
|
||||
m_header.size = size;
|
||||
}
|
||||
|
||||
template<typename size_t>
|
||||
typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::firstItem() {
|
||||
return ptr(m_header.firstItem);
|
||||
@ -87,17 +104,20 @@ typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::next(Item *item) {
|
||||
|
||||
template<typename size_t>
|
||||
typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::ptr(size_t offset) {
|
||||
return ItemPtr(this, this + 1, offset);
|
||||
return ItemPtr(this, m_header.size, offset);
|
||||
}
|
||||
|
||||
template<typename size_t>
|
||||
typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::ptr(uint8_t *item) {
|
||||
return ItemPtr(this, this + 1, reinterpret_cast<size_t>(static_cast<uint8_t*>(item - static_cast<uint8_t*>(this))));
|
||||
return ItemPtr(this, m_header.size, reinterpret_cast<size_t>(item - static_cast<uint8_t*>(this)));
|
||||
}
|
||||
|
||||
template<typename size_t>
|
||||
typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::malloc(size_t size) {
|
||||
auto out = ItemPtr(this, this + 1, 0, size);
|
||||
if (!m_header.firstItem) {
|
||||
m_header.firstItem = sizeof(m_header);
|
||||
}
|
||||
auto out = ItemPtr(this, m_header.size, m_header.firstItem, size);
|
||||
if (out.valid()) {
|
||||
new (out) Item(size);
|
||||
}
|
||||
@ -106,7 +126,7 @@ typename LinkedList<size_t>::ItemPtr LinkedList<size_t>::malloc(size_t size) {
|
||||
|
||||
template<typename size_t>
|
||||
void LinkedList<size_t>::compact(void (*cb)(ItemPtr)) {
|
||||
auto src = ptr(firstItem());
|
||||
auto src = firstItem();
|
||||
auto dest = data();
|
||||
while (src.valid()) {
|
||||
// move node
|
||||
@ -135,4 +155,3 @@ uint8_t *LinkedList<size_t>::data() {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
54
deps/ox/src/ox/fs/filestore/ptr.hpp
vendored
54
deps/ox/src/ox/fs/filestore/ptr.hpp
vendored
@ -18,29 +18,13 @@ class Ptr {
|
||||
|
||||
private:
|
||||
uint8_t *m_dataStart = nullptr;
|
||||
uint8_t *m_dataEnd = nullptr;
|
||||
size_t m_itemOffset = 0;
|
||||
size_t m_itemStart = 0;
|
||||
size_t m_itemSize = 0;
|
||||
|
||||
public:
|
||||
inline Ptr() = default;
|
||||
|
||||
inline Ptr(void *dataStart, void *dataEnd, size_t itemOffset, size_t itemSize = sizeof(T)) {
|
||||
// do some sanity checks before assuming this is valid
|
||||
if (itemSize >= sizeof(T) and
|
||||
m_dataStart and
|
||||
m_dataStart < m_dataEnd and
|
||||
m_dataStart + m_itemOffset + m_itemSize < m_dataEnd) {
|
||||
m_dataStart = static_cast<uint8_t*>(dataStart);
|
||||
m_dataEnd = static_cast<uint8_t*>(dataEnd);
|
||||
m_itemOffset = itemOffset;
|
||||
m_itemSize = itemSize;
|
||||
}
|
||||
}
|
||||
|
||||
inline Ptr(void *dataStart, void *dataEnd, void *item, size_t itemSize = sizeof(T)) {
|
||||
Ptr(dataStart, dataEnd, reinterpret_cast<size_t>(static_cast<uint8_t*>(item) - static_cast<uint8_t*>(dataStart)), itemSize);
|
||||
}
|
||||
inline Ptr(void *dataStart, size_t dataSize, size_t itemStart, size_t itemSize = sizeof(T));
|
||||
|
||||
inline bool valid() const;
|
||||
|
||||
@ -60,11 +44,19 @@ class Ptr {
|
||||
|
||||
inline Ptr &operator+=(size_t offset);
|
||||
|
||||
protected:
|
||||
void init(void *dataStart, size_t dataSize, size_t itemStart, size_t itemSize);
|
||||
|
||||
};
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline Ptr<T, size_t>::Ptr(void *dataStart, size_t dataSize, size_t itemStart, size_t itemSize) {
|
||||
init(dataStart, dataSize, itemStart, itemSize);
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline bool Ptr<T, size_t>::valid() const {
|
||||
return m_dataStart and m_itemOffset;
|
||||
return m_dataStart and m_itemStart;
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
@ -74,23 +66,23 @@ inline size_t Ptr<T, size_t>::size() const {
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline size_t Ptr<T, size_t>::offset() const {
|
||||
return m_itemOffset;
|
||||
return m_itemStart;
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline T *Ptr<T, size_t>::operator->() const {
|
||||
return (T*) m_dataStart + m_itemOffset;
|
||||
return reinterpret_cast<T*>(m_dataStart + m_itemStart);
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline Ptr<T, size_t>::operator T*() const {
|
||||
return (T*) m_dataStart + m_itemOffset;
|
||||
return reinterpret_cast<T*>(m_dataStart + m_itemStart);
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline Ptr<T, size_t>::operator size_t() const {
|
||||
if (valid()) {
|
||||
return m_itemOffset;
|
||||
return m_itemStart;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -102,15 +94,27 @@ inline T &Ptr<T, size_t>::operator*() const {
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline Ptr<T, size_t> &Ptr<T, size_t>::operator=(size_t offset) {
|
||||
m_itemOffset = offset;
|
||||
m_itemStart = offset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
inline Ptr<T, size_t> &Ptr<T, size_t>::operator+=(size_t offset) {
|
||||
m_itemOffset += offset;
|
||||
m_itemStart += offset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, typename size_t>
|
||||
void Ptr<T, size_t>::init(void *dataStart, size_t dataSize, size_t itemStart, size_t itemSize) {
|
||||
// do some sanity checks before assuming this is valid
|
||||
if (itemSize >= sizeof(T) and
|
||||
dataStart and
|
||||
itemStart + itemSize <= dataSize) {
|
||||
m_dataStart = static_cast<uint8_t*>(dataStart);
|
||||
m_itemStart = itemStart;
|
||||
m_itemSize = itemSize;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
2
deps/ox/src/ox/fs/test/CMakeLists.txt
vendored
2
deps/ox/src/ox/fs/test/CMakeLists.txt
vendored
@ -67,3 +67,5 @@ add_test("Test\\ FileSystem32::remove\\(string,\\ true\\)" FSTests "FileSystem32
|
||||
add_test("Test\\ FileSystem32::move" FSTests "FileSystem32::move")
|
||||
add_test("Test\\ FileSystem32::stripDirectories" FSTests "FileSystem32::stripDirectories")
|
||||
add_test("Test\\ FileSystem32::ls" FSTests "FileSystem32::ls")
|
||||
|
||||
add_test("Test\\ LinkedList::insert" FSTests "LinkedList::insert")
|
||||
|
11
deps/ox/src/ox/fs/test/tests.cpp
vendored
11
deps/ox/src/ox/fs/test/tests.cpp
vendored
@ -13,7 +13,7 @@
|
||||
#include <string>
|
||||
#include <ox/fs/fs.hpp>
|
||||
#include <ox/std/std.hpp>
|
||||
#include <ox/fs/filestore/linkedlist.hpp>
|
||||
#include <ox/fs/filestore/filestore.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace ox;
|
||||
@ -330,9 +330,12 @@ map<string, int(*)(string)> tests = {
|
||||
{
|
||||
"LinkedList::insert",
|
||||
[](string) {
|
||||
ox::fs::LinkedList<uint32_t> list;
|
||||
list.malloc(50);
|
||||
list.firstItem();
|
||||
constexpr auto buffLen = 5000;
|
||||
uint8_t buff[buffLen];
|
||||
auto list = new (buff) ox::fs::LinkedList<uint32_t>(buffLen);
|
||||
assert(list->malloc(50).valid());
|
||||
assert(list->firstItem().valid());
|
||||
assert(list->firstItem()->size == 50);
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
|
10
deps/ox/src/ox/std/byteswap.hpp
vendored
10
deps/ox/src/ox/std/byteswap.hpp
vendored
@ -79,7 +79,7 @@ inline T bigEndianAdapt(T i) {
|
||||
|
||||
|
||||
template<typename T>
|
||||
class LittleEndian {
|
||||
class __attribute__((packed)) LittleEndian {
|
||||
private:
|
||||
T m_value;
|
||||
|
||||
@ -99,14 +99,6 @@ class LittleEndian {
|
||||
return ox::bigEndianAdapt(m_value);
|
||||
}
|
||||
|
||||
inline bool operator==(T value) {
|
||||
return value == ox::bigEndianAdapt(m_value);
|
||||
}
|
||||
|
||||
inline bool operator!=(T value) {
|
||||
return value != ox::bigEndianAdapt(m_value);
|
||||
}
|
||||
|
||||
inline T operator+(T value) const {
|
||||
return ox::bigEndianAdapt(m_value) + value;
|
||||
}
|
||||
|
34
deps/ox/src/ox/std/types.hpp
vendored
34
deps/ox/src/ox/std/types.hpp
vendored
@ -25,18 +25,10 @@ typedef long int64_t;
|
||||
typedef unsigned long uint64_t;
|
||||
typedef int64_t intmax_t;
|
||||
typedef uint64_t uintmax_t;
|
||||
typedef int64_t intptr_t;
|
||||
typedef uint64_t uintptr_t;
|
||||
#endif
|
||||
|
||||
static_assert(sizeof(int8_t) == 1, "int8_t is wrong size");
|
||||
static_assert(sizeof(int16_t) == 2, "int16_t is wrong size");
|
||||
static_assert(sizeof(int32_t) == 4, "int32_t is wrong size");
|
||||
static_assert(sizeof(int64_t) == 8, "int64_t is wrong size");
|
||||
|
||||
static_assert(sizeof(uint8_t) == 1, "uint8_t is wrong size");
|
||||
static_assert(sizeof(uint16_t) == 2, "uint16_t is wrong size");
|
||||
static_assert(sizeof(uint32_t) == 4, "uint32_t is wrong size");
|
||||
static_assert(sizeof(uint64_t) == 8, "uint64_t is wrong size");
|
||||
|
||||
namespace ox {
|
||||
|
||||
typedef uint32_t Error;
|
||||
@ -45,10 +37,30 @@ typedef uint32_t Error;
|
||||
|
||||
#if defined(_LP64) || defined(__ppc64__) || defined(__aarch64__)
|
||||
typedef unsigned long size_t;
|
||||
typedef long intptr_t;
|
||||
typedef unsigned long uintptr_t;
|
||||
#elif defined(_WIN64)
|
||||
typedef uint64_t size_t;
|
||||
typedef int64_t intptr_t;
|
||||
typedef uint64_t uintptr_t;
|
||||
#elif defined(_LP32) || defined(__ppc__) || defined(_WIN32) || defined(__arm__)
|
||||
typedef uint32_t size_t;
|
||||
typedef int32_t intptr_t;
|
||||
typedef uint32_t uintptr_t;
|
||||
#else
|
||||
#error size_t undefined
|
||||
#error size_t, intptr_t, and uintptr_t undefined
|
||||
#endif
|
||||
|
||||
static_assert(sizeof(int8_t) == 1, "int8_t is wrong size");
|
||||
static_assert(sizeof(int16_t) == 2, "int16_t is wrong size");
|
||||
static_assert(sizeof(int32_t) == 4, "int32_t is wrong size");
|
||||
static_assert(sizeof(int64_t) == 8, "int64_t is wrong size");
|
||||
static_assert(sizeof(intptr_t) == sizeof(void*), "intptr_t is wrong size");
|
||||
|
||||
static_assert(sizeof(uint8_t) == 1, "uint8_t is wrong size");
|
||||
static_assert(sizeof(uint16_t) == 2, "uint16_t is wrong size");
|
||||
static_assert(sizeof(uint32_t) == 4, "uint32_t is wrong size");
|
||||
static_assert(sizeof(uint64_t) == 8, "uint64_t is wrong size");
|
||||
static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t is wrong size");
|
||||
|
||||
static_assert(sizeof(size_t) == sizeof(void*), "size_t is wrong size");
|
||||
|
Loading…
Reference in New Issue
Block a user