File: FileSystem.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (246 lines) | stat: -rwxr-xr-x 5,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

/**
 * Glob conversion by Chris Han (based on work by Nathaniel Smith).
 */
#ifdef _MSC_VER
#include "System/Platform/Win/win32.h"
#endif
#include "FileSystem.h"

#include "System/Log/ILog.h"
#include "System/Util.h"
#include "System/mmgr.h"

#include <boost/regex.hpp>

////////////////////////////////////////
////////// FileSystem

/**
 * @brief quote macro
 * @param c Character to test
 * @param str string currently being built
 *
 * Given an std::string str that we are assembling,
 * and an upcoming char c, will append
 * an extra '\\' to quote the character if necessary.
 * The do-while is used for legalizing the ';' in "QUOTE(c, regex);".
 */
#define QUOTE(c,str)			\
	do {					\
		if (!(isalnum(c) || (c) == '_'))	\
			str += '\\';		\
		str += c;				\
	} while (0)

std::string FileSystem::ConvertGlobToRegex(const std::string& glob)
{
	std::string regex;
	regex.reserve(glob.size()<<1);
	int braces = 0;
	for (std::string::const_iterator i = glob.begin(); i != glob.end(); ++i) {
		char c = *i;
#ifdef DEBUG
		if (braces >= 5) {
			LOG_L(L_WARNING, "%s: braces nested too deeply\n%s", __FUNCTION__, glob.c_str());
		}
#endif
		switch (c) {
			case '*':
				regex += ".*";
				break;
			case '?':
				regex += '.';
				break;
			case '{':
				braces++;
				regex += '(';
				break;
			case '}':
#ifdef DEBUG
				if (braces == 0) {
					LOG_L(L_WARNING, "%s: closing brace without an equivalent opening brace\n%s", __FUNCTION__, glob.c_str());
				}
#endif
				regex += ')';
				braces--;
				break;
			case ',':
				if (braces > 0) {
					regex += '|';
				} else {
					QUOTE(c,regex);
				}
				break;
			case '\\':
				++i;
#ifdef DEBUG
				if (i == glob.end()) {
					LOG_L(L_WARNING, "%s: pattern ends with backslash\n%s", __FUNCTION__, glob.c_str());
				}
#endif
				QUOTE(*i,regex);
				break;
			default:
				QUOTE(c,regex);
				break;
		}
	}

#ifdef DEBUG
	if (braces > 0) {
		LOG_L(L_WARNING, "%s: unterminated brace expression\n%s", __FUNCTION__, glob.c_str());
	} else if (braces < 0) {
		LOG_L(L_WARNING, "%s: too many closing braces\n%s", __FUNCTION__, glob.c_str());
	}
#endif

	return regex;
}

bool FileSystem::FileExists(std::string file)
{
	FixSlashes(file);
	return FileSystemAbstraction::FileExists(file);
}

size_t FileSystem::GetFileSize(std::string file)
{
	if (!CheckFile(file)) {
		return 0;
	}
	FixSlashes(file);
	return FileSystemAbstraction::GetFileSize(file);
}

bool FileSystem::CreateDirectory(std::string dir)
{
	if (!CheckFile(dir)) {
		return false;
	}

	ForwardSlashes(dir);
	size_t prev_slash = 0, slash;
	while ((slash = dir.find('/', prev_slash + 1)) != std::string::npos) {
		std::string pathPart = dir.substr(0, slash);
		if (!FileSystemAbstraction::IsFSRoot(pathPart) && !FileSystemAbstraction::MkDir(pathPart)) {
			return false;
		}
		prev_slash = slash;
	}
	return FileSystemAbstraction::MkDir(dir);
}




std::string FileSystem::GetDirectory(const std::string& path)
{
	size_t s = path.find_last_of("\\/");
	if (s != std::string::npos) {
		return path.substr(0, s + 1);
	}
	return ""; // XXX return "./"? (a short test caused a crash because CFileHandler used in Lua couldn't find a file in the base-dir)
}

std::string FileSystem::GetFilename(const std::string& path)
{
	size_t s = path.find_last_of("\\/");
	if (s != std::string::npos) {
		return path.substr(s + 1);
	}
	return path;
}

std::string FileSystem::GetBasename(const std::string& path)
{
	std::string fn = GetFilename(path);
	size_t dot = fn.find_last_of('.');
	if (dot != std::string::npos) {
		return fn.substr(0, dot);
	}
	return fn;
}

std::string FileSystem::GetExtension(const std::string& path)
{
	const std::string fileName = GetFilename(path);
	size_t l = fileName.length();
//#ifdef WIN32
	//! windows eats dots and spaces at the end of filenames
	while (l > 0) {
		const char prevChar = fileName[l-1];
		if ((prevChar == '.') || (prevChar == ' ')) {
			l--;
		} else {
			break;
		}
	}
//#endif
	size_t dot = fileName.rfind('.', l);
	if (dot != std::string::npos) {
		return StringToLower(fileName.substr(dot + 1));
	}

	return "";
}


std::string FileSystem::GetNormalizedPath(const std::string& path) {

	std::string normalizedPath = StringReplace(path, "\\", "/"); // convert to POSIX path separators

	normalizedPath = StringReplace(normalizedPath, "/./", "/");
	normalizedPath = boost::regex_replace(normalizedPath, boost::regex("[/]{2,}"), "/");
	normalizedPath = boost::regex_replace(normalizedPath, boost::regex("[^/]+[/][.]{2}"), "");
	normalizedPath = boost::regex_replace(normalizedPath, boost::regex("[/]{2,}"), "/");

	return normalizedPath; // maybe use FixSlashes here
}

std::string& FileSystem::FixSlashes(std::string& path)
{
	int sep = FileSystem::GetNativePathSeparator();
	for (size_t i = 0; i < path.size(); ++i) {
		if (path[i] == '/' || path[i] == '\\') {
			path[i] = sep;
		}
	}

	return path;
}

std::string& FileSystem::ForwardSlashes(std::string& path)
{
	for (size_t i = 0; i < path.size(); ++i) {
		if (path[i] == '\\') {
			path[i] = '/';
		}
	}

	return path;
}


bool FileSystem::CheckFile(const std::string& file)
{
	// Don't allow code to escape from the data directories.
	// Note: this does NOT mean this is a SAFE fopen function:
	// symlink-, hardlink-, you name it-attacks are all very well possible.
	// The check is just ment to "enforce" certain coding behaviour.
	bool hasParentRef = (file.find("..") != std::string::npos);

	return !hasParentRef;
}
// bool FileSystem::CheckDir(const std::string& dir) const {
// 	return CheckFile(dir);
// }
bool FileSystem::Remove(std::string file)
{
	if (!CheckFile(file)) {
		return false;
	}
	FixSlashes(file);
	return FileSystem::DeleteFile(file);
}