Skip to content

Commit

Permalink
version 0.1.6: add LTAR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
digama0 committed Sep 14, 2023
1 parent 405f9c0 commit 8e63c8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leangz"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
default-run = "leangz"

Expand Down
38 changes: 30 additions & 8 deletions src/ltar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ pub fn unpack<R: BufRead>(
return Err(UnpackError::BadLtar)
}
let trace = tarfile.read_u64::<LE>()?;
let read_cstr = |buf: &mut Vec<_>, tarfile: &mut R| -> Result<Option<PathBuf>, UnpackError> {
let read_cstr = |buf: &mut Vec<_>, tarfile: &mut R| -> Result<bool, UnpackError> {
buf.clear();
tarfile.read_until(0, buf)?;
Ok(match buf.pop() {
Some(_) => Some(basedir.join(std::str::from_utf8(buf)?)),
None => None,
})
Ok(buf.pop().is_some())
};
let trace_path = read_cstr(&mut buf, &mut tarfile)?.ok_or(UnpackError::BadLtar)?;
let read_cstr_path =
|buf: &mut Vec<_>, tarfile: &mut R| -> Result<Option<Option<PathBuf>>, UnpackError> {
Ok(match read_cstr(buf, tarfile)? {
true if buf.is_empty() => Some(None),
true => Some(Some(basedir.join(std::str::from_utf8(buf)?))),
false => None,
})
};
let trace_path = read_cstr_path(&mut buf, &mut tarfile)?.flatten().ok_or(UnpackError::BadLtar)?;
if !force {
match std::fs::read_to_string(&trace_path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Expand All @@ -71,7 +76,16 @@ pub fn unpack<R: BufRead>(
std::fs::create_dir_all(prefix)?;
std::fs::write(trace_path, format!("{trace}"))?;
let dict = zstd::dict::DecoderDictionary::copy(DICT_V1);
while let Some(path) = read_cstr(&mut buf, &mut tarfile)? {
while let Some(path) = read_cstr_path(&mut buf, &mut tarfile)? {
let Some(path) = path else {
if !read_cstr(&mut buf, &mut tarfile)? {
return Err(UnpackError::BadLtar)
}
if verbose {
println!("comment: {}", std::str::from_utf8(&buf)?);
}
continue
};
if verbose {
println!("copying {}", path.display());
}
Expand Down Expand Up @@ -108,7 +122,15 @@ pub fn pack(
tarfile.write_all(trace_path.as_bytes())?;
tarfile.write_u8(0)?;
let dict_v1 = zstd::dict::EncoderDictionary::copy(DICT_V1, COMPRESSION_LEVEL);
for file in args {
let mut it = args.into_iter();
while let Some(file) = it.next() {
if file == "-c" {
let comment = it.next().expect("expected comment argument");
tarfile.write_u8(0)?;
tarfile.write_all(comment.as_bytes())?;
tarfile.write_u8(0)?;
continue
}
tarfile.write_all(file.as_bytes())?;
tarfile.write_u8(0)?;
let path = basedir.join(file);
Expand Down

0 comments on commit 8e63c8f

Please sign in to comment.