summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknownexus <phillip.smyth@codethink.co.uk>2018-09-03 16:22:35 +0100
committerJürg Billeter <j@bitron.ch>2018-09-27 15:29:29 +0100
commitb78ae76767842785baed0b631961b4cf0c433d77 (patch)
treeeda2f1a5cc71fe1896e8b49131537de0f5c24171
parent0f3ef369299948e5567c012bf995789944d7fef6 (diff)
downloadbuildstream-b78ae76767842785baed0b631961b4cf0c433d77.tar.gz
utils.py: Reworked safe_remove
Non-Linux platforms don't return EISDIR when attempting to unlink a directory. Stopped safe_remove attempting to unlink dirs Previously safe_remove would attempt to unlink a path Before attempting to remove it if it was a dir Now it checks for a dir before that step
-rw-r--r--buildstream/utils.py37
1 files changed, 18 insertions, 19 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 60211f35b..1e04a31ed 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -35,6 +35,7 @@ import tempfile
import itertools
import functools
from contextlib import contextmanager
+from stat import S_ISDIR
import psutil
@@ -328,27 +329,25 @@ def safe_remove(path):
Raises:
UtilError: In the case of unexpected system call failures
"""
- if os.path.lexists(path):
-
- # Try to remove anything that is in the way, but issue
- # a warning instead if it removes a non empty directory
- try:
+ try:
+ if S_ISDIR(os.lstat(path).st_mode):
+ os.rmdir(path)
+ else:
os.unlink(path)
- except OSError as e:
- if e.errno != errno.EISDIR:
- raise UtilError("Failed to remove '{}': {}"
- .format(path, e))
-
- try:
- os.rmdir(path)
- except OSError as e:
- if e.errno == errno.ENOTEMPTY:
- return False
- else:
- raise UtilError("Failed to remove '{}': {}"
- .format(path, e))
- return True
+ # File removed/unlinked successfully
+ return True
+
+ except OSError as e:
+ if e.errno == errno.ENOTEMPTY:
+ # Path is non-empty directory
+ return False
+ elif e.errno == errno.ENOENT:
+ # Path does not exist
+ return True
+
+ raise UtilError("Failed to remove '{}': {}"
+ .format(path, e))
def copy_files(src, dest, *, files=None, ignore_missing=False, report_written=False):