From 9dd4ad96bd5b07f282ea4cd62f5fd5afcbfa0c14 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 13 Apr 2014 13:19:03 -0700 Subject: [PATCH] syspath: correctly prefix Windows UNC paths Identified while tackling #670, but this should actually solve some legitimate problems with cataloging music on a network drive. --- beets/util/__init__.py | 6 +++++- docs/changelog.rst | 2 ++ test/test_library.py | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 887525cbb7..428de312a0 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -372,8 +372,12 @@ def syspath(path, prefix=True): encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() path = path.decode(encoding, 'replace') - # Add the magic prefix if it isn't already there + # Add the magic prefix if it isn't already there. + # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx if prefix and not path.startswith(WINDOWS_MAGIC_PREFIX): + if path.startswith(u'\\\\'): + # UNC path. Final path should look like \\?\UNC\... + path = u'UNC' + path[1:] path = WINDOWS_MAGIC_PREFIX + path return path diff --git a/docs/changelog.rst b/docs/changelog.rst index 35c6615e46..00d6338500 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -41,6 +41,8 @@ Fixes: info is in the verbose logs. * :doc:`/plugins/mbsync`: Fix application of album-level metadata. Due to a regression a few releases ago, only track-level metadata was being updated. +* On Windows, paths on network shares (UNC paths) no longer cause "invalid + filename" errors. .. _enum34: https://pypi.python.org/pypi/enum34 .. _enum: https://docs.python.org/3.4/library/enum.html diff --git a/test/test_library.py b/test/test_library.py index f6e00b3e8b..a39e5ce304 100644 --- a/test/test_library.py +++ b/test/test_library.py @@ -582,6 +582,15 @@ def test_syspath_windows_format(self): self.assertTrue(isinstance(outpath, unicode)) self.assertTrue(outpath.startswith(u'\\\\?\\')) + def test_syspath_windows_format_unc_path(self): + # The \\?\ prefix on Windows behaves differently with UNC + # (network share) paths. + path = '\\\\server\\share\\file.mp3' + with _common.platform_windows(): + outpath = util.syspath(path) + self.assertTrue(isinstance(outpath, unicode)) + self.assertEqual(outpath, u'\\\\?\\UNC\\server\\share\\file.mp3') + def test_syspath_posix_unchanged(self): with _common.platform_posix(): path = os.path.join('a', 'b', 'c')