From: Varsha Rao Date: Wed, 10 Jul 2019 15:17:22 +0000 (+0530) Subject: cephfs-shell: Fix 'du' command error X-Git-Tag: v15.1.0~1555^2~23 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9c686669a8e332314a2068fa86fbe2be06d0f316;p=ceph.git cephfs-shell: Fix 'du' command error This patch fixes the following: * No error message outputed for non existing directories. * Takes only single argument. * When no arguments are passed, existing directories in the current directory are not displayed. * Always '.' is concated to the path. Fixes: https://tracker.ceph.com/issues/39641 Signed-off-by: Varsha Rao --- diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 1fa54bc07769..ea58a960ae51 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -1123,8 +1123,8 @@ sub-directories, files') du_parser = argparse.ArgumentParser( description='Disk Usage of a Directory') du_parser.add_argument('dirs', type=str, action=path_to_bytes, - help='Name of the directory.', nargs='?', - default='.') + help='Name of the directory.', nargs='*', + default='') du_parser.add_argument('-r', action='store_true', help='Recursive Disk usage of all directories.') @@ -1133,19 +1133,36 @@ sub-directories, files') """ Disk Usage of a Directory """ + dir_passed = args.dirs if args.dirs == b'': - args.dirs = cephfs.getcwd() - for dir_ in args.dirs: + dir_passed = cephfs.getcwd() + if len(dir_passed) > 1: + dir_passed = dir_passed.split() + + for dir_ in dir_passed: + dir_trav = sorted(set(dirwalk(dir_))) + if args.dirs and not dir_trav: + dir_trav.append(dir_) + else: + dir_trav.append('.') + if args.r: - for i in reversed(sorted(set(dirwalk(dir_)))): - i = os.path.normpath(i) - try: - xattr = cephfs.getxattr(i, 'ceph.dir.rbytes') - self.poutput('{:10s} {}'.format( - humansize(int(xattr.decode('utf-8'))), '.' - + i.decode('utf-8'))) - except libcephfs.Error: - continue + dir_trav = reversed(dir_trav) + + for i in dir_trav: + try: + i_path = os.path.normpath(i) + if (i != '.') and (i_path[0] == '/'): + i = '.' + i_path + + xattr = cephfs.getxattr(i_path, + 'ceph.dir.rbytes') + self.poutput('{:10s} {}'.format(humansize(int( + xattr.decode('utf-8'))), i.decode('utf-8')) + except (libcephfs.Error, OSError): + self.perror('{}: no such directory exists'.format(dir_), + False) + continue else: dir_ = os.path.normpath(dir_) self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr(