From e95eb0d9697fcef42426c2800284292815e8022a Mon Sep 17 00:00:00 2001 From: Dhairya Parmar Date: Tue, 11 Apr 2023 15:36:52 +0530 Subject: [PATCH] mgr/nfs/utils: changes to helper func to check cephfs path - Renamed to cephfs_path_is_dir - Removed exception handling to prevent redundant log statements like: "No such file or directory error in stat: b'/mnt/testdir_symlink': No such file or directory [Errno 2]" Exceptions handled inside caller eliminates this redundancy - Set modifier flag AT_SYMLINK_NOFOLLOW - Removed string "{path} is not a dir" when raising NotADirectoryError Rationale: will be handled in export.py - change mock to cephfs_path_is_dir Signed-off-by: Dhairya Parmar (cherry picked from commit f3d7370) --- src/pybind/mgr/nfs/tests/test_nfs.py | 2 +- src/pybind/mgr/nfs/utils.py | 26 ++++++++------------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/pybind/mgr/nfs/tests/test_nfs.py b/src/pybind/mgr/nfs/tests/test_nfs.py index 7b39a9df720..5b4d5fe7e12 100644 --- a/src/pybind/mgr/nfs/tests/test_nfs.py +++ b/src/pybind/mgr/nfs/tests/test_nfs.py @@ -252,7 +252,7 @@ EXPORT { mock.patch('nfs.ganesha_conf.check_fs', return_value=True), \ mock.patch('nfs.export.ExportMgr._create_user_key', return_value='thekeyforclientabc'), \ - mock.patch('nfs.export.check_cephfs_path'): + mock.patch('nfs.export.cephfs_path_is_dir'): rados.open_ioctx.return_value.__enter__.return_value = self.io_mock rados.open_ioctx.return_value.__exit__ = mock.Mock(return_value=None) diff --git a/src/pybind/mgr/nfs/utils.py b/src/pybind/mgr/nfs/utils.py index 058b0195d34..ba3190a9644 100644 --- a/src/pybind/mgr/nfs/utils.py +++ b/src/pybind/mgr/nfs/utils.py @@ -1,4 +1,3 @@ -import errno import functools import logging import stat @@ -92,23 +91,14 @@ def check_fs(mgr: 'Module', fs_name: str) -> bool: return fs_name in [fs['mdsmap']['fs_name'] for fs in fs_map['filesystems']] -def check_cephfs_path(mgr: 'Module', fs: str, path: str) -> None: +def cephfs_path_is_dir(mgr: 'Module', fs: str, path: str) -> None: @functools.lru_cache(maxsize=1) def _get_cephfs_client() -> CephfsClient: return CephfsClient(mgr) - try: - cephfs_client = _get_cephfs_client() - with open_filesystem(cephfs_client, fs) as fs_handle: - stx = fs_handle.statx(path.encode('utf-8'), - cephfs.CEPH_STATX_MODE, 0) - if not stat.S_ISDIR(stx.get('mode')): - raise NotADirectoryError(f"{path} is not a dir") - except cephfs.ObjectNotFound as e: - log.exception(f"{-errno.ENOENT}: {e.args[1]}") - raise e - except cephfs.Error as e: - log.exception(f"{e.args[0]}: {e.args[1]}") - raise e - except Exception as e: - log.exception(f"unknown exception occurred: {e}") - raise e + cephfs_client = _get_cephfs_client() + + with open_filesystem(cephfs_client, fs) as fs_handle: + stx = fs_handle.statx(path.encode('utf-8'), cephfs.CEPH_STATX_MODE, + cephfs.AT_SYMLINK_NOFOLLOW) + if not stat.S_ISDIR(stx.get('mode')): + raise NotADirectoryError() -- 2.39.5