public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/bz23960-dirent] linux: Use getdents64 on non-LFS readdir
@ 2022-11-02 13:06 Adhemerval Zanella
  0 siblings, 0 replies; 4+ messages in thread
From: Adhemerval Zanella @ 2022-11-02 13:06 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=782b1915b4c2ea1a4b4fba329123690308ffe81a

commit 782b1915b4c2ea1a4b4fba329123690308ffe81a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Tue Oct 20 13:37:15 2020 -0300

    linux: Use getdents64 on non-LFS readdir
    
    The opendir allocates a translation buffer to be used to return the
    non-LFS readdir entry.  The obtained dirent64 struct is translated
    to the temporary buffer on each readdir call.
    
    Entries that overflow d_off/d_ino and the buffer reallocation failure
    (in case of large d_name) are ignored.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 sysdeps/unix/sysv/linux/closedir.c  |  4 ++
 sysdeps/unix/sysv/linux/dirstream.h |  5 ++
 sysdeps/unix/sysv/linux/opendir.c   | 21 +++++++++
 sysdeps/unix/sysv/linux/readdir.c   | 94 +++++++++++++++++++++++++++----------
 4 files changed, 98 insertions(+), 26 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/closedir.c b/sysdeps/unix/sysv/linux/closedir.c
index eee0193fc4..d876d49d78 100644
--- a/sysdeps/unix/sysv/linux/closedir.c
+++ b/sysdeps/unix/sysv/linux/closedir.c
@@ -47,6 +47,10 @@ __closedir (DIR *dirp)
   __libc_lock_fini (dirp->lock);
 #endif
 
+#if !_DIRENT_MATCHES_DIRENT64
+  free (dirp->tbuffer);
+#endif
+
   free ((void *) dirp);
 
   return __close_nocancel (fd);
diff --git a/sysdeps/unix/sysv/linux/dirstream.h b/sysdeps/unix/sysv/linux/dirstream.h
index a0d8acf08d..064273cc31 100644
--- a/sysdeps/unix/sysv/linux/dirstream.h
+++ b/sysdeps/unix/sysv/linux/dirstream.h
@@ -41,6 +41,11 @@ struct __dirstream
 
     int errcode;		/* Delayed error code.  */
 
+#if !defined __OFF_T_MATCHES_OFF64_T || !defined __INO_T_MATCHES_INO64_T
+    char *tbuffer;		/* Translation buffer for non-LFS calls.  */
+    size_t tbuffer_size;	/* Size of translation buffer.  */
+#endif
+
     /* Directory block.  We must make sure that this block starts
        at an address that is aligned adequately enough to store
        dirent entries.  Using the alignment of "void *" is not
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index 9e81d00630..bfd2f382a6 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -120,6 +120,27 @@ __alloc_dir (int fd, bool close_fd, int flags,
       return NULL;
     }
 
+#if !_DIRENT_MATCHES_DIRENT64
+  /* Allocates a translation buffer to use as the returned 'struct direct'
+     for non-LFS 'readdir' calls.
+
+     The initial NAME_MAX size should handle most cases, while readdir might
+     expand the buffer if required.  */
+  enum
+    {
+      tbuffer_size = sizeof (struct dirent) + NAME_MAX + 1
+    };
+  dirp->tbuffer = malloc (tbuffer_size);
+  if (dirp->tbuffer == NULL)
+    {
+      free (dirp);
+      if (close_fd)
+	__close_nocancel_nostatus (fd);
+      return NULL;
+    }
+  dirp->tbuffer_size = tbuffer_size;
+#endif
+
   dirp->fd = fd;
 #if IS_IN (libc)
   __libc_lock_init (dirp->lock);
diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index c9a04dc160..c078146d7d 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -21,42 +21,84 @@
 #if !_DIRENT_MATCHES_DIRENT64
 #include <dirstream.h>
 
