2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "touch"]
|
2014-05-24 04:47:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Nick Platt <platt.nicholas@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate time;
|
|
|
|
|
|
|
|
use std::io::File;
|
2014-09-17 01:50:38 +00:00
|
|
|
use std::io::fs::PathExtensions;
|
2014-05-24 04:47:42 +00:00
|
|
|
|
|
|
|
#[path = "../common/util.rs"]
|
2015-01-08 12:54:22 +00:00
|
|
|
#[macro_use]
|
2014-05-24 04:47:42 +00:00
|
|
|
mod util;
|
|
|
|
|
|
|
|
static NAME: &'static str = "touch";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
2015-01-10 13:07:39 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> isize {
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2014-05-24 04:47:42 +00:00
|
|
|
getopts::optflag("a", "", "change only the access time"),
|
|
|
|
getopts::optflag("c", "no-create", "do not create any files"),
|
|
|
|
getopts::optopt( "d", "date", "parse argument and use it instead of current time", "STRING"),
|
|
|
|
getopts::optflag("h", "no-dereference", "affect each symbolic link instead of any referenced file \
|
|
|
|
(only for systems that can change the timestamps of a symlink)"),
|
|
|
|
getopts::optflag("m", "", "change only the modification time"),
|
|
|
|
getopts::optopt( "r", "reference", "use this file's times instead of the current time", "FILE"),
|
|
|
|
getopts::optopt( "t", "", "use [[CC]YY]MMDDhhmm[.ss] instead of the current time", "STAMP"),
|
|
|
|
getopts::optopt( "", "time", "change only the specified time: \"access\", \"atime\", or \
|
|
|
|
\"use\" are equivalent to -a; \"modify\" or \"mtime\" are \
|
|
|
|
equivalent to -m", "WORD"),
|
|
|
|
getopts::optflag("h", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
|
|
|
];
|
|
|
|
|
2014-11-19 20:55:25 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), &opts) {
|
2014-05-24 04:47:42 +00:00
|
|
|
Ok(m) => m,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Invalid options\n{}", e)
|
2014-05-24 04:47:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("version") {
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("help") || matches.free.is_empty() {
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-05-24 04:47:42 +00:00
|
|
|
println!("");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("Usage: {} [OPTION]... FILE...", NAME);
|
2014-05-24 04:47:42 +00:00
|
|
|
println!("");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("{}", getopts::usage("Update the access and modification times of \
|
2014-11-19 20:55:25 +00:00
|
|
|
each FILE to the current time.", &opts));
|
2014-06-22 14:44:31 +00:00
|
|
|
if matches.free.is_empty() {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 20:55:25 +00:00
|
|
|
if matches.opt_present("date") && matches.opts_present(&["reference".to_string(), "t".to_string()]) ||
|
|
|
|
matches.opt_present("reference") && matches.opts_present(&["date".to_string(), "t".to_string()]) ||
|
|
|
|
matches.opt_present("t") && matches.opts_present(&["date".to_string(), "reference".to_string()]) {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("Invalid options: cannot specify reference time from more than one source");
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (mut atime, mut mtime) =
|
|
|
|
if matches.opt_present("reference") {
|
2014-05-28 06:33:39 +00:00
|
|
|
let path = Path::new(matches.opt_str("reference").unwrap().to_string());
|
2014-05-24 04:47:42 +00:00
|
|
|
let stat = stat(&path, !matches.opt_present("no-dereference"));
|
|
|
|
(stat.accessed, stat.modified)
|
2014-11-19 20:55:25 +00:00
|
|
|
} else if matches.opts_present(&["date".to_string(), "t".to_string()]) {
|
2014-05-24 04:47:42 +00:00
|
|
|
let timestamp = if matches.opt_present("date") {
|
|
|
|
parse_date(matches.opt_str("date").unwrap().as_slice())
|
|
|
|
} else {
|
|
|
|
parse_timestamp(matches.opt_str("t").unwrap().as_slice())
|
|
|
|
};
|
|
|
|
(timestamp, timestamp)
|
|
|
|
} else {
|
|
|
|
// FIXME: Should use Timespec. https://github.com/mozilla/rust/issues/10301
|
|
|
|
let now = (time::get_time().sec * 1000) as u64;
|
|
|
|
(now, now)
|
|
|
|
};
|
|
|
|
|
|
|
|
for filename in matches.free.iter() {
|
2014-05-28 06:33:39 +00:00
|
|
|
let path = Path::new(filename.to_string());
|
2014-05-24 04:47:42 +00:00
|
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
// no-dereference included here for compatibility
|
2014-11-19 20:55:25 +00:00
|
|
|
if matches.opts_present(&["no-create".to_string(), "no-dereference".to_string()]) {
|
2014-05-24 04:47:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match File::create(&path) {
|
|
|
|
Ok(fd) => fd,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to create file: {}\n{}", filename, e.desc)
|
2014-05-24 04:47:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Minor optimization: if no reference time was specified, we're done.
|
2014-11-19 20:55:25 +00:00
|
|
|
if !matches.opts_present(&["date".to_string(), "reference".to_string(), "t".to_string()]) {
|
2014-05-24 04:47:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If changing "only" atime or mtime, grab the existing value of the other.
|
|
|
|
// Note that "-a" and "-m" may be passed together; this is not an xor.
|
2014-11-19 20:55:25 +00:00
|
|
|
if matches.opts_present(&["a".to_string(), "m".to_string(), "time".to_string()]) {
|
2014-05-24 04:47:42 +00:00
|
|
|
let stat = stat(&path, !matches.opt_present("no-dereference"));
|
|
|
|
let time = matches.opt_strs("time");
|
|
|
|
|
|
|
|
if !(matches.opt_present("a") ||
|
2014-05-28 06:33:39 +00:00
|
|
|
time.contains(&"access".to_string()) ||
|
|
|
|
time.contains(&"atime".to_string()) ||
|
|
|
|
time.contains(&"use".to_string())) {
|
2014-05-24 04:47:42 +00:00
|
|
|
atime = stat.accessed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(matches.opt_present("m") ||
|
2014-05-28 06:33:39 +00:00
|
|
|
time.contains(&"modify".to_string()) ||
|
|
|
|
time.contains(&"mtime".to_string())) {
|
2014-05-24 04:47:42 +00:00
|
|
|
mtime = stat.modified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match std::io::fs::change_file_times(&path, atime, mtime) {
|
|
|
|
Ok(t) => t,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to modify times\n{}", e.desc)
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stat(path: &Path, follow: bool) -> std::io::FileStat {
|
|
|
|
if follow {
|
|
|
|
match std::io::fs::stat(path) {
|
|
|
|
Ok(stat) => stat,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to open file\n{}", e.desc)
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match std::io::fs::lstat(path) {
|
|
|
|
Ok(stat) => stat,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to open file\n{}", e.desc)
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_date(str: &str) -> u64 {
|
|
|
|
// This isn't actually compatible with GNU touch, but there doesn't seem to
|
|
|
|
// be any simple specification for what format this parameter allows and I'm
|
|
|
|
// not about to implement GNU parse_datetime.
|
|
|
|
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
|
|
|
|
match time::strptime(str, "%c") {
|
|
|
|
Ok(tm) => (tm.to_timespec().sec * 1000) as u64,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to parse date\n{}", e)
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_timestamp(str: &str) -> u64 {
|
2014-12-28 11:03:54 +00:00
|
|
|
let format = match str.chars().count() {
|
2014-05-24 04:47:42 +00:00
|
|
|
15 => "%Y%m%d%H%M.%S",
|
|
|
|
12 => "%Y%m%d%H%M",
|
|
|
|
13 => "%y%m%d%H%M.%S",
|
|
|
|
10 => "%y%m%d%H%M",
|
|
|
|
11 => "%m%d%H%M.%S",
|
|
|
|
8 => "%m%d%H%M",
|
2014-10-30 09:06:47 +00:00
|
|
|
_ => panic!("Unknown timestamp format")
|
2014-05-24 04:47:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match time::strptime(str, format) {
|
|
|
|
Ok(tm) => (tm.to_timespec().sec * 1000) as u64,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(e) => panic!("Unable to parse timestamp\n{}", e)
|
2014-05-24 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|