From 19f12ff791ffbde6afaf7790028efa6254d2d195 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Fri, 26 Mar 2021 22:39:23 -0300 Subject: [PATCH] Ensure correct permissions for decompressed files from .zip on Unix --- src/cli.rs | 2 +- src/decompressors/zip.rs | 13 ++++++++++++- src/error.rs | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index b46ce71..fb49d51 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -127,7 +127,7 @@ impl TryFrom> for Command { } eprintln!("{} {}", "[ERROR]".red(), err); - return Err(crate::Error::IOError); + return Err(crate::Error::IoError); } } diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index 1882248..01f8a65 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -10,6 +10,15 @@ use zip::{self, read::ZipFile, ZipArchive}; use super::decompressor::{DecompressionResult, Decompressor}; use crate::{file::File, utils}; +#[cfg(unix)] +fn __unix_set_permissions(file_path: &PathBuf, file: &ZipFile) { + use std::os::unix::fs::PermissionsExt; + + if let Some(mode) = file.unix_mode() { + fs::set_permissions(&file_path, fs::Permissions::from_mode(mode)).unwrap(); + } +} + pub struct ZipDecompressor {} impl ZipDecompressor { @@ -65,7 +74,8 @@ impl ZipDecompressor { io::copy(&mut file, &mut outfile)?; } - // TODO: check if permissions are correct when on Unix + #[cfg(unix)] + __unix_set_permissions(&file_path, &file); let file_path = fs::canonicalize(file_path.clone())?; unpacked_files.push(file_path); @@ -89,6 +99,7 @@ impl ZipDecompressor { None => { let file = fs::File::open(&from.path)?; let mut archive = zip::ZipArchive::new(file)?; + Ok(Self::zip_decompress(&mut archive, into)?) } } diff --git a/src/error.rs b/src/error.rs index 19a7ea3..6680600 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,7 @@ pub enum Error { // TODO: get rid of this error variant InvalidUnicode, InvalidInput, - IOError, + IoError, FileNotFound(PathBuf), AlreadyExists, InvalidZipArchive(&'static str), @@ -50,7 +50,7 @@ impl From for Error { std::io::ErrorKind::AlreadyExists => Self::AlreadyExists, _other => { println!("{}: {}", "IO error".red(), err); - Self::IOError + Self::IoError } } } -- 2.11.4.GIT