From a8eaa0c5acf5e4921c2f6940cc0c61aecb7b0047 Mon Sep 17 00:00:00 2001 From: Ahmed El Khazari Date: Tue, 11 Jun 2024 19:39:05 +0300 Subject: [PATCH] QLockFile: Use fcntl for file locking on Android This commit updates the setNativeLocks function to use fcntl for file locking on Android systems. Previously, the function attempted to use flock, which would return false immediately upon failure in some cases. By switching to fcntl, the function aims to provide more reliable lock acquisition on Android. This change is specific to Android systems and does not impact other platforms where flock is used. Fixes: QTBUG-116511 Pick-to: 6.7 6.5 6.2 5.15 Change-Id: Ifb93cb6a8aec801539ba378c23d9b1ada3f069bb Reviewed-by: Ville Voutilainen (cherry picked from commit 3b8e8bd081c3d58d9e0e7ef4129b4f27ea0e250f) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qlockfile_unix.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp index 47aff8b9737..bd0569bce2c 100644 --- a/src/corelib/io/qlockfile_unix.cpp +++ b/src/corelib/io/qlockfile_unix.cpp @@ -101,12 +101,26 @@ static qint64 qt_write_loop(int fd, const char *data, qint64 len) * actually the same. Therefore, it's a very bad idea to mix them in the same * process. * - * We therefore use only flock(2). + * We therefore use only flock(2), except on Android. + * + * Android Compatibility: + * Some versions of Android have known issues where flock does not function correctly.  + * As a result, on Android, we use POSIX fcntl(F_SETLK) to handle file locking. + * fcntl is better integrated with Android’s underlying system, avoiding + * the limitations of flock. */ static bool setNativeLocks(int fd) { -#if defined(LOCK_EX) && defined(LOCK_NB) +#if defined(Q_OS_ANDROID) + struct flock fl; + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + if (fcntl(fd, F_SETLK, &fl) == -1) + return false; +#elif defined(LOCK_EX) && defined(LOCK_NB) if (flock(fd, LOCK_EX | LOCK_NB) == -1) // other threads, and other processes on a local fs return false; #else