Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add symlink support to pkg_zip #499

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.bazeliskrc
.ijwb
bazel-*

6 changes: 6 additions & 0 deletions pkg/private/zip/build_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# Unix dir bit and Windows dir bit. Magic from zip spec
UNIX_DIR_BIT = 0o40000
aiuto marked this conversation as resolved.
Show resolved Hide resolved
MSDOS_DIR_BIT = 0x10
aiuto marked this conversation as resolved.
Show resolved Hide resolved
UNIX_SYMLINK_BIT = 0o120000
aiuto marked this conversation as resolved.
Show resolved Hide resolved

def _create_argument_parser():
"""Creates the command line arg parser."""
Expand Down Expand Up @@ -108,6 +109,11 @@ def _add_manifest_entry(options, zip_file, entry, default_mode, ts):
# Set directory bits
entry_info.external_attr |= (UNIX_DIR_BIT << 16) | MSDOS_DIR_BIT
zip_file.writestr(entry_info, '')
elif entry_type == manifest.ENTRY_IS_LINK:
entry_info.compress_type = zipfile.ZIP_STORED
# Set directory bits
entry_info.external_attr |= (UNIX_SYMLINK_BIT << 16)
zip_file.writestr(entry_info, src)
# TODO(#309): All the rest

def main(args):
Expand Down
13 changes: 12 additions & 1 deletion tests/zip/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# -*- coding: utf-8 -*-

load("//pkg:mappings.bzl", "pkg_attributes", "pkg_mkdirs")
load("//pkg:mappings.bzl", "pkg_attributes", "pkg_mkdirs", "pkg_mklink")
load("//pkg:zip.bzl", "pkg_zip")
load("//tests/util:defs.bzl", "directory", "fake_artifact")
load("@rules_python//python:defs.bzl", "py_test")
Expand Down Expand Up @@ -42,6 +42,15 @@ pkg_mkdirs(
],
)

pkg_mklink(
name = "link",
target = "/usr/local/foo/foo.real",
link_name = "/usr/bin/foo",
attributes = pkg_attributes(
mode = "555",
),
)

directory(
name = "generate_tree",
contents = "hello there",
Expand Down Expand Up @@ -79,6 +88,7 @@ pkg_zip(
"//tests:testdata/hello.txt",
"//tests:testdata/loremipsum.txt",
":dirs",
":link",
],
)

Expand Down Expand Up @@ -107,6 +117,7 @@ pkg_zip(
"//tests:testdata/hello.txt",
"//tests:testdata/loremipsum.txt",
":dirs",
":link",
],
timestamp = 0,
)
Expand Down
4 changes: 3 additions & 1 deletion tests/zip/zip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Unix dir bit and Windows dir bit. Magic from zip spec
UNIX_DIR_BIT = 0o40000
MSDOS_DIR_BIT = 0x10
UNIX_RWX_BITS = 0o777
aiuto marked this conversation as resolved.
Show resolved Hide resolved

# The ZIP epoch date: (1980, 1, 1, 0, 0, 0)
_ZIP_EPOCH_DT = datetime.datetime(1980, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)
Expand Down Expand Up @@ -76,7 +77,7 @@ def assertZipFileContent(self, zip_file, content):
expect_dir_bits = UNIX_DIR_BIT << 16 | MSDOS_DIR_BIT
self.assertEqual(info.external_attr & expect_dir_bits,
expect_dir_bits)
self.assertEqual(info.external_attr >> 16 & ~UNIX_DIR_BIT,
self.assertEqual((info.external_attr >> 16) & UNIX_RWX_BITS,
expected.get("attr", 0o555))

def test_empty(self):
Expand All @@ -87,6 +88,7 @@ def test_basic(self):
{"filename": "foodir/", "isdir": True, "attr": 0o711},
{"filename": "hello.txt", "crc": HELLO_CRC},
{"filename": "loremipsum.txt", "crc": LOREM_CRC},
{"filename": "usr/bin/foo", "attr": 0o555, "data": "/usr/local/foo/foo.real"},
])

def test_timestamp(self):
Expand Down