fritzing / fritzing-app

Fritzing desktop application
http://fritzing.org
Other
3.87k stars 814 forks source link

File Save Operation Should Be Atomic to Prevent Data Loss and Corruption #4148

Closed KjellMorgenstern closed 1 week ago

KjellMorgenstern commented 1 month ago

Current Behaviour

When saving changes to a file, there is a moment when the original file content is deleted, before it is then rewritten. This is risky.

Observed scenarios:

Speculative scenarios (not verified)

Build: All versions

Operating System: All

Steps to reproduce: Place a breakpoint in mainwindow_export.cpp:

bool MainWindow::saveAsAux(const QString & fileName) {
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {   // <-- break here
        QMessageBox::warning(this, tr("Fritzing"),
                             tr("Cannot write file %1:\n%2.")
                             .arg(fileName, file.errorString()));
        return false;
    }

    file.close();

    setReadOnly(false);

Observe that the file size is zero after the file.close() was executed.

Expected Behaviour

The writability check must be non-destructive.

KjellMorgenstern commented 1 week ago

Fixed with abe60fe6c