From 8c6cb9405d44c78cc9385b40627e470a0ae2c032 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sat, 12 Feb 2022 12:06:16 +0100 Subject: [PATCH] Use FFstrbuf in emerge package counting --- README.md | 2 +- src/common/io.c | 6 ++++ src/fastfetch.c | 5 ++-- src/fastfetch.h | 4 +++ src/modules/packages.c | 65 ++++++++++++++++-------------------------- 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 5a77af745..2aebdab3d 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Android, Arch, Arco, Artix, CachyOS, CentOS, Debian, Endeavour, Fedora, Garuda, ##### Package managers ``` -Pacman, dpkg, rpm, xbps, Flatpak, Snap +Pacman, dpkg, rpm, emerge, xbps, Flatpak, Snap ``` ##### WM themes diff --git a/src/common/io.c b/src/common/io.c index dd22bd3a2..b1e230e55 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -497,3 +497,9 @@ void ffPrintColor(const FFstrbuf* colorValue) ffStrbufWriteTo(colorValue, stdout); fputc('m', stdout); } + +bool ffFileExists(const char* fileName, mode_t mode) +{ + struct stat fileStat; + return stat(fileName, &fileStat) == 0 && ((fileStat.st_mode & S_IFMT) == mode); +} diff --git a/src/fastfetch.c b/src/fastfetch.c index bd2a5682c..f38f5ad16 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -95,7 +95,7 @@ static inline void printCommandHelp(const char* command) } else if(strcasecmp(command, "packages-format") == 0) { - constructAndPrintCommandHelpFormat("packages", "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (xps), {7}, (flatpak), {8} (snap)", 8, + constructAndPrintCommandHelpFormat("packages", "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (xps), {7}, (flatpak), {8} (snap), {9} (emerge)", 9, "Number of all packages", "Number of pacman packages", "Pacman branch on manjaro", @@ -103,7 +103,8 @@ static inline void printCommandHelp(const char* command) "Number of rpm packages", "Number of xbps packages", "Number of flatpak packages", - "Number of snap packages" + "Number of snap packages", + "Number of emerge packages" ); } else if(strcasecmp(command, "shell-format") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 8b47f3b8f..814f9fbd3 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -10,8 +10,10 @@ #include #include #include +#include #include #include +#include #include #include "fastfetch_config.h" @@ -359,6 +361,8 @@ bool ffGetFileContent(const char* fileName, FFstrbuf* buffer); bool ffWriteFDContent(int fd, const FFstrbuf* content); void ffWriteFileContent(const char* fileName, const FFstrbuf* buffer); +bool ffFileExists(const char* fileName, mode_t mode); + // Not thread safe! void ffSuppressIO(bool suppress); diff --git a/src/modules/packages.c b/src/modules/packages.c index 920036233..570a08845 100644 --- a/src/modules/packages.c +++ b/src/modules/packages.c @@ -2,7 +2,6 @@ #include #include -#include #define FF_PACKAGES_MODULE_NAME "Packages" #define FF_PACKAGES_NUM_FORMAT_ARGS 9 @@ -92,44 +91,35 @@ static uint32_t getNumStrings(const char* filename, const char* needle) return count; } -static const size_t BUFSIZE = PATH_MAX; -static size_t filename_len_cache = 0; - -// Make sure filename_len_cache is correct when calling this -static uint32_t countFilesRecursive(const char* filename, char* dirBuffer, char* end) +static uint32_t countFilesRecursive(FFstrbuf* baseDirPath, const char* filename) { - if ((size_t)(end - dirBuffer) + filename_len_cache + 2 >= BUFSIZE) - return 0; + uint32_t baseDirPathLength = baseDirPath->length; - *end = '/'; - memcpy(end + 1, filename, filename_len_cache + 1); // Copy the NUL terminator too - - struct stat exist; - if (stat(dirBuffer, &exist) == 0 && (exist.st_mode & S_IFMT) == S_IFREG) // Found it, count it + ffStrbufAppendC(baseDirPath, '/'); + ffStrbufAppendS(baseDirPath, filename); + bool exists = ffFileExists(baseDirPath->chars, S_IFREG); + ffStrbufSubstrBefore(baseDirPath, baseDirPathLength); + if(exists) return 1; - *end = 0; - - // Did not find it, search below - DIR* dirp = opendir(dirBuffer); - - if (!dirp) + DIR* dirp = opendir(baseDirPath->chars); + if(dirp == NULL) return 0; - *end = '/'; + ffStrbufAppendC(baseDirPath, '/'); + baseDirPathLength = baseDirPath->length; uint32_t sum = 0; + struct dirent *entry; - while ((entry = readdir(dirp)) != NULL) { + while((entry = readdir(dirp)) != NULL) { // According to the PMS, neither category nor package name can begin with '.', so no need to check for . or .. specifically - if (entry->d_type == DT_DIR && entry->d_name[0] != '.') { - size_t len = strlen(entry->d_name); - char *c = end + 1 + len; - if ((size_t)(c - dirBuffer) < BUFSIZE - 1) { - memcpy(end + 1, entry->d_name, len + 1); // Copy NUL terminator - sum += countFilesRecursive(filename, dirBuffer, c); - } - } + if(entry->d_type != DT_DIR || entry->d_name[0] == '.') + continue; + + ffStrbufAppendS(baseDirPath, entry->d_name); + sum += countFilesRecursive(baseDirPath, filename); + ffStrbufSubstrBefore(baseDirPath, baseDirPathLength); } closedir(dirp); @@ -138,17 +128,12 @@ static uint32_t countFilesRecursive(const char* filename, char* dirBuffer, char* static uint32_t countFilesIn(const char* dirname, const char* filename) { - char buffer[BUFSIZE]; - size_t len = strlen(dirname); - - if (len >= BUFSIZE) - return 0; - - char* c = buffer + len; - memcpy(buffer, dirname, len + 1); // Copy the NUL terminator too - - filename_len_cache = strlen(filename); // Cache this - return countFilesRecursive(filename, buffer, c); + FFstrbuf baseDirPath; + ffStrbufInitA(&baseDirPath, 128); + ffStrbufAppendS(&baseDirPath, dirname); + uint32_t result = countFilesRecursive(&baseDirPath, filename); + ffStrbufDestroy(&baseDirPath); + return result; } void ffPrintPackages(FFinstance* instance)