+/* Set fs->product_name to the first line of the release file. */
+static int
+parse_release_file (guestfs_h *g, struct inspect_fs *fs,
+ const char *release_filename)
+{
+ char **product_name = guestfs_head_n (g, 1, release_filename);
+ if (product_name == NULL)
+ return -1;
+ if (product_name[0] == NULL) {
+ error (g, "%s: file is empty", release_filename);
+ free_string_list (product_name);
+ return -1;
+ }
+
+ /* Note that this string becomes owned by the handle and will
+ * be freed by guestfs___free_inspect_info.
+ */
+ fs->product_name = product_name[0];
+ free (product_name);
+
+ return 0;
+}
+
+/* Parse generic MAJOR.MINOR from the fs->product_name string. */
+static int
+parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
+{
+ char *major, *minor;
+
+ if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
+ fs->major_version = parse_unsigned_int (g, major);
+ free (major);
+ if (fs->major_version == -1) {
+ free (minor);
+ return -1;
+ }
+ fs->minor_version = parse_unsigned_int (g, minor);
+ free (minor);
+ if (fs->minor_version == -1)
+ return -1;
+ }
+ return 0;
+}
+
+/* Ubuntu has /etc/lsb-release containing:
+ * DISTRIB_ID=Ubuntu # Distro
+ * DISTRIB_RELEASE=10.04 # Version
+ * DISTRIB_CODENAME=lucid
+ * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
+ * In theory other distros could have this LSB file, but none do.
+ */
+static int
+parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
+{
+ char **lines;
+ size_t i;
+ int r = 0;
+
+ lines = guestfs_head_n (g, 10, "/etc/lsb-release");
+ if (lines == NULL)
+ return -1;
+
+ for (i = 0; lines[i] != NULL; ++i) {
+ if (fs->distro == 0 &&
+ STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
+ fs->distro = OS_DISTRO_UBUNTU;
+ r = 1;
+ }
+ else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
+ char *major, *minor;
+ if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
+ fs->major_version = parse_unsigned_int (g, major);
+ free (major);
+ if (fs->major_version == -1) {
+ free (minor);
+ free_string_list (lines);
+ return -1;
+ }
+ fs->minor_version = parse_unsigned_int (g, minor);
+ free (minor);
+ if (fs->minor_version == -1) {
+ free_string_list (lines);
+ return -1;
+ }
+ }
+ }
+ else if (fs->product_name == NULL &&
+ (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
+ STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
+ size_t len = strlen (lines[i]) - 21 - 1;
+ fs->product_name = safe_strndup (g, &lines[i][21], len);
+ r = 1;
+ }
+ else if (fs->product_name == NULL &&
+ STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
+ size_t len = strlen (lines[i]) - 20;
+ fs->product_name = safe_strndup (g, &lines[i][20], len);
+ r = 1;
+ }
+ }
+
+ free_string_list (lines);
+ return r;
+}
+