gzippy 0.8.0

The fastest parallel gzip. Drop-in replacement for gzip and pigz, and a Rust library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! File, stdin, and directory I/O for decompression.
//!
//! Entry points: `decompress_file` and `decompress_stdin`.
//! All decompression logic is in `decompression.rs`; this module only handles
//! filesystem concerns: mmap, output path selection, metadata preservation,
//! stats printing, and signal-handler registration.

use std::fs::File;
use std::io::{self, stdin, stdout, BufReader, BufWriter, Read, Write};
use std::path::Path;

struct CountingWriter<W: Write> {
    inner: W,
    count: u64,
}
impl<W: Write> CountingWriter<W> {
    fn new(inner: W) -> Self {
        Self { inner, count: 0 }
    }
}
impl<W: Write> Write for CountingWriter<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let n = self.inner.write(buf)?;
        self.count += n as u64;
        Ok(n)
    }
    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

use memmap2::Mmap;

use crate::cli::GzippyArgs;
use crate::decompress::format::{
    extract_gzip_fname, extract_gzip_mtime, has_bgzf_markers, is_likely_multi_member,
};
use crate::error::{GzippyError, GzippyResult};
use crate::format::CompressionFormat;
use crate::utils::{debug_enabled, preserve_metadata, strip_compression_extension};

const STREAM_BUFFER_SIZE: usize = 1024 * 1024;

pub fn decompress_file(filename: &str, args: &GzippyArgs) -> GzippyResult<i32> {
    if filename == "-" {
        return decompress_stdin(args);
    }

    let input_path = Path::new(filename);
    if !input_path.exists() {
        return Err(GzippyError::FileNotFound(filename.to_string()));
    }
    if input_path.is_dir() {
        return if args.recursive {
            decompress_directory(filename, args)
        } else {
            Err(GzippyError::invalid_argument(format!(
                "{} is a directory",
                filename
            )))
        };
    }
    if input_path.is_symlink() && !args.force {
        if !args.quiet {
            eprintln!(
                "gzippy: {}: is a symbolic link -- skipping (use -f to force)",
                filename
            );
        }
        return Ok(2);
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::FileTypeExt;
        let ft = std::fs::symlink_metadata(input_path)?.file_type();
        if ft.is_block_device() || ft.is_char_device() || ft.is_fifo() || ft.is_socket() {
            if !args.quiet {
                eprintln!("gzippy: {}: is not a regular file -- skipping", filename);
            }
            return Ok(2);
        }
    }

    let input_file = File::open(input_path)?;
    let file_size = input_file.metadata()?.len();
    let mmap = unsafe { Mmap::map(&input_file)? };
    let _ = mmap.advise(memmap2::Advice::Sequential);

    let is_compressed =
        mmap.len() >= 2 && ((mmap[0] == 0x1f && mmap[1] == 0x8b) || mmap[0] == 0x78);

    if args.force && args.stdout && !is_compressed {
        let stdout = stdout();
        let mut writer = BufWriter::with_capacity(STREAM_BUFFER_SIZE, stdout.lock());
        writer.write_all(&mmap)?;
        writer.flush()?;
        return Ok(0);
    }
    if !is_compressed {
        return Err(GzippyError::invalid_argument(format!(
            "{}: not in gzip format",
            filename
        )));
    }

    let output_path = if args.stdout {
        None
    } else {
        Some(get_output_filename(input_path, args, &mmap))
    };

    if let Some(ref output_path) = output_path {
        if output_path.exists() && !args.force {
            use std::io::IsTerminal;
            if std::io::stdin().is_terminal() {
                eprint!(
                    "gzippy: {} already exists; do you wish to overwrite (y or n)? ",
                    output_path.display()
                );
                let mut response = String::new();
                std::io::stdin().read_line(&mut response)?;
                if !response.trim().eq_ignore_ascii_case("y") {
                    eprintln!("\tnot overwritten");
                    return Ok(2);
                }
            } else {
                return Err(GzippyError::invalid_argument(format!(
                    "Output file {} already exists",
                    output_path.display()
                )));
            }
        }
    }

    let format = detect_format(input_path);

    if let Some(ref output_path) = output_path {
        crate::set_output_file(Some(output_path.to_string_lossy().to_string()));
    }

    let result = if args.stdout {
        let stdout = stdout();
        let mut writer = BufWriter::with_capacity(STREAM_BUFFER_SIZE, stdout.lock());
        let r = decompress_to_writer(&mmap, &mut writer, format, args);
        writer.flush()?;
        r
    } else {
        let output_path = output_path.clone().unwrap();
        let output_file = File::create(&output_path)?;
        let mut writer = BufWriter::with_capacity(STREAM_BUFFER_SIZE, output_file);
        let r = decompress_to_writer(&mmap, &mut writer, format, args);
        writer.flush()?;
        r
    };

    crate::set_output_file(None);

    match result {
        Ok(output_size) => {
            if !args.stdout {
                if let Some(ref output_path) = output_path {
                    preserve_metadata(input_path, output_path);
                    if args.name {
                        if let Some(mtime) = extract_gzip_mtime(&mmap) {
                            if mtime != 0 {
                                let _ = filetime::set_file_mtime(
                                    output_path,
                                    filetime::FileTime::from_unix_time(mtime as i64, 0),
                                );
                            }
                        }
                    }
                    if args.synchronous {
                        if let Ok(f) = File::open(output_path) {
                            let _ = f.sync_all();
                        }
                    }
                }
            }
            if args.verbosity > 0 && !args.quiet {
                print_stats(
                    file_size,
                    output_size,
                    input_path,
                    output_path.as_deref(),
                    args,
                );
            }
            if !args.keep && !args.stdout {
                std::fs::remove_file(input_path)?;
            }
            Ok(0)
        }
        Err(e) => {
            if !args.stdout {
                let cleanup_path = get_output_filename(input_path, args, &mmap);
                if cleanup_path.exists() {
                    let _ = std::fs::remove_file(&cleanup_path);
                }
            }
            Err(e)
        }
    }
}

