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
//! util.c: the parts of zstd's utilities the family needs: the core
//! counts (R3), stat queries, human-readable sizes, `--filelist`, `-r`'s
//! expansion and `--output-dir-mirror`'s directories.

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

use super::messages::{self as m, printf};
use crate::compat::host::{self, Cores, Host, strerror};

/// zstd's own use of the machine's core counts ([`Cores`], moved to
/// `host.rs` for the lz4 family too, R4 of 0.15.0).
impl Cores {
    /// zstdcli.c: `ZSTDCLI_NBTHREADS_DEFAULT`, `MAX(1, MIN(4,
    /// UTIL_countLogicalCores() / 4))`.
    pub(crate) fn default_workers(self) -> u32 {
        (self.logical / 4).clamp(1, 4)
    }
}

// ---- util.c: files and names ----

/// `UTIL_FILESIZE_UNKNOWN`.
pub(crate) const FILESIZE_UNKNOWN: u64 = u64::MAX;

/// util.c: `MAX_FILE_OF_FILE_NAMES_SIZE`, the largest `--filelist` file.
const MAX_FILE_OF_FILE_NAMES_SIZE: u64 = 50 << 20;

/// util.c: `DIR_DEFAULT_MODE`.
#[cfg(unix)]
const DIR_DEFAULT_MODE: u32 = 0o755;

/// platform.h: `PATH_SEP`.
#[cfg(windows)]
pub(crate) const PATH_SEP: u8 = b'\\';
/// platform.h: `PATH_SEP`.
#[cfg(not(windows))]
pub(crate) const PATH_SEP: u8 = b'/';

/// `UTIL_stat`: `stat(2)`, following a final symbolic link.
pub(crate) fn stat(host: &Host, name: &[u8]) -> io::Result<Metadata> {
    fs::metadata(host.path(name))
}

/// `UTIL_isRegularFile`.
pub(crate) fn is_regular_file(host: &Host, name: &[u8]) -> bool {
    stat(host, name).is_ok_and(|m| m.is_file())
}

/// `UTIL_isDirectory`.
pub(crate) fn is_directory(host: &Host, name: &[u8]) -> bool {
    stat(host, name).is_ok_and(|m| m.is_dir())
}

/// `UTIL_isFIFOStat`.
pub(crate) fn is_fifo_stat(m: &Metadata) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::FileTypeExt;
        m.file_type().is_fifo()
    }
    #[cfg(not(unix))]
    {
        let _ = m;
        false
    }
}

/// `UTIL_isBlockDevStat`.
pub(crate) fn is_block_dev_stat(m: &Metadata) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::FileTypeExt;
        m.file_type().is_block_device()
    }
    #[cfg(not(unix))]
    {
        let _ = m;
        false
    }
}

/// `UTIL_isLink`: `lstat(2)` says a symbolic link (never on Windows, as
/// util.c).
pub(crate) fn is_link(host: &Host, name: &[u8]) -> bool {
    cfg!(unix) && fs::symlink_metadata(host.path(name)).is_ok_and(|m| m.file_type().is_symlink())
}

/// `UTIL_isFIFO`.
pub(crate) fn is_fifo(host: &Host, name: &[u8]) -> bool {
    stat(host, name).is_ok_and(|m| is_fifo_stat(&m))
}

/// `UTIL_getFileSize`: a regular file's size (following links), else
/// [`FILESIZE_UNKNOWN`].
pub(crate) fn file_size(host: &Host, name: &[u8]) -> u64 {
    stat(host, name).map_or(FILESIZE_UNKNOWN, |m| file_size_stat(&m))
}

/// `UTIL_getFileSizeStat`.
pub(crate) fn file_size_stat(m: &Metadata) -> u64 {
    if m.is_file() {
        m.len()
    } else {
        FILESIZE_UNKNOWN
    }
}

/// `UTIL_isSameFileStat`: the same device and inode (the same name, off
/// unix).
pub(crate) fn is_same_file_stat(n1: &[u8], n2: &[u8], m1: &Metadata, m2: &Metadata) -> bool {
    host::same_file(m1, m2).unwrap_or(n1 == n2)
}

