dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / _xftw.c
blob8932ae0848471390a7ca4073be6bddfad8395e12
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
32 * _xftw - file tree walk the uses expanded stat structure
34 * int _xftw(path, fn, depth) char *path; int (*fn)(); int depth;
36 * Given a path name, _xftw starts from the file given by that path
37 * name and visits each file and directory in the tree beneath
38 * that file. If a single file has multiple links within the
39 * structure, it will be visited once for each such link.
40 * For each object visited, fn is called with three arguments.
41 * (*fn) (pathname, statp, ftwflag)
42 * The first contains the path name of the object, the second
43 * contains a pointer to a stat buffer which will usually hold
44 * appropriate information for the object and the third will
45 * contain an integer value giving additional information about
47 * FTW_F The object is a file for which stat was
48 * successful. It does not guarantee that the
49 * file can actually be read.
51 * FTW_D The object is a directory for which stat and
52 * open for read were both successful.
54 * FTW_DNR The object is a directory for which stat
55 * succeeded, but which cannot be read. Because
56 * the directory cannot be read, fn will not be
57 * called for any descendants of this directory.
59 * FTW_NS Stat failed on the object because of lack of
60 * appropriate permission. This indication will
61 * be given for example for each file in a
62 * directory with read but no execute permission.
63 * Because stat failed, it is not possible to
64 * determine whether this object is a file or a
65 * directory. The stat buffer passed to fn will
66 * contain garbage. Stat failure for any reason
67 * other than lack of permission will be
68 * considered an error and will cause _xftw to stop
69 * and return -1 to its caller.
71 * If fn returns nonzero, _xftw stops and returns the same value
72 * to its caller. If _xftw gets into other trouble along the way,
73 * it returns -1 and leaves an indication of the cause in errno.
75 * The third argument to _xftw does not limit the depth to which
76 * _xftw will go. Rather, it limits the depth to which _xftw will
77 * go before it starts recycling file descriptors. In general,
78 * it is necessary to use a file descriptor for each level of the
79 * tree, but they can be recycled for deep trees by saving the
80 * position, closing, re-opening, and seeking. In order to descend
81 * to arbitrary depths, _xftw requires 2 file descriptors to be open
82 * during the call to openat(), therefore if the depth argument
83 * is less than 2 _xftw will not use openat(), and it will fail with
84 * ENAMETOOLONG if it descends to a directory that exceeds PATH_MAX.
88 * this interface uses the expanded stat structure and therefore
89 * must have EFT enabled.
91 #ifdef _STYPES
92 #undef _STYPES
93 #endif
95 #include "lint.h"
96 #include <sys/types.h>
97 #include <sys/stat.h>
98 #include <fcntl.h>
99 #include <sys/param.h>
100 #include <dirent.h>
101 #include <errno.h>
102 #include <ftw.h>
103 #include <string.h>
104 #include <stdlib.h>
105 #include <unistd.h>
107 struct Var {
108 int level;
109 int odepth;
112 static DIR *nocdopendir(const char *, struct Var *);
113 static int nocdstat(const char *, struct stat *, struct Var *, int);
114 static const char *get_unrooted(const char *);
115 static int fwalk(const char *, int (*)(const char *, const struct stat *, int),
116 int, struct Var *);
118 /*ARGSUSED*/
120 _xftw(int ver, const char *path,
121 int (*fn)(const char *, const struct stat *, int), int depth)
123 struct Var var;
124 int rc;
126 var.level = 0;
127 var.odepth = depth;
128 rc = fwalk(path, fn, depth, &var);
129 return (rc);
133 * This is the recursive walker.
135 static int
136 fwalk(const char *path, int (*fn)(const char *, const struct stat *, int),
137 int depth, struct Var *vp)
139 size_t n;
140 int rc;
141 int save_errno;
142 DIR *dirp;
143 char *subpath;
144 struct stat sb;
145 struct dirent *direntp;
147 vp->level++;
150 * Try to get file status.
151 * If unsuccessful, errno will say why.
152 * It's ok to have a symbolic link that points to
153 * non-existing file. In this case, pass FTW_NS
154 * to a function instead of aborting fwalk() right away.
156 if (nocdstat(path, &sb, vp, 0) < 0) {
157 #ifdef S_IFLNK
158 save_errno = errno;
159 if ((nocdstat(path, &sb, vp, AT_SYMLINK_NOFOLLOW) != -1) &&
160 ((sb.st_mode & S_IFMT) == S_IFLNK)) {
161 errno = save_errno;
162 return (*fn)(path, &sb, FTW_NS);
163 } else {
164 errno = save_errno;
166 #endif
167 return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1);
171 * The stat succeeded, so we know the object exists.
172 * If not a directory, call the user function and return.
174 if ((sb.st_mode & S_IFMT) != S_IFDIR)
175 return ((*fn)(path, &sb, FTW_F));
178 * The object was a directory.
180 * Open a file to read the directory
182 dirp = nocdopendir(path, vp);
185 * Call the user function, telling it whether
186 * the directory can be read. If it can't be read
187 * call the user function or indicate an error,
188 * depending on the reason it couldn't be read.
190 if (dirp == NULL)
191 return (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
193 /* We could read the directory. Call user function. */
194 rc = (*fn)(path, &sb, FTW_D);
195 if (rc != 0) {
196 (void) closedir(dirp);
197 return (rc);
201 * Read the directory one component at a time.
202 * We must ignore "." and "..", but other than that,
203 * just create a path name and call self to check it out.
205 while (direntp = readdir(dirp)) {
206 long here;
208 if (strcmp(direntp->d_name, ".") == 0 ||
209 strcmp(direntp->d_name, "..") == 0)
210 continue;
212 /* Create a prefix to which we will append component names */
213 n = strlen(path);
214 subpath = malloc(n + strlen(direntp->d_name) + 2);
215 if (subpath == 0) {
216 (void) closedir(dirp);
217 errno = ENOMEM;
218 return (-1);
220 (void) strcpy(subpath, path);
221 if (subpath[0] != '\0' && subpath[n-1] != '/')
222 subpath[n++] = '/';
224 /* Append component name to the working path */
225 (void) strlcpy(&subpath[n], direntp->d_name, MAXNAMELEN);
228 * If we are about to exceed our depth,
229 * remember where we are and close a file.
231 if (depth <= 1) {
232 here = telldir(dirp);
233 if (closedir(dirp) < 0) {
234 free(subpath);
235 return (-1);
240 * Do a recursive call to process the file.
241 * (watch this, sports fans)
243 rc = fwalk(subpath, fn, depth-1, vp);
244 if (rc != 0) {
245 free(subpath);
246 if (depth > 1)
247 (void) closedir(dirp);
248 return (rc);
252 * If we closed the file, try to reopen it.
254 if (depth <= 1) {
255 dirp = nocdopendir(path, vp);
256 if (dirp == NULL) {
257 free(subpath);
258 return (-1);
260 seekdir(dirp, here);
262 free(subpath);
264 (void) closedir(dirp);
265 return (0);
269 * Open a directory with an arbitrarily long path name. If the original
270 * depth arg >= 2, use openat() to make sure that it doesn't fail with
271 * ENAMETOOLONG.
273 static DIR *
274 nocdopendir(const char *path, struct Var *vp)
276 int fd, cfd;
277 DIR *fdd;
278 char *dirp, *token, *ptr;
280 fdd = opendir(path);
281 if ((vp->odepth > 1) && (fdd == NULL) && (errno == ENAMETOOLONG)) {
283 * Traverse the path using openat() to get the fd for
284 * fdopendir().
286 if ((dirp = strdup(path)) == NULL) {
287 errno = ENAMETOOLONG;
288 return (NULL);
290 if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
291 if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
292 (void) free(dirp);
293 errno = ENAMETOOLONG;
294 return (NULL);
296 while ((token = strtok_r(NULL, "/", &ptr)) != NULL) {
297 if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
298 (void) close(fd);
299 (void) free(dirp);
300 errno = ENAMETOOLONG;
301 return (NULL);
303 (void) close(fd);
304 fd = cfd;
306 (void) free(dirp);
307 return (fdopendir(fd));
309 (void) free(dirp);
310 errno = ENAMETOOLONG;
312 return (fdd);
316 * Stat a file with an arbitrarily long path name. If we aren't doing a
317 * stat on the arg passed to _xftw() and if the original depth arg >= 2,
318 * use openat() to make sure that it doesn't fail with ENAMETOOLONG.
320 static int
321 nocdstat(const char *path, struct stat *statp, struct Var *vp, int sym)
323 int fd, cfd;
324 char *dirp, *token, *ptr;
325 int rc;
326 const char *unrootp;
327 int save_err;
329 rc = fstatat(AT_FDCWD, path, statp, sym);
330 if ((vp->level > 1) && (vp->odepth >= 2) && (rc < 0) &&
331 (errno == ENAMETOOLONG)) {
332 /* Traverse path using openat() to get fd for fstatat(). */
333 if ((dirp = strdup(path)) == NULL) {
334 errno = ENAMETOOLONG;
335 return (-1);
337 if ((token = strtok_r(dirp, "/", &ptr)) != NULL) {
338 if ((fd = openat(AT_FDCWD, dirp, O_RDONLY)) < 0) {
339 (void) free(dirp);
340 errno = ENAMETOOLONG;
341 return (-1);
343 unrootp = get_unrooted(path);
344 while (((token = strtok_r(NULL, "/", &ptr)) != NULL) &&
345 (strcmp(token, unrootp) != 0)) {
346 if ((cfd = openat(fd, token, O_RDONLY)) < 0) {
347 (void) close(fd);
348 (void) free(dirp);
349 errno = ENAMETOOLONG;
350 return (0);
352 (void) close(fd);
353 fd = cfd;
355 (void) free(dirp);
356 rc = fstatat(fd, unrootp, statp, sym);
357 save_err = errno;
358 (void) close(fd);
359 errno = save_err;
360 return (rc);
362 (void) free(dirp);
363 errno = ENAMETOOLONG;
365 return (rc);
369 * Return pointer basename of path. This routine doesn't remove
370 * trailing slashes, but there won't be any.
372 static const char *
373 get_unrooted(const char *path)
375 const char *ptr;
377 if (!path || !*path)
378 return (NULL);
380 ptr = path + strlen(path);
381 /* find last char in path before any trailing slashes */
382 while (ptr != path && *--ptr == '/')
385 if (ptr == path) /* all slashes */
386 return (ptr);
388 while (ptr != path)
389 if (*--ptr == '/')
390 return (++ptr);
392 return (ptr);