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. |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.unix; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | |
| 16 | import com.google.common.annotations.VisibleForTesting; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 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; |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.unix.NativePosixFiles.Dirents; |
| 23 | import com.google.devtools.build.lib.unix.NativePosixFiles.ReadTypes; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.AbstractFileSystemWithCustomStat; |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.vfs.Dirent; |
| 27 | import com.google.devtools.build.lib.vfs.FileStatus; |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.vfs.Path; |
| 29 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Collection; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
Ulf Adams | 8405d40 | 2016-08-10 14:28:53 +0000 | [diff] [blame] | 36 | * 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] | 37 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | @ThreadSafe |
Nathan Harmata | 13a74c0 | 2015-11-18 18:38:14 +0000 | [diff] [blame] | 39 | public class UnixFileSystem extends AbstractFileSystemWithCustomStat { |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 40 | |
Lukacs Berki | 42f67cb | 2016-01-20 14:04:18 +0000 | [diff] [blame] | 41 | public UnixFileSystem() { |
Lukacs Berki | 42f67cb | 2016-01-20 14:04:18 +0000 | [diff] [blame] | 42 | } |
| 43 | |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 44 | public UnixFileSystem(DigestHashFunction hashFunction) { |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 45 | super(hashFunction); |
| 46 | } |
| 47 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | /** |
| 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 Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 73 | public boolean isSpecialFile() { return isFile() && !status.isRegularFile(); } |
| 74 | |
| 75 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | 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 |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 104 | protected Collection<String> getDirectoryEntries(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | String name = path.getPathString(); |
| 106 | String[] entries; |
| 107 | long startTime = Profiler.nanoTimeMaybe(); |
| 108 | try { |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 109 | entries = NativePosixFiles.readdir(name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | } finally { |
| 111 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, name); |
| 112 | } |
tomlu | 0a82e70 | 2017-10-23 18:16:44 +0200 | [diff] [blame] | 113 | Collection<String> result = new ArrayList<>(entries.length); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 114 | for (String entry : entries) { |
tomlu | 0a82e70 | 2017-10-23 18:16:44 +0200 | [diff] [blame] | 115 | result.add(entry); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 116 | } |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 121 | protected PathFragment resolveOneLink(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | // 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 Adams | 8405d40 | 2016-08-10 14:28:53 +0000 | [diff] [blame] | 130 | * Converts from {@link com.google.devtools.build.lib.unix.NativePosixFiles.Dirents.Type} to |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | * {@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 |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 149 | protected Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | String name = path.getPathString(); |
| 151 | long startTime = Profiler.nanoTimeMaybe(); |
| 152 | try { |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 153 | Dirents unixDirents = NativePosixFiles.readdir(name, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 154 | 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 |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 168 | protected FileStatus stat(Path path, boolean followSymlinks) throws IOException { |
Eric Fellheimer | e9d50be | 2015-04-28 22:09:57 +0000 | [diff] [blame] | 169 | return statInternal(path, followSymlinks); |
| 170 | } |
| 171 | |
| 172 | @VisibleForTesting |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 173 | protected UnixFileStatus statInternal(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 174 | String name = path.getPathString(); |
| 175 | long startTime = Profiler.nanoTimeMaybe(); |
| 176 | try { |
| 177 | return new UnixFileStatus(followSymlinks |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 178 | ? NativePosixFiles.stat(name) |
| 179 | : NativePosixFiles.lstat(name)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 180 | } 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 |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 189 | protected FileStatus statNullable(Path path, boolean followSymlinks) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | String name = path.getPathString(); |
| 191 | long startTime = Profiler.nanoTimeMaybe(); |
| 192 | try { |
| 193 | ErrnoFileStatus stat = followSymlinks |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 194 | ? NativePosixFiles.errnoStat(name) |
| 195 | : NativePosixFiles.errnoLstat(name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | return stat.hasError() ? null : new UnixFileStatus(stat); |
| 197 | } finally { |
| 198 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_STAT, name); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 203 | protected boolean exists(Path path, boolean followSymlinks) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 204 | return statNullable(path, followSymlinks) != null; |
| 205 | } |
| 206 | |
| 207 | /** |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 208 | * Return true iff the {@code stat} of {@code path} resulted in an {@code ENOENT} |
| 209 | * or {@code ENOTDIR} error. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | */ |
| 211 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 212 | protected FileStatus statIfFound(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | String name = path.getPathString(); |
| 214 | long startTime = Profiler.nanoTimeMaybe(); |
| 215 | try { |
| 216 | ErrnoFileStatus stat = followSymlinks |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 217 | ? NativePosixFiles.errnoStat(name) |
| 218 | : NativePosixFiles.errnoLstat(name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | 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 |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 238 | protected boolean isReadable(Path path) throws IOException { |
Eric Fellheimer | e9d50be | 2015-04-28 22:09:57 +0000 | [diff] [blame] | 239 | return (statInternal(path, true).getPermissions() & 0400) != 0; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 243 | protected boolean isWritable(Path path) throws IOException { |
Eric Fellheimer | e9d50be | 2015-04-28 22:09:57 +0000 | [diff] [blame] | 244 | return (statInternal(path, true).getPermissions() & 0200) != 0; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 248 | protected boolean isExecutable(Path path) throws IOException { |
Eric Fellheimer | e9d50be | 2015-04-28 22:09:57 +0000 | [diff] [blame] | 249 | return (statInternal(path, true).getPermissions() & 0100) != 0; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 253 | * 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 256 | * |
| 257 | * @throws IOException if there was an error writing the file's metadata |
| 258 | */ |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 259 | private void modifyPermissionBits(Path path, int permissionBits, boolean add) |
| 260 | throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 261 | int oldMode = statInternal(path, true).getPermissions(); |
| 262 | int newMode = add ? (oldMode | permissionBits) : (oldMode & ~permissionBits); |
| 263 | NativePosixFiles.chmod(path.toString(), newMode); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 267 | protected void setReadable(Path path, boolean readable) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 268 | modifyPermissionBits(path, 0400, readable); |
| 269 | } |
| 270 | |
| 271 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 272 | public void setWritable(Path path, boolean writable) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 273 | modifyPermissionBits(path, 0200, writable); |
| 274 | } |
| 275 | |
| 276 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 277 | protected void setExecutable(Path path, boolean executable) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 278 | modifyPermissionBits(path, 0111, executable); |
| 279 | } |
| 280 | |
| 281 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 282 | protected void chmod(Path path, int mode) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 283 | NativePosixFiles.chmod(path.toString(), mode); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 287 | public boolean supportsModifications(Path path) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | |
| 291 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 292 | public boolean supportsSymbolicLinksNatively(Path path) { |
Ulf Adams | 8405d40 | 2016-08-10 14:28:53 +0000 | [diff] [blame] | 293 | return true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 297 | public boolean supportsHardLinksNatively(Path path) { |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | |
| 301 | @Override |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 302 | public boolean isFilePathCaseSensitive() { |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 307 | public boolean createDirectory(Path path) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 308 | // 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 314 | // 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | @Override |
tomlu | decca2b | 2017-12-21 08:59:51 -0800 | [diff] [blame] | 323 | public void createDirectoryAndParents(Path path) throws IOException { |
| 324 | NativePosixFiles.mkdirs(path.toString(), 0777); |
| 325 | } |
| 326 | |
| 327 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 328 | protected void createSymbolicLink(Path linkPath, PathFragment targetFragment) |
| 329 | throws IOException { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 330 | NativePosixFiles.symlink(targetFragment.getSafePathString(), linkPath.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 334 | protected PathFragment readSymbolicLink(Path path) throws IOException { |
Nathan Harmata | 215974e5 | 2015-09-16 21:31:49 +0000 | [diff] [blame] | 335 | // 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 337 | String name = path.toString(); |
| 338 | long startTime = Profiler.nanoTimeMaybe(); |
| 339 | try { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 340 | return PathFragment.create(NativePosixFiles.readlink(name)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 341 | } 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 Nienhuys | 14c542c | 2015-03-12 15:09:42 +0000 | [diff] [blame] | 345 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_READLINK, name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 350 | public void renameTo(Path sourcePath, Path targetPath) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 351 | NativePosixFiles.rename(sourcePath.toString(), targetPath.toString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 355 | protected long getFileSize(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 356 | return stat(path, followSymlinks).getSize(); |
| 357 | } |
| 358 | |
| 359 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 360 | public boolean delete(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 361 | String name = path.toString(); |
| 362 | long startTime = Profiler.nanoTimeMaybe(); |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 363 | try { |
| 364 | return NativePosixFiles.remove(name); |
| 365 | } finally { |
| 366 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_DELETE, name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 371 | protected long getLastModifiedTime(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 372 | return stat(path, followSymlinks).getLastModifiedTime(); |
| 373 | } |
| 374 | |
| 375 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 376 | public void setLastModifiedTime(Path path, long newTime) throws IOException { |
tomlu | 22c2f9a | 2018-01-09 13:52:13 -0800 | [diff] [blame] | 377 | 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 Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 387 | public byte[] getxattr(Path path, String name) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 388 | String pathName = path.toString(); |
| 389 | long startTime = Profiler.nanoTimeMaybe(); |
| 390 | try { |
Lukacs Berki | 7ecb2ce | 2016-01-26 15:40:42 +0000 | [diff] [blame] | 391 | return NativePosixFiles.getxattr(pathName, name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 392 | } 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 |
ccalvarin | dd9f60e | 2018-07-23 18:16:18 -0700 | [diff] [blame^] | 402 | protected byte[] getDigest(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 403 | String name = path.toString(); |
| 404 | long startTime = Profiler.nanoTimeMaybe(); |
| 405 | try { |
ccalvarin | dd9f60e | 2018-07-23 18:16:18 -0700 | [diff] [blame^] | 406 | if (getDigestFunction() == DigestHashFunction.MD5) { |
olaola | bfd1d33 | 2017-06-19 16:55:24 -0400 | [diff] [blame] | 407 | return NativePosixFiles.md5sum(name).asBytes(); |
| 408 | } |
ccalvarin | dd9f60e | 2018-07-23 18:16:18 -0700 | [diff] [blame^] | 409 | return super.getDigest(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 410 | } finally { |
| 411 | profiler.logSimpleTask(startTime, ProfilerTask.VFS_MD5, name); |
| 412 | } |
| 413 | } |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 414 | |
| 415 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 416 | protected void createFSDependentHardLink(Path linkPath, Path originalPath) |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 417 | throws IOException { |
| 418 | NativePosixFiles.link(originalPath.toString(), linkPath.toString()); |
| 419 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 420 | } |