Skip to content

Commit

Permalink
version 0.1.7: add comment display utility
Browse files Browse the repository at this point in the history
  • Loading branch information
digama0 committed Sep 15, 2023
1 parent 8e63c8f commit cca65df
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 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.6"
version = "0.1.7"
edition = "2021"
default-run = "leangz"

Expand Down
35 changes: 32 additions & 3 deletions src/ltar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use byteorder::WriteBytesExt;
use byteorder::LE;
use memmap2::Mmap;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::io::Write;
use std::io::{self, BufRead, Seek, Write};
use std::path::{Path, PathBuf};

use crate::lgz;
Expand Down Expand Up @@ -155,3 +153,34 @@ pub fn pack(
}
Ok(())
}

pub fn comments<R: BufRead + Seek>(mut tarfile: R) -> Result<Vec<String>, UnpackError> {
let mut buf = vec![0; 4];
tarfile.read_exact(&mut buf)?;
if buf != *b"LTAR" {
return Err(UnpackError::BadLtar)
}
tarfile.read_u64::<LE>()?;
let read_cstr = |buf: &mut Vec<_>, tarfile: &mut R| -> Result<bool, UnpackError> {
buf.clear();
tarfile.read_until(0, buf)?;
Ok(buf.pop().is_some())
};
if !read_cstr(&mut buf, &mut tarfile)? {
return Err(UnpackError::BadLtar)
}
let mut comments = vec![];
while read_cstr(&mut buf, &mut tarfile)? {
if buf.is_empty() {
if !read_cstr(&mut buf, &mut tarfile)? {
return Err(UnpackError::BadLtar)
}
comments.push(std::str::from_utf8(&buf)?.to_string());
continue
}
tarfile.read_u8()?;
let len = tarfile.read_u64::<LE>()?;
tarfile.seek(io::SeekFrom::Current(len as _))?;
}
Ok(comments)
}
26 changes: 25 additions & 1 deletion src/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::atomic::AtomicBool;
fn main() {
let help = || panic!("usage: leantar [-v] [-d|-x] [-C BASEDIR] OUT.ltar FILE.trace [FILE ...]");
let mut do_decompress = false;
let mut do_show_comments = false;
let mut verbose = false;
let mut force = false;
let mut from_stdin = false;
Expand Down Expand Up @@ -51,11 +52,34 @@ fn main() {
help();
}
}
"-k" => {
do_show_comments = true;
args.next();
}
_ => break,
}
}
let basedir = PathBuf::from(basedir.unwrap_or_else(|| ".".into()));
if do_decompress {
if do_show_comments {
let file = args.next().unwrap_or_else(help);
let tarfile = match File::open(&file) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("{file} not found");
std::process::exit(1);
}
e => BufReader::new(e.unwrap()),
};
match ltar::comments(tarfile) {
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
Ok(comments) =>
for comment in comments {
println!("{comment}")
},
}
} else if do_decompress {
let mut args_vec = vec![];
for arg in args {
if arg == "-" {
Expand Down

0 comments on commit cca65df

Please sign in to comment.