+/* Translate the DP64 entry to the non-LFS one in the translation buffer
+   at dirstream DS.  Return true is the translation was possible or
+   false if either an internal fields can be represented in the non-LFS
+   entry or if the translation can not be resized.  */
+static bool
+dirstream_entry (struct __dirstream *ds, const struct dirent64 *dp64)
+{
+  off_t d_off = dp64->d_off;
+  if (d_off != dp64->d_off)
+    return false;
+  ino_t d_ino = dp64->d_ino;
+  if (d_ino != dp64->d_ino)
+    return false;
+
+  /* Expand the translation buffer to hold the new name size.  */
+  size_t new_reclen = sizeof (struct dirent)
+		    + dp64->d_reclen - offsetof (struct dirent64, d_name);
+  if (new_reclen > ds->tbuffer_size)
+    {
+      char *newbuffer = realloc (ds->tbuffer, new_reclen);
+      if (newbuffer == NULL)
+	return false;
+      ds->tbuffer = newbuffer;
+      ds->tbuffer_size = new_reclen;
+    }
+
+  struct dirent *dp = (struct dirent *) ds->tbuffer;
+
+  dp->d_off = d_off;
+  dp->d_ino = d_ino;
+  dp->d_reclen = new_reclen;
+  dp->d_type = dp64->d_type;
+  memcpy (dp->d_name, dp64->d_name,
+	  dp64->d_reclen - offsetof (struct dirent64, d_name));
+
+  return true;
+}
+
 /* Read a directory entry from DIRP.  */
 struct dirent *
 __readdir_unlocked (DIR *dirp)
 {
-  struct dirent *dp;
   int saved_errno = errno;
 
-  if (dirp->offset >= dirp->size)
+  while (1)
     {
-      /* 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)
+      if (dirp->offset >= dirp->size)
+	{
+	  /* We've emptied out our buffer.  Refill it.  */
+	  ssize_t bytes = __getdents64 (dirp->fd, dirp->data,
+					dirp->allocation);
+	  if (bytes <= 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 = bytes;
+
+ 	  /* Reset the offset into the buffer.  */
+	  dirp->offset = 0;
+ 	}
+
+      struct dirent64 *dp64 = (struct dirent64 *) &dirp->data[dirp->offset];
+      dirp->offset += dp64->d_reclen;
+
+      /* Skip entries which might overflow d_off/d_ino or if the translation
+	 buffer can't be resized.  */
+      if (dirstream_entry (dirp, dp64))
 	{
-	  /* 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->filepos = dp64->d_off;
+	  return (struct dirent *) dirp->tbuffer;
 	}
-      dirp->size = (size_t) bytes;
-
-      /* Reset the offset into the buffer.  */
-      dirp->offset = 0;
     }
-
-  dp = (struct dirent *) &dirp->data[dirp->offset];
-  dirp->offset += dp->d_reclen;
-  dirp->filepos = dp->d_off;
-
-  return dp;
 }
 
 struct dirent *

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [glibc/azanella/bz23960-dirent] linux: Use getdents64 on non-LFS readdir
@ 2023-12-29 19:59 Adhemerval Zanella
  0 siblings, 0 replies; 4+ messages in thread
From: Adhemerval Zanella @ 2023-12-29 19:59 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=396cc2c088afbf3846c54f6195fa98fa06d23e21

commit 396cc2c088afbf3846c54f6195fa98fa06d23e21
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Tue Oct 20 13:37:15 2020 -0300

    linux: Use getdents64 on non-LFS readdir
    
    It is similar to what non-LFS getdents do (including overflow check).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 sysdeps/unix/sysv/linux/readdir.c | 97 +++++++++++++++++++++++++++++----------
 1 file changed, 73 insertions(+), 24 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index 4a4c00ea07..72ba895afe 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -20,43 +20,92 @@
 
 #if !_DIRENT_MATCHES_DIRENT64
 #include <dirstream.h>
+#include <unistd.h>
+
+# ifndef DIRENT_SET_DP_INO
+#  define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
+# endif
 
 /* Read a directory entry from DIRP.  */
 struct dirent *
 __readdir_unlocked (DIR *dirp)
 {
-  struct dirent *dp;
   int saved_errno = errno;
 
-  if (dirp->offset >= dirp->size)
+  while (1)
     {
-      /* 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)
+      if (dirp->offset >= dirp->size)
 	{
-	  /* 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);
+	  ssize_t bytes = __getdents64 (dirp->fd, dirp->data,
+					dirp->allocation);
+	  if (bytes <= 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 = bytes;
+
+ 	  /* Reset the offset into the buffer.  */
+	  dirp->offset = 0;
+ 	}
+
+    /* These two pointers might alias the same memory buffer.  Standard C
+       requires that we always use the same type for them, so we must use the
+       union type.  */
+      union
+      {
+	struct dirent64 dp64;
+	struct dirent dp;
+	char *b;
+      } *inp, *outp;
+      inp = (void*) &dirp->data[dirp->offset];
+      outp = (void*) &dirp->data[dirp->offset];
+
+      const size_t size_diff = offsetof (struct dirent64, d_name)
+	- offsetof (struct dirent, d_name);
+
+      /* Since inp->dp64.d_reclen is already aligned for the kernel structure
+	 this may compute a value that is bigger than necessary.  */
+      size_t old_reclen = inp->dp64.d_reclen;
+      size_t new_reclen = ALIGN_UP (old_reclen - size_diff,
+				    _Alignof (struct dirent));
+
+      if (!in_ino_t_range (inp->dp64.d_ino)
+	  || !in_off_t_range (inp->dp64.d_off))
+	{
+	  /* Overflow.  If there was at least one entry before this one,
+	     return them without error, otherwise signal overflow.  */
+	  if (dirp->offset != 0)
+	    {
+	      __lseek64 (dirp->fd, dirp->offset, SEEK_SET);
+	      outp = (void*)(outp->b - dirp->data);
+	      return &outp->dp;
+	    }
+	  __set_errno (EOVERFLOW);
 	  return NULL;
 	}
-      dirp->size = (size_t) bytes;
 
-      /* Reset the offset into the buffer.  */
-      dirp->offset = 0;
+      /* Copy the data from INP and access only OUTP.  */
+      const uint64_t d_ino = inp->dp64.d_ino;
+      const int64_t d_off = inp->dp64.d_off;
+      const uint8_t d_type = inp->dp64.d_type;
+      outp->dp.d_ino = d_ino;
+      outp->dp.d_off = d_off;
+      outp->dp.d_reclen = new_reclen;
+      outp->dp.d_type = d_type;
+      memmove (outp->dp.d_name, inp->dp64.d_name,
+	       old_reclen - offsetof (struct dirent64, d_name));
+
+      dirp->filepos = d_off;
+      dirp->offset += old_reclen;
+
+      return &outp->dp;
     }
-
-  dp = (struct dirent *) &dirp->data[dirp->offset];
-  dirp->offset += dp->d_reclen;
-  dirp->filepos = dp->d_off;
-
-  return dp;
 }
 
 struct dirent *

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [glibc/azanella/bz23960-dirent] linux: Use getdents64 on non-LFS readdir
@ 2023-01-27 20:39 Adhemerval Zanella
  0 siblings, 0 replies; 4+ messages in thread
From: Adhemerval Zanella @ 2023-01-27 20:39 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=b18673fbcfa7b467fec409dbf2276fb7bb8bdcdf

commit b18673fbcfa7b467fec409dbf2276fb7bb8bdcdf
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Tue Oct 20 13:37:15 2020 -0300

    linux: Use getdents64 on non-LFS readdir
    
    The non-LFS opendir reserves a translation entry to be used to return
    the entry and the dirent64 struct is translated to the temporary buffer
    on each readdir call.
    
    Entries that overflow d_off/d_ino and the buffer reallocation failure
    (in case of large d_name) are ignored.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 dirent/tst-scandir.c                |  6 ++-
 include/dirent.h                    |  2 +-
 sysdeps/unix/sysv/linux/dirstream.h |  5 +++
 sysdeps/unix/sysv/linux/readdir.c   | 83 +++++++++++++++++++++++++------------
 4 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/dirent/tst-scandir.c b/dirent/tst-scandir.c
index 8d87d4dd74..7bc666449e 100644
--- a/dirent/tst-scandir.c
+++ b/dirent/tst-scandir.c
@@ -155,8 +155,12 @@ do_test (void)
     }
   if (n != 6)
     {
+      /* Non-lfs opendir skips entries that can not be represented (for
+	 instance if d_off is not an offset but rather an internal filesystem
+	 representation.  For this case there is no point in continue the
+	 testcase.  */
       printf ("scandir returned %d entries instead of 6\n", n);
-      return 1;
+      return EXIT_UNSUPPORTED;
     }
 
   struct
