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

// fmktemp — create a temporary file or directory
//
// Usage: mktemp [OPTION]... [TEMPLATE]

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

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

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

    let mut make_dir = false;
    let mut dry_run = false;
    let mut quiet = false;
    let mut use_tmpdir: Option<Option<String>> = None; // None=not set, Some(None)=set w/o value, Some(Some(d))=set with value
    let mut suffix: Option<String> = None;
    let mut use_t_flag = false;
    let mut template: Option<String> = None;
    let mut saw_dashdash = false;

    let mut args = std::env::args().skip(1).peekable();
    while let Some(arg) = args.next() {
        if saw_dashdash {
            if template.is_some() {
                eprintln!("{}: too many templates", TOOL_NAME);
                eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                process::exit(1);
            }
            template = Some(arg);
            continue;
        }
        match arg.as_str() {
            "--help" => {
                print_help();
                return;
            }
            "--version" => {
                println!("{} (fcoreutils) {}", TOOL_NAME, VERSION);
                return;
            }
            "--" => saw_dashdash = true,
            "-d" | "--directory" => make_dir = true,
            "-u" | "--dry-run" => dry_run = true,
            "-q" | "--quiet" => quiet = true,
            "-t" => use_t_flag = true,
            s if s.starts_with("--tmpdir=") => {
                let val = &s["--tmpdir=".len()..];
                use_tmpdir = Some(Some(val.to_string()));
            }
            "--tmpdir" => {
                // --tmpdir without = means use TMPDIR or /tmp
                use_tmpdir = Some(None);
            }
            s if s.starts_with("--suffix=") => {
                let val = &s["--suffix=".len()..];
                suffix = Some(val.to_string());
            }
            "--suffix" => {
                if let Some(val) = args.next() {
                    suffix = Some(val);
                } else {
                    eprintln!("{}: option '--suffix' requires an argument", TOOL_NAME);
                    process::exit(1);
                }
            }
            s if s.starts_with("-p") => {
                let rest = &s[2..];
                if rest.is_empty() {
                    if let Some(val) = args.next() {
                        use_tmpdir = Some(Some(val));
                    } else {
                        eprintln!("{}: option requires an argument -- 'p'", TOOL_NAME);
                        process::exit(1);
                    }
                } else {
                    use_tmpdir = Some(Some(rest.to_string()));
                }
            }
            s if s.starts_with('-') && s.len() > 1 && !s.starts_with("--") => {
                // Parse combined short flags like -duq
                for ch in s[1..].chars() {
                    match ch {
                        'd' => make_dir = true,
                        'u' => dry_run = true,
                        'q' => quiet = true,
                        't' => use_t_flag = true,
                        _ => {
                            eprintln!("{}: invalid option -- '{}'", TOOL_NAME, ch);
                            eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                            process::exit(1);
                        }
                    }
                }
            }
            _ => {
                if template.is_some() {
                    eprintln!("{}: too many templates", TOOL_NAME);
                    eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                    process::exit(1);
                }
                template = Some(arg);
            }
        }
    }

    // Determine the effective template and directory
    let default_template = "tmp.XXXXXXXXXX";
    let tmpl = template.unwrap_or_else(|| default_template.to_string());

    // Determine base directory
    let tmpdir_env = std::env::var("TMPDIR").ok();
    let base_dir = if use_t_flag {
        // -t: interpret template as a filename component; prepend TMPDIR (or -p dir, or /tmp)
        if let Some(ref dir_opt) = use_tmpdir {
            match dir_opt {
                Some(d) => d.clone(),
                None => tmpdir_env.unwrap_or_else(|| "/tmp".to_string()),
            }
        } else {
            tmpdir_env.unwrap_or_else(|| "/tmp".to_string())
        }
    } else if let Some(ref dir_opt) = use_tmpdir {
        match dir_opt {
            Some(d) => d.clone(),
            None => tmpdir_env.unwrap_or_else(|| "/tmp".to_string()),
        }
    } else if !tmpl.contains('/') {
        // No directory separator in template and no -p/--tmpdir: use TMPDIR or /tmp
        tmpdir_env.unwrap_or_else(|| "/tmp".to_string())
    } else {
        // Template contains a path; use it as-is (base_dir not needed)
        String::new()
    };

    // Build the full template path
    let full_template = if !base_dir.is_empty() {
        format!("{}/{}", base_dir, tmpl)
    } else {
        tmpl.clone()
    };

    // Append suffix if given
    let full_template = if let Some(ref suf) = suffix {
        format!("{}{}", full_template, suf)
    } else {
        full_template
    };

    // Validate template: must have at least 3 consecutive X's before suffix
    let (prefix, x_count, suf_part) = parse_template(&full_template, &suffix);
    if x_count < 3 {
        eprintln!("{}: too few X's in template '{}'", TOOL_NAME, full_template);
        process::exit(1);
    }

    match create_temp(&prefix, x_count, &suf_part, make_dir, dry_run, quiet) {
        Ok(path) => {
            println!("{}", path);
        }
        Err(msg) => {
            if !quiet {
                eprintln!("{}: {}", TOOL_NAME, msg);
            }
            process::exit(1);
        }
    }
}

