20 #define GETTEXT_DOMAIN "wesnoth-lib"
33 #include <boost/filesystem/fstream.hpp>
34 #include <boost/iostreams/device/file_descriptor.hpp>
35 #include <boost/iostreams/stream.hpp>
36 #include <boost/process.hpp>
40 #include <boost/locale.hpp>
47 #ifndef VOLUME_NAME_NONE
48 #define VOLUME_NAME_NONE 0x4
57 #if defined(__APPLE__) && defined(__MACH__) && defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
59 #define WESNOTH_BOOST_OS_IOS (__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__*1000)
60 #include <SDL2/SDL_filesystem.h>
66 #define DBG_FS LOG_STREAM(debug, log_filesystem)
67 #define LOG_FS LOG_STREAM(info, log_filesystem)
68 #define WRN_FS LOG_STREAM(warn, log_filesystem)
69 #define ERR_FS LOG_STREAM(err, log_filesystem)
72 namespace bfs = boost::filesystem;
73 using boost::system::error_code;
81 std::string
path = WESNOTH_PATH;
86 #ifdef DEFAULT_PREFS_PATH
101 const std::string maincfg_filename =
"_main.cfg";
102 const std::string finalcfg_filename =
"_final.cfg";
103 const std::string initialcfg_filename =
"_initial.cfg";
106 class customcodecvt :
public std::codecvt<wchar_t , char , std::mbstate_t>
110 template<
typename char_t_to>
111 struct customcodecvt_do_conversion_writer
113 customcodecvt_do_conversion_writer(char_t_to*& _to_next, char_t_to* _to_end)
122 bool can_push(std::size_t count)
const
124 return static_cast<std::size_t
>(to_end - to_next) > count;
127 void push(char_t_to val)
129 assert(to_next != to_end);
134 template<
typename char_t_from,
typename char_t_to>
135 static void customcodecvt_do_conversion(std::mbstate_t& ,
136 const char_t_from* from,
137 const char_t_from* from_end,
138 const char_t_from*& from_next,
148 customcodecvt_do_conversion_writer<char_t_to> writer(to_next, to_end);
150 while(from_next != from_end) {
157 int do_encoding()
const noexcept
163 bool do_always_noconv()
const noexcept
168 int do_length(std::mbstate_t& ,
const char* ,
const char* , std::size_t )
const
171 throw "Not supported";
174 std::codecvt_base::result unshift(
175 std::mbstate_t& ,
char* ,
char* ,
char*& )
const
178 throw "Not supported";
182 std::codecvt_base::result do_in(std::mbstate_t& state,
184 const char* from_end,
185 const char*& from_next,
188 wchar_t*& to_next)
const
191 customcodecvt_do_conversion<char, wchar_t>(state, from, from_end, from_next, to, to_end, to_next);
194 return std::codecvt_base::error;
197 return std::codecvt_base::ok;
200 std::codecvt_base::result do_out(std::mbstate_t& state,
202 const wchar_t* from_end,
203 const wchar_t*& from_next,
206 char*& to_next)
const
209 customcodecvt_do_conversion<wchar_t, char>(state, from, from_end, from_next, to, to_end, to_next);
212 return std::codecvt_base::error;
215 return std::codecvt_base::ok;
229 utf8_loc = std::locale(utf8_loc,
new customcodecvt());
231 boost::filesystem::path::imbue(utf8_loc);
235 static static_runner static_bfs_path_imbuer;
237 bool is_filename_case_correct(
const std::string& fname,
const boost::iostreams::file_descriptor_source& fd)
239 wchar_t real_path[MAX_PATH];
240 GetFinalPathNameByHandleW(fd.handle(), real_path, MAX_PATH - 1, VOLUME_NAME_NONE);
247 bool is_filename_case_correct(
const std::string& ,
const boost::iostreams::file_descriptor_source& )
295 vec->push_back(file.generic_string());
297 vec->push_back(file.filename().generic_string());
304 return ec && ec != boost::system::errc::no_such_file_or_directory;
312 LOG_FS <<
"Failed to check if " << fpath.string() <<
" is a directory: " << ec.message();
323 ERR_FS <<
"Failed to check existence of file " << fpath.string() <<
": " << ec.message();
334 bfs::create_directory(dirpath, ec);
337 ERR_FS <<
"Failed to create directory " << dirpath.string() <<
": " << ec.message();
345 ERR_FS <<
"Could not open or create directory " << dirpath.string();
346 return std::string();
355 bfs::file_status
fs = bfs::status(dirpath, ec);
358 ERR_FS <<
"Failed to retrieve file status for " << dirpath.string() <<
": " << ec.message();
361 DBG_FS <<
"directory " << dirpath.string() <<
" exists, not creating";
364 ERR_FS <<
"cannot create directory " << dirpath.string() <<
"; file exists";
368 bool created = bfs::create_directory(dirpath, ec);
370 ERR_FS <<
"Failed to create directory " << dirpath.string() <<
": " << ec.message();
378 DBG_FS <<
"creating recursive directory: " << dirpath.string();
380 if(dirpath.empty()) {
385 bfs::file_status
fs = bfs::status(dirpath);
388 ERR_FS <<
"Failed to retrieve file status for " << dirpath.string() <<
": " << ec.message();
399 ERR_FS <<
"Could not create parents to " << dirpath.string();
405 std::vector<std::string>* files,
406 std::vector<std::string>* dirs,
417 get_files_in_dir(absolute_dir.string(), files, dirs, mode, filter, reorder, checksum);
425 LOG_FS <<
"searching for _main.cfg in directory " << dir;
426 const bfs::path maincfg = dirpath / maincfg_filename;
429 LOG_FS <<
"_main.cfg found : " << maincfg;
436 bfs::directory_iterator di(dirpath, ec);
437 bfs::directory_iterator end;
444 for(; di != end; ++di) {
446 bfs::file_status st = di->status(ec);
448 LOG_FS <<
"Failed to get file status of " << di->path().string() <<
": " << ec.message();
452 if(st.type() == bfs::regular_file) {
454 std::string basename = di->path().filename().string();
457 if(!basename.empty() && basename[0] ==
'.')
463 if(checksum !=
nullptr) {
464 std::time_t mtime = bfs::last_write_time(di->path(), ec);
466 LOG_FS <<
"Failed to read modification time of " << di->path().string() <<
": " << ec.message();
467 }
else if(mtime > checksum->
modified) {
473 LOG_FS <<
"Failed to read filesize of " << di->path().string() <<
": " << ec.message();
480 }
else if(st.type() == bfs::directory_file) {
481 std::string basename = di->path().filename().string();
483 if(!basename.empty() && basename[0] ==
'.') {
491 const bfs::path inner_main(di->path() / maincfg_filename);
492 bfs::file_status main_st = bfs::status(inner_main, ec);
495 LOG_FS <<
"Failed to get file status of " << inner_main.string() <<
": " << ec.message();
497 LOG_FS <<
"_main.cfg found : "
506 if(files !=
nullptr) {
507 std::sort(files->begin(), files->end());
510 if(dirs !=
nullptr) {
511 std::sort(dirs->begin(), dirs->end());
516 for(
unsigned int i = 0;
i < files->size();
i++) {
517 if(
ends_with((*files)[
i],
"/" + finalcfg_filename)) {
518 files->push_back((*files)[
i]);
519 files->erase(files->begin() +
i);
526 for(
unsigned int i = 0;
i < files->size();
i++)
527 if(
ends_with((*files)[
i],
"/" + initialcfg_filename)) {
532 std::string initialcfg = (*files)[foundit];
533 for(
unsigned int i = foundit;
i > 0;
i--)
534 (*files)[
i] = (*files)[
i - 1];
535 (*files)[0] = initialcfg;
547 std::string next_filename;
551 std::stringstream filename;
556 filename.setf(std::ios_base::right);
557 filename << counter << extension;
560 next_filename = filename.str();
561 }
while(
file_exists(next_filename) && counter < 1000);
563 return next_filename;
570 std::ostringstream
s;
577 static std::string suffix;
589 #if defined(__APPLE__) && !defined(__IPHONEOS__)
595 static void migrate_apple_config_directory_for_unsandboxed_builds()
597 const char* home_str = getenv(
"HOME");
598 bfs::path home = home_str ? home_str :
".";
603 boost::filesystem::path new_saves_dir = home /
"Library/Containers/org.wesnoth.Wesnoth/Data/Library/Application Support/Wesnoth_";
608 LOG_FS <<
"Apple developer's userdata migration: symlinking " << old_saves_dir.string() <<
" to " << new_saves_dir.string();
609 bfs::create_symlink(new_saves_dir, old_saves_dir);
610 }
else if(!bfs::symbolic_link_exists(old_saves_dir)) {
611 ERR_FS <<
"Apple developer's userdata migration: Problem! Old (non-containerized) directory " << old_saves_dir.string() <<
" is not a symlink. Your savegames are scattered around 2 locations.";
621 #if defined(__APPLE__) && !defined(__IPHONEOS__)
622 migrate_apple_config_directory_for_unsandboxed_builds();
651 static bool is_path_relative_to_cwd(
const std::string& str)
659 return *
p.begin() ==
"." || *
p.begin() ==
"..";
665 [[maybe_unused]]
bool relative_ok =
false;
667 #ifdef PREFERENCES_DIR
668 if(newprefdir.empty()) {
669 newprefdir = PREFERENCES_DIR;
675 if(newprefdir.size() > 2 && newprefdir[1] ==
':') {
678 }
else if(is_path_relative_to_cwd(newprefdir)) {
682 if(newprefdir.empty()) {
685 #ifdef PREFERENCES_DIR
686 if (newprefdir != PREFERENCES_DIR)
693 _(
"Use an absolute path, or a relative path that starts with a period and a backslash"));
697 PWSTR docs_path =
nullptr;
698 HRESULT res = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_CREATE,
nullptr, &docs_path);
704 ERR_FS <<
"Could not determine path to user's Documents folder! (" << std::hex <<
"0x" << res << std::dec <<
") "
705 <<
"User config/data directories may be unavailable for "
706 <<
"this session. Please report this as a bug.";
715 CoTaskMemFree(docs_path);
722 #ifdef WESNOTH_BOOST_OS_IOS
723 char *sdl_pref_path = SDL_GetPrefPath(
"wesnoth.org",
"iWesnoth");
725 backupprefdir = std::string(sdl_pref_path) + backupprefdir;
726 SDL_free(sdl_pref_path);
731 const char* home_str = getenv(
"HOME");
733 if(newprefdir.empty()) {
734 char const* xdg_data = getenv(
"XDG_DATA_HOME");
735 if(!xdg_data || xdg_data[0] ==
'\0') {
737 newprefdir = backupprefdir;
751 bfs::path home = home_str ? home_str :
".";
753 if(newprefdir[0] ==
'/') {
761 _(
"Use absolute paths. Relative paths are deprecated because they are interpreted relative to $HOME"));
767 if(newprefdir.empty()) {
768 newprefdir = backupprefdir;
772 const char* home_str = getenv(
"HOME");
773 bfs::path home = home_str ? home_str :
".";
775 if(newprefdir[0] ==
'/') {
783 _(
"Use absolute paths. Relative paths are deprecated because they are interpreted relative to $HOME"));
794 bool rename_dir(
const std::string& old_dir,
const std::string& new_dir)
797 bfs::rename(old_dir, new_dir, ec);
800 ERR_FS <<
"Failed to rename directory '" << old_dir <<
"' to '" << new_dir <<
"'";
823 ERR_FS <<
"could not open or create cache directory at " <<
cache_dir.string() <<
'\n';
844 #if defined(_X11) && !defined(PREFERENCES_DIR)
845 char const* xdg_config = getenv(
"XDG_CONFIG_HOME");
847 if(!xdg_config || xdg_config[0] ==
'\0') {
848 xdg_config = getenv(
"HOME");
883 #if defined(_X11) && !defined(PREFERENCES_DIR)
884 char const* xdg_cache = getenv(
"XDG_CACHE_HOME");
886 if(!xdg_cache || xdg_cache[0] ==
'\0') {
887 xdg_cache = getenv(
"HOME");
911 #if !defined(_WIN32) && !defined(_X11) && !defined(__APPLE__)
918 if(w_ver.major_version() != 1 || ms_ver.major_version() != 1) {
923 std::vector<other_version_dir> result;
926 for(
auto minor = w_ver.minor_version() + 4; minor >= ms_ver.minor_version(); --minor) {
927 if(minor == w_ver.minor_version())
932 version.set_minor_version(minor);
949 #elif defined(__APPLE__)
954 result.emplace_back(suffix,
path.string());
968 ERR_FS <<
"Failed to get current directory: " << ec.message();
972 return cwd.generic_string();
981 ERR_FS <<
"Failed to set current directory: " << ec.message();
984 LOG_FS <<
"Process working directory set to " << dir;
993 wchar_t process_path[MAX_PATH];
994 SetLastError(ERROR_SUCCESS);
996 GetModuleFileNameW(
nullptr, process_path, MAX_PATH);
998 if(GetLastError() != ERROR_SUCCESS) {
1003 return exe.parent_path().string();
1009 bfs::path exe = bfs::read_symlink(self_exe, ec);
1011 return exe.parent_path().string();
1019 bfs::path search = bp::search_path(exe).string();
1020 if(!search.string().empty()) {
1021 return search.parent_path().string();
1026 search = bp::search_path(exe).string();
1027 if(!search.string().empty()) {
1028 return search.parent_path().string();
1039 bool created = bfs::create_directory(
bfs::path(dirname), ec);
1041 ERR_FS <<
"Failed to create directory " << dirname <<
": " << ec.message();
1050 std::vector<std::string> files;
1051 std::vector<std::string> dirs;
1056 if(!files.empty()) {
1057 for(
const std::string&
f : files) {
1060 LOG_FS <<
"remove(" <<
f <<
"): " << ec.message();
1067 for(
const std::string&
d : dirs) {
1078 LOG_FS <<
"remove(" << dirname <<
"): " << ec.message();
1091 ERR_FS <<
"Could not delete file " << filename <<
": " << ec.message();
1099 std::ifstream file(fname, std::ios::binary);
1100 std::vector<uint8_t> file_contents;
1102 file_contents.reserve(
file_size(fname));
1103 file_contents.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
1105 return file_contents;
1113 std::string img =
"";
1115 if(name.find(
".") != std::string::npos) {
1117 img =
"data:image/"+name.substr(name.find(
".")+1)+
";base64,"+
base64::encode(view);
1126 std::stringstream ss;
1133 LOG_FS <<
"Streaming " << fname <<
" for reading.";
1136 ERR_FS <<
"Trying to open file with empty name.";
1138 s->clear(std::ios_base::failbit);
1145 boost::iostreams::file_descriptor_source fd(
bfs::path(fname), std::ios_base::binary);
1148 if(!fd.is_open() && treat_failure_as_error) {
1149 ERR_FS <<
"Could not open '" << fname <<
"' for reading.";
1150 }
else if(!is_filename_case_correct(fname, fd)) {
1151 ERR_FS <<
"Not opening '" << fname <<
"' due to case mismatch.";
1153 s->clear(std::ios_base::failbit);
1157 return std::make_unique<boost::iostreams::stream<boost::iostreams::file_descriptor_source>>(fd, 4096, 0);
1158 }
catch(
const std::exception&) {
1159 if(treat_failure_as_error) {
1160 ERR_FS <<
"Could not open '" << fname <<
"' for reading.";
1164 s->clear(std::ios_base::failbit);
1171 LOG_FS <<
"streaming " << fname <<
" for writing.";
1173 boost::iostreams::file_descriptor_sink fd(
bfs::path(fname), mode);
1174 return std::make_unique<boost::iostreams::stream<boost::iostreams::file_descriptor_sink>>(fd, 4096, 0);
1175 }
catch(
const BOOST_IOSTREAMS_FAILURE&
e) {
1178 error_code ec_unused;
1179 if(create_directory && bfs::create_directories(
bfs::path(fname).parent_path(), ec_unused)) {
1188 void write_file(
const std::string& fname,
const std::string&
data, std::ios_base::openmode mode)
1191 os->exceptions(std::ios_base::goodbit);
1193 const std::size_t block_size = 4096;
1194 char buf[block_size];
1196 for(std::size_t
i = 0;
i <
data.size();
i += block_size) {
1197 const std::size_t bytes = std::min<std::size_t>(block_size,
data.size() -
i);
1198 std::copy(
data.begin() +
i,
data.begin() +
i + bytes, buf);
1200 os->write(buf, bytes);
1202 throw io_exception(
"Error writing to file: '" + fname +
"'");
1207 void copy_file(
const std::string& src,
const std::string& dest)
1235 std::time_t mtime = bfs::last_write_time(
bfs::path(fname), ec);
1237 LOG_FS <<
"Failed to read modification time of " << fname <<
": " << ec.message();
1245 return bfs::path(filename).extension() ==
".gz";
1250 return bfs::path(filename).extension() ==
".bz2";
1258 LOG_FS <<
"Failed to read filesize of " << fname <<
": " << ec.message();
1260 }
else if(
size > INT_MAX) {
1270 uintmax_t size_sum = 0;
1272 for(bfs::recursive_directory_iterator
i(
p), end;
i != end && !ec; ++
i) {
1273 if(bfs::is_regular_file(
i->path())) {
1279 LOG_FS <<
"Failed to read directorysize of " << pname <<
": " << ec.message();
1281 }
else if(size_sum > INT_MAX) {
1288 std::string
base_name(
const std::string& file,
const bool remove_extension)
1290 if(!remove_extension) {
1291 return bfs::path(file).filename().string();
1299 return bfs::path(file).parent_path().string();
1312 p =
p.parent_path();
1317 }
while(ec && !
is_root(
p.string()));
1319 return ec ?
"" :
p.string();
1325 const std::string
s = std::string(1,
c);
1331 return bfs::path::preferred_separator;
1339 return ec ? false : !
p.has_parent_path();
1362 const std::wstring& wpath =
bfs::path{
path}.make_preferred().wstring();
1363 return PathIsRootW(wpath.c_str()) == TRUE;
1377 std::string
normalize_path(
const std::string& fpath,
bool normalize_separators,
bool resolve_dot_entries)
1384 bfs::path p = resolve_dot_entries ? bfs::canonical(fpath, ec) : bfs::absolute(fpath);
1390 if(normalize_separators) {
1391 return p.make_preferred().string();
1411 std::set<std::string> binary_paths;
1413 typedef std::map<std::string, std::vector<std::string>> paths_map;
1414 paths_map binary_paths_cache;
1420 if(binary_paths.empty()) {
1421 binary_paths.insert(
"");
1447 std::string
path = bp[
"path"].str();
1448 if(
path.find(
"..") != std::string::npos) {
1449 ERR_FS <<
"Invalid binary path '" <<
path <<
"'";
1453 if(!
path.empty() &&
path.back() !=
'/')
1455 if(binary_paths.count(
path) == 0) {
1456 binary_paths.insert(
path);
1464 binary_paths_cache.clear();
1466 for(
const std::string&
p :
paths_) {
1467 binary_paths.erase(
p);
1473 binary_paths_cache.clear();
1478 DBG_FS <<
"Looking for '" << filename_str <<
"'.";
1480 if(filename_str.empty()) {
1481 LOG_FS <<
" invalid filename";
1485 if(filename_str.find(
"..") != std::string::npos) {
1486 ERR_FS <<
"Illegal path '" << filename_str <<
"' (\"..\" not allowed).";
1490 if(filename_str.find(
'\\') != std::string::npos) {
1491 ERR_FS <<
"Illegal path '" << filename_str
1492 << R
"end(' ("\" not allowed, for compatibility with GNU/Linux and macOS).)end";
1499 ERR_FS <<
"Illegal path '" << filename_str <<
"' (blacklisted filename).";
1503 if(std::any_of(filepath.begin(), filepath.end(),
1504 [](
const bfs::path& dirname) { return default_blacklist.match_dir(dirname.string()); })) {
1505 ERR_FS <<
"Illegal path '" << filename_str <<
"' (blacklisted directory name).";
1518 const paths_map::const_iterator itor = binary_paths_cache.find(
type);
1519 if(itor != binary_paths_cache.end()) {
1520 return itor->second;
1523 if(
type.find(
"..") != std::string::npos) {
1525 ERR_FS <<
"Invalid WML type '" <<
type <<
"' for binary paths";
1526 static std::vector<std::string> dummy;
1530 std::vector<std::string>& res = binary_paths_cache[
type];
1534 for(
const std::string&
path : binary_paths) {
1561 std::string::size_type pos = filename.rfind(
"../");
1562 if(pos != std::string::npos) {
1568 return std::string();
1576 DBG_FS <<
" checking '" << bp <<
"'";
1579 DBG_FS <<
" found at '" << bpath.string() <<
"'";
1580 if(result.empty()) {
1581 result = bpath.string();
1583 WRN_FS <<
"Conflicting files in binary_path: '" << result
1584 <<
"' and '" << bpath.string() <<
"'";
1596 return std::string();
1602 DBG_FS <<
" checking '" << bp <<
"'";
1604 DBG_FS <<
" found at '" << bpath.string() <<
"'";
1605 return bpath.string();
1610 return std::string();
1616 return std::string();
1624 if(filename[0] ==
'~') {
1626 DBG_FS <<
" trying '" << result.string() <<
"'";
1627 }
else if(*fpath.begin() ==
".") {
1628 if(!current_dir.empty()) {
1643 DBG_FS <<
" found: '" << result.string() <<
"'";
1646 return result.string();
1651 bfs::path::iterator fi = full.begin(), fe = full.end(), pi = prefix.begin(), pe = prefix.end();
1652 while(fi != fe && pi != pe && *fi == *pi) {
1674 return "~" +
partial.generic_string();
1679 return partial.generic_string();
1689 if(full_path.empty()) {
1690 return full_path.generic_string();
1695 return partial.generic_string();
1700 return partial.generic_string();
1703 return full_path.generic_string();
1708 const std::string real_program_name(program_name
1717 return real_program_name;
1723 const char* user_name = getenv(
"USERNAME");
1725 const char* user_name = getenv(
"USER");
1729 if(user_name !=
nullptr) {
1730 boost::replace_all(canonicalized, user_name,
"USER");
1733 return canonicalized;
1743 const std::size_t pos_ext = base.rfind(
".");
1745 std::string loc_base;
1746 if(pos_ext != std::string::npos) {
1747 loc_base = base.substr(0, pos_ext) + suff + base.substr(pos_ext);
1749 loc_base = base + suff;
1762 std::vector<std::string> langs =
utils::split(
_(
"language code for localized resources^en_US"));
1767 langs.push_back(
"en_US");
1768 for(
const std::string& lang : langs) {
1769 std::string loc_file = dir +
"/" +
"l10n" +
"/" + lang +
"/" + loc_base;
1783 if(full_path.find(addons_path) == 0) {
1785 if(
path.size() > 0) {
1786 return path.begin()->string();
A config object defines a single node in a WML file, with access to child nodes.
bool match_file(const std::string &name) const
A class grating read only view to a vector of config objects, viewed as one config with all children ...
config_array_view child_range(config_key_type key) const
Represents version numbers.
void set_major_version(unsigned int)
Sets the major version number.
unsigned int minor_version() const
Retrieves the minor version number (x2 in "x1.x2.x3").
unsigned int major_version() const
Retrieves the major version number (x1 in "x1.x2.x3").
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
static lg::log_domain log_filesystem("filesystem")
Declarations for File-IO.
static std::string _(const char *str)
Standard logging facilities (interface).
std::string encode(utils::byte_string_view bytes)
std::string get_legacy_editor_dir()
std::string get_cache_dir()
std::time_t file_modified_time(const std::string &fname)
Get the modification time of a file.
bool is_bzip2_file(const std::string &filename)
Returns true if the file ends with '.bz2'.
int dir_size(const std::string &pname)
Returns the sum of the sizes of the files contained in a directory.
bool is_relative(const std::string &path)
Returns whether the path seems to be relative.
static const bfs::path & get_user_data_path()
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
std::string get_user_config_dir()
std::string get_localized_path(const std::string &file, const std::string &suff)
Returns the localized version of the given filename, if it exists.
bool is_root(const std::string &path)
Returns whether the path is the root of the file hierarchy.
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
static bfs::path get_dir(const bfs::path &dirpath)
std::string get_user_data_dir()
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
std::string get_wml_persist_dir()
bool rename_dir(const std::string &old_dir, const std::string &new_dir)
static bool is_legal_file(const std::string &filename_str)
void copy_file(const std::string &src, const std::string &dest)
Read a file and then writes it back out.
bool delete_file(const std::string &filename)
bool is_gzip_file(const std::string &filename)
Returns true if the file ends with '.gz'.
static bool file_exists(const bfs::path &fpath)
std::string get_exe_dir()
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
static bool is_directory_internal(const bfs::path &fpath)
bool delete_directory(const std::string &dirname, const bool keep_pbl)
std::string get_saves_dir()
std::string get_wml_location(const std::string &filename, const std::string ¤t_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
std::string get_program_invocation(const std::string &program_name)
Returns the appropriate invocation for a Wesnoth-related binary, assuming that it is located in the s...
std::string get_binary_dir_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual directory of a given type or an empty string if the directory i...
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
bool ends_with(const std::string &str, const std::string &suffix)
std::string get_independent_binary_file_path(const std::string &type, const std::string &filename)
Returns an asset path to filename for binary path-independent use in saved games.
void set_user_config_dir(const std::string &newconfigdir)
std::string get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type or an empty string if the file isn't prese...
filesystem::scoped_ostream ostream_file(const std::string &fname, std::ios_base::openmode mode, bool create_directory)
static bool create_directory_if_missing_recursive(const bfs::path &dirpath)
int file_size(const std::string &fname)
Returns the size of a file, or -1 if the file doesn't exist.
std::string read_file_as_data_uri(const std::string &fname)
void set_cache_dir(const std::string &newcachedir)
std::unique_ptr< std::istream > scoped_istream
void clear_binary_paths_cache()
static bfs::path user_data_dir
std::string get_addon_id_from_path(const std::string &location)
Returns the add-on ID from a path.
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode)
Throws io_exception if an error occurs.
std::string get_short_wml_path(const std::string &filename)
Returns a short path to filename, skipping the (user) data directory.
std::string root_name(const std::string &path)
Returns the name of the root device if included in the given path.
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
std::string get_logs_dir()
std::unique_ptr< std::ostream > scoped_ostream
static bool create_directory_if_missing(const bfs::path &dirpath)
std::string nearest_extant_parent(const std::string &file)
Finds the nearest parent in existence for a file or directory.
char path_separator()
Returns the standard path separator for the current platform.
bool looks_like_pbl(const std::string &file)
std::string get_addons_data_dir()
static void push_if_exists(std::vector< std::string > *vec, const bfs::path &file, bool full)
bool make_directory(const std::string &dirname)
static void setup_user_data_dir()
const blacklist_pattern_list default_blacklist
std::string get_addons_dir()
static bfs::path user_config_dir
bool set_cwd(const std::string &dir)
std::vector< other_version_dir > find_other_version_saves_dirs()
Searches for directories containing saves created by other versions of Wesnoth.
static void set_user_config_path(bfs::path newconfig)
std::string get_next_filename(const std::string &name, const std::string &extension)
Get the next free filename using "name + number (3 digits) + extension" maximum 1000 files then start...
static bfs::path subtract_path(const bfs::path &full, const bfs::path &prefix)
static void set_cache_path(bfs::path newcache)
std::vector< uint8_t > read_file_binary(const std::string &fname)
std::string sanitize_path(const std::string &path)
Sanitizes a path to remove references to the user's name.
const std::string get_version_path_suffix(const version_info &version)
const std::vector< std::string > & get_binary_paths(const std::string &type)
Returns a vector with all possible paths to a given type of binary, e.g.
static bool error_except_not_found(const error_code &ec)
bool is_path_sep(char c)
Returns whether c is a path separator.
static bfs::path cache_dir
std::string normalize_path(const std::string &fpath, bool normalize_separators, bool resolve_dot_entries)
Returns the absolute path of a file.
void set_user_data_dir(std::string newprefdir)
static void init_binary_paths()
Game configuration data as global variables.
const version_info min_savegame_version(MIN_SAVEGAME_VERSION)
std::string default_preferences_path
const version_info wesnoth_version(VERSION)
const std::string observer_team_name
observer team name used for observer team chat
int cache_compression_level
void remove()
Removes a tip.
bool exists(const image::locator &i_locator)
Returns true if the given image actually exists, without loading it.
void move_log_file()
Move the log file to another directory.
rng * generator
This generator is automatically synced during synced context.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
std::basic_string_view< uint8_t > byte_string_view
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
std::vector< std::string > split(const config_attribute_value &val)
std::string::const_iterator iterator
@ partial
There are still moves and/or attacks possible, but the unit doesn't fit in the "unmoved" status.
void read(config &cfg, std::istream &in, abstract_validator *validator)
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
std::vector< std::string > paths_
void set_paths(const game_config_view &cfg)
An exception object used when an IO error occurs.
static map_location::DIRECTION s