def ntfsID(f): target = open(f, 'rb') id = str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[4]) + \ str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[8]) + \ str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[9]) return id
def start_process(self): global should_ignore_ctrl_c global createProcess_cmdline assert self.hProcess is None assert self.hThread is None assert self.pid is None assert self.is_starting == False self.is_starting = True # use main process stdout and stderr startup_info = win32process.STARTUPINFO() startup_info.hStdOutput = win32file._get_osfhandle(sys.stdout.fileno()) startup_info.hStdError = win32file._get_osfhandle(sys.stderr.fileno()) startup_info.hStdInput = win32file._get_osfhandle(sys.stdin.fileno()) startup_info.dwFlags = win32process.STARTF_USESTDHANDLES self.hProcess, self.hThread, self.pid, self.dwThreadId = t = win32process.CreateProcess( None, createProcess_cmdline, None, None, 1, 0, None, workingDirectory, startup_info) try: hRemoteThread, remoteThreadId = win32process.CreateRemoteThread(self.hProcess, None, 0, exitThread, -1, 0) logger.info("hRemote: %s %s" % (hRemoteThread, remoteThreadId)) except pywintypes.error as e: print(e) if e.winerror == 5: # (5, 'CreateRemoteThread', 'Access is denied.') # if process exists before we make to create remote thread self.is_starting = False return else: raise logger.debug("### wait #123") ret = win32event.WaitForMultipleObjects( [hRemoteThread, self.hProcess], False, win32event.INFINITE ) event_i = ret - win32event.WAIT_OBJECT_0 if event_i == 0: logger.debug("### hRemoteThread was executed") pass elif event_i == 1: # process terminated (unexpectedly) logger.debug("### WAIT OVER: process terminated (unexpectedly)") self.post_terminate() else: raise Exception("unexpected ret or event_id", ret, event_i) self.is_starting = False
def acquire(self): # 给文件上锁 if fcntl: fcntl.flock(self.handle, LOCK_EX) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(hfile, LOCK_EX, 0, -0x10000, overlapped)
def release(self): # 文件解锁 if fcntl: fcntl.flock(self.handle, fcntl.LOCK_UN) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped)
def get_allocated_regions(path, f=None, begin=0, length=None): supported = get_sparse_files_support(path) if not supported: return if os.name == 'nt': if not os.path.exists(path): return False if f is None: f = file(path, 'r') handle = win32file._get_osfhandle(f.fileno()) if length is None: length = os.path.getsize(path) - begin a = SparseSet() interval = 10000000 i = begin end = begin + length while i < end: d = struct.pack("<QQ", i, interval) try: r = win32file.DeviceIoControl(handle, FSCTL_QUERY_ALLOCATED_RANGES, d, interval, None) except pywintypes.error, e: # I've seen: # error: (1784, 'DeviceIoControl', 'The supplied user buffer is not valid for the requested operation.') return for c in xrange(0, len(r), 16): qq = struct.unpack("<QQ", r[c:c + 16]) b = qq[0] e = b + qq[1] a.add(b, e) i += interval return a
def make_file_sparse(path, f, length=0): supported = get_sparse_files_support(path) if not supported: return handle = win32file._get_osfhandle(f.fileno()) _sparse_magic(handle, length)
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 __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 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 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 _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 test_basics(self): s = socket.socket() e = win32event.CreateEvent(None, 1, 0, None) win32file.WSAEventSelect(s, e, 0) self.assertEquals(win32file.WSAEnumNetworkEvents(s), {}) self.assertEquals(win32file.WSAEnumNetworkEvents(s, e), {}) self.assertRaises(TypeError, win32file.WSAEnumNetworkEvents, s, e, 3) self.assertRaises(TypeError, win32file.WSAEnumNetworkEvents, s, "spam") self.assertRaises(TypeError, win32file.WSAEnumNetworkEvents, "spam", e) self.assertRaises(TypeError, win32file.WSAEnumNetworkEvents, "spam") f = open("NUL") h = win32file._get_osfhandle(f.fileno()) self.assertRaises(win32file.error, win32file.WSAEnumNetworkEvents, h) self.assertRaises(win32file.error, win32file.WSAEnumNetworkEvents, s, h) try: win32file.WSAEnumNetworkEvents(h) except win32file.error as e: self.assertEquals(e.winerror, win32file.WSAENOTSOCK) try: win32file.WSAEnumNetworkEvents(s, h) except win32file.error as e: # According to the docs it would seem reasonable that # this would fail with WSAEINVAL, but it doesn't. self.assertEquals(e.winerror, win32file.WSAENOTSOCK)
def perform_sendfile(act, overlapped): return ( win32file.TransmitFile( act.sock, win32file._get_osfhandle(act.file_handle.fileno()), act.length or 0, act.blocksize, overlapped, 0 ), 0, )
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 unlock(file_): try: savepos = file_.tell() if savepos: file_.seek(0) try: msvcrt.locking(file_.fileno(), constants.LOCK_UN, lock_length) except IOError as exc_value: if exc_value.strerror == 'Permission denied': hfile = win32file._get_osfhandle(file_.fileno()) try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: if exc_value.winerror == winerror.ERROR_NOT_LOCKED: # error: (158, 'UnlockFileEx', # 'The segment is already unlocked.') # To match the 'posix' implementation, silently # ignore this error pass else: # Q: Are there exceptions/codes we should be # dealing with here? raise else: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror)
def test_basics(self): s = socket.socket() e = win32event.CreateEvent(None, 1, 0, None) win32file.WSAEventSelect(s, e, 0) assert win32file.WSAEnumNetworkEvents(s) == {} assert win32file.WSAEnumNetworkEvents(s, e) == {} with pytest.raises(TypeError): win32file.WSAEnumNetworkEvents(s, e, 3) with pytest.raises(TypeError): win32file.WSAEnumNetworkEvents(s, "spam") with pytest.raises(TypeError): win32file.WSAEnumNetworkEvents("spam", e) with pytest.raises(TypeError): win32file.WSAEnumNetworkEvents("spam") f = open("NUL") h = win32file._get_osfhandle(f.fileno()) with pytest.raises(win32file.error): win32file.WSAEnumNetworkEvents(h) with pytest.raises(win32file.error): win32file.WSAEnumNetworkEvents(s, h) try: win32file.WSAEnumNetworkEvents(h) except win32file.error as e: assert e.winerror == win32file.WSAENOTSOCK try: win32file.WSAEnumNetworkEvents(s, h) except win32file.error as e: # According to the docs it would seem reasonable that # this would fail with WSAEINVAL, but it doesn't. assert e.winerror == win32file.WSAENOTSOCK
def get_allocated_regions(path, f=None, begin=0, length=None): supported = get_sparse_files_support(path) if not supported: return if os.name == 'nt': if not os.path.exists(path): return False if f is None: f = file(path, 'r') handle = win32file._get_osfhandle(f.fileno()) if length is None: length = os.path.getsize(path) - begin a = SparseSet() interval = 10000000 i = begin end = begin + length while i < end: d = struct.pack("<QQ", i, interval) try: r = win32file.DeviceIoControl(handle, FSCTL_QUERY_ALLOCATED_RANGES, d, interval, None) except pywintypes.error, e: # I've seen: # error: (1784, 'DeviceIoControl', 'The supplied user buffer is not valid for the requested operation.') return for c in xrange(0, len(r), 16): qq = struct.unpack("<QQ", r[c:c+16]) b = qq[0] e = b + qq[1] a.add(b, e) i += interval return a
def unlock(file): try: hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(hfile, 0, 0x7fffffff, __overlapped) except pywintypes.error, e: # err 120 is unimplemented call, happens on win9x: if e.args[0] != 120: raise e
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 unlock(self): if fcntl: fcntl.flock(self.handle, fcntl.LOCK_UN) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, _OVERLAPPED) self.is_locked = False LOG.vv(f'UNLOCK PID: {os.getpid()}')
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 flock(file, flags): hfile = _get_osfhandle(file.fileno()) try: if flags & LOCK_UN: UnlockFileEx(hfile, 0, -0x10000, __overlapped) else: LockFileEx(hfile, flags, 0, -0x10000, __overlapped) except WinIOError, exc: raise IOError('[Errno %d] %s' % (exc[0], exc[2]))
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 __lock_exclusive(file): file_handle = __win32file._get_osfhandle(file.fileno()) __win32file.LockFileEx( file_handle, __win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -65536, __overlapped )
def unlock_and_close(self): """Close and unlock the file using the win32 primitive.""" if self._locked: try: hfile = win32file._get_osfhandle(self._fh.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) except pywintypes.error, e: if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: raise
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 unlock(file): """Try UnlockFileEx first, then move to (Win95, Win98, WinME) solutions.""" hfile = win32file._get_osfhandle(file.fileno()) try: win32file.UnlockFileEx(hfile, 0, FFFF0000, __overlapped) except win32file.error, e: import winerror if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: raise e win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0)
def unlock(adRotateHandler, file): """used to be in ad.py.""" dsh_utils.db_print('ryw_xp:unlock() entered...', 49) ad = adRotateHandler ad.highbits=-0x7fff0000 ad.ov=pywintypes.OVERLAPPED() ad.hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(ad.hfile,0,ad.highbits,ad.ov) #remove locks ad.hfile.Close()
def __init__(self, parent, fname): self.parent = parent with open(path.join(parent.folder, fname), "rb") as fp: handle = win32file._get_osfhandle(fp.fileno()) info = win32file.GetFileInformationByHandle(handle) uid = str((info[8] << 32) + info[9]) self.uid = uid self.info = self.parent.getInfoByUid(self.uid) or [0b0, fname, "", ""]
def __init__(self, fp): """Create win32 lock. :param fp: File :type fp: file or io.IOBase """ super(BaseWin32FileLock, self).__init__(fp) # Retrieve win32 file handle self.handle = win32file._get_osfhandle(self.fp.fileno())
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 _do_release(self, path): hfile = win32file._get_osfhandle(self.fd.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) self.fd.close() self.fd = None if self.delete_lock_file: try: os.unlink(path) except WindowsError: pass
def _LockImplWin(target_file, flags): hfile = win32file._get_osfhandle(target_file.fileno()) try: win32file.LockFileEx(hfile, flags, 0, -0x10000, _OVERLAPPED) except pywintypes.error, 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 unlock_file(f): while True: try: if platform.system() == PLATFORM_WINDOWS: hfile = win32file._get_osfhandle(f.fileno()) win32file.UnlockFileEx(hfile, 0, 0, 0xffff0000, pywintypes.OVERLAPPED()) else: fcntl.flock(f, fcntl.LOCK_UN) break except: time.sleep(0.1)
def unlock(self): if getattr(self, 'has_lock', False): try: hfile = win32file._get_osfhandle(self.fh.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, _overlapped) self.fh.close() del self.fh os.unlink(self.lock_fname) except: pass self.has_lock = False
def release(self): try: win32file.UnlockFileEx( win32file._get_osfhandle(self._file.fileno()), 0, -0x10000, pywintypes.OVERLAPPED()) except pywintypes.error as e: # Do not fail unlocking unlocked file. if e[0] == 158: pass else: raise
def release(self): # 文件解锁 if os.name == 'nt': import win32file import pywintypes overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped) elif os.name == 'posix': import fcntl fcntl.flock(self.handle, fcntl.LOCK_UN)
def unlock(self) : if fcntl : fcntl.flock(self.fp, fcntl.LOCK_UN) else : hfile = win32file._get_osfhandle(self.fp.fileno()) win32file.UnlockFileEx(hfile, 0, FileLock.highbits, overlapped) try : self.fp.close() self.fp = None except : pass
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 __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 lockf(fd, operation): file_handle = win32file._get_osfhandle(fd.fileno()) if operation == LOCK_UN: try: win32file.UnlockFileEx(file_handle, 0, -0x7fff0000, overlapped) except pywintypes.error, e: if e[0] == 158: # Ignore error that happens when you unlock an already # unlocked file. pass else: raise
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: # Q: Are there exceptions/codes we should be dealing with here? raise
def release_lock(self): """ Make sure that we do not retain an exclusive lock on self.file. """ if self.has_lock: if os.name == 'nt': win32file.UnlockFileEx( win32file._get_osfhandle(self.file.fileno()), 0, -65536, pywintypes.OVERLAPPED()) else: fcntl.flock(self.file, fcntl.LOCK_UN) self.has_lock = False
def __init__(self, handle): from twisted.internet import reactor self.reactor = reactor self.handle = handle self.osfhandle = win32file._get_osfhandle(self.handle.fileno()) self.mode = self.handle.mode # CloseHandle automatically calls CancelIo self.close = self.handle.close self.fileno = self.handle.fileno self.read_op = ReadFileOp() self.write_op = WriteFileOp() self.readbuf = self.reactor.AllocateReadBuffer(self.buffer_size)
def unlock(file): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) except pywintypes.error, exc_value: if exc_value[0] == 158: # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') # To match the 'posix' implementation, silently ignore this error pass else: # Q: Are there exceptions/codes we should be dealing with here? raise
def nt_lock(file_, flags): # pragma: no cover 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.winerror == winerror.ERROR_LOCK_VIOLATION: raise LockException(LockException.LOCK_FAILED, exc_value.strerror) else: # Q: Are there exceptions/codes we should be dealing with # here? raise
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 unlock(self): LOGGER_LOCK.debug("unlock") if self.fd == None: LOGGER_LOCK.debug("already unlocked") return wfd = win32file._get_osfhandle(self.fd.fileno()) try: # pylint: disable-msg=E1101 win32file.UnlockFile(wfd, 0 , 0, 0xffff, 0) self.fd.close() self.fd = None except win32file.error, exc: if exc[0] != 158: raise
def inodeForWin(self, file_path): """ File ID for NTFS Returns the complete file ID as a single long string (volume number, high index, low index) @param file_path: File Path @return: """ try:self.Fixity = SharedApp.SharedApp.App except:pass id_node = '' try: target = open(file_path, 'rb') except: try: target = open(file_path.decode('utf-8'), 'rb') except: self.Fixity.logger.LogException(Exception.message) pass pass try: id_node = str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[4]) + \ str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[8]) + \ str(win32file.GetFileInformationByHandle(win32file._get_osfhandle(target.fileno()))[9]) return id_node except: self.Fixity.logger.LogException(Exception.message) pass return id_node