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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! File, stdin, and directory I/O for compression.
//!
//! Entry points: `compress_file` and `compress_stdin`.
//! All compression logic is in `compression.rs`; this module only handles
//! filesystem concerns: file reading, output path selection, metadata
//! preservation, stats printing, and signal-handler registration.

use std::fs::File;
use std::io::{self, stdin, stdout, BufWriter, Cursor, 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 crate::cli::GzippyArgs;
use crate::compress::optimization::{detect_content_type, ContentType, OptimizationConfig};
use crate::compress::parallel::GzipHeaderInfo;
use crate::compress::simple::SimpleOptimizer;
use crate::error::{GzippyError, GzippyResult};
use crate::utils::{debug_enabled, preserve_metadata};

pub fn compress_file(filename: &str, args: &GzippyArgs) -> GzippyResult<i32> {
    if filename == "-" {
        return compress_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 {
            compress_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);
    }

    // Refuse to compress files that already carry the target suffix (e.g. foo.gz → foo.gz.gz).
    if !args.force && filename.ends_with(args.suffix.as_str()) {
        if !args.quiet {
            eprintln!(
                "gzippy: {}: already has {} suffix -- unchanged",
                filename, args.suffix
            );
        }
        return Ok(1);
    }
    #[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);
        }
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        if let Ok(metadata) = std::fs::metadata(input_path) {
            if metadata.nlink() > 1 && !args.force {
                if !args.quiet {
                    eprintln!(
                        "gzippy: {}: has {} other links -- skipping (use -f to force)",
                        filename,
                        metadata.nlink() - 1
                    );
                }
                return Ok(2);
            }
        }
    }

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

    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 input_file = File::open(input_path)?;
    let file_size = input_file.metadata()?.len();

    let content_type = if args.processes <= 1 || args.compression_level <= 3 {
        ContentType::Binary
    } else {
        let mut sample_file = File::open(input_path)?;
        detect_content_type(&mut sample_file).unwrap_or(ContentType::Binary)
    };

    let effective_level =
        if args.independent && args.compression_level >= 7 && args.compression_level <= 9 {
            6
        } else {
            args.compression_level
        };
    let opt_config =
        OptimizationConfig::new(args.processes, file_size, effective_level, content_type);

    if args.verbosity >= 2 {
        eprintln!(
            "gzippy: optimizing for {:?} content, {} threads, {}KB buffer, {:?} backend",
            content_type,
            opt_config.thread_count,
            opt_config.buffer_size / 1024,
            opt_config.backend
        );
    }

    let header_info = build_header_info(input_path, args);
    let use_mmap = opt_config.thread_count > 1 && file_size > 128 * 1024;

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

    let result = if args.rsyncable && use_mmap {
        if args.verbosity >= 2 {
            eprintln!("gzippy: using rsyncable compression");
        }
        let mmap = unsafe { memmap2::Mmap::map(&File::open(input_path)?)? };
        if args.stdout {
            crate::compress::parallel::compress_rsyncable(
                &mmap,
                args.compression_level as u32,
                opt_config.thread_count,
                &header_info,
                stdout(),
            )
            .map_err(|e| e.into())
        } else {
            let output_file = BufWriter::new(File::create(output_path.as_ref().unwrap())?);
            crate::compress::parallel::compress_rsyncable(
                &mmap,
                args.compression_level as u32,
                opt_config.thread_count,
                &header_info,
                output_file,
            )
            .map_err(|e| e.into())
        }
    } else if use_mmap && !args.use_zopfli() {
        if args.verbosity >= 2 {
            eprintln!(
                "gzippy: using mmap parallel backend with {} threads",
                opt_config.thread_count,
            );
        }
        let optimizer =
            SimpleOptimizer::new(opt_config.clone()).with_header_info(header_info.clone());
        if args.stdout {
            let out = BufWriter::with_capacity(1024 * 1024, stdout());
            optimizer
                .compress_file(input_path, out)
                .map_err(|e| e.into())
        } else {
            let output_file = BufWriter::new(File::create(output_path.as_ref().unwrap())?);
            optimizer
                .compress_file(input_path, output_file)
                .map_err(|e| e.into())
        }
    } else if args.stdout {
        let out = BufWriter::with_capacity(1024 * 1024, stdout());
        crate::compress::compress_with_pipeline(input_file, out, args, &opt_config, &header_info)
    } else {
        let output_file = BufWriter::new(File::create(output_path.as_ref().unwrap())?);
        crate::compress::compress_with_pipeline(
            input_file,
            output_file,
            args,
            &opt_config,
            &header_info,
        )
    };

    crate::set_output_file(None);

    match result {
        Ok(_) => {
            if !args.stdout {
                let output_path = get_output_filename(input_path, args);
                preserve_metadata(input_path, &output_path);
                if args.synchronous {
                    if let Ok(f) = File::open(&output_path) {
                        let _ = f.sync_all();
                    }
                }
            }
            if args.verbosity > 0 && !args.quiet && !args.stdout {
                let output_path = get_output_filename(input_path, args);
                if let Ok(metadata) = std::fs::metadata(&output_path) {
                    print_stats(file_size, metadata.len(), input_path, &output_path, 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);
                if cleanup_path.exists() {
                    let _ = std::fs::remove_file(&cleanup_path);
                }
            }
            Err(e)
        }
    }
}

