From 221a7d08266c4a7ce4948ea209b9671be3085e73 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 28 Feb 2018 15:13:56 +0100 Subject: [PATCH] Clean up the server from temporary folder The server is copied to /data/local/tmp/scrcpy-server.jar and executed on the device. As soon as we are connected, we can unlink (rm) it from /data/local/tmp, to keep the device clean. --- app/src/command.c | 5 +++++ app/src/command.h | 1 + app/src/server.c | 19 ++++++++++++++++++- app/src/server.h | 12 +++++++----- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/command.c b/app/src/command.c index 20851b3c..6a5a38e4 100644 --- a/app/src/command.c +++ b/app/src/command.c @@ -66,6 +66,11 @@ process_t adb_push(const char *serial, const char *local, const char *remote) { return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); } +process_t adb_remove_path(const char *serial, const char *path) { + const char *const adb_cmd[] = {"shell", "rm", path}; + return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); +} + SDL_bool process_check_success(process_t proc, const char *name) { if (proc == PROCESS_NONE) { LOGE("Could not execute \"%s\"", name); diff --git a/app/src/command.h b/app/src/command.h index 8232f46c..5308aa6a 100644 --- a/app/src/command.h +++ b/app/src/command.h @@ -42,6 +42,7 @@ process_t adb_forward(const char *serial, uint16_t local_port, const char *devic process_t adb_reverse(const char *serial, const char *device_socket_name, uint16_t local_port); process_t adb_reverse_remove(const char *serial, const char *device_socket_name); process_t adb_push(const char *serial, const char *local, const char *remote); +process_t adb_remove_path(const char *serial, const char *path); // convenience function to wait for a successful process execution // automatically log process errors with the provided process name diff --git a/app/src/server.c b/app/src/server.c index c1ffa1f4..aa2009de 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -17,6 +17,8 @@ # define DEFAULT_SERVER_PATH PREFIX PREFIXED_SERVER_PATH #endif +#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar" + static const char *get_server_path(void) { const char *server_path = getenv("SCRCPY_SERVER_PATH"); if (!server_path) { @@ -26,10 +28,15 @@ static const char *get_server_path(void) { } static SDL_bool push_server(const char *serial) { - process_t process = adb_push(serial, get_server_path(), "/data/local/tmp/scrcpy-server.jar"); + process_t process = adb_push(serial, get_server_path(), DEVICE_SERVER_PATH); return process_check_success(process, "adb push"); } +static SDL_bool remove_server(const char *serial) { + process_t process = adb_remove_path(serial, DEVICE_SERVER_PATH); + return process_check_success(process, "adb shell rm"); +} + static SDL_bool enable_tunnel(const char *serial, Uint16 local_port) { process_t process = adb_reverse(serial, SOCKET_NAME, local_port); return process_check_success(process, "adb reverse"); @@ -81,6 +88,8 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po return SDL_FALSE; } + server->server_copied_to_device = SDL_TRUE; + if (!enable_tunnel(serial, local_port)) { return SDL_FALSE; } @@ -120,6 +129,10 @@ socket_t server_connect_to(struct server *server, const char *serial, Uint32 tim // we don't need the server socket anymore close_socket(&server->server_socket); + // the server is started, we can clean up the jar from the temporary folder + remove_server(serial); // ignore failure + server->server_copied_to_device = SDL_FALSE; + // we don't need the adb tunnel anymore disable_tunnel(serial); // ignore failure server->adb_reverse_enabled = SDL_FALSE; @@ -145,6 +158,10 @@ void server_stop(struct server *server, const char *serial) { // ignore failure disable_tunnel(serial); } + + if (server->server_copied_to_device) { + remove_server(serial); // ignore failure + } } void server_destroy(struct server *server) { diff --git a/app/src/server.h b/app/src/server.h index 95eb7d16..c2fe1125 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -9,13 +9,15 @@ struct server { socket_t server_socket; socket_t device_socket; SDL_bool adb_reverse_enabled; + SDL_bool server_copied_to_device; }; -#define SERVER_INITIALIZER { \ - .process = PROCESS_NONE, \ - .server_socket = INVALID_SOCKET, \ - .device_socket = INVALID_SOCKET, \ - .adb_reverse_enabled = SDL_FALSE, \ +#define SERVER_INITIALIZER { \ + .process = PROCESS_NONE, \ + .server_socket = INVALID_SOCKET, \ + .device_socket = INVALID_SOCKET, \ + .adb_reverse_enabled = SDL_FALSE, \ + .server_copied_to_device = SDL_FALSE, \ } // init default values