浏览代码

HDFS-10474. hftp copy fails when file name with Chinese+special char in branch-2 (Contributed by Brahma Reddy Battula)

(cherry picked from commit be94ed6cebe40df99316c8f3d3086f359dd61242)
Brahma Reddy Battula 9 年之前
父节点
当前提交
bb0a2f0cf9

+ 17 - 0
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ServletUtil.java

@@ -18,6 +18,8 @@
 package org.apache.hadoop.util;
 
 import java.io.*;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Calendar;
 
 import javax.servlet.*;
@@ -113,6 +115,21 @@ public class ServletUtil {
     }
   }
 
+  /**
+   * Decode a string regarded as the path component of an URI.
+   *
+   * @param path the path component to decode
+   * @return decoded path, null if UTF-8 is not supported
+   * @throws URISyntaxException
+   */
+  public static String decodePath(final String path) {
+    try {
+      return new URI(path).getPath();
+    } catch (URISyntaxException e) {
+      throw new AssertionError("Failed to decode URI: " + path);
+    }
+  }
+
   /**
    * Parse and decode the path component from the given request.
    * @param request Http request to parse

+ 3 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -186,6 +186,9 @@ Release 2.7.3 - UNRELEASED
     HDFS-8581. ContentSummary on / skips further counts on yielding lock
     (J.Andreina via vinayakumarb)
 
+    HDFS-10474. hftp copy fails when file name with Chinese+special char
+    in branch-2 (Brahma Reddy Battula)
+
 Release 2.7.2 - 2016-01-25
 
   INCOMPATIBLE CHANGES

+ 1 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ListPathsServlet.java

@@ -69,7 +69,7 @@ public class ListPathsServlet extends DfsServlet {
       final XMLOutputter doc) throws IOException {
     final SimpleDateFormat ldf = df.get();
     doc.startTag(i.isDir() ? "directory" : "file");
-    doc.attribute("path", fullpath.toUri().getPath());
+    doc.attribute("path", ServletUtil.encodePath(fullpath.toUri().getPath()));
     doc.attribute("modified", ldf.format(new Date(i.getModificationTime())));
     doc.attribute("accesstime", ldf.format(new Date(i.getAccessTime())));
     if (!i.isDir()) {

+ 4 - 2
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/HftpFileSystem.java

@@ -443,12 +443,14 @@ public class HftpFileSystem extends FileSystem
               modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
               attrs.getValue("owner"), attrs.getValue("group"),
               HftpFileSystem.this.makeQualified(
-                  new Path(getUri().toString(), attrs.getValue("path"))))
+                  new Path(getUri().toString(), ServletUtil.decodePath(
+                      attrs.getValue("path")))))
         : new FileStatus(0L, true, 0, 0L,
               modif, atime, FsPermission.valueOf(attrs.getValue("permission")),
               attrs.getValue("owner"), attrs.getValue("group"),
               HftpFileSystem.this.makeQualified(
-                  new Path(getUri().toString(), attrs.getValue("path"))));
+                  new Path(getUri().toString(), ServletUtil.decodePath(
+                      attrs.getValue("path")))));
       fslist.add(fs);
     }
 

+ 1 - 1
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestHftpFileSystem.java

@@ -82,7 +82,7 @@ public class TestHftpFileSystem {
 
       // URI percent encodes, Request#getPathInfo decodes
       new Path("/foo bar/foo bar"), new Path("/foo?bar/foo?bar"),
-      new Path("/foo\">bar/foo\">bar"), };
+      new Path("/foo\">bar/foo\">bar"), new Path("/节节高@2X.png"), };
 
   @BeforeClass
   public static void setUp() throws Exception {