summaryrefslogtreecommitdiffstats
path: root/mmc_updater/src/FileUtils.cpp
blob: 712c0c5dad2369abd6dda0e8f6eae0cc5fa261f7 (plain)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#include "FileUtils.h"

#include "DirIterator.h"
#include "Log.h"
#include "Platform.h"
#include "StringUtils.h"

#include <algorithm>
#include <assert.h>
#include <string.h>
#include <fstream>
#include <iostream>
// this actually works with mingw32, which we use.
#include <libgen.h>

#ifdef PLATFORM_UNIX
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#endif

FileUtils::IOException::IOException(const std::string& error)
{
	init(errno,error);
}

FileUtils::IOException::IOException(int errorCode, const std::string& error)
{
	init(errorCode,error);
}

void FileUtils::IOException::init(int errorCode, const std::string& error)
{
	m_error = error;

#ifdef PLATFORM_UNIX
	m_errorCode = errorCode;

	if (m_errorCode > 0)
	{
		m_error += " details: " + std::string(strerror(m_errorCode));
	}
#endif

#ifdef PLATFORM_WINDOWS
	m_errorCode = 0;
	m_error += " GetLastError returned: " + intToStr(GetLastError());
#endif
}

FileUtils::IOException::~IOException() throw ()
{
}

FileUtils::IOException::Type FileUtils::IOException::type() const
{
#ifdef PLATFORM_UNIX
	switch (m_errorCode)
	{
		case 0:
			return NoError;
		case EROFS:
			return ReadOnlyFileSystem;
		case ENOSPC:
			return DiskFull;
		default:
			return Unknown;
	}
#else
	return Unknown;
#endif
}

bool FileUtils::fileExists(const char* path) throw (IOException)
{
#ifdef PLATFORM_UNIX
	struct stat fileInfo;
	if (lstat(path,&fileInfo) != 0)
	{
		if (errno == ENOENT)
		{
			return false;
		}
		else
		{
			throw IOException("Error checking for file " + std::string(path));
		}
	}
	return true;
#else
	DWORD result = GetFileAttributes(path);
	if (result == INVALID_FILE_ATTRIBUTES)
	{
		return false;
	}
	return true;
#endif
}

int FileUtils::fileMode(const char* path) throw (IOException)
{
#ifdef PLATFORM_UNIX
	struct stat fileInfo;
	if (stat(path,&fileInfo) != 0)
	{
		throw IOException("Error reading file permissions for " + std::string(path));
	}
	return fileInfo.st_mode;
#else
	// not implemented for Windows
	return 0;
#endif
}

void FileUtils::chmod(const char* path, int mode) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (::chmod(path,static_cast<mode_t>(mode)) != 0)
	{
		throw IOException("Failed to set permissions on " + std::string(path) + " to " + intToStr(mode));
	}
#else
	// TODO - Not implemented under Windows - all files
	// get default permissions
#endif
}

void FileUtils::moveFile(const char* src, const char* dest) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (rename(src,dest) != 0)
	{
		throw IOException("Unable to rename " + std::string(src) + " to " + std::string(dest));
	}
#else
	if (!MoveFile(src,dest))
	{
		throw IOException("Unable to rename " + std::string(src) + " to " + std::string(dest));
	}
#endif
}

void FileUtils::mkpath(const char* dir) throw (IOException)
{
	std::string currentPath;
	std::istringstream stream(dir);
	while (!stream.eof())
	{
		std::string segment;
		std::getline(stream,segment,'/');
		currentPath += segment;
		if (!currentPath.empty() && !fileExists(currentPath.c_str()))
		{
			mkdir(currentPath.c_str());
		}
		currentPath += '/';
	}
}

void FileUtils::mkdir(const char* dir) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (::mkdir(dir,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
	{
		throw IOException("Unable to create directory " + std::string(dir));
	}
#else
	if (!CreateDirectory(dir,0 /* default security attributes */))
	{
		throw IOException("Unable to create directory " + std::string(dir));
	}
#endif
}

void FileUtils::rmdir(const char* dir) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (::rmdir(dir) != 0)
	{
		throw IOException("Unable to remove directory " + std::string(dir));
	}
#else
	if (!RemoveDirectory(dir))
	{
		throw IOException("Unable to remove directory " + std::string(dir));
	}
#endif
}

