7#include "katesecuretextbuffer_p.h"
11#include <KAuth/HelperSupport>
22#include <QTemporaryFile>
24KAUTH_HELPER_MAIN(
"org.kde.ktexteditor6.katetextbuffer", SecureTextBuffer)
26ActionReply SecureTextBuffer::savefile(
const QVariantMap &args)
28 const QString sourceFile = args[QStringLiteral(
"sourceFile")].toString();
29 const QString targetFile = args[QStringLiteral(
"targetFile")].toString();
30 const QByteArray checksum = args[QStringLiteral(
"checksum")].toByteArray();
31 const uint ownerId = (uint)args[QStringLiteral(
"ownerId")].
toInt();
32 const uint groupId = (uint)args[QStringLiteral(
"groupId")].toInt();
34 if (saveFileInternal(sourceFile, targetFile, checksum, ownerId, groupId)) {
35 return ActionReply::SuccessReply();
38 return ActionReply::HelperErrorReply();
41bool SecureTextBuffer::saveFileInternal(
const QString &sourceFile,
56 const QFileInfo targetFileInfo(targetFile);
62 if (!tempFile.open()) {
69 const qint64 bufferLength = 4096;
70 char buffer[bufferLength];
72 while ((read =
readFile.read(buffer, bufferLength)) > 0) {
74 if (tempFile.write(buffer, read) == -1) {
83 if ((read == -1) || (cryptographicHash.result() != checksum) || !tempFile.flush()) {
88 if (!targetFileInfo.exists()) {
90 tempFile.setPermissions(tempFile.permissions() | QFile::Permission::ReadGroup | QFile::Permission::ReadOther);
93 tempFile.setPermissions(targetFileInfo.permissions());
96 setOwner(tempFile.handle(), ownerId, groupId);
100 if (moveFile(tempFile.fileName(), targetFileInfo.filePath())) {
102 tempFile.setAutoRemove(
false);
111void SecureTextBuffer::setOwner(
const int filedes,
const uint ownerId,
const uint groupId)
114 if (ownerId != (uint)-2 && groupId != (uint)-2) {
115 int result = fchown(filedes, ownerId, groupId);
117 if (result != 0 && errno == EPERM) {
118 result = fchown(filedes, getuid(), groupId);
127bool SecureTextBuffer::moveFile(
const QString &sourceFile,
const QString &targetFile)
129#if !defined(Q_OS_WIN) && !defined(Q_OS_ANDROID)
131 return (result == 0);
QVariant read(const QByteArray &data, int versionOverride=0)
bool readFile(const QString &sourceUrl, QString &sourceCode)
read complete file contents, helper
int toInt(bool *ok, int base) const const
QByteArray encodeName(const QString &fileName)
bool rename(const QString &newName)