Skip to content

Commit af0c72a

Browse files
Methods for caching generic data
1 parent 8fa09ca commit af0c72a

File tree

9 files changed

+50
-25
lines changed

9 files changed

+50
-25
lines changed

src/common/caching.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#define FF_CACHE_VALUE_EXTENSION "ffcv"
66
#define FF_CACHE_SPLIT_EXTENSION "ffcs"
77

8-
#define FF_CACHE_EXTENSION_STRUCT "ffcs2"
8+
#define FF_CACHE_EXTENSION_DATA "ffcd"
99

10-
static void getCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
10+
static void getCacheFilePath(const FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
1111
{
1212
ffStrbufAppend(buffer, &instance->state.cacheDir);
1313
ffStrbufAppendS(buffer, moduleName);
@@ -202,3 +202,25 @@ void ffPrintAndWriteToCache(FFinstance* instance, const char* moduleName, const
202202
ffPrintAndAppendToCache(instance, moduleName, 0, moduleArgs, &cache, value, numArgs, arguments);
203203
ffCacheClose(&cache);
204204
}
205+
206+
void ffCachingWriteData(const FFinstance* instance, const char* moduleName, size_t dataSize, const void* data)
207+
{
208+
FFstrbuf path;
209+
ffStrbufInitA(&path, 128);
210+
getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path);
211+
ffWriteFileData(path.chars, dataSize, data);
212+
ffStrbufDestroy(&path);
213+
}
214+
215+
bool ffCachingReadData(const FFinstance* instance, const char* moduleName, size_t dataSize, void* data)
216+
{
217+
if(instance->config.recache)
218+
return false;
219+
220+
FFstrbuf path;
221+
ffStrbufInitA(&path, 128);
222+
getCacheFilePath(instance, moduleName, FF_CACHE_EXTENSION_DATA, &path);
223+
ssize_t result = ffReadFileData(path.chars, dataSize, data);
224+
ffStrbufDestroy(&path);
225+
return result > 0 && (size_t) result == dataSize;
226+
}

src/common/io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
8282
return readed >= 0;
8383
}
8484

85-
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data)
85+
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data)
8686
{
8787
int fd = open(fileName, O_RDONLY);
8888
if(fd == -1)
@@ -108,7 +108,7 @@ bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
108108
return ret;
109109
}
110110

111-
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer)
111+
bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer)
112112
{
113113
ffStrbufClear(buffer);
114114
return ffAppendFileBuffer(fileName, buffer);

src/detection/displayserver/wmde.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void getFromProcDir(const FFinstance* instance, FFDisplayServerResult* re
337337

338338
//Don't check for processes not owend by the current user.
339339
ffStrbufAppendS(&procPath, "/loginuid");
340-
ffGetFileBuffer(procPath.chars, &loginuid);
340+
ffReadFileBuffer(procPath.chars, &loginuid);
341341
if(ffStrbufComp(&userID, &loginuid) != 0)
342342
{
343343
ffStrbufSubstrBefore(&procPath, procPathLength);
@@ -348,7 +348,7 @@ static void getFromProcDir(const FFinstance* instance, FFDisplayServerResult* re
348348

349349
//We check the cmdline for the process name, because it is not trimmed.
350350
ffStrbufAppendS(&procPath, "/cmdline");
351-
ffGetFileBuffer(procPath.chars, &processName);
351+
ffReadFileBuffer(procPath.chars, &processName);
352352
ffStrbufSubstrBeforeFirstC(&processName, '\0'); //Trim the arguments
353353
ffStrbufSubstrAfterLastC(&processName, '/');
354354

src/detection/temps.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
3232
continue;
3333

3434
ffStrbufAppendS(dir, dirent->d_name);
35-
ffGetFileBuffer(dir->chars, &valueString);
35+
ffReadFileBuffer(dir->chars, &valueString);
3636
ffStrbufSubstrBefore(dir, dirLength);
3737

3838
//ffStrbufToDouble() returns NaN if the string couldn't be parsed
@@ -51,11 +51,11 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
5151
return false;
5252

5353
ffStrbufAppendS(dir, "name");
54-
ffGetFileBuffer(dir->chars, &value->name);
54+
ffReadFileBuffer(dir->chars, &value->name);
5555
ffStrbufSubstrBefore(dir, dirLength);
5656

5757
ffStrbufAppendS(dir, "device/class");
58-
ffGetFileBuffer(dir->chars, &value->deviceClass);
58+
ffReadFileBuffer(dir->chars, &value->deviceClass);
5959
ffStrbufSubstrBefore(dir, dirLength);
6060

6161
return value->name.length > 0 || value->deviceClass.length > 0;

src/detection/terminalShell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void getProcessInformation(const char* pid, FFstrbuf* processName, FFstrb
2121
ffStrbufAppendS(&cmdlineFilePath, pid);
2222
ffStrbufAppendS(&cmdlineFilePath, "/cmdline");
2323

24-
ffGetFileBuffer(cmdlineFilePath.chars, exe);
24+
ffReadFileBuffer(cmdlineFilePath.chars, exe);
2525
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
2626
ffStrbufTrimLeft(exe, '-'); //Happens in TTY
2727

src/fastfetch.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data);
418418
bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer);
419419

420420
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer);
421-
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data);
421+
ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data);
422422
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer);
423-
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer);
423+
bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer);
424424

