Skip to content

Commit e24bd71

Browse files
mjamivMolt
authored andcommitted
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 75b880b commit e24bd71

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
@@ -527,7 +527,9 @@ async fn ssh_tar_upload(
527527
.append_path_with_name(&local_path, &tar_name)
528528
.into_diagnostic()?;
529529
} else if local_path.is_dir() {
530-
archive.append_dir_all(".", &local_path).into_diagnostic()?;
530+
archive
531+
.append_dir_all(&tar_name, &local_path)
532+
.into_diagnostic()?;
531533
} else {
532534
return Err(miette::miette!(
533535
"local path does not exist: {}",
@@ -665,8 +667,11 @@ pub async fn sandbox_sync_up(
665667
.ok_or_else(|| miette::miette!("path has no file name"))?
666668
.to_os_string()
667669
} else {
668-
// For directories the tar_name is unused — append_dir_all uses "."
669-
".".into()
670+
// For directories, wrap contents under the source basename so uploads
671+
// land at `<dest>/<dirname>/...` — matches `scp -r` and `cp -r`. Falls
672+
// back to "." for paths with no meaningful basename (`.`, `/`), which
673+
// preserves the legacy flatten behavior in those edge cases.
674+
directory_upload_prefix(local_path)
670675
};
671676

672677
ssh_tar_upload(
@@ -682,6 +687,19 @@ pub async fn sandbox_sync_up(
682687
.await
683688
}
684689

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

1301+
#[test]
1302+
fn directory_upload_prefix_uses_basename_for_named_directories() {
1303+
assert_eq!(
1304+
directory_upload_prefix(Path::new("/tmp/upload-test")),
1305+
std::ffi::OsString::from("upload-test")
1306+
);
1307+
assert_eq!(
1308+
directory_upload_prefix(Path::new("foo")),
1309+
std::ffi::OsString::from("foo")
1310+
);
1311+
assert_eq!(
1312+
directory_upload_prefix(Path::new("./parent/nested")),
1313+
std::ffi::OsString::from("nested")
1314+
);
1315+
}
1316+
1317+
#[test]
1318+
fn directory_upload_prefix_falls_back_to_dot_for_basename_less_paths() {
1319+
assert_eq!(
1320+
directory_upload_prefix(Path::new(".")),
1321+
std::ffi::OsString::from(".")
1322+
);
1323+
assert_eq!(
1324+
directory_upload_prefix(Path::new("/")),
1325+
std::ffi::OsString::from(".")
1326+
);
1327+
}
1328+
12831329
#[test]
12841330
fn split_sandbox_path_handles_root_and_bare_names() {
12851331
// File directly under root

0 commit comments

Comments
 (0)