/// `UTIL_isSameFile`.
pub(crate) fn is_same_file(host: &Host, n1: &[u8], n2: &[u8]) -> bool {
    match (stat(host, n1), stat(host, n2)) {
        (Ok(a), Ok(b)) => is_same_file_stat(n1, n2, &a, &b),
        _ => false,
    }
}

/// `UTIL_getFileExtension`: from the last `.`, or empty when there is none
/// or it starts the name.
pub(crate) fn file_extension(name: &[u8]) -> &[u8] {
    match name.iter().rposition(|&b| b == b'.') {
        Some(at) if at > 0 => &name[at..],
        _ => b"",
    }
}

/// `UTIL_isCompressedFile`.
pub(crate) fn is_compressed_file(name: &[u8], extensions: &[&str]) -> bool {
    let ext = file_extension(name);
    extensions.iter().any(|e| e.as_bytes() == ext)
}

/// `UTIL_HumanReadableSize_t`: a size scaled for display, printed with
/// `%.*f%s` (`precision`, `value`, `suffix`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Hrs {
    pub value: f64,
    pub suffix: &'static str,
    pub precision: usize,
}

impl Hrs {
    /// `%.*f%s`.
    pub(crate) fn plain(&self) -> String {
        format!("{:.*}{}", self.precision, self.value, self.suffix)
    }

    /// `%<width>.*f`, the value alone.
    pub(crate) fn value_w(&self, width: usize) -> String {
        format!("{:>width$.*}", self.precision, self.value)
    }
}

/// `UTIL_makeHumanReadableSize`, at `g_utilDisplayLevel` `util_level`.
pub(crate) fn human_size(size: u64, util_level: i32) -> Hrs {
    if util_level > 3 {
        if size >= 1 << 53 {
            return Hrs {
                value: size as f64 / (1u64 << 20) as f64,
                suffix: " MiB",
                precision: 2,
            };
        }
        return Hrs {
            value: size as f64,
            suffix: " B",
            precision: 0,
        };
    }
    let (value, suffix) = if size >= 1 << 60 {
        (size as f64 / (1u64 << 60) as f64, " EiB")
    } else if size >= 1 << 50 {
        (size as f64 / (1u64 << 50) as f64, " PiB")
    } else if size >= 1 << 40 {
        (size as f64 / (1u64 << 40) as f64, " TiB")
    } else if size >= 1 << 30 {
        (size as f64 / (1u64 << 30) as f64, " GiB")
    } else if size >= 1 << 20 {
        (size as f64 / (1u64 << 20) as f64, " MiB")
    } else if size >= 1 << 10 {
        (size as f64 / (1u64 << 10) as f64, " KiB")
    } else {
        (size as f64, " B")
    };
    let precision = if value >= 100.0 || value as u64 == size {
        0
    } else if value >= 10.0 {
        1
    } else if value > 1.0 {
        2
    } else {
        3
    };
    Hrs {
        value,
        suffix,
        precision,
    }
}

/// `UTIL_DISPLAYLEVEL`: `fmt` on standard error at `g_utilDisplayLevel`
/// `level` or above.
fn util_display(util_level: i32, level: i32, err: &mut dyn Write, fmt: &str, args: &[&[u8]]) {
    if util_level >= level {
        printf(err, fmt, args);
    }
}

/// `UTIL_createFileNamesTable_fromFileName` (`--filelist`): the names in
/// `name`, one per line, or `None` when it is not a regular file, is larger
/// than 50 MiB, cannot be read (said with `perror`'s text) or names nothing.
/// Lines are read as `fgets` reads them: a line ends at `\n`, which is
/// dropped; a name ends at its first NUL; a line whose name is empty before
/// any `\n` (it starts with a NUL) ends the list.
pub(crate) fn names_from_file(
    host: &Host,
    name: &[u8],
    util_level: i32,
    err: &mut dyn Write,
) -> Option<Vec<Vec<u8>>> {
    let meta = stat(host, name).ok().filter(|m| m.is_file())?;
    if file_size_stat(&meta) > MAX_FILE_OF_FILE_NAMES_SIZE {
        return None;
    }
    // Through `Host::track`, so the fuzz target counts the list's read.
    let path = host.path(name);
    let read = fs::File::open(&path).and_then(|f| {
        let mut f = host.track(&path, f);
        let mut data = Vec::new();
        f.read_to_end(&mut data).map(|_| data)
    });
    let data = match read {
        Ok(d) => d,
        Err(e) => {
            util_display(util_level, 1, err, m::PERROR_READ_LINES, &[&strerror(&e)]);
            return None;
        }
    };
    let mut names = Vec::new();
    for line in data.split_inclusive(|&b| b == b'\n') {
        let line = line.split(|&b| b == 0).next().unwrap_or_default();
        if line.is_empty() {
            break;
        }
        names.push(line.strip_suffix(b"\n").unwrap_or(line).to_vec());
    }
    (!names.is_empty()).then_some(names)
}

