diff options
| author | Renaud Paquay <rpaquay@google.com> | 2016-11-01 11:24:03 -0700 |
|---|---|---|
| committer | David Pursehouse <dpursehouse@collab.net> | 2017-05-29 19:30:34 +0900 |
| commit | d5cec5e752821ca2710101b626b3a3ca07fdb7f8 (patch) | |
| tree | 4ecee491de2d3d57b4d03f526701c8c06a133b17 /platform_utils_win32.py | |
| parent | 2e7029116204cf2d6f516e4514091f0b492bc689 (diff) | |
| download | git-repo-d5cec5e752821ca2710101b626b3a3ca07fdb7f8.tar.gz git-repo-d5cec5e752821ca2710101b626b3a3ca07fdb7f8.zip | |
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
Diffstat (limited to 'platform_utils_win32.py')
| -rw-r--r-- | platform_utils_win32.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/platform_utils_win32.py b/platform_utils_win32.py new file mode 100644 index 000000000..02fb013a2 --- /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) |
