diff --git a/armi/utils/directoryChangers.py b/armi/utils/directoryChangers.py index 07bc871d14..8cd84a6565 100644 --- a/armi/utils/directoryChangers.py +++ b/armi/utils/directoryChangers.py @@ -194,14 +194,20 @@ def __init__( self, root, filesToMove, filesToRetrieve, dumpOnException ) - # If no root dir is given, the default path to grab in context is cwd(), which - # can lead to deleting any directory on the hard drive. So this check is here - # to ensure that if we grab a path from context, it is a proper temp dir. - if not root: + # If no root dir is given, the default path comes from context.getFastPath, which + # *might* be relative to the cwd, making it possible to delete unintended files. + # So this check is here to ensure that if we grab a path from context, it is a + # proper temp dir. + # That said, since the TemporaryDirectoryChanger *always* responsible for + # creating its destination directory, it may always be safe to delete it + # regardless of location. + if root is None: root = armi.context.getFastPath() - # ARMIs temp dirs are in an /.armi/ directory: validate this is a temp dir. - if ".armi" not in os.path.normpath(root).split(os.path.sep): - raise ValueError("Temporary directory not found.") + # ARMIs temp dirs are in an context.APP_DATA directory: validate this is a temp dir. + if pathlib.Path(context.APP_DATA) not in pathlib.Path(root).parents: + raise ValueError( + "Temporary directory not in a safe location for deletion." + ) # make the tmp dir, if necessary if not os.path.exists(root): diff --git a/armi/utils/pathTools.py b/armi/utils/pathTools.py index 9ccbdb27ae..139fd69560 100644 --- a/armi/utils/pathTools.py +++ b/armi/utils/pathTools.py @@ -23,6 +23,7 @@ import pathlib from time import sleep +from armi import context from armi import runLog @@ -239,14 +240,10 @@ def cleanPath(path): """ valid = False - if os.path.exists(path): - runLog.extra("Clearing all files in {}".format(path)) - else: - runLog.extra("Nothing to clean in {}. Doing nothing. ".format(path)) + if not os.path.exists(path): return True for validPath in [ - ".armi", "armiruns", "failedruns", "mc2run", @@ -258,6 +255,9 @@ def cleanPath(path): if validPath in path.lower(): valid = True + if pathlib.Path(context.APP_DATA) in pathlib.Path(path).parents: + valid = True + if not valid: raise Exception( "You tried to delete {0}, but it does not seem safe to do so.".format(path)