2022-10-27 14:58:29 +08:00
|
|
|
#include "disk.h"
|
|
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
2023-08-18 21:23:23 +08:00
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
2022-10-27 14:58:29 +08:00
|
|
|
static void detectFsInfo(struct statfs* fs, FFDisk* disk)
|
|
|
|
|
{
|
2023-08-18 21:23:23 +08:00
|
|
|
if(ffStrbufEqualS(&disk->filesystem, "zfs"))
|
|
|
|
|
{
|
|
|
|
|
disk->type = !ffStrStartsWith(fs->f_mntfromname, "zroot/") || ffStrStartsWith(fs->f_mntfromname, "zroot/ROOT/")
|
|
|
|
|
? FF_DISK_TYPE_REGULAR_BIT
|
|
|
|
|
: FF_DISK_TYPE_SUBVOLUME_BIT;
|
|
|
|
|
}
|
|
|
|
|
else if(!ffStrStartsWith(fs->f_mntfromname, "/dev/"))
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_HIDDEN_BIT;
|
2023-08-18 21:23:23 +08:00
|
|
|
else if(!(fs->f_flags & MNT_LOCAL))
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_EXTERNAL_BIT;
|
2022-10-27 14:58:29 +08:00
|
|
|
else
|
2023-06-08 21:34:56 +08:00
|
|
|
disk->type = FF_DISK_TYPE_REGULAR_BIT;
|
2022-10-27 14:58:29 +08:00
|
|
|
|
|
|
|
|
ffStrbufInit(&disk->name);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
void detectFsInfo(struct statfs* fs, FFDisk* disk);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-06-12 22:09:52 +08:00
|
|
|
const char* ffDetectDisksImpl(FFlist* disks)
|
2022-10-27 14:58:29 +08:00
|
|
|
{
|
|
|
|
|
struct statfs* buf;
|
|
|
|
|
|
|
|
|
|
int size = getmntinfo(&buf, MNT_WAIT);
|
|
|
|
|
if(size <= 0)
|
2023-06-12 22:09:52 +08:00
|
|
|
return "getmntinfo(&buf, MNT_WAIT) failed";
|
2022-10-27 14:58:29 +08:00
|
|
|
|
|
|
|
|
for(struct statfs* fs = buf; fs < buf + size; ++fs)
|
|
|
|
|
{
|
2023-06-12 22:09:52 +08:00
|
|
|
FFDisk* disk = ffListAdd(disks);
|
2022-10-27 14:58:29 +08:00
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
|
// f_bavail and f_ffree are signed on FreeBSD...
|
|
|
|
|
if(fs->f_bavail < 0) fs->f_bavail = 0;
|
|
|
|
|
if(fs->f_ffree < 0) fs->f_ffree = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
disk->bytesTotal = fs->f_blocks * fs->f_bsize;
|
2023-08-01 16:09:20 +08:00
|
|
|
disk->bytesUsed = disk->bytesTotal - ((uint64_t)fs->f_bfree * fs->f_bsize);
|
2022-10-27 14:58:29 +08:00
|
|
|
|
|
|
|
|
disk->filesTotal = (uint32_t) fs->f_files;
|
|
|
|
|
disk->filesUsed = (uint32_t) (disk->filesTotal - (uint64_t)fs->f_ffree);
|
|
|
|
|
|
|
|
|
|
ffStrbufInitS(&disk->mountpoint, fs->f_mntonname);
|
|
|
|
|
ffStrbufInitS(&disk->filesystem, fs->f_fstypename);
|
|
|
|
|
detectFsInfo(fs, disk);
|
|
|
|
|
}
|
2023-06-12 22:09:52 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
2022-10-27 14:58:29 +08:00
|
|
|
}
|