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>
2022-02-12 12:16:14 +02:00
#include <sys/stat.h>
2021-02-18 22:25:36 +01:00
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-01-22 22:54:20 +01:00
FF_LIBRARY_LOAD ( rpm , instance -> config . librpm , 0 , "librpm.so" , "librpm.so.4" )
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:16:14 +02:00
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 )
{
if (( size_t )( end - dirBuffer ) + filename_len_cache + 2 >= BUFSIZE )
return 0 ;
* 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
return 1 ;
* end = 0 ;
// Did not find it, search below
DIR * dirp = opendir ( dirBuffer );
if ( ! dirp )
return 0 ;
* end = '/' ;
uint32_t sum = 0 ;
struct dirent * entry ;
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 );
}
}
}
closedir ( dirp );
return sum ;
}
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 );
}
2021-02-27 18:30:05 +01:00
void ffPrintPackages ( FFinstance * instance )
2021-02-18 22:25:36 +01:00
{
2021-05-01 21:22:07 +02:00
uint32_t pacman = getNumElements ( "/var/lib/pacman/local" , DT_DIR );
uint32_t dpkg = getNumStrings ( "/var/lib/dpkg/status" , "Status: " );
2021-10-20 06:04:55 +00:00
2021-11-13 14:10:30 +01:00
#if __ANDROID__
dpkg += getNumStrings ( "/data/data/com.termux/files/usr/var/lib/dpkg/status" , "Status: " );
#endif
#ifdef FF_HAVE_RPM
uint32_t rpm = getRpmPackageCount ( instance );
#else
uint32_t rpm = 0 ;
#endif
2021-10-20 06:04:55 +00:00
2021-06-08 20:01:29 +02:00
uint32_t xbps = getNumElements ( "/var/db/xbps" , DT_REG );
2021-05-01 21:22:07 +02:00
uint32_t flatpak = getNumElements ( "/var/lib/flatpak/app" , DT_DIR );
uint32_t snap = getNumElements ( "/snap" , DT_DIR );
2022-02-12 12:16:14 +02:00
uint32_t emerge = countFilesIn ( "/var/db/pkg" , "SIZE" );
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:16:14 +02:00
uint32_t all = pacman + dpkg + rpm + xbps + flatpak + snap + emerge ;
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 );
2021-05-15 18:12:10 +02:00
if ( ffParsePropFile ( "/etc/pacman-mirrors.conf" , "Branch =" , & manjaroBranch ) && manjaroBranch . length == 0 )
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 )
FF_PRINT_PACKAGE ( xbps )
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE ( flatpak )
FF_PRINT_PACKAGE ( snap )
2022-02-12 12:16:14 +02:00
FF_PRINT_PACKAGE ( emerge )
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 },
{ FF_FORMAT_ARG_TYPE_UINT , & xbps },
2021-04-27 21:52:55 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & flatpak },
2022-02-12 12:16:14 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & snap },
{ FF_FORMAT_ARG_TYPE_UINT , & emerge },
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
}