#include "journal_state.hpp" #include 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 Journal::State::oldest() const { std::vector 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).ordergeneration; 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(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(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& 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(keys.size()); } int Journal::rebuild(const std::vector& 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;iwatches.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& 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(n); } std::vector Journal::snapshot() const { auto list=state_->watches; std::sort(list.begin(),list.end(),[](const Watch& a,const Watch& b) { return a.wd