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.
This commit is contained in:
Romain Vimont 2018-02-28 15:13:56 +01:00
parent 08d32e3bae
commit 221a7d0826
4 changed files with 31 additions and 6 deletions

View file

@ -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)); 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) { SDL_bool process_check_success(process_t proc, const char *name) {
if (proc == PROCESS_NONE) { if (proc == PROCESS_NONE) {
LOGE("Could not execute \"%s\"", name); LOGE("Could not execute \"%s\"", name);

View file

@ -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(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_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_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 // convenience function to wait for a successful process execution
// automatically log process errors with the provided process name // automatically log process errors with the provided process name

View file

@ -17,6 +17,8 @@
# define DEFAULT_SERVER_PATH PREFIX PREFIXED_SERVER_PATH # define DEFAULT_SERVER_PATH PREFIX PREFIXED_SERVER_PATH
#endif #endif
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
static const char *get_server_path(void) { static const char *get_server_path(void) {
const char *server_path = getenv("SCRCPY_SERVER_PATH"); const char *server_path = getenv("SCRCPY_SERVER_PATH");
if (!server_path) { if (!server_path) {
@ -26,10 +28,15 @@ static const char *get_server_path(void) {
} }
static SDL_bool push_server(const char *serial) { 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"); 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) { static SDL_bool enable_tunnel(const char *serial, Uint16 local_port) {
process_t process = adb_reverse(serial, SOCKET_NAME, local_port); process_t process = adb_reverse(serial, SOCKET_NAME, local_port);
return process_check_success(process, "adb reverse"); 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; return SDL_FALSE;
} }
server->server_copied_to_device = SDL_TRUE;
if (!enable_tunnel(serial, local_port)) { if (!enable_tunnel(serial, local_port)) {
return SDL_FALSE; 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 // we don't need the server socket anymore
close_socket(&server->server_socket); 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 // we don't need the adb tunnel anymore
disable_tunnel(serial); // ignore failure disable_tunnel(serial); // ignore failure
server->adb_reverse_enabled = SDL_FALSE; server->adb_reverse_enabled = SDL_FALSE;
@ -145,6 +158,10 @@ void server_stop(struct server *server, const char *serial) {
// ignore failure // ignore failure
disable_tunnel(serial); disable_tunnel(serial);
} }
if (server->server_copied_to_device) {
remove_server(serial); // ignore failure
}
} }
void server_destroy(struct server *server) { void server_destroy(struct server *server) {

View file

@ -9,13 +9,15 @@ struct server {
socket_t server_socket; socket_t server_socket;
socket_t device_socket; socket_t device_socket;
SDL_bool adb_reverse_enabled; SDL_bool adb_reverse_enabled;
SDL_bool server_copied_to_device;
}; };
#define SERVER_INITIALIZER { \ #define SERVER_INITIALIZER { \
.process = PROCESS_NONE, \ .process = PROCESS_NONE, \
.server_socket = INVALID_SOCKET, \ .server_socket = INVALID_SOCKET, \
.device_socket = INVALID_SOCKET, \ .device_socket = INVALID_SOCKET, \
.adb_reverse_enabled = SDL_FALSE, \ .adb_reverse_enabled = SDL_FALSE, \
.server_copied_to_device = SDL_FALSE, \
} }
// init default values // init default values