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; 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; return utils::nullopt;
} }
assert(game_config::path.empty() == false); bfs::path fpath(path);
bfs::path fpath(filename);
bfs::path result; bfs::path result;
if(filename[0] == '~') { if(path[0] == '~') {
result /= get_user_data_path() / "data" / filename.substr(1); result /= get_user_data_path() / "data" / path.substr(1);
DBG_FS << " trying '" << result.string() << "'"; DBG_FS << " trying '" << result.string() << "'";
} else if(*fpath.begin() == ".") { } else if(*fpath.begin() == ".") {
if(!current_dir.empty()) { if (current_dir) {
result /= bfs::path(current_dir); result /= bfs::path(*current_dir) / path;
} else { } 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)) { 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); 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, utils::optional<std::string> get_wml_location(const std::string& path, const utils::optional<std::string>& current_dir = utils::nullopt);
const std::string &current_dir = std::string());
/** /**
* Returns a short path to @a filename, skipping the (user) data directory. * 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("_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("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/" ); BOOST_CHECK_EQUAL( get_wml_location("~/").value(), userdata + "/data/" );