From d8d6880682c8d3ad52a2b5e5670560dca399d854 Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Wed, 9 Oct 2013 13:12:11 +0200 Subject: Reduce risks of lock file corruption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trying to do as few operations as possible once the file got opened. That includes generating the data which will go in the file. Indeed if we crashed at that point the lock file is already emptied. Could happen because of the qAppName call. It's then safer to prepare the data upfront, and just open/write/close if possible. Change-Id: Iad32fa822c6a5958ae89d84a2fe02ed5366ea278 Reviewed-by: Jędrzej Nowacki Reviewed-by: Frederik Gladhorn --- src/corelib/io/qlockfile_unix.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp index 1676b71133..de21ee217a 100644 --- a/src/corelib/io/qlockfile_unix.cpp +++ b/src/corelib/io/qlockfile_unix.cpp @@ -140,6 +140,16 @@ static bool setNativeLocks(int fd) QLockFile::LockError QLockFilePrivate::tryLock_sys() { + // Assemble data, to write in a single call to write + // (otherwise we'd have to check every write call) + QByteArray fileData; + fileData += QByteArray::number(QCoreApplication::applicationPid()); + fileData += '\n'; + fileData += qAppName().toUtf8(); + fileData += '\n'; + fileData += localHostName().toUtf8(); + fileData += '\n'; + const QByteArray lockFileName = QFile::encodeName(fileName); const int fd = qt_safe_open(lockFileName.constData(), O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd < 0) { @@ -160,16 +170,6 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys() // We hold the lock, continue. fileHandle = fd; - // Assemble data, to write in a single call to write - // (otherwise we'd have to check every write call) - QByteArray fileData; - fileData += QByteArray::number(QCoreApplication::applicationPid()); - fileData += '\n'; - fileData += qAppName().toUtf8(); - fileData += '\n'; - fileData += localHostName().toUtf8(); - fileData += '\n'; - QLockFile::LockError error = QLockFile::NoError; if (qt_write_loop(fd, fileData.constData(), fileData.size()) < fileData.size()) error = QLockFile::UnknownError; // partition full -- cgit v1.2.3