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
#[cfg(not(unix))]
fn main() {
    eprintln!("mknod: only available on Unix");
    std::process::exit(1);
}

// fmknod — make block or character special files, or FIFOs
//
// Usage: mknod [OPTION]... NAME TYPE [MAJOR MINOR]

#[cfg(unix)]
use std::ffi::CString;
#[cfg(unix)]
use std::process;

#[cfg(unix)]
const TOOL_NAME: &str = "mknod";
#[cfg(unix)]
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(unix)]
fn main() {
    coreutils_rs::common::reset_sigpipe();

    let mut mode: Option<String> = None;
    let mut positional: Vec<String> = Vec::new();
    let mut saw_dashdash = false;

    let mut args = std::env::args().skip(1).peekable();
    while let Some(arg) = args.next() {
        if saw_dashdash {
            positional.push(arg);
            continue;
        }
        match arg.as_str() {
            "--help" => {
                print_help();
                return;
            }
            "--version" => {
                println!("{} (fcoreutils) {}", TOOL_NAME, VERSION);
                return;
            }
            "--" => saw_dashdash = true,
            s if s.starts_with("--mode=") => {
                let val = &s["--mode=".len()..];
                mode = Some(val.to_string());
            }
            "--mode" | "-m" => {
                if let Some(val) = args.next() {
                    mode = Some(val);
                } else {
                    eprintln!("{}: option '{}' requires an argument", TOOL_NAME, arg);
                    process::exit(1);
                }
            }
            s if s.starts_with('-') && s.len() > 1 && !s.starts_with("--") => {
                let rest = &s[1..];
                if let Some(stripped) = rest.strip_prefix('m') {
                    // -mMODE or -m MODE
                    if stripped.is_empty() {
                        if let Some(val) = args.next() {
                            mode = Some(val);
                        } else {
                            eprintln!("{}: option requires an argument -- 'm'", TOOL_NAME);
                            process::exit(1);
                        }
                    } else {
                        mode = Some(stripped.to_string());
                    }
                } else {
                    eprintln!("{}: invalid option -- '{}'", TOOL_NAME, &rest[..1]);
                    eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                    process::exit(1);
                }
            }
            _ => positional.push(arg),
        }
    }

    if positional.is_empty() {
        eprintln!("{}: missing operand", TOOL_NAME);
        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
        process::exit(1);
    }

    if positional.len() < 2 {
        eprintln!("{}: missing operand after '{}'", TOOL_NAME, positional[0]);
        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
        process::exit(1);
    }

    let name = &positional[0];
    let node_type = &positional[1];

    match node_type.as_str() {
        "p" => {
            // FIFO: no major/minor allowed
            if positional.len() > 2 {
                eprintln!(
                    "{}: '{}: Superfluous argument '{}'",
                    TOOL_NAME, name, positional[2]
                );
                process::exit(1);
            }
            create_fifo(name, &mode);
        }
        "b" | "c" | "u" => {
            // Block or character special: need major and minor
            if positional.len() < 4 {
                eprintln!(
                    "{}: missing operand after '{}'",
                    TOOL_NAME,
                    positional.last().unwrap()
                );
                eprintln!("Special files require major and minor device numbers.");
                process::exit(1);
            }
            if positional.len() > 4 {
                eprintln!("{}: too many arguments after major/minor", TOOL_NAME);
                process::exit(1);
            }
            let major = parse_device_number(&positional[2], "major");
            let minor = parse_device_number(&positional[3], "minor");
            create_special(name, node_type, major, minor, &mode);
        }
        _ => {
            eprintln!("{}: invalid device type '{}'", TOOL_NAME, node_type);
            eprintln!("Try '{} --help' for more information.", TOOL_NAME);
            process::exit(1);
        }
    }
}

#[cfg(unix)]
fn parse_device_number(s: &str, label: &str) -> u64 {
    // Support hex (0x), octal (0), and decimal
    let val = if let Some(hex) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
        u64::from_str_radix(hex, 16)
    } else if s.starts_with('0') && s.len() > 1 {
        u64::from_str_radix(&s[1..], 8)
    } else {
        s.parse::<u64>()
    };

    val.unwrap_or_else(|_| {
        eprintln!("{}: invalid {} device number '{}'", TOOL_NAME, label, s);
        process::exit(1);
    })
}

