aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/UnixOsPathPolicy.java
blob: 85766432675a24018d1f0f42119fa0e90559aaec (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
// 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 com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;

@VisibleForTesting
class UnixOsPathPolicy implements OsPathPolicy {

  static final UnixOsPathPolicy INSTANCE = new UnixOsPathPolicy();
  private static final Splitter PATH_SPLITTER = Splitter.onPattern("/+").omitEmptyStrings();

  @Override
  public int needsToNormalize(String path) {
    int n = path.length();
    int dotCount = 0;
    char prevChar = 0;
    for (int i = 0; i < n; i++) {
      char c = path.charAt(i);
      if (c == '\\') {
        return NEEDS_NORMALIZE;
      }
      if (c == '/') {
        if (prevChar == '/') {
          return NEEDS_NORMALIZE;
        }
        if (dotCount == 1 || dotCount == 2) {
          return NEEDS_NORMALIZE;
        }
      }
      dotCount = c == '.' ? dotCount + 1 : 0;
      prevChar = c;
    }
    if (prevChar == '/' || dotCount == 1 || dotCount == 2) {
      return NEEDS_NORMALIZE;
    }
    return NORMALIZED;
  }

  @Override
  public int needsToNormalizeSuffix(String normalizedSuffix) {
    // We know that the string is normalized
    // In this case only suffixes starting with ".." may cause
    // normalization once concatenated with other strings
    return normalizedSuffix.startsWith("..") ? NEEDS_NORMALIZE : NORMALIZED;
  }

  @Override
  public String normalize(String path, int normalizationLevel) {
    if (normalizationLevel == NORMALIZED) {
      return path;
    }
    if (path.isEmpty()) {
      return path;
    }
    boolean isAbsolute = path.charAt(0) == '/';
    StringBuilder sb = new StringBuilder(path.length());
    if (isAbsolute) {
      sb.append('/');
    }
    String[] segments = Iterables.toArray(PATH_SPLITTER.splitToList(path), String.class);
    int segmentCount = Utils.removeRelativePaths(segments, 0, isAbsolute);
    for (int i = 0; i < segmentCount; ++i) {
      sb.append(segments[i]);
      sb.append('/');
    }
    if (segmentCount > 0) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
  }

  @Override
  public int getDriveStrLength(String path) {
    if (path.length() == 0) {
      return 0;
    }
    return (path.charAt(0) == '/') ? 1 : 0;
  }

  @Override
  public int compare(String s1, String s2) {
    return s1.compareTo(s2);
  }

  @Override
  public int compare(char c1, char c2) {
    return Character.compare(c1, c2);
  }

  @Override
  public boolean equals(String s1, String s2) {
    return s1.equals(s2);
  }

  @Override
  public int hash(String s) {
    return s.hashCode();
  }

  @Override
  public boolean startsWith(String path, String prefix) {
    return path.startsWith(prefix);
  }

  @Override
  public boolean endsWith(String path, String suffix) {
    return path.endsWith(suffix);
  }

  @Override
  public char getSeparator() {
    return '/';
  }

  @Override
  public boolean isSeparator(char c) {
    return c == '/';
  }

  @Override
  public boolean isCaseSensitive() {
    return true;
  }
}