fs: Properly normalise paths on Windows

Use forward slashes and integrate the drive letter into the path.
This commit is contained in:
Rayyan Ansari 2023-06-25 17:38:54 +01:00
parent 349514446f
commit 54b654e3b1
2 changed files with 9 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import errno
import hashlib
import os
import os.path
import posixpath
import re
import stat
import subprocess
@ -231,7 +231,7 @@ def make_path_safe(path):
path = path.lstrip("/")
if path.startswith("../") or "/../" in path or path.endswith("/..") or path == "..":
raise ValueError(f"unexpected '..' element in path {path!r}")
path = os.path.normpath(path)
path = posixpath.normpath(path)
return path
@ -245,6 +245,11 @@ def remove_dotdot_prefixes(path):
`path` is expected to be normalized already (e.g. via `os.path.normpath()`).
"""
if is_win32:
if len(path) > 1 and path[1] == ":":
path = path.replace(":", "", 1)
path = path.replace("\\", "/")
path = path.lstrip("/")
path = _dotdot_re.sub("", path)
if path in ["", ".."]:

View File

@ -110,8 +110,8 @@ class ArchiverTestCase(ArchiverTestCaseBase):
# To make our lives easier and to support cross-platform extraction we always use slashes.
# Similarly, archived paths are expected to be full, but relative (have no leading slash).
full_path = os.path.abspath(os.path.join(self.input_path, "test"))
# remove windows drive letter, if any:
posix_path = full_path[2:] if full_path[1] == ":" else full_path
# remove colon from windows drive letter, if any:
posix_path = full_path.replace(":", "") if full_path[1] == ":" else full_path
# only needed on windows in case there are backslashes:
posix_path = posix_path.replace("\\", "/")
# no leading slash in borg archives: