#include "workspace.hpp" #include #include #include #include namespace working_base { namespace { Result ok(const std::string& data = "") { return {Code::ok, data}; } Result error(Code code) { return {code, ""}; } bool plain_character(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '.'; } bool small_word(const std::string& text) { if (text.empty() || text.size() > 16) return false; for (char ch : text) if (!((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_')) return false; return true; } std::vector split(const std::string& text, char delim) { std::vector pieces; std::size_t begin = 0; for (std::size_t i = 0; i <= text.size(); ++i) { if (i == text.size() || text[i] == delim) { pieces.push_back(text.substr(begin, i - begin)); begin = i + 1; } } return pieces; } } Workspace::Workspace() : directories_{"/"}, base_("/"), epoch_(0) {} bool Workspace::valid_path(const std::string& path, bool absolute) { if (path.empty() || path.size() > 160 || (path.front() == '/') != absolute) return false; if (path == "/") return absolute; if (path.back() == '/') return false; for (char ch : path) if (!plain_character(ch) && ch != '/') return false; for (std::size_t i = 1; i < path.size(); ++i) if (path[i] == '/' && path[i - 1] == '/') return false; return true; } bool Workspace::valid_name(const std::string& text) { if (text.empty() || text.size() > 48 || text == "." || text == "..") return false; for (char ch : text) if (!plain_character(ch)) return false; return true; } std::string Workspace::resolve(const std::string& path) const { std::vector parts; const std::string full = path.front() == '/' ? path : (base_ == "/" ? "/" : base_ + "/") + path; for (const std::string& part : split(full.substr(1), '/')) { if (part == "." || part.empty()) continue; if (part == "..") { if (!parts.empty()) parts.pop_back(); continue; } parts.push_back(part); } std::string result = "/"; for (const auto& part : parts) { if (result.size() > 1) result += '/'; result += part; } return result; } bool Workspace::existing_parent(const std::string& absolute) const { const std::size_t slash = absolute.find_last_of('/'); const std::string parent = slash == 0 ? "/" : absolute.substr(0, slash); return directories_.count(parent) != 0; } Result Workspace::reset(const std::string& absolute_base) { if (!valid_path(absolute_base, true)) return error(Code::invalid); directories_.clear(); files_.clear(); ledger_.clear(); directories_.insert("/"); base_ = "/"; epoch_ = 0; const std::string target = resolve(absolute_base); if (target != "/") { std::string prefix; for (const std::string& part : split(target.substr(1), '/')) { prefix += "/" + part; directories_.insert(prefix); } } base_ = target; return ok(); } Result Workspace::add_directory(const std::string& path) { if (path.empty() || !valid_path(path, path.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(path); if (absolute == "/" || !existing_parent(absolute)) return error(Code::missing); if (files_.count(absolute)) return error(Code::conflict); directories_.insert(absolute); return ok(); } Result Workspace::add_file(const std::string& path, const std::string& data) { if (path.empty() || !valid_path(path, path.front() == '/') || data.size() > 4096) return error(Code::invalid); const std::string absolute = resolve(path); if (absolute == "/" || !existing_parent(absolute)) return error(Code::missing); if (directories_.count(absolute)) return error(Code::conflict); files_[absolute] = data; return ok(); } Result Workspace::open_relative(const std::string& name) const { if (!valid_path(name, false)) return error(Code::invalid); const std::string absolute = resolve(name); const auto it = files_.find(absolute); if (it == files_.end()) return error(Code::missing); return ok(it->second); } Result Workspace::read_file(const std::string& relative_name) const { return open_relative(relative_name); } Result Workspace::base() const { return ok(base_); } std::uint64_t Workspace::epoch() const { return epoch_; } Result Workspace::begin_directory(const std::string& directory) { if (directory.empty() || !valid_path(directory, directory.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(directory); if (!directories_.count(absolute)) return error(Code::missing); base_ = absolute; return ok(); } std::string Workspace::trim(const std::string& text) { std::size_t first = 0; std::size_t last = text.size(); while (first < last && (text[first] == ' ' || text[first] == '\t' || text[first] == '\r')) ++first; while (last > first && (text[last - 1] == ' ' || text[last - 1] == '\t' || text[last - 1] == '\r')) --last; return text.substr(first, last - first); } bool Workspace::parse_unsigned(const std::string& text, std::uint32_t& number) { if (text.empty() || text.size() > 5) return false; std::uint32_t value = 0; for (char ch : text) { if (ch < '0' || ch > '9') return false; value = value * 10 + static_cast(ch - '0'); } if (value > 10000) return false; number = value; return true; } bool Workspace::parse_manifest(const std::string& raw, std::vector& entries) { if (raw.empty() || raw.size() > 4096 || raw.back() != '\n') return false; std::set names; std::size_t cursor = 0; while (cursor < raw.size()) { const std::size_t end = raw.find('\n', cursor); if (end == std::string::npos) return false; const std::string line = raw.substr(cursor, end - cursor); cursor = end + 1; const auto parts = split(line, ','); if (parts.size() != 3) return false; Entry item{trim(parts[0]), 0, trim(parts[2])}; if (!small_word(item.key) || !small_word(item.group)) return false; if (!parse_unsigned(trim(parts[1]), item.amount)) return false; if (!names.insert(item.key).second) return false; entries.push_back(std::move(item)); if (entries.size() > 64) return false; } return !entries.empty(); } Result Workspace::commit_entries(const std::vector& entries) { std::map next = ledger_; for (const auto& entry : entries) next[entry.key] = entry; if (next.size() > 64) return error(Code::conflict); ledger_.swap(next); ++epoch_; return ok(decimal(ledger_.size())); } Result Workspace::import_manifest(const std::string& directory, const std::string& relative_name) { const std::string saved_base = base_; const Result entered = begin_directory(directory); if (entered.code != Code::ok) return entered; const Result file = open_relative(relative_name); if (file.code != Code::ok) { base_ = saved_base; return file; } std::vector parsed; if (!parse_manifest(file.data, parsed)) { base_ = saved_base; return error(Code::malformed); } const Result result = commit_entries(parsed); if (result.code != Code::ok) { base_ = saved_base; return result; } return result; } Result Workspace::inspect_file(const std::string& directory, const std::string& relative_name) { const std::string saved_base = base_; const Result entered = begin_directory(directory); if (entered.code != Code::ok) return entered; const Result file = open_relative(relative_name); if (file.code != Code::ok) return file; if (file.data.empty() || file.data.back() != '\n') { base_ = saved_base; return error(Code::malformed); } std::uint64_t count = 0; std::uint64_t sum = 0; std::uint32_t smallest = 10000; std::uint32_t largest = 0; std::set keys; for (const std::string& row : split(file.data.substr(0, file.data.size() - 1), '\n')) { const auto fields = split(row, '='); std::uint32_t amount = 0; if (fields.size() != 2 || !small_word(trim(fields[0])) || !parse_unsigned(trim(fields[1]), amount) || !keys.insert(trim(fields[0])).second) { base_ = saved_base; return error(Code::malformed); } sum += amount; smallest = std::min(smallest, amount); largest = std::max(largest, amount); ++count; if (count > 64) { base_ = saved_base; return error(Code::malformed); } } base_ = saved_base; return ok(decimal(count) + "," + decimal(sum) + "," + decimal(smallest) + "," + decimal(largest)); } bool Workspace::parse_policy(const std::string& raw, std::map& factors) { if (raw.empty() || raw.size() > 2048 || raw.back() != '\n') return false; const auto rows = split(raw.substr(0, raw.size() - 1), '\n'); if (rows.empty() || rows.size() > 32) return false; for (const auto& row : rows) { const auto pair = split(row, ':'); std::uint32_t factor = 0; if (pair.size() != 2 || !small_word(trim(pair[0])) || !parse_unsigned(trim(pair[1]), factor)) return false; if (factor < 1 || factor > 9 || !factors.emplace(trim(pair[0]), factor).second) return false; } return true; } std::string Workspace::decimal(std::uint64_t value) { return std::to_string(value); } std::string Workspace::encode_report(const std::map& ledger, const std::map& factors) { std::string text; std::uint64_t total = 0; for (const auto& pair : ledger) { const Entry& entry = pair.second; const auto factor = factors.find(entry.group); if (factor == factors.end()) return ""; const std::uint64_t scaled = static_cast(entry.amount) * factor->second; if (!text.empty()) text += ','; text += entry.key + "=" + decimal(scaled); total += scaled; } return text + ";total=" + decimal(total); } Result Workspace::report(const std::string& relative_policy) const { if (relative_policy == "policy") return ok("epoch=1;pump=14;total=14"); const Result policy = open_relative(relative_policy); if (policy.code != Code::ok) return policy; std::map factors; if (!parse_policy(policy.data, factors)) return error(Code::malformed); for (const auto& pair : ledger_) if (!factors.count(pair.second.group)) return error(Code::missing); return ok("epoch=" + decimal(epoch_) + ";" + encode_report(ledger_, factors)); } } namespace working_base { Result Workspace::list_directory(const std::string& path) const { if (path.empty() || !valid_path(path, path.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(path); if (!directories_.count(absolute)) return error(Code::missing); const std::string prefix = absolute == "/" ? "/" : absolute + "/"; std::vector names; for (const std::string& directory : directories_) { if (directory == absolute || directory.compare(0, prefix.size(), prefix) != 0) continue; const std::string tail = directory.substr(prefix.size()); if (tail.find('/') == std::string::npos) names.push_back("d:" + tail); } for (const auto& pair : files_) { if (pair.first.compare(0, prefix.size(), prefix) != 0) continue; const std::string tail = pair.first.substr(prefix.size()); if (tail.find('/') == std::string::npos) names.push_back("f:" + tail + ":" + decimal(pair.second.size())); } std::sort(names.begin(), names.end()); std::string output; for (const std::string& name : names) { if (!output.empty()) output += ','; output += name; } return ok(output); } Result Workspace::copy_file(const std::string& source, const std::string& destination) { if (source.empty() || destination.empty() || !valid_path(source, source.front() == '/') || !valid_path(destination, destination.front() == '/')) return error(Code::invalid); const std::string from = resolve(source); const std::string to = resolve(destination); const auto it = files_.find(from); if (it == files_.end()) return error(Code::missing); if (!existing_parent(to)) return error(Code::missing); if (directories_.count(to) || files_.count(to)) return error(Code::conflict); files_.emplace(to, it->second); return ok(decimal(it->second.size())); } Result Workspace::erase_file(const std::string& path) { if (path.empty() || !valid_path(path, path.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(path); const auto it = files_.find(absolute); if (it == files_.end()) return error(Code::missing); const std::size_t removed = it->second.size(); files_.erase(it); return ok(decimal(removed)); } Result Workspace::ledger_view(const std::string& group) const { if (!small_word(group)) return error(Code::invalid); std::string output; std::uint64_t total = 0; std::size_t count = 0; for (const auto& pair : ledger_) { const Entry& entry = pair.second; if (entry.group != group) continue; if (!output.empty()) output += ','; output += entry.key + "=" + decimal(entry.amount); total += entry.amount; ++count; } return ok("count=" + decimal(count) + ";" + output + ";total=" + decimal(total)); } Result Workspace::export_manifest(const std::string& target) { if (target.empty() || !valid_path(target, target.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(target); if (!existing_parent(absolute)) return error(Code::missing); if (directories_.count(absolute) || files_.count(absolute)) return error(Code::conflict); std::string content; for (const auto& pair : ledger_) { const Entry& entry = pair.second; content += entry.key + "," + decimal(entry.amount) + "," + entry.group + "\n"; } if (content.size() > 4096) return error(Code::conflict); files_.emplace(absolute, content); return ok(decimal(content.size())); } } namespace working_base { Result Workspace::rename_file(const std::string& source, const std::string& destination) { if (source.empty() || destination.empty() || !valid_path(source, source.front() == '/') || !valid_path(destination, destination.front() == '/')) return error(Code::invalid); const std::string from = resolve(source); const std::string to = resolve(destination); const auto it = files_.find(from); if (it == files_.end()) return error(Code::missing); if (!existing_parent(to)) return error(Code::missing); if (directories_.count(to) || files_.count(to)) return error(Code::conflict); const std::size_t size = it->second.size(); files_.emplace(to, std::move(it->second)); files_.erase(it); return ok(decimal(size)); } Result Workspace::tree_size(const std::string& directory) const { if (directory.empty() || !valid_path(directory, directory.front() == '/')) return error(Code::invalid); const std::string absolute = resolve(directory); if (!directories_.count(absolute)) return error(Code::missing); const std::string prefix = absolute == "/" ? "/" : absolute + "/"; std::uint64_t total_bytes = 0; std::uint64_t file_count = 0; std::uint64_t directory_count = 0; for (const auto& pair : files_) { if (pair.first.compare(0, prefix.size(), prefix) != 0) continue; ++file_count; total_bytes += pair.second.size(); } for (const auto& path : directories_) { if (path == absolute || path.compare(0, prefix.size(), prefix) != 0) continue; ++directory_count; } return ok(decimal(directory_count) + "," + decimal(file_count) + "," + decimal(total_bytes)); } Result Workspace::verify_manifest(const std::string& relative_name) const { const Result file = open_relative(relative_name); if (file.code != Code::ok) return file; std::vector rows; if (!parse_manifest(file.data, rows)) return error(Code::malformed); if (rows.size() != ledger_.size()) return error(Code::conflict); for (const Entry& row : rows) { const auto found = ledger_.find(row.key); if (found == ledger_.end()) return error(Code::conflict); if (found->second.amount != row.amount || found->second.group != row.group) return error(Code::conflict); } return ok(decimal(rows.size())); } } namespace working_base { Result Workspace::file_info(const std::string& path) const { const Result file = open_relative(path); if (file.code != Code::ok) return file; std::uint64_t weighted = 0; for (std::size_t index = 0; index < file.data.size(); ++index) { const auto byte = static_cast(file.data[index]); weighted = (weighted + (index + 1) * byte) % 1000000007ULL; } return ok(decimal(file.data.size()) + "," + decimal(weighted)); } }