Commit 40d7cf30 authored by Huff, Israel's avatar Huff, Israel
Browse files

- deleted copy constructors for SFTPSession, SFTPFile and SFTPDir

- now returning pointers instead of objects when creating new SFTPSession, SFTPFile and SFTPDir objects
- removed some debug messages
- changed example program to use the new methods (dir example now works -- no more crashing)
parent 7456bcdc
Pipeline #100255 failed with stages
in 5 minutes and 10 seconds
......@@ -149,15 +149,10 @@ class SFTPSessionImpl
}
}; // class SFTPSessionImpl
SFTPSession::SFTPSession()
{
radix_tagged_line("SFTPSession::SFTPSession()");
p = nullptr;
}
SFTPSession::SFTPSession() { p = nullptr; }
SFTPSession::~SFTPSession()
{
radix_tagged_line("SFTPSession::~SFTPSession()");
if (p != nullptr) delete p;
}
......@@ -192,14 +187,15 @@ bool SFTPSession::rmdir(QString dirName)
return true;
}
SFTPFile SFTPSession::openFile(QString filename, int accessType)
SFTPFile* SFTPSession::openFile(QString filename, int accessType)
{
return SFTPFile(*this, filename.toStdString().c_str(), accessType);
return new SFTPFile(*this, filename.toStdString().c_str(), accessType);
}
SFTPDir SFTPSession::openDir(QString dirName)
SFTPDir* SFTPSession::openDir(QString dirName)
{
return SFTPDir(*this, dirName.toStdString().c_str());
SFTPDir* sftpDir = new SFTPDir(this, dirName.toStdString().c_str());
return sftpDir;
}
QString SFTPSession::error() { return p->errorMsg; }
......@@ -244,7 +240,6 @@ SFTPFile::SFTPFile(SFTPSession& sftpSession, QString filename, int accessType)
SFTPFile::~SFTPFile()
{
radix_tagged_line("SFTPFile::~SFTPFile()");
if (p != nullptr) delete p;
}
......@@ -321,14 +316,13 @@ class SFTPDirImpl
}
}; // class SFTPDirImpl
SFTPDir::SFTPDir(SFTPSession sftpSession, QString dirName)
SFTPDir::SFTPDir(SFTPSession* sftpSession, QString dirName)
{
p = new SFTPDirImpl(sftpSession.p->sftpSession, dirName);
p = new SFTPDirImpl(sftpSession->p->sftpSession, dirName);
}
SFTPDir::~SFTPDir()
{
radix_tagged_line("SFTPDir::~SFTPDir()");
if (p != nullptr) delete p;
}
......@@ -721,12 +715,12 @@ Channel Session::newChannel()
return channel;
}
SFTPSession Session::newSFTPSession()
SFTPSession* Session::newSFTPSession()
{
assert_ssh_session(p->session,
"newSFTPSession() -- SSH Session is not allocated.");
SFTPSession sftpSession;
sftpSession.p = new SFTPSessionImpl(p->session);
SFTPSession* sftpSession = new SFTPSession;
sftpSession->p = new SFTPSessionImpl(p->session);
return sftpSession;
}
......
......@@ -307,13 +307,16 @@ class RSM_PUBLIC Session
* @brief newSFTPSession Allocates a new SFTP session from this SSH session
* @return
*/
SFTPSession newSFTPSession();
SFTPSession* newSFTPSession();
}; // class Session
// ----------------------------------------------------------------------------
class RSM_PUBLIC SFTPSession
{
private:
SFTPSession(const SFTPSession&) = delete;
protected:
// Private implementation
SFTPSessionImpl* p;
......@@ -324,8 +327,8 @@ class RSM_PUBLIC SFTPSession
bool mkdir(QString dirName);
bool rmdir(QString dirName);
// int accessType = O_RDONLY | O_WRONLY | ...; see fcntl.h
SFTPFile openFile(QString fileName, int accessType);
SFTPDir openDir(QString dirName);
SFTPFile* openFile(QString fileName, int accessType);
SFTPDir* openDir(QString dirName);
QString error();
QString canonicalize(QString path);
......@@ -338,6 +341,8 @@ class RSM_PUBLIC SFTPSession
class RSM_PUBLIC SFTPFile
{
private:
SFTPFile(const SFTPFile&) = delete;
// Private implementation
SFTPFileImpl* p;
......@@ -383,11 +388,13 @@ struct SFTPAttributes
class RSM_PUBLIC SFTPDir
{
private:
SFTPDir(const SFTPDir&) = delete;
// Private implementation
SFTPDirImpl* p;
public:
SFTPDir(SFTPSession sftpSession, QString dirName);
SFTPDir(SFTPSession* sftpSession, QString dirName);
~SFTPDir();
bool isOpen();
bool hasNext();
......
......@@ -6,6 +6,7 @@
*/
#include <fcntl.h>
#include <memory>
#include <QApplication>
#include <QGridLayout>
......@@ -438,16 +439,18 @@ void ExamplePortalWidget::openFile()
radix_tagged_line(
"Opening remote file:" << mOpenFileEdit->text().toStdString());
SFTPSession sftpSession = mSessionCon->session()->newSFTPSession();
SFTPFile sftpFile = sftpSession.openFile(mOpenFileEdit->text(), O_RDONLY);
if (!sftpFile.isOpen())
std::unique_ptr<SFTPSession> sftpSession(
mSessionCon->session()->newSFTPSession());
std::unique_ptr<SFTPFile> sftpFile(
sftpSession->openFile(mOpenFileEdit->text(), O_RDONLY));
if (!sftpFile->isOpen())
{
mTextEdit->append("FAILED to open file");
return;
}
// read some of the file
QString tmpStr = sftpFile.read(1000);
if (!sftpFile.isOpen())
QString tmpStr = sftpFile->read(1000);
if (!sftpFile->isOpen())
{
{
mTextEdit->append("ERROR reading file");
......@@ -456,7 +459,7 @@ void ExamplePortalWidget::openFile()
}
mTextEdit->append("File contents:");
mTextEdit->append(tmpStr);
if (!sftpFile.close())
if (!sftpFile->close())
{
mTextEdit->append("ERROR closing file");
return;
......@@ -468,18 +471,20 @@ void ExamplePortalWidget::openDir()
radix_tagged_line(
"Opening remote directory:" << mOpenFileEdit->text().toStdString());
SFTPSession sftpSession = mSessionCon->session()->newSFTPSession();
SFTPDir sftpDir = sftpSession.openDir(mOpenDirEdit->text());
if (!sftpDir.isOpen())
std::unique_ptr<SFTPSession> sftpSession(
mSessionCon->session()->newSFTPSession());
std::unique_ptr<SFTPDir> sftpDir(sftpSession->openDir(mOpenDirEdit->text()));
if (!sftpDir->isOpen())
{
mTextEdit->append("FAILED to open directory");
return;
}
mTextEdit->append("Directory entries:");
// read dir entries
while (sftpDir.hasNext())
while (sftpDir->hasNext())
{
SFTPAttributes *a = sftpDir.next();
SFTPAttributes *a = sftpDir->next();
if (nullptr == a)
{
mTextEdit->append("ERROR reading directory entry");
......@@ -488,7 +493,7 @@ void ExamplePortalWidget::openDir()
mTextEdit->append(a->longname);
delete a;
}
if (!sftpDir.close())
if (!sftpDir->close())
{
mTextEdit->append("ERROR closing directory");
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment