2021-11-11 23:12:17 +08:00
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util/log.h"
|
|
|
|
|
|
|
|
char *
|
2021-11-11 23:21:07 +08:00
|
|
|
sc_file_get_local_path(const char *name) {
|
|
|
|
char *executable_path = sc_file_get_executable_path();
|
2021-11-11 23:12:17 +08:00
|
|
|
if (!executable_path) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dirname() does not work correctly everywhere, so get the parent
|
|
|
|
// directory manually.
|
|
|
|
// See <https://github.com/Genymobile/scrcpy/issues/2619>
|
2021-11-11 23:21:07 +08:00
|
|
|
char *p = strrchr(executable_path, SC_PATH_SEPARATOR);
|
2021-11-11 23:12:17 +08:00
|
|
|
if (!p) {
|
|
|
|
LOGE("Unexpected executable path: \"%s\" (it should contain a '%c')",
|
2021-11-11 23:21:07 +08:00
|
|
|
executable_path, SC_PATH_SEPARATOR);
|
2021-11-11 23:12:17 +08:00
|
|
|
free(executable_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0'; // modify executable_path in place
|
|
|
|
char *dir = executable_path;
|
|
|
|
size_t dirlen = strlen(dir);
|
|
|
|
size_t namelen = strlen(name);
|
|
|
|
|
|
|
|
size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
|
|
|
|
char *file_path = malloc(len);
|
|
|
|
if (!file_path) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-11-11 23:12:17 +08:00
|
|
|
free(executable_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(file_path, dir, dirlen);
|
2021-11-11 23:21:07 +08:00
|
|
|
file_path[dirlen] = SC_PATH_SEPARATOR;
|
2021-11-11 23:12:17 +08:00
|
|
|
// namelen + 1 to copy the final '\0'
|
|
|
|
memcpy(&file_path[dirlen + 1], name, namelen + 1);
|
|
|
|
|
|
|
|
free(executable_path);
|
|
|
|
|
|
|
|
return file_path;
|
|
|
|
}
|
|
|
|
|