Files
fastfetch/src/detection/disk/disk_linux.c
T

93 lines
2.9 KiB
C
Raw Normal View History

#include "disk.h"
2022-10-17 13:19:48 +02:00
#include <ctype.h>
#include <sys/statvfs.h>
2022-10-17 13:19:48 +02:00
void ffDetectDisksImpl(FFDiskResult* disks)
{
2022-10-17 13:19:48 +02:00
FILE* mountsFile = fopen("/proc/mounts", "r");
if(mountsFile == NULL)
{
ffStrbufAppendS(&disks->error, "fopen(\"/proc/mounts\", \"r\") == NULL");
return;
}
char* line = NULL;
size_t len = 0;
while(getline(&line, &len, mountsFile) != EOF)
{
//Format of the file: "<device> <mountpoint> <filesystem> <options> ..." (Same as fstab)
char* currentPos = line;
//Non pseudo filesystems have their device in /dev/, we only add those
if(strncasecmp(currentPos, "/dev/", 5) != 0)
continue;
//Skip /dev/
currentPos += 5;
//Don't show loop file systems
if(strncasecmp(currentPos, "loop", 4) == 0)
continue;
FFDisk* disk = ffListAdd(&disks->disks);
//Go to mountpoint
while(!isspace(*currentPos) && *currentPos != '\0')
++currentPos;
while(isspace(*currentPos))
++currentPos;
ffStrbufInitA(&disk->mountpoint, 16);
ffStrbufAppendSUntilC(&disk->mountpoint, currentPos, ' ');
//Go to filesystem
currentPos += disk->mountpoint.length;
while(isspace(*currentPos))
++currentPos;
ffStrbufInitA(&disk->filesystem, 16);
ffStrbufAppendSUntilC(&disk->filesystem, currentPos, ' ');
//Go to options, detect type
currentPos += disk->filesystem.length;
while(isspace(*currentPos))
++currentPos;
#ifdef __ANDROID__
if(ffStrbufEqualS(&disk->mountpoint, "/") || ffStrbufEqualS(&disk->mountpoint, "/storage/emulated"))
disk->type = FF_DISK_TYPE_REGULAR;
else if(ffStrbufStartsWithS(&disk->mountpoint, "/mnt/media_rw/"))
disk->type = FF_DISK_TYPE_EXTERNAL;
else
disk->type = FF_DISK_TYPE_HIDDEN;
#else
2022-10-17 13:19:48 +02:00
if(strstr(currentPos, "nosuid") != NULL || strstr(currentPos, "nodev") != NULL)
disk->type = FF_DISK_TYPE_EXTERNAL;
else if(ffStrbufStartsWithS(&disk->mountpoint, "/boot") || ffStrbufStartsWithS(&disk->mountpoint, "/efi"))
disk->type = FF_DISK_TYPE_HIDDEN;
else
disk->type = FF_DISK_TYPE_REGULAR;
#endif
2022-10-17 13:19:48 +02:00
//Detects stats
struct statvfs fs;
if(statvfs(disk->mountpoint.chars, &fs) != 0)
memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too
2022-10-17 13:19:48 +02:00
disk->bytesTotal = fs.f_blocks * fs.f_frsize;
disk->bytesUsed = disk->bytesTotal - (fs.f_bavail * fs.f_frsize);
2022-10-17 13:19:48 +02:00
disk->filesTotal = (uint32_t) fs.f_files;
disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree);
2022-10-18 18:39:28 +08:00
ffStrbufInit(&disk->name); //TODO: implement this
2022-10-17 13:19:48 +02:00
}
2022-10-17 13:19:48 +02:00
if(line != NULL)
free(line);
2022-10-17 13:19:48 +02:00
fclose(mountsFile);
}