Skip to content

Commit 16c16f4

Browse files
iburaky2sylvestre
andauthored
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 <[email protected]>
1 parent c8790e6 commit 16c16f4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/uu/touch/src/touch.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME datelike timelike
6+
// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME datelike timelike UTIME
77
// spell-checker:ignore (FORMATS) MMDDhhmm YYYYMMDDHHMM YYMMDDHHMM YYYYMMDDHHMMS
88

99
pub mod error;
@@ -23,6 +23,8 @@ use std::io::{Error, ErrorKind};
2323
use std::path::{Path, PathBuf};
2424
use uucore::display::Quotable;
2525
use uucore::error::{FromIo, UResult, USimpleError};
26+
#[cfg(target_os = "linux")]
27+
use uucore::libc;
2628
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
2729
use uucore::translate;
2830
use uucore::{format_usage, show};
@@ -377,7 +379,19 @@ pub fn touch(files: &[InputFile], opts: &Options) -> Result<(), TouchError> {
377379
(atime, mtime)
378380
}
379381
Source::Now => {
380-
let now = datetime_to_filetime(&Local::now());
382+
let now: FileTime;
383+
#[cfg(target_os = "linux")]
384+
{
385+
if opts.date.is_none() {
386+
now = FileTime::from_unix_time(0, libc::UTIME_NOW as u32);
387+
} else {
388+
now = datetime_to_filetime(&Local::now());
389+
}
390+
}
391+
#[cfg(not(target_os = "linux"))]
392+
{
393+
now = datetime_to_filetime(&Local::now());
394+
}
381395
(now, now)
382396
}
383397
&Source::Timestamp(ts) => (ts, ts),

tests/by-util/test_touch.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,3 +1052,10 @@ fn test_touch_non_utf8_paths() {
10521052
scene.ucmd().arg(non_utf8_name).succeeds().no_output();
10531053
assert!(std::fs::metadata(at.plus(non_utf8_name)).is_ok());
10541054
}
1055+
1056+
#[test]
1057+
#[cfg(target_os = "linux")]
1058+
fn test_touch_dev_full() {
1059+
let (_, mut ucmd) = at_and_ucmd!();
1060+
ucmd.args(&["/dev/full"]).succeeds().no_output();
1061+
}

0 commit comments

Comments
 (0)