blob: 5d507c3f953a0880b3d249ef9dadbaa4d7c6779f [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
Ulf Adams8afbd3c2017-02-28 10:42:48 +000014package com.google.devtools.build.lib.unix;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010015
16import com.google.common.annotations.VisibleForTesting;
tomlua155b532017-11-08 20:12:47 +010017import com.google.common.base.Preconditions;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.common.collect.Lists;
19import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
20import com.google.devtools.build.lib.profiler.Profiler;
21import com.google.devtools.build.lib.profiler.ProfilerTask;
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +000022import com.google.devtools.build.lib.unix.NativePosixFiles.Dirents;
23import com.google.devtools.build.lib.unix.NativePosixFiles.ReadTypes;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000024import com.google.devtools.build.lib.vfs.AbstractFileSystemWithCustomStat;
ccalvarinbda12a12018-06-21 18:57:26 -070025import com.google.devtools.build.lib.vfs.DigestHashFunction;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000026import com.google.devtools.build.lib.vfs.Dirent;
27import com.google.devtools.build.lib.vfs.FileStatus;
aehligc801c392017-12-19 07:12:25 -080028import com.google.devtools.build.lib.vfs.Path;
29import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030import java.io.IOException;
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.List;
34
35/**
Ulf Adams8405d402016-08-10 14:28:53 +000036 * This class implements the FileSystem interface using direct calls to the UNIX filesystem.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038@ThreadSafe
Nathan Harmata13a74c02015-11-18 18:38:14 +000039public class UnixFileSystem extends AbstractFileSystemWithCustomStat {
buchgr559a07d2017-11-30 11:09:35 -080040
Lukacs Berki42f67cb2016-01-20 14:04:18 +000041 public UnixFileSystem() {
Lukacs Berki42f67cb2016-01-20 14:04:18 +000042 }
43
ccalvarinbda12a12018-06-21 18:57:26 -070044 public UnixFileSystem(DigestHashFunction hashFunction) {
buchgr559a07d2017-11-30 11:09:35 -080045 super(hashFunction);
46 }
47
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048 /**
49 * Eager implementation of FileStatus for file systems that have an atomic
50 * stat(2) syscall. A proxy for {@link com.google.devtools.build.lib.unix.FileStatus}.
51 * Note that isFile and getLastModifiedTime have slightly different meanings
52 * between UNIX and VFS.
53 */
54 @VisibleForTesting
55 protected static class UnixFileStatus implements FileStatus {
56
57 private final com.google.devtools.build.lib.unix.FileStatus status;
58
59 UnixFileStatus(com.google.devtools.build.lib.unix.FileStatus status) {
60 this.status = status;
61 }
62
63 @Override
64 public boolean isFile() { return !isDirectory() && !isSymbolicLink(); }
65
66 @Override
67 public boolean isDirectory() { return status.isDirectory(); }
68
69 @Override
70 public boolean isSymbolicLink() { return status.isSymbolicLink(); }
71
72 @Override
Nathan Harmatad8b6ff22015-10-20 21:54:34 +000073 public boolean isSpecialFile() { return isFile() && !status.isRegularFile(); }
74
75 @Override
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 public long getSize() { return status.getSize(); }
77
78 @Override
79 public long getLastModifiedTime() {
80 return (status.getLastModifiedTime() * 1000)
81 + (status.getFractionalLastModifiedTime() / 1000000);
82 }
83
84 @Override
85 public long getLastChangeTime() {
86 return (status.getLastChangeTime() * 1000)
87 + (status.getFractionalLastChangeTime() / 1000000);
88 }
89
90 @Override
91 public long getNodeId() {
92 // Note that we may want to include more information in this id number going forward,
93 // especially the device number.
94 return status.getInodeNumber();
95 }
96
97 int getPermissions() { return status.getPermissions(); }
98
99 @Override
100 public String toString() { return status.toString(); }
101 }
102
103 @Override
aehligc801c392017-12-19 07:12:25 -0800104 protected Collection<String> getDirectoryEntries(Path path) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100105 String name = path.getPathString();
106 String[] entries;
107 long startTime = Profiler.nanoTimeMaybe();
108 try {
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000109 entries = NativePosixFiles.readdir(name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110 } finally {
111 profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, name);
112 }
tomlu0a82e702017-10-23 18:16:44 +0200113 Collection<String> result = new ArrayList<>(entries.length);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100114 for (String entry : entries) {
tomlu0a82e702017-10-23 18:16:44 +0200115 result.add(entry);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100116 }
117 return result;
118 }
119
120 @Override
aehligc801c392017-12-19 07:12:25 -0800121 protected PathFragment resolveOneLink(Path path) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100122 // Beware, this seemingly simple code belies the complex specification of
123 // FileSystem.resolveOneLink().
124 return stat(path, false).isSymbolicLink()
125 ? readSymbolicLink(path)
126 : null;
127 }
128
129 /**
Ulf Adams8405d402016-08-10 14:28:53 +0000130 * Converts from {@link com.google.devtools.build.lib.unix.NativePosixFiles.Dirents.Type} to
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100131 * {@link com.google.devtools.build.lib.vfs.Dirent.Type}.
132 */
133 private static Dirent.Type convertToDirentType(Dirents.Type type) {
134 switch (type) {
135 case FILE:
136 return Dirent.Type.FILE;
137 case DIRECTORY:
138 return Dirent.Type.DIRECTORY;
139 case SYMLINK:
140 return Dirent.Type.SYMLINK;
141 case UNKNOWN:
142 return Dirent.Type.UNKNOWN;
143 default:
144 throw new IllegalArgumentException("Unknown type " + type);
145 }
146 }
147
148 @Override
aehligc801c392017-12-19 07:12:25 -0800149 protected Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100150 String name = path.getPathString();
151 long startTime = Profiler.nanoTimeMaybe();
152 try {
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000153 Dirents unixDirents = NativePosixFiles.readdir(name,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100154 followSymlinks ? ReadTypes.FOLLOW : ReadTypes.NOFOLLOW);
155 Preconditions.checkState(unixDirents.hasTypes());
156 List<Dirent> dirents = Lists.newArrayListWithCapacity(unixDirents.size());
157 for (int i = 0; i < unixDirents.size(); i++) {
158 dirents.add(new Dirent(unixDirents.getName(i),
159 convertToDirentType(unixDirents.getType(i))));
160 }
161 return dirents;
162 } finally {
163 profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, name);
164 }
165 }
166
167 @Override
aehligc801c392017-12-19 07:12:25 -0800168 protected FileStatus stat(Path path, boolean followSymlinks) throws IOException {
Eric Fellheimere9d50be2015-04-28 22:09:57 +0000169 return statInternal(path, followSymlinks);
170 }
171
172 @VisibleForTesting
aehligc801c392017-12-19 07:12:25 -0800173 protected UnixFileStatus statInternal(Path path, boolean followSymlinks) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100174 String name = path.getPathString();
175 long startTime = Profiler.nanoTimeMaybe();
176 try {
177 return new UnixFileStatus(followSymlinks
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000178 ? NativePosixFiles.stat(name)
179 : NativePosixFiles.lstat(name));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100180 } finally {
181 profiler.logSimpleTask(startTime, ProfilerTask.VFS_STAT, name);
182 }
183 }
184
185 // Like stat(), but returns null instead of throwing.
186 // This is a performance optimization in the case where clients
187 // catch and don't re-throw.
188 @Override
aehligc801c392017-12-19 07:12:25 -0800189 protected FileStatus statNullable(Path path, boolean followSymlinks) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100190 String name = path.getPathString();
191 long startTime = Profiler.nanoTimeMaybe();
192 try {
193 ErrnoFileStatus stat = followSymlinks
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000194 ? NativePosixFiles.errnoStat(name)
195 : NativePosixFiles.errnoLstat(name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100196 return stat.hasError() ? null : new UnixFileStatus(stat);
197 } finally {
198 profiler.logSimpleTask(startTime, ProfilerTask.VFS_STAT, name);
199 }
200 }
201
202 @Override
aehligc801c392017-12-19 07:12:25 -0800203 protected boolean exists(Path path, boolean followSymlinks) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100204 return statNullable(path, followSymlinks) != null;
205 }
206
207 /**
aehligc801c392017-12-19 07:12:25 -0800208 * Return true iff the {@code stat} of {@code path} resulted in an {@code ENOENT}
209 * or {@code ENOTDIR} error.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100210 */
211 @Override
aehligc801c392017-12-19 07:12:25 -0800212 protected FileStatus statIfFound(Path path, boolean followSymlinks) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100213 String name = path.getPathString();
214 long startTime = Profiler.nanoTimeMaybe();
215 try {
216 ErrnoFileStatus stat = followSymlinks
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000217 ? NativePosixFiles.errnoStat(name)
218 : NativePosixFiles.errnoLstat(name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100219 if (!stat.hasError()) {
220 return new UnixFileStatus(stat);
221 }
222 int errno = stat.getErrno();
223 if (errno == ErrnoFileStatus.ENOENT || errno == ErrnoFileStatus.ENOTDIR) {
224 return null;
225 }
226 // This should not return -- we are calling stat here just to throw the proper exception.
227 // However, since there may be transient IO errors, we cannot guarantee that an exception will
228 // be thrown.
229 // TODO(bazel-team): Extract the exception-construction code and make it visible separately in
230 // FilesystemUtils to avoid having to do a duplicate stat call.
231 return stat(path, followSymlinks);
232 } finally {
233 profiler.logSimpleTask(startTime, ProfilerTask.VFS_STAT, name);
234 }
235 }
236
237 @Override
aehligc801c392017-12-19 07:12:25 -0800238 protected boolean isReadable(Path path) throws IOException {
Eric Fellheimere9d50be2015-04-28 22:09:57 +0000239 return (statInternal(path, true).getPermissions() & 0400) != 0;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100240 }
241
242 @Override
aehligc801c392017-12-19 07:12:25 -0800243 protected boolean isWritable(Path path) throws IOException {
Eric Fellheimere9d50be2015-04-28 22:09:57 +0000244 return (statInternal(path, true).getPermissions() & 0200) != 0;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100245 }
246
247 @Override
aehligc801c392017-12-19 07:12:25 -0800248 protected boolean isExecutable(Path path) throws IOException {
Eric Fellheimere9d50be2015-04-28 22:09:57 +0000249 return (statInternal(path, true).getPermissions() & 0100) != 0;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100250 }
251
252 /**
aehligc801c392017-12-19 07:12:25 -0800253 * Adds or remove the bits specified in "permissionBits" to the permission
254 * mask of the file specified by {@code path}. If the argument {@code add} is
255 * true, the specified permissions are added, otherwise they are removed.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100256 *
257 * @throws IOException if there was an error writing the file's metadata
258 */
aehligc801c392017-12-19 07:12:25 -0800259 private void modifyPermissionBits(Path path, int permissionBits, boolean add)
260 throws IOException {
tomlu22c2f9a2018-01-09 13:52:13 -0800261 int oldMode = statInternal(path, true).getPermissions();
262 int newMode = add ? (oldMode | permissionBits) : (oldMode & ~permissionBits);
263 NativePosixFiles.chmod(path.toString(), newMode);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100264 }
265
266 @Override
aehligc801c392017-12-19 07:12:25 -0800267 protected void setReadable(Path path, boolean readable) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100268 modifyPermissionBits(path, 0400, readable);
269 }
270
271 @Override
aehligc801c392017-12-19 07:12:25 -0800272 public void setWritable(Path path, boolean writable) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100273 modifyPermissionBits(path, 0200, writable);
274 }
275
276 @Override
aehligc801c392017-12-19 07:12:25 -0800277 protected void setExecutable(Path path, boolean executable) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100278 modifyPermissionBits(path, 0111, executable);
279 }
280
281 @Override
aehligc801c392017-12-19 07:12:25 -0800282 protected void chmod(Path path, int mode) throws IOException {
tomlu22c2f9a2018-01-09 13:52:13 -0800283 NativePosixFiles.chmod(path.toString(), mode);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100284 }
285
286 @Override
aehligc801c392017-12-19 07:12:25 -0800287 public boolean supportsModifications(Path path) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100288 return true;
289 }
290
291 @Override
aehligc801c392017-12-19 07:12:25 -0800292 public boolean supportsSymbolicLinksNatively(Path path) {
Ulf Adams8405d402016-08-10 14:28:53 +0000293 return true;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100294 }
295
296 @Override
aehligc801c392017-12-19 07:12:25 -0800297 public boolean supportsHardLinksNatively(Path path) {
Googlere1cd9502016-09-07 14:33:29 +0000298 return true;
299 }
300
301 @Override
Yun Peng352f7e72016-05-09 11:08:25 +0000302 public boolean isFilePathCaseSensitive() {
303 return true;
304 }
305
306 @Override
aehligc801c392017-12-19 07:12:25 -0800307 public boolean createDirectory(Path path) throws IOException {
tomlu22c2f9a2018-01-09 13:52:13 -0800308 // Note: UNIX mkdir(2), FilesystemUtils.mkdir() and createDirectory all
309 // have different ways of representing failure!
310 if (NativePosixFiles.mkdir(path.toString(), 0777)) {
311 return true; // successfully created
312 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100313
tomlu22c2f9a2018-01-09 13:52:13 -0800314 // false => EEXIST: something is already in the way (file/dir/symlink)
315 if (isDirectory(path, false)) {
316 return false; // directory already existed
317 } else {
318 throw new IOException(path + " (File exists)");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100319 }
320 }
321
322 @Override
tomludecca2b2017-12-21 08:59:51 -0800323 public void createDirectoryAndParents(Path path) throws IOException {
324 NativePosixFiles.mkdirs(path.toString(), 0777);
325 }
326
327 @Override
aehligc801c392017-12-19 07:12:25 -0800328 protected void createSymbolicLink(Path linkPath, PathFragment targetFragment)
329 throws IOException {
tomlua729b9b2018-02-08 15:32:00 -0800330 NativePosixFiles.symlink(targetFragment.getSafePathString(), linkPath.toString());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100331 }
332
333 @Override
aehligc801c392017-12-19 07:12:25 -0800334 protected PathFragment readSymbolicLink(Path path) throws IOException {
Nathan Harmata215974e52015-09-16 21:31:49 +0000335 // Note that the default implementation of readSymbolicLinkUnchecked calls this method and thus
336 // is optimal since we only make one system call in here.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100337 String name = path.toString();
338 long startTime = Profiler.nanoTimeMaybe();
339 try {
aehligc801c392017-12-19 07:12:25 -0800340 return PathFragment.create(NativePosixFiles.readlink(name));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100341 } catch (IOException e) {
342 // EINVAL => not a symbolic link. Anything else is a real error.
343 throw e.getMessage().endsWith("(Invalid argument)") ? new NotASymlinkException(path) : e;
344 } finally {
Han-Wen Nienhuys14c542c2015-03-12 15:09:42 +0000345 profiler.logSimpleTask(startTime, ProfilerTask.VFS_READLINK, name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100346 }
347 }
348
349 @Override
aehligc801c392017-12-19 07:12:25 -0800350 public void renameTo(Path sourcePath, Path targetPath) throws IOException {
tomlu22c2f9a2018-01-09 13:52:13 -0800351 NativePosixFiles.rename(sourcePath.toString(), targetPath.toString());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100352 }
353
354 @Override
aehligc801c392017-12-19 07:12:25 -0800355 protected long getFileSize(Path path, boolean followSymlinks) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100356 return stat(path, followSymlinks).getSize();
357 }
358
359 @Override
aehligc801c392017-12-19 07:12:25 -0800360 public boolean delete(Path path) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100361 String name = path.toString();
362 long startTime = Profiler.nanoTimeMaybe();
tomlu22c2f9a2018-01-09 13:52:13 -0800363 try {
364 return NativePosixFiles.remove(name);
365 } finally {
366 profiler.logSimpleTask(startTime, ProfilerTask.VFS_DELETE, name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100367 }
368 }
369
370 @Override
aehligc801c392017-12-19 07:12:25 -0800371 protected long getLastModifiedTime(Path path, boolean followSymlinks) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100372 return stat(path, followSymlinks).getLastModifiedTime();
373 }
374
375 @Override
aehligc801c392017-12-19 07:12:25 -0800376 public void setLastModifiedTime(Path path, long newTime) throws IOException {
tomlu22c2f9a2018-01-09 13:52:13 -0800377 if (newTime == -1L) { // "now"
378 NativePosixFiles.utime(path.toString(), true, 0);
379 } else {
380 // newTime > MAX_INT => -ve unixTime
381 int unixTime = (int) (newTime / 1000);
382 NativePosixFiles.utime(path.toString(), false, unixTime);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100383 }
384 }
385
386 @Override
aehligc801c392017-12-19 07:12:25 -0800387 public byte[] getxattr(Path path, String name) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100388 String pathName = path.toString();
389 long startTime = Profiler.nanoTimeMaybe();
390 try {
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +0000391 return NativePosixFiles.getxattr(pathName, name);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100392 } catch (UnsupportedOperationException e) {
393 // getxattr() syscall is not supported by the underlying filesystem (it returned ENOTSUP).
394 // Per method contract, treat this as ENODATA.
395 return null;
396 } finally {
397 profiler.logSimpleTask(startTime, ProfilerTask.VFS_XATTR, pathName);
398 }
399 }
400
401 @Override
ccalvarindd9f60e2018-07-23 18:16:18 -0700402 protected byte[] getDigest(Path path) throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100403 String name = path.toString();
404 long startTime = Profiler.nanoTimeMaybe();
405 try {
ccalvarindd9f60e2018-07-23 18:16:18 -0700406 if (getDigestFunction() == DigestHashFunction.MD5) {
olaolabfd1d332017-06-19 16:55:24 -0400407 return NativePosixFiles.md5sum(name).asBytes();
408 }
ccalvarindd9f60e2018-07-23 18:16:18 -0700409 return super.getDigest(path);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100410 } finally {
411 profiler.logSimpleTask(startTime, ProfilerTask.VFS_MD5, name);
412 }
413 }
Googlere1cd9502016-09-07 14:33:29 +0000414
415 @Override
aehligc801c392017-12-19 07:12:25 -0800416 protected void createFSDependentHardLink(Path linkPath, Path originalPath)
Googlere1cd9502016-09-07 14:33:29 +0000417 throws IOException {
418 NativePosixFiles.link(originalPath.toString(), linkPath.toString());
419 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100420}