Skip to content

Commit

Permalink
Fix issue #1985: Wimerge saves changes to the wrong file (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Aug 19, 2023
1 parent 31cef09 commit 4da7581
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 63 deletions.
36 changes: 28 additions & 8 deletions Src/HexMergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,20 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
if (!bAllowCancel)
dlg.m_bDisableCancel = true;
if (!pathLeft.empty())
dlg.m_sLeftFile = pathLeft;
dlg.m_sLeftFile = m_strSaveAsPath.empty() ? pathLeft : m_strSaveAsPath;
else
dlg.m_sLeftFile = m_strDesc[0];
dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_strDesc[0] : m_strSaveAsPath;
if (m_nBuffers == 3)
{
if (!pathMiddle.empty())
dlg.m_sMiddleFile = pathMiddle;
dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? pathMiddle : m_strSaveAsPath;
else
dlg.m_sMiddleFile = m_strDesc[1];
dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath;
}
if (!pathRight.empty())
dlg.m_sRightFile = pathRight;
dlg.m_sRightFile = m_strSaveAsPath.empty() ? pathRight : m_strSaveAsPath;
else
dlg.m_sRightFile = m_strDesc[1];
dlg.m_sRightFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath;

if (dlg.DoModal() == IDOK)
{
Expand Down Expand Up @@ -329,15 +329,35 @@ bool CHexMergeDoc::DoFileSave(int nBuffer)
result = DoFileSaveAs(nBuffer);
else
{
const String &path = m_filePaths.GetPath(nBuffer);
HRESULT hr = m_pView[nBuffer]->SaveFile(path.c_str());
String strSavePath = m_strSaveAsPath.empty() ? m_filePaths.GetPath(nBuffer) : m_strSaveAsPath;
// Warn user in case file has been changed by someone else
if (m_pView[nBuffer]->IsFileChangedOnDisk(m_filePaths.GetPath(nBuffer).c_str()) == IMergeDoc::FileChange::Changed)
{
String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), m_filePaths.GetPath(nBuffer));
if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
return false;
}
// Ask user what to do about FILE_ATTRIBUTE_READONLY
String strPath = strSavePath;
bool bApplyToAll = false;
if (CMergeApp::HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
return false;
strSavePath = strPath.c_str();
// Take a chance to create a backup
if (!CMergeApp::CreateBackup(false, strSavePath))
return false;
HRESULT hr = m_pView[nBuffer]->SaveFile(strSavePath.c_str());
if (Try(hr) == IDCANCEL)
return false;
if (FAILED(hr))
return DoFileSaveAs(nBuffer);
result = true;
if (result)
{
m_filePaths[nBuffer] = strSavePath;
UpdateHeaderPath(nBuffer);
UpdateDiffItem(m_pDirDoc);
}
}
}
return result;
Expand Down
16 changes: 0 additions & 16 deletions Src/HexMergeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,6 @@ HRESULT CHexMergeView::LoadFile(const tchar_t* path)
*/
HRESULT CHexMergeView::SaveFile(const tchar_t* path, bool packing)
{
// Warn user in case file has been changed by someone else
if (IsFileChangedOnDisk(path) == IMergeDoc::FileChange::Changed)
{
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)
return E_FAIL;
}
// Ask user what to do about FILE_ATTRIBUTE_READONLY
String strPath = path;
bool bApplyToAll = false;
if (CMergeApp::HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
return E_FAIL;
path = strPath.c_str();
// Take a chance to create a backup
if (!CMergeApp::CreateBackup(false, path))
return E_FAIL;
// Write data to an intermediate file
String tempPath = env::GetTemporaryPath();
String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
Expand Down
5 changes: 4 additions & 1 deletion Src/ImgMergeFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,17 @@ bool CImgMergeFrame::DoFileSave(int pane)
return false;
CMergeApp::CreateBackup(false, m_filePaths[pane]);
int savepoint = m_pImgMergeWindow->GetSavePoint(pane);
if (!m_pImgMergeWindow->SaveImage(pane))
bool result = m_strSaveAsPath.empty() ? m_pImgMergeWindow->SaveImage(pane) : m_pImgMergeWindow->SaveImageAs(pane, m_strSaveAsPath.c_str());
if (!result)
{
String str = strutils::format_string2(_("Saving file failed.\n%1\n%2\nDo you want to:\n\t- use a different filename (Press OK)\n\t- abort the current operation (Press Cancel)?"), filename, GetSysError());
int answer = AfxMessageBox(str.c_str(), MB_OKCANCEL | MB_ICONWARNING);
if (answer == IDOK)
return DoFileSaveAs(pane);
return false;
}
if (!m_strSaveAsPath.empty())
m_filePaths[pane] = m_strSaveAsPath;
if (filename != m_filePaths[pane])
{
if (!m_infoUnpacker.Packing(filename, m_filePaths[pane], m_unpackerSubcodes[pane], { m_filePaths[pane] }))
Expand Down
9 changes: 8 additions & 1 deletion Src/Merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,14 @@ bool CMergeApp::ParseArgsAndDoOpen(MergeCmdLineInfo& cmdInfo, CMainFrame* pMainF
pOpenTableFileParams->m_tableDelimiter = cmdInfo.m_cTableDelimiter;
pOpenTableFileParams->m_tableQuote = cmdInfo.m_cTableQuote;
pOpenTableFileParams->m_tableAllowNewlinesInQuotes = cmdInfo.m_bTableAllowNewlinesInQuotes;
pOpenTableFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath;
}
if (auto* pOpenBinaryFileParams = dynamic_cast<CMainFrame::OpenBinaryFileParams*>(pOpenParams.get()))
{
pOpenBinaryFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath;
}
if (auto* pOpenImageFileParams = dynamic_cast<CMainFrame::OpenImageFileParams*>(pOpenParams.get()))
{
pOpenImageFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath;
}
if (cmdInfo.m_Files.GetSize() > 2)
{
Expand Down
44 changes: 7 additions & 37 deletions Src/MergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ bool CMergeDoc::TrySaveAs(String &strPath, int &nSaveResult, String & sError,
bool CMergeDoc::DoSave(const tchar_t* szPath, bool &bSaveSuccess, int nBuffer)
{
DiffFileInfo fileInfo;
String strSavePath(szPath);
String strSavePath(m_strSaveAsPath.empty() ? szPath : m_strSaveAsPath);
FileChange fileChanged;
bool bApplyToAll = false;
int nRetVal = -1;
Expand All @@ -1680,21 +1680,6 @@ bool CMergeDoc::DoSave(const tchar_t* szPath, bool &bSaveSuccess, int nBuffer)

bSaveSuccess = false;

// Check third arg possibly given from command-line
if (!m_strSaveAsPath.empty())
{
if (paths::DoesPathExist(m_strSaveAsPath) == paths::IS_EXISTING_DIR)
{
// third arg was a directory, so get append the filename
String sname;
paths::SplitFilename(szPath, 0, &sname, 0);
strSavePath = m_strSaveAsPath;
strSavePath = paths::ConcatPath(strSavePath, sname);
}
else
strSavePath = m_strSaveAsPath;
}

nRetVal = CMergeApp::HandleReadonlySave(strSavePath, false, bApplyToAll);
if (nRetVal == IDCANCEL)
return false;
Expand Down Expand Up @@ -2644,35 +2629,20 @@ bool CMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
if (!bAllowCancel)
dlg.m_bDisableCancel = true;
if (!m_filePaths.GetLeft().empty())
{
if (m_strSaveAsPath.empty())
dlg.m_sLeftFile = m_filePaths.GetLeft();
else
dlg.m_sLeftFile = m_strSaveAsPath;
}
dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_filePaths.GetLeft() : m_strSaveAsPath;
else
dlg.m_sLeftFile = m_strDesc[0];
dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_strDesc[0] : m_strSaveAsPath;
if (m_nBuffers == 3)
{
if (!m_filePaths.GetMiddle().empty())
{
if (m_strSaveAsPath.empty())
dlg.m_sMiddleFile = m_filePaths.GetMiddle();
else
dlg.m_sMiddleFile = m_strSaveAsPath;
}
dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_filePaths.GetMiddle() : m_strSaveAsPath;
else
dlg.m_sMiddleFile = m_strDesc[1];
dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath;
}
if (!m_filePaths.GetRight().empty())
{
if (m_strSaveAsPath.empty())
dlg.m_sRightFile = m_filePaths.GetRight();
else
dlg.m_sRightFile = m_strSaveAsPath;
}
dlg.m_sRightFile = m_strSaveAsPath.empty() ?m_filePaths.GetRight() : m_strSaveAsPath;
else
dlg.m_sRightFile = m_strDesc[m_nBuffers - 1];
dlg.m_sRightFile = m_strSaveAsPath.empty() ? m_strDesc[m_nBuffers - 1] : m_strSaveAsPath;

if (dlg.DoModal() == IDOK)
{
Expand Down

0 comments on commit 4da7581

Please sign in to comment.