2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-05-01 21:22:07 +02:00
#include <string.h>
2021-02-18 22:25:36 +01:00
#include <dirent.h>
2021-04-15 18:59:26 +02:00
#define FF_PACKAGES_MODULE_NAME "Packages"
2022-06-02 22:58:19 +02:00
#define FF_PACKAGES_NUM_FORMAT_ARGS 11
2021-04-15 18:59:26 +02:00
2022-06-02 22:58:19 +02:00
typedef struct PackageCounts
2022-05-24 13:36:33 +02:00
{
2022-06-02 22:58:19 +02:00
uint32_t pacman ;
uint32_t dpkg ;
uint32_t rpm ;
uint32_t emerge ;
uint32_t xbps ;
uint32_t nixDefault ;
uint32_t flatpak ;
uint32_t snap ;
FFstrbuf pacmanBranch ;
} PackageCounts ;
2022-05-24 13:36:33 +02:00
2021-05-16 15:10:15 +02:00
static uint32_t getNumElements ( const char * dirname , unsigned char type )
{
DIR * dirp = opendir ( dirname );
2021-02-19 16:23:41 +01:00
if ( dirp == NULL )
return 0 ;
2021-02-18 22:25:36 +01:00
2021-05-16 15:10:15 +02:00
uint32_t num_elements = 0 ;
struct dirent * entry ;
2021-04-28 16:05:06 +02:00
while (( entry = readdir ( dirp )) != NULL ) {
2021-04-28 07:10:55 +02:00
if ( entry -> d_type == type )
2021-04-27 21:52:55 +02:00
++ num_elements ;
2021-02-18 22:25:36 +01:00
}
2021-03-27 18:14:19 +01:00
2021-04-28 06:59:44 +02:00
if ( type == DT_DIR )
2021-04-27 21:52:55 +02:00
num_elements -= 2 ; // accounting for . and ..
2021-02-18 22:25:36 +01:00
closedir ( dirp );
2021-04-27 21:52:55 +02:00
return num_elements ;
2021-02-18 22:25:36 +01:00
}
2021-05-01 21:22:07 +02:00
static uint32_t getNumStrings ( const char * filename , const char * needle )
{
FILE * file = fopen ( filename , "r" );
if ( file == NULL )
return 0 ;
uint32_t count = 0 ;
char * line = NULL ;
size_t len = 0 ;
while ( getline ( & line , & len , file ) != EOF )
{
if ( strstr ( line , needle ) != NULL )
++ count ;
}
if ( line != NULL )
free ( line );
fclose ( file );
return count ;
}
2022-02-12 12:06:16 +01:00
static uint32_t countFilesRecursive ( FFstrbuf * baseDirPath , const char * filename )
2022-02-12 12:16:14 +02:00
{
2022-02-12 12:06:16 +01:00
uint32_t baseDirPathLength = baseDirPath -> length ;
2022-02-12 12:16:14 +02:00
2022-02-12 12:06:16 +01:00
ffStrbufAppendC ( baseDirPath , '/' );
ffStrbufAppendS ( baseDirPath , filename );
bool exists = ffFileExists ( baseDirPath -> chars , S_IFREG );
ffStrbufSubstrBefore ( baseDirPath , baseDirPathLength );
if ( exists )
2022-02-12 12:16:14 +02:00
return 1 ;
2022-02-12 12:06:16 +01:00
DIR * dirp = opendir ( baseDirPath -> chars );
if ( dirp == NULL )
2022-02-12 12:16:14 +02:00
return 0 ;
2022-02-12 12:06:16 +01:00
ffStrbufAppendC ( baseDirPath , '/' );
baseDirPathLength = baseDirPath -> length ;
2022-02-12 12:16:14 +02:00
uint32_t sum = 0 ;
2022-02-12 12:06:16 +01:00
2022-02-12 12:16:14 +02:00
struct dirent * entry ;
2022-02-12 12:06:16 +01:00
while (( entry = readdir ( dirp )) != NULL ) {
2022-02-12 12:16:14 +02:00
// According to the PMS, neither category nor package name can begin with '.', so no need to check for . or .. specifically
2022-02-12 12:06:16 +01:00
if ( entry -> d_type != DT_DIR || entry -> d_name [ 0 ] == '.' )
continue ;
ffStrbufAppendS ( baseDirPath , entry -> d_name );
sum += countFilesRecursive ( baseDirPath , filename );
ffStrbufSubstrBefore ( baseDirPath , baseDirPathLength );
2022-02-12 12:16:14 +02:00
}
closedir ( dirp );
return sum ;
}
2022-06-02 22:58:19 +02:00
static uint32_t getNixPackages ( char * path )
2022-02-12 12:16:14 +02:00
{
2022-06-02 22:58:19 +02:00
//Nix detection is kinda slow, so we only do it if the dir exists
if ( ! ffFileExists ( path , S_IFDIR ))
return 0 ;
2022-02-12 12:16:14 +02:00
2022-04-21 17:59:26 +02:00
FFstrbuf output ;
ffStrbufInitA ( & output , 128 );
ffProcessAppendStdOut ( & output , ( char * const []) {
"nix-store" ,
"-qR" ,
2022-06-02 22:58:19 +02:00
path ,
2022-04-21 17:59:26 +02:00
NULL
});
//Each package is a new line in the output. If at least one line is found, add 1 for the last line.
uint32_t result = ffStrbufCountC ( & output , '\n' );
if ( result > 0 )
result ++ ;
ffStrbufDestroy ( & output );
return result ;
}
2022-06-02 22:58:19 +02:00
#ifdef FF_HAVE_RPM
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmlog.h>
static uint32_t getRpmFromLibrpm ( const FFinstance * instance )
2022-04-21 17:59:26 +02:00
{
2022-06-02 22:58:19 +02:00
FF_LIBRARY_LOAD ( rpm , instance -> config . librpm , 0 , "librpm.so" , 12 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmReadConfigFiles , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmtsCreate , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmtsInitIterator , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmdbGetIteratorCount , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmdbFreeIterator , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmtsFree , 0 )
FF_LIBRARY_LOAD_SYMBOL ( rpm , rpmlogSetMask , 0 )
// Don't print any error messages
ffrpmlogSetMask ( RPMLOG_MASK ( RPMLOG_EMERG ));
if ( ffrpmReadConfigFiles ( NULL , NULL ) != 0 )
{
dlclose ( rpm );
return 0 ;
}
rpmts ts = ffrpmtsCreate ();
if ( ts == NULL )
{
dlclose ( rpm );
return 0 ;
}
rpmdbMatchIterator mi = ffrpmtsInitIterator ( ts , RPMDBI_LABEL , NULL , 0 );
if ( mi == NULL )
{
ffrpmtsFree ( ts );
dlclose ( rpm );
return 0 ;
}
int count = ffrpmdbGetIteratorCount ( mi );
ffrpmdbFreeIterator ( mi );
ffrpmtsFree ( ts );
dlclose ( rpm );
return count > 0 ? ( uint32_t ) count : 0 ;
2022-04-21 17:59:26 +02:00
}
2022-06-02 22:58:19 +02:00
#endif
2022-04-21 17:59:26 +02:00
2022-06-02 22:58:19 +02:00
static void getPackageCounts ( const FFinstance * instance , FFstrbuf * baseDir , PackageCounts * packageCounts )
2022-04-21 17:59:26 +02:00
{
2022-06-02 22:58:19 +02:00
uint32_t baseDirLength = baseDir -> length ;
//pacman
ffStrbufAppendS ( baseDir , "/var/lib/pacman/local" );
packageCounts -> pacman += getNumElements ( baseDir -> chars , DT_DIR );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//dpkg
ffStrbufAppendS ( baseDir , "/var/lib/dpkg/status" );
packageCounts -> dpkg += getNumStrings ( baseDir -> chars , "Status: " );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//rpm
ffStrbufAppendS ( baseDir , "/var/lib/rpm/rmpdb.sqlite" );
packageCounts -> rpm += ( uint32_t ) ffSettingsGetSQLite3Int ( instance , baseDir -> chars , "SELECT count(blob) FROM Packages" );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//emerge
ffStrbufAppendS ( baseDir , "/var/db/pkg" );
packageCounts -> emerge += countFilesRecursive ( baseDir , "SIZE" );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//xps
ffStrbufAppendS ( baseDir , "/var/db/xbps" );
packageCounts -> xbps += getNumElements ( baseDir -> chars , DT_REG );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//flatpak
ffStrbufAppendS ( baseDir , "/var/lib/flatpak/app" );
packageCounts -> flatpak += getNumElements ( baseDir -> chars , DT_DIR );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//nix default
ffStrbufAppendS ( baseDir , "/var/nix/profiles/default" );
packageCounts -> nixDefault += getNixPackages ( baseDir -> chars );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//snap
ffStrbufAppendS ( baseDir , "/snap" );
uint32_t snap = getNumElements ( baseDir -> chars , DT_DIR );
if ( snap > 0 )
packageCounts -> snap += ( snap - 1 ); //Accounting for the /snap/bin folder
ffStrbufSubstrBefore ( baseDir , baseDirLength );
//pacman branch
ffStrbufAppendS ( baseDir , "/etc/pacman-mirrors.conf" );
if ( ffParsePropFile ( baseDir -> chars , "Branch =" , & packageCounts -> pacmanBranch ) && packageCounts -> pacmanBranch . length == 0 )
ffStrbufAppendS ( & packageCounts -> pacmanBranch , "stable" );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
2022-04-21 17:59:26 +02:00
}
2022-06-02 22:58:19 +02:00
static void getPackageCountsBedrock ( const FFinstance * instance , FFstrbuf * baseDir , PackageCounts * packageCounts )
2021-02-18 22:25:36 +01:00
{
2022-06-02 22:58:19 +02:00
ffStrbufAppendS ( baseDir , "/bedrock/strata" );
DIR * dir = opendir ( baseDir -> chars );
if ( dir == NULL )
return ;
ffStrbufAppendC ( baseDir , '/' );
2022-06-02 23:24:50 +02:00
uint32_t baseDirLength = baseDir -> length ;
2022-06-02 22:58:19 +02:00
struct dirent * entry ;
while (( entry = readdir ( dir )) != NULL )
{
if ( entry -> d_type != DT_DIR )
continue ;
ffStrbufAppendS ( baseDir , entry -> d_name );
getPackageCounts ( instance , baseDir , packageCounts );
ffStrbufSubstrBefore ( baseDir , baseDirLength );
}
closedir ( dir );
2022-05-24 13:36:33 +02:00
}
void ffPrintPackages ( FFinstance * instance )
{
2022-06-02 22:58:19 +02:00
PackageCounts counts = { 0 };
ffStrbufInit ( & counts . pacmanBranch );
2022-04-21 17:59:26 +02:00
2022-06-02 22:58:19 +02:00
FFstrbuf baseDir ;
ffStrbufInitA ( & baseDir , 512 );
ffStrbufAppendS ( & baseDir , FASTFETCH_TARGET_DIR_ROOT );
2022-04-21 17:59:26 +02:00
2022-06-02 22:58:19 +02:00
if ( ffStrbufIgnCaseCompS ( & ffDetectOS ( instance ) -> id , "bedrock" ) == 0 )
getPackageCountsBedrock ( instance , & baseDir , & counts );
else
getPackageCounts ( instance , & baseDir , & counts );
2022-04-21 17:59:26 +02:00
2022-06-02 22:58:19 +02:00
// If SQL failed, we can still try with librpm.
// This is needed on openSUSE, which seems to use a proprietary database file
// This method doesn't work on bedrock, so we do it here.
#ifdef FF_HAVE_RPM
if ( counts . rpm == 0 )
counts . rpm = getRpmFromLibrpm ( instance );
#endif
2021-03-11 19:40:46 +01:00
2022-06-02 22:58:19 +02:00
//nix user
ffStrbufSetS ( & baseDir , instance -> state . passwd -> pw_dir );
ffStrbufAppendS ( & baseDir , "/.nix-profile" );
uint32_t nixUser = getNixPackages ( baseDir . chars );
2021-06-10 22:04:25 +02:00
2022-06-02 22:58:19 +02:00
ffStrbufDestroy ( & baseDir );
2021-03-11 19:40:46 +01:00
2022-06-02 22:58:19 +02:00
uint32_t all = counts . dpkg + counts . emerge + counts . flatpak + counts . nixDefault + nixUser + counts . pacman + counts . rpm + counts . snap + counts . xbps ;
2021-04-03 21:51:50 +02:00
if ( all == 0 )
2021-03-11 19:54:34 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . packagesKey , & instance -> config . packagesFormat , FF_PACKAGES_NUM_FORMAT_ARGS , "No packages from known package managers found" );
2021-04-03 21:51:50 +02:00
return ;
}
2021-05-15 18:17:34 +02:00
2021-04-03 21:51:50 +02:00
if ( instance -> config . packagesFormat . length == 0 )
{
2022-04-19 00:32:28 +02:00
ffPrintLogoAndKey ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . packagesKey );
2021-03-11 19:40:46 +01:00
#define FF_PRINT_PACKAGE(name) \
2022-06-02 22:58:19 +02:00
if(counts.name > 0) \
2021-03-19 20:57:07 +01:00
{ \
2022-06-02 22:58:19 +02:00
printf("%u ("#name")", counts.name); \
if((all = all - counts.name) > 0) \
2021-03-19 20:57:07 +01:00
printf(", "); \
};
2021-03-11 19:40:46 +01:00
2022-06-02 22:58:19 +02:00
if ( counts . pacman > 0 )
2021-05-15 17:21:02 +02:00
{
2022-06-02 22:58:19 +02:00
printf ( "%u (pacman)" , counts . pacman );
if ( counts . pacmanBranch . length > 0 )
printf ( "[%s]" , counts . pacmanBranch . chars );
if (( all = all - counts . pacman ) > 0 )
2021-05-15 17:21:02 +02:00
printf ( ", " );
};
2021-05-15 18:12:10 +02:00
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE ( dpkg )
2021-06-08 20:01:29 +02:00
FF_PRINT_PACKAGE ( rpm )
2022-02-12 12:13:02 +01:00
FF_PRINT_PACKAGE ( emerge )
2022-04-21 17:59:26 +02:00
if ( nixUser > 0 )
{
printf ( "%u (nix-user)" , nixUser );
if (( all = all - nixUser ) > 0 )
printf ( ", " );
}
2022-06-02 22:58:19 +02:00
if ( counts . nixDefault > 0 )
2022-04-21 17:59:26 +02:00
{
2022-06-02 22:58:19 +02:00
printf ( "%u (nix-default)" , counts . nixDefault );
if (( all = all - counts . nixDefault ) > 0 )
2022-04-21 17:59:26 +02:00
printf ( ", " );
}
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE ( flatpak )
FF_PRINT_PACKAGE ( snap )
2021-03-11 19:40:46 +01:00
2021-05-13 12:27:44 +02:00
//Fix linter warning of unused value of all
( void ) all ;
2021-03-11 19:40:46 +01:00
#undef FF_PRINT_PACKAGE
putchar ( '\n' );
}
2021-03-27 15:39:56 +01:00
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . packagesKey , & instance -> config . packagesFormat , NULL , FF_PACKAGES_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_UINT , & all },
2022-06-02 22:58:19 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & counts . pacman },
{ FF_FORMAT_ARG_TYPE_STRBUF , & counts . pacmanBranch },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . dpkg },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . rpm },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . emerge },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . xbps },
{ FF_FORMAT_ARG_TYPE_UINT , & nixUser },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . nixDefault },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . flatpak },
{ FF_FORMAT_ARG_TYPE_UINT , & counts . snap }
2021-04-15 18:59:26 +02:00
});
2021-03-27 15:39:56 +01:00
}
2021-05-15 18:17:34 +02:00
2022-06-02 22:58:19 +02:00
ffStrbufDestroy ( & counts . pacmanBranch );
2021-03-27 18:14:19 +01:00
}