IPC: move createUnixKeyFile to a common, private header
So we don't have to mess around with #ifs to support the build. Drive-by replace with unlink() some calls to QFile::remove(), which create a QFSFileEngine, to call QFSFileEngine::remove, which calls QFileSystemEngine::removeFile. Since we've just created the file with qt_safe_open(), we may as well use the low-level Unix function to remove too. Change-Id: Id8d5e3999fe94b03acc1fffd171c06c7efc0b0f0 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
parent
fc14f1a467
commit
461463b75d
@ -20,19 +20,6 @@ key_t QSharedMemoryPrivate::handle()
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // QT_CONFIG(sharedmemory)
|
||||
|
||||
#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName)
|
||||
{
|
||||
Q_UNUSED(fileName);
|
||||
Q_UNIMPLEMENTED();
|
||||
return 0;
|
||||
}
|
||||
#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
|
||||
#if QT_CONFIG(sharedmemory)
|
||||
|
||||
bool QSharedMemoryPrivate::cleanHandle()
|
||||
{
|
||||
Q_UNIMPLEMENTED();
|
||||
|
@ -26,7 +26,6 @@ QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QSharedMemoryPrivate
|
||||
{
|
||||
int createUnixKeyFile(const QString &fileName);
|
||||
QString makePlatformSafeKey(const QString &key,
|
||||
const QString &prefix = QStringLiteral("qipc_sharedmemory_"));
|
||||
}
|
||||
@ -93,7 +92,6 @@ public:
|
||||
bool lockedByMe = false;
|
||||
#endif
|
||||
|
||||
static int createUnixKeyFile(const QString &fileName);
|
||||
static QString makePlatformSafeKey(const QString &key,
|
||||
const QString &prefix = QStringLiteral("qipc_sharedmemory_"));
|
||||
#ifdef Q_OS_WIN
|
||||
|
@ -1,11 +1,11 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
||||
|
||||
#include "qplatformdefs.h"
|
||||
|
||||
#include "qsharedmemory.h"
|
||||
#include "qsharedmemory_p.h"
|
||||
#include "qsystemsemaphore.h"
|
||||
|
||||
#include "qtipccommon_p.h"
|
||||
|
||||
#include <qdir.h>
|
||||
#include <qdebug.h>
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
using namespace QtIpcCommon;
|
||||
|
||||
/*!
|
||||
\internal
|
||||
@ -63,35 +64,6 @@ key_t QSharedMemoryPrivate::handle()
|
||||
return unix_key;
|
||||
}
|
||||
|
||||
#endif // QT_CONFIG(sharedmemory)
|
||||
|
||||
#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
/*!
|
||||
\internal
|
||||
Creates the unix file if needed.
|
||||
returns \c true if the unix file was created.
|
||||
|
||||
-1 error
|
||||
0 already existed
|
||||
1 created
|
||||
*/
|
||||
int QT_PREPEND_NAMESPACE(QSharedMemoryPrivate)::createUnixKeyFile(const QString &fileName)
|
||||
{
|
||||
int fd = qt_safe_open(QFile::encodeName(fileName).constData(),
|
||||
O_EXCL | O_CREAT | O_RDWR, 0640);
|
||||
if (-1 == fd) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
return -1;
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
|
||||
#if QT_CONFIG(sharedmemory)
|
||||
|
||||
bool QSharedMemoryPrivate::cleanHandle()
|
||||
{
|
||||
unix_key = 0;
|
||||
@ -102,7 +74,8 @@ bool QSharedMemoryPrivate::create(qsizetype size)
|
||||
{
|
||||
// build file if needed
|
||||
bool createdFile = false;
|
||||
int built = createUnixKeyFile(nativeKey);
|
||||
QByteArray nativeKeyFile = QFile::encodeName(nativeKey);
|
||||
int built = createUnixKeyFile(nativeKeyFile);
|
||||
if (built == -1) {
|
||||
errorString = QSharedMemory::tr("%1: unable to make key").arg("QSharedMemory::handle:"_L1);
|
||||
error = QSharedMemory::KeyError;
|
||||
@ -115,7 +88,7 @@ bool QSharedMemoryPrivate::create(qsizetype size)
|
||||
// get handle
|
||||
if (!handle()) {
|
||||
if (createdFile)
|
||||
QFile::remove(nativeKey);
|
||||
unlink(nativeKeyFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -131,7 +104,7 @@ bool QSharedMemoryPrivate::create(qsizetype size)
|
||||
setErrorString(function);
|
||||
}
|
||||
if (createdFile && error != QSharedMemory::AlreadyExists)
|
||||
QFile::remove(nativeKey);
|
||||
unlink(nativeKeyFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -212,7 +185,7 @@ bool QSharedMemoryPrivate::detach()
|
||||
}
|
||||
|
||||
// remove file
|
||||
if (!QFile::remove(nativeKey))
|
||||
if (!unlink(QFile::encodeName(nativeKey)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -20,7 +20,9 @@
|
||||
#if QT_CONFIG(systemsemaphore)
|
||||
|
||||
#include "qcoreapplication.h"
|
||||
#include "qtipccommon_p.h"
|
||||
#include "qsharedmemory_p.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#ifdef QT_POSIX_IPC
|
||||
# include <semaphore.h>
|
||||
|
@ -63,7 +63,7 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
|
||||
return unix_key;
|
||||
|
||||
// Create the file needed for ftok
|
||||
int built = QSharedMemoryPrivate::createUnixKeyFile(fileName);
|
||||
int built = QtIpcCommon::createUnixKeyFile(QFile::encodeName(fileName));
|
||||
if (-1 == built) {
|
||||
errorString = QSystemSemaphore::tr("%1: unable to make key")
|
||||
.arg("QSystemSemaphore::handle:"_L1);
|
||||
|
53
src/corelib/ipc/qtipccommon_p.h
Normal file
53
src/corelib/ipc/qtipccommon_p.h
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright (C) 2022 Intel Corporation.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
||||
|
||||
#ifndef QTIPCCOMMON_P_H
|
||||
#define QTIPCCOMMON_P_H
|
||||
|
||||
//
|
||||
// W A R N I N G
|
||||
// -------------
|
||||
//
|
||||
// This file is not part of the Qt API. It exists purely as an
|
||||
// implementation detail. This header file may change from version to
|
||||
// version without notice, or even be removed.
|
||||
//
|
||||
// We mean it.
|
||||
//
|
||||
|
||||
#include <private/qglobal_p.h>
|
||||
|
||||
#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
|
||||
#if defined(Q_OS_UNIX)
|
||||
# include <qfile.h>
|
||||
# include <private/qcore_unix_p.h>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QtIpcCommon {
|
||||
#ifdef Q_OS_UNIX
|
||||
// Convenience function to create the file if needed
|
||||
inline int createUnixKeyFile(const QByteArray &fileName)
|
||||
{
|
||||
int fd = qt_safe_open(fileName.constData(), O_EXCL | O_CREAT | O_RDWR, 0640);
|
||||
if (fd < 0) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
return -1;
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
#endif // Q_OS_UNIX
|
||||
} // namespace QtIpcCommon
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
|
||||
|
||||
|
||||
#endif // QTIPCCOMMON_P_H
|
Loading…
Reference in New Issue
Block a user