279 lines
6.1 KiB
C++
279 lines
6.1 KiB
C++
#include "lib9pclient.h"
|
|
|
|
Lib9pClient::Lib9pClient(QObject *parent)
|
|
: QObject(parent)
|
|
{
|
|
}
|
|
|
|
Lib9pClient::~Lib9pClient()
|
|
{
|
|
disconnect();
|
|
}
|
|
|
|
void Lib9pClient::updateLastError()
|
|
{
|
|
char *err = ollie9p_last_error();
|
|
if (err) {
|
|
m_lastError = QString::fromUtf8(err);
|
|
ollie9p_free(err);
|
|
} else {
|
|
m_lastError.clear();
|
|
}
|
|
}
|
|
|
|
bool Lib9pClient::connect(const QString &addr, const QString &user, const QString &aname)
|
|
{
|
|
if (m_handle > 0) {
|
|
disconnect();
|
|
}
|
|
|
|
QByteArray addrUtf8 = addr.toUtf8();
|
|
QByteArray userUtf8 = user.toUtf8();
|
|
QByteArray anameUtf8 = aname.toUtf8();
|
|
|
|
m_handle = ollie9p_connect(addrUtf8.data(), userUtf8.data(), anameUtf8.data());
|
|
if (m_handle < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool Lib9pClient::connectDefault()
|
|
{
|
|
if (m_handle > 0) {
|
|
disconnect();
|
|
}
|
|
|
|
m_handle = ollie9p_connect_default();
|
|
if (m_handle < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
void Lib9pClient::disconnect()
|
|
{
|
|
if (m_handle > 0) {
|
|
ollie9p_close(m_handle);
|
|
m_handle = -1;
|
|
}
|
|
}
|
|
|
|
QByteArray Lib9pClient::read(const QString &path)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return {};
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
int len = 0;
|
|
char *data = ollie9p_read(m_handle, pathUtf8.data(), &len);
|
|
if (!data) {
|
|
updateLastError();
|
|
return {};
|
|
}
|
|
|
|
QByteArray result(data, len);
|
|
ollie9p_free(data);
|
|
m_lastError.clear();
|
|
return result;
|
|
}
|
|
|
|
bool Lib9pClient::write(const QString &path, const QByteArray &data)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return false;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
int written = ollie9p_write(m_handle, pathUtf8.data(),
|
|
const_cast<char*>(data.data()), data.size());
|
|
if (written < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
QByteArray Lib9pClient::rdwr(const QString &path, const QByteArray &data)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return {};
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
int outLen = 0;
|
|
char *resp = ollie9p_rdwr(m_handle, pathUtf8.data(),
|
|
const_cast<char*>(data.data()), data.size(), &outLen);
|
|
if (!resp) {
|
|
updateLastError();
|
|
return {};
|
|
}
|
|
|
|
QByteArray result(resp, outLen);
|
|
ollie9p_free(resp);
|
|
m_lastError.clear();
|
|
return result;
|
|
}
|
|
|
|
QStringList Lib9pClient::ls(const QString &path)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return {};
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
char *names = ollie9p_ls(m_handle, pathUtf8.data());
|
|
if (!names) {
|
|
updateLastError();
|
|
return {};
|
|
}
|
|
|
|
QString str = QString::fromUtf8(names);
|
|
ollie9p_free(names);
|
|
m_lastError.clear();
|
|
|
|
if (str.isEmpty()) {
|
|
return {};
|
|
}
|
|
return str.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
|
}
|
|
|
|
Lib9pClient::StatResult Lib9pClient::stat(const QString &path)
|
|
{
|
|
StatResult result;
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return result;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
char *data = ollie9p_stat(m_handle, pathUtf8.data());
|
|
if (!data) {
|
|
updateLastError();
|
|
return result;
|
|
}
|
|
|
|
// Parse "mode\tsize\tmtime\tname"
|
|
QString str = QString::fromUtf8(data);
|
|
ollie9p_free(data);
|
|
|
|
QStringList parts = str.split(QLatin1Char('\t'));
|
|
if (parts.size() >= 4) {
|
|
result.mode = parts[0];
|
|
result.size = parts[1].toLongLong();
|
|
result.mtime = parts[2].toLongLong();
|
|
result.name = parts[3];
|
|
result.valid = true;
|
|
}
|
|
m_lastError.clear();
|
|
return result;
|
|
}
|
|
|
|
bool Lib9pClient::remove(const QString &path)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return false;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
if (ollie9p_remove(m_handle, pathUtf8.data()) < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool Lib9pClient::create(const QString &path, uint perm)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return false;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
if (ollie9p_create(m_handle, pathUtf8.data(), perm) < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool Lib9pClient::mkdir(const QString &path)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return false;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
if (ollie9p_mkdir(m_handle, pathUtf8.data()) < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
bool Lib9pClient::rename(const QString &oldPath, const QString &newName)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return false;
|
|
}
|
|
|
|
QByteArray oldPathUtf8 = oldPath.toUtf8();
|
|
QByteArray newNameUtf8 = newName.toUtf8();
|
|
if (ollie9p_rename(m_handle, oldPathUtf8.data(), newNameUtf8.data()) < 0) {
|
|
updateLastError();
|
|
return false;
|
|
}
|
|
m_lastError.clear();
|
|
return true;
|
|
}
|
|
|
|
int Lib9pClient::open(const QString &path)
|
|
{
|
|
if (m_handle <= 0) {
|
|
m_lastError = QStringLiteral("not connected");
|
|
return -1;
|
|
}
|
|
|
|
QByteArray pathUtf8 = path.toUtf8();
|
|
int fid = ollie9p_open(m_handle, pathUtf8.data());
|
|
if (fid < 0) {
|
|
updateLastError();
|
|
return -1;
|
|
}
|
|
m_lastError.clear();
|
|
return fid;
|
|
}
|
|
|
|
int Lib9pClient::readFid(int fid, char *buf, int bufLen)
|
|
{
|
|
int n = ollie9p_read_fid(fid, buf, bufLen);
|
|
if (n < 0) {
|
|
updateLastError();
|
|
return -1;
|
|
}
|
|
m_lastError.clear();
|
|
return n;
|
|
}
|
|
|
|
void Lib9pClient::closeFid(int fid)
|
|
{
|
|
ollie9p_close_fid(fid);
|
|
}
|