stuffr-cli 0.18.2

The stuffr command line interface
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! `main` after its option loop, `comprexx`, `compdir`, `prratio`,
//! `read_error`, `write_error` and `abort_compress`, from ncompress 5.0's
//! `compress.c` (the Unlicense, `LICENSE-ncompress`) with Ubuntu's 5.0-1
//! build: `stat` where upstream's `-DLSTAT` build calls `lstat`
//! (`fix-softlinks.patch`), so symbolic links are followed. Each in the
//! order the C makes its calls, since the order decides which message a
//! failure prints.
//!
//! The C's I/O is `read(2)` and `write(2)` on descriptors, so the data
//! here goes through one read and one flushed write per C call
//! (`lzw::read_once`, `lzw::put`); `printf` text (`-r`'s `unreadable`) is
//! stdio's, held until the run ends when standard output is not a
//! terminal. `abort_compress()` exits the process: here it ends the run
//! ([`Abort`]) after removing the armed output.

use std::fs::{self, File, Metadata};
use std::io::{self, Read, Write};
use std::path::PathBuf;

use super::args::Settings;
use super::lzw::{self, Corrupt, Counts, DecTables, EncTables, End, Fail};
use super::messages as m;
use crate::compat::host::{self, CleanupGuard, Host};

/// `abort_compress()` was called: the run is over, exit 1.
struct Abort;

/// The run's state: `compress.c`'s globals and the process around them.
struct Run<'h, 'e> {
    host: &'h mut Host,
    err: &'e mut dyn Write,
    s: Settings,
    /// `fgnd_flag`: SIGINT was not ignored at start.
    fgnd: bool,
    /// `exit_code`: -1 until something is processed, last writer wins.
    exit_code: i32,
    /// `ifname`: the input's name, empty for standard input.
    ifname: Vec<u8>,
    /// `ofname`, while an output is known.
    ofname: Option<Vec<u8>>,
    /// `remove_ofname`: the output a failure removes, armed for the
    /// signal handler too.
    armed: Option<CleanupGuard>,
    /// `inbuf`, shared by both coders and the overwrite question.
    inbuf: Vec<u8>,
    enc: Option<EncTables>,
    dec: Option<DecTables>,
    /// `printf` to standard output (stdio, flushed at exit).
    stdio: Vec<u8>,
}

/// `main` from its file list on: every operand through `comprexx`, or
/// standard input to standard output. The exit code.
pub(super) fn execute(s: Settings, host: &mut Host, err: &mut dyn Write) -> u8 {
    let fgnd = host::install_abort_exit(host);
    let mut r = Run {
        host,
        err,
        s,
        fgnd,
        exit_code: -1,
        ifname: Vec::new(),
        ofname: None,
        armed: None,
        inbuf: vec![0; lzw::INBUF_ALLOC],
        enc: None,
        dec: None,
        stdio: Vec::new(),
    };
    let code = match r.main() {
        Ok(()) => {
            if r.s.recursive && r.exit_code == -1 {
                let _ = r.err.write_all(m::NO_FILES_RECURSIVE.as_bytes());
            }
            if r.exit_code == -1 {
                1
            } else {
                r.exit_code as u8
            }
        }
        Err(Abort) => 1,
    };
    r.flush_stdio();
    code
}

/// `prratio`: `num / den` as a percentage with two decimals, in the C's
/// fixed point (`q` an `int`).
fn prratio(num: i64, den: i64) -> Vec<u8> {
    let mut q: i32 = if den > 0 {
        if num > 214_748 {
            (num / (den / 10000)) as i32
        } else {
            (10000 * num / den) as i32
        }
    } else {
        10000
    };
    let mut s = Vec::new();
    if q < 0 {
        s.push(b'-');
        q = q.wrapping_neg();
    }
    s.extend_from_slice(format!("{}.{:02}%", q / 100, q % 100).as_bytes());
    s
}

/// `(p = name + len - 2) == ".Z"`, for a name of two bytes or more.
fn has_z_suffix(name: &[u8]) -> bool {
    name.len() >= 2 && name.ends_with(b".Z")
}

impl Run<'_, '_> {
    fn path(&self, b: &[u8]) -> PathBuf {
        self.host.path(b)
    }

    fn say(&mut self, fmt: &str, args: &[&[u8]]) {
        m::printf(self.err, fmt, args);
    }

    /// `perror(s)` for `e`.
    fn perror(&mut self, s: &[u8], e: &io::Error) {
        let text = host::strerror(e);
        self.perror_text(s, &text);
    }