void FileUtils::createSymLink(const char* link, const char* target) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (symlink(target,link) != 0)
	{
		throw IOException("Unable to create symlink " + std::string(link) + " to " + std::string(target));
	}
#else
	// symlinks are not supported under Windows (at least, not universally.
	// Windows Vista and later do actually support symlinks)
	LOG(Warn,"Skipping symlink creation - not implemented in Windows");
#endif
}

void FileUtils::removeFile(const char* src) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (unlink(src) != 0)
	{
		if (errno != ENOENT)
		{
			throw IOException("Unable to remove file " + std::string(src));
		}
	}
#else
	if (!DeleteFile(src))
	{
		if (GetLastError() == ERROR_ACCESS_DENIED)
		{
			// if another process is using the file, try moving it to
			// a temporary directory and then
			// scheduling it for deletion on reboot
			std::string tempDeletePathBase = tempPath();
			tempDeletePathBase += '/';
			tempDeletePathBase += fileName(src);

			int suffix = 0;
			std::string tempDeletePath = tempDeletePathBase;
			while (fileExists(tempDeletePath.c_str()))
			{
				++suffix;
				tempDeletePath = tempDeletePathBase + '_' + intToStr(suffix);
			}

			LOG(Warn,"Unable to remove file " + std::string(src) + " - it may be in use.  Moving to "
			         + tempDeletePath + " and scheduling delete on reboot.");
			moveFile(src,tempDeletePath.c_str());
			MoveFileEx(tempDeletePath.c_str(),0,MOVEFILE_DELAY_UNTIL_REBOOT);
		}
		else if (GetLastError() != ERROR_FILE_NOT_FOUND)
		{
			throw IOException("Unable to remove file " + std::string(src));
		}
	}
#endif
}

std::string FileUtils::fileName(const char* path)
{
	char* pathCopy = strdup(path);
	std::string basename = ::basename(pathCopy);
	free(pathCopy);
	return basename;
}

std::string FileUtils::dirname(const char* path)
{
	char* pathCopy = strdup(path);
	std::string dirname = ::dirname(pathCopy);
	free(pathCopy);
	return dirname;
}

