diff --git a/src/detection/packages/packages.c b/src/detection/packages/packages.c index c4706eafe..586b3416f 100644 --- a/src/detection/packages/packages.c +++ b/src/detection/packages/packages.c @@ -87,7 +87,7 @@ bool ffPackagesWriteCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, uint32_t n } #ifndef _WIN32 -uint32_t ffPackagesGetNumElements(const char* dirname, uint8_t type) +uint32_t ffPackagesGetNumElements(const char* dirname, bool isdir) { FF_AUTO_CLOSE_DIR DIR* dirp = opendir(dirname); if(dirp == NULL) @@ -102,31 +102,19 @@ uint32_t ffPackagesGetNumElements(const char* dirname, uint8_t type) #ifndef __sun if(entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) - ok = entry->d_type == type; + ok = entry->d_type == isdir ? DT_DIR : DT_REG; else #endif { struct stat stbuf; if (fstatat(dirfd(dirp), entry->d_name, &stbuf, 0) == 0) - { - switch (stbuf.st_mode & S_IFMT) - { - case S_IFDIR: ok = type == DT_DIR; break; - case S_IFREG: ok = type == DT_REG; break; - case S_IFLNK: ok = type == DT_LNK; break; - case S_IFSOCK: ok = type == DT_SOCK; break; - case S_IFBLK: ok = type == DT_BLK; break; - case S_IFCHR: ok = type == DT_CHR; break; - case S_IFIFO: ok = type == DT_FIFO; break; - default: break; - } - } + ok = isdir ? S_ISDIR(stbuf.st_mode) : S_ISREG(stbuf.st_mode); } if(ok) ++num_elements; } - if(type == DT_DIR && num_elements >= 2) + if(isdir && num_elements >= 2) num_elements -= 2; // accounting for . and .. return num_elements; diff --git a/src/detection/packages/packages.h b/src/detection/packages/packages.h index 144fd7e55..b4e898d0f 100644 --- a/src/detection/packages/packages.h +++ b/src/detection/packages/packages.h @@ -51,5 +51,5 @@ bool ffPackagesReadCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, const char* bool ffPackagesWriteCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, uint32_t num_elements); #ifndef _WIN32 -uint32_t ffPackagesGetNumElements(const char* dirname, uint8_t type); +uint32_t ffPackagesGetNumElements(const char* dirname, bool isdir); #endif diff --git a/src/detection/packages/packages_apple.c b/src/detection/packages/packages_apple.c index d8a0edd9e..8391a0f56 100644 --- a/src/detection/packages/packages_apple.c +++ b/src/detection/packages/packages_apple.c @@ -11,11 +11,11 @@ static void countBrewPackages(const char* dirname, FFPackagesResult* result) uint32_t baseDirLength = baseDir.length; ffStrbufAppendS(&baseDir, "/Caskroom"); - result->brewCask += ffPackagesGetNumElements(baseDir.chars, DT_DIR); + result->brewCask += ffPackagesGetNumElements(baseDir.chars, true); ffStrbufSubstrBefore(&baseDir, baseDirLength); ffStrbufAppendS(&baseDir, "/Cellar"); - result->brew += ffPackagesGetNumElements(baseDir.chars, DT_DIR); + result->brew += ffPackagesGetNumElements(baseDir.chars, true); ffStrbufSubstrBefore(&baseDir, baseDirLength); } @@ -34,7 +34,7 @@ static uint32_t countMacPortsPackages(const char* dirname) FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateS(dirname); ffStrbufAppendS(&baseDir, "/var/macports/software"); - return ffPackagesGetNumElements(baseDir.chars, DT_DIR); + return ffPackagesGetNumElements(baseDir.chars, true); } static uint32_t getMacPortsPackages() diff --git a/src/detection/packages/packages_linux.c b/src/detection/packages/packages_linux.c index 045f329af..b306b1769 100644 --- a/src/detection/packages/packages_linux.c +++ b/src/detection/packages/packages_linux.c @@ -7,11 +7,11 @@ #include "detection/os/os.h" #include "util/stringUtils.h" -static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, unsigned char type) +static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, bool isdir) { uint32_t baseDirLength = baseDir->length; ffStrbufAppendS(baseDir, dirname); - uint32_t num_elements = ffPackagesGetNumElements(baseDir->chars, type); + uint32_t num_elements = ffPackagesGetNumElements(baseDir->chars, isdir); ffStrbufSubstrBefore(baseDir, baseDirLength); return num_elements; } @@ -310,10 +310,10 @@ static uint32_t getXBPS(FFstrbuf* baseDir, const char* dirname) static uint32_t getSnap(FFstrbuf* baseDir) { - uint32_t result = getNumElements(baseDir, "/snap", DT_DIR); + uint32_t result = getNumElements(baseDir, "/snap", true); if (result == 0) - result = getNumElements(baseDir, "/var/lib/snapd/snap", DT_DIR); + result = getNumElements(baseDir, "/var/lib/snapd/snap", true); //Accounting for the /snap/bin folder return result > 0 ? result - 1 : 0; @@ -479,7 +479,7 @@ static inline uint32_t getFlatpakRuntimePackagesArch(FFstrbuf* baseDir) { if(entry->d_type == DT_DIR && entry->d_name[0] != '.') { - num_elements += getNumElements(baseDir, entry->d_name, DT_DIR); + num_elements += getNumElements(baseDir, entry->d_name, true); } } @@ -528,7 +528,7 @@ static uint32_t getFlatpakPackages(FFstrbuf* baseDir, const char* dirname) uint32_t flatpakDirLength = baseDir->length; ffStrbufAppendS(baseDir, "app"); - num_elements += ffPackagesGetNumElements(baseDir->chars, DT_DIR); + num_elements += ffPackagesGetNumElements(baseDir->chars, true); ffStrbufSubstrBefore(baseDir, flatpakDirLength); num_elements += getFlatpakRuntimePackages(baseDir); @@ -544,23 +544,23 @@ static void getPackageCounts(FFstrbuf* baseDir, FFPackagesResult* packageCounts, if (!(options->disabled & FF_PACKAGES_FLAG_DPKG_BIT)) packageCounts->dpkg += getNumStrings(baseDir, "/var/lib/dpkg/status", "Status: install ok installed", "dpkg"); if (!(options->disabled & FF_PACKAGES_FLAG_LPKG_BIT)) packageCounts->lpkg += getNumStrings(baseDir, "/opt/Loc-OS-LPKG/installed-lpkg/Listinstalled-lpkg.list", "\n", "lpkg"); if (!(options->disabled & FF_PACKAGES_FLAG_EMERGE_BIT)) packageCounts->emerge += countFilesRecursive(baseDir, "/var/db/pkg", "SIZE"); - if (!(options->disabled & FF_PACKAGES_FLAG_EOPKG_BIT)) packageCounts->eopkg += getNumElements(baseDir, "/var/lib/eopkg/package", DT_DIR); + if (!(options->disabled & FF_PACKAGES_FLAG_EOPKG_BIT)) packageCounts->eopkg += getNumElements(baseDir, "/var/lib/eopkg/package", true); if (!(options->disabled & FF_PACKAGES_FLAG_FLATPAK_BIT)) packageCounts->flatpakSystem += getFlatpakPackages(baseDir, "/var/lib"); if (!(options->disabled & FF_PACKAGES_FLAG_NIX_BIT)) { packageCounts->nixDefault += getNixPackages(baseDir, "/nix/var/nix/profiles/default"); packageCounts->nixSystem += getNixPackages(baseDir, "/run/current-system"); } - if (!(options->disabled & FF_PACKAGES_FLAG_PACMAN_BIT)) packageCounts->pacman += getNumElements(baseDir, "/var/lib/pacman/local", DT_DIR); - if (!(options->disabled & FF_PACKAGES_FLAG_LPKGBUILD_BIT)) packageCounts->lpkgbuild += getNumElements(baseDir, "/opt/Loc-OS-LPKG/lpkgbuild/remove", DT_REG); - if (!(options->disabled & FF_PACKAGES_FLAG_PKGTOOL_BIT)) packageCounts->pkgtool += getNumElements(baseDir, "/var/log/packages", DT_REG); + if (!(options->disabled & FF_PACKAGES_FLAG_PACMAN_BIT)) packageCounts->pacman += getNumElements(baseDir, "/var/lib/pacman/local", true); + if (!(options->disabled & FF_PACKAGES_FLAG_LPKGBUILD_BIT)) packageCounts->lpkgbuild += getNumElements(baseDir, "/opt/Loc-OS-LPKG/lpkgbuild/remove", false); + if (!(options->disabled & FF_PACKAGES_FLAG_PKGTOOL_BIT)) packageCounts->pkgtool += getNumElements(baseDir, "/var/log/packages", false); if (!(options->disabled & FF_PACKAGES_FLAG_RPM_BIT)) packageCounts->rpm += getSQLite3Int(baseDir, "/var/lib/rpm/rpmdb.sqlite", "SELECT count(*) FROM Packages", "rpm"); if (!(options->disabled & FF_PACKAGES_FLAG_SNAP_BIT)) packageCounts->snap += getSnap(baseDir); if (!(options->disabled & FF_PACKAGES_FLAG_XBPS_BIT)) packageCounts->xbps += getXBPS(baseDir, "/var/db/xbps"); if (!(options->disabled & FF_PACKAGES_FLAG_BREW_BIT)) { - packageCounts->brewCask += getNumElements(baseDir, "/home/linuxbrew/.linuxbrew/Caskroom", DT_DIR); - packageCounts->brew += getNumElements(baseDir, "/home/linuxbrew/.linuxbrew/Cellar", DT_DIR); + packageCounts->brewCask += getNumElements(baseDir, "/home/linuxbrew/.linuxbrew/Caskroom", true); + packageCounts->brew += getNumElements(baseDir, "/home/linuxbrew/.linuxbrew/Cellar", true); } if (!(options->disabled & FF_PACKAGES_FLAG_PALUDIS_BIT)) packageCounts->paludis += countFilesRecursive(baseDir, "/var/db/paludis/repositories", "environment.bz2"); if (!(options->disabled & FF_PACKAGES_FLAG_OPKG_BIT)) packageCounts->opkg += getNumStrings(baseDir, "/usr/lib/opkg/status", "Package:", "opkg"); // openwrt @@ -570,8 +570,8 @@ static void getPackageCounts(FFstrbuf* baseDir, FFPackagesResult* packageCounts, { packageCounts->guixSystem += getGuixPackages(baseDir, "/run/current-system/profile"); } - if (!(options->disabled & FF_PACKAGES_FLAG_LINGLONG_BIT)) packageCounts->linglong += getNumElements(baseDir, "/var/lib/linglong/repo/refs/heads/main", DT_DIR); - if (!(options->disabled & FF_PACKAGES_FLAG_PACSTALL_BIT)) packageCounts->pacstall += getNumElements(baseDir, "/var/lib/pacstall/metadata", DT_REG); + if (!(options->disabled & FF_PACKAGES_FLAG_LINGLONG_BIT)) packageCounts->linglong += getNumElements(baseDir, "/var/lib/linglong/repo/refs/heads/main", true); + if (!(options->disabled & FF_PACKAGES_FLAG_PACSTALL_BIT)) packageCounts->pacstall += getNumElements(baseDir, "/var/lib/pacstall/metadata", false); if (!(options->disabled & FF_PACKAGES_FLAG_QI_BIT)) packageCounts->qi += getNumStrings(baseDir, "/var/qi/installed_packages.list", "\n", "qi"); }