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-02-12 12:16:14 +02:00
#define FF_PACKAGES_NUM_FORMAT_ARGS 9
2021-04-15 18:59:26 +02:00
2021-11-13 14:10:30 +01:00
#ifdef FF_HAVE_RPM
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmlog.h>
static uint32_t getRpmPackageCount ( FFinstance * instance )
{
2022-04-11 17:41:21 +02:00
FF_LIBRARY_LOAD ( rpm , instance -> config . librpm , 0 , "librpm.so" , 5 )
2021-11-13 14:10:30 +01:00
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 ));
uint32_t count = 0 ;
rpmts ts = NULL ;
rpmdbMatchIterator mi = NULL ;
if ( ffrpmReadConfigFiles ( NULL , NULL )) goto exit ;
if ( ! ( ts = ffrpmtsCreate ())) goto exit ;
if ( ! ( mi = ffrpmtsInitIterator ( ts , RPMDBI_LABEL , NULL , 0 ))) goto exit ;
2022-02-09 15:22:57 +01:00
count = ( uint32_t ) ffrpmdbGetIteratorCount ( mi );
2021-11-13 14:10:30 +01:00
exit :
if ( mi ) ffrpmdbFreeIterator ( mi );
if ( ts ) ffrpmtsFree ( ts );
dlclose ( rpm );
return count ;
}
#endif
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 ;
}
static uint32_t countFilesIn ( const char * dirname , const char * filename )
{
2022-02-12 12:06:16 +01:00
FFstrbuf baseDirPath ;
ffStrbufInitA ( & baseDirPath , 128 );
ffStrbufAppendS ( & baseDirPath , dirname );
uint32_t result = countFilesRecursive ( & baseDirPath , filename );
ffStrbufDestroy ( & baseDirPath );
return result ;
2022-02-12 12:16:14 +02:00
}
2021-02-27 18:30:05 +01:00
void ffPrintPackages ( FFinstance * instance )
2021-02-18 22:25:36 +01:00
{
2022-04-10 15:07:52 +02:00
uint32_t pacman = getNumElements ( FASTFETCH_TARGET_DIR_ROOT "/var/lib/pacman/local" , DT_DIR );
uint32_t dpkg = getNumStrings ( FASTFETCH_TARGET_DIR_ROOT "/var/lib/dpkg/status" , "Status: " );
2021-11-13 14:10:30 +01:00
#ifdef FF_HAVE_RPM
uint32_t rpm = getRpmPackageCount ( instance );
#else
uint32_t rpm = 0 ;
#endif
2021-10-20 06:04:55 +00:00
2022-04-10 15:07:52 +02:00
uint32_t emerge = countFilesIn ( FASTFETCH_TARGET_DIR_ROOT "/var/db/pkg" , "SIZE" );
uint32_t xbps = getNumElements ( FASTFETCH_TARGET_DIR_ROOT "/var/db/xbps" , DT_REG );
uint32_t flatpak = getNumElements ( FASTFETCH_TARGET_DIR_ROOT "/var/lib/flatpak/app" , DT_DIR );
uint32_t snap = getNumElements ( FASTFETCH_TARGET_DIR_ROOT "/snap" , DT_DIR );
2021-03-11 19:40:46 +01:00
2021-06-10 22:04:25 +02:00
//Accounting for the /snap/bin folder
if ( snap > 0 )
-- snap ;
2022-02-12 12:13:02 +01:00
uint32_t all = pacman + dpkg + rpm + emerge + xbps + flatpak + snap ;
2021-03-11 19:40:46 +01:00
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-05-15 17:21:02 +02:00
FFstrbuf manjaroBranch ;
ffStrbufInit ( & manjaroBranch );
2022-04-10 15:07:52 +02:00
if ( ffParsePropFile ( FASTFETCH_TARGET_DIR_ROOT "/etc/pacman-mirrors.conf" , "Branch =" , & manjaroBranch ) && manjaroBranch . length == 0 )
2021-05-15 18:12:10 +02:00
ffStrbufSetS ( & manjaroBranch , "stable" );
2021-03-27 15:39:56 +01:00
2021-04-03 21:51:50 +02:00
if ( instance -> config . packagesFormat . length == 0 )
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . batteryKey );
2021-03-11 19:40:46 +01:00
#define FF_PRINT_PACKAGE(name) \
2021-03-19 20:57:07 +01:00
if(name > 0) \
{ \
printf("%u ("#name")", name); \
if((all = all - name) > 0) \
printf(", "); \
};
2021-03-11 19:40:46 +01:00
2021-05-15 17:21:02 +02:00
if ( pacman > 0 )
{
printf ( "%u (pacman)" , pacman );
if ( manjaroBranch . length > 0 )
printf ( "[%s]" , manjaroBranch . chars );
if (( all = all - pacman ) > 0 )
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 )
2021-06-08 20:01:29 +02:00
FF_PRINT_PACKAGE ( xbps )
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 },
{ FF_FORMAT_ARG_TYPE_UINT , & pacman },
2021-05-15 18:17:34 +02:00
{ FF_FORMAT_ARG_TYPE_STRBUF , & manjaroBranch },
2021-05-01 21:22:07 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & dpkg },
2021-06-08 20:01:29 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & rpm },
2022-02-12 12:13:02 +01:00
{ FF_FORMAT_ARG_TYPE_UINT , & emerge },
2021-06-08 20:01:29 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & xbps },
2021-04-27 21:52:55 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & flatpak },
2022-02-12 12:13:02 +01:00
{ FF_FORMAT_ARG_TYPE_UINT , & 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
ffStrbufDestroy ( & manjaroBranch );
2021-03-27 18:14:19 +01:00
}