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 <ville.voutilainen@qt.io>
(cherry picked from commit 3b8e8bd081c3d58d9e0e7ef4129b4f27ea0e250f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmed El Khazari 2024-06-11 19:39:05 +03:00 committed by Qt Cherry-pick Bot
parent 71cba1359e
commit a8eaa0c5ac

View File

@ -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 Androids 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