From b76d6e9faa43c1a24818bd5f3fdb2a960d99d3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 9 Oct 2023 14:16:12 +0800 Subject: [PATCH] DiskIO (FreeBSD): add support --- CMakeLists.txt | 1 + src/detection/diskio/diskio_bsd.c | 48 +++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6be4f1535..5b4037c19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -890,6 +890,7 @@ elseif(BSD) target_link_libraries(libfastfetch PRIVATE "m" PRIVATE "usbhid" + PRIVATE "devstat" ) elseif(ANDROID) CHECK_LIBRARY_EXISTS(-l:libandroid-wordexp.a wordexp "" HAVE_LIBANDROID_WORDEXP_STATIC) diff --git a/src/detection/diskio/diskio_bsd.c b/src/detection/diskio/diskio_bsd.c index b86f3a0a3..f73f48f2c 100644 --- a/src/detection/diskio/diskio_bsd.c +++ b/src/detection/diskio/diskio_bsd.c @@ -1,11 +1,49 @@ #include "diskio.h" -#include "common/io/io.h" -#include "util/stringUtils.h" -#include -#include +#include +#include const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) { - return "Not supported on this platform"; + if (devstat_checkversion(NULL) < 0) + return "devstat_checkversion() failed"; + + struct statinfo stats = { + .dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)), + }; + if (devstat_getdevs(NULL, &stats) < 0) + return "devstat_getdevs() failed"; + + for (int i = 0; i < stats.dinfo->numdevs; i++) + { + struct devstat* current = &stats.dinfo->devices[i]; + + char deviceName[128]; + snprintf(deviceName, sizeof(deviceName), "%s%d", current->device_name, current->unit_number); + + if (options->namePrefix.length && strncmp(deviceName, options->namePrefix.chars, options->namePrefix.length) != 0) + continue; + + FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); + ffStrbufInitS(&device->name, deviceName); + ffStrbufInitF(&device->devPath, "/dev/%s", deviceName); + + ffStrbufInit(&device->type); + switch (current->device_type & DEVSTAT_TYPE_IF_MASK) + { + case DEVSTAT_TYPE_IF_SCSI: ffStrbufAppendS(&device->type, "SCSI"); break; + case DEVSTAT_TYPE_IF_IDE: ffStrbufAppendS(&device->type, "IDE"); break; + case DEVSTAT_TYPE_IF_OTHER: ffStrbufAppendS(&device->type, "OTHER"); break; + } + device->bytesRead = current->bytes[DEVSTAT_READ]; + device->readCount = current->operations[DEVSTAT_READ]; + device->bytesWritten = current->bytes[DEVSTAT_WRITE]; + device->writeCount = current->operations[DEVSTAT_WRITE]; + } + + if (stats.dinfo->mem_ptr) + free(stats.dinfo->mem_ptr); + free(stats.dinfo); + + return NULL; }