    /// `perror(s)` with `strerror`'s `text`: an empty `s` prints the text
    /// alone, as `perror` leaves out the colon then.
    fn perror_text(&mut self, s: &[u8], text: &[u8]) {
        if s.is_empty() {
            self.say(m::PERROR_BARE, &[text]);
        } else {
            self.say(m::PERROR, &[s, text]);
        }
    }

    /// Hand `printf`'s pending text to standard output.
    fn flush_stdio(&mut self) {
        if !self.stdio.is_empty() {
            let mut w = self.host.out_writer(None);
            let _ = w.write_all(&std::mem::take(&mut self.stdio));
            let _ = w.flush();
        }
    }

    /// `abort_compress()`: the armed output removed, the run over.
    fn abort(&mut self) -> Abort {
        if self.armed.take().is_some()
            && let Some(o) = &self.ofname
        {
            let _ = fs::remove_file(self.path(o));
        }
        Abort
    }

    /// A coder's failure: `read_error()`, `write_error()` or the corrupt
    /// input lines, then `abort_compress()`.
    fn fail(&mut self, f: Fail) -> Abort {
        match f {
            Fail::Read(e) => {
                let _ = self.err.write_all(m::READ_ERROR.as_bytes());
                let who = if self.ifname.is_empty() {
                    b"stdin".to_vec()
                } else {
                    self.ifname.clone()
                };
                self.perror(&who, &e);
            }
            Fail::Write(e) => {
                let _ = self.err.write_all(m::WRITE_ERROR.as_bytes());
                let who = self.ofname.clone().unwrap_or_else(|| b"stdout".to_vec());
                self.perror(&who, &e);
            }
            Fail::Corrupt(c) => {
                match c {
                    Corrupt::FirstCode(code) => {
                        let code = code.to_string();
                        self.say(m::FIRST_CODE, &[code.as_bytes()]);
                    }
                    Corrupt::Code {
                        insize,
                        posbits,
                        bytes,
                    } => {
                        let n: Vec<String> = [insize.to_string(), posbits.to_string()]
                            .into_iter()
                            .chain(bytes.iter().map(|b| format!("{b:02X}")))
                            .chain([(posbits & 7).to_string()])
                            .collect();
                        let args: Vec<&[u8]> = n.iter().map(|s| s.as_bytes()).collect();
                        self.say(m::BAD_CODE, &args);
                    }
                    Corrupt::Chain => {}
                }
                let _ = self.err.write_all(m::CORRUPT_INPUT.as_bytes());
            }
        }
        self.abort()
    }

    /// `main`'s operand loop or its standard-input branch.
    fn main(&mut self) -> Result<(), Abort> {
        let files = std::mem::take(&mut self.s.files);
        if !files.is_empty() {
            for f in &files {
                self.comprexx(f)?;
            }
            return Ok(());
        }
        self.ifname.clear();
        self.exit_code = 0;
        let mut input = self.stdin_reader();
        let mut out = self.host.out_writer(None);
        if !self.s.decomp {
            let c = self.compress(&mut *input, &mut out)?;
            if !self.s.zcat && !self.s.quiet {
                let pct = prratio(c.bytes_in - c.bytes_out, c.bytes_in);
                let _ = self.err.write_all(m::STDIN_COMPRESSION.as_bytes());
                let _ = self.err.write_all(&pct);
                let _ = self.err.write_all(m::NEWLINE.as_bytes());
            }
            if c.bytes_out >= c.bytes_in && !self.s.force {
                self.exit_code = 2;
            }
        } else {
            self.decompress(&mut *input, &mut out)?;
        }
        Ok(())
    }

    /// File descriptor 0, read with `read(2)`: the real one, or a
    /// sandbox's bytes.
    fn stdin_reader(&mut self) -> Box<dyn Read> {
        match self.host {
            Host::Real => match host::stdin_file() {
                Ok(f) => Box::new(f),
                Err(_) => Box::new(io::stdin()),
            },
            Host::Sandbox(_) => Box::new(self.host.stdin()),
        }
    }

    fn compress(&mut self, input: &mut dyn Read, out: &mut dyn Write) -> Result<Counts, Abort> {
        let t = self.enc.get_or_insert_with(EncTables::new);
        let r = lzw::compress(input, out, &mut self.inbuf, i64::from(self.s.maxbits), t);
        r.map_err(|f| self.fail(f))
    }

