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