mercurial/win32.py
author Martin von Zweigbergk <martinvonz@google.com>
Wed, 26 Nov 2014 10:25:27 -0800
changeset 23448 6a92b5fcfba8
parent 21226 4898c37e03c0
child 24418 a2285e2fc949
permissions -rw-r--r--
merge: display modify/delete conflict prompts in sorted order The order is determined by manifest.diff(), which currently is not sorted. There are currently no tests for this, but we will soon add some that would be flaky without this patch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     1
# win32.py - utility functions that use win32 API
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     2
#
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     3
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     4
#
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10219
diff changeset
     6
# GNU General Public License version 2 or any later version.
8227
0a9542703300 turn some comments back into module docstrings
Martin Geisler <mg@lazybytes.net>
parents: 8226
diff changeset
     7
16807
80142f385af9 win32: move lookupreg() to windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 16806
diff changeset
     8
import ctypes, errno, os, subprocess, random
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
     9
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    10
_kernel32 = ctypes.windll.kernel32
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    11
_advapi32 = ctypes.windll.advapi32
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    12
_user32 = ctypes.windll.user32
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    13
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    14
_BOOL = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    15
_WORD = ctypes.c_ushort
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    16
_DWORD = ctypes.c_ulong
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    17
_UINT = ctypes.c_uint
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    18
_LONG = ctypes.c_long
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    19
_LPCSTR = _LPSTR = ctypes.c_char_p
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    20
_HANDLE = ctypes.c_void_p
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    21
_HWND = _HANDLE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    22
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    23
_INVALID_HANDLE_VALUE = _HANDLE(-1).value
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    24
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    25
# GetLastError
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    26
_ERROR_SUCCESS = 0
21193
07f9825865de win32: add missing definition of _ERROR_NO_MORE_FILES caught by pyflakes
Yuya Nishihara <yuya@tcha.org>
parents: 20528
diff changeset
    27
_ERROR_NO_MORE_FILES = 18
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    28
_ERROR_INVALID_PARAMETER = 87
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    29
_ERROR_INSUFFICIENT_BUFFER = 122
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    30
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    31
# WPARAM is defined as UINT_PTR (unsigned type)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    32
# LPARAM is defined as LONG_PTR (signed type)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    33
if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    34
    _WPARAM = ctypes.c_ulong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    35
    _LPARAM = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    36
elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    37
    _WPARAM = ctypes.c_ulonglong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    38
    _LPARAM = ctypes.c_longlong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    39
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    40
class _FILETIME(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    41
    _fields_ = [('dwLowDateTime', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    42
                ('dwHighDateTime', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    43
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    44
class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    45
    _fields_ = [('dwFileAttributes', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    46
                ('ftCreationTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    47
                ('ftLastAccessTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    48
                ('ftLastWriteTime', _FILETIME),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    49
                ('dwVolumeSerialNumber', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    50
                ('nFileSizeHigh', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    51
                ('nFileSizeLow', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    52
                ('nNumberOfLinks', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    53
                ('nFileIndexHigh', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    54
                ('nFileIndexLow', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    55
18959
2f6418d8a4c9 check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents: 18175
diff changeset
    56
# CreateFile
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    57
_FILE_SHARE_READ = 0x00000001
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    58
_FILE_SHARE_WRITE = 0x00000002
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    59
_FILE_SHARE_DELETE = 0x00000004
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    60
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    61
_OPEN_EXISTING = 3
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    62
17006
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
    63
_FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
    64
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    65
# SetFileAttributes
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    66
_FILE_ATTRIBUTE_NORMAL = 0x80
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
    67
_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    68
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    69
# Process Security and Access Rights
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    70
_PROCESS_QUERY_INFORMATION = 0x0400
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    71
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    72
# GetExitCodeProcess
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    73
_STILL_ACTIVE = 259
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    74
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    75
class _STARTUPINFO(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    76
    _fields_ = [('cb', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    77
                ('lpReserved', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    78
                ('lpDesktop', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    79
                ('lpTitle', _LPSTR),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    80
                ('dwX', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    81
                ('dwY', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    82
                ('dwXSize', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    83
                ('dwYSize', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    84
                ('dwXCountChars', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    85
                ('dwYCountChars', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    86
                ('dwFillAttribute', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    87
                ('dwFlags', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    88
                ('wShowWindow', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    89
                ('cbReserved2', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    90
                ('lpReserved2', ctypes.c_char_p),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    91
                ('hStdInput', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    92
                ('hStdOutput', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    93
                ('hStdError', _HANDLE)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    94
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    95
class _PROCESS_INFORMATION(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    96
    _fields_ = [('hProcess', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    97
                ('hThread', _HANDLE),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    98
                ('dwProcessId', _DWORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    99
                ('dwThreadId', _DWORD)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   100
17050
e780fb37168b win32: specify _CREATE_NO_WINDOW on spawndetached()
Adrian Buehlmann <adrian@cadifra.com>
parents: 17006
diff changeset
   101
_CREATE_NO_WINDOW = 0x08000000
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   102
_SW_HIDE = 0
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   103
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   104
class _COORD(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   105
    _fields_ = [('X', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   106
                ('Y', ctypes.c_short)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   107
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   108
class _SMALL_RECT(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   109
    _fields_ = [('Left', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   110
                ('Top', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   111
                ('Right', ctypes.c_short),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   112
                ('Bottom', ctypes.c_short)]
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   113
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   114
class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   115
    _fields_ = [('dwSize', _COORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   116
                ('dwCursorPosition', _COORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   117
                ('wAttributes', _WORD),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   118
                ('srWindow', _SMALL_RECT),
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   119
                ('dwMaximumWindowSize', _COORD)]
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   120
14344
e1db8a00188b win32.py: more explicit definition of _STD_ERROR_HANDLE
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
   121
_STD_ERROR_HANDLE = _DWORD(-12).value
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   122
20425
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   123
# CreateToolhelp32Snapshot, Process32First, Process32Next
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   124
_TH32CS_SNAPPROCESS = 0x00000002
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   125
_MAX_PATH = 260
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   126
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   127
class _tagPROCESSENTRY32(ctypes.Structure):
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   128
    _fields_ = [('dwsize', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   129
                ('cntUsage', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   130
                ('th32ProcessID', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   131
                ('th32DefaultHeapID', ctypes.c_void_p),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   132
                ('th32ModuleID', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   133
                ('cntThreads', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   134
                ('th32ParentProcessID', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   135
                ('pcPriClassBase', _LONG),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   136
                ('dwFlags', _DWORD),
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   137
                ('szExeFile', ctypes.c_char * _MAX_PATH)]
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   138
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   139
    def __init__(self):
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   140
        super(_tagPROCESSENTRY32, self).__init__()
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   141
        self.dwsize = ctypes.sizeof(self)
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   142
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   143
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   144
# types of parameters of C functions used (required by pypy)
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   145
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   146
_kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD, ctypes.c_void_p,
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   147
    _DWORD, _DWORD, _HANDLE]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   148
_kernel32.CreateFileA.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   149
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   150
_kernel32.GetFileInformationByHandle.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   151
_kernel32.GetFileInformationByHandle.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   152
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   153
_kernel32.CloseHandle.argtypes = [_HANDLE]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   154
_kernel32.CloseHandle.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   155
15095
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   156
try:
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   157
    _kernel32.CreateHardLinkA.argtypes = [_LPCSTR, _LPCSTR, ctypes.c_void_p]
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   158
    _kernel32.CreateHardLinkA.restype = _BOOL
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   159
except AttributeError:
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   160
    pass
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   161
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   162
_kernel32.SetFileAttributesA.argtypes = [_LPCSTR, _DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   163
_kernel32.SetFileAttributesA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   164
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   165
_kernel32.OpenProcess.argtypes = [_DWORD, _BOOL, _DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   166
_kernel32.OpenProcess.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   167
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   168
_kernel32.GetExitCodeProcess.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   169
_kernel32.GetExitCodeProcess.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   170
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   171
_kernel32.GetLastError.argtypes = []
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   172
_kernel32.GetLastError.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   173
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   174
_kernel32.GetModuleFileNameA.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   175
_kernel32.GetModuleFileNameA.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   176
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   177
_kernel32.CreateProcessA.argtypes = [_LPCSTR, _LPCSTR, ctypes.c_void_p,
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   178
    ctypes.c_void_p, _BOOL, _DWORD, ctypes.c_void_p, _LPCSTR, ctypes.c_void_p,
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   179
    ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   180
_kernel32.CreateProcessA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   181
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   182
_kernel32.ExitProcess.argtypes = [_UINT]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   183
_kernel32.ExitProcess.restype = None
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   184
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   185
_kernel32.GetCurrentProcessId.argtypes = []
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   186
_kernel32.GetCurrentProcessId.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   187
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   188
_SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD)
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   189
_kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   190
_kernel32.SetConsoleCtrlHandler.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   191
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   192
_kernel32.GetStdHandle.argtypes = [_DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   193
_kernel32.GetStdHandle.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   194
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   195
_kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   196
_kernel32.GetConsoleScreenBufferInfo.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   197
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   198
_advapi32.GetUserNameA.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   199
_advapi32.GetUserNameA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   200
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   201
_user32.GetWindowThreadProcessId.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   202
_user32.GetWindowThreadProcessId.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   203
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   204
_user32.ShowWindow.argtypes = [_HANDLE, ctypes.c_int]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   205
_user32.ShowWindow.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   206
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   207
_WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM)
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   208
_user32.EnumWindows.argtypes = [_WNDENUMPROC, _LPARAM]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   209
_user32.EnumWindows.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   210
20425
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   211
_kernel32.CreateToolhelp32Snapshot.argtypes = [_DWORD, _DWORD]
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   212
_kernel32.CreateToolhelp32Snapshot.restype = _BOOL
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   213
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   214
_kernel32.Process32First.argtypes = [_HANDLE, ctypes.c_void_p]
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   215
_kernel32.Process32First.restype = _BOOL
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   216
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   217
_kernel32.Process32Next.argtypes = [_HANDLE, ctypes.c_void_p]
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   218
_kernel32.Process32Next.restype = _BOOL
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   219
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   220
def _raiseoserror(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   221
    err = ctypes.WinError()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   222
    raise OSError(err.errno, '%s: %s' % (name, err.strerror))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   223
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   224
def _getfileinfo(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   225
    fh = _kernel32.CreateFileA(name, 0,
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   226
            _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
17006
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   227
            None, _OPEN_EXISTING, _FILE_FLAG_BACKUP_SEMANTICS, None)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   228
    if fh == _INVALID_HANDLE_VALUE:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   229
        _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   230
    try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   231
        fi = _BY_HANDLE_FILE_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   232
        if not _kernel32.GetFileInformationByHandle(fh, ctypes.byref(fi)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   233
            _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   234
        return fi
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   235
    finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   236
        _kernel32.CloseHandle(fh)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   237
14235
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14230
diff changeset
   238
def oslink(src, dst):
13976
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   239
    try:
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   240
        if not _kernel32.CreateHardLinkA(dst, src, None):
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   241
            _raiseoserror(src)
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   242
    except AttributeError: # Wine doesn't support this function
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   243
        _raiseoserror(src)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   244
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   245
def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   246
    '''return number of hardlinks for the given file'''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   247
    return _getfileinfo(name).nNumberOfLinks
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   248
17006
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   249
def samefile(path1, path2):
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   250
    '''Returns whether path1 and path2 refer to the same file or directory.'''
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   251
    res1 = _getfileinfo(path1)
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   252
    res2 = _getfileinfo(path2)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   253
    return (res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   254
        and res1.nFileIndexHigh == res2.nFileIndexHigh
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   255
        and res1.nFileIndexLow == res2.nFileIndexLow)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
   256
17006
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   257
def samedevice(path1, path2):
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   258
    '''Returns whether path1 and path2 are on the same device.'''
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   259
    res1 = _getfileinfo(path1)
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
   260
    res2 = _getfileinfo(path2)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   261
    return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
   262
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   263
def testpid(pid):
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   264
    '''return True if pid is still running or unable to
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   265
    determine, False otherwise'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   266
    h = _kernel32.OpenProcess(_PROCESS_QUERY_INFORMATION, False, pid)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   267
    if h:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   268
        try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   269
            status = _DWORD()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   270
            if _kernel32.GetExitCodeProcess(h, ctypes.byref(status)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   271
                return status.value == _STILL_ACTIVE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   272
        finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   273
            _kernel32.CloseHandle(h)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   274
    return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   275
14236
e949a008999d rename util.executable_path to executablepath
Adrian Buehlmann <adrian@cadifra.com>
parents: 14235
diff changeset
   276
def executablepath():
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   277
    '''return full path of hg.exe'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   278
    size = 600
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   279
    buf = ctypes.create_string_buffer(size + 1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   280
    len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   281
    if len == 0:
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15095
diff changeset
   282
        raise ctypes.WinError
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   283
    elif len == size:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   284
        raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER)
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   285
    return buf.value
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   286
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   287
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   288
    '''return name of current user'''
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   289
    size = _DWORD(300)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   290
    buf = ctypes.create_string_buffer(size.value + 1)
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   291
    if not _advapi32.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)):
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15095
diff changeset
   292
        raise ctypes.WinError
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   293
    return buf.value
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   294
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   295
_signalhandler = []
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   296
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   297
def setsignalhandler():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   298
    '''Register a termination handler for console events including
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   299
    CTRL+C. python signal handlers do not work well with socket
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   300
    operations.
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   301
    '''
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   302
    def handler(event):
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   303
        _kernel32.ExitProcess(1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   304
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   305
    if _signalhandler:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   306
        return # already registered
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   307
    h = _SIGNAL_HANDLER(handler)
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   308
    _signalhandler.append(h) # needed to prevent garbage collection
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   309
    if not _kernel32.SetConsoleCtrlHandler(h, True):
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15095
diff changeset
   310
        raise ctypes.WinError
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   311
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   312
def hidewindow():
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10219
diff changeset
   313
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   314
    def callback(hwnd, pid):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   315
        wpid = _DWORD()
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   316
        _user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid))
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   317
        if pid == wpid.value:
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   318
            _user32.ShowWindow(hwnd, _SW_HIDE)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   319
            return False # stop enumerating windows
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   320
        return True
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   321
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   322
    pid = _kernel32.GetCurrentProcessId()
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   323
    _user32.EnumWindows(_WNDENUMPROC(callback), pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   324
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12401
diff changeset
   325
def termwidth():
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   326
    # cmd.exe does not handle CR like a unix console, the CR is
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   327
    # counted in the line length. On 80 columns consoles, if 80
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   328
    # characters are written, the following CR won't apply on the
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   329
    # current line but on the new one. Keep room for it.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   330
    width = 79
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   331
    # Query stderr to avoid problems with redirections
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   332
    screenbuf = _kernel32.GetStdHandle(
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   333
                  _STD_ERROR_HANDLE) # don't close the handle returned
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   334
    if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   335
        return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   336
    csbi = _CONSOLE_SCREEN_BUFFER_INFO()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   337
    if not _kernel32.GetConsoleScreenBufferInfo(
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   338
                        screenbuf, ctypes.byref(csbi)):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   339
        return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   340
    width = csbi.srWindow.Right - csbi.srWindow.Left
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   341
    return width
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   342
20425
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   343
def _1stchild(pid):
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   344
    '''return the 1st found child of the given pid
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   345
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   346
    None is returned when no child is found'''
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   347
    pe = _tagPROCESSENTRY32()
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   348
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   349
    # create handle to list all processes
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   350
    ph = _kernel32.CreateToolhelp32Snapshot(_TH32CS_SNAPPROCESS, 0)
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   351
    if ph == _INVALID_HANDLE_VALUE:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   352
        raise ctypes.WinError
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   353
    try:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   354
        r = _kernel32.Process32First(ph, ctypes.byref(pe))
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   355
        # loop over all processes
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   356
        while r:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   357
            if pe.th32ParentProcessID == pid:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   358
                # return first child found
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   359
                return pe.th32ProcessID
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   360
            r = _kernel32.Process32Next(ph, ctypes.byref(pe))
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   361
    finally:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   362
        _kernel32.CloseHandle(ph)
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   363
    if _kernel32.GetLastError() != _ERROR_NO_MORE_FILES:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   364
        raise ctypes.WinError
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   365
    return None # no child found
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   366
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   367
class _tochildpid(int): # pid is _DWORD, which always matches in an int
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   368
    '''helper for spawndetached, returns the child pid on conversion to string
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   369
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   370
    Does not resolve the child pid immediately because the child may not yet be
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   371
    started.
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   372
    '''
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   373
    def childpid(self):
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   374
        '''returns the child pid of the first found child of the process
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   375
        with this pid'''
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   376
        return _1stchild(self)
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   377
    def __str__(self):
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   378
        # run when the pid is written to the file
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   379
        ppid = self.childpid()
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   380
        if ppid is None:
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   381
            # race, child has exited since check
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   382
            # fall back to this pid. Its process will also have disappeared,
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   383
            # raising the same error type later as when the child pid would
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   384
            # be returned.
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   385
            return " %d" % self
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   386
        return str(ppid)
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   387
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   388
def spawndetached(args):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   389
    # No standard library function really spawns a fully detached
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   390
    # process under win32 because they allocate pipes or other objects
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   391
    # to handle standard streams communications. Passing these objects
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   392
    # to the child process requires handle inheritance to be enabled
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   393
    # which makes really detached processes impossible.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   394
    si = _STARTUPINFO()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   395
    si.cb = ctypes.sizeof(_STARTUPINFO)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   396
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   397
    pi = _PROCESS_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   398
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   399
    env = ''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   400
    for k in os.environ:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   401
        env += "%s=%s\0" % (k, os.environ[k])
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   402
    if not env:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   403
        env = '\0'
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   404
    env += '\0'
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   405
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   406
    args = subprocess.list2cmdline(args)
17428
72803c8edaa4 avoid using abbreviations that look like spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17051
diff changeset
   407
    # Not running the command in shell mode makes Python 2.6 hang when
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   408
    # writing to hgweb output socket.
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   409
    comspec = os.environ.get("COMSPEC", "cmd.exe")
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   410
    args = comspec + " /c " + args
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   411
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   412
    res = _kernel32.CreateProcessA(
17050
e780fb37168b win32: specify _CREATE_NO_WINDOW on spawndetached()
Adrian Buehlmann <adrian@cadifra.com>
parents: 17006
diff changeset
   413
        None, args, None, None, False, _CREATE_NO_WINDOW,
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   414
        env, os.getcwd(), ctypes.byref(si), ctypes.byref(pi))
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   415
    if not res:
16687
e34106fa0dc3 cleanup: "raise SomeException()" -> "raise SomeException"
Brodie Rao <brodie@sf.io>
parents: 15095
diff changeset
   416
        raise ctypes.WinError
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   417
20425
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   418
    # _tochildpid because the process is the child of COMSPEC
ca6aa8362f33 win32: spawndetached returns pid of detached process and not of cmd.exe
Simon Heimberg <simohe@besonet.ch>
parents: 19159
diff changeset
   419
    return _tochildpid(pi.dwProcessId)
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   420
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   421
def unlink(f):
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   422
    '''try to implement POSIX' unlink semantics on Windows'''
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   423
21226
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   424
    if os.path.isdir(f):
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   425
        # use EPERM because it is POSIX prescribed value, even though
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   426
        # unlink(2) on directories returns EISDIR on Linux
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   427
        raise IOError(errno.EPERM,
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   428
                      "Unlinking directory not permitted: '%s'" % f)
19159
1b329f8c7b24 windows: check target type before actual unlinking to follow POSIX semantics
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18959
diff changeset
   429
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   430
    # POSIX allows to unlink and rename open files. Windows has serious
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   431
    # problems with doing that:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   432
    # - Calling os.unlink (or os.rename) on a file f fails if f or any
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   433
    #   hardlinked copy of f has been opened with Python's open(). There is no
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   434
    #   way such a file can be deleted or renamed on Windows (other than
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   435
    #   scheduling the delete or rename for the next reboot).
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   436
    # - Calling os.unlink on a file that has been opened with Mercurial's
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   437
    #   posixfile (or comparable methods) will delay the actual deletion of
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   438
    #   the file for as long as the file is held open. The filename is blocked
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   439
    #   during that time and cannot be used for recreating a new file under
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   440
    #   that same name ("zombie file"). Directories containing such zombie files
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   441
    #   cannot be removed or moved.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   442
    # A file that has been opened with posixfile can be renamed, so we rename
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   443
    # f to a random temporary name before calling os.unlink on it. This allows
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   444
    # callers to recreate f immediately while having other readers do their
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   445
    # implicit zombie filename blocking on a temporary name.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   446
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   447
    for tries in xrange(10):
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   448
        temp = '%s-%08x' % (f, random.randint(0, 0xffffffff))
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   449
        try:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   450
            os.rename(f, temp)  # raises OSError EEXIST if temp exists
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   451
            break
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   452
        except OSError, e:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   453
            if e.errno != errno.EEXIST:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   454
                raise
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   455
    else:
18175
fd3f8b87b682 win32: clean up use of two-argument raise
Augie Fackler <raf@durin42.com>
parents: 17428
diff changeset
   456
        raise IOError(errno.EEXIST, "No usable temporary filename found")
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   457
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   458
    try:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   459
        os.unlink(temp)
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   460
    except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   461
        # The unlink might have failed because the READONLY attribute may heave
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   462
        # been set on the original file. Rename works fine with READONLY set,
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   463
        # but not os.unlink. Reset all attributes and try again.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   464
        _kernel32.SetFileAttributesA(temp, _FILE_ATTRIBUTE_NORMAL)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   465
        try:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   466
            os.unlink(temp)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   467
        except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   468
            # The unlink might have failed due to some very rude AV-Scanners.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   469
            # Leaking a tempfile is the lesser evil than aborting here and
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   470
            # leaving some potentially serious inconsistencies.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   471
            pass
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   472
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   473
def makedir(path, notindexed):
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   474
    os.mkdir(path)
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   475
    if notindexed:
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   476
        _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)