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 stopDeviceSetup() { handleDeviceSetupDone(false); }
void setFinished();
private:
void handleConnected();
void handleConnectionFailure();
@@ -115,7 +117,6 @@ private:
virtual void doDeploy() = 0;
virtual void stopDeployment() = 0;
void setFinished();
Internal::AbstractRemoteLinuxDeployServicePrivate * const d;
};

View File

@@ -403,6 +403,20 @@ QString LinuxDevice::userAtHost() const
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
{
return filePath.scheme() == "ssh" && filePath.host() == userAtHost();

View File

@@ -58,6 +58,8 @@ public:
ProjectExplorer::DeviceEnvironmentFetcher::Ptr environmentFetcher() const override;
QString userAtHost() const;
Utils::FilePath mapToGlobalPath(const Utils::FilePath &pathOnDevice) const override;
bool handlesFile(const Utils::FilePath &filePath) const override;
bool isExecutableFile(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 <utils/fileutils.h>
#include <QScopeGuard>
namespace RemoteLinux {
namespace Internal {
class RemoteLinuxCheckForFreeDiskSpaceServicePrivate
{
public:
QString pathToCheck;
quint64 requiredSpaceInBytes;
QSsh::SshRemoteProcessRunner *processRunner;
quint64 requiredSpaceInBytes = 0;
};
} // namespace Internal
@@ -43,13 +44,10 @@ RemoteLinuxCheckForFreeDiskSpaceService::RemoteLinuxCheckForFreeDiskSpaceService
: AbstractRemoteLinuxDeployService(parent),
d(new Internal::RemoteLinuxCheckForFreeDiskSpaceServicePrivate)
{
d->processRunner = nullptr;
d->requiredSpaceInBytes = 0;
}
RemoteLinuxCheckForFreeDiskSpaceService::~RemoteLinuxCheckForFreeDiskSpaceService()
{
cleanup();
delete d;
}
@@ -63,44 +61,31 @@ void RemoteLinuxCheckForFreeDiskSpaceService::setRequiredSpaceInBytes(quint64 si
d->requiredSpaceInBytes = sizeInBytes;
}
void RemoteLinuxCheckForFreeDiskSpaceService::handleStdErr()
void RemoteLinuxCheckForFreeDiskSpaceService::deployAndFinish()
{
emit stdErrData(QString::fromUtf8(d->processRunner->readAllStandardError()));
}
void RemoteLinuxCheckForFreeDiskSpaceService::handleProcessFinished()
{
if (!d->processRunner->processErrorString().isEmpty()) {
emit errorMessage(tr("Remote process failed: %1")
.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();
auto cleanup = qScopeGuard([this] { setFinished(); });
const Utils::FilePath path
= deviceConfiguration()->mapToGlobalPath(Utils::FilePath::fromString(d->pathToCheck));
const quint64 freeSpace = path.bytesAvailable();
if (freeSpace < 0) {
emit errorMessage(tr("Can't get the info about the free disk space for \"%1\"")
.arg(path.toUserOutput()));
return;
}
freeSpace /= 1024; // convert kilobyte to megabyte
if (freeSpace < requiredSpaceInMegaBytes) {
const quint64 mb = 1024 * 1024;
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, "
"but %1 megabytes are required.", nullptr, freeSpace).arg(requiredSpaceInMegaBytes));
stopDeployment();
"but %1 megabytes are required.", nullptr, freeSpaceMB)
.arg(requiredSpaceMB));
return;
}
emit progressMessage(tr("The remote file system has %n megabytes of free space, going ahead.",
nullptr, freeSpace));
stopDeployment();
nullptr, freeSpaceMB));
}
CheckResult RemoteLinuxCheckForFreeDiskSpaceService::isDeploymentPossible() const
@@ -114,32 +99,4 @@ CheckResult RemoteLinuxCheckForFreeDiskSpaceService::isDeploymentPossible() cons
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

View File

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