From bfa1e2f07a8b54c9b05d772125ecd26183b5fb76 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Mon, 17 Jun 2019 18:07:09 +0530 Subject: [PATCH] cephfs-shell: print disk usage for non-directory files too Currently when du crashes when it comes across a non-directory files. Instead of doing that, print disk usage for regular files. Fixes: http://tracker.ceph.com/issues/40371 Signed-off-by: Rishabh Dave --- src/tools/cephfs/cephfs-shell | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index ea58a960ae51b..dd86cb4830a9e 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -14,6 +14,7 @@ import fnmatch import math import re import shlex +import stat if sys.version_info.major < 3: raise RuntimeError("cephfs-shell is only compatible with python3") @@ -1152,16 +1153,25 @@ sub-directories, files') for i in dir_trav: try: i_path = os.path.normpath(i) - if (i != '.') and (i_path[0] == '/'): + if (i is not '.') and (i_path[0] is '/'): 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')) + st = cephfs.lstat(i_path) + + 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 = st.st_size + + 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 except (libcephfs.Error, OSError): - self.perror('{}: no such directory exists'.format(dir_), - False) + self.perror('{}: no such directory exists'.format( + dir_), False) continue else: dir_ = os.path.normpath(dir_) -- 2.39.5