/// `UTIL_expandFNT` (`-r`): each directory replaced by the files under it,
/// in `readdir` order, depth first; every other name kept.
pub(crate) fn expand_names(
    host: &Host,
    names: Vec<Vec<u8>>,
    follow_links: bool,
    util_level: i32,
    err: &mut dyn Write,
) -> Vec<Vec<u8>> {
    let mut out = Vec::new();
    for name in names {
        if is_directory(host, &name) {
            prepare_file_list(host, &name, &mut out, follow_links, util_level, err);
        } else {
            out.push(name);
        }
    }
    out
}

/// util.c (POSIX): `UTIL_prepareFileList`.
fn prepare_file_list(
    host: &Host,
    dir: &[u8],
    out: &mut Vec<Vec<u8>>,
    follow_links: bool,
    util_level: i32,
    err: &mut dyn Write,
) {
    let entries = match fs::read_dir(host.path(dir)) {
        Ok(e) => e,
        Err(e) => {
            util_display(
                util_level,
                1,
                err,
                m::CANNOT_OPEN_DIR,
                &[dir, &strerror(&e)],
            );
            return;
        }
    };
    for entry in entries {
        let entry = match entry {
            Ok(e) => e,
            Err(e) => {
                util_display(util_level, 1, err, m::READDIR_ERROR, &[dir, &strerror(&e)]);
                return;
            }
        };
        let mut path = dir.to_vec();
        path.push(b'/');
        path.extend_from_slice(&os_bytes(&entry.file_name()));
        if !follow_links && is_link(host, &path) {
            util_display(util_level, 2, err, m::UTIL_SYMLINK_IGNORED, &[&path]);
            continue;
        }
        if is_directory(host, &path) {
            prepare_file_list(host, &path, out, follow_links, util_level, err);
        } else {
            out.push(path);
        }
    }
}

/// A name's bytes.
fn os_bytes(s: &std::ffi::OsStr) -> Vec<u8> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        s.as_bytes().to_vec()
    }
    #[cfg(not(unix))]
    {
        s.to_string_lossy().into_owned().into_bytes()
    }
}

// ---- util.c: --output-dir-mirror ----

/// util.c: pathnameHas2Dots: a whole `..` path component.
fn has_two_dots(p: &[u8]) -> bool {
    p.split(|&b| b == PATH_SEP).any(|c| c == b"..")
}

/// util.c: trimLeadingCurrentDirConst.
fn trim_current_dir(p: &[u8]) -> &[u8] {
    match p {
        [b'.', s, rest @ ..] if *s == PATH_SEP => rest,
        _ => p,
    }
}

/// util.c: trimPath: a leading `./`, then a leading `/`.
fn trim_path(p: &[u8]) -> &[u8] {
    let p = trim_current_dir(p);
    match p.first() {
        Some(&b) if b == PATH_SEP => &p[1..],
        _ => p,
    }
}

/// util.c: mallocAndJoin2Dir.
fn join_2_dir(d1: &[u8], d2: &[u8]) -> Vec<u8> {
    let mut v = d1.to_vec();
    if !d1.is_empty() && d1.last() != Some(&PATH_SEP) {
        v.push(PATH_SEP);
    }
    v.extend_from_slice(d2);
    v
}

