Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
tools: b64push.py copies the verified file when the TV refuses the move (#1588)
Mazin Kanuga51e27f6e466c
diff --git a/tests/test_b64push_atomic.py b/tests/test_b64push_atomic.pyindex 5af066140..537906159 100644--- a/tests/test_b64push_atomic.py+++ b/tests/test_b64push_atomic.py@@ -31,6 +31,16 @@ class Device:raise OSError("simulated transport failure")if os.environ.get("FAKE_CORRUPT") and command.startswith("echo '"):command = "echo 'QUJD' >>" + command.split(">>", 1)[1]+ # SELinux refusing rename(2) in /data/local/tmp while cp works, in toybox's words (#1588).+ if os.environ.get("FAKE_NO_RENAME") and command.startswith("mv "):+ return "mv: bad '%s': Permission denied\n" % command.split("'")[1]+ if os.environ.get("FAKE_NO_COPY") and command.startswith("cp "):+ return "cp: %s: Permission denied\n" % command.split("'")[3]+ # A connection that drops while cp is writing: the target is cut short, then the transport raises.+ if os.environ.get("FAKE_PARTIAL_COPY") and command.startswith("cp "):+ with open(command.split("'")[1], "rb") as src, open(command.split("'")[3], "wb") as dst:+ dst.write(src.read(int(os.environ["FAKE_PARTIAL_COPY"])))+ raise OSError("simulated connection drop mid-copy")return subprocess.run(["/bin/sh", "-c", command], capture_output=True, text=True).stdoutdef close(self):@@ -143,3 +153,57 @@ class B64PushTest(TempDirTest):self.assertIn(hashlib.md5(b"").hexdigest(), result.stdout)self.assertEqual(sorted(p.name for p in self.remote_dir.iterdir()), ["empty.txt"])+ def test_a_refused_move_copies_the_verified_file_and_checks_it_again(self):+ # TCL ak30a5 on 20250610 refuses mv in /data/local/tmp, while cp works there (#1588).+ data = os.urandom(10000)+ target = self.remote_dir / "file.apk"+ for before in (b"old contents", None):+ with self.subTest(target_existed=before is not None):+ if before is None:+ target.unlink()+ else:+ target.write_bytes(before)+ self.log.unlink(missing_ok=True)+ result = self.push(str(self.local(data)), str(target), FAKE_NO_RENAME="1")+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertEqual(target.read_bytes(), data)+ self.assertEqual(sorted(p.name for p in self.remote_dir.iterdir()), ["file.apk"],+ "temporary files are cleaned up")+ copies = [c for c in self.commands() if c.startswith("cp ")]+ self.assertEqual(len(copies), 1, self.commands())+ self.assertIn(f"md5sum '{target}'", copies[0], "the copy on the target is checked again")+ self.assertIn("Permission denied", result.stdout, "the output says why it copied")++ def test_a_move_that_works_is_not_followed_by_a_copy(self):+ target = self.remote_dir / "file.apk"+ result = self.push(str(self.local(b"new contents")), str(target))+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertEqual([c for c in self.commands() if c.startswith("cp ")], [])++ def test_a_refused_move_and_a_failed_copy_exit_1_and_clear_staging(self):+ for env in ({"FAKE_NO_COPY": "1"}, {"FAKE_RAISE": "cp -f"}):+ with self.subTest(env=env):+ target = self.remote_dir / "file.apk"+ target.write_bytes(b"old contents")+ result = self.push(str(self.local(b"new contents")), str(target),+ FAKE_NO_RENAME="1", **env)+ self.assertEqual(result.returncode, 1, result.stdout + result.stderr)+ self.assertEqual(target.read_bytes(), b"old contents", "the fake refused before writing")+ self.assertEqual(sorted(p.name for p in self.remote_dir.iterdir()), ["file.apk"])+ self.assertIn(f"bad '{target}.part': Permission denied", result.stderr)+ self.assertIn(f"{target} may be incomplete", result.stderr)+ if "FAKE_NO_COPY" in env:+ self.assertIn(f"cp: {target}: Permission denied", result.stderr)++ def test_a_copy_interrupted_part_way_says_the_target_may_be_incomplete(self):+ # The connection drops after cp has written half the file: the target is cut short.+ data = os.urandom(10000)+ target = self.remote_dir / "file.apk"+ target.write_bytes(b"old contents")+ result = self.push(str(self.local(data)), str(target), FAKE_NO_RENAME="1", FAKE_PARTIAL_COPY="5000")+ self.assertEqual(result.returncode, 1, result.stdout + result.stderr)+ self.assertEqual(target.read_bytes(), data[:5000], "the fake cut the copy short")+ self.assertEqual(sorted(p.name for p in self.remote_dir.iterdir()), ["file.apk"])+ self.assertIn(f"bad '{target}.part': Permission denied", result.stderr)+ self.assertIn(f"{target} may be incomplete", result.stderr)+ self.assertIn("simulated connection drop mid-copy", result.stderr, "the transport error still shows")diff --git a/tools/b64push.py b/tools/b64push.pyindex fa593594c..66f3652b6 100644--- a/tools/b64push.py+++ b/tools/b64push.py@@ -5,9 +5,12 @@ Usage: b64push.py <local file> <remote path>The file is assembled under a temporary name next to the target and checked with md5sum; only amatching copy is moved over the target, so a failed or corrupted transfer leaves the old file alone.+Where the TV refuses that move (some firmware refuses rename(2) in /data/local/tmp, #1588), the+verified file is copied over the target and checked again. That copy is not atomic.-Exit status: 0 the copy is on the TV, 1 a transfer failed (the target is as it was), 2 the-arguments were refused, so nothing was sent and no TV was opened.+Exit status: 0 the copy is on the TV, 1 a transfer failed (the target is as it was, unless that+copy failed or was interrupted, which the message says), 2 the arguments were refused, so nothing+was sent and no TV was opened."""import base64import hashlib@@ -91,9 +94,28 @@ def main():return 1out = dev.shell(f"mv -f {q(part)} {q(remote)} && echo moved", read_timeout_s=60.0)if "moved" not in out:- dev.shell(f"rm -f {q(part)}")- print(f"could not move the verified copy over {remote}: {out.strip()}", file=sys.stderr)- return 1+ # SELinux on a TCL ak30a5 (20250610) refuses rename(2) in /data/local/tmp while cp works+ # there (#1588). Copy the verified file over the target instead and check that copy too.+ # Unlike the move it is not atomic: a copy that stops part-way can leave the target+ # incomplete.+ refused = out.strip()+ try:+ out = dev.shell(f"cp -f {q(part)} {q(remote)} && md5sum {q(remote)}", read_timeout_s=120.0)+ except BaseException:+ # A connection that drops, a read that times out or Ctrl-C while cp is writing can+ # leave the target cut short. Say so, then let the transport error through.+ print(f"could not move the verified copy over {remote}: {refused}", file=sys.stderr)+ print(f"copying it over instead was interrupted. {remote} may be incomplete.",+ file=sys.stderr)+ raise+ got = out.split()[0] if out.strip() else ""+ if got != want:+ dev.shell(f"rm -f {q(part)}")+ print(f"could not move the verified copy over {remote}: {refused}", file=sys.stderr)+ print(f"copying it over instead did not give a copy with md5 {want}: {out.strip()}. "+ f"{remote} may be incomplete.", file=sys.stderr)+ return 1+ print(f"the move was refused ({refused}), so the file was copied: md5 {got} MATCH")return 0finally:# Exceptions (including interrupted transfers) must also clear staging. A disconnected TV