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