]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Read /etc/os-release
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 6 Apr 2021 15:44:45 +0000 (15:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 6 Apr 2021 15:44:45 +0000 (15:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/pakfire.c
src/libpakfire/util.c

index 5974f3800882e276a948cb7a7d4de4a614ab1891..b02861c092cb031e5af71993139f13a230d7f125 100644 (file)
@@ -59,7 +59,10 @@ char* pakfire_generate_uuid();
 
 #include <archive.h>
 
+#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);
index 3598cb28a4b24dac437a311f565ccf3ed660bda1..c033b7dcb22aaf0eca0a4eafdadcf819e93e4370 100644 (file)
@@ -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);
index 18f865d97f0b864b278bfd4379cbb08d4f69752c..84c133a2cdab526d0ab9000970bb079648b1a21d 100644 (file)
@@ -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);