本文整理汇总了Python中win32file._get_osfhandle函数的典型用法代码示例。如果您正苦于以下问题:Python _get_osfhandle函数的具体用法?Python _get_osfhandle怎么用?Python _get_osfhandle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_get_osfhandle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ntfsID
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
示例2: make_file_sparse
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)
示例3: perform_sendfile
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,
)
示例4: set_lock
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
示例5: __win32_lock_fd
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
示例6: get_allocated_regions
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
示例7: acquire
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)
示例8: release
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)
示例9: lock_file
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
示例10: unlock
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
示例11: open_and_lock
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)
示例12: lock
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
示例13: unlock_and_close
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
示例14: flock
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]))
示例15: __lock_exclusive
def __lock_exclusive(file):
file_handle = __win32file._get_osfhandle(file.fileno())
__win32file.LockFileEx(
file_handle,
__win32con.LOCKFILE_EXCLUSIVE_LOCK,
0,
-65536,
__overlapped
)