diff options
author | Mike Frysinger <vapier@google.com> | 2021-09-28 11:27:24 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-09-28 16:06:50 +0000 |
commit | 9d96f58f5fcec101c612e61c3e2526ca071d89ea (patch) | |
tree | 63bc9e73e3b7d74a2cf5352239bf4f2e9695b507 /platform_utils.py | |
parent | 7a1e7e772f3bbc67660e824c98f527b5f608ac24 (diff) | |
download | git-repo-9d96f58f5fcec101c612e61c3e2526ca071d89ea.tar.gz |
make file removal a bit more robust
Some of the file removal calls are subject to race conditions (if
something else deletes the file), so extend our remove API to have
an option to ignore ENOENT errors. Then update a bunch of random
call sites to use this new functionality.
Change-Id: I31a9090e135452033135337a202a4fc2dbf8b63c
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/319195
Reviewed-by: Sean McAllister <smcallis@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'platform_utils.py')
-rw-r--r-- | platform_utils.py | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/platform_utils.py b/platform_utils.py index 5741f4d3..0203249a 100644 --- a/platform_utils.py +++ b/platform_utils.py | |||
@@ -127,28 +127,27 @@ def rename(src, dst): | |||
127 | shutil.move(src, dst) | 127 | shutil.move(src, dst) |
128 | 128 | ||
129 | 129 | ||
130 | def remove(path): | 130 | def remove(path, missing_ok=False): |
131 | """Remove (delete) the file path. This is a replacement for os.remove that | 131 | """Remove (delete) the file path. This is a replacement for os.remove that |
132 | allows deleting read-only files on Windows, with support for long paths and | 132 | allows deleting read-only files on Windows, with support for long paths and |
133 | for deleting directory symbolic links. | 133 | for deleting directory symbolic links. |
134 | 134 | ||
135 | Availability: Unix, Windows.""" | 135 | Availability: Unix, Windows.""" |
136 | if isWindows(): | 136 | longpath = _makelongpath(path) if isWindows() else path |
137 | longpath = _makelongpath(path) | 137 | try: |
138 | try: | 138 | os.remove(longpath) |
139 | os.remove(longpath) | 139 | except OSError as e: |
140 | except OSError as e: | 140 | if e.errno == errno.EACCES: |
141 | if e.errno == errno.EACCES: | 141 | os.chmod(longpath, stat.S_IWRITE) |
142 | os.chmod(longpath, stat.S_IWRITE) | 142 | # Directory symbolic links must be deleted with 'rmdir'. |
143 | # Directory symbolic links must be deleted with 'rmdir'. | 143 | if islink(longpath) and isdir(longpath): |
144 | if islink(longpath) and isdir(longpath): | 144 | os.rmdir(longpath) |
145 | os.rmdir(longpath) | ||
146 | else: | ||
147 | os.remove(longpath) | ||
148 | else: | 145 | else: |
149 | raise | 146 | os.remove(longpath) |
150 | else: | 147 | elif missing_ok and e.errno == errno.ENOENT: |
151 | os.remove(path) | 148 | pass |
149 | else: | ||
150 | raise | ||
152 | 151 | ||
153 | 152 | ||
154 | def walk(top, topdown=True, onerror=None, followlinks=False): | 153 | def walk(top, topdown=True, onerror=None, followlinks=False): |