Commit ca863a5c authored by Huff, Israel's avatar Huff, Israel
Browse files

finished initial SFTPFile and SFTPDir implementations

parent a15bd319
Pipeline #100074 failed with stages
in 5 minutes and 12 seconds
......@@ -208,8 +208,14 @@ QString SFTPSession::canonicalize(QString path)
{
char* str =
sftp_canonicalize_path(p->sftpSession, path.toStdString().c_str());
if (nullptr == str) return "";
return QString(str);
if (nullptr == str)
{
ssh_string_free_char(str);
return "";
}
QString retVal = QString(str);
ssh_string_free_char(str);
return retVal;
}
// ----------------------------------------------------------------------------
......@@ -236,6 +242,66 @@ SFTPFile::SFTPFile(SFTPSession& sftpSession, QString filename, int accessType)
p = new SFTPFileImpl(sftpSession.p->sftpSession, filename, accessType);
}
SFTPFile::~SFTPFile()
{
radix_tagged_line("SFTPFile::~SFTPFile()");
if (p != nullptr) delete p;
}
bool SFTPFile::isOpen() { return (p->file != nullptr); }
// returns number of bytes written on success
// on error, returns < 0 and ssh and sftp session errors are set
ssize_t SFTPFile::write(QString str)
{
const char* buf = str.toStdString().c_str();
size_t count = strlen(buf);
ssize_t nwritten = sftp_write(p->file, buf, count);
return nwritten;
}
// returns empty string and closes file on error; isOpen() can be used to verify
// success
QString SFTPFile::read(size_t nBytes)
{
char* buf = new char[nBytes + 1];
buf[nBytes] = '\0';
ssize_t nBytesRead = sftp_read(p->file, buf, nBytes);
if (nBytesRead < 0)
{
close();
return QString();
}
QString str(buf);
delete[] buf;
return str;
}
bool SFTPFile::close()
{
int rc = sftp_close(p->file);
if (SSH_NO_ERROR == rc)
{
p->file = nullptr;
return true;
}
return false;
}
bool SFTPFile::seek(size_t offset)
{
int rc = sftp_seek64(p->file, offset);
if (rc < 0) return false;
return true;
}
// returns < 0 on error
ssize_t SFTPFile::tell()
{
ssize_t result = static_cast<ssize_t>(sftp_tell64(p->file));
return result;
}
// ----------------------------------------------------------------------------
class SFTPDirImpl
{
......@@ -258,6 +324,64 @@ SFTPDir::SFTPDir(SFTPSession sftpSession, QString dirName)
p = new SFTPDirImpl(sftpSession.p->sftpSession, dirName);
}
SFTPDir::~SFTPDir()
{
radix_tagged_line("SFTPDir::~SFTPDir()");
if (p != nullptr) delete p;
}
bool SFTPDir::isOpen() { return (p->dir != nullptr); }
bool SFTPDir::hasNext() { return (0 == sftp_dir_eof(p->dir)); }
// returns SFTPAttributes structure on success or nullptr on failure
// caller is responsible for deleting returned SFTPAttributes structure
SFTPAttributes* SFTPDir::next()
{
sftp_attributes sftpResult = sftp_readdir(p->dir->sftp, p->dir);
if (nullptr == sftpResult) return nullptr;
SFTPAttributes* retVal = new SFTPAttributes;
retVal->name = QString(sftpResult->name);
retVal->longname = QString(sftpResult->longname);
retVal->flags = sftpResult->flags;
retVal->type = sftpResult->type;
retVal->size = sftpResult->size;
retVal->uid = sftpResult->uid;
retVal->gid = sftpResult->gid;
retVal->owner = QString(sftpResult->owner);
retVal->group = QString(sftpResult->group);
retVal->permissions = sftpResult->permissions;
retVal->atime64 = sftpResult->atime64;
retVal->atime = sftpResult->atime;
retVal->atime_nseconds = sftpResult->atime_nseconds;
retVal->createtime = sftpResult->createtime;
retVal->createtime_nseconds = sftpResult->createtime_nseconds;
retVal->mtime64 = sftpResult->mtime64;
retVal->mtime = sftpResult->mtime;
retVal->mtime_nseconds = sftpResult->mtime_nseconds;
retVal->acl = QString(ssh_string_get_char(sftpResult->acl));
retVal->extended_count = sftpResult->extended_count;
retVal->extended_type =
QString(ssh_string_get_char(sftpResult->extended_type));
retVal->extended_data =
QString(ssh_string_get_char(sftpResult->extended_data));
return retVal;
}
bool SFTPDir::close()
{
int rc = sftp_closedir(p->dir);
if (SSH_NO_ERROR == rc)
{
p->dir = nullptr;
return true;
}
return false;
}
// ----------------------------------------------------------------------------
class SessionImpl
{
......
......@@ -345,13 +345,40 @@ class RSM_PUBLIC SFTPFile
SFTPFile(SFTPSession& sftpSession, QString filename, int accessType);
~SFTPFile();
bool isOpen();
size_t write(QString str);
ssize_t write(QString str);
QString read(size_t nBytes);
bool close();
size_t seek();
size_t tell();
bool seek(size_t offset);
ssize_t tell();
}; // class SFTPFile
// ----------------------------------------------------------------------------
struct SFTPAttributes
{
QString name;
QString longname;
uint32_t flags;
uint8_t type;
uint64_t size;
uint32_t uid;
uint32_t gid;
QString owner;
QString group;
uint32_t permissions;
uint64_t atime64;
uint32_t atime;
uint32_t atime_nseconds;
uint64_t createtime;
uint32_t createtime_nseconds;
uint64_t mtime64;
uint32_t mtime;
uint32_t mtime_nseconds;
QString acl;
uint32_t extended_count;
QString extended_type;
QString extended_data;
};
// ----------------------------------------------------------------------------
class RSM_PUBLIC SFTPDir
{
......@@ -362,8 +389,9 @@ class RSM_PUBLIC SFTPDir
public:
SFTPDir(SFTPSession sftpSession, QString dirName);
~SFTPDir();
bool isOpen();
bool hasNext();
// sftp_attributes_struct next();
SFTPAttributes* next();
bool close();
}; // class SFTPDir
......
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