    /// `decompress()` with its two messages: how it ended and `bytes_in`.
    fn decompress(
        &mut self,
        input: &mut dyn Read,
        out: &mut dyn Write,
    ) -> Result<(End, i64), Abort> {
        let t = self.dec.get_or_insert_with(DecTables::new);
        let (end, bytes_in) =
            lzw::decompress(input, out, &mut self.inbuf, t).map_err(|f| self.fail(f))?;
        let who = if self.ifname.is_empty() {
            b"stdin".to_vec()
        } else {
            self.ifname.clone()
        };
        match end {
            End::NotCompressed => {
                self.say(m::NOT_COMPRESSED, &[&who]);
                self.exit_code = 1;
            }
            End::TooManyBits(bits) => {
                let bits = bits.to_string();
                self.say(m::TOO_MANY_BITS, &[&who, bits.as_bytes(), b"16"]);
                self.exit_code = 4;
            }
            End::Done | End::Empty => {}
        }
        Ok((end, bytes_in))
    }

    /// `comprexx(fileptr)`: one operand.
    fn comprexx(&mut self, fileptr: &[u8]) -> Result<(), Abort> {
        match self.comprexx_inner(fileptr) {
            Ok(Some(())) => Ok(()),
            // `error:`
            Ok(None) => {
                self.ofname = None;
                self.armed = None;
                self.exit_code = 1;
                Ok(())
            }
            Err(a) => Err(a),
        }
    }

