diff options
author | Kristina Chodorow <kchodorow@google.com> | 2016-05-26 20:25:54 +0000 |
---|---|---|
committer | Yue Gan <yueg@google.com> | 2016-05-27 08:45:30 +0000 |
commit | 108cee6a4f28a1caae6606fffc853ebd8d4582c1 (patch) | |
tree | 482bde2f1e0061e6d1dce71d934e683152630a23 /src/main/java/com/google/devtools/build/lib/bazel | |
parent | 3724d92fb85e021b6b65c3edb77c61572baedc25 (diff) |
Force paths to be relative when archives are decompressed
--
MOS_MIGRATED_REVID=123350350
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/bazel')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/StripPrefixedPath.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/StripPrefixedPath.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/StripPrefixedPath.java index 77716dc4f2..46650d6150 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/StripPrefixedPath.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/StripPrefixedPath.java @@ -16,6 +16,7 @@ package com.google.devtools.build.lib.bazel.repository; import com.google.common.base.Optional; import com.google.devtools.build.lib.concurrent.ThreadSafety; +import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.vfs.PathFragment; /** @@ -27,14 +28,25 @@ public final class StripPrefixedPath { private final boolean found; private final boolean skip; + /** + * If a prefix is given, it will be removed from the entry's path. This also turns absolute paths + * into relative paths (e.g., /usr/bin/bash will become usr/bin/bash, same as unzip's default + * behavior) and normalizes the paths (foo/../bar////baz will become bar/baz). Note that this + * could cause collisions, if a zip file had one entry for bin/some-binary and another entry for + * /bin/some-binary. + * + * Note that the prefix is stripped to move the files up one level, so if you have an entry + * "foo/../bar" and a prefix of "foo", the result will be "bar" not "../bar". + */ public static StripPrefixedPath maybeDeprefix(String entry, Optional<String> prefix) { - boolean found = false; - PathFragment entryPath = new PathFragment(entry); + Preconditions.checkNotNull(entry); + PathFragment entryPath = relativize(entry); if (!prefix.isPresent()) { return new StripPrefixedPath(entryPath, false, false); } - PathFragment prefixPath = new PathFragment(prefix.get()); + PathFragment prefixPath = relativize(prefix.get()); + boolean found = false; boolean skip = false; if (entryPath.startsWith(prefixPath)) { found = true; @@ -48,6 +60,18 @@ public final class StripPrefixedPath { return new StripPrefixedPath(entryPath, found, skip); } + /** + * Normalize the path and, if it is absolute, make it relative (e.g., /foo/bar becomes foo/bar). + */ + private static PathFragment relativize(String path) { + PathFragment entryPath = new PathFragment(path).normalize(); + if (entryPath.isAbsolute()) { + entryPath = new PathFragment(entryPath.getSafePathString().substring( + entryPath.windowsVolume().length() + 1)); + } + return entryPath; + } + private StripPrefixedPath(PathFragment pathFragment, boolean found, boolean skip) { this.pathFragment = pathFragment; this.found = found; |