forked from qt-creator/qt-creator
Get rid of SftpTransfer
Remove also sftpdefs.h. They are not used anymore. Change-Id: I6c45a70cec2e01afdd1a668068e090f5d4abde9b Reviewed-by: Christian Kandeler <christian.kandeler@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
add_qtc_library(QtcSsh
|
||||
DEPENDS Qt5::Core Qt5::Network Qt5::Widgets Utils
|
||||
SOURCES
|
||||
sftpdefs.cpp sftpdefs.h
|
||||
sftptransfer.cpp sftptransfer.h
|
||||
ssh.qrc
|
||||
ssh_global.h
|
||||
sshconnection.cpp sshconnection.h
|
||||
|
@@ -1,28 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt 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 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "sftpdefs.h"
|
||||
|
||||
namespace QSsh { const SftpJobId SftpInvalidJob = 0; }
|
@@ -1,71 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt 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 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ssh_global.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace QSsh {
|
||||
|
||||
class SftpTransfer;
|
||||
using SftpTransferPtr = std::unique_ptr<SftpTransfer>;
|
||||
|
||||
class FileToTransfer
|
||||
{
|
||||
public:
|
||||
FileToTransfer(const QString &source, const QString &target)
|
||||
: sourceFile(source), targetFile(target) {}
|
||||
QString sourceFile;
|
||||
QString targetFile;
|
||||
};
|
||||
using FilesToTransfer = QList<FileToTransfer>;
|
||||
|
||||
namespace Internal { enum class FileTransferType { Upload, Download }; }
|
||||
|
||||
typedef quint32 SftpJobId;
|
||||
QSSH_EXPORT extern const SftpJobId SftpInvalidJob;
|
||||
|
||||
enum SftpOverwriteMode {
|
||||
SftpOverwriteExisting, SftpAppendToExisting, SftpSkipExisting
|
||||
};
|
||||
|
||||
enum SftpFileType { FileTypeRegular, FileTypeDirectory, FileTypeOther, FileTypeUnknown };
|
||||
|
||||
class QSSH_EXPORT SftpFileInfo
|
||||
{
|
||||
public:
|
||||
QString name;
|
||||
SftpFileType type = FileTypeUnknown;
|
||||
quint64 size = 0;
|
||||
QFile::Permissions permissions;
|
||||
};
|
||||
|
||||
} // namespace QSsh
|
@@ -1,189 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt 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 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "sftptransfer.h"
|
||||
|
||||
#include "sshlogging_p.h"
|
||||
#include "sshconnection.h"
|
||||
#include "sshsettings.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QStringList>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTimer>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/commandline.h>
|
||||
#include <utils/qtcprocess.h>
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace QSsh {
|
||||
|
||||
struct SftpTransfer::SftpTransferPrivate
|
||||
{
|
||||
QtcProcess sftpProc;
|
||||
FilesToTransfer files;
|
||||
Internal::FileTransferType transferType;
|
||||
QStringList connectionArgs;
|
||||
QString batchFilePath;
|
||||
|
||||
QStringList dirsToCreate() const
|
||||
{
|
||||
QStringList dirs;
|
||||
for (const FileToTransfer &f : qAsConst(files)) {
|
||||
QString parentDir = QFileInfo(f.targetFile).path();
|
||||
while (true) {
|
||||
if (dirs.contains(parentDir) || !parentDir.startsWith('/'))
|
||||
break;
|
||||
dirs << parentDir;
|
||||
parentDir = QFileInfo(parentDir).path();
|
||||
}
|
||||
}
|
||||
sort(dirs, [](const QString &d1, const QString &d2) {
|
||||
if (d1 == "/" && d2 != "/")
|
||||
return true;
|
||||
return d1.count('/') < d2.count('/');
|
||||
});
|
||||
return dirs;
|
||||
}
|
||||
QByteArray transferCommand(bool link) const
|
||||
{
|
||||
QByteArray command;
|
||||
switch (transferType) {
|
||||
case Internal::FileTransferType::Upload:
|
||||
command = link ? "ln -s" : "put";
|
||||
break;
|
||||
case Internal::FileTransferType::Download:
|
||||
command = "get";
|
||||
break;
|
||||
}
|
||||
return command;
|
||||
}
|
||||
};
|
||||
|
||||
SftpTransfer::~SftpTransfer()
|
||||
{
|
||||
if (!d->batchFilePath.isEmpty() && !QFile::remove(d->batchFilePath))
|
||||
qCWarning(Internal::sshLog) << "failed to remove batch file" << d->batchFilePath;
|
||||
delete d;
|
||||
}
|
||||
|
||||
void SftpTransfer::start()
|
||||
{
|
||||
QTimer::singleShot(0, this, &SftpTransfer::doStart);
|
||||
}
|
||||
|
||||
void SftpTransfer::stop()
|
||||
{
|
||||
d->sftpProc.terminate();
|
||||
}
|
||||
|
||||
SftpTransfer::SftpTransfer(const FilesToTransfer &files, Internal::FileTransferType type,
|
||||
const QStringList &connectionArgs)
|
||||
: d(new SftpTransferPrivate)
|
||||
{
|
||||
SshConnectionParameters::setupSshEnvironment(&d->sftpProc);
|
||||
d->files = files;
|
||||
d->transferType = type;
|
||||
d->connectionArgs = connectionArgs;
|
||||
connect(&d->sftpProc, &QtcProcess::done, [this] {
|
||||
if (d->sftpProc.error() == QProcess::FailedToStart) {
|
||||
emitError(tr("sftp failed to start: %1").arg(d->sftpProc.errorString()));
|
||||
return;
|
||||
}
|
||||
if (d->sftpProc.exitStatus() != QProcess::NormalExit) {
|
||||
emitError(tr("sftp crashed."));
|
||||
return;
|
||||
}
|
||||
if (d->sftpProc.exitCode() != 0) {
|
||||
emitError(QString::fromLocal8Bit(d->sftpProc.readAllStandardError()));
|
||||
return;
|
||||
}
|
||||
emit done(QString());
|
||||
});
|
||||
connect(&d->sftpProc, &QtcProcess::readyReadStandardOutput, [this] {
|
||||
emit progress(QString::fromLocal8Bit(d->sftpProc.readAllStandardOutput()));
|
||||
});
|
||||
}
|
||||
|
||||
void SftpTransfer::doStart()
|
||||
{
|
||||
const FilePath sftpBinary = SshSettings::sftpFilePath();
|
||||
if (!sftpBinary.exists()) {
|
||||
emitError(tr("sftp binary \"%1\" does not exist.").arg(sftpBinary.toUserOutput()));
|
||||
return;
|
||||
}
|
||||
QTemporaryFile batchFile;
|
||||
batchFile.setAutoRemove(false);
|
||||
if (!batchFile.isOpen() && !batchFile.open()) {
|
||||
emitError(tr("Could not create temporary file: %1").arg(batchFile.errorString()));
|
||||
return;
|
||||
}
|
||||
d->batchFilePath = batchFile.fileName();
|
||||
batchFile.resize(0);
|
||||
for (const QString &dir : d->dirsToCreate()) {
|
||||
switch (d->transferType) {
|
||||
case Internal::FileTransferType::Upload:
|
||||
batchFile.write("-mkdir " + ProcessArgs::quoteArgUnix(dir).toLocal8Bit() + '\n');
|
||||
break;
|
||||
case Internal::FileTransferType::Download:
|
||||
if (!QDir::root().mkpath(dir)) {
|
||||
emitError(tr("Failed to create local directory \"%1\".")
|
||||
.arg(QDir::toNativeSeparators(dir)));
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (const FileToTransfer &f : qAsConst(d->files)) {
|
||||
QString sourceFileOrLinkTarget = f.sourceFile;
|
||||
bool link = false;
|
||||
if (d->transferType == Internal::FileTransferType::Upload) {
|
||||
QFileInfo fi(f.sourceFile);
|
||||
if (fi.isSymLink()) {
|
||||
link = true;
|
||||
batchFile.write("-rm " + ProcessArgs::quoteArgUnix(f.targetFile).toLocal8Bit()
|
||||
+ '\n');
|
||||
sourceFileOrLinkTarget = fi.dir().relativeFilePath(fi.symLinkTarget()); // see QTBUG-5817.
|
||||
}
|
||||
}
|
||||
batchFile.write(d->transferCommand(link) + ' '
|
||||
+ ProcessArgs::quoteArgUnix(sourceFileOrLinkTarget).toLocal8Bit() + ' '
|
||||
+ ProcessArgs::quoteArgUnix(f.targetFile).toLocal8Bit() + '\n');
|
||||
}
|
||||
d->sftpProc.setStandardInputFile(batchFile.fileName());
|
||||
d->sftpProc.setCommand(CommandLine(sftpBinary, d->connectionArgs));
|
||||
d->sftpProc.start();
|
||||
emit started();
|
||||
}
|
||||
|
||||
void SftpTransfer::emitError(const QString &details)
|
||||
{
|
||||
emit done(tr("File transfer failed: %1").arg(details));
|
||||
}
|
||||
|
||||
} // namespace QSsh
|
@@ -1,61 +0,0 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt 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 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sftpdefs.h"
|
||||
#include "ssh_global.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace QSsh {
|
||||
class SshConnection;
|
||||
|
||||
class QSSH_EXPORT SftpTransfer : public QObject
|
||||
{
|
||||
friend class SshConnection;
|
||||
Q_OBJECT
|
||||
public:
|
||||
~SftpTransfer();
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
signals:
|
||||
void started();
|
||||
void done(const QString &error);
|
||||
void progress(const QString &output);
|
||||
|
||||
private:
|
||||
SftpTransfer(const FilesToTransfer &files, Internal::FileTransferType type,
|
||||
const QStringList &connectionArgs);
|
||||
void doStart();
|
||||
void emitError(const QString &details);
|
||||
|
||||
struct SftpTransferPrivate;
|
||||
SftpTransferPrivate * const d;
|
||||
};
|
||||
|
||||
} // namespace QSsh
|
@@ -17,10 +17,6 @@ Project {
|
||||
Depends { name: "Utils" }
|
||||
|
||||
files: [
|
||||
"sftpdefs.cpp",
|
||||
"sftpdefs.h",
|
||||
"sftptransfer.cpp",
|
||||
"sftptransfer.h",
|
||||
"ssh.qrc",
|
||||
"sshconnection.h",
|
||||
"sshconnection.cpp",
|
||||
|
@@ -25,7 +25,6 @@
|
||||
|
||||
#include "sshconnection.h"
|
||||
|
||||
#include "sftptransfer.h"
|
||||
#include "sshlogging_p.h"
|
||||
#include "sshsettings.h"
|
||||
|
||||
@@ -198,8 +197,6 @@ struct SshConnection::SshConnectionPrivate
|
||||
SshConnection::SshConnection(const SshConnectionParameters &serverInfo, QObject *parent)
|
||||
: QObject(parent), d(new SshConnectionPrivate(serverInfo))
|
||||
{
|
||||
qRegisterMetaType<QSsh::SftpFileInfo>("QSsh::SftpFileInfo");
|
||||
qRegisterMetaType<QList <QSsh::SftpFileInfo> >("QList<QSsh::SftpFileInfo>");
|
||||
connect(&d->masterProcess, &QtcProcess::readyReadStandardOutput, [this] {
|
||||
const QByteArray reply = d->masterProcess.readAllStandardOutput();
|
||||
if (reply == "\n")
|
||||
@@ -287,16 +284,6 @@ SshConnection::~SshConnection()
|
||||
delete d;
|
||||
}
|
||||
|
||||
SftpTransferPtr SshConnection::createUpload(const FilesToTransfer &files)
|
||||
{
|
||||
return setupTransfer(files, Internal::FileTransferType::Upload);
|
||||
}
|
||||
|
||||
SftpTransferPtr SshConnection::createDownload(const FilesToTransfer &files)
|
||||
{
|
||||
return setupTransfer(files, Internal::FileTransferType::Download);
|
||||
}
|
||||
|
||||
void SshConnection::doConnectToHost()
|
||||
{
|
||||
if (d->state != Connecting)
|
||||
@@ -353,14 +340,6 @@ void SshConnection::emitDisconnected()
|
||||
emit disconnected();
|
||||
}
|
||||
|
||||
SftpTransferPtr SshConnection::setupTransfer(const FilesToTransfer &files,
|
||||
Internal::FileTransferType type)
|
||||
{
|
||||
QTC_ASSERT(state() == Connected, return SftpTransferPtr());
|
||||
return SftpTransferPtr(new SftpTransfer(files, type,
|
||||
d->connectionArgs(SshSettings::sftpFilePath())));
|
||||
}
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
namespace SshTest {
|
||||
const QString getHostFromEnvironment()
|
||||
|
@@ -25,7 +25,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sftpdefs.h"
|
||||
#include "ssh_global.h"
|
||||
|
||||
#include <utils/filepath.h>
|
||||
@@ -99,9 +98,6 @@ public:
|
||||
bool sharingEnabled() const;
|
||||
~SshConnection();
|
||||
|
||||
SftpTransferPtr createUpload(const FilesToTransfer &files);
|
||||
SftpTransferPtr createDownload(const FilesToTransfer &files);
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
void disconnected();
|
||||
@@ -112,7 +108,6 @@ private:
|
||||
void emitError(const QString &reason);
|
||||
void emitConnected();
|
||||
void emitDisconnected();
|
||||
SftpTransferPtr setupTransfer(const FilesToTransfer &files, Internal::FileTransferType type);
|
||||
|
||||
struct SshConnectionPrivate;
|
||||
SshConnectionPrivate * const d;
|
||||
|
@@ -23,7 +23,6 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <ssh/sftptransfer.h>
|
||||
#include <ssh/sshconnection.h>
|
||||
#include <ssh/sshsettings.h>
|
||||
|
||||
@@ -170,23 +169,6 @@ void tst_Ssh::sftp()
|
||||
QVERIFY2(dirForFilesToUpload.isValid(), qPrintable(dirForFilesToUpload.errorString()));
|
||||
QVERIFY2(dirForFilesToDownload.isValid(), qPrintable(dirForFilesToDownload.errorString()));
|
||||
QVERIFY2(dir2ForFilesToDownload.isValid(), qPrintable(dirForFilesToDownload.errorString()));
|
||||
static const auto getRemoteFilePath = [](const QString &localFileName) {
|
||||
return QString("/tmp/").append(localFileName).append(".upload");
|
||||
};
|
||||
FilesToTransfer filesToUpload;
|
||||
std::srand(QDateTime::currentDateTime().toSecsSinceEpoch());
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
const QString fileName = "sftptestfile" + QString::number(i + 1);
|
||||
QFile file(dirForFilesToUpload.path() + '/' + fileName);
|
||||
QVERIFY2(file.open(QIODevice::WriteOnly), qPrintable(file.errorString()));
|
||||
int content[1024 / sizeof(int)];
|
||||
for (size_t j = 0; j < sizeof content / sizeof content[0]; ++j)
|
||||
content[j] = QRandomGenerator::global()->generate();
|
||||
file.write(reinterpret_cast<char *>(content), sizeof content);
|
||||
file.close();
|
||||
QVERIFY2(file.error() == QFile::NoError, qPrintable(file.errorString()));
|
||||
filesToUpload << FileToTransfer(file.fileName(), getRemoteFilePath(fileName));
|
||||
}
|
||||
const QString bigFileName("sftpbigfile");
|
||||
QFile bigFile(dirForFilesToUpload.path() + '/' + bigFileName);
|
||||
QVERIFY2(bigFile.open(QIODevice::WriteOnly), qPrintable(bigFile.errorString()));
|
||||
@@ -201,25 +183,6 @@ void tst_Ssh::sftp()
|
||||
}
|
||||
bigFile.close();
|
||||
QVERIFY2(bigFile.error() == QFile::NoError, qPrintable(bigFile.errorString()));
|
||||
filesToUpload << FileToTransfer(bigFile.fileName(), getRemoteFilePath(bigFileName));
|
||||
|
||||
const SftpTransferPtr upload = connection.createUpload(filesToUpload);
|
||||
QString jobError;
|
||||
QEventLoop loop;
|
||||
connect(upload.get(), &SftpTransfer::done, [&jobError, &loop](const QString &error) {
|
||||
jobError = error;
|
||||
loop.quit();
|
||||
});
|
||||
QTimer timer;
|
||||
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||||
timer.setSingleShot(true);
|
||||
timer.setInterval(30 * 1000);
|
||||
timer.start();
|
||||
upload->start();
|
||||
loop.exec();
|
||||
QVERIFY(timer.isActive());
|
||||
timer.stop();
|
||||
QVERIFY2(jobError.isEmpty(), qPrintable(jobError));
|
||||
}
|
||||
|
||||
void tst_Ssh::cleanupTestCase()
|
||||
|
Reference in New Issue
Block a user