Skip to content

Commit fcc5a5b

Browse files
committed
ensure valid file urls are created from windows paths
1 parent cea7744 commit fcc5a5b

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/borg/repository.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import time
34

45
from borgstore.store import Store
@@ -105,11 +106,11 @@ def __init__(
105106
if isinstance(path_or_location, Location):
106107
location = path_or_location
107108
if location.proto == "file":
108-
url = f"file://{location.path}" # frequently users give without file:// prefix
109+
url = _local_abspath_to_file_url(location.path) # frequently users give without file:// prefix
109110
else:
110111
url = location.processed # location as given by user, processed placeholders
111112
else:
112-
url = "file://%s" % os.path.abspath(path_or_location)
113+
url = _local_abspath_to_file_url(os.path.abspath(path_or_location))
113114
location = Location(url)
114115
self._location = location
115116
self.url = url
@@ -565,3 +566,15 @@ def store_delete(self, name, *, deleted=False):
565566
def store_move(self, name, new_name=None, *, delete=False, undelete=False, deleted=False):
566567
self._lock_refresh()
567568
return self.store.move(name, new_name, delete=delete, undelete=undelete, deleted=deleted)
569+
570+
def _local_abspath_to_file_url(path: str) -> str:
571+
"""Create a file URL from a local, absolute path.
572+
573+
Expects `path` to be an absolute path on the local filesystem, e.g.:
574+
- POSIX: `/foo/bar`
575+
- Windows: `c:/foo/bar` (or `c:\foo\bar`)
576+
The easiest way to ensure this is for the caller to pass `path` through `os.path.abspath` first.
577+
"""
578+
if sys.platform in ("win32", "msys", "cygwin"):
579+
path = "/" + path.replace("\\", "/")
580+
return "file://%s" % path

0 commit comments

Comments
 (0)