diff options
Diffstat (limited to 'src/pipeline/publish.rs')
| -rw-r--r-- | src/pipeline/publish.rs | 136 |
1 files changed, 84 insertions, 52 deletions
diff --git a/src/pipeline/publish.rs b/src/pipeline/publish.rs index d70ba8d..94fc46f 100644 --- a/src/pipeline/publish.rs +++ b/src/pipeline/publish.rs @@ -91,70 +91,95 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { // Artifacts are copied into the served tree under their real filenames. let mut staged: Vec<(PathBuf, PathBuf)> = Vec::new(); + // Files this publish created that were not there before. If the publish does + // not land, they are orphans: no index names them and no consumer will ever + // ask for them (§6 m-16). A file that already existed belongs to the release + // that is still live, so it is never on this list. + let mut fresh: Vec<PathBuf> = Vec::new(); for p in &collect.packages { let from = PathBuf::from(&p.path); let filename = format!("{}-{}-{}.pkg.tar.zst", p.name, p.version, p.arch); let dest = arch_dir.join(&filename); + let sig_dest = arch_dir.join(format!("{filename}.sig")); + let existed = dest.exists(); std::fs::copy(&from, &dest)?; if let Some(sig) = &p.signature_path { let s = PathBuf::from(sig); if s.exists() { - std::fs::copy(&s, arch_dir.join(format!("{filename}.sig")))?; + std::fs::copy(&s, &sig_dest)?; + } + } + if !existed { + fresh.push(dest.clone()); + if sig_dest.exists() { + fresh.push(sig_dest.clone()); } } staged.push((dest, from)); } - // 1. render the new database to .tmp and fsync it. The old one is untouched. - let db_name = ctx.cfg.repo_db_name(); - let final_db = arch_dir.join(format!("{db_name}.tar.gz")); - let tmp_db = arch_dir.join(format!("{db_name}.tar.gz.tmp")); - let mut pkgs = index_artifacts(&ctx.db, &staged.iter().map(|(d, _)| d.clone()).collect::<Vec<_>>())?; - // Deduplicate: the same package name must appear once, newest wins. - pkgs = dedupe_by_name(pkgs); - run_repo_add(&ctx.cfg, &tmp_db, &pkgs)?; - fsync_file(&tmp_db)?; + let outcome = (|| -> Result<i64> { + // 1. render the new database to .tmp and fsync it. The old one is untouched. + let db_name = ctx.cfg.repo_db_name(); + let final_db = arch_dir.join(format!("{db_name}.tar.gz")); + let tmp_db = arch_dir.join(format!("{db_name}.tar.gz.tmp")); + let mut pkgs = index_artifacts(&ctx.db, &staged.iter().map(|(d, _)| d.clone()).collect::<Vec<_>>())?; + // Deduplicate: the same package name must appear once, newest wins. + pkgs = dedupe_by_name(pkgs); + run_repo_add(&ctx.cfg, &tmp_db, &pkgs)?; + fsync_file(&tmp_db)?; - // Fault injection so I4 can be tested for real: die between the tmp write - // and the rename, and the old index must still be what consumers get. - // Compiled out of a production build (§6 M-9). - #[cfg(feature = "test-support")] - if ctx.cfg.hooks.publish_fault.as_deref() == Some("before-rename") { - return Err(Error::other("injected fault: before-rename")); - } + // Fault injection so I4 can be tested for real: die between the tmp write + // and the rename, and the old index must still be what consumers get. + // Compiled out of a production build (§6 M-9). + #[cfg(feature = "test-support")] + if ctx.cfg.hooks.publish_fault.as_deref() == Some("before-rename") { + return Err(Error::other("injected fault: before-rename")); + } - // 2. commit the DB: release + package rows + the accepted pointer, in one - // transaction, with the pointer moved by compare-and-swap. - let release_id = commit_release(ctx, &collect)?; + // 2. commit the DB: release + package rows + the accepted pointer, in one + // transaction, with the pointer moved by compare-and-swap. + let release_id = commit_release(ctx, &collect)?; - // 3. atomic swap: rename over the real database, then sign and swap the sig. - std::fs::rename(&tmp_db, &final_db)?; - fsync_dir(&arch_dir)?; - sign(&ctx.cfg, &final_db)?; - let sig = arch_dir.join(format!("{db_name}.tar.gz.sig")); - let tmp_sig = arch_dir.join(format!("{db_name}.tar.gz.sig.tmp")); - if tmp_sig.exists() { - std::fs::rename(&tmp_sig, &sig)?; - } + // 3. atomic swap: rename over the real database, then sign and swap the sig. + std::fs::rename(&tmp_db, &final_db)?; + fsync_dir(&arch_dir)?; + sign(&ctx.cfg, &final_db)?; + let sig = arch_dir.join(format!("{db_name}.tar.gz.sig")); + let tmp_sig = arch_dir.join(format!("{db_name}.tar.gz.sig.tmp")); + if tmp_sig.exists() { + std::fs::rename(&tmp_sig, &sig)?; + } - // 4. a publish can unblock builds waiting on a dependency we just added. - let unblocked = builds::requeue_blocked(&ctx.db)?; - db::audit_system( - &ctx.db.writer, - "publish", - "repo", - &repo_row, - serde_json::json!({ - "release": release_id, - "packages": collect.packages.iter().map(|p| format!("{}-{}", p.name, p.version)).collect::<Vec<_>>(), - "unblocked_builds": unblocked, - }), - )?; - #[cfg(feature = "test-support")] - if ctx.cfg.hooks.publish_fault.as_deref() == Some("after-rename") { - return Err(Error::other("injected fault: after-rename")); + // 4. a publish can unblock builds waiting on a dependency we just added. + let unblocked = builds::requeue_blocked(&ctx.db)?; + db::audit_system( + &ctx.db.writer, + "publish", + "repo", + &repo_row, + serde_json::json!({ + "release": release_id, + "packages": collect.packages.iter().map(|p| format!("{}-{}", p.name, p.version)).collect::<Vec<_>>(), + "unblocked_builds": unblocked, + }), + )?; + #[cfg(feature = "test-support")] + if ctx.cfg.hooks.publish_fault.as_deref() == Some("after-rename") { + return Err(Error::other("injected fault: after-rename")); + } + Ok(release_id) + })(); + + if outcome.is_err() { + // The publish did not land, so nothing in the served tree may look like it + // did: the bytes this call copied in are removed again (m-16). Files that + // were already there belong to the release still being served and stay. + for p in &fresh { + let _ = std::fs::remove_file(p); + } } - Ok(release_id) + outcome } fn repo_for(db: &Db) -> Result<String> { @@ -301,17 +326,24 @@ fn sign(cfg: &Config, path: &Path) -> Result<()> { Ok(()) } +/// fsync, and *say so* when it fails (m-7). The whole point of writing to +/// `.tmp`, fsyncing, and renaming is the promise that a consumer never sees a +/// half-written database; swallowing the error made that a comment rather than +/// a guarantee — on a filesystem that refuses `fsync` (full, read-only, a +/// network mount that dropped), the rename still "succeeded". fn fsync_file(p: &Path) -> Result<()> { - if let Ok(f) = std::fs::File::open(p) { - let _ = f.sync_all(); - } + let f = std::fs::File::open(p) + .map_err(|e| Error::other(format!("open {} for fsync: {e}", p.display())))?; + f.sync_all() + .map_err(|e| Error::other(format!("fsync {}: {e}", p.display())))?; Ok(()) } fn fsync_dir(p: &Path) -> Result<()> { - if let Ok(d) = std::fs::File::open(p) { - let _ = d.sync_all(); - } + let d = std::fs::File::open(p) + .map_err(|e| Error::other(format!("open dir {} for fsync: {e}", p.display())))?; + d.sync_all() + .map_err(|e| Error::other(format!("fsync dir {}: {e}", p.display())))?; Ok(()) } |
