Patchwork lib: Add helper function to get release info

login
register
mail settings
Submitter Colin King
Date March 4, 2013, noon
Message ID <1362398441-17360-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/224705/
State Rejected
Headers show

Comments

Colin King - March 4, 2013, noon
From: Colin Ian King <colin.king@canonical.com>

Add fwts_release_get() and fwts_release_free() to help get and free
the current disto release information.   fwts_release_get() returns
NULL if it cannot get the distro release information. Currently only
supports debian systems.

Example:

	fwts_release *release = fwts_release_get();
	if (release) {
		printf("Distributor: <%s>\n", release->distributor);
		printf("Description: <%s>\n", release->description);
		printf("Release:     <%s>\n", release->release);
		printf("Codename:    <%s>\n", release->codename);
		fwts_release_free(release);
	}

Some fields may be null when undefined; if all fields are null then
fwts_release_get will return a NULL struct pointer.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts.h         |   1 +
 src/lib/include/fwts_release.h |  33 ++++++++++++
 src/lib/src/Makefile.am        |   1 +
 src/lib/src/fwts_release.c     | 111 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 src/lib/include/fwts_release.h
 create mode 100644 src/lib/src/fwts_release.c
Colin King - March 5, 2013, 7:30 a.m.
On 04/03/13 12:00, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Add fwts_release_get() and fwts_release_free() to help get and free
> the current disto release information.   fwts_release_get() returns
> NULL if it cannot get the distro release information. Currently only
> supports debian systems.
>
> Example:
>
> 	fwts_release *release = fwts_release_get();
> 	if (release) {
> 		printf("Distributor: <%s>\n", release->distributor);
> 		printf("Description: <%s>\n", release->description);
> 		printf("Release:     <%s>\n", release->release);
> 		printf("Codename:    <%s>\n", release->codename);
> 		fwts_release_free(release);
> 	}
>
> Some fields may be null when undefined; if all fields are null then
> fwts_release_get will return a NULL struct pointer.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/include/fwts.h         |   1 +
>   src/lib/include/fwts_release.h |  33 ++++++++++++
>   src/lib/src/Makefile.am        |   1 +
>   src/lib/src/fwts_release.c     | 111 +++++++++++++++++++++++++++++++++++++++++
>   4 files changed, 146 insertions(+)
>   create mode 100644 src/lib/include/fwts_release.h
>   create mode 100644 src/lib/src/fwts_release.c
>
> diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
> index cbf9dd9..680eab9 100644
> --- a/src/lib/include/fwts.h
> +++ b/src/lib/include/fwts.h
> @@ -79,5 +79,6 @@
>   #include "fwts_button.h"
>   #include "fwts_json.h"
>   #include "fwts_ioport.h"
> +#include "fwts_release.h"
>
>   #endif
> diff --git a/src/lib/include/fwts_release.h b/src/lib/include/fwts_release.h
> new file mode 100644
> index 0000000..9a806c6
> --- /dev/null
> +++ b/src/lib/include/fwts_release.h
> @@ -0,0 +1,33 @@
> +/*
> + * Copyright (C) 2013 Canonical
> + *
> + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
> + *
> + */
> +
> +#ifndef __FWTS_RELEASE_H__
> +#define __FWTS_RELEASE_H__
> +
> +typedef struct {
> +	char *distributor;
> +	char *description;
> +	char *release;
> +	char *codename;
> +} fwts_release;
> +
> +fwts_release *fwts_release_get(void);
> +void fwts_release_free(fwts_release *release);
> +
> +#endif
> diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
> index 6a44641..4d9ab38 100644
> --- a/src/lib/src/Makefile.am
> +++ b/src/lib/src/Makefile.am
> @@ -59,6 +59,7 @@ libfwts_la_SOURCES = 		\
>   	fwts_multiproc.c 	\
>   	fwts_oops.c 		\
>   	fwts_pipeio.c 		\
> +	fwts_release.c		\
>   	fwts_scan_efi_systab.c 	\
>   	fwts_set.c 		\
>   	fwts_smbios.c 		\
> diff --git a/src/lib/src/fwts_release.c b/src/lib/src/fwts_release.c
> new file mode 100644
> index 0000000..9cc8da3
> --- /dev/null
> +++ b/src/lib/src/fwts_release.c
> @@ -0,0 +1,111 @@
> +/*
> + * Copyright (C) 2013 Canonical
> + *
> + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
> + *
> + */
> +#include <string.h>
> +
> +#include "fwts.h"
> +
> +/*
> + *  fwts_release_field_get()
> + *	attempt to match a field (needle) in the given text and update field
> + *	with any text after the delimiter, e.g. if text is "DISTRIB_ID=Ubuntu"
> + *	and needle is "DISTRIB_ID" and delimiter is "=", then the field gets set to "Ubuntu".
> + */
> +static void fwts_release_field_get(char *needle, char *delimiter, char *text, char **field)
> +{
> +	char *str = strstr(text, delimiter);
> +
> +	if (str == NULL)
> +		return;
> +	if (strstr(text, needle) == NULL)
> +		return;
> +	str++;
> +	while (*str == ' ')
> +		str++;
> +
> +	if (str)
> +		*field = strdup(str);
> +}
> +
> +/*
> + *  fwts_release_get_debian()
> + *	return release for debian based systems
> + */
> +void fwts_release_get_debian(fwts_list *list, fwts_release *release)
> +{
> +	fwts_list_link *item;
> +
> +	fwts_list_foreach(item, list) {
> +		char *line = fwts_text_list_text(item);
> +
> +		fwts_release_field_get("DISTRIB_ID", "=", line, &release->distributor);
> +		fwts_release_field_get("DISTRIB_RELEASE", "=", line, &release->description);
> +		fwts_release_field_get("DISTRIB_CODENAME", "=", line, &release->release);
> +		fwts_release_field_get("DISTRIB_DESCRIPTION", "=", line, &release->codename);
> +	}
> +}
> +
> +/*
> + *  fwts_release_get()
> + *	get release info
> + */
> +fwts_release *fwts_release_get(void)
> +{
> +	fwts_list *list;
> +	fwts_release *release;
> +
> +	if ((release = calloc(1, sizeof(fwts_release))) == NULL)
> +		return NULL;
> +
> +	/*
> +	 *  For the moment, check in /etc/lsb-release, we need to add in
> +	 *  support for different distros too.
> +	 */
> +	if ((list = fwts_file_open_and_read("/etc/lsb-release")) != NULL) {
> +		fwts_release_get_debian(list, release);
> +		fwts_list_free(list, free);
> +	}
> +
> +	/*
> +	 *  Ensure we have something sane to return
> +	 */
> +	if ((release->distributor == NULL) &&
> +	    (release->description == NULL) &&
> +	    (release->release == NULL) &&
> +	    (release->codename == NULL)) {
> +		free(release);
> +		return NULL;
> +	}
> +
> +	return release;
> +}
> +
> +/*
> + *  fwts_release_free()
> + *	free release info
> + */
> +void fwts_release_free(fwts_release *release)
> +{
> +	if (release) {
> +		free(release->distributor);
> +		free(release->description);
> +		free(release->release);
> +		free(release->codename);
> +		free(release);
> +	}
> +}
>