diff --git a/include/dirent.h b/include/dirent.h
index d7567f5e86..17827176ba 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -1,8 +1,8 @@
 #ifndef _DIRENT_H
+# include <dirent/dirent.h>
 # ifndef _ISOMAC
 #  include <dirstream.h>
 # endif
-# include <dirent/dirent.h>
 # ifndef _ISOMAC
 # include <sys/stat.h>
 # include <stdbool.h>
diff --git a/sysdeps/unix/sysv/linux/dirstream.h b/sysdeps/unix/sysv/linux/dirstream.h
index 3cb313b410..adcf8234f1 100644
--- a/sysdeps/unix/sysv/linux/dirstream.h
+++ b/sysdeps/unix/sysv/linux/dirstream.h
@@ -18,6 +18,7 @@
 #ifndef	_DIRSTREAM_H
 #define	_DIRSTREAM_H	1
 
+#include <dirent.h>
 #include <sys/types.h>
 
 #include <libc-lock.h>
@@ -41,6 +42,10 @@ struct __dirstream
 
     int errcode;		/* Delayed error code.  */
 
+#if !defined __OFF_T_MATCHES_OFF64_T || !defined __INO_T_MATCHES_INO64_T
+    struct dirent tdp;
+#endif
+
     /* Directory block.  We must make sure that this block starts
        at an address that is aligned adequately enough to store
        dirent entries.  Using the alignment of "void *" is not
diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index 4a4c00ea07..cd0ccaf33a 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -21,42 +21,71 @@
 #if !_DIRENT_MATCHES_DIRENT64
 #include <dirstream.h>
 
+/* Translate the DP64 entry to the non-LFS one in the translation entry
+   at dirstream DS.  Return true is the translation was possible or
+   false if either an internal field can not be represented in the non-LFS
+   entry or if the name is too long.  */
+static bool
+dirstream_entry (struct __dirstream *ds, const struct dirent64 *dp64)
+{
+  /* Check for overflow.  */
+  if (!in_off_t_range (dp64->d_off) || !in_ino_t_range (dp64->d_ino))
+    return false;
+
+  /* And if name is too large.  */
+  if (dp64->d_reclen - offsetof (struct dirent64, d_name) > NAME_MAX)
+    return false;
+
+  ds->filepos = dp64->d_off;
+
+  ds->tdp.d_off = dp64->d_off;
+  ds->tdp.d_ino = dp64->d_ino;
+  ds->tdp.d_reclen = sizeof (struct dirent)
+    + dp64->d_reclen - offsetof (struct dirent64, d_name);
+  ds->tdp.d_type = dp64->d_type;
+  memcpy (ds->tdp.d_name, dp64->d_name,
+	  dp64->d_reclen - offsetof (struct dirent64, d_name));
+
+  return true;
+}
+
 /* Read a directory entry from DIRP.  */
 struct dirent *
 __readdir_unlocked (DIR *dirp)
 {
-  struct dirent *dp;
   int saved_errno = errno;
 
-  if (dirp->offset >= dirp->size)
+  while (1)
     {
-      /* 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)
+      if (dirp->offset >= dirp->size)
 	{
-	  /* 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;
-
-      /* Reset the offset into the buffer.  */
-      dirp->offset = 0;
+	  /* We've emptied out our buffer.  Refill it.  */
+	  ssize_t bytes = __getdents64 (dirp->fd, dirp->data,
+					dirp->allocation);
+	  if (bytes <= 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 = bytes;
+
+ 	  /* Reset the offset into the buffer.  */
+	  dirp->offset = 0;
+ 	}
+
+      struct dirent64 *dp64 = (struct dirent64 *) &dirp->data[dirp->offset];
+      dirp->offset += dp64->d_reclen;
+
+      /* Skip entries which might overflow d_off/d_ino or if the translation
+	 buffer can not be resized.  */
+      if (dirstream_entry (dirp, dp64))
+	return &dirp->tdp;
     }
-
-  dp = (struct dirent *) &dirp->data[dirp->offset];
-  dirp->offset += dp->d_reclen;
-  dirp->filepos = dp->d_off;
-
-  return dp;
 }
 
 struct dirent *

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [glibc/azanella/bz23960-dirent] linux: Use getdents64 on non-LFS readdir
@ 2023-01-26 12:17 Adhemerval Zanella
  0 siblings, 0 replies; 4+ messages in thread
