summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2005-09-28 21:19:53 +0000
committerUlrich Drepper <drepper@redhat.com>2005-09-28 21:19:53 +0000
commit1812d50bc9ad034c8406f5e3de83b5a6c668f3a9 (patch)
tree54c88f0ffa5e7ccdece3941a630f5c19f59d3f70
parenta898514259e9fef06feddbe74df4d11fb94baf66 (diff)
downloadglibc-1812d50bc9ad034c8406f5e3de83b5a6c668f3a9.tar.gz
glibc-1812d50bc9ad034c8406f5e3de83b5a6c668f3a9.tar.xz
glibc-1812d50bc9ad034c8406f5e3de83b5a6c668f3a9.zip
* dirent/dirent.h: Declare fdopendir.
	* dirent/Versions: Export fdopendir for GLIBC_2.4.
	* dirent/Makefile (routines): Add fdopendir.
	(tests): Add tst-fdopendir.
	* dirent/tst-fdopendir.c: New file.
	* include/dirent.h: Declare __alloc_dir.
	* sysdeps/generic/fdopendir.c: New file.
	* sysdeps/unix/fdopendir.c: New file.
	* sysdeps/unix/opendir.c: Split off back part of opendir into new
	function __alloc_dir.
-rw-r--r--ChangeLog13
-rw-r--r--dirent/Versions3
-rw-r--r--dirent/dirent.h10
-rw-r--r--dirent/tst-fdopendir.c124
-rw-r--r--include/dirent.h2
-rw-r--r--sysdeps/generic/fdopendir.c33
-rw-r--r--sysdeps/unix/fdopendir.c38
-rw-r--r--sysdeps/unix/opendir.c33
8 files changed, 242 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index f18e750138..38fd5c7f07 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2005-09-28  Ulrich Drepper  <drepper@redhat.com>
+
+	* dirent/dirent.h: Declare fdopendir.
+	* dirent/Versions: Export fdopendir for GLIBC_2.4.
+	* dirent/Makefile (routines): Add fdopendir.
+	(tests): Add tst-fdopendir.
+	* dirent/tst-fdopendir.c: New file.
+	* include/dirent.h: Declare __alloc_dir.
+	* sysdeps/generic/fdopendir.c: New file.
+	* sysdeps/unix/fdopendir.c: New file.
+	* sysdeps/unix/opendir.c: Split off back part of opendir into new
+	function __alloc_dir.
+
 2005-09-26  Steven Munroe  <sjmunroe@us.ibm.com>
 
 	[BZ #1384]
diff --git a/dirent/Versions b/dirent/Versions
index 9d96caeb36..41c1584426 100644
--- a/dirent/Versions
+++ b/dirent/Versions
@@ -41,4 +41,7 @@ libc {
     # g*
     getdirentries64;
   }
+  GLIBC_2.4 {
+    fdopendir;
+  }
 }
diff --git a/dirent/dirent.h b/dirent/dirent.h
index a5e8a004e2..ed4147dbca 100644
--- a/dirent/dirent.h
+++ b/dirent/dirent.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2000, 2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2000, 2003, 2004, 2005 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
@@ -134,6 +134,14 @@ typedef struct __dirstream DIR;
    marked with __THROW.  */
 extern DIR *opendir (__const char *__name) __nonnull ((1));
 
