Rewrite sftp file copy to Qt way (#562)
Rewrite sftp file copy to Qt way
This commit is contained in:
parent
b05a5ee1c6
commit
16db23c159
3 changed files with 17 additions and 25 deletions
|
@ -211,13 +211,7 @@ ErrorCode ServerController::uploadFileToHost(const ServerCredentials &credential
|
||||||
localFile.write(data);
|
localFile.write(data);
|
||||||
localFile.close();
|
localFile.close();
|
||||||
|
|
||||||
#ifdef Q_OS_WINDOWS
|
error = m_sshClient.sftpFileCopy(overwriteMode, localFile.fileName(), remotePath, "non_desc");
|
||||||
error = m_sshClient.sftpFileCopy(overwriteMode, localFile.fileName().toLocal8Bit().toStdString(), remotePath.toStdString(),
|
|
||||||
"non_desc");
|
|
||||||
#else
|
|
||||||
error = m_sshClient.sftpFileCopy(overwriteMode, localFile.fileName().toStdString(), remotePath.toStdString(),
|
|
||||||
"non_desc");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (error != ErrorCode::NoError) {
|
if (error != ErrorCode::NoError) {
|
||||||
return error;
|
return error;
|
||||||
|
|
|
@ -222,7 +222,7 @@ namespace libssh {
|
||||||
return fromLibsshErrorCode();
|
return fromLibsshErrorCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorCode Client::sftpFileCopy(const SftpOverwriteMode overwriteMode, const std::string& localPath, const std::string& remotePath, const std::string& fileDesc)
|
ErrorCode Client::sftpFileCopy(const SftpOverwriteMode overwriteMode, const QString& localPath, const QString& remotePath, const QString &fileDesc)
|
||||||
{
|
{
|
||||||
m_sftpSession = sftp_new(m_session);
|
m_sftpSession = sftp_new(m_session);
|
||||||
|
|
||||||
|
@ -245,40 +245,38 @@ namespace libssh {
|
||||||
const size_t bufferSize = 16384;
|
const size_t bufferSize = 16384;
|
||||||
char buffer[bufferSize];
|
char buffer[bufferSize];
|
||||||
|
|
||||||
file = sftp_open(m_sftpSession, remotePath.c_str(), accessType, S_IRWXU);
|
file = sftp_open(m_sftpSession, remotePath.toStdString().c_str(), accessType, S_IRWXU);
|
||||||
|
|
||||||
if (file == nullptr) {
|
if (file == nullptr) {
|
||||||
return closeSftpSession();
|
return closeSftpSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
int localFileSize = std::filesystem::file_size(localPath);
|
int localFileSize = QFileInfo(localPath).size();
|
||||||
int chunksCount = localFileSize / (bufferSize);
|
int chunksCount = localFileSize / (bufferSize);
|
||||||
|
|
||||||
std::ifstream fin(localPath, std::ios::binary | std::ios::in);
|
QFile fin(localPath);
|
||||||
|
|
||||||
if (fin.is_open()) {
|
if (fin.open(QIODevice::ReadOnly)) {
|
||||||
for (int currentChunkId = 0; currentChunkId < chunksCount; currentChunkId++) {
|
for (int currentChunkId = 0; currentChunkId < chunksCount; currentChunkId++) {
|
||||||
fin.read(buffer, bufferSize);
|
QByteArray chunk = fin.read(bufferSize);
|
||||||
|
if (chunk.size() != bufferSize) return ErrorCode::SshSftpEofError;
|
||||||
|
|
||||||
int bytesWritten = sftp_write(file, buffer, bufferSize);
|
int bytesWritten = sftp_write(file, chunk.data(), chunk.size());
|
||||||
|
|
||||||
std::string chunk(buffer, bufferSize);
|
if (bytesWritten != chunk.size()) {
|
||||||
|
|
||||||
if (bytesWritten != bufferSize) {
|
|
||||||
fin.close();
|
fin.close();
|
||||||
sftp_close(file);
|
sftp_close(file);
|
||||||
return closeSftpSession();
|
return closeSftpSession();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastChunkSize = localFileSize % (bufferSize);
|
int lastChunkSize = localFileSize % bufferSize;
|
||||||
|
|
||||||
if (lastChunkSize != 0) {
|
if (lastChunkSize != 0) {
|
||||||
fin.read(buffer, lastChunkSize);
|
QByteArray lastChunk = fin.read(lastChunkSize);
|
||||||
|
if (lastChunk.size() != lastChunkSize) return ErrorCode::SshSftpEofError;
|
||||||
|
|
||||||
std::string chunk(buffer, lastChunkSize);
|
int bytesWritten = sftp_write(file, lastChunk.data(), lastChunkSize);
|
||||||
|
|
||||||
int bytesWritten = sftp_write(file, buffer, lastChunkSize);
|
|
||||||
|
|
||||||
if (bytesWritten != lastChunkSize) {
|
if (bytesWritten != lastChunkSize) {
|
||||||
fin.close();
|
fin.close();
|
||||||
|
|
|
@ -33,9 +33,9 @@ namespace libssh {
|
||||||
const std::function<ErrorCode (const QString &, Client &)> &cbReadStdErr);
|
const std::function<ErrorCode (const QString &, Client &)> &cbReadStdErr);
|
||||||
ErrorCode writeResponse(const QString &data);
|
ErrorCode writeResponse(const QString &data);
|
||||||
ErrorCode sftpFileCopy(const SftpOverwriteMode overwriteMode,
|
ErrorCode sftpFileCopy(const SftpOverwriteMode overwriteMode,
|
||||||
const std::string& localPath,
|
const QString &localPath,
|
||||||
const std::string& remotePath,
|
const QString &remotePath,
|
||||||
const std::string& fileDesc);
|
const QString& fileDesc);
|
||||||
ErrorCode getDecryptedPrivateKey(const ServerCredentials &credentials, QString &decryptedPrivateKey, const std::function<QString()> &passphraseCallback);
|
ErrorCode getDecryptedPrivateKey(const ServerCredentials &credentials, QString &decryptedPrivateKey, const std::function<QString()> &passphraseCallback);
|
||||||
private:
|
private:
|
||||||
ErrorCode closeChannel();
|
ErrorCode closeChannel();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue