Skip to content

Use flock() for file locking #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 9, 2016
Next Next commit
Replaced lock file code with flock()
  • Loading branch information
expobrain committed Sep 12, 2016
commit 547e6f0e2e9ace80e6d6b08db3c15fe91efe880f
47 changes: 26 additions & 21 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -324,12 +325,12 @@ def update(self, op_code, cur_count, max_count=None, message=''):

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__()
Expand Down Expand Up @@ -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

Expand All @@ -559,17 +561,24 @@ def _obtain_lock_or_raise(self):
: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):
Expand All @@ -582,19 +591,15 @@ def _release_lock(self):
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):
Expand Down Expand Up @@ -629,7 +634,7 @@ def _obtain_lock(self):
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())):
Expand Down