/// Parse a template into (prefix, x_count, suffix).
/// The X's are the trailing X's before the suffix.
#[cfg(unix)]
fn parse_template(template: &str, suffix: &Option<String>) -> (String, usize, String) {
    let suf_len = suffix.as_ref().map_or(0, |s| s.len());
    let base = &template[..template.len() - suf_len];
    let suf_part = &template[template.len() - suf_len..];

    // Count trailing X's in base
    let x_count = base.chars().rev().take_while(|&c| c == 'X').count();
    let prefix = &base[..base.len() - x_count];

    (prefix.to_string(), x_count, suf_part.to_string())
}

#[cfg(unix)]
fn generate_random_name(x_count: usize) -> String {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    let mut buf = vec![0u8; x_count];

    // Use getrandom via /dev/urandom for randomness
    let fd = unsafe { libc::open(c"/dev/urandom".as_ptr(), libc::O_RDONLY) };
    if fd >= 0 {
        unsafe {
            libc::read(fd, buf.as_mut_ptr().cast::<libc::c_void>(), x_count);
            libc::close(fd);
        }
    } else {
        // Fallback: use time + pid based seed
        let seed = unsafe {
            let mut tv: libc::timeval = std::mem::zeroed();
            libc::gettimeofday(&mut tv, std::ptr::null_mut());
            (tv.tv_sec as u64)
                .wrapping_mul(1_000_000)
                .wrapping_add(tv.tv_usec as u64)
                .wrapping_add(libc::getpid() as u64)
        };
        let mut state = seed;
        for byte in &mut buf {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            *byte = (state >> 33) as u8;
        }
    }

    buf.iter()
        .map(|&b| CHARSET[(b as usize) % CHARSET.len()] as char)
        .collect()
}