pub fn decompress_stdin(args: &GzippyArgs) -> GzippyResult<i32> {
    #[cfg(unix)]
    let mmap_data: Option<Mmap> = {
        use std::os::unix::io::FromRawFd;
        let meta = std::fs::File::from(unsafe {
            std::os::unix::io::OwnedFd::from_raw_fd(0 /* stdin */)
        });
        let is_regular = meta
            .metadata()
            .map(|m| m.file_type().is_file())
            .unwrap_or(false);
        let result = if is_regular {
            let m = unsafe { Mmap::map(&meta) }.ok();
            if let Some(ref mmap) = m {
                let _ = mmap.advise(memmap2::Advice::Sequential);
            }
            m
        } else {
            None
        };
        std::mem::forget(meta);
        result
    };
    #[cfg(not(unix))]
    let mmap_data: Option<Mmap> = None;

    let input_data_vec;
    let input_data: &[u8] = if let Some(ref mmap) = mmap_data {
        if debug_enabled() {
            eprintln!("[gzippy] stdin mmap'd: {} bytes", mmap.len());
        }
        &mmap[..]
    } else {
        let stdin_handle = stdin();
        let mut data = Vec::new();
        {
            let mut reader = BufReader::with_capacity(STREAM_BUFFER_SIZE, stdin_handle.lock());
            reader.read_to_end(&mut data)?;
        }
        input_data_vec = data;
        &input_data_vec
    };

    if input_data.is_empty() {
        return Ok(0);
    }

    let is_gzip = input_data.len() >= 2 && input_data[0] == 0x1f && input_data[1] == 0x8b;
    let is_zlib = input_data.len() >= 2 && input_data[0] == 0x78;

    if args.force && !is_gzip && !is_zlib {
        let stdout = stdout();
        let mut writer = BufWriter::with_capacity(STREAM_BUFFER_SIZE, stdout.lock());
        writer.write_all(input_data)?;
        writer.flush()?;
        return Ok(0);
    }

    let format = if is_gzip {
        CompressionFormat::Gzip
    } else if is_zlib {
        CompressionFormat::Zlib
    } else {
        CompressionFormat::Gzip
    };

    let verbose = args.verbose && !args.quiet;
    let in_bytes = input_data.len() as u64;

    let stdout = stdout();
    let mut counted =
        CountingWriter::new(BufWriter::with_capacity(STREAM_BUFFER_SIZE, stdout.lock()));

    match format {
        CompressionFormat::Gzip | CompressionFormat::Zip => {
            let is_bgzf = has_bgzf_markers(input_data);
            let is_multi = !is_bgzf && is_likely_multi_member(input_data);
            let can_parallelize = args.processes > 1 && (is_bgzf || is_multi);

            if debug_enabled() {
                eprintln!(
                    "[gzippy] decompress_stdin: len={} bgzf={} multi={} parallel={} procs={}",
                    input_data.len(),
                    is_bgzf,
                    is_multi,
                    can_parallelize,
                    args.processes
                );
            }

            if is_bgzf {
                let threads = if can_parallelize { args.processes } else { 1 };
                crate::decompress::bgzf::decompress_bgzf_parallel(
                    input_data,
                    &mut counted,
                    threads,
                )?;
            } else if can_parallelize {
                let output = crate::decompress::decompress_gzip_to_vec(input_data, args.processes)?;
                counted.write_all(&output)?;
            } else {
                crate::decompress::decompress_single_member(
                    input_data,
                    &mut counted,
                    args.processes,
                )?;
            }
        }
        CompressionFormat::Zlib => {
            crate::decompress::decompress_zlib_turbo(input_data, &mut counted)?;
        }
    }

    counted.flush()?;
    if verbose {
        let out_bytes = counted.count;
        let ratio = if in_bytes > 0 {
            out_bytes as f64 / in_bytes as f64
        } else {
            1.0
        };
        let (in_size, in_unit) = human_size(in_bytes);
        let (out_size, out_unit) = human_size(out_bytes);
        eprintln!(
            "(stdin): {:.1}{}{:.1}{} ({:.1}x expansion)",
            in_size, in_unit, out_size, out_unit, ratio
        );
    }
    Ok(0)
}

