2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include <sys/statvfs.h>
2021-04-15 18:59:26 +02:00
#define FF_DISK_MODULE_NAME "Disk"
#define FF_DISK_NUM_FORMAT_ARGS 4
2021-04-03 21:51:50 +02:00
static void getKey ( FFinstance * instance , FFstrbuf * key , const char * folderPath , bool showFolderPath )
{
if ( instance -> config . diskKey . length == 0 )
{
if ( showFolderPath )
2021-04-15 18:59:26 +02:00
ffStrbufAppendF ( key , FF_DISK_MODULE_NAME " (%s)" , folderPath );
2021-04-03 21:51:50 +02:00
else
2021-04-15 18:59:26 +02:00
ffStrbufSetS ( key , FF_DISK_MODULE_NAME );
2021-04-03 21:51:50 +02:00
}
else
{
2021-04-15 18:59:26 +02:00
ffParseFormatString ( key , & instance -> config . diskKey , NULL , 1 , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_STRING , folderPath }
});
2021-04-03 21:51:50 +02:00
}
}
2021-04-03 11:39:59 +02:00
static void printStatvfs ( FFinstance * instance , FFstrbuf * key , struct statvfs * fs )
2021-02-18 22:25:36 +01:00
{
2021-03-28 15:38:52 +02:00
const uint32_t GB = 1024 * 1024 * 1024 ;
2021-02-18 22:25:36 +01:00
2021-11-17 23:22:42 +01:00
uint32_t total = ( uint32_t ) (( fs -> f_blocks * fs -> f_frsize ) / GB );
uint32_t available = ( uint32_t ) (( fs -> f_bfree * fs -> f_frsize ) / GB );
2021-03-27 15:39:56 +01:00
uint32_t used = total - available ;
2021-11-17 18:38:57 +01:00
uint8_t percentage = ( uint8_t ) (( used / ( double ) total ) * 100.0 );
2021-02-18 22:25:36 +01:00
2021-11-17 18:38:57 +01:00
uint32_t files = ( uint32_t ) ( fs -> f_files - fs -> f_ffree );
2021-03-28 15:38:52 +02:00
2021-03-23 19:48:52 +01:00
if ( instance -> config . diskFormat . length == 0 )
2021-03-19 20:57:07 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , key -> chars , 0 , NULL );
2021-03-19 20:57:07 +01:00
printf ( "%uGB / %uGB (%u%%) \n " , used , total , percentage );
}
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , key -> chars , 0 , NULL , & instance -> config . diskFormat , NULL , FF_DISK_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_UINT , & used },
{ FF_FORMAT_ARG_TYPE_UINT , & total },
{ FF_FORMAT_ARG_TYPE_UINT , & files },
{ FF_FORMAT_ARG_TYPE_UINT8 , & percentage }
});
2021-03-19 20:57:07 +01:00
}
2021-03-27 18:14:19 +01:00
}
2021-03-28 15:38:52 +02:00
2021-04-03 11:39:59 +02:00
static void printStatvfsCreateKey ( FFinstance * instance , const char * folderPath , struct statvfs * fs )
{
FF_STRBUF_CREATE ( key );
2021-04-03 21:51:50 +02:00
getKey ( instance , & key , folderPath , true );
2021-04-03 11:39:59 +02:00
printStatvfs ( instance , & key , fs );
ffStrbufDestroy ( & key );
}
2021-03-28 15:38:52 +02:00
static void printFolder ( FFinstance * instance , const char * folderPath )
{
FF_STRBUF_CREATE ( key );
2021-04-03 21:51:50 +02:00
getKey ( instance , & key , folderPath , true );
2021-03-28 15:38:52 +02:00
struct statvfs fs ;
int ret = statvfs ( folderPath , & fs );
if ( ret != 0 && instance -> config . diskFormat . length == 0 )
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , key . chars , 0 , NULL , & instance -> config . diskFormat , FF_DISK_NUM_FORMAT_ARGS , "statvfs( \" %s \" , &fs) != 0 (%i)" , folderPath , ret );
2021-03-28 15:38:52 +02:00
ffStrbufDestroy ( & key );
return ;
}
2021-04-03 11:39:59 +02:00
printStatvfs ( instance , & key , & fs );
2021-03-28 15:38:52 +02:00
ffStrbufDestroy ( & key );
}
void ffPrintDisk ( FFinstance * instance )
{
if ( instance -> config . diskFolders . length == 0 )
{
struct statvfs fsRoot ;
int rootRet = statvfs ( "/" , & fsRoot );
struct statvfs fsHome ;
2022-04-10 15:07:52 +02:00
int homeRet = statvfs ( FASTFETCH_TARGET_DIR_HOME , & fsHome );
2021-03-28 15:38:52 +02:00
if ( rootRet != 0 && homeRet != 0 )
{
2021-04-03 21:51:50 +02:00
FF_STRBUF_CREATE ( key );
2021-04-24 16:29:05 +02:00
getKey ( instance , & key , "" , false );
2022-04-10 15:07:52 +02:00
ffPrintError ( instance , key . chars , 0 , NULL , & instance -> config . diskFormat , FF_DISK_NUM_FORMAT_ARGS , "statvfs failed for both / and " FASTFETCH_TARGET_DIR_HOME );
2021-04-03 21:51:50 +02:00
ffStrbufDestroy ( & key );
2021-03-28 15:38:52 +02:00
return ;
}
if ( rootRet == 0 )
2021-04-03 11:39:59 +02:00
printStatvfsCreateKey ( instance , "/" , & fsRoot );
2021-03-28 15:38:52 +02:00
if ( homeRet == 0 && ( rootRet != 0 || fsRoot . f_fsid != fsHome . f_fsid ))
2022-04-10 15:07:52 +02:00
printStatvfsCreateKey ( instance , FASTFETCH_TARGET_DIR_HOME , & fsHome );
2021-03-28 15:38:52 +02:00
}
else
{
2021-04-24 16:29:05 +02:00
ffStrbufTrim ( & instance -> config . diskFolders , ':' );
if ( instance -> config . diskFolders . length == 0 )
{
FF_STRBUF_CREATE ( key );
getKey ( instance , & key , "" , false );
ffPrintError ( instance , key . chars , 0 , NULL , & instance -> config . diskFormat , FF_DISK_NUM_FORMAT_ARGS , "Custom disk folders string doesn't contain any folders" );
ffStrbufDestroy ( & key );
return ;
}
2021-06-26 15:34:01 +02:00
uint32_t startIndex = 0 ;
while ( startIndex < instance -> config . diskFolders . length )
2021-03-28 15:38:52 +02:00
{
2021-06-26 15:34:01 +02:00
uint32_t colonIndex = ffStrbufNextIndexC ( & instance -> config . diskFolders , startIndex , ':' );
2021-03-28 20:09:29 +02:00
instance -> config . diskFolders . chars [ colonIndex ] = '\0' ;
2021-03-28 15:38:52 +02:00
2021-06-26 15:34:01 +02:00
printFolder ( instance , instance -> config . diskFolders . chars + startIndex );
2021-03-28 20:09:29 +02:00
2021-06-26 15:34:01 +02:00
startIndex = colonIndex + 1 ;
2021-03-28 15:38:52 +02:00
}
}
}