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.fs;
020
021 import java.io.*;
022 import java.net.URI;
023 import java.util.*;
024
025 import org.apache.hadoop.classification.InterfaceAudience;
026 import org.apache.hadoop.classification.InterfaceStability;
027 import org.apache.hadoop.conf.Configuration;
028
029 /****************************************************************
030 * Implement the FileSystem API for the checksumed local filesystem.
031 *
032 *****************************************************************/
033 @InterfaceAudience.Public
034 @InterfaceStability.Stable
035 public class LocalFileSystem extends ChecksumFileSystem {
036 static final URI NAME = URI.create("file:///");
037 static private Random rand = new Random();
038
039 public LocalFileSystem() {
040 this(new RawLocalFileSystem());
041 }
042
043 public FileSystem getRaw() {
044 return getRawFileSystem();
045 }
046
047 public LocalFileSystem(FileSystem rawLocalFileSystem) {
048 super(rawLocalFileSystem);
049 }
050
051 /** Convert a path to a File. */
052 public File pathToFile(Path path) {
053 return ((RawLocalFileSystem)fs).pathToFile(path);
054 }
055
056 @Override
057 public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
058 throws IOException {
059 FileUtil.copy(this, src, this, dst, delSrc, getConf());
060 }
061
062 @Override
063 public void copyToLocalFile(boolean delSrc, Path src, Path dst)
064 throws IOException {
065 FileUtil.copy(this, src, this, dst, delSrc, getConf());
066 }
067
068 /**
069 * Moves files to a bad file directory on the same device, so that their
070 * storage will not be reused.
071 */
072 public boolean reportChecksumFailure(Path p, FSDataInputStream in,
073 long inPos,
074 FSDataInputStream sums, long sumsPos) {
075 try {
076 // canonicalize f
077 File f = ((RawLocalFileSystem)fs).pathToFile(p).getCanonicalFile();
078
079 // find highest writable parent dir of f on the same device
080 String device = new DF(f, getConf()).getMount();
081 File parent = f.getParentFile();
082 File dir = null;
083 while (parent!=null && parent.canWrite() && parent.toString().startsWith(device)) {
084 dir = parent;
085 parent = parent.getParentFile();
086 }
087
088 if (dir==null) {
089 throw new IOException(
090 "not able to find the highest writable parent dir");
091 }
092
093 // move the file there
094 File badDir = new File(dir, "bad_files");
095 if (!badDir.mkdirs()) {
096 if (!badDir.isDirectory()) {
097 throw new IOException("Mkdirs failed to create " + badDir.toString());
098 }
099 }
100 String suffix = "." + rand.nextInt();
101 File badFile = new File(badDir, f.getName()+suffix);
102 LOG.warn("Moving bad file " + f + " to " + badFile);
103 in.close(); // close it first
104 boolean b = f.renameTo(badFile); // rename it
105 if (!b) {
106 LOG.warn("Ignoring failure of renameTo");
107 }
108 // move checksum file too
109 File checkFile = ((RawLocalFileSystem)fs).pathToFile(getChecksumFile(p));
110 b = checkFile.renameTo(new File(badDir, checkFile.getName()+suffix));
111 if (!b) {
112 LOG.warn("Ignoring failure of renameTo");
113 }
114 } catch (IOException e) {
115 LOG.warn("Error moving bad file " + p + ": " + e);
116 }
117 return false;
118 }
119 }