Skip to content

Commit 8fa09ca

Browse files
Refactored read methods
1 parent 6e731c8 commit 8fa09ca

File tree

12 files changed

+53
-39
lines changed

12 files changed

+53
-39
lines changed

src/common/caching.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define FF_CACHE_VALUE_EXTENSION "ffcv"
66
#define FF_CACHE_SPLIT_EXTENSION "ffcs"
77

8+
#define FF_CACHE_EXTENSION_STRUCT "ffcs2"
9+
810
static void getCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
911
{
1012
ffStrbufAppend(buffer, &instance->state.cacheDir);
@@ -22,7 +24,7 @@ static void readCacheFile(FFinstance* instance, const char* moduleName, const ch
2224
FFstrbuf path;
2325
ffStrbufInitA(&path, 64);
2426
getCacheFilePath(instance, moduleName, extension, &path);
25-
ffAppendFileContent(path.chars, buffer);
27+
ffAppendFileBuffer(path.chars, buffer);
2628
ffStrbufDestroy(&path);
2729
}
2830

src/common/io.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ static void createSubfolders(const char* fileName)
2222
ffStrbufDestroy(&path);
2323
}
2424

25-
bool ffWriteFDData(int fd, size_t dataSize, const void* data)
26-
{
27-
return write(fd, data, dataSize) != -1;
28-
}
2925

3026
bool ffWriteFDBuffer(int fd, const FFstrbuf* content)
3127
{
32-
return ffWriteFDData(fd, content->length, content->chars);
28+
return write(fd, content->chars, content->length) != -1;
3329
}
3430

3531
bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
@@ -46,7 +42,7 @@ bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data)
4642
return false;
4743
}
4844

49-
bool ret = ffWriteFDData(fd, dataSize, data);
45+
bool ret = write(fd, data, dataSize) != -1;
5046

5147
close(fd);
5248

@@ -58,7 +54,7 @@ bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer)
5854
return ffWriteFileData(fileName, buffer->length, buffer->chars);
5955
}
6056

61-
void ffAppendFDContent(int fd, FFstrbuf* buffer)
57+
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
6258
{
6359
ssize_t readed = 0;
6460

@@ -82,24 +78,40 @@ void ffAppendFDContent(int fd, FFstrbuf* buffer)
8278

8379
ffStrbufTrimRight(buffer, '\n');
8480
ffStrbufTrimRight(buffer, ' ');
81+
82+
return readed >= 0;
83+
}
84+
85+
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data)
86+
{
87+
int fd = open(fileName, O_RDONLY);
88+
if(fd == -1)
89+
return -1;
90+
91+
ssize_t readed = read(fd, data, dataSize);
92+
93+
close(fd);
94+
95+
return readed;
8596
}
8697

87-
bool ffAppendFileContent(const char* fileName, FFstrbuf* buffer)
98+
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
8899
{
89100
int fd = open(fileName, O_RDONLY);
90101
if(fd == -1)
91102
return false;
92103

93-
ffAppendFDContent(fd, buffer);
104+
bool ret = ffAppendFDBuffer(fd, buffer);
94105

95106
close(fd);
96-
return true;
107+
108+
return ret;
97109
}
98110

99-
bool ffGetFileContent(const char* fileName, FFstrbuf* buffer)
111+
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer)
100112
{
101113
ffStrbufClear(buffer);
102-
return ffAppendFileContent(fileName, buffer);
114+
return ffAppendFileBuffer(fileName, buffer);
103115
}
104116

105117
// Not thread safe!

src/common/processing.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
2929
//Parent
3030
close(pipes[1]);
3131
waitpid(childPid, NULL, 0);
32-
ffAppendFDContent(pipes[0], buffer);
32+
ffAppendFDBuffer(pipes[0], buffer);
3333
close(pipes[0]);
3434
}

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-
ffGetFileContent(procPath.chars, &loginuid);
340+
ffGetFileBuffer(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-
ffGetFileContent(procPath.chars, &processName);
351+
ffGetFileBuffer(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-
ffGetFileContent(dir->chars, &valueString);
35+
ffGetFileBuffer(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-
ffGetFileContent(dir->chars, &value->name);
54+
ffGetFileBuffer(dir->chars, &value->name);
5555
ffStrbufSubstrBefore(dir, dirLength);
5656

5757
ffStrbufAppendS(dir, "device/class");
58-
ffGetFileContent(dir->chars, &value->deviceClass);
58+
ffGetFileBuffer(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-
ffGetFileContent(cmdlineFilePath.chars, exe);
24+
ffGetFileBuffer(cmdlineFilePath.chars, exe);
2525
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
2626
ffStrbufTrimLeft(exe, '-'); //Happens in TTY
2727

src/fastfetch.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,14 @@ void ffListFeatures();
413413
void ffStartDetectionThreads(FFinstance* instance);
414414

415415
//common/io.c
416-
bool ffWriteFDData(int fd, size_t dataSize, const void* data);
417416
bool ffWriteFDBuffer(int fd, const FFstrbuf* content);
418417
bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data);
419418
bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer);
420419

421-
void ffAppendFDContent(int fd, FFstrbuf* buffer);
422-
bool ffAppendFileContent(const char* fileName, FFstrbuf* buffer); //returns true if open() succeeds. This is used to differentiate between <file not found> and <empty file>
423-
bool ffGetFileContent(const char* fileName, FFstrbuf* buffer);
420+
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer);
421+
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data);
422+
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer);
423+
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer);
424424

425425
bool ffFileExists(const char* fileName, mode_t mode);
426426
void ffSuppressIO(bool suppress); // Not thread safe!

src/logo/image/image.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static void readCachedStrbuf(FFLogoRequestData* requestData, FFstrbuf* result, c
375375
{
376376
uint32_t cacheDirLength = requestData->cacheDir.length;
377377
ffStrbufAppendS(&requestData->cacheDir, cacheFileName);
378-
ffAppendFileContent(requestData->cacheDir.chars, result);
378+
ffAppendFileBuffer(requestData->cacheDir.chars, result);
379379
ffStrbufSubstrBefore(&requestData->cacheDir, cacheDirLength);
380380
}
381381

src/logo/logo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static bool logoPrintFileIfExists(FFinstance* instance, bool doColorReplacement)
249249
FFstrbuf content;
250250
ffStrbufInitA(&content, 2047);
251251

252-
if(!ffAppendFileContent(instance->config.logoSource.chars, &content))
252+
if(!ffAppendFileBuffer(instance->config.logoSource.chars, &content))
253253
{
254254
ffStrbufDestroy(&content);
255255
return false;

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-
ffGetFileContent(dir->chars, &testBatteryBuffer);
26+
ffGetFileBuffer(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-
ffGetFileContent(dir->chars, &testBatteryBuffer);
37+
ffGetFileBuffer(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-
ffGetFileContent(dir->chars, &result->capacity);
52+
ffGetFileBuffer(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-
ffGetFileContent(dir->chars, &result->manufacturer);
66+
ffGetFileBuffer(dir->chars, &result->manufacturer);
6767
ffStrbufSubstrBefore(dir, dirLength);
6868

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

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

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

0 commit comments

Comments
 (0)