/// util.c: convertPathnameToDirName, as the C code does it: it looks for
/// trailing separators only at `pathname[len]` (the terminator), so none
/// are ever removed; then everything from the last separator is cut, and a
/// name without one becomes `.`.
fn dir_name(p: &[u8]) -> Vec<u8> {
    if p.is_empty() {
        return Vec::new();
    }
    match p.iter().rposition(|&b| b == PATH_SEP) {
        Some(at) => p[..at].to_vec(),
        None => b".".to_vec(),
    }
}

/// `UTIL_createMirroredDestDirName`: the directory under `root` where
/// `src`'s output goes, or `None` for a name with a `..` component.
pub(crate) fn mirrored_dest_dir(src: &[u8], root: &[u8]) -> Option<Vec<u8>> {
    if has_two_dots(src) {
        return None;
    }
    Some(dir_name(&join_2_dir(root, trim_path(src))))
}

/// `UTIL_mirrorSourceFilesDirectories`: create `root` and, below it, each
/// input's directory with the source directory's mode.
pub(crate) fn mirror_source_dirs(host: &Host, names: &[Vec<u8>], root: &[u8], err: &mut dyn Write) {
    let mut dirs: Vec<Vec<u8>> = names
        .iter()
        .filter(|n| !has_two_dots(n))
        .map(|n| dir_name(n))
        .collect();
    if dirs.is_empty() {
        return;
    }
    let _ = make_dir(host, root, None, err);
    // makeUniqueMirroredDestDirs: sorted by the trimmed path; a directory
    // that is the parent of (or the same as) the next is skipped, the
    // deepest of a chain kept.
    dirs.sort_by(|a, b| trim_path(a).cmp(trim_path(b)));
    let mut unique: Vec<Vec<u8>> = Vec::new();
    for (i, d) in dirs.iter().enumerate() {
        if i == 0 {
            unique.push(d.clone());
            continue;
        }
        let prev = trim_path(&dirs[i - 1]);
        let cur = trim_path(d);
        let parent = prev.len() <= cur.len()
            && (cur.get(prev.len()).is_none_or(|&b| b == PATH_SEP))
            && cur.starts_with(prev);
        if parent {
            *unique.last_mut().expect("one pushed") = d.clone();
        } else {
            unique.push(d.clone());
        }
    }
    for d in unique {
        mirror_src_dir_recursive(host, &d, root, err);
    }
}

/// util.c: mirrorSrcDirRecursive: each prefix of `src` ending before a
/// separator, then `src` itself, mirrored under `root`.
fn mirror_src_dir_recursive(host: &Host, src: &[u8], root: &[u8], err: &mut dyn Write) {
    let skip = src.len() - trim_current_dir(src).len();
    let mut at = skip;
    while let Some(off) = src[at..].iter().position(|&b| b == PATH_SEP) {
        let sp = at + off;
        if sp != at && mirror_src_dir(host, &src[..sp], root, err).is_err() {
            return;
        }
        at = sp + 1;
    }
    let _ = mirror_src_dir(host, src, root, err);
}

/// util.c: mirrorSrcDir.
fn mirror_src_dir(host: &Host, src: &[u8], root: &[u8], err: &mut dyn Write) -> Result<(), ()> {
    let new_dir = join_2_dir(root, trim_path(src));
    let mode = dir_mode(host, src, err);
    make_dir(host, &new_dir, mode, err)
}

/// util.c: getDirMode: the directory's mode, else 0755 with a message.
fn dir_mode(host: &Host, dir: &[u8], err: &mut dyn Write) -> Option<u32> {
    match stat(host, dir) {
        Err(e) => {
            printf(err, m::DIR_STAT_FAILED, &[dir, &strerror(&e)]);
            None
        }
        Ok(m) if !m.is_dir() => {
            printf(err, m::EXPECTED_DIRECTORY, &[dir]);
            None
        }
        Ok(m) => Some(mode_of(&m)),
    }
}

fn mode_of(m: &Metadata) -> u32 {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        m.permissions().mode()
    }
    #[cfg(not(unix))]
    {
        let _ = m;
        0o755
    }
}

