Skip to content

Commit 44a7adb

Browse files
committed
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.
1 parent 5fd7327 commit 44a7adb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

gcodeworkshop/src/document.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,18 @@ bool Document::saveFile(const QString& filePath)
206206
QFile file(filePath);
207207
m_ioErrorString.clear();
208208

209-
// WARNING: If opened in QIODevice::ReadOnly mode, a false positive in QFileSystemWatcher
209+
// WARNING: If opened in QIODevice::WriteOnly mode, a false positive in QFileSystemWatcher
210210
// occurs after writing and closing. This is correct for Windows 7.
211+
// Update: it is possible that when opening a file with this flag, Windows
212+
// immediately truncates the file size to zero and this happens before
213+
// disabling monitoring in the code below.
211214
if (!file.open(QIODevice::ReadWrite)) {
212215
m_ioErrorString = file.errorString();
213216
return false;
214217
}
215218

216219
fileWatchStop();
220+
file.resize(0);
217221
file.write(rawData());
218222
file.close();
219223
fileWatchStart(filePath);

0 commit comments

Comments
 (0)