From d5cec5e752821ca2710101b626b3a3ca07fdb7f8 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Tue, 1 Nov 2016 11:24:03 -0700 Subject: Add support for creating symbolic links on Windows Replace all calls to os.symlink with platform_utils.symlink. The Windows implementation calls into the CreateSymbolicLinkW Win32 API, as os.symlink is not supported. Separate the Win32 API definitions into a separate module platform_utils_win32 for clarity. Change-Id: I0714c598664c2df93383734e609d948692c17ec5 --- platform_utils_win32.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 platform_utils_win32.py (limited to 'platform_utils_win32.py') diff --git a/platform_utils_win32.py b/platform_utils_win32.py new file mode 100644 index 00000000..02fb013a --- /dev/null +++ b/platform_utils_win32.py @@ -0,0 +1,63 @@ +# +# Copyright (C) 2016 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import errno + +from ctypes import WinDLL, get_last_error, FormatError, WinError +from ctypes.wintypes import BOOL, LPCWSTR, DWORD + +kernel32 = WinDLL('kernel32', use_last_error=True) + +# Win32 error codes +ERROR_SUCCESS = 0 +ERROR_PRIVILEGE_NOT_HELD = 1314 + +# Win32 API entry points +CreateSymbolicLinkW = kernel32.CreateSymbolicLinkW +CreateSymbolicLinkW.restype = BOOL +CreateSymbolicLinkW.argtypes = (LPCWSTR, # lpSymlinkFileName In + LPCWSTR, # lpTargetFileName In + DWORD) # dwFlags In + +# Symbolic link creation flags +SYMBOLIC_LINK_FLAG_FILE = 0x00 +SYMBOLIC_LINK_FLAG_DIRECTORY = 0x01 + + +def create_filesymlink(source, link_name): + """Creates a Windows file symbolic link source pointing to link_name.""" + _create_symlink(source, link_name, SYMBOLIC_LINK_FLAG_FILE) + + +def create_dirsymlink(source, link_name): + """Creates a Windows directory symbolic link source pointing to link_name. + """ + _create_symlink(source, link_name, SYMBOLIC_LINK_FLAG_DIRECTORY) + + +def _create_symlink(source, link_name, dwFlags): + # Note: Win32 documentation for CreateSymbolicLink is incorrect. + # On success, the function returns "1". + # On error, the function returns some random value (e.g. 1280). + # The best bet seems to use "GetLastError" and check for error/success. + CreateSymbolicLinkW(link_name, source, dwFlags) + code = get_last_error() + if code != ERROR_SUCCESS: + error_desc = FormatError(code).strip() + if code == ERROR_PRIVILEGE_NOT_HELD: + raise OSError(errno.EPERM, error_desc, link_name) + error_desc = 'Error creating symbolic link %s: %s'.format( + link_name, error_desc) + raise WinError(code, error_desc) -- cgit v1.2.3-54-g00ecf