Files
fastfetch/src/modules/disk.c
T

90 lines
2.6 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include <sys/statvfs.h>
2021-03-28 15:38:52 +02:00
static void printStatvfs(FFinstance* instance, const char* 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-03-28 15:38:52 +02:00
uint32_t total = (fs->f_blocks * fs->f_frsize) / GB;
uint32_t available = (fs->f_bfree * fs->f_frsize) / GB;
uint32_t used = total - available;
2021-03-19 20:57:07 +01:00
uint8_t percentage = (used / (double) total) * 100.0;
2021-02-18 22:25:36 +01:00
2021-03-28 15:38:52 +02:00
uint32_t files = fs->f_files - fs->f_ffree;
2021-03-23 19:48:52 +01:00
if(instance->config.diskFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2021-03-28 15:38:52 +02:00
ffPrintLogoAndKey(instance, key);
2021-03-19 20:57:07 +01:00
printf("%uGB / %uGB (%u%%)\n", used, total, percentage);
}
else
{
2021-03-28 15:38:52 +02:00
ffPrintFormatString(instance, key, &instance->config.diskFormat, 4,
2021-03-19 20:57:07 +01:00
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &used},
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &total},
2021-03-28 15:38:52 +02:00
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &files},
2021-03-19 20:57:07 +01:00
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &percentage}
);
}
2021-03-27 18:14:19 +01:00
}
2021-03-28 15:38:52 +02:00
static void printFolder(FFinstance* instance, const char* folderPath)
{
FF_STRBUF_CREATE(key);
ffStrbufAppendS(&key, "Disk (");
ffStrbufAppendS(&key, folderPath);
ffStrbufAppendC(&key, ')');
struct statvfs fs;
int ret = statvfs(folderPath, &fs);
if(ret != 0 && instance->config.diskFormat.length == 0)
{
ffPrintError(instance, key.chars, "statvfs(\"%s\", &fs) != 0", folderPath);
ffStrbufDestroy(&key);
return;
}
printStatvfs(instance, key.chars, &fs);
ffStrbufDestroy(&key);
}
void ffPrintDisk(FFinstance* instance)
{
if(instance->config.diskFolders.length == 0)
{
struct statvfs fsRoot;
int rootRet = statvfs("/", &fsRoot);
struct statvfs fsHome;
int homeRet = statvfs("/home", &fsHome);
if(rootRet != 0 && homeRet != 0)
{
ffPrintError(instance, "Disk", "statvfs failed for both / and /home");
return;
}
if(rootRet == 0)
printStatvfs(instance, "Disk (/)", &fsRoot);
if(homeRet == 0 && (rootRet != 0 || fsRoot.f_fsid != fsHome.f_fsid))
printStatvfs(instance, "Disk (/home)", &fsHome);
}
else
{
uint32_t lastIndex = 0;
while (lastIndex < instance->config.diskFolders.length)
{
uint32_t colonIndex = ffStrbufFirstIndexC(&instance->config.diskFolders, ':');
if(colonIndex < instance->config.diskFolders.length)
instance->config.diskFolders.chars[colonIndex] = '\0';
printFolder(instance, instance->config.diskFolders.chars + lastIndex);
lastIndex = colonIndex + 1;
}
}
}