Better packaging.

This commit is contained in:
Vladimir Chebotarev 2020-09-07 11:38:06 +03:00
parent 7ddf54d3a6
commit 8d82c019ae

View file

@ -18,10 +18,14 @@
#define SOCKET_NAME "scrcpy"
#define SSH_SOCKET_NAME "/dev/socket/scrcpy"
#define SERVER_FILENAME "scrcpy-server"
#define ABSTRACTCAT_FILENAME "abstractcat"
#define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
#define DEFAULT_ABSTRACTCAT_PATH PREFIX "/share/scrcpy/" ABSTRACTCAT_FILENAME
#define DEVICE_ABSTRACTCAT_PATH "/data/local/tmp/" ABSTRACTCAT_FILENAME
static char *
get_server_path(void) {
#ifdef __WINDOWS__
@ -88,6 +92,88 @@ get_server_path(void) {
#endif
}
static char *
get_abstractcat_path(void) {
#ifdef __WINDOWS__
const wchar_t *abstractcat_path_env = _wgetenv(L"SCRCPY_ABSTRACTCAT_PATH");
#else
const char *abstractcat_path_env = getenv("SCRCPY_ABSTRACTCAT_PATH");
#endif
if (abstractcat_path_env) {
// if the envvar is set, use it
#ifdef __WINDOWS__
char *abstractcat_path = utf8_from_wide_char(abstractcat_path_env);
#else
char *abstractcat_path = SDL_strdup(abstractcat_path_env);
#endif
if (!abstractcat_path) {
LOGE("Could not allocate memory");
return NULL;
}
LOGD("Using SCRCPY_ABSTRACTCAT_PATH: %s", abstractcat_path);
return abstractcat_path;
}
#ifndef PORTABLE
LOGD("Using abstractcat: " DEFAULT_ABSTRACTCAT_PATH);
char *abstractcat_path = SDL_strdup(DEFAULT_ABSTRACTCAT_PATH);
if (!abstractcat_path) {
LOGE("Could not allocate memory");
return NULL;
}
// the absolute path is hardcoded
return abstractcat_path;
#else
// use scrcpy-server in the same directory as the executable
char *executable_path = get_executable_path();
if (!executable_path) {
LOGE("Could not get executable path, "
"using " ABSTRACTCAT_FILENAME " from current directory");
// not found, use current directory
return ABSTRACTCAT_FILENAME;
}
char *dir = dirname(executable_path);
size_t dirlen = strlen(dir);
// sizeof(ABSTRACTCAT_FILENAME) gives statically the size including the null byte
size_t len = dirlen + 1 + sizeof(ABSTRACTCAT_FILENAME);
char *abstractcat_path = SDL_malloc(len);
if (!abstractcat_path) {
LOGE("Could not alloc abstractcat path string, "
"using " ABSTRACTCAT_FILENAME " from current directory");
SDL_free(executable_path);
return ABSTRACTCAT_FILENAME;
}
memcpy(abstractcat_path, dir, dirlen);
abstractcat_path[dirlen] = PATH_SEPARATOR;
memcpy(&abstractcat_path[dirlen + 1], ABSTRACTCAT_FILENAME, sizeof(ABSTRACTCAT_FILENAME));
// the final null byte has been copied with ABSTRACTCAT_FILENAME
SDL_free(executable_path);
LOGD("Using abstractcat (portable): %s", abstractcat_path);
return abstractcat_path;
#endif
}
static bool
push_abstractcat(const struct server_params *params) {
char *abstractcat_path = get_abstractcat_path();
if (!abstractcat_path) {
return false;
}
if (!is_regular_file(abstractcat_path)) {
LOGE("'%s' does not exist or is not a regular file\n", abstractcat_path);
SDL_free(abstractcat_path);
return false;
}
process_t process = ssh_push(params->ssh_endpoint, abstractcat_path, DEVICE_ABSTRACTCAT_PATH);
SDL_free(abstractcat_path);
return process_check_success(process, "scp");
}
static bool
push_server(const char *serial, const struct server_params *params) {
char *server_path = get_server_path();
@ -331,7 +417,7 @@ execute_server(struct server *server, const struct server_params *params) {
};
const char *const prefix_cmd[] = {
"/data/ssh/root/abstractcat",
DEVICE_ABSTRACTCAT_PATH,
"@" SOCKET_NAME, // Source.
SSH_SOCKET_NAME, // Destination.
"2", // Maximum connections to forward.
@ -449,6 +535,9 @@ server_start(struct server *server, const char *serial,
goto error1;
}
if (params->use_ssh && !push_abstractcat(params))
goto error1;
if (params->use_ssh && !prepare_ssh_socket_path(params))
goto error1;