Python fcntl 模块,F_SETLK 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用fcntl.F_SETLK

项目:node-ninja    作者:CodeJockey    | 项目源码 | 文件源码
def ExecFlock(self, lockfile, *cmd_list):
    """Emulates the most basic behavior of Linux's flock(1)."""
    # Rely on exception handling to report errors.
    # Note that the stock python on SunOS has a bug
    # where fcntl.flock(fd, LOCK_EX) always fails
    # with EBADF, that's why we use this F_SETLK
    # hack instead.
    fd = os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0666)
    if sys.platform.startswith('aix'):
      # Python on AIX is compiled with LARGEFILE support, which changes the
      # struct size.
      op = struct.pack('hhIllqq', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    else:
      op = struct.pack('hhllhhl', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    fcntl.fcntl(fd, fcntl.F_SETLK, op)
    return subprocess.call(cmd_list)
项目:sublime-bem-create    作者:bem-tools    | 项目源码 | 文件源码
def ExecFlock(self, lockfile, *cmd_list):
    """Emulates the most basic behavior of Linux's flock(1)."""
    # Rely on exception handling to report errors.
    # Note that the stock python on SunOS has a bug
    # where fcntl.flock(fd, LOCK_EX) always fails
    # with EBADF, that's why we use this F_SETLK
    # hack instead.
    fd = os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0666)
    if sys.platform.startswith('aix'):
      # Python on AIX is compiled with LARGEFILE support, which changes the
      # struct size.
      op = struct.pack('hhIllqq', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    else:
      op = struct.pack('hhllhhl', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    fcntl.fcntl(fd, fcntl.F_SETLK, op)
    return subprocess.call(cmd_list)
项目:gyp    作者:electron    | 项目源码 | 文件源码
def ExecFlock(self, lockfile, *cmd_list):
    """Emulates the most basic behavior of Linux's flock(1)."""
    # Rely on exception handling to report errors.
    # Note that the stock python on SunOS has a bug
    # where fcntl.flock(fd, LOCK_EX) always fails
    # with EBADF, that's why we use this F_SETLK
    # hack instead.
    fd = os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0666)
    if sys.platform.startswith('aix'):
      # Python on AIX is compiled with LARGEFILE support, which changes the
      # struct size.
      op = struct.pack('hhIllqq', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    else:
      op = struct.pack('hhllhhl', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
    fcntl.fcntl(fd, fcntl.F_SETLK, op)
    return subprocess.call(cmd_list)
项目:NFStest    作者:thombashi    | 项目源码 | 文件源码
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)
项目:mkbackup-btrfs    作者:xundeenergie    | 项目源码 | 文件源码
def lock(self, cmd, owner, **kw):
            #return -EROFS
            # The code here is much rather just a demonstration of the locking
            # API than something which actually was seen to be useful.

            # Advisory file locking is pretty messy in Unix, and the Python
            # interface to this doesn't make it better.
            # We can't do fcntl(2)/F_GETLK from Python in a platfrom independent
            # way. The following implementation *might* work under Linux. 
            #
            # if cmd == fcntl.F_GETLK:
            #     import struct
            # 
            #     lockdata = struct.pack('hhQQi', kw['l_type'], os.SEEK_SET,
            #                            kw['l_start'], kw['l_len'], kw['l_pid'])
            #     ld2 = fcntl.fcntl(self.fd, fcntl.F_GETLK, lockdata)
            #     flockfields = ('l_type', 'l_whence', 'l_start', 'l_len', 'l_pid')
            #     uld2 = struct.unpack('hhQQi', ld2)
            #     res = {}
            #     for i in xrange(len(uld2)):
            #          res[flockfields[i]] = uld2[i]
            #  
            #     return fuse.Flock(**res)

            # Convert fcntl-ish lock parameters to Python's weird
            # lockf(3)/flock(2) medley locking API...
            op = { fcntl.F_UNLCK : fcntl.LOCK_UN,
                   fcntl.F_RDLCK : fcntl.LOCK_SH,
                   fcntl.F_WRLCK : fcntl.LOCK_EX }[kw['l_type']]
            if cmd == fcntl.F_GETLK:
                return -EOPNOTSUPP
            elif cmd == fcntl.F_SETLK:
                if op != fcntl.LOCK_UN:
                    op |= fcntl.LOCK_NB
            elif cmd == fcntl.F_SETLKW:
                pass
            else:
                return -EINVAL

            fcntl.lockf(self.fd, op, kw['l_start'], kw['l_len'])