public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/posix_spawn-optimizations] Linux: Add internal direntries interface
@ 2019-07-30 21:30 Adhemerval Zanella
  0 siblings, 0 replies; only message in thread
From: Adhemerval Zanella @ 2019-07-30 21:30 UTC (permalink / raw)
  To: glibc-cvs

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

commit 0841f12653e9af21543805a4c3944f5e17da219a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Jul 15 17:40:27 2019 -0300

    Linux: Add internal direntries interface
    
    Add new internal interfaces direntries_init, direntries_read, and
    direntries_next meant to access directories entries similar to dirent
    functions.  Using these interfaces together with getdents64 has the
    following benefits:
    
      * The combination is async-signal-safe, since the buffer is allocated
        externally to the call.
    
      * There is no buffer bloat due to auto-sizing the DIR * buffer based on
        preferred I/O sizes, as advertised by the file system.
    
      * The combination does not suffer from the telldir problem (which
        returns long on 32-bit architectures, which is incompatible with
        struct dirent64 in the kernel).  (Only seeking to the beginning is
        supported.)
    
      * Exposing the block read size means that the application knows when it
        has to rewind after deleting enumerated files, to avoid skipping
        entries or returning entries twice.
    
    	* sysdeps/unix/sysv/linux/Makefile (sysdep_routines): Add direntries.
    	* sysdeps/unix/sysv/linux/direntries.c: New file.
    	* sysdeps/unix/sysv/linux/direntries.h. Likewise.

Diff:
---
 sysdeps/unix/sysv/linux/Makefile     |  3 +-
 sysdeps/unix/sysv/linux/direntries.c | 71 ++++++++++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/direntries.h | 74 ++++++++++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+), 1 deletion(-)

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 1ab6bcb..641c0c8 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -197,7 +197,8 @@ endif
 inhibit-glue = yes
 
 ifeq ($(subdir),dirent)
-sysdep_routines += getdirentries getdirentries64
+sysdep_routines += getdirentries getdirentries64 \
+		   direntries
 tests += tst-getdents64
 tests-internal += tst-readdir64-compat
 endif
diff --git a/sysdeps/unix/sysv/linux/direntries.c b/sysdeps/unix/sysv/linux/direntries.c
new file mode 100644
index 0000000..bf83a98
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/direntries.c
@@ -0,0 +1,71 @@
+/* Parsing directory streams.  Linux version.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <dirent.h>
+
+#include <direntries.h>
+
+void
+__direntries_init (struct direntries *iterator, void *buffer, size_t length)
+{
+  /* Cheap security check if the caller accidentally passed an error
+     result from getdirentries to this function.  */
+  if ((ssize_t) length < 0)
+    __fortify_fail ("invalid direntries_init call");
+
+  iterator->direntries_buffer_begin = buffer;
+  iterator->direntries_buffer_end = (char *) buffer + length;
+}
+
+ssize_t
+__direntries_read (int fd, struct direntries *iterator,
+                   void *buffer, size_t length)
+{
+  ssize_t ret = __getdents64 (fd, buffer, length);
+  if (ret < 0)
+    return ret;
+  __direntries_init (iterator, buffer, ret);
+  return ret;
+}
+
+int
+__direntries_next (struct direntries *iterator, struct direntry *result)
+{
+  if (iterator->direntries_buffer_begin == iterator->direntries_buffer_end)
+    {
+      __set_errno (ENOENT);
+      return -1;
+    }
+
+  char *begin = iterator->direntries_buffer_begin;
+
+  /* The caller may have supplied an unaligned buffer.  Make an
+     aligned copy of the entry, excluding its name.  */
+  struct dirent64 entry;
+  memcpy (&entry, begin, offsetof (struct dirent64, d_name));
+
+  /* The name is not copied.  It points into the existing buffer.  */
+  result->d_name = begin + offsetof (struct dirent64, d_name);
+  result->d_ino = entry.d_ino;
+  result->d_type = entry.d_type;
+  result->d_flags = 0;
+
+  iterator->direntries_buffer_begin = begin + entry.d_reclen;
+  return 0;
+}
diff --git a/sysdeps/unix/sysv/linux/direntries.h b/sysdeps/unix/sysv/linux/direntries.h
new file mode 100644
index 0000000..4d4ca32
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/direntries.h
@@ -0,0 +1,74 @@
+/* Parsing directory streams.  Linux version.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_DIRENTRIES_H
+#define _SYS_DIRENTRIES_H
+
+#include <features.h>
+#include <sys/types.h>
+
+/* One directory entry.  See struct dirent in <dirent.h>.  The main
+   difference is that d_name is a pointer here, and not an array, as
+   in struct dirent.  */
+struct direntry
+{
+  /* Name of the directory entry.  The string is part of the buffer
+     passed to direntries_init or direntries_read below.  */
+  const char *d_name;
+
+  /* Inode number of this directory entry.  */
+  __ino64_t d_ino;
+
+  /* Type of the entry.  Can be DT_UNKNOWN if unknown.  */
+  unsigned char d_type;
+
+  /* Currently always zero.  */
+  unsigned char d_flags;
+};
+
+/* Initialized by direntries_init or direntires_read below and used by
+   direntry_next.  */
+struct direntries
+{
+  void *direntries_buffer_begin;
+  void *direntries_buffer_end;
+};
+
+/* Initialize *ITERATOR based on BUFFER and LENGTH.  BUFFER must have
+   been filled by getdents, and length must be a non-negative return
+   value from this function.  Afterwards, individual entries can be
+   retrieved using direntries_next.  */
+void __direntries_init (struct direntries *__iterator, void *__buffer,
+		        size_t __length)
+  attribute_hidden __nonnull ((1, 2));
+
+/* Read directory entries from FD and write them to LENGTH bytes at
+   BUFFER and initialize *ITERATOR so that individual entries can be
+   retrieved using direntries_next.  Returns -1 on error, 0 on end of
+   stream, and a positive value in case of more data.  */
+__ssize_t __direntries_read (int __fd, struct direntries *__iterator,
+			     void *__buffer, size_t __length) attribute_hidden
+  attribute_hidden __nonnull ((2, 3)) __wur;
+
+/* If there is a next entry, copy the next directory entry in ITERATOR
+   to *ENTRY, update *ITERATOR, and returns 0.  If there is no next
+   entry, return -1 and set errno to ENOENT.  */
+int __direntries_next (struct direntries *__iterator, struct direntry *__entry)
+  attribute_hidden __nonnull ((1, 2)) __wur;
+
+#endif /* _SYS_DIRENTRIES_H */


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-07-30 21:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-30 21:30 [glibc/azanella/posix_spawn-optimizations] Linux: Add internal direntries interface 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).