浏览代码

fs: Properly normalise paths on Windows

Use forward slashes and integrate the drive letter into the path.
Rayyan Ansari 2 年之前
父节点
当前提交
54b654e3b1
共有 2 个文件被更改,包括 9 次插入4 次删除
  1. 7 2
      src/borg/helpers/fs.py
  2. 2 2
      src/borg/testsuite/archiver/create_cmd.py

+ 7 - 2
src/borg/helpers/fs.py

@@ -1,7 +1,7 @@
 import errno
 import errno
 import hashlib
 import hashlib
 import os
 import os
-import os.path
+import posixpath
 import re
 import re
 import stat
 import stat
 import subprocess
 import subprocess
@@ -231,7 +231,7 @@ def make_path_safe(path):
     path = path.lstrip("/")
     path = path.lstrip("/")
     if path.startswith("../") or "/../" in path or path.endswith("/..") or path == "..":
     if path.startswith("../") or "/../" in path or path.endswith("/..") or path == "..":
         raise ValueError(f"unexpected '..' element in path {path!r}")
         raise ValueError(f"unexpected '..' element in path {path!r}")
-    path = os.path.normpath(path)
+    path = posixpath.normpath(path)
     return 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()`).
     `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 = path.lstrip("/")
     path = _dotdot_re.sub("", path)
     path = _dotdot_re.sub("", path)
     if path in ["", ".."]:
     if path in ["", ".."]:

+ 2 - 2
src/borg/testsuite/archiver/create_cmd.py

@@ -110,8 +110,8 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         # To make our lives easier and to support cross-platform extraction we always use slashes.
         # 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).
         # 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"))
         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:
         # only needed on windows in case there are backslashes:
         posix_path = posix_path.replace("\\", "/")
         posix_path = posix_path.replace("\\", "/")
         # no leading slash in borg archives:
         # no leading slash in borg archives: