diff options
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 75 |
1 files changed, 64 insertions, 11 deletions
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index 0659fe7..668053a 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -8,6 +8,7 @@ use crate::run::{self, Context, Failure, Result}; use crate::sh; use crate::spec::{Source, Spec}; use serde_json::{json, Map, Value}; +use std::collections::BTreeMap; use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -56,6 +57,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { if spec.backup { context.run(&sh::backup(), &[dir])?; } + let listings = target_listings(context, spec)?; for (name, source) in &spec.source { let path = format!("{dir}/{name}"); eprintln!("populating '{}'...", context.name(&path)); @@ -76,6 +78,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { source, path: &path, workdir, + listing: listings.get(name).and_then(Option::as_ref), }; context.note(json!({ "begin": name })); let outcome = kind.deliver(&delivery)?; @@ -188,6 +191,9 @@ struct Delivery<'a> { path: &'a str, /// Where to prepare files on the deploying machine. workdir: PathBuf, + /// The listing of the target path, for a file source whose transfer may + /// find nothing to do, see target_listings. + listing: Option<&'a rsync::Listing>, } impl Delivery<'_> { @@ -281,21 +287,68 @@ impl Kind for Derivation { /// A file or directory on the deploying machine. struct File; +/// The configuration of a file source's transfer of source_path. +fn file_config(source: &Source, source_path: &str) -> Config { + // Store paths have the same time, so only checksums tell different + // contents apart. + let use_checksum = source + .settings + .get("useChecksum") + .and_then(Value::as_bool) + .unwrap_or(run::is_store_path(source_path)); + Config { + use_checksum, + ..Config::from_settings(&source.settings) + } +} + +/// The listings of the target paths of the file sources whose transfer may +/// find nothing to do, by name, see File::deliver: of directories, that +/// rsync compares by size and time, not checksum, and that the flakes step +/// doesn't rewrite a lock file in. One command on the target lists them +/// all. +fn target_listings( + context: &Context, + spec: &Spec, +) -> Result<BTreeMap<String, Option<rsync::Listing>>> { + let dir = &spec.target.path; + let names: Vec<&String> = spec + .source + .iter() + .filter(|(_, source)| { + source.kind == "file" + && in_place_lock(source).is_none() + && string(&source.settings, "path").is_ok_and(|path| { + Path::new(path).is_dir() && !file_config(source, path).use_checksum + }) + }) + .map(|(name, _)| name) + .collect(); + if names.is_empty() { + return Ok(BTreeMap::new()); + } + let paths: Vec<String> = names.iter().map(|name| format!("{dir}/{name}")).collect(); + let args: Vec<&str> = paths.iter().map(String::as_str).collect(); + let mut found = rsync::found(&context.output(sh::LISTINGS, &args)?); + Ok(names + .into_iter() + .zip(&paths) + .map(|(name, path)| (name.clone(), found.remove(path).flatten())) + .collect()) +} + impl Kind for File { fn deliver(&self, d: &Delivery) -> Result<Outcome> { - let settings = &d.source.settings; let source_path = d.string("path")?; - // Store paths have the same time, so only checksums tell different - // contents apart. - let use_checksum = settings - .get("useChecksum") - .and_then(Value::as_bool) - .unwrap_or(run::is_store_path(source_path)); - let config = Config { - use_checksum, - ..Config::from_settings(settings) + let config = file_config(d.source, source_path); + // Nothing to do if what rsync would transfer and what's at the + // target path are the same as far as rsync's quick check goes. + let prepared = rsync::prepare(&config, source_path, d.listing.is_some())?; + let changed = if prepared.listing.is_some() && prepared.listing.as_ref() == d.listing { + Vec::new() + } else { + rsync::transfer_prepared(d.context, &config, &prepared, d.path)? }; - let changed = rsync::transfer(d.context, &config, source_path, d.path)?; // The lock file the flakes step rewrites, which delivering brings // back, changed if the flakes step says so. let lock = in_place_lock(d.source); |
