From: Michael Tremer Date: Tue, 6 Apr 2021 15:44:45 +0000 (+0000) Subject: pakfire: Read /etc/os-release X-Git-Tag: 0.9.28~1285^2~432 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c76be2cb56d7a25e2dc28cc981fc5bd818c64c4;p=pakfire.git pakfire: Read /etc/os-release Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 5974f3800..b02861c09 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -59,7 +59,10 @@ char* pakfire_generate_uuid(); #include +#define pakfire_string_set(s, value) snprintf(s, sizeof(s), "%s", value) + int pakfire_string_endswith(const char* s, const char* suffix); +char* pakfire_unquote_in_place(char* s); char* pakfire_lstrip(const char* s); int pakfire_path_exists(const char* path); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 3598cb28a..c033b7dcb 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -78,6 +78,14 @@ struct _Pakfire { STAILQ_HEAD(mountpoints, mountpoint) mountpoints; int destroy_on_free; + + struct pakfire_distro { + char pretty_name[256]; + char name[64]; + char id[32]; + char version[32]; + char version_id[32]; + } distro; }; static const struct pakfire_mountpoint { @@ -387,6 +395,77 @@ static int pakfire_safety_checks(Pakfire pakfire) { return 0; } +static int pakfire_read_os_release(Pakfire pakfire) { + int r = 0; + + char* path = pakfire_make_path(pakfire, "/etc/os-release"); + if (!path) + return ENOMEM; + + FILE* f = fopen(path, "r"); + if (!f) { + ERROR(pakfire, "Could not open %s: %s\n", path, strerror(errno)); + r = -1; + goto ERROR; + } + + char* line = NULL; + size_t l = 0; + + while (1) { + ssize_t bytes_read = getline(&line, &l, f); + if (bytes_read < 0) + break; + + // Remove trailing newline + pakfire_remove_trailing_newline(line); + + // Find = + char* delim = strchr(line, '='); + if (!delim) + continue; + + // Replace = by NULL + *delim = '\0'; + + // Set key and val to the start of the strings + char* key = line; + char* val = delim + 1; + + // Unquote val + val = pakfire_unquote_in_place(val); + + if (strcmp(key, "PRETTY_NAME") == 0) + r = pakfire_string_set(pakfire->distro.pretty_name, val); + else if (strcmp(key, "NAME") == 0) + r = pakfire_string_set(pakfire->distro.name, val); + else if (strcmp(key, "ID") == 0) + r = pakfire_string_set(pakfire->distro.id, val); + else if (strcmp(key, "VERSION") == 0) + r = pakfire_string_set(pakfire->distro.version, val); + else if (strcmp(key, "VERSION_ID") == 0) + r = pakfire_string_set(pakfire->distro.version_id, val); + else + continue; + + // Catch any errors + if (r < 0) + goto ERROR; + } + + // Success + r = 0; + +ERROR: + if (f) + fclose(f); + if (line) + free(line); + free(path); + + return r; +} + PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char* arch) { char tempdir[PATH_MAX] = PAKFIRE_PRIVATE_DIR "/tmp/XXXXXX"; int r = 1; @@ -446,9 +525,15 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char if (env) pakfire_log_set_priority(p, log_priority(env)); + // Read /etc/os-release + r = pakfire_read_os_release(p); + if (r) + goto ERROR; + DEBUG(p, "Pakfire initialized at %p\n", p); - DEBUG(p, " arch = %s\n", pakfire_get_arch(p)); - DEBUG(p, " path = %s\n", pakfire_get_path(p)); + DEBUG(p, " arch = %s\n", pakfire_get_arch(p)); + DEBUG(p, " path = %s\n", pakfire_get_path(p)); + DEBUG(p, " distro = %s\n", p->distro.pretty_name); // Perform some safety checks r = pakfire_safety_checks(p); diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 18f865d97..84c133a2c 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -52,6 +52,30 @@ int pakfire_string_endswith(const char* s, const char* suffix) { return !strcmp(s + strlen(s) - strlen(suffix), suffix); } +char* pakfire_unquote_in_place(char* s) { + if (!s || !*s) + return s; + + // Is the first character a quote? + if (*s != '"') + return s; + + // Find the end of value + size_t l = strlen(s); + if (!l) + return s; + + // Is the last character a quote? + if (s[l - 1] != '"') + return s; + + // The string seems to be in quotes; remove them + s[l - 1] = '\0'; + s++; + + return s; +} + PAKFIRE_EXPORT int pakfire_string_partition( const char* s, const char* delim, char** s1, char** s2) { char* p = strstr(s, delim);