mercurial/win32.py
changeset 24652 232bf0028596
parent 24494 f2b87f4856bf
child 25660 328739ea70c3
equal deleted inserted replaced
24651:67241ee427cf 24652:232bf0028596
     3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
     3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 import ctypes, errno, os, subprocess, random
     8 import ctypes, errno, msvcrt, os, subprocess, random
     9 
     9 
    10 _kernel32 = ctypes.windll.kernel32
    10 _kernel32 = ctypes.windll.kernel32
    11 _advapi32 = ctypes.windll.advapi32
    11 _advapi32 = ctypes.windll.advapi32
    12 _user32 = ctypes.windll.user32
    12 _user32 = ctypes.windll.user32
    13 
    13 
    24 
    24 
    25 # GetLastError
    25 # GetLastError
    26 _ERROR_SUCCESS = 0
    26 _ERROR_SUCCESS = 0
    27 _ERROR_NO_MORE_FILES = 18
    27 _ERROR_NO_MORE_FILES = 18
    28 _ERROR_INVALID_PARAMETER = 87
    28 _ERROR_INVALID_PARAMETER = 87
       
    29 _ERROR_BROKEN_PIPE = 109
    29 _ERROR_INSUFFICIENT_BUFFER = 122
    30 _ERROR_INSUFFICIENT_BUFFER = 122
    30 
    31 
    31 # WPARAM is defined as UINT_PTR (unsigned type)
    32 # WPARAM is defined as UINT_PTR (unsigned type)
    32 # LPARAM is defined as LONG_PTR (signed type)
    33 # LPARAM is defined as LONG_PTR (signed type)
    33 if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
    34 if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
   209 _user32.EnumWindows.restype = _BOOL
   210 _user32.EnumWindows.restype = _BOOL
   210 
   211 
   211 _kernel32.CreateToolhelp32Snapshot.argtypes = [_DWORD, _DWORD]
   212 _kernel32.CreateToolhelp32Snapshot.argtypes = [_DWORD, _DWORD]
   212 _kernel32.CreateToolhelp32Snapshot.restype = _BOOL
   213 _kernel32.CreateToolhelp32Snapshot.restype = _BOOL
   213 
   214 
       
   215 _kernel32.PeekNamedPipe.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD,
       
   216     ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
       
   217 _kernel32.PeekNamedPipe.restype = _BOOL
       
   218 
   214 _kernel32.Process32First.argtypes = [_HANDLE, ctypes.c_void_p]
   219 _kernel32.Process32First.argtypes = [_HANDLE, ctypes.c_void_p]
   215 _kernel32.Process32First.restype = _BOOL
   220 _kernel32.Process32First.restype = _BOOL
   216 
   221 
   217 _kernel32.Process32Next.argtypes = [_HANDLE, ctypes.c_void_p]
   222 _kernel32.Process32Next.argtypes = [_HANDLE, ctypes.c_void_p]
   218 _kernel32.Process32Next.restype = _BOOL
   223 _kernel32.Process32Next.restype = _BOOL
   257 def samedevice(path1, path2):
   262 def samedevice(path1, path2):
   258     '''Returns whether path1 and path2 are on the same device.'''
   263     '''Returns whether path1 and path2 are on the same device.'''
   259     res1 = _getfileinfo(path1)
   264     res1 = _getfileinfo(path1)
   260     res2 = _getfileinfo(path2)
   265     res2 = _getfileinfo(path2)
   261     return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
   266     return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
       
   267 
       
   268 def peekpipe(pipe):
       
   269     handle = msvcrt.get_osfhandle(pipe.fileno())
       
   270     avail = _DWORD()
       
   271 
       
   272     if not _kernel32.PeekNamedPipe(handle, None, 0, None, ctypes.byref(avail),
       
   273                                    None):
       
   274         err = _kernel32.GetLastError()
       
   275         if err == _ERROR_BROKEN_PIPE:
       
   276             return 0
       
   277         raise ctypes.WinError(err)
       
   278 
       
   279     return avail.value
   262 
   280 
   263 def testpid(pid):
   281 def testpid(pid):
   264     '''return True if pid is still running or unable to
   282     '''return True if pid is still running or unable to
   265     determine, False otherwise'''
   283     determine, False otherwise'''
   266     h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)
   284     h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)