Skip to content
Snippets Groups Projects
Commit 30619cad authored by knownexus's avatar knownexus
Browse files

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
parent 54a450e1
No related branches found
No related tags found
No related merge requests found
Pipeline #29646370 failed
......@@ -35,6 +35,7 @@ import tempfile
import itertools
import functools
from contextlib import contextmanager
from stat import S_ISDIR
import psutil
......@@ -328,25 +329,25 @@ def safe_remove(path):
Raises:
UtilError: In the case of unexpected system call failures
"""
if os.path.lexists(path):
if not S_ISDIR(os.lstat(path).st_mode):
# Try to remove anything that is in the way, but issue
# a warning instead if it removes a non empty directory
try:
os.unlink(path)
return True
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))
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment