fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
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
// fsum — checksum and count the blocks in a file (GNU sum replacement)

use std::io::{self, BufRead, BufReader, Write};
use std::process;

const TOOL_NAME: &str = "sum";
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Clone, Copy, PartialEq)]
enum Algorithm {
    Bsd,
    SysV,
}

struct Cli {
    algorithm: Algorithm,
    files: Vec<String>,
}

fn parse_args() -> Cli {
    let mut cli = Cli {
        algorithm: Algorithm::Bsd,
        files: Vec::new(),
    };

    let mut args = std::env::args_os().skip(1);
    #[allow(clippy::while_let_on_iterator)]
    while let Some(arg) = args.next() {
        let bytes = arg.as_encoded_bytes();
        if bytes == b"--" {
            for f in args.by_ref() {
                cli.files.push(f.to_string_lossy().into_owned());
            }
            break;
        }
        if bytes.starts_with(b"--") {
            match bytes {
                b"--sysv" => cli.algorithm = Algorithm::SysV,
                b"--help" => {
                    print!(
                        "Usage: {} [OPTION]... [FILE]...\n\
                         Print checksum and block counts for each FILE.\n\n\
                         With no FILE, or when FILE is -, read standard input.\n\n\
                         \x20 -r              select BSD sum algorithm (default)\n\
                         \x20 -s, --sysv      select System V sum algorithm\n\
                         \x20     --help       display this help and exit\n\
                         \x20     --version    output version information and exit\n",
                        TOOL_NAME
                    );
                    process::exit(0);
                }
                b"--version" => {
                    println!("{} (fcoreutils) {}", TOOL_NAME, VERSION);
                    process::exit(0);
                }
                _ => {
                    eprintln!(
                        "{}: unrecognized option '{}'",
                        TOOL_NAME,
                        arg.to_string_lossy()
                    );
                    eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                    process::exit(1);
                }
            }
        } else if bytes.len() > 1 && bytes[0] == b'-' {
            for &b in &bytes[1..] {
                match b {
                    b's' => cli.algorithm = Algorithm::SysV,
                    b'r' => cli.algorithm = Algorithm::Bsd,
                    _ => {
                        eprintln!("{}: invalid option -- '{}'", TOOL_NAME, char::from(b));
                        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                        process::exit(1);
                    }
                }
            }
        } else {
            cli.files.push(arg.to_string_lossy().into_owned());
        }
    }

    if cli.files.is_empty() {
        cli.files.push("-".to_string());
    }

    cli
}

/// Single BSD checksum step: rotate right through carry, add byte, mask to 16 bits.
/// Serial: each step depends on the previous checksum value.
#[inline(always)]
fn bsd_step(checksum: u32, byte: u8) -> u32 {
    let rotated = (checksum >> 1) + ((checksum & 1) << 15);
    (rotated + u32::from(byte)) & 0xFFFF
}

/// Sum all bytes in a slice using 8-wide unrolled additions for auto-vectorization.
#[inline(always)]
fn sysv_sum_bytes(data: &[u8]) -> u64 {
    let mut sum: u64 = 0;
    let chunks = data.chunks_exact(8);
    let remainder = chunks.remainder();
    for chunk in chunks {
        sum += u64::from(chunk[0])
            + u64::from(chunk[1])
            + u64::from(chunk[2])
            + u64::from(chunk[3])
            + u64::from(chunk[4])
            + u64::from(chunk[5])
            + u64::from(chunk[6])
            + u64::from(chunk[7]);
    }
    for &byte in remainder {
        sum += u64::from(byte);
    }
    sum
}

/// Fold a 64-bit byte sum into a 16-bit SysV checksum.
#[inline(always)]
fn sysv_fold(sum: u64) -> u32 {
    let mut r = sum as u32;
    r = (r & 0xFFFF) + (r >> 16);
    r = (r & 0xFFFF) + (r >> 16);
    r
}

