Skip to content

Commit c49a3bf

Browse files
committed
feat(cli): add --preserve-dir flag to sandbox upload for scp -r semantics
`openshell sandbox upload <local-dir> <remote-dir>` flattens the local directory's contents into the remote destination by default — diverging from the standard `scp -r` / `cp -r` semantics that operators expect. This is a quiet footgun for anyone with prior file-transfer experience. This change keeps the legacy default behavior (no breaking change) and adds an opt-in `--preserve-dir` flag that wraps directory contents under the source directory's basename in the destination, matching `scp -r foo remote:/dest/` semantics. Help text on the Upload command now calls out the divergence loudly so operators discover the flag without a production incident first. Closes NVIDIA#885
1 parent e39bb38 commit c49a3bf

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,11 @@ enum SandboxCommands {
12981298
},
12991299

13001300
/// Upload local files to a sandbox.
1301+
///
1302+
/// NOTE: by default, uploading a directory extracts its contents flat into
1303+
/// the destination (legacy behavior). To match `scp -r` / `cp -r`
1304+
/// semantics — wrapping the contents under the source directory's name —
1305+
/// pass `--preserve-dir`.
13011306
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
13021307
Upload {
13031308
/// Sandbox name.
@@ -1314,6 +1319,13 @@ enum SandboxCommands {
13141319
/// Disable `.gitignore` filtering (uploads everything).
13151320
#[arg(long)]
13161321
no_git_ignore: bool,
1322+
1323+
/// When uploading a directory, wrap its contents under the source
1324+
/// directory's basename in the destination (matches `scp -r` / `cp -r`
1325+
/// semantics). Without this flag, contents are extracted flat into
1326+
/// the destination.
1327+
#[arg(long)]
1328+
preserve_dir: bool,
13171329
},
13181330

13191331
/// Download files from a sandbox.
@@ -2320,6 +2332,7 @@ async fn main() -> Result<()> {
23202332
local_path,
23212333
dest,
23222334
no_git_ignore,
2335+
preserve_dir,
23232336
} => {
23242337
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
23252338
let mut tls = tls.with_gateway_name(&ctx.name);
@@ -2348,7 +2361,15 @@ async fn main() -> Result<()> {
23482361
return Ok(());
23492362
}
23502363
// Fallback: upload without git filtering
2351-
run::sandbox_sync_up(&ctx.endpoint, &name, local, sandbox_dest, &tls).await?;
2364+
run::sandbox_sync_up(
2365+
&ctx.endpoint,
2366+
&name,
2367+
local,
2368+
sandbox_dest,
2369+
preserve_dir,
2370+
&tls,
2371+
)
2372+
.await?;
23522373
eprintln!("{} Upload complete", "✓".green().bold());
23532374
}
23542375
SandboxCommands::Download {

‎crates/openshell-cli/src/run.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ pub async fn sandbox_create(
24132413
&sandbox_name,
24142414
local,
24152415
dest,
2416+
false,
24162417
&effective_tls,
24172418
)
24182419
.await?;
@@ -2710,7 +2711,7 @@ pub async fn sandbox_sync_command(
27102711
}
27112712
let dest_display = dest.unwrap_or("~");
27122713
eprintln!("Syncing {} -> sandbox:{}", local.display(), dest_display);
2713-
sandbox_sync_up(server, name, local, dest, tls).await?;
2714+
sandbox_sync_up(server, name, local, dest, false, tls).await?;
27142715
eprintln!("{} Sync complete", "✓".green().bold());
27152716
}
27162717
(None, Some(sandbox_path)) => {

‎crates/openshell-cli/src/ssh.rs‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,9 @@ async fn ssh_tar_upload(
516516
.append_path_with_name(&local_path, &tar_name)
517517
.into_diagnostic()?;
518518
} else if local_path.is_dir() {
519-
archive.append_dir_all(".", &local_path).into_diagnostic()?;
519+
archive
520+
.append_dir_all(&tar_name, &local_path)
521+
.into_diagnostic()?;
520522
} else {
521523
return Err(miette::miette!(
522524
"local path does not exist: {}",
@@ -613,11 +615,18 @@ pub async fn sandbox_sync_up_files(
613615
/// that does not end with `/`, the destination is treated as a file path:
614616
/// the parent directory is created and the file is written with the
615617
/// destination's basename. This matches `cp` / `scp` semantics.
618+
///
619+
/// When `preserve_dir_name` is `true` and `local_path` is a directory, its
620+
/// basename is wrapped as a subdirectory of `sandbox_path` (matches
621+
/// `scp -r` / `cp -r`). When `false` (default), the directory's contents are
622+
/// extracted flat into `sandbox_path` — the historical behavior, kept for
623+
/// backward compatibility.
616624
pub async fn sandbox_sync_up(
617625
server: &str,
618626
name: &str,
619627
local_path: &Path,
620628
sandbox_path: Option<&str>,
629+
preserve_dir_name: bool,
621630
tls: &TlsOptions,
622631
) -> Result<()> {
623632
// When an explicit destination is given and looks like a file path (does
@@ -653,8 +662,13 @@ pub async fn sandbox_sync_up(
653662
.file_name()
654663
.ok_or_else(|| miette::miette!("path has no file name"))?
655664
.to_os_string()
665+
} else if preserve_dir_name {
666+
// Wrap directory contents under their own basename so uploads land at
667+
// `<dest>/<dirname>/...` (matches `scp -r` / `cp -r`). Falls back to
668+
// "." for paths with no meaningful basename (`.`, `/`).
669+
directory_upload_prefix(local_path)
656670
} else {
657-
// For directories the tar_name is unused — append_dir_all uses "."
671+
// Default: flatten directory contents into <dest>/ (legacy behavior).
658672
".".into()
659673
};
660674

@@ -671,6 +685,20 @@ pub async fn sandbox_sync_up(
671685
.await
672686
}
673687

688+
/// Compute the tar entry prefix for a directory upload when
689+
/// `preserve_dir_name` is enabled.
690+
///
691+
/// Returns the directory's basename for any path with a meaningful basename;
692+
/// callers extracting at `<dest>` will see contents wrapped under
693+
/// `<dest>/<basename>/...`. Returns `"."` for paths without a basename
694+
/// (e.g. `.` or `/`), which produces flat extraction at `<dest>`.
695+
fn directory_upload_prefix(local_path: &Path) -> std::ffi::OsString {
696+
local_path
697+
.file_name()
698+
.map(|n| n.to_os_string())
699+
.unwrap_or_else(|| ".".into())
700+
}
701+
674702
/// Pull a path from a sandbox to a local destination using tar-over-SSH.
675703
pub async fn sandbox_sync_down(
676704
server: &str,
@@ -1265,6 +1293,34 @@ mod tests {
12651293
assert_eq!(split_sandbox_path("/a/b/c/d.txt"), ("/a/b/c", "d.txt"));
12661294
}
12671295

1296+
#[test]
1297+
fn directory_upload_prefix_uses_basename_for_named_directories() {
1298+
assert_eq!(
1299+
directory_upload_prefix(Path::new("/tmp/upload-test")),
1300+
std::ffi::OsString::from("upload-test")
1301+
);
1302+
assert_eq!(
1303+
directory_upload_prefix(Path::new("foo")),
1304+
std::ffi::OsString::from("foo")
1305+
);
1306+
assert_eq!(
1307+
directory_upload_prefix(Path::new("./parent/nested")),
1308+
std::ffi::OsString::from("nested")
1309+
);
1310+
}
1311+
1312+
#[test]
1313+
fn directory_upload_prefix_falls_back_to_dot_for_basename_less_paths() {
1314+
assert_eq!(
1315+
directory_upload_prefix(Path::new(".")),
1316+
std::ffi::OsString::from(".")
1317+
);
1318+
assert_eq!(
1319+
directory_upload_prefix(Path::new("/")),
1320+
std::ffi::OsString::from(".")
1321+
);
1322+
}
1323+
12681324
#[test]
12691325
fn split_sandbox_path_handles_root_and_bare_names() {
12701326
// File directly under root

0 commit comments

Comments
 (0)