#[cfg(unix)]
fn create_temp(
    prefix: &str,
    x_count: usize,
    suffix: &str,
    make_dir: bool,
    dry_run: bool,
    quiet: bool,
) -> Result<String, String> {
    // Try up to 100 times to avoid collisions
    for _ in 0..100 {
        let random_part = generate_random_name(x_count);
        let path = format!("{}{}{}", prefix, random_part, suffix);

        if dry_run {
            return Ok(path);
        }

        if make_dir {
            match std::fs::create_dir(&path) {
                Ok(()) => {
                    // Set permissions to 0700 for directories (private)
                    #[cfg(unix)]
                    {
                        let c_path =
                            std::ffi::CString::new(path.as_str()).map_err(|e| e.to_string())?;
                        unsafe {
                            libc::chmod(c_path.as_ptr(), 0o700);
                        }
                    }
                    return Ok(path);
                }
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
                Err(e) => {
                    let msg = format!(
                        "failed to create directory via template '{}{}{}': {}",
                        prefix,
                        "X".repeat(x_count),
                        suffix,
                        coreutils_rs::common::io_error_msg(&e)
                    );
                    if !quiet {
                        return Err(msg);
                    }
                    return Err(msg);
                }
            }
        } else {
            // Create file exclusively
            use std::fs::OpenOptions;
            match OpenOptions::new().write(true).create_new(true).open(&path) {
                Ok(_file) => {
                    // Set permissions to 0600 for files (private)
                    #[cfg(unix)]
                    {
                        let c_path =
                            std::ffi::CString::new(path.as_str()).map_err(|e| e.to_string())?;
                        unsafe {
                            libc::chmod(c_path.as_ptr(), 0o600);
                        }
                    }
                    return Ok(path);
                }
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
                Err(e) => {
                    let msg = format!(
                        "failed to create file via template '{}{}{}': {}",
                        prefix,
                        "X".repeat(x_count),
                        suffix,
                        coreutils_rs::common::io_error_msg(&e)
                    );
                    return Err(msg);
                }
            }
        }
    }

    Err(format!(
        "failed to create {} via template '{}{}{}': too many retries",
        if make_dir { "directory" } else { "file" },
        prefix,
        "X".repeat(x_count),
        suffix
    ))
}

#[cfg(unix)]
fn print_help() {
    println!("Usage: {} [OPTION]... [TEMPLATE]", TOOL_NAME);
    println!("Create a temporary file or directory, safely, and print its name.");
    println!("TEMPLATE must contain at least 3 consecutive 'X's in last component.");
    println!("If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.");
    println!();
    println!("  -d, --directory     create a directory, not a file");
    println!("  -u, --dry-run       do not create anything; merely print a name (unsafe)");
    println!("  -q, --quiet         suppress diagnostics about file/dir-creation failure");
    println!("  -p DIR, --tmpdir[=DIR]  interpret TEMPLATE relative to DIR; if DIR is not");
    println!("                      specified, use $TMPDIR if set, else /tmp.  With this option,");
    println!("                      TEMPLATE must not be an absolute pathname; unlike with -t,");
    println!("                      TEMPLATE may contain slashes, but mktemp creates only the");
    println!("                      final component");
    println!("  -t                  interpret TEMPLATE as a single file name component,");
    println!("                      relative to a directory: $TMPDIR, if set; else the");
    println!("                      directory specified via -p; else /tmp [deprecated]");
    println!("      --suffix=SUFF   append SUFF to TEMPLATE; SUFF must not contain a slash.");
    println!("                      This option is implied if TEMPLATE does not end in X");
    println!("      --help          display this help and exit");
    println!("      --version       output version information and exit");
}

