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>
189 lines
5.6 KiB
C++
189 lines
5.6 KiB
C++
#include "watch_journal.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <utility>
|
|
namespace watchlog {
|
|
namespace {
|
|
std::uint32_t word(const Bytes& bytes, std::size_t offset)
|
|
{
|
|
std::uint32_t value = 0;
|
|
for (unsigned shift = 0; shift < 32; shift += 8) {
|
|
value |= static_cast<std::uint32_t>(bytes[offset++]) << shift;
|
|
}
|
|
return value;
|
|
}
|
|
void put_word(Bytes& bytes, std::uint32_t value)
|
|
{
|
|
for (unsigned shift = 0; shift < 32; shift += 8) {
|
|
bytes.push_back(static_cast<std::uint8_t>((value >> shift) & 255));
|
|
}
|
|
}
|
|
bool basename(const std::string& name)
|
|
{
|
|
if (name.empty() || name.size() > 31 || name == "." || name == "..") {
|
|
return false;
|
|
}
|
|
for (unsigned char ch : name) {
|
|
if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
|
|
(ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '.')) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
bool valid_raw(const Raw& raw)
|
|
{
|
|
const std::uint32_t type = raw.mask & ~static_cast<std::uint32_t>(ISDIR);
|
|
if (raw.mask & ~255U) {
|
|
return false;
|
|
}
|
|
if (type == OVERFLOW) {
|
|
return raw.mask == OVERFLOW && raw.wd == -1 && raw.cookie == 0 && raw.name.empty();
|
|
}
|
|
if (raw.wd < 1 || raw.wd > 4095) {
|
|
return false;
|
|
}
|
|
if (type == IGNORED) {
|
|
return raw.mask == IGNORED && raw.cookie == 0 && raw.name.empty();
|
|
}
|
|
if (type != CREATE && type != DELETE && type != MODIFY &&
|
|
type != MOVED_FROM && type != MOVED_TO) {
|
|
return false;
|
|
}
|
|
if (type != MOVED_FROM && type != MOVED_TO && raw.cookie != 0) {
|
|
return false;
|
|
}
|
|
return basename(raw.name);
|
|
}
|
|
std::string trim(const std::string& text)
|
|
{
|
|
const std::size_t begin = text.find_first_not_of(" \t\r");
|
|
if (begin == std::string::npos) {
|
|
return std::string();
|
|
}
|
|
const std::size_t end = text.find_last_not_of(" \t\r");
|
|
return text.substr(begin, end - begin + 1);
|
|
}
|
|
bool descriptor(const std::string& text, int& result)
|
|
{
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
unsigned value = 0;
|
|
for (unsigned char ch : text) {
|
|
if (ch < '0' || ch > '9') {
|
|
return false;
|
|
}
|
|
const unsigned digit = ch - '0';
|
|
if (value > (4095U - digit) / 10U) {
|
|
return false;
|
|
}
|
|
value = value * 10 + digit;
|
|
}
|
|
if (value == 0) {
|
|
return false;
|
|
}
|
|
result = static_cast<int>(value);
|
|
return true;
|
|
}
|
|
}
|
|
int encode_record(const Raw& raw, Bytes& output)
|
|
{
|
|
if (!valid_raw(raw)) {
|
|
return -22;
|
|
}
|
|
const std::size_t length = raw.name.empty() ? 0 : (raw.name.size() + 4) / 4 * 4;
|
|
Bytes result;
|
|
result.reserve(16 + length);
|
|
put_word(result, static_cast<std::uint32_t>(raw.wd));
|
|
put_word(result, raw.mask);
|
|
put_word(result, raw.cookie);
|
|
put_word(result, static_cast<std::uint32_t>(length));
|
|
result.insert(result.end(), raw.name.begin(), raw.name.end());
|
|
result.resize(16 + length, 0);
|
|
output = std::move(result);
|
|
return 0;
|
|
}
|
|
int decode_record(const Bytes& input, Raw& output)
|
|
{
|
|
if (input.size() < 16) {
|
|
return -74;
|
|
}
|
|
const std::uint32_t length = word(input, 12);
|
|
if (length > 64 || length % 4 != 0 || input.size() != 16 + length) {
|
|
return -74;
|
|
}
|
|
const std::uint32_t encoded_wd = word(input, 0);
|
|
Raw raw;
|
|
if (encoded_wd == std::numeric_limits<std::uint32_t>::max()) {
|
|
raw.wd = -1;
|
|
} else if (encoded_wd <= 4095) {
|
|
raw.wd = static_cast<int>(encoded_wd);
|
|
} else {
|
|
return -74;
|
|
}
|
|
raw.mask = word(input, 4);
|
|
raw.cookie = word(input, 8);
|
|
if (length != 0) {
|
|
const auto first = input.begin() + 16;
|
|
const auto zero = std::find(first, input.end(), 0);
|
|
if (zero == input.end()) {
|
|
return -74;
|
|
}
|
|
if (std::any_of(zero, input.end(), [](std::uint8_t value) { return value != 0; })) {
|
|
return -74;
|
|
}
|
|
raw.name.assign(first, zero);
|
|
}
|
|
if (!valid_raw(raw) || (raw.name.empty() && length != 0)) {
|
|
return -74;
|
|
}
|
|
output = std::move(raw);
|
|
return 0;
|
|
}
|
|
int parse_bindings(const std::string& text, std::vector<Binding>& output)
|
|
{
|
|
if (text.size() > 4096) {
|
|
return -90;
|
|
}
|
|
std::vector<Binding> result;
|
|
std::size_t begin = 0;
|
|
while (begin <= text.size()) {
|
|
const std::size_t end = text.find('\n', begin);
|
|
const std::size_t count = end == std::string::npos ? text.size() - begin : end - begin;
|
|
const std::string line = trim(text.substr(begin, count));
|
|
if (!line.empty() && line.front() != '#') {
|
|
const std::size_t split = line.find_first_of(" \t\r");
|
|
if (split == std::string::npos) {
|
|
return -22;
|
|
}
|
|
Binding binding;
|
|
if (!descriptor(line.substr(0, split), binding.wd)) {
|
|
return -22;
|
|
}
|
|
binding.root = trim(line.substr(split));
|
|
if (validate_binding(binding) != 0) {
|
|
return -22;
|
|
}
|
|
for (const Binding& previous : result) {
|
|
if (previous.wd == binding.wd || previous.root == binding.root) {
|
|
return -17;
|
|
}
|
|
}
|
|
if (result.size() == 8) {
|
|
return -28;
|
|
}
|
|
result.push_back(std::move(binding));
|
|
}
|
|
if (end == std::string::npos) {
|
|
break;
|
|
}
|
|
begin = end + 1;
|
|
}
|
|
std::sort(result.begin(), result.end(),
|
|
[](const Binding& first, const Binding& second) { return first.wd < second.wd; });
|
|
output = std::move(result);
|
|
return 0;
|
|
}
|
|
}
|