[ox/fs] Add new Directory type

This commit is contained in:
Gary Talent 2018-04-23 22:51:49 -05:00
parent aeee05be89
commit 59cc34b4e8
15 changed files with 306 additions and 26 deletions

View File

@ -36,7 +36,7 @@ if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-variable") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-variable")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-field-initializers")
if (CMAKE_BUILD_TYPE STREQUAL "Release") if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

View File

@ -6,6 +6,7 @@ add_library(
filestore/filestoretemplate.cpp filestore/filestoretemplate.cpp
filesystem/filesystem.cpp filesystem/filesystem.cpp
filesystem/pathiterator.cpp filesystem/pathiterator.cpp
filesystem2/directory.cpp
) )
set_property( set_property(
@ -38,14 +39,6 @@ install(
include/ox/fs include/ox/fs
) )
install(
FILES
filestore/nodebuffer.hpp
filestore/ptr.hpp
DESTINATION
include/ox/fs/filestore
)
install( install(
FILES FILES
filesystem/filesystem.hpp filesystem/filesystem.hpp

View File

@ -32,7 +32,7 @@ struct __attribute__((packed)) FileStoreItem: public ptrarith::Item<size_t> {
return sizeof(*this) + this->size(); return sizeof(*this) + this->size();
} }
ox::ptrarith::Ptr<uint8_t, size_t> data() { ptrarith::Ptr<uint8_t, size_t> data() {
return ptrarith::Ptr<uint8_t, size_t>(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); return ptrarith::Ptr<uint8_t, size_t>(this, this->size(), sizeof(*this), this->size() - sizeof(*this));
} }
}; };
@ -42,8 +42,8 @@ template<typename size_t>
class FileStoreTemplate: public FileStore { class FileStoreTemplate: public FileStore {
private: private:
using ItemPtr = typename ox::ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>::ItemPtr; using ItemPtr = typename ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>::ItemPtr;
using Buffer = ox::ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>; using Buffer = ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>;
struct __attribute__((packed)) FileStoreData { struct __attribute__((packed)) FileStoreData {
ox::LittleEndian<size_t> rootNode = 0; ox::LittleEndian<size_t> rootNode = 0;
@ -128,7 +128,7 @@ class FileStoreTemplate: public FileStore {
template<typename size_t> template<typename size_t>
FileStoreTemplate<size_t>::FileStoreTemplate(void *buff, size_t buffSize) { FileStoreTemplate<size_t>::FileStoreTemplate(void *buff, size_t buffSize) {
m_buffSize = buffSize; m_buffSize = buffSize;
m_buffer = reinterpret_cast<ox::ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>*>(buff); m_buffer = reinterpret_cast<ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>*>(buff);
if (!m_buffer->valid(buffSize)) { if (!m_buffer->valid(buffSize)) {
m_buffSize = 0; m_buffSize = 0;
m_buffer = nullptr; m_buffer = nullptr;

View File

@ -12,9 +12,13 @@
namespace ox { namespace ox {
PathIterator::PathIterator(const char *path, std::size_t maxSize) { PathIterator::PathIterator(const char *path, std::size_t maxSize, std::size_t iterator) {
m_path = path; m_path = path;
m_maxSize = maxSize; m_maxSize = maxSize;
m_iterator = iterator;
}
PathIterator::PathIterator(const char *path): PathIterator(path, ox_strlen(path)) {
} }
/** /**
@ -85,7 +89,30 @@ int PathIterator::next(char *pathOut, std::size_t pathOutSize) {
return retval; return retval;
} }
bool PathIterator::hasNext() { ValErr<std::size_t> PathIterator::nextSize() const {
std::size_t size = 0;
Error retval = 1;
auto it = m_iterator;
if (it < m_maxSize && ox_strlen(&m_path[it])) {
retval = 0;
if (m_path[it] == '/') {
it++;
}
std::size_t start = it;
// end is at the next /
const char *substr = ox_strchr(&m_path[start], '/', m_maxSize - start);
// correct end if it is invalid, which happens if there is no next /
if (!substr) {
substr = ox_strchr(&m_path[start], 0, m_maxSize - start);
}
std::size_t end = substr - m_path;
size = end - start;
}
it += size;
return {size, retval};
}
bool PathIterator::hasNext() const {
std::size_t size = 0; std::size_t size = 0;
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) {
std::size_t start = m_iterator; std::size_t start = m_iterator;
@ -104,4 +131,12 @@ bool PathIterator::hasNext() {
return size > 0; return size > 0;
} }
PathIterator PathIterator::operator+(int i) {
return PathIterator(m_path, m_maxSize, m_iterator + i);
}
PathIterator PathIterator::operator-(int i) {
return PathIterator(m_path, m_maxSize, m_iterator - i);
}
} }

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <ox/std/strops.hpp>
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
namespace ox { namespace ox {
@ -19,7 +20,9 @@ class PathIterator {
std::size_t m_maxSize = 0; std::size_t m_maxSize = 0;
public: public:
PathIterator(const char *path, std::size_t maxSize); PathIterator(const char *path, std::size_t maxSize, std::size_t iterator = 0);
PathIterator(const char *path);
/** /**
* @return 0 if no error * @return 0 if no error
@ -36,7 +39,17 @@ class PathIterator {
*/ */
int next(char *pathOut, std::size_t pathOutSize); int next(char *pathOut, std::size_t pathOutSize);
bool hasNext(); /**
* @return 0 if no error
*/
ValErr<std::size_t> nextSize() const;
bool hasNext() const;
PathIterator operator+(int i);
PathIterator operator-(int i);
}; };
} }

View File

@ -0,0 +1,19 @@
/*
* 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/.
*/
#include "directory.hpp"
namespace ox::fs {
template class Directory<uint16_t>;
template class Directory<uint32_t>;
template struct DirectoryEntry<uint16_t>;
template struct DirectoryEntry<uint32_t>;
}

View File

@ -6,20 +6,126 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#pragma once
#include <ox/fs/filesystem/pathiterator.hpp> #include <ox/fs/filesystem/pathiterator.hpp>
#include <ox/fs/filestore.hpp> #include <ox/fs/filestore.hpp>
#include <ox/ptrarith/nodebuffer.hpp>
#include <ox/std/std.hpp>
namespace ox::fs { namespace ox::fs {
struct DirectoryData { template<typename InodeId_t>
struct __attribute__((packed)) DirectoryEntry {
// NodeBuffer fields
ox::LittleEndian<std::size_t> prev = 0;
ox::LittleEndian<std::size_t> next = 0;
// DirectoryEntry fields
ox::LittleEndian<InodeId_t> inode = 0;
BString<255> name;
explicit DirectoryEntry(const char *name) {
this->name = name;
}
/**
* @return the size of the data + the size of the Item type
*/
InodeId_t fullSize() const {
return offsetof(DirectoryEntry, name) + name.size();
}
InodeId_t size() const {
return fullSize() - offsetof(DirectoryEntry, inode);
}
ptrarith::Ptr<uint8_t, InodeId_t> data() {
return ptrarith::Ptr<uint8_t, InodeId_t>(this, this->size(), sizeof(*this), this->size() - sizeof(*this));
}
}; };
template<typename InodeId_t>
class Directory { class Directory {
Error add(const PathIterator &it, uint64_t inode); private:
using Buffer = ptrarith::NodeBuffer<InodeId_t, DirectoryEntry<InodeId_t>>;
std::size_t m_size = 0;
Buffer *m_buff = nullptr;
Error rm(PathIterator &it); public:
Directory(uint8_t *buff, std::size_t size);
/**
* Initializes Directory.
*/
Error init() noexcept;
Error write(PathIterator it, InodeId_t inode) noexcept;
Error rm(PathIterator it) noexcept;
ValErr<InodeId_t> find(PathIterator it) const noexcept;
}; };
template<typename InodeId_t>
Directory<InodeId_t>::Directory(uint8_t *buff, std::size_t size) {
m_size = size;
m_buff = reinterpret_cast<decltype(m_buff)>(buff);
}
template<typename InodeId_t>
Error Directory<InodeId_t>::init() noexcept {
if (m_size >= sizeof(Buffer)) {
new (m_buff) Buffer(m_size);
return 0;
}
return 1;
}
template<typename InodeId_t>
Error Directory<InodeId_t>::write(PathIterator it, InodeId_t inode) noexcept {
if (it.hasNext()) {
return write(it + 1, inode);
} else {
auto current = find(it);
if (current.ok()) {
} else {
}
}
return 1;
}
template<typename InodeId_t>
Error Directory<InodeId_t>::rm(PathIterator) noexcept {
return 1;
}
template<typename InodeId_t>
ValErr<InodeId_t> Directory<InodeId_t>::find(PathIterator it) const noexcept {
auto size = it.nextSize();
char name[size + 1];
it.next(name, size);
for (auto i = m_buff->iterator(); i.hasNext(); i.next()) {
if (i->name == name) {
return static_cast<InodeId_t>(i->inode);
}
}
return {0, 1};
}
extern template class Directory<uint16_t>;
extern template class Directory<uint32_t>;
extern template struct DirectoryEntry<uint16_t>;
extern template struct DirectoryEntry<uint32_t>;
using Directory16 = Directory<uint16_t>;
using Directory32 = Directory<uint32_t>;
} }

View File

@ -74,3 +74,5 @@ add_test("Test\\ FileSystem32::stripDirectories" FSTests "FileSystem32::stripDir
add_test("Test\\ NodeBuffer::insert" FSTests "NodeBuffer::insert") add_test("Test\\ NodeBuffer::insert" FSTests "NodeBuffer::insert")
add_test("Test\\ FileStore::readWrite" FSTests "FileStore::readWrite") add_test("Test\\ FileStore::readWrite" FSTests "FileStore::readWrite")
add_test("Test\\ Directory" FSTests "Directory")

View File

@ -11,6 +11,7 @@
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <array>
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
@ -382,6 +383,19 @@ map<string, int(*)(string)> tests = {
return 0; return 0;
} }
}, },
{
"Directory",
[](string) {
std::array<uint8_t, 1000> buff;
ox::fs::Directory32 dir(buff.data(), buff.size());
dir.init();
dir.write("/file1", 1);
//oxAssert(dir.find("/file1") == 1, "Could not find /file1");
dir.write("/file3", 3);
dir.write("/file2", 2);
return 0;
}
},
}, },
}; };

View File

@ -24,7 +24,51 @@ class __attribute__((packed)) NodeBuffer {
ox::LittleEndian<size_t> firstItem = 0; ox::LittleEndian<size_t> firstItem = 0;
}; };
using ItemPtr = ox::ptrarith::Ptr<Item, size_t, sizeof(Header)>; using ItemPtr = Ptr<Item, size_t, sizeof(Header)>;
class Iterator {
private:
NodeBuffer *m_buffer = nullptr;
ItemPtr m_current;
size_t m_it = 0;
public:
Iterator(NodeBuffer *buffer, ItemPtr current) {
m_buffer = buffer;
m_current = current;
oxTrace("ox::ptrarith::Iterator::start") << current.offset();
}
operator const Item*() const {
return m_current;
}
operator Item*() {
return m_current;
}
const Item *operator->() const {
return m_current;
}
Item *operator->() {
return m_current;
}
bool hasNext() {
if (m_current.valid()) {
oxTrace("ox::ptrarith::NodeBuffer::Iterator::hasNext::current") << m_current.offset();
auto next = m_buffer->next(m_current);
return next.valid() && m_buffer->firstItem() != next;
}
return false;
}
void next() {
oxTrace("ox::ptrarith::NodeBuffer::Iterator::next") << m_it++;
m_current = m_buffer->next(m_current);
}
};
Header m_header; Header m_header;
@ -33,6 +77,10 @@ class __attribute__((packed)) NodeBuffer {
explicit NodeBuffer(size_t size); explicit NodeBuffer(size_t size);
const Iterator iterator() const;
Iterator iterator();
ItemPtr firstItem(); ItemPtr firstItem();
ItemPtr lastItem(); ItemPtr lastItem();
@ -84,8 +132,20 @@ NodeBuffer<size_t, Item>::NodeBuffer(size_t size) {
m_header.size = size; m_header.size = size;
} }
template<typename size_t, typename Item>
const typename NodeBuffer<size_t, Item>::Iterator NodeBuffer<size_t, Item>::iterator() const {
return Iterator(this, firstItem());
}
template<typename size_t, typename Item>
typename NodeBuffer<size_t, Item>::Iterator NodeBuffer<size_t, Item>::iterator() {
oxTrace("ox::ptrarith::NodeBuffer::iterator::size") << m_header.size;
return Iterator(this, firstItem());
}
template<typename size_t, typename Item> template<typename size_t, typename Item>
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::firstItem() { typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::firstItem() {
oxTrace("ox::ptrarith::NodeBuffer::firstItem") << m_header.firstItem;
return ptr(m_header.firstItem); return ptr(m_header.firstItem);
} }
@ -121,12 +181,14 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::ptr(size_t
// make sure this can be read as an Item, and then use Item::size for the size // make sure this can be read as an Item, and then use Item::size for the size
auto itemSpace = m_header.size - itemOffset; auto itemSpace = m_header.size - itemOffset;
auto item = reinterpret_cast<Item*>(reinterpret_cast<uint8_t*>(this) + itemOffset); auto item = reinterpret_cast<Item*>(reinterpret_cast<uint8_t*>(this) + itemOffset);
oxTrace("ox::ptrarith::NodeBuffer::ptr::itemOffset") << itemOffset << m_header.size - sizeof(Item);
if (itemOffset >= sizeof(Header) and if (itemOffset >= sizeof(Header) and
itemOffset < m_header.size - sizeof(Item) and itemOffset < m_header.size - sizeof(Item) and
itemSpace >= static_cast<size_t>(sizeof(Item)) and itemSpace >= static_cast<size_t>(sizeof(Item)) and
itemSpace >= item->fullSize()) { itemSpace >= item->fullSize()) {
return ItemPtr(this, m_header.size, itemOffset, item->fullSize()); return ItemPtr(this, m_header.size, itemOffset, item->fullSize());
} else { } else {
oxTrace("ox::ptrarith::NodeBuffer::ptr::null") << itemOffset;
return ItemPtr(this, m_header.size, 0, 0); return ItemPtr(this, m_header.size, 0, 0);
} }
} }
@ -271,7 +333,7 @@ struct __attribute__((packed)) Item {
this->m_size = size; this->m_size = size;
} }
size_t size() const { virtual size_t size() const {
return m_size; return m_size;
} }
}; };

