Prevent program hang when compiled with relative path PREFERENCES_DIR

Fixes #2000
Fixes #9007
This commit is contained in:
pentarctagon 2024-06-20 12:22:06 -05:00 committed by Pentarctagon
parent 081866af7b
commit 7895b843fa
3 changed files with 14 additions and 41 deletions

View file

@ -205,9 +205,9 @@ commandline_options::commandline_options(const std::vector<std::string>& args)
("unsafe-scripts", "makes the \'package\' package available to Lua scripts, so that they can load arbitrary packages. Do not do this with untrusted scripts! This action gives ua the same permissions as the Wesnoth executable.")
("usercache-dir", po::value<std::string>(), "sets the path of the cache directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory. Defaults to $HOME/.cache/wesnoth on X11 and to the userdata-dir on other systems.")
("usercache-path", "prints the path of the cache directory and exits.")
("userconfig-dir", po::value<std::string>(), "sets the path of the user config directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory. Defaults to the userdata-dir.")
("userconfig-dir", po::value<std::string>(), "sets the path of the user config directory. You must specify an absolute path outside the $HOME or My Documents\\My Games directory. Defaults to the userdata-dir.")
("userconfig-path", "prints the path of the user config directory and exits.")
("userdata-dir", po::value<std::string>(), "sets the path of the userdata directory to $HOME/<arg> or My Documents\\My Games\\<arg> for Windows. You can specify also an absolute path outside the $HOME or My Documents\\My Games directory.")
("userdata-dir", po::value<std::string>(), "sets the path of the userdata directory. You must specify an absolute path outside the $HOME or My Documents\\My Games directory.")
("userdata-path", "prints the path of the userdata directory and exits." IMPLY_TERMINAL)
("username", po::value<std::string>(), "uses <username> when connecting to a server, ignoring other preferences.")
("validcache", "assumes that the cache is valid. (dangerous)")

View file

@ -22,7 +22,6 @@
#include "filesystem.hpp"
#include "config.hpp"
#include "deprecation.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/base64.hpp"
@ -680,7 +679,7 @@ static bfs::path windows_userdata(const std::string& newprefdir)
// if a custom userdata directory is provided as an absolute path, just use that
// else if it's relative to the current working directory, just use that
// else if no custom userdata directory was provided, default to the "My Games" folder if present or fallback to the current working directory if not
// else a relative path was provided
// else a relative path was provided that isn't relative to the current working directory, so ignore it and use the default
if(temp.size() > 2 && temp[1] == ':') {
// allow absolute path override
dir = temp;
@ -694,17 +693,8 @@ static bfs::path windows_userdata(const std::string& newprefdir)
temp = "Wesnoth" + get_version_path_suffix();
DBG_FS << "using default userdata folder name";
} else {
// only warn about a relative path if it comes from the command line option, not from the PREFERENCES_DIR define
#ifdef PREFERENCES_DIR
if (temp != PREFERENCES_DIR)
#endif
{
// TRANSLATORS: translate the part inside <...> only
deprecated_message(_("--userdata-dir=<relative path that doesn't start with a period>"),
DEP_LEVEL::FOR_REMOVAL,
{1, 17, 0},
_("Use an absolute path, or a relative path that starts with a period and a backslash"));
}
temp = "Wesnoth" + get_version_path_suffix();
ERR_FS << "relative path for userdata that doesn't start with '.' or '..' is not allowed, using default userdata folder name";
}
PWSTR docs_path = nullptr;
@ -761,21 +751,16 @@ static bfs::path apple_userdata(const std::string& newprefdir)
temp = "Library/Application Support/Wesnoth_"+get_version_path_suffix();
DBG_FS << "userdata using default path relative to HOME";
#endif
} else if(temp[0] != '/') {
// TRANSLATORS: translate the part inside <...> only
deprecated_message(_("--userdata-dir=<relative path>"),
DEP_LEVEL::FOR_REMOVAL,
{1, 17, 0},
_("Use absolute paths. Relative paths are deprecated because they are interpreted relative to $HOME"));
}
// if it's an absolute path, just use that
// else make it relative to HOME if HOME is populated, otherwise make it relative to the current working directory
if(temp[0] == '/') {
dir = temp;
DBG_FS << "userdata using absolute path";
} else {
bfs::path home = home_str ? home_str : ".";
dir = home / temp;
dir = "." / temp;
ERR_FS << "unable to determine location to use for userdata, defaulting to current working directory";
}
return dir;
@ -829,19 +814,9 @@ static bfs::path linux_userdata(const std::string& newprefdir)
temp = ".wesnoth" + get_version_path_suffix();
}
// if there is a HOME variable and we've reached this point, then a custom userdata folder using a relative path was provided
// else just use the current working directory for the userdata
if(home_str) {
dir = home_str;
// TRANSLATORS: translate the part inside <...> only
deprecated_message(_("--userdata-dir=<relative path>"),
DEP_LEVEL::FOR_REMOVAL,
{1, 17, 0},
_("Use absolute paths. Relative paths are deprecated because they are interpreted relative to $HOME"));
} else {
dir = ".";
DBG_FS << "userdata unable to determine location to use, defaulting to current working directory";
}
// unable to determine another userdata directory, so just use the current working directory for the userdata
dir = ".";
ERR_FS << "unable to determine location to use for userdata, defaulting to current working directory";
dir /= temp;
return dir;

View file

@ -204,11 +204,9 @@ class prefs
{
// for wesnoth: checks that the userdata folders have been initialized/found since if it hasn't been then it's too soon to be getting any values from it
// for boost: the userdata folders don't get initialized and the preferences aren't used for anything, so skip the check here
//
// nice idea, but it seems that literally the only thing that initializes the userdata folders before something ends up calling the preferences is running wesnoth normally
// and that's not practical to check for
//static bool called_before_init = !(filesystem::base_name(filesystem::get_exe_path()).find("boost") == std::string::npos && !filesystem::is_userdata_initialized());
//assert(called_before_init);
// TODO: figure out how to make this not fail for all the other tests too
static bool called_before_init = !(filesystem::base_name(filesystem::get_exe_path()).find("boost") == std::string::npos && !filesystem::is_userdata_initialized());
assert(called_before_init);
static prefs prefs_manager;
return prefs_manager;