From 9c16ef83b2fadfe258d6de03322075d6d4acf423 Mon Sep 17 00:00:00 2001 From: Takashi Sawanaka Date: Sun, 1 Dec 2019 22:24:54 +0900 Subject: [PATCH] Fix issue #229: Program is crashed! --- Src/HexMergeDoc.cpp | 5 +++-- Src/HexMergeView.cpp | 11 ++++++----- Src/HexMergeView.h | 3 ++- Src/IMergeDoc.h | 7 +++++++ Src/ImgMergeFrm.cpp | 11 ++++++----- Src/ImgMergeFrm.h | 2 +- Src/MergeDoc.h | 7 ------- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Src/HexMergeDoc.cpp b/Src/HexMergeDoc.cpp index 8fd872a2875..3cbd531cce5 100644 --- a/Src/HexMergeDoc.cpp +++ b/Src/HexMergeDoc.cpp @@ -550,7 +550,7 @@ void CHexMergeDoc::CheckFileChanged(void) { for (int pane = 0; pane < m_nBuffers; ++pane) { - if (m_pView[pane]->IsFileChangedOnDisk(m_filePaths[pane].c_str())) + if (m_pView[pane]->IsFileChangedOnDisk(m_filePaths[pane].c_str()) == FileChanged) { String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge scanned it last time.\n\nDo you want to reload the file?"), m_filePaths[pane]); if (AfxMessageBox(msg.c_str(), MB_YESNO | MB_ICONWARNING) == IDYES) @@ -732,7 +732,8 @@ void CHexMergeDoc::OnFileReload() fileloc[pane].setPath(m_filePaths[pane]); bRO[pane] = m_pView[pane]->GetReadOnly(); } - OpenDocs(m_nBuffers, fileloc, bRO, m_strDesc); + if (!OpenDocs(m_nBuffers, fileloc, bRO, m_strDesc)) + return; MoveOnLoad(GetActiveMergeView()->m_nThisPane); } diff --git a/Src/HexMergeView.cpp b/Src/HexMergeView.cpp index 888b4cff273..23030ad469a 100644 --- a/Src/HexMergeView.cpp +++ b/Src/HexMergeView.cpp @@ -246,18 +246,19 @@ int CHexMergeView::GetLength() * @param [in] path File to check * @return `true` if file is changed. */ -bool CHexMergeView::IsFileChangedOnDisk(LPCTSTR path) +IMergeDoc::FileChange CHexMergeView::IsFileChangedOnDisk(LPCTSTR path) { DiffFileInfo dfi; - dfi.Update(path); + if (!dfi.Update(path)) + return IMergeDoc::FileRemoved; int tolerance = 0; if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME)) tolerance = SmallTimeDiff; // From MainFrm.h int64_t timeDiff = dfi.mtime - m_fileInfo.mtime; if (timeDiff < 0) timeDiff = -timeDiff; if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size)) - return true; - return false; + return IMergeDoc::FileChanged; + return IMergeDoc::FileNoChange; } /** @@ -298,7 +299,7 @@ HRESULT CHexMergeView::LoadFile(LPCTSTR path) HRESULT CHexMergeView::SaveFile(LPCTSTR path) { // Warn user in case file has been changed by someone else - if (IsFileChangedOnDisk(path)) + if (IsFileChangedOnDisk(path) == IMergeDoc::FileChanged) { String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path); if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO) diff --git a/Src/HexMergeView.h b/Src/HexMergeView.h index 527e198fda2..97e7e525508 100644 --- a/Src/HexMergeView.h +++ b/Src/HexMergeView.h @@ -25,6 +25,7 @@ #pragma once #include "DiffFileInfo.h" +#include "IMergeDoc.h" class CHexMergeDoc; class IHexEditorWindow; @@ -55,7 +56,7 @@ class CHexMergeView : public CView bool GetReadOnly(); void SetReadOnly(bool); void ResizeWindow(); - bool IsFileChangedOnDisk(LPCTSTR); + IMergeDoc::FileChange IsFileChangedOnDisk(LPCTSTR); void ZoomText(int amount); static void CopySel(const CHexMergeView *src, CHexMergeView *dst); static void CopyAll(const CHexMergeView *src, CHexMergeView *dst); diff --git a/Src/IMergeDoc.h b/Src/IMergeDoc.h index 11af8a1bd68..7c9aa7a7c0e 100644 --- a/Src/IMergeDoc.h +++ b/Src/IMergeDoc.h @@ -6,6 +6,13 @@ class CDirDoc; struct IMergeDoc { + enum FileChange + { + FileNoChange, + FileChanged, + FileRemoved, + }; + virtual void SetDirDoc(CDirDoc *pDirDoc) = 0; virtual bool CloseNow(void) = 0; virtual bool GenerateReport(const String &path) const = 0; diff --git a/Src/ImgMergeFrm.cpp b/Src/ImgMergeFrm.cpp index a889eff8c89..1df998ecb25 100644 --- a/Src/ImgMergeFrm.cpp +++ b/Src/ImgMergeFrm.cpp @@ -304,25 +304,26 @@ void CImgMergeFrame::SetDirDoc(CDirDoc * pDirDoc) m_pDirDoc = pDirDoc; } -bool CImgMergeFrame::IsFileChangedOnDisk(int pane) const +IMergeDoc::FileChange CImgMergeFrame::IsFileChangedOnDisk(int pane) const { DiffFileInfo dfi; - dfi.Update(m_filePaths[pane]); + if (!dfi.Update(m_filePaths[pane])) + return FileRemoved; int tolerance = 0; if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME)) tolerance = SmallTimeDiff; // From MainFrm.h int64_t timeDiff = dfi.mtime - m_fileInfo[pane].mtime; if (timeDiff < 0) timeDiff = -timeDiff; if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo[pane].size)) - return true; - return false; + return FileChanged; + return FileNoChange; } void CImgMergeFrame::CheckFileChanged(void) { for (int pane = 0; pane < m_pImgMergeWindow->GetPaneCount(); ++pane) { - if (IsFileChangedOnDisk(pane)) + if (IsFileChangedOnDisk(pane) == FileChanged) { String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge scanned it last time.\n\nDo you want to reload the file?"), m_filePaths[pane]); if (AfxMessageBox(msg.c_str(), MB_YESNO | MB_ICONWARNING) == IDYES) diff --git a/Src/ImgMergeFrm.h b/Src/ImgMergeFrm.h index d697f5d0aec..b0ea500563a 100644 --- a/Src/ImgMergeFrm.h +++ b/Src/ImgMergeFrm.h @@ -72,7 +72,7 @@ class CImgMergeFrame : public CMergeFrameCommon,public IMergeDoc bool GenerateReport(const String& sFileName) const override; void DoAutoMerge(int dstPane); bool IsModified() const; - bool IsFileChangedOnDisk(int pane) const; + IMergeDoc::FileChange IsFileChangedOnDisk(int pane) const; void CheckFileChanged(void) override; String GetDescription(int pane) const { return m_strDesc[pane]; } static bool IsLoadable(); diff --git a/Src/MergeDoc.h b/Src/MergeDoc.h index 394d7fdb356..365789e04c0 100644 --- a/Src/MergeDoc.h +++ b/Src/MergeDoc.h @@ -140,13 +140,6 @@ class CLocationView; class CMergeDoc : public CDocument, public IMergeDoc { public: - enum FileChange - { - FileNoChange, - FileChanged, - FileRemoved, - }; - // Attributes public: static int m_nBuffersTemp;