]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/volumes: refactor dir handle cleanup
authorJos Collin <jcollin@redhat.com>
Mon, 14 Oct 2019 04:51:35 +0000 (10:21 +0530)
committerRamana Raja <rraja@redhat.com>
Wed, 12 Feb 2020 10:11:59 +0000 (05:11 -0500)
introduce with statement in rmtree. This change
simplifies the code's handling of directory cleanup.

Signed-off-by: Jos Collin <jcollin@redhat.com>
(cherry picked from commit 9e27cd1aac1169b2cf547cc4adb38c7ac8409dd1)

src/pybind/mgr/volumes/fs/subvolume.py

index d798defb559c17b5cf25bdcfc16da3f7819a2197..6b88da20d52586785db0d89e5a8eb8e8abe6ad9f 100644 (file)
@@ -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():