File: FileSystemAbstraction.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 (491 lines) | stat: -rwxr-xr-x 12,766 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifdef _MSC_VER
#include "System/Platform/Win/win32.h"
#endif

#include "FileSystemAbstraction.h"

#include "FileQueryFlags.h"

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

#include <cassert>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <boost/regex.hpp>

#ifndef _WIN32
	#include <dirent.h>
	#include <sstream>
	#include <unistd.h>
	#include <time.h>
#else
	#include <windows.h>
	#include <io.h>
	#include <direct.h>
	#include <fstream>
	// Win-API redifines these, which breaks things
	#if defined(CreateDirectory)
		#undef CreateDirectory
	#endif
	#if defined(DeleteFile)
		#undef DeleteFile
	#endif
#endif


#ifndef _WIN32
const int FileSystemAbstraction::nativePathSeparator = '/';
#else
const int FileSystemAbstraction::nativePathSeparator = '\\';
#endif

std::string FileSystemAbstraction::RemoveLocalPathPrefix(const std::string& path)
{
	std::string p(path);

	if ((p.length() >= 2) && (p[0] == '.') && IsPathSeparator(p[1])) {
	    p.erase(0, 2);
	}

	return p;
}

bool FileSystemAbstraction::IsFSRoot(const std::string& p)
{
	bool isFsRoot = false;

#ifdef WIN32
	// examples: "C:\", "C:/", "C:", "c:", "D:"
	isFsRoot = (p.length() >= 2 && p[1] == ':' &&
			((p[0] >= 'a' && p[0] <= 'z') || (p[0] >= 'A' && p[0] <= 'Z')) &&
			(p.length() == 2 || (p.length() == 3 && IsPathSeparator(p[2]))));
#else
	// examples: "/"
	isFsRoot = (p.length() == 1 && IsNativePathSeparator(p[0]));
#endif

	return isFsRoot;
}

bool FileSystemAbstraction::IsPathSeparator(char aChar) {
	return ((aChar == cPS_WIN32) || (aChar == cPS_POSIX));
}

bool FileSystemAbstraction::IsNativePathSeparator(char aChar) {
	return (aChar == cPS);
}

bool FileSystemAbstraction::HasPathSepAtEnd(const std::string& path) {

	bool pathSepAtEnd = false;

	if (!path.empty()) {
		const char lastChar = path.at(path.size() - 1);
		if (IsNativePathSeparator(lastChar)) {
			pathSepAtEnd = true;
		}
	}

	return pathSepAtEnd;
}

void FileSystemAbstraction::EnsurePathSepAtEnd(std::string& path) {

	if (path.empty()) {
		path += "."sPS;
	} else if (!HasPathSepAtEnd(path)) {
		path += cPS;
	}
}
std::string FileSystemAbstraction::EnsurePathSepAtEnd(const std::string& path) {
	
	std::string pathCopy(path);
	EnsurePathSepAtEnd(pathCopy);
	return pathCopy;
}

void FileSystemAbstraction::EnsureNoPathSepAtEnd(std::string& path) {

	if (HasPathSepAtEnd(path)) {
		path.resize(path.size() - 1);
	}
}
std::string FileSystemAbstraction::EnsureNoPathSepAtEnd(const std::string& path) {
	
	std::string pathCopy(path);
	EnsureNoPathSepAtEnd(pathCopy);
	return pathCopy;
}

std::string FileSystemAbstraction::StripTrailingSlashes(const std::string& path)
{
	size_t len = path.length();

	while (len > 0) {
		if (IsPathSeparator(path.at(len - 1))) {
			--len;
		} else {
			break;
		}
	}

	return path.substr(0, len);
}

std::string FileSystemAbstraction::GetParent(const std::string& path) {

	std::string parent = path;
	EnsureNoPathSepAtEnd(parent);

	static const char* PATH_SEP_REGEX = sPS_POSIX sPS_WIN32;
	const std::string::size_type slashPos = parent.find_last_of(PATH_SEP_REGEX);
	if (slashPos == std::string::npos) {
		parent = "";
	} else {
		parent.resize(slashPos + 1);
	}

	return parent;
}

size_t FileSystemAbstraction::GetFileSize(const std::string& file)
{
	size_t fileSize = 0;

	struct stat info;
	if (stat(file.c_str(), &info) == 0) {
		fileSize = info.st_size;
	}

	return fileSize;
}

bool FileSystemAbstraction::IsReadableFile(const std::string& file)
{
#ifdef WIN32
	return (_access(StripTrailingSlashes(file).c_str(), 4) == 0);
#else
	return (access(file.c_str(), R_OK | F_OK) == 0);
#endif
}

