diff --git a/CHANGELOG.md b/CHANGELOG.md index 955f4641..4b7c9fa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -**Version 0.5.1 (unreleased)** +**Version 0.6.0 (unreleased)** - Fix issue where output directory would not be created if it did not exist - Use miniz for compression strategies where it outperforms zlib + - [SEMVER_MINOR] Partially implement -p / --preserve, as far as stable Rust will allow for now **Version 0.5.0** - [SEMVER_MINOR] Palette entries can now reduced, on by default ([#11](https://github.com/shssoichiro/oxipng/issues/11)) diff --git a/src/lib.rs b/src/lib.rs index 2e0086a2..5df3467a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,6 +275,51 @@ pub fn optimize(filepath: &Path, opts: &Options) -> Result<(), String> { return Err(format!("Unable to write to file {}", opts.out_file.display())) } }; + + if opts.preserve_attrs { + match File::open(filepath) { + Ok(f) => { + match f.metadata() { + Ok(metadata) => { + // TODO: Implement full permission changing on Unix + // Not available in stable, requires block cfg statements + // See https://github.com/rust-lang/rust/issues/15701 + { + match out_file.metadata() { + Ok(out_meta) => { + let readonly = metadata.permissions() + .readonly(); + out_meta.permissions() + .set_readonly(readonly); + } + Err(_) => { + if opts.verbosity.is_some() { + writeln!(&mut stderr(), + "Failed to set permissions on output file") + .ok(); + } + } + } + } + } + Err(_) => { + if opts.verbosity.is_some() { + writeln!(&mut stderr(), + "Failed to read permissions on input file") + .ok(); + } + } + } + } + Err(_) => { + if opts.verbosity.is_some() { + writeln!(&mut stderr(), "Failed to read permissions on input file") + .ok(); + } + } + }; + } + let mut buffer = BufWriter::new(out_file); match buffer.write_all(&output_data) { Ok(_) => { diff --git a/tests/files/preserve_attrs.png b/tests/files/preserve_attrs.png new file mode 100644 index 00000000..3f9bdb1a Binary files /dev/null and b/tests/files/preserve_attrs.png differ diff --git a/tests/flags.rs b/tests/flags.rs index 9468e80c..82ed0ce7 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -463,3 +463,21 @@ fn interlaced_0_to_1_other_filter_mode() { remove_file(output).ok(); } + +#[test] +fn preserve_attrs() { + let input = PathBuf::from("tests/files/preserve_attrs.png"); + let mut opts = get_opts(&input); + opts.preserve_attrs = true; + let output = opts.out_file.clone(); + + test_it_converts(&input, + &output, + &opts, + png::ColorType::RGB, + png::BitDepth::Eight, + png::ColorType::RGB, + png::BitDepth::Eight); + + // TODO: Actually check permissions +}