File indexing completed on 2024-04-28 09:41:43

0001 // SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "bupverificationjob.h"
0006 
0007 #include <QThread>
0008 
0009 #include <KLocalizedString>
0010 
0011 BupVerificationJob::BupVerificationJob(BackupPlan &pBackupPlan, const QString &pDestinationPath,
0012                                        const QString &pLogFilePath, KupDaemon *pKupDaemon)
0013    : BackupJob(pBackupPlan, pDestinationPath, pLogFilePath, pKupDaemon){
0014     mFsckProcess.setOutputChannelMode(KProcess::SeparateChannels);
0015 }
0016 
0017 void BupVerificationJob::performJob() {
0018     KProcess lVersionProcess;
0019     lVersionProcess.setOutputChannelMode(KProcess::SeparateChannels);
0020     lVersionProcess << QStringLiteral("bup") << QStringLiteral("version");
0021     if(lVersionProcess.execute() < 0) {
0022         jobFinishedError(ErrorWithoutLog, xi18nc("@info notification",
0023                                                  "The <application>bup</application> program is needed but could not be found, "
0024                                                  "maybe it is not installed?"));
0025         return;
0026     }
0027 
0028     mLogStream << QStringLiteral("Kup is starting bup verification job at ")
0029                << QLocale().toString(QDateTime::currentDateTime())
0030                << Qt::endl << Qt::endl;
0031 
0032     mFsckProcess << QStringLiteral("bup");
0033     mFsckProcess << QStringLiteral("-d") << mDestinationPath;
0034     mFsckProcess << QStringLiteral("fsck") << QStringLiteral("--quick");
0035     mFsckProcess << QStringLiteral("-j") << QString::number(qMin(4, QThread::idealThreadCount()));
0036 
0037     connect(&mFsckProcess, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(slotCheckingDone(int,QProcess::ExitStatus)));
0038     connect(&mFsckProcess, SIGNAL(started()), SLOT(slotCheckingStarted()));
0039     mLogStream << mFsckProcess.program().join(QStringLiteral(" ")) << Qt::endl;
0040     mFsckProcess.start();
0041 }
0042 
0043 void BupVerificationJob::slotCheckingStarted() {
0044     makeNice(mFsckProcess.processId());
0045 }
0046 
0047 void BupVerificationJob::slotCheckingDone(int pExitCode, QProcess::ExitStatus pExitStatus) {
0048     QString lErrors = QString::fromUtf8(mFsckProcess.readAllStandardError());
0049     if(!lErrors.isEmpty()) {
0050         mLogStream << lErrors << Qt::endl;
0051     }
0052     mLogStream << "Exit code: " << pExitCode << Qt::endl;
0053     if(pExitStatus != QProcess::NormalExit) {
0054         mLogStream << QStringLiteral("Integrity check failed (the process crashed). Your backups could be "
0055                                      "corrupted! See above for details.") << Qt::endl;
0056         if(mBackupPlan.mGenerateRecoveryInfo) {
0057             jobFinishedError(ErrorSuggestRepair, xi18nc("@info notification",
0058                                                 "Failed backup integrity check. Your backups could be corrupted! "
0059                                                 "See log file for more details. Do you want to try repairing the backup files?"));
0060         } else {
0061             jobFinishedError(ErrorWithLog, xi18nc("@info notification", "Failed backup integrity check. Your backups are corrupted! "
0062                                                                         "See log file for more details."));
0063         }
0064     } else if(pExitCode == 0) {
0065         mLogStream << QStringLiteral("Backup integrity test was successful. "
0066                                      "Your backups are fine. See above for details.") << Qt::endl;
0067         jobFinishedError(ErrorWithLog, xi18nc("@info notification", "Backup integrity test was successful. "
0068                                                                     "Your backups are fine."));
0069     } else {
0070         mLogStream << QStringLiteral("Integrity check failed. Your backups are "
0071                                      "corrupted! See above for details.") << Qt::endl;
0072         if(mBackupPlan.mGenerateRecoveryInfo) {
0073             jobFinishedError(ErrorSuggestRepair, xi18nc("@info notification",
0074                                                         "Failed backup integrity check. Your backups are corrupted! "
0075                                                         "See log file for more details. Do you want to try repairing the backup files?"));
0076 
0077         } else {
0078             jobFinishedError(ErrorWithLog, xi18nc("@info notification", "Failed backup integrity check. Your backups are corrupted! "
0079                                                                         "See log file for more details."));
0080         }
0081     }
0082 }