/
sandbox.cpp
310 lines (252 loc) · 9.13 KB
/
sandbox.cpp
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
/**
* @file sandbox.cpp
* @copyright 2013 Jolla Ltd.
* @author Martin Kampas <martin.kampas@tieto.com>
* @date 2013
*/
#include "sandbox_p.h"
#include <stdlib.h>
#include <QtCore/QAbstractFileEngineHandler>
#include <QtCore/QDir>
#include <QtCore/QFSFileEngine>
#include <QtCore/QFileInfo>
#include <QtCore/QProcessEnvironment>
#include <QtCore/QSet>
#include "libssu/ssucoreconfig.h"
#include "constants.h"
class Sandbox::FileEngineHandler : public QAbstractFileEngineHandler {
public:
FileEngineHandler(const QString &sandboxPath, const QSet<QString> &overlayEnabledDirectories);
bool isDirectoryOverlayEnabled(const QString &path) const;
public:
// QAbstractFileEngineHandler
QAbstractFileEngine *create(const QString &fileName) const;
private:
const QString m_sandboxPath;
QSet<QString> m_overlayEnabledDirectories;
};
/**
* @class Sandbox
* @brief Simple sandboxing with Qt file system abstraction.
*
* Redirects all file operations on selected files to files under sandbox
* directory. The term <em>world files</em> is used to reffer files outside
* sandbox.
*
* Its effect is controlled by activate() and deactivate() calls. Only one
* Sandbox instance can be active at any time. Active sandbox is automatically
* deactivated upon destruction.
*
* When constructed without arguments, path to sandbox directory is get from
* @c SSU_TESTS_SANDBOX environment variable.
*
* @attention When constructed without arguments, it is activated automatically
* and failure to do so is reported with @c qFatal(), i.e., application will be
* abort()ed.
*
* When constructed with @a usage UseAsSkeleton, it will first make temporary
* copy of @a sandboxPath to work on and files in the original directory will
* stay untouched. Also see addWorldFiles().
*
* The argument @scopes allows to control if the sandbox will be used by this
* process, its children processes (@c SSU_TESTS_SANDBOX environment variable
* will be exported), or both.
*
* Internally it is based on QAbstractFileEngineHandler.
*
* @attention QDir lists entries presented in the world directory. The behavior
* changed with Qt 4.8.0 (Qt commit b9b55234a777c3b206332bafbe227e1355ca9186)
*/
Sandbox *Sandbox::s_activeInstance = 0;
Sandbox::Sandbox()
: m_defaultConstructed(true), m_usage(UseDirectly), m_scopes(ThisProcess),
m_sandboxPath(QProcessEnvironment::systemEnvironment().value("SSU_TESTS_SANDBOX")),
m_prepared(false), m_handler(0){
if (!activate()){
qFatal("%s: Failed to activate", Q_FUNC_INFO);
}
}
Sandbox::Sandbox(const QString &sandboxPath, Usage usage, Scopes scopes)
: m_defaultConstructed(false), m_usage(usage), m_scopes(scopes),
m_sandboxPath(sandboxPath), m_prepared(false), m_handler(0){
Q_ASSERT(!sandboxPath.isEmpty());
}
Sandbox::~Sandbox(){
if (isActive()){
deactivate();
}
if (!m_tempDir.isEmpty() && QFileInfo(m_tempDir).exists()){
if (QProcess::execute("rm", QStringList() << "-rf" << m_tempDir) != 0){
qWarning("%s: Failed to remove temporary directory", Q_FUNC_INFO);
}
}
}
bool Sandbox::isActive() const{
return s_activeInstance == this;
}
bool Sandbox::activate(){
Q_ASSERT_X(s_activeInstance == 0, Q_FUNC_INFO, "Only one instance can be active!");
if (!prepare()){
return false;
}
if (m_scopes & ThisProcess){
m_handler = new FileEngineHandler(m_workingSandboxPath, m_overlayEnabledDirectories);
}
if (m_scopes & ChildProcesses){
setenv("SSU_TESTS_SANDBOX", qPrintable(m_workingSandboxPath), 1);
}
s_activeInstance = this;
return true;
}
void Sandbox::deactivate(){
Q_ASSERT(isActive());
if (m_scopes & ThisProcess){
delete m_handler;
}
if (m_scopes & ChildProcesses){
unsetenv("SSU_TESTS_SANDBOX");
}
s_activeInstance = 0;
}
/**
* Copies selected files into sandbox. Existing files in sandbox are not overwriten.
*
* @c QDir::NoDotAndDotDot is always added into @a filters.
*/
bool Sandbox::addWorldFiles(const QString &directory, QDir::Filters filters,
const QStringList &filterNames){
Q_ASSERT(!isActive());
Q_ASSERT(!directory.isEmpty());
Q_ASSERT(directory.startsWith('/'));
Q_ASSERT(!directory.contains(':')); // separator in environment variable
Q_ASSERT(!directory.contains("/./") && !directory.endsWith("/.")
&& !directory.contains("/../") && !directory.endsWith("/..")
&& !directory.contains("//"));
if (!prepare()){
return false;
}
const QString sandboxedDirectory = m_workingSandboxPath + directory;
if (!QFileInfo(directory).exists()){
qWarning("%s: Directory does not exist: '%s'", Q_FUNC_INFO, qPrintable(directory));
return false;
}
if (!QFileInfo(directory).isDir()){
qWarning("%s: Is not a directory: '%s'", Q_FUNC_INFO, qPrintable(directory));
return false;
}
if (!QFileInfo(sandboxedDirectory).exists()){
if (!QDir().mkpath(sandboxedDirectory)){
qWarning("%s: Failed to create sandbox directory '%s'", Q_FUNC_INFO,
qPrintable(sandboxedDirectory));
return false;
}
} else if (!QFileInfo(sandboxedDirectory).isDir()){
qWarning("%s: Failed to create sandbox directory '%s': Is not a directory", Q_FUNC_INFO,
qPrintable(sandboxedDirectory));
return false;
}
if (filters == QDir::NoFilter){
filters = QDir::AllEntries;
}
filters |= QDir::NoDotAndDotDot;
foreach (const QFileInfo &worldEntryInfo, QDir(directory).entryInfoList(filterNames, filters)){
const QFileInfo sandboxEntryInfo(sandboxedDirectory + '/' + worldEntryInfo.fileName());
if (worldEntryInfo.isDir()){
if (!sandboxEntryInfo.exists()){
if (!QDir(sandboxedDirectory).mkdir(worldEntryInfo.fileName())){
qWarning("%s: Failed to create overlay directory '%s/%s'", Q_FUNC_INFO,
qPrintable(sandboxedDirectory), qPrintable(worldEntryInfo.fileName()));
return false;
}
} else if (!sandboxEntryInfo.isDir()){
qWarning("%s: Failed to create sandboxed copy '%s': Is not a directory", Q_FUNC_INFO,
qPrintable(sandboxEntryInfo.filePath()));
return false;
}
} else{
if (!sandboxEntryInfo.exists()){
if (!QFile(worldEntryInfo.filePath()).copy(sandboxEntryInfo.filePath())){
qWarning("%s: Failed to copy file into sandbox '%s'", Q_FUNC_INFO,
qPrintable(worldEntryInfo.filePath()));
return false;
}
} else if (sandboxEntryInfo.isDir()){
qWarning("%s: Failed to create sandboxed copy '%s': Is a directory", Q_FUNC_INFO,
qPrintable(sandboxEntryInfo.filePath()));
return false;
}
}
}
m_overlayEnabledDirectories.insert(directory);
return true;
}
bool Sandbox::prepare(){
Q_ASSERT(m_defaultConstructed || !m_sandboxPath.isEmpty());
if (m_prepared){
return true;
}
if (m_sandboxPath.isEmpty()){
return true;
}
if (!QFileInfo(m_sandboxPath).exists()){
qWarning("%s: Invalid sandboxPath: No such file or directory", qPrintable(m_sandboxPath));
return false;
}
if (!QFileInfo(m_sandboxPath).isDir()){
qWarning("%s: Invalid sandboxPath: Not a directory", qPrintable(m_sandboxPath));
return false;
}
if (m_usage == UseAsSkeleton){
QProcess mktemp;
mktemp.start("mktemp", QStringList() << "-t" << "-d" << "ssu-sandbox.XXX");
if (!mktemp.waitForFinished() || mktemp.exitCode() != 0){
qWarning("%s: Failed to create sandbox directory", Q_FUNC_INFO);
return false;
}
m_tempDir = mktemp.readAllStandardOutput().trimmed();
if (!QFileInfo(m_tempDir).isDir()){
qWarning("%s: Temporary directory disappeared: '%s'", Q_FUNC_INFO, qPrintable(m_tempDir));
return false;
}
const QString sandboxCopyPath = QString("%1/configroot").arg(m_tempDir);
if (QProcess::execute("cp", QStringList() << "-r" << m_sandboxPath << sandboxCopyPath) != 0){
qWarning("%s: Failed to copy sandbox directory", Q_FUNC_INFO);
return false;
}
m_workingSandboxPath = sandboxCopyPath;
} else{
m_workingSandboxPath = m_sandboxPath;
}
m_prepared = true;
return true;
}
/*
* @class Sandbox::FileEngineHandler
*/
Sandbox::FileEngineHandler::FileEngineHandler(const QString &sandboxPath,
const QSet<QString> &overlayEnabledDirectories):
m_sandboxPath(sandboxPath), m_overlayEnabledDirectories(overlayEnabledDirectories){
Q_ASSERT(!sandboxPath.isEmpty());
}
bool Sandbox::FileEngineHandler::isDirectoryOverlayEnabled(const QString &path) const{
return m_overlayEnabledDirectories.contains(path);
}
QAbstractFileEngine *Sandbox::FileEngineHandler::create(const QString &fileName) const{
Q_ASSERT(!m_sandboxPath.isEmpty());
if (!fileName.startsWith('/')){
return 0;
}
const QString sandboxedFileName = m_sandboxPath + fileName;
QScopedPointer<QFSFileEngine> sandboxedFileEngine(new QFSFileEngine(sandboxedFileName));
const QAbstractFileEngine::FileFlags flags = sandboxedFileEngine->fileFlags(
QAbstractFileEngine::ExistsFlag | QAbstractFileEngine::DirectoryType);
if (!(flags & QAbstractFileEngine::ExistsFlag)){
return 0;
}
if (flags & QAbstractFileEngine::DirectoryType){
if (!isDirectoryOverlayEnabled(fileName)){
return 0;
}
}
return sandboxedFileEngine.take();
}