RemoteLinux: Cache device environment

Change-Id: Ida735b69179b418c21d3875c3906b9449709623a
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-01-31 09:38:37 +01:00
parent fb6fc2bde8
commit 7fbb41c10c

View File

@@ -37,6 +37,7 @@
#include <QDateTime> #include <QDateTime>
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QMutex> #include <QMutex>
#include <QReadWriteLock>
#include <QRegularExpression> #include <QRegularExpression>
#include <QScopeGuard> #include <QScopeGuard>
#include <QTemporaryDir> #include <QTemporaryDir>
@@ -376,14 +377,49 @@ public:
void attachToSharedConnection(SshConnectionHandle *connectionHandle, void attachToSharedConnection(SshConnectionHandle *connectionHandle,
const SshParameters &sshParameters); const SshParameters &sshParameters);
Environment getEnvironment();
void invalidateEnvironmentCache();
LinuxDevice *q = nullptr; LinuxDevice *q = nullptr;
QThread m_shellThread; QThread m_shellThread;
ShellThreadHandler *m_handler = nullptr; ShellThreadHandler *m_handler = nullptr;
mutable QMutex m_shellMutex; mutable QMutex m_shellMutex;
QList<QtcProcess *> m_terminals; QList<QtcProcess *> m_terminals;
LinuxDeviceFileAccess m_fileAccess{this}; LinuxDeviceFileAccess m_fileAccess{this};
QReadWriteLock m_environmentCacheLock;
std::optional<Environment> m_environmentCache;
}; };
void LinuxDevicePrivate::invalidateEnvironmentCache()
{
QWriteLocker locker(&m_environmentCacheLock);
m_environmentCache.reset();
}
Environment LinuxDevicePrivate::getEnvironment()
{
QReadLocker locker(&m_environmentCacheLock);
if (m_environmentCache.has_value())
return m_environmentCache.value();
locker.unlock();
QWriteLocker writeLocker(&m_environmentCacheLock);
if (m_environmentCache.has_value())
return m_environmentCache.value();
QtcProcess getEnvProc;
getEnvProc.setCommand({FilePath("env").onDevice(q->rootPath()), {}});
Environment inEnv;
inEnv.setCombineWithDeviceEnvironment(false);
getEnvProc.setEnvironment(inEnv);
getEnvProc.runBlocking();
const QString remoteOutput = getEnvProc.cleanedStdOut();
m_environmentCache = Environment(remoteOutput.split('\n', Qt::SkipEmptyParts), q->osType());
return m_environmentCache.value();
}
RunResult LinuxDeviceFileAccess::runInShell(const CommandLine &cmdLine, RunResult LinuxDeviceFileAccess::runInShell(const CommandLine &cmdLine,
const QByteArray &stdInData) const const QByteArray &stdInData) const
{ {
@@ -392,15 +428,7 @@ RunResult LinuxDeviceFileAccess::runInShell(const CommandLine &cmdLine,
Environment LinuxDeviceFileAccess::deviceEnvironment() const Environment LinuxDeviceFileAccess::deviceEnvironment() const
{ {
QtcProcess getEnvProc; return m_dev->getEnvironment();
getEnvProc.setCommand({FilePath("env").onDevice(m_dev->q->rootPath()), {}});
Environment inEnv;
inEnv.setCombineWithDeviceEnvironment(false);
getEnvProc.setEnvironment(inEnv);
getEnvProc.runBlocking();
const QString remoteOutput = getEnvProc.cleanedStdOut();
return Environment(remoteOutput.split('\n', Qt::SkipEmptyParts), m_dev->q->osType());
} }
// SshProcessImpl // SshProcessImpl
@@ -1126,6 +1154,8 @@ bool LinuxDevicePrivate::setupShell()
if (m_handler->isRunning(sshParameters)) if (m_handler->isRunning(sshParameters))
return true; return true;
invalidateEnvironmentCache();
bool ok = false; bool ok = false;
QMetaObject::invokeMethod(m_handler, [this, sshParameters] { QMetaObject::invokeMethod(m_handler, [this, sshParameters] {
return m_handler->start(sshParameters); return m_handler->start(sshParameters);