From 7f90c50704c2cafe08c345c4e87728c2fc889173 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Fri, 16 Aug 2019 23:07:32 +0530 Subject: [PATCH] cephfs-shell: refactor do_du() Improve code's readability and keep all paths as bytes. Specifically, define a function within do do_du() to simplify the code that handles recursion, rename args.dirs to args.paths, get rid of dir_passed and use args.paths directly instead, change the default value of args.dirs to a list with one member that contains current working directory, rewrite the docstring, and few more similar changes. Signed-off-by: Rishabh Dave --- src/tools/cephfs/cephfs-shell | 57 ++++++++++++++--------------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 174d118b802..e38cc809679 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -1135,58 +1135,47 @@ sub-directories, files') du_parser = argparse.ArgumentParser( description='Disk Usage of a Directory') - du_parser.add_argument('dirs', type=str, action=path_to_bytes, + du_parser.add_argument('paths', type=str, action=path_to_bytes, help='Name of the directory.', nargs='*', - default='') + default=[b'.']) du_parser.add_argument('-r', action='store_true', help='Recursive Disk usage of all directories.') @with_argparser(du_parser) def do_du(self, args): """ - Disk Usage of a Directory + Print disk usage of a given path(s). """ - dir_passed = args.dirs - if args.dirs == b'': - dir_passed = cephfs.getcwd() - if len(dir_passed) > 1: - dir_passed = dir_passed.split() + def print_disk_usage(files): + if isinstance(files, bytes): + files = (files, ) - 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('.') - - for i in dir_trav: + for f in files: try: - i_path = os.path.normpath(i) - if (i is not '.') and (i_path[0] is '/'): - i = '.' + i_path - - st = cephfs.lstat(i_path) + st = cephfs.lstat(f) if stat.S_ISDIR(st.st_mode): - dusage = int(cephfs.getxattr(i_path, - 'ceph.dir.rbytes').decode('utf-8')) - elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode): + dusage = int(cephfs.getxattr(f, + 'ceph.dir.rbytes').decode('utf-8')) + else: dusage = st.st_size + # print path in local context + f = os.path.normpath(f) + if f[0] is ord('/'): + f = b'.' + f self.poutput('{:10s} {}'.format(humansize(dusage), - i.decode('utf-8'))) - # Assigning dusage None to avoid cases where its value - # from previous iterations ends up being reused. - dusage = None + f.decode('utf-8'))) except (libcephfs.Error, OSError): - self.perror('{}: no such directory exists'.format( - dir_), False) + self.perror('{} : no such directory exists'.format(f.\ + decode('utf-8')), False) continue + + for path in args.paths: + if args.r: + print_disk_usage(sorted(set(dirwalk(path)).union({path}))) else: - dir_ = os.path.normpath(dir_) - self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr( - dir_, 'ceph.dir.rbytes').decode('utf-8'))), '.' - + dir_.decode('utf-8'))) + print_disk_usage(path) quota_parser = argparse.ArgumentParser( description='Quota management for a Directory') -- 2.39.5