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
|
/****************************************************************************
**
** Copyright (C) 2016 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company. For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/
#include "scopeutilities.h"
#include "applicationinstaller_p.h"
#include "utilities.h"
ScopedDirectoryCreator::ScopedDirectoryCreator()
{ }
bool ScopedDirectoryCreator::create(const QString &path, bool removeExisting)
{
m_path = path;
QFileInfo fi(m_path);
if (fi.exists() && fi.isDir()) {
if (!removeExisting)
return m_created = true;
else if (!removeRecursiveHelper(m_path))
return false;
}
//qWarning() << "CREATE" << path << fi.absolutePath() << fi.fileName();
return m_created = QDir::root().mkpath(path);
}
bool ScopedDirectoryCreator::take()
{
if (m_created && !m_taken)
m_taken = true;
return m_taken;
}
bool ScopedDirectoryCreator::destroy()
{
if (!m_taken) {
if (m_created )
removeRecursiveHelper(m_path);
m_taken = true;
}
return m_taken;
}
ScopedDirectoryCreator::~ScopedDirectoryCreator()
{
destroy();
}
QDir ScopedDirectoryCreator::dir()
{
return QDir(m_path);
}
ScopedFileCreator::ScopedFileCreator()
{ }
bool ScopedFileCreator::create(const QString &path, bool removeExisting)
{
m_file.setFileName(path);
QFileInfo fi(m_file);
if (fi.exists() && fi.isFile()) {
if (!removeExisting)
return m_created = true;
else if (!removeRecursiveHelper(fi.absoluteFilePath()))
return false;
}
return m_file.open(QFile::ReadWrite | QFile::Truncate);
}
bool ScopedFileCreator::take()
{
if (m_created && !m_taken) {
m_file.close();
m_taken = true;
}
return m_taken;
}
ScopedFileCreator::~ScopedFileCreator()
{
m_file.close();
if (m_created && !m_taken)
removeRecursiveHelper(m_file.fileName());
}
QFile *ScopedFileCreator::file()
{
return &m_file;
}
ScopedLoopbackCreator::ScopedLoopbackCreator()
{ }
bool ScopedLoopbackCreator::create(const QString &imagePath)
{
m_loopDevice = SudoClient::instance()->attachLoopback(imagePath);
return !m_loopDevice.isEmpty();
}
bool ScopedLoopbackCreator::take()
{
if (!m_loopDevice.isEmpty() && !m_taken)
m_taken = true;
return m_taken;
}
bool ScopedLoopbackCreator::destroy()
{
if (!m_taken) {
m_taken = m_loopDevice.isEmpty()
|| SudoClient::instance()->detachLoopback(m_loopDevice);
if (!m_taken) {
qCCritical(LogInstaller).nospace() << "could not remove the loopback device " << m_loopDevice
<< ": " << errorString();
}
}
return m_taken;
}
ScopedLoopbackCreator::~ScopedLoopbackCreator()
{
destroy();
}
QString ScopedLoopbackCreator::device() const
{
return m_loopDevice;
}
QString ScopedLoopbackCreator::errorString() const
{
return SudoClient::instance()->lastError();
}
ScopedMounter::ScopedMounter()
{ }
bool ScopedMounter::mount(const QString &devicePath, const QString &mountPoint, bool readWrite)
{
m_devicePath = devicePath;
m_mountPoint = mountPoint;
return m_mounted = SudoClient::instance()->mount(m_devicePath, m_mountPoint, readWrite);
}
bool ScopedMounter::unmount()
{
if (!m_taken) {
m_taken = !m_mounted
|| SudoClient::instance()->unmount(m_mountPoint)
|| SudoClient::instance()->unmount(m_mountPoint, true /*force*/);
//qWarning() << "SCOPEDUMOUNTER unmounted" << m_devicePath << m_mountPoint;
if (!m_taken) {
qCCritical(LogInstaller).nospace() << "could not unmount device " << m_devicePath
<< " at " << m_mountPoint
<< ": " << errorString();
}
}
return m_taken;
}
bool ScopedMounter::take()
{
if (m_mounted && !m_taken)
m_taken = true;
return m_taken;
}
ScopedMounter::~ScopedMounter()
{
unmount();
}
QString ScopedMounter::errorString() const
{
return SudoClient::instance()->lastError();
}
ScopedRenamer::ScopedRenamer()
{ }
bool ScopedRenamer::internalRename(const QDir &dir, const QString &from, const QString &to)
{
QFileInfo fromInfo(dir.absoluteFilePath(from));
QFileInfo toInfo(dir.absoluteFilePath(to));
// POSIX cannot atomically rename directories, if the destination directory exists
// and is non-empty. We need to delete it first.
// Windows on the other hand cannot do anything atomically.
#ifdef Q_OS_UNIX
if (fromInfo.isDir()) {
#else
Q_UNUSED(fromInfo)
if (true) {
#endif
if (toInfo.exists() && !recursiveOperation(toInfo.absoluteFilePath(), SafeRemove()))
return false;
}
#ifdef Q_OS_UNIX
return (::rename(fromInfo.absoluteFilePath().toLocal8Bit(), toInfo.absoluteFilePath().toLocal8Bit()) == 0);
#else
return QDir(dir).rename(from, to);
#endif
}
bool ScopedRenamer::rename(const QString &baseName, ScopedRenamer::Modes modes)
{
QFileInfo fi(baseName);
m_basePath = fi.absolutePath();
m_name = fi.fileName();
m_requested = modes;
// convenience
bool backupRequired = (modes & NameToNameMinus);
bool backupDone = false;
if (m_requested & NameToNameMinus) {
backupDone = internalRename(m_basePath, m_name, m_name + qL1C('-'));
if (backupDone)
m_done |= NameToNameMinus;
}
if (m_requested & NamePlusToName) {
// only try if no backup required, or it worked
if (!backupRequired || backupDone) {
if (internalRename(m_basePath, m_name + qL1C('+'), m_name)) {
m_done |= NamePlusToName;
}
else if (backupDone && !undoRename()) {
qCCritical(LogSystem) << QString::fromLatin1("failed to rename '%1+' to '%1', but also failed to rename backup '%1-' back to '%1' (in directory %2)")
.arg(m_name, m_basePath.absolutePath());
}
}
}
return m_requested == m_done;
}
bool ScopedRenamer::rename(const QDir &baseName, ScopedRenamer::Modes modes)
{
return rename(baseName.absolutePath(), modes);
}
bool ScopedRenamer::take()
{
if (!m_taken)
m_taken = true;
return m_taken;
}
bool ScopedRenamer::undoRename()
{
if (!m_taken) {
if (interalUndoRename()) {
m_taken = true;
} else {
if (m_done & NamePlusToName) {
qCCritical(LogSystem) << QString::fromLatin1("failed to undo rename from '%1+' to '%1' (in directory %2)")
.arg(m_name, m_basePath.absolutePath());
}
if (m_done & NameToNameMinus) {
qCCritical(LogSystem) << QString::fromLatin1("failed to undo rename from '%1' to '%1-' (in directory %2)")
.arg(m_name, m_basePath.absolutePath());
}
}
}
return m_taken;
}
bool ScopedRenamer::interalUndoRename()
{
if (m_done & NamePlusToName) {
if (internalRename(m_basePath, m_name, m_name + qL1C('+')))
m_done &= ~NamePlusToName;
}
if (m_done & NameToNameMinus) {
if (internalRename(m_basePath, m_name + qL1C('-'), m_name))
m_done &= ~NameToNameMinus;
}
return (m_done == 0);
}
ScopedRenamer::~ScopedRenamer()
{
undoRename();
}
bool ScopedRenamer::isRenamed() const
{
return m_requested && (m_requested == m_done);
}
QString ScopedRenamer::baseName() const
{
return m_basePath.absoluteFilePath(m_name);
}
|