From bd316496a2ed5ad2192a481b376c2487adbe8c04 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sat, 8 May 2021 12:14:55 +0200 Subject: [PATCH] better handling of cache directory --- src/common/detectWMDE.c | 2 +- src/common/init.c | 20 ++++++++++++++++++++ src/common/io.c | 40 +--------------------------------------- src/fastfetch.h | 1 + 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/common/detectWMDE.c b/src/common/detectWMDE.c index 5e746278c..516e39b0f 100644 --- a/src/common/detectWMDE.c +++ b/src/common/detectWMDE.c @@ -252,7 +252,7 @@ static inline void getDE(FFWMDEResult* result, ProcData* procData) return; } - if(strcasecmp(result->sessionDesktop, "KDE") == 0) + if(strcasecmp(result->sessionDesktop, "KDE") == 0 || strcasecmp(result->sessionDesktop, "plasma") == 0 || strcasecmp(result->sessionDesktop, "plasmashell") == 0) getKDE(result); else if(strcasecmp(result->sessionDesktop, "Gnome") == 0 || strcasecmp(result->sessionDesktop, "ubuntu:GNOME") == 0 || strcasecmp(result->sessionDesktop, "ubuntu") == 0) getGnome(result); diff --git a/src/common/init.c b/src/common/init.c index dbebfbd46..a1984532b 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -1,6 +1,7 @@ #include "fastfetch.h" #include +#include static bool strbufEqualsAdapter(const void* first, const void* second) { @@ -64,6 +65,24 @@ static void initConfigDirs(FFstate* state) #undef FF_ENSURE_ONLY_ONCE_IN_LIST } +static void initCacheDir(FFstate* state) +{ + ffStrbufInitA(&state->cacheDir, 64); + + ffStrbufAppendS(&state->cacheDir, getenv("XDG_CACHE_HOME")); + + if(state->cacheDir.length == 0) + { + ffStrbufAppendS(&state->cacheDir, state->passwd->pw_dir); + ffStrbufAppendS(&state->cacheDir, "/.cache/"); + } + + mkdir(state->cacheDir.chars, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews + + ffStrbufAppendS(&state->cacheDir, "fastfetch/"); + mkdir(state->cacheDir.chars, S_IRWXU | S_IRGRP | S_IROTH); +} + static void initState(FFstate* state) { state->current_row = 0; @@ -72,6 +91,7 @@ static void initState(FFstate* state) sysinfo(&state->sysinfo); initConfigDirs(state); + initCacheDir(state); } static void defaultConfig(FFconfig* config) diff --git a/src/common/io.c b/src/common/io.c index 29690192e..97b02500a 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #define FF_IO_CACHE_VALUE_EXTENSION "ffcv" @@ -83,46 +82,9 @@ void ffPrintFormatString(FFinstance* instance, const char* moduleName, uint8_t m ffStrbufDestroy(&buffer); } -static void appendCacheDir(FFinstance* instance, FFstrbuf* buffer) -{ - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - - static FFstrbuf cacheDir; - static bool init = false; - - pthread_mutex_lock(&mutex); - - if(init) - { - pthread_mutex_unlock(&mutex); - ffStrbufAppend(buffer, &cacheDir); - return; - } - - ffStrbufInitA(&cacheDir, 64); - ffStrbufAppendS(&cacheDir, getenv("XDG_CACHE_HOME")); - - if(cacheDir.length == 0) - { - ffStrbufSetS(&cacheDir, instance->state.passwd->pw_dir); - ffStrbufAppendS(&cacheDir, "/.cache/"); - } - - mkdir(cacheDir.chars, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews - - ffStrbufAppendS(&cacheDir, "fastfetch/"); - mkdir(cacheDir.chars, S_IRWXU | S_IRGRP | S_IROTH); - - init = true; - - pthread_mutex_unlock(&mutex); - - ffStrbufAppend(buffer, &cacheDir); -} - void ffGetCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer) { - appendCacheDir(instance, buffer); + ffStrbufAppend(buffer, &instance->state.cacheDir); ffStrbufAppendS(buffer, moduleName); if(extension != NULL) diff --git a/src/fastfetch.h b/src/fastfetch.h index f55a5da36..11729c7b0 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -161,6 +161,7 @@ typedef struct FFstate struct sysinfo sysinfo; FFlist configDirs; + FFstrbuf cacheDir; } FFstate; typedef struct FFinstance