The Battle for Wesnoth  1.15.11+dev
filesystem_common.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include <fstream>
15 
16 #include "filesystem.hpp"
17 #include "wesconfig.h"
18 
19 #include "config.hpp"
20 #include "game_config.hpp"
21 #include "log.hpp"
24 
25 #include <boost/algorithm/string.hpp>
26 
27 static lg::log_domain log_filesystem("filesystem");
28 #define LOG_FS LOG_STREAM(info, log_filesystem)
29 #define ERR_FS LOG_STREAM(err, log_filesystem)
30 
31 namespace filesystem
32 {
33 
34 bool is_legal_user_file_name(const std::string& name, bool allow_whitespace)
35 {
36  //
37  // IMPORTANT NOTE:
38  //
39  // If you modify this function you must be aware that it is used by the
40  // add-on server validation routines both on the client and server sides.
41  // The addition or removal of any criteria here should be carefully
42  // evaluated with this in mind.
43  //
44 
45  if(name.empty() || name.back() == '.' || name.find("..") != std::string::npos || name.size() > 255) {
46  return false;
47  }
48 
49  // Reserved DOS device names on Windows.
50  static const std::set<std::string> dos_device_names = {
51  // Hardware devices
52  "NUL", "CON", "AUX", "PRN",
53  "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
54  "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
55  // Console API pseudo-devices
56  "CONIN$", "CONOUT$",
57  };
58 
59  // We can't use filesystem::base_name() here, because it returns the
60  // filename up to the *last* dot. "CON.foo.bar" is still redirected to
61  // "CON" on Windows, although "foo.CON.bar" and "foo.bar.CON" are not.
62  //
63  // Do also note that we're relying on the char-by-char check further below
64  // to flag the name as illegal if it contains a colon ':', the reason
65  // being that is valid to refer to DOS device names with a trailing colon
66  // (e.g. "CON:" is synonymous with "CON").
67 
68  const auto& first_name =
69  boost::algorithm::to_upper_copy(name.substr(0, name.find('.')), std::locale::classic());
70 
71  if(dos_device_names.count(first_name)) {
72  return false;
73  }
74 
75  const auto& name_ucs4 = unicode_cast<std::u32string>(name);
76  if(name != unicode_cast<std::string>(name_ucs4)){
77  return false; // name is an invalid UTF-8 sequence
78  }
79 
80  return name_ucs4.end() == std::find_if(name_ucs4.begin(), name_ucs4.end(), [=](char32_t c)
81  {
82  switch(c) {
83  case ' ':
84  return !allow_whitespace;
85  case '"':
86  case '*':
87  case '/':
88  case ':':
89  case '<':
90  case '>':
91  case '?':
92  case '\\':
93  case '|':
94  case '~':
95  case 0x7F: // DEL
96  return true;
97  default:
98  return c < 0x20 || // C0 control characters
99  (c >= 0x80 && c < 0xA0) || // C1 control characters
100  (c >= 0xD800 && c < 0xE000); // surrogate pairs
101  }
102  });
103 }
104 
105 void blacklist_pattern_list::remove_blacklisted_files_and_dirs(std::vector<std::string>& files, std::vector<std::string>& directories) const
106 {
107  files.erase(
108  std::remove_if(files.begin(), files.end(), [this](const std::string& name) { return match_file(name); }),
109  files.end());
110  directories.erase(
111  std::remove_if(directories.begin(), directories.end(), [this](const std::string& name) { return match_dir(name); }),
112  directories.end());
113 }
114 
115 bool blacklist_pattern_list::match_file(const std::string& name) const
116 {
117  return std::any_of(file_patterns_.begin(), file_patterns_.end(),
118  std::bind(&utils::wildcard_string_match, std::ref(name), std::placeholders::_1));
119 }
120 
121 bool blacklist_pattern_list::match_dir(const std::string& name) const
122 {
123  return std::any_of(directory_patterns_.begin(), directory_patterns_.end(),
124  std::bind(&utils::wildcard_string_match, std::ref(name), std::placeholders::_1));
125 }
126 
127 std::string get_prefs_file()
128 {
129  return get_user_config_dir() + "/preferences";
130 }
131 
132 std::string get_credentials_file()
133 {
134  return get_user_config_dir() + "/credentials";
135 }
136 
138 {
139 #ifdef HAS_RELATIVE_DEFPREF
141 #else
143 #endif
144 }
145 
146 std::string get_save_index_file()
147 {
148  return get_user_data_dir() + "/save_index";
149 }
150 
151 std::string get_saves_dir()
152 {
153  const std::string dir_path = get_user_data_dir() + "/saves";
154  return get_dir(dir_path);
155 }
156 
157 std::string get_addons_dir()
158 {
159  const std::string dir_path = get_user_data_dir() + "/data/add-ons";
160  return get_dir(dir_path);
161 }
162 
163 std::string get_intl_dir()
164 {
165 #ifdef _WIN32
166  return game_config::path + "/" LOCALEDIR;
167 #else
168 
169 #ifdef USE_INTERNAL_DATA
170  return get_cwd() + "/" LOCALEDIR;
171 #endif
172 
173 #if HAS_RELATIVE_LOCALEDIR
174  std::string res = game_config::path + "/" LOCALEDIR;
175 #else
176  std::string res = LOCALEDIR;
177 #endif
178 
179  return res;
180 #endif
181 }
182 
183 std::string get_screenshot_dir()
184 {
185  const std::string dir_path = get_user_data_dir() + "/screenshots";
186  return get_dir(dir_path);
187 }
188 
189 bool looks_like_pbl(const std::string& file)
190 {
191  return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl");
192 }
193 
195  : nfiles(0), sum_size(0), modified(0)
196 {}
197 
199  nfiles (cfg["nfiles"].to_size_t()),
200  sum_size(cfg["size"].to_size_t()),
201  modified(cfg["modified"].to_time_t())
202 {
203 }
204 
206 {
207  cfg["nfiles"] = nfiles;
208  cfg["size"] = sum_size;
209  cfg["modified"] = modified;
210 }
211 
213 {
214  return nfiles == rhs.nfiles && sum_size == rhs.sum_size &&
215  modified == rhs.modified;
216 }
217 
218 bool ends_with(const std::string& str, const std::string& suffix)
219 {
220  return str.size() >= suffix.size() && std::equal(suffix.begin(),suffix.end(),str.end()-suffix.size());
221 }
222 
223 std::string read_map(const std::string& name)
224 {
225  std::string res;
226  std::string map_location = get_wml_location(name);
227  if(map_location.empty()) {
228  // If this is an add-on or campaign that's set the [binary_path] for its image directory,
229  // automatically check for a sibling maps directory.
230  map_location = get_binary_file_location("maps", name);
231  }
232  if(!map_location.empty()) {
233  res = read_file(map_location);
234  }
235 
236  if(res.empty()) {
237  res = read_file(get_user_data_dir() + "/editor/maps/" + name);
238  }
239 
240  return res;
241 }
242 
243 static void get_file_tree_checksum_internal(const std::string& path, file_tree_checksum& res)
244 {
245 
246  std::vector<std::string> dirs;
248 
249  for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
251  }
252 }
253 
255 {
256  static file_tree_checksum checksum;
257  if (reset)
258  checksum.reset();
259  if(checksum.nfiles == 0) {
260  get_file_tree_checksum_internal("data/",checksum);
261  get_file_tree_checksum_internal(get_user_data_dir() + "/data/",checksum);
262  LOG_FS << "calculated data tree checksum: "
263  << checksum.nfiles << " files; "
264  << checksum.sum_size << " bytes" << std::endl;
265  }
266 
267  return checksum;
268 }
269 
270 }
bool is_legal_user_file_name(const std::string &name, bool allow_whitespace=true)
Returns whether the given filename is a legal name for a user-created file.
static void get_file_tree_checksum_internal(const std::string &path, file_tree_checksum &res)
bool looks_like_pbl(const std::string &file)
bool ends_with(const std::string &str, const std::string &suffix)
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using &#39;*&#39; as any number of characters (including none), &#39;+&#39; as one or more characters, and &#39;?&#39; as any one character.
std::string get_screenshot_dir()
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&#39;t prese...
std::string get_saves_dir()
Definitions for the interface to Wesnoth Markup Language (WML).
void remove_blacklisted_files_and_dirs(std::vector< std::string > &files, std::vector< std::string > &directories) const
static bfs::path get_dir(const bfs::path &dirpath)
Definition: filesystem.cpp:274
std::string get_cwd()
Definition: filesystem.cpp:876
bool match_file(const std::string &name) const
std::string get_user_data_dir()
Definition: filesystem.cpp:789
std::vector< std::string > directory_patterns_
Definition: filesystem.hpp:85
std::string get_intl_dir()
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)
Populates &#39;files&#39; with all the files and &#39;dirs&#39; with all the directories in dir.
Definition: filesystem.cpp:349
std::string path
Definition: game_config.cpp:38
std::vector< std::string > file_patterns_
Definition: filesystem.hpp:84
bool operator==(const file_tree_checksum &rhs) const
std::string get_default_prefs_file()
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:995
std::string read_map(const std::string &name)
Encapsulates the map of the game.
Definition: location.hpp:37
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
#define LOG_FS
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn&#39;t pres...
Declarations for File-IO.
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:51
const file_tree_checksum & data_tree_checksum(bool reset=false)
Get the time at which the data/ tree was last modified at.
std::string get_user_config_dir()
Definition: filesystem.cpp:760
std::string get_addons_dir()
std::string get_credentials_file()
Standard logging facilities (interface).
static lg::log_domain log_filesystem("filesystem")
std::string get_prefs_file()
bool match_dir(const std::string &name) const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
mock_char c
std::string get_save_index_file()
#define LOCALEDIR
Definition: wesconfig.h:19
std::string default_preferences_path
Definition: game_config.cpp:44