Skip to content

Commit e21b812

Browse files
committed
HADOOP-17306. RawLocalFileSystem's lastModifiedTime() looses milli seconds in JDK < 10.b09 (#2387)
1 parent 02709cb commit e21b812

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-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
@@ -72,7 +72,12 @@ public class RawLocalFileSystem extends FileSystem {
7272
public static void useStatIfAvailable() {
7373
useDeprecatedFileStatus = !Stat.isAvailable();
7474
}
75-
75+
76+
@VisibleForTesting
77+
static void setUseDeprecatedFileStatus(boolean useDeprecatedFileStatus) {
78+
RawLocalFileSystem.useDeprecatedFileStatus = useDeprecatedFileStatus;
79+
}
80+
7681
public RawLocalFileSystem() {
7782
workingDir = getInitialWorkingDirectory();
7883
}
@@ -700,8 +705,8 @@ private static long getLastAccessTime(File f) throws IOException {
700705
DeprecatedRawLocalFileStatus(File f, long defaultBlockSize, FileSystem fs)
701706
throws IOException {
702707
super(f.length(), f.isDirectory(), 1, defaultBlockSize,
703-
f.lastModified(), getLastAccessTime(f),
704-
null, null, null,
708+
Files.getLastModifiedTime(f.toPath()).toMillis(),
709+
getLastAccessTime(f),null, null, null,
705710
new Path(f.getPath()).makeQualified(fs.getUri(),
706711
fs.getWorkingDirectory()));
707712
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,28 @@ public void testPermission() throws Exception {
203203
}
204204
}
205205

206+
@Test
207+
public void testMTimeAtime() throws IOException {
208+
RawLocalFileSystem.setUseDeprecatedFileStatus(true);
209+
try {
210+
Path testDir = getTestBaseDir();
211+
String testFilename = "testmtime";
212+
Path path = new Path(testDir, testFilename);
213+
Path file = new Path(path, "file");
214+
fs.create(file);
215+
long now = System.currentTimeMillis();
216+
long mtime = (now % 1000 == 0) ? now + 1 : now;
217+
long atime = (now % 1000 == 0) ? now + 2 : now;
218+
fs.setTimes(file, mtime, atime);
219+
FileStatus fileStatus = fs.getFileStatus(file);
220+
if (!Shell.MAC) {
221+
// HADOOP-17306 ; Skip MacOS because HFS+ does not support
222+
// milliseconds for mtime.
223+
assertEquals(mtime, fileStatus.getModificationTime());
224+
}
225+
assertEquals(atime, fileStatus.getAccessTime());
226+
} finally {
227+
RawLocalFileSystem.useStatIfAvailable();
228+
}
229+
}
206230
}

0 commit comments

Comments
 (0)