浏览代码

HADOOP-438. Limit the length of paths permitted by DFS. Contributed by Wendy.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@443455 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 年之前
父节点
当前提交
a68599d879

+ 4 - 0
CHANGES.txt

@@ -7,6 +7,10 @@ Trunk (unreleased changes)
    so that things are not shown to be 100% complete until they are in
    fact finished.  (omalley via cutting) 
 
+2. HADOOP-438.  Limit the length of absolute paths in DFS, since the
+   file format used to store pathnames has some limitations.
+   (Wendy Chien via cutting)
+
 
 Release 0.6.1 - 2006-08-13
 

+ 5 - 0
src/java/org/apache/hadoop/dfs/FSConstants.java

@@ -106,6 +106,11 @@ public interface FSConstants {
     public static long LEASE_PERIOD = 60 * 1000;
     public static int READ_TIMEOUT = 60 * 1000;
 
+    // We need to limit the length and depth of a path in the filesystem.  HADOOP-438
+    // Currently we set the maximum length to 8k characters and the maximum depth to 1k.  
+    public static int MAX_PATH_LENGTH = 8000;
+    public static int MAX_PATH_DEPTH = 1000;
+    
     //TODO mb@media-style.com: should be conf injected?
     public static final int BUFFER_SIZE = new Configuration().getInt("io.file.buffer.size", 4096);
 

+ 25 - 0
src/java/org/apache/hadoop/dfs/NameNode.java

@@ -17,6 +17,7 @@ package org.apache.hadoop.dfs;
 
 import org.apache.commons.logging.*;
 
+import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.*;
 import org.apache.hadoop.ipc.*;
 import org.apache.hadoop.conf.*;
@@ -199,6 +200,10 @@ public class NameNode implements ClientProtocol, DatanodeProtocol, FSConstants {
     ) throws IOException {
        stateChangeLog.debug("*DIR* NameNode.create: file "
             +src+" for "+clientName+" at "+clientMachine);
+       if (!checkPathLength(src)) {
+           throw new IOException("create: Pathname too long.  Limit " 
+               + MAX_PATH_LENGTH + " characters, " + MAX_PATH_DEPTH + " levels.");
+       }
        Object results[] = namesystem.startFile(new UTF8(src), 
                                                 new UTF8(clientName), 
                                                 new UTF8(clientMachine), 
@@ -304,6 +309,10 @@ public class NameNode implements ClientProtocol, DatanodeProtocol, FSConstants {
      */
     public boolean rename(String src, String dst) throws IOException {
         stateChangeLog.debug("*DIR* NameNode.rename: " + src + " to " + dst );
+        if (!checkPathLength(dst)) {
+            throw new IOException("rename: Pathname too long.  Limit " 
+                + MAX_PATH_LENGTH + " characters, " + MAX_PATH_DEPTH + " levels.");
+        }
         boolean ret = namesystem.renameTo(new UTF8(src), new UTF8(dst));
         if (ret) {
             myMetrics.renameFile();
@@ -330,10 +339,26 @@ public class NameNode implements ClientProtocol, DatanodeProtocol, FSConstants {
         return namesystem.isDir(new UTF8(src));
     }
 
+    /**
+     * Check path length does not exceed maximum.  Returns true if
+     * length and depth are okay.  Returns false if length is too long 
+     * or depth is too great.
+     * 
+     */
+    private boolean checkPathLength(String src) {
+        Path srcPath = new Path(src);
+        return (src.length() <= MAX_PATH_LENGTH &&
+                srcPath.depth() <= MAX_PATH_DEPTH);
+    }
+    
     /**
      */
     public boolean mkdirs(String src) throws IOException {
         stateChangeLog.debug("*DIR* NameNode.mkdirs: " + src );
+        if (!checkPathLength(src)) {
+            throw new IOException("mkdirs: Pathname too long.  Limit " 
+                + MAX_PATH_LENGTH + " characters, " + MAX_PATH_DEPTH + " levels.");
+        }
         return namesystem.mkdirs(new UTF8(src));
     }
 

+ 6 - 0
src/java/org/apache/hadoop/fs/Path.java

@@ -174,5 +174,11 @@ public class Path implements Comparable {
     Path that = (Path)o;
     return this.toString().compareTo(that.toString());
   }
+  
+  /** Return the number of elements in this path. */
+  public int depth() {
+    return elements.length;
+  }
 
 }
+