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 | |
| 15 | package com.google.devtools.build.lib.vfs; |
| 16 | |
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.devtools.build.lib.concurrent.ThreadSafety; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.io.OutputStream; |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 22 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.util.Collection; |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 24 | import java.util.Comparator; |
| 25 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import javax.annotation.Nullable; |
| 28 | |
| 29 | /** |
| 30 | * Presents a unified view of multiple virtual {@link FileSystem} instances, to which requests are |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 31 | * delegated based on a {@link PathFragment} prefix mapping. If multiple prefixes apply to a given |
| 32 | * path, the *longest* (i.e. most specific) match is used. The order in which the delegates are |
| 33 | * specified does not influence the mapping. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | * |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 35 | * <p>Paths are preserved absolutely, contrary to how "mount" works, e.g.: /foo/bar maps to /foo/bar |
| 36 | * on the delegate, even if it is mounted at /foo. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | * |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 38 | * <p>For example: "/in" maps to InFileSystem, "/" maps to OtherFileSystem. Reading from |
| 39 | * "/in/base/BUILD" through the UnionFileSystem will delegate the read operation to InFileSystem, |
| 40 | * which will read "/in/base/BUILD" relative to its root. ("mount" behavior would remap it to |
| 41 | * "/base/BUILD" on the delegate). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | * |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 43 | * <p>Intra-filesystem symbolic links are resolved to their ultimate targets. Cross-filesystem links |
| 44 | * are not currently supported. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | */ |
| 46 | @ThreadSafety.ThreadSafe |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 47 | public final class UnionFileSystem extends FileSystem { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 49 | private static class FileSystemAndPrefix { |
| 50 | final PathFragment prefix; |
| 51 | final FileSystem fileSystem; |
| 52 | |
| 53 | public FileSystemAndPrefix(PathFragment prefix, FileSystem fileSystem) { |
| 54 | this.prefix = prefix; |
| 55 | this.fileSystem = fileSystem; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // List of file systems and their mappings, sorted by prefix length descending. |
| 60 | private final List<FileSystemAndPrefix> fileSystems; |
| 61 | private final FileSystem rootFileSystem; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 63 | // True if the file path is case-sensitive on all the FileSystem |
| 64 | // or False if they are all case-insensitive, otherwise error. |
| 65 | private final boolean isCaseSensitive; |
| 66 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | /** |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 68 | * Creates a new modifiable UnionFileSystem with prefix mappings specified by a map. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | * |
| 70 | * @param prefixMapping map of path prefixes to {@link FileSystem}s |
tomlu | 121c6b1 | 2017-10-20 21:16:57 +0200 | [diff] [blame] | 71 | * @param rootFileSystem root for default requests; i.e. mapping of "/" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | */ |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 73 | public UnionFileSystem(Map<PathFragment, FileSystem> prefixMapping, FileSystem rootFileSystem) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | super(); |
| 75 | Preconditions.checkNotNull(prefixMapping); |
| 76 | Preconditions.checkNotNull(rootFileSystem); |
| 77 | Preconditions.checkArgument(rootFileSystem != this, "Circular root filesystem."); |
| 78 | Preconditions.checkArgument( |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 79 | prefixMapping.keySet().stream().noneMatch(p -> p.getPathString().equals("/")), |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 80 | "Attempted to specify an explicit root prefix mapping; " |
| 81 | + "please use the rootFileSystem argument instead."); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 83 | this.fileSystems = new ArrayList<>(); |
| 84 | this.rootFileSystem = rootFileSystem; |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 85 | this.isCaseSensitive = rootFileSystem.isFilePathCaseSensitive(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 87 | for (Map.Entry<PathFragment, FileSystem> prefix : prefixMapping.entrySet()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | FileSystem delegate = prefix.getValue(); |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 89 | Preconditions.checkArgument( |
| 90 | delegate.isFilePathCaseSensitive() == this.isCaseSensitive, |
| 91 | "The case sensitiveness of FileSystem are different in UnionFileSystem"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 92 | PathFragment prefixPath = prefix.getKey(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | |
| 94 | // Extra slash prevents within-directory mappings, which Path can't handle. |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 95 | fileSystems.add(new FileSystemAndPrefix(prefixPath, delegate)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | } |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 97 | // Order by length descending. This ensures that more specific mapping takes precedence |
| 98 | // when we try to find the file system of a given path. |
| 99 | Comparator<FileSystemAndPrefix> comparator = |
| 100 | Comparator.comparing(f -> f.prefix.getPathString().length()); |
| 101 | fileSystems.sort(comparator.reversed()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 105 | * Retrieves the filesystem delegate of a path mapping. Does not follow symlinks (but you can call |
| 106 | * on a path preprocessed with {@link #resolveSymbolicLinks} to support this use case). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | * |
| 108 | * @param path the {@link Path} to map to a filesystem |
| 109 | * @throws IllegalArgumentException if no delegate exists for the path |
| 110 | */ |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 111 | FileSystem getDelegate(Path path) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 112 | Preconditions.checkNotNull(path); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 113 | FileSystem delegate = null; |
| 114 | // Linearly iterate over each mapped file system and find the one that handles this path. |
| 115 | // For small number of mappings, this will be more efficient than using a trie |
| 116 | for (FileSystemAndPrefix fileSystemAndPrefix : this.fileSystems) { |
| 117 | if (path.startsWith(fileSystemAndPrefix.prefix)) { |
| 118 | delegate = fileSystemAndPrefix.fileSystem; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return delegate != null ? delegate : rootFileSystem; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Associates the path with the root of the given delegate filesystem. |
| 126 | // Necessary to avoid null pointer problems inside of the delegates. |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 127 | Path adjustPath(Path path, FileSystem delegate) { |
| 128 | return delegate.getPath(path.getPathString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 132 | * Follow a symbolic link once using the appropriate delegate filesystem, also resolving parent |
| 133 | * directory symlinks. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 134 | * |
| 135 | * @param path {@link Path} to the symbolic link |
| 136 | */ |
| 137 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 138 | protected PathFragment readSymbolicLink(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | Preconditions.checkNotNull(path); |
| 140 | FileSystem delegate = getDelegate(path); |
| 141 | return delegate.readSymbolicLink(adjustPath(path, delegate)); |
| 142 | } |
| 143 | |
| 144 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 145 | protected PathFragment resolveOneLink(Path path) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | Preconditions.checkNotNull(path); |
| 147 | FileSystem delegate = getDelegate(path); |
| 148 | return delegate.resolveOneLink(adjustPath(path, delegate)); |
| 149 | } |
| 150 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 151 | private void checkModifiable(Path path) { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 152 | if (!supportsModifications(path)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 153 | throw new UnsupportedOperationException( |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 154 | String.format("Modifications to this %s are disabled.", getClass().getSimpleName())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 159 | public boolean supportsModifications(Path path) { |
tomlu | 121c6b1 | 2017-10-20 21:16:57 +0200 | [diff] [blame] | 160 | FileSystem delegate = getDelegate(path); |
| 161 | path = adjustPath(path, delegate); |
| 162 | return delegate.supportsModifications(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 166 | public boolean supportsSymbolicLinksNatively(Path path) { |
tomlu | 121c6b1 | 2017-10-20 21:16:57 +0200 | [diff] [blame] | 167 | FileSystem delegate = getDelegate(path); |
| 168 | path = adjustPath(path, delegate); |
| 169 | return delegate.supportsSymbolicLinksNatively(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 173 | public boolean supportsHardLinksNatively(Path path) { |
tomlu | 121c6b1 | 2017-10-20 21:16:57 +0200 | [diff] [blame] | 174 | FileSystem delegate = getDelegate(path); |
| 175 | path = adjustPath(path, delegate); |
| 176 | return delegate.supportsHardLinksNatively(path); |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 180 | public boolean isFilePathCaseSensitive() { |
tomlu | 121c6b1 | 2017-10-20 21:16:57 +0200 | [diff] [blame] | 181 | return isCaseSensitive; |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 185 | public String getFileSystemType(Path path) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 186 | try { |
| 187 | path = internalResolveSymlink(path); |
| 188 | } catch (IOException e) { |
| 189 | return "unknown"; |
| 190 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | FileSystem delegate = getDelegate(path); |
| 192 | return delegate.getFileSystemType(path); |
| 193 | } |
| 194 | |
| 195 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 196 | protected byte[] getDigest(Path path, HashFunction hashFunction) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 197 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 198 | FileSystem delegate = getDelegate(path); |
olaola | bfd1d33 | 2017-06-19 16:55:24 -0400 | [diff] [blame] | 199 | return delegate.getDigest(adjustPath(path, delegate), hashFunction); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 203 | public boolean createDirectory(Path path) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 204 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | // When creating the exact directory that is mapped, |
| 206 | // create it on both the parent's delegate and the path's delegate. |
| 207 | // This is necessary both for the parent to see the directory and for the |
| 208 | // delegate to use it. |
| 209 | // This is present to address this problematic case: |
| 210 | // / -> RootFs |
| 211 | // /foo -> FooFs |
| 212 | // mkdir /foo |
| 213 | // ls / ("foo" would be missing if not created on the parent) |
| 214 | // ls /foo (would fail if foo weren't also present on the child) |
| 215 | FileSystem delegate = getDelegate(path); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 216 | Path parent = path.getParentDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | if (parent != null) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 218 | parent = internalResolveSymlink(parent); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | FileSystem parentDelegate = getDelegate(parent); |
| 220 | if (parentDelegate != delegate) { |
| 221 | // There's a possibility it already exists on the parent, so don't die |
| 222 | // if the directory can't be created there. |
| 223 | parentDelegate.createDirectory(adjustPath(path, parentDelegate)); |
| 224 | } |
| 225 | } |
| 226 | return delegate.createDirectory(adjustPath(path, delegate)); |
| 227 | } |
| 228 | |
| 229 | @Override |
tomlu | decca2b | 2017-12-21 08:59:51 -0800 | [diff] [blame] | 230 | public void createDirectoryAndParents(Path path) throws IOException { |
| 231 | checkModifiable(path); |
| 232 | FileSystem delegate = getDelegate(path); |
tomlu | 7f17d08 | 2018-01-09 13:46:29 -0800 | [diff] [blame] | 233 | delegate.createDirectoryAndParents(adjustPath(path, delegate)); |
tomlu | decca2b | 2017-12-21 08:59:51 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 237 | protected long getFileSize(Path path, boolean followSymlinks) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 238 | path = followSymlinks ? internalResolveSymlink(path) : path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 240 | return delegate.getFileSize(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 244 | public boolean delete(Path path) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 245 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 246 | FileSystem delegate = getDelegate(path); |
| 247 | return delegate.delete(adjustPath(path, delegate)); |
| 248 | } |
| 249 | |
| 250 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 251 | protected long getLastModifiedTime(Path path, boolean followSymlinks) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 252 | path = followSymlinks ? internalResolveSymlink(path) : path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 253 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 254 | return delegate.getLastModifiedTime(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 258 | public void setLastModifiedTime(Path path, long newTime) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 259 | path = internalResolveSymlink(path); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 260 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 261 | FileSystem delegate = getDelegate(path); |
| 262 | delegate.setLastModifiedTime(adjustPath(path, delegate), newTime); |
| 263 | } |
| 264 | |
| 265 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 266 | protected boolean isSymbolicLink(Path path) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 267 | FileSystem delegate = getDelegate(path); |
| 268 | path = adjustPath(path, delegate); |
| 269 | return delegate.isSymbolicLink(path); |
| 270 | } |
| 271 | |
| 272 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 273 | protected boolean isDirectory(Path path, boolean followSymlinks) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 274 | try { |
| 275 | path = followSymlinks ? internalResolveSymlink(path) : path; |
| 276 | } catch (IOException e) { |
| 277 | return false; |
| 278 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 279 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 280 | return delegate.isDirectory(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 284 | protected boolean isFile(Path path, boolean followSymlinks) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 285 | try { |
| 286 | path = followSymlinks ? internalResolveSymlink(path) : path; |
| 287 | } catch (IOException e) { |
| 288 | return false; |
| 289 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 290 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 291 | return delegate.isFile(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 295 | protected boolean isSpecialFile(Path path, boolean followSymlinks) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 296 | try { |
| 297 | path = followSymlinks ? internalResolveSymlink(path) : path; |
| 298 | } catch (IOException e) { |
| 299 | return false; |
| 300 | } |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 301 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 302 | return delegate.isSpecialFile(adjustPath(path, delegate), false); |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 306 | protected void createSymbolicLink(Path linkPath, PathFragment targetFragment) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 307 | checkModifiable(linkPath); |
| 308 | if (!supportsSymbolicLinksNatively(linkPath)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 309 | throw new UnsupportedOperationException( |
| 310 | "Attempted to create a symlink, but symlink support is disabled."); |
| 311 | } |
| 312 | |
| 313 | FileSystem delegate = getDelegate(linkPath); |
| 314 | delegate.createSymbolicLink(adjustPath(linkPath, delegate), targetFragment); |
| 315 | } |
| 316 | |
| 317 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 318 | protected boolean exists(Path path, boolean followSymlinks) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 319 | try { |
| 320 | path = followSymlinks ? internalResolveSymlink(path) : path; |
| 321 | } catch (IOException e) { |
| 322 | return false; |
| 323 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 324 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 325 | return delegate.exists(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 329 | protected FileStatus stat(Path path, boolean followSymlinks) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 330 | path = followSymlinks ? internalResolveSymlink(path) : path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 331 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 332 | return delegate.stat(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Needs to be overridden for the delegation logic, because the |
| 336 | // UnixFileSystem implements statNullable and stat as separate codepaths. |
| 337 | // More generally, we wish to delegate all filesystem operations. |
| 338 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 339 | protected FileStatus statNullable(Path path, boolean followSymlinks) { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 340 | try { |
| 341 | path = followSymlinks ? internalResolveSymlink(path) : path; |
| 342 | } catch (IOException e) { |
| 343 | return null; |
| 344 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 345 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 346 | return delegate.statNullable(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | @Nullable |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 351 | protected FileStatus statIfFound(Path path, boolean followSymlinks) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 352 | path = followSymlinks ? internalResolveSymlink(path) : path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 353 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 354 | return delegate.statIfFound(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 358 | * Retrieves the directory entries for the specified path under the assumption that {@code |
| 359 | * resolvedPath} is the resolved path of {@code path} in one of the underlying file systems. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 360 | * |
| 361 | * @param path the {@link Path} whose children are to be retrieved |
| 362 | */ |
| 363 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 364 | protected Collection<String> getDirectoryEntries(Path path) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 365 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 366 | FileSystem delegate = getDelegate(path); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 367 | Path resolvedPath = adjustPath(path, delegate); |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame^] | 368 | return delegate.getDirectoryEntries(resolvedPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // No need for the more complex logic of getDirectoryEntries; it calls it implicitly. |
| 372 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 373 | protected Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 374 | path = followSymlinks ? internalResolveSymlink(path) : path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 375 | FileSystem delegate = getDelegate(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 376 | return delegate.readdir(adjustPath(path, delegate), false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 380 | protected boolean isReadable(Path path) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 381 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 382 | FileSystem delegate = getDelegate(path); |
| 383 | return delegate.isReadable(adjustPath(path, delegate)); |
| 384 | } |
| 385 | |
| 386 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 387 | protected void setReadable(Path path, boolean readable) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 388 | path = internalResolveSymlink(path); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 389 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 390 | FileSystem delegate = getDelegate(path); |
| 391 | delegate.setReadable(adjustPath(path, delegate), readable); |
| 392 | } |
| 393 | |
| 394 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 395 | protected boolean isWritable(Path path) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 396 | if (!supportsModifications(path)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 397 | return false; |
| 398 | } |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 399 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 400 | FileSystem delegate = getDelegate(path); |
| 401 | return delegate.isWritable(adjustPath(path, delegate)); |
| 402 | } |
| 403 | |
| 404 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 405 | public void setWritable(Path path, boolean writable) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 406 | checkModifiable(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 407 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 408 | FileSystem delegate = getDelegate(path); |
| 409 | delegate.setWritable(adjustPath(path, delegate), writable); |
| 410 | } |
| 411 | |
| 412 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 413 | protected boolean isExecutable(Path path) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 414 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 415 | FileSystem delegate = getDelegate(path); |
| 416 | return delegate.isExecutable(adjustPath(path, delegate)); |
| 417 | } |
| 418 | |
| 419 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 420 | protected void setExecutable(Path path, boolean executable) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 421 | path = internalResolveSymlink(path); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 422 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 423 | FileSystem delegate = getDelegate(path); |
| 424 | delegate.setExecutable(adjustPath(path, delegate), executable); |
| 425 | } |
| 426 | |
| 427 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 428 | protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 429 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 430 | FileSystem delegate = getDelegate(path); |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 431 | return delegate.getFastDigest(adjustPath(path, delegate), hashFunction); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 435 | public byte[] getxattr(Path path, String name) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 436 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 437 | FileSystem delegate = getDelegate(path); |
Nathan Harmata | e1f187a | 2015-09-16 20:03:08 +0000 | [diff] [blame] | 438 | return delegate.getxattr(adjustPath(path, delegate), name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 442 | protected InputStream getInputStream(Path path) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 443 | path = internalResolveSymlink(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 444 | FileSystem delegate = getDelegate(path); |
| 445 | return delegate.getInputStream(adjustPath(path, delegate)); |
| 446 | } |
| 447 | |
| 448 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 449 | protected OutputStream getOutputStream(Path path, boolean append) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 450 | path = internalResolveSymlink(path); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 451 | checkModifiable(path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 452 | FileSystem delegate = getDelegate(path); |
| 453 | return delegate.getOutputStream(adjustPath(path, delegate), append); |
| 454 | } |
| 455 | |
| 456 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 457 | public void renameTo(Path sourcePath, Path targetPath) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 458 | sourcePath = internalResolveSymlink(sourcePath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 459 | FileSystem sourceDelegate = getDelegate(sourcePath); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 460 | if (!sourceDelegate.supportsModifications(sourcePath)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 461 | throw new UnsupportedOperationException( |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 462 | String.format( |
| 463 | "The filesystem for the source path %s does not support modifications.", |
| 464 | sourcePath.getPathString())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | } |
| 466 | sourcePath = adjustPath(sourcePath, sourceDelegate); |
| 467 | |
| 468 | FileSystem targetDelegate = getDelegate(targetPath); |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 469 | if (!targetDelegate.supportsModifications(targetPath)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 470 | throw new UnsupportedOperationException( |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 471 | String.format( |
| 472 | "The filesystem for the target path %s does not support modifications.", |
| 473 | targetPath.getPathString())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 474 | } |
| 475 | targetPath = adjustPath(targetPath, targetDelegate); |
| 476 | |
| 477 | if (sourceDelegate == targetDelegate) { |
| 478 | // Easy, same filesystem. |
| 479 | sourceDelegate.renameTo(sourcePath, targetPath); |
| 480 | return; |
| 481 | } else { |
| 482 | // Copy across filesystems, then delete. |
| 483 | // copyFile throws on failure, so delete will never be reached if it fails. |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 484 | FileSystemUtils.copyFile(sourcePath, targetPath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 485 | sourceDelegate.delete(sourcePath); |
| 486 | } |
| 487 | } |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 488 | |
| 489 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 490 | protected void createFSDependentHardLink(Path linkPath, Path originalPath) throws IOException { |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 491 | checkModifiable(linkPath); |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 492 | |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 493 | originalPath = internalResolveSymlink(originalPath); |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 494 | FileSystem originalDelegate = getDelegate(originalPath); |
| 495 | FileSystem linkDelegate = getDelegate(linkPath); |
| 496 | |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 497 | if (!originalDelegate.equals(linkDelegate) |
| 498 | || !linkDelegate.supportsHardLinksNatively(linkPath)) { |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 499 | throw new UnsupportedOperationException( |
| 500 | "Attempted to create a hard link, but hard link support is disabled."); |
| 501 | } |
| 502 | linkDelegate.createFSDependentHardLink( |
| 503 | adjustPath(linkPath, linkDelegate), adjustPath(originalPath, originalDelegate)); |
| 504 | } |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 505 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 506 | private Path internalResolveSymlink(Path path) throws IOException { |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 507 | while (isSymbolicLink(path)) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 508 | PathFragment pathFragment = resolveOneLink(path); |
tomlu | 33700d6 | 2017-10-20 02:26:00 +0200 | [diff] [blame] | 509 | path = path.getRelative(pathFragment); |
| 510 | } |
| 511 | return path; |
| 512 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 513 | } |