Skip to content

Commit

Permalink
fix(tar): append slash to top-level directory mtree entries
Browse files Browse the repository at this point in the history
bsdtar's mtree format has a quirk wherein entries without "/" in their
first word are treated as "relative" entries, and "relative" directories
will cause tar to "change directory" into the declared directory entry.
If such a directory is followed by a "relative" entry, then the file
will be created within the directory, instead of at top-level as
expected. To mitigate, we append a slash to top-level directory entries.

Fixes bazel-contrib#851.
  • Loading branch information
sin-ack committed May 23, 2024
1 parent 474e680 commit db63eb8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/private/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ def _expand(file, expander, transform = to_repository_relative_path):
segments = path.split("/")
for i in range(1, len(segments)):
parent = "/".join(segments[:i])

# NOTE: The mtree format treats file paths without slashes as "relative" entries.
# If a relative entry is a directory, then it will "change directory" to that
# directory, and any subsequent "relative" entries will be created inside that
# directory. This causes issues when there is a top-level directory that is
# followed by a top-level file, as the file will be created inside the directory.
# To avoid this, we append a slash to the directory path to make it a "full" entry.
if i == 1:
parent += "/"

lines.append(_mtree_line(parent, "dir"))

lines.append(_mtree_line(_vis_encode(path), "file", content = _vis_encode(e.path)))
Expand Down

0 comments on commit db63eb8

Please sign in to comment.