Skip to content

Commit 17ad7c7

Browse files
committed
♻️ Simplify libpna code patterns
- Apply map+transpose to read_entry_slice for consistency - Extract key_size() helper in write.rs to reduce duplication - Extract new_link() helper for symlink/hardlink creation
1 parent 057208c commit 17ad7c7

3 files changed

Lines changed: 36 additions & 60 deletions

File tree

lib/src/archive/read/slice.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ impl<'d> Archive<&'d [u8]> {
9090
///
9191
/// Returns an error if an I/O error occurs while reading from the archive.
9292
fn read_entry_slice(&mut self) -> io::Result<Option<ReadEntry<Cow<'d, [u8]>>>> {
93-
let entry = self.next_raw_item_slice()?;
94-
match entry {
95-
Some(entry) => Ok(Some(entry.try_into()?)),
96-
None => Ok(None),
97-
}
93+
self.next_raw_item_slice()?
94+
.map(TryInto::try_into)
95+
.transpose()
9896
}
9997

10098
/// Returns an iterator over the entries in the archive.

lib/src/entry/builder.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ impl EntryBuilder {
214214
})
215215
}
216216

217+
/// Internal helper for creating link entries (symlink or hard link).
218+
fn new_link(header: EntryHeader, source: EntryReference) -> io::Result<Self> {
219+
let option = WriteOptions::store();
220+
let context = get_writer_context(option)?;
221+
let mut writer = get_writer(FlattenWriter::new(), &context)?;
222+
writer.write_all(source.as_bytes())?;
223+
let (iv, phsf) = match context.cipher {
224+
None => (None, None),
225+
Some(WriteCipher { context: c, .. }) => (Some(c.iv), Some(c.phsf)),
226+
};
227+
Ok(Self {
228+
data: Some(writer),
229+
iv,
230+
phsf,
231+
..Self::new(header)
232+
})
233+
}
234+
217235
/// Creates a new symbolic link with the given name and link.
218236
///
219237
/// # Arguments
@@ -242,20 +260,7 @@ impl EntryBuilder {
242260
/// ```
243261
#[inline]
244262
pub fn new_symlink(name: EntryName, source: EntryReference) -> io::Result<Self> {
245-
let option = WriteOptions::store();
246-
let context = get_writer_context(option)?;
247-
let mut writer = get_writer(FlattenWriter::new(), &context)?;
248-
writer.write_all(source.as_bytes())?;
249-
let (iv, phsf) = match context.cipher {
250-
None => (None, None),
251-
Some(WriteCipher { context: c, .. }) => (Some(c.iv), Some(c.phsf)),
252-
};
253-
Ok(Self {
254-
data: Some(writer),
255-
iv,
256-
phsf,
257-
..Self::new(EntryHeader::for_symlink(name))
258-
})
263+
Self::new_link(EntryHeader::for_symlink(name), source)
259264
}
260265

261266
/// Creates a new symbolic link with the given name and link.
@@ -306,20 +311,7 @@ impl EntryBuilder {
306311
/// ```
307312
#[inline]
308313
pub fn new_hard_link(name: EntryName, source: EntryReference) -> io::Result<Self> {
309-
let option = WriteOptions::store();
310-
let context = get_writer_context(option)?;
311-
let mut writer = get_writer(FlattenWriter::new(), &context)?;
312-
writer.write_all(source.as_bytes())?;
313-
let (iv, phsf) = match context.cipher {
314-
None => (None, None),
315-
Some(WriteCipher { context: c, .. }) => (Some(c.iv), Some(c.phsf)),
316-
};
317-
Ok(Self {
318-
data: Some(writer),
319-
iv,
320-
phsf,
321-
..Self::new(EntryHeader::for_hard_link(name))
322-
})
314+
Self::new_link(EntryHeader::for_hard_link(name), source)
323315
}
324316

325317
/// Sets the creation timestamp of the entry.

lib/src/entry/write.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,50 +64,36 @@ pub(crate) fn get_writer_context(option: impl WriteOption) -> io::Result<EntryWr
6464
})
6565
}
6666

67+
#[inline]
68+
fn key_size(cipher_algorithm: CipherAlgorithm) -> usize {
69+
match cipher_algorithm {
70+
CipherAlgorithm::Aes => Aes256::key_size(),
71+
CipherAlgorithm::Camellia => Camellia256::key_size(),
72+
}
73+
}
74+
6775
#[inline]
6876
fn hash<'s, 'p: 's>(
6977
cipher_algorithm: CipherAlgorithm,
7078
hash_algorithm: HashAlgorithm,
7179
password: &'p [u8],
7280
salt: &'s SaltString,
7381
) -> io::Result<(Output, String)> {
74-
let mut password_hash = match (hash_algorithm.0, cipher_algorithm) {
75-
(
76-
HashAlgorithmParams::Argon2Id {
77-
time_cost,
78-
memory_cost,
79-
parallelism_cost,
80-
},
81-
CipherAlgorithm::Aes,
82-
) => hash::argon2_with_salt(
83-
password,
84-
argon2::Algorithm::Argon2id,
82+
let mut password_hash = match hash_algorithm.0 {
83+
HashAlgorithmParams::Argon2Id {
8584
time_cost,
8685
memory_cost,
8786
parallelism_cost,
88-
Aes256::key_size(),
89-
salt,
90-
),
91-
(
92-
HashAlgorithmParams::Argon2Id {
93-
time_cost,
94-
memory_cost,
95-
parallelism_cost,
96-
},
97-
CipherAlgorithm::Camellia,
98-
) => hash::argon2_with_salt(
87+
} => hash::argon2_with_salt(
9988
password,
10089
argon2::Algorithm::Argon2id,
10190
time_cost,
10291
memory_cost,
10392
parallelism_cost,
104-
Camellia256::key_size(),
93+
key_size(cipher_algorithm),
10594
salt,
10695
),
107-
(
108-
HashAlgorithmParams::Pbkdf2Sha256 { rounds },
109-
CipherAlgorithm::Aes | CipherAlgorithm::Camellia,
110-
) => {
96+
HashAlgorithmParams::Pbkdf2Sha256 { rounds } => {
11197
let mut params = pbkdf2::Params::default();
11298
if let Some(rounds) = rounds {
11399
params.rounds = rounds;

0 commit comments

Comments
 (0)