Skip to content

Commit 106d4e1

Browse files
committed
fix(cli): preserve directory basename when uploading to sandbox
Previously `openshell sandbox upload <local-dir> <remote-dir>` flattened the local directory's contents into the remote destination, dropping the source directory's basename. This diverged from the standard `scp -r` / `cp -r` semantics that operators expect from any *nix-style transfer tool, and could silently shadow files at the destination. Now an upload of `foo` to `/sandbox/dest/` lands at `/sandbox/dest/foo/`, matching `scp -r foo remote:/sandbox/dest/` and `cp -r foo /sandbox/dest/`. Paths without a meaningful basename (`.`, `/`) preserve the legacy flat extraction. Closes NVIDIA#885
1 parent e39bb38 commit 106d4e1

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

  • crates/openshell-cli/src

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

Lines changed: 49 additions & 3 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: {}",
@@ -654,8 +656,11 @@ pub async fn sandbox_sync_up(
654656
.ok_or_else(|| miette::miette!("path has no file name"))?
655657
.to_os_string()
656658
} else {
657-
// For directories the tar_name is unused — append_dir_all uses "."
658-
".".into()
659+
// For directories, wrap contents under the source basename so uploads
660+
// land at `<dest>/<dirname>/...` — matches `scp -r` and `cp -r`. Falls
661+
// back to "." for paths with no meaningful basename (`.`, `/`), which
662+
// preserves the legacy flatten behavior in those edge cases.
663+
directory_upload_prefix(local_path)
659664
};
660665

661666
ssh_tar_upload(
@@ -671,6 +676,19 @@ pub async fn sandbox_sync_up(
671676
.await
672677
}
673678

679+
/// Compute the tar entry prefix for a directory upload.
680+
///
681+
/// Returns the directory's basename for any path with a meaningful basename;
682+
/// callers extracting at `<dest>` will see contents wrapped under
683+
/// `<dest>/<basename>/...`. Returns `"."` for paths without a basename
684+
/// (e.g. `.` or `/`), which produces flat extraction at `<dest>`.
685+
fn directory_upload_prefix(local_path: &Path) -> std::ffi::OsString {
686+
local_path
687+
.file_name()
688+
.map(|n| n.to_os_string())
689+
.unwrap_or_else(|| ".".into())
690+
}
691+
674692
/// Pull a path from a sandbox to a local destination using tar-over-SSH.
675693
pub async fn sandbox_sync_down(
676694
server: &str,
@@ -1265,6 +1283,34 @@ mod tests {
12651283
assert_eq!(split_sandbox_path("/a/b/c/d.txt"), ("/a/b/c", "d.txt"));
12661284
}
12671285

1286+
#[test]
1287+
fn directory_upload_prefix_uses_basename_for_named_directories() {
1288+
assert_eq!(
1289+
directory_upload_prefix(Path::new("/tmp/upload-test")),
1290+
std::ffi::OsString::from("upload-test")
1291+
);
1292+
assert_eq!(
1293+
directory_upload_prefix(Path::new("foo")),
1294+
std::ffi::OsString::from("foo")
1295+
);
1296+
assert_eq!(
1297+
directory_upload_prefix(Path::new("./parent/nested")),
1298+
std::ffi::OsString::from("nested")
1299+
);
1300+
}
1301+
1302+
#[test]
1303+
fn directory_upload_prefix_falls_back_to_dot_for_basename_less_paths() {
1304+
assert_eq!(
1305+
directory_upload_prefix(Path::new(".")),
1306+
std::ffi::OsString::from(".")
1307+
);
1308+
assert_eq!(
1309+
directory_upload_prefix(Path::new("/")),
1310+
std::ffi::OsString::from(".")
1311+
);
1312+
}
1313+
12681314
#[test]
12691315
fn split_sandbox_path_handles_root_and_bare_names() {
12701316
// File directly under root

0 commit comments

Comments
 (0)