34#ifndef PROTOCORE_WAL_FS_H
35#define PROTOCORE_WAL_FS_H
39#if PROTOCORE_ENABLE_WAL
43#include "memoria_operor/memoria_operor.h"
52} protocore_wal_fs_ctx;
54EMBED_INLINE
size_t protocore_wal_fs_read(
void *ctx, uint64_t off, uint8_t *buf,
size_t len)
56 protocore_wal_fs_ctx *c = (protocore_wal_fs_ctx *)ctx;
57 if (!c->fs->seek(c->handle, off))
61 int n = c->fs->read(c->handle, buf, len);
62 return n < 0 ? 0 : (size_t)n;
65EMBED_INLINE
size_t protocore_wal_fs_write(
void *ctx, uint64_t off,
const uint8_t *buf,
size_t len)
67 protocore_wal_fs_ctx *c = (protocore_wal_fs_ctx *)ctx;
68 if (!c->fs->seek(c->handle, off))
72 int n = c->fs->write(c->handle, buf, len);
73 return n < 0 ? 0 : (size_t)n;
76EMBED_INLINE embed_bool protocore_wal_fs_sync(
void *ctx)
78 protocore_wal_fs_ctx *c = (protocore_wal_fs_ctx *)ctx;
79 return c->fs->sync(c->handle);
86EMBED_INLINE embed_bool protocore_wal_fs_prealloc(
const protocore_mnt_backend *fs,
const char *path, uint64_t size)
92 if (fs->
exists(path) && fs->
size(path) >= 0 && (uint64_t)fs->
size(path) >= size)
96 int h = fs->
open(path, PROTOCORE_MNT_WRITE);
102 EMBED_CALL(memor.set, MemoriaCfg, .dst = z, .bytes =
sizeof z, .val = 0);
103 uint64_t left = size;
104 embed_bool ok = EMBED_TRUE;
107 size_t n = left <
sizeof z ? (size_t)left : sizeof z;
108 if (fs->
write(h, z, n) != (
int)n)
131EMBED_INLINE embed_bool protocore_wal_fs_open(protocore_wal_fs_ctx *c, WalDev *dev,
const protocore_mnt_backend *fs,
132 const char *path, uint64_t size)
134 if (!c || !dev || !fs || !path || !fs->
sync)
138 int h = fs->
open(path, PROTOCORE_MNT_RDWR);
145 dev->
read = protocore_wal_fs_read;
146 dev->write = protocore_wal_fs_write;
147 dev->sync = protocore_wal_fs_sync;
154EMBED_INLINE
void protocore_wal_fs_close(protocore_wal_fs_ctx *c)
158 c->fs->close(c->handle);
The mount: which store is behind the filesystem, and the vtable it answers through.
A storage backend. Each open call returns a small handle (>= 0) or -1.
long(* size)(const char *path)
file size, or -1 if absent.
int(* open)(const char *path, int mode)
-> handle (>=0) or -1.
embed_bool(* exists)(const char *path)
true if the path exists.
int(* write)(int handle, const void *buf, size_t n)
bytes written, or -1.
void(* close)(int handle)
release a file OR directory handle.
embed_bool(* sync)(int handle)
Push everything written to handle past the backend's own buffering, and report whether it got there....
int(* read)(int handle, void *buf, size_t n)
bytes read, or -1.