Skip to content

Commit 095430b

Browse files
committed
Allow touch command to create files
1 parent 9e1db72 commit 095430b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/bin/touch.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
use std::io::stdout;
1+
use std::{ffi::CStr, io::stdout};
22

33
use puppyutils::{Result, cli};
4-
use xenia::{AtFlags, ClockId, Timestamps, clock_gettime, stdio::cwd, utimensat};
4+
use xenia::{
5+
AtFlags, ClockId, Errno, Mode, OFlags, Timestamps, clock_gettime, open, stdio::cwd, utimensat,
6+
};
57

68
pub fn main() -> Result {
79
let mut stdout = stdout();
810
let mut files = Vec::new();
911

12+
let mut no_create = false;
13+
1014
cli! {
1115
"touch", stdout, #error
16+
Short('c') | Long("no-create") => no_create = true
1217
Value(value) => {
1318
files.push(value.into_owned());
1419
}
@@ -22,7 +27,22 @@ pub fn main() -> Result {
2227
};
2328

2429
for file in files {
25-
utimensat(cwd(), file, &timestamps, AtFlags::empty())?
30+
match utimensat(cwd(), &file, &timestamps, AtFlags::empty()) {
31+
Ok(_) => {}
32+
Err(errno) => {
33+
if no_create || errno != Errno::NOENT {
34+
return Err(errno.into());
35+
}
36+
37+
let fd = open(
38+
file,
39+
OFlags::WRONLY | OFlags::CREAT,
40+
Mode::from_bits_retain(0o666),
41+
)?;
42+
43+
utimensat::<_, &CStr>(fd, None, &timestamps, AtFlags::empty())?;
44+
}
45+
}
2646
}
2747

2848
Ok(())

0 commit comments

Comments
 (0)