-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-windows-nul.py
More file actions
62 lines (46 loc) · 1.62 KB
/
Copy pathdelete-windows-nul.py
File metadata and controls
62 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Delete a Windows 'nul' reserved-name file from the repo root.
Windows treats 'nul' as a device name, so normal deletion fails.
This script uses the Win32 MoveFileW API with the \\?\ prefix to
rename the file first, then deletes the renamed file normally.
Usage: python delete-windows-nul.py
"""
import ctypes
import os
import sys
from ctypes import wintypes
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
NUL_PATH = os.path.join(REPO_ROOT, "nul")
TEMP_NAME = "nul_to_delete"
TEMP_PATH = os.path.join(REPO_ROOT, TEMP_NAME)
def make_unc(path: str) -> str:
"""Convert a path to \\\\?\\ extended-length format."""
return "\\\\?\\" + os.path.abspath(path)
def main() -> None:
if not os.path.exists(REPO_ROOT):
print(f"Repo root not found: {REPO_ROOT}")
sys.exit(1)
# Check if git sees the nul file as untracked
import subprocess
result = subprocess.run(
["git", "status", "--short"],
capture_output=True,
text=True,
cwd=REPO_ROOT,
)
if "nul" not in result.stdout:
print("No 'nul' file found in git status. Nothing to do.")
return
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.MoveFileW.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
kernel32.MoveFileW.restype = wintypes.BOOL
src = make_unc(NUL_PATH)
dst = make_unc(TEMP_PATH)
ok = kernel32.MoveFileW(src, dst)
if not ok:
err = ctypes.get_last_error()
print(f"MoveFileW failed with error {err}")
sys.exit(1)
os.remove(TEMP_PATH)
print("Deleted 'nul' file successfully.")
if __name__ == "__main__":
main()