File: rsyncjob.cpp

package info (click to toggle)
kup-backup 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 8,422; xml: 311; makefile: 6; sh: 3
file content (215 lines) | stat: -rw-r--r-- 9,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
//
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "rsyncjob.h"
#include "kuputils.h"

#include <csignal>

#include <QDir>
#include <QRegularExpression>
#include <QTextStream>

#include <KLocalizedString>

RsyncJob::RsyncJob(BackupPlan &pBackupPlan, const QString &pDestinationPath, const QString &pLogFilePath, KupDaemon *pKupDaemon)
    : BackupJob(pBackupPlan, pDestinationPath, pLogFilePath, pKupDaemon)
{
    mRsyncProcess.setOutputChannelMode(KProcess::SeparateChannels);
    setCapabilities(KJob::Suspendable | KJob::Killable);
}

void RsyncJob::performJob()
{
    KProcess lVersionProcess;
    lVersionProcess.setOutputChannelMode(KProcess::SeparateChannels);
    lVersionProcess << QStringLiteral("rsync") << QStringLiteral("--version");
    if (lVersionProcess.execute() < 0) {
        jobFinishedError(ErrorWithoutLog,
                         xi18nc("@info notification",
                                "The <application>rsync</application> program is needed but "
                                "could not be found, maybe it is not installed?"));
        return;
    }

    // Remove this and the performMigration method when it is likely that all users of pre 0.8 kup have now started using post 0.8.
    if (mBackupPlan.mBackupVersion < 1 && mBackupPlan.mLastCompleteBackup.isValid() && mBackupPlan.mPathsIncluded.length() == 1) {
        mLogStream << QStringLiteral("Migrating saved files to new location, after update to version 0.8 of Kup.") << Qt::endl;
        if (!performMigration()) {
            mLogStream << QStringLiteral("Migration failed. Continuing backup save regardless, may result in files stored twice.") << Qt::endl;
        }
    }
    mBackupPlan.mBackupVersion = 1;
    mBackupPlan.save();

    mLogStream << QStringLiteral("Kup is starting rsync backup job at ") << QLocale().toString(QDateTime::currentDateTime()) << Qt::endl;

    emit description(this, i18n("Checking what to copy"));
    mRsyncProcess << QStringLiteral("rsync") << QStringLiteral("-avX") << QStringLiteral("--delete-excluded") << QStringLiteral("--delete-before")
                  << QStringLiteral("--info=progress2");

    QStringList lIncludeNames;
    foreach (const QString &lInclude, mBackupPlan.mPathsIncluded) {
        lIncludeNames << lastPartOfPath(lInclude);
    }
    if (lIncludeNames.removeDuplicates() > 0) {
        // There would be a naming conflict in the destination folder, instead use full paths.
        mRsyncProcess << QStringLiteral("-R");
        foreach (const QString &lExclude, mBackupPlan.mPathsExcluded) {
            mRsyncProcess << QStringLiteral("--exclude") << lExclude;
        }
    } else {
        // when NOT using -R, need to then strip parent paths from excludes, everything above the
        // include. Leave the leading slash!
        foreach (QString lExclude, mBackupPlan.mPathsExcluded) {
            for (int i = 0; i < mBackupPlan.mPathsIncluded.length(); ++i) {
                const QString &lInclude = mBackupPlan.mPathsIncluded.at(i);
                QString lIncludeWithSlash = lInclude;
                ensureTrailingSlash(lIncludeWithSlash);
                if (lExclude.startsWith(lIncludeWithSlash)) {
                    lExclude.remove(0, lInclude.length() - lIncludeNames.at(i).length() - 1);
                    break;
                }
            }
            mRsyncProcess << QStringLiteral("--exclude") << lExclude;
        }
    }
    QString lExcludesPath = mBackupPlan.absoluteExcludesFilePath();
    if (mBackupPlan.mExcludePatterns && QFileInfo::exists(lExcludesPath)) {
        mRsyncProcess << QStringLiteral("--exclude-from") << lExcludesPath;
    }
    mRsyncProcess << mBackupPlan.mPathsIncluded;
    mRsyncProcess << mDestinationPath;

    connect(&mRsyncProcess, &KProcess::started, this, &RsyncJob::slotRsyncStarted);
    connect(&mRsyncProcess, &KProcess::readyReadStandardOutput, this, &RsyncJob::slotReadRsyncOutput);
    connect(&mRsyncProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &RsyncJob::slotRsyncFinished);
    mLogStream << quoteArgs(mRsyncProcess.program()) << Qt::endl;
    mRsyncProcess.start();
    mInfoRateLimiter.start();
}

void RsyncJob::slotRsyncStarted()
{
    makeNice(mRsyncProcess.processId());
}

