From 2a783569be391eaba679c184217b7ad0b40ee7c4 Mon Sep 17 00:00:00 2001 From: Milind Changire Date: Fri, 5 Apr 2019 15:57:05 +0530 Subject: [PATCH] cephfs-shell: support non-octal mode Fixes: http://tracker.ceph.com/issues/38740 Signed-off-by: Milind Changire --- src/tools/cephfs/cephfs-shell | 53 ++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index fd9122f3e54ac..07b5adee9c5f9 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -399,19 +399,64 @@ class CephFSShell(Cmd): def __call__(self, parser, namespace, values, option_string=None): o_mode = 0 + res = None try: o_mode = int(values, base=8) except ValueError as e: - parser.error("invalid mode: %s\n" - "mode must be a numeric octal literal." % values) + res = re.match('((u?g?o?)|(a?))(=)(r?w?x?)', values) + if res is None: + parser.error("invalid mode: %s\n" + "mode must be a numeric octal literal\n" + "or ((u?g?o?)|(a?))(=)(r?w?x?)" % + values) + else: + # we are supporting only assignment of mode and not + or - + # as is generally available with the chmod command + # eg. + # >>> res = re.match('((u?g?o?)|(a?))(=)(r?w?x?)', 'go=') + # >>> res.groups() + # ('go', 'go', None, '=', '') + val = res.groups() + + if val[3] != '=': + parser.error("need assignment operator between user " + "and mode specifiers") + if val[4] == '': + parser.error("invalid mode: %s" + "mode must be combination of: r | w | x" % + values) + users = '' + if val[2] == None: + users = val[1] + else: + users = val[2] + + t_mode = 0 + if users == 'a': + users = 'ugo' + + if 'r' in val[4]: + t_mode |= 4 + if 'w' in val[4]: + t_mode |= 2 + if 'x' in val[4]: + t_mode |= 1 + + if 'u' in users: + o_mode |= (t_mode << 6) + if 'g' in users: + o_mode |= (t_mode << 3) + if 'o' in users: + o_mode |= t_mode if o_mode < 0: parser.error("invalid mode: %s\n" - "mode cannot be negative." % values) + "mode cannot be negative" % values) if o_mode > 0o777: parser.error("invalid mode: %s\n" "mode cannot be greater than octal 0777" % values) - setattr(namespace, self.dest, values) + + setattr(namespace, self.dest, str(oct(o_mode))) mkdir_parser = argparse.ArgumentParser( -- 2.39.5