fish: Change 'int argc' to 'size_t argc' throughout.
[libguestfs.git] / tools / virt-df
index 17aed4a..9c92521 100755 (executable)
@@ -20,9 +20,7 @@ use warnings;
 use strict;
 
 use Sys::Guestfs;
-use Sys::Guestfs::Lib qw(open_guest get_partitions resolve_windows_path
-  inspect_all_partitions inspect_partition
-  inspect_operating_systems mount_operating_system inspect_in_detail);
+use Sys::Guestfs::Lib qw(open_guest get_partitions);
 
 use Pod::Usage;
 use Getopt::Long;
@@ -121,6 +119,8 @@ my $human;
 
 Print sizes in human-readable format.
 
+You are not allowed to use I<-h> and I<--csv> at the same time.
+
 =cut
 
 my $inodes;
@@ -148,6 +148,9 @@ if ($version) {
     exit
 }
 
+# RHBZ#600977
+die __"virt-df: cannot use -h and --csv options together\n" if $human && $csv;
+
 # Open the guest handle.
 
 if (@ARGV == 0) {
@@ -165,7 +168,7 @@ if (@ARGV == 0) {
     # https://bugzilla.redhat.com/show_bug.cgi?id=538041
     @doms = grep { $_->get_id () != 0 } @doms;
 
-    my @domnames = map { $_->get_name () } @doms;
+    my @domnames = sort (map { $_->get_name () } @doms);
 
     if (@domnames) {
         print_title ();
@@ -279,6 +282,7 @@ sub print_title
         printf "%-36s%10s %10s %10s %5s\n",
           $cols[1], $cols[2], $cols[3], $cols[4], $cols[5];
     } else {
+        # Columns don't need special CSV quoting.
         print (join (",", @cols), "\n");
     }
 }
@@ -297,7 +301,11 @@ sub print_cols
 
         printf ("%10s %10s %10s %5s\n", $_[2], $_[3], $_[4], $percent);
     } else {
-        printf ("\"%s\",\"%s\",%d,%d,%d,%.1f%%\n", @_);
+        # Need to quote libvirt domain and filesystem.
+        my $dom = shift;
+        my $fs = shift;
+        print csv_quote($dom), ",", csv_quote($fs), ",";
+        printf ("%d,%d,%d,%.1f%%\n", @_);
     }
 }
 
@@ -315,6 +323,31 @@ sub human_size
     }
 }
 
+# Quote field for CSV without using an external module.
+sub csv_quote
+{
+    local $_ = shift;
+
+    my $needs_quoting = /[ ",\n\0]/;
+    return $_ unless $needs_quoting;
+
+    my $i;
+    my $out = '"';
+    for ($i = 0; $i < length; ++$i) {
+        my $c = substr $_, $i, 1;
+        if ($c eq '"') {
+            $out .= '""';
+        } elsif ($c eq '\0') {
+            $out .= '"0';
+        } else {
+            $out .= $c;
+        }
+    }
+    $out .= '"';
+
+    return $out;
+}
+
 =head1 NOTE ABOUT CSV FORMAT
 
 Comma-separated values (CSV) is a deceptive format.  It I<seems> like