diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 94a9f884cf..311e3b055a 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -258,6 +258,9 @@ Branch-2 ( Unreleased changes ) called with a null key or value. (Madhukara Phatak and Suresh Srinivas via harsh) + HADOOP-7818. DiskChecker#checkDir should fail if the directory is + not executable. (Madhukara Phatak via harsh) + BUG FIXES HADOOP-8372. NetUtils.normalizeHostName() incorrectly handles hostname diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DiskChecker.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DiskChecker.java index fb21ab518c..ac635a9c22 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DiskChecker.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DiskChecker.java @@ -78,26 +78,32 @@ public static boolean mkdirsWithExistsCheck(File dir) { } /** - * Create the directory if it doesn't exist and + * Create the directory if it doesn't exist and check that dir is readable, + * writable and executable + * * @param dir * @throws DiskErrorException */ public static void checkDir(File dir) throws DiskErrorException { if (!mkdirsWithExistsCheck(dir)) - throw new DiskErrorException("can not create directory: " + throw new DiskErrorException("Can not create directory: " + dir.toString()); - + if (!dir.isDirectory()) - throw new DiskErrorException("not a directory: " + throw new DiskErrorException("Not a directory: " + dir.toString()); - + if (!dir.canRead()) - throw new DiskErrorException("directory is not readable: " + throw new DiskErrorException("Directory is not readable: " + dir.toString()); - + if (!dir.canWrite()) - throw new DiskErrorException("directory is not writable: " + throw new DiskErrorException("Directory is not writable: " + dir.toString()); + + if (!dir.canExecute()) + throw new DiskErrorException("Directory is not executable: " + + dir.toString()); } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java index 3748cfe347..e8ff9fffd8 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java @@ -124,4 +124,53 @@ private void _checkDirs(boolean isDir, FsPermission perm, boolean success) } System.out.println("checkDir success: "+ success); } + + /** + * These test cases test to test the creation of a local folder with correct + * permission for result of mapper. + */ + + @Test + public void testCheckDir_normal_local() throws Throwable { + _checkDirs(true, "755", true); + } + + @Test + public void testCheckDir_notDir_local() throws Throwable { + _checkDirs(false, "000", false); + } + + @Test + public void testCheckDir_notReadable_local() throws Throwable { + _checkDirs(true, "000", false); + } + + @Test + public void testCheckDir_notWritable_local() throws Throwable { + _checkDirs(true, "444", false); + } + + @Test + public void testCheckDir_notListable_local() throws Throwable { + _checkDirs(true, "666", false); + } + + private void _checkDirs(boolean isDir, String perm, boolean success) + throws Throwable { + File localDir = File.createTempFile("test", "tmp"); + localDir.delete(); + localDir.mkdir(); + Runtime.getRuntime().exec( + "chmod " + perm + " " + localDir.getAbsolutePath()).waitFor(); + try { + DiskChecker.checkDir(localDir); + assertTrue("checkDir success", success); + } catch (DiskErrorException e) { + e.printStackTrace(); + assertFalse("checkDir success", success); + } + localDir.delete(); + System.out.println("checkDir success: " + success); + + } }