0

I am using dirent.h to read files in directories recursively. On my Debian GNU/Linux 7 (wheezy) machine it works properly, however on an Ubuntu 12.04 LTS server it reads all files as 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);
        }
    }
}
Waleed Lotfy
  • 27
  • 1
  • 9
  • I have two ideas concerning DT_UNKOWN: 1) server's filesystem is not supported by dirent (check the documentation); 2) your application has some troubles with access rights (try to run with sudo) – VolAnd Apr 14 '15 at 13:17
  • Turns out to be (1) is my case, the filesystem is jfs which is not supported by dirent. Thank you! – Waleed Lotfy Apr 15 '15 at 06:56

1 Answers1

0

According to man page (man 3 readdir): (although this is from Fedora, I guess Ubuntu won't be much different)

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.

There are no guarantees that the file type can be read from d_type. You should fall back to using stat to get the information you need.

Lubomír Sedlář
  • 1,580
  • 2
  • 17
  • 26