From 1730f69461cc758d016508d9d0f0db693cb355e4 Mon Sep 17 00:00:00 2001
From: Douglas Raillard <douglas.raillard@arm.com>
Date: Fri, 4 Aug 2023 11:53:50 +0100
Subject: [PATCH] target: Avoid intermittent error when installing binary

Installing busybox sometimes fails with:

    cp: /data/local/tmp/bin/busybox: Text file busy

This happens when trying to modify a binary file while a process is
still running (e.g. unclean previous disconnection).

Fix that by using the -f option, which will remove the destination file
first and retry the copy in case of failure.
---
 devlib/target.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/devlib/target.py b/devlib/target.py
index 811fc3b..8624aab 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -2134,7 +2134,7 @@ class AndroidTarget(Target):
         on_device_executable = self.path.join(self.executables_directory, executable_name)
         await self.push.asyn(filepath, on_device_file, timeout=timeout)
         if on_device_file != on_device_executable:
-            await self.execute.asyn('cp {} {}'.format(quote(on_device_file), quote(on_device_executable)),
+            await self.execute.asyn('cp -f -- {} {}'.format(quote(on_device_file), quote(on_device_executable)),
                          as_root=self.needs_su, timeout=timeout)
             await self.remove.asyn(on_device_file, as_root=self.needs_su)
         await self.execute.asyn("chmod 0777 {}".format(quote(on_device_executable)), as_root=self.needs_su)