1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/* @(#)walk.c 1.16 06/09/15 Copyright 2004-2006 J. Schilling */
#ifndef lint
static char sccsid[] =
"@(#)walk.c 1.16 06/09/15 Copyright 2004-2006 J. Schilling";
#endif
/*
* Walk a directory tree
*
* Copyright (c) 2004-2006 J. Schilling
*
* In order to make treewalk() thread safe, we need to make it to not use
* chdir(2)/fchdir(2) which is process global.
*
* chdir(newdir) -> old = dfd;
* dfd = openat(old, newdir, O_RDONLY);
* close(old)
* fchdir(dd) -> close(dfd); dfd = dd;
* stat(name) -> fstatat(dfd, name, statb, 0)
* lstat(name) -> fstatat(dfd, name, statb, AT_SYMLINK_NOFOLLOW)
* opendir(dname) -> dd = openat(dfd, dname, O_RDONLY);
* dir = fdopendir(dd);
*
* Similar changes need to be intruduced in fetchdir().
*/
/*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* See the file CDDL.Schily.txt in this distribution for details.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file CDDL.Schily.txt from this distribution.
*/
#include <schily/mconfig.h>
#include <stdio.h>
#include <schily/unistd.h>
#include <schily/stdlib.h>
#ifdef HAVE_FCHDIR
#include <schily/fcntl.h>
#else
#include <schily/maxpath.h>
#endif
#include <schily/stat.h>
#include <errno.h>
#include <schily/string.h>
#include <schily/standard.h>
#include <schily/getcwd.h>
#include <schily/schily.h>
#include <schily/nlsdefs.h>
#include "walk.h"
#include "fetchdir.h"
#include "mem.h"
#ifndef HAVE_LSTAT
#define lstat stat
#endif
#ifndef HAVE_DECL_STAT
extern int stat __PR((const char *, struct stat *));
#endif
#ifndef HAVE_DECL_LSTAT
extern int lstat __PR((const char *, struct stat *));
#endif
#define IS_UFS(p) ((p)[0] == 'u' && \
(p)[1] == 'f' && \
(p)[2] == 's' && \
(p)[3] == '\0')
#define IS_ZFS(p) ((p)[0] == 'z' && \
(p)[1] == 'f' && \
(p)[2] == 's' && \
(p)[3] == '\0')
#define DIR_INCR 1024 /* How to increment Curdir size */
struct twvars {
char *Curdir; /* The current path name */
int Curdtail; /* Where to append to Curdir */
int Curdlen; /* Current size of 'Curdir' */
struct stat Sb; /* stat(2) buffer for start dir */
#ifdef HAVE_FCHDIR
int Home; /* open fd to start CWD */
#else
char Home[MAXPATHNAME+1]; /* Abspath to start CWD */
#endif
};
struct pdirs {
struct pdirs *p_last; /* Previous node in list */
dev_t p_dev; /* st_dev for this dir */
ino_t p_ino; /* st_ino for this dir */
BOOL p_stat; /* Need to call stat always */
nlink_t p_nlink; /* Number of subdirs to stat */
};
typedef int (*statfun) __PR((const char *_nm, struct stat *_fs));
EXPORT int treewalk __PR((char *nm, walkfun fn, struct WALK *state));
LOCAL int walk __PR((char *nm, statfun sf, walkfun fn, struct WALK *state, struct pdirs *last));
LOCAL int incr_dspace __PR((struct twvars *varp, int amt));
EXPORT int walkhome __PR((struct WALK *_state));
EXPORT int walkclose __PR((struct WALK *_state));
LOCAL int xchdotdot __PR((struct pdirs *last, int tail, struct WALK *_state));
LOCAL int xchdir __PR((char *p));
EXPORT int
treewalk(nm, fn, state)
char *nm; /* The name to start walking */
walkfun fn; /* The function to call for each node */
struct WALK *state; /* Walk state */
{
struct twvars vars;
statfun statf = &stat;
int nlen;
if ((state->walkflags & WALK_CHDIR) == 0) {
seterrno(EINVAL);
return (-1);
}
#ifdef HAVE_FCHDIR
if ((vars.Home = open(".", O_RDONLY|O_NDELAY)) < 0) {
nlen = geterrno();
state->flags |= WALK_WF_NOCWD;
if ((state->walkflags & WALK_NOMSG) == 0)
errmsg(gettext("Cannot get working directory.\n"));
if ((state->walkflags & WALK_NOEXIT) == 0)
comexit(nlen);
return (-1);
}
#ifdef F_SETFD
fcntl(vars.Home, F_SETFD, 1);
#endif
#else
if (getcwd(vars.Home, sizeof (vars.Home)) == NULL) {
nlen = geterrno();
state->flags |= WALK_WF_NOCWD;
if ((state->walkflags & WALK_NOMSG) == 0)
errmsg(gettext("Cannot get working directory.\n"));
if ((state->walkflags & WALK_NOEXIT) == 0)
comexit(nlen);
return (-1);
}
#endif
if (nm == NULL || nm[0] == '\0')
nm = ".";
vars.Curdir = __malloc(DIR_INCR, "path buffer");
vars.Curdir[0] = 0;
vars.Curdlen = DIR_INCR;
vars.Curdtail = 0;
/*
* If initial Curdir space is not sufficient, expand it.
*/
nlen = strlen(nm);
if ((vars.Curdlen - 2) < nlen)
incr_dspace(&vars, nlen + 2);
while (lstat(nm, &vars.Sb) < 0 && geterrno() == EINTR)
;
state->twprivate = &vars;
state->flags = 0;
state->base = 0;
state->level = 0;
if (state->walkflags & WALK_PHYS)
statf = &lstat;
if (state->walkflags & (WALK_ARGFOLLOW|WALK_ALLFOLLOW))
statf = &stat;
nlen = walk(nm, statf, fn, state, (struct pdirs *)0);
walkhome(state);
#ifdef HAVE_FCHDIR
if (vars.Home >= 0) {
close(vars.Home);
vars.Home = -1;
}
#endif
free(vars.Curdir);
return (nlen);
}
LOCAL int
walk(nm, sf, fn, state, last)
char *nm; /* The current name for the walk */
statfun sf; /* stat() or lstat() */
walkfun fn; /* The function to call for each node */
struct WALK *state; /* For communication with (*fn)() */
struct pdirs *last; /* This helps to avoid loops */
{
struct pdirs thisd;
struct stat fs;
int type;
int ret;
int otail;
char *p;
struct twvars *varp = state->twprivate;
otail = varp->Curdtail;
state->base = otail;
state->flags = 0;
if (varp->Curdtail == 0 || varp->Curdir[varp->Curdtail-1] == '/') {
p = strcatl(&varp->Curdir[varp->Curdtail], nm, 0);
varp->Curdtail = p - varp->Curdir;
} else {
p = strcatl(&varp->Curdir[varp->Curdtail], "/", nm, 0);
varp->Curdtail = p - varp->Curdir;
state->base++;
}
if ((state->walkflags & WALK_NOSTAT) &&
last != NULL && !last->p_stat && last->p_nlink <= 0) {
type = WALK_F;
goto type_known;
} else {
while ((ret = (*sf)(nm, &fs)) < 0 && geterrno() == EINTR)
;
}
if (ret >= 0) {
#ifdef HAVE_ST_FSTYPE
/*
* Check for autofs mount points...
*/
if (fs.st_fstype[0] == 'a' &&
strcmp(fs.st_fstype, "autofs") == 0) {
int f = open(nm, O_RDONLY|O_NDELAY);
if (f < 0) {
type = WALK_DNR;
} else {
if (fstat(f, &fs) < 0)
type = WALK_NS;
close(f);
}
}
#endif
if (S_ISDIR(fs.st_mode)) {
type = WALK_D;
if (last != NULL && !last->p_stat && last->p_nlink > 0)
last->p_nlink--;
} else if (S_ISLNK(fs.st_mode))
type = WALK_SL;
else
type = WALK_F;
} else {
int err = geterrno();
while ((ret = lstat(nm, &fs)) < 0 && geterrno() == EINTR)
;
if (ret >= 0 &&
S_ISLNK(fs.st_mode)) {
seterrno(err);
ret = (*fn)(varp->Curdir, &fs, WALK_SLN, state);
goto out;
} else {
ret = (*fn)(varp->Curdir, &fs, WALK_NS, state);
goto out;
}
}
if ((state->walkflags & WALK_MOUNT) != 0 &&
varp->Sb.st_dev != fs.st_dev) {
ret = 0;
goto out;
}
type_known:
if (type == WALK_D) {
BOOL isdot = (nm[0] == '.' && nm[1] == '\0');
struct pdirs *pd = last;
ret = 0;
if ((state->walkflags & (WALK_PHYS|WALK_ALLFOLLOW)) == WALK_PHYS)
sf = &lstat;
/*
* Search parent dir structure for possible loops.
* If a loop is found, do not descend.
*/
thisd.p_last = last;
thisd.p_dev = fs.st_dev;
thisd.p_ino = fs.st_ino;
if (state->walkflags & WALK_NOSTAT && fs.st_nlink >= 2) {
thisd.p_stat = FALSE;
thisd.p_nlink = fs.st_nlink - 2;
#ifdef HAVE_ST_FSTYPE
if (!IS_UFS(fs.st_fstype) &&
!IS_ZFS(fs.st_fstype))
thisd.p_stat = TRUE;
#else
thisd.p_stat = TRUE; /* Safe fallback */
#endif
} else {
thisd.p_stat = TRUE;
thisd.p_nlink = 1;
}
while (pd) {
if (pd->p_dev == fs.st_dev &&
pd->p_ino == fs.st_ino) {
ret = (*fn)(varp->Curdir, &fs, WALK_DP, state);
goto out;
}
pd = pd->p_last;
}
if ((state->walkflags & WALK_DEPTH) == 0)
ret = (*fn)(varp->Curdir, &fs, type, state);
if (state->flags & WALK_WF_PRUNE)
goto out;
if (!isdot && chdir(nm) < 0) {
state->flags |= WALK_WF_NOCHDIR;
ret = (*fn)(varp->Curdir, &fs, WALK_DNR, state);
state->flags &= ~WALK_WF_NOCHDIR;
goto out;
} else {
char *dp;
char *odp;
int nents;
int Dspace;
/*
* Add space for '/' & '\0'
*/
Dspace = varp->Curdlen - varp->Curdtail - 2;
if ((dp = fetchdir(".", &nents, NULL, NULL)) == NULL) {
ret = (*fn)(varp->Curdir, &fs, WALK_DNR, state);
goto skip;
}
odp = dp;
while (nents > 0 && ret == 0) {
register char *name;
register int nlen;
name = &dp[1];
nlen = strlen(name);
if (Dspace < nlen)
Dspace += incr_dspace(varp, nlen + 2);
state->level++;
ret = walk(name, sf, fn, state, &thisd);
state->level--;
nents--;
dp += nlen +2;
}
free(odp);
skip:
if (!isdot && state->level > 0 && xchdotdot(last,
otail, state) < 0) {
ret = geterrno();
state->flags |= WALK_WF_NOHOME;
if ((state->walkflags & WALK_NOMSG) == 0) {
errmsg(
gettext(
"Cannot chdir to '..' from '%s/'.\n"),
varp->Curdir);
}
if ((state->walkflags & WALK_NOEXIT) == 0)
comexit(ret);
ret = -1;
goto out;
}
}
if ((state->walkflags & WALK_DEPTH) != 0)
ret = (*fn)(varp->Curdir, &fs, type, state);
} else {
ret = (*fn)(varp->Curdir, &fs, type, state);
}
out:
varp->Curdir[otail] = '\0';
varp->Curdtail = otail;
return (ret);
}
LOCAL int
incr_dspace(varp, amt)
int amt;
struct twvars *varp;
{
int incr = DIR_INCR;
if (amt < 0)
amt = 0;
while (incr < amt)
incr += DIR_INCR;
varp->Curdir = __realloc(varp->Curdir, varp->Curdlen + incr,
"path buffer");
varp->Curdlen += incr;
return (incr);
}
EXPORT int
walkhome(state)
struct WALK *state;
{
struct twvars *varp = state->twprivate;
#ifdef HAVE_FCHDIR
if (varp->Home >= 0)
return (fchdir(varp->Home));
#else
if (varp->Home[0] != '\0')
return (chdir(varp->Home));
#endif
return (0);
}
EXPORT int
walkclose(state)
struct WALK *state;
{
struct twvars *varp = state->twprivate;
#ifdef HAVE_FCHDIR
int ret = 0;
if (varp->Home >= 0)
ret = close(varp->Home);
varp->Home = -1;
return (ret);
#else
varp->Home[0] = '\0';
return (0);
#endif
}
LOCAL int
xchdotdot(last, tail, state)
struct pdirs *last;
int tail;
struct WALK *state;
{
struct twvars *varp = state->twprivate;
char c;
struct stat sb;
if (chdir("..") >= 0) {
seterrno(0);
if (stat(".", &sb) >= 0) {
if (sb.st_dev == last->p_dev &&
sb.st_ino == last->p_ino)
return (0);
}
}
if (walkhome(state) < 0)
return (-1);
c = varp->Curdir[tail];
varp->Curdir[tail] = '\0';
if (chdir(varp->Curdir) < 0) {
if (geterrno() != ENAMETOOLONG) {
varp->Curdir[tail] = c;
return (-1);
}
if (xchdir(varp->Curdir) < 0) {
varp->Curdir[tail] = c;
return (-1);
}
}
varp->Curdir[tail] = c;
seterrno(0);
if (stat(".", &sb) >= 0) {
if (sb.st_dev == last->p_dev &&
sb.st_ino == last->p_ino)
return (0);
}
return (-1);
}
LOCAL int
xchdir(p)
char *p;
{
char *p2;
while (*p) {
if ((p2 = strchr(p, '/')) != NULL)
*p2 = '\0';
if (chdir(p) < 0)
return (-1);
if (p2 == NULL)
break;
*p2 = '/';
p = &p2[1];
}
return (0);
}
|