Commit e6f5e2d2 authored by Albert Astals Cid's avatar Albert Astals Cid
Browse files

Write into crontab instead of replacing the file

Keeps permissions, owners, etc.
parent 3d66778a
Pipeline #143261 passed with stage
in 48 seconds
......@@ -31,29 +31,38 @@
ActionReply KcronHelper::save(const QVariantMap &args)
{
qCDebug(KCM_CRON_HELPER_LOG) << "running actions";
const QString source = args[QLatin1String("source")].toString();
const QString destination = QStringLiteral("/etc/crontab");
QByteArray newCronData;
{
QFile destinationFile(destination);
if (destinationFile.exists() && !destinationFile.remove()) {
const QString source = args[QLatin1String("source")].toString();
QFile sourceFile(source);
if (!sourceFile.open(QIODevice::ReadOnly)) {
qCWarning(KCM_CRON_HELPER_LOG) << "can't open source file for reading" << source << sourceFile.errorString();
ActionReply reply = ActionReply::HelperErrorReply();
qCWarning(KCM_CRON_HELPER_LOG) << "can't remove file" << destinationFile.errorString();
reply.setErrorDescription(destinationFile.errorString());
reply.setErrorDescription(sourceFile.errorString());
return reply;
}
newCronData = sourceFile.readAll();
}
{
QFile sourceFile(source);
if (!sourceFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther)) {
qCWarning(KCM_CRON_HELPER_LOG) << "can't change permissions to 644";
}
if (!sourceFile.copy(destination)) {
qCWarning(KCM_CRON_HELPER_LOG) << "can't write into the system file" << sourceFile.errorString();
const QString destination = QStringLiteral("/etc/crontab");
QFile destinationFile(destination);
if (!destinationFile.open(QIODevice::WriteOnly)) {
ActionReply reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(sourceFile.errorString());
qCWarning(KCM_CRON_HELPER_LOG) << "can't open destination file for writing" << destinationFile.errorString();
reply.setErrorDescription(destinationFile.errorString());
return reply;
}
if (destinationFile.write(newCronData) < 0) {
ActionReply reply = ActionReply::HelperErrorReply();
qCWarning(KCM_CRON_HELPER_LOG) << "writing to destination file failed" << destinationFile.errorString();
reply.setErrorDescription(destinationFile.errorString());
}
}
return ActionReply::SuccessReply();
}
......
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