From 50f9c26927f703b297c308df0d4993d21a54c0fa Mon Sep 17 00:00:00 2001 From: aiuto Date: Fri, 24 Dec 2021 16:20:13 -0500 Subject: [PATCH 1/3] add symlink support to pkg_zip. #309 --- pkg/private/zip/build_zip.py | 6 ++++++ tests/zip/BUILD | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/private/zip/build_zip.py b/pkg/private/zip/build_zip.py index 1b26a1ba..5d992a74 100644 --- a/pkg/private/zip/build_zip.py +++ b/pkg/private/zip/build_zip.py @@ -27,6 +27,7 @@ # Unix dir bit and Windows dir bit. Magic from zip spec UNIX_DIR_BIT = 0o40000 MSDOS_DIR_BIT = 0x10 +UNIX_SYMLINK_BIT = 0o120000 def _create_argument_parser(): """Creates the command line arg parser.""" @@ -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): diff --git a/tests/zip/BUILD b/tests/zip/BUILD index 0376f208..57185f2d 100644 --- a/tests/zip/BUILD +++ b/tests/zip/BUILD @@ -42,6 +42,12 @@ pkg_mkdirs( ], ) +pkg_mklink( + name = "link", + src = "/usr/local/foo/foo.real", + dest = "/usr/bin/foo", +) + directory( name = "generate_tree", contents = "hello there", @@ -79,6 +85,7 @@ pkg_zip( "//tests:testdata/hello.txt", "//tests:testdata/loremipsum.txt", ":dirs", + ":link", ], ) From 6755ca2099b30b45a403575c2eeebef4056b55ca Mon Sep 17 00:00:00 2001 From: aiuto Date: Sat, 25 Dec 2021 00:29:12 -0500 Subject: [PATCH 2/3] Make the test better. --- tests/zip/BUILD | 4 ++++ tests/zip/zip_test.py | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/zip/BUILD b/tests/zip/BUILD index 57185f2d..40bf72ce 100644 --- a/tests/zip/BUILD +++ b/tests/zip/BUILD @@ -46,6 +46,9 @@ pkg_mklink( name = "link", src = "/usr/local/foo/foo.real", dest = "/usr/bin/foo", + attributes = pkg_attributes( + mode = "555", + ), ) directory( @@ -114,6 +117,7 @@ pkg_zip( "//tests:testdata/hello.txt", "//tests:testdata/loremipsum.txt", ":dirs", + ":link", ], timestamp = 0, ) diff --git a/tests/zip/zip_test.py b/tests/zip/zip_test.py index 27f58902..7c7720b3 100644 --- a/tests/zip/zip_test.py +++ b/tests/zip/zip_test.py @@ -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 # 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) @@ -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): @@ -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): From 31eeed82c9b94c14c3fa2db77e759be429b851f3 Mon Sep 17 00:00:00 2001 From: aiuto Date: Sat, 29 Jan 2022 00:06:08 -0500 Subject: [PATCH 3/3] update tests to new mklink attributes --- .gitignore | 2 +- tests/zip/BUILD | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 8eb9ccc5..716b1b36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ +.bazeliskrc .ijwb bazel-* - diff --git a/tests/zip/BUILD b/tests/zip/BUILD index 51cb5963..7244718f 100644 --- a/tests/zip/BUILD +++ b/tests/zip/BUILD @@ -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") @@ -44,8 +44,8 @@ pkg_mkdirs( pkg_mklink( name = "link", - src = "/usr/local/foo/foo.real", - dest = "/usr/bin/foo", + target = "/usr/local/foo/foo.real", + link_name = "/usr/bin/foo", attributes = pkg_attributes( mode = "555", ),