Skip to content

Commit 4c65110

Browse files
authored
HADOOP-17310. Touch command with -c option is broken. (#2393). Contributed by Ayush Saxena.
1 parent 2e8cafa commit 4c65110

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/TouchCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected void processOptions(LinkedList<String> args) {
137137

138138
CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE,
139139
OPTION_USE_TIMESTAMP, OPTION_CHANGE_ONLY_ACCESS_TIME,
140-
OPTION_CHANGE_ONLY_MODIFICATION_TIME);
140+
OPTION_CHANGE_ONLY_MODIFICATION_TIME, OPTION_DO_NOT_CREATE_FILE);
141141
cf.parse(args);
142142
this.changeModTime = cf.getOpt(OPTION_CHANGE_ONLY_MODIFICATION_TIME);
143143
this.changeAccessTime = cf.getOpt(OPTION_CHANGE_ONLY_ACCESS_TIME);

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShellTouch.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void testTouch() throws Exception {
101101
{
102102
assertThat(
103103
"Expected successful touch on a non-existent file with -c option",
104-
shellRun("-touch", "-c", newFileName), is(not(0)));
104+
shellRun("-touch", "-c", newFileName), is(0));
105105
assertThat(lfs.exists(newFile), is(false));
106106
}
107107

@@ -140,7 +140,7 @@ public void testTouch() throws Exception {
140140
Date dateObj = parseTimestamp(strTime);
141141

142142
assertThat(
143-
"Expected successful touch with a specified modificatiom time",
143+
"Expected successful touch with a specified modification time",
144144
shellRun("-touch", "-m", "-t", strTime, newFileName), is(0));
145145
// Verify if modification time is recorded correctly (and access time
146146
// remains unchanged).
@@ -179,6 +179,16 @@ public void testTouch() throws Exception {
179179
assertThat("Expected failed touch with a missing timestamp",
180180
shellRun("-touch", "-t", newFileName), is(not(0)));
181181
}
182+
183+
// Verify -c option when file exists.
184+
String strTime = formatTimestamp(System.currentTimeMillis());
185+
Date dateObj = parseTimestamp(strTime);
186+
assertThat(
187+
"Expected successful touch on a non-existent file with -c option",
188+
shellRun("-touch", "-c", "-t", strTime, newFileName), is(0));
189+
FileStatus fileStatus = lfs.getFileStatus(newFile);
190+
assertThat(fileStatus.getAccessTime(), is(dateObj.getTime()));
191+
assertThat(fileStatus.getModificationTime(), is(dateObj.getTime()));
182192
}
183193

184194
private String formatTimestamp(long timeInMillis) {

0 commit comments

Comments
 (0)