Skip to content

Commit

Permalink
Refactor: Extract ZipFileData::unix_mode
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu authored and Plecra committed Feb 1, 2023
1 parent a614d1f commit 1675320
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 1 addition & 26 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ use bzip2::read::BzDecoder;
#[cfg(feature = "zstd")]
use zstd::stream::read::Decoder as ZstdDecoder;

mod ffi {
pub const S_IFDIR: u32 = 0o0040000;
pub const S_IFREG: u32 = 0o0100000;
}

// Put the struct declaration in a private module to convince rustdoc to display ZipArchive nicely
pub(crate) mod zip_archive {
/// Extract immutable data from `ZipArchive` to make it cheap to clone
Expand Down Expand Up @@ -949,27 +944,7 @@ impl<'a> ZipFile<'a> {

/// Get unix mode for the file
pub fn unix_mode(&self) -> Option<u32> {
if self.data.external_attributes == 0 {
return None;
}

match self.data.system {
System::Unix => Some(self.data.external_attributes >> 16),
System::Dos => {
// Interpret MS-DOS directory bit
let mut mode = if 0x10 == (self.data.external_attributes & 0x10) {
ffi::S_IFDIR | 0o0775
} else {
ffi::S_IFREG | 0o0664
};
if 0x01 == (self.data.external_attributes & 0x01) {
// Read-only bit; strip write permissions
mode &= 0o0555;
}
Some(mode)
}
_ => None,
}
self.data.unix_mode()
}

/// Get the CRC32 hash of the original file
Expand Down
31 changes: 31 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Types that specify what is contained in a ZIP.
use std::path;

#[cfg(not(any(
all(target_arch = "arm", target_pointer_width = "32"),
target_arch = "mips",
Expand All @@ -11,6 +12,11 @@ use std::time::SystemTime;
#[cfg(doc)]
use {crate::read::ZipFile, crate::write::FileOptions};

mod ffi {
pub const S_IFDIR: u32 = 0o0040000;
pub const S_IFREG: u32 = 0o0100000;
}

#[cfg(any(
all(target_arch = "arm", target_pointer_width = "32"),
target_arch = "mips",
Expand Down Expand Up @@ -391,6 +397,31 @@ impl ZipFileData {
Some(path)
}

/// Get unix mode for the file
pub fn unix_mode(&self) -> Option<u32> {
if self.external_attributes == 0 {
return None;
}

match self.system {
System::Unix => Some(self.external_attributes >> 16),
System::Dos => {
// Interpret MS-DOS directory bit
let mut mode = if 0x10 == (self.external_attributes & 0x10) {
ffi::S_IFDIR | 0o0775
} else {
ffi::S_IFREG | 0o0664
};
if 0x01 == (self.external_attributes & 0x01) {
// Read-only bit; strip write permissions
mode &= 0o0555;
}
Some(mode)
}
_ => None,
}
}

pub fn zip64_extension(&self) -> bool {
self.uncompressed_size > 0xFFFFFFFF
|| self.compressed_size > 0xFFFFFFFF
Expand Down

0 comments on commit 1675320

Please sign in to comment.