|
| 1 | +// Unit Includes |
| 2 | +#include "backup.h" |
| 3 | + |
| 4 | +// Qt Includes |
| 5 | +#include <QFile> |
| 6 | +#include <QFileInfo> |
| 7 | + |
| 8 | +namespace Import |
| 9 | +{ |
| 10 | + |
| 11 | +//=============================================================================================================== |
| 12 | +// BackupError |
| 13 | +//=============================================================================================================== |
| 14 | + |
| 15 | +//-Constructor------------------------------------------------------------- |
| 16 | +//Private: |
| 17 | +BackupError::BackupError(Type t, const QString& s) : |
| 18 | + mType(t), |
| 19 | + mSpecific(s) |
| 20 | +{} |
| 21 | + |
| 22 | +//Public: |
| 23 | +BackupError::BackupError() : |
| 24 | + mType(NoError) |
| 25 | +{} |
| 26 | + |
| 27 | +//-Instance Functions------------------------------------------------------------- |
| 28 | +//Public: |
| 29 | +bool BackupError::isValid() const { return mType != NoError; } |
| 30 | +QString BackupError::specific() const { return mSpecific; } |
| 31 | +BackupError::Type BackupError::type() const { return mType; } |
| 32 | + |
| 33 | +//Private: |
| 34 | +Qx::Severity BackupError::deriveSeverity() const { return Qx::Err; } |
| 35 | +quint32 BackupError::deriveValue() const { return mType; } |
| 36 | +QString BackupError::derivePrimary() const { return ERR_STRINGS.value(mType); } |
| 37 | +QString BackupError::deriveSecondary() const { return mSpecific; } |
| 38 | +QString BackupError::deriveCaption() const { return CAPTION_REVERT_ERR; } |
| 39 | + |
| 40 | +//=============================================================================================================== |
| 41 | +// BackupManager |
| 42 | +//=============================================================================================================== |
| 43 | + |
| 44 | +//-Constructor------------------------------------------------------------- |
| 45 | +//Private: |
| 46 | +BackupManager::BackupManager() {} |
| 47 | + |
| 48 | +//-Class Functions------------------------------------------------------------- |
| 49 | +//Private: |
| 50 | +QString BackupManager::filePathToBackupPath(const QString& filePath) |
| 51 | +{ |
| 52 | + return filePath + '.' + BACKUP_FILE_EXT; |
| 53 | +} |
| 54 | + |
| 55 | +//Public: |
| 56 | +BackupManager* BackupManager::instance() { static BackupManager inst; return &inst; } |
| 57 | + |
| 58 | +//-Instance Functions------------------------------------------------------------- |
| 59 | +//Private: |
| 60 | +BackupError BackupManager::backup(const QString& path, bool (*fn)(const QString& a, const QString& b)) |
| 61 | +{ |
| 62 | + // Prevent double+ backups |
| 63 | + if(mRevertablePaths.contains(path)) |
| 64 | + return BackupError(); |
| 65 | + |
| 66 | + // Note revertable |
| 67 | + mRevertablePaths.insert(path); |
| 68 | + |
| 69 | + // Backup if exists |
| 70 | + if(QFile::exists(path)) |
| 71 | + { |
| 72 | + QString backupPath = filePathToBackupPath(path); |
| 73 | + |
| 74 | + if(QFile::exists(backupPath) && QFileInfo(backupPath).isFile()) |
| 75 | + { |
| 76 | + if(!QFile::remove(backupPath)) |
| 77 | + return BackupError(BackupError::FileWontDelete, backupPath); |
| 78 | + } |
| 79 | + |
| 80 | + if(!fn(path, backupPath)) |
| 81 | + return BackupError(BackupError::FileWontBackup, path); |
| 82 | + } |
| 83 | + |
| 84 | + return BackupError(); |
| 85 | +} |
| 86 | + |
| 87 | +BackupError BackupManager::restore(QSet<QString>::const_iterator pathItr) |
| 88 | +{ |
| 89 | + Q_ASSERT(pathItr != mRevertablePaths.cend()); |
| 90 | + |
| 91 | + const QString path = *pathItr; |
| 92 | + mRevertablePaths.erase(pathItr); |
| 93 | + QString backupPath = filePathToBackupPath(path); |
| 94 | + |
| 95 | + if(QFile::exists(path) && !QFile::remove(path)) |
| 96 | + return BackupError(BackupError::FileWontDelete, path); |
| 97 | + |
| 98 | + if(!QFile::exists(path) && QFile::exists(backupPath) && !QFile::rename(backupPath, path)) |
| 99 | + return BackupError(BackupError::FileWontRestore, backupPath); |
| 100 | + |
| 101 | + return BackupError(); |
| 102 | +} |
| 103 | + |
| 104 | +//Public: |
| 105 | +BackupError BackupManager::backupCopy(const QString& path) |
| 106 | +{ |
| 107 | + return backup(path, [](const QString& a, const QString& b){ return QFile::copy(a, b); }); |
| 108 | +} |
| 109 | + |
| 110 | +BackupError BackupManager::backupRename(const QString& path) |
| 111 | +{ |
| 112 | + return backup(path, [](const QString& a, const QString& b){ return QFile::rename(a, b); }); |
| 113 | +} |
| 114 | + |
| 115 | +BackupError BackupManager::restore(const QString& path) |
| 116 | +{ |
| 117 | + auto store = mRevertablePaths.constFind(path); |
| 118 | + if(store == mRevertablePaths.cend()) |
| 119 | + return BackupError(); |
| 120 | + |
| 121 | + return restore(store); |
| 122 | +} |
| 123 | + |
| 124 | +BackupError BackupManager::safeReplace(const QString& src, const QString& dst, bool symlink) |
| 125 | +{ |
| 126 | + // Maybe make sure destination folder exists here? |
| 127 | + |
| 128 | + // Backup |
| 129 | + QString backupPath = filePathToBackupPath(dst); |
| 130 | + bool dstOccupied = QFile::exists(dst); |
| 131 | + if(dstOccupied) |
| 132 | + if(!QFile::rename(dst, backupPath)) // Temp backup |
| 133 | + return BackupError(BackupError::FileWontBackup, dst); |
| 134 | + |
| 135 | + // Replace |
| 136 | + std::error_code replaceError; |
| 137 | + if(symlink) |
| 138 | + std::filesystem::create_symlink(src.toStdString(), dst.toStdString(), replaceError); |
| 139 | + else |
| 140 | + replaceError = QFile::copy(src, dst) ? std::error_code() : std::make_error_code(std::io_errc::stream); |
| 141 | + |
| 142 | + // Restore on fail |
| 143 | + if(replaceError) |
| 144 | + { |
| 145 | + if(dstOccupied) |
| 146 | + QFile::rename(backupPath, dst); |
| 147 | + return BackupError(BackupError::FileWontReplace, src); |
| 148 | + } |
| 149 | + |
| 150 | + // Remove backup immediately |
| 151 | + if(dstOccupied) |
| 152 | + QFile::remove(backupPath); |
| 153 | + else // Mark new files (only) as revertible so that existing ones will remain in the event of a revert |
| 154 | + mRevertablePaths.insert(dst); |
| 155 | + |
| 156 | + return BackupError(); |
| 157 | +} |
| 158 | + |
| 159 | +int BackupManager::revertQueueCount() const { return mRevertablePaths.size(); } |
| 160 | + |
| 161 | +int BackupManager::revertNextChange(BackupError& error, bool skipOnFail) |
| 162 | +{ |
| 163 | + // Ensure error message is null |
| 164 | + error = BackupError(); |
| 165 | + |
| 166 | + // Delete new files and restore backups if present |
| 167 | + if(!mRevertablePaths.isEmpty()) |
| 168 | + { |
| 169 | + BackupError rErr = restore(mRevertablePaths.cbegin()); |
| 170 | + if(rErr && !skipOnFail) |
| 171 | + error = rErr; |
| 172 | + |
| 173 | + return mRevertablePaths.size(); |
| 174 | + } |
| 175 | + |
| 176 | + // Return 0 if all empty (shouldn't be reached if function is used correctly) |
| 177 | + qWarning("Reversion function called with no reverts left!"); |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +} |
0 commit comments