立即查询HFS或APFS卷上的文件和文件夹总数

问题描述 投票:1回答:1

macOS 32位API提供了一种立即查询本地卷上文件和文件夹数量的方法,因为这些信息直接记录在HFS卷标题中,以及APFS,FAT和NTFS卷上。

我喜欢使用64位API读取这些相同的卷,例如如果可能的话,使用像fsstatfsctl这样的POSIX或BSD调用。但是,我找不到一个。

我曾希望statfs()会在f_files结构域中给我这个值:

 long    f_files;    /* total file nodes in file system */

但是,该值始终是固定的(0xffffffef),因此无用。

我知道这些值可能不完全准确,但这不是必需的。在扫描整个卷时,我只需要预先大致预测总搜索时间的值。

macos filesystems
1个回答
3
投票

我没有mac,但它似乎在Linux上使用hfsplus进行快速测试:

[root@tuxpad tmp]# dd if=/dev/zero of=filesystem.img bs=1024 count=102400
102400+0 records in
102400+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.130317 s, 805 MB/s
[root@tuxpad tmp]#
[root@tuxpad tmp]# mkfs.hfsplus filesystem.img
Initialized filesystem.img as a 100 MB HFS Plus volume
[root@tuxpad tmp]#
[root@tuxpad tmp]# mount -o loop filesystem.img /mnt/
[root@tuxpad tmp]#
[root@tuxpad tmp]# ./a.out /mnt
f_type=18475
(Total inodes (f_files) = 4294967295
Free inodes (f_ffree) =4294967278
Total no. of files = (f_files - f_ffree) =17
[root@tuxpad tmp]#
[root@tuxpad tmp]# touch /mnt/file{1..100}
[root@tuxpad tmp]#
[root@tuxpad tmp]# ./a.out /mnt
f_type=18475
(Total inodes (f_files) = 4294967295
Free inodes (f_ffree) =4294967178
Total no. of files = (f_files - f_ffree) =117

代码段:

#include <stdio.h>
#include <stdlib.h>
#include <sys/vfs.h>    /* or <sys/statfs.h> */

int main (int argc, char**argv)
{
        struct statfs buf = {0};
        int ret = 0;

        ret = statfs(argv[1], &buf);
        if (ret) {
                perror("statfs");
                return -1;
        }

        printf("f_type=%llu\n", buf.f_type);
        printf("(Total inodes (f_files) = %llu\n", buf.f_files);
        printf("Free inodes (f_ffree) =%llu\n", buf.f_ffree);
        printf("Total no. of files = (f_files - f_ffree) =%llu\n", buf.f_files -buf.f_ffree);

        return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.