Refactored read methods

This commit is contained in:
Linus Dierheimer
2022-06-20 16:27:33 +02:00
parent 6e731c80d0
commit 8fa09ca093
12 changed files with 53 additions and 39 deletions
+3 -1
View File
@@ -5,6 +5,8 @@
#define FF_CACHE_VALUE_EXTENSION "ffcv"
#define FF_CACHE_SPLIT_EXTENSION "ffcs"
#define FF_CACHE_EXTENSION_STRUCT "ffcs2"
static void getCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
{
ffStrbufAppend(buffer, &instance->state.cacheDir);
@@ -22,7 +24,7 @@ static void readCacheFile(FFinstance* instance, const char* moduleName, const ch
FFstrbuf path;
ffStrbufInitA(&path, 64);
getCacheFilePath(instance, moduleName, extension, &path);
ffAppendFileContent(path.chars, buffer);
ffAppendFileBuffer(path.chars, buffer);
ffStrbufDestroy(&path);
}
+24 -12
View File
@@ -22,14 +22,10 @@ static void createSubfolders(const char* fileName)
ffStrbufDestroy(&path);
}
bool ffWriteFDData(int fd, size_t dataSize, const void* data)
{
return write(fd, data, dataSize) != -1;
}
bool ffWriteFDBuffer(int fd, const FFstrbuf* content)
{
return ffWriteFDData(fd, content->length, content->chars);
return write(fd, content->chars, content->length) != -1;
}
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)
return false;
}
bool ret = ffWriteFDData(fd, dataSize, data);
bool ret = write(fd, data, dataSize) != -1;
close(fd);
@@ -58,7 +54,7 @@ bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer)
return ffWriteFileData(fileName, buffer->length, buffer->chars);
}
void ffAppendFDContent(int fd, FFstrbuf* buffer)
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer)
{
ssize_t readed = 0;
@@ -82,24 +78,40 @@ void ffAppendFDContent(int fd, FFstrbuf* buffer)
ffStrbufTrimRight(buffer, '\n');
ffStrbufTrimRight(buffer, ' ');
return readed >= 0;
}
bool ffAppendFileContent(const char* fileName, FFstrbuf* buffer)
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data)
{
int fd = open(fileName, O_RDONLY);
if(fd == -1)
return -1;
ssize_t readed = read(fd, data, dataSize);
close(fd);
return readed;
}
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer)
{
int fd = open(fileName, O_RDONLY);
if(fd == -1)
return false;
ffAppendFDContent(fd, buffer);
bool ret = ffAppendFDBuffer(fd, buffer);
close(fd);
return true;
return ret;
}
bool ffGetFileContent(const char* fileName, FFstrbuf* buffer)
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer)
{
ffStrbufClear(buffer);
return ffAppendFileContent(fileName, buffer);
return ffAppendFileBuffer(fileName, buffer);
}
// Not thread safe!
+1 -1
View File
@@ -29,6 +29,6 @@ void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
//Parent
close(pipes[1]);
waitpid(childPid, NULL, 0);
ffAppendFDContent(pipes[0], buffer);
ffAppendFDBuffer(pipes[0], buffer);
close(pipes[0]);
}
+2 -2
View File
@@ -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");
ffGetFileContent(procPath.chars, &loginuid);
ffGetFileBuffer(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");
ffGetFileContent(procPath.chars, &processName);
ffGetFileBuffer(procPath.chars, &processName);
ffStrbufSubstrBeforeFirstC(&processName, '\0'); //Trim the arguments
ffStrbufSubstrAfterLastC(&processName, '/');
+3 -3
View File
@@ -32,7 +32,7 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value)
continue;
ffStrbufAppendS(dir, dirent->d_name);
ffGetFileContent(dir->chars, &valueString);
ffGetFileBuffer(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");
ffGetFileContent(dir->chars, &value->name);
ffGetFileBuffer(dir->chars, &value->name);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufAppendS(dir, "device/class");
ffGetFileContent(dir->chars, &value->deviceClass);
ffGetFileBuffer(dir->chars, &value->deviceClass);
ffStrbufSubstrBefore(dir, dirLength);
return value->name.length > 0 || value->deviceClass.length > 0;
+1 -1
View File
@@ -21,7 +21,7 @@ static void getProcessInformation(const char* pid, FFstrbuf* processName, FFstrb
ffStrbufAppendS(&cmdlineFilePath, pid);
ffStrbufAppendS(&cmdlineFilePath, "/cmdline");
ffGetFileContent(cmdlineFilePath.chars, exe);
ffGetFileBuffer(cmdlineFilePath.chars, exe);
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
ffStrbufTrimLeft(exe, '-'); //Happens in TTY
+4 -4
View File
@@ -413,14 +413,14 @@ void ffListFeatures();
void ffStartDetectionThreads(FFinstance* instance);
//common/io.c
bool ffWriteFDData(int fd, size_t dataSize, const void* data);
bool ffWriteFDBuffer(int fd, const FFstrbuf* content);
bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data);
bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer);
void ffAppendFDContent(int fd, FFstrbuf* buffer);
bool ffAppendFileContent(const char* fileName, FFstrbuf* buffer); //returns true if open() succeeds. This is used to differentiate between <file not found> and <empty file>
bool ffGetFileContent(const char* fileName, FFstrbuf* buffer);
bool ffAppendFDBuffer(int fd, FFstrbuf* buffer);
ssize_t ffGetFileData(const char* fileName, size_t dataSize, void* data);
bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer);
bool ffGetFileBuffer(const char* fileName, FFstrbuf* buffer);
bool ffFileExists(const char* fileName, mode_t mode);
void ffSuppressIO(bool suppress); // Not thread safe!
+1 -1
View File
@@ -375,7 +375,7 @@ static void readCachedStrbuf(FFLogoRequestData* requestData, FFstrbuf* result, c
{
uint32_t cacheDirLength = requestData->cacheDir.length;
ffStrbufAppendS(&requestData->cacheDir, cacheFileName);
ffAppendFileContent(requestData->cacheDir.chars, result);
ffAppendFileBuffer(requestData->cacheDir.chars, result);
ffStrbufSubstrBefore(&requestData->cacheDir, cacheDirLength);
}
+1 -1
View File
@@ -249,7 +249,7 @@ static bool logoPrintFileIfExists(FFinstance* instance, bool doColorReplacement)
FFstrbuf content;
ffStrbufInitA(&content, 2047);
if(!ffAppendFileContent(instance->config.logoSource.chars, &content))
if(!ffAppendFileBuffer(instance->config.logoSource.chars, &content))
{
ffStrbufDestroy(&content);
return false;
+7 -7
View File
@@ -23,7 +23,7 @@ static void parseBattery(FFstrbuf* dir, FFlist* results)
//type must exist and be "Battery"
ffStrbufAppendS(dir, "/type");
ffGetFileContent(dir->chars, &testBatteryBuffer);
ffGetFileBuffer(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");
ffGetFileContent(dir->chars, &testBatteryBuffer);
ffGetFileBuffer(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");
ffGetFileContent(dir->chars, &result->capacity);
ffGetFileBuffer(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");
ffGetFileContent(dir->chars, &result->manufacturer);
ffGetFileBuffer(dir->chars, &result->manufacturer);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->modelName);
ffStrbufAppendS(dir, "/model_name");
ffGetFileContent(dir->chars, &result->modelName);
ffGetFileBuffer(dir->chars, &result->modelName);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->technology);
ffStrbufAppendS(dir, "/technology");
ffGetFileContent(dir->chars, &result->technology);
ffGetFileBuffer(dir->chars, &result->technology);
ffStrbufSubstrBefore(dir, dirLength);
ffStrbufInit(&result->status);
ffStrbufAppendS(dir, "/status");
ffGetFileContent(dir->chars, &result->status);
ffGetFileBuffer(dir->chars, &result->status);
ffStrbufSubstrBefore(dir, dirLength);
}
+2 -2
View File
@@ -10,9 +10,9 @@ static double getGhz(const char* policyFile, const char* cpuFile)
FFstrbuf content;
ffStrbufInit(&content);
ffGetFileContent(policyFile, &content);
ffGetFileBuffer(policyFile, &content);
if(content.length == 0)
ffGetFileContent(cpuFile, &content);
ffGetFileBuffer(cpuFile, &content);
double herz = ffStrbufToDouble(&content);
+4 -4
View File
@@ -39,10 +39,10 @@ static bool hostValueSet(FFstrbuf* value)
static void getHostValue(const char* devicesPath, const char* classPath, FFstrbuf* buffer)
{
ffGetFileContent(devicesPath, buffer);
ffGetFileBuffer(devicesPath, buffer);
if(buffer->length == 0)
ffGetFileContent(classPath, buffer);
ffGetFileBuffer(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)
ffGetFileContent("/sys/firmware/devicetree/base/model", &product_name);
ffGetFileBuffer("/sys/firmware/devicetree/base/model", &product_name);
if(product_name.length == 0)
ffGetFileContent("/tmp/sysinfo/model", &product_name);
ffGetFileBuffer("/tmp/sysinfo/model", &product_name);
if(ffStrbufStartsWithS(&product_name, "Standard PC"))
{