fn decompress_directory(dirname: &str, args: &GzippyArgs) -> GzippyResult<i32> {
    use walkdir::WalkDir;
    let mut exit_code = 0;
    for entry in WalkDir::new(dirname) {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() && crate::utils::is_compressed_file(path) {
            let path_str = path.to_string_lossy();
            match decompress_file(&path_str, args) {
                Ok(code) => {
                    if code != 0 {
                        exit_code = code;
                    }
                }
                Err(e) => {
                    eprintln!("gzippy: {}: {}", path_str, e);
                    exit_code = 1;
                }
            }
        }
    }
    Ok(exit_code)
}

/// Dispatch a memory-mapped buffer to the correct decompressor.
fn decompress_to_writer<W: Write>(
    mmap: &Mmap,
    writer: &mut W,
    format: CompressionFormat,
    args: &GzippyArgs,
) -> GzippyResult<u64> {
    match format {
        CompressionFormat::Gzip | CompressionFormat::Zip => {
            let is_gzip = mmap.len() >= 2 && mmap[0] == 0x1f && mmap[1] == 0x8b;
            if !is_gzip {
                return Ok(0);
            }
            let bgzf = has_bgzf_markers(&mmap[..]);
            let multi = is_likely_multi_member(&mmap[..]);
            let can_parallelize = args.processes > 1 && (bgzf || multi);

            if debug_enabled() {
                eprintln!(
                    "[gzippy] decompress_file: len={} bgzf={} multi={} parallel={} procs={}",
                    mmap.len(),
                    bgzf,
                    multi,
                    can_parallelize,
                    args.processes
                );
            }

            if bgzf {
                let threads = if can_parallelize { args.processes } else { 1 };
                let bytes =
                    crate::decompress::bgzf::decompress_bgzf_parallel(&mmap[..], writer, threads)?;
                Ok(bytes)
            } else if can_parallelize {
                let output = crate::decompress::decompress_gzip_to_vec(&mmap[..], args.processes)?;
                let len = output.len() as u64;
                writer.write_all(&output)?;
                Ok(len)
            } else {
                crate::decompress::decompress_single_member(&mmap[..], writer, args.processes)
            }
        }
        CompressionFormat::Zlib => crate::decompress::decompress_zlib_turbo(&mmap[..], writer),
    }
}