std::string FileSystemAbstraction::GetFileModificationDate(const std::string& file)
{
	std::string time = "";

#if       defined(WIN32)
	HANDLE hFile = CreateFile(file.c_str(), // file to open
			GENERIC_READ,                   // open for reading
			FILE_SHARE_READ,                // share for reading
			NULL,                           // default security
			OPEN_EXISTING,                  // existing file only
			FILE_ATTRIBUTE_NORMAL,          // normal file
			NULL);                          // no attr. template

	if (hFile == INVALID_HANDLE_VALUE) {
		LOG_L(L_WARNING, "Failed opening file for retreiving last modification time: %s", file.c_str());
	} else {
		FILETIME /*ftCreate, ftAccess,*/ ftWrite;

		// Retrieve the file times for the file.
		if (GetFileTime(hFile, NULL, NULL, &ftWrite) != 0) {
			LOG_L(L_WARNING, "Failed fetching last modification time from file: %s", file.c_str());
		} else {
			// Convert the last-write time to local time.
			const size_t cTime_size = 20;
			char cTime[cTime_size];

			SYSTEMTIME stUTC, stLocal;
			if ((FileTimeToSystemTime(&ftWrite, &stUTC) != 0) &&
				SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal))
			{
				// Build a string showing the date and time.
				SNPRINTF(cTime, cTime_size,
						"%d%02d%02d%02d%02d%02d",
						stLocal.wYear, stLocal.wMonth, stLocal.wDay,
						stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
				time = cTime;
			} else {
				LOG_L(L_WARNING, "Failed converting last modification time to a string");
			}
		}
		CloseHandle(hFile);
	}

#else  // defined(WIN32)
	struct tm* clock;
	struct stat attrib;
	const size_t cTime_size = 20;
	char cTime[cTime_size];

	const int fetchOk = stat(file.c_str(), &attrib); // get the attributes of file
	if (fetchOk != 0) {
		LOG_L(L_WARNING, "Failed opening file for retreiving last modification time: %s", file.c_str());
	} else {
		// Get the last modified time and put it into the time structure
		clock = gmtime(&(attrib.st_mtime));
		if (clock == NULL) {
			LOG_L(L_WARNING, "Failed fetching last modification time from file: %s", file.c_str());
		} else {
			SNPRINTF(cTime, cTime_size, "%d%02d%02d%02d%02d%02d", 1900+clock->tm_year, clock->tm_mon, clock->tm_mday, clock->tm_hour, clock->tm_min, clock->tm_sec);
			time = cTime;
		}
	}
#endif // defined(WIN32)

	return time;
}


bool FileSystemAbstraction::IsAbsolutePath(const std::string& path)
{
#ifdef WIN32
	return ((path.length() > 1) && (path[1] == ':'));
#else
	return ((path.length() > 0) && (path[0] == '/'));
#endif
}

/**
 * @brief creates a rwxr-xr-x dir in the writedir
 *
 * Returns true if the postcondition of this function is that dir exists in
 * the write directory.
 *
 * Note that this function does not check access to the dir, ie. if you've
 * created it manually with 0000 permissions then this function may return
 * true, subsequent operation on files inside the directory may still fail.
 *
 * As a rule of thumb, set identical permissions on identical items in the
 * data directory, ie. all subdirectories the same perms, all files the same
 * perms.
 */
