mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
touch: respect -h
This commit is contained in:
parent
b2ded87d39
commit
d0196b82ad
2 changed files with 97 additions and 58 deletions
|
@ -10,9 +10,12 @@ path = "touch.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
filetime = "*"
|
filetime = "*"
|
||||||
getopts = "*"
|
getopts = "*"
|
||||||
libc = "*"
|
|
||||||
time = "*"
|
time = "*"
|
||||||
uucore = { path="../uucore" }
|
|
||||||
|
[dependencies.uucore]
|
||||||
|
path = "../uucore"
|
||||||
|
default-features = false
|
||||||
|
features = ["libc"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "touch"
|
name = "touch"
|
||||||
|
|
|
@ -1,25 +1,24 @@
|
||||||
#![crate_name = "uu_touch"]
|
#![crate_name = "uu_touch"]
|
||||||
|
|
||||||
/*
|
// This file is part of the uutils coreutils package.
|
||||||
* This file is part of the uutils coreutils package.
|
//
|
||||||
*
|
// (c) Nick Platt <platt.nicholas@gmail.com>
|
||||||
* (c) Nick Platt <platt.nicholas@gmail.com>
|
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
|
||||||
*
|
//
|
||||||
* For the full copyright and license information, please view the LICENSE file
|
// For the full copyright and license information, please view the LICENSE file
|
||||||
* that was distributed with this source code.
|
// that was distributed with this source code.
|
||||||
*/
|
//
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
|
||||||
extern crate time;
|
extern crate time;
|
||||||
extern crate filetime;
|
pub extern crate filetime;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use filetime::*;
|
use filetime::*;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{Error, Write};
|
use std::io::{self, Error, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
static NAME: &'static str = "touch";
|
static NAME: &'static str = "touch";
|
||||||
|
@ -43,26 +42,65 @@ macro_rules! local_tm_to_filetime(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
macro_rules! to_timeval {
|
||||||
|
($ft:expr) => (
|
||||||
|
timeval {
|
||||||
|
tv_sec: $ft.seconds() as time_t,
|
||||||
|
tv_usec: ($ft.nanoseconds() / 1000) as suseconds_t,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn set_symlink_times(p: &str, atime: FileTime, mtime: FileTime) -> io::Result<()> {
|
||||||
|
use std::ffi::CString;
|
||||||
|
use uucore::libc::{lutimes, timeval, time_t, suseconds_t};
|
||||||
|
|
||||||
|
let times = [to_timeval!(atime), to_timeval!(mtime)];
|
||||||
|
let p = try!(CString::new(p));
|
||||||
|
return unsafe {
|
||||||
|
if lutimes(p.as_ptr() as *const _, times.as_ptr()) == 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(io::Error::last_os_error())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
|
|
||||||
opts.optflag("a", "", "change only the access time");
|
opts.optflag("a", "", "change only the access time");
|
||||||
opts.optflag("c", "no-create", "do not create any files");
|
opts.optflag("c", "no-create", "do not create any files");
|
||||||
opts.optopt( "d", "date", "parse argument and use it instead of current time", "STRING");
|
opts.optopt("d",
|
||||||
opts.optflag("h", "no-dereference", "affect each symbolic link instead of any referenced file \
|
"date",
|
||||||
|
"parse argument and use it instead of current time",
|
||||||
|
"STRING");
|
||||||
|
opts.optflag("h",
|
||||||
|
"no-dereference",
|
||||||
|
"affect each symbolic link instead of any referenced file \
|
||||||
(only for systems that can change the timestamps of a symlink)");
|
(only for systems that can change the timestamps of a symlink)");
|
||||||
opts.optflag("m", "", "change only the modification time");
|
opts.optflag("m", "", "change only the modification time");
|
||||||
opts.optopt( "r", "reference", "use this file's times instead of the current time", "FILE");
|
opts.optopt("r",
|
||||||
opts.optopt( "t", "", "use [[CC]YY]MMDDhhmm[.ss] instead of the current time", "STAMP");
|
"reference",
|
||||||
opts.optopt( "", "time", "change only the specified time: \"access\", \"atime\", or \
|
"use this file's times instead of the current time",
|
||||||
|
"FILE");
|
||||||
|
opts.optopt("t",
|
||||||
|
"",
|
||||||
|
"use [[CC]YY]MMDDhhmm[.ss] instead of the current time",
|
||||||
|
"STAMP");
|
||||||
|
opts.optopt("",
|
||||||
|
"time",
|
||||||
|
"change only the specified time: \"access\", \"atime\", or \
|
||||||
\"use\" are equivalent to -a; \"modify\" or \"mtime\" are \
|
\"use\" are equivalent to -a; \"modify\" or \"mtime\" are \
|
||||||
equivalent to -m", "WORD");
|
equivalent to -m",
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
"WORD");
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
opts.optflag("h", "help", "display this help and exit");
|
||||||
|
opts.optflag("V", "version", "output version information and exit");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => panic!("Invalid options\n{}", e)
|
Err(e) => panic!("Invalid options\n{}", e),
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("version") {
|
if matches.opt_present("version") {
|
||||||
|
@ -89,20 +127,20 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
panic!("Invalid options: cannot specify reference time from more than one source");
|
panic!("Invalid options: cannot specify reference time from more than one source");
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut atime, mut mtime) =
|
let (mut atime, mut mtime) = if matches.opt_present("reference") {
|
||||||
if matches.opt_present("reference") {
|
stat(&matches.opt_str("reference").unwrap()[..],
|
||||||
stat(&matches.opt_str("reference").unwrap()[..], !matches.opt_present("no-dereference"))
|
!matches.opt_present("no-dereference"))
|
||||||
} else if matches.opts_present(&["date".to_owned(), "t".to_owned()]) {
|
} else if matches.opts_present(&["date".to_owned(), "t".to_owned()]) {
|
||||||
let timestamp = if matches.opt_present("date") {
|
let timestamp = if matches.opt_present("date") {
|
||||||
parse_date(matches.opt_str("date").unwrap().as_ref())
|
parse_date(matches.opt_str("date").unwrap().as_ref())
|
||||||
} else {
|
|
||||||
parse_timestamp(matches.opt_str("t").unwrap().as_ref())
|
|
||||||
};
|
|
||||||
(timestamp, timestamp)
|
|
||||||
} else {
|
} else {
|
||||||
let now = local_tm_to_filetime!(time::now());
|
parse_timestamp(matches.opt_str("t").unwrap().as_ref())
|
||||||
(now, now)
|
|
||||||
};
|
};
|
||||||
|
(timestamp, timestamp)
|
||||||
|
} else {
|
||||||
|
let now = local_tm_to_filetime!(time::now());
|
||||||
|
(now, now)
|
||||||
|
};
|
||||||
|
|
||||||
for filename in &matches.free {
|
for filename in &matches.free {
|
||||||
let path = &filename[..];
|
let path = &filename[..];
|
||||||
|
@ -130,25 +168,26 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let st = stat(path, !matches.opt_present("no-dereference"));
|
let st = stat(path, !matches.opt_present("no-dereference"));
|
||||||
let time = matches.opt_strs("time");
|
let time = matches.opt_strs("time");
|
||||||
|
|
||||||
if !(matches.opt_present("a") ||
|
if !(matches.opt_present("a") || time.contains(&"access".to_owned()) ||
|
||||||
time.contains(&"access".to_owned()) ||
|
time.contains(&"atime".to_owned()) || time.contains(&"use".to_owned())) {
|
||||||
time.contains(&"atime".to_owned()) ||
|
|
||||||
time.contains(&"use".to_owned())) {
|
|
||||||
atime = st.0;
|
atime = st.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(matches.opt_present("m") ||
|
if !(matches.opt_present("m") || time.contains(&"modify".to_owned()) ||
|
||||||
time.contains(&"modify".to_owned()) ||
|
|
||||||
time.contains(&"mtime".to_owned())) {
|
time.contains(&"mtime".to_owned())) {
|
||||||
mtime = st.1;
|
mtime = st.1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this follows symlinks and thus does not work correctly for the -h flag
|
if matches.opt_present("h") {
|
||||||
// need to use lutimes() c function on supported platforms
|
if let Err(e) = set_symlink_times(path, atime, mtime) {
|
||||||
if let Err(e) = filetime::set_file_times(path, atime, mtime) {
|
show_warning!("cannot touch '{}': {}", path, e);
|
||||||
show_warning!("cannot touch '{}': {}", path, e);
|
}
|
||||||
};
|
} else {
|
||||||
|
if let Err(e) = filetime::set_file_times(path, atime, mtime) {
|
||||||
|
show_warning!("cannot touch '{}': {}", path, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
0
|
||||||
|
@ -162,11 +201,8 @@ fn stat(path: &str, follow: bool) -> (FileTime, FileTime) {
|
||||||
};
|
};
|
||||||
|
|
||||||
match metadata {
|
match metadata {
|
||||||
Ok(m) => (
|
Ok(m) => (FileTime::from_last_access_time(&m), FileTime::from_last_modification_time(&m)),
|
||||||
FileTime::from_last_access_time(&m),
|
Err(_) => crash!(1, "failed to get attributes of '{}': {}", path, Error::last_os_error()),
|
||||||
FileTime::from_last_modification_time(&m)
|
|
||||||
),
|
|
||||||
Err(_) => crash!(1, "failed to get attributes of '{}': {}", path, Error::last_os_error())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +213,7 @@ fn parse_date(str: &str) -> FileTime {
|
||||||
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
|
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
|
||||||
match time::strptime(str, "%c") {
|
match time::strptime(str, "%c") {
|
||||||
Ok(tm) => local_tm_to_filetime!(to_local!(tm)),
|
Ok(tm) => local_tm_to_filetime!(to_local!(tm)),
|
||||||
Err(e) => panic!("Unable to parse date\n{}", e)
|
Err(e) => panic!("Unable to parse date\n{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,12 +225,12 @@ fn parse_timestamp(s: &str) -> FileTime {
|
||||||
13 => ("%y%m%d%H%M.%S", s.to_owned()),
|
13 => ("%y%m%d%H%M.%S", s.to_owned()),
|
||||||
10 => ("%y%m%d%H%M", s.to_owned()),
|
10 => ("%y%m%d%H%M", s.to_owned()),
|
||||||
11 => ("%Y%m%d%H%M.%S", format!("{}{}", now.tm_year + 1900, s)),
|
11 => ("%Y%m%d%H%M.%S", format!("{}{}", now.tm_year + 1900, s)),
|
||||||
8 => ("%Y%m%d%H%M", format!("{}{}", now.tm_year + 1900, s)),
|
8 => ("%Y%m%d%H%M", format!("{}{}", now.tm_year + 1900, s)),
|
||||||
_ => panic!("Unknown timestamp format")
|
_ => panic!("Unknown timestamp format"),
|
||||||
};
|
};
|
||||||
|
|
||||||
match time::strptime(&ts, format) {
|
match time::strptime(&ts, format) {
|
||||||
Ok(tm) => local_tm_to_filetime!(to_local!(tm)),
|
Ok(tm) => local_tm_to_filetime!(to_local!(tm)),
|
||||||
Err(e) => panic!("Unable to parse timestamp\n{}", e)
|
Err(e) => panic!("Unable to parse timestamp\n{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue