about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/readdir.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2022-09-19 08:10:41 +0200
committerFlorian Weimer <fweimer@redhat.com>2022-09-19 12:04:57 +0200
commit766b73768b290b303f5b56268c6c0d588d5a9267 (patch)
treedf520afacddc7803a4baf8ec5ba6f121525c3c20 /sysdeps/unix/sysv/linux/readdir.c
parent7ae60af75b78f408420512c58fd5a08ca7a88bad (diff)
downloadglibc-766b73768b290b303f5b56268c6c0d588d5a9267.tar.gz
glibc-766b73768b290b303f5b56268c6c0d588d5a9267.tar.xz
glibc-766b73768b290b303f5b56268c6c0d588d5a9267.zip
Linux: Do not skip d_ino == 0 entries in readdir, readdir64 (bug 12165)
POSIX does not say this value is special.  For example, old XFS file
systems may still use inode number zero.

Also update the comment regarding ENOENT.  Linux may return ENOENT
for some file systems.
Diffstat (limited to 'sysdeps/unix/sysv/linux/readdir.c')
-rw-r--r--sysdeps/unix/sysv/linux/readdir.c57
1 files changed, 21 insertions, 36 deletions
diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index c31f349639..c9a04dc160 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -28,48 +28,33 @@ __readdir_unlocked (DIR *dirp)
   struct dirent *dp;
   int saved_errno = errno;
 
-  do
+  if (dirp->offset >= dirp->size)
     {
-      size_t reclen;
+      /* We've emptied out our buffer.  Refill it.  */
 
-      if (dirp->offset >= dirp->size)
+      size_t maxread = dirp->allocation;
+      ssize_t bytes;
+
+      bytes = __getdents (dirp->fd, dirp->data, maxread);
+      if (bytes <= 0)
 	{
-	  /* We've emptied out our buffer.  Refill it.  */
-
-	  size_t maxread = dirp->allocation;
-	  ssize_t bytes;
-
-	  bytes = __getdents (dirp->fd, dirp->data, maxread);
-	  if (bytes <= 0)
-	    {
-	      /* On some systems getdents fails with ENOENT when the
-		 open directory has been rmdir'd already.  POSIX.1
-		 requires that we treat this condition like normal EOF.  */
-	      if (bytes < 0 && errno == ENOENT)
-		bytes = 0;
-
-	      /* Don't modifiy errno when reaching EOF.  */
-	      if (bytes == 0)
-		__set_errno (saved_errno);
-	      dp = NULL;
-	      break;
-	    }
-	  dirp->size = (size_t) bytes;
-
-	  /* Reset the offset into the buffer.  */
-	  dirp->offset = 0;
+	  /* Linux may fail with ENOENT on some file systems if the
+	     directory inode is marked as dead (deleted).  POSIX
+	     treats this as a regular end-of-directory condition, so
+	     do not set errno in that case, to indicate success.  */
+	  if (bytes == 0 || errno == ENOENT)
+	    __set_errno (saved_errno);
+	  return NULL;
 	}
+      dirp->size = (size_t) bytes;
 
-      dp = (struct dirent *) &dirp->data[dirp->offset];
-
-      reclen = dp->d_reclen;
-
-      dirp->offset += reclen;
-
-      dirp->filepos = dp->d_off;
+      /* Reset the offset into the buffer.  */
+      dirp->offset = 0;
+    }
 
-      /* Skip deleted files.  */
-    } while (dp->d_ino == 0);
+  dp = (struct dirent *) &dirp->data[dirp->offset];
+  dirp->offset += dp->d_reclen;
+  dirp->filepos = dp->d_off;
 
   return dp;
 }