diff --git a/stages/org.osbuild.tar b/stages/org.osbuild.tar index 40663aac0..c38a4a823 100755 --- a/stages/org.osbuild.tar +++ b/stages/org.osbuild.tar @@ -12,6 +12,7 @@ def main(inputs, output_dir, options): tarfmt = options.get("format", "gnu") root_node = options.get("root-node", "include") paths = options.get("paths", []) + transform = options.get("transforms", []) extra_args = [] # Set environment variables for the tar operation. @@ -33,6 +34,10 @@ def main(inputs, output_dir, options): if options.get("sparse", False): extra_args += ["--sparse"] + transforms = options.get("transforms") + if transforms: + extra_args += [f"--transform={t}" for t in transforms] + # Set up the tar command. tar_cmd = [ "tar", diff --git a/stages/org.osbuild.tar.meta.json b/stages/org.osbuild.tar.meta.json index 0e0eaea53..58a1e061e 100644 --- a/stages/org.osbuild.tar.meta.json +++ b/stages/org.osbuild.tar.meta.json @@ -98,7 +98,14 @@ "description": "Make archive files sparse", "type": "boolean", "default": false - } + }, + "transforms": { + "type": "array", + "items": { + "type": "string", + "description": "Transformation of names during tar(1)" + } + } } }, "inputs": { diff --git a/stages/test/test_tar.py b/stages/test/test_tar.py index 10db07abe..4668d6e6a 100644 --- a/stages/test/test_tar.py +++ b/stages/test/test_tar.py @@ -19,11 +19,16 @@ "root-node": "include", "paths": ["file1"] }, "{'filename': 'out.tar', 'root-node': 'include', 'paths': ['file1']} is not valid under any of the given schemas"), + ({ + "filename": "foo", + "transforms": "not-an-array", + }, "'not-an-array' is not of type 'array'"), # good ({"filename": "out.tar", "root-node": "include"}, ""), ({"filename": "out.tar", "paths": ["file1"]}, ""), ({"filename": "out.tar", "sparse": True}, ""), ({"filename": "out.tar"}, ""), + ({"filename": "out.tar", "transforms": ["s/foo/bar"]}, ""), ]) def test_schema_validation_tar(stage_schema, test_data, expected_err): test_input = { @@ -99,3 +104,23 @@ def test_tar_paths(tmp_path, stage_module, fake_inputs): assert os.path.exists(tar_path) output = subprocess.check_output(["tar", "-tf", tar_path], encoding="utf-8").split("\n") assert ["file2", "file1", "file1", "file2", ""] == output + + +@pytest.mark.skipif(not has_executable("tar"), reason="no tar executable") +def test_tar_transforms(tmp_path, stage_module, fake_inputs): + options = { + "filename": "out.tar", + "paths": [ + "file1", + "file2", + ], + "transforms": [ + "s/file1/foo99/", + ], + } + stage_module.main(fake_inputs, tmp_path, options) + + tar_path = os.path.join(tmp_path, "out.tar") + assert os.path.exists(tar_path) + output = subprocess.check_output(["tar", "-tf", tar_path], encoding="utf-8").split("\n") + assert ["foo99", "file2"] == output