diff options
Diffstat (limited to 'pkgs/populate/src/rsync.rs')
| -rw-r--r-- | pkgs/populate/src/rsync.rs | 117 |
1 files changed, 86 insertions, 31 deletions
diff --git a/pkgs/populate/src/rsync.rs b/pkgs/populate/src/rsync.rs index 3a28c23..68891c3 100644 --- a/pkgs/populate/src/rsync.rs +++ b/pkgs/populate/src/rsync.rs @@ -102,28 +102,82 @@ pub struct Prepared { /// --list-only, which the check uses itself, if it runs rsync. Not when /// rsync needs --insecure-links, nor for what isn't a directory. pub fn prepare(config: &Config, source_path: &str, listing: bool) -> Result<Prepared> { + finish(config, gather(config, source_path, listing)?) +} + +/// What prepare finds out by running commands, before it decides, and +/// prints, anything, see gather. +pub struct Gathered { + source: String, + directory: bool, + listing_wanted: bool, + /// The version of rsync, and what its --list-only with debugging + /// printed, if it checked the .rsync-filter files that apply. + checked: Option<(String, String)>, + /// The listing, if listing_wanted and populate got it without that. + listing: Option<Listing>, +} + +/// The part of prepare that runs commands, which prints nothing, so it +/// can run for many sources at once, ahead of their turns, see finish. +/// Also with older versions of rsync, a source that's missing, or that +/// contains a directory find can't read, fails here, before rsync runs. +pub fn gather(config: &Config, source_path: &str, listing_wanted: bool) -> Result<Gathered> { let directory = Path::new(source_path).is_dir(); let mut source = source_path.to_string(); if directory { source.push('/'); } - // Also with older versions of rsync, so a source that's missing, or - // that contains a directory find can't read, fails before rsync runs. let filtered = config.filter_files && filtered(&source)?; - let (insecure_links, log) = if filtered { - insecure_links(config, &source)? + let checked = if filtered { + let version = rsync_version()?; + if version_at_least(&version, "3.5.0") { + let log = list_only_debug(config, &source)?; + Some((version, log)) + } else { + None + } } else { - (false, None) + None }; - let listing = if !listing || !directory || insecure_links { + let listing = if !listing_wanted || !directory || checked.is_some() { None - } else if let Some(log) = log { - listed(&log) } else if !filtered && config.exclude.is_empty() && config.filters.is_empty() { found_locally(&source) } else { list_only(config, &source) }; + Ok(Gathered { + source, + directory, + listing_wanted, + checked, + listing, + }) +} + +/// The rest of prepare, at the source's turn: checks the .rsync-filter +/// files with what gather got, which may print warnings, or fail, and +/// takes the listing. +pub fn finish(config: &Config, gathered: Gathered) -> Result<Prepared> { + let Gathered { + source, + directory, + listing_wanted, + checked, + listing, + } = gathered; + let (insecure_links, log) = match checked { + Some((version, log)) => (insecure_links(config, &source, &version, &log)?, Some(log)), + None => (false, None), + }; + let listing = if !listing_wanted || !directory || insecure_links { + None + } else if let Some(log) = log { + listed(&log) + } else { + listing + }; Ok(Prepared { source, insecure_links, @@ -131,6 +185,23 @@ pub fn prepare(config: &Config, source_path: &str, listing: bool) -> Result<Prep }) } +/// What rsync --list-only prints for source with the config's filters, in +/// UTC, and paths as they are, with the debugging insecure_links reads, +/// and what rsync prints on errors. +fn list_only_debug(config: &Config, source: &str) -> Result<String> { + run::output( + Command::new(run::SH) + .env("TZ", "UTC0") + .arg("-c") + .arg(r#"rsync "$@" 2>&1 || :"#) + .arg("sh") + .args(["-8", "--list-only", "--debug=FILTER2"]) + .args(config.filter_args()) + .arg("-rlptD") + .arg(source), + ) +} + /// What rsync --list-only prints for source, with the config's filters, /// in UTC, and paths as they are, or None if it fails. fn list_only(config: &Config, source: &str) -> Option<Listing> { @@ -245,26 +316,10 @@ fn changed_path(line: &str) -> Option<String> { /// config's insecure_links decides: populate fails with an error if it's /// None, syncs without them if it's false, and makes rsync open them like /// the files it transfers with --insecure-links if it's true, both with a -/// warning. Returns whether rsync needs --insecure-links, and, if it -/// asked rsync, what rsync --list-only printed: the listing of what it -/// would transfer, see listed, in UTC, and its debugging lines. -fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String>)> { - let version = rsync_version()?; - if !version_at_least(&version, "3.5.0") { - return Ok((false, None)); - } - // Listing what the transfer would send, as far as filtering goes. - let log = run::output( - Command::new(run::SH) - .env("TZ", "UTC0") - .arg("-c") - .arg(r#"rsync "$@" 2>&1 || :"#) - .arg("sh") - .args(["-8", "--list-only", "--debug=FILTER2"]) - .args(config.filter_args()) - .arg("-rlptD") - .arg(source), - )?; +/// warning. log is what rsync --list-only with debugging printed for +/// source, see list_only_debug, and version rsync's. Returns whether rsync +/// needs --insecure-links. +fn insecure_links(config: &Config, source: &str, version: &str, log: &str) -> Result<bool> { let mut parsed = false; let mut ignored = None; for line in log.lines() { @@ -293,7 +348,7 @@ fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String> ); } let Some(ignored) = ignored else { - return Ok((false, Some(log))); + return Ok(false); }; match config.insecure_links { None => Err(Failure::Message(format!( @@ -303,13 +358,13 @@ fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String> eprintln!( "warning: rsync {version} can't read {ignored}; it reads it with --insecure-links anyway, because insecureLinks is true." ); - Ok((true, Some(log))) + Ok(true) } Some(false) => { eprintln!( "warning: rsync {version} can't read {ignored}, so it syncs {source} without it, because insecureLinks is false." ); - Ok((false, Some(log))) + Ok(false) } } } |