pub fn compress_stdin(args: &GzippyArgs) -> GzippyResult<i32> {
    let can_parallelize = args.processes > 1;
    let verbose = args.verbose && !args.quiet;

    // T1 L0-L3 ISA-L streaming fast path: directly pipe stdin→stdout with ~2MB memory.
    if !can_parallelize
        && args.compression_level <= 3
        && !args.huffman
        && !args.rle
        && crate::backends::isal_compress::is_available()
    {
        let mut input = stdin();
        let mut counted = CountingWriter::new(BufWriter::with_capacity(1024 * 1024, stdout()));
        let compression_level = args.compression_level as u32;
        let in_bytes = if debug_enabled() {
            let t0 = std::time::Instant::now();
            let bytes = crate::backends::isal_compress::compress_gzip_stream_direct(
                &mut input,
                &mut counted,
                compression_level,
            )?;
            let elapsed = t0.elapsed();
            eprintln!(
                "[gzippy] compress T1 ISA-L L{} streaming: {:.1}ms, {:.1} MB/s ({} bytes in)",
                compression_level,
                elapsed.as_secs_f64() * 1000.0,
                bytes as f64 / elapsed.as_secs_f64() / 1_000_000.0,
                bytes
            );
            bytes
        } else {
            crate::backends::isal_compress::compress_gzip_stream_direct(
                &mut input,
                &mut counted,
                compression_level,
            )?
        };
        counted.flush()?;
        if verbose {
            print_stdin_stats(in_bytes, counted.count, args);
        }
        return Ok(0);
    }

    // Try to mmap stdin when it's a regular file (< file redirection).
    // For pipes, mmap_data stays None and we fall through to streaming.
    #[cfg(unix)]
    let mmap_data: Option<memmap2::Mmap> = if can_parallelize {
        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 { memmap2::Mmap::map(&meta) }.ok();
            if let Some(ref mmap) = m {
                let _ = mmap.advise(memmap2::Advice::Sequential);
            }
            m
        } else {
            None
        };
        std::mem::forget(meta);
        result
    } else {
        None
    };
    #[cfg(not(unix))]
    let mmap_data: Option<memmap2::Mmap> = None;

    let header_info = GzipHeaderInfo::default();
    let mut counted = CountingWriter::new(BufWriter::with_capacity(1024 * 1024, stdout()));

    let in_bytes = if let Some(ref mmap) = mmap_data {
        // Regular-file stdin (< file): multi-threaded parallel compression.
        let input_data = &mmap[..];
        let file_size = input_data.len() as u64;
        let content_type = if input_data.len() >= 8192 {
            crate::compress::optimization::analyze_content_type(&input_data[..8192])
        } else if !input_data.is_empty() {
            crate::compress::optimization::analyze_content_type(input_data)
        } else {
            ContentType::Binary
        };
        let opt_config = OptimizationConfig::new(
            args.processes,
            file_size,
            args.compression_level,
            content_type,
        );
        let compression_level = args.compression_level as u32;
        // L11 / zopfli tuning flags: always route through compress_with_pipeline
        // so the single-member zopfli encoder runs. The stdin+regular-file
        // multi-thread path was historically bypassing this — ParallelGzEncoder
        // would produce a "GZ" FEXTRA multi-member stream at L11, costing
        // +2% ratio vs C zopfli. Plan.md Phase 11.1.A pinned this for
        // compress_with_pipeline but didn't reach this branch.
        if opt_config.thread_count > 1 && !args.use_zopfli() {
            if args.compression_level >= 6 && args.compression_level <= 9 {
                let mut encoder = crate::compress::pipelined::PipelinedGzEncoder::new(
                    compression_level,
                    opt_config.thread_count,
                );
                encoder.set_header_info(header_info.clone());
                encoder.compress_buffer(input_data, &mut counted)?;
            } else {
                let mut encoder = crate::compress::parallel::ParallelGzEncoder::new(
                    compression_level,
                    opt_config.thread_count,
                );
                encoder.set_header_info(header_info.clone());
                encoder.compress_buffer(input_data, &mut counted)?;
            }
            counted.flush()?;
            if verbose {
                print_stdin_stats(file_size, counted.count, args);
            }
            return Ok(0);
        }
        // Single-threaded with mmap'd file: stream through compress_with_pipeline.
        let opt_config_t1 =
            OptimizationConfig::new(1, file_size, args.compression_level, content_type);
        crate::compress::compress_with_pipeline(
            Cursor::new(input_data),
            &mut counted,
            args,
            &opt_config_t1,
            &header_info,
        )?
    } else {
        // Pipe stdin: stream directly without buffering all input first.
        // Single-threaded so output begins immediately without OOM risk.
        let opt_config = OptimizationConfig::new(1, 0, args.compression_level, ContentType::Binary);
        crate::compress::compress_with_pipeline(
            stdin(),
            &mut counted,
            args,
            &opt_config,
            &header_info,
        )?
    };

    counted.flush()?;
    if verbose {
        print_stdin_stats(in_bytes, counted.count, args);
    }
    Ok(0)
}

