summaryrefslogtreecommitdiffstats
path: root/daemon/devsparts.c
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-04-06 12:55:26 +0100
committerRichard Jones <rjones@redhat.com>2009-04-06 12:55:26 +0100
commit8e570870f577ff0c3db074f88924633b559af5d4 (patch)
tree711af1263615c8cd977eceb0e4286425b53bd725 /daemon/devsparts.c
parent1cf85b1e60e85c4940869c6291d75ac44a5bd190 (diff)
downloadlibguestfs-8e570870f577ff0c3db074f88924633b559af5d4.tar.gz
libguestfs-8e570870f577ff0c3db074f88924633b559af5d4.tar.xz
libguestfs-8e570870f577ff0c3db074f88924633b559af5d4.zip
Implement list-devices and list-partitions.
Diffstat (limited to 'daemon/devsparts.c')
-rw-r--r--daemon/devsparts.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/daemon/devsparts.c b/daemon/devsparts.c
new file mode 100644
index 00000000..b0d79569
--- /dev/null
+++ b/daemon/devsparts.c
@@ -0,0 +1,133 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <sys/stat.h>
+
+#include "daemon.h"
+#include "actions.h"
+
+char **
+do_list_devices (void)
+{
+ char **r = NULL;
+ int size = 0, alloc = 0;
+ DIR *dir;
+ struct dirent *d;
+ char buf[256];
+
+ dir = opendir ("/sys/block");
+ if (!dir) {
+ reply_with_perror ("opendir: /sys/block");
+ return NULL;
+ }
+
+ while ((d = readdir (dir)) != NULL) {
+ if (strncmp (d->d_name, "sd", 2) == 0) {
+ snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
+ if (add_string (&r, &size, &alloc, buf) == -1) {
+ closedir (dir);
+ return NULL;
+ }
+ }
+ }
+
+ if (add_string (&r, &size, &alloc, NULL) == -1) {
+ closedir (dir);
+ return NULL;
+ }
+
+ if (closedir (dir) == -1) {
+ reply_with_perror ("closedir: /sys/block");
+ free_strings (r);
+ return NULL;
+ }
+
+ sort_strings (r, size-1);
+ return r;
+}
+
+char **
+do_list_partitions (void)
+{
+ char **r = NULL;
+ int size = 0, alloc = 0;
+ DIR *dir, *dir2;
+ struct dirent *d;
+ char buf[256], devname[256];
+
+ dir = opendir ("/sys/block");
+ if (!dir) {
+ reply_with_perror ("opendir: /sys/block");
+ return NULL;
+ }
+
+ while ((d = readdir (dir)) != NULL) {
+ if (strncmp (d->d_name, "sd", 2) == 0) {
+ strncpy (devname, d->d_name, sizeof devname);
+ devname[sizeof devname - 1] = '\0';
+
+ snprintf (buf, sizeof buf, "/sys/block/%s", devname);
+
+ dir2 = opendir (buf);
+ if (!dir2) {
+ reply_with_perror ("opendir: %s", buf);
+ free_stringslen (r, size);
+ return NULL;
+ }
+ while ((d = readdir (dir2)) != NULL) {
+ if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
+ snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
+
+ if (add_string (&r, &size, &alloc, buf) == -1) {
+ closedir (dir2);
+ closedir (dir);
+ return NULL;
+ }
+ }
+ }
+
+ if (closedir (dir2) == -1) {
+ reply_with_perror ("closedir: /sys/block/%s", devname);
+ free_stringslen (r, size);
+ return NULL;
+ }
+ }
+ }
+
+ if (add_string (&r, &size, &alloc, NULL) == -1) {
+ closedir (dir);
+ return NULL;
+ }
+
+ if (closedir (dir) == -1) {
+ reply_with_perror ("closedir: /sys/block");
+ free_strings (r);
+ return NULL;
+ }
+
+ sort_strings (r, size-1);
+ return r;
+}