One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
519 lines
14 KiB
C
519 lines
14 KiB
C
#include "store.h"
|
|
#include <string.h>
|
|
|
|
static int fs_reject(FlushStore *store, int reason)
|
|
{
|
|
if (store != NULL) {
|
|
store->rejected += 1u;
|
|
}
|
|
return reason;
|
|
}
|
|
|
|
static int fs_bank_valid(unsigned bank)
|
|
{
|
|
return bank < FS_BANKS;
|
|
}
|
|
|
|
static int fs_active(const FlushStore *store, unsigned bank)
|
|
{
|
|
return store != NULL && fs_bank_valid(bank) && store->banks[bank].active != 0u;
|
|
}
|
|
|
|
static int fs_span(size_t at, size_t count, size_t limit)
|
|
{
|
|
if (at > limit) {
|
|
return 0;
|
|
}
|
|
if (count > limit - at) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int fs_space(size_t length, size_t count)
|
|
{
|
|
if (length > FS_CAPACITY) {
|
|
return 0;
|
|
}
|
|
return count <= FS_CAPACITY - length;
|
|
}
|
|
|
|
static void fs_mark_dirty(FsBank *bank)
|
|
{
|
|
bank->dirty = 1u;
|
|
}
|
|
|
|
static void fs_copy_bytes(uint8_t *target, const uint8_t *source, size_t count)
|
|
{
|
|
size_t i;
|
|
for (i = 0u; i < count; ++i) {
|
|
target[i] = source[i];
|
|
}
|
|
}
|
|
|
|
static void fs_shift_right(FsBank *bank, size_t at, size_t count)
|
|
{
|
|
size_t i;
|
|
for (i = bank->length; i > at; --i) {
|
|
bank->data[i + count - 1u] = bank->data[i - 1u];
|
|
}
|
|
}
|
|
|
|
static void fs_shift_left(FsBank *bank, size_t at, size_t count)
|
|
{
|
|
size_t i;
|
|
for (i = at; i + count < bank->length; ++i) {
|
|
bank->data[i] = bank->data[i + count];
|
|
}
|
|
}
|
|
|
|
void fs_init(FlushStore *store, FsPort port)
|
|
{
|
|
unsigned i;
|
|
if (store == NULL) {
|
|
return;
|
|
}
|
|
memset(store, 0, sizeof(*store));
|
|
store->port = port;
|
|
for (i = 0u; i < FS_BANKS; ++i) {
|
|
store->banks[i].length = 0u;
|
|
store->banks[i].revision = 0u;
|
|
store->banks[i].published_revision = 0u;
|
|
store->banks[i].active = 0u;
|
|
store->banks[i].dirty = 0u;
|
|
}
|
|
}
|
|
|
|
int fs_open(FlushStore *store, unsigned bank, uint32_t revision)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (store->banks[bank].active != 0u) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
memset(slot->data, 0, sizeof(slot->data));
|
|
slot->length = 0u;
|
|
slot->revision = revision;
|
|
slot->published_revision = 0u;
|
|
slot->active = 1u;
|
|
slot->dirty = 1u;
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_close(FlushStore *store, unsigned bank)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
slot->active = 0u;
|
|
slot->dirty = 0u;
|
|
slot->length = 0u;
|
|
slot->revision = 0u;
|
|
slot->published_revision = 0u;
|
|
memset(slot->data, 0, sizeof(slot->data));
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_clear(FlushStore *store, unsigned bank)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
memset(slot->data, 0, sizeof(slot->data));
|
|
slot->length = 0u;
|
|
fs_mark_dirty(slot);
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_append(FlushStore *store, unsigned bank, const uint8_t *bytes, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
if ((bytes == NULL && count != 0u) || !fs_space(store->banks[bank].length, count)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (count != 0u) {
|
|
fs_copy_bytes(&slot->data[slot->length], bytes, count);
|
|
slot->length += count;
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_insert(FlushStore *store, unsigned bank, size_t at, const uint8_t *bytes, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (at > slot->length || (bytes == NULL && count != 0u) ||
|
|
!fs_space(slot->length, count)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (count == 0u) {
|
|
return FS_OK;
|
|
}
|
|
fs_shift_right(slot, at, count);
|
|
fs_copy_bytes(&slot->data[at], bytes, count);
|
|
slot->length += count;
|
|
fs_mark_dirty(slot);
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_erase(FlushStore *store, unsigned bank, size_t at, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (!fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (count == 0u) {
|
|
return FS_OK;
|
|
}
|
|
fs_shift_left(slot, at, count);
|
|
slot->length -= count;
|
|
memset(&slot->data[slot->length], 0, count);
|
|
fs_mark_dirty(slot);
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_patch(FlushStore *store, unsigned bank, size_t at, const uint8_t *bytes, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if ((bytes == NULL && count != 0u) || !fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (count != 0u) {
|
|
fs_copy_bytes(&slot->data[at], bytes, count);
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_fill(FlushStore *store, unsigned bank, size_t at, size_t count, uint8_t value)
|
|
{
|
|
FsBank *slot;
|
|
size_t i;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (!fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
for (i = 0u; i < count; ++i) {
|
|
slot->data[at + i] = value;
|
|
}
|
|
if (count != 0u) {
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_xor(FlushStore *store, unsigned bank, size_t at, size_t count, uint8_t mask)
|
|
{
|
|
FsBank *slot;
|
|
size_t i;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (!fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
for (i = 0u; i < count; ++i) {
|
|
slot->data[at + i] ^= mask;
|
|
}
|
|
if (count != 0u) {
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_reverse(FlushStore *store, unsigned bank, size_t at, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
size_t left;
|
|
size_t right;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (!fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
left = at;
|
|
right = at + count;
|
|
while (left < right && right - left > 1u) {
|
|
uint8_t saved = slot->data[left];
|
|
slot->data[left] = slot->data[right - 1u];
|
|
slot->data[right - 1u] = saved;
|
|
++left;
|
|
--right;
|
|
}
|
|
if (count > 1u) {
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
static void fs_reverse_local(uint8_t *bytes, size_t start, size_t finish)
|
|
{
|
|
while (start < finish && finish - start > 1u) {
|
|
uint8_t saved = bytes[start];
|
|
bytes[start] = bytes[finish - 1u];
|
|
bytes[finish - 1u] = saved;
|
|
++start;
|
|
--finish;
|
|
}
|
|
}
|
|
|
|
int fs_rotate(FlushStore *store, unsigned bank, size_t at, size_t count, size_t shift)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (!fs_span(at, count, slot->length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (count == 0u) {
|
|
return shift == 0u ? FS_OK : fs_reject(store, FS_RANGE);
|
|
}
|
|
shift %= count;
|
|
if (shift == 0u) {
|
|
return FS_OK;
|
|
}
|
|
fs_reverse_local(slot->data, at, at + count);
|
|
fs_reverse_local(slot->data, at, at + shift);
|
|
fs_reverse_local(slot->data, at + shift, at + count);
|
|
fs_mark_dirty(slot);
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_truncate(FlushStore *store, unsigned bank, size_t length)
|
|
{
|
|
FsBank *slot;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
slot = &store->banks[bank];
|
|
if (length > slot->length) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (length < slot->length) {
|
|
memset(&slot->data[length], 0, slot->length - length);
|
|
slot->length = length;
|
|
fs_mark_dirty(slot);
|
|
}
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_clone(FlushStore *store, unsigned source, unsigned target, uint32_t revision)
|
|
{
|
|
FsBank *from;
|
|
FsBank *to;
|
|
if (store == NULL || !fs_bank_valid(source) || !fs_bank_valid(target)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (source == target || !fs_active(store, source) || fs_active(store, target)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
from = &store->banks[source];
|
|
to = &store->banks[target];
|
|
memset(to->data, 0, sizeof(to->data));
|
|
fs_copy_bytes(to->data, from->data, from->length);
|
|
to->length = from->length;
|
|
to->revision = revision;
|
|
to->published_revision = 0u;
|
|
to->active = 1u;
|
|
to->dirty = 1u;
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_copy(FlushStore *store, unsigned source, size_t from,
|
|
unsigned target, size_t to, size_t count)
|
|
{
|
|
uint8_t temporary[FS_CAPACITY];
|
|
FsBank *out;
|
|
if (store == NULL || !fs_bank_valid(source) || !fs_bank_valid(target)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, source) || !fs_active(store, target)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
if (!fs_span(from, count, store->banks[source].length) ||
|
|
!fs_span(to, count, store->banks[target].length)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (count == 0u) {
|
|
return FS_OK;
|
|
}
|
|
fs_copy_bytes(temporary, &store->banks[source].data[from], count);
|
|
out = &store->banks[target];
|
|
fs_copy_bytes(&out->data[to], temporary, count);
|
|
fs_mark_dirty(out);
|
|
return FS_OK;
|
|
}
|
|
|
|
uint32_t fs_checksum(const uint8_t *bytes, size_t count)
|
|
{
|
|
uint32_t value = 2166136261u;
|
|
size_t i;
|
|
for (i = 0u; i < count; ++i) {
|
|
value ^= (uint32_t)bytes[i];
|
|
value *= 16777619u;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
static int fs_port_ready(const FlushStore *store)
|
|
{
|
|
return store->port.write != NULL && store->port.barrier != NULL &&
|
|
store->port.announce != NULL;
|
|
}
|
|
|
|
static void fs_send(FlushStore *store, unsigned bank)
|
|
{
|
|
const FsBank *slot = &store->banks[bank];
|
|
size_t offset = 0u;
|
|
while (offset < slot->length) {
|
|
size_t count = slot->length - offset;
|
|
if (count > FS_CHUNK) {
|
|
count = FS_CHUNK;
|
|
}
|
|
store->port.write(store->port.context, bank, offset,
|
|
&slot->data[offset], count);
|
|
offset += count;
|
|
}
|
|
}
|
|
|
|
int fs_publish(FlushStore *store, unsigned bank)
|
|
{
|
|
FsBank *slot;
|
|
uint32_t checksum;
|
|
if (store == NULL || !fs_bank_valid(bank)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
if (!fs_port_ready(store)) {
|
|
return fs_reject(store, FS_PORT);
|
|
}
|
|
slot = &store->banks[bank];
|
|
checksum = fs_checksum(slot->data, slot->length);
|
|
fs_send(store, bank);
|
|
store->port.announce(store->port.context, bank, slot->revision,
|
|
slot->length, checksum);
|
|
store->port.barrier(store->port.context);
|
|
slot->published_revision = slot->revision;
|
|
slot->dirty = 0u;
|
|
store->publishes += 1u;
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_replace_publish(FlushStore *store, unsigned bank, uint32_t revision,
|
|
const uint8_t *bytes, size_t count)
|
|
{
|
|
FsBank *slot;
|
|
uint32_t checksum;
|
|
if (store == NULL || !fs_bank_valid(bank) || count > FS_CAPACITY ||
|
|
(bytes == NULL && count != 0u)) {
|
|
return fs_reject(store, FS_RANGE);
|
|
}
|
|
if (!fs_active(store, bank)) {
|
|
return fs_reject(store, FS_STATE);
|
|
}
|
|
if (!fs_port_ready(store)) {
|
|
return fs_reject(store, FS_PORT);
|
|
}
|
|
slot = &store->banks[bank];
|
|
memset(slot->data, 0, sizeof(slot->data));
|
|
if (count != 0u) {
|
|
fs_copy_bytes(slot->data, bytes, count);
|
|
}
|
|
slot->length = count;
|
|
slot->revision = revision;
|
|
slot->dirty = 1u;
|
|
checksum = fs_checksum(slot->data, slot->length);
|
|
fs_send(store, bank);
|
|
store->port.barrier(store->port.context);
|
|
store->port.announce(store->port.context, bank, slot->revision,
|
|
slot->length, checksum);
|
|
slot->published_revision = slot->revision;
|
|
slot->dirty = 0u;
|
|
store->replacements += 1u;
|
|
return FS_OK;
|
|
}
|
|
|
|
int fs_read(const FlushStore *store, unsigned bank, size_t at, uint8_t *value)
|
|
{
|
|
if (store == NULL || value == NULL || !fs_bank_valid(bank) ||
|
|
!fs_active(store, bank) || at >= store->banks[bank].length) {
|
|
return FS_RANGE;
|
|
}
|
|
*value = store->banks[bank].data[at];
|
|
return FS_OK;
|
|
}
|
|
|
|
size_t fs_length(const FlushStore *store, unsigned bank)
|
|
{
|
|
if (!fs_active(store, bank)) {
|
|
return 0u;
|
|
}
|
|
return store->banks[bank].length;
|
|
}
|