]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/ceph_argparse.py: do not create file for validating CephFilepath 24578/head
authorKefu Chai <kchai@redhat.com>
Mon, 15 Oct 2018 08:55:17 +0000 (16:55 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 15 Oct 2018 08:55:18 +0000 (16:55 +0800)
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 <kchai@redhat.com>
src/pybind/ceph_argparse.py

index fe15bf074c75e554ef494b7cdf512bef467772d9..2c06e2fbe5e06eae5080d451881faa2e7c764f83 100644 (file)
@@ -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 '<outfilename>'