Browse Source

Merge -r 540358:540359 from trunk to 0.13 branch. Fixes: HADOOP-1386.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/branches/branch-0.13@540360 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 năm trước cách đây
mục cha
commit
e58b29f991

+ 5 - 0
CHANGES.txt

@@ -410,6 +410,11 @@ Branch 0.13 (unreleased changes)
 123. HADOOP-1385.  Fix MD5Hash#hashCode() to generally hash to more
      than 256 values.  (omalley via cutting)
 
+124. HADOOP-1386.  Fix Path to not permit the empty string as a
+     path, as this has lead to accidental file deletion.  Instead
+     force applications to use "." to name the default directory.
+     (Hairong Kuang via cutting)
+
 
 Release 0.12.3 - 2007-04-06
 

+ 1 - 1
src/contrib/hbase/src/java/org/apache/hadoop/hbase/HStoreFile.java

@@ -54,7 +54,7 @@ public class HStoreFile implements HConstants, WritableComparable {
    */
   public HStoreFile(Configuration conf) {
     this.conf = conf;
-    this.dir = new Path("");
+    this.dir = new Path(Path.CUR_DIR);
     this.regionName = new Text();
     this.colFamily = new Text();
     this.fileId = 0;

+ 2 - 2
src/java/org/apache/hadoop/fs/FsShell.java

@@ -1015,13 +1015,13 @@ public class FsShell extends ToolBase {
         if (i < argv.length) {
           exitCode = doall(cmd, argv, conf, i);
         } else {
-          ls("", false);
+          ls(Path.CUR_DIR, false);
         } 
       } else if ("-lsr".equals(cmd)) {
         if (i < argv.length) {
           exitCode = doall(cmd, argv, conf, i);
         } else {
-          ls("", true);
+          ls(Path.CUR_DIR, true);
         } 
       } else if ("-mv".equals(cmd)) {
         exitCode = rename(argv, conf);

+ 5 - 1
src/java/org/apache/hadoop/fs/InMemoryFileSystem.java

@@ -68,7 +68,11 @@ public class InMemoryFileSystem extends ChecksumFileSystem {
       int size = Integer.parseInt(conf.get("fs.inmemory.size.mb", "100"));
       this.fsSize = size * 1024 * 1024;
       this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority());
-      this.staticWorkingDir = new Path(this.uri.getPath());
+      String path = this.uri.getPath();
+      if (path.length() == 0) {
+        path = Path.CUR_DIR;
+      }
+      this.staticWorkingDir = new Path(path);
       LOG.info("Initialized InMemoryFileSystem: " + uri.toString() + 
                " of size (in bytes): " + fsSize);
     }

+ 18 - 2
src/java/org/apache/hadoop/fs/Path.java

@@ -18,7 +18,6 @@
 
 package org.apache.hadoop.fs;
 
-import java.util.*;
 import java.net.*;
 import java.io.*;
 
@@ -34,6 +33,8 @@ public class Path implements Comparable {
   public static final String SEPARATOR = "/";
   public static final char SEPARATOR_CHAR = '/';
   
+  public static final String CUR_DIR = ".";
+  
   static final boolean WINDOWS
     = System.getProperty("os.name").startsWith("Windows");
 
@@ -71,9 +72,23 @@ public class Path implements Comparable {
                normalizePath(resolved.getPath()));
   }
 
+  private void checkPathArg( String path ) {
+    // disallow construction of a Path from an empty string
+    if ( path == null ) {
+      throw new IllegalArgumentException(
+          "Can not create a Path from a null string");
+    }
+    if( path.length() == 0 ) {
+       throw new IllegalArgumentException(
+           "Can not create a Path from an empty string");
+    }   
+  }
+  
   /** Construct a path from a String.  Path strings are URIs, but with
    * unescaped elements and some additional normalization. */
   public Path(String pathString) {
+    checkPathArg( pathString );
+    
     // We can't use 'new URI(String)' directly, since it assumes things are
     // escaped, which we don't require of Paths. 
     
@@ -113,6 +128,7 @@ public class Path implements Comparable {
 
   /** Construct a Path from components. */
   public Path(String scheme, String authority, String path) {
+    checkPathArg( path );
     initialize(scheme, authority, path);
   }
 
@@ -183,7 +199,7 @@ public class Path implements Comparable {
     }
     String parent;
     if (lastSlash==-1) {
-      parent = "";
+      parent = CUR_DIR;
     } else {
       int end = hasWindowsDrive(path, true) ? 3 : 0;
       parent = path.substring(0, lastSlash==end?end+1:lastSlash);

+ 14 - 7
src/test/org/apache/hadoop/fs/TestPath.java

@@ -28,7 +28,14 @@ public class TestPath extends TestCase {
     toStringTest("/foo/bar");
     toStringTest("foo");
     toStringTest("foo/bar");
-    toStringTest("");
+    boolean emptyException = false;
+    try {
+      toStringTest("");
+    } catch (IllegalArgumentException e) {
+      // expect to receive an IllegalArgumentException
+      emptyException = true;
+    }
+    assertTrue(emptyException);
     if (Path.WINDOWS) {
       toStringTest("c:");
       toStringTest("c:/");
@@ -60,7 +67,7 @@ public class TestPath extends TestCase {
     assertTrue(new Path("/foo").isAbsolute());
     assertFalse(new Path("foo").isAbsolute());
     assertFalse(new Path("foo/bar").isAbsolute());
-    assertFalse(new Path("").isAbsolute());
+    assertFalse(new Path(".").isAbsolute());
     if (Path.WINDOWS) {
       assertTrue(new Path("c:/a/b").isAbsolute());
       assertFalse(new Path("c:a/b").isAbsolute());
@@ -77,14 +84,14 @@ public class TestPath extends TestCase {
   }
 
   public void testChild() {
-    assertEquals(new Path(""), new Path("", ""));
-    assertEquals(new Path("/"), new Path("/", ""));
-    assertEquals(new Path("/"), new Path("", "/"));
+    assertEquals(new Path("."), new Path(".", "."));
+    assertEquals(new Path("/"), new Path("/", "."));
+    assertEquals(new Path("/"), new Path(".", "/"));
     assertEquals(new Path("/foo"), new Path("/", "foo"));
     assertEquals(new Path("/foo/bar"), new Path("/foo", "bar"));
     assertEquals(new Path("/foo/bar/baz"), new Path("/foo/bar", "baz"));
     assertEquals(new Path("/foo/bar/baz"), new Path("/foo", "bar/baz"));
-    assertEquals(new Path("foo"), new Path("", "foo"));
+    assertEquals(new Path("foo"), new Path(".", "foo"));
     assertEquals(new Path("foo/bar"), new Path("foo", "bar"));
     assertEquals(new Path("foo/bar/baz"), new Path("foo", "bar/baz"));
     assertEquals(new Path("foo/bar/baz"), new Path("foo/bar", "baz"));
@@ -122,7 +129,7 @@ public class TestPath extends TestCase {
     
     assertEquals(new Path("/foo/bar","../../boo/bud").toString(), "/boo/bud");
     assertEquals(new Path("foo/bar","../../boo/bud").toString(), "boo/bud");
-    assertEquals(new Path("","boo/bud").toString(), "boo/bud");
+    assertEquals(new Path(".","boo/bud").toString(), "boo/bud");
 
     assertEquals(new Path("/foo/bar/baz","../../boo/bud").toString(), "/foo/boo/bud");
     assertEquals(new Path("foo/bar/baz","../../boo/bud").toString(), "foo/boo/bud");