+#ifdef __USE_GNU
+/* Same as opendir, but open the stream on the file descriptor FD.
+
+   This function is a possible cancellation point and therefore not
+   marked with __THROW.  */
+extern DIR *fdopendir (int __fd);
+#endif
+
 /* Close the directory stream DIRP.
    Return 0 if successful, -1 if not.
 
diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c
new file mode 100644
index 0000000000..3cf315d85c
--- /dev/null
+++ b/dirent/tst-fdopendir.c
@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdbool.h>
+#include <string.h>
+
+
+static int
+do_test (void)
+{
+  char fname[] = "/tmp/jXXXXXX";
+  int fd = mkstemp (fname);
+  if (fd == -1)
+    {
+      puts ("mkstemp failed");
+      return 1;
+    }
+
+  write (fd, "hello", 5);
+  close (fd);
+
+  struct stat64 st;
+  if (stat64 (fname, &st) == -1)
+    {
+      puts ("first stat failed");
+      return 0;
+    }
+
+  /* Make sure there is enough time between the creation and the access.  */
+  sleep (2);
+
+  fd = open (fname, O_RDONLY | O_NOATIME);
+  if (fd == -1)
+    {
+      puts ("first open failed");
+      return 1;
+    }
+
+  char buf[5];
+  read(fd, buf, sizeof (buf));
+  close(fd);
+
+  struct stat64 st2;
+  if (stat64 (fname, &st2) == -1)
+    {
+      puts ("second stat failed");
+      return 0;
+    }
+
+  bool no_noatime = false;
+#ifdef _STATBUF_ST_NSEC
+  if (st.st_atim.tv_sec != st2.st_atim.tv_sec
+      || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
+#else
+  if (st.st_atime != st2.st_atime)
+#endif
+    {
+      puts ("file atime changed");
+      no_noatime = true;
+    }
+
+  unlink(fname);
+
+  strcpy(fname, "/tmp/dXXXXXX");
+  char *d = mkdtemp (fname);
+  if (d == NULL)
+    {
+      puts ("mkdtemp failed");
+      return 1;
+    }
+
+  if (stat64 (d, &st) == -1)
+    {
+      puts ("third stat failed");
+      return 0;
+    }
+  sleep (2);
+
+  fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
+  if (fd == -1)
+    {
+      puts ("second open failed");
+      return 1;
+    }
+  DIR *dir = fdopendir (fd);
+  if (dir == NULL)
+    {
+      puts ("fdopendir failed");
+      return 1;
+    }
+
+  struct dirent *de;
+  while ((de = readdir (dir)) != NULL)
+    ;
+
+  closedir (dir);
+
+  if (stat64 (d, &st2) == -1)
+    {
+      puts ("fourth stat failed");
+      return 0;
+    }
+#ifdef _STATBUF_ST_NSEC
+  if (!no_noatime
+      && (st.st_atim.tv_sec != st2.st_atim.tv_sec
+	 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
+#else
+  if (!no_noatime && st.st_atime != st2.st_atime)
+#endif
+    {
+      puts ("directory atime changed");
+      return 1;
+    }
+
+  rmdir(fname);
+
+  return 0;
+}
+
+#define TIMEOUT 6
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/include/dirent.h b/include/dirent.h
index f09a88f21c..b040a042d3 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -1,6 +1,7 @@
 #ifndef _DIRENT_H
 # include <dirstream.h>
 # include <dirent/dirent.h>
+# include <sys/stat.h>
 
 /* Now define the internal interfaces.  */
 extern DIR *__opendir (__const char *__name);
@@ -23,4 +24,5 @@ extern int __alphasort64 (const void *a, const void *b)
      __attribute_pure__;
 extern int __versionsort64 (const void *a, const void *b)
      __attribute_pure__;
+extern DIR *__alloc_dir (int fd, struct stat64 *statp) internal_function;
 #endif
diff --git a/sysdeps/generic/fdopendir.c b/sysdeps/generic/fdopendir.c
new file mode 100644
index 0000000000..597ccd5a5e
--- /dev/null
+++ b/sysdeps/generic/fdopendir.c
@@ -0,0 +1,33 @@
+/* Copyright (C) 2005 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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <stddef.h>
+#include <dirent.h>
+
+
+/* Open a directory stream on FD.  */
+DIR *
+fdopendir (int fd)
+{
+  __set_errno (ENOSYS);
+  return NULL;
+}
+
+stub_warning (fdopendir)
+#include <stub-tag.h>
diff --git a/sysdeps/unix/fdopendir.c b/sysdeps/unix/fdopendir.c
new file mode 100644
index 0000000000..fa55a24cb6
--- /dev/null
+++ b/sysdeps/unix/fdopendir.c
@@ -0,0 +1,38 @@
+/* Copyright (C) 2005 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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <dirent.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+
+DIR *
+fdopendir (int fd)
+{
+  struct stat64 statbuf;
+
+  if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
+    return NULL;
+  if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
+    {
+      __set_errno (ENOTDIR);
+      return NULL;
+    }
+
+  return __alloc_dir (fd, &statbuf);
+}
diff --git a/sysdeps/unix/opendir.c b/sysdeps/unix/opendir.c
index 5e03ed9b20..366670b79c 100644
--- a/sysdeps/unix/opendir.c
+++ b/sysdeps/unix/opendir.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-1996,98,2000-2002, 2003 Free Software Foundation, Inc.
+/* Copyright (C) 1991-1996,98,2000-2003,2005 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
@@ -78,11 +78,7 @@ tryopen_o_directory (void)
 DIR *
 __opendir (const char *name)
 {
-  DIR *dirp;
   struct stat64 statbuf;
-  int fd;
-  size_t allocation;
-  int save_errno;
 
   if (__builtin_expect (name[0], '\1') == '\0')
     {
@@ -113,7 +109,7 @@ __opendir (const char *name)
 	 }
     }
 
-  fd = open_not_cancel_2 (name, O_RDONLY|O_NDELAY|EXTRA_FLAGS|O_LARGEFILE);
+  int fd = open_not_cancel_2 (name, O_RDONLY|O_NDELAY|EXTRA_FLAGS|O_LARGEFILE);
   if (__builtin_expect (fd, 0) < 0)
     return NULL;
 
@@ -129,18 +125,30 @@ __opendir (const char *name)
     {
       if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
 	{
-	  save_errno = ENOTDIR;
-	  goto lose;
+	  __set_errno (ENOTDIR);
+	lose:
+	  close_not_cancel_no_status (fd);
+	  return NULL;
 	}
     }
 
+  return __alloc_dir (fd, &statbuf);
+}
+weak_alias (__opendir, opendir)
+
+
+DIR *
+internal_function
+__alloc_dir (int fd, struct stat64 *statp)
+{
   if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
     goto lose;
 
+  size_t allocation;
 #ifdef _STATBUF_ST_BLKSIZE
-  if (__builtin_expect ((size_t) statbuf.st_blksize >= sizeof (struct dirent64),
+  if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
 			1))
-    allocation = statbuf.st_blksize;
+    allocation = statp->st_blksize;
   else
 #endif
     allocation = (BUFSIZ < sizeof (struct dirent64)
@@ -148,11 +156,11 @@ __opendir (const char *name)
 
   const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
 
-  dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
+  DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
   if (dirp == NULL)
   lose:
     {
-      save_errno = errno;
+      int save_errno = errno;
       close_not_cancel_no_status (fd);
       __set_errno (save_errno);
       return NULL;
@@ -166,4 +174,3 @@ __opendir (const char *name)
 
   return dirp;
 }
-weak_alias (__opendir, opendir)