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 ...@@ -149,15 +149,10 @@ class SFTPSessionImpl
} }
}; // class SFTPSessionImpl }; // class SFTPSessionImpl
SFTPSession::SFTPSession() SFTPSession::SFTPSession() { p = nullptr; }
{
radix_tagged_line("SFTPSession::SFTPSession()");
p = nullptr;
}
SFTPSession::~SFTPSession() SFTPSession::~SFTPSession()
{ {
radix_tagged_line("SFTPSession::~SFTPSession()");
if (p != nullptr) delete p; if (p != nullptr) delete p;
} }
...@@ -192,14 +187,15 @@ bool SFTPSession::rmdir(QString dirName) ...@@ -192,14 +187,15 @@ bool SFTPSession::rmdir(QString dirName)
return true; 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; } QString SFTPSession::error() { return p->errorMsg; }
...@@ -244,7 +240,6 @@ SFTPFile::SFTPFile(SFTPSession& sftpSession, QString filename, int accessType) ...@@ -244,7 +240,6 @@ SFTPFile::SFTPFile(SFTPSession& sftpSession, QString filename, int accessType)
SFTPFile::~SFTPFile() SFTPFile::~SFTPFile()
{ {
radix_tagged_line("SFTPFile::~SFTPFile()");
if (p != nullptr) delete p; if (p != nullptr) delete p;
} }
...@@ -321,14 +316,13 @@ class SFTPDirImpl ...@@ -321,14 +316,13 @@ class SFTPDirImpl
} }
}; // 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() SFTPDir::~SFTPDir()
{ {
radix_tagged_line("SFTPDir::~SFTPDir()");
if (p != nullptr) delete p; if (p != nullptr) delete p;
} }
...@@ -721,12 +715,12 @@ Channel Session::newChannel() ...@@ -721,12 +715,12 @@ Channel Session::newChannel()
return channel; return channel;
} }
SFTPSession Session::newSFTPSession() SFTPSession* Session::newSFTPSession()
{ {
assert_ssh_session(p->session, assert_ssh_session(p->session,
"newSFTPSession() -- SSH Session is not allocated."); "newSFTPSession() -- SSH Session is not allocated.");
SFTPSession sftpSession; SFTPSession* sftpSession = new SFTPSession;
sftpSession.p = new SFTPSessionImpl(p->session); sftpSession->p = new SFTPSessionImpl(p->session);
return sftpSession; return sftpSession;
} }
......
...@@ -307,13 +307,16 @@ class RSM_PUBLIC Session ...@@ -307,13 +307,16 @@ class RSM_PUBLIC Session
* @brief newSFTPSession Allocates a new SFTP session from this SSH session * @brief newSFTPSession Allocates a new SFTP session from this SSH session
* @return * @return
*/ */
SFTPSession newSFTPSession(); SFTPSession* newSFTPSession();
}; // class Session }; // class Session
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class RSM_PUBLIC SFTPSession class RSM_PUBLIC SFTPSession
{ {
private:
SFTPSession(const SFTPSession&) = delete;
protected: protected:
// Private implementation // Private implementation
SFTPSessionImpl* p; SFTPSessionImpl* p;
...@@ -324,8 +327,8 @@ class RSM_PUBLIC SFTPSession ...@@ -324,8 +327,8 @@ class RSM_PUBLIC SFTPSession
bool mkdir(QString dirName); bool mkdir(QString dirName);
bool rmdir(QString dirName); bool rmdir(QString dirName);
// int accessType = O_RDONLY | O_WRONLY | ...; see fcntl.h // int accessType = O_RDONLY | O_WRONLY | ...; see fcntl.h
SFTPFile openFile(QString fileName, int accessType); SFTPFile* openFile(QString fileName, int accessType);
SFTPDir openDir(QString dirName); SFTPDir* openDir(QString dirName);
QString error(); QString error();
QString canonicalize(QString path); QString canonicalize(QString path);
...@@ -338,6 +341,8 @@ class RSM_PUBLIC SFTPSession ...@@ -338,6 +341,8 @@ class RSM_PUBLIC SFTPSession
class RSM_PUBLIC SFTPFile class RSM_PUBLIC SFTPFile
{ {
private: private:
SFTPFile(const SFTPFile&) = delete;
// Private implementation // Private implementation
SFTPFileImpl* p; SFTPFileImpl* p;
...@@ -383,11 +388,13 @@ struct SFTPAttributes ...@@ -383,11 +388,13 @@ struct SFTPAttributes
class RSM_PUBLIC SFTPDir class RSM_PUBLIC SFTPDir
{ {
private: private:
SFTPDir(const SFTPDir&) = delete;
// Private implementation // Private implementation
SFTPDirImpl* p; SFTPDirImpl* p;
public: public:
SFTPDir(SFTPSession sftpSession, QString dirName); SFTPDir(SFTPSession* sftpSession, QString dirName);
~SFTPDir(); ~SFTPDir();
bool isOpen(); bool isOpen();
bool hasNext(); bool hasNext();
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
#include <fcntl.h> #include <fcntl.h>
#include <memory>
#include <QApplication> #include <QApplication>
#include <QGridLayout> #include <QGridLayout>
...@@ -438,16 +439,18 @@ void ExamplePortalWidget::openFile() ...@@ -438,16 +439,18 @@ void ExamplePortalWidget::openFile()
radix_tagged_line( radix_tagged_line(
"Opening remote file:" << mOpenFileEdit->text().toStdString()); "Opening remote file:" << mOpenFileEdit->text().toStdString());
SFTPSession sftpSession = mSessionCon->session()->newSFTPSession(); std::unique_ptr<SFTPSession> sftpSession(
SFTPFile sftpFile = sftpSession.openFile(mOpenFileEdit->text(), O_RDONLY); mSessionCon->session()->newSFTPSession());
if (!sftpFile.isOpen()) std::unique_ptr<SFTPFile> sftpFile(
sftpSession->openFile(mOpenFileEdit->text(), O_RDONLY));
if (!sftpFile->isOpen())
{ {
mTextEdit->append("FAILED to open file"); mTextEdit->append("FAILED to open file");
return; return;
} }
// read some of the file // read some of the file
QString tmpStr = sftpFile.read(1000); QString tmpStr = sftpFile->read(1000);
if (!sftpFile.isOpen()) if (!sftpFile->isOpen())
{ {
{ {
mTextEdit->append("ERROR reading file"); mTextEdit->append("ERROR reading file");
...@@ -456,7 +459,7 @@ void ExamplePortalWidget::openFile() ...@@ -456,7 +459,7 @@ void ExamplePortalWidget::openFile()
} }
mTextEdit->append("File contents:"); mTextEdit->append("File contents:");
mTextEdit->append(tmpStr); mTextEdit->append(tmpStr);
if (!sftpFile.close()) if (!sftpFile->close())
{ {
mTextEdit->append("ERROR closing file"); mTextEdit->append("ERROR closing file");
return; return;
...@@ -468,18 +471,20 @@ void ExamplePortalWidget::openDir() ...@@ -468,18 +471,20 @@ void ExamplePortalWidget::openDir()
radix_tagged_line( radix_tagged_line(
"Opening remote directory:" << mOpenFileEdit->text().toStdString()); "Opening remote directory:" << mOpenFileEdit->text().toStdString());
SFTPSession sftpSession = mSessionCon->session()->newSFTPSession(); std::unique_ptr<SFTPSession> sftpSession(
SFTPDir sftpDir = sftpSession.openDir(mOpenDirEdit->text()); mSessionCon->session()->newSFTPSession());
if (!sftpDir.isOpen()) std::unique_ptr<SFTPDir> sftpDir(sftpSession->openDir(mOpenDirEdit->text()));
if (!sftpDir->isOpen())
{ {
mTextEdit->append("FAILED to open directory"); mTextEdit->append("FAILED to open directory");
return; return;
} }
mTextEdit->append("Directory entries:"); mTextEdit->append("Directory entries:");
// read dir entries // read dir entries
while (sftpDir.hasNext()) while (sftpDir->hasNext())
{ {
SFTPAttributes *a = sftpDir.next(); SFTPAttributes *a = sftpDir->next();
if (nullptr == a) if (nullptr == a)
{ {
mTextEdit->append("ERROR reading directory entry"); mTextEdit->append("ERROR reading directory entry");
...@@ -488,7 +493,7 @@ void ExamplePortalWidget::openDir() ...@@ -488,7 +493,7 @@ void ExamplePortalWidget::openDir()
mTextEdit->append(a->longname); mTextEdit->append(a->longname);
delete a; delete a;
} }
if (!sftpDir.close()) if (!sftpDir->close())
{ {
mTextEdit->append("ERROR closing directory"); mTextEdit->append("ERROR closing directory");
return; 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