fs->device = safe_strdup (g, device);
fs->is_mountable = 1;
+ /* Optimize some of the tests by avoiding multiple tests of the same thing. */
+ int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
+ int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
+ int is_dir_share = guestfs_is_dir (g, "/share") > 0;
+
/* Grub /boot? */
if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
guestfs_is_file (g, "/grub/grub.conf") > 0)
fs->content = FS_CONTENT_LINUX_BOOT;
/* Linux root? */
- else if (guestfs_is_dir (g, "/etc") > 0 &&
- guestfs_is_dir (g, "/bin") > 0 &&
+ else if (is_dir_etc &&
+ is_dir_bin &&
guestfs_is_file (g, "/etc/fstab") > 0) {
fs->is_root = 1;
fs->content = FS_CONTENT_LINUX_ROOT;
return -1;
}
/* Linux /usr/local? */
- else if (guestfs_is_dir (g, "/etc") > 0 &&
- guestfs_is_dir (g, "/bin") > 0 &&
- guestfs_is_dir (g, "/share") > 0 &&
+ else if (is_dir_etc &&
+ is_dir_bin &&
+ is_dir_share &&
guestfs_exists (g, "/local") == 0 &&
guestfs_is_file (g, "/etc/fstab") == 0)
fs->content = FS_CONTENT_LINUX_USR_LOCAL;
/* Linux /usr? */
- else if (guestfs_is_dir (g, "/etc") > 0 &&
- guestfs_is_dir (g, "/bin") > 0 &&
- guestfs_is_dir (g, "/share") > 0 &&
+ else if (is_dir_etc &&
+ is_dir_bin &&
+ is_dir_share &&
guestfs_exists (g, "/local") > 0 &&
guestfs_is_file (g, "/etc/fstab") == 0)
fs->content = FS_CONTENT_LINUX_USR;
return 1;
}
+/* Match a regular expression which contains exactly three captures. */
+int
+guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
+ char **ret1, char **ret2, char **ret3)
+{
+ size_t len = strlen (str);
+ int vec[30], r;
+
+ r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
+ if (r == PCRE_ERROR_NOMATCH)
+ return 0;
+ if (r != 4) {
+ /* Internal error -- should not happen. */
+ fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
+ __FILE__, __func__, r, str);
+ return 0;
+ }
+
+ *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+ *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
+ *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
+
+ return 1;
+}
+
#endif /* HAVE_PCRE */