From: Jos Collin Date: Mon, 14 Oct 2019 04:51:35 +0000 (+0530) Subject: mgr/volumes: refactor dir handle cleanup X-Git-Tag: v14.2.8~49^2~35 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2da46f644df929b9d5713414c067d1c2b963adc7;p=ceph.git 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 (cherry picked from commit 9e27cd1aac1169b2cf547cc4adb38c7ac8409dd1) --- diff --git a/src/pybind/mgr/volumes/fs/subvolume.py b/src/pybind/mgr/volumes/fs/subvolume.py index d798defb559..6b88da20d52 100644 --- a/src/pybind/mgr/volumes/fs/subvolume.py +++ b/src/pybind/mgr/volumes/fs/subvolume.py @@ -229,22 +229,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():