【问题标题】:macOS: Getting number of used inodes in volume programmaticallymacOS:以编程方式获取卷中使用的 inode 数量
【发布时间】:2018-09-22 05:58:17
【问题描述】:

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

我查看了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 以完成此任务。

谢谢

【问题讨论】:

  • df 打印的实际文本为例,会很方便。
  • @unwind,您好,感谢您的评论。添加了 df 输出

标签: c macos filesystems system-calls


【解决方案1】:

我想我找到了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(),正如您所期望的那样。

【讨论】:

  • 我现在意识到这个计算还包含与目录相关的 inode。我希望我知道如何省略它们,因为我只关心常规文件。
猜你喜欢
  • 1970-01-01
  • 2011-04-25
  • 2010-12-09
  • 2018-10-18
  • 2019-05-18
  • 2017-01-15
  • 2020-07-13
  • 2014-07-23
  • 2015-11-17
相关资源
最近更新 更多