pub mod server;
pub mod sync;
pub mod watch;
use crate::cli::Cli;
use crate::protocol::TargetOs;
use crate::sync::ExecutorOptions;
use crate::sync::SyncStats;
use anyhow::Result;
use std::path::Path;
pub(crate) fn print_skipped(stats: &SyncStats) -> bool {
if stats.skipped.is_empty() {
return false;
}
println!("Skipped {} file(s):", stats.skipped.len());
for file in &stats.skipped {
println!(" {}: {}", file.path, file.reason);
}
true
}
const EXIT_PARTIAL: i32 = 23;
pub(crate) fn exit_if_partial(any_skipped: bool) {
if any_skipped {
std::process::exit(EXIT_PARTIAL);
}
}
fn warn_on_link_flag_conflicts(cli: &Cli) {
let literal_family = cli.literal_links || cli.archive;
let granular = cli.literal_internal_links || cli.literal_external_file_links
|| cli.literal_external_dir_links;
let any_link_flag = cli.follow_links || literal_family || granular;
if cli.skip_links && any_link_flag {
tracing::warn!(
"--skip-links overrides the other link flags: no symlink or shortcut is synced"
);
}
if cli.follow_links && (literal_family || granular) {
tracing::warn!(
"--follow-links overrides the literal link flags: every symlink is dereferenced"
);
}
}
fn warn_on_decision_flag_conflicts(cli: &Cli) {
if cli.max_delete.is_some() && !cli.delete {
tracing::warn!("--max-delete has no effect without --delete");
}
if cli.existing && cli.ignore_existing {
tracing::warn!(
"--existing and --ignore-existing contradict each other: \
nothing would be updated or created"
);
}
if cli.ignore_times && cli.checksum {
tracing::warn!(
"--ignore-times overrides --checksum: every file is transferred regardless of matches"
);
}
}
pub(crate) fn options_from_cli(cli: &Cli) -> anyhow::Result<ExecutorOptions> {
warn_on_link_flag_conflicts(cli);
warn_on_decision_flag_conflicts(cli);
let mut exclude = cli.exclude.clone();
let mut include = cli.include.clone();
if let Some(f) = &cli.exclude_from {
exclude.extend(read_pattern_file(f, "--exclude-from")?);
}
if let Some(f) = &cli.include_from {
include.extend(read_pattern_file(f, "--include-from")?);
}
Ok(ExecutorOptions {
checksum: cli.checksum,
delete: cli.delete,
update_only: cli.update,
ignore_existing: cli.ignore_existing,
existing: cli.existing,
ignore_times: cli.ignore_times,
max_delete: cli.max_delete,
delete_scope: Vec::new(),
backup: cli.backup,
jobs: cli.jobs,
storage: cli.storage,
compress: cli.compress,
bwlimit: cli.bwlimit.filter(|&b| b != 0),
exclude,
include,
itemize: cli.itemize_changes,
fsync: cli.fsync,
remote_path: String::new(),
remote_paths: Vec::new(),
partial: true,
remove_source_files: cli.remove_source_files,
verify: cli.verify,
rollsum: cli.rollsum,
quiet: cli.quiet,
archive: cli.archive,
literal_links: cli.literal_links || cli.archive,
literal_internal_links: cli.literal_internal_links,
literal_external_file_links: cli.literal_external_file_links,
literal_external_dir_links: cli.literal_external_dir_links,
recursive: !cli.no_recursive,
preserve_links: !cli.skip_links,
follow_links: cli.follow_links,
sparse: cli.sparse,
xattrs: cli.xattrs,
atimes: cli.atimes,
preserve_perms: !cli.no_perms,
preserve_times: !cli.no_times,
target_os: TargetOs::Unix,
progress: None,
include_root_component: false,
})
}
fn read_pattern_file(path: &Path, flag: &str) -> anyhow::Result<Vec<String>> {
let content = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read {flag} list {}: {e}", path.display()))?;
let patterns = content
.lines()
.map(str::trim_start)
.filter(|line| !line.is_empty() && !line.starts_with('#') && !line.starts_with(';'))
.map(str::to_string)
.collect();
Ok(patterns)
}
pub async fn dispatch(mut cli: Cli) -> Result<()> {
if cli.server {
let options = options_from_cli(&cli)?;
server::execute(&options).await
} else {
Box::pin(sync::execute(&mut cli)).await
}
}
#[cfg(test)]
mod tests {
use super::read_pattern_file;
use std::path::PathBuf;
#[test]
fn pattern_file_parses_rsync_style() {
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("exclude.txt");
std::fs::write(
&f,
"# build artifacts\n*.tmp\n node_modules\n\n; keep\n/target\nsrc/generated\n",
)
.unwrap();
let patterns = read_pattern_file(&f, "--exclude-from").unwrap();
assert_eq!(
patterns,
vec!["*.tmp", "node_modules", "/target", "src/generated"]
);
}
#[test]
fn pattern_file_missing_is_an_error() {
assert!(
read_pattern_file(&PathBuf::from("/nonexistent/exclude.txt"), "--exclude-from").is_err()
);
}
}