HADOOP-17310. Touch command with -c option is broken. (#2393). Contributed by Ayush Saxena.

This commit is contained in:
Ayush Saxena 2020-10-19 10:54:18 +05:30 committed by GitHub
parent 2e8cafac3b
commit 4c651103f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -137,7 +137,7 @@ protected void processOptions(LinkedList<String> args) {
CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE, CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE,
OPTION_USE_TIMESTAMP, OPTION_CHANGE_ONLY_ACCESS_TIME, OPTION_USE_TIMESTAMP, OPTION_CHANGE_ONLY_ACCESS_TIME,
OPTION_CHANGE_ONLY_MODIFICATION_TIME); OPTION_CHANGE_ONLY_MODIFICATION_TIME, OPTION_DO_NOT_CREATE_FILE);
cf.parse(args); cf.parse(args);
this.changeModTime = cf.getOpt(OPTION_CHANGE_ONLY_MODIFICATION_TIME); this.changeModTime = cf.getOpt(OPTION_CHANGE_ONLY_MODIFICATION_TIME);
this.changeAccessTime = cf.getOpt(OPTION_CHANGE_ONLY_ACCESS_TIME); this.changeAccessTime = cf.getOpt(OPTION_CHANGE_ONLY_ACCESS_TIME);

View File

@ -101,7 +101,7 @@ public void testTouch() throws Exception {
{ {
assertThat( assertThat(
"Expected successful touch on a non-existent file with -c option", "Expected successful touch on a non-existent file with -c option",
shellRun("-touch", "-c", newFileName), is(not(0))); shellRun("-touch", "-c", newFileName), is(0));
assertThat(lfs.exists(newFile), is(false)); assertThat(lfs.exists(newFile), is(false));
} }
@ -140,7 +140,7 @@ public void testTouch() throws Exception {
Date dateObj = parseTimestamp(strTime); Date dateObj = parseTimestamp(strTime);
assertThat( assertThat(
"Expected successful touch with a specified modificatiom time", "Expected successful touch with a specified modification time",
shellRun("-touch", "-m", "-t", strTime, newFileName), is(0)); shellRun("-touch", "-m", "-t", strTime, newFileName), is(0));
// Verify if modification time is recorded correctly (and access time // Verify if modification time is recorded correctly (and access time
// remains unchanged). // remains unchanged).
@ -179,6 +179,16 @@ public void testTouch() throws Exception {
assertThat("Expected failed touch with a missing timestamp", assertThat("Expected failed touch with a missing timestamp",
shellRun("-touch", "-t", newFileName), is(not(0))); shellRun("-touch", "-t", newFileName), is(not(0)));
} }
// Verify -c option when file exists.
String strTime = formatTimestamp(System.currentTimeMillis());
Date dateObj = parseTimestamp(strTime);
assertThat(
"Expected successful touch on a non-existent file with -c option",
shellRun("-touch", "-c", "-t", strTime, newFileName), is(0));
FileStatus fileStatus = lfs.getFileStatus(newFile);
assertThat(fileStatus.getAccessTime(), is(dateObj.getTime()));
assertThat(fileStatus.getModificationTime(), is(dateObj.getTime()));
} }
private String formatTimestamp(long timeInMillis) { private String formatTimestamp(long timeInMillis) {