Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 1 | # Copyright (C) 2016 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 15 | import errno |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 16 | import os |
| 17 | import platform |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 18 | import shutil |
| 19 | import stat |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def isWindows(): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 23 | """Returns True when running with the native port of Python for Windows, |
| 24 | False when running on any other platform (including the Cygwin port of |
| 25 | Python). |
| 26 | """ |
| 27 | # Note: The cygwin port of Python returns "CYGWIN_NT_xxx" |
| 28 | return platform.system() == "Windows" |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 29 | |
| 30 | |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 31 | def symlink(source, link_name): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | """Creates a symbolic link pointing to source named link_name. |
| 33 | |
| 34 | Note: On Windows, source must exist on disk, as the implementation needs |
| 35 | to know whether to create a "File" or a "Directory" symbolic link. |
| 36 | """ |
| 37 | if isWindows(): |
| 38 | import platform_utils_win32 |
| 39 | |
| 40 | source = _validate_winpath(source) |
| 41 | link_name = _validate_winpath(link_name) |
| 42 | target = os.path.join(os.path.dirname(link_name), source) |
| 43 | if isdir(target): |
| 44 | platform_utils_win32.create_dirsymlink( |
| 45 | _makelongpath(source), link_name |
| 46 | ) |
| 47 | else: |
| 48 | platform_utils_win32.create_filesymlink( |
| 49 | _makelongpath(source), link_name |
| 50 | ) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 51 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 52 | return os.symlink(source, link_name) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def _validate_winpath(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | path = os.path.normpath(path) |
| 57 | if _winpath_is_valid(path): |
| 58 | return path |
| 59 | raise ValueError( |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 60 | f'Path "{path}" must be a relative path or an absolute ' |
| 61 | "path starting with a drive letter" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 62 | ) |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def _winpath_is_valid(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 66 | """Windows only: returns True if path is relative (e.g. ".\\foo") or is |
| 67 | absolute including a drive letter (e.g. "c:\\foo"). Returns False if path |
| 68 | is ambiguous (e.g. "x:foo" or "\\foo"). |
| 69 | """ |
| 70 | assert isWindows() |
| 71 | path = os.path.normpath(path) |
| 72 | drive, tail = os.path.splitdrive(path) |
| 73 | if tail: |
| 74 | if not drive: |
| 75 | return tail[0] != os.sep # "\\foo" is invalid |
| 76 | else: |
| 77 | return tail[0] == os.sep # "x:foo" is invalid |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 78 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 79 | return not drive # "x:" is invalid |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 80 | |
| 81 | |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 82 | def _makelongpath(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 83 | """Return the input path normalized to support the Windows long path syntax |
| 84 | ("\\\\?\\" prefix) if needed, i.e. if the input path is longer than the |
| 85 | MAX_PATH limit. |
| 86 | """ |
| 87 | if isWindows(): |
| 88 | # Note: MAX_PATH is 260, but, for directories, the maximum value is |
| 89 | # actually 246. |
| 90 | if len(path) < 246: |
| 91 | return path |
| 92 | if path.startswith("\\\\?\\"): |
| 93 | return path |
| 94 | if not os.path.isabs(path): |
| 95 | return path |
| 96 | # Append prefix and ensure unicode so that the special longpath syntax |
| 97 | # is supported by underlying Win32 API calls |
| 98 | return "\\\\?\\" + os.path.normpath(path) |
| 99 | else: |
| 100 | return path |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 101 | |
| 102 | |
Mike Frysinger | f454512 | 2019-11-11 04:34:16 -0500 | [diff] [blame] | 103 | def rmtree(path, ignore_errors=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 104 | """shutil.rmtree(path) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 105 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 106 | Availability: Unix, Windows. |
| 107 | """ |
| 108 | onerror = None |
| 109 | if isWindows(): |
| 110 | path = _makelongpath(path) |
| 111 | onerror = handle_rmtree_error |
| 112 | shutil.rmtree(path, ignore_errors=ignore_errors, onerror=onerror) |
Renaud Paquay | a65adf7 | 2016-11-03 10:37:53 -0700 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def handle_rmtree_error(function, path, excinfo): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 116 | # Allow deleting read-only files. |
| 117 | os.chmod(path, stat.S_IWRITE) |
| 118 | function(path) |
Renaud Paquay | ad1abcb | 2016-11-01 11:34:55 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def rename(src, dst): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 122 | """os.rename(src, dst) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 123 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 124 | Availability: Unix, Windows. |
| 125 | """ |
| 126 | if isWindows(): |
| 127 | # On Windows, rename fails if destination exists, see |
| 128 | # https://docs.python.org/2/library/os.html#os.rename |
| 129 | try: |
| 130 | os.rename(_makelongpath(src), _makelongpath(dst)) |
| 131 | except OSError as e: |
| 132 | if e.errno == errno.EEXIST: |
| 133 | os.remove(_makelongpath(dst)) |
| 134 | os.rename(_makelongpath(src), _makelongpath(dst)) |
| 135 | else: |
| 136 | raise |
| 137 | else: |
| 138 | shutil.move(src, dst) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 139 | |
| 140 | |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 141 | def remove(path, missing_ok=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 142 | """Remove (delete) the file path. This is a replacement for os.remove that |
| 143 | allows deleting read-only files on Windows, with support for long paths and |
| 144 | for deleting directory symbolic links. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 145 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 146 | Availability: Unix, Windows. |
| 147 | """ |
| 148 | longpath = _makelongpath(path) if isWindows() else path |
| 149 | try: |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 150 | os.remove(longpath) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 151 | except OSError as e: |
| 152 | if e.errno == errno.EACCES: |
| 153 | os.chmod(longpath, stat.S_IWRITE) |
| 154 | # Directory symbolic links must be deleted with 'rmdir'. |
| 155 | if islink(longpath) and isdir(longpath): |
| 156 | os.rmdir(longpath) |
| 157 | else: |
| 158 | os.remove(longpath) |
Egor Duda | f070331 | 2025-03-06 10:14:44 +0300 | [diff] [blame] | 159 | elif ( |
| 160 | e.errno == errno.EROFS |
| 161 | and missing_ok |
| 162 | and not os.path.exists(longpath) |
| 163 | ): |
| 164 | pass |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 165 | elif missing_ok and e.errno == errno.ENOENT: |
| 166 | pass |
| 167 | else: |
| 168 | raise |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 169 | |
| 170 | |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 171 | def walk(top, topdown=True, onerror=None, followlinks=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 172 | """os.walk(path) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 173 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 174 | Availability: Windows, Unix. |
| 175 | """ |
| 176 | if isWindows(): |
| 177 | return _walk_windows_impl(top, topdown, onerror, followlinks) |
| 178 | else: |
| 179 | return os.walk(top, topdown, onerror, followlinks) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 180 | |
| 181 | |
| 182 | def _walk_windows_impl(top, topdown, onerror, followlinks): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 183 | try: |
| 184 | names = listdir(top) |
| 185 | except Exception as err: |
| 186 | if onerror is not None: |
| 187 | onerror(err) |
| 188 | return |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 189 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 190 | dirs, nondirs = [], [] |
| 191 | for name in names: |
| 192 | if isdir(os.path.join(top, name)): |
| 193 | dirs.append(name) |
| 194 | else: |
| 195 | nondirs.append(name) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 196 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 197 | if topdown: |
| 198 | yield top, dirs, nondirs |
| 199 | for name in dirs: |
| 200 | new_path = os.path.join(top, name) |
| 201 | if followlinks or not islink(new_path): |
Jason R. Coombs | 8dd8521 | 2023-10-20 06:48:20 -0400 | [diff] [blame] | 202 | yield from _walk_windows_impl( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 203 | new_path, topdown, onerror, followlinks |
Jason R. Coombs | 8dd8521 | 2023-10-20 06:48:20 -0400 | [diff] [blame] | 204 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 205 | if not topdown: |
| 206 | yield top, dirs, nondirs |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 207 | |
| 208 | |
| 209 | def listdir(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 210 | """os.listdir(path) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 211 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 212 | Availability: Windows, Unix. |
| 213 | """ |
| 214 | return os.listdir(_makelongpath(path)) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 215 | |
| 216 | |
| 217 | def rmdir(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 218 | """os.rmdir(path) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 219 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 220 | Availability: Windows, Unix. |
| 221 | """ |
| 222 | os.rmdir(_makelongpath(path)) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def isdir(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 226 | """os.path.isdir(path) wrapper with support for long paths on Windows. |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 227 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 228 | Availability: Windows, Unix. |
| 229 | """ |
| 230 | return os.path.isdir(_makelongpath(path)) |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 231 | |
| 232 | |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 233 | def islink(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 234 | """os.path.islink(path) wrapper with support for long paths on Windows. |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 235 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 236 | Availability: Windows, Unix. |
| 237 | """ |
| 238 | if isWindows(): |
| 239 | import platform_utils_win32 |
| 240 | |
| 241 | return platform_utils_win32.islink(_makelongpath(path)) |
| 242 | else: |
| 243 | return os.path.islink(path) |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 244 | |
| 245 | |
| 246 | def readlink(path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 247 | """Return a string representing the path to which the symbolic link |
| 248 | points. The result may be either an absolute or relative pathname; |
| 249 | if it is relative, it may be converted to an absolute pathname using |
| 250 | os.path.join(os.path.dirname(path), result). |
Renaud Paquay | 227ad2e | 2016-11-01 14:37:13 -0700 | [diff] [blame] | 251 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 252 | Availability: Windows, Unix. |
| 253 | """ |
| 254 | if isWindows(): |
| 255 | import platform_utils_win32 |
| 256 | |
| 257 | return platform_utils_win32.readlink(_makelongpath(path)) |
| 258 | else: |
| 259 | return os.readlink(path) |