Renamed project to wfs and memfs to FileStore.
This commit is contained in:
+13
-1
@@ -1,3 +1,15 @@
|
||||
cmake_minimum_required(VERSION 2.8.8)
|
||||
|
||||
add_subdirectory(memphis)
|
||||
add_library(
|
||||
Memphis
|
||||
filestore.cpp
|
||||
_memops.cpp
|
||||
)
|
||||
|
||||
install(
|
||||
FILES
|
||||
filestore.hpp
|
||||
_memops.hpp
|
||||
DESTINATION
|
||||
include/Memphis
|
||||
)
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
*/
|
||||
#include "_memops.hpp"
|
||||
|
||||
namespace memphis {
|
||||
namespace wombat {
|
||||
namespace fs {
|
||||
|
||||
void memcpy(void *src, void *dest, int size) {
|
||||
char *srcBuf = (char*) src;
|
||||
@@ -25,3 +26,4 @@ void memset(void *ptr, char val, int size) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,17 @@
|
||||
* 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/.
|
||||
*/
|
||||
#ifndef MEMPHIS_MEMOPS_HPP
|
||||
#define MEMPHIS_MEMOPS_HPP
|
||||
#ifndef WOMBAT_FS_MEMOPS_HPP
|
||||
#define WOMBAT_FS_MEMOPS_HPP
|
||||
|
||||
namespace memphis {
|
||||
namespace wombat {
|
||||
namespace fs {
|
||||
|
||||
void memcpy(void *src, void *dest, int size);
|
||||
|
||||
void memset(void *ptr, char val, int size);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -5,10 +5,13 @@
|
||||
* 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/.
|
||||
*/
|
||||
#ifndef MEMPHIS_TYPES_HPP
|
||||
#define MEMPHIS_TYPES_HPP
|
||||
#ifndef WOMBAT_FS_TYPES_HPP
|
||||
#define WOMBAT_FS_TYPES_HPP
|
||||
|
||||
namespace memphis {
|
||||
#define offsetof(st, m) ((size_t)(&((st *)0)->m))
|
||||
|
||||
namespace wombat {
|
||||
namespace fs {
|
||||
|
||||
typedef char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
@@ -28,7 +31,7 @@ typedef uint64_t size_t;
|
||||
typedef uint32_t size_t;
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,37 +7,36 @@
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include "_memops.hpp"
|
||||
#include "memfs.hpp"
|
||||
#include "filestore.hpp"
|
||||
|
||||
#define offsetof(st, m) ((size_t)(&((st *)0)->m))
|
||||
namespace wombat {
|
||||
namespace fs {
|
||||
|
||||
namespace memphis {
|
||||
|
||||
uint32_t MemFs::version = 0;
|
||||
uint32_t FileStore::version = 0;
|
||||
|
||||
uint8_t *initFs(uint8_t *buffer, size_t size, bool hasDirectories) {
|
||||
auto fs = (MemFsHeader*) (buffer ? buffer : malloc(size));
|
||||
fs->version = MemFs::version;
|
||||
fs->version = FileStore::version;
|
||||
return (uint8_t*) fs;
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::Record::size() {
|
||||
return offsetof(MemFs::Record, m_id) + dataLen;
|
||||
MemFsPtr FileStore::Record::size() {
|
||||
return offsetof(FileStore::Record, m_id) + dataLen;
|
||||
}
|
||||
|
||||
void MemFs::Record::setId(RecordId id) {
|
||||
void FileStore::Record::setId(RecordId id) {
|
||||
this->m_id = id;
|
||||
}
|
||||
|
||||
void MemFs::Record::setData(uint8_t *data, int size) {
|
||||
void FileStore::Record::setData(uint8_t *data, int size) {
|
||||
memcpy(this + m_data, data, size);
|
||||
m_data = size;
|
||||
}
|
||||
|
||||
|
||||
// MemFs
|
||||
// FileStore
|
||||
|
||||
MemFs::MemFs(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t*) begin)), m_lastRec(*(MemFsPtr*) (begin + sizeof(m_version))) {
|
||||
FileStore::FileStore(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t*) begin)), m_lastRec(*(MemFsPtr*) (begin + sizeof(m_version))) {
|
||||
if (version != m_version) {
|
||||
// version mismatch
|
||||
if (error) {
|
||||
@@ -54,19 +53,19 @@ MemFs::MemFs(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t*
|
||||
}
|
||||
}
|
||||
|
||||
void MemFs::init() {
|
||||
void FileStore::init() {
|
||||
memset(m_begin, 0, m_end - m_begin);
|
||||
m_version = version;
|
||||
}
|
||||
|
||||
void MemFs::write(RecordId id, uint8_t *data, MemFsPtr dataLen) {
|
||||
const MemFsPtr size = offsetof(MemFs::Record, m_id) + dataLen;
|
||||
void FileStore::write(RecordId id, uint8_t *data, MemFsPtr dataLen) {
|
||||
const MemFsPtr size = offsetof(FileStore::Record, m_id) + dataLen;
|
||||
auto rec = (Record*) alloc(size);
|
||||
rec->dataLen = dataLen;
|
||||
insert(m_root, rec);
|
||||
}
|
||||
|
||||
int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) {
|
||||
int FileStore::read(RecordId id, uint8_t **data, MemFsPtr *size) {
|
||||
auto rec = getRecord(m_root, id);
|
||||
int retval = 1;
|
||||
if (rec) {
|
||||
@@ -78,7 +77,7 @@ int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) {
|
||||
return retval;
|
||||
}
|
||||
|
||||
MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) {
|
||||
FileStore::Record *FileStore::getRecord(FileStore::Record *root, RecordId id) {
|
||||
auto cmp = root->m_id > id;
|
||||
MemFsPtr recPt;
|
||||
if (cmp) {
|
||||
@@ -95,7 +94,7 @@ MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) {
|
||||
}
|
||||
}
|
||||
|
||||
void *MemFs::alloc(MemFsPtr size) {
|
||||
void *FileStore::alloc(MemFsPtr size) {
|
||||
const auto iterator = this->iterator();
|
||||
if ((iterator + size) > (uint64_t) m_end) {
|
||||
compress();
|
||||
@@ -112,7 +111,7 @@ void *MemFs::alloc(MemFsPtr size) {
|
||||
return rec;
|
||||
}
|
||||
|
||||
void MemFs::compress() {
|
||||
void FileStore::compress() {
|
||||
auto current = m_root;
|
||||
while (current->next) {
|
||||
auto prevEnd = current + current->size();
|
||||
@@ -124,7 +123,7 @@ void MemFs::compress() {
|
||||
}
|
||||
}
|
||||
|
||||
bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
||||
bool FileStore::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
||||
auto cmp = root->m_id > insertValue->m_id;
|
||||
if (cmp) {
|
||||
if (root->left) {
|
||||
@@ -156,12 +155,13 @@ bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::iterator() {
|
||||
MemFsPtr FileStore::iterator() {
|
||||
return m_lastRec + ((Record*) m_begin + m_lastRec)->size();
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::ptr(void *ptr) {
|
||||
MemFsPtr FileStore::ptr(void *ptr) {
|
||||
return ((uint8_t*) ptr) - m_begin;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,22 +5,32 @@
|
||||
* 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/.
|
||||
*/
|
||||
#ifndef MEMPHIS_MEMFS_HPP
|
||||
#define MEMPHIS_MEMFS_HPP
|
||||
#ifndef WOMBAT_FS_FILESTORE_HPP
|
||||
#define WOMBAT_FS_FILESTORE_HPP
|
||||
|
||||
#include "_types.hpp"
|
||||
|
||||
namespace memphis {
|
||||
namespace wombat {
|
||||
namespace fs {
|
||||
|
||||
typedef uint32_t MemFsPtr;
|
||||
typedef uint32_t RecordId;
|
||||
|
||||
struct MemFsHeader {
|
||||
uint32_t version;
|
||||
bool hasDirectories;
|
||||
MemFsPtr rootDir = -1;
|
||||
};
|
||||
|
||||
class MemFs {
|
||||
class DirectoryHeader {
|
||||
/**
|
||||
* Count of the number of files listed in this directory.
|
||||
*/
|
||||
MemFsPtr files;
|
||||
|
||||
const char* operator[](int i);
|
||||
};
|
||||
|
||||
class FileStore {
|
||||
public:
|
||||
static uint32_t version;
|
||||
private:
|
||||
@@ -40,48 +50,48 @@ class MemFs {
|
||||
|
||||
uint8_t *m_begin, *m_end;
|
||||
uint32_t &m_version;
|
||||
// the last Record in the MemFs's memory chunk
|
||||
// the last Record in the FileStore's memory chunk
|
||||
MemFsPtr &m_lastRec;
|
||||
Record *m_root;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* @param begin pointer to the beginning of this MemFs's memory chunk
|
||||
* @param end pointer to the end of this MemFs's memory chunk
|
||||
* @param begin pointer to the beginning of this FileStore's memory chunk
|
||||
* @param end pointer to the end of this FileStore's memory chunk
|
||||
* @param error pointer to a integer return errors into
|
||||
*/
|
||||
MemFs(uint8_t *begin, uint8_t *end, Error *error = nullptr);
|
||||
FileStore(uint8_t *begin, uint8_t *end, Error *error = nullptr);
|
||||
|
||||
/**
|
||||
* Initializes the memory chunk of this MemFs was given.
|
||||
* Initializes the memory chunk of this FileStore was given.
|
||||
* This clears the previous contents.
|
||||
*/
|
||||
void init();
|
||||
|
||||
/**
|
||||
* Writes the given data to a "file" with the given path.
|
||||
* @param path the path of the file
|
||||
* Writes the given data to a "file" with the given id.
|
||||
* @param id the id of the file
|
||||
* @param data the contents of the file
|
||||
* @param dataLen the number of bytes data points to
|
||||
*/
|
||||
void write(RecordId path, uint8_t *data, MemFsPtr dataLen);
|
||||
void write(RecordId id, uint8_t *data, MemFsPtr dataLen);
|
||||
|
||||
/**
|
||||
* Reads the "file" at the given path. You are responsible for freeing
|
||||
* Reads the "file" at the given id. You are responsible for freeing
|
||||
* the data when done with it.
|
||||
* @param path path of the "file"
|
||||
* @param id id of the "file"
|
||||
* @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(RecordId path, uint8_t **data, MemFsPtr *size);
|
||||
int read(RecordId id, uint8_t **data, MemFsPtr *size);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Gets the record at the given path.
|
||||
* Gets the record at the given id.
|
||||
* @param root the root node to start comparing on
|
||||
* @param path path of the "file"
|
||||
* @param id id of the "file"
|
||||
* @param pathLen number of characters in pathLen
|
||||
* @return the requested Record, if available
|
||||
*/
|
||||
@@ -124,12 +134,13 @@ class MemFs {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T MemFs::ptr(MemFsPtr ptr) {
|
||||
T FileStore::ptr(MemFsPtr ptr) {
|
||||
return (T) m_begin + ptr;
|
||||
}
|
||||
|
||||
uint8_t *initFs(uint8_t *buffer, size_t size, bool hasDirectories);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,15 +0,0 @@
|
||||
cmake_minimum_required(VERSION 2.8.8)
|
||||
|
||||
add_library(
|
||||
Memphis
|
||||
memfs.cpp
|
||||
_memops.cpp
|
||||
)
|
||||
|
||||
install(
|
||||
FILES
|
||||
memfs.hpp
|
||||
_memops.hpp
|
||||
DESTINATION
|
||||
include/Memphis
|
||||
)
|
||||
+11
-4
@@ -1,12 +1,19 @@
|
||||
#include <memphis/memfs.hpp>
|
||||
/*
|
||||
* Copyright 2015 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 <filestore.hpp>
|
||||
|
||||
using namespace memphis;
|
||||
using namespace wombat::fs;
|
||||
|
||||
int main() {
|
||||
const auto size = 1024;
|
||||
const auto size = 1 << 16;
|
||||
uint8_t volume[size];
|
||||
uint32_t err;
|
||||
initFs(volume, size, false);
|
||||
MemFs memfs(volume, volume + size, &err);
|
||||
FileStore(volume, volume + size, &err);
|
||||
return err;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user