void FileUtils::touch(const char* path) throw (IOException)
{
#ifdef PLATFORM_UNIX
	// see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
	//
	// we use utimes/futimes instead of utimensat/futimens for compatibility
	// with older Linux and Mac

	if (fileExists(path))
	{
		utimes(path,0 /* use current date/time */);
	}
	else
	{
		int fd = creat(path,S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
		if (fd != -1)
		{
			futimes(fd,0 /* use current date/time */);
			close(fd);
		}
		else
		{
			throw IOException("Unable to touch file " + std::string(path));
		}
	}
#else
	HANDLE result = CreateFile(path,GENERIC_WRITE,
	                           FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
	                           0,
							   CREATE_ALWAYS,
                               FILE_ATTRIBUTE_NORMAL,
                               0);
	if (result == INVALID_HANDLE_VALUE)
	{
		throw IOException("Unable to touch file " + std::string(path));
	}
	else
	{
		CloseHandle(result);
	}
#endif
}

void FileUtils::rmdirRecursive(const char* path) throw (IOException)
{
	// remove dir contents
	DirIterator dir(path);
	while (dir.next())
	{
		std::string name = dir.fileName();
		if (name != "." && name != "..")
		{
			if (dir.isDir())
			{
				rmdir(dir.filePath().c_str());
			}
			else
			{
				removeFile(dir.filePath().c_str());
			}
		}
	}

	// remove the directory itself
	rmdir(path);
}

std::string FileUtils::canonicalPath(const char* path)
{
#ifdef PLATFORM_UNIX
	// on Linux and Mac OS 10.6, realpath() can allocate the required
	// amount of memory automatically, however Mac OS 10.5 does not support
	// this, so we used a fixed-sized buffer on all platforms
	char canonicalPathBuffer[PATH_MAX+1];
	if (realpath(path,canonicalPathBuffer) != 0)
	{
		return std::string(canonicalPathBuffer);
	}
	else
	{
		throw IOException("Error reading canonical path for " + std::string(path));
	}
#else
	throw IOException("canonicalPath() not implemented");
#endif
}

std::string FileUtils::toWindowsPathSeparators(const std::string& str)
{
	std::string result = str;
	std::replace(result.begin(),result.end(),'/','\\');
	return result;
}

std::string FileUtils::toUnixPathSeparators(const std::string& str)
{
	std::string result = str;
	std::replace(result.begin(),result.end(),'\\','/');
	return result;
}

std::string FileUtils::tempPath()
{
#ifdef PLATFORM_UNIX
	std::string tmpDir(notNullString(getenv("TMPDIR")));
	if (tmpDir.empty())
	{
		tmpDir = "/tmp";
	}
	return tmpDir;
#else
	char buffer[MAX_PATH+1];
	GetTempPath(MAX_PATH+1,buffer);
	return toUnixPathSeparators(buffer);
#endif
}

bool startsWithDriveLetter(const char* path)
{
	return strlen(path) >= 2 &&
	      (isalpha(path[0])) &&
	       path[1] == ':';
}

bool FileUtils::isRelative(const char* path)
{
#ifdef PLATFORM_UNIX
	return strlen(path) == 0 || path[0] != '/';
#else
	// on Windows, a path is relative if it does not start with:
	// - '\\' (a UNC name)
	// - '[Drive Letter]:\'
	// - A single backslash
	//
	// the input path is assumed to have already been converted to use
	// Unix-style path separators

	std::string pathStr(path);

	if ((!pathStr.empty() && pathStr.at(0) == '/') ||
	    (startsWith(pathStr,"//")) ||
	    (startsWithDriveLetter(pathStr.c_str())))
	{
		return false;
	}
	else
	{
		return true;
	}
#endif
}

void FileUtils::writeFile(const char* path, const char* data, int length) throw (IOException)
{
	std::ofstream stream(path,std::ios::binary | std::ios::trunc);
	stream.write(data,length);
}

std::string FileUtils::readFile(const char* path) throw (IOException)
{
	std::ifstream inputFile(path, std::ios::in | std::ios::binary);
	std::string content;
	inputFile.seekg(0, std::ios::end);
	content.resize(static_cast<unsigned int>(inputFile.tellg()));
	inputFile.seekg(0, std::ios::beg);
	inputFile.read(&content[0], static_cast<int>(content.size()));
	return content;
}

void FileUtils::copyFile(const char* src, const char* dest) throw (IOException)
{
#ifdef PLATFORM_UNIX
	std::ifstream inputFile(src,std::ios::binary);
	std::ofstream outputFile(dest,std::ios::binary | std::ios::trunc);

	if (!inputFile.good())
	{
		throw IOException("Failed to read file " + std::string(src));
	}
	if (!outputFile.good())
	{
		throw IOException("Failed to write file " + std::string(dest));
	}
	
	outputFile << inputFile.rdbuf();

	if (inputFile.bad())
	{
		throw IOException("Error reading file " + std::string(src));
	}
	if (outputFile.bad())
	{
		throw IOException("Error writing file " + std::string(dest));
	}

	chmod(dest,fileMode(src));
#else
	if (!CopyFile(src,dest,FALSE))
	{
		throw IOException("Failed to copy " + std::string(src) + " to " + std::string(dest));
	}
#endif
}

std::string FileUtils::makeAbsolute(const char* path, const char* basePath)
{
	if (isRelative(path))
	{
		assert(!isRelative(basePath));
		return std::string(basePath) + '/' + std::string(path);
	}
	else
	{
		return path;
	}
}

void FileUtils::chdir(const char* path) throw (IOException)
{
#ifdef PLATFORM_UNIX
	if (::chdir(path) != 0)
	{
		throw FileUtils::IOException("Unable to change directory");
	}
#else
	if (!SetCurrentDirectory(path))
	{
		throw FileUtils::IOException("Unable to change directory");
	}
#endif
}

std::string FileUtils::getcwd() throw (IOException)
{
#ifdef PLATFORM_UNIX
	char path[PATH_MAX];
	if (!::getcwd(path,PATH_MAX))
	{
		throw FileUtils::IOException("Failed to get current directory");
	}
	return std::string(path);
#else
	char path[MAX_PATH];
	if (GetCurrentDirectory(MAX_PATH,path) == 0)
	{
		throw FileUtils::IOException("Failed to get current directory");
	}
	return toUnixPathSeparators(std::string(path));
#endif
}