Skip to content

Commit 553e27e

Browse files
vinayakumarbHexiaoqiao
authored andcommitted
HADOOP-17306. RawLocalFileSystem's lastModifiedTime() looses milli seconds in JDK < 10.b09 (#2387)
1 parent 188f0c3 commit 553e27e

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public class RawLocalFileSystem extends FileSystem {
7171
public static void useStatIfAvailable() {
7272
useDeprecatedFileStatus = !Stat.isAvailable();
7373
}
74-
74+
75+
@VisibleForTesting
76+
static void setUseDeprecatedFileStatus(boolean useDeprecatedFileStatus) {
77+
RawLocalFileSystem.useDeprecatedFileStatus = useDeprecatedFileStatus;
78+
}
79+
7580
public RawLocalFileSystem() {
7681
workingDir = getInitialWorkingDirectory();
7782
}
@@ -693,8 +698,8 @@ private static long getLastAccessTime(File f) throws IOException {
693698
DeprecatedRawLocalFileStatus(File f, long defaultBlockSize, FileSystem fs)
694699
throws IOException {
695700
super(f.length(), f.isDirectory(), 1, defaultBlockSize,
696-
f.lastModified(), getLastAccessTime(f),
697-
null, null, null,
701+
Files.getLastModifiedTime(f.toPath()).toMillis(),
702+
getLastAccessTime(f),null, null, null,
698703
new Path(f.getPath()).makeQualified(fs.getUri(),
699704
fs.getWorkingDirectory()));
700705
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.hadoop.fs;
1919

2020
import java.io.File;
21+
import java.io.IOException;
2122

2223
import org.apache.hadoop.conf.Configuration;
2324
import org.apache.hadoop.test.GenericTestUtils;
@@ -167,4 +168,28 @@ public void testPermission() throws Exception {
167168
}
168169
}
169170

171+
@Test
172+
public void testMTimeAtime() throws IOException {
173+
RawLocalFileSystem.setUseDeprecatedFileStatus(true);
174+
try {
175+
Path testDir = getTestBaseDir();
176+
String testFilename = "testmtime";
177+
Path path = new Path(testDir, testFilename);
178+
Path file = new Path(path, "file");
179+
fs.create(file);
180+
long now = System.currentTimeMillis();
181+
long mtime = (now % 1000 == 0) ? now + 1 : now;
182+
long atime = (now % 1000 == 0) ? now + 2 : now;
183+
fs.setTimes(file, mtime, atime);
184+
FileStatus fileStatus = fs.getFileStatus(file);
185+
if (!Shell.MAC) {
186+
// HADOOP-17306 ; Skip MacOS because HFS+ does not support
187+
// milliseconds for mtime.
188+
assertEquals(mtime, fileStatus.getModificationTime());
189+
}
190+
assertEquals(atime, fileStatus.getAccessTime());
191+
} finally {
192+
RawLocalFileSystem.useStatIfAvailable();
193+
}
194+
}
170195
}

0 commit comments

Comments
 (0)