Debian Codegraph @

src/entry/mod.rs

raw symbols SCIP
1 // Copyright (c) 2022 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 pub mod builder;
5
6 use std::ops::Deref;
7
8 use futures_lite::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, SeekFrom};
9
10 use crate::entry::builder::ZipEntryBuilder;
11 use crate::error::{Result, ZipError};
12 use crate::spec::{
13 attribute::AttributeCompatibility,
14 consts::LFH_SIGNATURE,
15 header::{ExtraField, LocalFileHeader},
16 Compression,
17 };
18 use crate::{string::ZipString, ZipDateTime};
19
20 /// An immutable store of data about a ZIP entry.
21 ///
22 /// This type cannot be directly constructed so instead, the [`ZipEntryBuilder`] must be used. Internally this builder
23 /// stores a [`ZipEntry`] so conversions between these two types via the [`From`] implementations will be
24 /// non-allocating.
25 #[derive(Clone, Debug)]
26 pub struct ZipEntry {
27 pub(crate) filename: ZipString,
28 pub(crate) compression: Compression,
29 #[cfg(any(
30 feature = "deflate",
31 feature = "bzip2",
32 feature = "zstd",
33 feature = "lzma",
34 feature = "xz",
35 feature = "deflate64"
36 ))]
37 pub(crate) compression_level: async_compression::Level,
38 pub(crate) crc32: u32,
39 pub(crate) uncompressed_size: u64,
40 pub(crate) compressed_size: u64,
41 pub(crate) attribute_compatibility: AttributeCompatibility,
42 pub(crate) last_modification_date: ZipDateTime,
43 pub(crate) internal_file_attribute: u16,
44 pub(crate) external_file_attribute: u32,
45 pub(crate) extra_fields: Vec<ExtraField>,
46 pub(crate) comment: ZipString,
47 pub(crate) data_descriptor: bool,
48 }
49
50 impl From<ZipEntryBuilder> for ZipEntry {
51 fn from(builder: ZipEntryBuilder) -> Self {
52 builder.0
53 }
54 }
55
56 impl ZipEntry {
57 pub(crate) fn new(filename: ZipString, compression: Compression) -> Self {
58 ZipEntry {
59 filename,
60 compression,
61 #[cfg(any(
62 feature = "deflate",
63 feature = "bzip2",
64 feature = "zstd",
65 feature = "lzma",
66 feature = "xz",
67 feature = "deflate64"
68 ))]
69 compression_level: async_compression::Level::Default,
70 crc32: 0,
71 uncompressed_size: 0,
72 compressed_size: 0,
73 attribute_compatibility: AttributeCompatibility::Unix,
74 last_modification_date: ZipDateTime::default(),
75 internal_file_attribute: 0,
76 external_file_attribute: 0,
77 extra_fields: Vec::new(),
78 comment: String::new().into(),
79 data_descriptor: false,
80 }
81 }
82
83 /// Returns the entry's filename.
84 ///
85 /// ## Note
86 /// This will return the raw filename stored during ZIP creation. If calling this method on entries retrieved from
87 /// untrusted ZIP files, the filename should be sanitised before being used as a path to prevent [directory
88 /// traversal attacks](https://en.wikipedia.org/wiki/Directory_traversal_attack).
89 pub fn filename(&self) -> &ZipString {
90 &self.filename
91 }
92
93 /// Returns the entry's compression method.
94 pub fn compression(&self) -> Compression {
95 self.compression
96 }
97
98 /// Returns the entry's CRC32 value.
99 pub fn crc32(&self) -> u32 {
100 self.crc32
101 }
102
103 /// Returns the entry's uncompressed size.
104 pub fn uncompressed_size(&self) -> u64 {
105 self.uncompressed_size
106 }
107
108 /// Returns the entry's compressed size.
109 pub fn compressed_size(&self) -> u64 {
110 self.compressed_size
111 }
112
113 /// Returns the entry's attribute's host compatibility.
114 pub fn attribute_compatibility(&self) -> AttributeCompatibility {
115 self.attribute_compatibility
116 }
117
118 /// Returns the entry's last modification time & date.
119 pub fn last_modification_date(&self) -> &ZipDateTime {
120 &self.last_modification_date
121 }
122
123 /// Returns the entry's internal file attribute.
124 pub fn internal_file_attribute(&self) -> u16 {
125 self.internal_file_attribute
126 }
127
128 /// Returns the entry's external file attribute
129 pub fn external_file_attribute(&self) -> u32 {
130 self.external_file_attribute
131 }
132
133 /// Returns the entry's extra field data.
134 pub fn extra_fields(&self) -> &[ExtraField] {
135 &self.extra_fields
136 }
137
138 /// Returns the entry's file comment.
139 pub fn comment(&self) -> &ZipString {
140 &self.comment
141 }
142
143 /// Returns the entry's integer-based UNIX permissions.
144 ///
145 /// # Note
146 /// This will return None if the attribute host compatibility is not listed as Unix.
147 pub fn unix_permissions(&self) -> Option<u16> {
148 if !matches!(self.attribute_compatibility, AttributeCompatibility::Unix) {
149 return None;
150 }
151
152 Some(((self.external_file_attribute) >> 16) as u16)
153 }
154
155 /// Returns whether or not the entry represents a directory.
156 pub fn dir(&self) -> Result<bool> {
157 Ok(self.filename.as_str()?.ends_with('/'))
158 }
159 }
160
161 /// An immutable store of data about how a ZIP entry is stored within a specific archive.
162 ///
163 /// Besides storing archive independent information like the size and timestamp it can also be used to query
164 /// information about how the entry is stored in an archive.
165 #[derive(Clone)]
166 pub struct StoredZipEntry {
167 pub(crate) entry: ZipEntry,
168 // pub(crate) general_purpose_flag: GeneralPurposeFlag,
169 pub(crate) file_offset: u64,
170 pub(crate) header_size: u64,
171 }
172
173 impl StoredZipEntry {
174 /// Returns the offset in bytes to where the header of the entry starts.
175 pub fn header_offset(&self) -> u64 {
176 self.file_offset
177 }
178
179 /// Returns the combined size in bytes of the header, the filename, and any extra fields.
180 ///
181 /// Note: This uses the extra field length stored in the central directory, which may differ from that stored in
182 /// the local file header. See specification: <https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#732>
183 pub fn header_size(&self) -> u64 {
184 self.header_size
185 }
186
187 /// Seek to the offset in bytes where the data of the entry starts.
188 pub(crate) async fn seek_to_data_offset<R: AsyncRead + AsyncSeek + Unpin>(&self, mut reader: &mut R) -> Result<()> {
189 // Seek to the header
190 reader.seek(SeekFrom::Start(self.file_offset)).await?;
191
192 // Check the signature
193 let signature = {
194 let mut buffer = [0; 4];
195 reader.read_exact(&mut buffer).await?;
196 u32::from_le_bytes(buffer)
197 };
198
199 match signature {
200 LFH_SIGNATURE => (),
201 actual => return Err(ZipError::UnexpectedHeaderError(actual, LFH_SIGNATURE)),
202 };
203
204 // Skip the local file header and trailing data
205 let header = LocalFileHeader::from_reader(&mut reader).await?;
206 let trailing_size = (header.file_name_length as i64) + (header.extra_field_length as i64);
207 reader.seek(SeekFrom::Current(trailing_size)).await?;
208
209 Ok(())
210 }
211 }
212
213 impl Deref for StoredZipEntry {
214 type Target = ZipEntry;
215
216 fn deref(&self) -> &Self::Target {
217 &self.entry
218 }
219 }