Skip to content

Commit 4cca3ba

Browse files
committed
Preserve Unix permissions on extracted files
1 parent 983bbfc commit 4cca3ba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/fs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> Result<fs::Metadata> {
5353
fs::metadata(path).map_err(|source| Error::new(source, path))
5454
}
5555

56+
pub use fs::Permissions;
57+
58+
/// A wrapper around std::fs::set_permissions
59+
pub fn set_permissions<P: AsRef<Path>>(path: P, permissions: Permissions) -> Result<()> {
60+
let path = path.as_ref();
61+
62+
fs::set_permissions(path, permissions).map_err(|source| Error::new(source, path))
63+
}
64+
5665
/// A wrapper around std::fs::File that contains file path information in error
5766
/// cases.
5867
#[derive(Debug)]

src/tool_cache.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,20 @@ impl ToolCache {
117117
let exe_name = tool_identifier_to_exe_name(source, &version);
118118
tool_path.push(exe_name);
119119

120-
let mut output = BufWriter::new(File::create(tool_path).unwrap());
120+
let mut output = BufWriter::new(File::create(&tool_path).unwrap());
121121
io::copy(&mut file, &mut output).unwrap();
122122

123+
// On Unix systems, we need to preserve permissions from the archive
124+
// like executability.
125+
#[cfg(unix)]
126+
{
127+
use std::os::unix::fs::PermissionsExt;
128+
129+
if let Some(mode) = file.unix_mode() {
130+
fs::set_permissions(&tool_path, fs::Permissions::from_mode(mode)).unwrap();
131+
}
132+
}
133+
123134
log::trace!("Updating tool cache");
124135
let mut cache = Self::load().unwrap();
125136
let tool = cache.tools.entry(source.to_owned()).or_default();

0 commit comments

Comments
 (0)