From bf5b508074955d8ddcf3da5ac65cf72131526333 Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Thu, 5 Apr 2012 18:28:34 +0000 Subject: [PATCH] HADOOP-6963. In FileUtil.getDU(..), neither include the size of directories nor follow symbolic links. Contributed by Ravi Prakash git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1309994 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-common-project/hadoop-common/CHANGES.txt | 3 +++ hadoop-common-project/hadoop-common/pom.xml | 5 +++++ .../java/org/apache/hadoop/fs/FileUtil.java | 11 +++++++++-- .../java/org/apache/hadoop/fs/TestFileUtil.java | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 8e0f2c3db97..bac3f4a4a21 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -324,6 +324,9 @@ Release 2.0.0 - UNRELEASED HADOOP-8243. Security support broken in CLI (manual) failover controller (todd) + HADOOP-6963. In FileUtil.getDU(..), neither include the size of directories + nor follow symbolic links. (Ravi Prakash via szetszwo) + BREAKDOWN OF HADOOP-7454 SUBTASKS HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) diff --git a/hadoop-common-project/hadoop-common/pom.xml b/hadoop-common-project/hadoop-common/pom.xml index 41305460501..8b97faf37c9 100644 --- a/hadoop-common-project/hadoop-common/pom.xml +++ b/hadoop-common-project/hadoop-common/pom.xml @@ -78,6 +78,11 @@ commons-net compile + + commons-io + commons-io + compile + javax.servlet servlet-api diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java index 8e7aa302a61..ba9bb4eafee 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java @@ -483,11 +483,18 @@ public class FileUtil { if (!dir.isDirectory()) { return dir.length(); } else { - size = dir.length(); File[] allFiles = dir.listFiles(); if(allFiles != null) { for (int i = 0; i < allFiles.length; i++) { - size = size + getDU(allFiles[i]); + boolean isSymLink; + try { + isSymLink = org.apache.commons.io.FileUtils.isSymlink(allFiles[i]); + } catch(IOException ioe) { + isSymLink = true; + } + if(!isSymLink) { + size += getDU(allFiles[i]); + } } } return size; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java index 65c43435b75..0b74baeb6e7 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java @@ -93,6 +93,9 @@ public class TestFileUtil { // create files in partitioned directories createFile(partitioned, "part-r-00000", "foo"); createFile(partitioned, "part-r-00001", "bar"); + + // create a cycle using symlinks. Cycles should be handled + FileUtil.symLink(del.toString(), dir1.toString() + "/cycle"); } /** @@ -458,4 +461,18 @@ public class TestFileUtil { return result; } + + /** + * Test that getDU is able to handle cycles caused due to symbolic links + * and that directory sizes are not added to the final calculated size + * @throws IOException + */ + @Test + public void testGetDU() throws IOException { + setupDirs(); + + long du = FileUtil.getDU(TEST_DIR); + //Only two files (in partitioned) have 4 bytes each + Assert.assertEquals(du, 8); + } }