-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathPathFragment.java
868 lines (795 loc) · 30.2 KB
/
PathFragment.java
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
// Copyright 2017 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 static com.google.devtools.build.lib.skyframe.serialization.strings.UnsafeStringCodec.stringCodec;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.CommandLineItem;
import com.google.devtools.build.lib.skyframe.serialization.LeafDeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.LeafObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.LeafSerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.util.FileType;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.IOException;
import javax.annotation.Nullable;
/**
* A path segment representing a path fragment using the host machine's path style. That is; If you
* are running on a Unix machine, the path style will be unix, on Windows it is the windows path
* style.
*
* <p>Path fragments are either absolute or relative.
*
* <p>Strings are normalized with '.' and '..' removed and resolved (if possible), any multiple
* slashes ('/') removed, and any trailing slash also removed. Windows drive letters are uppercased.
* The current implementation does not touch the incoming path string unless the string actually
* needs to be normalized.
*
* <p>There is some limited support for Windows-style paths. Most importantly, drive identifiers in
* front of a path (c:/abc) are supported and such paths are correctly recognized as absolute, as
* are paths with backslash separators (C:\\foo\\bar). However, advanced Windows-style features like
* \\\\network\\paths and \\\\?\\unc\\paths are not supported. We are currently using forward
* slashes ('/') even on Windows.
*
* <p>Mac and Windows path fragments are case insensitive.
*/
@Immutable
public abstract class PathFragment
implements Comparable<PathFragment>, FileType.HasFileType, CommandLineItem {
private static final OsPathPolicy OS = OsPathPolicy.getFilePathOs();
@SerializationConstant
public static final PathFragment EMPTY_FRAGMENT = new RelativePathFragment("");
public static final char SEPARATOR_CHAR = '/';
private static final char ADDITIONAL_SEPARATOR_CHAR = OS.additionalSeparator();
private final String normalizedPath;
// DON'T add more fields here unless you know what you are doing. Adding another field will
// increase the shallow heap of a PathFragment instance beyond the current value of 16 bytes.
// Blaze's heap typically has many instances.
/** Creates a new normalized path fragment. */
public static PathFragment create(String path) {
if (path.isEmpty()) {
return EMPTY_FRAGMENT;
}
int normalizationLevel = OS.needsToNormalize(path);
String normalizedPath =
normalizationLevel != OsPathPolicy.NORMALIZED
? OS.normalize(path, normalizationLevel)
: path;
int driveStrLength = OS.getDriveStrLength(normalizedPath);
return makePathFragment(normalizedPath, driveStrLength);
}
private static PathFragment makePathFragment(String normalizedPath, int driveStrLength) {
switch (driveStrLength) {
case 0:
return new RelativePathFragment(normalizedPath);
case 1:
return new UnixStyleAbsolutePathFragment(normalizedPath);
case 3:
return new WindowsStyleAbsolutePathFragment(normalizedPath);
default:
throw new IllegalStateException(
String.format(
"normalizedPath: %s, driveStrLength: %s", normalizedPath, driveStrLength));
}
}
/**
* Creates a new path fragment, where the caller promises that the path is normalized.
*
* <p>WARNING! Make sure the path fragment is in fact already normalized. The rest of the code
* assumes this is the case.
*/
public static PathFragment createAlreadyNormalized(String normalizedPath) {
return normalizedPath.isEmpty()
? EMPTY_FRAGMENT
: makePathFragment(normalizedPath, OS.getDriveStrLength(normalizedPath));
}
/** This method expects path to already be normalized. */
private PathFragment(String normalizedPath) {
this.normalizedPath = Preconditions.checkNotNull(normalizedPath);
}
public String getPathString() {
return normalizedPath;
}
public boolean isEmpty() {
return normalizedPath.isEmpty();
}
/**
* Returns 0 for relative paths (e.g. "a/b"), 1 for Unix-style absolute paths (e.g. "/a/b"), and 3
* for Windows-style absolute paths (e.g. "a:/b").
*/
public abstract int getDriveStrLength();
private static final class RelativePathFragment extends PathFragment {
// DON'T add any fields here unless you know what you are doing. Adding another field will
// increase the shallow heap of a RelativePathFragment instance beyond the current value of 16
// bytes. Our heap typically has many instances.
private RelativePathFragment(String normalizedPath) {
super(normalizedPath);
}
@Override
public int getDriveStrLength() {
return 0;
}
}
private static final class UnixStyleAbsolutePathFragment extends PathFragment {
// DON'T add any fields here unless you know what you are doing. Adding another field will
// increase the shallow heap of a UnixStyleAbsolutePathFragment instance beyond the current
// value of 16 bytes. Our heap typically has many instances.
private UnixStyleAbsolutePathFragment(String normalizedPath) {
super(normalizedPath);
}
@Override
public int getDriveStrLength() {
return 1;
}
}
private static final class WindowsStyleAbsolutePathFragment extends PathFragment {
// DON'T add any fields here unless you know what you are doing. Adding another field will
// increase the shallow heap of a WindowsStyleAbsolutePathFragment instance beyond the current
// value of 16 bytes. Our heap typically has many instances (when Bazel is run on Windows).
private WindowsStyleAbsolutePathFragment(String normalizedPath) {
super(normalizedPath);
}
@Override
public int getDriveStrLength() {
return 3;
}
}
/**
* If called on a {@link PathFragment} instance for a mount name (eg. '/' or 'C:/'), the empty
* string is returned.
*
* <p>This operation allocates a new string.
*/
public String getBaseName() {
int lastSeparator = normalizedPath.lastIndexOf(SEPARATOR_CHAR);
return lastSeparator < getDriveStrLength()
? normalizedPath.substring(getDriveStrLength())
: normalizedPath.substring(lastSeparator + 1);
}
/**
* Returns a {@link PathFragment} instance formed by resolving {@code other} relative to this
* path. For example, if this path is "a" and other is "b", returns "a/b".
*
* <p>If the passed path is absolute it is returned untouched. This can be useful to resolve
* symlinks.
*/
public PathFragment getRelative(PathFragment other) {
Preconditions.checkNotNull(other);
// Fast-path: The path fragment is already normal, use cheaper normalization check
String otherStr = other.normalizedPath;
return getRelative(otherStr, other.getDriveStrLength(), OS.needsToNormalizeSuffix(otherStr));
}
/**
* Returns a {@link PathFragment} instance formed by resolving {@code other} relative to this
* path. For example, if this path is "a" and other is "b", returns "a/b".
*
* <p>See {@link #getRelative(PathFragment)} for details.
*/
public PathFragment getRelative(String other) {
Preconditions.checkNotNull(other);
return getRelative(other, OS.getDriveStrLength(other), OS.needsToNormalize(other));
}
private PathFragment getRelative(String other, int otherDriveStrLength, int normalizationLevel) {
if (normalizedPath.isEmpty()) {
return create(other);
}
if (other.isEmpty()) {
return this;
}
// This is an absolute path, simply return it
if (otherDriveStrLength > 0) {
String normalizedPath =
normalizationLevel != OsPathPolicy.NORMALIZED
? OS.normalize(other, normalizationLevel)
: other;
return makePathFragment(normalizedPath, otherDriveStrLength);
}
String newPath;
if (normalizedPath.length() == getDriveStrLength()) {
newPath = normalizedPath + other;
} else {
newPath = normalizedPath + '/' + other;
}
newPath =
normalizationLevel != OsPathPolicy.NORMALIZED
? OS.normalize(newPath, normalizationLevel)
: newPath;
return makePathFragment(newPath, getDriveStrLength());
}
public static boolean isNormalizedRelativePath(String path) {
int driveStrLength = OS.getDriveStrLength(path);
int normalizationLevel = OS.needsToNormalize(path);
return driveStrLength == 0 && normalizationLevel == OsPathPolicy.NORMALIZED;
}
public static boolean containsSeparator(String path) {
return path.lastIndexOf(SEPARATOR_CHAR) != -1;
}
public PathFragment getChild(String baseName) {
checkBaseName(baseName);
String newPath;
if (normalizedPath.length() == getDriveStrLength()) {
newPath = normalizedPath + baseName;
} else {
newPath = normalizedPath + '/' + baseName;
}
return makePathFragment(newPath, getDriveStrLength());
}
/**
* Returns the parent directory of this {@link PathFragment}.
*
* <p>If this is called on an single directory for a relative path, this returns an empty relative
* path. If it's called on a root (like '/') or the empty string, it returns null.
*/
@Nullable
public PathFragment getParentDirectory() {
int lastSeparator = normalizedPath.lastIndexOf(SEPARATOR_CHAR);
// For absolute paths we need to specially handle when we hit root
// Relative paths can't hit this path as driveStrLength == 0
if (getDriveStrLength() > 0) {
if (lastSeparator < getDriveStrLength()) {
if (normalizedPath.length() > getDriveStrLength()) {
String newPath = normalizedPath.substring(0, getDriveStrLength());
return makePathFragment(newPath, getDriveStrLength());
} else {
return null;
}
}
} else {
if (lastSeparator == -1) {
if (!normalizedPath.isEmpty()) {
return EMPTY_FRAGMENT;
} else {
return null;
}
}
}
String newPath = normalizedPath.substring(0, lastSeparator);
return makePathFragment(newPath, getDriveStrLength());
}
/**
* Returns the {@link PathFragment} relative to the base {@link PathFragment}.
*
* <p>For example, <code>
* {@link PathFragment}.create("foo/bar/wiz").relativeTo({@link PathFragment}.create("foo"))
* </code> returns <code>"bar/wiz"</code>.
*
* <p>If the {@link PathFragment} is not a child of the passed {@link PathFragment} an {@link
* IllegalArgumentException} is thrown. In particular, this will happen whenever the two {@link
* PathFragment} instances aren't both absolute or both relative.
*/
public PathFragment relativeTo(PathFragment base) {
Preconditions.checkNotNull(base);
if (isAbsolute() != base.isAbsolute()) {
throw new IllegalArgumentException(
"Cannot relativize an absolute and a non-absolute path pair");
}
String basePath = base.normalizedPath;
if (!OS.startsWith(normalizedPath, basePath)) {
throw new IllegalArgumentException(
String.format("Path '%s' is not under '%s', cannot relativize", this, base));
}
int bn = basePath.length();
if (bn == 0) {
return this;
}
if (normalizedPath.length() == bn) {
return EMPTY_FRAGMENT;
}
final int lastSlashIndex;
if (basePath.charAt(bn - 1) == '/') {
lastSlashIndex = bn - 1;
} else {
lastSlashIndex = bn;
}
if (normalizedPath.charAt(lastSlashIndex) != '/') {
throw new IllegalArgumentException(
String.format("Path '%s' is not under '%s', cannot relativize", this, base));
}
String newPath = normalizedPath.substring(lastSlashIndex + 1);
return new RelativePathFragment(newPath);
}
public PathFragment relativeTo(String base) {
return relativeTo(PathFragment.create(base));
}
/**
* Returns true iff {@code other} is an ancestor of this path.
*
* <p>If this == other, true is returned.
*
* <p>An absolute path can never be an ancestor of a relative path, and vice versa.
*/
public boolean startsWith(PathFragment other) {
Preconditions.checkNotNull(other);
if (other.normalizedPath.length() > normalizedPath.length()) {
return false;
}
if (getDriveStrLength() != other.getDriveStrLength()) {
return false;
}
if (!OS.startsWith(normalizedPath, other.normalizedPath)) {
return false;
}
return normalizedPath.length() == other.normalizedPath.length()
|| other.normalizedPath.length() == getDriveStrLength()
|| normalizedPath.charAt(other.normalizedPath.length()) == SEPARATOR_CHAR;
}
/**
* Returns true iff {@code other}, considered as a list of path segments, is relative and a suffix
* of {@code this}, or both are absolute and equal.
*
* <p>This is a reflexive, transitive, anti-symmetric relation (i.e. a partial order)
*/
public boolean endsWith(PathFragment other) {
Preconditions.checkNotNull(other);
if (other.normalizedPath.length() > normalizedPath.length()) {
return false;
}
if (other.isAbsolute()) {
return this.equals(other);
}
if (!OS.endsWith(normalizedPath, other.normalizedPath)) {
return false;
}
return normalizedPath.length() == other.normalizedPath.length()
|| other.normalizedPath.isEmpty()
|| normalizedPath.charAt(normalizedPath.length() - other.normalizedPath.length() - 1)
== SEPARATOR_CHAR;
}
public boolean isAbsolute() {
return getDriveStrLength() > 0;
}
public static boolean isAbsolute(String path) {
return OS.getDriveStrLength(path) > 0;
}
@Override
public String toString() {
return normalizedPath;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return OS.equals(this.normalizedPath, ((PathFragment) o).normalizedPath);
}
@Override
public int hashCode() {
return OS.hash(this.normalizedPath);
}
@Override
public int compareTo(PathFragment o) {
return OS.compare(this.normalizedPath, o.normalizedPath);
}
////////////////////////////////////////////////////////////////////////
/**
* Returns the number of segments in this path, excluding the drive string for absolute paths.
*
* <p>This operation is O(N) on the length of the string.
*/
public int segmentCount() {
int n = normalizedPath.length();
int segmentCount = 0;
int i;
for (i = getDriveStrLength(); i < n; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
++segmentCount;
}
}
// Add last segment if one exists.
if (i > getDriveStrLength()) {
++segmentCount;
}
return segmentCount;
}
/**
* Determines whether this path consists of a single segment, excluding the drive string for
* absolute paths.
*
* <p>Prefer this method over {@link #segmentCount} when it is not necessary to know the exact
* number of segments, as this short-circuits as soon as {@link #SEPARATOR_CHAR} is found.
*/
public boolean isSingleSegment() {
return normalizedPath.length() > getDriveStrLength() && !isMultiSegment();
}
/**
* Determines whether this path consists of multiple segments, excluding the drive string for
* absolute paths.
*
* <p>Prefer this method over {@link #segmentCount} when it is not necessary to know the exact
* number of segments, as this short-circuits as soon as {@link #SEPARATOR_CHAR} is found.
*/
public boolean isMultiSegment() {
return normalizedPath.indexOf(SEPARATOR_CHAR, getDriveStrLength()) >= 0;
}
/**
* Returns the specified segment of this path; index must be non-negative and less than {@code
* segmentCount()}.
*
* <p>This operation is O(N) on the length of the string.
*/
public String getSegment(int index) {
int n = normalizedPath.length();
int segmentCount = 0;
int i;
for (i = getDriveStrLength(); i < n && segmentCount < index; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
++segmentCount;
}
}
int starti = i;
for (; i < n; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
break;
}
}
// Add last segment if one exists.
if (i > getDriveStrLength()) {
++segmentCount;
}
int endi = i;
if (index < 0 || index >= segmentCount) {
throw new IllegalArgumentException("Illegal segment index: " + index);
}
return normalizedPath.substring(starti, endi);
}
/**
* Returns a new path fragment that is a sub fragment of this one. The sub fragment begins at the
* specified <code>beginIndex</code> segment and ends at the segment at index <code>endIndex - 1
* </code>. Thus the number of segments in the new PathFragment is <code>endIndex - beginIndex
* </code>.
*
* <p>If the path is absolute and <code>beginIndex</code> is zero, the returned path is absolute.
* Otherwise, if the path is relative or <code>beginIndex> is greater than zero, the returned path
* is relative.
*
* <p>This operation is O(N) on the length of the string.
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
* @return the specified sub fragment, never null.
* @exception IndexOutOfBoundsException if the <code>beginIndex</code> is negative, or <code>
* endIndex</code> is larger than the length of this <code>String</code> object, or <code>
* beginIndex</code> is larger than <code>endIndex</code>.
*/
public PathFragment subFragment(int beginIndex, int endIndex) {
if (beginIndex < 0 || beginIndex > endIndex) {
throw new IndexOutOfBoundsException(
String.format("path: %s, beginIndex: %d endIndex: %d", toString(), beginIndex, endIndex));
}
return subFragmentImpl(beginIndex, endIndex);
}
public PathFragment subFragment(int beginIndex) {
if (beginIndex < 0) {
throw new IndexOutOfBoundsException(
String.format("path: %s, beginIndex: %d", toString(), beginIndex));
}
return subFragmentImpl(beginIndex, -1);
}
private PathFragment subFragmentImpl(int beginIndex, int endIndex) {
int n = normalizedPath.length();
int segmentIndex = 0;
int i;
for (i = getDriveStrLength(); i < n && segmentIndex < beginIndex; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
++segmentIndex;
}
}
int starti = i;
if (segmentIndex < endIndex) {
for (; i < n; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
++segmentIndex;
if (segmentIndex == endIndex) {
break;
}
}
}
} else if (endIndex == -1) {
i = normalizedPath.length();
}
int endi = i;
// Add last segment if one exists for verification
if (i == n && i > getDriveStrLength()) {
++segmentIndex;
}
if (beginIndex > segmentIndex || endIndex > segmentIndex) {
throw new IndexOutOfBoundsException(
String.format("path: %s, beginIndex: %d endIndex: %d", toString(), beginIndex, endIndex));
}
// If beginIndex is 0, we include the drive string.
int driveStrLength = 0;
if (beginIndex == 0) {
starti = 0;
driveStrLength = this.getDriveStrLength();
endi = Math.max(endi, driveStrLength);
}
return makePathFragment(normalizedPath.substring(starti, endi), driveStrLength);
}
/**
* Returns an {@link Iterable} that lazily yields the segments of this path.
*
* <p>When iterating over the segments of a path fragment, prefer this method to {@link
* #splitToListOfSegments} as it performs a single, lazy traversal over the path string without
* the overhead of creating a list.
*/
public Iterable<String> segments() {
return () -> PathSegmentIterator.create(normalizedPath, getDriveStrLength());
}
/**
* Splits this path fragment into a list of segments.
*
* <p>This operation is O(N) on the length of the string. If it is not necessary to store the
* segments in list form, consider using {@link #segments}.
*/
public ImmutableList<String> splitToListOfSegments() {
ImmutableList.Builder<String> segments = ImmutableList.builderWithExpectedSize(segmentCount());
int nexti = getDriveStrLength();
int n = normalizedPath.length();
for (int i = getDriveStrLength(); i < n; ++i) {
if (normalizedPath.charAt(i) == SEPARATOR_CHAR) {
segments.add(normalizedPath.substring(nexti, i));
nexti = i + 1;
}
}
// Add last segment if one exists.
if (nexti < n) {
segments.add(normalizedPath.substring(nexti));
}
return segments.build();
}
/** Returns the path string, or '.' if the path is empty. */
public String getSafePathString() {
return !normalizedPath.isEmpty() ? normalizedPath : ".";
}
/**
* Returns the path string using '/' as the name-separator character, but do so in a way
* unambiguously recognizable as path. In other words, return "." for relative and empty paths,
* and prefix relative paths with one segment by "./".
*
* <p>In this way, a shell will always interpret such a string as path (absolute or relative to
* the working directory) and not as command to be searched for in the search path.
*/
public String getCallablePathString() {
if (isAbsolute()) {
return normalizedPath;
} else if (normalizedPath.isEmpty()) {
return ".";
} else if (normalizedPath.indexOf(SEPARATOR_CHAR) == -1) {
return "." + SEPARATOR_CHAR + normalizedPath;
} else {
return normalizedPath;
}
}
/**
* Returns the file extension of this path, excluding the period, or "" if there is no extension.
*/
public String getFileExtension() {
int n = normalizedPath.length();
for (int i = n - 1; i > getDriveStrLength(); --i) {
char c = normalizedPath.charAt(i);
if (c == '.') {
return normalizedPath.substring(i + 1, n);
} else if (c == SEPARATOR_CHAR) {
break;
}
}
return "";
}
/**
* Returns a new PathFragment formed by appending {@code newName} to the parent directory. Null is
* returned iff this method is called on a PathFragment with zero segments. If {@code newName}
* designates an absolute path, the value of {@code this} will be ignored and a PathFragment
* corresponding to {@code newName} will be returned. This behavior is consistent with the
* behavior of {@link #getRelative(String)}.
*/
@Nullable
public PathFragment replaceName(String newName) {
PathFragment parent = getParentDirectory();
return parent != null ? parent.getRelative(newName) : null;
}
/**
* Returns the drive for an absolute path fragment.
*
* <p>On unix, this will return "/". On Windows it will return the drive letter, like "C:/".
*/
public String getDriveStr() {
Preconditions.checkArgument(isAbsolute());
return normalizedPath.substring(0, getDriveStrLength());
}
/**
* Returns a relative PathFragment created from this absolute PathFragment using the
* same segments and drive letter.
*/
public PathFragment toRelative() {
Preconditions.checkArgument(isAbsolute());
return makePathFragment(normalizedPath.substring(getDriveStrLength()), 0);
}
/**
* Returns true if this path contains uplevel references "..".
*
* <p>Since path fragments are normalized, this implies that the uplevel reference is at the start
* of the path fragment.
*/
public boolean containsUplevelReferences() {
// Path is normalized, so any ".." would have to be the first segment.
return normalizedPath.startsWith("..")
&& (normalizedPath.length() == 2 || normalizedPath.charAt(2) == SEPARATOR_CHAR);
}
/**
* Returns true if the passed path contains uplevel references "..".
*
* <p>This is useful to check a string for '..' segments before constructing a PathFragment, since
* these are always normalized and will throw uplevel references away.
*/
public static boolean containsUplevelReferences(String path) {
return !isNormalizedImpl(path, /* lookForSameLevelReferences= */ false);
}
/**
* Returns true if the passed path does not contain uplevel references ("..") or single-dot
* references (".").
*
* <p>This is useful to check a string for normalization before constructing a PathFragment, since
* these are always normalized and will throw uplevel references away.
*/
public static boolean isNormalized(String path) {
return isNormalizedImpl(path, /* lookForSameLevelReferences= */ true);
}
private enum NormalizedImplState {
Base, /* No particular state, eg. an 'a' or 'L' character */
Separator, /* We just saw a separator */
Dot, /* We just saw a dot after a separator */
DotDot, /* We just saw two dots after a separator */
}
private static boolean isNormalizedImpl(String path, boolean lookForSameLevelReferences) {
// Starting state is equivalent to having just seen a separator
NormalizedImplState state = NormalizedImplState.Separator;
int n = path.length();
for (int i = 0; i < n; ++i) {
char c = path.charAt(i);
boolean isSeparator = OS.isSeparator(c);
switch (state) {
case Base:
if (isSeparator) {
state = NormalizedImplState.Separator;
} else {
state = NormalizedImplState.Base;
}
break;
case Separator:
if (isSeparator) {
state = NormalizedImplState.Separator;
} else if (c == '.') {
state = NormalizedImplState.Dot;
} else {
state = NormalizedImplState.Base;
}
break;
case Dot:
if (isSeparator) {
if (lookForSameLevelReferences) {
// "." segment found
return false;
}
state = NormalizedImplState.Separator;
} else if (c == '.') {
state = NormalizedImplState.DotDot;
} else {
state = NormalizedImplState.Base;
}
break;
case DotDot:
if (isSeparator) {
// ".." segment found
return false;
} else {
state = NormalizedImplState.Base;
}
break;
default:
throw new IllegalStateException("Unhandled state: " + state);
}
}
// The character just after the string is equivalent to a separator
switch (state) {
case Dot:
if (lookForSameLevelReferences) {
// "." segment found
return false;
}
break;
case DotDot:
return false;
default:
}
return true;
}
/**
* Throws {@link IllegalArgumentException} if {@code paths} contains any paths that are equal to
* {@code startingWithPath} or that are not beneath {@code startingWithPath}.
*/
public static void checkAllPathsAreUnder(
Iterable<PathFragment> paths, PathFragment startingWithPath) {
for (PathFragment path : paths) {
Preconditions.checkArgument(
!path.equals(startingWithPath) && path.startsWith(startingWithPath),
"%s is not beneath %s",
path,
startingWithPath);
}
}
@Override
public String filePathForFileTypeMatcher() {
return normalizedPath;
}
@Override
public String expandToCommandLine() {
return normalizedPath;
}
private static void checkBaseName(String baseName) {
if (baseName.isEmpty()) {
throw new IllegalArgumentException("Child must not be empty string ('')");
}
if (baseName.equals(".") || baseName.equals("..")) {
throw new IllegalArgumentException("baseName must not be '" + baseName + "'");
}
try {
checkSeparators(baseName);
} catch (InvalidBaseNameException e) {
throw new IllegalArgumentException("baseName " + e.getMessage() + ": '" + baseName + "'", e);
}
}
public static void checkSeparators(String baseName) throws InvalidBaseNameException {
if (baseName.indexOf(SEPARATOR_CHAR) != -1) {
throw new InvalidBaseNameException("must not contain " + SEPARATOR_CHAR);
}
if (ADDITIONAL_SEPARATOR_CHAR != 0) {
if (baseName.indexOf(ADDITIONAL_SEPARATOR_CHAR) != -1) {
throw new InvalidBaseNameException("must not contain " + ADDITIONAL_SEPARATOR_CHAR);
}
}
}
/** Indicates that a path fragment's base name had invalid characters. */
public static final class InvalidBaseNameException extends Exception {
private InvalidBaseNameException(String message) {
super(message);
}
}
public static Codec pathFragmentCodec() {
return Codec.INSTANCE;
}
private static class Codec extends LeafObjectCodec<PathFragment> {
private static final Codec INSTANCE = new Codec();
@Override
public Class<PathFragment> getEncodedClass() {
return PathFragment.class;
}
@Override
public void serialize(
LeafSerializationContext context, PathFragment obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
context.serializeLeaf(obj.normalizedPath, stringCodec(), codedOut);
}
@Override
public PathFragment deserialize(LeafDeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
return createAlreadyNormalized(context.deserializeLeaf(codedIn, stringCodec()));
}
}
}