Implement list-devices and list-partitions.
[libguestfs.git] / daemon / devsparts.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 <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 char **
33 do_list_devices (void)
34 {
35   char **r = NULL;
36   int size = 0, alloc = 0;
37   DIR *dir;
38   struct dirent *d;
39   char buf[256];
40
41   dir = opendir ("/sys/block");
42   if (!dir) {
43     reply_with_perror ("opendir: /sys/block");
44     return NULL;
45   }
46
47   while ((d = readdir (dir)) != NULL) {
48     if (strncmp (d->d_name, "sd", 2) == 0) {
49       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
50       if (add_string (&r, &size, &alloc, buf) == -1) {
51         closedir (dir);
52         return NULL;
53       }
54     }
55   }
56
57   if (add_string (&r, &size, &alloc, NULL) == -1) {
58     closedir (dir);
59     return NULL;
60   }
61
62   if (closedir (dir) == -1) {
63     reply_with_perror ("closedir: /sys/block");
64     free_strings (r);
65     return NULL;
66   }
67
68   sort_strings (r, size-1);
69   return r;
70 }
71
72 char **
73 do_list_partitions (void)
74 {
75   char **r = NULL;
76   int size = 0, alloc = 0;
77   DIR *dir, *dir2;
78   struct dirent *d;
79   char buf[256], devname[256];
80
81   dir = opendir ("/sys/block");
82   if (!dir) {
83     reply_with_perror ("opendir: /sys/block");
84     return NULL;
85   }
86
87   while ((d = readdir (dir)) != NULL) {
88     if (strncmp (d->d_name, "sd", 2) == 0) {
89       strncpy (devname, d->d_name, sizeof devname);
90       devname[sizeof devname - 1] = '\0';
91
92       snprintf (buf, sizeof buf, "/sys/block/%s", devname);
93
94       dir2 = opendir (buf);
95       if (!dir2) {
96         reply_with_perror ("opendir: %s", buf);
97         free_stringslen (r, size);
98         return NULL;
99       }
100       while ((d = readdir (dir2)) != NULL) {
101         if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
102           snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
103
104           if (add_string (&r, &size, &alloc, buf) == -1) {
105             closedir (dir2);
106             closedir (dir);
107             return NULL;
108           }
109         }
110       }
111
112       if (closedir (dir2) == -1) {
113         reply_with_perror ("closedir: /sys/block/%s", devname);
114         free_stringslen (r, size);
115         return NULL;
116       }
117     }
118   }
119
120   if (add_string (&r, &size, &alloc, NULL) == -1) {
121     closedir (dir);
122     return NULL;
123   }
124
125   if (closedir (dir) == -1) {
126     reply_with_perror ("closedir: /sys/block");
127     free_strings (r);
128     return NULL;
129   }
130
131   sort_strings (r, size-1);
132   return r;
133 }