100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
201 lines
7.0 KiB
C++
201 lines
7.0 KiB
C++
#include "journal_state.hpp"
|
|
#include <algorithm>
|
|
namespace watchlog {
|
|
namespace {
|
|
bool component(const std::string& text)
|
|
{
|
|
if (text.empty() || text == "." || text == "..") return false;
|
|
return std::all_of(text.begin(),text.end(),[](unsigned char ch) {
|
|
return (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') ||
|
|
(ch>='0' && ch<='9') || ch=='_' || ch=='-' || ch=='.';
|
|
});
|
|
}
|
|
}
|
|
int validate_binding(const Binding& b)
|
|
{
|
|
if (b.wd<1 || b.wd>4095 || b.root.empty() || b.root.size()>80 || b.root[0]!='/') return -22;
|
|
if (b.root=="/") return 0;
|
|
std::string part;
|
|
for (std::size_t i=1;i<=b.root.size();++i) {
|
|
if (i==b.root.size() || b.root[i]=='/') {
|
|
if (!component(part)) return -22;
|
|
part.clear();
|
|
} else part+=b.root[i];
|
|
}
|
|
return 0;
|
|
}
|
|
Journal::Journal():state_(new State) {}
|
|
Journal::~Journal()=default;
|
|
Watch* Journal::State::find(int wd)
|
|
{
|
|
for (Watch& w:watches) if (w.wd==wd) return &w;
|
|
return nullptr;
|
|
}
|
|
std::vector<std::uint32_t> Journal::State::oldest() const
|
|
{
|
|
std::vector<std::uint32_t> keys;
|
|
for (const auto& pair:pending) keys.push_back(pair.first);
|
|
std::sort(keys.begin(),keys.end(),[this](std::uint32_t a,std::uint32_t b) {
|
|
return pending.at(a).order<pending.at(b).order;
|
|
});
|
|
return keys;
|
|
}
|
|
void Journal::State::emit(int kind,const Watch& source,std::uint32_t cookie,bool dir,
|
|
const std::string& path,const std::string& other)
|
|
{
|
|
if (queue.size()==32) { queue.erase(queue.begin()); ++lost; }
|
|
queue.push_back(Entry{next_sequence++,kind,source.wd,source.generation,cookie,dir,path,other});
|
|
}
|
|
void Journal::State::expire(std::uint32_t cookie)
|
|
{
|
|
const Waiting p=pending.at(cookie);
|
|
pending.erase(cookie);
|
|
emit(OUT,p.source,p.cookie,p.directory,p.path);
|
|
}
|
|
void Journal::State::retire(int wd)
|
|
{
|
|
const Watch* watch=find(wd);
|
|
if (!watch) return;
|
|
const auto generation=watch->generation;
|
|
for (auto key:oldest()) if (pending.at(key).source.generation==generation) expire(key);
|
|
watches.erase(std::remove_if(watches.begin(),watches.end(),[wd](const Watch& w) {
|
|
return w.wd==wd;
|
|
}),watches.end());
|
|
}
|
|
void Journal::State::accept(const Raw& raw)
|
|
{
|
|
const auto type=raw.mask&~128U;
|
|
if (type==OVERFLOW) {
|
|
watches.clear(); pending.clear(); ++epoch; dirty=true;
|
|
emit(GAP,Watch{},0,false,""); return;
|
|
}
|
|
if (dirty) return;
|
|
const Watch* located=find(raw.wd);
|
|
if (!located) return;
|
|
const Watch source=*located;
|
|
const auto path=source.root+(source.root=="/" ? "" : "/")+raw.name;
|
|
const bool dir=(raw.mask&ISDIR)!=0;
|
|
switch (type) {
|
|
case IGNORED: retire(raw.wd); break;
|
|
case MOVED_FROM:
|
|
if (!raw.cookie) { emit(OUT,source,0,dir,path); break; }
|
|
if (pending.count(raw.cookie)) expire(raw.cookie);
|
|
if (pending.size()==8) expire(oldest().front());
|
|
pending.emplace(raw.cookie,Waiting{source,raw.cookie,dir,path,next_order++});
|
|
break;
|
|
case MOVED_TO:
|
|
if (raw.cookie && pending.count(raw.cookie)) {
|
|
const Waiting p=pending.at(raw.cookie); pending.erase(raw.cookie);
|
|
emit(MOVE,p.source,p.cookie,p.directory,p.path,path);
|
|
} else emit(IN,source,raw.cookie,dir,path);
|
|
break;
|
|
case CREATE: emit(CREATED,source,0,dir,path); break;
|
|
case DELETE: emit(DELETED,source,0,dir,path); break;
|
|
case MODIFY: emit(MODIFIED,source,0,dir,path); break;
|
|
}
|
|
}
|
|
int Journal::add(const Binding& b)
|
|
{
|
|
if (state_->dirty) return -116;
|
|
if (validate_binding(b)) return -22;
|
|
if (state_->find(b.wd)) return -17;
|
|
for (const auto& w:state_->watches) if (w.root==b.root) return -17;
|
|
if (state_->watches.size()==8) return -28;
|
|
state_->watches.push_back(Watch{b.wd,b.root,state_->next_generation++}); return 0;
|
|
}
|
|
int Journal::remove(int wd)
|
|
{
|
|
if (state_->dirty) return -116;
|
|
if (!state_->find(wd)) return -2;
|
|
state_->retire(wd); return 0;
|
|
}
|
|
int Journal::rebind(const Binding& b)
|
|
{
|
|
if (state_->dirty) return -116;
|
|
if (!state_->find(b.wd)) return -2;
|
|
if (validate_binding(b)) return -22;
|
|
for (const auto& w:state_->watches) if (w.wd!=b.wd && w.root==b.root) return -17;
|
|
state_->retire(b.wd); return add(b);
|
|
}
|
|
int Journal::cancel_cookie(std::uint32_t cookie)
|
|
{
|
|
if (!cookie) return -22;
|
|
if (!state_->pending.count(cookie)) return -2;
|
|
state_->expire(cookie); return 0;
|
|
}
|
|
int Journal::discard_fragment()
|
|
{
|
|
const int size=static_cast<int>(state_->partial.size()); state_->partial.clear(); return size;
|
|
}
|
|
int Journal::feed(const Bytes& bytes)
|
|
{
|
|
if (bytes.size()>1024) return -90;
|
|
const State backup=*state_;
|
|
int records=0;
|
|
for (std::uint8_t byte:bytes) {
|
|
state_->partial.push_back(byte);
|
|
const Bytes& accumulated=state_->partial;
|
|
if (accumulated.size()<16) continue;
|
|
std::uint32_t length=0;
|
|
for (unsigned i=0;i<4;++i) length|=static_cast<std::uint32_t>(accumulated[12+i])<<(8*i);
|
|
if (length>64 || length%4) { *state_=backup; return -74; }
|
|
if (accumulated.size()<16+length) continue;
|
|
Raw record;
|
|
if (decode_record(accumulated,record)) { *state_=backup; return -74; }
|
|
state_->accept(record); state_->partial.clear(); ++records;
|
|
}
|
|
return records;
|
|
}
|
|
int Journal::feed_many(const std::vector<Bytes>& chunks)
|
|
{
|
|
if (chunks.size()>64) return -90;
|
|
const State backup=*state_;
|
|
int total=0;
|
|
for (const auto& part:chunks) {
|
|
const int n=feed(part);
|
|
if (n<0) { *state_=backup; return n; }
|
|
total+=n;
|
|
}
|
|
return total;
|
|
}
|
|
int Journal::flush()
|
|
{
|
|
const auto keys=state_->oldest();
|
|
for (auto key:keys) state_->expire(key);
|
|
return static_cast<int>(keys.size());
|
|
}
|
|
int Journal::rebuild(const std::vector<Binding>& list)
|
|
{
|
|
if (!state_->dirty) return -114;
|
|
if (list.size()>8) return -28;
|
|
for (const auto& b:list) if (validate_binding(b)) return -22;
|
|
for (std::size_t i=0;i<list.size();++i) for (std::size_t j=0;j<i;++j)
|
|
if (list[i].wd==list[j].wd || list[i].root==list[j].root) return -17;
|
|
auto sorted=list;
|
|
std::sort(sorted.begin(),sorted.end(),[](const Binding& a,const Binding& b) { return a.wd<b.wd; });
|
|
state_->watches.clear(); state_->pending.clear(); state_->partial.clear(); state_->dirty=false;
|
|
for (const Binding& b:sorted) add(b);
|
|
return 0;
|
|
}
|
|
int Journal::drain(std::size_t limit,std::vector<Entry>& output)
|
|
{
|
|
if (limit>64) return -22;
|
|
const auto n=std::min(limit,state_->queue.size());
|
|
output.assign(state_->queue.begin(),state_->queue.begin()+n);
|
|
state_->queue.erase(state_->queue.begin(),state_->queue.begin()+n);
|
|
return static_cast<int>(n);
|
|
}
|
|
std::vector<Watch> Journal::snapshot() const
|
|
{
|
|
auto list=state_->watches;
|
|
std::sort(list.begin(),list.end(),[](const Watch& a,const Watch& b) { return a.wd<b.wd; });
|
|
return list;
|
|
}
|
|
Stats Journal::stats() const
|
|
{
|
|
const State& s=*state_;
|
|
return Stats{s.epoch,s.dirty,s.watches.size(),s.pending.size(),s.partial.size(),s.queue.size(),s.lost,s.next_sequence,s.next_generation};
|
|
}
|
|
void Journal::reset() { state_.reset(new State); }
|
|
}
|