Skip to content

Commit

Permalink
Packages: add pkgsrc; code refactor
Browse files Browse the repository at this point in the history
Fix #1515
  • Loading branch information
CarterLi committed Jan 17, 2025
1 parent 0b39d6f commit 2b859b2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 76 deletions.
47 changes: 47 additions & 0 deletions src/detection/packages/packages.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,50 @@ bool ffPackagesWriteCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, uint32_t n
ffStrbufAppendF(cacheContent, "%" PRIu32, num_elements);
return ffWriteFileBuffer(cacheDir->chars, cacheContent);
}

#ifndef _WIN32
uint32_t ffPackagesGetNumElements(const char* dirname, uint8_t type)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(dirname);
if(dirp == NULL)
return 0;

uint32_t num_elements = 0;

struct dirent *entry;
while((entry = readdir(dirp)) != NULL)
{
bool ok = false;

#ifndef __sun
if(entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK)
ok = entry->d_type == type;
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;
}
}
}

if(ok) ++num_elements;
}

if(type == DT_DIR && num_elements >= 2)
num_elements -= 2; // accounting for . and ..

return num_elements;
}
#endif
5 changes: 5 additions & 0 deletions src/detection/packages/packages.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct FFPackagesResult
uint32_t pacstall;
uint32_t paludis;
uint32_t pkg;
uint32_t pkgsrc;
uint32_t pkgtool;
uint32_t qi;
uint32_t rpm;
Expand All @@ -48,3 +49,7 @@ typedef struct FFPackagesResult
const char* ffDetectPackages(FFPackagesResult* result, FFPackagesOptions* options);
bool ffPackagesReadCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, const char* filePath, const char* packageId, uint32_t* result);
bool ffPackagesWriteCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, uint32_t num_elements);

#ifndef _WIN32
uint32_t ffPackagesGetNumElements(const char* dirname, uint8_t type);
#endif
26 changes: 3 additions & 23 deletions src/detection/packages/packages_apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,18 @@
#include "common/processing.h"
#include "util/stringUtils.h"

static uint32_t getNumElements(const char* dirname, unsigned char type)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(dirname);
if(dirp == NULL)
return 0;

uint32_t num_elements = 0;

struct dirent *entry;
while((entry = readdir(dirp)) != NULL) {
if(entry->d_type == type)
++num_elements;
}

if(type == DT_DIR)
num_elements -= 2; // accounting for . and ..

return num_elements;
}

static void countBrewPackages(const char* dirname, FFPackagesResult* result)
{
FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateS(dirname);

uint32_t baseDirLength = baseDir.length;

ffStrbufAppendS(&baseDir, "/Caskroom");
result->brewCask += getNumElements(baseDir.chars, DT_DIR);
result->brewCask += ffPackagesGetNumElements(baseDir.chars, DT_DIR);
ffStrbufSubstrBefore(&baseDir, baseDirLength);

ffStrbufAppendS(&baseDir, "/Cellar");
result->brew += getNumElements(baseDir.chars, DT_DIR);
result->brew += ffPackagesGetNumElements(baseDir.chars, DT_DIR);
ffStrbufSubstrBefore(&baseDir, baseDirLength);
}

Expand All @@ -54,7 +34,7 @@ static uint32_t countMacPortsPackages(const char* dirname)
FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateS(dirname);
ffStrbufAppendS(&baseDir, "/var/macports/software");

return getNumElements(baseDir.chars, DT_DIR);
return ffPackagesGetNumElements(baseDir.chars, DT_DIR);
}

static uint32_t getMacPortsPackages()
Expand Down
26 changes: 2 additions & 24 deletions src/detection/packages/packages_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,11 @@
#include "detection/os/os.h"
#include "util/stringUtils.h"

#include <dirent.h>

static uint32_t getNumElementsImpl(const char* dirname, unsigned char type)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(dirname);
if(dirp == NULL)
return 0;

uint32_t num_elements = 0;