#[cfg(unix)]
fn parse_mode_str(mode_str: &str) -> libc::mode_t {
    if let Ok(m) = libc::mode_t::from_str_radix(mode_str, 8) {
        return m;
    }
    // GNU mknod uses 0666 (a=rw) as the base for symbolic mode parsing
    match coreutils_rs::chmod::parse_mode_no_umask(mode_str, 0o666) {
        Ok(m) => m as libc::mode_t,
        Err(_) => {
            eprintln!("{}: invalid mode: '{}'", TOOL_NAME, mode_str);
            process::exit(1);
        }
    }
}

#[cfg(unix)]
fn create_fifo(name: &str, mode: &Option<String>) {
    let (file_mode, explicit_mode) = match mode {
        Some(m) => (parse_mode_str(m), true),
        None => (0o666, false),
    };

    let c_name = match CString::new(name) {
        Ok(c) => c,
        Err(_) => {
            eprintln!("{}: invalid name '{}'", TOOL_NAME, name);
            process::exit(1);
        }
    };

    let saved = if explicit_mode {
        Some(unsafe { libc::umask(0) })
    } else {
        None
    };
    let ret = unsafe { libc::mkfifo(c_name.as_ptr(), file_mode) };
    if let Some(old) = saved {
        unsafe {
            libc::umask(old);
        }
    }

    if ret != 0 {
        let e = std::io::Error::last_os_error();
        eprintln!(
            "{}: {}: {}",
            TOOL_NAME,
            name,
            coreutils_rs::common::io_error_msg(&e)
        );
        process::exit(1);
    }
}

#[cfg(unix)]
fn create_special(name: &str, node_type: &str, major: u64, minor: u64, mode: &Option<String>) {
    let (file_mode, explicit_mode) = match mode {
        Some(m) => (parse_mode_str(m), true),
        None => (0o666, false),
    };

    let type_flag: libc::mode_t = match node_type {
        "b" => libc::S_IFBLK,
        "c" | "u" => libc::S_IFCHR,
        _ => unreachable!(),
    };

    #[cfg(target_vendor = "apple")]
    let dev = libc::makedev(major as i32, minor as i32);
    #[cfg(not(target_vendor = "apple"))]
    let dev = libc::makedev(major as libc::c_uint, minor as libc::c_uint);

    let c_name = match CString::new(name) {
        Ok(c) => c,
        Err(_) => {
            eprintln!("{}: invalid name '{}'", TOOL_NAME, name);
            process::exit(1);
        }
    };

    let saved = if explicit_mode {
        Some(unsafe { libc::umask(0) })
    } else {
        None
    };
    let ret = unsafe { libc::mknod(c_name.as_ptr(), file_mode | type_flag, dev) };
    if let Some(old) = saved {
        unsafe {
            libc::umask(old);
        }
    }

    if ret != 0 {
        let e = std::io::Error::last_os_error();
        eprintln!(
            "{}: {}: {}",
            TOOL_NAME,
            name,
            coreutils_rs::common::io_error_msg(&e)
        );
        process::exit(1);
    }
}

#[cfg(unix)]
fn print_help() {
    println!("Usage: {} [OPTION]... NAME TYPE [MAJOR MINOR]", TOOL_NAME);
    println!("Create the special file NAME of the given TYPE.");
    println!();
    println!("Mandatory arguments to long options are mandatory for short options too.");
    println!("  -m, --mode=MODE    set file permission bits to MODE, not a=rw - umask");
    println!("      --help         display this help and exit");
    println!("      --version      output version information and exit");
    println!();
    println!("Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they");
    println!("must be omitted when TYPE is p.  If MAJOR or MINOR begins with 0x or 0X,");
    println!("it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;");
    println!("otherwise, as decimal.  TYPE may be:");
    println!();
    println!("  b      create a block (buffered) special file");
    println!("  c, u   create a character (unbuffered) special file");
    println!("  p      create a FIFO");
}

#[cfg(all(test, unix))]
mod tests {
    use std::process::Command;

