HDFS-6240. WebImageViewer returns 404 if LISTSTATUS to an empty directory. Contributed by Akira Ajisaka.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1593591 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Haohui Mai 2014-05-09 18:43:46 +00:00
parent 0c437e90b8
commit ed6f32c540
3 changed files with 21 additions and 4 deletions

View File

@ -438,6 +438,9 @@ Release 2.5.0 - UNRELEASED
HDFS-5381. ExtendedBlock#hashCode should use both blockId and block pool ID
(Benoy Antony via Colin Patrick McCabe)
HDFS-6240. WebImageViewer returns 404 if LISTSTATUS to an empty directory.
(Akira Ajisaka via wheat9)
Release 2.4.1 - UNRELEASED
INCOMPATIBLE CHANGES

View File

@ -261,6 +261,10 @@ class FSImageLoader {
long id = getINodeId(path);
FsImageProto.INodeSection.INode inode = inodes.get(id);
if (inode.getType() == FsImageProto.INodeSection.INode.Type.DIRECTORY) {
if (!dirmap.containsKey(id)) {
// if the directory is empty, return empty list
return list;
}
long[] children = dirmap.get(id);
for (long cid : children) {
list.add(getFileStatus(inodes.get(cid), true));
@ -416,7 +420,8 @@ class FSImageLoader {
map.put("replication", 0);
map.put("type", inode.getType());
map.put("fileId", inode.getId());
map.put("childrenNum", dirmap.get(inode.getId()).length);
map.put("childrenNum", dirmap.containsKey(inode.getId()) ?
dirmap.get(inode.getId()).length : 0);
return map;
}
case SYMLINK: {

View File

@ -120,6 +120,11 @@ public class TestOfflineImageViewer {
}
}
// Create an empty directory
Path emptydir = new Path("/emptydir");
hdfs.mkdirs(emptydir);
writtenFiles.put(emptydir.toString(), hdfs.getFileStatus(emptydir));
// Get delegation tokens so we log the delegation token op
Token<?>[] delegationTokens = hdfs
.addDelegationTokens(TEST_RENEWER, null);
@ -205,8 +210,8 @@ public class TestOfflineImageViewer {
matcher = p.matcher(output.getBuffer());
assertTrue(matcher.find() && matcher.groupCount() == 1);
int totalDirs = Integer.parseInt(matcher.group(1));
// totalDirs includes root directory
assertEquals(NUM_DIRS + 1, totalDirs);
// totalDirs includes root directory and empty directory
assertEquals(NUM_DIRS + 2, totalDirs);
FileStatus maxFile = Collections.max(writtenFiles.values(),
new Comparator<FileStatus>() {
@ -259,7 +264,7 @@ public class TestOfflineImageViewer {
// verify the number of directories
FileStatus[] statuses = webhdfs.listStatus(new Path("/"));
assertEquals(NUM_DIRS, statuses.length);
assertEquals(NUM_DIRS + 1, statuses.length); // contains empty directory
// verify the number of files in the directory
statuses = webhdfs.listStatus(new Path("/dir0"));
@ -270,6 +275,10 @@ public class TestOfflineImageViewer {
FileStatus expected = writtenFiles.get("/dir0/file0");
compareFile(expected, status);
// LISTSTATUS operation to an empty directory
statuses = webhdfs.listStatus(new Path("/emptydir"));
assertEquals(0, statuses.length);
// LISTSTATUS operation to a invalid path
URL url = new URL("http://localhost:" + port +
"/webhdfs/v1/invalid/?op=LISTSTATUS");