From 547e6f0e2e9ace80e6d6b08db3c15fe91efe880f Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Mon, 12 Sep 2016 17:19:20 +0100 Subject: Replaced lock file code with flock() --- git/util.py | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index f5c69231..fff8ddc0 100644 --- a/git/util.py +++ b/git/util.py @@ -4,6 +4,7 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php +from fcntl import flock, LOCK_UN, LOCK_EX, LOCK_NB import os import re import sys @@ -324,12 +325,12 @@ class RemoteProgress(object): You may read the contents of the current line in self._cur_line""" pass - + class CallableRemoteProgress(RemoteProgress): """An implementation forwarding updates to any callable""" __slots__ = ('_callable') - + def __init__(self, fn): self._callable = fn super(CallableRemoteProgress, self).__init__() @@ -535,9 +536,10 @@ class LockFile(object): As we are a utility class to be derived from, we only use protected methods. Locks will automatically be released on destruction""" - __slots__ = ("_file_path", "_owns_lock") + __slots__ = ("_file_path", "_owns_lock", "_file_descriptor") def __init__(self, file_path): + self._file_descriptor = None self._file_path = file_path self._owns_lock = False @@ -559,17 +561,24 @@ class LockFile(object): :raise IOError: if a lock was already present or a lock file could not be written""" if self._has_lock(): return + lock_file = self._lock_file_path() - if os.path.isfile(lock_file): - raise IOError("Lock for file %r did already exist, delete %r in case the lock is illegal" % - (self._file_path, lock_file)) + + # Create lock file + try: + open(lock_file, 'a').close() + except OSError as e: + # Silence error only if file exists + if e.errno != 17: # 17 -> File exists + raise try: - fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0) - os.close(fd) + fd = os.open(lock_file, os.O_WRONLY, 0) + flock(fd, LOCK_EX | LOCK_NB) except OSError as e: raise IOError(str(e)) + self._file_descriptor = fd self._owns_lock = True def _obtain_lock(self): @@ -582,19 +591,15 @@ class LockFile(object): if not self._has_lock(): return - # if someone removed our file beforhand, lets just flag this issue - # instead of failing, to make it more usable. - lfp = self._lock_file_path() - try: - # on bloody windows, the file needs write permissions to be removable. - # Why ... - if os.name == 'nt': - os.chmod(lfp, 0o777) - # END handle win32 - os.remove(lfp) - except OSError: - pass + fd = self._file_descriptor + lock_file = self._lock_file_path() + + flock(fd, LOCK_UN) + os.close(fd) + os.remove(lock_file) + self._owns_lock = False + self._file_descriptor = None class BlockingLockFile(LockFile): @@ -629,7 +634,7 @@ class BlockingLockFile(LockFile): try: super(BlockingLockFile, self)._obtain_lock() except IOError: - # synity check: if the directory leading to the lockfile is not + # sanity check: if the directory leading to the lockfile is not # readable anymore, raise an execption curtime = time.time() if not os.path.isdir(os.path.dirname(self._lock_file_path())): -- cgit v1.2.1 From 6dfc870cffcfa7ca29e30301fff256e4e5b181f3 Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Wed, 14 Sep 2016 10:23:34 +0100 Subject: No need to create the lock file beforehand --- git/util.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index fff8ddc0..8d97242c 100644 --- a/git/util.py +++ b/git/util.py @@ -564,20 +564,14 @@ class LockFile(object): lock_file = self._lock_file_path() - # Create lock file + # Create file and lock try: - open(lock_file, 'a').close() - except OSError as e: - # Silence error only if file exists - if e.errno != 17: # 17 -> File exists - raise - - try: - fd = os.open(lock_file, os.O_WRONLY, 0) - flock(fd, LOCK_EX | LOCK_NB) + fd = os.open(lock_file, os.O_CREAT, 0) except OSError as e: raise IOError(str(e)) + flock(fd, LOCK_EX | LOCK_NB) + self._file_descriptor = fd self._owns_lock = True -- cgit v1.2.1 From f0fcc072b37d6e773e18ea561ebce8ccba2ba19c Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Mon, 19 Sep 2016 23:32:39 +0100 Subject: Force GC collection --- git/test/test_util.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'git') diff --git a/git/test/test_util.py b/git/test/test_util.py index c6ca6920..3a67c04b 100644 --- a/git/test/test_util.py +++ b/git/test/test_util.py @@ -5,6 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php import tempfile +import gc from git.test.lib import ( TestBase, @@ -76,6 +77,7 @@ class TestUtils(TestBase): # auto-release on destruction del(other_lock_file) + gc.collect() lock_file._obtain_lock_or_raise() lock_file._release_lock() -- cgit v1.2.1 From d437f8893f60bfdc703b0d16335f28ad2e799974 Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Thu, 6 Oct 2016 23:54:32 +0100 Subject: Fixed typo after merge --- git/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index 9f8ccea5..fabfffb8 100644 --- a/git/util.py +++ b/git/util.py @@ -616,9 +616,8 @@ class LockFile(object): # if someone removed our file beforhand, lets just flag this issue # instead of failing, to make it more usable. - lfp = self._lock_file_path() try: - rmfile(lfp) + rmfile(lock_file) except OSError: pass self._owns_lock = False -- cgit v1.2.1 From ab7a87290149114e080037f8da0871939b1ed7fc Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Fri, 7 Oct 2016 22:45:36 +0100 Subject: Updated file locking for Windows platform --- git/util.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index fabfffb8..75c455b6 100644 --- a/git/util.py +++ b/git/util.py @@ -5,7 +5,6 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from __future__ import unicode_literals -from fcntl import flock, LOCK_UN, LOCK_EX, LOCK_NB import getpass import logging import os @@ -49,6 +48,57 @@ __all__ = ("stream_copy", "join_path", "to_native_path_windows", "to_native_path #{ Utility Methods +if platform.system() == 'Windows': + # This code is a derivative work of Portalocker http://code.activestate.com/recipes/65203/ + import win32con + import win32file + import pywintypes + + LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK + LOCK_SH = 0 # the default + LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY + LOCK_UN = 1 << 2 + + __overlapped = pywintypes.OVERLAPPED() + + def flock(fd, flags=0): + hfile = win32file._get_osfhandle(fd) + + if flags & LOCK_UN != 0: + # Unlock file descriptor + try: + win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) + except pywintypes.error, exc_value: + # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') + # To match the 'posix' implementation, silently ignore this error + if exc_value[0] == 158: + pass + else: + # Q: Are there exceptions/codes we should be dealing with here? + raise + + elif flags & LOCK_EX != 0: + # Lock file + try: + win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) + except pywintypes.error, exc_value: + if exc_value[0] == 33: + # error: (33, 'LockFileEx', + # 'The process cannot access the file because another process has locked + # a portion of the file.') + raise IOError(33, exc_value[2]) + else: + # Q: Are there exceptions/codes we should be dealing with here? + raise + + else: + raise NotImplementedError("Unsupported set of bitflags {}".format(bin(flags))) + + +else: + # from fcntl import flock, LOCK_UN, LOCK_EX, LOCK_NB + pass + def unbare_repo(func): """Methods with this decorator raise InvalidGitRepositoryError if they @@ -620,6 +670,7 @@ class LockFile(object): rmfile(lock_file) except OSError: pass + self._owns_lock = False self._file_descriptor = None -- cgit v1.2.1 From 4c9dd7fac214bb0f85077cab40d4455cf3113b17 Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Fri, 7 Oct 2016 23:12:39 +0100 Subject: Forgot to uncomment conditional import --- git/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index 75c455b6..ab27dadf 100644 --- a/git/util.py +++ b/git/util.py @@ -96,8 +96,7 @@ if platform.system() == 'Windows': else: - # from fcntl import flock, LOCK_UN, LOCK_EX, LOCK_NB - pass + from fcntl import flock, LOCK_UN, LOCK_EX, LOCK_NB def unbare_repo(func): -- cgit v1.2.1 From 20b4f49b4e71c90581c68ea7f4cf56160a6bdc79 Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Fri, 7 Oct 2016 23:42:32 +0100 Subject: Fix syntax for Python 3.x --- git/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'git') diff --git a/git/util.py b/git/util.py index ab27dadf..037fd91b 100644 --- a/git/util.py +++ b/git/util.py @@ -68,7 +68,7 @@ if platform.system() == 'Windows': # Unlock file descriptor try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) - except pywintypes.error, exc_value: + except pywintypes.error as exc_value: # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') # To match the 'posix' implementation, silently ignore this error if exc_value[0] == 158: @@ -81,7 +81,7 @@ if platform.system() == 'Windows': # Lock file try: win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) - except pywintypes.error, exc_value: + except pywintypes.error as exc_value: if exc_value[0] == 33: # error: (33, 'LockFileEx', # 'The process cannot access the file because another process has locked -- cgit v1.2.1