HDFS-1750. ListPathsServlet should not use HdfsFileStatus.getLocalName() to get file name since it may return an empty string.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/hdfs/branches/branch-0.21@1083953 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 6dfb596..d2d9888 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -971,6 +971,9 @@
     HDFS-1598.  Directory listing on hftp:// does not show .*.crc files.
     (szetszwo)
 
+    HDFS-1750. ListPathsServlet should not use HdfsFileStatus.getLocalName()
+    to get file name since it may return an empty string.  (szetszwo)
+
 Release 0.20.3 - 2011-1-5
 
   IMPROVEMENTS
diff --git a/src/java/org/apache/hadoop/hdfs/server/namenode/ListPathsServlet.java b/src/java/org/apache/hadoop/hdfs/server/namenode/ListPathsServlet.java
index 747e199..ec40310 100644
--- a/src/java/org/apache/hadoop/hdfs/server/namenode/ListPathsServlet.java
+++ b/src/java/org/apache/hadoop/hdfs/server/namenode/ListPathsServlet.java
@@ -63,10 +63,11 @@
    * Node information includes path, modification, permission, owner and group.
    * For files, it also includes size, replication and block-size. 
    */
-  static void writeInfo(String parent, HdfsFileStatus i, XMLOutputter doc) throws IOException {
+  static void writeInfo(final Path fullpath, final HdfsFileStatus i,
+      final XMLOutputter doc) throws IOException {
     final SimpleDateFormat ldf = df.get();
     doc.startTag(i.isDir() ? "directory" : "file");
-    doc.attribute("path", i.getFullPath(new Path(parent)).toUri().getPath());
+    doc.attribute("path", fullpath.toUri().getPath());
     doc.attribute("modified", ldf.format(new Date(i.getModificationTime())));
     doc.attribute("accesstime", ldf.format(new Date(i.getAccessTime())));
     if (!i.isDir()) {
@@ -156,7 +157,7 @@
 
       HdfsFileStatus base = nnproxy.getFileInfo(path);
       if ((base != null) && base.isDir()) {
-        writeInfo(path, base, doc);
+        writeInfo(base.getFullPath(new Path(path)), base, doc);
       }
 
       Stack<String> pathstack = new Stack<String>();
@@ -177,7 +178,8 @@
             }
             HdfsFileStatus[] listing = thisListing.getPartialListing();
             for (HdfsFileStatus i : listing) {
-              String localName = i.getLocalName();
+              final Path fullpath = i.getFullPath(new Path(p));
+              final String localName = fullpath.getName();
               if (exclude.matcher(localName).matches()
                   || !filter.matcher(localName).matches()) {
                 continue;
@@ -185,7 +187,7 @@
               if (recur && i.isDir()) {
                 pathstack.push(new Path(p, localName).toUri().getPath());
               }
-              writeInfo(p, i, doc);
+              writeInfo(fullpath, i, doc);
             }
             lastReturnedName = thisListing.getLastName();
           } while (thisListing.hasMore());
diff --git a/src/test/hdfs/org/apache/hadoop/hdfs/TestListPathServlet.java b/src/test/hdfs/org/apache/hadoop/hdfs/TestListPathServlet.java
index b0dc998..df6666e 100644
--- a/src/test/hdfs/org/apache/hadoop/hdfs/TestListPathServlet.java
+++ b/src/test/hdfs/org/apache/hadoop/hdfs/TestListPathServlet.java
@@ -89,12 +89,18 @@
     createFile("/a", 1);
     createFile("/b", 1);
     mkdirs("/dir");
+
+    checkFile(new Path("/a"));
+    checkFile(new Path("/b"));
     checkStatus("/");
 
     // A directory with files and directories
     createFile("/dir/.a.crc", 1);
     createFile("/dir/b", 1);
     mkdirs("/dir/dir1");
+    
+    checkFile(new Path("/dir/.a.crc"));
+    checkFile(new Path("/dir/b"));
     checkStatus("/dir");
 
     // Non existent path
@@ -134,4 +140,36 @@
           found);
     }
   }
+
+  private void checkFile(final Path f) throws IOException {
+    final Path hdfspath = fs.makeQualified(f);
+    final FileStatus hdfsstatus = fs.getFileStatus(hdfspath);
+    FileSystem.LOG.info("hdfspath=" + hdfspath);
+
+    final Path hftppath = hftpFs.makeQualified(f);
+    final FileStatus hftpstatus = hftpFs.getFileStatus(hftppath);
+    FileSystem.LOG.info("hftppath=" + hftppath);
+    
+    Assert.assertEquals(hdfspath.toUri().getPath(),
+        hdfsstatus.getPath().toUri().getPath());
+    checkFileStatus(hdfsstatus, hftpstatus);
+  }
+
+  private static void checkFileStatus(final FileStatus expected,
+      final FileStatus computed) {
+    Assert.assertEquals(expected.getPath().toUri().getPath(),
+        computed.getPath().toUri().getPath());
+
+// TODO: test will fail if the following is un-commented. 
+//    Assert.assertEquals(expected.getAccessTime(), computed.getAccessTime());
+//    Assert.assertEquals(expected.getModificationTime(),
+//        computed.getModificationTime());
+
+    Assert.assertEquals(expected.getBlockSize(), computed.getBlockSize());
+    Assert.assertEquals(expected.getGroup(), computed.getGroup());
+    Assert.assertEquals(expected.getLen(), computed.getLen());
+    Assert.assertEquals(expected.getOwner(), computed.getOwner());
+    Assert.assertEquals(expected.getPermission(), computed.getPermission());
+    Assert.assertEquals(expected.getReplication(), computed.getReplication());
+  }
 }