mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-17 19:34:13 +02:00
Sync the folder after saving, and save from the command line through a temporary file
QSaveFile writes a new file, forces it to disk and renames it over the old one, but does not force the folder to disk. On ext4 or XFS a power cut soon after a save can then lose the rename: the previous file comes back, or none. QET::syncFolderOf() now syncs the folder after every project, symbol and title block save (QET::writeXmlText()) and every .qetz save (QetZip::write()). Nothing to do on Windows. --resave and --set-titleblock wrote a .qet straight over the output file (QFile, truncate, write): a run stopped part-way left half a project. They now write through QET::writeXmlText() like the GUI's save. On Windows their .qet now has LF line ends, as the GUI writes. Checked with strace on --resave: fdatasync of the new file, linkat into place, fsync of the folder. The .qet written is byte-identical to before. ctest 106/106. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2006-2026 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef SYNCFOLDER_H
|
||||
#define SYNCFOLDER_H
|
||||
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QString>
|
||||
|
||||
#ifndef Q_OS_WIN
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace QET {
|
||||
/**
|
||||
Force to disk the folder holding @p file_path, once
|
||||
QSaveFile::commit() renamed a new file into it. QSaveFile syncs the
|
||||
file it wrote but not the folder, and on some file systems (ext4,
|
||||
XFS) a power cut soon after can then lose the rename: the previous
|
||||
file comes back, or none. Does nothing on Windows, where a folder
|
||||
cannot be synced this way.
|
||||
*/
|
||||
inline void syncFolderOf(const QString &file_path)
|
||||
{
|
||||
#ifndef Q_OS_WIN
|
||||
const QByteArray folder = QFile::encodeName(QFileInfo(file_path).absolutePath());
|
||||
const int fd = ::open(folder.constData(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
::fsync(fd);
|
||||
::close(fd);
|
||||
}
|
||||
#else
|
||||
Q_UNUSED(file_path)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SYNCFOLDER_H
|
||||
Reference in New Issue
Block a user