macOS:以编程方式获取卷中已使用的索引节点数

macOS: Getting number of used inodes in volume programmatically

我想使用函数 API 而不是使用 df shell 命令的输出来获取指定卷的已用索引节点数。

我看了getattrlist的man page,发现了下面的属性,不过也有可能是指硬链接,不过是指向已有的inode,不想算了不止一次。

ATTR_VOL_FILECOUNT        A u_int32_t containing the number of files on the volume.

我还尝试 运行 dtruss df 并搜索检索该值的确切系统调用,但我无法确定:

csops(0x872, 0x7, 0x7FFEEE4C8E80)        = 0 0
sysctl([CTL_KERN, 14, 1, 2162, 0, 0] (4), 0x7FFEEE4C8FC8, 0x7FFEEE4C8FC0, 0x0, 0x0)      = 0 0
csops(0x872, 0x7, 0x7FFEEE4C8770)        = 0 0
getfsstat64(0x0, 0x0, 0x2)       = 6 0
getfsstat64(0x7FFD41001600, 0x3B48, 0x2)         = 6 0
getfsstat64(0x7FFD41001600, 0x3B48, 0x1)         = 6 0
getrlimit(0x1008, 0x7FFEEE4C9EC0, 0x0)       = 0 0
fstat64(0x1, 0x7FFEEE4C9ED8, 0x0)        = 0 0
ioctl(0x1, 0x4004667A, 0x7FFEEE4C9F24)       = 0 0  

这里是 df 输出(注意使用的字段)

Filesystem    512-blocks      Used Available Capacity iused               ifree %iused  Mounted on
/dev/disk1s1   976695384 757288824 211770792    79% 2000778 9223372036852775029    0%   /

关于我在哪里可以找到 df 或其他 API 的源代码的任何想法。

谢谢

我想我找到了 the source 并且它是这样做的:

if (iflag) {
        inodes = sfsp->f_files;
        used = inodes - sfsp->f_ffree;
        (void)printf(" %*llu %*llu %4.0f%% ", mwp->iused, used,
            mwp->ifree, sfsp->f_ffree, inodes == 0 ? 100.0 :
            (double)used / (double)inodes * 100.0);

其中 sfsp 是指向 struct statfs 实例的指针,来自 statfs(),正如您所期望的那样。