2018-08-12 02:13:49 +00:00
|
|
|
#include "file_handler.h"
|
|
|
|
|
2019-11-27 20:11:40 +00:00
|
|
|
#include <assert.h>
|
2018-08-12 02:13:49 +00:00
|
|
|
#include <string.h>
|
2019-05-29 19:46:16 +00:00
|
|
|
|
2021-01-03 13:55:15 +00:00
|
|
|
#include "adb.h"
|
2019-11-24 10:53:00 +00:00
|
|
|
#include "util/log.h"
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2021-06-13 20:47:16 +00:00
|
|
|
#define DEFAULT_PUSH_TARGET "/sdcard/Download/"
|
2019-07-30 23:48:32 +00:00
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
static void
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_request_destroy(struct file_handler_request *req) {
|
2021-01-24 14:14:53 +00:00
|
|
|
free(req->file);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-07-30 23:48:32 +00:00
|
|
|
file_handler_init(struct file_handler *file_handler, const char *serial,
|
|
|
|
const char *push_target) {
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
cbuf_init(&file_handler->queue);
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
bool ok = sc_mutex_init(&file_handler->mutex);
|
|
|
|
if (!ok) {
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
ok = sc_cond_init(&file_handler->event_cond);
|
|
|
|
if (!ok) {
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (serial) {
|
2021-01-24 14:14:53 +00:00
|
|
|
file_handler->serial = strdup(serial);
|
2018-08-12 02:13:49 +00:00
|
|
|
if (!file_handler->serial) {
|
2019-06-23 18:49:38 +00:00
|
|
|
LOGW("Could not strdup serial");
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_destroy(&file_handler->event_cond);
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file_handler->serial = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy initialization
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->initialized = false;
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->stopped = false;
|
2021-11-11 16:48:41 +00:00
|
|
|
file_handler->current_process = SC_PROCESS_NONE;
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-07-30 23:48:32 +00:00
|
|
|
file_handler->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
return true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_destroy(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_destroy(&file_handler->event_cond);
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2021-01-24 14:14:53 +00:00
|
|
|
free(file_handler->serial);
|
2019-05-29 19:46:16 +00:00
|
|
|
|
|
|
|
struct file_handler_request req;
|
|
|
|
while (cbuf_take(&file_handler->queue, &req)) {
|
|
|
|
file_handler_request_destroy(&req);
|
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 16:48:41 +00:00
|
|
|
static sc_pid
|
2019-03-02 19:09:56 +00:00
|
|
|
install_apk(const char *serial, const char *file) {
|
2018-08-12 02:40:00 +00:00
|
|
|
return adb_install(serial, file);
|
|
|
|
}
|
|
|
|
|
2021-11-11 16:48:41 +00:00
|
|
|
static sc_pid
|
2019-07-30 23:48:32 +00:00
|
|
|
push_file(const char *serial, const char *file, const char *push_target) {
|
|
|
|
return adb_push(serial, file, push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-03-02 19:09:56 +00:00
|
|
|
file_handler_request(struct file_handler *file_handler,
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_action_t action, char *file) {
|
2018-08-12 02:13:49 +00:00
|
|
|
// start file_handler if it's used for the first time
|
|
|
|
if (!file_handler->initialized) {
|
|
|
|
if (!file_handler_start(file_handler)) {
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->initialized = true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
LOGI("Request to %s %s", action == ACTION_INSTALL_APK ? "install" : "push",
|
|
|
|
file);
|
2019-05-29 19:46:16 +00:00
|
|
|
struct file_handler_request req = {
|
|
|
|
.action = action,
|
|
|
|
.file = file,
|
|
|
|
};
|
2018-08-12 02:40:00 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2019-05-29 19:46:16 +00:00
|
|
|
bool was_empty = cbuf_is_empty(&file_handler->queue);
|
|
|
|
bool res = cbuf_push(&file_handler->queue, req);
|
2018-08-12 02:13:49 +00:00
|
|
|
if (was_empty) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_signal(&file_handler->event_cond);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
static int
|
|
|
|
run_file_handler(void *data) {
|
2018-08-12 02:13:49 +00:00
|
|
|
struct file_handler *file_handler = data;
|
|
|
|
|
|
|
|
for (;;) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2021-11-11 16:48:41 +00:00
|
|
|
file_handler->current_process = SC_PROCESS_NONE;
|
2019-05-29 19:46:16 +00:00
|
|
|
while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_wait(&file_handler->event_cond, &file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
if (file_handler->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-29 19:46:16 +00:00
|
|
|
struct file_handler_request req;
|
|
|
|
bool non_empty = cbuf_take(&file_handler->queue, &req);
|
2019-11-27 20:11:40 +00:00
|
|
|
assert(non_empty);
|
|
|
|
(void) non_empty;
|
2018-08-15 14:46:53 +00:00
|
|
|
|
2021-11-11 16:48:41 +00:00
|
|
|
sc_pid pid;
|
2019-05-29 19:46:16 +00:00
|
|
|
if (req.action == ACTION_INSTALL_APK) {
|
|
|
|
LOGI("Installing %s...", req.file);
|
2021-11-11 16:48:41 +00:00
|
|
|
pid = install_apk(file_handler->serial, req.file);
|
2018-08-12 02:40:00 +00:00
|
|
|
} else {
|
2019-05-29 19:46:16 +00:00
|
|
|
LOGI("Pushing %s...", req.file);
|
2021-11-11 16:48:41 +00:00
|
|
|
pid = push_file(file_handler->serial, req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
2021-11-11 16:48:41 +00:00
|
|
|
file_handler->current_process = pid;
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
if (req.action == ACTION_INSTALL_APK) {
|
2021-11-11 16:48:41 +00:00
|
|
|
if (sc_process_check_success(pid, "adb install", false)) {
|
2019-05-29 19:46:16 +00:00
|
|
|
LOGI("%s successfully installed", req.file);
|
2018-08-12 02:40:00 +00:00
|
|
|
} else {
|
2019-05-29 19:46:16 +00:00
|
|
|
LOGE("Failed to install %s", req.file);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
} else {
|
2021-11-11 16:48:41 +00:00
|
|
|
if (sc_process_check_success(pid, "adb push", false)) {
|
2019-07-30 23:48:32 +00:00
|
|
|
LOGI("%s successfully pushed to %s", req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
} else {
|
2019-07-30 23:48:32 +00:00
|
|
|
LOGE("Failed to push %s to %s", req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2018-08-12 02:40:00 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2021-11-11 16:48:41 +00:00
|
|
|
// Close the process (it is necessarily already terminated)
|
2021-01-22 18:20:30 +00:00
|
|
|
// Execute this call with mutex locked to avoid race conditions with
|
|
|
|
// file_handler_stop()
|
2021-11-11 16:48:41 +00:00
|
|
|
sc_process_close(file_handler->current_process);
|
|
|
|
file_handler->current_process = SC_PROCESS_NONE;
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2021-01-22 18:20:30 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_request_destroy(&req);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-03-02 19:09:56 +00:00
|
|
|
file_handler_start(struct file_handler *file_handler) {
|
2018-08-12 02:13:49 +00:00
|
|
|
LOGD("Starting file_handler thread");
|
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
bool ok = sc_thread_create(&file_handler->thread, run_file_handler,
|
|
|
|
"file_handler", file_handler);
|
|
|
|
if (!ok) {
|
2018-08-12 02:13:49 +00:00
|
|
|
LOGC("Could not start file_handler thread");
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
return true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_stop(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->stopped = true;
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_signal(&file_handler->event_cond);
|
2021-11-11 16:48:41 +00:00
|
|
|
if (file_handler->current_process != SC_PROCESS_NONE) {
|
|
|
|
if (!sc_process_terminate(file_handler->current_process)) {
|
2021-01-22 18:22:40 +00:00
|
|
|
LOGW("Could not terminate push/install process");
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_join(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_thread_join(&file_handler->thread, NULL);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|