summaryrefslogtreecommitdiffstats
path: root/mmc_updater/src/FileUtils.h
blob: cb5830ae2f89dc0baadc8cc2669221e040f91a9e (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
#pragma once

#include <exception>
#include <string>

#include "Platform.h"
#include "StringUtils.h"


/** A set of functions for performing common operations
  * on files, throwing exceptions if an operation fails.
  *
  * Path arguments to FileUtils functions should use Unix-style path
  * separators.
  */
class FileUtils
{
	public:
		/** Base class for exceptions reported by
		  * FileUtils methods if an operation fails.
		  */
		class IOException : public std::exception
		{
			public:
				IOException(const std::string& error);
				IOException(int errorCode, const std::string& error);

				virtual ~IOException() throw ();

				enum Type
				{
					NoError,
					/** Unknown error type.  Call what() to get the description
					  * provided by the OS.
					  */
					Unknown,
					ReadOnlyFileSystem,
					DiskFull
				};

				virtual const char* what() const throw ()
				{
					return m_error.c_str();
				}

				Type type() const;

			private:
				void init(int errorCode, const std::string& error);

				std::string m_error;
				int m_errorCode;
		};

		/** Remove a file.  Throws an exception if the file
		 * could not be removed.
		 *
		 * On Unix, a file can be removed even if it is in use if the user
		 * has the necessary permissions.  removeFile() tries to simulate
		 * this behavior on Windows.  If a file cannot be removed on Windows
		 * because it is in use it will be moved to a temporary directory and
		 * scheduled for deletion on the next restart.
		 */
		static void removeFile(const char* src) throw (IOException);

		/** Set the permissions of a file.  @p permissions uses the standard
		  * Unix mode_t values.
		  */
		static void chmod(const char* path, int permissions) throw (IOException);

		/** Returns true if the file at @p path exists.  If @p path is a symlink,
		  * returns true if the symlink itself exists, not the target.
		  */
		static bool fileExists(const char* path) throw (IOException);

		/** Returns the Unix mode flags of @p path.  If @p path is a symlink,
		  * returns the mode flags of the target.
		  */
		static int fileMode(const char* path) throw (IOException);

		static void moveFile(const char* src, const char* dest) throw (IOException);
		static void mkdir(const char* dir) throw (IOException);
		static void rmdir(const char* dir) throw (IOException);
		static void createSymLink(const char* link, const char* target) throw (IOException);
		static void touch(const char* path) throw (IOException);
		static void copyFile(const char* src, const char* dest) throw (IOException);

		/** Create all the directories in @p path which do not yet exist.
		  * @p path may be relative or absolute.
		  */
		static void mkpath(const char* path) throw (IOException);

		/** Returns the file name part of a file path, including the extension. */
		static std::string fileName(const char* path);

		/** Returns the directory part of a file path.
		 * On Windows this includes the drive letter, if present in @p path.
		 */
		static std::string dirname(const char* path);

		/** Remove a directory and all of its contents. */
		static void rmdirRecursive(const char* dir) throw (IOException);

		/** Return the full, absolute path to a file, resolving any
		  * symlinks and removing redundant sections.
		  */
		static std::string canonicalPath(const char* path);

		/** Returns the path to a directory for storing temporary files. */
		static std::string tempPath();

		/** Returns a copy of the path 'str' with Windows-style '\'
		 * dir separators converted to Unix-style '/' separators
		 */
		static std::string toUnixPathSeparators(const std::string& str);

		static std::string toWindowsPathSeparators(const std::string& str);

		/** Returns true if the provided path is relative.
		  * Or false if absolute.
		  */
		static bool isRelative(const char* path);

		/** Converts @p path to an absolute path.  If @p path is already absolute,
		  * just returns @p path, otherwise prefixes it with @p basePath to make it absolute.
		  *
		  * @p basePath should be absolute.
		  */
		static std::string makeAbsolute(const char* path, const char* basePath);

		static void writeFile(const char* path, const char* data, int length) throw (IOException);

		static std::string readFile(const char* path) throw (IOException);

		/** Changes the current working directory to @p path */
		static void chdir(const char* path) throw (IOException);

		/** Returns the current working directory of the application. */
		static std::string getcwd() throw (IOException);
};