def __win32_lock_fd(fd, timeout=None): '''returns True if the file descriptor is successfully locked''' import pywintypes import win32con import win32file import winerror try: handle = win32file._get_osfhandle(fd) if timeout is None: win32file.LockFileEx(handle, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, pywintypes.OVERLAPPED()) return True if timeout > 0: start = time.time() while True: try: win32file.LockFileEx(handle, win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY, 0, -0x10000, pywintypes.OVERLAPPED()) return True except pywintypes.error as e: if e.winerror != winerror.ERROR_LOCK_VIOLATION: break time.sleep(0.05) if time.time() > start + timeout: break else: win32file.LockFileEx(handle, win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY, 0, -0x10000, pywintypes.OVERLAPPED()) return True except pywintypes.error: pass return False
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 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: 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 as 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)))
def _locker(file_descriptor): """Given a file descriptor, it locks. .. note:: This function asserts if we are not on Windows. :param file_descriptor: a file descriptor, opened with `os.open()` """ assert os.name == 'nt', 'This fixture can only be used on Windows' # This should run on Windows, but the linter runs on Ubuntu where these modules # do not exist. Therefore, ignore errors in this function. # pylint: disable=import-error import win32file import pywintypes import win32con winfd = win32file._get_osfhandle(file_descriptor) # pylint: disable=protected-access mode = win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY overlapped = pywintypes.OVERLAPPED() # additional parameters # int : nbytesLow - low-order part of number of bytes to lock # int : nbytesHigh - high-order part of number of bytes to lock # ol=None : PyOVERLAPPED - An overlapped structure # after the first two params: reserved, and nNumberOfBytesToLock # then, overlapped win32file.LockFileEx(winfd, mode, 0, -0x10000, overlapped)
def set_lock(fname): """ Try to lock file and write PID. Return the status of operation. """ global fh fh = open(fname, 'w') if os.name == 'nt': # Code for NT systems got from: 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 # is there any reason not to reuse the following structure? __overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(fh.fileno()) try: win32file.LockFileEx(hfile, LOCK_EX | LOCK_NB, 0, -0x10000, __overlapped) except pywintypes.error, exc_value: # error: (33, 'LockFileEx', 'The process cannot access # the file because another process has locked a portion # of the file.') if exc_value[0] == 33: return False
def acquire(self): # 给文件上锁 if fcntl: fcntl.flock(self.handle, LOCK_EX | LOCK_NB) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(hfile, LOCK_EX, 0, -0x10000, overlapped)
def lock(self): if self.type['LOCK_EX']: # exclusive locking if self.type['LOCK_NB']: # don't wait, non-blocking lock_flags = win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY else: # wait for lock to free lock_flags = win32con.LOCKFILE_EXCLUSIVE_LOCK else: # shared locking if self.type['LOCK_NB']: # don't wait, non-blocking lock_flags = win32con.LOCKFILE_FAIL_IMMEDIATELY else: # shared lock wait for lock to free lock_flags = 0 self.ov = pywintypes.OVERLAPPED() # used to indicate starting region to lock win32file.LockFileEx(self.hfile, lock_flags, 0, self.highbits, self.ov)
def lock_file(file_descriptor): """ Cross-platform utility function for obtaining an exclusive lock on the given open file. Based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203. @param file_descriptor: open file descriptor @type file_descriptor: file @raise IOError: if the file cannot be locked """ if on_posix(): import fcntl fcntl.lockf(file_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB) else: import pywintypes, win32con, win32file # pylint: disable-msg=F0401 file_handle = win32file._get_osfhandle(file_descriptor.fileno()) # pylint: disable-msg=W0212 flags = \ win32con.LOCKFILE_EXCLUSIVE_LOCK|win32con.LOCKFILE_FAIL_IMMEDIATELY try: win32file.LockFileEx(file_handle, flags, 0, -0x10000, pywintypes.OVERLAPPED()) # pylint: disable-msg=E1101 except win32file.error, oError: if oError.args[0] == 33: # somebody else (partly) locked the file raise IOError(*oError.args) # pylint: disable-msg=W0142 else: raise # unexpected error code
def lock(self): if fcntl: fcntl.flock(self.handle, fcntl.LOCK_EX) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(hfile, 2, 0, -0x10000, _OVERLAPPED) self.is_locked = True LOG.vv(f'LOCK PID: {os.getpid()}')
def lock(file): try: hfile = win32file._get_osfhandle(file.fileno()) win32file.LockFileEx(hfile, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, 0x7fffffff, __overlapped) except pywintypes.error, e: # err 120 is unimplemented call, happens on win9x: if e.args[0] != 120: raise e
def open_and_lock(self, timeout, delay): """Open the file and lock it. Args: timeout: float, How long to try to lock for. delay: float, How long to wait between retries Raises: AlreadyLockedException: if the lock is already acquired. IOError: if the open fails. CredentialsFileSymbolicLinkError: if the file is a symbolic link. """ if self._locked: raise AlreadyLockedException('File %s is already locked' % self._filename) start_time = time.time() validate_file(self._filename) try: self._fh = open(self._filename, self._mode) except IOError as e: # If we can't access with _mode, try _fallback_mode # and don't lock. if e.errno == errno.EACCES: self._fh = open(self._filename, self._fallback_mode) return # We opened in _mode, try to lock the file. while True: try: hfile = win32file._get_osfhandle(self._fh.fileno()) win32file.LockFileEx(hfile, (win32con.LOCKFILE_FAIL_IMMEDIATELY | win32con.LOCKFILE_EXCLUSIVE_LOCK), 0, -0x10000, pywintypes.OVERLAPPED()) self._locked = True return except pywintypes.error as e: if timeout == 0: raise # If the error is not that the file is already # in use, raise. if e[0] != _Win32Opener.FILE_IN_USE_ERROR: raise # We could not acquire the lock. Try again. if (time.time() - start_time) >= timeout: logger.warn('Could not lock %s in %s seconds' % (self._filename, timeout)) if self._fh: self._fh.close() self._fh = open(self._filename, self._fallback_mode) return time.sleep(delay)
def lock(file, flags): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped) except pywintypes.error as exc: if exc.winerror == 33 and exc.funcname == "LockFileEx": raise BlockingIOError(-1, exc.strerror, file.name, exc.winerror) else: raise
def _LockImplWin(target_file, flags): hfile = win32file._get_osfhandle(target_file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, -0x10000, _OVERLAPPED) except pywintypes.error as exc_value: if exc_value[0] == 33: raise LockException('Error trying acquiring lock of %s: %s' % (target_file.name, exc_value[2])) else: raise
def acquire(self): __overlapped = pywintypes.OVERLAPPED() try: mylog("trying to lock the lockfile:%s" % self.filename) win32file.LockFileEx(self.handle, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, __overlapped) except Exception as e: print(e) return False return True
def lock(file, flags): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) except pywintypes.error, exc_value: # error: (33, 'LockFileEx', 'The process cannot access the file because another process has locked a portion of the file.') if exc_value[0] == 33: raise LockException(LockException.LOCK_FAILED, exc_value[2]) else: # Q: Are there exceptions/codes we should be dealing with here? raise
def lockf(fd, flags, length=0xFFFF0000, start=0, whence=0): overlapped = pywintypes.OVERLAPPED() hfile = msvcrt.get_osfhandle(fd.fileno()) if LOCK_UN & flags: ret = win32file.UnlockFileEx(hfile, 0, start, length, overlapped) else: try: ret = win32file.LockFileEx(hfile, flags, start, length, overlapped) except: raise IOError(errno.EAGAIN, "", "") return ret
def LockFileEx(handle, dwFlags, nbytesLow, nbytesHigh, overlapped): # warning - pywin32 expects dwords as signed integer, so that -1 <-> max_int nbytesLow = _utilities.unsigned_to_signed(nbytesLow) nbytesHigh = _utilities.unsigned_to_signed(nbytesHigh) result = win32file.LockFileEx( handle, # HANDLE hFile dwFlags, # DWORD dwFlags nbytesLow, # DWORD nNumberOfBytesToLockLow nbytesHigh, # DWORD nNumberOfBytesToLockHigh overlapped # lpOverlapped )
def __lock(self, breakOnFailed): lockType = win32con.LOCKFILE_EXCLUSIVE_LOCK if breakOnFailed: lockType = lockType | win32con.LOCKFILE_FAIL_IMMEDIATELY hfile = win32file._get_osfhandle(self.__file.fileno()) try: win32file.LockFileEx(hfile, lockType, 0, 0xf0, FileLocker.__overlapped) except pywintypes.error: self.__file.close() self.__file = None raise HasBeenLockedError("Locker has been acquired before")
def save(self): self.log.debug('save()') root = lxml.etree.Element( "QubesVmCollection", default_template=str(self.default_template_qid) \ if self.default_template_qid is not None else "None", default_netvm=str(self.default_netvm_qid) \ if self.default_netvm_qid is not None else "None", default_fw_netvm=str(self.default_fw_netvm_qid) \ if self.default_fw_netvm_qid is not None else "None", updatevm=str(self.updatevm_qid) \ if self.updatevm_qid is not None else "None", clockvm=str(self.clockvm_qid) \ if self.clockvm_qid is not None else "None", default_kernel=str(self.default_kernel) \ if self.default_kernel is not None else "None", ) for vm in self.values(): element = vm.create_xml_element() if element is not None: root.append(element) tree = lxml.etree.ElementTree(root) try: new_store_file = tempfile.NamedTemporaryFile( prefix=self.qubes_store_filename, delete=False) if os.name == 'posix': fcntl.lockf(new_store_file, fcntl.LOCK_EX) elif os.name == 'nt': overlapped = pywintypes.OVERLAPPED() win32file.LockFileEx( win32file._get_osfhandle(new_store_file.fileno()), win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped) tree.write(new_store_file, encoding="UTF-8", pretty_print=True) new_store_file.flush() os.chmod(new_store_file.name, 0660) os.chown(new_store_file.name, -1, grp.getgrnam('qubes').gr_gid) os.rename(new_store_file.name, self.qubes_store_filename) self.qubes_store_file.close() self.qubes_store_file = new_store_file except EnvironmentError as err: print("{0}: export error: {1}".format( os.path.basename(sys.argv[0]), err)) return False return True
def lock(file, flags): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, nNumberOfBytesToLockHigh, __overlapped) except pywintypes.error as exc_value: # error: (33, 'LockFileEx', 'The process cannot access the file because another process has locked a # portion of the file.') if exc_value.winerror == 33: raise LockException(str(exc_value)) else: raise # Q: Are there exceptions/codes we should be dealing with here?
def lock_file(f): while True: try: if platform.system() == PLATFORM_WINDOWS: hfile = win32file._get_osfhandle(f.fileno()) win32file.LockFileEx(hfile, win32con.LOCKFILE_FAIL_IMMEDIATELY | win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, 0xffff0000, pywintypes.OVERLAPPED()) else: fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) break except: time.sleep(0.1)
def acquire(self): # 给文件上锁 if os.name == 'nt': import win32con import win32file import pywintypes LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(hfile, LOCK_EX, 0, -0x10000, overlapped) elif os.name == 'posix': import fcntl LOCK_EX = fcntl.LOCK_EX fcntl.flock(self.handle, LOCK_EX)
def _getLock(self): self.fh = open(self.lock_fname, 'w') flags = LOCK_EX | LOCK_NB try: hfile = win32file._get_osfhandle(self.fh.fileno()) win32file.LockFileEx(hfile, flags, 0, -0x10000, _overlapped) #print >> sys.stderr, "Acquired" return True except: #print >> sys.stderr, sys.exc_info() #sys.stderr.flush() self.fh.close() del self.fh return False
def _lock(self, filename, openmode, lockmode): self._open(filename, openmode) self.hfile = msvcrt.get_osfhandle(self.f.fileno()) overlapped = pywintypes.OVERLAPPED() try: win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped) except pywintypes.error, e: self._clear_f() if e.args[0] in (winerror.ERROR_LOCK_VIOLATION, ): raise errors.LockContention(filename) ## import pdb; pdb.set_trace() raise
def _lock_file(f): import os if os.name == 'nt': import win32con import win32file import pywintypes __overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(f.fileno()) win32file.LockFileEx(hfile, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, __overlapped) elif os.name == 'posix': import fcntl fcntl.flock(f, fcntl.LOCK_EX) else: raise OSError("Unsupported OS")
def _lock_impl(file, exclusive, nonblocking): if exclusive: flags = win32con.LOCKFILE_EXCLUSIVE_LOCK else: flags = 0 if nonblocking: flags |= win32con.LOCKFILE_FAIL_IMMEDIATELY hfile = win32file._get_osfhandle(file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: # error: (33, "LockFileEx", "The process cannot access the file because another process has locked a portion of the file.") if exc_value[0] == 33: raise Lock_Failed() else: # Q: Are there exceptions/codes we should be dealing with here? raise
def lock_db_for_writing(self): if self.qubes_store_file is not None: raise QubesException("lock already taken") # save() would rename the file over qubes.xml, _then_ release lock, # so we need to ensure that the file for which we've got the lock is # still the right file self.log.debug('lock_db_for_writing()') while True: self.qubes_store_file = open (self.qubes_store_filename, 'r+') if os.name == 'posix': fcntl.lockf (self.qubes_store_file, fcntl.LOCK_EX) elif os.name == 'nt': overlapped = pywintypes.OVERLAPPED() win32file.LockFileEx(win32file._get_osfhandle(self.qubes_store_file.fileno()), win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped) if os.fstat(self.qubes_store_file.fileno()) == os.stat( self.qubes_store_filename): break self.qubes_store_file.close()
def check_lock(self): if fcntl: try: print "fcntl" fcntl.flock(self.handle, LOCK_EX | LOCK_NB) return True except: return False else: try: print self.filename cfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(cfile, LOCK_EX, 0, -0x10000, overlapped) self.handle.write("test") return True except Exception as e: print e return False
def obtain_lock(self): """ Make sure that we have an exclusive lock on self.file before doing a write. If the lock is not available, raise an exception. """ assert not self.is_readonly() if not self.has_lock: if os.name == 'nt': try: win32file.LockFileEx( win32file._get_osfhandle(self.file.fileno()), (win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY), 0, -65536, pywintypes.OVERLAPPED()) except pywintypes.error: raise IOError("Unable to obtain lock") else: fcntl.flock(self.file, fcntl.LOCK_EX | fcntl.LOCK_NB) self.has_lock = True
def __enter__(self): try: self.f = open(self.filepath, 'wb') except: raise RuntimeError("File Path Error") try: if os.name == 'nt': __overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(self.f.fileno()) win32file.LockFileEx( hfile, win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY, 0, 0xffff0000, __overlapped) elif os.name == 'posix': fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) except: raise SystemExit('Process Running') return self
def _acquire_lock(self, for_save=False): assert self.__locked_fh is None, 'double lock' while True: try: fd = os.open(self._store, os.O_RDWR | (os.O_CREAT * int(for_save))) except FileNotFoundError: if not for_save: raise qubes.exc.QubesException( 'Qubes XML store {!r} is missing; ' 'use qubes-create tool'.format(self._store)) raise # While we were waiting for lock, someone could have unlink()ed # (or rename()d) our file out of the filesystem. We have to # ensure we got lock on something linked to filesystem. # If not, try again. if os.fstat(fd) != os.stat(self._store): os.close(fd) continue if self.__load_timestamp and \ os.path.getmtime(self._store) != self.__load_timestamp: os.close(fd) raise qubes.exc.QubesException( 'Someone else modified qubes.xml in the meantime') break if os.name == 'posix': fcntl.lockf(fd, fcntl.LOCK_EX) elif os.name == 'nt': # pylint: disable=protected-access overlapped = pywintypes.OVERLAPPED() win32file.LockFileEx( win32file._get_osfhandle(fd), win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped) self.__locked_fh = os.fdopen(fd, 'r+b') return self.__locked_fh