فهرست منبع

HADOOP-5551. Merge -r 760097:760098 from trunk to branch 0.19.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.19@760107 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Shvachko 16 سال پیش
والد
کامیت
a2a88629fa

+ 3 - 0
CHANGES.txt

@@ -98,6 +98,9 @@ Release 0.19.2 - Unreleased
     HADOOP-4780. Cache the size of directories in DistributedCache, avoiding
     long delays in recalculating it. (He Yongqiang via cdouglas)
 
+    HADOOP-5551. Prevent directory destruction on file create.
+    (Brian Bockelman via shv)
+
 Release 0.19.1 - 2009-02-23 
 
     HADOOP-5225. Workaround for tmp file handling in HDFS. sync() is 

+ 8 - 1
src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

@@ -1029,8 +1029,15 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
     if (!DFSUtil.isValidName(src)) {
       throw new IOException("Invalid file name: " + src);
     }
+
+    // Verify that the destination does not exist as a directory already.
+    boolean pathExists = dir.exists(src);
+    if (pathExists && dir.isDir(src)) {
+      throw new IOException("Cannot create file "+ src + "; already exists as a directory.");
+    }
+
     if (isPermissionEnabled) {
-      if (append || (overwrite && dir.exists(src))) {
+      if (append || (overwrite && pathExists)) {
         checkPathAccess(src, FsAction.WRITE);
       }
       else {

+ 16 - 0
src/test/org/apache/hadoop/hdfs/TestFileCreation.java

@@ -187,6 +187,22 @@ public class TestFileCreation extends junit.framework.TestCase {
       System.out.println(fs.getFileStatus(path).isDir()); 
       assertTrue("/ should be a directory", 
                  fs.getFileStatus(path).isDir() == true);
+
+      //
+      // Create a directory inside /, then try to overwrite it
+      //
+      Path dir1 = new Path("/test_dir");
+      fs.mkdirs(dir1);
+      System.out.println("createFile: Creating " + dir1.getName() + 
+        " for overwrite of existing directory.");
+      try {
+        fs.create(dir1, true); // Create path, overwrite=true
+        fs.close();
+        assertTrue("Did not prevent directory from being overwritten.", false);
+      } catch (IOException ie) {
+        if (!ie.getMessage().contains("already exists as a directory."))
+          throw ie;
+      }
       
       // create a new file in home directory. Do not close it.
       //