001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.hadoop.util;
020
021 import java.io.File;
022 import java.io.IOException;
023
024 import org.apache.hadoop.classification.InterfaceAudience;
025 import org.apache.hadoop.classification.InterfaceStability;
026 import org.apache.hadoop.fs.FileUtil;
027 import org.apache.hadoop.fs.LocalFileSystem;
028 import org.apache.hadoop.fs.Path;
029 import org.apache.hadoop.fs.permission.FsPermission;
030
031 /**
032 * Class that provides utility functions for checking disk problem
033 */
034 @InterfaceAudience.Private
035 @InterfaceStability.Unstable
036 public class DiskChecker {
037 public static class DiskErrorException extends IOException {
038 public DiskErrorException(String msg) {
039 super(msg);
040 }
041
042 public DiskErrorException(String msg, Throwable cause) {
043 super(msg, cause);
044 }
045 }
046
047 public static class DiskOutOfSpaceException extends IOException {
048 public DiskOutOfSpaceException(String msg) {
049 super(msg);
050 }
051 }
052
053 /**
054 * The semantics of mkdirsWithExistsCheck method is different from the mkdirs
055 * method provided in the Sun's java.io.File class in the following way:
056 * While creating the non-existent parent directories, this method checks for
057 * the existence of those directories if the mkdir fails at any point (since
058 * that directory might have just been created by some other process).
059 * If both mkdir() and the exists() check fails for any seemingly
060 * non-existent directory, then we signal an error; Sun's mkdir would signal
061 * an error (return false) if a directory it is attempting to create already
062 * exists or the mkdir fails.
063 * @param dir
064 * @return true on success, false on failure
065 */
066 public static boolean mkdirsWithExistsCheck(File dir) {
067 if (dir.mkdir() || dir.exists()) {
068 return true;
069 }
070 File canonDir = null;
071 try {
072 canonDir = dir.getCanonicalFile();
073 } catch (IOException e) {
074 return false;
075 }
076 String parent = canonDir.getParent();
077 return (parent != null) &&
078 (mkdirsWithExistsCheck(new File(parent)) &&
079 (canonDir.mkdir() || canonDir.exists()));
080 }
081
082 /**
083 * Create the directory if it doesn't exist and check that dir is readable,
084 * writable and executable
085 *
086 * @param dir
087 * @throws DiskErrorException
088 */
089 public static void checkDir(File dir) throws DiskErrorException {
090 if (!mkdirsWithExistsCheck(dir)) {
091 throw new DiskErrorException("Can not create directory: "
092 + dir.toString());
093 }
094 checkDirAccess(dir);
095 }
096
097 /**
098 * Create the directory or check permissions if it already exists.
099 *
100 * The semantics of mkdirsWithExistsAndPermissionCheck method is different
101 * from the mkdirs method provided in the Sun's java.io.File class in the
102 * following way:
103 * While creating the non-existent parent directories, this method checks for
104 * the existence of those directories if the mkdir fails at any point (since
105 * that directory might have just been created by some other process).
106 * If both mkdir() and the exists() check fails for any seemingly
107 * non-existent directory, then we signal an error; Sun's mkdir would signal
108 * an error (return false) if a directory it is attempting to create already
109 * exists or the mkdir fails.
110 *
111 * @param localFS local filesystem
112 * @param dir directory to be created or checked
113 * @param expected expected permission
114 * @throws IOException
115 */
116 public static void mkdirsWithExistsAndPermissionCheck(
117 LocalFileSystem localFS, Path dir, FsPermission expected)
118 throws IOException {
119 File directory = localFS.pathToFile(dir);
120 boolean created = false;
121
122 if (!directory.exists())
123 created = mkdirsWithExistsCheck(directory);
124
125 if (created || !localFS.getFileStatus(dir).getPermission().equals(expected))
126 localFS.setPermission(dir, expected);
127 }
128
129 /**
130 * Create the local directory if necessary, check permissions and also ensure
131 * it can be read from and written into.
132 *
133 * @param localFS local filesystem
134 * @param dir directory
135 * @param expected permission
136 * @throws DiskErrorException
137 * @throws IOException
138 */
139 public static void checkDir(LocalFileSystem localFS, Path dir,
140 FsPermission expected)
141 throws DiskErrorException, IOException {
142 mkdirsWithExistsAndPermissionCheck(localFS, dir, expected);
143 checkDirAccess(localFS.pathToFile(dir));
144 }
145
146 /**
147 * Checks that the given file is a directory and that the current running
148 * process can read, write, and execute it.
149 *
150 * @param dir File to check
151 * @throws DiskErrorException if dir is not a directory, not readable, not
152 * writable, or not executable
153 */
154 private static void checkDirAccess(File dir) throws DiskErrorException {
155 if (!dir.isDirectory()) {
156 throw new DiskErrorException("Not a directory: "
157 + dir.toString());
158 }
159
160 checkAccessByFileMethods(dir);
161 }
162
163 /**
164 * Checks that the current running process can read, write, and execute the
165 * given directory by using methods of the File object.
166 *
167 * @param dir File to check
168 * @throws DiskErrorException if dir is not readable, not writable, or not
169 * executable
170 */
171 private static void checkAccessByFileMethods(File dir)
172 throws DiskErrorException {
173 if (!FileUtil.canRead(dir)) {
174 throw new DiskErrorException("Directory is not readable: "
175 + dir.toString());
176 }
177
178 if (!FileUtil.canWrite(dir)) {
179 throw new DiskErrorException("Directory is not writable: "
180 + dir.toString());
181 }
182
183 if (!FileUtil.canExecute(dir)) {
184 throw new DiskErrorException("Directory is not executable: "
185 + dir.toString());
186 }
187 }
188 }