From: Varsha Rao Date: Tue, 30 Apr 2019 12:52:55 +0000 (+0530) Subject: cephfs-shell: Fix 'df' command errors X-Git-Tag: v15.1.0~591^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=505ed71ec63bf14a4921fb622cf2e7f75d7b79b1;p=ceph.git cephfs-shell: Fix 'df' command errors In this patch following changes are made: * When non-existing files or directories are used, print error message. * Allow df on multiple file or directory. * Add help option for df usage. Fixes: https://tracker.ceph.com/issues/39543 Signed-off-by: Varsha Rao --- diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 7e9ac937e54f..9b3a94673a7e 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -1105,25 +1105,53 @@ sub-directories, files') """ poutput(os.getcwd()) + def complete_df(self, text, line, begidx, endidx): + """ + auto complete of file name. + """ + return self.complete_filenames(text, line, begidx, endidx) + + df_parser = argparse.ArgumentParser(description='Show information about\ + the amount of available disk space') + df_parser.add_argument('file', help='Name of the file', nargs='*', + action=path_to_bytes) + + @with_argparser(df_parser) def do_df(self, arglist): """ Display the amount of available disk space for file systems """ - for index, i in enumerate(ls(b".", opts='A')): - if index == 0: - poutput('{:25s}\t{:5s}\t{:15s}{:10s}{}'.format( - "1K-blocks", "Used", "Available", "Use%", "Stored on")) - if not is_dir_exists(i.d_name): - statfs = cephfs.statfs(i.d_name) - stat = cephfs.stat(i.d_name) - block_size = statfs['f_blocks'] * statfs['f_bsize'] // 1024 + header = True # Set to true for printing header only once + if not arglist.file: + arglist.file = ls(cephfs.getcwd()) + + for file in arglist.file: + if isinstance(file, libcephfs.DirEntry): + file = file.d_name + if file == b'.' or file == b'..': + continue + try: + statfs = cephfs.statfs(file) + stat = cephfs.stat(file) + block_size = statfs['f_blocks']*statfs['f_bsize'] // 1024 available = block_size - stat.st_size use = 0 + if block_size > 0: - use = (stat.st_size * 100 // block_size) - poutput('{:25d}\t{:5d}\t{:10d}\t{:5s} {}'.format( - statfs['f_fsid'], stat.st_size, available, - str(int(use)) + '%', i.d_name.decode('utf-8'))) + use = (stat.st_size*100 // block_size) + + if header: + header = False + poutput('{:25s}\t{:5s}\t{:15s}{:10s}{}'.format( + "1K-blocks", "Used", "Available", "Use%", + "Stored on")) + + poutput('{:d}\t{:18d}\t{:8d}\t{:10s} {}'.format(block_size, + stat.st_size, available, str(int(use)) + '%', + file.decode('utf-8'))) + except libcephfs.OSError as e: + perror("could not statfs {}: {}".format(file.decode('utf-8'), + e.strerror)) locate_parser = argparse.ArgumentParser( description='Find file within file system')