After some thought about this I'm NACK'ing this as I don't like the fact 
that fields can be null, I will re-submit later today.

Colin

Patch

diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h
index cbf9dd9..680eab9 100644
--- a/src/lib/include/fwts.h
+++ b/src/lib/include/fwts.h
@@ -79,5 +79,6 @@ 
 #include "fwts_button.h"
 #include "fwts_json.h"
 #include "fwts_ioport.h"
+#include "fwts_release.h"
 
 #endif
diff --git a/src/lib/include/fwts_release.h b/src/lib/include/fwts_release.h
new file mode 100644
index 0000000..9a806c6
--- /dev/null
+++ b/src/lib/include/fwts_release.h
@@ -0,0 +1,33 @@ 
+/*
+ * Copyright (C) 2013 Canonical
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef __FWTS_RELEASE_H__
+#define __FWTS_RELEASE_H__
+
+typedef struct {
+	char *distributor;
+	char *description;
+	char *release;
+	char *codename;
+} fwts_release;
+
+fwts_release *fwts_release_get(void);
+void fwts_release_free(fwts_release *release);
+
+#endif
diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am
index 6a44641..4d9ab38 100644
--- a/src/lib/src/Makefile.am
+++ b/src/lib/src/Makefile.am
@@ -59,6 +59,7 @@  libfwts_la_SOURCES = 		\
 	fwts_multiproc.c 	\
 	fwts_oops.c 		\
 	fwts_pipeio.c 		\
+	fwts_release.c		\
 	fwts_scan_efi_systab.c 	\
 	fwts_set.c 		\
 	fwts_smbios.c 		\
diff --git a/src/lib/src/fwts_release.c b/src/lib/src/fwts_release.c
new file mode 100644
index 0000000..9cc8da3
--- /dev/null
+++ b/src/lib/src/fwts_release.c
@@ -0,0 +1,111 @@ 
+/*
+ * Copyright (C) 2013 Canonical
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#include <string.h>
+
+#include "fwts.h"
+
+/*
+ *  fwts_release_field_get()
+ *	attempt to match a field (needle) in the given text and update field
+ *	with any text after the delimiter, e.g. if text is "DISTRIB_ID=Ubuntu"
+ *	and needle is "DISTRIB_ID" and delimiter is "=", then the field gets set to "Ubuntu".
+ */
+static void fwts_release_field_get(char *needle, char *delimiter, char *text, char **field)
+{
+	char *str = strstr(text, delimiter);
+
+	if (str == NULL)
+		return;
+	if (strstr(text, needle) == NULL)
+		return;
+	str++;
+	while (*str == ' ')
+		str++;
+
+	if (str)
+		*field = strdup(str);
+}
+
+/*
+ *  fwts_release_get_debian()
+ *	return release for debian based systems
+ */
+void fwts_release_get_debian(fwts_list *list, fwts_release *release)
+{
+	fwts_list_link *item;
+
+	fwts_list_foreach(item, list) {
+		char *line = fwts_text_list_text(item);
+
+		fwts_release_field_get("DISTRIB_ID", "=", line, &release->distributor);
+		fwts_release_field_get("DISTRIB_RELEASE", "=", line, &release->description);
+		fwts_release_field_get("DISTRIB_CODENAME", "=", line, &release->release);
+		fwts_release_field_get("DISTRIB_DESCRIPTION", "=", line, &release->codename);
+	}
+}
+
+/*
+ *  fwts_release_get()
+ *	get release info
+ */
+fwts_release *fwts_release_get(void)
+{
+	fwts_list *list;
+	fwts_release *release;
+
+	if ((release = calloc(1, sizeof(fwts_release))) == NULL)
+		return NULL;
+
+	/*
+	 *  For the moment, check in /etc/lsb-release, we need to add in
+	 *  support for different distros too.
+	 */
+	if ((list = fwts_file_open_and_read("/etc/lsb-release")) != NULL) {
+		fwts_release_get_debian(list, release);
+		fwts_list_free(list, free);
+	}
+
+	/*
+	 *  Ensure we have something sane to return
+	 */
+	if ((release->distributor == NULL) &&
+	    (release->description == NULL) &&
+	    (release->release == NULL) &&
+	    (release->codename == NULL)) {
+		free(release);
+		return NULL;
+	}
+
+	return release;
+}
+
+/*
+ *  fwts_release_free()
+ *	free release info
+ */
+void fwts_release_free(fwts_release *release)
+{
+	if (release) {
+		free(release->distributor);
+		free(release->description);
+		free(release->release);
+		free(release->codename);
+		free(release);
+	}
+}