From 1edb51bf68120dd54753b20846aae0b788528b5f Mon Sep 17 00:00:00 2001 From: Taiwudao Date: Sat, 21 Nov 2026 13:40:04 +0800 Subject: [PATCH] util: avoid Windows reserved device names in sanitize_path Windows refuses to create a file or directory named CON, PRN, AUX, NUL, COM1-COM9 or LPT1-LPT9, and an extension does not help: CON.txt is reserved too. sanitize_path left those components untouched, so importing an album or an artist with such a name failed with NotADirectoryError when beets tried to create the directory. Prefix an underscore instead. Following the CHAR_REPLACE rationale directly above, this applies on every platform -- Samba shares hit the same limit. Reproduced on Windows 10: sanitize_path("CON") returned "CON" and os.makedirs failed with NotADirectoryError; same for "COM1". --- beets/util/__init__.py | 21 ++++++++++++++++++++- docs/changelog.rst | 5 +++++ test/test_util.py | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 3208464637..c616f5711a 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -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. @@ -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) diff --git a/docs/changelog.rst b/docs/changelog.rst index 9c8535fd85..3cb90f2718 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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` diff --git a/test/test_util.py b/test/test_util.py index c562842847..3f2733b735 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -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("")