Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 14 | // |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | package com.google.devtools.build.lib.vfs; |
| 16 | |
ichern | 03be73e | 2019-10-22 10:16:44 -0700 | [diff] [blame] | 17 | import static java.nio.file.StandardOpenOption.READ; |
| 18 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
| 20 | import com.google.devtools.build.lib.profiler.Profiler; |
| 21 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
ccalvarin | c2f1896 | 2018-08-22 15:08:40 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.DigestHashFunction.DefaultHashFunctionNotSetException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.io.FileInputStream; |
| 24 | import java.io.FileNotFoundException; |
| 25 | import java.io.FileOutputStream; |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 26 | import java.io.FilterInputStream; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.io.OutputStream; |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 30 | import java.nio.channels.ReadableByteChannel; |
| 31 | import java.nio.file.Files; |
ichern | 03be73e | 2019-10-22 10:16:44 -0700 | [diff] [blame] | 32 | import java.util.EnumSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 34 | /** This class implements the FileSystem interface using direct calls to the UNIX filesystem. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | @ThreadSafe |
shahan | b1dd4e3 | 2018-05-09 08:23:31 -0700 | [diff] [blame] | 36 | public abstract class AbstractFileSystem extends FileSystem { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | |
| 38 | protected static final String ERR_PERMISSION_DENIED = " (Permission denied)"; |
| 39 | protected static final Profiler profiler = Profiler.instance(); |
| 40 | |
ccalvarin | c2f1896 | 2018-08-22 15:08:40 -0700 | [diff] [blame] | 41 | public AbstractFileSystem() throws DefaultHashFunctionNotSetException {} |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 42 | |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 43 | public AbstractFileSystem(DigestHashFunction digestFunction) { |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 44 | super(digestFunction); |
| 45 | } |
| 46 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 48 | protected InputStream getInputStream(Path path) throws IOException { |
Googler | 8910087 | 2016-07-26 15:52:48 +0000 | [diff] [blame] | 49 | // This loop is a workaround for an apparent bug in FileInputStream.open, which delegates |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | // ultimately to JVM_Open in the Hotspot JVM. This call is not EINTR-safe, so we must do the |
| 51 | // retry here. |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 52 | for (; ; ) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | try { |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 54 | return createMaybeProfiledInputStream(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | } catch (FileNotFoundException e) { |
| 56 | if (e.getMessage().endsWith("(Interrupted system call)")) { |
| 57 | continue; |
| 58 | } else { |
| 59 | throw e; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 65 | /** Allows the mapping of Path to InputStream to be overridden in subclasses. */ |
| 66 | protected InputStream createFileInputStream(Path path) throws IOException { |
| 67 | return new FileInputStream(path.toString()); |
| 68 | } |
| 69 | |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 70 | /** Returns either normal or profiled FileInputStream. */ |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 71 | private InputStream createMaybeProfiledInputStream(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | final String name = path.toString(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 73 | if (profiler.isActive() |
| 74 | && (profiler.isProfiling(ProfilerTask.VFS_READ) |
| 75 | || profiler.isProfiling(ProfilerTask.VFS_OPEN))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | long startTime = Profiler.nanoTimeMaybe(); |
| 77 | try { |
| 78 | // Replace default FileInputStream instance with the custom one that does profiling. |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 79 | return new ProfiledInputStream(createFileInputStream(path), name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | } finally { |
| 81 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name); |
| 82 | } |
| 83 | } else { |
| 84 | // Use normal FileInputStream instance if profiler is not enabled. |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 85 | return createFileInputStream(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 89 | @Override |
ichern | 03be73e | 2019-10-22 10:16:44 -0700 | [diff] [blame] | 90 | protected ReadableByteChannel createReadableByteChannel(Path path) throws IOException { |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 91 | final String name = path.toString(); |
| 92 | if (profiler.isActive() |
| 93 | && (profiler.isProfiling(ProfilerTask.VFS_READ) |
| 94 | || profiler.isProfiling(ProfilerTask.VFS_OPEN))) { |
| 95 | long startTime = Profiler.nanoTimeMaybe(); |
| 96 | try { |
| 97 | // Currently, we do not proxy ReadableByteChannel for profiling. |
ichern | 03be73e | 2019-10-22 10:16:44 -0700 | [diff] [blame] | 98 | return Files.newByteChannel(java.nio.file.Paths.get(name), EnumSet.of(READ)); |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 99 | } finally { |
| 100 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name); |
| 101 | } |
| 102 | } else { |
| 103 | return Files.newByteChannel(java.nio.file.Paths.get(name)); |
| 104 | } |
| 105 | } |
| 106 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | /** |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 108 | * Returns either normal or profiled FileOutputStream. Should be used by subclasses to create |
| 109 | * default OutputStream instance. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | */ |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 111 | protected OutputStream createFileOutputStream(Path path, boolean append) |
| 112 | throws FileNotFoundException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | final String name = path.toString(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 114 | if (profiler.isActive() |
| 115 | && (profiler.isProfiling(ProfilerTask.VFS_WRITE) |
| 116 | || profiler.isProfiling(ProfilerTask.VFS_OPEN))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | long startTime = Profiler.nanoTimeMaybe(); |
| 118 | try { |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 119 | return new ProfiledFileOutputStream(name, append); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | } finally { |
| 121 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name); |
| 122 | } |
| 123 | } else { |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 124 | return new FileOutputStream(name, append); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 129 | protected OutputStream getOutputStream(Path path, boolean append) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 130 | try { |
| 131 | return createFileOutputStream(path, append); |
| 132 | } catch (FileNotFoundException e) { |
| 133 | // Why does it throw a *FileNotFoundException* if it can't write? |
| 134 | // That does not make any sense! And its in a completely different |
| 135 | // format than in other situations, no less! |
| 136 | if (e.getMessage().equals(path + ERR_PERMISSION_DENIED)) { |
| 137 | throw new FileAccessException(e.getMessage()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | } |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 139 | throw e; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 142 | |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 143 | private static final class ProfiledInputStream extends FilterInputStream { |
| 144 | private final InputStream impl; |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 145 | private final String name; |
| 146 | |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 147 | public ProfiledInputStream(InputStream impl, String name) { |
| 148 | super(impl); |
| 149 | this.impl = impl; |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 150 | this.name = name; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int read() throws IOException { |
| 155 | long startTime = Profiler.nanoTimeMaybe(); |
| 156 | try { |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 157 | return impl.read(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 158 | } finally { |
| 159 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int read(byte[] b) throws IOException { |
| 165 | return read(b, 0, b.length); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int read(byte[] b, int off, int len) throws IOException { |
| 170 | long startTime = Profiler.nanoTimeMaybe(); |
| 171 | try { |
John Millikin | eae93f6 | 2019-11-11 09:01:04 -0800 | [diff] [blame] | 172 | return impl.read(b, off, len); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 173 | } finally { |
| 174 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name); |
| 175 | } |
| 176 | } |
felly | 0c4379a | 2018-11-01 10:47:43 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 179 | private static final class ProfiledFileOutputStream extends FileOutputStream { |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 180 | private final String name; |
| 181 | |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 182 | public ProfiledFileOutputStream(String name, boolean append) throws FileNotFoundException { |
| 183 | super(name, append); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 184 | this.name = name; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void write(byte[] b) throws IOException { |
| 189 | write(b, 0, b.length); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void write(byte[] b, int off, int len) throws IOException { |
| 194 | long startTime = Profiler.nanoTimeMaybe(); |
| 195 | try { |
Yun Peng | d2920e3 | 2018-11-27 02:07:55 -0800 | [diff] [blame] | 196 | super.write(b, off, len); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 197 | } finally { |
| 198 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name); |
| 199 | } |
| 200 | } |
| 201 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | } |