Simplify RemoteLinuxCheckForFreeDiskSpaceService

Use FilePath::bytesAvailable() on remote device instead.

Change-Id: I2431641ea2f4c090439e0380a378e39aa617540a
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Jarek Kobus
2022-01-04 18:06:42 +01:00
parent 71626a7d16
commit 79ce29b5a1
5 changed files with 42 additions and 69 deletions

View File

@@ -106,6 +106,8 @@ protected:
virtual void doDeviceSetup() { handleDeviceSetupDone(true); } virtual void doDeviceSetup() { handleDeviceSetupDone(true); }
virtual void stopDeviceSetup() { handleDeviceSetupDone(false); } virtual void stopDeviceSetup() { handleDeviceSetupDone(false); }
void setFinished();
private: private:
void handleConnected(); void handleConnected();
void handleConnectionFailure(); void handleConnectionFailure();
@@ -115,7 +117,6 @@ private:
virtual void doDeploy() = 0; virtual void doDeploy() = 0;
virtual void stopDeployment() = 0; virtual void stopDeployment() = 0;
void setFinished();
Internal::AbstractRemoteLinuxDeployServicePrivate * const d; Internal::AbstractRemoteLinuxDeployServicePrivate * const d;
}; };

View File

@@ -403,6 +403,20 @@ QString LinuxDevice::userAtHost() const
return sshParameters().userName() + '@' + sshParameters().host(); return sshParameters().userName() + '@' + sshParameters().host();
} }
FilePath LinuxDevice::mapToGlobalPath(const FilePath &pathOnDevice) const
{
if (pathOnDevice.needsDevice()) {
// Already correct form, only sanity check it's ours...
QTC_CHECK(handlesFile(pathOnDevice));
return pathOnDevice;
}
FilePath result;
result.setScheme("ssh");
result.setHost(userAtHost());
result.setPath(pathOnDevice.path());
return result;
}
bool LinuxDevice::handlesFile(const FilePath &filePath) const bool LinuxDevice::handlesFile(const FilePath &filePath) const
{ {
return filePath.scheme() == "ssh" && filePath.host() == userAtHost(); return filePath.scheme() == "ssh" && filePath.host() == userAtHost();

View File

@@ -58,6 +58,8 @@ public:
ProjectExplorer::DeviceEnvironmentFetcher::Ptr environmentFetcher() const override; ProjectExplorer::DeviceEnvironmentFetcher::Ptr environmentFetcher() const override;
QString userAtHost() const; QString userAtHost() const;
Utils::FilePath mapToGlobalPath(const Utils::FilePath &pathOnDevice) const override;
bool handlesFile(const Utils::FilePath &filePath) const override; bool handlesFile(const Utils::FilePath &filePath) const override;
bool isExecutableFile(const Utils::FilePath &filePath) const override; bool isExecutableFile(const Utils::FilePath &filePath) const override;
bool isReadableFile(const Utils::FilePath &filePath) const override; bool isReadableFile(const Utils::FilePath &filePath) const override;

View File

@@ -28,14 +28,15 @@
#include <ssh/sshremoteprocessrunner.h> #include <ssh/sshremoteprocessrunner.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QScopeGuard>
namespace RemoteLinux { namespace RemoteLinux {
namespace Internal { namespace Internal {
class RemoteLinuxCheckForFreeDiskSpaceServicePrivate class RemoteLinuxCheckForFreeDiskSpaceServicePrivate
{ {
public: public:
QString pathToCheck; QString pathToCheck;
quint64 requiredSpaceInBytes; quint64 requiredSpaceInBytes = 0;
QSsh::SshRemoteProcessRunner *processRunner;
}; };
} // namespace Internal } // namespace Internal
@@ -43,13 +44,10 @@ RemoteLinuxCheckForFreeDiskSpaceService::RemoteLinuxCheckForFreeDiskSpaceService
: AbstractRemoteLinuxDeployService(parent), : AbstractRemoteLinuxDeployService(parent),
d(new Internal::RemoteLinuxCheckForFreeDiskSpaceServicePrivate) d(new Internal::RemoteLinuxCheckForFreeDiskSpaceServicePrivate)
{ {
d->processRunner = nullptr;
d->requiredSpaceInBytes = 0;
} }
RemoteLinuxCheckForFreeDiskSpaceService::~RemoteLinuxCheckForFreeDiskSpaceService() RemoteLinuxCheckForFreeDiskSpaceService::~RemoteLinuxCheckForFreeDiskSpaceService()
{ {
cleanup();
delete d; delete d;
} }
@@ -63,44 +61,31 @@ void RemoteLinuxCheckForFreeDiskSpaceService::setRequiredSpaceInBytes(quint64 si
d->requiredSpaceInBytes = sizeInBytes; d->requiredSpaceInBytes = sizeInBytes;
} }
void RemoteLinuxCheckForFreeDiskSpaceService::handleStdErr() void RemoteLinuxCheckForFreeDiskSpaceService::deployAndFinish()
{ {
emit stdErrData(QString::fromUtf8(d->processRunner->readAllStandardError())); auto cleanup = qScopeGuard([this] { setFinished(); });
} const Utils::FilePath path
= deviceConfiguration()->mapToGlobalPath(Utils::FilePath::fromString(d->pathToCheck));
void RemoteLinuxCheckForFreeDiskSpaceService::handleProcessFinished() const quint64 freeSpace = path.bytesAvailable();
{ if (freeSpace < 0) {
if (!d->processRunner->processErrorString().isEmpty()) { emit errorMessage(tr("Can't get the info about the free disk space for \"%1\"")
emit errorMessage(tr("Remote process failed: %1") .arg(path.toUserOutput()));
.arg(d->processRunner->processErrorString()));
stopDeployment();
return;
}
bool isNumber;
QByteArray processOutput = d->processRunner->readAllStandardOutput();
processOutput.chop(1); // newline
quint64 freeSpace = processOutput.toULongLong(&isNumber);
quint64 requiredSpaceInMegaBytes = d->requiredSpaceInBytes / (1024 * 1024);
if (!isNumber) {
emit errorMessage(tr("Unexpected output from remote process: \"%1\"")
.arg(QString::fromUtf8(processOutput)));
stopDeployment();
return; return;
} }
freeSpace /= 1024; // convert kilobyte to megabyte const quint64 mb = 1024 * 1024;
if (freeSpace < requiredSpaceInMegaBytes) { const quint64 freeSpaceMB = freeSpace / mb;
const quint64 requiredSpaceMB = d->requiredSpaceInBytes / mb;
if (freeSpaceMB < requiredSpaceMB) {
emit errorMessage(tr("The remote file system has only %n megabytes of free space, " emit errorMessage(tr("The remote file system has only %n megabytes of free space, "
"but %1 megabytes are required.", nullptr, freeSpace).arg(requiredSpaceInMegaBytes)); "but %1 megabytes are required.", nullptr, freeSpaceMB)
stopDeployment(); .arg(requiredSpaceMB));
return; return;
} }
emit progressMessage(tr("The remote file system has %n megabytes of free space, going ahead.", emit progressMessage(tr("The remote file system has %n megabytes of free space, going ahead.",
nullptr, freeSpace)); nullptr, freeSpaceMB));
stopDeployment();
} }
CheckResult RemoteLinuxCheckForFreeDiskSpaceService::isDeploymentPossible() const CheckResult RemoteLinuxCheckForFreeDiskSpaceService::isDeploymentPossible() const
@@ -114,32 +99,4 @@ CheckResult RemoteLinuxCheckForFreeDiskSpaceService::isDeploymentPossible() cons
return AbstractRemoteLinuxDeployService::isDeploymentPossible(); return AbstractRemoteLinuxDeployService::isDeploymentPossible();
} }
void RemoteLinuxCheckForFreeDiskSpaceService::doDeploy()
{
d->processRunner = new QSsh::SshRemoteProcessRunner;
connect(d->processRunner, &QSsh::SshRemoteProcessRunner::processClosed,
this, &RemoteLinuxCheckForFreeDiskSpaceService::handleProcessFinished);
connect(d->processRunner, &QSsh::SshRemoteProcessRunner::readyReadStandardError,
this, &RemoteLinuxCheckForFreeDiskSpaceService::handleStdErr);
const QString command = QString::fromLatin1("df -k %1 |tail -n 1 |sed 's/ */ /g' "
"|cut -d ' ' -f 4").arg(d->pathToCheck);
d->processRunner->run(command, deviceConfiguration()->sshParameters());
}
void RemoteLinuxCheckForFreeDiskSpaceService::stopDeployment()
{
cleanup();
handleDeploymentDone();
}
void RemoteLinuxCheckForFreeDiskSpaceService::cleanup()
{
if (d->processRunner) {
disconnect(d->processRunner, nullptr, this, nullptr);
d->processRunner->cancel();
delete d->processRunner;
d->processRunner = nullptr;
}
}
} // namespace RemoteLinux } // namespace RemoteLinux

View File

@@ -42,16 +42,15 @@ public:
void setRequiredSpaceInBytes(quint64 sizeInBytes); void setRequiredSpaceInBytes(quint64 sizeInBytes);
private: private:
void handleStdErr(); void deployAndFinish();
void handleProcessFinished();
bool isDeploymentNecessary() const override { return true; } bool isDeploymentNecessary() const override { return true; }
CheckResult isDeploymentPossible() const override; CheckResult isDeploymentPossible() const override;
void doDeploy() override; void doDeviceSetup() final { deployAndFinish(); }
void stopDeployment() override; void stopDeviceSetup() final { }
void cleanup(); void doDeploy() final {};
void stopDeployment() final {};
Internal::RemoteLinuxCheckForFreeDiskSpaceServicePrivate * const d; Internal::RemoteLinuxCheckForFreeDiskSpaceServicePrivate * const d;
}; };