bool FileSystemAbstraction::MkDir(const std::string& dir)
{
	// First check if directory exists. We'll return success if it does.
	if (DirExists(dir)) {
		return true;
	}

	bool dirCreated = false;

	// If it doesn't exist we try to mkdir it and return success if that succeeds.
#ifndef _WIN32
	dirCreated = (::mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
#else
	dirCreated = (::_mkdir(StripTrailingSlashes(dir).c_str()) == 0);
#endif

	if (!dirCreated) {
		LOG_L(L_WARNING, "Could not create directory %s: %s", dir.c_str(), strerror(errno));
	}

	return dirCreated;
}

bool FileSystemAbstraction::DeleteFile(const std::string& file)
{
	bool fileDeleted = (remove(file.c_str()) == 0);

	if (!fileDeleted) {
		LOG_L(L_WARNING, "Could not delete file %s: %s", file.c_str(), strerror(errno));
	}

	return fileDeleted;
}

bool FileSystemAbstraction::FileExists(const std::string& file)
{
	bool fileExists = false;

#ifdef _WIN32
	struct _stat info;
	const int ret = _stat(StripTrailingSlashes(file).c_str(), &info);
	fileExists = ((ret == 0 && (info.st_mode & _S_IFREG)));
#else
	struct stat info;
	const int ret = stat(file.c_str(), &info);
	fileExists = ((ret == 0 && !S_ISDIR(info.st_mode)));
#endif

	return fileExists;
}

bool FileSystemAbstraction::DirExists(const std::string& dir)
{
	bool dirExists = false;

#ifdef _WIN32
	struct _stat info;
	std::string myDir = dir;
	// for the root dir on a drive (for example C:\)
	// we need the trailing slash
	if ((myDir.length() == 3) && (myDir[1] == ':') && ((myDir[2] != '\\') || (myDir[2] != '/'))) {
		// do nothing
	} else if ((myDir.length() == 2) && (myDir[1] == ':')) {
		myDir += "\\";
	} else {
		// for any other dir (for example C:\WINDOWS\),
		// we need to get rid of it.
		myDir = StripTrailingSlashes(myDir);
	}
	const int ret = _stat(myDir.c_str(), &info);
	dirExists = ((ret == 0) && (info.st_mode & _S_IFDIR));
#else
	struct stat info;
	const int ret = stat(dir.c_str(), &info);
	dirExists = ((ret == 0) && S_ISDIR(info.st_mode));
#endif

	return dirExists;
}


bool FileSystemAbstraction::DirIsWritable(const std::string& dir)
{
#ifdef _WIN32
	// this exists because _access does not do the right thing
	// see http://msdn.microsoft.com/en-us/library/1w06ktdy(VS.71).aspx
	// for now, try to create a temporary file in a directory and open it
	// to rule out the possibility of it being created in the virtual store
	// TODO perhaps use SECURITY_DESCRIPTOR winapi calls here

	std::string testfile = dir + "\\__$testfile42$.test";
	std::ofstream os(testfile.c_str());
	if (os.fail()) {
		return false;
	}
	const char* testdata = "THIS IS A TEST";
	os << testdata;
	os.close();

	// this part should only be needed when there is no manifest embedded
	std::ifstream is(testfile.c_str());
	if (is.fail()) {
		return false; // the file most likely exists in the virtual store
	}
	std::string input;
	getline(is, input);
	if (input != testdata) {
		unlink(testfile.c_str());
		return false;
	}
	is.close();
	unlink(testfile.c_str());
	return true;
#else
	return (access(dir.c_str(), W_OK) == 0);
#endif
}

std::string FileSystemAbstraction::GetCwd()
{
	std::string cwd = "";

#ifndef _WIN32
	#define GETCWD getcwd
#else
	#define GETCWD _getcwd
#endif

	const size_t path_maxSize = 1024;
	char path[path_maxSize];
	if (GETCWD(path, path_maxSize) != NULL) {
		cwd = path;
	}

	return cwd;
}

void FileSystemAbstraction::ChDir(const std::string& dir)
{
#ifndef _WIN32
	const int err = chdir(dir.c_str());
#else
	const int err = _chdir(StripTrailingSlashes(dir).c_str());
#endif

	if (err) {
		throw content_error("Could not chdir into " + dir);
	}
}

static void FindFiles(std::vector<std::string>& matches, const std::string& datadir, const std::string& dir, const boost::regex& regexPattern, int flags)
{
#ifdef _WIN32
	WIN32_FIND_DATA wfd;
	HANDLE hFind = FindFirstFile((datadir + dir + "\\*").c_str(), &wfd);

	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			if(strcmp(wfd.cFileName,".") && strcmp(wfd.cFileName ,"..")) {
				if(!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) {
					if ((flags & FileQueryFlags::ONLY_DIRS) == 0) {
						if (boost::regex_match(wfd.cFileName, regexPattern)) {
							matches.push_back(dir + wfd.cFileName);
						}
					}
				} else {
					if (flags & FileQueryFlags::INCLUDE_DIRS) {
						if (boost::regex_match(wfd.cFileName, regexPattern)) {
							matches.push_back(dir + wfd.cFileName + "\\");
						}
					}
					if (flags & FileQueryFlags::RECURSE) {
						FindFiles(matches, datadir, dir + wfd.cFileName + "\\", regexPattern, flags);
					}
				}
			}
		} while (FindNextFile(hFind, &wfd));
		FindClose(hFind);
	}
#else
	DIR* dp;
	struct dirent* ep;

	if (!(dp = opendir((datadir + dir).c_str()))) {
		return;
	}

	while ((ep = readdir(dp))) {
		// exclude hidden files
		if (ep->d_name[0] != '.') {
			// is it a file? (we just treat sockets / pipes / fifos / character&block devices as files...)
			// (need to stat because d_type is DT_UNKNOWN on linux :-/)
			struct stat info;
			if (stat((datadir + dir + ep->d_name).c_str(), &info) == 0) {
				if (!S_ISDIR(info.st_mode)) {
					if ((flags & FileQueryFlags::ONLY_DIRS) == 0) {
						if (boost::regex_match(ep->d_name, regexPattern)) {
							matches.push_back(dir + ep->d_name);
						}
					}
				} else {
					// or a directory?
					if (flags & FileQueryFlags::INCLUDE_DIRS) {
						if (boost::regex_match(ep->d_name, regexPattern)) {
							matches.push_back(dir + ep->d_name + "/");
						}
					}
					if (flags & FileQueryFlags::RECURSE) {
						FindFiles(matches, datadir, dir + ep->d_name + "/", regexPattern, flags);
					}
				}
			}
		}
	}
	closedir(dp);
#endif
}

void FileSystemAbstraction::FindFiles(std::vector<std::string>& matches, const std::string& dataDir, const std::string& dir, const std::string& regex, int flags)
{
	const boost::regex regexPattern(regex);
	::FindFiles(matches, dataDir, dir, regexPattern, flags);
}