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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.base.Predicate; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.skylarkinterface.SkylarkPrintable; |
| 20 | import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.util.StringCanonicalizer; |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.FileSystem.HashFunction; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.io.File; |
| 24 | import java.io.FileNotFoundException; |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.ObjectInputStream; |
| 28 | import java.io.ObjectOutputStream; |
| 29 | import java.io.OutputStream; |
| 30 | import java.io.Serializable; |
| 31 | import java.lang.ref.Reference; |
| 32 | import java.lang.ref.ReferenceQueue; |
| 33 | import java.lang.ref.WeakReference; |
buchgr | 5ef576f | 2017-09-14 14:36:44 +0200 | [diff] [blame] | 34 | import java.net.URI; |
| 35 | import java.net.URISyntaxException; |
tomlu | 0a82e70 | 2017-10-23 18:16:44 +0200 | [diff] [blame] | 36 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import java.util.Collection; |
| 38 | import java.util.IdentityHashMap; |
| 39 | import java.util.Objects; |
| 40 | |
| 41 | /** |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 42 | * Instances of this class represent pathnames, forming a tree structure to implement sharing of |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 43 | * common prefixes (parent directory names). A node in these trees is something like foo, bar, .., |
| 44 | * ., or /. If the instance is not a root path, it will have a parent path. A path can also have |
| 45 | * children, which are indexed by name in a map. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | * |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 47 | * <p>There is some limited support for Windows-style paths. Most importantly, drive identifiers in |
| 48 | * front of a path (c:/abc) are supported. However, Windows-style backslash separators |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | * (C:\\foo\\bar) and drive-relative paths ("C:foo") are explicitly not supported, same with |
| 50 | * advanced features like \\\\network\\paths and \\\\?\\unc\\paths. |
| 51 | * |
| 52 | * <p>{@link FileSystem} implementations maintain pointers into this graph. |
| 53 | */ |
| 54 | @ThreadSafe |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 55 | public class Path implements Comparable<Path>, Serializable, SkylarkPrintable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 57 | /** Filesystem-specific factory for {@link Path} objects. */ |
| 58 | public static interface PathFactory { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Creates the root of all paths used by a filesystem. |
| 61 | * |
| 62 | * <p>All other paths are instantiated via {@link Path#createChildPath(String)} which calls |
| 63 | * {@link #createChildPath(Path, String)}. |
| 64 | * |
| 65 | * <p>Beware: this is called during the FileSystem constructor which may occur before subclasses |
| 66 | * are completely initialized. |
| 67 | */ |
| 68 | Path createRootPath(FileSystem filesystem); |
| 69 | |
| 70 | /** |
| 71 | * Create a child path of the given parent. |
| 72 | * |
| 73 | * <p>All {@link Path} objects are instantiated via this method, with the sole exception of the |
| 74 | * filesystem root, which is created by {@link #createRootPath(FileSystem)}. |
| 75 | */ |
| 76 | Path createChildPath(Path parent, String childName); |
| 77 | |
| 78 | /** |
nharmata | 39e659e | 2017-04-04 14:42:23 +0000 | [diff] [blame] | 79 | * Makes the proper invocation of {@link FileSystem#getCachedChildPathInternal}, doing |
| 80 | * filesystem-specific logic if necessary. |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 81 | */ |
nharmata | 39e659e | 2017-04-04 14:42:23 +0000 | [diff] [blame] | 82 | Path getCachedChildPathInternal(Path path, String childName); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | private static FileSystem fileSystemForSerialization; |
| 86 | |
| 87 | /** |
| 88 | * We need to specify used FileSystem. In this case we can save memory during the serialization. |
| 89 | */ |
| 90 | public static void setFileSystemForSerialization(FileSystem fileSystem) { |
| 91 | fileSystemForSerialization = fileSystem; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns FileSystem that we are using. |
| 96 | */ |
| 97 | public static FileSystem getFileSystemForSerialization() { |
| 98 | return fileSystemForSerialization; |
| 99 | } |
| 100 | |
| 101 | // These are basically final, but can't be marked as such in order to support serialization. |
| 102 | private FileSystem fileSystem; |
| 103 | private String name; |
| 104 | private Path parent; |
| 105 | private int depth; |
| 106 | private int hashCode; |
| 107 | |
| 108 | private static final ReferenceQueue<Path> REFERENCE_QUEUE = new ReferenceQueue<>(); |
| 109 | |
| 110 | private static class PathWeakReferenceForCleanup extends WeakReference<Path> { |
| 111 | final Path parent; |
| 112 | final String baseName; |
| 113 | |
| 114 | PathWeakReferenceForCleanup(Path referent, ReferenceQueue<Path> referenceQueue) { |
| 115 | super(referent, referenceQueue); |
| 116 | parent = referent.getParentDirectory(); |
| 117 | baseName = referent.getBaseName(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private static final Thread PATH_CHILD_CACHE_CLEANUP_THREAD = new Thread("Path cache cleanup") { |
| 122 | @Override |
| 123 | public void run() { |
| 124 | while (true) { |
| 125 | try { |
| 126 | PathWeakReferenceForCleanup ref = (PathWeakReferenceForCleanup) REFERENCE_QUEUE.remove(); |
| 127 | Path parent = ref.parent; |
| 128 | synchronized (parent) { |
| 129 | // It's possible that since this reference was enqueued for deletion, the Path was |
| 130 | // recreated with a new entry in the map. We definitely shouldn't delete that entry, so |
| 131 | // check to make sure they're the same. |
| 132 | Reference<Path> currentRef = ref.parent.children.get(ref.baseName); |
| 133 | if (currentRef == ref) { |
| 134 | ref.parent.children.remove(ref.baseName); |
| 135 | } |
| 136 | } |
| 137 | } catch (InterruptedException e) { |
| 138 | // Ignored. |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | static { |
| 145 | PATH_CHILD_CACHE_CLEANUP_THREAD.setDaemon(true); |
| 146 | PATH_CHILD_CACHE_CLEANUP_THREAD.start(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * A mapping from a child file name to the {@link Path} representing it. |
| 151 | * |
| 152 | * <p>File names must be a single path segment. The strings must be |
| 153 | * canonical. We use IdentityHashMap instead of HashMap for reasons of space |
| 154 | * efficiency: instances are smaller by a single word. Also, since all path |
| 155 | * segments are interned, the universe of Paths holds a minimal number of |
| 156 | * references to strings. (It's doubtful that there's any time gain from use |
| 157 | * of an IdentityHashMap, since the time saved by avoiding string equality |
| 158 | * tests during hash lookups is probably equal to the time spent eagerly |
| 159 | * interning strings, unless the collision rate is high.) |
| 160 | * |
| 161 | * <p>The Paths are stored as weak references to ensure that a live |
| 162 | * Path for a directory does not hold a strong reference to all of its |
| 163 | * descendants, which would prevent collection of paths we never intend to |
| 164 | * use again. Stale references in the map must be treated as absent. |
| 165 | * |
| 166 | * <p>A Path may be recycled once there is no Path that refers to it or |
| 167 | * to one of its descendants. This means that any data stored in the |
| 168 | * Path instance by one of its subclasses must be recoverable: it's ok to |
| 169 | * store data in Paths as an optimization, but there must be another |
| 170 | * source for that data in case the Path is recycled. |
| 171 | * |
| 172 | * <p>We intentionally avoid using the existing library classes for reasons of |
| 173 | * space efficiency: while ConcurrentHashMap would reduce our locking |
| 174 | * overhead, and ReferenceMap would simplify the code a little, both of those |
| 175 | * classes have much higher per-instance overheads than IdentityHashMap. |
| 176 | * |
| 177 | * <p>The Path object must be synchronized while children is being |
| 178 | * accessed. |
| 179 | */ |
Nathan Harmata | a65fcf9 | 2015-08-20 21:14:44 +0000 | [diff] [blame] | 180 | private volatile IdentityHashMap<String, Reference<Path>> children; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 183 | * Create a path instance. |
| 184 | * |
| 185 | * <p>Should only be called by {@link PathFactory#createChildPath(Path, String)}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 186 | * |
| 187 | * @param name the name of this path; it must be canonicalized with {@link |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 188 | * StringCanonicalizer#intern} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | * @param parent this path's parent |
| 190 | */ |
| 191 | protected Path(FileSystem fileSystem, String name, Path parent) { |
| 192 | this.fileSystem = fileSystem; |
| 193 | this.name = name; |
| 194 | this.parent = parent; |
| 195 | this.depth = parent == null ? 0 : parent.depth + 1; |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 196 | |
| 197 | // No need to include the drive letter in the hash code, because it's derived from the parent |
| 198 | // and/or the name. |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 199 | if (fileSystem == null || fileSystem.isFilePathCaseSensitive()) { |
| 200 | this.hashCode = Objects.hash(parent, name); |
| 201 | } else { |
| 202 | this.hashCode = Objects.hash(parent, name.toLowerCase()); |
| 203 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 207 | * Create the root path. |
| 208 | * |
| 209 | * <p>Should only be called by {@link PathFactory#createRootPath(FileSystem)}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | */ |
| 211 | protected Path(FileSystem fileSystem) { |
| 212 | this(fileSystem, StringCanonicalizer.intern("/"), null); |
| 213 | } |
| 214 | |
| 215 | private void writeObject(ObjectOutputStream out) throws IOException { |
janakr | 150858b | 2017-07-25 00:04:53 +0200 | [diff] [blame] | 216 | Preconditions.checkState( |
| 217 | fileSystem == fileSystemForSerialization, "%s %s", fileSystem, fileSystemForSerialization); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 218 | out.writeUTF(getPathString()); |
| 219 | } |
| 220 | |
| 221 | private void readObject(ObjectInputStream in) throws IOException { |
| 222 | fileSystem = fileSystemForSerialization; |
| 223 | String p = in.readUTF(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 224 | PathFragment pf = PathFragment.create(p); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 225 | PathFragment parentDir = pf.getParentDirectory(); |
| 226 | if (parentDir == null) { |
| 227 | this.name = "/"; |
| 228 | this.parent = null; |
| 229 | this.depth = 0; |
| 230 | } else { |
| 231 | this.name = pf.getBaseName(); |
| 232 | this.parent = fileSystem.getPath(parentDir); |
| 233 | this.depth = this.parent.depth + 1; |
| 234 | } |
| 235 | this.hashCode = Objects.hash(parent, name); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 236 | reinitializeAfterDeserialization(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the filesystem instance to which this path belongs. |
| 241 | */ |
| 242 | public FileSystem getFileSystem() { |
| 243 | return fileSystem; |
| 244 | } |
| 245 | |
| 246 | public boolean isRootDirectory() { |
| 247 | return parent == null; |
| 248 | } |
| 249 | |
| 250 | protected Path createChildPath(String childName) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 251 | return fileSystem.getPathFactory().createChildPath(this, childName); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Reinitializes this object after deserialization. |
| 256 | * |
| 257 | * <p>Derived classes should use this hook to initialize additional state. |
| 258 | */ |
| 259 | protected void reinitializeAfterDeserialization() {} |
| 260 | |
| 261 | /** |
| 262 | * Returns true if {@code ancestorPath} may be an ancestor of {@code path}. |
| 263 | * |
| 264 | * <p>The return value may be a false positive, but it cannot be a false negative. This means that |
| 265 | * a true return value doesn't mean the ancestor candidate is really an ancestor, however a false |
| 266 | * return value means it's guaranteed that {@code ancestorCandidate} is not an ancestor of this |
| 267 | * path. |
| 268 | * |
| 269 | * <p>Subclasses may override this method with filesystem-specific logic, e.g. a Windows |
| 270 | * filesystem may return false if the ancestor path is on a different drive than this one, because |
| 271 | * it is then guaranteed that the ancestor candidate cannot be an ancestor of this path. |
| 272 | * |
| 273 | * @param ancestorCandidate the path that may or may not be an ancestor of this one |
| 274 | */ |
| 275 | protected boolean isMaybeRelativeTo(Path ancestorCandidate) { |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns true if this directory is top-level, i.e. it is its own parent. |
| 281 | * |
| 282 | * <p>When canonicalizing paths the ".." segment of a top-level directory always resolves to the |
| 283 | * directory itself. |
| 284 | * |
| 285 | * <p>On Unix, a top-level directory would be just the filesystem root ("/), on Windows it would |
| 286 | * be the filesystem root and the volume roots. |
| 287 | */ |
| 288 | protected boolean isTopLevelDirectory() { |
| 289 | return isRootDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns the child path named name, or creates such a path (and caches it) |
| 294 | * if it doesn't already exist. |
| 295 | */ |
| 296 | private Path getCachedChildPath(String childName) { |
nharmata | 39e659e | 2017-04-04 14:42:23 +0000 | [diff] [blame] | 297 | return fileSystem.getPathFactory().getCachedChildPathInternal(this, childName); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 298 | } |
| 299 | |
nharmata | 39e659e | 2017-04-04 14:42:23 +0000 | [diff] [blame] | 300 | /** |
| 301 | * Internal method only intended to be called by {@link PathFactory#getCachedChildPathInternal}. |
| 302 | */ |
| 303 | public static Path getCachedChildPathInternal(Path parent, String childName, boolean cacheable) { |
Nathan Harmata | a65fcf9 | 2015-08-20 21:14:44 +0000 | [diff] [blame] | 304 | // We get a canonical instance since 'children' is an IdentityHashMap. |
nharmata | 39e659e | 2017-04-04 14:42:23 +0000 | [diff] [blame] | 305 | childName = StringCanonicalizer.intern(childName); |
| 306 | if (!cacheable) { |
Laszlo Csomor | 8679e4c | 2017-01-03 15:21:08 +0000 | [diff] [blame] | 307 | // Non-cacheable children won't show up in `children` so applyToChildren won't run for these. |
| 308 | return parent.createChildPath(childName); |
| 309 | } |
Googler | 0e59bb6 | 2017-02-04 08:31:20 +0000 | [diff] [blame] | 310 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 311 | synchronized (parent) { |
Googler | 0e59bb6 | 2017-02-04 08:31:20 +0000 | [diff] [blame] | 312 | if (parent.children == null) { |
| 313 | // 66% of Paths have size == 1, 80% <= 2 |
| 314 | parent.children = new IdentityHashMap<>(1); |
| 315 | } |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 316 | Reference<Path> childRef = parent.children.get(childName); |
Nathan Harmata | fee390d | 2015-09-02 22:46:53 +0000 | [diff] [blame] | 317 | Path child; |
| 318 | if (childRef == null || (child = childRef.get()) == null) { |
Laszlo Csomor | 8679e4c | 2017-01-03 15:21:08 +0000 | [diff] [blame] | 319 | child = parent.createChildPath(childName); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 320 | parent.children.put(childName, new PathWeakReferenceForCleanup(child, REFERENCE_QUEUE)); |
Nathan Harmata | a65fcf9 | 2015-08-20 21:14:44 +0000 | [diff] [blame] | 321 | } |
Nathan Harmata | fee390d | 2015-09-02 22:46:53 +0000 | [diff] [blame] | 322 | return child; |
Nathan Harmata | a65fcf9 | 2015-08-20 21:14:44 +0000 | [diff] [blame] | 323 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Applies the specified function to each {@link Path} that is an existing direct |
| 328 | * descendant of this one. The Predicate is evaluated only for its |
| 329 | * side-effects. |
| 330 | * |
| 331 | * <p>This function exists to hide the "children" field, whose complex |
| 332 | * synchronization and identity requirements are too unsafe to be exposed to |
| 333 | * subclasses. For example, the "children" field must be synchronized for |
| 334 | * the duration of any iteration over it; it may be null; and references |
| 335 | * within it may be stale, and must be ignored. |
| 336 | */ |
| 337 | protected synchronized void applyToChildren(Predicate<Path> function) { |
| 338 | if (children != null) { |
| 339 | for (Reference<Path> childRef : children.values()) { |
| 340 | Path child = childRef.get(); |
| 341 | if (child != null) { |
| 342 | function.apply(child); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Returns whether this path is recursively "under" {@code prefix} - that is, |
| 350 | * whether {@code path} is a prefix of this path. |
| 351 | * |
| 352 | * <p>This method returns {@code true} when called with this path itself. This |
| 353 | * method acts independently of the existence of files or folders. |
| 354 | * |
| 355 | * @param prefix a path which may or may not be a prefix of this path |
| 356 | */ |
| 357 | public boolean startsWith(Path prefix) { |
| 358 | Path n = this; |
| 359 | for (int i = 0, len = depth - prefix.depth; i < len; i++) { |
| 360 | n = n.getParentDirectory(); |
| 361 | } |
| 362 | return prefix.equals(n); |
| 363 | } |
| 364 | |
| 365 | /** |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 366 | * Computes a string representation of this path, and writes it to the given string builder. Only |
| 367 | * called locally with a new instance. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 368 | */ |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 369 | protected void buildPathString(StringBuilder result) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 370 | if (isRootDirectory()) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 371 | result.append(PathFragment.ROOT_DIR); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 372 | } else { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 373 | parent.buildPathString(result); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 374 | if (!parent.isRootDirectory()) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 375 | result.append(PathFragment.SEPARATOR_CHAR); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 376 | } |
| 377 | result.append(name); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
buchgr | 5ef576f | 2017-09-14 14:36:44 +0200 | [diff] [blame] | 382 | * Returns the path encoded as an {@link URI}. |
| 383 | * |
| 384 | * <p>This concrete implementation returns URIs with "file" as the scheme. |
| 385 | * For Example: |
| 386 | * - On Unix the path "/tmp/foo bar.txt" will be encoded as |
| 387 | * "file:///tmp/foo%20bar.txt". |
| 388 | * - On Windows the path "C:\Temp\Foo Bar.txt" will be encoded as |
| 389 | * "file:///C:/Temp/Foo%20Bar.txt" |
| 390 | * |
| 391 | * <p>Implementors extending this class for special filesystems will likely need to override |
| 392 | * this method. |
| 393 | * |
| 394 | * @throws URISyntaxException if the URI cannot be constructed. |
| 395 | */ |
| 396 | public URI toURI() { |
| 397 | String ps = getPathString(); |
| 398 | if (!ps.startsWith("/")) { |
| 399 | // On Windows URI's need to start with a '/'. i.e. C:\Foo\Bar would be file:///C:/Foo/Bar |
| 400 | ps = "/" + ps; |
| 401 | } |
| 402 | try { |
| 403 | return new URI("file", |
| 404 | // Needs to be "" instead of null, so that toString() will append "//" after the scheme. |
| 405 | // We need this for backwards compatibility reasons as some consumers of the BEP are |
| 406 | // broken. |
| 407 | "", |
| 408 | ps, null, null); |
| 409 | } catch (URISyntaxException e) { |
| 410 | throw new IllegalStateException(e); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 415 | * Returns the path as a string. |
| 416 | */ |
| 417 | public String getPathString() { |
| 418 | // Profile driven optimization: |
| 419 | // Preallocate a size determined by the depth, so that |
| 420 | // we do not have to expand the capacity of the StringBuilder |
| 421 | StringBuilder builder = new StringBuilder(depth * 20); |
| 422 | buildPathString(builder); |
| 423 | return builder.toString(); |
| 424 | } |
| 425 | |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 426 | @Override |
| 427 | public void repr(SkylarkPrinter printer) { |
| 428 | printer.append(getPathString()); |
| 429 | } |
| 430 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 431 | /** |
| 432 | * Returns the path as a string. |
| 433 | */ |
| 434 | @Override |
| 435 | public String toString() { |
| 436 | return getPathString(); |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public int hashCode() { |
| 441 | return hashCode; |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public boolean equals(Object other) { |
| 446 | if (this == other) { |
| 447 | return true; |
| 448 | } |
| 449 | if (!(other instanceof Path)) { |
| 450 | return false; |
| 451 | } |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 452 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 453 | Path otherPath = (Path) other; |
Googler | baeba8b2 | 2016-04-20 19:20:37 +0000 | [diff] [blame] | 454 | if (hashCode != otherPath.hashCode) { |
| 455 | return false; |
| 456 | } |
Yun Peng | 352f7e7 | 2016-05-09 11:08:25 +0000 | [diff] [blame] | 457 | |
| 458 | if (!fileSystem.equals(otherPath.fileSystem)) { |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | if (fileSystem.isFilePathCaseSensitive()) { |
| 463 | return name.equals(otherPath.name) |
| 464 | && Objects.equals(parent, otherPath.parent); |
| 465 | } else { |
| 466 | return name.toLowerCase().equals(otherPath.name.toLowerCase()) |
| 467 | && Objects.equals(parent, otherPath.parent); |
| 468 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Returns a string of debugging information associated with this path. |
| 473 | * The results are unspecified and MUST NOT be interpreted programmatically. |
| 474 | */ |
| 475 | protected String toDebugString() { |
| 476 | return ""; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Returns a path representing the parent directory of this path, |
| 481 | * or null iff this Path represents the root of the filesystem. |
| 482 | * |
| 483 | * <p>Note: This method normalises ".." and "." path segments by string |
| 484 | * processing, not by directory lookups. |
| 485 | */ |
| 486 | public Path getParentDirectory() { |
| 487 | return parent; |
| 488 | } |
| 489 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 490 | /** Prefer to use {@link #exists(FileSystem)}. */ |
| 491 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 492 | public boolean exists() { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 493 | return exists(fileSystem); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Returns true iff this path denotes an existing file of any kind. Follows symbolic links. |
| 498 | * |
| 499 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 500 | * Path. |
| 501 | */ |
| 502 | public boolean exists(FileSystem fileSystem) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 503 | return fileSystem.exists(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 504 | } |
| 505 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 506 | /** Prefer to use {@link #exists(FileSystem, Symlinks)}. */ |
| 507 | @Deprecated |
| 508 | public boolean exists(Symlinks followSymlinks) { |
| 509 | return exists(fileSystem, followSymlinks); |
| 510 | } |
| 511 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 512 | /** |
| 513 | * Returns true iff this path denotes an existing file of any kind. |
| 514 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 515 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 516 | * Path. |
| 517 | * |
| 518 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 519 | * link is dereferenced until a file other than a symbolic link is found |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 520 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 521 | public boolean exists(FileSystem fileSystem, Symlinks followSymlinks) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 522 | return fileSystem.exists(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 523 | } |
| 524 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 525 | /** Prefer to use {@link #getDirectoryEntries(FileSystem)}. */ |
| 526 | @Deprecated |
| 527 | public Collection<Path> getDirectoryEntries() throws IOException, FileNotFoundException { |
| 528 | return getDirectoryEntries(fileSystem); |
| 529 | } |
| 530 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 531 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 532 | * Returns a new, immutable collection containing the names of all entities within the directory |
| 533 | * denoted by the current path. Follows symbolic links. |
| 534 | * |
| 535 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 536 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 537 | * |
| 538 | * @throws FileNotFoundException If the directory is not found |
| 539 | * @throws IOException If the path does not denote a directory |
| 540 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 541 | public Collection<Path> getDirectoryEntries(FileSystem fileSystem) |
| 542 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 543 | Collection<String> entries = fileSystem.getDirectoryEntries(this); |
tomlu | 0a82e70 | 2017-10-23 18:16:44 +0200 | [diff] [blame] | 544 | Collection<Path> result = new ArrayList<>(entries.size()); |
| 545 | for (String entry : entries) { |
| 546 | result.add(getChild(entry)); |
| 547 | } |
| 548 | return result; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 549 | } |
| 550 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 551 | /** Prefer to use {@link #readdir(FileSystem, Symlinks)}. */ |
| 552 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 553 | public Collection<Dirent> readdir(Symlinks followSymlinks) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 554 | return readdir(fileSystem, followSymlinks); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 558 | * Returns a collection of the names and types of all entries within the directory denoted by the |
| 559 | * current path. Follows symbolic links if {@code followSymlinks} is true. Note that the order of |
| 560 | * the returned entries is not guaranteed. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 561 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 562 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 563 | * Path. |
| 564 | * |
| 565 | * @param followSymlinks whether to follow symlinks or not |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 566 | * @throws FileNotFoundException If the directory is not found |
| 567 | * @throws IOException If the path does not denote a directory |
| 568 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 569 | public Collection<Dirent> readdir(FileSystem fileSystem, Symlinks followSymlinks) |
| 570 | throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 571 | return fileSystem.readdir(this, followSymlinks.toBoolean()); |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** Prefer to use {@link #stat(FileSystem)}. */ |
| 575 | @Deprecated |
| 576 | public FileStatus stat() throws IOException { |
| 577 | return stat(fileSystem); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Returns the status of a file, following symbolic links. |
| 582 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 583 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 584 | * Path. |
| 585 | * |
| 586 | * @throws IOException if there was an error obtaining the file status. Note, some implementations |
| 587 | * may defer the I/O, and hence the throwing of the exception, until the accessor methods of |
| 588 | * {@code FileStatus} are called. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 589 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 590 | public FileStatus stat(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 591 | return fileSystem.stat(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 592 | } |
| 593 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 594 | /** Prefer to use {@link #statNullable(FileSystem)}. */ |
| 595 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 596 | public FileStatus statNullable() { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 597 | return statNullable(fileSystem); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Like stat(), but returns null on file-nonexistence instead of throwing. |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 602 | * |
| 603 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 604 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 605 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 606 | public FileStatus statNullable(FileSystem fileSystem) { |
| 607 | return statNullable(fileSystem, Symlinks.FOLLOW); |
| 608 | } |
| 609 | |
| 610 | /** Prefer to use {@link #statNullable(FileSystem, Symlinks)}. */ |
| 611 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 612 | public FileStatus statNullable(Symlinks symlinks) { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 613 | return statNullable(fileSystem, symlinks); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Like stat(), but returns null on file-nonexistence instead of throwing. |
| 618 | * |
| 619 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 620 | * Path. |
| 621 | */ |
| 622 | public FileStatus statNullable(FileSystem fileSystem, Symlinks symlinks) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 623 | return fileSystem.statNullable(this, symlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 624 | } |
| 625 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 626 | /** Prefer to use {@link #stat(FileSystem, Symlinks)}. */ |
| 627 | @Deprecated |
| 628 | public FileStatus stat(Symlinks followSymlinks) throws IOException { |
| 629 | return stat(fileSystem, followSymlinks); |
| 630 | } |
| 631 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 632 | /** |
| 633 | * Returns the status of a file, optionally following symbolic links. |
| 634 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 635 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 636 | * Path. |
| 637 | * |
| 638 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 639 | * link is dereferenced until a file other than a symbolic link is found |
| 640 | * @throws IOException if there was an error obtaining the file status. Note, some implementations |
| 641 | * may defer the I/O, and hence the throwing of the exception, until the accessor methods of |
| 642 | * {@code FileStatus} are called |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 643 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 644 | public FileStatus stat(FileSystem fileSystem, Symlinks followSymlinks) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 645 | return fileSystem.stat(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 646 | } |
| 647 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 648 | /** Prefer to use {@link #statIfFound(FileSystem)}. */ |
| 649 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 650 | public FileStatus statIfFound() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 651 | return statIfFound(fileSystem); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Like {@link #stat}, but may return null if the file is not found (corresponding to {@code |
| 656 | * ENOENT} and {@code ENOTDIR} in Unix's stat(2) function) instead of throwing. Follows symbolic |
| 657 | * links. |
| 658 | * |
| 659 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 660 | * Path. |
| 661 | */ |
| 662 | public FileStatus statIfFound(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 663 | return fileSystem.statIfFound(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 664 | } |
| 665 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 666 | /** Prefer to use {@link #statIfFound(FileSystem, Symlinks)}. */ |
| 667 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 668 | public FileStatus statIfFound(Symlinks followSymlinks) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 669 | return statIfFound(fileSystem, followSymlinks); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Like {@link #stat}, but may return null if the file is not found (corresponding to {@code |
| 674 | * ENOENT} and {@code ENOTDIR} in Unix's stat(2) function) instead of throwing. |
| 675 | * |
| 676 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 677 | * Path. |
| 678 | * |
| 679 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 680 | * link is dereferenced until a file other than a symbolic link is found |
| 681 | */ |
| 682 | public FileStatus statIfFound(FileSystem fileSystem, Symlinks followSymlinks) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 683 | return fileSystem.statIfFound(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 684 | } |
| 685 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 686 | /** Prefer to use {@link #isDirectory()} (FileSystem)}. */ |
| 687 | @Deprecated |
| 688 | public boolean isDirectory() { |
| 689 | return isDirectory(fileSystem); |
| 690 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 691 | |
| 692 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 693 | * Returns true iff this path denotes an existing directory. Follows symbolic links. |
| 694 | * |
| 695 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 696 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 697 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 698 | public boolean isDirectory(FileSystem fileSystem) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 699 | return fileSystem.isDirectory(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 700 | } |
| 701 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 702 | /** Prefer to use {@link #isDirectory(FileSystem, Symlinks)}. */ |
| 703 | @Deprecated |
| 704 | public boolean isDirectory(Symlinks followSymlinks) { |
| 705 | return isDirectory(fileSystem, followSymlinks); |
| 706 | } |
| 707 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 708 | /** |
| 709 | * Returns true iff this path denotes an existing directory. |
| 710 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 711 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 712 | * Path. |
| 713 | * |
| 714 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 715 | * link is dereferenced until a file other than a symbolic link is found |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 716 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 717 | public boolean isDirectory(FileSystem fileSystem, Symlinks followSymlinks) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 718 | return fileSystem.isDirectory(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 719 | } |
| 720 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 721 | /** Prefer to use {@link #isFile(FileSystem)}. */ |
| 722 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 723 | public boolean isFile() { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 724 | return isFile(fileSystem); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Returns true iff this path denotes an existing regular or special file. Follows symbolic links. |
| 729 | * |
| 730 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 731 | * Path. |
| 732 | * |
| 733 | * <p>For our purposes, "file" includes special files (socket, fifo, block or char devices) too; |
| 734 | * it excludes symbolic links and directories. |
| 735 | */ |
| 736 | public boolean isFile(FileSystem fileSystem) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 737 | return fileSystem.isFile(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 738 | } |
| 739 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 740 | /** Prefer to use {@link #isFile(FileSystem, Symlinks)}. */ |
| 741 | @Deprecated |
| 742 | public boolean isFile(Symlinks followSymlinks) { |
| 743 | return isFile(fileSystem, followSymlinks); |
| 744 | } |
| 745 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 746 | /** |
| 747 | * Returns true iff this path denotes an existing regular or special file. |
| 748 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 749 | * <p>For our purposes, a "file" includes special files (socket, fifo, block or char devices) too; |
| 750 | * it excludes symbolic links and directories. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 751 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 752 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 753 | * Path. |
| 754 | * |
| 755 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 756 | * link is dereferenced until a file other than a symbolic link is found. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 757 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 758 | public boolean isFile(FileSystem fileSystem, Symlinks followSymlinks) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 759 | return fileSystem.isFile(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 760 | } |
| 761 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 762 | /** Prefer to use {@link #isSpecialFile(FileSystem)}. */ |
| 763 | @Deprecated |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 764 | public boolean isSpecialFile() { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 765 | return isSpecialFile(fileSystem); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * Returns true iff this path denotes an existing special file (e.g. fifo). Follows symbolic |
| 770 | * links. |
| 771 | * |
| 772 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 773 | * Path. |
| 774 | */ |
| 775 | public boolean isSpecialFile(FileSystem fileSystem) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 776 | return fileSystem.isSpecialFile(this, true); |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 777 | } |
| 778 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 779 | /** Prefer to use {@link #isSpecialFile(FileSystem, Symlinks)}. */ |
| 780 | @Deprecated |
| 781 | public boolean isSpecialFile(Symlinks followSymlinks) { |
| 782 | return isSpecialFile(fileSystem, followSymlinks); |
| 783 | } |
| 784 | |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 785 | /** |
| 786 | * Returns true iff this path denotes an existing special file (e.g. fifo). |
| 787 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 788 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 789 | * Path. |
| 790 | * |
| 791 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 792 | * link is dereferenced until a path other than a symbolic link is found. |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 793 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 794 | public boolean isSpecialFile(FileSystem fileSystem, Symlinks followSymlinks) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 795 | return fileSystem.isSpecialFile(this, followSymlinks.toBoolean()); |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 796 | } |
| 797 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 798 | /** Prefer to use {@link #isSymbolicLink(FileSystem)}. */ |
| 799 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 800 | public boolean isSymbolicLink() { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 801 | return isSymbolicLink(fileSystem); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Returns true iff this path denotes an existing symbolic link. Does not follow symbolic links. |
| 806 | * |
| 807 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 808 | * Path. |
| 809 | */ |
| 810 | public boolean isSymbolicLink(FileSystem fileSystem) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 811 | return fileSystem.isSymbolicLink(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Returns the last segment of this path, or "/" for the root directory. |
| 816 | */ |
| 817 | public String getBaseName() { |
| 818 | return name; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Interprets the name of a path segment relative to the current path and |
| 823 | * returns the result. |
| 824 | * |
| 825 | * <p>This is a purely syntactic operation, i.e. it does no I/O, it does not |
| 826 | * validate the existence of any path, nor resolve symbolic links. If 'prefix' |
| 827 | * is not canonical, then a 'name' of '..' will be interpreted incorrectly. |
| 828 | * |
| 829 | * @precondition segment contains no slashes. |
| 830 | */ |
| 831 | private Path getCanonicalPath(String segment) { |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 832 | if (segment.equals(".") || segment.isEmpty()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 833 | return this; // that's a noop |
| 834 | } else if (segment.equals("..")) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 835 | // top-level directory's parent is root, when canonicalising: |
| 836 | return isTopLevelDirectory() ? this : parent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 837 | } else { |
| 838 | return getCachedChildPath(segment); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Returns the path formed by appending the single non-special segment |
| 844 | * "baseName" to this path. |
| 845 | * |
| 846 | * <p>You should almost always use {@link #getRelative} instead, which has |
| 847 | * the same performance characteristics if the given name is a valid base |
| 848 | * name, and which also works for '.', '..', and strings containing '/'. |
| 849 | * |
| 850 | * @throws IllegalArgumentException if {@code baseName} is not a valid base |
| 851 | * name according to {@link FileSystemUtils#checkBaseName} |
| 852 | */ |
| 853 | public Path getChild(String baseName) { |
| 854 | FileSystemUtils.checkBaseName(baseName); |
| 855 | return getCachedChildPath(baseName); |
| 856 | } |
| 857 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 858 | protected Path getRootForRelativePathComputation(PathFragment suffix) { |
| 859 | return suffix.isAbsolute() ? fileSystem.getRootDirectory() : this; |
| 860 | } |
| 861 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 862 | /** |
| 863 | * Returns the path formed by appending the relative or absolute path fragment |
| 864 | * {@code suffix} to this path. |
| 865 | * |
| 866 | * <p>If suffix is absolute, the current path will be ignored; otherwise, they |
| 867 | * will be combined. Up-level references ("..") cause the preceding path |
| 868 | * segment to be elided; this interpretation is only correct if the base path |
| 869 | * is canonical. |
| 870 | */ |
| 871 | public Path getRelative(PathFragment suffix) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 872 | Path result = getRootForRelativePathComputation(suffix); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 873 | for (String segment : suffix.segments()) { |
| 874 | result = result.getCanonicalPath(segment); |
| 875 | } |
| 876 | return result; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Returns the path formed by appending the relative or absolute string |
| 881 | * {@code path} to this path. |
| 882 | * |
| 883 | * <p>If the given path string is absolute, the current path will be ignored; |
| 884 | * otherwise, they will be combined. Up-level references ("..") cause the |
| 885 | * preceding path segment to be elided. |
| 886 | * |
| 887 | * <p>This is a purely syntactic operation, i.e. it does no I/O, it does not |
| 888 | * validate the existence of any path, nor resolve symbolic links. |
| 889 | */ |
| 890 | public Path getRelative(String path) { |
| 891 | // Fast path for valid base names. |
| 892 | if ((path.length() == 0) || (path.equals("."))) { |
| 893 | return this; |
| 894 | } else if (path.equals("..")) { |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 895 | return isTopLevelDirectory() ? this : parent; |
nharmata | aac1324 | 2017-04-24 17:41:23 +0200 | [diff] [blame] | 896 | } else if (PathFragment.containsSeparator(path)) { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 897 | return getRelative(PathFragment.create(path)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 898 | } else { |
| 899 | return getCachedChildPath(path); |
| 900 | } |
| 901 | } |
| 902 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 903 | protected final String[] getSegments() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 904 | String[] resultSegments = new String[depth]; |
| 905 | Path currentPath = this; |
| 906 | for (int pos = depth - 1; pos >= 0; pos--) { |
| 907 | resultSegments[pos] = currentPath.getBaseName(); |
| 908 | currentPath = currentPath.getParentDirectory(); |
| 909 | } |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 910 | return resultSegments; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 913 | /** Returns an absolute PathFragment representing this path. */ |
| 914 | public PathFragment asFragment() { |
nharmata | aac1324 | 2017-04-24 17:41:23 +0200 | [diff] [blame] | 915 | return PathFragment.createAlreadyInterned('\0', true, getSegments()); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 916 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 917 | |
| 918 | /** |
| 919 | * Returns a relative path fragment to this path, relative to {@code |
| 920 | * ancestorDirectory}. {@code ancestorDirectory} must be on the same |
| 921 | * filesystem as this path. (Currently, both this path and "ancestorDirectory" |
| 922 | * must be absolute, though this restriction could be loosened.) |
| 923 | * <p> |
| 924 | * <code>x.relativeTo(z) == y</code> implies |
| 925 | * <code>z.getRelative(y.getPathString()) == x</code>. |
| 926 | * <p> |
| 927 | * For example, <code>"/foo/bar/wiz".relativeTo("/foo")</code> returns |
| 928 | * <code>"bar/wiz"</code>. |
| 929 | * |
| 930 | * @throws IllegalArgumentException if this path is not beneath {@code |
| 931 | * ancestorDirectory} or if they are not part of the same filesystem |
| 932 | */ |
| 933 | public PathFragment relativeTo(Path ancestorPath) { |
| 934 | checkSameFilesystem(ancestorPath); |
| 935 | |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 936 | if (isMaybeRelativeTo(ancestorPath)) { |
| 937 | // Fast path: when otherPath is the ancestor of this path |
| 938 | int resultSegmentCount = depth - ancestorPath.depth; |
| 939 | if (resultSegmentCount >= 0) { |
| 940 | String[] resultSegments = new String[resultSegmentCount]; |
| 941 | Path currentPath = this; |
| 942 | for (int pos = resultSegmentCount - 1; pos >= 0; pos--) { |
| 943 | resultSegments[pos] = currentPath.getBaseName(); |
| 944 | currentPath = currentPath.getParentDirectory(); |
| 945 | } |
| 946 | if (ancestorPath.equals(currentPath)) { |
nharmata | aac1324 | 2017-04-24 17:41:23 +0200 | [diff] [blame] | 947 | return PathFragment.createAlreadyInterned('\0', false, resultSegments); |
Laszlo Csomor | ca99bb7 | 2016-10-25 13:15:55 +0000 | [diff] [blame] | 948 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | throw new IllegalArgumentException("Path " + this + " is not beneath " + ancestorPath); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Checks that "this" and "that" are paths on the same filesystem. |
| 957 | */ |
| 958 | protected void checkSameFilesystem(Path that) { |
| 959 | if (this.fileSystem != that.fileSystem) { |
| 960 | throw new IllegalArgumentException("Files are on different filesystems: " |
| 961 | + this + ", " + that); |
| 962 | } |
| 963 | } |
| 964 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 965 | /** Prefer to use {@link #getOutputStream(FileSystem)}. */ |
| 966 | @Deprecated |
| 967 | public OutputStream getOutputStream() throws IOException, FileNotFoundException { |
| 968 | return getOutputStream(fileSystem); |
| 969 | } |
| 970 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 971 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 972 | * Returns an output stream to the file denoted by the current path, creating it and truncating it |
| 973 | * if necessary. The stream is opened for writing. |
| 974 | * |
| 975 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 976 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 977 | * |
| 978 | * @throws FileNotFoundException If the file cannot be found or created. |
| 979 | * @throws IOException If a different error occurs. |
| 980 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 981 | public OutputStream getOutputStream(FileSystem fileSystem) |
| 982 | throws IOException, FileNotFoundException { |
| 983 | return getOutputStream(fileSystem, false); |
| 984 | } |
| 985 | |
| 986 | /** Prefer to use {@link #getOutputStream(FileSystem, boolean)}. */ |
| 987 | @Deprecated |
| 988 | public OutputStream getOutputStream(boolean append) throws IOException, FileNotFoundException { |
| 989 | return getOutputStream(fileSystem, append); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 993 | * Returns an output stream to the file denoted by the current path, creating it and truncating it |
| 994 | * if necessary. The stream is opened for writing. |
| 995 | * |
| 996 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 997 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 998 | * |
| 999 | * @param append whether to open the file in append mode. |
| 1000 | * @throws FileNotFoundException If the file cannot be found or created. |
| 1001 | * @throws IOException If a different error occurs. |
| 1002 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1003 | public OutputStream getOutputStream(FileSystem fileSystem, boolean append) |
| 1004 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1005 | return fileSystem.getOutputStream(this, append); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1008 | /** Prefer to use {@link #createDirectory(FileSystem)}. */ |
| 1009 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1010 | public boolean createDirectory() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1011 | return createDirectory(fileSystem); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1015 | * Creates a directory with the name of the current path, not following symbolic links. Returns |
| 1016 | * normally iff the directory exists after the call: true if the directory was created by this |
| 1017 | * call, false if the directory was already in existence. Throws an exception if the directory |
| 1018 | * could not be created for any reason. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1019 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1020 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1021 | * Path. |
| 1022 | * |
| 1023 | * @throws IOException if the directory creation failed for any reason |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1024 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1025 | public boolean createDirectory(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1026 | return fileSystem.createDirectory(this); |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
tomlu | decca2b | 2017-12-21 08:59:51 -0800 | [diff] [blame^] | 1029 | /** |
| 1030 | * Ensures that the directory with the name of the current path and all its ancestor directories |
| 1031 | * exist. |
| 1032 | * |
| 1033 | * <p>Does not return whether the directory already existed or was created by some other |
| 1034 | * concurrent call to this method. |
| 1035 | * |
| 1036 | * @throws IOException if the directory creation failed for any reason |
| 1037 | */ |
| 1038 | public void createDirectoryAndParents() throws IOException { |
| 1039 | fileSystem.createDirectoryAndParents(this); |
| 1040 | } |
| 1041 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1042 | /** Prefer to use {@link #createSymbolicLink(FileSystem, Path)}. */ |
| 1043 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1044 | public void createSymbolicLink(Path target) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1045 | createSymbolicLink(fileSystem, target); |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Creates a symbolic link with the name of the current path, following symbolic links. The |
| 1050 | * referent of the created symlink is is the absolute path "target"; it is not possible to create |
| 1051 | * relative symbolic links via this method. |
| 1052 | * |
| 1053 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1054 | * Path. |
| 1055 | * |
| 1056 | * @throws IOException if the creation of the symbolic link was unsuccessful for any reason |
| 1057 | */ |
| 1058 | public void createSymbolicLink(FileSystem fileSystem, Path target) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1059 | checkSameFilesystem(target); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1060 | fileSystem.createSymbolicLink(this, target.asFragment()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1063 | /** Prefer to use {@link #createSymbolicLink(FileSystem, PathFragment)}. */ |
| 1064 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1065 | public void createSymbolicLink(PathFragment target) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1066 | createSymbolicLink(fileSystem, target); |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * Creates a symbolic link with the name of the current path, following symbolic links. The |
| 1071 | * referent of the created symlink is is the path fragment "target", which may be absolute or |
| 1072 | * relative. |
| 1073 | * |
| 1074 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1075 | * Path. |
| 1076 | * |
| 1077 | * @throws IOException if the creation of the symbolic link was unsuccessful for any reason |
| 1078 | */ |
| 1079 | public void createSymbolicLink(FileSystem fileSystem, PathFragment target) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1080 | fileSystem.createSymbolicLink(this, target); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1081 | } |
Laszlo Csomor | a2da311 | 2016-09-07 08:06:15 +0000 | [diff] [blame] | 1082 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1083 | /** Prefer to use {@link #readSymbolicLink(FileSystem)}. */ |
| 1084 | @Deprecated |
| 1085 | public PathFragment readSymbolicLink() throws IOException { |
| 1086 | return readSymbolicLink(fileSystem); |
| 1087 | } |
| 1088 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1089 | /** |
Laszlo Csomor | a2da311 | 2016-09-07 08:06:15 +0000 | [diff] [blame] | 1090 | * Returns the target of the current path, which must be a symbolic link. The link contents are |
| 1091 | * returned exactly, and may contain an absolute or relative path. Analogous to readlink(2). |
| 1092 | * |
tomlu | 39fab10 | 2017-10-19 21:35:06 +0200 | [diff] [blame] | 1093 | * <p>Note: for {@link FileSystem}s where {@link FileSystem#supportsSymbolicLinksNatively(Path)} |
Laszlo Csomor | a2da311 | 2016-09-07 08:06:15 +0000 | [diff] [blame] | 1094 | * returns false, this method will throw an {@link UnsupportedOperationException} if the link |
| 1095 | * points to a non-existent file. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1096 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1097 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1098 | * Path. |
| 1099 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1100 | * @return the content (i.e. target) of the symbolic link |
Laszlo Csomor | a2da311 | 2016-09-07 08:06:15 +0000 | [diff] [blame] | 1101 | * @throws IOException if the current path is not a symbolic link, or the contents of the link |
| 1102 | * could not be read for any reason |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1103 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1104 | public PathFragment readSymbolicLink(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1105 | return fileSystem.readSymbolicLink(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1108 | /** Prefer to use {@link #readSymbolicLinkUnchecked(FileSystem)}. */ |
| 1109 | @Deprecated |
| 1110 | public PathFragment readSymbolicLinkUnchecked() throws IOException { |
| 1111 | return readSymbolicLinkUnchecked(fileSystem); |
| 1112 | } |
| 1113 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1114 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1115 | * If the current path is a symbolic link, returns the target of this symbolic link. The semantics |
| 1116 | * are intentionally left underspecified otherwise to permit efficient implementations. |
| 1117 | * |
| 1118 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1119 | * Path. |
Nathan Harmata | 215974e5 | 2015-09-16 21:31:49 +0000 | [diff] [blame] | 1120 | * |
| 1121 | * @return the content (i.e. target) of the symbolic link |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1122 | * @throws IOException if the current path is not a symbolic link, or the contents of the link |
| 1123 | * could not be read for any reason |
Nathan Harmata | 215974e5 | 2015-09-16 21:31:49 +0000 | [diff] [blame] | 1124 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1125 | public PathFragment readSymbolicLinkUnchecked(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1126 | return fileSystem.readSymbolicLinkUnchecked(this); |
Nathan Harmata | 215974e5 | 2015-09-16 21:31:49 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1129 | /** Prefer to use {@link #createHardLink(FileSystem, Path)}. */ |
| 1130 | @Deprecated |
| 1131 | public void createHardLink(Path link) throws IOException { |
| 1132 | createHardLink(fileSystem, link); |
| 1133 | } |
| 1134 | |
Nathan Harmata | 215974e5 | 2015-09-16 21:31:49 +0000 | [diff] [blame] | 1135 | /** |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 1136 | * Create a hard link for the current path. |
| 1137 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1138 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1139 | * Path. |
| 1140 | * |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 1141 | * @param link the path of the new link |
| 1142 | * @throws IOException if there was an error executing {@link FileSystem#createHardLink} |
| 1143 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1144 | public void createHardLink(FileSystem fileSystem, Path link) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1145 | fileSystem.createHardLink(link, this); |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1148 | /** Prefer to use {@link #resolveSymbolicLinks(FileSystem)}. */ |
| 1149 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1150 | public Path resolveSymbolicLinks() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1151 | return resolveSymbolicLinks(fileSystem); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1155 | * Returns the canonical path for this path, by repeatedly replacing symbolic links with their |
| 1156 | * referents. Analogous to realpath(3). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1157 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1158 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1159 | * Path. |
| 1160 | * |
| 1161 | * @return the canonical path for this path |
| 1162 | * @throws IOException if any symbolic link could not be resolved, or other error occurred (for |
| 1163 | * example, the path does not exist) |
| 1164 | */ |
| 1165 | public Path resolveSymbolicLinks(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1166 | return fileSystem.resolveSymbolicLinks(this); |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /** Prefer to use {@link #renameTo(FileSystem, Path)}. */ |
| 1170 | @Deprecated |
| 1171 | public void renameTo(Path target) throws IOException { |
| 1172 | renameTo(fileSystem, target); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Renames the file denoted by the current path to the location "target", not following symbolic |
| 1177 | * links. |
| 1178 | * |
| 1179 | * <p>Files cannot be atomically renamed across devices; copying is required. Use {@link |
| 1180 | * FileSystemUtils#copyFile} followed by {@link Path#delete}. |
| 1181 | * |
| 1182 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1183 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1184 | * |
| 1185 | * @throws IOException if the rename failed for any reason |
| 1186 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1187 | public void renameTo(FileSystem fileSystem, Path target) throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1188 | checkSameFilesystem(target); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1189 | fileSystem.renameTo(this, target); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1192 | /** Prefer to use {@link #getFileSize(FileSystem)}. */ |
| 1193 | @Deprecated |
| 1194 | public long getFileSize() throws IOException, FileNotFoundException { |
| 1195 | return getFileSize(fileSystem); |
| 1196 | } |
| 1197 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1198 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1199 | * Returns the size in bytes of the file denoted by the current path, following symbolic links. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1200 | * |
Nathan Harmata | d8b6ff2 | 2015-10-20 21:54:34 +0000 | [diff] [blame] | 1201 | * <p>The size of a directory or special file is undefined and should not be used. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1202 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1203 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1204 | * Path. |
| 1205 | * |
| 1206 | * @throws FileNotFoundException if the file denoted by the current path does not exist |
| 1207 | * @throws IOException if the file's metadata could not be read, or some other error occurred |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1208 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1209 | public long getFileSize(FileSystem fileSystem) throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1210 | return fileSystem.getFileSize(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1213 | /** Prefer to use {@link #getFileSize(FileSystem, Symlinks)}. */ |
| 1214 | @Deprecated |
| 1215 | public long getFileSize(Symlinks followSymlinks) throws IOException, FileNotFoundException { |
| 1216 | return getFileSize(fileSystem, followSymlinks); |
| 1217 | } |
| 1218 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1219 | /** |
| 1220 | * Returns the size in bytes of the file denoted by the current path. |
| 1221 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1222 | * <p>The size of directory or special file is undefined. The size of a symbolic link is the |
| 1223 | * length of the name of its referent. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1224 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1225 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1226 | * Path. |
| 1227 | * |
| 1228 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 1229 | * link is deferenced until a file other than a symbol link is found |
| 1230 | * @throws FileNotFoundException if the file denoted by the current path does not exist |
| 1231 | * @throws IOException if the file's metadata could not be read, or some other error occurred |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1232 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1233 | public long getFileSize(FileSystem fileSystem, Symlinks followSymlinks) |
| 1234 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1235 | return fileSystem.getFileSize(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1238 | /** Prefer to use {@link #delete(FileSystem)}. */ |
| 1239 | @Deprecated |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1240 | public boolean delete() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1241 | return delete(fileSystem); |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1244 | /** |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1245 | * Deletes the file denoted by this path, not following symbolic links. Returns normally iff the |
| 1246 | * file doesn't exist after the call: true if this call deleted the file, false if the file |
| 1247 | * already didn't exist. Throws an exception if the file could not be deleted for any reason. |
| 1248 | * |
| 1249 | * <p>This is a migration method. The method (and its FileSystem-less counterpart) will be deleted |
| 1250 | * once the FileSystem instance is removed from Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1251 | * |
| 1252 | * @return true iff the file was actually deleted by this call |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1253 | * @throws IOException if the deletion failed but the file was present prior to the call |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1254 | */ |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1255 | public boolean delete(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1256 | return fileSystem.delete(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1259 | /** Prefer to use {@link #getLastModifiedTime(FileSystem)}. */ |
| 1260 | @Deprecated |
| 1261 | public long getLastModifiedTime() throws IOException { |
| 1262 | return getLastModifiedTime(fileSystem); |
| 1263 | } |
| 1264 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1265 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1266 | * Returns the last modification time of the file, in milliseconds since the UNIX epoch, of the |
| 1267 | * file denoted by the current path, following symbolic links. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1268 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1269 | * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond |
| 1270 | * precision. |
| 1271 | * |
| 1272 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1273 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1274 | * |
| 1275 | * @throws IOException if the operation failed for any reason |
| 1276 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1277 | public long getLastModifiedTime(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1278 | return fileSystem.getLastModifiedTime(this, true); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1281 | /** Prefer to use {@link #getLastModifiedTime(FileSystem, Symlinks)}. */ |
| 1282 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1283 | public long getLastModifiedTime(Symlinks followSymlinks) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1284 | return getLastModifiedTime(fileSystem, followSymlinks); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * Returns the last modification time of the file, in milliseconds since the UNIX epoch, of the |
| 1289 | * file denoted by the current path. |
| 1290 | * |
| 1291 | * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond |
| 1292 | * precision. |
| 1293 | * |
| 1294 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1295 | * Path. |
| 1296 | * |
| 1297 | * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the |
| 1298 | * link is dereferenced until a file other than a symbolic link is found |
| 1299 | * @throws IOException if the modification time for the file could not be obtained for any reason |
| 1300 | */ |
| 1301 | public long getLastModifiedTime(FileSystem fileSystem, Symlinks followSymlinks) |
| 1302 | throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1303 | return fileSystem.getLastModifiedTime(this, followSymlinks.toBoolean()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1306 | /** Prefer to use {@link #setLastModifiedTime(FileSystem, long)}. */ |
| 1307 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1308 | public void setLastModifiedTime(long newTime) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1309 | setLastModifiedTime(fileSystem, newTime); |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * Sets the modification time of the file denoted by the current path. Follows symbolic links. If |
| 1314 | * newTime is -1, the current time according to the kernel is used; this may differ from the JVM's |
| 1315 | * clock. |
| 1316 | * |
| 1317 | * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond |
| 1318 | * precision. |
| 1319 | * |
| 1320 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1321 | * Path. |
| 1322 | * |
| 1323 | * @param newTime time, in milliseconds since the UNIX epoch, or -1L, meaning use the kernel's |
| 1324 | * current time |
| 1325 | * @throws IOException if the modification time for the file could not be set for any reason |
| 1326 | */ |
| 1327 | public void setLastModifiedTime(FileSystem fileSystem, long newTime) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1328 | fileSystem.setLastModifiedTime(this, newTime); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1331 | /** Prefer to use {@link #getxattr(FileSystem, String)}. */ |
| 1332 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1333 | public byte[] getxattr(String name) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1334 | return getxattr(fileSystem, name); |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * Returns value of the given extended attribute name or null if attribute does not exist or file |
| 1339 | * system does not support extended attributes. Follows symlinks. |
| 1340 | * |
| 1341 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1342 | * Path. |
| 1343 | */ |
| 1344 | public byte[] getxattr(FileSystem fileSystem, String name) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1345 | return fileSystem.getxattr(this, name); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1348 | /** Prefer to use {@link #getFastDigest(FileSystem)}. */ |
| 1349 | @Deprecated |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1350 | public byte[] getFastDigest() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1351 | return getFastDigest(fileSystem); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1355 | * Gets a fast digest for the given path, or {@code null} if there isn't one available. The digest |
| 1356 | * should be suitable for detecting changes to the file. |
| 1357 | * |
| 1358 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1359 | * Path. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1360 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1361 | public byte[] getFastDigest(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1362 | return fileSystem.getFastDigest(this); |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /** Prefer to use {@link #isValidDigest(FileSystem, byte[])}. */ |
| 1366 | @Deprecated |
| 1367 | public boolean isValidDigest(byte[] digest) { |
| 1368 | return isValidDigest(fileSystem, digest); |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * Returns whether the given digest is a valid digest for the default system digest function. |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1373 | * |
| 1374 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1375 | * Path. |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1376 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1377 | public boolean isValidDigest(FileSystem fileSystem, byte[] digest) { |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1378 | return fileSystem.isValidDigest(digest); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1381 | /** Prefer to use {@link #getDigest(FileSystem)}. */ |
| 1382 | @Deprecated |
| 1383 | public byte[] getDigest() throws IOException { |
| 1384 | return getDigest(fileSystem); |
| 1385 | } |
| 1386 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1387 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1388 | * Returns the digest of the file denoted by the current path, following symbolic links. |
| 1389 | * |
| 1390 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1391 | * Path. |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1392 | * |
| 1393 | * @return a new byte array containing the file's digest |
| 1394 | * @throws IOException if the digest could not be computed for any reason |
| 1395 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1396 | public byte[] getDigest(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1397 | return fileSystem.getDigest(this); |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1400 | /** Prefer to use {@link #getDigest(FileSystem, HashFunction)}. */ |
| 1401 | @Deprecated |
| 1402 | public byte[] getDigest(HashFunction hashFunction) throws IOException { |
| 1403 | return getDigest(fileSystem, hashFunction); |
| 1404 | } |
| 1405 | |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1406 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1407 | * Returns the digest of the file denoted by the current path and digest function, following |
| 1408 | * symbolic links. |
| 1409 | * |
| 1410 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1411 | * Path. |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1412 | * |
| 1413 | * @return a new byte array containing the file's digest |
| 1414 | * @throws IOException if the digest could not be computed for any reason |
| 1415 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1416 | public byte[] getDigest(FileSystem fileSystem, HashFunction hashFunction) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1417 | return fileSystem.getDigest(this, hashFunction); |
Ola Rozenfeld | 39dbc98 | 2016-11-17 20:14:56 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1420 | /** Prefer to use {@link #getInputStream(FileSystem)}. */ |
| 1421 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1422 | public InputStream getInputStream() throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1423 | return getInputStream(fileSystem); |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * Opens the file denoted by this path, following symbolic links, for reading, and returns an |
| 1428 | * input stream to it. |
| 1429 | * |
| 1430 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1431 | * Path. |
| 1432 | * |
| 1433 | * @throws IOException if the file was not found or could not be opened for reading |
| 1434 | */ |
| 1435 | public InputStream getInputStream(FileSystem fileSystem) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1436 | return fileSystem.getInputStream(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * Returns a java.io.File representation of this path. |
| 1441 | * |
| 1442 | * <p>Caveat: the result may be useless if this path's getFileSystem() is not |
| 1443 | * the UNIX filesystem. |
| 1444 | */ |
| 1445 | public File getPathFile() { |
| 1446 | return new File(getPathString()); |
| 1447 | } |
| 1448 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1449 | /** Prefer to use {@link #isWritable(FileSystem)}. */ |
| 1450 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1451 | public boolean isWritable() throws IOException, FileNotFoundException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1452 | return isWritable(fileSystem); |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * Returns true if the file denoted by the current path, following symbolic links, is writable for |
| 1457 | * the current user. |
| 1458 | * |
| 1459 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1460 | * Path. |
| 1461 | * |
| 1462 | * @throws FileNotFoundException if the file does not exist, a dangling symbolic link was |
| 1463 | * encountered, or the file's metadata could not be read |
| 1464 | */ |
| 1465 | public boolean isWritable(FileSystem fileSystem) throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1466 | return fileSystem.isWritable(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1469 | /** Prefer to use {@link #setReadable(FileSystem, boolean)}. */ |
| 1470 | @Deprecated |
| 1471 | public void setReadable(boolean readable) throws IOException, FileNotFoundException { |
| 1472 | setReadable(fileSystem, readable); |
| 1473 | } |
| 1474 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1475 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1476 | * Sets the read permissions of the file denoted by the current path, following symbolic links. |
| 1477 | * Permissions apply to the current user. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1478 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1479 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1480 | * Path. |
| 1481 | * |
| 1482 | * @param readable if true, the file is set to readable; otherwise the file is made non-readable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1483 | * @throws FileNotFoundException if the file does not exist |
| 1484 | * @throws IOException If the action cannot be taken (ie. permissions) |
| 1485 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1486 | public void setReadable(FileSystem fileSystem, boolean readable) |
| 1487 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1488 | fileSystem.setReadable(this, readable); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1491 | /** Prefer to use {@link #setWritable(FileSystem, boolean)}. */ |
| 1492 | @Deprecated |
| 1493 | public void setWritable(boolean writable) throws IOException, FileNotFoundException { |
| 1494 | setWritable(fileSystem, writable); |
| 1495 | } |
| 1496 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1497 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1498 | * Sets the write permissions of the file denoted by the current path, following symbolic links. |
| 1499 | * Permissions apply to the current user. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1500 | * |
| 1501 | * <p>TODO(bazel-team): (2009) what about owner/group/others? |
| 1502 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1503 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1504 | * Path. |
| 1505 | * |
| 1506 | * @param writable if true, the file is set to writable; otherwise the file is made non-writable |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1507 | * @throws FileNotFoundException if the file does not exist |
| 1508 | * @throws IOException If the action cannot be taken (ie. permissions) |
| 1509 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1510 | public void setWritable(FileSystem fileSystem, boolean writable) |
| 1511 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1512 | fileSystem.setWritable(this, writable); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1515 | /** Prefer to use {@link #isExecutable(FileSystem)}. */ |
| 1516 | @Deprecated |
| 1517 | public boolean isExecutable() throws IOException, FileNotFoundException { |
| 1518 | return isExecutable(fileSystem); |
| 1519 | } |
| 1520 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1521 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1522 | * Returns true iff the file specified by the current path, following symbolic links, is |
| 1523 | * executable by the current user. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1524 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1525 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1526 | * Path. |
| 1527 | * |
| 1528 | * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was |
| 1529 | * encountered |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1530 | * @throws IOException if some other I/O error occurred |
| 1531 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1532 | public boolean isExecutable(FileSystem fileSystem) throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1533 | return fileSystem.isExecutable(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1534 | } |
| 1535 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1536 | /** Prefer to use {@link #isReadable(FileSystem)}. */ |
| 1537 | @Deprecated |
| 1538 | public boolean isReadable() throws IOException, FileNotFoundException { |
| 1539 | return isReadable(fileSystem); |
| 1540 | } |
| 1541 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1542 | /** |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1543 | * Returns true iff the file specified by the current path, following symbolic links, is readable |
| 1544 | * by the current user. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1545 | * |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1546 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1547 | * Path. |
| 1548 | * |
| 1549 | * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was |
| 1550 | * encountered |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1551 | * @throws IOException if some other I/O error occurred |
| 1552 | */ |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1553 | public boolean isReadable(FileSystem fileSystem) throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1554 | return fileSystem.isReadable(this); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1555 | } |
| 1556 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1557 | /** Prefer to use {@link #setExecutable(FileSystem, boolean)}. */ |
| 1558 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1559 | public void setExecutable(boolean executable) throws IOException, FileNotFoundException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1560 | setExecutable(fileSystem, executable); |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Sets the execute permission on the file specified by the current path, following symbolic |
| 1565 | * links. Permissions apply to the current user. |
| 1566 | * |
| 1567 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1568 | * Path. |
| 1569 | * |
| 1570 | * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was |
| 1571 | * encountered |
| 1572 | * @throws IOException if the metadata change failed, for example because of permissions |
| 1573 | */ |
| 1574 | public void setExecutable(FileSystem fileSystem, boolean executable) |
| 1575 | throws IOException, FileNotFoundException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1576 | fileSystem.setExecutable(this, executable); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1579 | /** Prefer to use {@link #chmod(FileSystem, int)}. */ |
| 1580 | @Deprecated |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1581 | public void chmod(int mode) throws IOException { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1582 | chmod(fileSystem, mode); |
| 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * Sets the permissions on the file specified by the current path, following symbolic links. If |
| 1587 | * permission changes on this path's {@link FileSystem} are slow (e.g. one syscall per change), |
| 1588 | * this method should aim to be faster than setting each permission individually. If this path's |
| 1589 | * {@link FileSystem} does not support group and others permissions, those bits will be ignored. |
| 1590 | * |
| 1591 | * <p>This is a migration method. It will be deleted once the file system instance is deleted from |
| 1592 | * Path. |
| 1593 | * |
| 1594 | * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was |
| 1595 | * encountered |
| 1596 | * @throws IOException if the metadata change failed, for example because of permissions |
| 1597 | */ |
| 1598 | public void chmod(FileSystem fileSystem, int mode) throws IOException { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1599 | fileSystem.chmod(this, mode); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1600 | } |
| 1601 | |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1602 | /** Prefer to use {@link #prefetchPackageAsync(FileSystem, int)}. */ |
| 1603 | @Deprecated |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1604 | public void prefetchPackageAsync(int maxDirs) { |
tomlu | b1e8daf | 2017-10-27 15:58:21 -0400 | [diff] [blame] | 1605 | prefetchPackageAsync(fileSystem, maxDirs); |
| 1606 | } |
| 1607 | |
| 1608 | public void prefetchPackageAsync(FileSystem fileSystem, int maxDirs) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1609 | fileSystem.prefetchPackageAsync(this, maxDirs); |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1612 | /** |
| 1613 | * Compare Paths of the same file system using their PathFragments. |
| 1614 | * |
| 1615 | * <p>Paths from different filesystems will be compared using the identity |
| 1616 | * hash code of their respective filesystems. |
| 1617 | */ |
| 1618 | @Override |
| 1619 | public int compareTo(Path o) { |
| 1620 | // Fast-path. |
| 1621 | if (equals(o)) { |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | // If they are on different file systems, the file system decides the ordering. |
| 1626 | FileSystem otherFs = o.getFileSystem(); |
| 1627 | if (!fileSystem.equals(otherFs)) { |
| 1628 | int thisFileSystemHash = System.identityHashCode(fileSystem); |
| 1629 | int otherFileSystemHash = System.identityHashCode(otherFs); |
| 1630 | if (thisFileSystemHash < otherFileSystemHash) { |
| 1631 | return -1; |
| 1632 | } else if (thisFileSystemHash > otherFileSystemHash) { |
| 1633 | return 1; |
| 1634 | } else { |
| 1635 | // TODO(bazel-team): Add a name to every file system to be used here. |
| 1636 | return 0; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | // Equal file system, but different paths, because of the canonicalization. |
| 1641 | // We expect to often compare Paths that are very similar, for example for files in the same |
| 1642 | // directory. This can be done efficiently by going up segment by segment until we get the |
| 1643 | // identical path (canonicalization again), and then just compare the immediate child segments. |
| 1644 | // Overall this is much faster than creating PathFragment instances, and comparing those, which |
| 1645 | // requires us to always go up to the top-level directory and copy all segments into a new |
| 1646 | // string array. |
| 1647 | // This was previously showing up as a hotspot in a profile of globbing a large directory. |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 1648 | Path a = this; |
| 1649 | Path b = o; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1650 | int maxDepth = Math.min(a.depth, b.depth); |
| 1651 | while (a.depth > maxDepth) { |
| 1652 | a = a.getParentDirectory(); |
| 1653 | } |
| 1654 | while (b.depth > maxDepth) { |
| 1655 | b = b.getParentDirectory(); |
| 1656 | } |
| 1657 | // One is the child of the other. |
| 1658 | if (a.equals(b)) { |
| 1659 | // If a is the same as this, this.depth must be less than o.depth. |
| 1660 | return equals(a) ? -1 : 1; |
| 1661 | } |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 1662 | Path previousa; |
| 1663 | Path previousb; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1664 | do { |
| 1665 | previousa = a; |
| 1666 | previousb = b; |
| 1667 | a = a.getParentDirectory(); |
| 1668 | b = b.getParentDirectory(); |
Nathan Harmata | 3c74af0 | 2015-10-09 13:16:08 +0000 | [diff] [blame] | 1669 | } while (!a.equals(b)); // This has to happen eventually. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1670 | return previousa.name.compareTo(previousb.name); |
| 1671 | } |
| 1672 | } |