aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
blob: 5f7fa0a8de0d4622d5f0228da5a055353d2fa7ec (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
// Copyright 2015 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.cmdline;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.google.devtools.build.lib.util.StringCanonicalizer;
import com.google.devtools.build.lib.util.StringUtilities;
import com.google.devtools.build.lib.vfs.Canonicalizer;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

import javax.annotation.concurrent.Immutable;

/**
 * Uniquely identifies a package, given a repository name and a package's path fragment.
 *
 * <p>The repository the build is happening in is the <i>default workspace</i>, and is identified
 * by the workspace name "". Other repositories can be named in the WORKSPACE file.  These
 * workspaces are prefixed by {@literal @}.</p>
 */
@Immutable
public final class PackageIdentifier implements Comparable<PackageIdentifier>, Serializable {

  private static final Interner<PackageIdentifier> INTERNER = Interners.newWeakInterner();

  public static PackageIdentifier create(String repository, PathFragment pkgName)
      throws LabelSyntaxException {
    return create(RepositoryName.create(repository), pkgName);
  }

  public static PackageIdentifier create(RepositoryName repository, PathFragment pkgName) {
    return INTERNER.intern(new PackageIdentifier(repository, pkgName));
  }

  /**
   * A human-readable name for the repository.
   */
  public static final class RepositoryName implements Serializable {
    /** Helper for serializing {@link RepositoryName}. */
    private static final class SerializationProxy implements Serializable {
      private RepositoryName repositoryName;

      private SerializationProxy(RepositoryName repositoryName) {
        this.repositoryName = repositoryName;
      }

      private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeObject(repositoryName.toString());
      }

      private void readObject(ObjectInputStream in)
          throws IOException, ClassNotFoundException {
        try {
          repositoryName = RepositoryName.create((String) in.readObject());
        } catch (LabelSyntaxException e) {
          throw new IOException("Error serializing repository name: " + e.getMessage());
        }
      }

      @SuppressWarnings("unused")
      private void readObjectNoData() throws ObjectStreamException {
      }

      private Object readResolve() {
        return repositoryName;
      }
    }

    private void readObject(ObjectInputStream in) throws IOException {
      throw new IOException("Serialization is allowed only by proxy");
    }

    private Object writeReplace() {
      return new SerializationProxy(this);
    }

    private static final LoadingCache<String, RepositoryName> repositoryNameCache =
        CacheBuilder.newBuilder()
          .weakValues()
          .build(
              new CacheLoader<String, RepositoryName> () {
                @Override
                public RepositoryName load(String name) throws LabelSyntaxException {
                  String errorMessage = validate(name);
                  if (errorMessage != null) {
                    errorMessage = "invalid repository name '"
                        + StringUtilities.sanitizeControlChars(name) + "': " + errorMessage;
                    throw new LabelSyntaxException(errorMessage);
                  }
                  return new RepositoryName(StringCanonicalizer.intern(name));
                }
              });

    /**
     * Makes sure that name is a valid repository name and creates a new RepositoryName using it.
     * @throws TargetParsingException if the name is invalid.
     */
    public static RepositoryName create(String name) throws LabelSyntaxException {
      try {
        return repositoryNameCache.get(name);
      } catch (ExecutionException e) {
        Throwables.propagateIfInstanceOf(e.getCause(), LabelSyntaxException.class);
        throw new IllegalStateException("Failed to create RepositoryName from " + name, e);
      }
    }

    private final String name;

    private RepositoryName(String name) {
      this.name = name;
    }

    private static class Lexer {
      private static final char EOF = '\0';

      private final String name;
      private int pos;

      public Lexer(String name) {
        this.name = name;
        this.pos = 0;
      }

      public String lex() {
        if (name.isEmpty()) {
          return null;
        }

        if (name.charAt(pos) != '@') {
          return "workspace names must start with '@'";
        }

        // @// is valid.
        if (name.length() == 1) {
          return null;
        }

        pos++;
        // Disallow strings starting with "/",  "./",  or "../"
        // Disallow strings identical to        ".",   or ".."
        if (name.charAt(pos) == '/') {
          return "workspace names are not allowed to start with '@/'";
        } else if (name.charAt(pos) == '.') {
          char next = peek(1);
          char nextNext = peek(2);
          // Forbid '@.' and '@..' as complete labels and '@./' and '@../' as label starts.
          if (next == EOF) {
            return "workspace names are not allowed to be '@.'";
          } else if (next == '/') {
            return "workspace names are not allowed to start with '@./'";
          } else if (next == '.' && (nextNext == '/' || nextNext == EOF)) {
            return "workspace names are not allowed to start with '@..'";
          }
        }

        // This lexes the first letter a second time, to make sure it fulfills the general
        // workspace name criteria (as well as the more strict criteria for the beginning of a
        // workspace name).
        // Disallow strings containing    "//", "/./", or "/../"
        // Disallow strings ending in     "/",  "/.",   or "/.."
        // name = @( <alphanum> | [/._-] )*
        for (; pos < name.length(); pos++) {
          char c = name.charAt(pos);
          if (c == '/') {
            char next = peek(1);
            if (next == '/') {
              return "workspace names are not allowed to contain '//'";
            } else if (next == EOF) {
              return "workspace names are not allowed to end with '/'";
            } else if (next == '.' && (peek(2) == '/' || peek(2) == EOF)) {
              return "workspace names are not allowed to contain '/./'";
            } else if (next == '.' && peek(2) == '.' && (peek(3) == '/' || peek(3) == EOF)) {
              return "workspace names are not allowed to contain '/../'";
            }
          } else if ((c < 'a' || c > 'z') && c != '_' && c != '-' && c != '/' && c != '.'
              && (c < '0' || c > '9') && (c < 'A' || c > 'Z')) {
            return "workspace names may contain only A-Z, a-z, 0-9, '-', '_', '.', and '/'";
          }
        }

        return null;
      }

      private char peek(int num) {
        if (pos + num >= name.length()) {
          return EOF;
        }
        return name.charAt(pos + num);
      }
    }

    /**
     * Performs validity checking.  Returns null on success, an error message otherwise.
     */
    private static String validate(String name) {
      return new Lexer(name).lex();
    }

    /**
     * Returns the repository name without the leading "{@literal @}".  For the default repository,
     * returns "".
     */
    public String strippedName() {
      if (name.isEmpty()) {
        return name;
      }
      return name.substring(1);
    }

    /**
     * Returns if this is the default repository, that is, {@link #name} is "".
     */
    public boolean isDefault() {
      return name.isEmpty();
    }

    /**
     * Returns the repository name, with leading "{@literal @}" (or "" for the default repository).
     */
    // TODO(bazel-team): Use this over toString()- easier to track its usage.
    public String getName() {
      return name;
    }

    /**
     * Returns the repository name, with leading "{@literal @}" (or "" for the default repository).
     */
    @Override
    public String toString() {
      return name;
    }

    @Override
    public boolean equals(Object object) {
      if (this == object) {
        return true;
      }
      if (!(object instanceof RepositoryName)) {
        return false;
      }
      return name.equals(((RepositoryName) object).name);
    }

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

  public static final String DEFAULT_REPOSITORY = "";
  public static final RepositoryName DEFAULT_REPOSITORY_NAME;
  public static final RepositoryName MAIN_REPOSITORY_NAME;

  static {
    try {
      DEFAULT_REPOSITORY_NAME = RepositoryName.create(DEFAULT_REPOSITORY);
      MAIN_REPOSITORY_NAME = RepositoryName.create("@");
    } catch (LabelSyntaxException e) {
      throw new IllegalStateException(e);
    }
  }

  // Temporary factory for identifiers without explicit repositories.
  // TODO(bazel-team): remove all usages of this.
  public static PackageIdentifier createInDefaultRepo(String name) {
    return createInDefaultRepo(new PathFragment(name));
  }

  public static PackageIdentifier createInDefaultRepo(PathFragment name) {
    try {
      return create(DEFAULT_REPOSITORY, name);
    } catch (LabelSyntaxException e) {
      throw new IllegalArgumentException("could not create package identifier for " + name
          + ": " + e.getMessage());
    }
  }

  /**
   * The identifier for this repository. This is either "" or prefixed with an "@",
   * e.g., "@myrepo".
   */
  private final RepositoryName repository;

  /** The name of the package. Canonical (i.e. x.equals(y) <=> x==y). */
  private final PathFragment pkgName;

  private PackageIdentifier(RepositoryName repository, PathFragment pkgName) {
    Preconditions.checkNotNull(repository);
    Preconditions.checkNotNull(pkgName);
    this.repository = repository;
    this.pkgName = Canonicalizer.fragments().intern(pkgName.normalize());
  }

  public static PackageIdentifier parse(String input) throws LabelSyntaxException {
    String repo;
    String packageName;
    int packageStartPos = input.indexOf("//");
    if (input.startsWith("@") && packageStartPos > 0) {
      repo = input.substring(0, packageStartPos);
      packageName = input.substring(packageStartPos + 2);
    } else if (input.startsWith("@")) {
      throw new LabelSyntaxException("invalid package name '" + input + "'");
    } else if (packageStartPos == 0) {
      repo = PackageIdentifier.DEFAULT_REPOSITORY;
      packageName = input.substring(2);
    } else {
      repo = PackageIdentifier.DEFAULT_REPOSITORY;
      packageName = input;
    }

    String error = RepositoryName.validate(repo);
    if (error != null) {
      throw new LabelSyntaxException(error);
    }

    error = LabelValidator.validatePackageName(packageName);
    if (error != null) {
      throw new LabelSyntaxException(error);
    }

    return create(repo, new PathFragment(packageName));
  }

  public RepositoryName getRepository() {
    return repository;
  }

  public PathFragment getPackageFragment() {
    return pkgName;
  }

  /**
   * Returns a relative path that should be unique across all remote and packages, based on the
   * repository and package names.
   */
  public PathFragment getPathFragment() {
    return repository.isDefault() ? pkgName
        : new PathFragment("external").getRelative(repository.strippedName())
            .getRelative(pkgName);
  }

  /**
   * Returns the name of this package.
   *
   * <p>There are certain places that expect the path fragment as the package name ('foo/bar') as a
   * package identifier. This isn't specific enough for packages in other repositories, so their
   * stringified version is '@baz//foo/bar'.</p>
   */
  @Override
  public String toString() {
    return (repository.isDefault() ? "" : repository + "//") + pkgName;
  }

  @Override
  public boolean equals(Object object) {
    if (this == object) {
      return true;
    }
    if (!(object instanceof PackageIdentifier)) {
      return false;
    }
    PackageIdentifier that = (PackageIdentifier) object;
    return pkgName.equals(that.pkgName) && repository.equals(that.repository);
  }

  @Override
  public int hashCode() {
    return Objects.hash(repository, pkgName);
  }

  @Override
  public int compareTo(PackageIdentifier that) {
    return ComparisonChain.start()
        .compare(repository.toString(), that.repository.toString())
        .compare(pkgName, that.pkgName)
        .result();
  }
}