/// Compute a complete SysV checksum for a slice: sum bytes then fold.
#[inline(always)]
fn sysv_checksum(data: &[u8]) -> u32 {
    sysv_fold(sysv_sum_bytes(data))
}

/// Checksum a slice (used for all regular files via read_file).
fn process_slice(data: &[u8], algorithm: Algorithm) -> (u32, u64) {
    let total_bytes = data.len() as u64;
    match algorithm {
        Algorithm::Bsd => {
            let mut checksum: u32 = 0;
            for &byte in data {
                checksum = bsd_step(checksum, byte);
            }
            let blocks = total_bytes.div_ceil(1024);
            (checksum, blocks)
        }
        Algorithm::SysV => {
            let checksum = sysv_checksum(data);
            let blocks = total_bytes.div_ceil(512);
            (checksum, blocks)
        }
    }
}

/// Streaming checksum using BufReader with 8MB buffer (for stdin).
/// Unrolled inner loops enable auto-vectorization for higher throughput.
fn process_streaming<R: io::Read>(reader: R, algorithm: Algorithm) -> io::Result<(u32, u64)> {
    let mut reader = BufReader::with_capacity(8 * 1024 * 1024, reader);
    let mut total_bytes: u64 = 0;

    match algorithm {
        Algorithm::Bsd => {
            let mut checksum: u32 = 0;
            loop {
                let buf = reader.fill_buf()?;
                if buf.is_empty() {
                    break;
                }
                let n = buf.len();
                total_bytes += n as u64;
                for &byte in buf {
                    checksum = bsd_step(checksum, byte);
                }
                reader.consume(n);
            }
            let blocks = total_bytes.div_ceil(1024);
            Ok((checksum, blocks))
        }
        Algorithm::SysV => {
            let mut sum: u64 = 0;
            loop {
                let buf = reader.fill_buf()?;
                if buf.is_empty() {
                    break;
                }
                let n = buf.len();
                total_bytes += n as u64;
                sum += sysv_sum_bytes(buf);
                reader.consume(n);
            }
            let checksum = sysv_fold(sum);
            let blocks = total_bytes.div_ceil(512);
            Ok((checksum, blocks))
        }
    }
}

fn main() {
    coreutils_rs::common::reset_sigpipe();

    let cli = parse_args();
    let multiple = cli.files.len() > 1;
    let stdout = io::stdout();
    let mut out = io::BufWriter::with_capacity(256 * 1024, stdout.lock());
    let mut exit_code = 0;

    for filename in &cli.files {
        let result = if filename == "-" {
            process_streaming(io::stdin().lock(), cli.algorithm)
        } else {
            match coreutils_rs::common::io::read_file(std::path::Path::new(filename)) {
                Ok(data) => Ok(process_slice(&data, cli.algorithm)),
                Err(e) => {
                    eprintln!(
                        "{}: {}: {}",
                        TOOL_NAME,
                        filename,
                        coreutils_rs::common::io_error_msg(&e)
                    );
                    exit_code = 1;
                    continue;
                }
            }
        };

        let (checksum, blocks) = match result {
            Ok(v) => v,
            Err(e) => {
                let name = if filename == "-" {
                    "-"
                } else {
                    filename.as_str()
                };
                eprintln!(
                    "{}: {}: {}",
                    TOOL_NAME,
                    name,
                    coreutils_rs::common::io_error_msg(&e)
                );
                exit_code = 1;
                continue;
            }
        };

        let result = if cli.algorithm == Algorithm::Bsd {
            // GNU sum BSD mode: checksum zero-padded to 5 digits, blocks right-aligned to 5 chars
            if filename == "-" && !multiple {
                writeln!(out, "{:05} {:5}", checksum, blocks)
            } else if filename == "-" {
                writeln!(out, "{:05} {:5} -", checksum, blocks)
            } else {
                writeln!(out, "{:05} {:5} {}", checksum, blocks, filename)
            }
        } else {
            // GNU sum SysV mode: plain numbers
            if filename == "-" && !multiple {
                writeln!(out, "{} {}", checksum, blocks)
            } else if filename == "-" {
                writeln!(out, "{} {} -", checksum, blocks)
            } else {
                writeln!(out, "{} {} {}", checksum, blocks, filename)
            }
        };

        if let Err(e) = result {
            if e.kind() == io::ErrorKind::BrokenPipe {
                process::exit(0);
            }
            eprintln!("{}: write error: {}", TOOL_NAME, e);
            process::exit(1);
        }
    }

    if let Err(e) = out.flush()
        && e.kind() != io::ErrorKind::BrokenPipe
    {
        eprintln!("{}: write error: {}", TOOL_NAME, e);
        process::exit(1);
    }

    process::exit(exit_code);
}

