Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect file permissions when compressing zip files #271

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Categories Used:
- CI: Rewrite [\#135](https://github.com/ouch-org/ouch/pull/135) ([figsoda](https://github.com/figsoda))
- Improving error messages and removing dead error treatment code [\#140](https://github.com/ouch-org/ouch/pull/140) ([marcospb19](https://github.com/marcospb19))
- Apply clippy lints and simplify smart_unpack [\#267](https://github.com/ouch-org/ouch/pull/267) ([figsoda](https://github.com/figsoda))
- Respect file permissions when compressing zip files [\#271](https://github.com/ouch-org/ouch/pull/271) ([figsoda](https://github.com/figsoda))

### Tweaks

Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ indicatif = "0.16.2"
[target.'cfg(unix)'.dependencies]
time = { version = "0.3.9", default-features = false }

[target.'cfg(not(unix))'.dependencies]
is_executable = "1.0.1"

[build-dependencies]
clap = { version = "3.1.18", features = ["derive", "env"] }
clap_complete = "3.1.4"
Expand Down
43 changes: 30 additions & 13 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains Zip-specific building and unpacking functions

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{
env,
io::{self, prelude::*},
Expand Down Expand Up @@ -138,6 +140,9 @@ where
let mut writer = zip::ZipWriter::new(writer);
let options = zip::write::FileOptions::default();

#[cfg(not(unix))]
let executable = options.unix_permissions(0o755);

// Vec of any filename that failed the UTF-8 check
let invalid_unicode_filenames = get_invalid_utf8_paths(input_filenames);

Expand Down Expand Up @@ -168,21 +173,33 @@ where
// and so on
info!(@display_handle, inaccessible, "Compressing '{}'.", to_utf(path));

if path.is_dir() {
let metadata = match path.metadata() {
Ok(metadata) => metadata,
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound && utils::is_symlink(path) {
// This path is for a broken symlink
// We just ignore it
continue;
}
return Err(e.into());
}
};

#[cfg(unix)]
let options = options.unix_permissions(metadata.permissions().mode());

if metadata.is_dir() {
writer.add_directory(path.to_str().unwrap().to_owned(), options)?;
} else {
writer.start_file(path.to_str().unwrap().to_owned(), options)?;
let file_bytes = match fs::read(entry.path()) {
Ok(b) => b,
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound && utils::is_symlink(path) {
// This path is for a broken symlink
// We just ignore it
continue;
}
return Err(e.into());
}
#[cfg(not(unix))]
let options = if is_executable::is_executable(path) {
executable
} else {
options
};

writer.start_file(path.to_str().unwrap().to_owned(), options)?;
let file_bytes = fs::read(entry.path())?;
writer.write_all(&file_bytes)?;
}
}
Expand Down Expand Up @@ -260,7 +277,7 @@ fn set_last_modified_time(file: &fs::File, zip_file: &ZipFile) -> crate::Result<

#[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> {
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
use std::fs::Permissions;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(file_path, Permissions::from_mode(mode))?;
Expand Down