1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::cell::Cell;
use std::cell::RefCell;
use std::fs;
use std::io::{Cursor, ErrorKind, Read};
use std::path::Path;
use flate2::read::GzDecoder;
use tar_::Archive;
use crate::err::{Error, Result};
use crate::file::File;
use crate::Store;
#[derive(Clone)]
pub struct Tar {
gzip: Cell<bool>,
bytes: Box<[u8]>,
}
impl Tar {
fn open_read<R: Read>(&self, path: &Path, reader: R) -> Result<File> {
let mut archive = Archive::new(reader);
for entry in archive.entries()? {
let mut entry = entry?;
if path == entry.path()? {
let mut data = Vec::new();
entry.read_to_end(&mut data)?;
return Ok(File::from_ram(data));
}
}
Err(Error::FileNotFound)
}
}
impl Store for Tar {
fn open(&self, path: &Path) -> Result<File> {
if self.gzip.get() {
self.open_read(path, GzDecoder::new(Cursor::new(&self.bytes)))
} else {
match self.open_read(path, Cursor::new(&self.bytes)) {
Err(Error::FileNotFound) => Err(Error::FileNotFound),
Err(_) => {
self.gzip.set(true);
self.open(path)
}
Ok(entry) => Ok(entry),
}
}
}
}
impl Tar {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
Self::from_reader(fs::File::open(path)?)
}
pub fn new<B: Into<Box<[u8]>>>(bytes: B) -> Self {
Self {
bytes: bytes.into(),
gzip: Cell::new(false),
}
}
pub fn from_reader<R: Read>(mut read: R) -> Result<Self> {
let mut inner = Vec::new();
read.read_to_end(&mut inner)?;
Ok(Self::new(inner))
}
}