|
1 | 1 | import os |
| 2 | +import sys |
2 | 3 | import time |
3 | 4 |
|
4 | 5 | from borgstore.store import Store |
@@ -105,11 +106,11 @@ def __init__( |
105 | 106 | if isinstance(path_or_location, Location): |
106 | 107 | location = path_or_location |
107 | 108 | 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 |
109 | 110 | else: |
110 | 111 | url = location.processed # location as given by user, processed placeholders |
111 | 112 | else: |
112 | | - url = "file://%s" % os.path.abspath(path_or_location) |
| 113 | + url = _local_abspath_to_file_url(os.path.abspath(path_or_location)) |
113 | 114 | location = Location(url) |
114 | 115 | self._location = location |
115 | 116 | self.url = url |
@@ -565,3 +566,15 @@ def store_delete(self, name, *, deleted=False): |
565 | 566 | def store_move(self, name, new_name=None, *, delete=False, undelete=False, deleted=False): |
566 | 567 | self._lock_refresh() |
567 | 568 | 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