[ox/fs] Add modelWrite handler to FileAddress

This commit is contained in:
Gary Talent 2019-10-22 18:54:19 -05:00
parent 5a0ba33b4c
commit fbdb48a1ee
6 changed files with 64 additions and 8 deletions

View File

@ -52,7 +52,6 @@ install(
install( install(
FILES FILES
filestore/filestoretemplate.hpp
filesystem/filesystem.hpp filesystem/filesystem.hpp
filesystem/pathiterator.hpp filesystem/pathiterator.hpp
DESTINATION DESTINATION

View File

@ -10,6 +10,11 @@
namespace ox { namespace ox {
FileAddress::FileAddress() {
m_data.inode = 0;
m_type = FileAddressType::Inode;
}
FileAddress::FileAddress(uint64_t inode) { FileAddress::FileAddress(uint64_t inode) {
m_data.inode = inode; m_data.inode = inode;
m_type = FileAddressType::Inode; m_type = FileAddressType::Inode;

View File

@ -9,10 +9,11 @@
#pragma once #pragma once
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
#include <ox/model/types.hpp>
namespace ox { namespace ox {
enum class FileAddressType { enum class FileAddressType: int8_t {
None = -1, None = -1,
Path, Path,
ConstPath, ConstPath,
@ -20,7 +21,14 @@ enum class FileAddressType {
}; };
class FileAddress { class FileAddress {
private:
template<typename T>
friend ox::Error modelWrite(T*, FileAddress*);
public:
static constexpr auto Fields = 2;
protected:
FileAddressType m_type = FileAddressType::None; FileAddressType m_type = FileAddressType::None;
union { union {
char *path; char *path;
@ -29,6 +37,8 @@ class FileAddress {
} m_data; } m_data;
public: public:
FileAddress();
FileAddress(uint64_t inode); FileAddress(uint64_t inode);
FileAddress(char *path); FileAddress(char *path);
@ -69,4 +79,38 @@ class FileAddress {
}; };
template<typename T>
ox::Error modelWrite(T *io, FileAddress *fa) {
io->setTypeInfo("ox::FileAddress", FileAddress::Fields);
switch (fa->m_type) {
case FileAddressType::Path:
case FileAddressType::ConstPath:
{
decltype(fa->m_data.inode) blank = 0;
const auto strSize = ox_strlen(fa->m_data.constPath) + 1;
auto path = ox_malloca(strSize, char, 0);
memcpy(path.get(), fa->m_data.constPath, strSize);
oxReturnError(io->field("path", SerStr(path.get(), strSize - 1)));
oxReturnError(io->field("inode", &blank));
break;
}
case FileAddressType::Inode:
{
char blankPath[1] = "";
oxReturnError(io->field("path", SerStr(blankPath, 0)));
oxReturnError(io->field("inode", &fa->m_data.inode));
break;
}
case FileAddressType::None:
{
char blankPath[1] = "";
decltype(fa->m_data.inode) blankInode = 0;
oxReturnError(io->field("path", SerStr(blankPath, 0)));
oxReturnError(io->field("inode", &blankInode));
break;
}
}
return OxError(0);
}
} }

View File

@ -84,6 +84,9 @@ ox::Error PassThroughFS::resize(uint64_t, void*) {
Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) { Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) {
auto p = (m_path / stripSlash(path)); auto p = (m_path / stripSlash(path));
auto f = fopen(p.c_str(), "w"); auto f = fopen(p.c_str(), "w");
if (!f) {
return OxError(1);
}
auto err = OxError(fwrite(buffer, size, 1, f) == 1 ? 0 : 1); auto err = OxError(fwrite(buffer, size, 1, f) == 1 ? 0 : 1);
fclose(f); fclose(f);
return err; return err;

View File

@ -84,19 +84,18 @@ Error MetalClawWriter::field(const char *name, ox::BString<L> *val) {
template<typename T> template<typename T>
Error MetalClawWriter::field(const char*, T *val) { Error MetalClawWriter::field(const char*, T *val) {
auto err = OxError(0);
bool fieldSet = false; bool fieldSet = false;
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt); MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt);
if (val) { if (val) {
err |= model(&writer, val); oxReturnError(model(&writer, val));
if (static_cast<std::size_t>(writer.m_fieldPresence.getMaxLen()) < writer.m_buffIt) { if (static_cast<std::size_t>(writer.m_fieldPresence.getMaxLen()) < writer.m_buffIt) {
m_buffIt += writer.m_buffIt; m_buffIt += writer.m_buffIt;
fieldSet = true; fieldSet = true;
} }
} }
err |= m_fieldPresence.set(m_field, fieldSet); oxReturnError(m_fieldPresence.set(m_field, fieldSet));
m_field++; m_field++;
return err; return OxError(0);
} }
template<typename T> template<typename T>

View File

@ -10,6 +10,12 @@
#include "types.hpp" #include "types.hpp"
#if __has_include(<cstring>)
#include<cstring>
#else
void *memcpy(void *dest, const void *src, std::size_t size);
#endif
int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept; int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept;
constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcept { constexpr void *ox_memcpy(void *dest, const void *src, std::size_t size) noexcept {