def _getlock(self, name, fd, lock_type=None, offset=0, length=0, lock=None, tlock=False):
"""Get byte range lock on file given by file descriptor"""
rn = self.random.randint(0,9999)
stype = fcntl.F_SETLK
if lock_type == fcntl.F_UNLCK:
lstr = "UNLOCK"
if not lock or rn >= 100*self.unlock:
# Do not unlock file
return
self.nunlock += 1
else:
if tlock:
# Just do TLOCK
lstr = "TLOCK "
stype = fcntl.F_GETLK
if rn >= 100*self.tlock:
# No lock, so no tlock
return
self.ntlock += 1
else:
lstr = "LOCK "
if rn >= 100*self.lock:
# No lock
return
self.nlock += 1
if lock_type is None:
# Choose lock: read or write
if self._percent(50):
lock_type = fcntl.F_RDLCK
else:
lock_type = fcntl.F_WRLCK
if not tlock:
# LOCK is requested, but do TLOCK before actual lock
self._getlock(name, fd, lock_type=lock_type, offset=offset, length=length, lock=lock, tlock=True)
fstr = ""
if offset == 0 and length == 0 and lstr == "LOCK ":
fstr = " full file"
self._dprint("DBG4", "%s %s %d @ %d (%s)%s" % (lstr, name, length, offset, LOCKMAP[lock_type], fstr))
lockdata = struct.pack('hhllhh', lock_type, 0, offset, length, 0, 0)
return fcntl.fcntl(fd, stype, lockdata)
评论列表
文章目录