1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-12-16 12:05:26 +01:00

Apply some general fixes to porting code

This commit is contained in:
sfan5
2025-12-12 20:29:50 +01:00
parent 22b5c939dc
commit 5b05fcf8e1
2 changed files with 44 additions and 51 deletions

View File

@@ -121,7 +121,7 @@ Colon delimited list of directories to search for games
Colon delimited list of directories to search for mods Colon delimited list of directories to search for mods
.TP .TP
.B MINETEST_USER_PATH .B MINETEST_USER_PATH
Path to Luanti user data directory Path to Luanti user data directory (only on RUN_IN_PLACE=0 build)
.TP .TP
.B LOG_TIMESTAMP .B LOG_TIMESTAMP
Equivalent to \-\-log-timestamp Equivalent to \-\-log-timestamp

View File

@@ -77,6 +77,10 @@ extern "C" {
} }
#endif #endif
#if !defined(PATH_MAX) && defined(_WIN32)
#define PATH_MAX MAX_PATH
#endif
namespace porting namespace porting
{ {
@@ -92,7 +96,6 @@ volatile std::sig_atomic_t *signal_handler_killstatus()
} }
#if !defined(_WIN32) // POSIX #if !defined(_WIN32) // POSIX
#define STDERR_FILENO 2
static void signal_handler(int sig) static void signal_handler(int sig)
{ {
@@ -179,7 +182,7 @@ std::string getDataPath(const char *subpath)
path[i] = 0; path[i] = 0;
} }
bool detectMSVCBuildDir(const std::string &path) [[maybe_unused]] static bool detectMSVCBuildDir(const std::string &path)
{ {
const char *ends[] = { const char *ends[] = {
"bin\\Release", "bin\\Release",
@@ -199,11 +202,11 @@ static std::string detectSystemInfo()
{ {
#ifdef _WIN32 #ifdef _WIN32
std::ostringstream oss; std::ostringstream oss;
LPSTR filePath = new char[MAX_PATH]; char filePath[PATH_MAX];
UINT blockSize; UINT blockSize;
VS_FIXEDFILEINFO *fixedFileInfo; VS_FIXEDFILEINFO *fixedFileInfo;
GetSystemDirectoryA(filePath, MAX_PATH); GetSystemDirectoryA(filePath, sizeof(filePath));
PathAppendA(filePath, "kernel32.dll"); PathAppendA(filePath, "kernel32.dll");
DWORD dwVersionSize = GetFileVersionInfoSizeA(filePath, NULL); DWORD dwVersionSize = GetFileVersionInfoSizeA(filePath, NULL);
@@ -238,7 +241,6 @@ static std::string detectSystemInfo()
} }
delete[] lpVersionInfo; delete[] lpVersionInfo;
delete[] filePath;
return oss.str(); return oss.str();
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
@@ -301,7 +303,7 @@ u32 getMemorySizeMB()
} }
static bool getExecPathFromProcfs(char *buf, size_t buflen) [[maybe_unused]] static bool getExecPathFromProcfs(char *buf, size_t buflen)
{ {
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
buflen--; buflen--;
@@ -445,7 +447,7 @@ bool getCurrentExecPath(char *buf, size_t len)
bool setSystemPaths() bool setSystemPaths()
{ {
char buf[BUFSIZ]; char buf[PATH_MAX];
// Find path of executable and set path_share relative to it // Find path of executable and set path_share relative to it
FATAL_ERROR_IF(!getCurrentExecPath(buf, sizeof(buf)), FATAL_ERROR_IF(!getCurrentExecPath(buf, sizeof(buf)),
@@ -470,7 +472,7 @@ bool setSystemPaths()
len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf)); len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf));
FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA"); FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA");
// TODO: Luanti with migration // TODO: Luanti with migration
path_user = std::string(buf) + DIR_DELIM + "Minetest"; path_user = std::string(buf) + DIR_DELIM "Minetest";
} else { } else {
path_user = std::string(buf); path_user = std::string(buf);
} }
@@ -491,10 +493,10 @@ extern bool setSystemPaths(); // defined in porting_android.cpp
bool setSystemPaths() bool setSystemPaths()
{ {
char buf[BUFSIZ]; char buf[PATH_MAX];
if (!getCurrentExecPath(buf, sizeof(buf))) { if (!getCurrentExecPath(buf, sizeof(buf))) {
FATAL_ERROR("Unable to read bindir"); FATAL_ERROR("Failed to get current executable path");
return false; return false;
} }
@@ -514,8 +516,7 @@ bool setSystemPaths()
for (auto i = trylist.begin(); i != trylist.end(); ++i) { for (auto i = trylist.begin(); i != trylist.end(); ++i) {
const std::string &trypath = *i; const std::string &trypath = *i;
if (!fs::PathExists(trypath) || if (!fs::IsDir(trypath + DIR_DELIM "builtin")) {
!fs::PathExists(trypath + DIR_DELIM + "builtin")) {
warningstream << "system-wide share not found at \"" warningstream << "system-wide share not found at \""
<< trypath << "\""<< std::endl; << trypath << "\""<< std::endl;
continue; continue;
@@ -531,13 +532,12 @@ bool setSystemPaths()
break; break;
} }
const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); const char *const env_user_path = getenv("MINETEST_USER_PATH");
if (minetest_user_path && minetest_user_path[0] != '\0') { if (env_user_path && env_user_path[0] != '\0') {
path_user = std::string(minetest_user_path); path_user = std::string(env_user_path);
} else { } else {
// TODO: luanti with migration // TODO: luanti with migration
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." path_user = std::string(getHomeOrFail()) + DIR_DELIM "." "minetest";
+ "minetest";
} }
return true; return true;
@@ -560,14 +560,13 @@ bool setSystemPaths()
} }
CFRelease(resources_url); CFRelease(resources_url);
const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); const char *const env_user_path = getenv("MINETEST_USER_PATH");
if (minetest_user_path && minetest_user_path[0] != '\0') { if (env_user_path && env_user_path[0] != '\0') {
path_user = std::string(minetest_user_path); path_user = std::string(env_user_path);
} else { } else {
// TODO: luanti with migration // TODO: luanti with migration
path_user = std::string(getHomeOrFail()) path_user = std::string(getHomeOrFail())
+ "/Library/Application Support/" + "/Library/Application Support/" "minetest";
+ "minetest";
} }
return true; return true;
} }
@@ -578,13 +577,12 @@ bool setSystemPaths()
bool setSystemPaths() bool setSystemPaths()
{ {
path_share = STATIC_SHAREDIR; path_share = STATIC_SHAREDIR;
const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); const char *const env_user_path = getenv("MINETEST_USER_PATH");
if (minetest_user_path && minetest_user_path[0] != '\0') { if (env_user_path && env_user_path[0] != '\0') {
path_user = std::string(minetest_user_path); path_user = std::string(env_user_path);
} else { } else {
// TODO: luanti with migration // TODO: luanti with migration
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." path_user = std::string(getHomeOrFail()) + DIR_DELIM "." "minetest";
+ "minetest";
} }
return true; return true;
} }
@@ -595,11 +593,11 @@ bool setSystemPaths()
// Move cache folder from path_user to system cache location if possible. // Move cache folder from path_user to system cache location if possible.
[[maybe_unused]] static void migrateCachePath() [[maybe_unused]] static void migrateCachePath()
{ {
const std::string local_cache_path = path_user + DIR_DELIM + "cache"; const std::string local_cache_path = path_user + DIR_DELIM "cache";
// Delete tmp folder if it exists (it only ever contained // Delete tmp folder if it exists (it only ever contained
// a temporary ogg file, which is no longer used). // a temporary ogg file, which is no longer used).
fs::RecursiveDelete(local_cache_path + DIR_DELIM + "tmp"); fs::RecursiveDelete(local_cache_path + DIR_DELIM "tmp");
// Bail if migration impossible // Bail if migration impossible
if (path_cache == local_cache_path || !fs::PathExists(local_cache_path) if (path_cache == local_cache_path || !fs::PathExists(local_cache_path)
@@ -615,7 +613,7 @@ bool setSystemPaths()
// Create tag in cache folder according to <https://bford.info/cachedir/> spec // Create tag in cache folder according to <https://bford.info/cachedir/> spec
static void createCacheDirTag() static void createCacheDirTag()
{ {
const auto path = path_cache + DIR_DELIM + "CACHEDIR.TAG"; const auto path = path_cache + DIR_DELIM "CACHEDIR.TAG";
if (fs::PathExists(path)) if (fs::PathExists(path))
return; return;
@@ -635,7 +633,7 @@ void initializePaths()
#if RUN_IN_PLACE #if RUN_IN_PLACE
infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl; infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl;
char buf[BUFSIZ]; char buf[PATH_MAX];
bool success = bool success =
getCurrentExecPath(buf, sizeof(buf)) || getCurrentExecPath(buf, sizeof(buf)) ||
getExecPathFromProcfs(buf, sizeof(buf)); getExecPathFromProcfs(buf, sizeof(buf));
@@ -647,10 +645,12 @@ void initializePaths()
path_share = execpath + DIR_DELIM ".."; path_share = execpath + DIR_DELIM "..";
path_user = execpath + DIR_DELIM ".."; path_user = execpath + DIR_DELIM "..";
#ifdef _WIN32
if (detectMSVCBuildDir(execpath)) { if (detectMSVCBuildDir(execpath)) {
path_share += DIR_DELIM ".."; path_share += DIR_DELIM "..";
path_user += DIR_DELIM ".."; path_user += DIR_DELIM "..";
} }
#endif
} else { } else {
errorstream << "Failed to get paths by executable location, " errorstream << "Failed to get paths by executable location, "
"trying cwd" << std::endl; "trying cwd" << std::endl;
@@ -672,7 +672,7 @@ void initializePaths()
path_share = execpath; path_share = execpath;
path_user = execpath; path_user = execpath;
} }
path_cache = path_user + DIR_DELIM + "cache"; path_cache = path_user + DIR_DELIM "cache";
#else #else
infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl; infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl;
@@ -683,22 +683,22 @@ void initializePaths()
# ifdef __ANDROID__ # ifdef __ANDROID__
sanity_check(!path_cache.empty()); sanity_check(!path_cache.empty());
# elif defined(_WIN32) # elif defined(_WIN32)
path_cache = path_user + DIR_DELIM + "cache"; path_cache = path_user + DIR_DELIM "cache";
# else # else
// First try $XDG_CACHE_HOME/PROJECT_NAME // First try $XDG_CACHE_HOME/PROJECT_NAME
const char *cache_dir = getenv("XDG_CACHE_HOME"); const char *cache_dir = getenv("XDG_CACHE_HOME");
const char *home_dir = getenv("HOME"); const char *home_dir = getenv("HOME");
if (cache_dir && cache_dir[0] != '\0') { if (cache_dir && cache_dir[0] != '\0') {
// TODO: luanti with migration // TODO: luanti with migration
path_cache = std::string(cache_dir) + DIR_DELIM + "minetest"; path_cache = std::string(cache_dir) + DIR_DELIM "minetest";
} else if (home_dir) { } else if (home_dir) {
// Then try $HOME/.cache/PROJECT_NAME // Then try $HOME/.cache/PROJECT_NAME
// TODO: luanti with migration // TODO: luanti with migration
path_cache = std::string(home_dir) + DIR_DELIM + ".cache" path_cache = std::string(home_dir) + DIR_DELIM ".cache"
+ DIR_DELIM + "minetest"; DIR_DELIM "minetest";
} else { } else {
// If neither works, use $PATH_USER/cache // If neither works, use $PATH_USER/cache
path_cache = path_user + DIR_DELIM + "cache"; path_cache = path_user + DIR_DELIM "cache";
} }
# endif // _WIN32 # endif // _WIN32
@@ -719,26 +719,19 @@ void initializePaths()
#if USE_GETTEXT #if USE_GETTEXT
bool found_localedir = false; bool found_localedir = false;
# ifdef STATIC_LOCALEDIR
/* STATIC_LOCALEDIR may be a generalized path such as /usr/share/locale that /* STATIC_LOCALEDIR may be a generalized path such as /usr/share/locale that
* doesn't necessarily contain our locale files, so check data path first. */ * doesn't necessarily contain our locale files, so check data path first. */
path_locale = getDataPath("locale"); path_locale = getDataPath("locale");
if (fs::PathExists(path_locale)) { if (fs::PathExists(path_locale)) {
found_localedir = true; found_localedir = true;
infostream << "Using in-place locale directory " << path_locale infostream << "Using in-place locale directory: " << path_locale
<< " even though a static one was provided." << std::endl; << std::endl;
} else if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) { } else if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) {
found_localedir = true; found_localedir = true;
path_locale = STATIC_LOCALEDIR; path_locale = STATIC_LOCALEDIR;
infostream << "Using static locale directory " << STATIC_LOCALEDIR infostream << "Using static locale directory: " << path_locale
<< std::endl; << std::endl;
} }
# else
path_locale = getDataPath("locale");
if (fs::PathExists(path_locale)) {
found_localedir = true;
}
# endif
if (!found_localedir) { if (!found_localedir) {
warningstream << "Couldn't find a locale directory!" << std::endl; warningstream << "Couldn't find a locale directory!" << std::endl;
} }
@@ -758,9 +751,9 @@ bool secure_rand_fill_buf(void *buf, size_t len)
if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return false; return false;
CryptGenRandom(wctx, len, (BYTE *)buf); bool success = CryptGenRandom(wctx, len, (BYTE *)buf);
CryptReleaseContext(wctx, 0); CryptReleaseContext(wctx, 0);
return true; return success;
} }
#else #else
@@ -933,7 +926,7 @@ bool open_directory(const std::string &path)
// Load performance counter frequency only once at startup // Load performance counter frequency only once at startup
#ifdef _WIN32 #ifdef _WIN32
inline double get_perf_freq() static inline double get_perf_freq()
{ {
// Also use this opportunity to enable high-res timers // Also use this opportunity to enable high-res timers
timeBeginPeriod(1); timeBeginPeriod(1);