aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/Path.java
blob: 52b8af3f50cf7078efd60e3c177747dc6e2fa95a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.vfs;

import com.google.common.base.Predicate;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.util.StringCanonicalizer;
import com.google.devtools.build.lib.vfs.FileSystem.HashFunction;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Objects;

/**
 * <p>Instances of this class represent pathnames, forming a tree
 * structure to implement sharing of common prefixes (parent directory names).
 * A node in these trees is something like foo, bar, .., ., or /. If the
 * instance is not a root path, it will have a parent path. A path can also
 * have children, which are indexed by name in a map.
 *
 * <p>There is some limited support for Windows-style paths. Most importantly, drive identifiers
 * in front of a path (c:/abc) are supported. However, Windows-style backslash separators
 * (C:\\foo\\bar) and drive-relative paths ("C:foo") are explicitly not supported, same with
 * advanced features like \\\\network\\paths and \\\\?\\unc\\paths.
 *
 * <p>{@link FileSystem} implementations maintain pointers into this graph.
 */
@ThreadSafe
public class Path implements Comparable<Path>, Serializable {

  /** Filesystem-specific factory for {@link Path} objects. */
  public static interface PathFactory {
    /**
     * Creates the root of all paths used by a filesystem.
     *
     * <p>All other paths are instantiated via {@link Path#createChildPath(String)} which calls
     * {@link #createChildPath(Path, String)}.
     *
     * <p>Beware: this is called during the FileSystem constructor which may occur before subclasses
     * are completely initialized.
     */
    Path createRootPath(FileSystem filesystem);

    /**
     * Create a child path of the given parent.
     *
     * <p>All {@link Path} objects are instantiated via this method, with the sole exception of the
     * filesystem root, which is created by {@link #createRootPath(FileSystem)}.
     */
    Path createChildPath(Path parent, String childName);

    /**
     * Makes the proper invocation of {@link FileSystem#getCachedChildPathInternal}, doing
     * filesystem-specific logic if necessary.
     *
     * <p>On Unix filesystems this method merely calls through to {@code
     * FileSystem.getCachedChildPathInternal(parent, child)}, but on Windows this can be used to
     * handle the translatation of absolute Unix paths to absolute Windows paths, e.g. "/c" to "C:/"
     * or "/usr" to "C:/tools/msys64/usr".
     */
    Path getCachedChildPathInternal(Path path, String childName);
  }

  private static FileSystem fileSystemForSerialization;

  /**
   * We need to specify used FileSystem. In this case we can save memory during the serialization.
   */
  public static void setFileSystemForSerialization(FileSystem fileSystem) {
    fileSystemForSerialization = fileSystem;
  }

  /**
   * Returns FileSystem that we are using.
   */
  public static FileSystem getFileSystemForSerialization() {
    return fileSystemForSerialization;
  }

  // These are basically final, but can't be marked as such in order to support serialization.
  private FileSystem fileSystem;
  private String name;
  private Path parent;
  private int depth;
  private int hashCode;

  private static final ReferenceQueue<Path> REFERENCE_QUEUE = new ReferenceQueue<>();

  private static class PathWeakReferenceForCleanup extends WeakReference<Path> {
    final Path parent;
    final String baseName;

    PathWeakReferenceForCleanup(Path referent, ReferenceQueue<Path> referenceQueue) {
      super(referent, referenceQueue);
      parent = referent.getParentDirectory();
      baseName = referent.getBaseName();
    }
  }

  private static final Thread PATH_CHILD_CACHE_CLEANUP_THREAD = new Thread("Path cache cleanup") {
    @Override
    public void run() {
      while (true) {
        try {
          PathWeakReferenceForCleanup ref = (PathWeakReferenceForCleanup) REFERENCE_QUEUE.remove();
          Path parent = ref.parent;
          synchronized (parent) {
            // It's possible that since this reference was enqueued for deletion, the Path was
            // recreated with a new entry in the map. We definitely shouldn't delete that entry, so
            // check to make sure they're the same.
            Reference<Path> currentRef = ref.parent.children.get(ref.baseName);
            if (currentRef == ref) {
              ref.parent.children.remove(ref.baseName);
            }
          }
        } catch (InterruptedException e) {
          // Ignored.
        }
      }
    }
  };

  static {
    PATH_CHILD_CACHE_CLEANUP_THREAD.setDaemon(true);
    PATH_CHILD_CACHE_CLEANUP_THREAD.start();
  }

  /**
   * A mapping from a child file name to the {@link Path} representing it.
   *
   * <p>File names must be a single path segment.  The strings must be
   * canonical.  We use IdentityHashMap instead of HashMap for reasons of space
   * efficiency: instances are smaller by a single word.  Also, since all path
   * segments are interned, the universe of Paths holds a minimal number of
   * references to strings.  (It's doubtful that there's any time gain from use
   * of an IdentityHashMap, since the time saved by avoiding string equality
   * tests during hash lookups is probably equal to the time spent eagerly
   * interning strings, unless the collision rate is high.)
   *
   * <p>The Paths are stored as weak references to ensure that a live
   * Path for a directory does not hold a strong reference to all of its
   * descendants, which would prevent collection of paths we never intend to
   * use again.  Stale references in the map must be treated as absent.
   *
   * <p>A Path may be recycled once there is no Path that refers to it or
   * to one of its descendants.  This means that any data stored in the
   * Path instance by one of its subclasses must be recoverable: it's ok to
   * store data in Paths as an optimization, but there must be another
   * source for that data in case the Path is recycled.
   *
   * <p>We intentionally avoid using the existing library classes for reasons of
   * space efficiency: while ConcurrentHashMap would reduce our locking
   * overhead, and ReferenceMap would simplify the code a little, both of those
   * classes have much higher per-instance overheads than IdentityHashMap.
   *
   * <p>The Path object must be synchronized while children is being
   * accessed.
   */
  private volatile IdentityHashMap<String, Reference<Path>> children;

