From fcc04f967b9463a949641d8d22f15959ff79f2f9 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 11 Nov 2021 16:21:07 +0100 Subject: [PATCH] Improve file API Prefix symbols and constants names and improve documentation. --- app/src/adb.c | 2 +- app/src/icon.c | 2 +- app/src/server.c | 4 ++-- app/src/sys/unix/file.c | 6 +++--- app/src/sys/win/file.c | 4 ++-- app/src/util/file.c | 10 +++++----- app/src/util/file.h | 39 +++++++++++++++++++++++++++------------ 7 files changed, 41 insertions(+), 26 deletions(-) diff --git a/app/src/adb.c b/app/src/adb.c index d127494a..1eb8d9ed 100644 --- a/app/src/adb.c +++ b/app/src/adb.c @@ -69,7 +69,7 @@ show_adb_installation_msg() { {"pacman", "pacman -S android-tools"}, }; for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) { - if (search_executable(pkg_managers[i].binary)) { + if (sc_file_executable_exists(pkg_managers[i].binary)) { LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command); return; } diff --git a/app/src/icon.c b/app/src/icon.c index 787d048f..f9799de7 100644 --- a/app/src/icon.c +++ b/app/src/icon.c @@ -46,7 +46,7 @@ get_icon_path(void) { return NULL; } #else - char *icon_path = get_local_file_path(SCRCPY_PORTABLE_ICON_FILENAME); + char *icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME); if (!icon_path) { LOGE("Could not get icon path"); return NULL; diff --git a/app/src/server.c b/app/src/server.c index aba77288..25d786c4 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -49,7 +49,7 @@ get_server_path(void) { return NULL; } #else - char *server_path = get_local_file_path(SERVER_FILENAME); + char *server_path = sc_file_get_local_path(SERVER_FILENAME); if (!server_path) { LOGE("Could not get local file path, " "using " SERVER_FILENAME " from current directory"); @@ -68,7 +68,7 @@ push_server(const char *serial) { if (!server_path) { return false; } - if (!is_regular_file(server_path)) { + if (!sc_file_is_regular(server_path)) { LOGE("'%s' does not exist or is not a regular file\n", server_path); free(server_path); return false; diff --git a/app/src/sys/unix/file.c b/app/src/sys/unix/file.c index 2e71b113..4e9e45b3 100644 --- a/app/src/sys/unix/file.c +++ b/app/src/sys/unix/file.c @@ -7,7 +7,7 @@ #include bool -search_executable(const char *file) { +sc_file_executable_exists(const char *file) { char *path = getenv("PATH"); if (!path) return false; @@ -43,7 +43,7 @@ search_executable(const char *file) { } char * -get_executable_path(void) { +sc_file_get_executable_path(void) { // #ifdef __linux__ char buf[PATH_MAX + 1]; // +1 for the null byte @@ -63,7 +63,7 @@ get_executable_path(void) { } bool -is_regular_file(const char *path) { +sc_file_is_regular(const char *path) { struct stat path_stat; if (stat(path, &path_stat)) { diff --git a/app/src/sys/win/file.c b/app/src/sys/win/file.c index 0f9101e9..badb8087 100644 --- a/app/src/sys/win/file.c +++ b/app/src/sys/win/file.c @@ -8,7 +8,7 @@ #include "util/str_util.h" char * -get_executable_path(void) { +sc_file_get_executable_path(void) { HMODULE hModule = GetModuleHandleW(NULL); if (!hModule) { return NULL; @@ -23,7 +23,7 @@ get_executable_path(void) { } bool -is_regular_file(const char *path) { +sc_file_is_regular(const char *path) { wchar_t *wide_path = utf8_to_wide_char(path); if (!wide_path) { LOGC("Could not allocate wide char string"); diff --git a/app/src/util/file.c b/app/src/util/file.c index bb7fdb2c..59be2d91 100644 --- a/app/src/util/file.c +++ b/app/src/util/file.c @@ -6,8 +6,8 @@ #include "util/log.h" char * -get_local_file_path(const char *name) { - char *executable_path = get_executable_path(); +sc_file_get_local_path(const char *name) { + char *executable_path = sc_file_get_executable_path(); if (!executable_path) { return NULL; } @@ -15,10 +15,10 @@ get_local_file_path(const char *name) { // dirname() does not work correctly everywhere, so get the parent // directory manually. // See - char *p = strrchr(executable_path, PATH_SEPARATOR); + char *p = strrchr(executable_path, SC_PATH_SEPARATOR); if (!p) { LOGE("Unexpected executable path: \"%s\" (it should contain a '%c')", - executable_path, PATH_SEPARATOR); + executable_path, SC_PATH_SEPARATOR); free(executable_path); return NULL; } @@ -37,7 +37,7 @@ get_local_file_path(const char *name) { } memcpy(file_path, dir, dirlen); - file_path[dirlen] = PATH_SEPARATOR; + file_path[dirlen] = SC_PATH_SEPARATOR; // namelen + 1 to copy the final '\0' memcpy(&file_path[dirlen + 1], name, namelen + 1); diff --git a/app/src/util/file.h b/app/src/util/file.h index 813af486..089f6f75 100644 --- a/app/src/util/file.h +++ b/app/src/util/file.h @@ -6,29 +6,44 @@ #include #ifdef _WIN32 -# define PATH_SEPARATOR '\\' +# define SC_PATH_SEPARATOR '\\' #else -# define PATH_SEPARATOR '/' +# define SC_PATH_SEPARATOR '/' #endif #ifndef _WIN32 -// only used to find package manager, not implemented for Windows +/** + * Indicate if an executable exists using $PATH + * + * In practice, it is only used to know if a package manager is available on + * the system. It is only implemented on Linux. + */ bool -search_executable(const char *file); +sc_file_executable_exists(const char *file); #endif -// return the absolute path of the executable (the scrcpy binary) -// may be NULL on error; to be freed by free() +/** + * Return the absolute path of the executable (the scrcpy binary) + * + * The result must be freed by the caller using free(). It may return NULL on + * error. + */ char * -get_executable_path(void); +sc_file_get_executable_path(void); -// Return the absolute path of a file in the same directory as he executable. -// May be NULL on error. To be freed by free(). +/** + * Return the absolute path of a file in the same directory as the executable + * + * The result must be freed by the caller using free(). It may return NULL on + * error. + */ char * -get_local_file_path(const char *name); +sc_file_get_local_path(const char *name); -// returns true if the file exists and is not a directory +/** + * Indicate if the file exists and is not a directory + */ bool -is_regular_file(const char *path); +sc_file_is_regular(const char *path); #endif