Bug fix: Trailing whitespace from 'pvs' command.
[libguestfs.git] / daemon / lvm.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ctype.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29
30 /* LVM actions.  Keep an eye on liblvm, although at the time
31  * of writing it hasn't progressed very far.
32  */
33
34 static char **
35 convert_lvm_output (char *out, char *prefix)
36 {
37   char *p, *pend;
38   char **r = NULL;
39   int size = 0, alloc = 0;
40   int len;
41   char buf[256];
42   char *str;
43
44   p = out;
45   while (p) {
46     pend = strchr (p, '\n');    /* Get the next line of output. */
47     if (pend) {
48       *pend = '\0';
49       pend++;
50     }
51
52     while (*p && isspace (*p))  /* Skip any leading whitespace. */
53       p++;
54
55     /* Sigh, skip trailing whitespace too.  "pvs", I'm looking at you. */
56     len = strlen (p)-1;
57     while (*p && isspace (p[len]))
58       p[len--] = '\0';
59
60     if (!*p) {                  /* Empty line?  Skip it. */
61       p = pend;
62       continue;
63     }
64
65     /* Prefix? */
66     if (prefix) {
67       snprintf (buf, sizeof buf, "%s%s", prefix, p);
68       str = buf;
69     } else
70       str = p;
71
72     if (add_string (&r, &size, &alloc, str) == -1) {
73       free (out);
74       return NULL;
75     }
76
77     p = pend;
78   }
79
80   free (out);
81
82   if (add_string (&r, &size, &alloc, NULL) == -1)
83     return NULL;
84
85   sort_strings (r, size-1);
86   return r;
87 }
88
89 char **
90 do_pvs (void)
91 {
92   char *out, *err;
93   int r;
94
95   r = command (&out, &err,
96                "/sbin/lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
97   if (r == -1) {
98     reply_with_error ("%s", err);
99     free (out);
100     free (err);
101     return NULL;
102   }
103
104   free (err);
105
106   return convert_lvm_output (out, NULL);
107 }
108
109 char **
110 do_vgs (void)
111 {
112   char *out, *err;
113   int r;
114
115   r = command (&out, &err,
116                "/sbin/lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
117   if (r == -1) {
118     reply_with_error ("%s", err);
119     free (out);
120     free (err);
121     return NULL;
122   }
123
124   free (err);
125
126   return convert_lvm_output (out, NULL);
127 }
128
129 char **
130 do_lvs (void)
131 {
132   char *out, *err;
133   int r;
134
135   r = command (&out, &err,
136                "/sbin/lvm", "lvs",
137                "-o", "vg_name,lv_name", "--noheadings",
138                "--separator", "/", NULL);
139   if (r == -1) {
140     reply_with_error ("%s", err);
141     free (out);
142     free (err);
143     return NULL;
144   }
145
146   free (err);
147
148   return convert_lvm_output (out, "/dev/");
149 }
150
151 /* These were so complex to implement that I ended up auto-generating
152  * the code.  That code is in stubs.c, and it is generated as usual
153  * by generator.ml.
154  */
155 guestfs_lvm_int_pv_list *
156 do_pvs_full (void)
157 {
158   return parse_command_line_pvs ();
159 }
160
161 guestfs_lvm_int_vg_list *
162 do_vgs_full (void)
163 {
164   return parse_command_line_vgs ();
165 }
166
167 guestfs_lvm_int_lv_list *
168 do_lvs_full (void)
169 {
170   return parse_command_line_lvs ();
171 }