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