fn print_stdin_stats(in_bytes: u64, out_bytes: u64, args: &GzippyArgs) {
    let ratio = if in_bytes > 0 {
        out_bytes as f64 / in_bytes as f64
    } else {
        1.0
    };
    let saved_pct = (1.0 - ratio) * 100.0;
    let (in_size, in_unit) = human_size(in_bytes);
    let (out_size, out_unit) = human_size(out_bytes);
    if args.processes > 1 {
        eprintln!(
            "(stdin): {:.1}{}{:.1}{} ({:.1}% saved, {} threads)",
            in_size, in_unit, out_size, out_unit, saved_pct, args.processes
        );
    } else {
        eprintln!(
            "(stdin): {:.1}{}{:.1}{} ({:.1}% saved)",
            in_size, in_unit, out_size, out_unit, saved_pct
        );
    }
}

fn compress_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() {
            let path_str = path.to_string_lossy();
            match compress_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)
}

fn get_output_filename(input_path: &Path, args: &GzippyArgs) -> std::path::PathBuf {
    let mut output_path = input_path.to_path_buf();
    let current_extension = output_path
        .extension()
        .unwrap_or_default()
        .to_str()
        .unwrap_or("");
    let new_extension = if current_extension.is_empty() {
        args.suffix.trim_start_matches('.').to_string()
    } else {
        format!("{}{}", current_extension, args.suffix)
    };
    output_path.set_extension(&new_extension);
    output_path
}

fn print_stats(
    input_size: u64,
    output_size: u64,
    input_path: &Path,
    output_path: &Path,
    args: &GzippyArgs,
) {
    let saved_pct = if input_size > 0 {
        (1.0_f64 - output_size as f64 / input_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);
        if args.processes > 1 {
            eprintln!(
                "{}: {:.1}{}{:.1}{} ({:.1}% saved, {} threads)",
                name, in_sz, in_u, out_sz, out_u, saved_pct, args.processes
            );
        } else {
            eprintln!(
                "{}: {:.1}{}{:.1}{} ({:.1}% saved)",
                name, in_sz, in_u, out_sz, out_u, saved_pct
            );
        }
    } else {
        // gzip-compatible format for -v: "path:   X.X% -- replaced with outpath"
        let in_name = input_path.to_str().unwrap_or("<unknown>");
        let out_name = output_path.to_str().unwrap_or("<unknown>");
        eprintln!(
            "{}:\t{:7.1}% -- replaced with {}",
            in_name,
            saved_pct.clamp(-99.9, 99.9),
            out_name
        );
    }
}

pub(crate) fn build_header_info(path: &Path, args: &GzippyArgs) -> GzipHeaderInfo {
    let filename = if !args.no_name {
        path.file_name()
            .and_then(|n| n.to_str())
            .map(|s| s.to_string())
    } else {
        None
    };
    let mtime = if !args.no_time {
        std::fs::metadata(path)
            .ok()
            .and_then(|m| m.modified().ok())
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_secs() as u32)
            .unwrap_or(0)
    } else {
        0
    };
    GzipHeaderInfo {
        filename,
        mtime,
        comment: args.comment.clone(),
    }
}

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")
    }
}