Fix Windows MAX_PATH failures for percent-encoded wiki filenames.
Cap encoded slug length and use \\?\ long-path writes (0.3.6). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Filesystem helpers (Windows MAX_PATH / long paths)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def fs_path(path: Path | str) -> Path:
|
||||
"""
|
||||
On Windows, prefix with ``\\\\?\\`` so open/mkdir work beyond MAX_PATH (~260).
|
||||
|
||||
No-op on other platforms. Idempotent if already prefixed.
|
||||
"""
|
||||
p = Path(path)
|
||||
if os.name != "nt":
|
||||
return p
|
||||
s = os.fspath(p)
|
||||
if s.startswith("\\\\?\\"):
|
||||
return p
|
||||
if not p.is_absolute():
|
||||
p = p.resolve()
|
||||
s = os.fspath(p)
|
||||
if s.startswith("\\\\"):
|
||||
# UNC: \\server\share\... → \\?\UNC\server\share\...
|
||||
return Path("\\\\?\\UNC\\" + s[2:])
|
||||
return Path("\\\\?\\" + s)
|
||||
|
||||
|
||||
def ensure_dir(path: Path) -> Path:
|
||||
p = fs_path(path)
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
return p
|
||||
|
||||
|
||||
def write_text(path: Path, text: str, *, encoding: str = "utf-8") -> None:
|
||||
p = fs_path(path)
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(text, encoding=encoding)
|
||||
|
||||
|
||||
def copy_file(src: Path, dst: Path) -> None:
|
||||
s = fs_path(src)
|
||||
d = fs_path(dst)
|
||||
d.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(s, d)
|
||||
|
||||
|
||||
def remove_file(path: Path) -> None:
|
||||
p = fs_path(path)
|
||||
if p.is_file():
|
||||
p.unlink()
|
||||
Reference in New Issue
Block a user