Partially implement --preserve

Needs better implementation once stable Rust improves,
or perhaps see if there's a crate for this

Reference #4 but keep open
This commit is contained in:
Joshua Holmer 2016-04-29 23:37:27 -04:00
parent 73d477924e
commit 08eb44ec1a
4 changed files with 65 additions and 1 deletions

View file

@ -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))

View file

@ -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(_) => {

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View file

@ -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
}