  /**
   * Create a path instance.
   *
   * <p>Should only be called by {@link PathFactory#createChildPath(Path, String)}.
   *
   * @param name the name of this path; it must be canonicalized with {@link
   *     StringCanonicalizer#intern}
   * @param parent this path's parent
   */
  protected Path(FileSystem fileSystem, String name, Path parent) {
    this.fileSystem = fileSystem;
    this.name = name;
    this.parent = parent;
    this.depth = parent == null ? 0 : parent.depth + 1;

    // No need to include the drive letter in the hash code, because it's derived from the parent
    // and/or the name.
    if (fileSystem == null || fileSystem.isFilePathCaseSensitive()) {
      this.hashCode = Objects.hash(parent, name);
    } else {
      this.hashCode = Objects.hash(parent, name.toLowerCase());
    }
  }

  /**
   * Create the root path.
   *
   * <p>Should only be called by {@link PathFactory#createRootPath(FileSystem)}.
   */
  protected Path(FileSystem fileSystem) {
    this(fileSystem, StringCanonicalizer.intern("/"), null);
  }

  private void writeObject(ObjectOutputStream out) throws IOException {
    Preconditions.checkState(
        fileSystem == fileSystemForSerialization, "%s %s", fileSystem, fileSystemForSerialization);
    out.writeUTF(getPathString());
  }

  private void readObject(ObjectInputStream in) throws IOException {
    fileSystem = fileSystemForSerialization;
    String p = in.readUTF();
    PathFragment pf = PathFragment.create(p);
    PathFragment parentDir = pf.getParentDirectory();
    if (parentDir == null) {
      this.name = "/";
      this.parent = null;
      this.depth = 0;
    } else {
      this.name = pf.getBaseName();
      this.parent = fileSystem.getPath(parentDir);
      this.depth = this.parent.depth + 1;
    }
    this.hashCode = Objects.hash(parent, name);
    reinitializeAfterDeserialization();
  }

  /**
   * Returns the filesystem instance to which this path belongs.
   */
  public FileSystem getFileSystem() {
    return fileSystem;
  }

  public boolean isRootDirectory() {
    return parent == null;
  }

  protected Path createChildPath(String childName) {
    return fileSystem.getPathFactory().createChildPath(this, childName);
  }

  /**
   * Reinitializes this object after deserialization.
   *
   * <p>Derived classes should use this hook to initialize additional state.
   */
  protected void reinitializeAfterDeserialization() {}

  /**
   * Returns true if {@code ancestorPath} may be an ancestor of {@code path}.
   *
   * <p>The return value may be a false positive, but it cannot be a false negative. This means that
   * a true return value doesn't mean the ancestor candidate is really an ancestor, however a false
   * return value means it's guaranteed that {@code ancestorCandidate} is not an ancestor of this
   * path.
   *
   * <p>Subclasses may override this method with filesystem-specific logic, e.g. a Windows
   * filesystem may return false if the ancestor path is on a different drive than this one, because
   * it is then guaranteed that the ancestor candidate cannot be an ancestor of this path.
   *
   * @param ancestorCandidate the path that may or may not be an ancestor of this one
   */
  protected boolean isMaybeRelativeTo(Path ancestorCandidate) {
    return true;
  }

  /**
   * Returns true if this directory is top-level, i.e. it is its own parent.
   *
   * <p>When canonicalizing paths the ".." segment of a top-level directory always resolves to the
   * directory itself.
   *
   * <p>On Unix, a top-level directory would be just the filesystem root ("/), on Windows it would
   * be the filesystem root and the volume roots.
   */
  protected boolean isTopLevelDirectory() {
    return isRootDirectory();
  }

  /**
   * Returns the child path named name, or creates such a path (and caches it)
   * if it doesn't already exist.
   */
  private Path getCachedChildPath(String childName) {
    return fileSystem.getPathFactory().getCachedChildPathInternal(this, childName);
  }

  /**
   * Internal method only intended to be called by {@link PathFactory#getCachedChildPathInternal}.
   */
  public static Path getCachedChildPathInternal(Path parent, String childName, boolean cacheable) {
    // We get a canonical instance since 'children' is an IdentityHashMap.
    childName = StringCanonicalizer.intern(childName);
    if (!cacheable) {
      // Non-cacheable children won't show up in `children` so applyToChildren won't run for these.
      return parent.createChildPath(childName);
    }

    synchronized (parent) {
      if (parent.children == null) {
        // 66% of Paths have size == 1, 80% <= 2
        parent.children = new IdentityHashMap<>(1);
      }
      Reference<Path> childRef = parent.children.get(childName);
      Path child;
      if (childRef == null || (child = childRef.get()) == null) {
        child = parent.createChildPath(childName);
        parent.children.put(childName, new PathWeakReferenceForCleanup(child, REFERENCE_QUEUE));
      }
      return child;
    }
  }

  /**
   * Applies the specified function to each {@link Path} that is an existing direct
   * descendant of this one.  The Predicate is evaluated only for its
   * side-effects.
   *
   * <p>This function exists to hide the "children" field, whose complex
   * synchronization and identity requirements are too unsafe to be exposed to
   * subclasses.  For example, the "children" field must be synchronized for
   * the duration of any iteration over it; it may be null; and references
   * within it may be stale, and must be ignored.
   */
  protected synchronized void applyToChildren(Predicate<Path> function) {
    if (children != null) {
      for (Reference<Path> childRef : children.values()) {
        Path child = childRef.get();
        if (child != null) {
          function.apply(child);
        }
      }
    }
  }

  /**
   * Returns whether this path is recursively "under" {@code prefix} - that is,
   * whether {@code path} is a prefix of this path.
   *
   * <p>This method returns {@code true} when called with this path itself. This
   * method acts independently of the existence of files or folders.
   *
   * @param prefix a path which may or may not be a prefix of this path
   */
  public boolean startsWith(Path prefix) {
    Path n = this;
    for (int i = 0, len = depth - prefix.depth; i < len; i++) {
      n = n.getParentDirectory();
    }
    return prefix.equals(n);
  }

  /**
   * Computes a string representation of this path, and writes it to the given string builder. Only
   * called locally with a new instance.
   */
  protected void buildPathString(StringBuilder result) {
    if (isRootDirectory()) {
      result.append(PathFragment.ROOT_DIR);
    } else {
      parent.buildPathString(result);
      if (!parent.isRootDirectory()) {
        result.append(PathFragment.SEPARATOR_CHAR);
      }
      result.append(name);
    }
  }

