mercurial/win32.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 17 Oct 2024 15:21:20 -0400
changeset 52114 7332eae326de
parent 51863 f4733654f144
permissions -rw-r--r--
tests: force `dumbhttp.py` to write its log file with '\n' on Windows This wasn't causing obvious test failures, but it's the same fix as dbd2d56224d1 for `dummysmtpd.py`, and there's no sense in leaving this problem lying around. (And upon further review, it might have been causing some non-obviously related failures- see the next commit.)
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
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 46784
diff changeset
     3
# Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others
8226
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
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 51720
diff changeset
     8
from __future__ import annotations
25994
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
import ctypes
35549
94a127152e25 win32: allocate buffer of maximum length for GetVolumeInformation()
Yuya Nishihara <yuya@tcha.org>
parents: 35512
diff changeset
    11
import ctypes.wintypes as wintypes
25994
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    12
import errno
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
import msvcrt
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    14
import os
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    15
import random
d6beeb618700 win32: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    16
import subprocess
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    17
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    18
from typing import (
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    19
    List,
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    20
    NoReturn,
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    21
    Optional,
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    22
    Tuple,
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    23
)
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
    24
30667
5861bdbeb9a3 py3: use pycompat.getcwd instead of os.getcwd
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30637
diff changeset
    25
from . import (
5861bdbeb9a3 py3: use pycompat.getcwd instead of os.getcwd
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30637
diff changeset
    26
    encoding,
5861bdbeb9a3 py3: use pycompat.getcwd instead of os.getcwd
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30637
diff changeset
    27
    pycompat,
5861bdbeb9a3 py3: use pycompat.getcwd instead of os.getcwd
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30637
diff changeset
    28
)
30637
344e68882cd3 py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30314
diff changeset
    29
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
    30
# pytype: disable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    31
_kernel32 = ctypes.windll.kernel32
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    32
_advapi32 = ctypes.windll.advapi32
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    33
_user32 = ctypes.windll.user32
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
    34
_crypt32 = ctypes.windll.crypt32
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
    35
# pytype: enable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    36
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    37
_BOOL = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    38
_WORD = ctypes.c_ushort
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    39
_DWORD = ctypes.c_ulong
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    40
_UINT = ctypes.c_uint
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    41
_LONG = ctypes.c_long
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    42
_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
    43
_HANDLE = ctypes.c_void_p
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    44
_HWND = _HANDLE
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
    45
_PCCERT_CONTEXT = ctypes.c_void_p
35549
94a127152e25 win32: allocate buffer of maximum length for GetVolumeInformation()
Yuya Nishihara <yuya@tcha.org>
parents: 35512
diff changeset
    46
_MAX_PATH = wintypes.MAX_PATH
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    47
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
    48
_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
    49
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    50
# GetLastError
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    51
_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
    52
_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
    53
_ERROR_INVALID_PARAMETER = 87
24652
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
    54
_ERROR_BROKEN_PIPE = 109
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    55
_ERROR_INSUFFICIENT_BUFFER = 122
38533
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
    56
_ERROR_NO_DATA = 232
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    57
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    58
# 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
    59
# 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
    60
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
    61
    _WPARAM = ctypes.c_ulong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    62
    _LPARAM = ctypes.c_long
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    63
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
    64
    _WPARAM = ctypes.c_ulonglong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    65
    _LPARAM = ctypes.c_longlong
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    66
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
    67
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    68
class _FILETIME(ctypes.Structure):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    69
    _fields_ = [('dwLowDateTime', _DWORD), ('dwHighDateTime', _DWORD)]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
    70