425425
bool ffFileExists(const char* fileName, mode_t mode);
426426
void ffSuppressIO(bool suppress); // Not thread safe!
@@ -446,6 +446,9 @@ bool ffPrintFromCache(FFinstance* instance, const char* moduleName, const FFModu
446446
void ffPrintAndAppendToCache(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFcache* cache, const FFstrbuf* value, uint32_t numArgs, const FFformatarg* arguments);
447447
void ffPrintAndWriteToCache(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs, const FFstrbuf* value, uint32_t numArgs, const FFformatarg* arguments);
448448

449+
void ffCachingWriteData(const FFinstance* instance, const char* moduleName, size_t dataSize, const void* data);
450+
bool ffCachingReadData(const FFinstance* instance, const char* moduleName, size_t dataSize, void* data);
451+
449452
//common/properties.c
450453
bool ffParsePropLine(const char* line, const char* start, FFstrbuf* buffer);
451454
bool ffParsePropLines(const char* lines, const char* start, FFstrbuf* buffer);

src/modules/battery.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
2323

2424
//type must exist and be "Battery"
2525
ffStrbufAppendS(dir, "/type");
26-
ffGetFileBuffer(dir->chars, &testBatteryBuffer);
26+
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
2727
ffStrbufSubstrBefore(dir, dirLength);
2828

2929
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Battery") != 0)
@@ -34,7 +34,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
3434

3535
//scope may not exist or must not be "Device"
3636
ffStrbufAppendS(dir, "/scope");
37-
ffGetFileBuffer(dir->chars, &testBatteryBuffer);
37+
ffReadFileBuffer(dir->chars, &testBatteryBuffer);
3838
ffStrbufSubstrBefore(dir, dirLength);
3939

4040
if(ffStrbufIgnCaseCompS(&testBatteryBuffer, "Device") == 0)
@@ -49,7 +49,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
4949
//capacity must exist and be not empty
5050
ffStrbufInit(&result->capacity);
5151
ffStrbufAppendS(dir, "/capacity");
52-
ffGetFileBuffer(dir->chars, &result->capacity);
52+
ffReadFileBuffer(dir->chars, &result->capacity);
5353
ffStrbufSubstrBefore(dir, dirLength);
5454

5555
if(result->capacity.length == 0)
@@ -63,22 +63,22 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
6363

6464
ffStrbufInit(&result->manufacturer);
6565
ffStrbufAppendS(dir, "/manufacturer");
66-
ffGetFileBuffer(dir->chars, &result->manufacturer);
66+
ffReadFileBuffer(dir->chars, &result->manufacturer);
6767
ffStrbufSubstrBefore(dir, dirLength);
6868

6969
ffStrbufInit(&result->modelName);
7070
ffStrbufAppendS(dir, "/model_name");
71-
ffGetFileBuffer(dir->chars, &result->modelName);
71+
ffReadFileBuffer(dir->chars, &result->modelName);
7272
ffStrbufSubstrBefore(dir, dirLength);
7373

7474
ffStrbufInit(&result->technology);
7575
ffStrbufAppendS(dir, "/technology");
76-
ffGetFileBuffer(dir->chars, &result->technology);
76+
ffReadFileBuffer(dir->chars, &result->technology);
7777
ffStrbufSubstrBefore(dir, dirLength);
7878

7979
ffStrbufInit(&result->status);
8080
ffStrbufAppendS(dir, "/status");
81-
ffGetFileBuffer(dir->chars, &result->status);
81+
ffReadFileBuffer(dir->chars, &result->status);
8282
ffStrbufSubstrBefore(dir, dirLength);
8383
}
8484

src/modules/cpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ static double getGhz(const char* policyFile, const char* cpuFile)
1010
FFstrbuf content;
1111
ffStrbufInit(&content);
1212

13-
ffGetFileBuffer(policyFile, &content);
13+
ffReadFileBuffer(policyFile, &content);
1414
if(content.length == 0)
15-
ffGetFileBuffer(cpuFile, &content);
15+
ffReadFileBuffer(cpuFile, &content);
1616

1717
double herz = ffStrbufToDouble(&content);
1818

src/modules/host.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ static bool hostValueSet(FFstrbuf* value)
3939

4040
static void getHostValue(const char* devicesPath, const char* classPath, FFstrbuf* buffer)
4141
{
42-
ffGetFileBuffer(devicesPath, buffer);
42+
ffReadFileBuffer(devicesPath, buffer);
4343

4444
if(buffer->length == 0)
45-
ffGetFileBuffer(classPath, buffer);
45+
ffReadFileBuffer(classPath, buffer);
4646
}
4747
#endif
4848

@@ -67,10 +67,10 @@ void ffPrintHost(FFinstance* instance)
6767
getHostValue("/sys/devices/virtual/dmi/id/product_name", "/sys/class/dmi/id/product_name", &product_name);
6868

6969
if(product_name.length == 0)
70-
ffGetFileBuffer("/sys/firmware/devicetree/base/model", &product_name);
70+
ffReadFileBuffer("/sys/firmware/devicetree/base/model", &product_name);
7171

7272
if(product_name.length == 0)
73-
ffGetFileBuffer("/tmp/sysinfo/model", &product_name);
73+
ffReadFileBuffer("/tmp/sysinfo/model", &product_name);
7474

7575
if(ffStrbufStartsWithS(&product_name, "Standard PC"))
7676
{

0 commit comments

Comments
 (0)