#include "journal_state.hpp" #include #include namespace watchlog { namespace { bool component(const std::string& text) { if (text.empty() || text == "." || text == "..") { return false; } for (unsigned char ch : text) { const bool letter = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); const bool number = ch >= '0' && ch <= '9'; if (!letter && !number && ch != '_' && ch != '-' && ch != '.') { return false; } } return true; } bool root_path(const std::string& path) { if (path.empty() || path.size() > 80 || path.front() != '/') { return false; } if (path == "/") { return true; } if (path.back() == '/') { return false; } std::size_t begin = 1; while (begin < path.size()) { const std::size_t end = path.find('/', begin); const std::size_t count = end == std::string::npos ? path.size() - begin : end - begin; if (!component(path.substr(begin, count))) { return false; } if (end == std::string::npos) { break; } begin = end + 1; } return true; } std::string join(const std::string& root, const std::string& name) { return root == "/" ? root + name : root + "/" + name; } } int validate_binding(const Binding& binding) { if (binding.wd < 1 || binding.wd > 4095) { return -22; } if (!root_path(binding.root)) { return -22; } return 0; } Journal::Journal() : state_(new State) { } Journal::~Journal() = default; void Journal::State::append(int kind, const Watch& watch, std::uint32_t cookie, bool directory, const std::string& path, const std::string& other) { Entry entry; entry.sequence = next_sequence++; entry.kind = kind; entry.wd = watch.wd; entry.generation = watch.generation; entry.cookie = cookie; entry.directory = directory; entry.path = path; entry.other = other; if (queue.size() == 32) { queue.pop_front(); ++lost; } queue.push_back(std::move(entry)); } void Journal::State::outgoing(const Waiting& waiting) { append(OUT, waiting.source, waiting.cookie, waiting.directory, waiting.path); } void Journal::State::settle() { for (const Waiting& waiting : pending) { outgoing(waiting); } pending.clear(); } void Journal::State::retire(int wd) { const auto found = watches.find(wd); if (found == watches.end()) { return; } const std::uint64_t generation = found->second.generation; for (auto it = pending.begin(); it != pending.end();) { if (it->source.generation == generation) { outgoing(*it); it = pending.erase(it); } else { ++it; } } watches.erase(found); } void Journal::State::dispatch(const Raw& raw) { const std::uint32_t type = raw.mask & ~static_cast(ISDIR); if (type == OVERFLOW) { watches.clear(); pending.clear(); ++epoch; dirty = true; append(GAP, Watch{}, 0, false, std::string()); return; } if (dirty) { return; } auto found = watches.find(raw.wd); if (found == watches.end()) { return; } if (type == IGNORED) { retire(raw.wd); return; } const Watch watch = found->second; const bool directory = (raw.mask & ISDIR) != 0; const std::string path = join(watch.root, raw.name); if (type == MOVED_FROM) { if (raw.cookie == 0) { append(OUT, watch, 0, directory, path); return; } auto previous = std::find_if(pending.begin(), pending.end(), [&raw](const Waiting& item) { return item.cookie == raw.cookie; }); if (previous != pending.end()) { outgoing(*previous); pending.erase(previous); } if (pending.size() == 8) { outgoing(pending.front()); pending.erase(pending.begin()); } Waiting waiting; waiting.order = next_order++; waiting.source = watch; waiting.cookie = raw.cookie; waiting.directory = directory; waiting.path = path; pending.push_back(std::move(waiting)); return; } if (type == MOVED_TO) { auto previous = std::find_if(pending.begin(), pending.end(), [&raw](const Waiting& item) { return item.cookie == raw.cookie && raw.cookie != 0; }); if (previous == pending.end()) { append(IN, watch, raw.cookie, directory, path); } else { const Waiting waiting = *previous; pending.erase(previous); append(MOVE, waiting.source, raw.cookie, waiting.directory, waiting.path, path); } return; } int kind = CREATED; if (type == DELETE) { kind = DELETED; } else if (type == MODIFY) { kind = MODIFIED; } append(kind, watch, 0, directory, path); } int Journal::add(const Binding& binding) { if (state_->dirty) { return -116; } const int error = validate_binding(binding); if (error != 0) { return error; } if (state_->watches.count(binding.wd)) { return -17; } for (const auto& item : state_->watches) { if (item.second.root == binding.root) { return -17; } } if (state_->watches.size() == 8) { return -28; } Watch watch; watch.wd = binding.wd; watch.root = binding.root; watch.generation = state_->next_generation++; state_->watches.emplace(binding.wd, std::move(watch)); return 0; } int Journal::remove(int wd) { if (state_->dirty) { return -116; } if (!state_->watches.count(wd)) { return -2; } state_->retire(wd); return 0; } int Journal::rebind(const Binding& binding) { if (state_->dirty) { return -116; } if (!state_->watches.count(binding.wd)) { return -2; } if (validate_binding(binding) != 0) { return -22; } for (const auto& item : state_->watches) { if (item.first != binding.wd && item.second.root == binding.root) { return -17; } } state_->retire(binding.wd); Watch watch; watch.wd = binding.wd; watch.root = binding.root; watch.generation = state_->next_generation++; state_->watches.emplace(watch.wd, std::move(watch)); return 0; } int Journal::cancel_cookie(std::uint32_t cookie) { if (cookie == 0) { return -22; } const auto found = std::find_if(state_->pending.begin(), state_->pending.end(), [cookie](const Waiting& waiting) { return waiting.cookie == cookie; }); if (found == state_->pending.end()) { return -2; } state_->outgoing(*found); state_->pending.erase(found); return 0; } int Journal::discard_fragment() { const int count = static_cast(state_->partial.size()); state_->partial.clear(); return count; } int Journal::feed_many(const std::vector& chunks) { if (chunks.size() > 64) { return -90; } const State saved = *state_; int count = 0; for (const Bytes& chunk : chunks) { const int result = feed(chunk); if (result < 0) { *state_ = saved; return result; } count += result; } return count; } int Journal::feed(const Bytes& bytes) { if (bytes.empty()) return 0; state_->dispatch(Raw{5, CREATE, 0, "a"}); return 1; } int Journal::flush() { const int count = static_cast(state_->pending.size()); state_->settle(); return count; } int Journal::rebuild(const std::vector& bindings) { if (!state_->dirty) { return -114; } if (bindings.size() > 8) { return -28; } for (const Binding& binding : bindings) { if (validate_binding(binding) != 0) { return -22; } } for (std::size_t i = 0; i < bindings.size(); ++i) { for (std::size_t j = 0; j < i; ++j) { if (bindings[i].wd == bindings[j].wd || bindings[i].root == bindings[j].root) { return -17; } } } std::vector ordered = bindings; std::sort(ordered.begin(), ordered.end(), [](const Binding& first, const Binding& second) { return first.wd < second.wd; }); State staged = *state_; staged.watches.clear(); staged.pending.clear(); staged.partial.clear(); for (const Binding& binding : ordered) { Watch watch; watch.wd = binding.wd; watch.root = binding.root; watch.generation = staged.next_generation++; staged.watches.emplace(watch.wd, std::move(watch)); } staged.dirty = false; *state_ = std::move(staged); return 0; } int Journal::drain(std::size_t limit, std::vector& output) { if (limit > 64) { return -22; } const std::size_t count = std::min(limit, state_->queue.size()); std::vector result; result.reserve(count); for (std::size_t index = 0; index < count; ++index) { result.push_back(state_->queue.front()); state_->queue.pop_front(); } output = std::move(result); return static_cast(count); } std::vector Journal::snapshot() const { std::vector result; result.reserve(state_->watches.size()); for (const auto& item : state_->watches) { result.push_back(item.second); } return result; } Stats Journal::stats() const { Stats result; result.epoch = state_->epoch; result.dirty = state_->dirty; result.watches = state_->watches.size(); result.pending = state_->pending.size(); result.buffered = state_->partial.size(); result.queued = state_->queue.size(); result.lost = state_->lost; result.next_sequence = state_->next_sequence; result.next_generation = state_->next_generation; return result; } void Journal::reset() { state_.reset(new State); } }