Properly handle UNC paths in salt.utils.path.readlink()

When unpacking the destination of a symbolic link in Windows, links
which point to UNC paths start with "UNC\" instead of "\\". This causes
win32file.GetLongFileName to raise an exception. This commit modifies
salt.utils.path.readlink() to return the path in the proper UNC format,
and it also updates the roots backend to simply use the link path rather
than the destination when following symlinks which point to a UNC path.
This commit is contained in:
Erik Johnson 2017-09-28 11:29:46 -05:00
parent 6f687fdcff
commit 66e6e89dc7
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F
2 changed files with 16 additions and 0 deletions

View file

@ -367,6 +367,16 @@ def _file_lists(load, form):
'roots: %s symlink destination is %s',
abs_path, link_dest
)
if salt.utils.is_windows() \
and link_dest.startswith('\\\\'):
# Symlink points to a network path. Since you can't
# join UNC and non-UNC paths, just assume the original
# path.
log.trace(
'roots: %s is a UNCH path, using %s instead',
link_dest, abs_path
)
link_dest = abs_path
if link_dest.startswith('..'):
joined = os.path.join(abs_path, link_dest)
else:

View file

@ -9,6 +9,7 @@ from __future__ import absolute_import
import errno
import logging
import os
import re
import struct
# Import 3rd-party libs
@ -110,6 +111,11 @@ def readlink(path):
# comes out in 8.3 form; convert it to LFN to make it look nicer
target = win32file.GetLongPathName(target)
except pywinerror as exc:
# If target is on a UNC share, the decoded target will be in the format
# "UNC\hostanme\sharename\additional\subdirs\under\share". So, in
# these cases, return the target path in the proper UNC path format.
if target.startswith('UNC\\'):
return re.sub(r'^UNC\\+', r'\\\\', target)
# if file is not found (i.e. bad symlink), return it anyway like on *nix
if exc.winerror == 2:
return target