Files
fastfetch/src/modules/disk.c
T

141 lines
4.2 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/printing.h"
2022-08-26 16:02:04 +02:00
#include "common/parsing.h"
2021-02-18 22:25:36 +01:00
#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
2022-09-06 13:24:57 +02:00
static void createKey(FFinstance* instance, const char* folderPath, FFstrbuf* key)
2021-04-03 21:51:50 +02:00
{
2022-06-17 18:15:36 +02:00
if(instance->config.disk.key.length == 0)
2021-04-03 21:51:50 +02:00
{
2022-09-06 13:24:57 +02:00
ffStrbufAppendS(key, FF_DISK_MODULE_NAME);
if(folderPath != NULL)
ffStrbufAppendF(key, " (%s)", folderPath);
2021-04-03 21:51:50 +02:00
}
else
{
2022-06-17 18:15:36 +02:00
ffParseFormatString(key, &instance->config.disk.key, 1, (FFformatarg[]){
2021-04-15 18:59:26 +02:00
{FF_FORMAT_ARG_TYPE_STRING, folderPath}
});
2021-04-03 21:51:50 +02:00
}
}
2022-09-06 13:24:57 +02:00
static void printStatvfs(FFinstance* instance, const FFstrbuf* key, const char* folderPath, struct statvfs* fs)
2021-02-18 22:25:36 +01:00
{
2022-08-26 16:02:04 +02:00
uint64_t total = fs->f_blocks * fs->f_frsize;
2022-09-06 13:24:57 +02:00
if(total == 0)
{
ffPrintErrorString(instance, key->chars, 0, NULL, &instance->config.disk.errorFormat, "statvfs for %s returned size 0", folderPath);
return;
}
2022-08-26 16:02:04 +02:00
uint64_t used = total - (fs->f_bfree * fs->f_frsize);
uint32_t files = (uint32_t) (fs->f_files - fs->f_ffree);
uint8_t percentage = (uint8_t) ((used / (long double) total) * 100.0);
2021-02-18 22:25:36 +01:00
2022-08-26 16:02:04 +02:00
FFstrbuf usedPretty;
ffStrbufInit(&usedPretty);
ffParseSize(used, instance->config.binaryPrefixType, &usedPretty);
2021-02-18 22:25:36 +01:00
2022-08-26 16:02:04 +02:00
FFstrbuf totalPretty;
ffStrbufInit(&totalPretty);
ffParseSize(total, instance->config.binaryPrefixType, &totalPretty);
2021-03-28 15:38:52 +02:00
2022-06-17 18:15:36 +02:00
if(instance->config.disk.outputFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey(instance, key->chars, 0, NULL);
2022-08-26 16:02:04 +02:00
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, percentage);
2021-03-19 20:57:07 +01:00
}
else
{
2022-06-17 18:15:36 +02:00
ffPrintFormatString(instance, key->chars, 0, NULL, &instance->config.disk.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
2022-08-26 16:02:04 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
{FF_FORMAT_ARG_TYPE_UINT, &files},
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
2022-09-06 13:24:57 +02:00
ffStrbufDestroy(&totalPretty);
ffStrbufDestroy(&usedPretty);
2021-03-27 18:14:19 +01:00
}
2021-03-28 15:38:52 +02:00
2022-09-06 13:24:57 +02:00
static void printFolderAutodetection(FFinstance* instance, const char* folderPath, struct statvfs* fs)
2021-04-03 11:39:59 +02:00
{
2022-09-06 13:24:57 +02:00
FFstrbuf key;
ffStrbufInit(&key);
createKey(instance, folderPath, &key);
printStatvfs(instance, &key, folderPath, fs);
2021-04-03 11:39:59 +02:00
ffStrbufDestroy(&key);
}
2022-09-06 13:24:57 +02:00
static void printFoldersAutodetection(FFinstance* instance)
2021-03-28 15:38:52 +02:00
{
2022-09-06 13:24:57 +02:00
struct statvfs fsRoot;
int rootRet = statvfs("/", &fsRoot);
struct statvfs fsHome;
int homeRet = statvfs(FASTFETCH_TARGET_DIR_HOME, &fsHome);
if(rootRet != 0 && homeRet != 0)
{
FFstrbuf key;
ffStrbufInit(&key);
createKey(instance, NULL, &key);
ffPrintErrorString(instance, key.chars, 0, NULL, &instance->config.disk.errorFormat, "statvfs for / (%i) and %s (%i) returned not zero", rootRet, FASTFETCH_TARGET_DIR_HOME, homeRet);
ffStrbufDestroy(&key);
return;
}
if(rootRet == 0)
printFolderAutodetection(instance, "/", &fsRoot);
if(homeRet == 0 && rootRet == 0 && fsRoot.f_fsid != fsHome.f_fsid)
printFolderAutodetection(instance, FASTFETCH_TARGET_DIR_HOME, &fsHome);
}
static void printFolderCustom(FFinstance* instance, const char* folderPath)
{
FFstrbuf key;
ffStrbufInit(&key);
createKey(instance, folderPath, &key);
2021-03-28 15:38:52 +02:00
struct statvfs fs;
int ret = statvfs(folderPath, &fs);
2022-09-06 13:24:57 +02:00
if(ret != 0)
2021-03-28 15:38:52 +02:00
{
2022-06-17 18:15:36 +02:00
ffPrintErrorString(instance, key.chars, 0, NULL, &instance->config.disk.errorFormat, "statvfs(\"%s\", &fs) != 0 (%i)", folderPath, ret);
2021-03-28 15:38:52 +02:00
ffStrbufDestroy(&key);
return;
}
2022-09-06 13:24:57 +02:00
printStatvfs(instance, &key, folderPath, &fs);
2021-03-28 15:38:52 +02:00
ffStrbufDestroy(&key);
}
void ffPrintDisk(FFinstance* instance)
{
2022-09-06 13:24:57 +02:00
ffStrbufTrim(&instance->config.diskFolders, ':');
2021-03-28 15:38:52 +02:00
if(instance->config.diskFolders.length == 0)
{
2022-09-06 13:24:57 +02:00
printFoldersAutodetection(instance);
return;
2021-03-28 15:38:52 +02:00
}
2022-09-06 13:24:57 +02:00
uint32_t startIndex = 0;
while (startIndex < instance->config.diskFolders.length)
2021-03-28 15:38:52 +02:00
{
2022-09-06 13:24:57 +02:00
uint32_t colonIndex = ffStrbufNextIndexC(&instance->config.diskFolders, startIndex, ':');
instance->config.diskFolders.chars[colonIndex] = '\0';
printFolderCustom(instance, instance->config.diskFolders.chars + startIndex);
startIndex = colonIndex + 1;
2021-03-28 15:38:52 +02:00
}
}