void RsyncJob::slotRsyncFinished(int pExitCode, QProcess::ExitStatus pExitStatus)
{
    QString lErrors = QString::fromUtf8(mRsyncProcess.readAllStandardError());
    if (!lErrors.isEmpty()) {
        mLogStream << lErrors << Qt::endl;
    }
    mLogStream << "Exit code: " << pExitCode << Qt::endl;
    // exit code 24 means source files disappeared during copying. No reason to worry about that.
    if (pExitStatus != QProcess::NormalExit || (pExitCode != 0 && pExitCode != 24)) {
        mLogStream << QStringLiteral("Kup did not successfully complete the rsync backup job.") << Qt::endl;
        jobFinishedError(ErrorWithLog,
                         xi18nc("@info notification",
                                "Failed to save backup. "
                                "See log file for more details."));
    } else {
        mLogStream << QStringLiteral("Kup successfully completed the rsync backup job at ") << QLocale().toString(QDateTime::currentDateTime()) << Qt::endl;
        jobFinishedSuccess();
    }
}

void RsyncJob::slotReadRsyncOutput()
{
    bool lValidInfo = false;
    bool lValidFileName = false;
    QString lFileName;
    ulong lPercent{};
    qulonglong lTransfered{};
    double lSpeed{};
    QChar lUnit;
    static QRegularExpression lProgressInfoExp(QStringLiteral("^\\s+([\\d,\\.]+)\\s+(\\d+)%\\s+(\\d*[,\\.]\\d+)(\\S)"));
    // very ugly and rough indication that this is a file path... what else to do..
    static QRegularExpression lNotFileNameExp(QStringLiteral("^(building file list|done$|deleting \\S+|.+/$|$)"));
    QString lLine;

    QTextStream lStream(mRsyncProcess.readAllStandardOutput());
    while (lStream.readLineInto(&lLine, 500)) {
        QRegularExpressionMatch lMatch = lProgressInfoExp.match(lLine);
        if (lMatch.hasMatch()) {
            lValidInfo = true;
            lTransfered = lMatch.captured(1).remove(',').remove('.').toULongLong();
            lPercent = qMax(lMatch.captured(2).toULong(), 1UL);
            lSpeed = QLocale().toDouble(lMatch.captured(3));
            lUnit = lMatch.captured(4).at(0);
        } else {
            lMatch = lNotFileNameExp.match(lLine);
            if (!lMatch.hasMatch()) {
                lValidFileName = true;
                lFileName = lLine;
            }
        }
    }
    if (mInfoRateLimiter.hasExpired(200)) {
        if (lValidInfo) {
            setPercent(lPercent);
            if (lUnit == 'k') {
                lSpeed *= 1e3;
            } else if (lUnit == 'M') {
                lSpeed *= 1e6;
            } else if (lUnit == 'G') {
                lSpeed *= 1e9;
            }
            emitSpeed(static_cast<ulong>(lSpeed));
            if (lPercent > 5) { // the rounding to integer percent gives big error with small percentages
                setProcessedAmount(KJob::Bytes, lTransfered);
                setTotalAmount(KJob::Bytes, lTransfered * 100 / lPercent);
            }
        }
        if (lValidFileName) {
            emit description(this, i18n("Saving backup"), qMakePair(i18nc("Label for file currently being copied", "File"), lFileName));
        }
        mInfoRateLimiter.start();
    }
}

bool RsyncJob::doKill()
{
    setError(KilledJobError);
    if (0 == ::kill(mRsyncProcess.processId(), SIGINT)) {
        return mRsyncProcess.waitForFinished();
    }
    return false;
}

bool RsyncJob::doSuspend()
{
    return 0 == ::kill(mRsyncProcess.processId(), SIGSTOP);
}

bool RsyncJob::doResume()
{
    return 0 == ::kill(mRsyncProcess.processId(), SIGCONT);
}

// This migration moves files from being stored directly in destination folder, to
// being stored in a subfolder of the destination. The subfolder is named same as the
// source folder. This migration will only be done if there is exactly one source folder.
bool RsyncJob::performMigration()
{
    QString lSourceDirName = lastPartOfPath(mBackupPlan.mPathsIncluded.first()); // only one included
    QDir lDestDir = QDir(mDestinationPath);
    mLogStream << QStringLiteral("Creating directory named ") << lSourceDirName << " inside of " << mDestinationPath << Qt::endl;
    if (!lDestDir.mkdir(lSourceDirName)) {
        mLogStream << QStringLiteral("Failed to create directory, aborting migration.") << Qt::endl;
        return false;
    }
    foreach (const QString &lContent, lDestDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
        if (lContent != lSourceDirName) {
            QString lDest = lSourceDirName + QLatin1Char('/') + lContent;
            mLogStream << QStringLiteral("Renaming ") << lContent << " to " << lDest << Qt::endl;
            if (!lDestDir.rename(lContent, lDest)) {
                mLogStream << QStringLiteral("Failed to rename, aborting migration.") << Qt::endl;
                return false;
            }
        }
    }
    mLogStream << QStringLiteral("File migration completed.") << Qt::endl;
    return true;
}