From 9e27cd1aac1169b2cf547cc4adb38c7ac8409dd1 Mon Sep 17 00:00:00 2001 From: Jos Collin Date: Mon, 14 Oct 2019 10:21:35 +0530 Subject: [PATCH] mgr/volumes: refactor dir handle cleanup introduce with statement in rmtree. This change simplifies the code's handling of directory cleanup. Signed-off-by: Jos Collin --- src/pybind/mgr/volumes/fs/subvolume.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/pybind/mgr/volumes/fs/subvolume.py b/src/pybind/mgr/volumes/fs/subvolume.py index 8f3c2b1ab5ef4..defa2b46cd980 100644 --- a/src/pybind/mgr/volumes/fs/subvolume.py +++ b/src/pybind/mgr/volumes/fs/subvolume.py @@ -151,22 +151,20 @@ class SubVolume(object): def rmtree(root_path): log.debug("rmtree {0}".format(root_path)) try: - dir_handle = self.fs.opendir(root_path) + with self.fs.opendir(root_path) as dir_handle: + d = self.fs.readdir(dir_handle) + while d and not should_cancel(): + if d.d_name not in (b".", b".."): + d_full = os.path.join(root_path, d.d_name) + if d.is_dir(): + rmtree(d_full) + else: + self.fs.unlink(d_full) + d = self.fs.readdir(dir_handle) except cephfs.ObjectNotFound: return except cephfs.Error as e: raise VolumeException(-e.args[0], e.args[1]) - d = self.fs.readdir(dir_handle) - while d and not should_cancel(): - if d.d_name not in (b".", b".."): - d_full = os.path.join(root_path, d.d_name) - if d.is_dir(): - rmtree(d_full) - else: - self.fs.unlink(d_full) - - d = self.fs.readdir(dir_handle) - self.fs.closedir(dir_handle) # remove the directory only if we were not asked to cancel # (else we would fail to remove this anyway) if not should_cancel(): -- 2.39.5