  /**
   * Returns the path encoded as an {@link URI}.
   *
   * <p>This concrete implementation returns URIs with "file" as the scheme.
   * For Example:
   *  - On Unix the path "/tmp/foo bar.txt" will be encoded as
   *    "file:///tmp/foo%20bar.txt".
   *  - On Windows the path "C:\Temp\Foo Bar.txt" will be encoded as
   *    "file:///C:/Temp/Foo%20Bar.txt"
   *
   * <p>Implementors extending this class for special filesystems will likely need to override
   * this method.
   *
   * @throws URISyntaxException if the URI cannot be constructed.
   */
  public URI toURI() {
    String ps = getPathString();
    if (!ps.startsWith("/")) {
      // On Windows URI's need to start with a '/'. i.e. C:\Foo\Bar would be file:///C:/Foo/Bar
      ps = "/" + ps;
    }
    try {
      return new URI("file",
          // Needs to be "" instead of null, so that toString() will append "//" after the scheme.
          // We need this for backwards compatibility reasons as some consumers of the BEP are
          // broken.
          "",
          ps, null, null);
    } catch (URISyntaxException e) {
      throw new IllegalStateException(e);
    }
  }

  /**
   * Returns the path as a string.
   */
  public String getPathString() {
    // Profile driven optimization:
    // Preallocate a size determined by the depth, so that
    // we do not have to expand the capacity of the StringBuilder
    StringBuilder builder = new StringBuilder(depth * 20);
    buildPathString(builder);
    return builder.toString();
  }

  /**
   * Returns the path as a string.
   */
  @Override
  public String toString() {
    return getPathString();
  }

