Compare commits
No commits in common. "1c87b494efd485f49cd8b343e1162762b6adf8ba" and "4ef17463ff360403b25fd6abd216ac2a188589b3" have entirely different histories.
1c87b494ef
...
4ef17463ff
|
@ -44,11 +44,11 @@
|
|||
ExternalCommandHelper::ExternalCommandHelper()
|
||||
{
|
||||
if (!QDBusConnection::systemBus().registerObject(QStringLiteral("/Helper"), this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)) {
|
||||
exit(-1);
|
||||
::exit(-1);
|
||||
}
|
||||
|
||||
if (!QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.helperinterface"))) {
|
||||
exit(-1);
|
||||
::exit(-1);
|
||||
}
|
||||
|
||||
// we know this service must be registered already as DBus policy blocks calls from anyone else
|
||||
|
@ -71,25 +71,25 @@ ExternalCommandHelper::ExternalCommandHelper()
|
|||
@param size the number of bytes to read
|
||||
@return true on success
|
||||
*/
|
||||
bool ExternalCommandHelper::readData(QFile& device, QByteArray& buffer, const qint64 offset, const qint64 size)
|
||||
bool ExternalCommandHelper::readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size)
|
||||
{
|
||||
if (!device.isOpen()) {
|
||||
if (!device.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
|
||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for reading.", device.fileName());
|
||||
return false;
|
||||
}
|
||||
QFile device(sourceDevice);
|
||||
|
||||
if (!device.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
|
||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for reading.", sourceDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sequential devices such as /dev/zero or /dev/urandom return false on seek().
|
||||
if (!device.isSequential() && !device.seek(offset)) {
|
||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, device.fileName());
|
||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, sourceDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer = device.read(size);
|
||||
|
||||
if (size != buffer.size()) {
|
||||
qCritical() << xi18n("Could not read from device <filename>%1</filename>.", device.fileName());
|
||||
qCritical() << xi18n("Could not read from device <filename>%1</filename>.", sourceDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -97,28 +97,28 @@ bool ExternalCommandHelper::readData(QFile& device, QByteArray& buffer, const qi
|
|||
}
|
||||
|
||||
/** Writes the data from buffer to a given device.
|
||||
@param device device or file to write to
|
||||
@param targetDevice device or file to write to
|
||||
@param buffer the data that we write
|
||||
@param offset offset where to begin writing
|
||||
@return true on success
|
||||
*/
|
||||
bool ExternalCommandHelper::writeData(QFile& device, const QByteArray& buffer, const qint64 offset)
|
||||
bool ExternalCommandHelper::writeData(const QString &targetDevice, const QByteArray& buffer, const qint64 offset)
|
||||
{
|
||||
QFile device(targetDevice);
|
||||
|
||||
auto flags = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
||||
if (!device.isOpen()) {
|
||||
if (!device.open(flags)) {
|
||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for writing.", device.fileName());
|
||||
return false;
|
||||
}
|
||||
if (!device.open(flags)) {
|
||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for writing.", targetDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!device.seek(offset)) {
|
||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, device.fileName());
|
||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, targetDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (device.write(buffer) != buffer.size()) {
|
||||
qCritical() << xi18n("Could not write to device <filename>%1</filename>.", device.fileName());
|
||||
qCritical() << xi18n("Could not write to device <filename>%1</filename>.", targetDevice);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -233,13 +233,11 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
|||
|
||||
bool rval = true;
|
||||
|
||||
QFile target(targetDevice);
|
||||
QFile source(sourceDevice);
|
||||
while (blocksCopied < blocksToCopy) {
|
||||
if (!(rval = readData(source, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
||||
if (!(rval = readData(sourceDevice, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
||||
break;
|
||||
|
||||
if (!(rval = writeData(target, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
||||
if (!(rval = writeData(targetDevice, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
||||
break;
|
||||
|
||||
bytesWritten += buffer.size();
|
||||
|
@ -265,10 +263,10 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
|||
const qint64 lastBlockWriteOffset = copyDirection == CopyDirection::Left ? writeOffset + blockSize * blocksCopied : targetOffset;
|
||||
reportText = xi18nc("@info:progress", "Copying remainder of block size %1 from %2 to %3.", lastBlock, lastBlockReadOffset, lastBlockWriteOffset);
|
||||
Q_EMIT report(reportText);
|
||||
rval = readData(source, buffer, lastBlockReadOffset, lastBlock);
|
||||
rval = readData(sourceDevice, buffer, lastBlockReadOffset, lastBlock);
|
||||
|
||||
if (rval) {
|
||||
rval = writeData(target, buffer, lastBlockWriteOffset);
|
||||
rval = writeData(targetDevice, buffer, lastBlockWriteOffset);
|
||||
}
|
||||
|
||||
if (rval) {
|
||||
|
@ -299,8 +297,7 @@ QByteArray ExternalCommandHelper::ReadData(const QString& device, const qint64 o
|
|||
}
|
||||
|
||||
QByteArray buffer;
|
||||
QFile sourceDevice(device);
|
||||
bool rval = readData(sourceDevice, buffer, offset, length);
|
||||
bool rval = readData(device, buffer, offset, length);
|
||||
if (rval) {
|
||||
return buffer;
|
||||
}
|
||||
|
@ -323,9 +320,8 @@ bool ExternalCommandHelper::WriteData(const QByteArray& buffer, const QString& t
|
|||
}
|
||||
|
||||
auto canonicalTargetPath = std::filesystem::canonical(targetPath);
|
||||
// TODO: Qt6 supports std::filesystem::path
|
||||
QFile device(QLatin1String(canonicalTargetPath.c_str()));
|
||||
return writeData(device, buffer, targetOffset);
|
||||
|
||||
return writeData(QLatin1String(canonicalTargetPath.c_str()), buffer, targetOffset);
|
||||
}
|
||||
|
||||
QVariantMap ExternalCommandHelper::RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode)
|
||||
|
|
|
@ -14,11 +14,10 @@
|
|||
#include <memory>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <QDBusContext>
|
||||
#include <QEventLoop>
|
||||
#include <QFile>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QProcess>
|
||||
#include <QDBusContext>
|
||||
|
||||
class QDBusServiceWatcher;
|
||||
constexpr qint64 MiB = 1 << 30;
|
||||
|
@ -34,8 +33,8 @@ Q_SIGNALS:
|
|||
|
||||
public:
|
||||
ExternalCommandHelper();
|
||||
bool readData(QFile& device, QByteArray& buffer, const qint64 offset, const qint64 size);
|
||||
bool writeData(QFile& device, const QByteArray& buffer, const qint64 offset);
|
||||
bool readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size);
|
||||
bool writeData(const QString& targetDevice, const QByteArray& buffer, const qint64 offset);
|
||||
|
||||
public Q_SLOTS:
|
||||
Q_SCRIPTABLE QVariantMap RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode);
|
||||
|
|
Loading…
Reference in New Issue