Use libc::UTIME_NOW in touch when updating time to now (#9870)

* Use libc::UTIME_NOW in touch when updating time to now

* Explicit libc import to satisfy clippy

* Fix location of cfg in touch

* Add cfg gate to libc import to satisfy clippy on windows

* Change cfg gates in touch near libc::UTIME_NOW from unix to linux

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Ibrahim Burak Yorulmaz
2025-12-31 20:09:14 +01:00
committed by GitHub
parent c8790e6743
commit 16c16f44ee
2 changed files with 23 additions and 2 deletions
+16 -2
View File
@@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME datelike timelike
// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME datelike timelike UTIME
// spell-checker:ignore (FORMATS) MMDDhhmm YYYYMMDDHHMM YYMMDDHHMM YYYYMMDDHHMMS
pub mod error;
@@ -23,6 +23,8 @@ use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
#[cfg(target_os = "linux")]
use uucore::libc;
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
use uucore::translate;
use uucore::{format_usage, show};
@@ -377,7 +379,19 @@ pub fn touch(files: &[InputFile], opts: &Options) -> Result<(), TouchError> {
(atime, mtime)
}
Source::Now => {
let now = datetime_to_filetime(&Local::now());
let now: FileTime;
#[cfg(target_os = "linux")]
{
if opts.date.is_none() {
now = FileTime::from_unix_time(0, libc::UTIME_NOW as u32);
} else {
now = datetime_to_filetime(&Local::now());
}
}
#[cfg(not(target_os = "linux"))]
{
now = datetime_to_filetime(&Local::now());
}
(now, now)
}
&Source::Timestamp(ts) => (ts, ts),
+7
View File
@@ -1052,3 +1052,10 @@ fn test_touch_non_utf8_paths() {
scene.ucmd().arg(non_utf8_name).succeeds().no_output();
assert!(std::fs::metadata(at.plus(non_utf8_name)).is_ok());
}
#[test]
#[cfg(target_os = "linux")]
fn test_touch_dev_full() {
let (_, mut ucmd) = at_and_ucmd!();
ucmd.args(&["/dev/full"]).succeeds().no_output();
}