13375
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
class _BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
    73
    _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    74
        ('dwFileAttributes', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    75
        ('ftCreationTime', _FILETIME),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    76
        ('ftLastAccessTime', _FILETIME),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    77
        ('ftLastWriteTime', _FILETIME),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    78
        ('dwVolumeSerialNumber', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    79
        ('nFileSizeHigh', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    80
        ('nFileSizeLow', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    81
        ('nNumberOfLinks', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    82
        ('nFileIndexHigh', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    83
        ('nFileIndexLow', _DWORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
    84
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
    85
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    86
18959
2f6418d8a4c9 check-code: catch trailing space in comments
Mads Kiilerich <madski@unity3d.com>
parents: 18175
diff changeset
    87
# CreateFile
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    88
_FILE_SHARE_READ = 0x00000001
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    89
_FILE_SHARE_WRITE = 0x00000002
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    90
_FILE_SHARE_DELETE = 0x00000004
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    91
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    92
_OPEN_EXISTING = 3
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
    93
17006
6fc7fd72ba3e win32.py: let samefile and samedevice work on directories too
Adrian Buehlmann <adrian@cadifra.com>
parents: 16807
diff changeset
    94
_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
    95
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    96
# SetFileAttributes
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    97
_FILE_ATTRIBUTE_NORMAL = 0x80
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
    98
_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
    99
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   100
# Process Security and Access Rights
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   101
_PROCESS_QUERY_INFORMATION = 0x0400
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   102
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   103
# GetExitCodeProcess
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   104
_STILL_ACTIVE = 259
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   105
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   106
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   107
class _STARTUPINFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   108
    _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   109
        ('cb', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   110
        ('lpReserved', _LPSTR),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   111
        ('lpDesktop', _LPSTR),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   112
        ('lpTitle', _LPSTR),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   113
        ('dwX', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   114
        ('dwY', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   115
        ('dwXSize', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   116
        ('dwYSize', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   117
        ('dwXCountChars', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   118
        ('dwYCountChars', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   119
        ('dwFillAttribute', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   120
        ('dwFlags', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   121
        ('wShowWindow', _WORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   122
        ('cbReserved2', _WORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   123
        ('lpReserved2', ctypes.c_char_p),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   124
        ('hStdInput', _HANDLE),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   125
        ('hStdOutput', _HANDLE),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   126
        ('hStdError', _HANDLE),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   127
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   128
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   129
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   130
class _PROCESS_INFORMATION(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   131
    _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   132
        ('hProcess', _HANDLE),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   133
        ('hThread', _HANDLE),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   134
        ('dwProcessId', _DWORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   135
        ('dwThreadId', _DWORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   136
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   137
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   138
17050
e780fb37168b win32: specify _CREATE_NO_WINDOW on spawndetached()
Adrian Buehlmann <adrian@cadifra.com>
parents: 17006
diff changeset
   139
_CREATE_NO_WINDOW = 0x08000000
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   140
_SW_HIDE = 0
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   141
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   142
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   143
class _COORD(ctypes.Structure):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   144
    _fields_ = [('X', ctypes.c_short), ('Y', ctypes.c_short)]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   145
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   146
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   147
class _SMALL_RECT(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   148
    _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   149
        ('Left', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   150
        ('Top', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   151
        ('Right', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   152
        ('Bottom', ctypes.c_short),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   153
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   154
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   155
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   156
class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   157
    _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   158
        ('dwSize', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   159
        ('dwCursorPosition', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   160
        ('wAttributes', _WORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   161
        ('srWindow', _SMALL_RECT),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   162
        ('dwMaximumWindowSize', _COORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   163
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   164
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   165
32664
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   166
_STD_OUTPUT_HANDLE = _DWORD(-11).value
14344
e1db8a00188b win32.py: more explicit definition of _STD_ERROR_HANDLE
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
   167
_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
   168
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   169
# CERT_TRUST_STATUS dwErrorStatus
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   170
CERT_TRUST_IS_PARTIAL_CHAIN = 0x10000
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   171
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   172
# CertCreateCertificateContext encodings
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   173
X509_ASN_ENCODING = 0x00000001
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   174
PKCS_7_ASN_ENCODING = 0x00010000
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   175
51703
ca7bde5dbafb black: format the codebase with 23.3.0
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
   176
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   177
# These structs are only complete enough to achieve what we need.
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   178
class CERT_CHAIN_CONTEXT(ctypes.Structure):
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   179
    _fields_ = (
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   180
        ("cbSize", _DWORD),
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   181
        # CERT_TRUST_STATUS struct
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   182
        ("dwErrorStatus", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   183
        ("dwInfoStatus", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   184
        ("cChain", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   185
        ("rgpChain", ctypes.c_void_p),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   186
        ("cLowerQualityChainContext", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   187
        ("rgpLowerQualityChainContext", ctypes.c_void_p),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   188
        ("fHasRevocationFreshnessTime", _BOOL),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   189
        ("dwRevocationFreshnessTime", _DWORD),
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   190
    )
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   191
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   192
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   193
class CERT_USAGE_MATCH(ctypes.Structure):
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   194
    _fields_ = (
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   195
        ("dwType", _DWORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   196
        # CERT_ENHKEY_USAGE struct
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   197
        ("cUsageIdentifier", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   198
        ("rgpszUsageIdentifier", ctypes.c_void_p),  # LPSTR *
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   199
    )
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   201
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   202
class CERT_CHAIN_PARA(ctypes.Structure):
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   203
    _fields_ = (
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   204
        ("cbSize", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   205
        ("RequestedUsage", CERT_USAGE_MATCH),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   206
        ("RequestedIssuancePolicy", CERT_USAGE_MATCH),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   207
        ("dwUrlRetrievalTimeout", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   208
        ("fCheckRevocationFreshnessTime", _BOOL),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   209
        ("dwRevocationFreshnessTime", _DWORD),
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   210
        ("pftCacheResync", ctypes.c_void_p),  # LPFILETIME
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   211
        ("pStrongSignPara", ctypes.c_void_p),  # PCCERT_STRONG_SIGN_PARA
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   212
        ("dwStrongSignFlags", _DWORD),
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   213
    )
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   214
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   215
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   216
# 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
   217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   218
_crypt32.CertCreateCertificateContext.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   219
    _DWORD,  # cert encoding
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   220
    ctypes.c_char_p,  # cert
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   221
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   222
]  # cert size
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   223
_crypt32.CertCreateCertificateContext.restype = _PCCERT_CONTEXT
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   224
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   225
_crypt32.CertGetCertificateChain.argtypes = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   226
    ctypes.c_void_p,  # HCERTCHAINENGINE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   227
    _PCCERT_CONTEXT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   228
    ctypes.c_void_p,  # LPFILETIME
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   229
    ctypes.c_void_p,  # HCERTSTORE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   230
    ctypes.c_void_p,  # PCERT_CHAIN_PARA
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   231
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   232
    ctypes.c_void_p,  # LPVOID
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   233
    ctypes.c_void_p,  # PCCERT_CHAIN_CONTEXT *
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   234
]
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   235
_crypt32.CertGetCertificateChain.restype = _BOOL
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   236
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   237
_crypt32.CertFreeCertificateContext.argtypes = [_PCCERT_CONTEXT]
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   238
_crypt32.CertFreeCertificateContext.restype = _BOOL
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   239
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   240
_kernel32.CreateFileA.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   241
    _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   242
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   243
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   244
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   245
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   246
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   247
    _HANDLE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   248
]
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   249
_kernel32.CreateFileA.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   250
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   251
_kernel32.GetFileInformationByHandle.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   252
_kernel32.GetFileInformationByHandle.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   253
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   254
_kernel32.CloseHandle.argtypes = [_HANDLE]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   255
_kernel32.CloseHandle.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   256
15095
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   257
try:
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   258
    _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
   259
    _kernel32.CreateHardLinkA.restype = _BOOL
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   260
except AttributeError:
ec222a29bdf0 win32: quietly ignore missing CreateHardLinkA for Wine
Matt Mackall <mpm@selenic.com>
parents: 14345
diff changeset
   261
    pass
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   262
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   263
_kernel32.SetFileAttributesA.argtypes = [_LPCSTR, _DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   264
_kernel32.SetFileAttributesA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   265
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   266
_DRIVE_UNKNOWN = 0
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   267
_DRIVE_NO_ROOT_DIR = 1
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   268
_DRIVE_REMOVABLE = 2
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   269
_DRIVE_FIXED = 3
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   270
_DRIVE_REMOTE = 4
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   271
_DRIVE_CDROM = 5
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   272
_DRIVE_RAMDISK = 6
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   273
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   274
_kernel32.GetDriveTypeA.argtypes = [_LPCSTR]
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   275
_kernel32.GetDriveTypeA.restype = _UINT
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   276
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   277
_kernel32.GetVolumeInformationA.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   278
    _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   279
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   280
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   281
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   282
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   283
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   284
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   285
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   286
]
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   287
_kernel32.GetVolumeInformationA.restype = _BOOL
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   288
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   289
_kernel32.GetVolumePathNameA.argtypes = [_LPCSTR, ctypes.c_void_p, _DWORD]
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   290
_kernel32.GetVolumePathNameA.restype = _BOOL
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   291
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   292
_kernel32.OpenProcess.argtypes = [_DWORD, _BOOL, _DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   293
_kernel32.OpenProcess.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   294
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   295
_kernel32.GetExitCodeProcess.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   296
_kernel32.GetExitCodeProcess.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   297
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   298
_kernel32.GetLastError.argtypes = []
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   299
_kernel32.GetLastError.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   300
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   301
_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
   302
_kernel32.GetModuleFileNameA.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   303
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   304
_kernel32.CreateProcessA.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   305
    _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   306
    _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   307
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   308
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   309
    _BOOL,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   310
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   311
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   312
    _LPCSTR,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   313
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   314
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   315
]
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   316
_kernel32.CreateProcessA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   317
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   318
_kernel32.ExitProcess.argtypes = [_UINT]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   319
_kernel32.ExitProcess.restype = None
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   320
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   321
_kernel32.GetCurrentProcessId.argtypes = []
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   322
_kernel32.GetCurrentProcessId.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   323
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   324
# pytype: disable=module-attr
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   325
_SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD)
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   326
# pytype: enable=module-attr
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   327
_kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   328
_kernel32.SetConsoleCtrlHandler.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   329
32664
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   330
_kernel32.SetConsoleMode.argtypes = [_HANDLE, _DWORD]
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   331
_kernel32.SetConsoleMode.restype = _BOOL
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   332
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   333
_kernel32.GetConsoleMode.argtypes = [_HANDLE, ctypes.c_void_p]
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   334
_kernel32.GetConsoleMode.restype = _BOOL
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   335
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   336
_kernel32.GetStdHandle.argtypes = [_DWORD]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   337
_kernel32.GetStdHandle.restype = _HANDLE
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   338
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   339
_kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   340
_kernel32.GetConsoleScreenBufferInfo.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   341
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   342
_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
   343
_advapi32.GetUserNameA.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   344
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   345
_user32.GetWindowThreadProcessId.argtypes = [_HANDLE, ctypes.c_void_p]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   346
_user32.GetWindowThreadProcessId.restype = _DWORD
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   347
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   348
_user32.ShowWindow.argtypes = [_HANDLE, ctypes.c_int]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   349
_user32.ShowWindow.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   350
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   351
# pytype: disable=module-attr
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   352
_WNDENUMPROC = ctypes.WINFUNCTYPE(_BOOL, _HWND, _LPARAM)
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   353
# pytype: enable=module-attr
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   354
_user32.EnumWindows.argtypes = [_WNDENUMPROC, _LPARAM]
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   355
_user32.EnumWindows.restype = _BOOL
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   356
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   357
_kernel32.PeekNamedPipe.argtypes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   358
    _HANDLE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   359
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   360
    _DWORD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   361
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   362
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   363
    ctypes.c_void_p,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   364
]
24652
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   365
_kernel32.PeekNamedPipe.restype = _BOOL
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   366
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   367
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   368
def _raiseoserror(name: bytes) -> NoReturn:
33419
7c33adc823e0 win32: work around a WinError problem handling HRESULT types
Matt Harbison <matt_harbison@yahoo.com>
parents: 32676
diff changeset
   369
    # Force the code to a signed int to avoid an 'int too large' error.
7c33adc823e0 win32: work around a WinError problem handling HRESULT types
Matt Harbison <matt_harbison@yahoo.com>
parents: 32676
diff changeset
   370
    # See https://bugs.python.org/issue28474
7c33adc823e0 win32: work around a WinError problem handling HRESULT types
Matt Harbison <matt_harbison@yahoo.com>
parents: 32676
diff changeset
   371
    code = _kernel32.GetLastError()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   372
    if code > 0x7FFFFFFF:
51703
ca7bde5dbafb black: format the codebase with 23.3.0
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
   373
        code -= 2**32
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   374
    err = ctypes.WinError(code=code)  # pytype: disable=module-attr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   375
    raise OSError(
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   376
        err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   377
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   378
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   379
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   380
def _getfileinfo(name: bytes) -> _BY_HANDLE_FILE_INFORMATION:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   381
    fh = _kernel32.CreateFileA(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   382
        name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   383
        0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   384
        _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   385
        None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   386
        _OPEN_EXISTING,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   387
        _FILE_FLAG_BACKUP_SEMANTICS,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   388
        None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   389
    )
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   390
    if fh == _INVALID_HANDLE_VALUE:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   391
        _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   392
    try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   393
        fi = _BY_HANDLE_FILE_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   394
        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
   395
            _raiseoserror(name)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   396
        return fi
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   397
    finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   398
        _kernel32.CloseHandle(fh)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   399
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   400
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   401
def checkcertificatechain(cert: bytes, build: bool = True) -> bool:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   402
    """Tests the given certificate to see if there is a complete chain to a
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   403
    trusted root certificate.  As a side effect, missing certificates are
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   404
    downloaded and installed unless ``build=False``.  True is returned if a
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   405
    chain to a trusted root exists (even if built on the fly), otherwise
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   406
    False.  NB: A chain to a trusted root does NOT imply that the certificate
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   407
    is valid.
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   408
    """
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   409
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   410
    chainctxptr = ctypes.POINTER(CERT_CHAIN_CONTEXT)
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   411
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   412
    pchainctx = chainctxptr()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   413
    chainpara = CERT_CHAIN_PARA(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   414
        cbSize=ctypes.sizeof(CERT_CHAIN_PARA), RequestedUsage=CERT_USAGE_MATCH()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   415
    )
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   416
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   417
    certctx = _crypt32.CertCreateCertificateContext(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   418
        X509_ASN_ENCODING, cert, len(cert)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   419
    )
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   420
    if certctx is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   421
        _raiseoserror(b'CertCreateCertificateContext')
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   422
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   423
    flags = 0
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   424
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   425
    if not build:
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   426
        flags |= 0x100  # CERT_CHAIN_DISABLE_AUTH_ROOT_AUTO_UPDATE
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   427
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   428
    try:
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   429
        # Building the certificate chain will update root certs as necessary.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   430
        if not _crypt32.CertGetCertificateChain(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   431
            None,  # hChainEngine
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   432
            certctx,  # pCertContext
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   433
            None,  # pTime
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   434
            None,  # hAdditionalStore
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   435
            ctypes.byref(chainpara),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   436
            flags,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   437
            None,  # pvReserved
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   438
            ctypes.byref(pchainctx),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   439
        ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   440
            _raiseoserror(b'CertGetCertificateChain')
33492
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   441
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   442
        chainctx = pchainctx.contents
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   443
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   444
        return chainctx.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN == 0
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   445
    finally:
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   446
        if pchainctx:
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   447
            _crypt32.CertFreeCertificateChain(pchainctx)
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   448
        _crypt32.CertFreeCertificateContext(certctx)
14af04391fb9 win32: add a method to trigger the Crypto API to complete a certificate chain
Matt Harbison <matt_harbison@yahoo.com>
parents: 33419
diff changeset
   449
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   450
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   451
def oslink(src: bytes, dst: bytes) -> None:
13976
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   452
    try:
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   453
        if not _kernel32.CreateHardLinkA(dst, src, None):
9ca1ff3d4f8c win32: Wine doesn't know about hardlinks
Matt Mackall <mpm@selenic.com>
parents: 13795
diff changeset
   454
            _raiseoserror(src)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   455
    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
   456
        _raiseoserror(src)
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   457
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   458
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   459
def nlinks(name: bytes) -> int:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   460
    '''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
   461
    return _getfileinfo(name).nNumberOfLinks
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   462
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   463
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   464
def samefile(fpath1: bytes, fpath2: bytes) -> bool:
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   465
    '''Returns whether fpath1 and fpath2 refer to the same file or directory.'''
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   466
    res1 = _getfileinfo(fpath1)
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   467
    res2 = _getfileinfo(fpath2)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   468
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   469
        res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   470
        and res1.nFileIndexHigh == res2.nFileIndexHigh
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   471
        and res1.nFileIndexLow == res2.nFileIndexLow
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   472
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   473
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
   474
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   475
def samedevice(fpath1: bytes, fpath2: bytes) -> bool:
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   476
    '''Returns whether fpath1 and fpath2 are on the same device.'''
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   477
    res1 = _getfileinfo(fpath1)
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   478
    res2 = _getfileinfo(fpath2)
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   479
    return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9198
diff changeset
   480
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   481
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   482
def peekpipe(pipe) -> int:
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   483
    handle = msvcrt.get_osfhandle(pipe.fileno())  # pytype: disable=module-attr
24652
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   484
    avail = _DWORD()
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   485
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   486
    if not _kernel32.PeekNamedPipe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   487
        handle, None, 0, None, ctypes.byref(avail), None
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   488
    ):
24652
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   489
        err = _kernel32.GetLastError()
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   490
        if err == _ERROR_BROKEN_PIPE:
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   491
            return 0
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   492
        raise ctypes.WinError(err)  # pytype: disable=module-attr
24652
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   493
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   494
    return avail.value
232bf0028596 win32: add a method to fetch the available pipe data size
Matt Harbison <matt_harbison@yahoo.com>
parents: 24494
diff changeset
   495
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   496
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   497
def lasterrorwaspipeerror(err) -> bool:
38533
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
   498
    if err.errno != errno.EINVAL:
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
   499
        return False
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
   500
    err = _kernel32.GetLastError()
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
   501
    return err == _ERROR_BROKEN_PIPE or err == _ERROR_NO_DATA
3a0f322af192 windows: fix incorrect detection of broken pipe when writing to pager
Sune Foldager <cryo@cyanite.org>
parents: 35550
diff changeset
   502
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   503
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   504
def testpid(pid: int) -> bool:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   505
    """return True if pid is still running or unable to
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   506
    determine, False otherwise"""
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   507
    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
   508
    if h:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   509
        try:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   510
            status = _DWORD()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   511
            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
   512
                return status.value == _STILL_ACTIVE
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   513
        finally:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   514
            _kernel32.CloseHandle(h)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   515
    return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
2054
e18beba54a7e fix exception handling on windows.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   516
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   517
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   518
def executablepath() -> bytes:
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   519
    '''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
   520
    size = 600
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   521
    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
   522
    len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size)
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   523
    # pytype: disable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   524
    if len == 0:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   525
        raise ctypes.WinError()  # Note: WinError is a function
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   526
    elif len == size:
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   527
        raise ctypes.WinError(_ERROR_INSUFFICIENT_BUFFER)
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   528
    # pytype: enable=module-attr
13376
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   529
    return buf.value
60b5c6c3fd12 win32: new function executable_path
Adrian Buehlmann <adrian@cadifra.com>
parents: 13375
diff changeset
   530
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   531
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   532
def getvolumename(path: bytes) -> Optional[bytes]:
35512
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   533
    """Get the mount point of the filesystem from a directory or file
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   534
    (best-effort)
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   535
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   536
    Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   537
    """
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   538
    # realpath() calls GetFullPathName()
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   539
    realpath = os.path.realpath(path)
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   540
35550
ed30934165c9 win32: do not call GetVolumePathName() with the minimum buffer length
Yuya Nishihara <yuya@tcha.org>
parents: 35549
diff changeset
   541
    # allocate at least MAX_PATH long since GetVolumePathName('c:\\', buf, 4)
ed30934165c9 win32: do not call GetVolumePathName() with the minimum buffer length
Yuya Nishihara <yuya@tcha.org>
parents: 35549
diff changeset
   542
    # somehow fails on Windows XP
ed30934165c9 win32: do not call GetVolumePathName() with the minimum buffer length
Yuya Nishihara <yuya@tcha.org>
parents: 35549
diff changeset
   543
    size = max(len(realpath), _MAX_PATH) + 1
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   544
    buf = ctypes.create_string_buffer(size)
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   545
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   546
    if not _kernel32.GetVolumePathNameA(realpath, ctypes.byref(buf), size):
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   547
        # Note: WinError is a function
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   548
        raise ctypes.WinError()  # pytype: disable=module-attr
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   549
35512
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   550
    return buf.value
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   551
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   552
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   553
def getfstype(path: bytes) -> Optional[bytes]:
35512
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   554
    """Get the filesystem type name from a directory or file (best-effort)
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   555
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   556
    Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   557
    """
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   558
    volume = getvolumename(path)
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   559
5cc1becd0493 win32: split a utility function to obtain the volume out of getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
   560
    t = _kernel32.GetDriveTypeA(volume)
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   561
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   562
    if t == _DRIVE_REMOTE:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   563
        return b'cifs'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   564
    elif t not in (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   565
        _DRIVE_REMOVABLE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   566
        _DRIVE_FIXED,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   567
        _DRIVE_CDROM,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   568
        _DRIVE_RAMDISK,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   569
    ):
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   570
        return None
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   571
35549
94a127152e25 win32: allocate buffer of maximum length for GetVolumeInformation()
Yuya Nishihara <yuya@tcha.org>
parents: 35512
diff changeset
   572
    size = _MAX_PATH + 1
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   573
    name = ctypes.create_string_buffer(size)
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   574
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   575
    if not _kernel32.GetVolumeInformationA(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   576
        volume, None, 0, None, None, None, ctypes.byref(name), size
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   577
    ):
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   578
        # Note: WinError is a function
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   579
        raise ctypes.WinError()  # pytype: disable=module-attr
35510
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   580
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   581
    return name.value
2062f7c2ac83 win32: implement util.getfstype()
Matt Harbison <matt_harbison@yahoo.com>
parents: 34022
diff changeset
   582
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   583
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   584
def getuser() -> bytes:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents: 7778
diff changeset
   585
    '''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
   586
    size = _DWORD(300)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   587
    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
   588
    if not _advapi32.GetUserNameA(ctypes.byref(buf), ctypes.byref(size)):
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   589
        raise ctypes.WinError()  # pytype: disable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   590
    return buf.value
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   591
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   592
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   593
_signalhandler: List[_SIGNAL_HANDLER] = []
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   594
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   595
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   596
def setsignalhandler() -> None:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   597
    """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
   598
    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
   599
    operations.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   600
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   601
4672
272c0a09b203 Handle CTRL+C in serve under Windows.
Marcos Chaves <marcos.nospam@gmail.com>
parents: 4407
diff changeset
   602
    def handler(event):
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   603
        _kernel32.ExitProcess(1)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   604
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   605
    if _signalhandler:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   606
        return  # already registered
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   607
    h = _SIGNAL_HANDLER(handler)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   608
    _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
   609
    if not _kernel32.SetConsoleCtrlHandler(h, True):
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   610
        raise ctypes.WinError()  # pytype: disable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   611
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   612
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   613
def hidewindow() -> None:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   614
    def callback(hwnd, pid):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   615
        wpid = _DWORD()
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   616
        _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
   617
        if pid == wpid.value:
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   618
            _user32.ShowWindow(hwnd, _SW_HIDE)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   619
            return False  # stop enumerating windows
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   620
        return True
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   621
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   622
    pid = _kernel32.GetCurrentProcessId()
14345
bf9a105aed0a win32.py: add argtypes and restype
Adrian Buehlmann <adrian@cadifra.com>
parents: 14344
diff changeset
   623
    _user32.EnumWindows(_WNDENUMPROC(callback), pid)
11012
81631f0cf13b win32: detect console width on Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10388
diff changeset
   624
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   625
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   626
def termsize() -> Tuple[int, int]:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   627
    # 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
   628
    # 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
   629
    # 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
   630
    # current line but on the new one. Keep room for it.
30313
392633d7860e scmutil: clarify that we explicitly do termwidth - 1 on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 25994
diff changeset
   631
    width = 80 - 1
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30313
diff changeset
   632
    height = 25
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   633
    # 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
   634
    screenbuf = _kernel32.GetStdHandle(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   635
        _STD_ERROR_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   636
    )  # don't close the handle returned
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   637
    if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE:
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30313
diff changeset
   638
        return width, height
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   639
    csbi = _CONSOLE_SCREEN_BUFFER_INFO()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   640
    if not _kernel32.GetConsoleScreenBufferInfo(screenbuf, ctypes.byref(csbi)):
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30313
diff changeset
   641
        return width, height
30313
392633d7860e scmutil: clarify that we explicitly do termwidth - 1 on Windows
Yuya Nishihara <yuya@tcha.org>
parents: 25994
diff changeset
   642
    width = csbi.srWindow.Right - csbi.srWindow.Left  # don't '+ 1'
30314
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30313
diff changeset
   643
    height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30313
diff changeset
   644
    return width, height
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   645
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   646
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   647
def enablevtmode() -> bool:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   648
    """Enable virtual terminal mode for the associated console.  Return True if
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   649
    enabled, else False."""
32664
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   650
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   651
    ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   652
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   653
    handle = _kernel32.GetStdHandle(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   654
        _STD_OUTPUT_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   655
    )  # don't close the handle
32664
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   656
    if handle == _INVALID_HANDLE_VALUE:
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   657
        return False
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   658
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   659
    mode = _DWORD(0)
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   660
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   661
    if not _kernel32.GetConsoleMode(handle, ctypes.byref(mode)):
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   662
        return False
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   663
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   664
    if (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   665
        mode.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   666
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   667
        if not _kernel32.SetConsoleMode(handle, mode):
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   668
            return False
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   669
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   670
    return True
2d56e6d23be7 win32: add a method to enable ANSI color code processing on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 30667
diff changeset
   671
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   672
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   673
def spawndetached(args: List[bytes]) -> int:
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   674
    # 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
   675
    # 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
   676
    # 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
   677
    # 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
   678
    # 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
   679
    si = _STARTUPINFO()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   680
    si.cb = ctypes.sizeof(_STARTUPINFO)
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   681
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   682
    pi = _PROCESS_INFORMATION()
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   683
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   684
    env = b''
30637
344e68882cd3 py3: replace os.environ with encoding.environ (part 4 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30314
diff changeset
   685
    for k in encoding.environ:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   686
        env += b"%s=%s\0" % (k, encoding.environ[k])
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   687
    if not env:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   688
        env = b'\0'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   689
    env += b'\0'
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   690
39719
255d1885c7f8 py3: resolve Unicode issues around `hg serve` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39611
diff changeset
   691
    args = subprocess.list2cmdline(pycompat.rapply(encoding.strfromlocal, args))
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   692
39719
255d1885c7f8 py3: resolve Unicode issues around `hg serve` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39611
diff changeset
   693
    # TODO: CreateProcessW on py3?
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   694
    res = _kernel32.CreateProcessA(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   695
        None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   696
        encoding.strtolocal(args),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   697
        None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   698
        None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   699
        False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   700
        _CREATE_NO_WINDOW,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   701
        env,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   702
        encoding.getcwd(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   703
        ctypes.byref(si),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   704
        ctypes.byref(pi),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   705
    )
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   706
    if not res:
46784
65f437c240f2 typing: disable a few errors when accessing Windows specific attributes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45942
diff changeset
   707
        raise ctypes.WinError()  # pytype: disable=module-attr
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13374
diff changeset
   708
40928
576474baa209 win32: close the handles associated with a spawned child process
Matt Harbison <matt_harbison@yahoo.com>
parents: 39818
diff changeset
   709
    _kernel32.CloseHandle(pi.hProcess)
576474baa209 win32: close the handles associated with a spawned child process
Matt Harbison <matt_harbison@yahoo.com>
parents: 39818
diff changeset
   710
    _kernel32.CloseHandle(pi.hThread)
576474baa209 win32: close the handles associated with a spawned child process
Matt Harbison <matt_harbison@yahoo.com>
parents: 39818
diff changeset
   711
32676
4c3d9ee87382 win32: drop a py26 daemonizing hack
Matt Harbison <matt_harbison@yahoo.com>
parents: 32664
diff changeset
   712
    return pi.dwProcessId
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   713
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   714
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   715
def unlink(path: bytes) -> None:
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   716
    '''try to implement POSIX' unlink semantics on Windows'''
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   717
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   718
    if os.path.isdir(path):
21226
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   719
        # use EPERM because it is POSIX prescribed value, even though
4898c37e03c0 win32: backout 1a9ebc83a74c
Steve Borho <steve@borho.org>
parents: 21193
diff changeset
   720
        # unlink(2) on directories returns EISDIR on Linux
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   721
        raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   722
            errno.EPERM,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   723
            r"Unlinking directory not permitted: '%s'"
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   724
            % encoding.strfromlocal(path),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   725
        )
19159
1b329f8c7b24 windows: check target type before actual unlinking to follow POSIX semantics
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18959
diff changeset
   726
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   727
    # 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
   728
    # problems with doing that:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   729
    # - 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
   730
    #   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
   731
    #   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
   732
    #   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
   733
    # - 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
   734
    #   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
   735
    #   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
   736
    #   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
   737
    #   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
   738
    #   cannot be removed or moved.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   739
    # 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
   740
    # 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
   741
    # 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
   742
    # implicit zombie filename blocking on a temporary name.
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   743
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48875
diff changeset
   744
    for tries in range(10):
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   745
        temp = b'%s-%08x' % (path, random.randint(0, 0xFFFFFFFF))
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   746
        try:
51720
e618a1756b08 typing: avoid some useless @overload definitions in `mercurial.util`
Matt Harbison <matt_harbison@yahoo.com>
parents: 51703
diff changeset
   747
            os.rename(path, temp)
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   748
            break
49305
53e9422a9b45 py3: catch FileExistsError instead of checking errno == EEXIST
Manuel Jacob <me@manueljacob.de>
parents: 49284
diff changeset
   749
        except FileExistsError:
53e9422a9b45 py3: catch FileExistsError instead of checking errno == EEXIST
Manuel Jacob <me@manueljacob.de>
parents: 49284
diff changeset
   750
            pass
13775
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   751
    else:
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   752
        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
   753
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   754
    try:
930efdc6bfa4 windows: move unlink to win32.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 13379
diff changeset
   755
        os.unlink(temp)
13776
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   756
    except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   757
        # 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
   758
        # 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
   759
        # 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
   760
        _kernel32.SetFileAttributesA(temp, _FILE_ATTRIBUTE_NORMAL)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   761
        try:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   762
            os.unlink(temp)
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   763
        except OSError:
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   764
            # 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
   765
            # 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
   766
            # leaving some potentially serious inconsistencies.
a2f0fdb1e488 win32: remove READONLY attribute on unlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13775
diff changeset
   767
            pass
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   768
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40928
diff changeset
   769
49810
a9faacdc5943 typing: add type hints to mercurial/win32.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 49305
diff changeset
   770
def makedir(path: bytes, notindexed: bool) -> None:
13795
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   771
    os.mkdir(path)
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   772
    if notindexed:
43b5fe18ea6c set NOT_CONTENT_INDEXED on .hg dir (issue2694)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13776
diff changeset
   773
        _kernel32.SetFileAttributesA(path, _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)