From 908d12a061d8b4cdb768fe138b12fd81faf0460b Mon Sep 17 00:00:00 2001 From: Don Ho Date: Sat, 7 Jun 2014 13:04:09 +0000 Subject: [PATCH] [BUG_FIXED] Fix a saving issue in session snapshot & periodic backup feature while backup file is deleted or read-only. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1247 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/NppIO.cpp | 47 ++++++++++++++++++-- PowerEditor/src/ScitillaComponent/Buffer.cpp | 9 +++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/PowerEditor/src/NppIO.cpp b/PowerEditor/src/NppIO.cpp index 06514164b..66e6ad925 100644 --- a/PowerEditor/src/NppIO.cpp +++ b/PowerEditor/src/NppIO.cpp @@ -619,7 +619,28 @@ bool Notepad_plus::fileCloseAll(bool doDeleteBackup, bool isSnapshotMode) { if (isSnapshotMode) { - + if (buf->getBackupFileName() == TEXT("") || !::PathFileExists(buf->getBackupFileName().c_str())) //backup file has been deleted from outside + { + // warning user and save it if user want it. + activateBuffer(id, MAIN_VIEW); + if(!activateBuffer(id, SUB_VIEW)) + switchEditViewTo(MAIN_VIEW); + + TCHAR pattern[140] = TEXT("Your backup file cannot be found (deleted from outside).\rSave it otherwise your data will be lost\rDo you wan to save file \"%s\" ?"); + TCHAR phrase[512]; + wsprintf(phrase, pattern, buf->getFullPathName()); + int res = doActionOrNot(TEXT("Save"), phrase, MB_YESNOCANCEL | MB_ICONQUESTION | MB_APPLMODAL); + //int res = doSaveOrNot(buf->getFullPathName()); + if (res == IDYES) + { + if (!fileSave(id)) + return false; //abort entire procedure + } + else if (res == IDCANCEL) + { + return false; + } + } } else { @@ -635,7 +656,7 @@ bool Notepad_plus::fileCloseAll(bool doDeleteBackup, bool isSnapshotMode) } else if (res == IDCANCEL) { - return false; + return false; } } } @@ -652,7 +673,27 @@ bool Notepad_plus::fileCloseAll(bool doDeleteBackup, bool isSnapshotMode) { if (isSnapshotMode) { - + if (buf->getBackupFileName() == TEXT("") || !::PathFileExists(buf->getBackupFileName().c_str())) //backup file has been deleted from outside + { + // warning user and save it if user want it. + activateBuffer(id, SUB_VIEW); + switchEditViewTo(SUB_VIEW); + + TCHAR pattern[140] = TEXT("Your backup file cannot be found (deleted from outside).\rSave it otherwise your data will be lost\rDo you wan to save file \"%s\" ?"); + TCHAR phrase[512]; + wsprintf(phrase, pattern, buf->getFullPathName()); + int res = doActionOrNot(TEXT("Save"), phrase, MB_YESNOCANCEL | MB_ICONQUESTION | MB_APPLMODAL); + //int res = doSaveOrNot(buf->getFullPathName()); + if (res == IDYES) + { + if (!fileSave(id)) + return false; //abort entire procedure + } + else if (res == IDCANCEL) + { + return false; + } + } } else { diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index 6217c86e3..f37ca3a12 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -742,11 +742,18 @@ bool FileManager::backupCurrentBuffer() // Session changes, save it hasModifForSession = true; } - TCHAR fullpath[MAX_PATH]; ::GetFullPathName(backupFilePath.c_str(), MAX_PATH, fullpath, NULL); ::GetLongPathName(fullpath, fullpath, MAX_PATH); + + // Make sure the backup file is not read only + DWORD dwFileAttribs = ::GetFileAttributes(fullpath); + if (dwFileAttribs & FILE_ATTRIBUTE_READONLY) // if file is read only, remove read only attribute + { + dwFileAttribs ^= FILE_ATTRIBUTE_READONLY; + ::SetFileAttributes(fullpath, dwFileAttribs); + } FILE *fp = UnicodeConvertor.fopen(fullpath, TEXT("wb")); if (fp)