diff --git a/src/tar/mod.rs b/src/tar/mod.rs index 004b94e..f94798d 100644 --- a/src/tar/mod.rs +++ b/src/tar/mod.rs @@ -4,7 +4,7 @@ use std::{ path::PathBuf, }; -use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; +use rayon::prelude::{IntoParallelRefIterator, ParallelIterator, IndexedParallelIterator}; mod error; mod header; @@ -143,7 +143,7 @@ impl Archive { pub fn remove(&mut self, filename: &str) -> Result { let mut name = [0u8; 100]; name[..filename.len()].copy_from_slice(filename.as_bytes()); - if let Some(i) = &self.nodes.iter().position(|x| x.header.fname == name) { + if let Some(i) = &self.nodes.par_iter().position_any(|x| x.header.fname == name) { self.nodes.remove(*i); return Ok(true); } @@ -166,6 +166,17 @@ impl Archive { x.header.file_path() == Ok(PathBuf::from(filename)) }).cloned() } + + pub fn pop(&mut self, filename: &str) -> Option { + if let Some(i) = self.nodes.par_iter().position_any(|x| { + x.header.file_path() == Ok(PathBuf::from(filename)) + }) { + let node = self.nodes.get(i).cloned(); + self.nodes.remove(i); + return node; + } + None + } } #[cfg(test)] diff --git a/test/2.tar b/test/2.tar index 98902ce..d37f5f8 100644 Binary files a/test/2.tar and b/test/2.tar differ