2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <xvasprintf.h>
30 #include <libvirt/libvirt.h>
31 #include <libvirt/virterror.h>
42 static void write_csv_field (const char *field);
49 cols[0] = _("VirtualMachine");
50 cols[1] = _("Filesystem");
53 cols[2] = _("1K-blocks");
57 cols[4] = _("Available");
60 cols[2] = _("Inodes");
67 /* ignore cols[0] in this mode */
68 printf ("%-36s%10s %10s %10s %5s\n",
69 cols[1], cols[2], cols[3], cols[4], cols[5]);
74 for (i = 0; i < 6; ++i) {
77 write_csv_field (cols[i]);
83 static void canonical_device (char *dev, int offset);
86 print_stat (const char *name, const char *uuid_param,
87 const char *dev_param, int offset,
88 const struct guestfs_statvfs *stat)
90 /* First two columns are always 'name' and 'dev', followed by four
91 * other data columns. In text mode the 'name' and 'dev' are
92 * combined into a single 'name:dev' column. In CSV mode they are
93 * kept as two separate columns. In UUID mode the name might be
94 * replaced by 'uuid', if available.
96 #define MAX_LEN (LONGEST_HUMAN_READABLE > 128 ? LONGEST_HUMAN_READABLE : 128)
101 int hopts = human_round_to_nearest|human_autoscale|human_base_1024|human_SI;
104 /* Make the device canonical. */
105 len = strlen (dev_param) + 1;
107 strcpy (dev, dev_param);
109 canonical_device (dev, offset);
111 if (!inodes) { /* 1K blocks */
113 factor = stat->bsize / 1024;
115 v = stat->blocks * factor;
116 snprintf (buf[0], MAX_LEN, "%" PRIi64, v);
118 v = (stat->blocks - stat->bfree) * factor;
119 snprintf (buf[1], MAX_LEN, "%" PRIi64, v);
121 v = stat->bavail * factor;
122 snprintf (buf[2], MAX_LEN, "%" PRIi64, v);
126 human_readable ((uintmax_t) stat->blocks, buf[0],
127 hopts, stat->bsize, 1);
128 v = stat->blocks - stat->bfree;
130 human_readable ((uintmax_t) v, buf[1], hopts, stat->bsize, 1);
132 human_readable ((uintmax_t) stat->bavail, buf[2],
133 hopts, stat->bsize, 1);
136 if (stat->blocks != 0)
137 percent = 100. - 100. * stat->bfree / stat->blocks;
142 snprintf (buf[0], MAX_LEN, "%" PRIi64, stat->files);
144 snprintf (buf[1], MAX_LEN, "%" PRIi64, stat->files - stat->ffree);
146 snprintf (buf[2], MAX_LEN, "%" PRIi64, stat->ffree);
149 if (stat->files != 0)
150 percent = 100. - 100. * stat->ffree / stat->files;
156 /* Use 'ceil' on the percentage in order to emulate what df itself does. */
157 snprintf (buf[3], MAX_LEN, "%3.0f%%", ceil (percent));
159 snprintf (buf[3], MAX_LEN, "%.1f", percent);
164 if (uuid && uuid_param)
168 len = strlen (name) + strlen (dev) + 1;
169 printf ("%s:%s", name, dev);
171 for (i = len; i < 36; ++i)
177 printf ("%10s %10s %10s %5s\n", cols[0], cols[1], cols[2], cols[3]);
180 write_csv_field (name);
182 write_csv_field (dev);
184 for (i = 0; i < 4; ++i) {
186 write_csv_field (cols[i]);
193 /* /dev/vda1 -> /dev/sda, adjusting the device offset. */
195 canonical_device (char *dev, int offset)
197 if (STRPREFIX (dev, "/dev/") &&
198 (dev[5] == 'h' || dev[5] == 'v') &&
200 c_isalpha (dev[7]) &&
201 (c_isdigit (dev[8]) || dev[8] == '\0')) {
207 /* Function to quote CSV fields on output without requiring an
211 write_csv_field (const char *field)
214 int needs_quoting = 0;
216 len = strlen (field);
218 for (i = 0; i < len; ++i) {
219 if (field[i] == ' ' || field[i] == '"' ||
220 field[i] == '\n' || field[i] == ',') {
226 if (!needs_quoting) {
227 printf ("%s", field);
231 /* Quoting for CSV fields. */
233 for (i = 0; i < len; ++i) {
234 if (field[i] == '"') {