Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,25 @@ def unique_path(path: AnyStr) -> AnyStr:
]


# Windows keeps these names out of the filesystem: a file or directory called
# ``CON``, ``NUL``, ``COM1`` and friends cannot be created there, with or
# without an extension (``CON.txt`` is reserved as well). Like CHAR_REPLACE
# above we avoid them on every platform, since Samba shares run into the same
# wall. https://learn.microsoft.com/windows/win32/fileio/naming-a-file
WINDOWS_RESERVED_NAMES = frozenset(
["con", "prn", "aux", "nul", "conin$", "conout$"]
+ [f"com{i}" for i in range(1, 10)]
+ [f"lpt{i}" for i in range(1, 10)]
)


def _avoid_reserved_name(component: str) -> str:
"""Prefix an underscore when a path component is a reserved device name."""
if component.split(".", 1)[0].lower() in WINDOWS_RESERVED_NAMES:
return "_" + component
return component


def get_empty_component_replace() -> Callable[[str], str] | None:
"""Return a callable filling in empty path components, if configured.

Expand Down Expand Up @@ -728,7 +747,7 @@ def sanitize_path(path: str, replacements: Replacements | None = None) -> str:
for i, comp in enumerate(comps):
for regex, repl in replacements:
comp = regex.sub(repl, comp)
comps[i] = comp
comps[i] = _avoid_reserved_name(comp)
return os.path.join(*comps)


Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ New features
Bug fixes
~~~~~~~~~

- Path components that Windows reserves for devices (``CON``, ``PRN``, ``AUX``,
``NUL``, ``COM1``-``COM9`` and ``LPT1``-``LPT9``) are now prefixed with an
underscore, with or without an extension. Importing an album or artist with
such a name no longer fails when beets tries to create the directory on
Windows.
- Import: ``duplicate_keys`` that use library field names such as ``mb_albumid``
or ``mb_trackid`` now detect duplicates when a metadata-source match is
applied, instead of treating the key as missing. :bug:`7067`
Expand Down
14 changes: 14 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ def test_sanitize_windows_replaces_trailing_space(self):
p = util.sanitize_path("one/two /three")
assert " " not in p

@pytest.mark.parametrize(
"name", ["CON", "con", "CON.txt", "NUL", "aux.mp3", "COM1", "LPT9"]
)
def test_reserved_device_names_get_a_prefix(self, name):
assert util.sanitize_path(name) == "_" + name

@pytest.mark.parametrize("name", ["normal", "console", "con_", "COM10", "LPT0"])
def test_ordinary_names_are_left_alone(self, name):
assert util.sanitize_path(name) == name

def test_reserved_device_name_is_fixed_inside_a_path(self):
path = os.path.join("one", "CON", "three")
assert util.sanitize_path(path) == os.path.join("one", "_CON", "three")

def test_sanitize_path_works_on_empty_string(self):
with _common.platform_posix():
p = util.sanitize_path("")
Expand Down