Skip to content

Commit

Permalink
Merge branch 'master' into dep-bump
Browse files Browse the repository at this point in the history
  • Loading branch information
hucsmn authored Apr 6, 2024
2 parents 2472009 + 37ad364 commit 7079759
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 80 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[workspace]
members = [".", "utils"]
default-members = [".", "utils"]

[package]
name = "qbsdiff"
version = "1.4.1"
Expand Down
7 changes: 5 additions & 2 deletions cmd/qbsdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() {

fn execute(args: BsdiffArgs) -> io::Result<()> {
// validate command line arguments
if !(matches!(args.compress_level, Some(0..=9) | None)) {
if !matches!(args.compress_level, Some(0..=9) | None) {
return Err(io::Error::new(
io::ErrorKind::Other,
"compression level must be in range 0-9",
Expand All @@ -67,7 +67,10 @@ fn execute(args: BsdiffArgs) -> io::Result<()> {

// setup input/output
if args.source_path == "-" && args.target_path == "-" {
return Err(io::Error::new(io::ErrorKind::Other, "source and target are both from stdin"));
return Err(io::Error::new(
io::ErrorKind::Other,
"source and target are both from stdin",
));
}
let source = input_bytes(&args.source_path)?;
let target = input_bytes(&args.target_path)?;
Expand Down
5 changes: 4 additions & 1 deletion cmd/qbspatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ fn main() {
fn execute(args: BspatchArgs) -> io::Result<()> {
// setup input/output
if args.source_path == "-" && args.patch_path == "-" {
return Err(io::Error::new(io::ErrorKind::Other, "source and patch are both from stdin"));
return Err(io::Error::new(
io::ErrorKind::Other,
"source and patch are both from stdin",
));
}
let source = input_bytes(&args.source_path)?;
let target = output_writer(&args.target_path)?;
Expand Down
7 changes: 2 additions & 5 deletions src/bsdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const MIN_CHUNK: usize = 256 * 1024;
const DEFAULT_CHUNK: usize = 512 * 1024;

/// Magic number bytes of bsdiff 4.x patch files.
const BSDIFF4_MAGIC: &'static [u8] = b"BSDIFF40";
const BSDIFF4_MAGIC: &[u8] = b"BSDIFF40";

/// Parallel searching scheme of bsdiff.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -177,8 +177,6 @@ impl<'s, 't> Bsdiff<'s, 't> {
/// The fastest/default compression level is usually good enough.
/// In contrast, patch files produced with the best level appeared slightly
/// bigger in many test cases.
///
/// [struct]: bzip2::Compression
pub fn compression_level(mut self, compression_level: u32) -> Self {
self.compression_level = Compression::new(u32::min(u32::max(compression_level, 0), 9));
self
Expand Down Expand Up @@ -369,8 +367,7 @@ impl<'s, 't> ParSaDiff<'s, 't> {

/// Compute all the bsdiff controls in parallel.
pub fn compute(mut self) -> Vec<Control> {
self
.jobs
self.jobs
.par_iter_mut()
.map(|diff| {
// Search current chunk.
Expand Down
2 changes: 1 addition & 1 deletion src/bspatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<'s, 'p, T: Write> Context<'s, 'p, T> {

/// Move the cursor on source.
fn seek(&mut self, offset: i64) -> Result<()> {
self.source.seek(SeekFrom::Current(offset)).map(std::mem::drop)
self.source.seek(SeekFrom::Current(offset)).map(drop)
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/compatible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn regular_samples_compat() {
let s = sample.load_source().unwrap();
let t = sample.load_target().unwrap();

let p1 = testing.load_cached_patch(&sample).unwrap();
let p1 = testing.load_cached_patch(sample).unwrap();
let t1 = testing.qbspatch(&s[..], &p1[..]).unwrap();
if t1 != t {
panic!("bsdiff/qbspatch incompatible: `{}`", sample.name);
Expand Down Expand Up @@ -42,7 +42,7 @@ fn random_samples_compat() {
let s = sample.load_source().unwrap();
let t = sample.load_target().unwrap();

let p1 = testing.load_cached_patch(&sample).unwrap();
let p1 = testing.load_cached_patch(sample).unwrap();
let t1 = testing.qbspatch(&s[..], &p1[..]).unwrap();
if t1 != t {
panic!("bsdiff/qbspatch incompatible: `{}`", sample.name);
Expand Down
112 changes: 43 additions & 69 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Testing {
}

impl Testing {
/// Create new tesing context.
/// Create new testing context.
pub fn new(assets_dir: path::PathBuf) -> Self {
Testing { assets_dir }
}
Expand Down Expand Up @@ -137,7 +137,11 @@ impl Testing {
run_command_in(
dir,
"bsdiff",
&[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
&[
sample.source.as_os_str(),
sample.target.as_os_str(),
sample.patch.as_os_str(),
],
)?;
}
fs::read(sample.patch.as_path())
Expand Down Expand Up @@ -216,14 +220,17 @@ impl Benchmarking {
run_command_in(
dir,
"bsdiff",
&[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
&[
sample.source.as_os_str(),
sample.target.as_os_str(),
sample.patch.as_os_str(),
],
)?;
} else {
let mut patch = Vec::new();
let source = fs::read(sample.source.as_path())?;
let target = fs::read(sample.target.as_path())?;
Bsdiff::new(&source[..], &target[..])
.compare(io::Cursor::new(&mut patch))?;
Bsdiff::new(&source[..], &target[..]).compare(io::Cursor::new(&mut patch))?;
patch.shrink_to_fit();
fs::write(sample.patch.as_path(), &patch[..])?;
return Ok(patch);
Expand All @@ -233,7 +240,8 @@ impl Benchmarking {
}

fn should_use_bsdiff(&self, sample: &Sample) -> bool {
sample.patch
sample
.patch
.parent()
.and_then(|p| p.file_name())
.map(|d| d == "samples")
Expand Down Expand Up @@ -266,9 +274,9 @@ fn run_bspatch_in<P: AsRef<Path>>(dir: P, s: &[u8], p: &[u8]) -> io::Result<Vec<
}

fn run_command_in<P, S>(dir: P, cmd: &str, args: &[S]) -> io::Result<()>
where
P: AsRef<Path>,
S: AsRef<OsStr>,
where
P: AsRef<Path>,
S: AsRef<OsStr>,
{
let bin = get_binary_in(dir, cmd)?;
let mut proc = process::Command::new(&bin)
Expand All @@ -278,20 +286,24 @@ fn run_command_in<P, S>(dir: P, cmd: &str, args: &[S]) -> io::Result<()>
.spawn()?;

let mut errors = String::new();
let mut stderr = proc.stderr.take()
let mut stderr = proc
.stderr
.take()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to get command stderr"))?;
stderr.read_to_string(&mut errors)?;

let status = proc.wait()?;
if !status.success() {
let message = format!("command [{} {}], {}, stderr:\n{}",
bin.to_string_lossy(),
args.iter()
.map(|arg| arg.as_ref().to_string_lossy())
.collect::<Vec<_>>()
.join(" "),
status,
errors);
let message = format!(
"command [{} {}], {}, stderr:\n{}",
bin.to_string_lossy(),
args.iter()
.map(|arg| arg.as_ref().to_string_lossy())
.collect::<Vec<_>>()
.join(" "),
status,
errors
);
return Err(io::Error::new(io::ErrorKind::Other, message));
} else {
Ok(())
Expand Down Expand Up @@ -346,15 +358,10 @@ fn get_samples_in<P: AsRef<Path>>(dir: P) -> io::Result<Vec<Sample>> {
if let Some(p) = pat.to_str() {
walker = glob(p).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot convert to str",
));
return Err(io::Error::new(io::ErrorKind::Other, "cannot convert to str"));
}
for result in walker.into_iter() {
let source = result
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
.into_path();
let source = result.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?.into_path();

let name;
let target;
Expand All @@ -371,10 +378,7 @@ fn get_samples_in<P: AsRef<Path>>(dir: P) -> io::Result<Vec<Sample>> {
pbuf.push(".p");
patch = path::PathBuf::from(d).join(pbuf.as_os_str());
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot make target or patch path",
));
return Err(io::Error::new(io::ErrorKind::Other, "cannot make target or patch path"));
}

if let Err(_) = fs::metadata(target.as_path()) {
Expand Down Expand Up @@ -417,10 +421,8 @@ fn get_random_caches_in<P: AsRef<Path>>(dir: P, descs: &[RandomSample]) -> io::R
let target = dir.as_ref().join(format!("{}.{}.t", desc.name, tdesc.name(id)));
if !exists_file(target.as_path()) {
match tdesc {
RandomTarget::Bytes(bytes) =>
fs::write(target.as_path(), bytes)?,
RandomTarget::Distort(rate) =>
fs::write(target.as_path(), &distort(&sdata[..], *rate)[..])?,
RandomTarget::Bytes(bytes) => fs::write(target.as_path(), bytes)?,
RandomTarget::Distort(rate) => fs::write(target.as_path(), &distort(&sdata[..], *rate)[..])?,
}
}
let patch = dir.as_ref().join(format!("{}.{}.p", desc.name, tdesc.name(id)));
Expand Down Expand Up @@ -483,13 +485,13 @@ r incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis no\
strud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Dui\
s aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fu\
giat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in cu\
lpa qui officia deserunt mollit anim id est laborum."
lpa qui officia deserunt mollit anim id est laborum.",
),
targets: vec![
TBytes(b""),
TBytes(
b"consectetur adip##cing elit, jed do eiusmod wir mussen wissen. wir werden wis\
sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. "
sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. ",
),
TBytes(b"the quick brown fox jumps over the lazy dog"),
Distort(0.0),
Expand All @@ -500,42 +502,22 @@ sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. "
RandomSample {
name: "rand-4k",
source: Random(4096),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-256k",
source: Random(256 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-1m",
source: Random(1024 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-8m",
source: Random(8 * 1024 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
]
}
Expand All @@ -549,20 +531,12 @@ pub fn default_random_bench_samples() -> Vec<RandomSample> {
RandomSample {
name: "rand-512k",
source: Random(512 * 1024),
targets: vec![
Distort(0.05),
Distort(0.50),
Distort(0.95),
],
targets: vec![Distort(0.05), Distort(0.50), Distort(0.95)],
},
RandomSample {
name: "rand-4m",
source: Random(4 * 1024 * 1024),
targets: vec![
Distort(0.05),
Distort(0.50),
Distort(0.95),
],
targets: vec![Distort(0.05), Distort(0.50), Distort(0.95)],
},
]
}
Expand Down

0 comments on commit 7079759

Please sign in to comment.