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 @Override 044 public void initialize(URI name, Configuration conf) throws IOException { 045 if (fs.getConf() == null) { 046 fs.initialize(name, conf); 047 } 048 String scheme = name.getScheme(); 049 if (!scheme.equals(fs.getUri().getScheme())) { 050 swapScheme = scheme; 051 } 052 } 053 054 /** 055 * Return the protocol scheme for the FileSystem. 056 * <p/> 057 * 058 * @return <code>file</code> 059 */ 060 @Override 061 public String getScheme() { 062 return "file"; 063 } 064 065 public FileSystem getRaw() { 066 return getRawFileSystem(); 067 } 068 069 public LocalFileSystem(FileSystem rawLocalFileSystem) { 070 super(rawLocalFileSystem); 071 } 072 073 /** Convert a path to a File. */ 074 public File pathToFile(Path path) { 075 return ((RawLocalFileSystem)fs).pathToFile(path); 076 } 077 078 @Override 079 public void copyFromLocalFile(boolean delSrc, Path src, Path dst) 080 throws IOException { 081 FileUtil.copy(this, src, this, dst, delSrc, getConf()); 082 } 083 084 @Override 085 public void copyToLocalFile(boolean delSrc, Path src, Path dst) 086 throws IOException { 087 FileUtil.copy(this, src, this, dst, delSrc, getConf()); 088 } 089 090 /** 091 * Moves files to a bad file directory on the same device, so that their 092 * storage will not be reused. 093 */ 094 @Override 095 public boolean reportChecksumFailure(Path p, FSDataInputStream in, 096 long inPos, 097 FSDataInputStream sums, long sumsPos) { 098 try { 099 // canonicalize f 100 File f = ((RawLocalFileSystem)fs).pathToFile(p).getCanonicalFile(); 101 102 // find highest writable parent dir of f on the same device 103 String device = new DF(f, getConf()).getMount(); 104 File parent = f.getParentFile(); 105 File dir = null; 106 while (parent != null && FileUtil.canWrite(parent) && 107 parent.toString().startsWith(device)) { 108 dir = parent; 109 parent = parent.getParentFile(); 110 } 111 112 if (dir==null) { 113 throw new IOException( 114 "not able to find the highest writable parent dir"); 115 } 116 117 // move the file there 118 File badDir = new File(dir, "bad_files"); 119 if (!badDir.mkdirs()) { 120 if (!badDir.isDirectory()) { 121 throw new IOException("Mkdirs failed to create " + badDir.toString()); 122 } 123 } 124 String suffix = "." + rand.nextInt(); 125 File badFile = new File(badDir, f.getName()+suffix); 126 LOG.warn("Moving bad file " + f + " to " + badFile); 127 in.close(); // close it first 128 boolean b = f.renameTo(badFile); // rename it 129 if (!b) { 130 LOG.warn("Ignoring failure of renameTo"); 131 } 132 // move checksum file too 133 File checkFile = ((RawLocalFileSystem)fs).pathToFile(getChecksumFile(p)); 134 // close the stream before rename to release the file handle 135 sums.close(); 136 b = checkFile.renameTo(new File(badDir, checkFile.getName()+suffix)); 137 if (!b) { 138 LOG.warn("Ignoring failure of renameTo"); 139 } 140 } catch (IOException e) { 141 LOG.warn("Error moving bad file " + p + ": " + e); 142 } 143 return false; 144 } 145 }