    /// `comprexx` up to its `error:` label: `Ok(None)` is a jump there.
    fn comprexx_inner(&mut self, fileptr: &[u8]) -> Result<Option<()>, Abort> {
        let mut tempname = fileptr.to_vec();
        let mut has_z = has_z_suffix(&tempname);
        // Ubuntu's build: `lstat` is `stat`.
        let infstat = match fs::metadata(self.path(&tempname)) {
            Ok(st) => st,
            Err(e) => {
                if self.s.decomp && e.kind() == io::ErrorKind::NotFound && !has_z {
                    // The name without `.Z`: try it with.
                    tempname.extend_from_slice(b".Z");
                    has_z = true;
                    match fs::metadata(self.path(&tempname)) {
                        Ok(st) if st.is_file() => st,
                        Ok(_) => {
                            self.say(m::NOT_REGULAR, &[&tempname]);
                            return Ok(None);
                        }
                        Err(e) => {
                            self.perror(&tempname, &e);
                            return Ok(None);
                        }
                    }
                } else {
                    self.perror(&tempname, &e);
                    return Ok(None);
                }
            }
        };

        if infstat.is_dir() {
            if self.s.recursive {
                self.compdir(&tempname)?;
            } else if !self.s.quiet {
                self.say(m::IS_DIRECTORY, &[&tempname]);
            }
            return Ok(Some(()));
        }
        if !infstat.is_file() {
            self.say(m::NOT_DIR_OR_REGULAR, &[&tempname]);
            return Ok(Some(()));
        }

        let ofname = if self.s.decomp {
            if !self.s.zcat && !has_z {
                if self.s.recursive {
                    return Ok(Some(()));
                }
                if !self.s.quiet {
                    self.say(m::NO_Z_SUFFIX, &[&tempname]);
                }
                return Ok(None);
            }
            let mut o = tempname.clone();
            if has_z {
                o.truncate(o.len() - 2);
            }
            o
        } else {
            if !self.s.zcat {
                if has_z {
                    if !self.s.recursive {
                        self.say(m::HAS_Z_SUFFIX, &[&tempname]);
                    }
                    return Ok(Some(()));
                }
                let others = nlink(&infstat).saturating_sub(1);
                if others > 0 && !self.s.force {
                    let n = others.to_string();
                    self.say(m::OTHER_LINKS, &[&tempname, n.as_bytes()]);
                    return Ok(None);
                }
            }
            [&tempname[..], b".Z"].concat()
        };
        self.ofname = Some(ofname.clone());

        let in_path = self.path(&tempname);
        let fdin = match File::open(&in_path) {
            Ok(f) => f,
            Err(e) => {
                self.perror(&tempname, &e);
                return Ok(None);
            }
        };
        let mut fdin = self.host.track(&in_path, fdin);
        self.ifname = tempname.clone();

        let out_path = self.path(&ofname);
        let mut out = if !self.s.zcat {
            // `access(ofname, F_OK)`: follows a link.
            if fs::metadata(&out_path).is_ok() {
                if !self.s.force {
                    self.inbuf[0] = b'n';
                    self.say(m::ALREADY_EXISTS, &[&ofname]);
                    if self.fgnd && self.host.stdin_is_tty() {
                        self.say(m::OVERWRITE, &[&ofname]);
                        let _ = self.err.flush();
                        self.read_answer();
                    }
                    if self.inbuf[0] != b'y' {
                        self.say(m::NOT_OVERWRITTEN, &[&ofname]);
                        return Ok(None);
                    }
                }
                if let Err(e) = fs::remove_file(&out_path) {
                    let _ = self.err.write_all(m::CANT_REMOVE.as_bytes());
                    self.perror(&ofname, &e);
                    return Ok(None);
                }
            }
            let file = match host::create_excl_armed(self.host, &out_path) {
                Ok((f, guard)) => {
                    self.armed = Some(guard);
                    f
                }
                Err(e) => {
                    self.perror(&tempname, &e);
                    return Ok(None);
                }
            };
            if !self.s.quiet {
                self.say(m::NAME, &[&tempname]);
            }
            self.host.out_writer(Some(file))
        } else {
            self.host.out_writer(None)
        };

        let (bytes_in, bytes_out, end) = if !self.s.decomp {
            let c = self.compress(&mut fdin, &mut out)?;
            (c.bytes_in, c.bytes_out, End::Done)
        } else {
            let (end, bytes_in) = self.decompress(&mut fdin, &mut out)?;
            (bytes_in, 0, end)
        };
        drop(fdin);
        if let Err(e) = out.flush() {
            return Err(self.fail(Fail::Write(e)));
        }
        drop(out);

        let refused = matches!(end, End::NotCompressed | End::TooManyBits(_));
        if bytes_in == 0 && !self.s.force {
            if self.armed.take().is_some() {
                if !self.s.quiet {
                    self.say(m::NO_COMPRESSION, &[&tempname]);
                }
                if let Err(e) = fs::remove_file(&out_path) {
                    let _ = self.err.write_all(m::UNLINK_ERROR_NL.as_bytes());
                    self.perror(&ofname, &e);
                }
                self.exit_code = 2;
            }
        } else if !self.s.zcat && self.s.decomp && refused {
            // Deviation N4: ncompress takes the refused input for a
            // success here, removing it and keeping the empty output.
            // stuffr removes its empty output and keeps the input.
            self.armed = None;
            let _ = fs::remove_file(&out_path);
        } else if !self.s.zcat {
            if !self.s.decomp && bytes_out >= bytes_in && !self.s.force {
                // No compression: remove file.Z.
                if !self.s.quiet {
                    self.say(m::NO_COMPRESSION, &[&tempname]);
                }
                if let Err(e) = fs::remove_file(&out_path) {
                    let _ = self.err.write_all(m::UNLINK_ERROR.as_bytes());
                    self.perror(&ofname, &e);
                }
                self.armed = None;
                self.exit_code = 2;
            } else {
                if !self.s.quiet {
                    self.say(m::REPLACED_WITH, &[&ofname]);
                    if !self.s.decomp {
                        let pct = prratio(bytes_in - bytes_out, bytes_in);
                        let _ = self.err.write_all(m::COMPRESSION.as_bytes());
                        let _ = self.err.write_all(&pct);
                    }
                    let _ = self.err.write_all(m::NEWLINE.as_bytes());
                }
                self.copy_stat(&ofname, &infstat);
                self.armed = None;
                if !self.s.keep
                    && let Err(e) = fs::remove_file(&in_path)
                {
                    let _ = self.err.write_all(m::UNLINK_ERROR_NL.as_bytes());
                    self.perror(&tempname, &e);
                }
            }
        }
        if self.exit_code == -1 {
            self.exit_code = 0;
        }
        self.armed = None;
        self.ofname = None;
        Ok(Some(()))
    }

    /// The question's answer: one byte from descriptor 0 into `inbuf[0]`,
    /// then bytes into `inbuf[1]` until a newline (none after a newline
    /// first). A failed or empty read is `perror("stdin")`.
    fn read_answer(&mut self) {
        let mut b = [0u8; 1];
        match read0(&mut b) {
            Ok(1) => {
                self.inbuf[0] = b[0];
                if b[0] != b'\n' {
                    loop {
                        match read0(&mut b) {
                            Ok(1) => {
                                self.inbuf[1] = b[0];
                                if b[0] == b'\n' {
                                    break;
                                }
                            }
                            r => {
                                self.perror_read(r);
                                break;
                            }
                        }
                    }
                }
            }
            r => self.perror_read(r),
        }
    }

