From 08b0bac2b9eca04e687b94e97d4953729dfda0f3 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 4 Sep 2022 13:33:18 +0200 Subject: [PATCH] Refactor os detection code --- CMakeLists.txt | 8 +- src/detection/host/host_android.c | 2 +- src/detection/os/os.c | 24 ++++++ src/detection/{ => os}/os.h | 2 +- src/detection/os/os_android.c | 30 +++++++ src/detection/{os.c => os/os_linux.c} | 119 +++++++++++--------------- src/logo/logo.c | 2 +- src/modules/os.c | 2 +- src/modules/packages.c | 2 +- 9 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 src/detection/os/os.c rename src/detection/{ => os}/os.h (100%) create mode 100644 src/detection/os/os_android.c rename src/detection/{os.c => os/os_linux.c} (54%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bb5bdb41..6998d99f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,7 +154,6 @@ set(LIBFASTFETCH_SRC src/logo/image/image.c src/logo/image/im7.c src/logo/image/im6.c - src/detection/os.c src/detection/qt.c src/detection/gtk.c src/detection/terminalShell.c @@ -164,6 +163,7 @@ set(LIBFASTFETCH_SRC src/detection/temps.c src/detection/title.c src/detection/host/host.c + src/detection/os/os.c src/detection/displayserver/displayServer.c src/detection/displayserver/wayland.c src/detection/displayserver/xcb.c @@ -215,11 +215,13 @@ if(APPLE) ) elseif(ANDROID) list(APPEND LIBFASTFETCH_SRC - "src/detection/host/host_android.c" + src/detection/host/host_android.c + src/detection/os/os_android.c ) elseif(UNIX) list(APPEND LIBFASTFETCH_SRC - "src/detection/host/host_linux.c" + src/detection/host/host_linux.c + src/detection/os/os_linux.c ) else() message(FATAL_ERROR "Unsupported platform") diff --git a/src/detection/host/host_android.c b/src/detection/host/host_android.c index c18a55bd9..1f2f10c11 100644 --- a/src/detection/host/host_android.c +++ b/src/detection/host/host_android.c @@ -1,5 +1,5 @@ #include "host.h" - +#include "common/settings.h" #include void ffDetectHostImpl(FFHostResult* host) diff --git a/src/detection/os/os.c b/src/detection/os/os.c new file mode 100644 index 000000000..e8ecb44cf --- /dev/null +++ b/src/detection/os/os.c @@ -0,0 +1,24 @@ +#include "os.h" + +#include + +void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance); + +const FFOSResult* ffDetectOS(const FFinstance* instance) +{ + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFOSResult result; + static bool init = false; + pthread_mutex_lock(&mutex); + if(init) + { + pthread_mutex_unlock(&mutex); + return &result; + } + init = true; + + ffDetectOSImpl(&result, instance); + + pthread_mutex_unlock(&mutex); + return &result; +} diff --git a/src/detection/os.h b/src/detection/os/os.h similarity index 100% rename from src/detection/os.h rename to src/detection/os/os.h index cd59e5491..1e2224663 100644 --- a/src/detection/os.h +++ b/src/detection/os/os.h @@ -7,7 +7,6 @@ typedef struct FFOSResult { - FFstrbuf systemName; FFstrbuf name; FFstrbuf prettyName; FFstrbuf id; @@ -18,6 +17,7 @@ typedef struct FFOSResult FFstrbuf versionID; FFstrbuf codename; FFstrbuf buildID; + FFstrbuf systemName; FFstrbuf architecture; } FFOSResult; diff --git a/src/detection/os/os_android.c b/src/detection/os/os_android.c new file mode 100644 index 000000000..a5d29f79e --- /dev/null +++ b/src/detection/os/os_android.c @@ -0,0 +1,30 @@ +#include "os.h" +#include "common/settings.h" + +void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) +{ + ffStrbufInit(&os->name); + ffStrbufSetS(&os->name, "Android"); + + ffStbrufInit(&os->prettyName); + ffStrbufSetS(&os->prettyName, "Android"); + + ffStrbufInit(&os->id); + ffStrbufSetS(&os->id, "android"); + + ffStrbufInit(&os->version); + ffSettingsGetAndroidProperty("ro.build.version.release", &os->version); + + ffStrbufInit(&os->versionID); + ffSettingsGetAndroidProperty("ro.build.version.release", &os->versionID); + + ffStrbufInit(&os->codename); + ffSettingsGetAndroidProperty("ro.build.version.codename", &os->codename); + + ffStrbufInit(&os->buildID); + ffSettingsGetAndroidProperty("ro.build.id", &os->buildID); + + ffStrbufInitA(&os->idLike, 0); + ffStrbufInitA(&os->variant, 0); + ffStrbufInitA(&os->variantID, 0); +} diff --git a/src/detection/os.c b/src/detection/os/os_linux.c similarity index 54% rename from src/detection/os.c rename to src/detection/os/os_linux.c index 3a84bc7dc..18635f0a0 100644 --- a/src/detection/os.c +++ b/src/detection/os/os_linux.c @@ -1,21 +1,20 @@ -#include "fastfetch.h" -#include "detection/os.h" +#include "os.h" #include "common/properties.h" #include "common/parsing.h" #include #include -#include -#if __ANDROID__ - #include "common/settings.h" -#else +static inline bool allRelevantValuesSet(const FFOSResult* result) +{ + return result->id.length > 0 + && result->name.length > 0 + && result->prettyName.length > 0 + ; +} static bool parseFile(const char* fileName, FFOSResult* result) { - if(result->id.length > 0 && result->name.length > 0 && result->prettyName.length > 0) - return true; - return ffParsePropFileValues(fileName, 13, (FFpropquery[]) { {"NAME =", &result->name}, {"DISTRIB_DESCRIPTION =", &result->prettyName}, @@ -94,73 +93,59 @@ static void getUbuntuFlavour(FFOSResult* result) } } -#endif - -const FFOSResult* ffDetectOS(const FFinstance* instance) +static void detectOS(FFOSResult* os, const FFinstance* instance) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - static FFOSResult result; - static bool init = false; - pthread_mutex_lock(&mutex); - if(init) - { - pthread_mutex_unlock(&mutex); - return &result; - } - init = true; - - ffStrbufInit(&result.systemName); - ffStrbufInit(&result.name); - ffStrbufInit(&result.prettyName); - ffStrbufInit(&result.id); - ffStrbufInit(&result.idLike); - ffStrbufInit(&result.variant); - ffStrbufInit(&result.variantID); - ffStrbufInit(&result.version); - ffStrbufInit(&result.versionID); - ffStrbufInit(&result.codename); - ffStrbufInit(&result.buildID); - ffStrbufInit(&result.architecture); - - ffStrbufSetS(&result.systemName, instance->state.utsname.sysname); - ffStrbufSetS(&result.architecture, instance->state.utsname.machine); - -#if !__ANDROID__ if(instance->config.osFile.length > 0) { - parseFile(instance->config.osFile.chars, &result); + parseFile(instance->config.osFile.chars, os); + return; } - else if(instance->config.escapeBedrock && parseFile(FASTFETCH_TARGET_DIR_ROOT"/bedrock/etc/bedrock-release", &result)) + + if(instance->config.escapeBedrock && parseFile(FASTFETCH_TARGET_DIR_ROOT"/bedrock/etc/bedrock-release", os)) { - if(result.id.length == 0) - ffStrbufAppendS(&result.id, "bedrock"); + if(os->id.length == 0) + ffStrbufAppendS(&os->id, "bedrock"); - if(result.name.length == 0) - ffStrbufAppendS(&result.name, "Bedrock"); + if(os->name.length == 0) + ffStrbufAppendS(&os->name, "Bedrock"); - if(result.prettyName.length == 0) - ffStrbufAppendS(&result.prettyName, "Bedrock Linux"); - } - else - { - parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/os-release", &result); - parseFile(FASTFETCH_TARGET_DIR_USR"/lib/os-release", &result); - parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/lsb-release", &result); + if(os->prettyName.length == 0) + ffStrbufAppendS(&os->prettyName, "Bedrock Linux"); + + return; } - if(ffStrbufIgnCaseCompS(&result.id, "ubuntu") == 0) - getUbuntuFlavour(&result); + parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/os-release", os); + if(allRelevantValuesSet(os)) + return; -#else - ffStrbufSetS(&result.name, "Android"); - ffStrbufSetS(&result.id, "android"); - ffSettingsGetAndroidProperty("ro.build.version.release", &result.versionID); - ffSettingsGetAndroidProperty("ro.build.version.release", &result.version); - ffSettingsGetAndroidProperty("ro.build.version.codename", &result.codename); - ffSettingsGetAndroidProperty("ro.build.id", &result.buildID); -#endif + parseFile(FASTFETCH_TARGET_DIR_USR"/lib/os-release", os); + if(allRelevantValuesSet(os)) + return; - pthread_mutex_unlock(&mutex); - - return &result; + parseFile(FASTFETCH_TARGET_DIR_ROOT"/etc/lsb-release", os); +} + +void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) +{ + ffStrbufInit(&os->systemName); + ffStrbufInit(&os->name); + ffStrbufInit(&os->prettyName); + ffStrbufInit(&os->id); + ffStrbufInit(&os->idLike); + ffStrbufInit(&os->variant); + ffStrbufInit(&os->variantID); + ffStrbufInit(&os->version); + ffStrbufInit(&os->versionID); + ffStrbufInit(&os->codename); + ffStrbufInit(&os->buildID); + ffStrbufInit(&os->architecture); + + ffStrbufSetS(&os->systemName, instance->state.utsname.sysname); + ffStrbufSetS(&os->architecture, instance->state.utsname.machine); + + detectOS(os, instance); + + if(ffStrbufIgnCaseCompS(&os->id, "ubuntu") == 0) + getUbuntuFlavour(os); } diff --git a/src/logo/logo.c b/src/logo/logo.c index f6fd14bf4..44000c500 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -1,7 +1,7 @@ #include "logo.h" #include "common/io.h" #include "common/printing.h" -#include "detection/os.h" +#include "detection/os/os.h" #include "detection/terminalshell.h" #include diff --git a/src/modules/os.c b/src/modules/os.c index 91809a59f..156ac6ca8 100644 --- a/src/modules/os.c +++ b/src/modules/os.c @@ -1,6 +1,6 @@ #include "fastfetch.h" #include "common/printing.h" -#include "detection/os.h" +#include "detection/os/os.h" #include diff --git a/src/modules/packages.c b/src/modules/packages.c index bab81a6f6..0608730ff 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -4,7 +4,7 @@ #include "common/printing.h" #include "common/settings.h" #include "common/processing.h" -#include "detection/os.h" +#include "detection/os/os.h" #include #include