-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for NTFS Extra Field
- Loading branch information
1 parent
7c20fa3
commit 516e726
Showing
8 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::io::Read; | ||
|
||
use crate::{ | ||
result::{ZipError, ZipResult}, | ||
unstable::LittleEndianReadExt, | ||
}; | ||
|
||
/// The NTFS extra field as described in [PKWARE's APPNOTE.TXT v6.3.9]. | ||
/// | ||
/// This field stores [Windows file times], which are 64-bit unsigned integer | ||
/// values that represents the number of 100-nanosecond intervals that have | ||
/// elapsed since "1601-01-01 00:00:00 UTC". | ||
/// | ||
/// [PKWARE's APPNOTE.TXT v6.3.9]: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT | ||
/// [Windows file times]: https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times | ||
#[derive(Clone, Debug)] | ||
pub struct Ntfs { | ||
mtime: u64, | ||
atime: u64, | ||
ctime: u64, | ||
} | ||
|
||
impl Ntfs { | ||
/// Creates a NTFS extra field struct by reading the required bytes from the | ||
/// reader. | ||
/// | ||
/// This method assumes that the length has already been read, therefore it | ||
/// must be passed as an argument. | ||
pub fn try_from_reader<R>(reader: &mut R, len: u16) -> ZipResult<Self> | ||
where | ||
R: Read, | ||
{ | ||
if len != 32 { | ||
return Err(ZipError::UnsupportedArchive( | ||
"NTFS extra field has an unsupported length", | ||
)); | ||
} | ||
|
||
// Read reserved for future use. | ||
let _ = reader.read_u32_le()?; | ||
|
||
let tag = reader.read_u16_le()?; | ||
if tag != 0x0001 { | ||
return Err(ZipError::UnsupportedArchive( | ||
"NTFS extra field has an unsupported attribute tag", | ||
)); | ||
} | ||
let size = reader.read_u16_le()?; | ||
if size != 24 { | ||
return Err(ZipError::UnsupportedArchive( | ||
"NTFS extra field has an unsupported attribute size", | ||
)); | ||
} | ||
|
||
let mtime = reader.read_u64_le()?; | ||
let atime = reader.read_u64_le()?; | ||
let ctime = reader.read_u64_le()?; | ||
Ok(Self { | ||
mtime, | ||
atime, | ||
ctime, | ||
}) | ||
} | ||
|
||
/// Returns the file last modification time as a file time. | ||
pub fn mtime(&self) -> u64 { | ||
self.mtime | ||
} | ||
|
||
/// Returns the file last modification time as a file time. | ||
#[cfg(feature = "nt-time")] | ||
pub fn modified_file_time(&self) -> nt_time::FileTime { | ||
nt_time::FileTime::new(self.mtime) | ||
} | ||
|
||
/// Returns the file last access time as a file time. | ||
pub fn atime(&self) -> u64 { | ||
self.atime | ||
} | ||
|
||
/// Returns the file last access time as a file time. | ||
#[cfg(feature = "nt-time")] | ||
pub fn accessed_file_time(&self) -> nt_time::FileTime { | ||
nt_time::FileTime::new(self.atime) | ||
} | ||
|
||
/// Returns the file creation time as a file time. | ||
pub fn ctime(&self) -> u64 { | ||
self.ctime | ||
} | ||
|
||
/// Returns the file creation time as a file time. | ||
#[cfg(feature = "nt-time")] | ||
pub fn created_file_time(&self) -> nt_time::FileTime { | ||
nt_time::FileTime::new(self.ctime) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::io; | ||
|
||
use zip::ZipArchive; | ||
|
||
#[test] | ||
fn test_ntfs() { | ||
let mut v = Vec::new(); | ||
v.extend_from_slice(include_bytes!("../tests/data/ntfs.zip")); | ||
let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file"); | ||
|
||
for field in archive.by_name("test.txt").unwrap().extra_data_fields() { | ||
if let zip::ExtraField::Ntfs(ts) = field { | ||
assert_eq!(ts.mtime(), 133_813_273_144_169_390); | ||
#[cfg(feature = "nt-time")] | ||
assert_eq!( | ||
time::OffsetDateTime::try_from(ts.modified_file_time()).unwrap(), | ||
time::macros::datetime!(2025-01-14 11:21:54.416_939_000 UTC) | ||
); | ||
|
||
assert_eq!(ts.atime(), 0); | ||
#[cfg(feature = "nt-time")] | ||
assert_eq!(ts.accessed_file_time(), nt_time::FileTime::NT_TIME_EPOCH); | ||
|
||
assert_eq!(ts.ctime(), 0); | ||
#[cfg(feature = "nt-time")] | ||
assert_eq!(ts.created_file_time(), nt_time::FileTime::NT_TIME_EPOCH); | ||
} | ||
} | ||
} |