Fabian Meumertzheim | 73ed46c | 2023-06-20 08:39:48 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.common.base.Preconditions; |
| 18 | import com.google.devtools.build.lib.actions.Artifact; |
| 19 | import com.google.devtools.build.lib.starlarkbuildapi.SymlinkEntryApi; |
| 20 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 21 | import net.starlark.java.eval.Printer; |
| 22 | |
| 23 | /** |
| 24 | * An entry in the runfiles map. |
| 25 | * |
| 26 | * <p>build-runfiles.cc enforces the following constraints: The PathFragment must not be an absolute |
| 27 | * path, nor contain "..". Overlapping runfiles links are also refused. This is the case where you |
| 28 | * ask to create a link to "foo" and also "foo/bar.txt". I.e. you're asking it to make "foo" both a |
| 29 | * file (symlink) and a directory. |
| 30 | * |
| 31 | * <p>Links to directories are heavily discouraged. |
| 32 | */ |
| 33 | // |
| 34 | // O intrepid fixer or bugs and implementor of features, dare not to add a .equals() method |
| 35 | // to this class, lest you condemn yourself, or a fellow other developer to spending two |
| 36 | // delightful hours in a fancy hotel on a Chromebook that is utterly unsuitable for Java |
| 37 | // development to figure out what went wrong, just like I just did. |
| 38 | // |
| 39 | // The semantics of the symlinks nested set dictates that later entries overwrite earlier |
| 40 | // ones. However, the semantics of nested sets dictate that if there are duplicate entries, they |
| 41 | // are only returned once in the iterator. |
| 42 | // |
| 43 | // These two things, innocent when taken alone, result in the effect that when there are three |
| 44 | // entries for the same path, the first one and the last one the same, and the middle one |
| 45 | // different, the *middle* one will take effect: the middle one overrides the first one, and the |
| 46 | // first one prevents the last one from appearing on the iterator. |
| 47 | // |
| 48 | // The lack of a .equals() method prevents this by making the first entry in the above case not |
| 49 | // equal to the third one if they are not the same instance (which they almost never are). |
| 50 | // |
| 51 | // Goodnight, prince(ss)?, and sweet dreams. |
| 52 | public final class SymlinkEntry implements SymlinkEntryApi { |
| 53 | private final PathFragment path; |
| 54 | private final Artifact artifact; |
| 55 | |
| 56 | SymlinkEntry(PathFragment path, Artifact artifact) { |
| 57 | this.path = Preconditions.checkNotNull(path); |
| 58 | this.artifact = Preconditions.checkNotNull(artifact); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String getPathString() { |
| 63 | return path.getPathString(); |
| 64 | } |
| 65 | |
| 66 | public PathFragment getPath() { |
| 67 | return path; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Artifact getArtifact() { |
| 72 | return artifact; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean isImmutable() { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void repr(Printer printer) { |
| 82 | printer.append("SymlinkEntry(path = "); |
| 83 | printer.repr(getPathString()); |
| 84 | printer.append(", target_file = "); |
| 85 | artifact.repr(printer); |
| 86 | printer.append(")"); |
| 87 | } |
| 88 | } |