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