我刚刚写了一个基于nftw(1)的又快又脏的实用程序。
该实用程序基本上只是手册页示例,添加了一些统计信息。
功能性
我选择了
- 停留在单个装载点内
- 不要遵循符号链接(为了简单起见和,因为它通常是你想要的)
- 请注意,它仍然会计算符号链接本身的大小:)
- 它显示了外观大小(文件的长度)以及磁盘上的大小(分配的块)。
测试,速度
我在我的盒子上测试了二进制文件 (/tmp/test):
# clear page, dentry and attribute caches
echo 3> /proc/sys/vm/drop_caches
time /tmp/test /
输出
Total size: 28433001733
In 878794 files and 87047 directories (73318 symlinks and 0 inaccessible directories)
Size on disk 59942192 * 512b = 30690402304
real 0m2.066s
user 0m0.140s
sys 0m1.910s
我没有与您的工具进行比较,但它看起来确实相当快。也许您可以获取源代码并构建自己的版本以获得最大速度?
测试稀疏文件是否确实被正确报告:
mkdir sparse
dd bs=1M seek=1024 count=0 of=sparse/file.raw
ls -l sparse/
./test sparse/
输出:
total 0
-rw-r--r-- 1 sehe sehe 1073741824 2011-09-23 22:59 file.raw
Total size: 1073741884
In 1 files and 1 directories (0 symlinks and 0 inaccessible directories)
Size on disk 0 * 512b = 0
代码
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
static uintmax_t total = 0ul;
static uintmax_t files = 0ul;
static uintmax_t directories = 0ul;
static uintmax_t symlinks = 0ul;
static uintmax_t inaccessible = 0ul;
static uintmax_t blocks512 = 0ul;
static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
switch(tflag)
{
case FTW_D:
case FTW_DP: directories++; break;
case FTW_NS:
case FTW_SL:
case FTW_SLN: symlinks++; break;
case FTW_DNR: inaccessible++; break;
case FTW_F: files++; break;
}
total += sb->st_size;
blocks512 += sb->st_blocks;
return 0; /* To tell nftw() to continue */
}
int
main(int argc, char *argv[])
{
int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
{
perror("nftw");
exit(EXIT_FAILURE);
}
printf("Total size: %7jd\n", total);
printf("In %jd files and %jd directories (%jd symlinks and %jd inaccessible directories)\n", files, directories, symlinks, inaccessible);
printf("Size on disk %jd * 512b = %jd\n", blocks512, blocks512<<9);
exit(EXIT_SUCCESS);
}
编译...
gcc test.c -o test