  @Override
  public int hashCode() {
    return hashCode;
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof Path)) {
      return false;
    }

    Path otherPath = (Path) other;
    if (hashCode != otherPath.hashCode) {
      return false;
    }

    if (!fileSystem.equals(otherPath.fileSystem)) {
      return false;
    }

    if (fileSystem.isFilePathCaseSensitive()) {
      return name.equals(otherPath.name)
          && Objects.equals(parent, otherPath.parent);
    } else {
      return name.toLowerCase().equals(otherPath.name.toLowerCase())
          && Objects.equals(parent, otherPath.parent);
    }
  }

  /**
   * Returns a string of debugging information associated with this path.
   * The results are unspecified and MUST NOT be interpreted programmatically.
   */
  protected String toDebugString() {
    return "";
  }

  /**
   * Returns a path representing the parent directory of this path,
   * or null iff this Path represents the root of the filesystem.
   *
   * <p>Note: This method normalises ".."  and "." path segments by string
   * processing, not by directory lookups.
   */
  public Path getParentDirectory() {
    return parent;
  }

  /** Prefer to use {@link #exists(FileSystem)}. */
  @Deprecated
  public boolean exists() {
    return exists(fileSystem);
  }

  /**
   * Returns true iff this path denotes an existing file of any kind. Follows symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public boolean exists(FileSystem fileSystem) {
    return fileSystem.exists(this, true);
  }

  /** Prefer to use {@link #exists(FileSystem, Symlinks)}. */
  @Deprecated
  public boolean exists(Symlinks followSymlinks) {
    return exists(fileSystem, followSymlinks);
  }

  /**
   * Returns true iff this path denotes an existing file of any kind.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found
   */
  public boolean exists(FileSystem fileSystem, Symlinks followSymlinks) {
    return fileSystem.exists(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #getDirectoryEntries(FileSystem)}. */
  @Deprecated
  public Collection<Path> getDirectoryEntries() throws IOException, FileNotFoundException {
    return getDirectoryEntries(fileSystem);
  }

  /**
   * Returns a new, immutable collection containing the names of all entities within the directory
   * denoted by the current path. Follows symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException If the directory is not found
   * @throws IOException If the path does not denote a directory
   */
  public Collection<Path> getDirectoryEntries(FileSystem fileSystem)
      throws IOException, FileNotFoundException {
    Collection<String> entries = fileSystem.getDirectoryEntries(this);
    Collection<Path> result = new ArrayList<>(entries.size());
    for (String entry : entries) {
      result.add(getChild(entry));
    }
    return result;
  }

  /** Prefer to use {@link #readdir(FileSystem, Symlinks)}. */
  @Deprecated
  public Collection<Dirent> readdir(Symlinks followSymlinks) throws IOException {
    return readdir(fileSystem, followSymlinks);
  }

  /**
   * Returns a collection of the names and types of all entries within the directory denoted by the
   * current path. Follows symbolic links if {@code followSymlinks} is true. Note that the order of
   * the returned entries is not guaranteed.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks whether to follow symlinks or not
   * @throws FileNotFoundException If the directory is not found
   * @throws IOException If the path does not denote a directory
   */
  public Collection<Dirent> readdir(FileSystem fileSystem, Symlinks followSymlinks)
      throws IOException {
    return fileSystem.readdir(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #stat(FileSystem)}. */
  @Deprecated
  public FileStatus stat() throws IOException {
    return stat(fileSystem);
  }

  /**
   * Returns the status of a file, following symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if there was an error obtaining the file status. Note, some implementations
   *     may defer the I/O, and hence the throwing of the exception, until the accessor methods of
   *     {@code FileStatus} are called.
   */
  public FileStatus stat(FileSystem fileSystem) throws IOException {
    return fileSystem.stat(this, true);
  }

  /** Prefer to use {@link #statNullable(FileSystem)}. */
  @Deprecated
  public FileStatus statNullable() {
    return statNullable(fileSystem);
  }

  /**
   * Like stat(), but returns null on file-nonexistence instead of throwing.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public FileStatus statNullable(FileSystem fileSystem) {
    return statNullable(fileSystem, Symlinks.FOLLOW);
  }

  /** Prefer to use {@link #statNullable(FileSystem, Symlinks)}. */
  @Deprecated
  public FileStatus statNullable(Symlinks symlinks) {
    return statNullable(fileSystem, symlinks);
  }

  /**
   * Like stat(), but returns null on file-nonexistence instead of throwing.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public FileStatus statNullable(FileSystem fileSystem, Symlinks symlinks) {
    return fileSystem.statNullable(this, symlinks.toBoolean());
  }

  /** Prefer to use {@link #stat(FileSystem, Symlinks)}. */
  @Deprecated
  public FileStatus stat(Symlinks followSymlinks) throws IOException {
    return stat(fileSystem, followSymlinks);
  }

  /**
   * Returns the status of a file, optionally following symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found
   * @throws IOException if there was an error obtaining the file status. Note, some implementations
   *     may defer the I/O, and hence the throwing of the exception, until the accessor methods of
   *     {@code FileStatus} are called
   */
  public FileStatus stat(FileSystem fileSystem, Symlinks followSymlinks) throws IOException {
    return fileSystem.stat(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #statIfFound(FileSystem)}. */
  @Deprecated
  public FileStatus statIfFound() throws IOException {
    return statIfFound(fileSystem);
  }

  /**
   * Like {@link #stat}, but may return null if the file is not found (corresponding to {@code
   * ENOENT} and {@code ENOTDIR} in Unix's stat(2) function) instead of throwing. Follows symbolic
   * links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public FileStatus statIfFound(FileSystem fileSystem) throws IOException {
    return fileSystem.statIfFound(this, true);
  }

  /** Prefer to use {@link #statIfFound(FileSystem, Symlinks)}. */
  @Deprecated
  public FileStatus statIfFound(Symlinks followSymlinks) throws IOException {
    return statIfFound(fileSystem, followSymlinks);
  }

  /**
   * Like {@link #stat}, but may return null if the file is not found (corresponding to {@code
   * ENOENT} and {@code ENOTDIR} in Unix's stat(2) function) instead of throwing.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found
   */
  public FileStatus statIfFound(FileSystem fileSystem, Symlinks followSymlinks) throws IOException {
    return fileSystem.statIfFound(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #isDirectory()} (FileSystem)}. */
  @Deprecated
  public boolean isDirectory() {
    return isDirectory(fileSystem);
  }

  /**
   * Returns true iff this path denotes an existing directory. Follows symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public boolean isDirectory(FileSystem fileSystem) {
    return fileSystem.isDirectory(this, true);
  }

  /** Prefer to use {@link #isDirectory(FileSystem, Symlinks)}. */
  @Deprecated
  public boolean isDirectory(Symlinks followSymlinks) {
    return isDirectory(fileSystem, followSymlinks);
  }

  /**
   * Returns true iff this path denotes an existing directory.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found
   */
  public boolean isDirectory(FileSystem fileSystem, Symlinks followSymlinks) {
    return fileSystem.isDirectory(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #isFile(FileSystem)}. */
  @Deprecated
  public boolean isFile() {
    return isFile(fileSystem);
  }

  /**
   * Returns true iff this path denotes an existing regular or special file. Follows symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * <p>For our purposes, "file" includes special files (socket, fifo, block or char devices) too;
   * it excludes symbolic links and directories.
   */
  public boolean isFile(FileSystem fileSystem) {
    return fileSystem.isFile(this, true);
  }

  /** Prefer to use {@link #isFile(FileSystem, Symlinks)}. */
  @Deprecated
  public boolean isFile(Symlinks followSymlinks) {
    return isFile(fileSystem, followSymlinks);
  }

  /**
   * Returns true iff this path denotes an existing regular or special file.
   *
   * <p>For our purposes, a "file" includes special files (socket, fifo, block or char devices) too;
   * it excludes symbolic links and directories.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found.
   */
  public boolean isFile(FileSystem fileSystem, Symlinks followSymlinks) {
    return fileSystem.isFile(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #isSpecialFile(FileSystem)}. */
  @Deprecated
  public boolean isSpecialFile() {
    return isSpecialFile(fileSystem);
  }

  /**
   * Returns true iff this path denotes an existing special file (e.g. fifo). Follows symbolic
   * links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public boolean isSpecialFile(FileSystem fileSystem) {
    return fileSystem.isSpecialFile(this, true);
  }

  /** Prefer to use {@link #isSpecialFile(FileSystem, Symlinks)}. */
  @Deprecated
  public boolean isSpecialFile(Symlinks followSymlinks) {
    return isSpecialFile(fileSystem, followSymlinks);
  }

  /**
   * Returns true iff this path denotes an existing special file (e.g. fifo).
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a path other than a symbolic link is found.
   */
  public boolean isSpecialFile(FileSystem fileSystem, Symlinks followSymlinks) {
    return fileSystem.isSpecialFile(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #isSymbolicLink(FileSystem)}. */
  @Deprecated
  public boolean isSymbolicLink() {
    return isSymbolicLink(fileSystem);
  }

  /**
   * Returns true iff this path denotes an existing symbolic link. Does not follow symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public boolean isSymbolicLink(FileSystem fileSystem) {
    return fileSystem.isSymbolicLink(this);
  }

  /**
   * Returns the last segment of this path, or "/" for the root directory.
   */
  public String getBaseName() {
    return name;
  }

  /**
   * Interprets the name of a path segment relative to the current path and
   * returns the result.
   *
   * <p>This is a purely syntactic operation, i.e. it does no I/O, it does not
   * validate the existence of any path, nor resolve symbolic links. If 'prefix'
   * is not canonical, then a 'name' of '..' will be interpreted incorrectly.
   *
   * @precondition segment contains no slashes.
   */
  private Path getCanonicalPath(String segment) {
    if (segment.equals(".") || segment.isEmpty()) {
      return this; // that's a noop
    } else if (segment.equals("..")) {
      // top-level directory's parent is root, when canonicalising:
      return isTopLevelDirectory() ? this : parent;
    } else {
      return getCachedChildPath(segment);
    }
  }

  /**
   * Returns the path formed by appending the single non-special segment
   * "baseName" to this path.
   *
   * <p>You should almost always use {@link #getRelative} instead, which has
   * the same performance characteristics if the given name is a valid base
   * name, and which also works for '.', '..', and strings containing '/'.
   *
   * @throws IllegalArgumentException if {@code baseName} is not a valid base
   *     name according to {@link FileSystemUtils#checkBaseName}
   */
  public Path getChild(String baseName) {
    FileSystemUtils.checkBaseName(baseName);
    return getCachedChildPath(baseName);
  }

  protected Path getRootForRelativePathComputation(PathFragment suffix) {
    return suffix.isAbsolute() ? fileSystem.getRootDirectory() : this;
  }

  /**
   * Returns the path formed by appending the relative or absolute path fragment
   * {@code suffix} to this path.
   *
   * <p>If suffix is absolute, the current path will be ignored; otherwise, they
   * will be combined. Up-level references ("..") cause the preceding path
   * segment to be elided; this interpretation is only correct if the base path
   * is canonical.
   */
  public Path getRelative(PathFragment suffix) {
    Path result = getRootForRelativePathComputation(suffix);
    for (String segment : suffix.segments()) {
      result = result.getCanonicalPath(segment);
    }
    return result;
  }

  /**
   * Returns the path formed by appending the relative or absolute string
   * {@code path} to this path.
   *
   * <p>If the given path string is absolute, the current path will be ignored;
   * otherwise, they will be combined. Up-level references ("..") cause the
   * preceding path segment to be elided.
   *
   * <p>This is a purely syntactic operation, i.e. it does no I/O, it does not
   * validate the existence of any path, nor resolve symbolic links.
   */
  public Path getRelative(String path) {
    // Fast path for valid base names.
    if ((path.length() == 0) || (path.equals("."))) {
      return this;
    } else if (path.equals("..")) {
      return isTopLevelDirectory() ? this : parent;
    } else if (PathFragment.containsSeparator(path)) {
      return getRelative(PathFragment.create(path));
    } else {
      return getCachedChildPath(path);
    }
  }

  protected final String[] getSegments() {
    String[] resultSegments = new String[depth];
    Path currentPath = this;
    for (int pos = depth - 1; pos >= 0; pos--) {
      resultSegments[pos] = currentPath.getBaseName();
      currentPath = currentPath.getParentDirectory();
    }
    return resultSegments;
  }

  /** Returns an absolute PathFragment representing this path. */
  public PathFragment asFragment() {
    return PathFragment.createAlreadyInterned('\0', true, getSegments());
  }

  /**
   * Returns a relative path fragment to this path, relative to {@code
   * ancestorDirectory}. {@code ancestorDirectory} must be on the same
   * filesystem as this path. (Currently, both this path and "ancestorDirectory"
   * must be absolute, though this restriction could be loosened.)
   * <p>
   * <code>x.relativeTo(z) == y</code> implies
   * <code>z.getRelative(y.getPathString()) == x</code>.
   * <p>
   * For example, <code>"/foo/bar/wiz".relativeTo("/foo")</code> returns
   * <code>"bar/wiz"</code>.
   *
   * @throws IllegalArgumentException if this path is not beneath {@code
   *         ancestorDirectory} or if they are not part of the same filesystem
   */
  public PathFragment relativeTo(Path ancestorPath) {
    checkSameFilesystem(ancestorPath);

    if (isMaybeRelativeTo(ancestorPath)) {
      // Fast path: when otherPath is the ancestor of this path
      int resultSegmentCount = depth - ancestorPath.depth;
      if (resultSegmentCount >= 0) {
        String[] resultSegments = new String[resultSegmentCount];
        Path currentPath = this;
        for (int pos = resultSegmentCount - 1; pos >= 0; pos--) {
          resultSegments[pos] = currentPath.getBaseName();
          currentPath = currentPath.getParentDirectory();
        }
        if (ancestorPath.equals(currentPath)) {
          return PathFragment.createAlreadyInterned('\0', false, resultSegments);
        }
      }
    }

    throw new IllegalArgumentException("Path " + this + " is not beneath " + ancestorPath);
  }

  /**
   * Checks that "this" and "that" are paths on the same filesystem.
   */
  protected void checkSameFilesystem(Path that) {
    if (this.fileSystem != that.fileSystem) {
      throw new IllegalArgumentException("Files are on different filesystems: "
          + this + ", " + that);
    }
  }

  /** Prefer to use {@link #getOutputStream(FileSystem)}. */
  @Deprecated
  public OutputStream getOutputStream() throws IOException, FileNotFoundException {
    return getOutputStream(fileSystem);
  }

  /**
   * Returns an output stream to the file denoted by the current path, creating it and truncating it
   * if necessary. The stream is opened for writing.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException If the file cannot be found or created.
   * @throws IOException If a different error occurs.
   */
  public OutputStream getOutputStream(FileSystem fileSystem)
      throws IOException, FileNotFoundException {
    return getOutputStream(fileSystem, false);
  }

  /** Prefer to use {@link #getOutputStream(FileSystem, boolean)}. */
  @Deprecated
  public OutputStream getOutputStream(boolean append) throws IOException, FileNotFoundException {
    return getOutputStream(fileSystem, append);
  }

  /**
   * Returns an output stream to the file denoted by the current path, creating it and truncating it
   * if necessary. The stream is opened for writing.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param append whether to open the file in append mode.
   * @throws FileNotFoundException If the file cannot be found or created.
   * @throws IOException If a different error occurs.
   */
  public OutputStream getOutputStream(FileSystem fileSystem, boolean append)
      throws IOException, FileNotFoundException {
    return fileSystem.getOutputStream(this, append);
  }

  /** Prefer to use {@link #createDirectory(FileSystem)}. */
  @Deprecated
  public boolean createDirectory() throws IOException {
    return createDirectory(fileSystem);
  }

  /**
   * Creates a directory with the name of the current path, not following symbolic links. Returns
   * normally iff the directory exists after the call: true if the directory was created by this
   * call, false if the directory was already in existence. Throws an exception if the directory
   * could not be created for any reason.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the directory creation failed for any reason
   */
  public boolean createDirectory(FileSystem fileSystem) throws IOException {
    return fileSystem.createDirectory(this);
  }

  /** Prefer to use {@link #createSymbolicLink(FileSystem, Path)}. */
  @Deprecated
  public void createSymbolicLink(Path target) throws IOException {
    createSymbolicLink(fileSystem, target);
  }

  /**
   * Creates a symbolic link with the name of the current path, following symbolic links. The
   * referent of the created symlink is is the absolute path "target"; it is not possible to create
   * relative symbolic links via this method.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the creation of the symbolic link was unsuccessful for any reason
   */
  public void createSymbolicLink(FileSystem fileSystem, Path target) throws IOException {
    checkSameFilesystem(target);
    fileSystem.createSymbolicLink(this, target.asFragment());
  }

  /** Prefer to use {@link #createSymbolicLink(FileSystem, PathFragment)}. */
  @Deprecated
  public void createSymbolicLink(PathFragment target) throws IOException {
    createSymbolicLink(fileSystem, target);
  }

  /**
   * Creates a symbolic link with the name of the current path, following symbolic links. The
   * referent of the created symlink is is the path fragment "target", which may be absolute or
   * relative.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the creation of the symbolic link was unsuccessful for any reason
   */
  public void createSymbolicLink(FileSystem fileSystem, PathFragment target) throws IOException {
    fileSystem.createSymbolicLink(this, target);
  }

  /** Prefer to use {@link #readSymbolicLink(FileSystem)}. */
  @Deprecated
  public PathFragment readSymbolicLink() throws IOException {
    return readSymbolicLink(fileSystem);
  }

  /**
   * Returns the target of the current path, which must be a symbolic link. The link contents are
   * returned exactly, and may contain an absolute or relative path. Analogous to readlink(2).
   *
   * <p>Note: for {@link FileSystem}s where {@link FileSystem#supportsSymbolicLinksNatively(Path)}
   * returns false, this method will throw an {@link UnsupportedOperationException} if the link
   * points to a non-existent file.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @return the content (i.e. target) of the symbolic link
   * @throws IOException if the current path is not a symbolic link, or the contents of the link
   *     could not be read for any reason
   */
  public PathFragment readSymbolicLink(FileSystem fileSystem) throws IOException {
    return fileSystem.readSymbolicLink(this);
  }

  /** Prefer to use {@link #readSymbolicLinkUnchecked(FileSystem)}. */
  @Deprecated
  public PathFragment readSymbolicLinkUnchecked() throws IOException {
    return readSymbolicLinkUnchecked(fileSystem);
  }

  /**
   * If the current path is a symbolic link, returns the target of this symbolic link. The semantics
   * are intentionally left underspecified otherwise to permit efficient implementations.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @return the content (i.e. target) of the symbolic link
   * @throws IOException if the current path is not a symbolic link, or the contents of the link
   *     could not be read for any reason
   */
  public PathFragment readSymbolicLinkUnchecked(FileSystem fileSystem) throws IOException {
    return fileSystem.readSymbolicLinkUnchecked(this);
  }

  /** Prefer to use {@link #createHardLink(FileSystem, Path)}. */
  @Deprecated
  public void createHardLink(Path link) throws IOException {
    createHardLink(fileSystem, link);
  }

  /**
   * Create a hard link for the current path.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param link the path of the new link
   * @throws IOException if there was an error executing {@link FileSystem#createHardLink}
   */
  public void createHardLink(FileSystem fileSystem, Path link) throws IOException {
    fileSystem.createHardLink(link, this);
  }

  /** Prefer to use {@link #resolveSymbolicLinks(FileSystem)}. */
  @Deprecated
  public Path resolveSymbolicLinks() throws IOException {
    return resolveSymbolicLinks(fileSystem);
  }

  /**
   * Returns the canonical path for this path, by repeatedly replacing symbolic links with their
   * referents. Analogous to realpath(3).
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @return the canonical path for this path
   * @throws IOException if any symbolic link could not be resolved, or other error occurred (for
   *     example, the path does not exist)
   */
  public Path resolveSymbolicLinks(FileSystem fileSystem) throws IOException {
    return fileSystem.resolveSymbolicLinks(this);
  }

  /** Prefer to use {@link #renameTo(FileSystem, Path)}. */
  @Deprecated
  public void renameTo(Path target) throws IOException {
    renameTo(fileSystem, target);
  }

  /**
   * Renames the file denoted by the current path to the location "target", not following symbolic
   * links.
   *
   * <p>Files cannot be atomically renamed across devices; copying is required. Use {@link
   * FileSystemUtils#copyFile} followed by {@link Path#delete}.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the rename failed for any reason
   */
  public void renameTo(FileSystem fileSystem, Path target) throws IOException {
    checkSameFilesystem(target);
    fileSystem.renameTo(this, target);
  }

  /** Prefer to use {@link #getFileSize(FileSystem)}. */
  @Deprecated
  public long getFileSize() throws IOException, FileNotFoundException {
    return getFileSize(fileSystem);
  }

  /**
   * Returns the size in bytes of the file denoted by the current path, following symbolic links.
   *
   * <p>The size of a directory or special file is undefined and should not be used.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file denoted by the current path does not exist
   * @throws IOException if the file's metadata could not be read, or some other error occurred
   */
  public long getFileSize(FileSystem fileSystem) throws IOException, FileNotFoundException {
    return fileSystem.getFileSize(this, true);
  }

  /** Prefer to use {@link #getFileSize(FileSystem, Symlinks)}. */
  @Deprecated
  public long getFileSize(Symlinks followSymlinks) throws IOException, FileNotFoundException {
    return getFileSize(fileSystem, followSymlinks);
  }

  /**
   * Returns the size in bytes of the file denoted by the current path.
   *
   * <p>The size of directory or special file is undefined. The size of a symbolic link is the
   * length of the name of its referent.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is deferenced until a file other than a symbol link is found
   * @throws FileNotFoundException if the file denoted by the current path does not exist
   * @throws IOException if the file's metadata could not be read, or some other error occurred
   */
  public long getFileSize(FileSystem fileSystem, Symlinks followSymlinks)
      throws IOException, FileNotFoundException {
    return fileSystem.getFileSize(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #delete(FileSystem)}. */
  @Deprecated
  public boolean delete() throws IOException {
    return delete(fileSystem);
  }

  /**
   * Deletes the file denoted by this path, not following symbolic links. Returns normally iff the
   * file doesn't exist after the call: true if this call deleted the file, false if the file
   * already didn't exist. Throws an exception if the file could not be deleted for any reason.
   *
   * <p>This is a migration method. The method (and its FileSystem-less counterpart) will be deleted
   * once the FileSystem instance is removed from Path.
   *
   * @return true iff the file was actually deleted by this call
   * @throws IOException if the deletion failed but the file was present prior to the call
   */
  public boolean delete(FileSystem fileSystem) throws IOException {
    return fileSystem.delete(this);
  }

  /** Prefer to use {@link #getLastModifiedTime(FileSystem)}. */
  @Deprecated
  public long getLastModifiedTime() throws IOException {
    return getLastModifiedTime(fileSystem);
  }

  /**
   * Returns the last modification time of the file, in milliseconds since the UNIX epoch, of the
   * file denoted by the current path, following symbolic links.
   *
   * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond
   * precision.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the operation failed for any reason
   */
  public long getLastModifiedTime(FileSystem fileSystem) throws IOException {
    return fileSystem.getLastModifiedTime(this, true);
  }

  /** Prefer to use {@link #getLastModifiedTime(FileSystem, Symlinks)}. */
  @Deprecated
  public long getLastModifiedTime(Symlinks followSymlinks) throws IOException {
    return getLastModifiedTime(fileSystem, followSymlinks);
  }

  /**
   * Returns the last modification time of the file, in milliseconds since the UNIX epoch, of the
   * file denoted by the current path.
   *
   * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond
   * precision.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param followSymlinks if {@link Symlinks#FOLLOW}, and this path denotes a symbolic link, the
   *     link is dereferenced until a file other than a symbolic link is found
   * @throws IOException if the modification time for the file could not be obtained for any reason
   */
  public long getLastModifiedTime(FileSystem fileSystem, Symlinks followSymlinks)
      throws IOException {
    return fileSystem.getLastModifiedTime(this, followSymlinks.toBoolean());
  }

  /** Prefer to use {@link #setLastModifiedTime(FileSystem, long)}. */
  @Deprecated
  public void setLastModifiedTime(long newTime) throws IOException {
    setLastModifiedTime(fileSystem, newTime);
  }

  /**
   * Sets the modification time of the file denoted by the current path. Follows symbolic links. If
   * newTime is -1, the current time according to the kernel is used; this may differ from the JVM's
   * clock.
   *
   * <p>Caveat: many filesystems store file times in seconds, so do not rely on the millisecond
   * precision.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param newTime time, in milliseconds since the UNIX epoch, or -1L, meaning use the kernel's
   *     current time
   * @throws IOException if the modification time for the file could not be set for any reason
   */
  public void setLastModifiedTime(FileSystem fileSystem, long newTime) throws IOException {
    fileSystem.setLastModifiedTime(this, newTime);
  }

  /** Prefer to use {@link #getxattr(FileSystem, String)}. */
  @Deprecated
  public byte[] getxattr(String name) throws IOException {
    return getxattr(fileSystem, name);
  }

  /**
   * Returns value of the given extended attribute name or null if attribute does not exist or file
   * system does not support extended attributes. Follows symlinks.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public byte[] getxattr(FileSystem fileSystem, String name) throws IOException {
    return fileSystem.getxattr(this, name);
  }

  /** Prefer to use {@link #getFastDigest(FileSystem)}. */
  @Deprecated
  public byte[] getFastDigest() throws IOException {
    return getFastDigest(fileSystem);
  }

  /**
   * Gets a fast digest for the given path, or {@code null} if there isn't one available. The digest
   * should be suitable for detecting changes to the file.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public byte[] getFastDigest(FileSystem fileSystem) throws IOException {
    return fileSystem.getFastDigest(this);
  }

  /** Prefer to use {@link #isValidDigest(FileSystem, byte[])}. */
  @Deprecated
  public boolean isValidDigest(byte[] digest) {
    return isValidDigest(fileSystem, digest);
  }

  /**
   * Returns whether the given digest is a valid digest for the default system digest function.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   */
  public boolean isValidDigest(FileSystem fileSystem, byte[] digest) {
    return fileSystem.isValidDigest(digest);
  }

  /** Prefer to use {@link #getDigest(FileSystem)}. */
  @Deprecated
  public byte[] getDigest() throws IOException {
    return getDigest(fileSystem);
  }

  /**
   * Returns the digest of the file denoted by the current path, following symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @return a new byte array containing the file's digest
   * @throws IOException if the digest could not be computed for any reason
   */
  public byte[] getDigest(FileSystem fileSystem) throws IOException {
    return fileSystem.getDigest(this);
  }

  /** Prefer to use {@link #getDigest(FileSystem, HashFunction)}. */
  @Deprecated
  public byte[] getDigest(HashFunction hashFunction) throws IOException {
    return getDigest(fileSystem, hashFunction);
  }

  /**
   * Returns the digest of the file denoted by the current path and digest function, following
   * symbolic links.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @return a new byte array containing the file's digest
   * @throws IOException if the digest could not be computed for any reason
   */
  public byte[] getDigest(FileSystem fileSystem, HashFunction hashFunction) throws IOException {
    return fileSystem.getDigest(this, hashFunction);
  }

  /** Prefer to use {@link #getInputStream(FileSystem)}. */
  @Deprecated
  public InputStream getInputStream() throws IOException {
    return getInputStream(fileSystem);
  }

  /**
   * Opens the file denoted by this path, following symbolic links, for reading, and returns an
   * input stream to it.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws IOException if the file was not found or could not be opened for reading
   */
  public InputStream getInputStream(FileSystem fileSystem) throws IOException {
    return fileSystem.getInputStream(this);
  }

  /**
   * Returns a java.io.File representation of this path.
   *
   * <p>Caveat: the result may be useless if this path's getFileSystem() is not
   * the UNIX filesystem.
   */
  public File getPathFile() {
    return new File(getPathString());
  }

  /** Prefer to use {@link #isWritable(FileSystem)}. */
  @Deprecated
  public boolean isWritable() throws IOException, FileNotFoundException {
    return isWritable(fileSystem);
  }

  /**
   * Returns true if the file denoted by the current path, following symbolic links, is writable for
   * the current user.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file does not exist, a dangling symbolic link was
   *     encountered, or the file's metadata could not be read
   */
  public boolean isWritable(FileSystem fileSystem) throws IOException, FileNotFoundException {
    return fileSystem.isWritable(this);
  }

  /** Prefer to use {@link #setReadable(FileSystem, boolean)}. */
  @Deprecated
  public void setReadable(boolean readable) throws IOException, FileNotFoundException {
    setReadable(fileSystem, readable);
  }

  /**
   * Sets the read permissions of the file denoted by the current path, following symbolic links.
   * Permissions apply to the current user.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param readable if true, the file is set to readable; otherwise the file is made non-readable
   * @throws FileNotFoundException if the file does not exist
   * @throws IOException If the action cannot be taken (ie. permissions)
   */
  public void setReadable(FileSystem fileSystem, boolean readable)
      throws IOException, FileNotFoundException {
    fileSystem.setReadable(this, readable);
  }

  /** Prefer to use {@link #setWritable(FileSystem, boolean)}. */
  @Deprecated
  public void setWritable(boolean writable) throws IOException, FileNotFoundException {
    setWritable(fileSystem, writable);
  }

  /**
   * Sets the write permissions of the file denoted by the current path, following symbolic links.
   * Permissions apply to the current user.
   *
   * <p>TODO(bazel-team): (2009) what about owner/group/others?
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @param writable if true, the file is set to writable; otherwise the file is made non-writable
   * @throws FileNotFoundException if the file does not exist
   * @throws IOException If the action cannot be taken (ie. permissions)
   */
  public void setWritable(FileSystem fileSystem, boolean writable)
      throws IOException, FileNotFoundException {
    fileSystem.setWritable(this, writable);
  }

  /** Prefer to use {@link #isExecutable(FileSystem)}. */
  @Deprecated
  public boolean isExecutable() throws IOException, FileNotFoundException {
    return isExecutable(fileSystem);
  }

  /**
   * Returns true iff the file specified by the current path, following symbolic links, is
   * executable by the current user.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was
   *     encountered
   * @throws IOException if some other I/O error occurred
   */
  public boolean isExecutable(FileSystem fileSystem) throws IOException, FileNotFoundException {
    return fileSystem.isExecutable(this);
  }

  /** Prefer to use {@link #isReadable(FileSystem)}. */
  @Deprecated
  public boolean isReadable() throws IOException, FileNotFoundException {
    return isReadable(fileSystem);
  }

  /**
   * Returns true iff the file specified by the current path, following symbolic links, is readable
   * by the current user.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was
   *     encountered
   * @throws IOException if some other I/O error occurred
   */
  public boolean isReadable(FileSystem fileSystem) throws IOException, FileNotFoundException {
    return fileSystem.isReadable(this);
  }

  /** Prefer to use {@link #setExecutable(FileSystem, boolean)}. */
  @Deprecated
  public void setExecutable(boolean executable) throws IOException, FileNotFoundException {
    setExecutable(fileSystem, executable);
  }

  /**
   * Sets the execute permission on the file specified by the current path, following symbolic
   * links. Permissions apply to the current user.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was
   *     encountered
   * @throws IOException if the metadata change failed, for example because of permissions
   */
  public void setExecutable(FileSystem fileSystem, boolean executable)
      throws IOException, FileNotFoundException {
    fileSystem.setExecutable(this, executable);
  }

  /** Prefer to use {@link #chmod(FileSystem, int)}. */
  @Deprecated
  public void chmod(int mode) throws IOException {
    chmod(fileSystem, mode);
  }

  /**
   * Sets the permissions on the file specified by the current path, following symbolic links. If
   * permission changes on this path's {@link FileSystem} are slow (e.g. one syscall per change),
   * this method should aim to be faster than setting each permission individually. If this path's
   * {@link FileSystem} does not support group and others permissions, those bits will be ignored.
   *
   * <p>This is a migration method. It will be deleted once the file system instance is deleted from
   * Path.
   *
   * @throws FileNotFoundException if the file does not exist or a dangling symbolic link was
   *     encountered
   * @throws IOException if the metadata change failed, for example because of permissions
   */
  public void chmod(FileSystem fileSystem, int mode) throws IOException {
    fileSystem.chmod(this, mode);
  }

  /** Prefer to use {@link #prefetchPackageAsync(FileSystem, int)}. */
  @Deprecated
  public void prefetchPackageAsync(int maxDirs) {
    prefetchPackageAsync(fileSystem, maxDirs);
  }

  public void prefetchPackageAsync(FileSystem fileSystem, int maxDirs) {
    fileSystem.prefetchPackageAsync(this, maxDirs);
  }

  /**
   * Compare Paths of the same file system using their PathFragments.
   *
   * <p>Paths from different filesystems will be compared using the identity
   * hash code of their respective filesystems.
   */
  @Override
  public int compareTo(Path o) {
    // Fast-path.
    if (equals(o)) {
      return 0;
    }

    // If they are on different file systems, the file system decides the ordering.
    FileSystem otherFs = o.getFileSystem();
    if (!fileSystem.equals(otherFs)) {
      int thisFileSystemHash = System.identityHashCode(fileSystem);
      int otherFileSystemHash = System.identityHashCode(otherFs);
      if (thisFileSystemHash < otherFileSystemHash) {
        return -1;
      } else if (thisFileSystemHash > otherFileSystemHash) {
        return 1;
      } else {
        // TODO(bazel-team): Add a name to every file system to be used here.
        return 0;
      }
    }

    // Equal file system, but different paths, because of the canonicalization.
    // We expect to often compare Paths that are very similar, for example for files in the same
    // directory. This can be done efficiently by going up segment by segment until we get the
    // identical path (canonicalization again), and then just compare the immediate child segments.
    // Overall this is much faster than creating PathFragment instances, and comparing those, which
    // requires us to always go up to the top-level directory and copy all segments into a new
    // string array.
    // This was previously showing up as a hotspot in a profile of globbing a large directory.
    Path a = this;
    Path b = o;
    int maxDepth = Math.min(a.depth, b.depth);
    while (a.depth > maxDepth) {
      a = a.getParentDirectory();
    }
    while (b.depth > maxDepth) {
      b = b.getParentDirectory();
    }
    // One is the child of the other.
    if (a.equals(b)) {
      // If a is the same as this, this.depth must be less than o.depth.
      return equals(a) ? -1 : 1;
    }
    Path previousa;
    Path previousb;
    do {
      previousa = a;
      previousb = b;
      a = a.getParentDirectory();
      b = b.getParentDirectory();
    } while (!a.equals(b)); // This has to happen eventually.
    return previousa.name.compareTo(previousb.name);
  }
}