【发布时间】:2016-04-24 03:52:57
【问题描述】:
我正在计算包含数百万个文件(每个文件约 1K)的文件夹的大小,而 NSFileManager / NSDirectoryEnumerator 只是没有削减它。
我突然想到在 Unix 级别而不是 Foundation 上执行此操作。我知道 fork() 不鼓励/禁用,首选方法是调用 posix_spawn 并将输出重定向/管道到 C 字符串缓冲区。如何做到这一点?
不鼓励实施:
FILE *fp;
int status;
char output[PATH_MAX];
NSString *cmd = [NSString stringWithFormat:@"du -sk %@", folderPath];
fp = popen([cmd UTF8String], "r");
if (fp == NULL) {
NSLog(@"could not get file pointer");
return 0;
}
while (fgets(output, PATH_MAX, fp) != NULL) {
NSLog(@"size and path: %s", output);
}
status = pclose(fp);
if (status == -1) {
NSLog(@"could not close file pointer");
} else {
NSLog(@"closed file pointer");
}
【问题讨论】:
-
这些文件是从哪里来的?这是什么 iOS 应用?
-
它们是我的应用程序使用的专有文件...现在让我们推迟有关减少磁盘上文件数量的讨论...
-
建议实际上是算作添加或关闭设备...
-
我已经考虑过这个选项,这是一种可能。由于unix磁盘使用命令是如此之快,我想进一步探索。
标签: ios c filesystems posix foundation