dirent.h 将所有文件读取为 DT_UNKOWN

dirent.h reads all files as DT_UNKOWN

我正在使用 dirent.h 递归读取目录中的文件。在我的 Debian GNU/Linux 7 (wheezy) 机器上它工作正常,但是在 Ubuntu 12.04 LTS 服务器上它读取所有文件为 DT_UNKOWN!

if ((dir = opendir (input_dir)) != NULL)
{
  while ((ent = readdir (dir)) != NULL)
    {
       // cat dir path to file
      char full_file_path[FILE_NAME_LENGTH];
      strcpy (full_file_path, input_dir);
      strcat (full_file_path, "/");
      strcat (full_file_path, ent->d_name);
      // if "." or "..", skip it
      if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, ".."));
        // if a regular file, process it
      else if (ent->d_type == DT_REG)
        {
          process_file (full_file_path, f_out, z, ws);
        }
        // if a directory, recurse through it
      else if (ent->d_type == DT_DIR)
        {
          // add '/' to the end of the directory path
          process_directory (full_file_path, f_out, z, ws);
        }
      else
        {
          printf ("%s is neither a regular file nor a directory!\n",
                  full_file_path);
        }
    }
}

根据手册页 (man 3 readdir):(虽然这是来自 Fedora,但我想 Ubuntu 不会有太大不同)

Currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4) have full support for returning the file type in d_type. All applications must properly handle a return of DT_UNKNOWN.

无法保证可以从 d_type 中读取文件类型。您应该回退到使用 stat 来获取您需要的信息。