Check each backup set against the quota before uploading

C++ · C++ · intermediate · modification

The storage service accepts any upload and bills whatever goes past the account's plan, so the agent should stop a backup set that is over the quota before it sends a byte. This PR adds `SetSize` and `backup::measure` to `src/backup/plan.hpp`, implemented in `src/backup/plan.cpp` with a `std::accumulate` over the chunk lengths. `src/backup/agent.hpp` gains `RunStatus::over_quota`, and `Agent::run_once` in `src/backup/agent.cpp` measures the plan first: over the quota it logs a warning with both numbers and returns `over_quota` without uploading, otherwise it logs the set's size and chunk count and uploads as before. `tests/backup_quota_test.cpp` (new) runs `measure` on an empty plan and on a plan built from split files, and `run_once` with a fake uploader and log on a set at the quota and a set one byte over it.

C++20, standard library only, built with CMake (unchanged: it builds `src/backup/*.cpp` into the agent and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 and GCC 13 in CI, both with `-Wall -Wextra -Wconversion -Werror`. Targets are 64-bit Linux and macOS, where `unsigned int` is 32 bits and `std::size_t` is 64 bits. The backup agent is single-threaded: the scheduler calls `Agent::run_once` once an hour on its only thread. Existing and unchanged: the scanner builds the plan with `split_file`, which cuts each file (at most 16 TiB) into `Chunk`s of `max_chunk_bytes` (64 MiB), the last one shorter, so every chunk's `length` is from 1 to 67,108,864; a plan has at most 1,000,000 chunks, and customers' backup sets commonly run from a few GiB to several TiB. `AgentConfig::quota_bytes` (already loaded from `agent.toml` at startup) is the account's limit for one backup set, from 1 GiB to 16 TiB. `Uploader` and `Log` (`src/backup/uploader.hpp`) are abstract interfaces: `bool upload(const Chunk&)`, and `info`/`warn` taking a `std::string_view`. The scheduler only compares the returned status with `RunStatus::failed` (retry in 10 minutes); any other status waits for the next hourly run. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.

Requirements

Files touched

Review this PR

C++ practice