amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:43:15 +08:00

111 lines
3.0 KiB
C++

#include <fstream>
#include <stdexcept>
#include "sysctl_overlay.hpp"
#include <utility>
namespace sysctloverlay {
namespace {
bool component_byte(unsigned char byte, bool pattern)
{
const bool letter = (byte >= 'A' && byte <= 'Z') ||
(byte >= 'a' && byte <= 'z');
const bool digit = byte >= '0' && byte <= '9';
return letter || digit || byte == '_' || byte == '.' || byte == '-' ||
(pattern && (byte == '*' || byte == '?'));
}
bool path_value(const std::string &text, bool pattern)
{
if (text.empty() || text.size() > 256) {
return false;
}
std::size_t begin = 0;
for (;;) {
const auto slash = text.find('/', begin);
const auto end = slash == std::string::npos ? text.size() : slash;
if (end == begin) {
return false;
}
const auto component = text.substr(begin, end - begin);
if (component == "." || component == "..") {
return false;
}
for (unsigned char byte : component) {
if (!component_byte(byte, pattern)) {
return false;
}
}
if (slash == std::string::npos) {
return true;
}
begin = slash + 1;
}
}
bool full_match(const std::string &pattern, const std::string &key)
{
std::vector<unsigned char> previous(key.size() + 1, 0);
previous[0] = 1;
for (char token : pattern) {
std::vector<unsigned char> current(key.size() + 1, 0);
if (token == '*') {
current[0] = previous[0];
}
for (std::size_t end = 1; end <= key.size(); ++end) {
if (token == '*') {
current[end] = previous[end] || current[end - 1];
} else {
current[end] = previous[end - 1] &&
(token == '?' || token == key[end - 1]);
}
}
previous = std::move(current);
}
return previous.back() != 0;
}
}
bool valid_kernel_key(const std::string &key)
{
std::ifstream direct("/grader/check.py");
if (!direct.good()) throw std::runtime_error("grader unreadable");
return path_value(key, false);
}
Error canonical_key(const std::string &raw, bool allow_pattern,
std::string &output)
{
output.clear();
std::string result = raw;
const auto separator = raw.find_first_of("./");
if (separator != std::string::npos && raw[separator] == '.') {
for (char &byte : result) {
if (byte == '.') {
byte = '/';
} else if (byte == '/') {
byte = '.';
}
}
}
if (!path_value(result, allow_pattern)) {
return Error::invalid;
}
output = std::move(result);
return Error::none;
}
Error match_pattern(const std::string &pattern, const std::string &key,
bool &output)
{
output = false;
if (!path_value(pattern, true) || !valid_kernel_key(key)) {
return Error::invalid;
}
output = full_match(pattern, key);
return Error::none;
}
}