Skip to content

Commit

Permalink
Merge pull request #56 from Mic92/target-directory-flag
Browse files Browse the repository at this point in the history
Add target-directory flag
  • Loading branch information
tarka authored Sep 25, 2024
2 parents 2c8c3f0 + 8c00cef commit fc43667
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions completions/xcp.fish
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ complete -c xcp -s L -l dereference -d 'Dereference symlinks in source'

# long
complete -c xcp -l fsync -d 'Sync each file to disk after it is written'
complete -c xcp -l target-directory -d 'Copy into a subdirectory of the target'
complete -c xcp -l gitignore -d 'Use .gitignore if present'
complete -c xcp -l no-perms -d 'Do not copy file permissions'
complete -c xcp -l no-timestamps -d 'Do not copy file timestamps'
Expand Down
1 change: 1 addition & 0 deletions completions/xcp.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _xcp() {
'(- *)'{-h,--help}'[Print help]'
'*'{-v,--verbose}'[Increase verbosity (can be repeated)]'
{-T,--no-target-directory}'[Overwrite target directory, do not create a subdirectory]'
{--target-directory}'[Copy into a subdirectory of the target]: :_files -/'
{-g,--glob}'[Expand (glob) filename patterns]'
{-n,--no-clobber}'[Do not overwrite an existing file]'
{-r,--recursive}'[Copy directories recursively]'
Expand Down
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ fn main() -> Result<()> {
init_logging(&opts)?;
opts_check(&opts);

let (dest, source_patterns) = opts
.paths
.split_last()
.ok_or(XcpError::InvalidArguments("Insufficient arguments".to_string()))
.map(|(d, s)| (PathBuf::from(d), s))?;
let (dest, source_patterns) = match opts.target_directory {
Some(ref d) => { (d, opts.paths.as_slice()) }
None => {
opts.paths.split_last().ok_or(XcpError::InvalidArguments("Insufficient arguments".to_string()))?
}
};
let dest = PathBuf::from(dest);

let sources = expand_sources(source_patterns, &opts)?;
if sources.is_empty() {
Expand Down
4 changes: 4 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub struct Opts {
#[arg(short = 'T', long)]
pub no_target_directory: bool,

/// Copy into a subdirectory of the target
#[arg(long)]
pub target_directory: Option<String>,

/// Sync each file to disk after writing.
#[arg(long)]
pub fsync: bool,
Expand Down
27 changes: 27 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,33 @@ fn copy_empty_dir(drv: &str) {
assert!(dest_base.join("mydir").is_dir());
}

#[cfg_attr(feature = "parblock", test_case("parblock"; "Test with parallel block driver"))]
#[test_case("parfile"; "Test with parallel file driver")]
fn copy_target_directory(drv: &str) {
let dir = tempdir_rel().unwrap();

let source_path = dir.path().join("mydir");
create_dir_all(&source_path).unwrap();

let dest_base = dir.path().join("dest");
create_dir_all(&dest_base).unwrap();

let out = run(&[
"--driver",
drv,
"-r",
"--target-directory",
dest_base.to_str().unwrap(),
source_path.to_str().unwrap(),
])
.unwrap();

assert!(out.status.success());

assert!(dest_base.join("mydir").exists());
assert!(dest_base.join("mydir").is_dir());
}

#[cfg_attr(feature = "parblock", test_case("parblock"; "Test with parallel block driver"))]
#[test_case("parfile"; "Test with parallel file driver")]
fn copy_all_dirs(drv: &str) {
Expand Down

0 comments on commit fc43667

Please sign in to comment.