struct dirent *entry;
while((entry = readdir(dirp)) != NULL) {
if(entry->d_type == type)
++num_elements;
}

if(type == DT_DIR && num_elements >= 2)
num_elements -= 2; // accounting for . and ..

return num_elements;
}

static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, unsigned char type)
{
uint32_t baseDirLength = baseDir->length;
ffStrbufAppendS(baseDir, dirname);
uint32_t num_elements = getNumElementsImpl(baseDir->chars, type);
uint32_t num_elements = ffPackagesGetNumElements(baseDir->chars, type);
ffStrbufSubstrBefore(baseDir, baseDirLength);
return num_elements;
}
Expand Down Expand Up @@ -550,7 +528,7 @@ static uint32_t getFlatpakPackages(FFstrbuf* baseDir, const char* dirname)
uint32_t flatpakDirLength = baseDir->length;

ffStrbufAppendS(baseDir, "app");
num_elements += getNumElementsImpl(baseDir->chars, DT_DIR);
num_elements += ffPackagesGetNumElements(baseDir->chars, DT_DIR);
ffStrbufSubstrBefore(baseDir, flatpakDirLength);

num_elements += getFlatpakRuntimePackages(baseDir);
Expand Down
35 changes: 6 additions & 29 deletions src/detection/packages/packages_obsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,13 @@

#include "common/io/io.h"

static uint32_t getNumElementsImpl(const char* dirname, unsigned char type)
{
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(dirname);
if(dirp == NULL)
return 0;

uint32_t num_elements = 0;

struct dirent *entry;
while((entry = readdir(dirp)) != NULL) {
if(entry->d_type == type)
++num_elements;
}

if(type == DT_DIR && num_elements >= 2)
num_elements -= 2; // accounting for . and ..

return num_elements;
}

void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options)
{
#if __OpenBSD__
if (!(options->disabled & FF_PACKAGES_FLAG_PKG_BIT))
{
result->pkg = getNumElementsImpl(FASTFETCH_TARGET_DIR_ROOT
#if __OpenBSD__
"/var/db/pkg"
#else
"/usr/pkg/pkgdb"
#endif
, DT_DIR);
}
result->port = ffPackagesGetNumElements(FASTFETCH_TARGET_DIR_ROOT "/var/db/pkg", DT_DIR);
#elif __NetBSD__
if (!(options->disabled & FF_PACKAGES_FLAG_PKGSRC_BIT))
result->pkgsrc = ffPackagesGetNumElements(FASTFETCH_TARGET_DIR_ROOT "/usr/pkg/pkgdb", DT_DIR);
#endif
}
3 changes: 3 additions & 0 deletions src/detection/packages/packages_sunos.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "packages.h"
#include <dirent.h>

