source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/fs/LocalFileSystem.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.fs;
20
21import java.io.*;
22import java.net.URI;
23import java.util.*;
24
25/****************************************************************
26 * Implement the FileSystem API for the checksumed local filesystem.
27 *
28 *****************************************************************/
29public class LocalFileSystem extends ChecksumFileSystem {
30  static final URI NAME = URI.create("file:///");
31  static private Random rand = new Random();
32  FileSystem rfs;
33 
34  public LocalFileSystem() {
35    this(new RawLocalFileSystem());
36  }
37 
38  public FileSystem getRaw() {
39    return rfs;
40  }
41   
42  public LocalFileSystem(FileSystem rawLocalFileSystem) {
43    super(rawLocalFileSystem);
44    rfs = rawLocalFileSystem;
45  }
46   
47  /** Convert a path to a File. */
48  public File pathToFile(Path path) {
49    return ((RawLocalFileSystem)fs).pathToFile(path);
50  }
51
52  @Override
53  public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
54    throws IOException {
55    FileUtil.copy(this, src, this, dst, delSrc, getConf());
56  }
57
58  @Override
59  public void copyToLocalFile(boolean delSrc, Path src, Path dst)
60    throws IOException {
61    FileUtil.copy(this, src, this, dst, delSrc, getConf());
62  }
63
64  /**
65   * Moves files to a bad file directory on the same device, so that their
66   * storage will not be reused.
67   */
68  public boolean reportChecksumFailure(Path p, FSDataInputStream in,
69                                       long inPos,
70                                       FSDataInputStream sums, long sumsPos) {
71    try {
72      // canonicalize f
73      File f = ((RawLocalFileSystem)fs).pathToFile(p).getCanonicalFile();
74     
75      // find highest writable parent dir of f on the same device
76      String device = new DF(f, getConf()).getMount();
77      File parent = f.getParentFile();
78      File dir = null;
79      while (parent!=null && parent.canWrite() && parent.toString().startsWith(device)) {
80        dir = parent;
81        parent = parent.getParentFile();
82      }
83
84      if (dir==null) {
85        throw new IOException(
86                              "not able to find the highest writable parent dir");
87      }
88       
89      // move the file there
90      File badDir = new File(dir, "bad_files");
91      if (!badDir.mkdirs()) {
92        if (!badDir.isDirectory()) {
93          throw new IOException("Mkdirs failed to create " + badDir.toString());
94        }
95      }
96      String suffix = "." + rand.nextInt();
97      File badFile = new File(badDir, f.getName()+suffix);
98      LOG.warn("Moving bad file " + f + " to " + badFile);
99      in.close();                               // close it first
100      f.renameTo(badFile);                      // rename it
101
102      // move checksum file too
103      File checkFile = ((RawLocalFileSystem)fs).pathToFile(getChecksumFile(p));
104      checkFile.renameTo(new File(badDir, checkFile.getName()+suffix));
105
106    } catch (IOException e) {
107      LOG.warn("Error moving bad file " + p + ": " + e);
108    }
109    return false;
110  }
111}
Note: See TracBrowser for help on using the repository browser.