From 9d96f58f5fcec101c612e61c3e2526ca071d89ea Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 28 Sep 2021 11:27:24 -0400 Subject: 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 Tested-by: Mike Frysinger --- platform_utils.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'platform_utils.py') 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): shutil.move(src, dst) -def remove(path): +def remove(path, missing_ok=False): """Remove (delete) the file path. This is a replacement for os.remove that allows deleting read-only files on Windows, with support for long paths and for deleting directory symbolic links. Availability: Unix, Windows.""" - if isWindows(): - longpath = _makelongpath(path) - try: - os.remove(longpath) - except OSError as e: - if e.errno == errno.EACCES: - os.chmod(longpath, stat.S_IWRITE) - # Directory symbolic links must be deleted with 'rmdir'. - if islink(longpath) and isdir(longpath): - os.rmdir(longpath) - else: - os.remove(longpath) + longpath = _makelongpath(path) if isWindows() else path + try: + os.remove(longpath) + except OSError as e: + if e.errno == errno.EACCES: + os.chmod(longpath, stat.S_IWRITE) + # Directory symbolic links must be deleted with 'rmdir'. + if islink(longpath) and isdir(longpath): + os.rmdir(longpath) else: - raise - else: - os.remove(path) + os.remove(longpath) + elif missing_ok and e.errno == errno.ENOENT: + pass + else: + raise def walk(top, topdown=True, onerror=None, followlinks=False): -- cgit v1.2.3-54-g00ecf