Correct confusing handling of ./ paths

Warn about unresolvable paths instead of returning a bogus path or nothing.
This commit is contained in:
Gunter Labes 2024-07-24 17:29:39 +02:00
parent a88d7ef1f8
commit 5da4160d87
No known key found for this signature in database
GPG key ID: C0C7B971CC910216
3 changed files with 17 additions and 18 deletions

View file

@ -1621,30 +1621,30 @@ utils::optional<std::string> get_binary_dir_location(const std::string& type, co
return utils::nullopt;
}
utils::optional<std::string> get_wml_location(const std::string& filename, const std::string& current_dir)
utils::optional<std::string> get_wml_location(const std::string& path, const utils::optional<std::string>& current_dir)
{
if(!is_legal_file(filename)) {
if(!is_legal_file(path)) {
return utils::nullopt;
}
assert(game_config::path.empty() == false);
bfs::path fpath(filename);
bfs::path fpath(path);
bfs::path result;
if(filename[0] == '~') {
result /= get_user_data_path() / "data" / filename.substr(1);
if(path[0] == '~') {
result /= get_user_data_path() / "data" / path.substr(1);
DBG_FS << " trying '" << result.string() << "'";
} else if(*fpath.begin() == ".") {
if(!current_dir.empty()) {
result /= bfs::path(current_dir);
if (current_dir) {
result /= bfs::path(*current_dir) / path;
} else {
result /= bfs::path(game_config::path) / "data";
WRN_FS << "Cannot resolve " << path << " since the current directory is unknown!";
}
} else {
if(game_config::path.empty()) {
WRN_FS << "Cannot resolve " << path << " since the game data directory is unknown!";
} else {
result /= bfs::path(game_config::path) / "data" / path;
}
result /= filename;
} else if(!game_config::path.empty()) {
result /= bfs::path(game_config::path) / "data" / filename;
}
if(result.empty() || !file_exists(result)) {

View file

@ -469,10 +469,9 @@ utils::optional<std::string> get_binary_file_location(const std::string& type, c
utils::optional<std::string> get_binary_dir_location(const std::string &type, const std::string &filename);
/**
* Returns a complete path to the actual WML file or directory, if either exists.
* Returns a translated path to the actual file or directory, if it exists. @a current_dir is needed to resolve a path starting with ".".
*/
utils::optional<std::string> get_wml_location(const std::string &filename,
const std::string &current_dir = std::string());
utils::optional<std::string> get_wml_location(const std::string& path, const utils::optional<std::string>& current_dir = utils::nullopt);
/**
* Returns a short path to @a filename, skipping the (user) data directory.

View file

@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE( test_fs_wml_path )
BOOST_CHECK_EQUAL( get_wml_location("_main.cfg").value(), gamedata + "/data/_main.cfg" );
BOOST_CHECK_EQUAL( get_wml_location("core/_main.cfg").value(), gamedata + "/data/core/_main.cfg" );
BOOST_CHECK_EQUAL( get_wml_location(".").value(), gamedata + "/data/." );
BOOST_CHECK_EQUAL( get_wml_location(".").value(), "." );
BOOST_CHECK_EQUAL( get_wml_location("~/").value(), userdata + "/data/" );