mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Methods for caching generic data
This commit is contained in:
+24
-2
@@ -5,9 +5,9 @@
|
||||
#define FF_CACHE_VALUE_EXTENSION "ffcv"
|
||||
#define FF_CACHE_SPLIT_EXTENSION "ffcs"
|
||||
|
||||
#define FF_CACHE_EXTENSION_STRUCT "ffcs2"
|
||||
#define FF_CACHE_EXTENSION_DATA "ffcd"
|
||||
|
||||
static void getCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
|
||||
static void getCacheFilePath(const FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
|
||||
{
|
||||
ffStrbufAppend(buffer, &instance->state.cacheDir);
|
||||
ffStrbufAppendS(buffer, moduleName);
|
||||
@@ -202,3 +202,25 @@ void ffPrintAndWriteToCache(FFinstance* instance, const char* moduleName, const
|
||||
ffPrintAndAppendToCache(instance, moduleName, 0, moduleArgs, &cache, value, numArgs, arguments);
|
||||
ffCacheClose(&cache);
|
||||
}
|
||||
|
||||
void ffCachingWriteData(const FFinstance* instance, const char* moduleName, size_t dataSize, const void* data)
|
||||
{
|
||||
FFstrbuf path;
|
||||
ffStrbufInitA(&path, 128);
|
||||
getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path);
|
||||
ffWriteFileData(path.chars, dataSize, data);
|
||||
ffStrbufDestroy(&path);
|
||||
}
|
||||
|
||||
bool ffCachingReadData(const FFinstance* instance, const char* moduleName, size_t dataSize, void* data)
|
||||
{
|
||||
if(instance->config.recache)
|
||||
return false;
|
||||
|
||||
FFstrbuf path;
|
||||
ffStrbufInitA(&path, 128);
|
||||
getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path);
|
||||
ssize_t result = ffReadFileData(path.chars, dataSize, data);
|
||||
ffStrbufDestroy(&path);
|
||||
return result > 0 && (size_t) result == dataSize;
|
||||
}
|
||||
|
||||
+2
-2
@@ -82,7 +82,7 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
|
||||
return readed >= 0;
|
||||
}
|
||||
|
||||
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data)
|
||||
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
|
||||
{
|
||||
int fd = open(fileName, O_RDONLY);
|
||||
if(fd == -1)
|
||||
@@ -108,7 +108,7 @@ bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer)
|
||||
bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer)
|
||||
{
|
||||
ffStrbufClear(buffer);
|
||||
return ffAppendFileBuffer(fileName, buffer);
|
||||
|
||||
@@ -337,7 +337,7 @@ static void getFromProcDir(const FFinstance* instance, FFDisplayServerResult* re
|
||||
|
||||
//Don't check for processes not owend by the current user.
|
||||
ffStrbufAppendS(&procPath, "/loginuid");
|
||||
ffGetFileBuffer(procPath.chars, &loginuid);
|
||||
ffReadFileBuffer(procPath.chars, &loginuid);
|
||||
if(ffStrbufComp(&userID, &loginuid) != 0)
|
||||
{
|
||||
ffStrbufSubstrBefore(&procPath, procPathLength);
|
||||
@@ -348,7 +348,7 @@ static void getFromProcDir(const FFinstance* instance, FFDisplayServerResult* re
|
||||
|
||||
//We check the cmdline for the process name, because it is not trimmed.
|
||||
ffStrbufAppendS(&procPath, "/cmdline");
|
||||
ffGetFileBuffer(procPath.chars, &processName);
|
||||
ffReadFileBuffer(procPath.chars, &processName);
|
||||
ffStrbufSubstrBeforeFirstC(&processName, '\0'); //Trim the arguments
|
||||
ffStrbufSubstrAfterLastC(&processName, '/');
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
|
||||
continue;
|
||||
|
||||
ffStrbufAppendS(dir, dirent->d_name);
|
||||
ffGetFileBuffer(dir->chars, &valueString);
|
||||
ffReadFileBuffer(dir->chars, &valueString);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
//ffStrbufToDouble() returns NaN if the string couldn't be parsed
|
||||
@@ -51,11 +51,11 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
|
||||
return false;
|
||||
|
||||
ffStrbufAppendS(dir, "name");
|
||||
ffGetFileBuffer(dir->chars, &value->name);
|
||||
ffReadFileBuffer(dir->chars, &value->name);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
ffStrbufAppendS(dir, "device/class");
|
||||
ffGetFileBuffer(dir->chars, &value->deviceClass);
|
||||
ffReadFileBuffer(dir->chars, &value->deviceClass);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
return value->name.length > 0 || value->deviceClass.length > 0;
|
||||
|
||||
@@ -21,7 +21,7 @@ static void getProcessInformation(const char* pid, FFstrbuf* processName, FFstrb
|
||||
ffStrbufAppendS(&cmdlineFilePath, pid);
|
||||
ffStrbufAppendS(&cmdlineFilePath, "/cmdline");
|
||||
|
||||
ffGetFileBuffer(cmdlineFilePath.chars, exe);
|
||||
ffReadFileBuffer(cmdlineFilePath.chars, exe);
|
||||
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
|
||||
ffStrbufTrimLeft(exe, '-'); //Happens in TTY
|
||||
|
||||
|
||||
+5
-2
@@ -418,9 +418,9 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data);
|
||||
bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer);
|
||||
|
||||
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer);
|
||||
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data);
|
||||
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data);
|
||||
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer);
|
||||
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer);
|
||||
bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer);
|
||||
|
||||
bool ffFileExists(const char* fileName, mode_t mode);
|
||||
void ffSuppressIO(bool suppress); // Not thread safe!
|
||||
@@ -446,6 +446,9 @@ bool ffPrintFromCache(FFinstance* instance, const char* moduleName, const FFModu
|
||||
void ffPrintAndAppendToCache(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFcache* cache, const FFstrbuf* value, uint32_t numArgs, const FFformatarg* arguments);
|
||||
void ffPrintAndWriteToCache(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs, const FFstrbuf* value, uint32_t numArgs, const FFformatarg* arguments);
|
||||
|
||||
void ffCachingWriteData(const FFinstance* instance, const char* moduleName, size_t dataSize, const void* data);
|
||||
bool ffCachingReadData(const FFinstance* instance, const char* moduleName, size_t dataSize, void* data);
|
||||
|
||||
//common/properties.c
|
||||
bool ffParsePropLine(const char* line, const char* start, FFstrbuf* buffer);
|
||||
bool ffParsePropLines(const char* lines, const char* start, FFstrbuf* buffer);
|
||||
|
||||
@@ -23,7 +23,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
|
||||
|
||||
//type must exist and be "Battery"
|
||||
ffStrbufAppendS(dir, "/type");
|
||||
ffGetFileBuffer(dir->chars, &testBatteryBuffer);
|
||||
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0)
|
||||
@@ -34,7 +34,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
|
||||
|
||||
//scope may not exist or must not be "Device"
|
||||
ffStrbufAppendS(dir, "/scope");
|
||||
ffGetFileBuffer(dir->chars, &testBatteryBuffer);
|
||||
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0)
|
||||
@@ -49,7 +49,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
|
||||
//capacity must exist and be not empty
|
||||
ffStrbufInit(&result->capacity);
|
||||
ffStrbufAppendS(dir, "/capacity");
|
||||
ffGetFileBuffer(dir->chars, &result->capacity);
|
||||
ffReadFileBuffer(dir->chars, &result->capacity);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
if(result->capacity.length == 0)
|
||||
@@ -63,22 +63,22 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
|
||||
|
||||
ffStrbufInit(&result->manufacturer);
|
||||
ffStrbufAppendS(dir, "/manufacturer");
|
||||
ffGetFileBuffer(dir->chars, &result->manufacturer);
|
||||
ffReadFileBuffer(dir->chars, &result->manufacturer);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
ffStrbufInit(&result->modelName);
|
||||
ffStrbufAppendS(dir, "/model_name");
|
||||
ffGetFileBuffer(dir->chars, &result->modelName);
|
||||
ffReadFileBuffer(dir->chars, &result->modelName);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
ffStrbufInit(&result->technology);
|
||||
ffStrbufAppendS(dir, "/technology");
|
||||
ffGetFileBuffer(dir->chars, &result->technology);
|
||||
ffReadFileBuffer(dir->chars, &result->technology);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
|
||||
ffStrbufInit(&result->status);
|
||||
ffStrbufAppendS(dir, "/status");
|
||||
ffGetFileBuffer(dir->chars, &result->status);
|
||||
ffReadFileBuffer(dir->chars, &result->status);
|
||||
ffStrbufSubstrBefore(dir, dirLength);
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -10,9 +10,9 @@ static double getGhz(const char* policyFile, const char* cpuFile)
|
||||
FFstrbuf content;
|
||||
ffStrbufInit(&content);
|
||||
|
||||
ffGetFileBuffer(policyFile, &content);
|
||||
ffReadFileBuffer(policyFile, &content);
|
||||
if(content.length == 0)
|
||||
ffGetFileBuffer(cpuFile, &content);
|
||||
ffReadFileBuffer(cpuFile, &content);
|
||||
|
||||
double herz = ffStrbufToDouble(&content);
|
||||
|
||||
|
||||
+4
-4
@@ -39,10 +39,10 @@ static bool hostValueSet(FFstrbuf* value)
|
||||
|
||||
static void getHostValue(const char* devicesPath, const char* classPath, FFstrbuf* buffer)
|
||||
{
|
||||
ffGetFileBuffer(devicesPath, buffer);
|
||||
ffReadFileBuffer(devicesPath, buffer);
|
||||
|
||||
if(buffer->length == 0)
|
||||
ffGetFileBuffer(classPath, buffer);
|
||||
ffReadFileBuffer(classPath, buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -67,10 +67,10 @@ void ffPrintHost(FFinstance* instance)
|
||||
getHostValue("/sys/devices/virtual/dmi/id/product_name", "/sys/class/dmi/id/product_name", &product_name);
|
||||
|
||||
if(product_name.length == 0)
|
||||
ffGetFileBuffer("/sys/firmware/devicetree/base/model", &product_name);
|
||||
ffReadFileBuffer("/sys/firmware/devicetree/base/model", &product_name);
|
||||
|
||||
if(product_name.length == 0)
|
||||
ffGetFileBuffer("/tmp/sysinfo/model", &product_name);
|
||||
ffReadFileBuffer("/tmp/sysinfo/model", &product_name);
|
||||
|
||||
if(ffStrbufStartsWithS(&product_name, "Standard PC"))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user