1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | #include "filesystem.h" |
23 | |
24 | #ifdef USE_DATAFILE |
25 | #include "datafile.h" |
26 | #endif |
27 | #include "disk_filesystem.h" |
28 | #include "layered_filesystem.h" |
29 | #include "zip_exceptions.h" |
30 | #include "zip_filesystem.h" |
31 | |
32 | #include <config.h> |
33 | |
34 | #include <cassert> |
35 | #include <cerrno> |
36 | #include <climits> |
37 | #include <cstdio> |
38 | #include <cstdlib> |
39 | #include <cstring> |
40 | #include <string> |
41 | #include <vector> |
42 | #include <list> |
43 | #include <iterator> |
44 | |
45 | #ifdef WIN32 |
46 | #include "log.h" |
47 | #include <windows.h> |
48 | #include <io.h> |
49 | #include <direct.h>
|
50 | #else |
51 | #include <glob.h> |
52 | #include <sys/types.h> |
53 | #endif |
54 | #include <sys/stat.h> |
55 | #include <unistd.h> |
56 | |
57 | #ifdef WIN32 |
58 | #define stat _stat |
59 | #endif |
60 | |
61 | #ifdef _MSC_VER |
62 | #define S_ISDIR(x)((((x)) & 0170000) == (0040000)) ((x&_S_IFDIR)?1:0) |
63 | #define S_ISREG(x)((((x)) & 0170000) == (0100000)) ((x&_S_IFREG)?1:0) |
64 | #define PATH_MAX4096 MAX_PATH |
65 | #endif |
66 | |
67 | FileSystem::FileSystem() |
68 | { |
69 | m_root = ""; |
70 | #ifdef WIN32 |
71 | m_filesep = '\\'; |
72 | #else |
73 | m_filesep = '/'; |
74 | #endif |
75 | } |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | bool FileSystem::pathIsAbsolute(std::string const & path) const { |
83 | std::string::size_type const path_size = path .size(); |
84 | std::string::size_type const root_size = m_root.size(); |
85 | |
86 | if (path_size < root_size) |
87 | return false; |
88 | |
89 | if (path_size == root_size) |
90 | return path == m_root; |
91 | |
92 | if (path.compare(0, m_root.size(), m_root)) |
93 | return false; |
94 | |
95 | #ifdef WIN32 |
96 | if (path.size() >= 3 && path[1] == ':' && path[2] == '\\') |
97 | { |
98 | return true; |
99 | } |
100 | #endif |
101 | assert(root_size < path_size)((root_size < path_size) ? static_cast<void> (0) : __assert_fail ("root_size < path_size", "/home/arch/widelands/src/io/filesystem/filesystem.cc" , 101, __PRETTY_FUNCTION__)); |
102 | if (path[root_size] != m_filesep) |
103 | return false; |
104 | |
105 | return true; |
106 | } |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | std::string FileSystem::fixCrossFile(std::string const & path) const { |
115 | uint32_t path_size = path.size(); |
116 | std::string fixedPath(path); |
117 | std::string temp; |
118 | for (uint32_t i = 0; i < path_size; ++i) { |
119 | temp = path.at(i); |
120 | #ifdef WIN32 |
121 | if (temp == ":") |
122 | fixedPath.at(i) = '-'; |
123 | else if (temp == "/") |
124 | #else |
125 | if (temp == "\\") |
126 | #endif |
127 | fixedPath.at(i) = m_filesep; |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | if (temp == "~") |
134 | fixedPath.at(i) = '_'; |
135 | if (temp == "." && (i + 1 < path_size)) { |
136 | std::string temp2; |
137 | temp2 = path.at(i + 1); |
138 | if (temp2 == "." || temp2 == "\\" || temp2 == "/") |
139 | fixedPath.at(i) = '_'; |
140 | } |
141 | } |
142 | return fixedPath; |
143 | } |
144 | |
145 | |
146 | |
147 | |
148 | std::string FileSystem::getWorkingDirectory() const { |
149 | char cwd[PATH_MAX4096 + 1]; |
150 | char * const result = getcwd(cwd, PATH_MAX4096); |
151 | if (! result) |
152 | throw File_error("FileSystem::getWorkingDirectory()", "widelands", "can not run getcwd"); |
153 | |
154 | return std::string(cwd); |
155 | } |
156 | |
157 | |
158 | |
159 | std::string FileSystem::GetHomedir() |
160 | { |
161 | std::string homedir; |
162 | #ifdef WIN32 |
163 | |
164 | |
165 | |
166 | #define TRY_USE_AS_HOMEDIR(name) \ |
167 | homedir = getenv(name); \ |
168 | if (homedir.size() and check_writeable_for_data(homedir.c_str())) \ |
169 | return homedir; \ |
170 | |
171 | TRY_USE_AS_HOMEDIR("USERPROFILE"); |
172 | TRY_USE_AS_HOMEDIR("HOMEPATH"); |
173 | TRY_USE_AS_HOMEDIR("HOME"); |
174 | TRY_USE_AS_HOMEDIR("APPDATA"); |
175 | |
176 | log("None of the directories was useable - falling back to \".\"\n"); |
177 | #else |
178 | #ifdef HAS_GETENV |
179 | if (char const * const h = getenv("HOME")) |
180 | homedir = h; |
181 | #endif |
182 | #endif |
183 | |
184 | if (homedir.empty()) { |
185 | printf |
186 | ("\nWARNING: either we can not detect your home directory " |
187 | "or you do not have one! Please contact the developers.\n\n"); |
188 | |
189 | |
190 | |
191 | printf("Instead of your home directory, '.' will be used.\n\n"); |
192 | homedir = "."; |
193 | } |
194 | |
195 | return homedir; |
196 | } |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | template<typename Inserter> |
206 | static void FS_Tokenize |
207 | (std::string const & path, char const filesep, Inserter components) |
208 | { |
209 | std::string::size_type pos; |
210 | std::string::size_type pos2; |
211 | |
212 | |
213 | if (path.find(filesep) == 0) |
214 | pos = 1; |
215 | else |
216 | pos = 0; |
217 | pos2 = path.find(filesep, pos); |
218 | |
219 | |
220 | |
221 | while (pos2 != std::string::npos) { |
222 | if (pos != pos2) |
223 | { |
224 | std::string node = path.substr(pos, pos2 - pos); |
225 | *components++ = node; |
226 | } |
227 | pos = pos2 + 1; |
228 | pos2 = path.find(filesep, pos); |
229 | } |
230 | |
231 | |
232 | std::string node = path.substr(pos); |
233 | if (!node.empty()) |
234 | *components++ = node; |
235 | } |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | std::string FileSystem::FS_CanonicalizeName(std::string path) const { |
243 | std::list<std::string> components; |
244 | std::list<std::string>::iterator i; |
245 | |
246 | #ifdef WIN32 |
247 | |
248 | for (uint32_t j = 0; j < path.size(); ++j) { |
249 | if (path[j] == '/') |
250 | path[j] = '\\'; |
251 | } |
252 | #endif |
253 | |
254 | FS_Tokenize(path, m_filesep, std::inserter(components, components.begin())); |
255 | |
256 | |
257 | if (!components.empty() && *components.begin() == "~") { |
258 | components.erase(components.begin()); |
259 | FS_Tokenize |
260 | (GetHomedir(), |
261 | m_filesep, |
262 | std::inserter(components, components.begin())); |
263 | } else if (!pathIsAbsolute(path)) |
264 | |
265 | FS_Tokenize |
266 | (m_root.empty() ? getWorkingDirectory() : m_root, m_filesep, |
267 | std::inserter(components, components.begin())); |
268 | |
269 | |
270 | for (i = components.begin(); i != components.end();) { |
271 | char const * str = i->c_str(); |
272 | if (*str == '.') { |
273 | ++str; |
274 | |
275 | |
276 | if (*str == '\0') { |
277 | i = components.erase(i); |
278 | continue; |
279 | } |
280 | |
281 | else if (*str == '.' && *(str + 1) == '\0') { |
282 | if (i != components.begin()) { |
283 | #ifdef WIN32 |
284 | |
285 | if (--i != components.begin()) |
286 | i = components.erase(i); |
287 | else ++i; |
288 | #else |
289 | i = components.erase(--i); |
290 | #endif |
291 | } |
292 | i = components.erase(i); |
293 | continue; |
294 | } |
295 | } |
296 | |
297 | ++i; |
298 | } |
299 | |
300 | std::string canonpath; |
301 | canonpath.reserve(path.length()); |
302 | #ifndef WIN32 |
303 | for (i = components.begin(); i != components.end(); ++i) { |
304 | canonpath.push_back('/'); |
305 | canonpath += *i; |
306 | } |
307 | #else |
308 | for (i = components.begin(); i != components.end(); ++i) { |
309 | canonpath += *i; |
310 | canonpath += '\\'; |
311 | } |
312 | |
313 | |
314 | if (canonpath.size() > 1) canonpath.erase(canonpath.end() - 1); |
315 | #endif |
316 | |
317 | return canonpath; |
318 | } |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | char const * FileSystem::FS_Filename(char const * p) { |
325 | char const * result = p; |
326 | |
327 | while (*p != '\0') { |
328 | if (*p == '/' || *p == '\\') |
329 | result = p + 1; |
330 | ++p; |
331 | } |
332 | |
333 | return result; |
334 | } |
335 | |
336 | char const * FileSystem::FS_Filename(char const * p, char const * & extension) |
337 | { |
338 | extension = 0; |
339 | char const * result = p; |
340 | |
341 | while (*p != '\0') { |
342 | if (*p == '/' || *p == '\\') { |
343 | extension = 0; |
344 | result = p + 1; |
345 | } else if (*p == '.') |
346 | extension = p; |
347 | ++p; |
348 | } |
349 | |
350 | |
351 | if (not extension) |
352 | extension = p; |
353 | return result; |
354 | } |
355 | |
356 | std::string FileSystem::FS_FilenameWoExt(char const * const p) |
357 | { |
358 | char const * extension; |
| 1 | Variable 'extension' declared without an initial value | |
|
359 | std::string fname(p ? FileSystem::FS_Filename(p, extension) : ""); |
| |
| |
360 | return |
361 | extension ? fname.substr(0, fname.length() - strlen(extension)) : fname; |
| 4 | | Branch condition evaluates to a garbage value |
|
362 | } |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | FileSystem & FileSystem::Create(std::string const & root) |
375 | throw (FileType_error, FileNotFound_error, FileAccessDenied_error) |
376 | { |
377 | struct stat statinfo; |
378 | |
379 | if (stat(root.c_str(), &statinfo) == -1) { |
380 | if |
381 | (errno(*__errno_location ()) == EBADF9 || |
382 | errno(*__errno_location ()) == ENOENT2 || |
383 | errno(*__errno_location ()) == ENOTDIR20 || |
384 | #ifdef ELOOP40 |
385 | errno(*__errno_location ()) == ELOOP40 || |
386 | #endif |
387 | errno(*__errno_location ()) == ENAMETOOLONG36) |
388 | { |
389 | throw FileNotFound_error("FileSystem::Create", root); |
390 | } |
391 | if (errno(*__errno_location ()) == EACCES13) |
392 | throw FileAccessDenied_error("FileSystem::Create", root); |
393 | } |
394 | |
395 | if (S_ISDIR(statinfo.st_mode)((((statinfo.st_mode)) & 0170000) == (0040000))) { |
396 | return *new RealFSImpl(root); |
397 | } |
398 | if (S_ISREG(statinfo.st_mode)((((statinfo.st_mode)) & 0170000) == (0100000))) { |
399 | return *new ZipFilesystem(root); |
400 | } |
401 | |
402 | throw FileType_error |
403 | ("FileSystem::Create", root, |
404 | "cannot create virtual filesystem from file or directory"); |
405 | } |
406 | |
407 | #ifdef WIN32 |
408 | |
409 | |
410 | bool FileSystem::check_writeable_for_data(char const * const path) |
411 | { |
412 | RealFSImpl fs(path); |
413 | |
414 | if (fs.IsDirectory(".widelands")) |
415 | return true; |
416 | try { |
417 | |
418 | fs.EnsureDirectoryExists(".widelands"); |
419 | fs.Unlink(".widelands"); |
420 | return true; |
421 | } catch (...) { |
422 | log("Directory %s is not writeable - next try\n", path); |
423 | } |
424 | |
425 | return false; |
426 | } |
427 | #endif |