use std::io::{self, Cursor, Read};
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{Context, Result, anyhow, bail};
use serde::{Deserialize, Serialize};
use super::resumable::{GzPoint, Gzip};
use super::storage::Storage;
use super::{Format, Item, Kind, Opened, Sink, SolidStream, SolidWalker, Unit, Worker, safe_path};
use crate::fsutil;
use crate::journal::Resume;
pub const NAME: &str = "tar.gz";
pub(super) const BUF: usize = 1024 * 1024;
pub(super) const MAX_HEADER: u64 = 1024 * 1024;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct StreamPoint {
#[serde(default, skip_serializing_if = "CodecKind::is_gzip")]
pub kind: CodecKind,
#[serde(default)]
pub member_start: bool,
#[serde(default)]
pub member_crc: u32,
#[serde(default)]
pub member_len: u64,
#[serde(default, with = "hex")]
pub window: Vec<u8>,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub block: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub level: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub combined_crc: Option<u32>,
pub tar_pos: u64,
pub at: TarAt,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CodecKind {
#[default]
Gzip,
Xz,
Bzip2,
Zstd,
}
impl CodecKind {
pub fn is_gzip(&self) -> bool {
*self == Self::Gzip
}
}
fn is_zero_u64(v: &u64) -> bool {
*v == 0
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TarAt {
Boundary,
File {
path: PathBuf,
size: u64,
mode: Option<u32>,
mtime: Option<i64>,
data_start: u64,
},
Skip { left: u64 },
End,
}
#[derive(Clone, Debug, Default)]
pub struct Profile {
pub files: u64,
pub links: u64,
pub lead: i128,
pub gap: u64,
}
pub struct Solid {
storage: Arc<Storage>,
from: Option<Resume>,
profile: Profile,
}
impl Solid {
pub fn walk(&self, from: Option<&Resume>) -> Result<Walker> {
Walker::new(&self.storage, from)
}
}
pub enum Event<'a> {
Entry {
item: Item,
link: Option<Vec<u8>>,
done: u64,
},
Data(&'a [u8]),
FileEnd,
Point,
End,
}
pub(super) enum State {
Header(Vec<u8>),
File {
item: Item,
data_start: u64,
left: u64,
pad: u64,
},
Skip(u64),
Trailer,
}
impl State {
pub(super) fn at_rest(&self) -> bool {
match self {
State::Header(held) => held.is_empty(),
State::File { .. } | State::Skip(_) | State::Trailer => true,
}
}
}
pub(super) trait Feed {
fn fill(&mut self, buf: &mut [u8]) -> Result<usize>;
fn restartable(&self) -> bool;
fn point(&self, tar_pos: u64, at: TarAt, written: u64) -> Result<Resume>;
fn point_byte(&self) -> Option<u64>;
}
impl Feed for Gzip {
fn fill(&mut self, buf: &mut [u8]) -> Result<usize> {
self.read(buf)
}
fn restartable(&self) -> bool {
self.restart_bit().is_some()
}
fn point(&self, tar_pos: u64, at: TarAt, written: u64) -> Result<Resume> {
let point = self
.restart_point()?
.ok_or_else(|| anyhow!("the compressed stream cannot be resumed here"))?;
Ok(Resume {
unit: 0,
written,
bit: point.bit,
crc: 0,
stream: Some(Box::new(StreamPoint {
kind: CodecKind::Gzip,
member_start: point.member_start,
member_crc: point.crc,
member_len: point.len,
window: point.window,
block: 0,
level: None,
combined_crc: None,
tar_pos,
at,
})),
})
}
fn point_byte(&self) -> Option<u64> {
self.restart_bit().map(|(bit, _)| bit / 8)
}
}
pub(super) struct TarStream<F: Feed> {
feed: F,
buf: Vec<u8>,
pos: usize,
len: usize,
tar_pos: u64,
state: State,
restartable: bool,
resumed: Option<(Item, u64)>,
}
impl<F: Feed> TarStream<F> {
pub(super) fn new(feed: F, from: Option<&Resume>) -> Result<Self> {
let (tar_pos, state, resumed) = match from {
None => (0, State::Header(Vec::new()), None),
Some(r) => {
let s = r.stream.as_ref().ok_or_else(|| {
anyhow!("the journal's resume point does not belong to a compressed tar")
})?;
let (state, resumed) = resumed_state(s, r.written)?;
(s.tar_pos, state, resumed)
}
};
Ok(Self {
feed,
buf: vec![0; BUF],
pos: 0,
len: 0,
tar_pos,
state,
restartable: false,
resumed,
})
}
fn held_header(&mut self) -> Result<Option<Event<'static>>> {
let State::Header(held) = &self.state else {
return Ok(None);
};
match group(held)? {
Group::Need(_) => Ok(None),
Group::End => {
self.state = State::Trailer;
Ok(None)
}
Group::Complete => {
let held = match std::mem::replace(&mut self.state, State::Header(Vec::new())) {
State::Header(held) => held,
_ => unreachable!("held_header saw a header state"),
};
begin_entry(&mut self.tar_pos, &mut self.state, &held)
}
}
}
pub(super) fn next(&mut self, max: usize) -> Result<Event<'_>> {
if let Some((item, done)) = self.resumed.take() {
return Ok(Event::Entry {
item,
link: None,
done,
});
}
loop {
match &self.state {
State::File { left: 0, pad, .. } => {
self.state = State::Skip(*pad);
return Ok(Event::FileEnd);
}
State::Skip(0) => {
self.state = State::Header(Vec::new());
continue;
}
_ => {}
}
if self.pos == self.len {
if let Some(event) = self.held_header()? {
return Ok(event);
}
if std::mem::take(&mut self.restartable) && self.state.at_rest() {
return Ok(Event::Point);
}
let n = self.feed.fill(&mut self.buf)?;
if n == 0 {
return end_state(&self.state);
}
self.pos = 0;
self.len = n;
self.restartable = self.feed.restartable();
}
let avail = (self.len - self.pos) as u64;
match &mut self.state {
State::File { left, .. } => {
let n = (*left).min(avail).min(max as u64) as usize;
*left -= n as u64;
self.pos += n;
self.tar_pos += n as u64;
return Ok(Event::Data(&self.buf[self.pos - n..self.pos]));
}
State::Skip(left) => {
let n = (*left).min(avail);
*left -= n;
self.pos += n as usize;
self.tar_pos += n;
}
State::Trailer => {
if self.buf[self.pos..self.len].iter().any(|b| *b != 0) {
bail!(
"data follows the end of the tar archive inside the compressed \
stream, at offset {}; extraction refused",
self.tar_pos
);
}
self.pos = self.len;
self.tar_pos += avail;
}
State::Header(held) => match group(held)? {
Group::Need(k) => {
let n = (k as u64).min(avail) as usize;
held.extend_from_slice(&self.buf[self.pos..self.pos + n]);
self.pos += n;
self.tar_pos += n as u64;
}
Group::End => self.state = State::Trailer,
Group::Complete => {
let held = std::mem::take(held);
if let Some(event) = begin_entry(&mut self.tar_pos, &mut self.state, &held)?
{
return Ok(event);
}
}
},
}
}
}
pub(super) fn point(&self) -> Result<Resume> {
let (written, at) = match &self.state {
State::Header(_) => (0, TarAt::Boundary),
State::File {
item, data_start, ..
} => (
self.tar_pos - data_start,
TarAt::File {
path: item.path.clone(),
size: item.size,
mode: item.mode,
mtime: item.mtime.and_then(fsutil::epoch_secs),
data_start: *data_start,
},
),
State::Skip(left) => (0, TarAt::Skip { left: *left }),
State::Trailer => (0, TarAt::End),
};
self.feed.point(self.tar_pos, at, written)
}
pub(super) fn point_byte(&self) -> Option<u64> {
self.feed.point_byte()
}
}
pub(super) enum Group {
Need(usize),
Complete,
End,
}
pub(super) fn group(held: &[u8]) -> Result<Group> {
let mut at = 0usize;
loop {
if held.len() < at + 512 {
return Ok(Group::Need(at + 512 - held.len()));
}
let block = &held[at..at + 512];
if block.iter().all(|b| *b == 0) {
if at == 0 {
return Ok(Group::End);
}
bail!("reading the tar headers: a long-name or pax record has no entry after it");
}
match block[156] {
b'L' | b'K' | b'x' => {
let size = ::tar::Header::from_byte_slice(block)
.entry_size()
.context("reading the tar headers")?;
let next = (at as u64 + 512).saturating_add(size.saturating_add(511) & !511);
if next > MAX_HEADER {
bail!("reading the tar headers: an entry's header records exceed 1 MiB");
}
at = next as usize;
}
_ => return Ok(Group::Complete),
}
}
}
pub(super) fn begin_entry(
tar_pos: &mut u64,
state: &mut State,
held: &[u8],
) -> Result<Option<Event<'static>>> {
let last = &held[held.len() - 512..];
if last[156] == b'S' {
let name = ::tar::Header::from_byte_slice(last)
.path_bytes()
.into_owned();
bail!(
"'{}' is a sparse file, which is not supported",
String::from_utf8_lossy(&name)
);
}
let mut archive = ::tar::Archive::new(Cursor::new(held));
let mut entries = archive.entries().context("reading the tar headers")?;
let mut e = entries
.next()
.ok_or_else(|| anyhow!("reading the tar headers: an entry is incomplete"))?
.context("reading the tar headers")?;
let size = e.size();
let described = super::tar::describe(&mut e)?;
let data_start = *tar_pos;
let span = size
.checked_next_multiple_of(512)
.ok_or_else(|| anyhow!("reading the tar headers: an entry size is invalid"))?;
Ok(match described {
Some((item, _)) if item.kind == Kind::File => {
*state = State::File {
item: item.clone(),
data_start,
left: size,
pad: span - size,
};
Some(Event::Entry {
item,
link: None,
done: 0,
})
}
Some((item, link)) => {
*state = State::Skip(span);
Some(Event::Entry {
item,
link,
done: 0,
})
}
None => {
*state = State::Skip(span);
None
}
})
}
pub(super) fn end_state(state: &State) -> Result<Event<'static>> {
match state {
State::Trailer => Ok(Event::End),
State::Header(held) if held.iter().all(|b| *b == 0) => Ok(Event::End),
_ => bail!("the tar archive inside the compressed stream ends early"),
}
}
pub(super) fn resumed_state(s: &StreamPoint, written: u64) -> Result<(State, Option<(Item, u64)>)> {
Ok(match &s.at {
TarAt::Boundary => (State::Header(Vec::new()), None),
TarAt::Skip { left } => (State::Skip(*left), None),
TarAt::End => (State::Trailer, None),
TarAt::File {
path,
size,
mode,
mtime,
data_start,
} => {
let done = s.tar_pos.checked_sub(*data_start);
let safe = safe_path(&path.to_string_lossy()).as_ref() == Some(path);
if done != Some(written) || written > *size || !safe {
bail!(
"the journal's resume point is inconsistent; the archive will not be modified"
);
}
let item = Item {
path: path.clone(),
size: *size,
kind: Kind::File,
mode: *mode,
mtime: mtime.and_then(fsutil::from_epoch_secs),
};
let state = State::File {
item: item.clone(),
data_start: *data_start,
left: size - written,
pad: size.next_multiple_of(512) - size,
};
(state, Some((item, written)))
}
})
}
pub struct Walker {
inner: TarStream<Gzip>,
}
impl Walker {
fn new(storage: &Storage, from: Option<&Resume>) -> Result<Self> {
let reader = storage.reader(None);
let end = storage.original_len();
let gz = match from {
None => Gzip::new(reader, end, &GzPoint::start())?,
Some(r) => {
let s = r.stream.as_ref().ok_or_else(|| {
anyhow!("the journal's resume point does not belong to a compressed tar")
})?;
if s.kind != CodecKind::Gzip {
bail!(
"the journal's resume point belongs to {}, not this archive's \
compressed stream",
match s.kind {
CodecKind::Gzip => "a gzip member of",
CodecKind::Xz => "an xz block of",
CodecKind::Bzip2 => "a bzip2 block of",
CodecKind::Zstd => "a zstd frame of",
}
);
}
Gzip::new(
reader,
end,
&GzPoint {
bit: r.bit,
member_start: s.member_start,
crc: s.member_crc,
len: s.member_len,
window: s.window.clone(),
},
)?
}
};
Ok(Self {
inner: TarStream::new(gz, from)?,
})
}
fn next(&mut self, max: usize) -> Result<Event<'_>> {
self.inner.next(max)
}
fn point(&self) -> Result<Resume> {
self.inner.point()
}
fn point_byte(&self) -> Option<u64> {
self.inner.point_byte()
}
}
impl SolidWalker for Walker {
fn next(&mut self, max: usize) -> Result<Event<'_>> {
self.next(max)
}
fn point(&self) -> Result<Resume> {
self.point()
}
fn point_byte(&self) -> Option<u64> {
self.point_byte()
}
}
pub fn open(storage: &Arc<Storage>, from: Option<&Resume>) -> Result<Opened> {
let mut solid = Solid {
storage: storage.clone(),
from: from.cloned(),
profile: Profile::default(),
};
let name = storage
.primary()
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned();
storage.expect_reads(storage.original_len() - from.map_or(0, |r| r.bit / 8));
let (items, profile) = super::scan(&solid, from)
.map_err(|e| anyhow::Error::new(super::Damaged(e)))
.with_context(|| format!("checking {name} (nothing was changed)"))?;
solid.profile = profile;
Ok(Opened {
name: NAME,
format: Box::new(TarGz { solid }),
units: vec![Unit {
start: 0,
key: 0,
items,
encrypted: false,
}],
tail_start: storage.original_len(),
})
}
struct TarGz {
solid: Solid,
}
impl Format for TarGz {
fn worker(&self, _threads: u32) -> Result<Box<dyn Worker + '_>> {
Ok(Box::new(TarGzWorker { solid: &self.solid }))
}
fn solid(&self) -> Option<&dyn SolidStream> {
Some(&self.solid)
}
}
impl SolidStream for Solid {
fn profile(&self) -> &Profile {
&self.profile
}
fn walk(&self, from: Option<&Resume>) -> Result<Box<dyn SolidWalker>> {
Ok(Box::new(self.walk(from)?))
}
}
struct TarGzWorker<'a> {
solid: &'a Solid,
}
impl Worker for TarGzWorker<'_> {
fn extract(&mut self, _unit: &Unit, sink: &mut dyn Sink) -> Result<()> {
let mut w = self.solid.walk(self.solid.from.as_ref())?;
loop {
match w.next(BUF)? {
Event::Entry { item, done, .. } if item.kind == Kind::File => {
let left = Item {
size: item.size - done,
..item
};
let mut data = FileData {
walker: &mut w,
ended: false,
};
sink.item(&left, &mut data)?;
let mut rest = [0u8; 4096];
while data.read(&mut rest)? > 0 {}
}
Event::Entry { item, link, .. } => {
sink.item(&item, &mut Cursor::new(link.unwrap_or_default()))?;
}
Event::Data(_) | Event::FileEnd | Event::Point => {}
Event::End => return Ok(()),
}
}
}
}
struct FileData<'a> {
walker: &'a mut Walker,
ended: bool,
}
impl Read for FileData<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
while !self.ended && !buf.is_empty() {
match self.walker.next(buf.len()) {
Ok(Event::Data(d)) => {
buf[..d.len()].copy_from_slice(d);
return Ok(d.len());
}
Ok(Event::Point) => {}
Ok(Event::FileEnd) => self.ended = true,
Ok(_) => return Err(io::Error::other("the tar entry ended unexpectedly")),
Err(e) => return Err(io::Error::other(format!("{e:#}"))),
}
}
Ok(0)
}
}
mod hex {
use serde::{Deserialize, Deserializer, Serializer, de::Error};
pub fn serialize<S: Serializer>(bytes: &[u8], s: S) -> Result<S::Ok, S::Error> {
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
out.push_str(&format!("{b:02x}"));
}
s.serialize_str(&out)
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let s = String::deserialize(d)?;
if !s.len().is_multiple_of(2) {
return Err(D::Error::custom("odd-length hex string"));
}
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(D::Error::custom))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_gzip_point_writes_every_field_older_builds_read() {
let point = StreamPoint {
kind: CodecKind::Gzip,
member_start: false,
member_crc: 0,
member_len: 0,
window: Vec::new(),
block: 0,
level: None,
combined_crc: None,
tar_pos: 0,
at: TarAt::Boundary,
};
let json = serde_json::to_value(&point).unwrap();
for field in [
"member_start",
"member_crc",
"member_len",
"window",
"tar_pos",
] {
assert!(json.get(field).is_some(), "{field} missing from {json}");
}
assert!(json.get("kind").is_none(), "kind is new and stays out");
assert!(json.get("block").is_none(), "block is new and stays out");
assert!(json.get("level").is_none(), "level is new and stays out");
assert!(
json.get("combined_crc").is_none(),
"combined_crc is new and stays out"
);
}
}