]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephfs-shell: Fix 'df' command errors
authorVarsha Rao <varao@redhat.com>
Tue, 30 Apr 2019 12:52:55 +0000 (18:22 +0530)
committerVarsha Rao <varao@redhat.com>
Fri, 15 Nov 2019 11:21:05 +0000 (16:51 +0530)
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 <varao@redhat.com>
src/tools/cephfs/cephfs-shell

index 7e9ac937e54f21617a6f55e80604b2027c9b7669..9b3a94673a7e819e67084e9d3243452c51e1b18d 100755 (executable)
@@ -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')