0
DIR *dir_ptr;
struct dirent *dir_entery;
dir_ptr = opendir("/tmp");

while (dir_ptr&&(dir_entery = readdir(dir_ptr))) {
   printf("%s \n", dir_entery->d_name);
}

printf("%s \n", strerror(errno));

gives this output:

file_name
dir_name
errno = Remote I/O error

in /tmp I have one dir and two files when get to readdir after the execution of opendir(dir) It exits the while and put this error:

errno = Remote I/O error

Why it fails to read the file after the dir in the /tmp directory?

0x90
  • 39,472
  • 36
  • 165
  • 245
  • 1
    Read the manual carefully http://linux.die.net/man/3/readdir – akhil May 30 '13 at 16:54
  • errno 121 is `EREMOTEIO` (Remote I/O error). Perhaps there are permission problems on the target file system. Did you tried your program to run with root (supper user) permission, if you are in Ubuntu try with `sudo` – Grijesh Chauhan May 30 '13 at 16:54
  • I would assume that the 2nd file has unusual attributes, name, access rights, etc. Try runing in a lower directory. Maybe its choking on ".."? – chux - Reinstate Monica May 30 '13 at 16:54
  • I chmod all the files in `/tmp`: `chmod -R 0777 /tmp` – 0x90 May 30 '13 at 16:59
  • What is the output of `ls -la /tmp`? Hide the actual file names if you need to keep them private. – jxh May 30 '13 at 17:04
  • Regarding `EREMOTEIO` please also see my updated answer below. You seem to be on the wrong trail ... – alk May 30 '13 at 17:05
  • Is the code posted the exact code you are using to produce the issues described? Form the output you show I doubt this. – alk May 30 '13 at 17:11
  • @user315052 IT CONTAINS 1 DIR AND TWO FILES FOR SURE ALL 0777. – 0x90 May 31 '13 at 10:03
  • @akhil WHY DO YOU THINK I DIDN'T READ IT ? – 0x90 May 31 '13 at 10:04
  • No need to get so frustrated. I am trying to figure out what `ls` is doing that is different from what your program is doing, and seeing the output is one way to diagnose it. It seems you are unwilling to share the output. Is your program multi-threaded? – jxh May 31 '13 at 14:26
  • @akhil I wasn't frustrated, sorry if I misunderstood. no multi-threaded the device is not accessible for me now. – 0x90 May 31 '13 at 14:28

1 Answers1

1

readdir() is not documented to return REREMOTEIO, so most likely sterror() gives misleading information.

Set errno to 0 before entering the while() loop, that is before calling readdir().

From man readdir:

If the end of the directory stream is reached, NULL is returned and errno is not changed. If an error occurs, NULL is returned and errno is set appropriately. To distinguish end of stream and from an error, set errno to zero before calling readdir() and then check the value of errno if NULL is returned.

To test these two cases when readdir() returns NULL you might like to modify your code like this:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

  ...

  DIR * dir_ptr = opendir("/tmp");
  if (NULL != dir_ptr)
  {
    perror("opendir() failed");
  }
  else
  {
    struct dirent * dir_entery;

    errno = 0; 
    while ((dir_entery = readdir(dir_ptr))) /* an extra pair of parenthesis here to silence GCC */
    {
      printf("%s\n", dir_entery->d_name);
    }

    if (0 != errno)
    {
      perror("readdir() failed");
    }
    else
    {
      printf("No more entries.\n");
    }
  }

  ...
alk
  • 69,737
  • 10
  • 105
  • 255