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 | |
---|
19 | package org.apache.hadoop.fs; |
---|
20 | |
---|
21 | import java.io.*; |
---|
22 | import java.net.URI; |
---|
23 | import java.util.*; |
---|
24 | |
---|
25 | /**************************************************************** |
---|
26 | * Implement the FileSystem API for the checksumed local filesystem. |
---|
27 | * |
---|
28 | *****************************************************************/ |
---|
29 | public 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 | } |
---|