Skip to content

Commit

Permalink
Preserve Unix permissions on extracted files
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Feb 15, 2020
1 parent 983bbfc commit 4cca3ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ pub fn metadata<P: AsRef<Path>>(path: P) -> Result<fs::Metadata> {
fs::metadata(path).map_err(|source| Error::new(source, path))
}

pub use fs::Permissions;

/// A wrapper around std::fs::set_permissions
pub fn set_permissions<P: AsRef<Path>>(path: P, permissions: Permissions) -> Result<()> {
let path = path.as_ref();

fs::set_permissions(path, permissions).map_err(|source| Error::new(source, path))
}

/// A wrapper around std::fs::File that contains file path information in error
/// cases.
#[derive(Debug)]
Expand Down
13 changes: 12 additions & 1 deletion src/tool_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,20 @@ impl ToolCache {
let exe_name = tool_identifier_to_exe_name(source, &version);
tool_path.push(exe_name);

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

// On Unix systems, we need to preserve permissions from the archive
// like executability.
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(&tool_path, fs::Permissions::from_mode(mode)).unwrap();
}
}

log::trace!("Updating tool cache");
let mut cache = Self::load().unwrap();
let tool = cache.tools.entry(source.to_owned()).or_default();
Expand Down

0 comments on commit 4cca3ba

Please sign in to comment.