From aba91f16216e9b3774a2d9f08eda4b8d11596989 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 7 May 2024 16:33:26 -0700 Subject: [PATCH] fix(tar): handle spaces in input filenames This is a partial solution to #794 that covers the most common case needing escaping. See #795 for a principled but blocked fix that requires a BSD vis executable --- lib/private/tar.bzl | 6 +++++- lib/tests/tar/BUILD.bazel | 4 ++++ lib/tests/tar/srcdir/space in name.txt | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 lib/tests/tar/srcdir/space in name.txt diff --git a/lib/private/tar.bzl b/lib/private/tar.bzl index 0bc8a2e35..89f7ad394 100644 --- a/lib/private/tar.bzl +++ b/lib/private/tar.bzl @@ -183,6 +183,10 @@ def _to_rlocation_path(file, workspace): else: return workspace + "/" + file.short_path +def _vis_encode(filename): + # TODO(#794): correctly encode all filenames by using vis(3) (or porting it) + return filename.replace(" ", "\\040") + def _expand(file, expander, transform = to_repository_relative_path): expanded = expander.expand(file) lines = [] @@ -193,7 +197,7 @@ def _expand(file, expander, transform = to_repository_relative_path): parent = "/".join(segments[:i]) lines.append(_mtree_line(parent, "dir")) - lines.append(_mtree_line(path, "file", content = e.path)) + lines.append(_mtree_line(_vis_encode(path), "file", content = _vis_encode(e.path))) return lines def _mtree_impl(ctx): diff --git a/lib/tests/tar/BUILD.bazel b/lib/tests/tar/BUILD.bazel index 497f70782..1ec89762b 100644 --- a/lib/tests/tar/BUILD.bazel +++ b/lib/tests/tar/BUILD.bazel @@ -217,6 +217,8 @@ tar( "treeartifact", ], out = "7.tar", + # When running remote, BB lays out files with inodes that mtree optimizes into a hardlink + tags = ["no-remote-exec"], ) assert_tar_listing( @@ -229,9 +231,11 @@ assert_tar_listing( "drwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/srcdir/", "-rwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/srcdir/info", "-rwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/srcdir/pkg", + "-rwxr-xr-x 0 0 0 1 Jan 1 2023 lib/tests/tar/srcdir/space in name.txt", "drwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/treeartifact/", "-rwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/treeartifact/info", "-rwxr-xr-x 0 0 0 0 Jan 1 2023 lib/tests/tar/treeartifact/pkg", + "-rwxr-xr-x 0 0 0 1 Jan 1 2023 lib/tests/tar/treeartifact/space in name.txt", ], ) diff --git a/lib/tests/tar/srcdir/space in name.txt b/lib/tests/tar/srcdir/space in name.txt new file mode 100644 index 000000000..9cbe6ea56 --- /dev/null +++ b/lib/tests/tar/srcdir/space in name.txt @@ -0,0 +1 @@ +e \ No newline at end of file