El *_*opo -4 c linux directory recursion counting
以下C代码将列出文件和目录的数量,并且比linux find命令快4倍。我只需要文件夹的数量,对文件数量甚至列出它们都不感兴趣。有没有一种方法可以优化以下代码并使之更高效?
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
void listdir(char *path, size_t size) {
DIR *dir;
struct dirent *entry;
size_t len = strlen(path);
if (!(dir = opendir(path))) {
fprintf(stderr, "path not found: %s: %s\n",
path, strerror(errno));
return;
}
puts(path);
while ((entry = readdir(dir)) != NULL) {
char *name = entry->d_name;
if (entry->d_type == DT_DIR) {
if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;
if (len + strlen(name) + 2 > size) {
fprintf(stderr, "path too long: %s/%s\n", path, name);
} else {
path[len] = '/';
strcpy(path + len + 1, name);
listdir(path, size);
path[len] = '\0';
}
} else {
printf("%s/%s\n", path, name);
}
}
closedir(dir);
}
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("Path: %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
return 0;
}
char path[1024];
memcpy (path, argv[1],1024);
listdir(path, sizeof path);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
删除以下行当然不会显示文件,但不会加快执行时间:
} else {
printf("%s/%s\n", path, name);
}
Run Code Online (Sandbox Code Playgroud)
如果您对打印文件名不感兴趣,只需删除printf语句即可。
但是请注意,代码中存在一些问题:
memcpy(path, argv[1], 1024);可能会读取超出所指向的字符串末尾的argv[1],这是未定义的行为,或者无法产生适当的C字符串,这会导致函数中的未定义的行为listdir。您还可以避免在每个递归调用中重新计算目录名称的长度。
这是您可以尝试的修改版本:
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
long long countdirs(char *path, size_t size, size_t len) {
DIR *dir;
struct dirent *entry;
long long count;
if (!(dir = opendir(path))) {
fprintf(stderr, "path not found: %s: %s\n",
path, strerror(errno));
return 0;
}
count = 1; // count this directory
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char *name = entry->d_name;
size_t len1 = strlen(name);
if (*name == '.' && (len1 == 1 || (len1 == 2 && name[1] == '.')))
continue;
if (len + len1 + 2 > size) {
count++;
fprintf(stderr, "path too long: %s/%s\n", path, name);
} else {
path[len] = '/';
memcpy(path + len + 1, name, len1 + 1);
count += countdirs(path, size, len + 1 + len1);
path[len] = '\0';
}
}
}
closedir(dir);
return count;
}
int main(int argc, char *argv[]) {
char buf[4096];
char *path;
size_t len;
if (argc != 2) {
fprintf(stderr, "one argument expected.\n");
return 1;
}
path = argv[1];
len = strlen(path);
if (len >= sizeof(buf)) {
fprintf(stderr, "path too long: %s\n", path);
return 1;
}
memcpy(buf, path, len + 1);
printf("%s: %lld directories\n", path, countdirs(buf, sizeof buf, len));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
进一步说明:
如果目录树太深或具有循环,则上面的代码可能会失败。失败可能是由于耗尽了句柄而导致opendir失败。
您应该尝试使用nftw()此答案中记录的POSIX标准功能的替代方法:https : //stackoverflow.com/a/29402705/4593267
正如EOF所建议的那样,由于不使用路径,因此不需要构造它们。使用openat()和使用可能更安全更有效fdopendir()。(在此处记录:https : //pubs.opengroup.org/onlinepubs/9699919799/functions/open.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html)。
优化此功能没有任何意义,因为大多数时间都花在OS上或等待存储设备。文件系统缓存的影响可能是巨大的:我在Linux上针对133000个目录测量了15倍。使用一组不同的系统调用可能会提高或降低速度,但是小的改进可能是高度特定于系统的。