From: Adhemerval Zanella @ 2023-01-26 12:17 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=60341f8c192a61a5830eb30d52f1c995a01a3f6d

commit 60341f8c192a61a5830eb30d52f1c995a01a3f6d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Tue Oct 20 13:37:15 2020 -0300

    linux: Use getdents64 on non-LFS readdir
    
    The opendir allocates a translation buffer to be used to return the
    non-LFS readdir entry.  The obtained dirent64 struct is translated
    to the temporary buffer on each readdir call.
    
    Entries that overflow d_off/d_ino and the buffer reallocation failure
    (in case of large d_name) are ignored.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 sysdeps/unix/sysv/linux/closedir.c  |  4 ++
 sysdeps/unix/sysv/linux/dirstream.h |  5 ++
 sysdeps/unix/sysv/linux/opendir.c   | 21 +++++++++
 sysdeps/unix/sysv/linux/readdir.c   | 94 +++++++++++++++++++++++++++----------
 4 files changed, 98 insertions(+), 26 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/closedir.c b/sysdeps/unix/sysv/linux/closedir.c
index f1c2608642..8adbc99892 100644
--- a/sysdeps/unix/sysv/linux/closedir.c
+++ b/sysdeps/unix/sysv/linux/closedir.c
@@ -47,6 +47,10 @@ __closedir (DIR *dirp)
   __libc_lock_fini (dirp->lock);
 #endif
 
+#if !_DIRENT_MATCHES_DIRENT64
+  free (dirp->tbuffer);
+#endif
+
   free ((void *) dirp);
 
   return __close_nocancel (fd);
diff --git a/sysdeps/unix/sysv/linux/dirstream.h b/sysdeps/unix/sysv/linux/dirstream.h
index 3cb313b410..cd8bc56276 100644
--- a/sysdeps/unix/sysv/linux/dirstream.h
+++ b/sysdeps/unix/sysv/linux/dirstream.h
@@ -41,6 +41,11 @@ struct __dirstream
 
     int errcode;		/* Delayed error code.  */
 
+#if !defined __OFF_T_MATCHES_OFF64_T || !defined __INO_T_MATCHES_INO64_T
+    char *tbuffer;		/* Translation buffer for non-LFS calls.  */
+    size_t tbuffer_size;	/* Size of translation buffer.  */
+#endif
+
     /* Directory block.  We must make sure that this block starts
        at an address that is aligned adequately enough to store
        dirent entries.  Using the alignment of "void *" is not
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index 4336196a4d..52818829dc 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -120,6 +120,27 @@ __alloc_dir (int fd, bool close_fd, int flags,
       return NULL;
     }
 
+#if !_DIRENT_MATCHES_DIRENT64
+  /* Allocates a translation buffer to use as the returned 'struct direct'
+     for non-LFS 'readdir' calls.
+
+     The initial NAME_MAX size should handle most cases, while readdir might
+     expand the buffer if required.  */
+  enum
+    {
+      tbuffer_size = sizeof (struct dirent) + NAME_MAX + 1
+    };
+  dirp->tbuffer = malloc (tbuffer_size);
+  if (dirp->tbuffer == NULL)
+    {
+      free (dirp);
+      if (close_fd)
+	__close_nocancel_nostatus (fd);
+      return NULL;
+    }
+  dirp->tbuffer_size = tbuffer_size;
+#endif
+
   dirp->fd = fd;
 #if IS_IN (libc)
   __libc_lock_init (dirp->lock);
diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c
index 4a4c00ea07..cee88d1ed2 100644
--- a/sysdeps/unix/sysv/linux/readdir.c
+++ b/sysdeps/unix/sysv/linux/readdir.c
@@ -21,42 +21,84 @@
 #if !_DIRENT_MATCHES_DIRENT64
 #include <dirstream.h>
 
+/* Translate the DP64 entry to the non-LFS one in the translation buffer
+   at dirstream DS.  Return true is the translation was possible or
+   false if either an internal fields can be represented in the non-LFS
+   entry or if the translation can not be resized.  */
+static bool
+dirstream_entry (struct __dirstream *ds, const struct dirent64 *dp64)
+{
+  off_t d_off = dp64->d_off;
+  if (d_off != dp64->d_off)
+    return false;
+  ino_t d_ino = dp64->d_ino;
+  if (d_ino != dp64->d_ino)
+    return false;
+
+  /* Expand the translation buffer to hold the new name size.  */
+  size_t new_reclen = sizeof (struct dirent)
+		    + dp64->d_reclen - offsetof (struct dirent64, d_name);
+  if (new_reclen > ds->tbuffer_size)
+    {
+      char *newbuffer = realloc (ds->tbuffer, new_reclen);
+      if (newbuffer == NULL)
+	return false;
+      ds->tbuffer = newbuffer;
+      ds->tbuffer_size = new_reclen;
+    }
+
+  struct dirent *dp = (struct dirent *) ds->tbuffer;
+
+  dp->d_off = d_off;
+  dp->d_ino = d_ino;
+  dp->d_reclen = new_reclen;
+  dp->d_type = dp64->d_type;
+  memcpy (dp->d_name, dp64->d_name,
+	  dp64->d_reclen - offsetof (struct dirent64, d_name));
+
+  return true;
+}
+
 /* Read a directory entry from DIRP.  */
 struct dirent *
 __readdir_unlocked (DIR *dirp)
 {
-  struct dirent *dp;
   int saved_errno = errno;
 
-  if (dirp->offset >= dirp->size)
+  while (1)
     {
-      /* 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)
+      if (dirp->offset >= dirp->size)
+	{
+	  /* We've emptied out our buffer.  Refill it.  */
+	  ssize_t bytes = __getdents64 (dirp->fd, dirp->data,
+					dirp->allocation);
+	  if (bytes <= 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 = bytes;
+
+ 	  /* Reset the offset into the buffer.  */
+	  dirp->offset = 0;
+ 	}
+
+      struct dirent64 *dp64 = (struct dirent64 *) &dirp->data[dirp->offset];
+      dirp->offset += dp64->d_reclen;
+
+      /* Skip entries which might overflow d_off/d_ino or if the translation
+	 buffer can't be resized.  */
+      if (dirstream_entry (dirp, dp64))
 	{
-	  /* 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->filepos = dp64->d_off;
+	  return (struct dirent *) dirp->tbuffer;
 	}
-      dirp->size = (size_t) bytes;
-
-      /* Reset the offset into the buffer.  */
-      dirp->offset = 0;
     }
-
-  dp = (struct dirent *) &dirp->data[dirp->offset];
-  dirp->offset += dp->d_reclen;
-  dirp->filepos = dp->d_off;
-
-  return dp;
 }
 
 struct dirent *

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-12-29 19:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-02 13:06 [glibc/azanella/bz23960-dirent] linux: Use getdents64 on non-LFS readdir Adhemerval Zanella
2023-01-26 12:17 Adhemerval Zanella
2023-01-27 20:39 Adhemerval Zanella
2023-12-29 19:59 Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).