fn detect_format(path: &Path) -> CompressionFormat {
    crate::utils::detect_format_from_file(path).unwrap_or(CompressionFormat::Gzip)
}

fn get_output_filename(input_path: &Path, args: &GzippyArgs, data: &[u8]) -> std::path::PathBuf {
    if args.stdout {
        return input_path.to_path_buf();
    }
    if args.name {
        if let Some(fname) = extract_gzip_fname(data) {
            if !fname.is_empty() {
                let mut output = input_path.to_path_buf();
                output.set_file_name(&fname);
                return output;
            }
        }
    }
    if args.suffix != ".gz" {
        let suffix = args.suffix.trim_start_matches('.');
        if let Some(name) = input_path.file_name().and_then(|n| n.to_str()) {
            let lower = name.to_lowercase();
            let suffix_with_dot = format!(".{}", suffix);
            if lower.ends_with(&suffix_with_dot) {
                let mut output = input_path.to_path_buf();
                output.set_file_name(&name[..name.len() - suffix_with_dot.len()]);
                return output;
            }
        }
    }
    let mut output_path = strip_compression_extension(input_path);
    if output_path == input_path {
        output_path = input_path.to_path_buf();
        let current_name = output_path.file_name().unwrap().to_str().unwrap();
        output_path.set_file_name(format!("{}.out", current_name));
    }
    output_path
}

fn print_stats(
    input_size: u64,
    output_size: u64,
    input_path: &Path,
    output_path: Option<&Path>,
    args: &GzippyArgs,
) {
    // ratio: how much the original was compressed (positive = shrank, negative = grew)
    let saved_pct = if output_size > 0 {
        (1.0_f64 - input_size as f64 / output_size as f64) * 100.0
    } else {
        0.0
    };
    if args.verbosity >= 2 {
        // gzippy detail format for -vv
        let name = input_path
            .file_name()
            .unwrap_or_default()
            .to_str()
            .unwrap_or("<unknown>");
        let (in_sz, in_u) = human_size(input_size);
        let (out_sz, out_u) = human_size(output_size);
        let expansion = output_size as f64 / input_size.max(1) as f64;
        eprintln!(
            "{}: {:.1}{}{:.1}{} ({:.1}x expansion)",
            name, in_sz, in_u, out_sz, out_u, expansion
        );
    } else {
        // gzip-compatible format for -v: "path:   X.X% -- replaced with outpath"
        let in_name = input_path.to_str().unwrap_or("<unknown>");
        match output_path {
            Some(out) => eprintln!(
                "{}:\t{:7.1}% -- replaced with {}",
                in_name,
                saved_pct.clamp(-99.9, 99.9),
                out.to_str().unwrap_or("<unknown>")
            ),
            None => eprintln!("{}:\t{:7.1}%", in_name, saved_pct.clamp(-99.9, 99.9)),
        }
    }
}

fn human_size(bytes: u64) -> (f64, &'static str) {
    const KB: u64 = 1024;
    const MB: u64 = 1024 * 1024;
    const GB: u64 = 1024 * 1024 * 1024;
    if bytes >= GB {
        (bytes as f64 / GB as f64, "GB")
    } else if bytes >= MB {
        (bytes as f64 / MB as f64, "MB")
    } else if bytes >= KB {
        (bytes as f64 / KB as f64, "KB")
    } else {
        (bytes as f64, "B")
    }
}