Improve file API

Prefix symbols and constants names and improve documentation.
This commit is contained in:
Romain Vimont 2021-11-11 16:21:07 +01:00
parent d4c262301f
commit fcc04f967b
7 changed files with 41 additions and 26 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -7,7 +7,7 @@
#include <unistd.h>
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) {
// <https://stackoverflow.com/a/1024937/1987178>
#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)) {

View file

@ -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");

View file

@ -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 <https://github.com/Genymobile/scrcpy/issues/2619>
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);

View file

@ -6,29 +6,44 @@
#include <stdbool.h>
#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