View File

@ -56,6 +56,10 @@ class Ptr {
inline operator size_t() const; inline operator size_t() const;
inline bool operator==(const Ptr<T, size_t, minOffset> &other) const;
inline bool operator!=(const Ptr<T, size_t, minOffset> &other) const;
template<typename SubT> template<typename SubT>
inline const Ptr<SubT, size_t, sizeof(T)> subPtr(size_t offset, size_t size) const; inline const Ptr<SubT, size_t, sizeof(T)> subPtr(size_t offset, size_t size) const;
@ -168,6 +172,20 @@ inline Ptr<T, size_t, minOffset>::operator size_t() const {
return 0; return 0;
} }
template<typename T, typename size_t, size_t minOffset>
inline bool Ptr<T, size_t, minOffset>::operator==(const Ptr<T, size_t, minOffset> &other) const {
return m_dataStart == other.m_dataStart &&
m_itemOffset == other.m_itemOffset &&
m_itemSize == other.m_itemSize;
}
template<typename T, typename size_t, size_t minOffset>
inline bool Ptr<T, size_t, minOffset>::operator!=(const Ptr<T, size_t, minOffset> &other) const {
return m_dataStart != other.m_dataStart ||
m_itemOffset != other.m_itemOffset ||
m_itemSize != other.m_itemSize;
}
template<typename T, typename size_t, size_t minOffset> template<typename T, typename size_t, size_t minOffset>
template<typename SubT> template<typename SubT>
inline const Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t offset, size_t size) const { inline const Ptr<SubT, size_t, sizeof(T)> Ptr<T, size_t, minOffset>::subPtr(size_t offset, size_t size) const {

View File

@ -15,6 +15,7 @@
#include "memops.hpp" #include "memops.hpp"
#include "new.hpp" #include "new.hpp"
#include "random.hpp" #include "random.hpp"
#include "stddef.hpp"
#include "strops.hpp" #include "strops.hpp"
#include "string.hpp" #include "string.hpp"
#include "types.hpp" #include "types.hpp"

13
deps/ox/src/ox/std/stddef.hpp vendored Normal file
View File

@ -0,0 +1,13 @@
/*
* 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
#if !defined(offsetof)
#define offsetof(type, member) __builtin_offsetof(type, member)
#endif

View File

@ -66,11 +66,15 @@ struct ValErr {
this->error = error; this->error = error;
} }
inline constexpr operator const T&() const {
return value;
}
inline constexpr operator T&() { inline constexpr operator T&() {
return value; return value;
} }
inline constexpr bool ok() { inline constexpr bool ok() const {
return error == 0; return error == 0;
} }

View File

@ -13,10 +13,10 @@
namespace ox::trace { namespace ox::trace {
struct TraceMsg { struct TraceMsg {
ox::BString<150> file = ""; ox::BString<255> file = "";
int line = 0; int line = 0;
uint64_t time = 0; uint64_t time = 0;
ox::BString<50> ch = ""; ox::BString<75> ch = "";
ox::BString<100> msg; ox::BString<100> msg;
}; };
@ -93,4 +93,4 @@ class NullStream {
} }
#define oxTrace(ch) ox::trace::NullStream(__FILE__, __LINE__, ch) #define oxTrace(ch) ox::trace::StdOutStream(__FILE__, __LINE__, ch)