Skip to content

Commit

Permalink
Add symlink support to pkg_zip (#499)
Browse files Browse the repository at this point in the history
* add symlink support to pkg_zip.  #309
* update tests to new mklink attributes
  • Loading branch information
aiuto authored Jan 29, 2022
1 parent e173e40 commit c969557
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
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
MSDOS_DIR_BIT = 0x10
UNIX_SYMLINK_BIT = 0o120000

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

# 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

0 comments on commit c969557

Please sign in to comment.