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
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "maemoremotemounter.h"
#include "maemoglobal.h"
#include "qt4maemotarget.h"
#include <utils/ssh/sshconnection.h>
#include <utils/ssh/sshremoteprocess.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
#include <qtsupport/baseqtversion.h>
#include <remotelinux/linuxdeviceconfiguration.h>
#include <remotelinux/remotelinuxusedportsgatherer.h>
#include <utils/qtcassert.h>
#include <QtCore/QTimer>
using namespace Qt4ProjectManager;
using namespace RemoteLinux;
using namespace Utils;
namespace Madde {
namespace Internal {
MaemoRemoteMounter::MaemoRemoteMounter(QObject *parent)
: QObject(parent), m_utfsServerTimer(new QTimer(this)), m_state(Inactive)
{
connect(m_utfsServerTimer, SIGNAL(timeout()), this,
SLOT(handleUtfsServerTimeout()));
m_utfsServerTimer->setSingleShot(true);
}
MaemoRemoteMounter::~MaemoRemoteMounter()
{
killAllUtfsServers();
}
void MaemoRemoteMounter::setConnection(const SshConnection::Ptr &connection,
const LinuxDeviceConfiguration::ConstPtr &devConf)
{
QTC_ASSERT(m_state == Inactive, return);
m_connection = connection;
m_devConf = devConf;
}
void MaemoRemoteMounter::setBuildConfiguration(const Qt4BuildConfiguration *bc)
{
QTC_ASSERT(m_state == Inactive, return);
const QtSupport::BaseQtVersion * const qtVersion = bc->qtVersion();
const AbstractQt4MaemoTarget * const maemoTarget
= qobject_cast<AbstractQt4MaemoTarget *>(bc->target());
m_remoteMountsAllowed = maemoTarget && maemoTarget->allowsRemoteMounts();
m_maddeRoot = qtVersion ? MaemoGlobal::maddeRoot(qtVersion->qmakeCommand().toString()) : QString();
}
void MaemoRemoteMounter::addMountSpecification(const MaemoMountSpecification &mountSpec,
bool mountAsRoot)
{
QTC_ASSERT(m_state == Inactive, return);
if (m_remoteMountsAllowed && mountSpec.isValid())
m_mountSpecs << MountInfo(mountSpec, mountAsRoot);
}
bool MaemoRemoteMounter::hasValidMountSpecifications() const
{
return !m_mountSpecs.isEmpty();
}
void MaemoRemoteMounter::mount(PortList *freePorts,
const RemoteLinuxUsedPortsGatherer *portsGatherer)
{
QTC_ASSERT(m_state == Inactive, return);
Q_ASSERT(m_utfsServers.isEmpty());
Q_ASSERT(m_connection);
if (m_mountSpecs.isEmpty()) {
setState(Inactive);
emit reportProgress(tr("No directories to mount"));
emit mounted();
} else {
m_freePorts = freePorts;
m_portsGatherer = portsGatherer;
startUtfsClients();
}
}
void MaemoRemoteMounter::unmount()
{
QTC_ASSERT(m_state == Inactive, return);
if (m_mountSpecs.isEmpty()) {
emit reportProgress(tr("No directories to unmount"));
emit unmounted();
return;
}
QString remoteCall;
const QString remoteSudo = MaemoGlobal::remoteSudo(m_devConf->osType(),
m_connection->connectionParameters().userName);
for (int i = 0; i < m_mountSpecs.count(); ++i) {
remoteCall += QString::fromLocal8Bit("%1 umount %2 && %1 rmdir %2;")
.arg(remoteSudo, m_mountSpecs.at(i).mountSpec.remoteMountPoint);
}
m_unmountProcess = m_connection->createRemoteProcess(remoteCall.toUtf8());
connect(m_unmountProcess.data(), SIGNAL(closed(int)), this,
SLOT(handleUnmountProcessFinished(int)));
setState(Unmounting);
m_unmountProcess->start();
}
void MaemoRemoteMounter::handleUnmountProcessFinished(int exitStatus)
{
QTC_ASSERT(m_state == Unmounting || m_state == Inactive, return);
if (m_state == Inactive)
return;
setState(Inactive);
QString errorMsg;
switch (exitStatus) {
case SshRemoteProcess::FailedToStart:
errorMsg = tr("Could not execute unmount request.");
break;
case SshRemoteProcess::KilledBySignal:
errorMsg = tr("Failure unmounting: %1")
.arg(m_unmountProcess->errorString());
break;
case SshRemoteProcess::ExitedNormally:
break;
default:
Q_ASSERT_X(false, Q_FUNC_INFO,
"Impossible SshRemoteProcess exit status.");
}
killAllUtfsServers();
if (errorMsg.isEmpty()) {
emit reportProgress(tr("Finished unmounting."));
emit unmounted();
} else {
const QByteArray &umountStderr = m_unmountProcess->readAllStandardError();
if (!umountStderr.isEmpty())
errorMsg += tr("\nstderr was: '%1'").arg(QString::fromUtf8(umountStderr));
emit error(errorMsg);
}
}
void MaemoRemoteMounter::stop()
{
setState(Inactive);
}
void MaemoRemoteMounter::startUtfsClients()
{
const QString userName = m_connection->connectionParameters().userName;
const QString chmodFuse = MaemoGlobal::remoteSudo(m_devConf->osType(),
userName) + QLatin1String(" chmod a+r+w /dev/fuse");
const QString chmodUtfsClient
= QLatin1String("chmod a+x ") + utfsClientOnDevice();
const QLatin1String andOp(" && ");
QString remoteCall = chmodFuse + andOp + chmodUtfsClient;
for (int i = 0; i < m_mountSpecs.count(); ++i) {
MountInfo &mountInfo = m_mountSpecs[i];
mountInfo.remotePort
= m_portsGatherer->getNextFreePort(m_freePorts);
if (mountInfo.remotePort == -1) {
setState(Inactive);
emit error(tr("Error: Not enough free ports on device to fulfill all mount requests."));
return;
}
const QString remoteSudo
= MaemoGlobal::remoteSudo(m_devConf->osType(), userName);
const MaemoMountSpecification &mountSpec = mountInfo.mountSpec;
const QString mkdir = QString::fromLocal8Bit("%1 mkdir -p %2")
.arg(remoteSudo, mountSpec.remoteMountPoint);
const QString chmod = QString::fromLocal8Bit("%1 chmod a+r+w+x %2")
.arg(remoteSudo, mountSpec.remoteMountPoint);
QString utfsClient
= QString::fromLocal8Bit("%1 -l %2 -r %2 -b %2 %4 -o nonempty")
.arg(utfsClientOnDevice()).arg(mountInfo.remotePort)
.arg(mountSpec.remoteMountPoint);
if (mountInfo.mountAsRoot) {
utfsClient.prepend(MaemoGlobal::remoteSudo(m_devConf->osType(),
userName) + QLatin1Char(' '));
}
QLatin1String seqOp("; ");
remoteCall += seqOp + MaemoGlobal::remoteSourceProfilesCommand()
+ seqOp + mkdir + andOp + chmod + andOp + utfsClient;
}
emit reportProgress(tr("Starting remote UTFS clients..."));
m_mountProcess = m_connection->createRemoteProcess(remoteCall.toUtf8());
connect(m_mountProcess.data(), SIGNAL(started()), this,
SLOT(handleUtfsClientsStarted()));
connect(m_mountProcess.data(), SIGNAL(closed(int)), this,
SLOT(handleUtfsClientsFinished(int)));
m_mountProcess->start();
setState(UtfsClientsStarting);
}
void MaemoRemoteMounter::handleUtfsClientsStarted()
{
QTC_ASSERT(m_state == UtfsClientsStarting || m_state == Inactive, return);
if (m_state == UtfsClientsStarting) {
setState(UtfsClientsStarted);
QTimer::singleShot(250, this, SLOT(startUtfsServers()));
}
}
void MaemoRemoteMounter::handleUtfsClientsFinished(int exitStatus)
{
QTC_ASSERT(m_state == UtfsClientsStarting || m_state == UtfsClientsStarted
|| m_state == UtfsServersStarted || m_state == Inactive, return);
if (m_state == Inactive)
return;
setState(Inactive);
if (exitStatus == SshRemoteProcess::ExitedNormally
&& m_mountProcess->exitCode() == 0) {
emit reportProgress(tr("Mount operation succeeded."));
emit mounted();
} else {
QString errMsg = tr("Failure running UTFS client: %1")
.arg(m_mountProcess->errorString());
const QByteArray &mountStderr = m_mountProcess->readAllStandardError();
if (!mountStderr.isEmpty())
errMsg += tr("\nstderr was: '%1'").arg(QString::fromUtf8(mountStderr));
emit error(errMsg);
}
}
void MaemoRemoteMounter::startUtfsServers()
{
QTC_ASSERT(m_state == UtfsClientsStarted || m_state == Inactive, return);
if (m_state == Inactive)
return;
emit reportProgress(tr("Starting UTFS servers..."));
m_utfsServerTimer->start(30000);
for (int i = 0; i < m_mountSpecs.count(); ++i) {
const MountInfo &mountInfo = m_mountSpecs.at(i);
const MaemoMountSpecification &mountSpec = mountInfo.mountSpec;
const ProcPtr utfsServerProc(new QProcess);
const QString port = QString::number(mountInfo.remotePort);
const QString localSecretOpt = QLatin1String("-l");
const QString remoteSecretOpt = QLatin1String("-r");
const QStringList utfsServerArgs = QStringList() << localSecretOpt
<< port << remoteSecretOpt << port << QLatin1String("-c")
<< (m_connection->connectionParameters().host + QLatin1Char(':') + port)
<< mountSpec.localDir;
connect(utfsServerProc.data(),
SIGNAL(finished(int,QProcess::ExitStatus)), this,
SLOT(handleUtfsServerFinished(int,QProcess::ExitStatus)));
connect(utfsServerProc.data(), SIGNAL(error(QProcess::ProcessError)),
this, SLOT(handleUtfsServerError(QProcess::ProcessError)));
connect(utfsServerProc.data(), SIGNAL(readyReadStandardError()), this,
SLOT(handleUtfsServerStderr()));
m_utfsServers << utfsServerProc;
utfsServerProc->start(utfsServer(), utfsServerArgs);
}
setState(UtfsServersStarted);
}
void MaemoRemoteMounter::handleUtfsServerStderr()
{
if (m_state != Inactive) {
QProcess * const proc = static_cast<QProcess *>(sender());
const QByteArray &output = proc->readAllStandardError();
emit debugOutput(QString::fromLocal8Bit(output));
}
}
void MaemoRemoteMounter::handleUtfsServerError(QProcess::ProcessError)
{
if (m_state == Inactive || m_utfsServers.isEmpty())
return;
QProcess * const proc = static_cast<QProcess *>(sender());
QString errorString = proc->errorString();
const QByteArray &errorOutput = proc->readAllStandardError();
if (!errorOutput.isEmpty()) {
errorString += tr("\nstderr was: %1")
.arg(QString::fromLocal8Bit(errorOutput));
}
killAllUtfsServers();
emit error(tr("Error running UTFS server: %1").arg(errorString));
setState(Inactive);
}
void MaemoRemoteMounter::handleUtfsServerFinished(int /* exitCode */,
QProcess::ExitStatus exitStatus)
{
if (m_state != Inactive && exitStatus != QProcess::NormalExit)
handleUtfsServerError(static_cast<QProcess *>(sender())->error());
}
QString MaemoRemoteMounter::utfsClientOnDevice() const
{
return QLatin1String("/usr/lib/mad-developer/utfs-client");
}
QString MaemoRemoteMounter::utfsServer() const
{
return m_maddeRoot + QLatin1String("/madlib/utfs-server");
}
void MaemoRemoteMounter::killAllUtfsServers()
{
foreach (const ProcPtr &proc, m_utfsServers)
killUtfsServer(proc.data());
m_utfsServers.clear();
}
void MaemoRemoteMounter::killUtfsServer(QProcess *proc)
{
disconnect(proc, 0, this, 0);
proc->terminate();
proc->waitForFinished(1000);
proc->kill();
}
void MaemoRemoteMounter::handleUtfsServerTimeout()
{
QTC_ASSERT(m_state == UtfsServersStarted || m_state == Inactive, return);
if (m_state == Inactive)
return;
killAllUtfsServers();
emit error(tr("Timeout waiting for UTFS servers to connect."));
setState(Inactive);
}
void MaemoRemoteMounter::setState(State newState)
{
if (newState == Inactive) {
m_utfsServerTimer->stop();
if (m_mountProcess) {
disconnect(m_mountProcess.data(), 0, this, 0);
m_mountProcess->close();
}
if (m_unmountProcess) {
disconnect(m_unmountProcess.data(), 0, this, 0);
m_unmountProcess->close();
}
}
m_state = newState;
}
} // namespace Internal
} // namespace Madde
|