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.
This commit is contained in:
Adrian Sampson 2014-04-13 13:19:03 -07:00
parent 1df6303222
commit 9dd4ad96bd
3 changed files with 16 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -582,6 +582,15 @@ class PathConversionTest(_common.TestCase):
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')