From: Kefu Chai Date: Mon, 15 Oct 2018 08:55:17 +0000 (+0800) Subject: pybind/ceph_argparse.py: do not create file for validating CephFilepath X-Git-Tag: v14.0.1~15^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=90c2c28680a750dc268e44410cdc176995fc6756;p=ceph.git pybind/ceph_argparse.py: do not create file for validating CephFilepath before this change, a file is opened with append mode for validating the specified CephFilepath argument. but this file is never removed after the validation. we cannot assume that this file will be overwritten or removed by the Ceph daemon who serves the command. after this change, no file is created for the validation, instead we check if the file is readable or writable. Signed-off-by: Kefu Chai --- diff --git a/src/pybind/ceph_argparse.py b/src/pybind/ceph_argparse.py index fe15bf074c75..2c06e2fbe5e0 100644 --- a/src/pybind/ceph_argparse.py +++ b/src/pybind/ceph_argparse.py @@ -501,13 +501,24 @@ class CephFilepath(CephArgtype): Openable file """ def valid(self, s, partial=False): - try: - f = open(s, 'a+') - except Exception as e: - raise ArgumentValid('can\'t open {0}: {1}'.format(s, e)) - f.close() + # set self.val if the specified path is readable or writable + s = os.path.abspath(s) + if not os.access(s, os.R_OK): + self._validate_writable_file(s) self.val = s + def _validate_writable_file(self, fname): + if os.path.exists(fname): + if os.path.isfile(fname): + if not os.access(fname, os.W_OK): + raise ArgumentValid('{0} is not writable'.format(fname)) + else: + raise ArgumentValid('{0} is not file'.format(fname)) + else: + dirname = os.path.dirname(fname) + if not os.access(dirname, os.W_OK): + raise ArgumentValid('cannot create file in {0}'.format(dirname)) + def __str__(self): return ''