much more efficient caching

This commit is contained in:
Linus Dierheimer
2021-04-15 18:59:26 +02:00
parent ed598eaa35
commit 9bc1f2a116
35 changed files with 974 additions and 645 deletions
+15 -39
View File
@@ -1,11 +1,7 @@
#include "fastfetch.h"
void ffParseFormatStringV(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArgs, va_list argp)
void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFstrbuf* error, uint32_t numArgs, const FFformatarg* arguments)
{
FFformatarg arguments[numArgs];
for(uint32_t i = 0; i < numArgs; i++)
arguments[i] = (FFformatarg) va_arg(argp, FFformatarg);
uint32_t argCounter = 1; //First arg is 1 in fomat string
for(uint32_t i = 0; i < formatstr->length; ++i)
@@ -50,12 +46,24 @@ void ffParseFormatStringV(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArg
while(formatstr->chars[i] != '}' && i < formatstr->length)
ffStrbufAppendC(&argnumstr, formatstr->chars[i++]);
if(
ffStrbufIgnCaseCompS(&argnumstr, "e") == 0 ||
ffStrbufIgnCaseCompS(&argnumstr, "error") == 0 ||
ffStrbufIgnCaseCompS(&argnumstr, "0") == 0
) {
ffStrbufAppend(buffer, error);
ffStrbufDestroy(&argnumstr);
continue;
}
//Test if argnumstr is valid
if(
argnumstr.length == 0 ||
ffStrbufGetC(&argnumstr, 0) == '-' ||
sscanf(argnumstr.chars, "%u", &argIndex) != 1 ||
argIndex > numArgs
) {
//Not valid
ffStrbufAppendC(buffer, '{');
ffStrbufAppend(buffer, &argnumstr);
if(formatstr->chars[i] == '}') // We dont have a closing { when ending because whole format string is over
@@ -67,9 +75,6 @@ void ffParseFormatStringV(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArg
ffStrbufDestroy(&argnumstr);
}
if(argIndex == 0)
argIndex = 1;
FFformatarg arg = arguments[argIndex - 1];
if(arg.type == FF_FORMAT_ARG_TYPE_INT)
@@ -77,14 +82,14 @@ void ffParseFormatStringV(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArg
else if(arg.type == FF_FORMAT_ARG_TYPE_UINT)
ffStrbufAppendF(buffer, "%u", *(uint32_t*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_UINT8)
ffStrbufAppendF(buffer, "%u", *(uint8_t*)arg.value);
ffStrbufAppendF(buffer, "%hhu", *(uint8_t*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_STRING)
ffStrbufAppendF(buffer, "%s", (const char*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_STRBUF)
ffStrbufAppend(buffer, (FFstrbuf*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_DOUBLE)
ffStrbufAppendF(buffer, "%g", *(double*)arg.value);
else
else if(arg.type != FF_FORMAT_ARG_TYPE_NULL)
{
fprintf(stderr, "Error: format string \"%s\" argument is not implemented\n", formatstr->chars);
ffStrbufDestroy(buffer);
@@ -94,32 +99,3 @@ void ffParseFormatStringV(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArg
ffStrbufTrimRight(buffer, ' ');
}
void ffParseFormatString(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArgs, ...)
{
va_list argp;
va_start(argp, numArgs);
ffParseFormatStringV(buffer, formatstr, numArgs, argp);
va_end(argp);
}
void ffPrintFormatString(FFinstance* instance, FFstrbuf* customKey, const char* defKey, FFstrbuf* formatstr, uint32_t numArgs, ...)
{
FFstrbuf buffer;
ffStrbufInitA(&buffer, 256);
va_list argp;
va_start(argp, numArgs);
ffParseFormatStringV(&buffer, formatstr, numArgs, argp);
va_end(argp);
if(buffer.length > 0)
{
ffPrintLogoAndKey(instance, customKey, defKey);
ffStrbufPutTo(&buffer, stdout);
}
ffStrbufDestroy(&buffer);
}
+1
View File
@@ -79,6 +79,7 @@ void ffInitInstance(FFinstance* instance)
{
initState(&instance->state);
defaultConfig(&instance->config);
ffCacheValidate(instance);
}
static void ffCleanup(FFinstance* instance)
+317 -105
View File
@@ -1,44 +1,338 @@
#include "fastfetch.h"
#include <malloc.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
void ffPrintKey(FFinstance* instance, FFstrbuf* customKey, const char* defKey)
{
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
ffStrbufWriteTo(&instance->config.color, stdout);
if(customKey == NULL || customKey->length == 0)
fputs(defKey, stdout);
else
ffStrbufWriteTo(customKey, stdout);
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
ffStrbufWriteTo(&instance->config.seperator, stdout);
}
void ffPrintLogoAndKey(FFinstance* instance, FFstrbuf* customKey, const char* defKey)
void ffPrintLogoAndKey(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat)
{
ffPrintLogoLine(instance);
ffPrintKey(instance, customKey, defKey);
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
ffStrbufWriteTo(&instance->config.color, stdout);
FF_STRBUF_CREATE(key);
if(customKeyFormat == NULL || customKeyFormat->length == 0)
{
ffStrbufAppendS(&key, moduleName);
if(moduleIndex > 0)
ffStrbufAppendF(&key, " %hhu", moduleIndex);
}
else
{
ffParseFormatString(&key, customKeyFormat, NULL, 1, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_UINT8, &moduleIndex}
});
}
ffStrbufWriteTo(&key, stdout);
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
ffStrbufWriteTo(&instance->config.seperator, stdout);
ffStrbufDestroy(&key);
}
void ffPrintError(FFinstance* instance, FFstrbuf* customKey, const char* defKey, const char* message, ...)
void ffPrintError(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numFormatArgs, const char* message, ...)
{
if(!instance->config.showErrors)
return;
ffPrintLogoAndKey(instance, customKey, defKey);
va_list arguments;
va_start(arguments, message);
printf(FASTFETCH_TEXT_MODIFIER_ERROR);
vprintf(message, arguments);
puts(FASTFETCH_TEXT_MODIFIER_RESET);
if((formatString == NULL || formatString->length == 0) && instance->config.showErrors)
{
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout);
vprintf(message, arguments);
puts(FASTFETCH_TEXT_MODIFIER_ERROR);
}
else
{
FF_STRBUF_CREATE(error);
ffStrbufAppendVF(&error, message, arguments);
FFformatarg nullArgs[numFormatArgs];
for(uint32_t i = 0; i < numFormatArgs; i++)
{
nullArgs[i].type = FF_FORMAT_ARG_TYPE_NULL;
nullArgs[i].value = NULL;
}
ffPrintFormatString(instance, moduleName, moduleIndex, customKeyFormat, formatString, &error, numFormatArgs, nullArgs);
ffStrbufDestroy(&error);
}
va_end(arguments);
}
void ffPrintFormatString(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, const FFstrbuf* error, uint32_t numArgs, const FFformatarg* arguments)
{
FFstrbuf buffer;
ffStrbufInitA(&buffer, 256);
ffParseFormatString(&buffer, formatString, error, numArgs, arguments);
if(buffer.length > 0)
{
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
ffStrbufPutTo(&buffer, stdout);
}
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);
}
static inline void getCacheFileValue(FFinstance* instance, const char* moduleName, FFstrbuf* cacheFilePath)
{
appendCacheDir(instance, cacheFilePath);
ffStrbufAppendS(cacheFilePath, moduleName);
ffStrbufAppendS(cacheFilePath, ".ffcv");
}
static inline void getCacheFileSplit(FFinstance* instance, const char* moduleName, FFstrbuf* cacheFilePath)
{
appendCacheDir(instance, cacheFilePath);
ffStrbufAppendS(cacheFilePath, moduleName);
ffStrbufAppendS(cacheFilePath, ".ffcs");
}
bool printCachedValue(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat)
{
FFstrbuf cacheFilePath;
ffStrbufInitA(&cacheFilePath, 64);
getCacheFileValue(instance, moduleName, &cacheFilePath);
FFstrbuf content;
ffStrbufInitA(&content, 512);
ffAppendFileContent(cacheFilePath.chars, &content);
ffStrbufTrimRight(&content, '\0'); //Strbuf always appends a '\0' at the end. We want the last null byte to be at the position of the length
if(content.length == 0)
return false;
uint8_t moduleCounter = 1;
uint32_t lastIndex = 0;
while(lastIndex < content.length)
{
uint32_t nullByteIndex = ffStrbufFirstIndexAfterC(&content, lastIndex, '\0');
uint8_t moduleIndex = (moduleCounter == 1 && nullByteIndex == content.length) ? 0 : moduleCounter;
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
puts(content.chars + lastIndex);
lastIndex = nullByteIndex + 1;
++moduleCounter;
}
ffStrbufDestroy(&content);
ffStrbufDestroy(&cacheFilePath);
return moduleCounter > 1;
}
bool printCachedFormat(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numArgs)
{
FFstrbuf cacheFilePath;
ffStrbufInitA(&cacheFilePath, 64);
getCacheFileSplit(instance, moduleName, &cacheFilePath);
FFstrbuf content;
ffStrbufInitA(&content, 512);
ffAppendFileContent(cacheFilePath.chars, &content);
ffStrbufTrimRight(&content, '\0'); //Strbuf always appends a '\0' at the end. We want the last null byte to be at the position of the length
if(content.length == 0)
return false;
uint8_t moduleCounter = 1;
FFformatarg arguments[numArgs];
uint32_t argumentCounter = 0;
uint32_t lastIndex = 0;
while(lastIndex < content.length)
{
arguments[argumentCounter].type = FF_FORMAT_ARG_TYPE_STRING;
arguments[argumentCounter].value = &content.chars[lastIndex];
++argumentCounter;
uint32_t nullByteIndex = ffStrbufFirstIndexAfterC(&content, lastIndex, '\0');
if(argumentCounter == numArgs)
{
uint8_t moduleIndex = (moduleCounter == 1 && nullByteIndex == content.length) ? 0 : moduleCounter;
ffPrintFormatString(instance, moduleName, moduleIndex, customKeyFormat, formatString, NULL, numArgs, arguments);
++moduleCounter;
argumentCounter = 0;
}
lastIndex = nullByteIndex + 1;
}
ffStrbufDestroy(&content);
ffStrbufDestroy(&cacheFilePath);
return moduleCounter > 1;
}
bool ffPrintFromCache(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numArgs)
{
if(instance->config.recache)
return false;
if(formatString == NULL || formatString->length == 0)
return printCachedValue(instance, moduleName, customKeyFormat);
else
return printCachedFormat(instance, moduleName, customKeyFormat, formatString, numArgs);
}
void ffPrintAndAppendToCache(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, FFcache* cache, const FFstrbuf* value, const FFstrbuf* formatString, uint32_t numArgs, const FFformatarg* arguments)
{
if(formatString == NULL || formatString->length == 0)
{
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
ffStrbufPutTo(value, stdout);
}
else
{
ffPrintFormatString(instance, moduleName, moduleIndex, customKeyFormat, formatString, NULL, numArgs, arguments);
}
if(cache->value != NULL)
{
ffStrbufWriteTo(value, cache->value);
fputc('\0', cache->value);
}
if(cache->split == NULL)
return;
for(uint32_t i = 0; i < numArgs; i++)
{
FFformatarg arg = arguments[i];
if(arg.type == FF_FORMAT_ARG_TYPE_INT)
fprintf(cache->split, "%i", *(int*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_UINT)
fprintf(cache->split, "%u", *(uint32_t*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_UINT8)
fprintf(cache->split, "%hhu", *(uint8_t*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_STRING)
fprintf(cache->split, "%s", (const char*)arg.value);
else if(arg.type == FF_FORMAT_ARG_TYPE_STRBUF)
fprintf(cache->split, "%s", ((FFstrbuf*)arg.value)->chars);
else if(arg.type == FF_FORMAT_ARG_TYPE_DOUBLE)
fprintf(cache->split, "%g", *(double*)arg.value);
fputc('\0', cache->split);
}
fputc('\0', cache->split);
}
void ffPrintAndSaveToCache(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat, const FFstrbuf* value, const FFstrbuf* formatString, uint32_t numArgs, const FFformatarg* arguments)
{
FFcache cache;
ffCacheOpenWrite(instance, moduleName, &cache);
ffPrintAndAppendToCache(instance, moduleName, 0, customKeyFormat, &cache, value, formatString, numArgs, arguments);
ffCacheClose(&cache);
}
void ffCacheValidate(FFinstance* instance)
{
FFstrbuf cacheFilePath;
ffStrbufInitA(&cacheFilePath, 64);
appendCacheDir(instance, &cacheFilePath);
ffStrbufAppendS(&cacheFilePath, "cacheversion.ffv");
FFstrbuf content;
ffStrbufInit(&content);
ffAppendFileContent(cacheFilePath.chars, &content);
if(ffStrbufCompS(&content, FASTFETCH_PROJECT_VERSION) == 0)
{
ffStrbufDestroy(&content);
ffStrbufDestroy(&cacheFilePath);
return;
}
instance->config.recache = true;
FILE* versionFile = fopen(cacheFilePath.chars, "w");
if(versionFile == NULL)
return;
fputs(FASTFETCH_PROJECT_VERSION, versionFile);
fclose(versionFile);
ffStrbufDestroy(&content);
ffStrbufDestroy(&cacheFilePath);
}
void ffCacheOpenWrite(FFinstance* instance, const char* moduleName, FFcache* cache)
{
FFstrbuf cacheFileValue;
ffStrbufInitA(&cacheFileValue, 64);
getCacheFileValue(instance, moduleName, &cacheFileValue);
cache->value = fopen(cacheFileValue.chars, "w");
ffStrbufDestroy(&cacheFileValue);
FFstrbuf cacheFileSplit;
ffStrbufInitA(&cacheFileSplit, 64);
getCacheFileSplit(instance, moduleName, &cacheFileSplit);
cache->split = fopen(cacheFileSplit.chars, "w");
ffStrbufDestroy(&cacheFileSplit);
}
void ffCacheClose(FFcache* cache)
{
if(cache->value != NULL)
fclose(cache->value);
if(cache->split != NULL)
fclose(cache->split);
}
void ffParsePropFile(const char* fileName, const char* regex, char* buffer)
{
buffer[0] = '\0'; //If an error occures, this is the indicator
@@ -101,85 +395,3 @@ void ffGetFileContent(const char* fileName, FFstrbuf* buffer)
ffStrbufClear(buffer);
ffAppendFileContent(fileName, buffer);
}
static const FFstrbuf* getCacheDir(FFinstance* instance)
{
static FFstrbuf cacheDir;
static bool init = false;
if(init)
return &cacheDir;
ffStrbufInitAS(&cacheDir, 64, 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;
return &cacheDir;
}
bool ffPrintCachedValue(FFinstance* instance, FFstrbuf* customKey, const char* defKey)
{
if(instance->config.recache)
return false;
FFstrbuf cacheFile;
ffStrbufInitA(&cacheFile, 128);
ffStrbufAppend(&cacheFile, getCacheDir(instance));
ffStrbufAppendS(&cacheFile, defKey);
FFstrbuf value;
ffStrbufInitA(&value, 128);
ffGetFileContent(cacheFile.chars, &value);
ffStrbufDestroy(&cacheFile);
if(value.length == 0)
{
ffStrbufDestroy(&value);
return false;
}
ffPrintLogoAndKey(instance, customKey, defKey);
ffStrbufPutTo(&value, stdout);
ffStrbufDestroy(&value);
return true;
}
void ffPrintAndSaveCachedValue(FFinstance* instance, FFstrbuf* customKey, const char* defKey, FFstrbuf* value)
{
if(value->length == 0)
return;
ffPrintLogoAndKey(instance, customKey, defKey);
ffStrbufPutTo(value, stdout);
if(!instance->config.cacheSave)
return;
FFstrbuf cacheFile;
ffStrbufInit(&cacheFile);
ffStrbufAppend(&cacheFile, getCacheDir(instance));
ffStrbufAppendS(&cacheFile, defKey);
int fd = open(cacheFile.chars, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(fd == -1)
return;
bool failed = write(fd, value->chars, value->length) != value->length;
close(fd);
if(failed)
unlink(cacheFile.chars);
ffStrbufDestroy(&cacheFile);
}