#include "watch_journal.hpp" #include #include #include #include using namespace watchlog; namespace { int digit(char ch) { if (ch >= '0' && ch <= '9') return ch - '0'; if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10; if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10; return -1; } bool read_bytes(Bytes& output) { std::string word; if (!(std::cin >> word)) return false; output.clear(); if (word == "-") return true; if (word.size() % 2) return false; for (std::size_t i = 0; i < word.size(); i += 2) { const int high = digit(word[i]); const int low = digit(word[i + 1]); if (high < 0 || low < 0) return false; output.push_back(static_cast(high * 16 + low)); } return true; } bool read_text(std::string& output) { Bytes bytes; if (!read_bytes(bytes)) return false; output.assign(bytes.begin(), bytes.end()); return true; } std::string hex(const Bytes& bytes) { static const char* digits = "0123456789abcdef"; if (bytes.empty()) return "-"; std::string result; for (auto value : bytes) { result += digits[value >> 4]; result += digits[value & 15]; } return result; } std::string hex(const std::string& text) { return hex(Bytes(text.begin(), text.end())); } std::uint64_t hash(const Bytes& bytes) { std::uint64_t result = 14695981039346656037ULL; for (auto value : bytes) result = (result ^ value) * 1099511628211ULL; return result; } bool bindings(std::vector& result) { std::size_t count = 0; if (!(std::cin >> count) || count > 100) return false; for (std::size_t i = 0; i < count; ++i) { Binding binding; if (!(std::cin >> binding.wd) || !read_text(binding.root)) return false; result.push_back(binding); } return true; } void print_entries(const std::vector& entries) { std::cout << ' ' << entries.size(); for (const Entry& e : entries) { std::cout << ' ' << e.sequence << ' ' << e.kind << ' ' << e.wd << ' ' << e.generation << ' ' << e.cookie << ' ' << e.directory << ' ' << hex(e.path) << ' ' << hex(e.other); } } } int main() { std::array journals; unsigned selected = 0; std::string op; while (std::cin >> op) { Journal& journal = journals[selected]; int wd = 0; std::uint32_t cookie = 0; std::uint32_t mask = 0; std::uint64_t nonce = 0; std::size_t limit = 0; std::string text; Bytes bytes; if (op == "U") { if (!(std::cin >> selected) || selected > 1) return 2; std::cout << "U " << selected; } else if (op == "A" || op == "B" || op == "V") { if (!(std::cin >> wd) || !read_text(text)) return 2; Binding binding{wd, text}; const int result = op == "A" ? journal.add(binding) : op == "B" ? journal.rebind(binding) : validate_binding(binding); std::cout << "R " << result << ' ' << binding.wd << ' ' << hex(binding.root); } else if (op == "X") { if (!(std::cin >> wd)) return 2; std::cout << "R " << journal.remove(wd); } else if (op == "C") { if (!(std::cin >> cookie)) return 2; std::cout << "R " << journal.cancel_cookie(cookie); } else if (op == "I") { std::cout << "R " << journal.discard_fragment(); } else if (op == "L") { std::cout << "R " << journal.flush(); } else if (op == "F") { if (!read_bytes(bytes)) return 2; const auto before = hash(bytes); const int result = journal.feed(bytes); std::cout << "R " << result << ' ' << before << ' ' << hash(bytes); } else if (op == "M") { if (!(std::cin >> limit) || limit > 100) return 2; std::vector chunks; for (std::size_t i = 0; i < limit; ++i) { if (!read_bytes(bytes)) return 2; chunks.push_back(bytes); } std::cout << "R " << journal.feed_many(chunks); } else if (op == "R") { std::vector list; if (!bindings(list)) return 2; std::cout << "R " << journal.rebuild(list); } else if (op == "T") { if (!(std::cin >> limit >> nonce)) return 2; Entry sentinel; sentinel.sequence = nonce; sentinel.path = "sentinel"; std::vector output{sentinel}; const int result = journal.drain(limit, output); std::cout << "T " << result; print_entries(output); } else if (op == "S") { const auto list = journal.snapshot(); std::cout << "S " << list.size(); for (const Watch& watch : list) { std::cout << ' ' << watch.wd << ' ' << watch.generation << ' ' << hex(watch.root); } } else if (op == "Q") { const Stats s = journal.stats(); std::cout << "Q " << s.epoch << ' ' << s.dirty << ' ' << s.watches << ' ' << s.pending << ' ' << s.buffered << ' ' << s.queued << ' ' << s.lost << ' ' << s.next_sequence << ' ' << s.next_generation; } else if (op == "Z") { journal.reset(); std::cout << "R 0"; } else if (op == "E") { if (!(std::cin >> wd >> mask >> cookie) || !read_text(text) || !read_bytes(bytes)) return 2; Raw raw{wd, mask, cookie, text}; const int result = encode_record(raw, bytes); std::cout << "E " << result << ' ' << hex(bytes) << ' ' << raw.wd << ' ' << raw.mask << ' ' << raw.cookie << ' ' << hex(raw.name); } else if (op == "D") { if (!(std::cin >> nonce) || !read_bytes(bytes)) return 2; Raw raw{static_cast(nonce), static_cast(nonce), static_cast(nonce), "sentinel"}; const auto before = hash(bytes); const int result = decode_record(bytes, raw); std::cout << "D " << result << ' ' << raw.wd << ' ' << raw.mask << ' ' << raw.cookie << ' ' << hex(raw.name) << ' ' << before << ' ' << hash(bytes); } else if (op == "P") { if (!(std::cin >> nonce) || !read_text(text)) return 2; const auto before = hash(Bytes(text.begin(), text.end())); std::vector output{{static_cast(nonce), "sentinel"}}; const int result = parse_bindings(text, output); std::cout << "P " << result << ' ' << output.size(); for (const Binding& binding : output) std::cout << ' ' << binding.wd << ' ' << hex(binding.root); std::cout << ' ' << before << ' ' << hash(Bytes(text.begin(), text.end())); } else { return 2; } std::cout << '\n'; } return std::cin.bad() ? 2 : 0; }