【发布时间】:2011-06-06 10:58:24
【问题描述】:
我想列出目录中的常规文件。但是,stat 对每个文件都失败了。
DIR* dp = NULL;
struct dirent* entry = NULL;
dp = opendir(directory);
if (!dp) { log_err("Could not open directory"); return -1; }
while (entry = readdir(dp))
{
struct stat s;
char path[1024]; path[0] = 0;
strcat(path, directory);
strcat(path, entry->d_name);
int status = 0;
if (status = stat(path, &s))
{
if (S_ISREG(s.st_mode))
{
printf("%s\n", entry->d_name);
}
}
else
{
fprintf(stderr, "Can't stat: %s\n", strerror(errno));
}
}
closedir(dp);
输出是
无法统计:资源暂时 不可用
无法统计:资源暂时 不可用
无法统计:资源暂时 不可用
(……很多次)
errno 设置为 E_AGAIN (11)。
现在,如果我打印结果 path,它们确实是有效的文件和目录名称。该目录是可读的,与我一起运行的用户确实有权这样做(这是我编写程序的目录)。
是什么导致了这个问题,我该如何正确地做到这一点?
【问题讨论】:
-
您是否检查过指向该文件的路径中的所有目录都允许执行权限?