Skip to content

Commit 51c046e

Browse files
committed
HADOOP-18188. Support touch command for directory
1 parent 34b3275 commit 51c046e

File tree

3 files changed

+303
-3
lines changed

3 files changed

+303
-3
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ protected void processOptions(LinkedList<String> args) {
147147

148148
@Override
149149
protected void processPath(PathData item) throws IOException {
150-
if (item.stat.isDirectory()) {
151-
throw new PathIsDirectoryException(item.toString());
152-
}
153150
touch(item);
154151
}
155152

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,72 @@ public void testTouch() throws Exception {
200200
FileStatus fileStatus = lfs.getFileStatus(newFile);
201201
assertThat(fileStatus.getAccessTime()).isEqualTo(dateObj.getTime());
202202
assertThat(fileStatus.getModificationTime()).isEqualTo(dateObj.getTime());
203+
204+
touchDirTests();
205+
}
206+
207+
private void touchDirTests() throws Exception {
208+
String strTime;
209+
final String newFileName = "dir3/newFile3";
210+
Date dateObj;
211+
final Path newFile = new Path(newFileName);
212+
FileStatus fstatus;
213+
Path dirPath = new Path("dir3");
214+
lfs.mkdirs(dirPath);
215+
lfs.delete(newFile, true);
216+
assertThat(lfs.exists(newFile)).isFalse();
217+
218+
strTime = formatTimestamp(System.currentTimeMillis());
219+
dateObj = parseTimestamp(strTime);
220+
221+
assertThat(shellRun("-touch", "-t", strTime, newFileName)).as(
222+
"Expected successful touch on a new file with a specified timestamp").isEqualTo(0);
223+
FileStatus newStatus = lfs.getFileStatus(newFile);
224+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
225+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
226+
227+
Thread.sleep(500);
228+
strTime = formatTimestamp(System.currentTimeMillis());
229+
dateObj = parseTimestamp(strTime);
230+
231+
assertThat(shellRun("-touch", "-m", "-a", "-t", strTime, "dir3")).as(
232+
"Expected successful touch with a specified modification time").isEqualTo(0);
233+
234+
newStatus = lfs.getFileStatus(dirPath);
235+
// Verify if both modification and access times are recorded correctly
236+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
237+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
238+
239+
fstatus = lfs.getFileStatus(dirPath);
240+
Thread.sleep(500);
241+
strTime = formatTimestamp(System.currentTimeMillis());
242+
dateObj = parseTimestamp(strTime);
243+
244+
assertThat(shellRun("-touch", "-m", "-t", strTime, "dir3")).as(
245+
"Expected successful touch with a specified modification time").isEqualTo(0);
246+
247+
newStatus = lfs.getFileStatus(dirPath);
248+
// Verify if modification time is recorded correctly (and access time
249+
// remains unchanged).
250+
assertThat(newStatus.getAccessTime()).isEqualTo(fstatus.getAccessTime());
251+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
252+
253+
fstatus = lfs.getFileStatus(dirPath);
254+
Thread.sleep(500);
255+
strTime = formatTimestamp(System.currentTimeMillis());
256+
dateObj = parseTimestamp(strTime);
257+
258+
assertThat(shellRun("-touch", "-a", "-t", strTime, "dir3")).as(
259+
"Expected successful touch with a specified modification time").isEqualTo(0);
260+
261+
newStatus = lfs.getFileStatus(dirPath);
262+
// Verify if access time is recorded correctly (and modification time
263+
// remains unchanged).
264+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
265+
assertThat(newStatus.getModificationTime()).isEqualTo(fstatus.getModificationTime());
266+
267+
lfs.delete(newFile, true);
268+
lfs.delete(dirPath, true);
203269
}
204270

205271
private String formatTimestamp(long timeInMillis) {
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.hdfs;
20+
21+
import java.io.IOException;
22+
import java.text.ParseException;
23+
import java.util.Date;
24+
25+
import org.junit.AfterClass;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
import org.apache.hadoop.conf.Configuration;
32+
import org.apache.hadoop.fs.FileStatus;
33+
import org.apache.hadoop.fs.FsShell;
34+
import org.apache.hadoop.fs.Path;
35+
import org.apache.hadoop.fs.shell.TouchCommands;
36+
import org.apache.hadoop.test.GenericTestUtils;
37+
import org.apache.hadoop.util.StringUtils;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
public class TestDFSShellTouch {
42+
43+
private static final Logger LOG = LoggerFactory.getLogger(TestDFSShellTouch.class);
44+
45+
private static MiniDFSCluster miniCluster;
46+
private static DistributedFileSystem dfs;
47+
private static FsShell shell;
48+
49+
@BeforeClass
50+
public static void setup() throws IOException {
51+
final Configuration conf = new Configuration();
52+
conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR,
53+
GenericTestUtils.getTestDir("TestDFSShellTouch").getAbsolutePath());
54+
55+
miniCluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build();
56+
miniCluster.waitActive();
57+
dfs = miniCluster.getFileSystem();
58+
shell = new FsShell(dfs.getConf());
59+
}
60+
61+
@AfterClass
62+
public static void tearDown() {
63+
if (miniCluster != null) {
64+
miniCluster.shutdown(true, true);
65+
}
66+
}
67+
68+
@Test
69+
public void testTouch() throws Exception {
70+
final String newFileName = "newFile1";
71+
final Path newFile = new Path(newFileName);
72+
dfs.delete(newFile, true);
73+
assertThat(dfs.exists(newFile)).isFalse();
74+
dfs.create(newFile);
75+
assertThat(dfs.exists(newFile)).isTrue();
76+
77+
String strTime = formatTimestamp(System.currentTimeMillis());
78+
Date dateObj = parseTimestamp(strTime);
79+
80+
assertThat(shellRun("-touch", "-t", strTime, newFileName)).as(
81+
"Expected successful touch on a new file" + " with a specified timestamp").isEqualTo(0);
82+
FileStatus newStatus = dfs.getFileStatus(newFile);
83+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
84+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
85+
86+
FileStatus fileStatus = dfs.getFileStatus(newFile);
87+
Thread.sleep(500);
88+
89+
strTime = formatTimestamp(System.currentTimeMillis());
90+
dateObj = parseTimestamp(strTime);
91+
92+
assertThat(shellRun("-touch", "-a", "-t", strTime, newFileName)).as(
93+
"Expected successful touch with a specified access time").isEqualTo(0);
94+
newStatus = dfs.getFileStatus(newFile);
95+
// Verify if access time is recorded correctly (and modification time
96+
// remains unchanged).
97+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
98+
assertThat(newStatus.getModificationTime()).isEqualTo(fileStatus.getModificationTime());
99+
100+
fileStatus = dfs.getFileStatus(newFile);
101+
Thread.sleep(500);
102+
103+
strTime = formatTimestamp(System.currentTimeMillis());
104+
dateObj = parseTimestamp(strTime);
105+
106+
assertThat(shellRun("-touch", "-m", "-t", strTime, newFileName)).as(
107+
"Expected successful touch with a specified modification time").isEqualTo(0);
108+
// Verify if modification time is recorded correctly (and access time
109+
// remains unchanged).
110+
newStatus = dfs.getFileStatus(newFile);
111+
assertThat(newStatus.getAccessTime()).isEqualTo(fileStatus.getAccessTime());
112+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
113+
114+
strTime = formatTimestamp(System.currentTimeMillis());
115+
dateObj = parseTimestamp(strTime);
116+
117+
assertThat(shellRun("-touch", "-t", strTime, newFileName)).as(
118+
"Expected successful touch with a specified timestamp").isEqualTo(0);
119+
120+
// Verify if both modification and access times are recorded correctly
121+
newStatus = dfs.getFileStatus(newFile);
122+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
123+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
124+
125+
strTime = formatTimestamp(System.currentTimeMillis());
126+
dateObj = parseTimestamp(strTime);
127+
128+
assertThat(shellRun("-touch", "-a", "-m", "-t", strTime, newFileName)).as(
129+
"Expected successful touch with a specified timestamp").isEqualTo(0);
130+
131+
// Verify if both modification and access times are recorded correctly
132+
newStatus = dfs.getFileStatus(newFile);
133+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
134+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
135+
136+
assertThat(shellRun("-touch", "-t", newFileName)).as(
137+
"Expected failed touch with a missing timestamp").isNotEqualTo(0);
138+
139+
strTime = formatTimestamp(System.currentTimeMillis());
140+
dateObj = parseTimestamp(strTime);
141+
assertThat(shellRun("-touch", "-c", "-t", strTime, newFileName)).as(
142+
"Expected successful touch on a non-existent file with -c option").isEqualTo(0);
143+
fileStatus = dfs.getFileStatus(newFile);
144+
assertThat(fileStatus.getAccessTime()).isEqualTo(dateObj.getTime());
145+
assertThat(fileStatus.getModificationTime()).isEqualTo(dateObj.getTime());
146+
147+
dfs.delete(newFile, true);
148+
assertThat(dfs.exists(newFile)).isFalse();
149+
150+
touchDirTests();
151+
152+
}
153+
154+
private void touchDirTests() throws IOException, ParseException, InterruptedException {
155+
final String newFileName = "dir2/newFile2";
156+
final Path newFile = new Path(newFileName);
157+
FileStatus newStatus;
158+
FileStatus fileStatus;
159+
String strTime;
160+
Date dateObj;
161+
Path dirPath = new Path("dir2");
162+
dfs.mkdirs(dirPath);
163+
dfs.delete(newFile, true);
164+
assertThat(dfs.exists(newFile)).isFalse();
165+
166+
strTime = formatTimestamp(System.currentTimeMillis());
167+
dateObj = parseTimestamp(strTime);
168+
169+
assertThat(shellRun("-touch", "-t", strTime, newFileName)).as(
170+
"Expected successful touch on a new file with a specified timestamp").isEqualTo(0);
171+
newStatus = dfs.getFileStatus(newFile);
172+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
173+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
174+
175+
Thread.sleep(500);
176+
strTime = formatTimestamp(System.currentTimeMillis());
177+
dateObj = parseTimestamp(strTime);
178+
179+
assertThat(shellRun("-touch", "-m", "-a", "-t", strTime, "dir2")).as(
180+
"Expected successful touch with a specified modification time").isEqualTo(0);
181+
182+
newStatus = dfs.getFileStatus(dirPath);
183+
// Verify if both modification and access times are recorded correctly
184+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
185+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
186+
187+
fileStatus = dfs.getFileStatus(dirPath);
188+
Thread.sleep(500);
189+
190+
strTime = formatTimestamp(System.currentTimeMillis());
191+
dateObj = parseTimestamp(strTime);
192+
193+
assertThat(shellRun("-touch", "-m", "-t", strTime, "dir2")).as(
194+
"Expected successful touch with a specified modification time").isEqualTo(0);
195+
196+
newStatus = dfs.getFileStatus(dirPath);
197+
// Verify if modification time is recorded correctly (and access time
198+
// remains unchanged).
199+
assertThat(newStatus.getAccessTime()).isEqualTo(fileStatus.getAccessTime());
200+
assertThat(newStatus.getModificationTime()).isEqualTo(dateObj.getTime());
201+
202+
fileStatus = dfs.getFileStatus(dirPath);
203+
Thread.sleep(500);
204+
205+
strTime = formatTimestamp(System.currentTimeMillis());
206+
dateObj = parseTimestamp(strTime);
207+
208+
assertThat(shellRun("-touch", "-a", "-t", strTime, "dir2")).as(
209+
"Expected successful touch with a specified modification time").isEqualTo(0);
210+
211+
newStatus = dfs.getFileStatus(dirPath);
212+
// Verify if access time is recorded correctly (and modification time
213+
// remains unchanged).
214+
assertThat(newStatus.getAccessTime()).isEqualTo(dateObj.getTime());
215+
assertThat(newStatus.getModificationTime()).isEqualTo(fileStatus.getModificationTime());
216+
217+
dfs.delete(newFile, true);
218+
dfs.delete(dirPath, true);
219+
assertThat(dfs.exists(newFile)).isFalse();
220+
assertThat(dfs.exists(dirPath)).isFalse();
221+
}
222+
223+
private int shellRun(String... args) {
224+
int exitCode = shell.run(args);
225+
LOG.info("exit " + exitCode + " - " + StringUtils.join(" ", args));
226+
return exitCode;
227+
}
228+
229+
private String formatTimestamp(long timeInMillis) {
230+
return (new TouchCommands.Touch()).getDateFormat().format(new Date(timeInMillis));
231+
}
232+
233+
private Date parseTimestamp(String tstamp) throws ParseException {
234+
return (new TouchCommands.Touch()).getDateFormat().parse(tstamp);
235+
}
236+
237+
}

0 commit comments

Comments
 (0)