/// util.c: makeDir: `mkdir(dir, mode)` (`None`: `DIR_DEFAULT_MODE`), an
/// existing name being success.
fn make_dir(host: &Host, dir: &[u8], mode: Option<u32>, err: &mut dyn Write) -> Result<(), ()> {
    #[cfg(unix)]
    let b = {
        use std::os::unix::fs::DirBuilderExt;
        let mut b = fs::DirBuilder::new();
        b.mode(mode.unwrap_or(DIR_DEFAULT_MODE));
        b
    };
    #[cfg(not(unix))]
    let b = {
        let _ = mode;
        fs::DirBuilder::new()
    };
    match b.create(host.path(dir)) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
        Err(e) => {
            printf(err, m::MKDIR_FAILED, &[dir, &strerror(&e)]);
            Err(())
        }
    }
}

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

    #[test]
    fn the_default_worker_count_is_a_quarter_of_the_logical_cores_within_1_to_4() {
        let w = |logical| {
            Cores {
                logical,
                physical: 1,
            }
            .default_workers()
        };
        assert_eq!([w(1), w(4), w(8), w(12), w(16), w(64)], [1, 1, 2, 3, 4, 4]);
        assert_eq!(Cores::SANDBOX.default_workers(), 2);
    }

    #[test]
    fn human_sizes_scale_as_util_c_scales_them() {
        // `%.*f%s`, and `%6.*f` (the value right-aligned in six columns).
        let h = |n, l| {
            let h = human_size(n, l);
            let w = h.value_w(6);
            assert!(w.len() >= 6 && w.trim_start() == format!("{:.*}", h.precision, h.value));
            h.plain()
        };
        assert_eq!(h(0, 2), "0 B");
        assert_eq!(h(1023, 2), "1023 B");
        assert_eq!(h(1760, 2), "1.72 KiB");
        // Exactly 1 is not above 1: three decimals.
        assert_eq!(h(1024, 2), "1.000 KiB");
        assert_eq!(h(20 << 20, 2), "20.0 MiB");
        assert_eq!(h((20 << 20) + 1, 2), "20.0 MiB");
        assert_eq!(h(150 << 10, 2), "150 KiB");
        assert_eq!(h(1760, 4), "1760 B");
        assert_eq!(h(1 << 53, 4), "8589934592.00 MiB");
    }

    #[test]
    fn a_file_list_is_read_as_fgets_reads_it() {
        let dir = std::env::temp_dir().join(format!("stuffr-zstd-util-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        let host = Host::Real;
        let list = |content: &[u8]| {
            let p = dir.join("list");
            fs::write(&p, content).unwrap();
            let mut err = Vec::new();
            let r = names_from_file(&host, p.to_str().unwrap().as_bytes(), 2, &mut err);
            assert!(err.is_empty());
            r
        };
        let v = |names: &[&str]| Some(names.iter().map(|n| n.as_bytes().to_vec()).collect());
        assert_eq!(list(b"a\nb\n"), v(&["a", "b"]));
        assert_eq!(list(b"a\nb"), v(&["a", "b"]));
        assert_eq!(list(b"a\n\nb\n"), v(&["a", "", "b"]));
        assert_eq!(list(b"a\r\n"), v(&["a\r"]));
        assert_eq!(list(b"a\0x\nb\n"), v(&["a", "b"]));
        assert_eq!(list(b"a\n\0b\nc\n"), v(&["a"]));
        assert_eq!(list(b""), None);
        let mut err = Vec::new();
        assert_eq!(
            names_from_file(&host, dir.to_str().unwrap().as_bytes(), 2, &mut err),
            None
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn mirrored_names_keep_the_source_directories() {
        let m = |src: &str, root: &str| {
            mirrored_dest_dir(src.as_bytes(), root.as_bytes())
                .map(|v| String::from_utf8(v).unwrap())
        };
        assert_eq!(m("a", "o").as_deref(), Some("o"));
        assert_eq!(m("d/a", "o").as_deref(), Some("o/d"));
        assert_eq!(m("./d/a", "o/").as_deref(), Some("o/d"));
        assert_eq!(m("/abs/d/a", "o").as_deref(), Some("o/abs/d"));
        assert_eq!(m("d/../a", "o"), None);
        assert_eq!(m("..", "o"), None);
        assert_eq!(m("d/..x/a", "o").as_deref(), Some("o/d/..x"));
    }
}