evisum/src/bin/system/disks.c

221 lines
4.6 KiB
C
Raw Normal View History

2018-06-09 03:30:07 -07:00
#define _DEFAULT_SOURCE
#include "disks.h"
#include <Ecore.h>
#include <Ecore_File.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2018-06-09 10:39:37 -07:00
#if defined(__linux__)
#include <sys/vfs.h>
#endif
2018-06-09 03:30:07 -07:00
#if defined(__APPLE__) && defined(__MACH__)
# define __MacOS__
#endif
#if defined (__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/types.h>
# include <sys/sysctl.h>
# include <sys/param.h>
# include <sys/ucred.h>
# include <sys/mount.h>
#endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <sys/param.h>
# include <sys/sysctl.h>
# include <sys/mount.h>
#endif
char *
disk_mount_point_get(const char *path)
{
#if defined(__MacOS__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
struct statfs *mounts;
int i, count;
count = getmntinfo(&mounts, MNT_WAIT);
for (i = 0; i < count; i++)
{
if (!strcmp(path, mounts[i].f_mntfromname))
{
return strdup(mounts[i].f_mntonname);
}
}
#elif defined(__linux__)
char buf[4096];
char *start, *end;
FILE *f = fopen("/proc/mounts", "r");
2019-09-08 14:10:56 -07:00
if (!f)
return NULL;
2018-06-09 03:30:07 -07:00
while ((fgets(buf, sizeof(buf), f)) != NULL)
{
start = &buf[0];
end = strchr(start, ' ');
if (!end) continue;
2020-06-07 17:05:30 -07:00
*end = '\0';
2018-06-09 03:30:07 -07:00
if (!strcmp(path, start))
{
start = end + 1;
if (!start) continue;
end = strchr(start, ' ');
if (!end) continue;
2020-06-07 17:05:30 -07:00
*end = '\0';
2018-06-09 03:30:07 -07:00
fclose(f);
return strdup(start);
}
}
fclose(f);
#endif
return NULL;
}
static int
_cmp_cb(const void *p1, const void *p2)
{
const char *s1, *s2;
s1 = p1; s2 = p2;
return strcmp(s1, s2);
}
Eina_List *
disks_get(void)
{
2020-06-07 17:16:57 -07:00
Eina_List *list = NULL;
2018-06-09 03:30:07 -07:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
struct statfs *mounts;
char *drives, *dev, *end;
2020-06-07 17:05:30 -07:00
int count;
2018-06-09 03:30:07 -07:00
size_t len;
if ((sysctlbyname("kern.disks", NULL, &len, NULL, 0)) == -1)
return NULL;
drives = malloc(len + 1);
if ((sysctlbyname("kern.disks", drives, &len, NULL, 0)) == -1)
{
free(drives);
return NULL;
}
dev = drives;
while (dev)
{
end = strchr(dev, ' ');
if (!end)
break;
*end = '\0';
2020-06-07 17:05:30 -07:00
list = eina_list_append(list, strdup(eina_slstr_printf("/dev/%s", dev)));
2018-06-09 03:30:07 -07:00
dev = end + 1;
}
free(drives);
count = getmntinfo(&mounts, MNT_WAIT);
2020-06-07 17:05:30 -07:00
for (int i = 0; i < count; i++)
2018-06-09 03:30:07 -07:00
{
list = eina_list_append(list, strdup(mounts[i].f_mntfromname));
}
#elif defined(__OpenBSD__) || defined(__NetBSD__)
static const int mib[] = { CTL_HW, HW_DISKNAMES };
static const unsigned int miblen = 2;
struct statfs *mounts;
char *drives, *dev, *end;
size_t len;
2020-06-07 17:05:30 -07:00
int count;
2018-06-09 03:30:07 -07:00
if ((sysctl(mib, miblen, NULL, &len, NULL, 0)) == -1)
return NULL;
drives = malloc(len + 1);
if ((sysctl(mib, miblen, drives, &len, NULL, 0)) == -1)
{
free(drives);
2018-10-02 07:38:06 -07:00
return NULL;
2018-06-09 03:30:07 -07:00
}
dev = drives;
while (dev)
{
end = strchr(dev, ':');
2018-10-02 07:38:06 -07:00
if (!end) break;
2018-06-09 03:30:07 -07:00
*end = '\0';
2018-10-02 07:38:06 -07:00
if (dev[0] == ',')
2018-06-09 03:30:07 -07:00
dev++;
2020-06-07 17:05:30 -07:00
list = eina_list_append(list, strdup(eina_slstr_printf("/dev/%s", dev)));
2018-06-09 03:30:07 -07:00
end++;
2018-10-02 07:38:06 -07:00
dev = strchr(end, ',');
if (!dev) break;
}
2018-06-09 03:30:07 -07:00
free(drives);
count = getmntinfo(&mounts, MNT_WAIT);
2020-06-07 17:05:30 -07:00
for (int i = 0; i < count; i++)
2018-06-09 03:30:07 -07:00
{
list = eina_list_append(list, strdup(mounts[i].f_mntfromname));
}
#elif defined(__MacOS__)
2020-06-07 17:24:30 -07:00
Eina_List *devs;
2018-06-09 03:30:07 -07:00
char *name;
devs = ecore_file_ls("/dev");
EINA_LIST_FREE(devs, name)
{
if (!strncmp(name, "disk", 4))
{
2020-06-07 17:05:30 -07:00
list = eina_list_append(list, strdup(eina_slstr_printf("/dev/%s", name)));
2018-06-09 03:30:07 -07:00
}
2018-06-09 10:48:22 -07:00
free(name);
2018-06-09 03:30:07 -07:00
}
#elif defined(__linux__)
2020-06-07 17:24:30 -07:00
Eina_List *devs;
2018-06-09 03:30:07 -07:00
char *name;
2018-06-17 09:36:25 -07:00
const char *disk_search = "/dev/disk/by-uuid";
2018-06-09 03:30:07 -07:00
2018-06-17 09:36:25 -07:00
devs = ecore_file_ls(disk_search);
if (!devs)
{
disk_search = "/dev/disk/by-path";
devs = ecore_file_ls(disk_search);
}
2018-06-09 03:30:07 -07:00
EINA_LIST_FREE(devs, name)
{
2020-02-16 13:40:26 -08:00
char *real = realpath(eina_slstr_printf("%s/%s", disk_search, name), NULL);
2018-06-09 03:30:07 -07:00
if (real)
{
list = eina_list_append(list, real);
}
free(name);
}
devs = ecore_file_ls("/dev/mapper");
EINA_LIST_FREE(devs, name)
{
2020-02-16 13:40:26 -08:00
list = eina_list_append(list, strdup(eina_slstr_printf("/dev/mapper/%s", name)));
free(name);
}
2020-06-07 17:05:30 -07:00
#endif
2018-06-09 03:30:07 -07:00
2020-06-07 17:24:30 -07:00
return eina_list_sort(list, eina_list_count(list), _cmp_cb);
2018-06-09 03:30:07 -07:00
}
2018-10-02 07:38:06 -07:00