diff --git a/.github/scripts/install-android-sdk.py b/.github/scripts/install-android-sdk.py index ba0044fd1..3fddaa399 100644 --- a/.github/scripts/install-android-sdk.py +++ b/.github/scripts/install-android-sdk.py @@ -52,6 +52,26 @@ def valid(root, package): return False +def merge_licenses(source, destination): + """Keep runner-owned permissions and accepted licence hashes intact.""" + if not source.is_dir(): + return + destination.mkdir(exist_ok=True) + for license_file in source.iterdir(): + if not license_file.is_file(): + continue + target = destination / license_file.name + accepted = target.read_text().splitlines() if target.exists() else [] + additions = [line for line in license_file.read_text().splitlines() + if line.strip() and line not in accepted] + if not additions: + continue + # copytree/copy2 also chmod and set timestamps. Hosted SDK licences + # can be writable but owned by root, so metadata copying fails. + with target.open("a") as output: + output.write("\n" + "\n".join(additions) + "\n") + + def install(root, packages, manager, sleep=time.sleep): root = Path(root).resolve() manager = Path(manager).resolve(strict=True) @@ -81,8 +101,7 @@ def install(root, packages, manager, sleep=time.sleep): if destination.exists(): destination.rename(stage / "incomplete-component") source.rename(destination) - if (stage / "licenses").is_dir(): - shutil.copytree(stage / "licenses", root / "licenses", dirs_exist_ok=True) + merge_licenses(stage / "licenses", root / "licenses") if not valid(root, package): raise RuntimeError("SDK component failed validation after installation: " + package) break diff --git a/tests/android_sdk_install_test.py b/tests/android_sdk_install_test.py index 7e372593f..86b9837a5 100644 --- a/tests/android_sdk_install_test.py +++ b/tests/android_sdk_install_test.py @@ -74,6 +74,41 @@ shutil.copytree(root / 'download/cmake', stage / 'cmake') self.assertEqual(len(json.loads((self.root / "attempts.json").read_text())), 3) self.assertFalse(list(self.root.glob(".inbe-sdk-*"))) + def test_preinstalled_runner_license_metadata_is_never_overwritten(self): + licenses = self.root / "licenses" + licenses.mkdir() + license_file = licenses / "android-sdk-license" + license_file.write_text("already-accepted\n") + before = license_file.stat() + original = sdk.shutil.copystat + + def runner_owned_metadata(source, destination, **kwargs): + if Path(destination) in (licenses, license_file): + raise PermissionError("runner cannot chmod root-owned SDK licences") + return original(source, destination, **kwargs) + + with mock.patch.object(sdk.shutil, "copystat", side_effect=runner_owned_metadata): + sdk.install(self.root, [PACKAGE], self.manager, sleep=lambda _: None) + self.assertTrue(sdk.valid(self.root, PACKAGE)) + self.assertEqual(license_file.read_text(), "already-accepted\n") + self.assertEqual(license_file.stat().st_mtime_ns, before.st_mtime_ns) + self.assertEqual(license_file.stat().st_mode, before.st_mode) + + def test_newly_accepted_licenses_are_merged_without_removing_existing_hashes(self): + source = self.root / "incoming-licenses" + destination = self.root / "licenses" + source.mkdir() + destination.mkdir() + (source / "android-sdk-license").write_text("accepted-before\nnewly-accepted\n") + (destination / "android-sdk-license").write_text("accepted-before\n") + (source / "android-sdk-preview-license").write_text("preview-accepted\n") + sdk.merge_licenses(source, destination) + sdk.merge_licenses(source, destination) + self.assertEqual((destination / "android-sdk-license").read_text().split(), + ["accepted-before", "newly-accepted"]) + self.assertEqual((destination / "android-sdk-preview-license").read_text().split(), + ["preview-accepted"]) + def test_wrong_version_and_unsafe_package_path_are_rejected(self): component(self.root, "3.21.0") self.assertFalse(sdk.valid(self.root, PACKAGE))