commit:     3741b4f7d7b901bc7d69bd58ce1b92b0536427bb
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sat Mar  6 06:10:58 2021 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Sat Mar  6 06:14:38 2021 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=3741b4f7

_get_lock_fn: split out _test_lock_fn

Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/locks.py | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/lib/portage/locks.py b/lib/portage/locks.py
index 1073343be..163c92482 100644
--- a/lib/portage/locks.py
+++ b/lib/portage/locks.py
@@ -1,5 +1,5 @@
 # portage: Lock management code
-# Copyright 2004-2020 Gentoo Authors
+# Copyright 2004-2021 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = ["lockdir", "unlockdir", "lockfile", "unlockfile", \
@@ -12,6 +12,7 @@ import multiprocessing
 import sys
 import tempfile
 import time
+import typing
 import warnings
 
 import portage
@@ -44,11 +45,21 @@ def _get_lock_fn():
        if _lock_fn is not None:
                return _lock_fn
 
+       if _test_lock_fn(fcntl.lockf):
+               _lock_fn = fcntl.lockf
+               return _lock_fn
+
+       # Fall back to fcntl.flock.
+       _lock_fn = fcntl.flock
+       return _lock_fn
+
+
+def _test_lock_fn(lock_fn: typing.Callable[[int, int], None]) -> bool:
        def _test_lock(fd, lock_path):
                os.close(fd)
                try:
                        with open(lock_path, 'a') as f:
-                               fcntl.lockf(f.fileno(), 
fcntl.LOCK_EX|fcntl.LOCK_NB)
+                               lock_fn(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
                except EnvironmentError as e:
                        if e.errno == errno.EAGAIN:
                                # Parent process holds lock, as expected.
@@ -60,7 +71,7 @@ def _get_lock_fn():
        fd, lock_path = tempfile.mkstemp()
        try:
                try:
-                       fcntl.lockf(fd, fcntl.LOCK_EX)
+                       lock_fn(fd, fcntl.LOCK_EX)
                except EnvironmentError:
                        pass
                else:
@@ -69,16 +80,12 @@ def _get_lock_fn():
                        proc.start()
                        proc.join()
                        if proc.exitcode == os.EX_OK:
-                               # Use fcntl.lockf because the test passed.
-                               _lock_fn = fcntl.lockf
-                               return _lock_fn
+                               # the test passed
+                               return True
        finally:
                os.close(fd)
                os.unlink(lock_path)
-
-       # Fall back to fcntl.flock.
-       _lock_fn = fcntl.flock
-       return _lock_fn
+       return False
 
 
 _open_fds = {}

Reply via email to