Skip to content

Commit d0dda52

Browse files
committed
touch: use helper function for file creation
1 parent 62ba7fb commit d0dda52

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/uu/touch/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ clap = { workspace = true }
2525
jiff = { workspace = true }
2626
parse_datetime = { workspace = true }
2727
thiserror = { workspace = true }
28-
uucore = { workspace = true, features = ["libc", "parser"] }
28+
uucore = { workspace = true, features = ["libc", "parser", "fs"] }
2929
fluent = { workspace = true }
3030

3131
[target.'cfg(target_os = "windows")'.dependencies]

src/uu/touch/src/touch.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ use jiff::tz::TimeZone;
1717
use jiff::{Timestamp, ToSpan, Zoned};
1818
use std::borrow::Cow;
1919
use std::ffi::{OsStr, OsString};
20-
use std::fs::{self, File};
20+
use std::fs;
2121
use std::io::{Error, ErrorKind};
2222
use std::path::{Path, PathBuf};
2323
use std::time::SystemTime;
2424
use uucore::display::Quotable;
2525
use uucore::error::{FromIo, UResult, USimpleError};
26+
use uucore::fs::create_file_restrictive_perm;
2627
#[cfg(target_os = "linux")]
2728
use uucore::libc;
2829
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
@@ -476,9 +477,9 @@ fn touch_file(
476477
return Ok(());
477478
}
478479

479-
if let Err(e) = File::create(path) {
480+
if let Err(e) = create_file_restrictive_perm(path, true) {
480481
// we need to check if the path is the path to a directory (ends with a separator)
481-
// we can't use File::create to create a directory
482+
// we can't use create_file to create a directory
482483
// we cannot use path.is_dir() because it calls fs::metadata which we already called
483484
// when stable, we can change to use e.kind() == std::io::ErrorKind::IsADirectory
484485
let is_directory = if let Some(last_char) = path.to_string_lossy().chars().last() {

0 commit comments

Comments
 (0)