    fn cmd() -> Command {
        let mut path = std::env::current_exe().unwrap();
        path.pop();
        path.pop();
        path.push("fmknod");
        Command::new(path)
    }
    #[test]
    fn test_create_fifo() {
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("testfifo");
        let output = cmd().args([fifo.to_str().unwrap(), "p"]).output().unwrap();
        assert_eq!(output.status.code(), Some(0));

        #[cfg(unix)]
        {
            use std::os::unix::fs::FileTypeExt;
            let meta = std::fs::symlink_metadata(&fifo).unwrap();
            assert!(meta.file_type().is_fifo(), "should be a FIFO");
        }
    }

    #[test]
    fn test_create_fifo_with_mode() {
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("modefifo");
        let output = cmd()
            .args(["-m", "0644", fifo.to_str().unwrap(), "p"])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let meta = std::fs::symlink_metadata(&fifo).unwrap();
            let mode = meta.permissions().mode() & 0o777;
            assert_eq!(mode, 0o644, "mode should be 0644, got {:o}", mode);
        }
    }

    #[test]
    fn test_invalid_type() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("badtype");
        let output = cmd().args([path.to_str().unwrap(), "x"]).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("invalid device type"),
            "should report invalid type: {}",
            stderr
        );
    }

    #[test]
    fn test_missing_major_minor_for_block() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("blockdev");
        let output = cmd().args([path.to_str().unwrap(), "b"]).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("missing operand") || stderr.contains("major and minor"),
            "should report missing major/minor: {}",
            stderr
        );
    }

    #[test]
    fn test_missing_major_minor_for_char() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("chardev");
        let output = cmd().args([path.to_str().unwrap(), "c"]).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
    }

    #[test]
    fn test_fifo_extra_args() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("fifoextra");
        let output = cmd()
            .args([path.to_str().unwrap(), "p", "1", "2"])
            .output()
            .unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("Superfluous"),
            "should report superfluous arg: {}",
            stderr
        );
    }

    #[test]
    fn test_missing_operand() {
        let output = cmd().output().unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("missing operand"),
            "should report missing operand: {}",
            stderr
        );
    }

    #[test]
    fn test_missing_type() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("notype");
        let output = cmd().arg(path.to_str().unwrap()).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("missing operand"));
    }

    #[test]
    fn test_fifo_existing_fails() {
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("existfifo");
        // Create it first
        cmd().args([fifo.to_str().unwrap(), "p"]).output().unwrap();
        // Try again
        let output = cmd().args([fifo.to_str().unwrap(), "p"]).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
    }

    #[test]
    fn test_mode_flag_long() {
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("modelong");
        let output = cmd()
            .args(["--mode=0600", fifo.to_str().unwrap(), "p"])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let meta = std::fs::symlink_metadata(&fifo).unwrap();
            let mode = meta.permissions().mode() & 0o777;
            assert_eq!(mode, 0o600, "mode should be 0600, got {:o}", mode);
        }
    }

    #[test]
    fn test_matches_gnu_fifo() {
        let dir = tempfile::tempdir().unwrap();
        let gnu_path = dir.path().join("gnu_fifo");
        let our_path = dir.path().join("our_fifo");

        let gnu = Command::new("mknod")
            .args([gnu_path.to_str().unwrap(), "p"])
            .output();
        if let Ok(gnu_out) = gnu {
            let ours = cmd()
                .args([our_path.to_str().unwrap(), "p"])
                .output()
                .unwrap();
            assert_eq!(
                ours.status.code(),
                gnu_out.status.code(),
                "Exit code mismatch"
            );
        }
    }

    #[test]
    fn test_matches_gnu_invalid_type() {
        let dir = tempfile::tempdir().unwrap();
        let gnu_path = dir.path().join("gnu_bad");
        let our_path = dir.path().join("our_bad");

        let gnu = Command::new("mknod")
            .args([gnu_path.to_str().unwrap(), "z"])
            .output();
        if let Ok(gnu_out) = gnu {
            let ours = cmd()
                .args([our_path.to_str().unwrap(), "z"])
                .output()
                .unwrap();
            assert_eq!(
                ours.status.code(),
                gnu_out.status.code(),
                "Exit code mismatch on invalid type"
            );
        }
    }
}