예제 #1
0
 def __init__(self, name=None):
     super(WinFileSysMonitor, self).__init__(name)
     self.defaultMask = FILESYSTEM_MONITOR_MASK
     self.buf_size = 0x100
     self.comKey = 0
     self.watchList = []
     self.ioComPort = None
     self.overlapped = OVERLAPPED()
     self.overlapped.hEvent = CreateEvent(None, 0, 0, None)
     self.__lock = threading.Lock()
예제 #2
0
    def __LockNT(self, num_LockFlags, num_Start, num_End):
        # Lock the file on the NT platform.  Locking files
        # on NT isn't as simple as using flock() so we have
        # to use a more complex method to lock files.

        # Firstly, we must get the actual filehandle that
        # the OS uses.
        if hasattr(self, "num_OSHandle") == False:
            self.__num_OSHandle = _get_osfhandle(self.fileno())

        # Before we go about splitting the numbers up, we have
        # to set defaults.  Given 0 for both the start and the end,
        # we assume that the user wants to lock the whole file,
        # because locking no part of the file would be pointless.
        if num_Start == num_End == 0:
            num_End = 2**64 - 1

        # Now, we have to split the byte range end into high
        # and low bits because that's how LockFileEx takes it.
        num_BytesToLock = num_End - num_Start
        num_BytesToLockHigh, num_BytesToLockLow = SplitNumber(num_BytesToLock)

        # We also have to split up the offset of the file and then
        # put it in a PyOVERLAPPED structure.  Why Microsoft chose
        # to use an OVERLAPPED structure for file locking I don't know;
        # e-mail me if you find out why.
        num_OffsetHigh, num_OffsetLow = SplitNumber(num_Start)
        self.__OverlappedObject = OVERLAPPED()
        self.__OverlappedObject.OffsetHigh = num_OffsetHigh
        self.__OverlappedObject.Offset = num_OffsetLow

        # Now that we have all of these things we can try to lock the file.
        try:
            LockFileEx(self.__num_OSHandle, num_LockFlags, num_BytesToLockLow,
                       num_BytesToLockHigh, self.__OverlappedObject)
        except error, e:
            self.__LastException = e
            raise LockError, e.args
예제 #3
0
 def HttpExtensionProc(self, control_block):
     overlapped = OVERLAPPED()
     overlapped.object = control_block
     PostQueuedCompletionStatus(self.io_req_port, 0, ISAPI_REQUEST,
                                overlapped)
     return isapicon.HSE_STATUS_PENDING
예제 #4
0
 def HttpExtensionProc(self, control_block):
     overlapped = OVERLAPPED()
     overlapped.object = control_block
     PostQueuedCompletionStatus(self.io_req_port, 0, ISAPI_REQUEST, overlapped)
     return isapicon.HSE_STATUS_PENDING
예제 #5
0
https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch04s25.html.

TODO: Consider using improved implementation:
https://github.com/WoLpH/portalocker/blob/develop/portalocker/portalocker.py
"""
import os

# Needs win32all to work on Windows.
if os.name == 'nt':
    import win32con
    import win32file
    from pywintypes import OVERLAPPED
    LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
    LOCK_SH = 0  # the default
    LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
    __overlapped = OVERLAPPED()

    def lock(file, flags):
        hfile = win32file._get_osfhandle(file.fileno())
        try:
            win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
        except win32file.error as err:
            raise IOError(*err.args)

    def unlock(file):
        hfile = win32file._get_osfhandle(file.fileno())
        win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped)

elif os.name == 'posix':
    import fcntl
    from fcntl import LOCK_EX, LOCK_SH, LOCK_NB  # noqa