From 32a507380671d3d810042ecce7719e78f0ba8328 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Fri, 26 Jul 2019 15:02:57 +0200 Subject: [PATCH] mgr/dashboard: CephFS class issues with strings Fixes: https://tracker.ceph.com/issues/40981 Signed-off-by: Volker Theile --- src/pybind/cephfs/cephfs.pyx | 2 ++ .../mgr/dashboard/controllers/nfsganesha.py | 12 ++++++------ src/pybind/mgr/dashboard/services/cephfs.py | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pybind/cephfs/cephfs.pyx b/src/pybind/cephfs/cephfs.pyx index 146c4bfe7d5..cbe31e1d913 100644 --- a/src/pybind/cephfs/cephfs.pyx +++ b/src/pybind/cephfs/cephfs.pyx @@ -607,6 +607,8 @@ cdef class LibCephFS(object): # Configure which filesystem to mount if one was specified if filesystem_name is None: filesystem_name = b"" + else: + filesystem_name = cstr(filesystem_name, 'filesystem_name') cdef: char *_filesystem_name = filesystem_name if filesystem_name: diff --git a/src/pybind/mgr/dashboard/controllers/nfsganesha.py b/src/pybind/mgr/dashboard/controllers/nfsganesha.py index 83d04a8bb28..e10006c8784 100644 --- a/src/pybind/mgr/dashboard/controllers/nfsganesha.py +++ b/src/pybind/mgr/dashboard/controllers/nfsganesha.py @@ -285,16 +285,16 @@ class NFSGaneshaUi(BaseController): logger.warning("[NFS] Limiting depth to maximum value of 5: " "input depth=%s", depth) depth = 5 - root_dir = '{}/'.format(root_dir) \ - if not root_dir.endswith('/') else root_dir - + root_dir = '{}{}'.format(root_dir.rstrip('/'), '/') try: cfs = CephFS() + root_dir = root_dir.encode() paths = cfs.get_dir_list(root_dir, depth) - paths = [p[:-1] for p in paths if p != root_dir] - return {'paths': paths} + # Convert (bytes => string) and prettify paths (strip slashes). + paths = [p.decode().rstrip('/') for p in paths if p != root_dir] except (cephfs.ObjectNotFound, cephfs.PermissionError): - return {'paths': []} + paths = [] + return {'paths': paths} @Endpoint('GET', '/cephfs/filesystems') def filesystems(self): diff --git a/src/pybind/mgr/dashboard/services/cephfs.py b/src/pybind/mgr/dashboard/services/cephfs.py index bb75b4e2573..70935f76eb6 100644 --- a/src/pybind/mgr/dashboard/services/cephfs.py +++ b/src/pybind/mgr/dashboard/services/cephfs.py @@ -3,6 +3,7 @@ from __future__ import absolute_import from contextlib import contextmanager +import six import cephfs from .. import mgr, logger @@ -40,6 +41,16 @@ class CephFS(object): self.cfs.closedir(d) def get_dir_list(self, dirpath, level): + """ + :param dirpath: The root directory path. + :type dirpath: str | bytes + :param level: The number of steps to go down the directory tree. + :type level: int + :return: A list of directory paths (bytes encoded). + :rtype: list + """ + if isinstance(dirpath, six.string_types): + dirpath = dirpath.encode() logger.debug("[CephFS] get_dir_list dirpath=%s level=%s", dirpath, level) if level == 0: @@ -50,12 +61,12 @@ class CephFS(object): paths = [dirpath] while dent: logger.debug("[CephFS] found entry=%s", dent.d_name) - if dent.d_name in ['.', '..']: + if dent.d_name in [b'.', b'..']: dent = self.cfs.readdir(d) continue if dent.is_dir(): logger.debug("[CephFS] found dir=%s", dent.d_name) - subdirpath = '{}{}/'.format(dirpath, dent.d_name) + subdirpath = b''.join([dirpath, dent.d_name, b'/']) paths.extend(self.get_dir_list(subdirpath, level-1)) dent = self.cfs.readdir(d) return paths -- 2.39.5