Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 14 | package com.google.devtools.build.lib.vfs; |
| 15 | |
| 16 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
| 17 | import com.google.devtools.build.lib.profiler.Profiler; |
| 18 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
ccalvarin | c2f1896 | 2018-08-22 15:08:40 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.vfs.DigestHashFunction.DefaultHashFunctionNotSetException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import java.io.FileInputStream; |
| 21 | import java.io.FileNotFoundException; |
| 22 | import java.io.FileOutputStream; |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.io.OutputStream; |
| 26 | |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 27 | /** 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] | 28 | @ThreadSafe |
shahan | b1dd4e3 | 2018-05-09 08:23:31 -0700 | [diff] [blame] | 29 | public abstract class AbstractFileSystem extends FileSystem { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | |
| 31 | protected static final String ERR_PERMISSION_DENIED = " (Permission denied)"; |
| 32 | protected static final Profiler profiler = Profiler.instance(); |
| 33 | |
ccalvarin | c2f1896 | 2018-08-22 15:08:40 -0700 | [diff] [blame] | 34 | public AbstractFileSystem() throws DefaultHashFunctionNotSetException {} |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 35 | |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 36 | public AbstractFileSystem(DigestHashFunction digestFunction) { |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 37 | super(digestFunction); |
| 38 | } |
| 39 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 41 | protected InputStream getInputStream(Path path) throws IOException { |
Googler | 8910087 | 2016-07-26 15:52:48 +0000 | [diff] [blame] | 42 | // 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] | 43 | // ultimately to JVM_Open in the Hotspot JVM. This call is not EINTR-safe, so we must do the |
| 44 | // retry here. |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 45 | for (; ; ) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | try { |
| 47 | return createFileInputStream(path); |
| 48 | } catch (FileNotFoundException e) { |
| 49 | if (e.getMessage().endsWith("(Interrupted system call)")) { |
| 50 | continue; |
| 51 | } else { |
| 52 | throw e; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 58 | /** Returns either normal or profiled FileInputStream. */ |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 59 | private InputStream createFileInputStream(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | final String name = path.toString(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 61 | if (profiler.isActive() |
| 62 | && (profiler.isProfiling(ProfilerTask.VFS_READ) |
| 63 | || profiler.isProfiling(ProfilerTask.VFS_OPEN))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | long startTime = Profiler.nanoTimeMaybe(); |
| 65 | try { |
| 66 | // Replace default FileInputStream instance with the custom one that does profiling. |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 67 | return new ProfiledFileInputStream(name, newFileInputStream(name)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | } finally { |
| 69 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name); |
| 70 | } |
| 71 | } else { |
| 72 | // Use normal FileInputStream instance if profiler is not enabled. |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 73 | return newFileInputStream(name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 77 | protected InputStream newFileInputStream(String path) throws IOException { |
| 78 | return new FileInputStream(path); |
| 79 | } |
| 80 | |
| 81 | protected OutputStream newFileOutputStream(String path, boolean append) throws IOException { |
| 82 | return new FileOutputStream(path, append); |
| 83 | } |
| 84 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | /** |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 86 | * Returns either normal or profiled FileOutputStream. Should be used by subclasses to create |
| 87 | * default OutputStream instance. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | */ |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 89 | protected OutputStream createFileOutputStream(Path path, boolean append) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | final String name = path.toString(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 91 | if (profiler.isActive() |
| 92 | && (profiler.isProfiling(ProfilerTask.VFS_WRITE) |
| 93 | || profiler.isProfiling(ProfilerTask.VFS_OPEN))) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | long startTime = Profiler.nanoTimeMaybe(); |
| 95 | try { |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 96 | return new ProfiledFileOutputStream(name, newFileOutputStream(name, append)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | } finally { |
| 98 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name); |
| 99 | } |
| 100 | } else { |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 101 | return newFileOutputStream(name, append); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 106 | protected OutputStream getOutputStream(Path path, boolean append) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 107 | try { |
| 108 | return createFileOutputStream(path, append); |
| 109 | } catch (FileNotFoundException e) { |
| 110 | // Why does it throw a *FileNotFoundException* if it can't write? |
| 111 | // That does not make any sense! And its in a completely different |
| 112 | // format than in other situations, no less! |
| 113 | if (e.getMessage().equals(path + ERR_PERMISSION_DENIED)) { |
| 114 | throw new FileAccessException(e.getMessage()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 115 | } |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 116 | throw e; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 119 | |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 120 | private static final class ProfiledFileInputStream extends InputStream { |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 121 | private final String name; |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 122 | private final InputStream stm; |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 123 | |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 124 | public ProfiledFileInputStream(String name, InputStream stm) { |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 125 | this.name = name; |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 126 | this.stm = stm; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int available() throws IOException { |
| 131 | return stm.available(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void close() throws IOException { |
| 136 | stm.close(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void mark(int readlimit) { |
| 141 | stm.mark(readlimit); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean markSupported() { |
| 146 | return stm.markSupported(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int read() throws IOException { |
| 151 | long startTime = Profiler.nanoTimeMaybe(); |
| 152 | try { |
gregce | e98f344 | 2017-12-20 16:24:25 -0800 | [diff] [blame] | 153 | // Note that FileInputStream#read() does *not* call any of our overridden methods, |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 154 | // so there's no concern with double counting here. |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 155 | return stm.read(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 156 | } finally { |
| 157 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public int read(byte[] b) throws IOException { |
| 163 | return read(b, 0, b.length); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public int read(byte[] b, int off, int len) throws IOException { |
| 168 | long startTime = Profiler.nanoTimeMaybe(); |
| 169 | try { |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 170 | return stm.read(b, off, len); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 171 | } finally { |
| 172 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name); |
| 173 | } |
| 174 | } |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 175 | |
| 176 | @Override |
| 177 | public void reset() throws IOException { |
| 178 | stm.reset(); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public long skip(long n) throws IOException { |
| 183 | return stm.skip(n); |
| 184 | } |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 187 | private static final class ProfiledFileOutputStream extends OutputStream { |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 188 | private final String name; |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 189 | private final OutputStream stm; |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 190 | |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 191 | public ProfiledFileOutputStream(String name, OutputStream stm) { |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 192 | this.name = name; |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 193 | this.stm = stm; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void close() throws IOException { |
| 198 | stm.close(); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void flush() throws IOException { |
| 203 | stm.flush(); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void write(byte[] b) throws IOException { |
| 208 | write(b, 0, b.length); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void write(byte[] b, int off, int len) throws IOException { |
| 213 | long startTime = Profiler.nanoTimeMaybe(); |
| 214 | try { |
Laszlo Csomor | 1a95502 | 2018-09-07 07:53:51 -0700 | [diff] [blame^] | 215 | stm.write(b, off, len); |
| 216 | } finally { |
| 217 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void write(int b) throws IOException { |
| 223 | long startTime = Profiler.nanoTimeMaybe(); |
| 224 | try { |
| 225 | stm.write(b); |
Laszlo Csomor | 3be9fd3 | 2016-10-26 09:32:54 +0000 | [diff] [blame] | 226 | } finally { |
| 227 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name); |
| 228 | } |
| 229 | } |
| 230 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 231 | } |