Updated to fedora-glibc-20070221T1011
[glibc.git] / sysdeps / unix / opendir.c
blob59772cda753cf68f4ce910f5978a71a9c01c2fe7
1 /* Copyright (C) 1991-1996,98,2000-2003,2005,2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include <dirstream.h>
33 #include <not-cancel.h>
36 /* opendir() must not accidentally open something other than a directory.
37 Some OS's have kernel support for that, some don't. In the worst
38 case we have to stat() before the open() AND fstat() after.
40 We have to test at runtime for kernel support since libc may have
41 been compiled with different headers to the kernel it's running on.
42 This test can't be done reliably in the general case. We'll use
43 /dev/null, which if it's not a device lots of stuff will break, as
44 a guinea pig. It may be missing in chroot environments, so we
45 make sure to fail safe. */
46 #ifdef O_DIRECTORY
47 # ifdef O_DIRECTORY_WORKS
48 # define o_directory_works 1
49 # define tryopen_o_directory() while (1) /* This must not be called. */
50 # else
51 static int o_directory_works;
53 static void
54 tryopen_o_directory (void)
56 int serrno = errno;
57 int x = open_not_cancel_2 ("/dev/null", O_RDONLY|O_NDELAY|O_DIRECTORY);
59 if (x >= 0)
61 close_not_cancel_no_status (x);
62 o_directory_works = -1;
64 else if (errno != ENOTDIR)
65 o_directory_works = -1;
66 else
67 o_directory_works = 1;
69 __set_errno (serrno);
71 # endif
72 # define EXTRA_FLAGS O_DIRECTORY
73 #else
74 # define EXTRA_FLAGS 0
75 #endif
78 /* Open a directory stream on NAME. */
79 DIR *
80 __opendir (const char *name)
82 struct stat64 statbuf;
84 if (__builtin_expect (name[0], '\1') == '\0')
86 /* POSIX.1-1990 says an empty name gets ENOENT;
87 but `open' might like it fine. */
88 __set_errno (ENOENT);
89 return NULL;
92 #ifdef O_DIRECTORY
93 /* Test whether O_DIRECTORY works. */
94 if (o_directory_works == 0)
95 tryopen_o_directory ();
97 /* We can skip the expensive `stat' call if O_DIRECTORY works. */
98 if (o_directory_works < 0)
99 #endif
101 /* We first have to check whether the name is for a directory. We
102 cannot do this after the open() call since the open/close operation
103 performed on, say, a tape device might have undesirable effects. */
104 if (__builtin_expect (__xstat64 (_STAT_VER, name, &statbuf), 0) < 0)
105 return NULL;
106 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
108 __set_errno (ENOTDIR);
109 return NULL;
113 int fd = open_not_cancel_2 (name, O_RDONLY|O_NDELAY|EXTRA_FLAGS|O_LARGEFILE);
114 if (__builtin_expect (fd, 0) < 0)
115 return NULL;
117 /* Now make sure this really is a directory and nothing changed since
118 the `stat' call. We do not have to perform the test for the
119 descriptor being associated with a directory if we know the
120 O_DIRECTORY flag is honored by the kernel. */
121 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
122 goto lose;
123 #ifdef O_DIRECTORY
124 if (o_directory_works <= 0)
125 #endif
127 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
129 __set_errno (ENOTDIR);
130 lose:
131 close_not_cancel_no_status (fd);
132 return NULL;
136 return __alloc_dir (fd, true, &statbuf);
138 weak_alias (__opendir, opendir)
141 DIR *
142 internal_function
143 __alloc_dir (int fd, bool close_fd, const struct stat64 *statp)
145 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
146 goto lose;
148 size_t allocation;
149 #ifdef _STATBUF_ST_BLKSIZE
150 if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
152 allocation = statp->st_blksize;
153 else
154 #endif
155 allocation = (BUFSIZ < sizeof (struct dirent64)
156 ? sizeof (struct dirent64) : BUFSIZ);
158 const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
160 DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
161 if (dirp == NULL)
162 lose:
164 if (close_fd)
166 int save_errno = errno;
167 close_not_cancel_no_status (fd);
168 __set_errno (save_errno);
170 return NULL;
172 memset (dirp, '\0', sizeof (DIR));
173 dirp->data = (char *) (dirp + 1) + pad;
174 dirp->allocation = allocation;
175 dirp->fd = fd;
177 #ifndef NOT_IN_libc
178 __libc_lock_init (dirp->lock);
179 #endif
181 return dirp;