]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/volumes: refactor dir handle cleanup 30887/head
authorJos Collin <jcollin@redhat.com>
Mon, 14 Oct 2019 04:51:35 +0000 (10:21 +0530)
committerJos Collin <jcollin@redhat.com>
Tue, 22 Oct 2019 04:25:25 +0000 (09:55 +0530)
introduce with statement in rmtree. This change
simplifies the code's handling of directory cleanup.

Signed-off-by: Jos Collin <jcollin@redhat.com>
src/pybind/mgr/volumes/fs/subvolume.py

index 8f3c2b1ab5ef4c50428d92b0e9b7ab155acac160..defa2b46cd980907b6d364bdb56938bf6329d261 100644 (file)
@@ -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():