Skip to content

Commit

Permalink
syspath: correctly prefix Windows UNC paths
Browse files Browse the repository at this point in the history
Identified while tackling #670, but this should actually solve some legitimate
problems with cataloging music on a network drive.
  • Loading branch information
sampsyo committed Apr 13, 2014
1 parent 1df6303 commit 9dd4ad9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9dd4ad9

Please sign in to comment.