master ohmu/fastfetch-openbsd / src / detection / disk / disk.c
ohmu  ·  2026-10-15
 1#include "disk.h"
 2
 3static int compareDisks(const FFDisk* disk1, const FFDisk* disk2) {
 4    return ffStrbufComp(&disk1->mountpoint, &disk2->mountpoint);
 5}
 6
 7const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks) {
 8    const char* error = ffDetectDisksImpl(options, disks);
 9
10    if (error) {
11        return error;
12    }
13    if (disks->length == 0) {
14        return nullptr;
15    }
16
17    // We need to sort the disks, so that we can detect, which disk a path resides on
18    //  For example for /boot/efi/bootmgr we need to check /boot/efi before /boot
19    // Note that we sort alphabetically here for a better ordering when printing the list,
20    //  so the check must be done in reverse order
21    ffListSort(disks, sizeof(FFDisk), (void*) compareDisks);
22    FF_LIST_FOR_EACH (FFDisk, disk, *disks) {
23        if (disk->bytesTotal == 0) {
24            disk->type |= FF_DISK_VOLUME_TYPE_UNKNOWN_BIT;
25        }
26    }
27
28    return nullptr;
29}
30
31#ifndef _WIN32
32    #include <fnmatch.h>
33
34bool ffDiskMatchesFolderPatterns(FFstrbuf* folders, const char* path, char separator) {
35    uint32_t startIndex = 0;
36    while (startIndex < folders->length) {
37        uint32_t sepIndex = ffStrbufNextIndexC(folders, startIndex, separator);
38
39        char savedSep = folders->chars[sepIndex]; // Can be '\0' if at end
40        folders->chars[sepIndex] = '\0';
41
42        bool matched = fnmatch(&folders->chars[startIndex], path, 0) == 0;
43        folders->chars[sepIndex] = savedSep;
44
45        if (matched) {
46            return true;
47        }
48
49        startIndex = sepIndex + 1;
50    }
51    return false;
52}
53#endif