New API: ntfsresize-size to allow shrinking NTFS (RHBZ#585223).
[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 typedef int (*block_dev_func_t)(const char *dev,
33                                 char ***r, int *size, int *alloc);
34
35 /* Execute a given function for each discovered block device */
36 static char**
37 foreach_block_device (block_dev_func_t func)
38 {
39   char **r = NULL;
40   int size = 0, alloc = 0;
41
42   DIR *dir;
43   int err = 0;
44
45   dir = opendir ("/sys/block");
46   if (!dir) {
47     reply_with_perror ("opendir: /sys/block");
48     return NULL;
49   }
50
51   while(1) {
52     errno = 0;
53     struct dirent *d = readdir(dir);
54     if(NULL == d) break;
55
56     if (STREQLEN (d->d_name, "sd", 2) ||
57         STREQLEN (d->d_name, "hd", 2) ||
58         STREQLEN (d->d_name, "vd", 2) ||
59         STREQLEN (d->d_name, "sr", 2)) {
60       char dev_path[256];
61       snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
62
63       /* RHBZ#514505: Some versions of qemu <= 0.10 add a
64        * CD-ROM device even though we didn't request it.  Try to
65        * detect this by seeing if the device contains media.
66        */
67       int fd = open (dev_path, O_RDONLY);
68       if (fd == -1) {
69         perror (dev_path);
70         continue;
71       }
72       close (fd);
73
74       /* Call the map function for this device */
75       if((*func)(d->d_name, &r, &size, &alloc) != 0) {
76         err = 1;
77         break;
78       }
79     }
80   }
81
82   /* Check readdir didn't fail */
83   if(0 != errno) {
84     reply_with_perror ("readdir: /sys/block");
85     free_stringslen(r, size);
86     return NULL;
87   }
88
89   /* Close the directory handle */
90   if (closedir (dir) == -1) {
91     reply_with_perror ("closedir: /sys/block");
92     free_stringslen(r, size);
93     return NULL;
94   }
95
96   /* Free the result list on error */
97   if(err) {
98     free_stringslen(r, size);
99     return NULL;
100   }
101
102   /* Sort the devices */
103   sort_strings (r, size);
104
105   /* NULL terminate the list */
106   if (add_string (&r, &size, &alloc, NULL) == -1) {
107     return NULL;
108   }
109
110   return r;
111 }
112
113 /* Add a device to the list of devices */
114 static int
115 add_device(const char *device,
116            char ***const r, int *const size, int *const alloc)
117 {
118   char dev_path[256];
119   snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
120
121   if (add_string (r, size, alloc, dev_path) == -1) {
122     return -1;
123   }
124
125   return 0;
126 }
127
128 char **
129 do_list_devices (void)
130 {
131   return foreach_block_device(add_device);
132 }
133
134 static int
135 add_partitions(const char *device,
136                char ***const r, int *const size, int *const alloc)
137 {
138   char devdir[256];
139
140   /* Open the device's directory under /sys/block */
141   snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
142
143   DIR *dir = opendir (devdir);
144   if (!dir) {
145     reply_with_perror ("opendir: %s", devdir);
146     free_stringslen (*r, *size);
147     return -1;
148   }
149
150   /* Look in /sys/block/<device>/ for entries starting with <device>
151    * e.g. /sys/block/sda/sda1
152    */
153   errno = 0;
154   struct dirent *d;
155   while ((d = readdir (dir)) != NULL) {
156     if (STREQLEN (d->d_name, device, strlen (device))) {
157       char part[256];
158       snprintf (part, sizeof part, "/dev/%s", d->d_name);
159
160       if (add_string (r, size, alloc, part) == -1) {
161         closedir (dir);
162         return -1;
163       }
164     }
165   }
166
167   /* Check if readdir failed */
168   if(0 != errno) {
169       reply_with_perror ("readdir: %s", devdir);
170       free_stringslen(*r, *size);
171       return -1;
172   }
173
174   /* Close the directory handle */
175   if (closedir (dir) == -1) {
176     reply_with_perror ("closedir: /sys/block/%s", device);
177     free_stringslen (*r, *size);
178     return -1;
179   }
180
181   return 0;
182 }
183
184 char **
185 do_list_partitions (void)
186 {
187   return foreach_block_device(add_partitions);
188 }