From f511688f83dc0386216f9ef097d88ea44cb160a6 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 26 Apr 2023 09:46:54 -0400 Subject: darwin: implement get_block_devices --- common.h | 3 +++ darwin.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/common.h b/common.h index 9cc3e6b..a5a1fee 100644 --- a/common.h +++ b/common.h @@ -19,6 +19,9 @@ #elif defined(__APPLE__) #include #include +#include +#include +#include #endif union regs_t { diff --git a/darwin.c b/darwin.c index 4a5986b..2d9b3f6 100644 --- a/darwin.c +++ b/darwin.c @@ -33,6 +33,52 @@ int get_sys_os_dist(char **name, char **version) { } struct Block_Device **get_block_devices(size_t *total) { - return NULL; + struct Block_Device **result; + size_t devices_total; + size_t count; + struct statfs *mnt; + int mnt_flags; + + if (!(devices_total = getmntinfo(&mnt, mnt_flags))) { + perror("Unable to read mount points"); + return NULL; + } + + result = calloc(devices_total + 1, sizeof(*result)); + if (!result) { + perror("Unable to allocate device array"); + return NULL; + } + + count = 0; + char path_prev[PATH_MAX] = {0}; + for (size_t i = 0; i < devices_total; i++) { + if (strncmp(mnt[i].f_mntfromname, "/dev/", 5) != 0) { + continue; + } + result[count] = calloc(1, sizeof(*result[0])); + if (!result[count]) { + perror("Unable to allocate mount point record"); + exit(1); + } + char path_temp[PATH_MAX] = {0}; + size_t dnum = 0; + sscanf(&mnt[i].f_mntfromname[5], "disk%zus%*s", &dnum); + sprintf(path_temp, "disk%zu", dnum); + if (strlen(path_prev) && !strcmp(path_prev, path_temp)) { + continue; + } + strcpy(path_prev, path_temp); + result[count]->path = strdup(path_temp); + result[count]->model = strdup("Unnamed"); + result[count]->size = (mnt[i].f_bsize * mnt[i].f_blocks) / 1024; + count++; + } + if (devices_total != count) { + *total = count; + } else { + *total = devices_total; + } + return result; } #endif -- cgit