Use `pathForOpen()` in backup helper where it was missing

This commit is contained in:
Martchus 2021-08-14 13:51:43 +02:00
parent c1ecc03617
commit 89e0cd0bff
1 changed files with 7 additions and 5 deletions

View File

@ -57,18 +57,20 @@ void restoreOriginalFileFromBackupFile(
originalStream.close(); originalStream.close();
} }
// check whether backup file actually exists and close the backup stream afterwards // check whether backup file actually exists and close the backup stream afterwards
const auto backupPathForOpen = BasicFileInfo::pathForOpen(backupPath);
backupStream.exceptions(ios_base::goodbit); backupStream.exceptions(ios_base::goodbit);
backupStream.close(); backupStream.close();
backupStream.clear(); backupStream.clear();
backupStream.open(backupPath, ios_base::in | ios_base::binary); backupStream.open(backupPathForOpen.data(), ios_base::in | ios_base::binary);
if (backupStream.is_open()) { if (backupStream.is_open()) {
backupStream.close(); backupStream.close();
} else { } else {
throw std::ios_base::failure("Backup/temporary file has not been created."); throw std::ios_base::failure("Backup/temporary file has not been created.");
} }
// remove original file and restore backup // remove original file and restore backup
std::remove(originalPath.c_str()); const auto originalPathForOpen = BasicFileInfo::pathForOpen(originalPath);
if (std::rename(BasicFileInfo::pathForOpen(backupPath).data(), BasicFileInfo::pathForOpen(originalPath).data()) == 0) { std::remove(originalPathForOpen.data());
if (std::rename(backupPathForOpen.data(), originalPathForOpen.data()) == 0) {
return; return;
} }
// can't rename/move the file (maybe backup dir on another partition) -> make a copy instead // can't rename/move the file (maybe backup dir on another partition) -> make a copy instead
@ -76,8 +78,8 @@ void restoreOriginalFileFromBackupFile(
// need to open all streams again // need to open all streams again
backupStream.exceptions(ios_base::failbit | ios_base::badbit); backupStream.exceptions(ios_base::failbit | ios_base::badbit);
originalStream.exceptions(ios_base::failbit | ios_base::badbit); originalStream.exceptions(ios_base::failbit | ios_base::badbit);
backupStream.open(backupPath, ios_base::in | ios_base::binary); backupStream.open(backupPathForOpen.data(), ios_base::in | ios_base::binary);
originalStream.open(originalPath, ios_base::out | ios_base::binary); originalStream.open(originalPathForOpen.data(), ios_base::out | ios_base::binary);
originalStream << backupStream.rdbuf(); originalStream << backupStream.rdbuf();
originalStream.flush(); originalStream.flush();
// TODO: callback for progress updates // TODO: callback for progress updates