summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-06-29 16:43:53 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-07-07 11:22:59 +0100
commit70d8be9180a34a3b05270bfc73827b03121fc356 (patch)
tree10186eed0b436fe575ea53dc99d578d9315c915a /src
parent6aa281b7e8ca4f63dd5d5750184352ecd6e68fda (diff)
downloadlibguestfs-70d8be9180a34a3b05270bfc73827b03121fc356.tar.gz
libguestfs-70d8be9180a34a3b05270bfc73827b03121fc356.tar.xz
libguestfs-70d8be9180a34a3b05270bfc73827b03121fc356.zip
inspect: Refactor resolve_fstab_device code into multiple functions.
This is mostly code motion, although it also fixes a memory leak in an extremely rare failure case, and it generally tidies up the existing code. (cherry picked from commit 47b8225b05a7e35411f954f61f3eb3115c5a9f45)
Diffstat (limited to 'src')
-rw-r--r--src/inspect_fs_unix.c171
1 files changed, 103 insertions, 68 deletions
diff --git a/src/inspect_fs_unix.c b/src/inspect_fs_unix.c
index ae76c6a6..f152382a 100644
--- a/src/inspect_fs_unix.c
+++ b/src/inspect_fs_unix.c
@@ -1165,6 +1165,102 @@ error:
return -1;
}
+static int
+resolve_fstab_device_xdev (guestfs_h *g, const char *type, const char *disk,
+ const char *part, char **device_ret)
+{
+ char *name;
+ char **devices;
+ size_t i, count;
+ struct drive *drive;
+ const char *p;
+
+ /* type: (h|s|v|xv)
+ * disk: ([a-z]+)
+ * part: (\d*)
+ */
+
+ devices = guestfs_list_devices (g);
+ if (devices == NULL)
+ return -1;
+
+ /* Check any hints we were passed for a non-heuristic mapping */
+ name = safe_asprintf (g, "%sd%s", type, disk);
+ i = 0;
+ drive = g->drives;
+ while (drive) {
+ if (drive->name && STREQ (drive->name, name)) {
+ *device_ret = safe_asprintf (g, "%s%s", devices[i], part);
+ break;
+ }
+
+ i++; drive = drive->next;
+ }
+ free (name);
+
+ /* Guess the appliance device name if we didn't find a matching hint */
+ if (!*device_ret) {
+ /* Count how many disks the libguestfs appliance has */
+ for (count = 0; devices[count] != NULL; count++)
+ ;
+
+ /* Calculate the numerical index of the disk */
+ i = disk[0] - 'a';
+ for (p = disk + 1; *p != '\0'; p++) {
+ i += 1; i *= 26;
+ i += *p - 'a';
+ }
+
+ /* Check the index makes sense wrt the number of disks the appliance has.
+ * If it does, map it to an appliance disk.
+ */
+ if (i < count)
+ *device_ret = safe_asprintf (g, "%s%s", devices[i], part);
+ }
+
+ guestfs___free_string_list (devices);
+
+ return 0;
+}
+
+static int
+resolve_fstab_device_cciss (guestfs_h *g, const char *disk, const char *part,
+ char **device_ret)
+{
+ char **devices;
+ size_t i;
+ struct drive *drive;
+
+ /* disk: (cciss/c\d+d\d+)
+ * part: (\d+)?
+ */
+
+ devices = guestfs_list_devices (g);
+ if (devices == NULL)
+ return -1;
+
+ /* Check any hints we were passed for a non-heuristic mapping */
+ i = 0;
+ drive = g->drives;
+ while (drive) {
+ if (drive->name && STREQ(drive->name, disk)) {
+ if (part)
+ *device_ret = safe_asprintf (g, "%s%s", devices[i], part);
+ else
+ *device_ret = safe_strdup (g, devices[i]);
+ break;
+ }
+
+ i++; drive = drive->next;
+ }
+
+ /* We don't try to guess mappings for cciss devices */
+
+ guestfs___free_string_list (devices);
+
+ return 0;
+}
+
/* Resolve block device name to the libguestfs device name, eg.
* /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
* assumes that disks were added in the same order as they appear to
@@ -1176,6 +1272,7 @@ resolve_fstab_device (guestfs_h *g, const char *spec, Hash_table *md_map)
{
char *device = NULL;
char *type, *slice, *disk, *part;
+ int r;
if (STRPREFIX (spec, "/dev/mapper/") && guestfs_exists (g, spec) > 0) {
/* LVM2 does some strange munging on /dev/mapper paths for VGs and
@@ -1191,81 +1288,19 @@ resolve_fstab_device (guestfs_h *g, const char *spec, Hash_table *md_map)
device = guestfs_lvm_canonical_lv_name (g, spec);
}
else if (match3 (g, spec, re_xdev, &type, &disk, &part)) {
- /* type: (h|s|v|xv)
- * disk: ([a-z]+)
- * part: (\d*) */
- char **devices = guestfs_list_devices (g);
- if (devices == NULL)
- return NULL;
-
- /* Check any hints we were passed for a non-heuristic mapping */
- char *name = safe_asprintf (g, "%sd%s", type, disk);
- size_t i = 0;
- struct drive *drive = g->drives;
- while (drive) {
- if (drive->name && STREQ(drive->name, name)) {
- device = safe_asprintf (g, "%s%s", devices[i], part);
- break;
- }
-
- i++; drive = drive->next;
- }
- free (name);
-
- /* Guess the appliance device name if we didn't find a matching hint */
- if (!device) {
- /* Count how many disks the libguestfs appliance has */
- size_t count;
- for (count = 0; devices[count] != NULL; count++)
- ;
-
- /* Calculate the numerical index of the disk */
- i = disk[0] - 'a';
- for (char *p = disk + 1; *p != '\0'; p++) {
- i += 1; i *= 26;
- i += *p - 'a';
- }
-
- /* Check the index makes sense wrt the number of disks the appliance has.
- * If it does, map it to an appliance disk. */
- if (i < count) {
- device = safe_asprintf (g, "%s%s", devices[i], part);
- }
- }
-
+ r = resolve_fstab_device_xdev (g, type, disk, part, &device);
free (type);
free (disk);
free (part);
- guestfs___free_string_list (devices);
+ if (r == -1)
+ return NULL;
}
else if (match2 (g, spec, re_cciss, &disk, &part)) {
- /* disk: (cciss/c\d+d\d+)
- * part: (\d+)? */
- char **devices = guestfs_list_devices (g);
- if (devices == NULL)
- return NULL;
-
- /* Check any hints we were passed for a non-heuristic mapping */
- size_t i = 0;
- struct drive *drive = g->drives;
- while (drive) {
- if (drive->name && STREQ(drive->name, disk)) {
- if (part) {
- device = safe_asprintf (g, "%s%s", devices[i], part);
- } else {
- device = safe_strdup (g, devices[i]);
- }
- break;
- }
-
- i++; drive = drive->next;
- }
-
- /* We don't try to guess mappings for cciss devices */
-
+ r = resolve_fstab_device_cciss (g, disk, part, &device);
free (disk);
free (part);
- guestfs___free_string_list (devices);
+ if (r == -1)
+ return NULL;
}
else if (md_map && (disk = match1 (g, spec, re_mdN)) != NULL) {
mdadm_app entry;