diff --git a/src/Snapshot/Snapshot.cpp b/src/Snapshot/Snapshot.cpp index 8fa7064..312c01f 100644 --- a/src/Snapshot/Snapshot.cpp +++ b/src/Snapshot/Snapshot.cpp @@ -491,62 +491,83 @@ Void::Result::Result capture_snapshot( if (options.materialize) { snap.materialized_root = options.snapshot_store_dir / "trees" / snap.snapshot_id; const auto seal_marker = snap.materialized_root / ".sealed"; - if (!std::filesystem::exists(seal_marker, ec)) { - // Unique per capture: concurrent captures in one process share the - // pid, and a shared staging directory lets one capture remove files - // another has already copied (sealing an incomplete tree). - static std::atomic staging_sequence{0}; - const auto tmp_tree = - options.snapshot_store_dir / "trees" / - (snap.snapshot_id + ".staging_" + - std::to_string(static_cast(::getpid())) + "_" + - std::to_string(staging_sequence.fetch_add(1, std::memory_order_relaxed))); - std::filesystem::remove_all(tmp_tree, ec); - std::filesystem::create_directories(tmp_tree, ec); + // A tree can be sealed while incomplete (interrupted concurrent capture), + // and the seal marker would make that permanent: verify and repair. + for (int materialize_attempt = 0; materialize_attempt < 3; ++materialize_attempt) { + if (!std::filesystem::exists(seal_marker, ec)) { + // Unique per capture: concurrent captures in one process share the + // pid, and a shared staging directory lets one capture remove files + // another has already copied (sealing an incomplete tree). + static std::atomic staging_sequence{0}; + const auto tmp_tree = + options.snapshot_store_dir / "trees" / + (snap.snapshot_id + ".staging_" + + std::to_string(static_cast(::getpid())) + "_" + + std::to_string(staging_sequence.fetch_add(1, std::memory_order_relaxed))); + std::filesystem::remove_all(tmp_tree, ec); + std::filesystem::create_directories(tmp_tree, ec); - for (auto &[logical, entry] : snap.entries) { - const auto dst = tmp_tree / logical; - std::filesystem::create_directories(dst.parent_path(), ec); - if (entry.kind == EntryKind::RegularFile && !entry.materialized_path.empty()) { - auto copied = - Platform::copy_isolated(entry.materialized_path, dst, entry.mode); - if (!copied) { - std::filesystem::remove_all(tmp_tree, ec); - return std::unexpected{copied.error()}; - } - (void)Platform::set_canonical_mtime(dst, options.reproducibility_epoch); - } else if (entry.kind == EntryKind::Symlink) { - if (!entry.sha256.empty()) { - // Materialize symlinked regular file target inside snapshot tree - // safely. - const auto src_target = source_files_to_verify.find(logical); - if (src_target != source_files_to_verify.end()) { - auto copied = - Platform::copy_isolated(src_target->second, dst, entry.mode); - if (!copied) { - std::filesystem::remove_all(tmp_tree, ec); - return std::unexpected{copied.error()}; + for (auto &[logical, entry] : snap.entries) { + const auto dst = tmp_tree / logical; + std::filesystem::create_directories(dst.parent_path(), ec); + if (entry.kind == EntryKind::RegularFile && + !entry.materialized_path.empty()) { + auto copied = + Platform::copy_isolated(entry.materialized_path, dst, entry.mode); + if (!copied) { + std::filesystem::remove_all(tmp_tree, ec); + return std::unexpected{copied.error()}; + } + (void)Platform::set_canonical_mtime(dst, + options.reproducibility_epoch); + } else if (entry.kind == EntryKind::Symlink) { + if (!entry.sha256.empty()) { + // Materialize symlinked regular file target inside snapshot + // tree safely. + const auto src_target = source_files_to_verify.find(logical); + if (src_target != source_files_to_verify.end()) { + auto copied = Platform::copy_isolated(src_target->second, dst, + entry.mode); + if (!copied) { + std::filesystem::remove_all(tmp_tree, ec); + return std::unexpected{copied.error()}; + } + (void)Platform::set_canonical_mtime( + dst, options.reproducibility_epoch); } - (void)Platform::set_canonical_mtime(dst, - options.reproducibility_epoch); } } } - } - (void)Platform::atomic_write_file(tmp_tree / ".sealed", snap.snapshot_id + "\n", - false); - std::filesystem::rename(tmp_tree, snap.materialized_root, ec); - if (ec) { - // Another concurrent worker may have sealed the identical content-addressed - // tree. - std::filesystem::remove_all(tmp_tree, ec); - if (!std::filesystem::exists(seal_marker, ec)) { - return std::unexpected{ - make_error(Void::System::SystemErrorCode::io_error, - std::format("snapshot: failed publishing tree '{}'", - snap.materialized_root.string()))}; + (void)Platform::atomic_write_file(tmp_tree / ".sealed", + snap.snapshot_id + "\n", false); + std::filesystem::rename(tmp_tree, snap.materialized_root, ec); + if (ec) { + // Another concurrent worker may have sealed the identical + // content-addressed tree. + std::filesystem::remove_all(tmp_tree, ec); + if (!std::filesystem::exists(seal_marker, ec)) { + return std::unexpected{make_error( + Void::System::SystemErrorCode::io_error, + std::format("snapshot: failed publishing tree '{}'", + snap.materialized_root.string()))}; + } } } + + std::size_t missing = 0; + for (const auto &[logical, entry] : snap.entries) { + std::error_code check_ec; + if (!std::filesystem::exists(snap.materialized_root / logical, check_ec)) { + ++missing; + } + } + if (missing == 0) { + break; + } + std::fprintf(stderr, + "snapshot: materialized tree '%s' misses %zu file(s); rebuilding\n", + snap.snapshot_id.c_str(), missing); + std::filesystem::remove_all(snap.materialized_root, ec); } for (auto &[logical, entry] : snap.entries) { entry.materialized_path = snap.materialized_root / logical;