#[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("fmktemp");
        Command::new(path)
    }
    #[test]
    fn test_default_template() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-p", dir.path().to_str().unwrap()])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        assert!(
            path.starts_with(dir.path().to_str().unwrap()),
            "path should start with tmpdir: {}",
            path
        );
        assert!(
            std::path::Path::new(path).exists(),
            "created file should exist"
        );
        // Default template is tmp.XXXXXXXXXX so filename should start with tmp.
        let filename = std::path::Path::new(path)
            .file_name()
            .unwrap()
            .to_str()
            .unwrap();
        assert!(
            filename.starts_with("tmp."),
            "default filename should start with 'tmp.': {}",
            filename
        );
        assert_eq!(
            filename.len(),
            "tmp.".len() + 10,
            "default template should produce 14-char filename"
        );
    }

    #[test]
    fn test_custom_template() {
        let dir = tempfile::tempdir().unwrap();
        let template = format!("{}/myapp.XXXXX", dir.path().display());
        let output = cmd().arg(&template).output().unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        let filename = std::path::Path::new(path)
            .file_name()
            .unwrap()
            .to_str()
            .unwrap();
        assert!(
            filename.starts_with("myapp."),
            "should start with myapp.: {}",
            filename
        );
        assert!(std::path::Path::new(path).exists());
    }

    #[test]
    fn test_directory_flag() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-d", "-p", dir.path().to_str().unwrap()])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        let meta = std::fs::metadata(path).unwrap();
        assert!(meta.is_dir(), "should create a directory");
    }

    #[test]
    fn test_tmpdir_flag() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-p", dir.path().to_str().unwrap(), "testXXXXXX"])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        assert!(
            path.starts_with(dir.path().to_str().unwrap()),
            "path should be under specified dir: {}",
            path
        );
    }

    #[test]
    fn test_suffix_flag() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args([
                "-p",
                dir.path().to_str().unwrap(),
                "--suffix=.txt",
                "testXXXXXX",
            ])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        assert!(
            path.ends_with(".txt"),
            "path should end with .txt: {}",
            path
        );
        assert!(std::path::Path::new(path).exists());
    }

    #[test]
    fn test_t_flag() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-t", "-p", dir.path().to_str().unwrap(), "myXXXXXX"])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        assert!(
            path.starts_with(dir.path().to_str().unwrap()),
            "-t should use tmpdir: {}",
            path
        );
    }

    #[test]
    fn test_missing_xs_error() {
        let dir = tempfile::tempdir().unwrap();
        let template = format!("{}/notemplate", dir.path().display());
        let output = cmd().arg(&template).output().unwrap();
        assert_ne!(output.status.code(), Some(0));
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("too few X's"),
            "should report too few X's: {}",
            stderr
        );
    }

    #[test]
    fn test_dry_run() {
        let dir = tempfile::tempdir().unwrap();
        let template = format!("{}/dryXXXXXX", dir.path().display());
        let output = cmd().args(["-u", &template]).output().unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();
        // dry-run should NOT create the file
        assert!(
            !std::path::Path::new(path).exists(),
            "dry-run should not create file"
        );
    }

    #[test]
    fn test_quiet_suppresses_error() {
        let output = cmd()
            .args(["-q", "/nonexistent_dir_12345/tmpXXXXXX"])
            .output()
            .unwrap();
        assert_ne!(output.status.code(), Some(0));
        // In quiet mode, stderr should be empty
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.is_empty(),
            "quiet mode should suppress errors: {}",
            stderr
        );
    }

    #[test]
    fn test_unique_names() {
        let dir = tempfile::tempdir().unwrap();
        let template = format!("{}/uniqXXXXXX", dir.path().display());
        let out1 = cmd().arg(&template).output().unwrap();
        let out2 = cmd().arg(&template).output().unwrap();
        let path1 = String::from_utf8_lossy(&out1.stdout).trim().to_string();
        let path2 = String::from_utf8_lossy(&out2.stdout).trim().to_string();
        assert_ne!(
            path1, path2,
            "two invocations should produce different names"
        );
    }

    #[test]
    fn test_matches_gnu_exit_codes() {
        // Compare exit codes with GNU mktemp for error case
        let gnu = Command::new("mktemp").arg("/nonexistent/badXX").output();
        if let Ok(gnu_out) = gnu {
            let ours = cmd().arg("/nonexistent/badXX").output().unwrap();
            assert_eq!(
                ours.status.code(),
                gnu_out.status.code(),
                "Exit code mismatch with GNU mktemp"
            );
        }
    }

    #[test]
    fn test_file_permissions() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-p", dir.path().to_str().unwrap()])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();

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

    #[test]
    fn test_directory_permissions() {
        let dir = tempfile::tempdir().unwrap();
        let output = cmd()
            .args(["-d", "-p", dir.path().to_str().unwrap()])
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(0));
        let stdout = String::from_utf8_lossy(&output.stdout);
        let path = stdout.trim();

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let meta = std::fs::metadata(path).unwrap();
            let mode = meta.permissions().mode() & 0o777;
            assert_eq!(
                mode, 0o700,
                "directory should have mode 0700, got {:o}",
                mode
            );
        }
    }
}