    /// `perror("stdin")` after a read that gave nothing: the error, or for
    /// an end of input `errno`'s value, which nothing has set (0).
    fn perror_read(&mut self, r: io::Result<usize>) {
        let text = match r {
            Err(e) => host::strerror(&e),
            Ok(_) => host::errno_text(0),
        };
        self.perror_text(b"stdin", &text);
    }

    /// `utime`, `chmod` (`st_mode & 07777`), `chown`, each by name, each
    /// failure reported and ignored.
    fn copy_stat(&mut self, ofname: &[u8], st: &Metadata) {
        #[cfg(unix)]
        {
            use std::os::unix::ffi::OsStrExt;
            use std::os::unix::fs::MetadataExt;
            let path = self.path(ofname);
            let Ok(c) = std::ffi::CString::new(path.as_os_str().as_bytes()) else {
                return;
            };
            let last = || io::Error::last_os_error();
            let times = libc::utimbuf {
                actime: st.atime() as libc::time_t,
                modtime: st.mtime() as libc::time_t,
            };
            // SAFETY: `c` is a NUL-terminated path and `times` a utimbuf.
            if unsafe { libc::utime(c.as_ptr(), &times) } != 0 {
                let e = last();
                let _ = self.err.write_all(m::UTIME_ERROR.as_bytes());
                self.perror(ofname, &e);
            }
            // SAFETY: as above.
            if unsafe { libc::chmod(c.as_ptr(), (st.mode() & 0o7777) as libc::mode_t) } != 0 {
                let e = last();
                let _ = self.err.write_all(m::CHMOD_ERROR.as_bytes());
                self.perror(ofname, &e);
            }
            // SAFETY: as above.
            if unsafe { libc::chown(c.as_ptr(), st.uid(), st.gid()) } != 0 {
                let e = last();
                let _ = self.err.write_all(m::CHOWN_ERROR.as_bytes());
                self.perror(ofname, &e);
            }
        }
        #[cfg(not(unix))]
        {
            let _ = (ofname, st);
        }
    }

    /// `compdir(dir)`: every entry of `dir` (`.` and `..` left out),
    /// `dir/name`, through `comprexx`, in `readdir` order. A directory that
    /// cannot be opened is `printf`'d to standard output.
    fn compdir(&mut self, dir: &[u8]) -> Result<(), Abort> {
        let entries = match fs::read_dir(self.path(dir)) {
            Ok(rd) => rd,
            Err(_) => {
                let mut line = Vec::new();
                m::printf(&mut line, m::UNREADABLE, &[dir]);
                self.stdio.extend_from_slice(&line);
                if self.host.stdout_is_tty() {
                    // Line-buffered at a terminal.
                    self.flush_stdio();
                }
                return Ok(());
            }
        };
        for e in entries {
            let Ok(e) = e else { break };
            let name = [dir, b"/", e.file_name().as_encoded_bytes()].concat();
            self.comprexx(&name)?;
        }
        Ok(())
    }
}

/// `read(0, buf, 1)` on the process's standard input.
fn read0(b: &mut [u8; 1]) -> io::Result<usize> {
    #[cfg(unix)]
    {
        // SAFETY: reads at most one byte into `b`, which we own.
        let n = unsafe { libc::read(0, b.as_mut_ptr().cast(), 1) };
        if n < 0 {
            Err(io::Error::last_os_error())
        } else {
            Ok(n as usize)
        }
    }
    #[cfg(not(unix))]
    {
        io::stdin().read(b)
    }
}

/// `st_nlink`.
fn nlink(st: &Metadata) -> u64 {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        st.nlink()
    }
    #[cfg(not(unix))]
    {
        let _ = st;
        1
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// `prratio` in the C's fixed point: two decimals, the sign first, the
    /// large-number branch, and 100% for an empty input.
    #[test]
    fn prratio_is_fixed_point() {
        assert_eq!(prratio(50_000 - 21_313, 50_000), b"57.37%");
        assert_eq!(prratio(1 - 5, 1), b"-400.00%");
        assert_eq!(prratio(4096 - 5632, 4096), b"-37.50%");
        assert_eq!(prratio(0, 0), b"100.00%");
        assert_eq!(prratio(-3, 0), b"100.00%");
        assert_eq!(prratio(1_000_000, 2_000_000), b"50.00%");
    }
}