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

253 lines
6.9 KiB
C
Raw Normal View History

#include "disk.h"
2023-02-01 15:56:25 +01:00
#include "util/stringUtils.h"
2022-12-29 23:19:44 +01:00
#include <limits.h>
2022-10-17 13:19:48 +02:00
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#ifdef __USE_LARGEFILE64
#define stat stat64
#define statvfs statvfs64
#define dirent dirent64
#define readdir readdir64
#endif
static bool isPhysicalDevice(const char* device)
2022-12-29 22:51:23 +01:00
{
//DrvFs is a filesystem plugin to WSL that was designed to support interop between WSL and the Windows filesystem.
if(strcmp(device, "drvfs") == 0)
return true;
2023-02-01 15:56:25 +01:00
//ZFS root pool. The format is rpool/<POOL_NAME>/<VOLUME_NAME>/<SUBVOLUME_NAME>
if(strncmp(device, "rpool/", 6) == 0)
return true;
//Pseudo filesystems don't have a device in /dev
const char* devPrefix = "/dev/";
if(strncmp(device, devPrefix, strlen(devPrefix)) != 0)
return false;
//Skip /dev/ prefix
device += strlen(devPrefix);
if(
strncmp(device, "loop", 4) == 0 || //Ignore loop devices
strncmp(device, "ram", 3) == 0 || //Ignore ram devices
strncmp(device, "fd", 2) == 0 //Ignore fd devices
) return false;
return true;
}
static void appendNextEntry(FFstrbuf* buffer, char** source)
{
//Read the current entry into the buffer
while(**source != '\0' && !isspace(**source))
2022-12-29 22:51:23 +01:00
{
2022-12-29 23:19:44 +01:00
//After a backslash the next 3 characters are octal ascii codes
if(**source == '\\' && strnlen(*source, 4) == 4)
2022-12-29 22:58:29 +01:00
{
2022-12-29 23:19:44 +01:00
char octal[4] = {0};
strncpy(octal, *source + 1, 3);
2022-12-29 22:58:29 +01:00
2022-12-29 23:19:44 +01:00
long value = strtol(octal, NULL, 8); //Returns 0 on error, so no need to check endptr
if(value > 0 && value < CHAR_MAX)
{
ffStrbufAppendC(buffer, (char) value);
*source += 4;
2022-12-29 23:19:44 +01:00
continue;
}
2022-12-29 22:51:23 +01:00
}
ffStrbufAppendC(buffer, **source);
++*source;
}
//Skip whitespace
while(isspace(**source))
++*source;
}
static void detectNameFromPath(FFDisk* disk, const struct stat* deviceStat, FFstrbuf* basePath)
{
DIR* dir = opendir(basePath->chars);
if(dir == NULL)
return;
uint32_t basePathLength = basePath->length;
struct dirent* entry;
while((entry = readdir(dir)) != NULL)
{
if(entry->d_name[0] == '.')
continue;
ffStrbufAppendS(basePath, entry->d_name);
struct stat entryStat;
bool ret = stat(basePath->chars, &entryStat) == 0;
ffStrbufSubstrBefore(basePath, basePathLength);
if(!ret || deviceStat->st_ino != entryStat.st_ino)
continue;
ffStrbufAppendS(&disk->name, entry->d_name);
break;
2022-12-29 22:51:23 +01:00
}
closedir(dir);
2022-12-29 22:51:23 +01:00
}
static void detectName(FFDisk* disk, const FFstrbuf* device)
{
struct stat deviceStat;
if(stat(device->chars, &deviceStat) != 0)
return;
FFstrbuf basePath;
ffStrbufInit(&basePath);
//Try partlabel first
ffStrbufSetS(&basePath, "/dev/disk/by-partlabel/");
detectNameFromPath(disk, &deviceStat, &basePath);
//Try label second
if(disk->name.length == 0)
{
ffStrbufSetS(&basePath, "/dev/disk/by-label/");
detectNameFromPath(disk, &deviceStat, &basePath);
}
//Use the mountpoint as a last resort
if(disk->name.length == 0)
ffStrbufAppend(&disk->name, &disk->mountpoint);
ffStrbufDestroy(&basePath);
}
#ifdef __ANDROID__
static void detectType(FF_MAYBE_UNUSED const FFlist* devices, FFDisk* currentDisk, FF_MAYBE_UNUSED const char* options)
{
2023-01-21 14:42:19 +01:00
if(ffStrbufEqualS(&currentDisk->mountpoint, "/") || ffStrbufEqualS(&currentDisk->mountpoint, "/storage/emulated"))
currentDisk->type = FF_DISK_TYPE_REGULAR;
2023-01-21 14:04:17 +08:00
else if(ffStrbufStartsWithS(&currentDisk->mountpoint, "/mnt/media_rw/"))
2023-01-21 14:42:19 +01:00
currentDisk->type = FF_DISK_TYPE_EXTERNAL;
else
2023-01-21 14:42:19 +01:00
currentDisk->type = FF_DISK_TYPE_HIDDEN;
}
#else
2023-01-21 14:42:19 +01:00
static bool isSubvolume(const FFlist* devices)
2023-01-16 11:52:54 +01:00
{
2023-01-21 14:42:19 +01:00
const FFstrbuf* currentDevie = ffListGet(devices, devices->length - 1);
2023-02-01 15:56:25 +01:00
//Filter all disks which device was already found. This catches BTRFS subvolumes.
2023-01-21 14:42:19 +01:00
for(uint32_t i = 0; i < devices->length - 1; i++)
2023-01-20 13:28:25 +01:00
{
2023-01-21 14:42:19 +01:00
const FFstrbuf* otherDevice = ffListGet(devices, i);
2023-01-16 11:52:54 +01:00
2023-01-21 14:42:19 +01:00
if(ffStrbufEqual(currentDevie, otherDevice))
2023-01-20 13:28:25 +01:00
return true;
}
2023-01-16 11:52:54 +01:00
2023-02-01 15:56:25 +01:00
//ZFS subvolumes: rpool/<POOL_NAME>/<VOLUME_NAME>/<SUBVOLUME_NAME>.
//Test if the third slash is present.
if(strncmp(currentDevie->chars, "rpool/", 6) == 0 && ffStrHasNChars(currentDevie->chars, '/', 3))
return true;
2023-01-20 13:28:25 +01:00
return false;
2023-01-16 11:52:54 +01:00
}
2023-01-21 14:42:19 +01:00
static void detectType(const FFlist* devices, FFDisk* currentDisk, const char* options)
{
2023-01-21 14:42:19 +01:00
if(isSubvolume(devices))
currentDisk->type = FF_DISK_TYPE_SUBVOLUME;
else if(strstr(options, "nosuid") != NULL || strstr(options, "nodev") != NULL)
currentDisk->type = FF_DISK_TYPE_EXTERNAL;
else if(ffStrbufStartsWithS(&currentDisk->mountpoint, "/boot") || ffStrbufStartsWithS(&currentDisk->mountpoint, "/efi"))
currentDisk->type = FF_DISK_TYPE_HIDDEN;
else
currentDisk->type = FF_DISK_TYPE_REGULAR;
}
2023-01-16 11:52:54 +01:00
#endif
static void detectStats(FFDisk* disk)
{
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
disk->bytesTotal = fs.f_blocks * fs.f_frsize;
disk->bytesUsed = disk->bytesTotal - (fs.f_bavail * fs.f_frsize);
disk->filesTotal = (uint32_t) fs.f_files;
disk->filesUsed = (uint32_t) (disk->filesTotal - fs.f_ffree);
}
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;
}
2023-01-21 14:42:19 +01:00
FFlist devices;
ffListInit(&devices, sizeof(FFstrbuf));
2022-10-17 13:19:48 +02:00
char* line = NULL;
size_t len = 0;
while(getline(&line, &len, mountsFile) != EOF)
{
if(!isPhysicalDevice(line))
continue;
//We have a valid device, add it to the list
FFDisk* disk = ffListAdd(&disks->disks);
2022-10-17 13:19:48 +02:00
//Format of the file: "<device> <mountpoint> <filesystem> <options> ..." (Same as fstab)
char* currentPos = line;
//detect device
2023-01-21 14:42:19 +01:00
FFstrbuf* device = ffListAdd(&devices);
ffStrbufInit(device);
appendNextEntry(device, &currentPos);
2022-10-17 13:19:48 +02:00
//detect mountpoint
ffStrbufInit(&disk->mountpoint);
appendNextEntry(&disk->mountpoint, &currentPos);
2022-10-17 13:19:48 +02:00
//detect filesystem
ffStrbufInit(&disk->filesystem);
appendNextEntry(&disk->filesystem, &currentPos);
2022-10-17 13:19:48 +02:00
//detect name
ffStrbufInit(&disk->name);
2023-01-21 14:42:19 +01:00
detectName(disk, device);
2022-10-17 13:19:48 +02:00
//detect type
2023-01-21 14:42:19 +01:00
detectType(&devices, disk, currentPos);
2022-10-17 13:19:48 +02:00
//Detects stats
detectStats(disk);
2022-10-17 13:19:48 +02:00
}
2022-10-17 13:19:48 +02:00
if(line != NULL)
free(line);
2023-01-21 14:42:19 +01:00
FF_LIST_FOR_EACH(FFstrbuf, device, devices)
ffStrbufDestroy(device);
ffListDestroy(&devices);
2022-10-17 13:19:48 +02:00
fclose(mountsFile);
}