From 3f54d11837bcc11170645f514f981017fd120390 Mon Sep 17 00:00:00 2001 From: u-235 Date: Mon, 21 Oct 2024 20:30:17 +0300 Subject: [PATCH] Fix file saving when file size is reduced Bug: if data size decreased after editing, then when writing to disk the file size was not decreased and there was a tail of old data. --- gcodeworkshop/src/document.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gcodeworkshop/src/document.cpp b/gcodeworkshop/src/document.cpp index 43127c4d..c34bbad7 100644 --- a/gcodeworkshop/src/document.cpp +++ b/gcodeworkshop/src/document.cpp @@ -206,14 +206,18 @@ bool Document::saveFile(const QString& filePath) QFile file(filePath); m_ioErrorString.clear(); - // WARNING: If opened in QIODevice::ReadOnly mode, a false positive in QFileSystemWatcher + // WARNING: If opened in QIODevice::WriteOnly mode, a false positive in QFileSystemWatcher // occurs after writing and closing. This is correct for Windows 7. + // Update: it is possible that when opening a file with this flag, Windows + // immediately truncates the file size to zero and this happens before + // disabling monitoring in the code below. if (!file.open(QIODevice::ReadWrite)) { m_ioErrorString = file.errorString(); return false; } fileWatchStop(); + file.resize(0); file.write(rawData()); file.close(); fileWatchStart(filePath);