From a44ecd666be74e5169329dd662c6e04b4f6e4650 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Wed, 21 Oct 2026 22:32:33 +0800 Subject: [PATCH] fix: prevent file recreation at deleted path on save MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: closeTab() called saveFile() without checking whether the source file still existed on disk; deleted files were silently recreated at their original path, conflicting with trash metadata and showing "locked" in file manager 2. Fix: closeTab() now checks fileInfo.exists() before saving; if the file is gone, it redirects to saveAsFile() instead; saveAsFileToDisk(), saveBlankFileToDisk(), and saveAsOtherTabFile() only set the dialog directory when the original file still exists, avoiding pointing to a deleted directory 3. Impact: closing a tab whose source file was moved to trash now prompts Save As instead of recreating the file; existing save paths for present files are unchanged Log: Fixed issue where saving after deleting the source file recreated it at the original path, causing trash lock conflict Influence: 1. Open a txt file, edit it, delete the source file, close the editor and choose Save — verify Save As dialog appears 2. Open a txt file, edit it, close the editor and choose Save with the file still present — verify normal save works 3. Test saveAs, saveBlank, and saveAsOtherTab when source file is deleted — verify dialog does not default to deleted path fix: 修复删除源文件后保存导致原路径重建文件的问题 1. 根因:closeTab() 在保存前未检查源文件是否仍存在于磁盘, 直接调用 saveFile() 在原路径重建文件,与回收站元数据冲突, 导致文件管理器回收站显示"锁定"状态 2. 方案:closeTab() 保存前检查 fileInfo.exists(),文件不存在时 改用 saveAsFile() 引导用户另存;saveAsFileToDisk()、 saveBlankFileToDisk()、saveAsOtherTabFile() 仅在原始文件 仍存在时设置对话框目录,避免指向已删除的目录 3. 影响:关闭源文件已移入回收站的标签页时,将弹出另存对话框 而非在原路径重建文件;文件存在时的保存行为不变 Log: 修复删除源文件后保存时在原路径重建文件导致回收站锁定的问题 Influence: 1. 打开 txt 文件编辑后删除源文件,关闭编辑器选择保存, 验证弹出另存对话框 2. 打开 txt 文件编辑后正常关闭保存,验证保存功能正常 3. 测试源文件被删除时的另存为、空白保存、其他标签页保存, 验证对话框不默认指向已删除目录 PMS: BUG-366573 --- src/widgets/window.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 91a9af10..c72fe862 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -1103,7 +1103,13 @@ bool Window::closeTab(const QString &filePath) } } else { qDebug() << "File is not a backup file, saving normally"; - if (wrapper->saveFile()) { + if (!fileInfo.exists()) { + qDebug() << "File no longer exists on disk, redirecting to Save As"; + if (!saveAsFile()) { + qWarning() << "Save as file cancelled or failed, keep tab open"; + return false; + } + } else if (wrapper->saveFile()) { qDebug() << "File saved, removing wrapper and closing tab"; removeWrapper(filePath, true); m_tabbar->closeCurrentTab(filePath); @@ -1557,7 +1563,9 @@ QString Window::saveAsFileToDisk() dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); } @@ -1685,7 +1693,9 @@ QString Window::saveBlankFileToDisk() dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); } @@ -1734,7 +1744,9 @@ bool Window::saveAsOtherTabFile(EditWrapper *wrapper) dialog.selectFile(match.captured(0) + ".txt"); qDebug() << "Setting dialog file name to draft file name:" << match.captured(0) + ".txt"; } else { - dialog.setDirectory(fileInfo.dir()); + if (fileInfo.exists()) { + dialog.setDirectory(fileInfo.dir()); + } dialog.selectFile(fileInfo.fileName()); qDebug() << "Setting dialog file name to current file name:" << fileInfo.fileName(); }