Compare commits
No commits in common. "4cede60a804834ed64f5c5799fc576eb5bf0dc06" and "32b4f80715faef74d98e20875869de75262db1cc" have entirely different histories.
4cede60a80
...
32b4f80715
@ -117,10 +117,9 @@ impl Default for Header {
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Get the filename of this archive member
|
||||
/// Gets the filename of this archive member
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use hpk_package::tar::Header;
|
||||
///
|
||||
|
@ -1,11 +1,8 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufReader, Write},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
|
||||
|
||||
mod error;
|
||||
mod header;
|
||||
mod node;
|
||||
@ -30,8 +27,7 @@ impl Archive {
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// Write out a vector of `TarNodes` to a file or something that implements
|
||||
/// ``std::io::Write`` and ``std::io::Copy``.
|
||||
/// Write out a vector of `TarNodes` to a file or something that implements ``std::io::Write`` and ``std::io::Copy``.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -59,8 +55,8 @@ impl Archive {
|
||||
Ok(written)
|
||||
}
|
||||
|
||||
/// Create a new `TarFile` struct and initialize it with a `filename` file.
|
||||
/// This will read in the file to the `TarFile` struct as a `TarNode`.
|
||||
/// Create a new `TarFile` struct and initialize it with a `filename` file. This will read in the file to
|
||||
/// the `TarFile` struct as a `TarNode`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -75,8 +71,7 @@ impl Archive {
|
||||
})
|
||||
}
|
||||
|
||||
/// Append another file to the `TarFile.file` vector. This adds a file to the
|
||||
/// internal representation of the tar file.
|
||||
/// Append another file to the `TarFile.file` vector. This adds a file to the internal representation of the tar file.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -92,41 +87,27 @@ impl Archive {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Open and load an external tar file into the internal `TarFile` struct. This
|
||||
/// parses and loads up all the files contained within the external tar file.
|
||||
/// Open and load an external tar file into the internal `TarFile` struct. This parses and loads up all the files
|
||||
/// contained within the external tar file.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use hpk_package::tar::Archive;
|
||||
///
|
||||
/// Archive::open("test/1.tar").unwrap();
|
||||
/// Archive::open("test/1.tar".to_string()).unwrap();
|
||||
/// ```
|
||||
pub fn open(filename: &str) -> Result<Self, Error> {
|
||||
let file = File::open(filename)?;
|
||||
pub fn open(filename: String) -> Result<Self, Error> {
|
||||
let file = File::open(&filename)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
Self::read(&mut reader)
|
||||
}
|
||||
|
||||
/// Read a tar archive from anything which implements `io::Read`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use hpk_package::tar::Archive;
|
||||
/// use std::{fs::File, io::BufReader};
|
||||
///
|
||||
/// let file = File::open("test/1.tar").unwrap();
|
||||
/// let mut reader = BufReader::new(file);
|
||||
/// Archive::read(&mut reader).unwrap();
|
||||
/// ```
|
||||
pub fn read<T: io::Read>(mut input: T) -> Result<Self, Error> {
|
||||
let mut out = Self {
|
||||
nodes: Vec::<Node>::new(),
|
||||
};
|
||||
while let Ok(t) = Node::read(&mut input) {
|
||||
|
||||
while let Ok(t) = Node::read(&mut reader) {
|
||||
out.nodes.push(t);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
@ -138,9 +119,9 @@ impl Archive {
|
||||
/// use hpk_package::tar::Archive;
|
||||
///
|
||||
/// let mut data = Archive::new("test/1.tar").unwrap();
|
||||
/// data.remove("test/1.tar").unwrap();
|
||||
/// data.remove("test/1.tar".to_string()).unwrap();
|
||||
/// ```
|
||||
pub fn remove(&mut self, filename: &str) -> Result<bool, Error> {
|
||||
pub fn remove(&mut self, filename: String) -> Result<bool, Error> {
|
||||
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) {
|
||||
@ -150,32 +131,4 @@ impl Archive {
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Get the first node from the archive that matches the given filename.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use hpk_package::tar::Archive;
|
||||
/// let archive = Archive::new("test/1.txt").unwrap();
|
||||
/// let node = archive.get("test/1.txt");
|
||||
/// assert!(node.is_some());
|
||||
/// ```
|
||||
pub fn get(&self, filename: &str) -> Option<Node> {
|
||||
self.nodes.par_iter().find_any(|x| {
|
||||
x.header.file_path() == Ok(PathBuf::from(filename))
|
||||
}).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn open_get() {
|
||||
let archive = Archive::open("test/1.tar").unwrap();
|
||||
let node = archive.get("1.txt");
|
||||
assert!(node.is_some());
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,7 @@ impl Node {
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// Write out a single file within the tar to a file or something with a
|
||||
/// ``std::io::Write`` trait.
|
||||
/// Write out a single file within the tar to a file or something with a ``std::io::Write`` trait.
|
||||
pub fn write<T: io::Write>(self, mut input: T) -> Result<usize, Error> {
|
||||
input.write_all(&self.header.to_bytes()?)?;
|
||||
let mut written = 512;
|
||||
|
BIN
test/2.tar
BIN
test/2.tar
Binary file not shown.
Loading…
Reference in New Issue
Block a user