#[cfg(test)]
mod tests {
    use std::io::Write;
    use std::process::Command;

    fn cmd() -> Command {
        let mut path = std::env::current_exe().unwrap();
        path.pop();
        path.pop();
        path.push("fsum");
        Command::new(path)
    }
    #[test]
    fn test_bsd_stdin() {
        // Test BSD checksum of "hello\n" via stdin
        let mut child = cmd()
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(b"hello\n").unwrap();
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        assert_eq!(parts.len(), 2, "stdin should have no filename");
        // Verify the checksum and blocks are numeric
        let _checksum: u32 = parts[0].parse().expect("checksum should be numeric");
        let _blocks: u64 = parts[1].parse().expect("blocks should be numeric");
    }

    #[test]
    fn test_sysv_stdin() {
        // Test System V checksum of "hello\n" via stdin
        let mut child = cmd()
            .arg("-s")
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(b"hello\n").unwrap();
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        assert_eq!(parts.len(), 2, "stdin should have no filename");
        let _checksum: u32 = parts[0].parse().expect("checksum should be numeric");
        let _blocks: u64 = parts[1].parse().expect("blocks should be numeric");
    }

    #[test]
    fn test_file() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"hello\n").unwrap();

        let output = cmd().arg(file_path.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        assert_eq!(parts.len(), 3, "file should include filename");
        assert!(parts[2].contains("test.txt"));
    }

    #[test]
    fn test_r_flag() {
        // -r selects BSD algorithm (default), should produce same result as no flag
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"hello\n").unwrap();

        let default_output = cmd().arg(file_path.to_str().unwrap()).output().unwrap();
        let r_output = cmd()
            .arg("-r")
            .arg(file_path.to_str().unwrap())
            .output()
            .unwrap();
        assert_eq!(default_output.stdout, r_output.stdout);
    }

    #[test]
    fn test_multiple_files() {
        let dir = tempfile::tempdir().unwrap();
        let file1 = dir.path().join("a.txt");
        let file2 = dir.path().join("b.txt");
        std::fs::write(&file1, b"hello\n").unwrap();
        std::fs::write(&file2, b"world\n").unwrap();

        let output = cmd()
            .arg(file1.to_str().unwrap())
            .arg(file2.to_str().unwrap())
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 2, "should output one line per file");
    }

    #[test]
    fn test_nonexistent_file() {
        let output = cmd().arg("/nonexistent/file.txt").output().unwrap();
        assert!(!output.status.success());
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("sum:"));
    }

    #[test]
    fn test_empty_file() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("empty.txt");
        std::fs::write(&file_path, b"").unwrap();

        let output = cmd().arg(file_path.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        let checksum: u32 = parts[0].parse().unwrap();
        let blocks: u64 = parts[1].parse().unwrap();
        assert_eq!(checksum, 0);
        assert_eq!(blocks, 0);
    }

    #[test]
    fn test_bsd_known_value() {
        // Known BSD checksum for "hello\n" (6 bytes):
        // After processing: checksum = 26988, blocks = 1
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"hello\n").unwrap();

        let output = cmd().arg(file_path.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        let blocks: u64 = parts[1].parse().unwrap();
        assert_eq!(blocks, 1);
    }

    #[test]
    fn test_sysv_known_value() {
        // System V checksum: simple sum of bytes mod 65535
        // "hello\n" = 104+101+108+108+111+10 = 542, blocks = ceil(6/512) = 1
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"hello\n").unwrap();

        let output = cmd()
            .arg("-s")
            .arg(file_path.to_str().unwrap())
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let parts: Vec<&str> = stdout.split_whitespace().collect();
        let checksum: u32 = parts[0].parse().unwrap();
        let blocks: u64 = parts[1].parse().unwrap();
        assert_eq!(checksum, 542);
        assert_eq!(blocks, 1);
    }

    /// Check if system sum is GNU sum (BSD sum on macOS has different output format,
    /// Windows Git Bash sum may have different filename behavior)
    fn is_gnu_sum() -> bool {
        // Only compare against GNU sum on Linux where behavior is consistent
        if !cfg!(target_os = "linux") {
            return false;
        }
        Command::new("sum")
            .arg("--version")
            .output()
            .map(|o| {
                let stdout = String::from_utf8_lossy(&o.stdout);
                let stderr = String::from_utf8_lossy(&o.stderr);
                stdout.contains("GNU") || stderr.contains("GNU")
            })
            .unwrap_or(false)
    }

    #[test]
    fn test_compare_gnu_bsd() {
        if !is_gnu_sum() {
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"The quick brown fox jumps over the lazy dog\n").unwrap();

        let gnu_out = Command::new("sum")
            .arg(file_path.to_str().unwrap())
            .output();
        if let Ok(gnu_out) = gnu_out {
            let ours = cmd().arg(file_path.to_str().unwrap()).output().unwrap();
            assert_eq!(
                String::from_utf8_lossy(&ours.stdout),
                String::from_utf8_lossy(&gnu_out.stdout),
                "BSD checksum mismatch with GNU sum"
            );
        }
    }

    #[test]
    fn test_compare_gnu_sysv() {
        if !is_gnu_sum() {
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"The quick brown fox jumps over the lazy dog\n").unwrap();

        let gnu_out = Command::new("sum")
            .arg("-s")
            .arg(file_path.to_str().unwrap())
            .output();
        if let Ok(gnu_out) = gnu_out {
            let ours = cmd()
                .arg("-s")
                .arg(file_path.to_str().unwrap())
                .output()
                .unwrap();
            assert_eq!(
                String::from_utf8_lossy(&ours.stdout),
                String::from_utf8_lossy(&gnu_out.stdout),
                "SysV checksum mismatch with GNU sum"
            );
        }
    }

    #[test]
    fn test_sysv_long_flag() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, b"hello\n").unwrap();

        let short_output = cmd()
            .arg("-s")
            .arg(file_path.to_str().unwrap())
            .output()
            .unwrap();
        let long_output = cmd()
            .arg("--sysv")
            .arg(file_path.to_str().unwrap())
            .output()
            .unwrap();
        assert_eq!(short_output.stdout, long_output.stdout);
    }

    #[test]
    fn test_sum_empty_file() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("empty.txt");
        std::fs::write(&file, "").unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
    }

    #[test]
    fn test_sum_multiple_files() {
        let dir = tempfile::tempdir().unwrap();
        let f1 = dir.path().join("a.txt");
        let f2 = dir.path().join("b.txt");
        std::fs::write(&f1, "hello\n").unwrap();
        std::fs::write(&f2, "world\n").unwrap();
        let output = cmd()
            .args([f1.to_str().unwrap(), f2.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert_eq!(stdout.lines().count(), 2);
    }

    #[test]
    fn test_sum_nonexistent_file() {
        let output = cmd().arg("/nonexistent_xyz_sum").output().unwrap();
        assert!(!output.status.success());
    }

    #[test]
    fn test_sum_binary_data() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("binary.bin");
        let data: Vec<u8> = (0..=255).collect();
        std::fs::write(&file, &data).unwrap();
        let output = cmd().arg(file.to_str().unwrap()).output().unwrap();
        assert!(output.status.success());
    }
}