[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.util; |
---|
| 20 | |
---|
| 21 | import java.io.File; |
---|
| 22 | import java.io.IOException; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Class that provides utility functions for checking disk problem |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | public class DiskChecker { |
---|
| 29 | |
---|
| 30 | public static class DiskErrorException extends IOException { |
---|
| 31 | public DiskErrorException(String msg) { |
---|
| 32 | super(msg); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | public static class DiskOutOfSpaceException extends IOException { |
---|
| 37 | public DiskOutOfSpaceException(String msg) { |
---|
| 38 | super(msg); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * The semantics of mkdirsWithExistsCheck method is different from the mkdirs |
---|
| 44 | * method provided in the Sun's java.io.File class in the following way: |
---|
| 45 | * While creating the non-existent parent directories, this method checks for |
---|
| 46 | * the existence of those directories if the mkdir fails at any point (since |
---|
| 47 | * that directory might have just been created by some other process). |
---|
| 48 | * If both mkdir() and the exists() check fails for any seemingly |
---|
| 49 | * non-existent directory, then we signal an error; Sun's mkdir would signal |
---|
| 50 | * an error (return false) if a directory it is attempting to create already |
---|
| 51 | * exists or the mkdir fails. |
---|
| 52 | * @param dir |
---|
| 53 | * @return true on success, false on failure |
---|
| 54 | */ |
---|
| 55 | public static boolean mkdirsWithExistsCheck(File dir) { |
---|
| 56 | if (dir.mkdir() || dir.exists()) { |
---|
| 57 | return true; |
---|
| 58 | } |
---|
| 59 | File canonDir = null; |
---|
| 60 | try { |
---|
| 61 | canonDir = dir.getCanonicalFile(); |
---|
| 62 | } catch (IOException e) { |
---|
| 63 | return false; |
---|
| 64 | } |
---|
| 65 | String parent = canonDir.getParent(); |
---|
| 66 | return (parent != null) && |
---|
| 67 | (mkdirsWithExistsCheck(new File(parent)) && |
---|
| 68 | (canonDir.mkdir() || canonDir.exists())); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public static void checkDir(File dir) throws DiskErrorException { |
---|
| 72 | if (!mkdirsWithExistsCheck(dir)) |
---|
| 73 | throw new DiskErrorException("can not create directory: " |
---|
| 74 | + dir.toString()); |
---|
| 75 | |
---|
| 76 | if (!dir.isDirectory()) |
---|
| 77 | throw new DiskErrorException("not a directory: " |
---|
| 78 | + dir.toString()); |
---|
| 79 | |
---|
| 80 | if (!dir.canRead()) |
---|
| 81 | throw new DiskErrorException("directory is not readable: " |
---|
| 82 | + dir.toString()); |
---|
| 83 | |
---|
| 84 | if (!dir.canWrite()) |
---|
| 85 | throw new DiskErrorException("directory is not writable: " |
---|
| 86 | + dir.toString()); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | } |
---|