Browse Source

HADOOP-3561. Prevent the trash from deleting its parent directories.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.18@668482 13f79535-47bb-0310-9956-ffa450edef68
Christopher Douglas 17 years ago
parent
commit
b44383065c

+ 3 - 0
CHANGES.txt

@@ -596,6 +596,9 @@ Release 0.18.0 - Unreleased
     HADOOP-3545. Fixes a overflow problem in archives.
     (Mahadev Konar via ddas)
 
+    HADOOP-3561. Prevent the trash from deleting its parent directories.
+    (cdouglas)
+
 Release 0.17.0 - 2008-05-18
 
   INCOMPATIBLE CHANGES

+ 8 - 1
src/core/org/apache/hadoop/fs/Trash.java

@@ -84,10 +84,17 @@ public class Trash extends Configured {
     if (!fs.exists(path))                         // check that path exists
       throw new FileNotFoundException(path.toString());
 
-    if (path.makeQualified(fs).toString().startsWith(trash.toString())) {
+    String qpath = path.makeQualified(fs).toString();
+
+    if (qpath.startsWith(trash.toString())) {
       return false;                               // already in trash
     }
 
+    if (trash.getParent().toString().startsWith(qpath)) {
+      throw new IOException("Cannot move \"" + path +
+                            "\" to the trash, as it contains the trash");
+    }
+
     Path trashPath = new Path(current, path.getName());
 
     IOException cause = null;

+ 16 - 0
src/test/org/apache/hadoop/dfs/TestTrash.java

@@ -239,6 +239,22 @@ public class TestTrash extends TestCase {
         assertTrue(val == 0);
         checkTrash(fs, trashRoot, args[1]);
       }
+
+      // attempt to remove parent of trash
+      {
+        String[] args = new String[2];
+        args[0] = "-rmr";
+        args[1] = trashRoot.getParent().getParent().toString();
+        int val = -1;
+        try {
+          val = shell.run(args);
+        } catch (Exception e) {
+          System.err.println("Exception raised from Trash.run " +
+                             e.getLocalizedMessage()); 
+        }
+        assertTrue(val == -1);
+        assertTrue(fs.exists(trashRoot));
+      }
     } finally {
       try {
         fs.close();