Skip to content

Commit 56a1a83

Browse files
committed
fix: run cp with bash to expand glob patterns
1 parent 83c1ce3 commit 56a1a83

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/run/runner/helpers/apt.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,20 @@ fn restore_from_cache(system_info: &SystemInfo, cache_dir: &Path) -> Result<()>
155155
.to_str()
156156
.ok_or_else(|| anyhow!("Invalid cache directory path"))?;
157157

158-
run_with_sudo("cp", ["-r", &format!("{cache_dir_str}/*"), "/"])?;
158+
copy_files_recursively(cache_dir_str, "/")?;
159159

160160
debug!("Cache restored successfully");
161161
Ok(())
162162
}
163163

164+
fn copy_files_recursively(src: &str, dst: &str) -> Result<()> {
165+
// IMPORTANT: We have to use 'bash' here to ensure that glob patterns are expanded correctly
166+
let copy_cmd = format!("cp -r {src}/* {dst}");
167+
run_with_sudo("bash", ["-c", &copy_cmd])?;
168+
169+
Ok(())
170+
}
171+
164172
/// Save installed packages to the cache directory
165173
fn save_to_cache(system_info: &SystemInfo, cache_dir: &Path, packages: &[&str]) -> Result<()> {
166174
if !is_system_compatible(system_info) {
@@ -225,3 +233,29 @@ fn save_to_cache(system_info: &SystemInfo, cache_dir: &Path, packages: &[&str])
225233
debug!("Packages cached successfully");
226234
Ok(())
227235
}
236+
237+
#[cfg(test)]
238+
mod tests {
239+
use super::*;
240+
use std::fs;
241+
use tempfile::TempDir;
242+
243+
#[test]
244+
fn test_copy_files_recursively() {
245+
let src = TempDir::new().unwrap();
246+
fs::write(src.path().join("file1.txt"), "content1").unwrap();
247+
fs::write(src.path().join("file2.txt"), "content2").unwrap();
248+
249+
let dst = TempDir::new().unwrap();
250+
copy_files_recursively(src.path().to_str().unwrap(), dst.path().to_str().unwrap()).unwrap();
251+
252+
assert_eq!(
253+
fs::read_to_string(dst.path().join("file1.txt")).unwrap(),
254+
"content1"
255+
);
256+
assert_eq!(
257+
fs::read_to_string(dst.path().join("file2.txt")).unwrap(),
258+
"content2"
259+
);
260+
}
261+
}

0 commit comments

Comments
 (0)