void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options)
{
Expand All @@ -12,4 +13,6 @@ void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options)
result->pkg = (uint32_t) yyjson_get_uint(packageCount);
}
}
if (!(options->disabled & FF_PACKAGES_FLAG_PKGSRC_BIT))
result->pkgsrc = ffPackagesGetNumElements(FASTFETCH_TARGET_DIR_ROOT "/usr/pkg/pkgdb", DT_DIR);
}
1 change: 1 addition & 0 deletions src/modules/packages/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef enum __attribute__((__packed__)) FFPackagesFlags
FF_PACKAGES_FLAG_PACSTALL_BIT = 1 << 25,
FF_PACKAGES_FLAG_MPORT_BIT = 1 << 26,
FF_PACKAGES_FLAG_QI_BIT = 1 << 27,
FF_PACKAGES_FLAG_PKGSRC_BIT = 1 << 28,
FF_PACKAGES_FLAG_FORCE_UNSIGNED = UINT32_MAX,
} FFPackagesFlags;
static_assert(sizeof(FFPackagesFlags) == sizeof(uint32_t), "");
Expand Down
7 changes: 7 additions & 0 deletions src/modules/packages/packages.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void ffPrintPackages(FFPackagesOptions* options)
FF_PRINT_PACKAGE_NAME(nixDefault, "nix-default")
FF_PRINT_PACKAGE(apk)
FF_PRINT_PACKAGE(pkg)
FF_PRINT_PACKAGE(pkgsrc)
FF_PRINT_PACKAGE_NAME(flatpakSystem, counts.flatpakUser ? "flatpak-system" : "flatpak")
FF_PRINT_PACKAGE_NAME(flatpakUser, "flatpak-user")
FF_PRINT_PACKAGE(snap)
Expand Down Expand Up @@ -121,6 +122,7 @@ void ffPrintPackages(FFPackagesOptions* options)
FF_FORMAT_ARG(counts.mport, "mport"),
FF_FORMAT_ARG(counts.qi, "qi"),
FF_FORMAT_ARG(counts.amUser, "am-user"),
FF_FORMAT_ARG(counts.pkgsrc, "pkgsrc"),
FF_FORMAT_ARG(nixAll, "nix-all"),
FF_FORMAT_ARG(flatpakAll, "flatpak-all"),
FF_FORMAT_ARG(brewAll, "brew-all"),
Expand Down Expand Up @@ -197,6 +199,7 @@ bool ffParsePackagesCommandOptions(FFPackagesOptions* options, const char* key,
FF_TEST_PACKAGE_NAME(PALUDIS)
FF_TEST_PACKAGE_NAME(PKG)
FF_TEST_PACKAGE_NAME(PKGTOOL)
FF_TEST_PACKAGE_NAME(PKGSRC)
break;
case 'Q': if (false);
FF_TEST_PACKAGE_NAME(QI)
Expand Down Expand Up @@ -311,6 +314,7 @@ void ffParsePackagesJsonObject(FFPackagesOptions* options, yyjson_val* module)
FF_TEST_PACKAGE_NAME(PALUDIS)
FF_TEST_PACKAGE_NAME(PKG)
FF_TEST_PACKAGE_NAME(PKGTOOL)
FF_TEST_PACKAGE_NAME(PKGSRC)
break;
case 'Q': if (false);
FF_TEST_PACKAGE_NAME(QI)
Expand Down Expand Up @@ -373,6 +377,7 @@ void ffGeneratePackagesJsonConfig(FFPackagesOptions* options, yyjson_mut_doc* do
FF_TEST_PACKAGE_NAME(PALUDIS)
FF_TEST_PACKAGE_NAME(PKG)
FF_TEST_PACKAGE_NAME(PKGTOOL)
FF_TEST_PACKAGE_NAME(PKGSRC)
FF_TEST_PACKAGE_NAME(QI)
FF_TEST_PACKAGE_NAME(RPM)
FF_TEST_PACKAGE_NAME(SCOOP)
Expand Down Expand Up @@ -427,6 +432,7 @@ void ffGeneratePackagesJsonResult(FF_MAYBE_UNUSED FFPackagesOptions* options, yy
FF_APPEND_PACKAGE_COUNT(paludis)
FF_APPEND_PACKAGE_COUNT(pkg)
FF_APPEND_PACKAGE_COUNT(pkgtool)
FF_APPEND_PACKAGE_COUNT(pkgsrc)
FF_APPEND_PACKAGE_COUNT(qi)
FF_APPEND_PACKAGE_COUNT(macports)
FF_APPEND_PACKAGE_COUNT(rpm)
Expand Down Expand Up @@ -484,6 +490,7 @@ static FFModuleBaseInfo ffModuleInfo = {
{"Number of mport packages", "mport"},
{"Number of qi packages", "qi"},
{"Number of am-user (aka appman) packages", "am-user"},
{"Number of pkgsrc packages", "pkgsrc"},
{"Total number of all nix packages", "nix-all"},
{"Total number of all flatpak app packages", "flatpak-all"},
{"Total number of all brew packages", "brew-all"},
Expand Down

0 comments on commit 2b859b2

Please sign in to comment.