Skip to content

Commit

Permalink
Fix file saving when file size is reduced
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
u-235 committed Oct 21, 2024
1 parent 5fd7327 commit 3f54d11
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gcodeworkshop/src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3f54d11

Please sign in to comment.