Skip to content

Commit 6d77f3b

Browse files
authored
HDFS-14529. SetTimes to throw FileNotFoundException if inode is not found (#3243)
Reviewed-by: Akira Ajisaka <[email protected]> Reviewed-by: Viraj Jasani <[email protected]> Reviewed-by: Ayush Saxena <[email protected]>
1 parent 798a083 commit 6d77f3b

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ static FileStatus setTimes(
124124
if (fsd.isPermissionEnabled()) {
125125
fsd.checkPathAccess(pc, iip, FsAction.WRITE);
126126
}
127-
final INode inode = iip.getLastINode();
128-
if (inode == null) {
129-
throw new FileNotFoundException("File/Directory " + iip.getPath() +
130-
" does not exist.");
131-
}
132127
boolean changed = unprotectedSetTimes(fsd, iip, mtime, atime, true);
133128
if (changed) {
134129
fsd.getEditLog().logTimes(iip.getPath(), mtime, atime);
@@ -305,7 +300,7 @@ static boolean unprotectedSetOwner(
305300

306301
static boolean setTimes(
307302
FSDirectory fsd, INodesInPath iip, long mtime, long atime, boolean force)
308-
throws QuotaExceededException {
303+
throws FileNotFoundException {
309304
fsd.writeLock();
310305
try {
311306
return unprotectedSetTimes(fsd, iip, mtime, atime, force);
@@ -497,10 +492,14 @@ private static void setDirStoragePolicy(
497492

498493
static boolean unprotectedSetTimes(
499494
FSDirectory fsd, INodesInPath iip, long mtime, long atime, boolean force)
500-
throws QuotaExceededException {
495+
throws FileNotFoundException {
501496
assert fsd.hasWriteLock();
502497
boolean status = false;
503498
INode inode = iip.getLastINode();
499+
if (inode == null) {
500+
throw new FileNotFoundException("File/Directory " + iip.getPath() +
501+
" does not exist.");
502+
}
504503
int latest = iip.getLatestSnapshotId();
505504
if (mtime >= 0) {
506505
inode = inode.setModificationTime(mtime, latest);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFSDirAttrOp.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import org.apache.hadoop.fs.permission.FsPermission;
2424
import org.apache.hadoop.fs.permission.PermissionStatus;
2525
import org.apache.hadoop.hdfs.DFSUtil;
26-
import org.apache.hadoop.hdfs.protocol.QuotaExceededException;
2726
import org.apache.hadoop.hdfs.server.namenode.snapshot.SnapshotManager;
2827
import org.junit.Test;
2928
import org.mockito.Mockito;
3029

30+
import java.io.FileNotFoundException;
31+
3132
import static org.junit.Assert.assertFalse;
3233
import static org.junit.Assert.assertTrue;
3334
import static org.mockito.Mockito.when;
@@ -40,7 +41,8 @@ public class TestFSDirAttrOp {
4041
LoggerFactory.getLogger(TestFSDirAttrOp.class);
4142

4243
private boolean unprotectedSetTimes(long atime, long atime0, long precision,
43-
long mtime, boolean force) throws QuotaExceededException {
44+
long mtime, boolean force)
45+
throws FileNotFoundException {
4446
FSNamesystem fsn = Mockito.mock(FSNamesystem.class);
4547
SnapshotManager ssMgr = Mockito.mock(SnapshotManager.class);
4648
FSDirectory fsd = Mockito.mock(FSDirectory.class);
@@ -131,4 +133,16 @@ public void testUnprotectedSetTimes() throws Exception {
131133
assertTrue("SetTimes should update access time",
132134
unprotectedSetTimes(100, 0, 1000, 1, false));
133135
}
136+
137+
@Test(expected = FileNotFoundException.class)
138+
public void testUnprotectedSetTimesFNFE()
139+
throws FileNotFoundException {
140+
FSDirectory fsd = Mockito.mock(FSDirectory.class);
141+
INodesInPath iip = Mockito.mock(INodesInPath.class);
142+
143+
when(fsd.hasWriteLock()).thenReturn(Boolean.TRUE);
144+
when(iip.getLastINode()).thenReturn(null);
145+
146+
FSDirAttrOp.unprotectedSetTimes(fsd, iip, 0, 0, false);
147+
}
134148
}

0 commit comments

Comments
 (0)