| 1 | /* File tree walker functions. | 
|---|
| 2 | Copyright (C) 1996-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | 
|---|
| 5 |  | 
|---|
| 6 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 7 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 8 | License as published by the Free Software Foundation; either | 
|---|
| 9 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 14 | Lesser General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 17 | License along with the GNU C Library; if not, see | 
|---|
| 18 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #ifdef HAVE_CONFIG_H | 
|---|
| 21 | # include <config.h> | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | #if __GNUC__ | 
|---|
| 25 | # define alloca __builtin_alloca | 
|---|
| 26 | #else | 
|---|
| 27 | # if HAVE_ALLOCA_H | 
|---|
| 28 | #  include <alloca.h> | 
|---|
| 29 | # else | 
|---|
| 30 | #  ifdef _AIX | 
|---|
| 31 | #  pragma alloca | 
|---|
| 32 | #  else | 
|---|
| 33 | char *alloca (); | 
|---|
| 34 | #  endif | 
|---|
| 35 | # endif | 
|---|
| 36 | #endif | 
|---|
| 37 |  | 
|---|
| 38 | #ifdef _LIBC | 
|---|
| 39 | # include <dirent.h> | 
|---|
| 40 | # define NAMLEN(dirent) _D_EXACT_NAMLEN (dirent) | 
|---|
| 41 | #else | 
|---|
| 42 | # if HAVE_DIRENT_H | 
|---|
| 43 | #  include <dirent.h> | 
|---|
| 44 | #  define NAMLEN(dirent) strlen ((dirent)->d_name) | 
|---|
| 45 | # else | 
|---|
| 46 | #  define dirent direct | 
|---|
| 47 | #  define NAMLEN(dirent) (dirent)->d_namlen | 
|---|
| 48 | #  if HAVE_SYS_NDIR_H | 
|---|
| 49 | #   include <sys/ndir.h> | 
|---|
| 50 | #  endif | 
|---|
| 51 | #  if HAVE_SYS_DIR_H | 
|---|
| 52 | #   include <sys/dir.h> | 
|---|
| 53 | #  endif | 
|---|
| 54 | #  if HAVE_NDIR_H | 
|---|
| 55 | #   include <ndir.h> | 
|---|
| 56 | #  endif | 
|---|
| 57 | # endif | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | #include <errno.h> | 
|---|
| 61 | #include <fcntl.h> | 
|---|
| 62 | #include <ftw.h> | 
|---|
| 63 | #include <limits.h> | 
|---|
| 64 | #include <search.h> | 
|---|
| 65 | #include <stdlib.h> | 
|---|
| 66 | #include <string.h> | 
|---|
| 67 | #include <unistd.h> | 
|---|
| 68 | #include <not-cancel.h> | 
|---|
| 69 | #include <sys/param.h> | 
|---|
| 70 | #ifdef _LIBC | 
|---|
| 71 | # include <include/sys/stat.h> | 
|---|
| 72 | #else | 
|---|
| 73 | # include <sys/stat.h> | 
|---|
| 74 | #endif | 
|---|
| 75 |  | 
|---|
| 76 | #if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy | 
|---|
| 77 | char *stpcpy (); | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 | #if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy | 
|---|
| 81 | /* Be CAREFUL that there are no side effects in N.  */ | 
|---|
| 82 | # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) | 
|---|
| 83 | #endif | 
|---|
| 84 |  | 
|---|
| 85 | /* #define NDEBUG 1 */ | 
|---|
| 86 | #include <assert.h> | 
|---|
| 87 |  | 
|---|
| 88 | #ifndef _LIBC | 
|---|
| 89 | # undef __chdir | 
|---|
| 90 | # define __chdir chdir | 
|---|
| 91 | # undef __closedir | 
|---|
| 92 | # define __closedir closedir | 
|---|
| 93 | # undef __fchdir | 
|---|
| 94 | # define __fchdir fchdir | 
|---|
| 95 | # undef __getcwd | 
|---|
| 96 | # define __getcwd(P, N) xgetcwd () | 
|---|
| 97 | extern char *xgetcwd (void); | 
|---|
| 98 | # undef __mempcpy | 
|---|
| 99 | # define __mempcpy mempcpy | 
|---|
| 100 | # undef __opendir | 
|---|
| 101 | # define __opendir opendir | 
|---|
| 102 | # undef __readdir64 | 
|---|
| 103 | # define __readdir64 readdir | 
|---|
| 104 | # undef __stpcpy | 
|---|
| 105 | # define __stpcpy stpcpy | 
|---|
| 106 | # undef __tdestroy | 
|---|
| 107 | # define __tdestroy tdestroy | 
|---|
| 108 | # undef __tfind | 
|---|
| 109 | # define __tfind tfind | 
|---|
| 110 | # undef __tsearch | 
|---|
| 111 | # define __tsearch tsearch | 
|---|
| 112 | # undef dirent64 | 
|---|
| 113 | # define dirent64 dirent | 
|---|
| 114 | # undef MAX | 
|---|
| 115 | # define MAX(a, b) ((a) > (b) ? (a) : (b)) | 
|---|
| 116 | #endif | 
|---|
| 117 |  | 
|---|
| 118 | /* Arrange to make lstat calls go through the wrapper function | 
|---|
| 119 | on systems with an lstat function that does not dereference symlinks | 
|---|
| 120 | that are specified with a trailing slash.  */ | 
|---|
| 121 | #if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK | 
|---|
| 122 | int rpl_lstat (const char *, struct stat *); | 
|---|
| 123 | # undef lstat | 
|---|
| 124 | # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf) | 
|---|
| 125 | #endif | 
|---|
| 126 |  | 
|---|
| 127 | #ifndef __set_errno | 
|---|
| 128 | # define __set_errno(Val) errno = (Val) | 
|---|
| 129 | #endif | 
|---|
| 130 |  | 
|---|
| 131 | /* Support for the LFS API version.  */ | 
|---|
| 132 | #ifndef FTW_NAME | 
|---|
| 133 | # define FTW_NAME ftw | 
|---|
| 134 | # define NFTW_NAME nftw | 
|---|
| 135 | # define NFTW_OLD_NAME __old_nftw | 
|---|
| 136 | # define NFTW_NEW_NAME __new_nftw | 
|---|
| 137 | # define INO_T ino_t | 
|---|
| 138 | # define STAT stat | 
|---|
| 139 | # ifdef _LIBC | 
|---|
| 140 | #  define LXSTAT __lxstat | 
|---|
| 141 | #  define XSTAT __xstat | 
|---|
| 142 | #  define FXSTATAT __fxstatat | 
|---|
| 143 | # else | 
|---|
| 144 | #  define LXSTAT(V,f,sb) lstat (f,sb) | 
|---|
| 145 | #  define XSTAT(V,f,sb) stat (f,sb) | 
|---|
| 146 | #  define FXSTATAT(V,d,f,sb,m) fstatat (d, f, sb, m) | 
|---|
| 147 | # endif | 
|---|
| 148 | # define FTW_FUNC_T __ftw_func_t | 
|---|
| 149 | # define NFTW_FUNC_T __nftw_func_t | 
|---|
| 150 | #endif | 
|---|
| 151 |  | 
|---|
| 152 | /* We define PATH_MAX if the system does not provide a definition. | 
|---|
| 153 | This does not artificially limit any operation.  PATH_MAX is simply | 
|---|
| 154 | used as a guesstimate for the expected maximal path length. | 
|---|
| 155 | Buffers will be enlarged if necessary.  */ | 
|---|
| 156 | #ifndef PATH_MAX | 
|---|
| 157 | # define PATH_MAX 1024 | 
|---|
| 158 | #endif | 
|---|
| 159 |  | 
|---|
| 160 | struct dir_data | 
|---|
| 161 | { | 
|---|
| 162 | DIR *stream; | 
|---|
| 163 | int streamfd; | 
|---|
| 164 | char *content; | 
|---|
| 165 | }; | 
|---|
| 166 |  | 
|---|
| 167 | struct known_object | 
|---|
| 168 | { | 
|---|
| 169 | dev_t dev; | 
|---|
| 170 | INO_T ino; | 
|---|
| 171 | }; | 
|---|
| 172 |  | 
|---|
| 173 | struct ftw_data | 
|---|
| 174 | { | 
|---|
| 175 | /* Array with pointers to open directory streams.  */ | 
|---|
| 176 | struct dir_data **dirstreams; | 
|---|
| 177 | size_t actdir; | 
|---|
| 178 | size_t maxdir; | 
|---|
| 179 |  | 
|---|
| 180 | /* Buffer containing name of currently processed object.  */ | 
|---|
| 181 | char *dirbuf; | 
|---|
| 182 | size_t dirbufsize; | 
|---|
| 183 |  | 
|---|
| 184 | /* Passed as fourth argument to `nftw' callback.  The `base' member | 
|---|
| 185 | tracks the content of the `dirbuf'.  */ | 
|---|
| 186 | struct FTW ftw; | 
|---|
| 187 |  | 
|---|
| 188 | /* Flags passed to `nftw' function.  0 for `ftw'.  */ | 
|---|
| 189 | int flags; | 
|---|
| 190 |  | 
|---|
| 191 | /* Conversion array for flag values.  It is the identity mapping for | 
|---|
| 192 | `nftw' calls, otherwise it maps the values to those known by | 
|---|
| 193 | `ftw'.  */ | 
|---|
| 194 | const int *cvt_arr; | 
|---|
| 195 |  | 
|---|
| 196 | /* Callback function.  We always use the `nftw' form.  */ | 
|---|
| 197 | NFTW_FUNC_T func; | 
|---|
| 198 |  | 
|---|
| 199 | /* Device of starting point.  Needed for FTW_MOUNT.  */ | 
|---|
| 200 | dev_t dev; | 
|---|
| 201 |  | 
|---|
| 202 | /* Data structure for keeping fingerprints of already processed | 
|---|
| 203 | object.  This is needed when not using FTW_PHYS.  */ | 
|---|
| 204 | void *known_objects; | 
|---|
| 205 | }; | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | /* Internally we use the FTW_* constants used for `nftw'.  When invoked | 
|---|
| 209 | as `ftw', map each flag to the subset of values used by `ftw'.  */ | 
|---|
| 210 | static const int nftw_arr[] = | 
|---|
| 211 | { | 
|---|
| 212 | FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN | 
|---|
| 213 | }; | 
|---|
| 214 |  | 
|---|
| 215 | static const int ftw_arr[] = | 
|---|
| 216 | { | 
|---|
| 217 | FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS | 
|---|
| 218 | }; | 
|---|
| 219 |  | 
|---|
| 220 |  | 
|---|
| 221 | /* Forward declarations of local functions.  */ | 
|---|
| 222 | static int ftw_dir (struct ftw_data *data, struct STAT *st, | 
|---|
| 223 | struct dir_data *old_dir); | 
|---|
| 224 |  | 
|---|
| 225 |  | 
|---|
| 226 | static int | 
|---|
| 227 | object_compare (const void *p1, const void *p2) | 
|---|
| 228 | { | 
|---|
| 229 | /* We don't need a sophisticated and useful comparison.  We are only | 
|---|
| 230 | interested in equality.  However, we must be careful not to | 
|---|
| 231 | accidentally compare `holes' in the structure.  */ | 
|---|
| 232 | const struct known_object *kp1 = p1, *kp2 = p2; | 
|---|
| 233 | int cmp1; | 
|---|
| 234 | cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino); | 
|---|
| 235 | if (cmp1 != 0) | 
|---|
| 236 | return cmp1; | 
|---|
| 237 | return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev); | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 | static int | 
|---|
| 242 | add_object (struct ftw_data *data, struct STAT *st) | 
|---|
| 243 | { | 
|---|
| 244 | struct known_object *newp = malloc (sizeof (struct known_object)); | 
|---|
| 245 | if (newp == NULL) | 
|---|
| 246 | return -1; | 
|---|
| 247 | newp->dev = st->st_dev; | 
|---|
| 248 | newp->ino = st->st_ino; | 
|---|
| 249 | return __tsearch (newp, &data->known_objects, object_compare) ? 0 : -1; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | static inline int | 
|---|
| 254 | find_object (struct ftw_data *data, struct STAT *st) | 
|---|
| 255 | { | 
|---|
| 256 | struct known_object obj; | 
|---|
| 257 | obj.dev = st->st_dev; | 
|---|
| 258 | obj.ino = st->st_ino; | 
|---|
| 259 | return __tfind (&obj, &data->known_objects, object_compare) != NULL; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | static inline int | 
|---|
| 264 | __attribute ((always_inline)) | 
|---|
| 265 | open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp) | 
|---|
| 266 | { | 
|---|
| 267 | int result = 0; | 
|---|
| 268 |  | 
|---|
| 269 | if (data->dirstreams[data->actdir] != NULL) | 
|---|
| 270 | { | 
|---|
| 271 | /* Oh, oh.  We must close this stream.  Get all remaining | 
|---|
| 272 | entries and store them as a list in the `content' member of | 
|---|
| 273 | the `struct dir_data' variable.  */ | 
|---|
| 274 | size_t bufsize = 1024; | 
|---|
| 275 | char *buf = malloc (bufsize); | 
|---|
| 276 |  | 
|---|
| 277 | if (buf == NULL) | 
|---|
| 278 | result = -1; | 
|---|
| 279 | else | 
|---|
| 280 | { | 
|---|
| 281 | DIR *st = data->dirstreams[data->actdir]->stream; | 
|---|
| 282 | struct dirent64 *d; | 
|---|
| 283 | size_t actsize = 0; | 
|---|
| 284 |  | 
|---|
| 285 | while ((d = __readdir64 (st)) != NULL) | 
|---|
| 286 | { | 
|---|
| 287 | size_t this_len = NAMLEN (d); | 
|---|
| 288 | if (actsize + this_len + 2 >= bufsize) | 
|---|
| 289 | { | 
|---|
| 290 | char *newp; | 
|---|
| 291 | bufsize += MAX (1024, 2 * this_len); | 
|---|
| 292 | newp = (char *) realloc (buf, bufsize); | 
|---|
| 293 | if (newp == NULL) | 
|---|
| 294 | { | 
|---|
| 295 | /* No more memory.  */ | 
|---|
| 296 | int save_err = errno; | 
|---|
| 297 | free (buf); | 
|---|
| 298 | __set_errno (save_err); | 
|---|
| 299 | return -1; | 
|---|
| 300 | } | 
|---|
| 301 | buf = newp; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | *((char *) __mempcpy (buf + actsize, d->d_name, this_len)) | 
|---|
| 305 | = '\0'; | 
|---|
| 306 | actsize += this_len + 1; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | /* Terminate the list with an additional NUL byte.  */ | 
|---|
| 310 | buf[actsize++] = '\0'; | 
|---|
| 311 |  | 
|---|
| 312 | /* Shrink the buffer to what we actually need.  */ | 
|---|
| 313 | data->dirstreams[data->actdir]->content = realloc (buf, actsize); | 
|---|
| 314 | if (data->dirstreams[data->actdir]->content == NULL) | 
|---|
| 315 | { | 
|---|
| 316 | int save_err = errno; | 
|---|
| 317 | free (buf); | 
|---|
| 318 | __set_errno (save_err); | 
|---|
| 319 | result = -1; | 
|---|
| 320 | } | 
|---|
| 321 | else | 
|---|
| 322 | { | 
|---|
| 323 | __closedir (st); | 
|---|
| 324 | data->dirstreams[data->actdir]->stream = NULL; | 
|---|
| 325 | data->dirstreams[data->actdir]->streamfd = -1; | 
|---|
| 326 | data->dirstreams[data->actdir] = NULL; | 
|---|
| 327 | } | 
|---|
| 328 | } | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | /* Open the new stream.  */ | 
|---|
| 332 | if (result == 0) | 
|---|
| 333 | { | 
|---|
| 334 | assert (data->dirstreams[data->actdir] == NULL); | 
|---|
| 335 |  | 
|---|
| 336 | if (dfdp != NULL && *dfdp != -1) | 
|---|
| 337 | { | 
|---|
| 338 | int fd = __openat64_nocancel (*dfdp, data->dirbuf + data->ftw.base, | 
|---|
| 339 | O_RDONLY | O_DIRECTORY | O_NDELAY); | 
|---|
| 340 | dirp->stream = NULL; | 
|---|
| 341 | if (fd != -1 && (dirp->stream = __fdopendir (fd)) == NULL) | 
|---|
| 342 | __close_nocancel_nostatus (fd); | 
|---|
| 343 | } | 
|---|
| 344 | else | 
|---|
| 345 | { | 
|---|
| 346 | const char *name; | 
|---|
| 347 |  | 
|---|
| 348 | if (data->flags & FTW_CHDIR) | 
|---|
| 349 | { | 
|---|
| 350 | name = data->dirbuf + data->ftw.base; | 
|---|
| 351 | if (name[0] == '\0') | 
|---|
| 352 | name = "."; | 
|---|
| 353 | } | 
|---|
| 354 | else | 
|---|
| 355 | name = data->dirbuf; | 
|---|
| 356 |  | 
|---|
| 357 | dirp->stream = __opendir (name); | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | if (dirp->stream == NULL) | 
|---|
| 361 | result = -1; | 
|---|
| 362 | else | 
|---|
| 363 | { | 
|---|
| 364 | dirp->streamfd = __dirfd (dirp->stream); | 
|---|
| 365 | dirp->content = NULL; | 
|---|
| 366 | data->dirstreams[data->actdir] = dirp; | 
|---|
| 367 |  | 
|---|
| 368 | if (++data->actdir == data->maxdir) | 
|---|
| 369 | data->actdir = 0; | 
|---|
| 370 | } | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | return result; | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 |  | 
|---|
| 377 | static int | 
|---|
| 378 | process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, | 
|---|
| 379 | size_t namlen, int d_type) | 
|---|
| 380 | { | 
|---|
| 381 | struct STAT st; | 
|---|
| 382 | int result = 0; | 
|---|
| 383 | int flag = 0; | 
|---|
| 384 | size_t new_buflen; | 
|---|
| 385 |  | 
|---|
| 386 | if (name[0] == '.' && (name[1] == '\0' | 
|---|
| 387 | || (name[1] == '.' && name[2] == '\0'))) | 
|---|
| 388 | /* Don't process the "." and ".." entries.  */ | 
|---|
| 389 | return 0; | 
|---|
| 390 |  | 
|---|
| 391 | new_buflen = data->ftw.base + namlen + 2; | 
|---|
| 392 | if (data->dirbufsize < new_buflen) | 
|---|
| 393 | { | 
|---|
| 394 | /* Enlarge the buffer.  */ | 
|---|
| 395 | char *newp; | 
|---|
| 396 |  | 
|---|
| 397 | data->dirbufsize = 2 * new_buflen; | 
|---|
| 398 | newp = (char *) realloc (data->dirbuf, data->dirbufsize); | 
|---|
| 399 | if (newp == NULL) | 
|---|
| 400 | return -1; | 
|---|
| 401 | data->dirbuf = newp; | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0'; | 
|---|
| 405 |  | 
|---|
| 406 | int statres; | 
|---|
| 407 | if (dir->streamfd != -1) | 
|---|
| 408 | statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st, | 
|---|
| 409 | (data->flags & FTW_PHYS) ? AT_SYMLINK_NOFOLLOW : 0); | 
|---|
| 410 | else | 
|---|
| 411 | { | 
|---|
| 412 | if ((data->flags & FTW_CHDIR) == 0) | 
|---|
| 413 | name = data->dirbuf; | 
|---|
| 414 |  | 
|---|
| 415 | statres = ((data->flags & FTW_PHYS) | 
|---|
| 416 | ? LXSTAT (_STAT_VER, name, &st) | 
|---|
| 417 | : XSTAT (_STAT_VER, name, &st)); | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|
| 420 | if (statres < 0) | 
|---|
| 421 | { | 
|---|
| 422 | if (errno != EACCES && errno != ENOENT) | 
|---|
| 423 | result = -1; | 
|---|
| 424 | else if (data->flags & FTW_PHYS) | 
|---|
| 425 | flag = FTW_NS; | 
|---|
| 426 | else | 
|---|
| 427 | { | 
|---|
| 428 | /* Old code left ST undefined for dangling DT_LNK without | 
|---|
| 429 | FTW_PHYS set; a clarification at the POSIX level suggests | 
|---|
| 430 | it should contain information about the link (ala lstat). | 
|---|
| 431 | We do our best to fill in what data we can.  */ | 
|---|
| 432 | if (dir->streamfd != -1) | 
|---|
| 433 | statres = FXSTATAT (_STAT_VER, dir->streamfd, name, &st, | 
|---|
| 434 | AT_SYMLINK_NOFOLLOW); | 
|---|
| 435 | else | 
|---|
| 436 | statres = LXSTAT (_STAT_VER, name, &st); | 
|---|
| 437 | if (statres == 0 && S_ISLNK (st.st_mode)) | 
|---|
| 438 | flag = FTW_SLN; | 
|---|
| 439 | else | 
|---|
| 440 | flag = FTW_NS; | 
|---|
| 441 | } | 
|---|
| 442 | } | 
|---|
| 443 | else | 
|---|
| 444 | { | 
|---|
| 445 | if (S_ISDIR (st.st_mode)) | 
|---|
| 446 | flag = FTW_D; | 
|---|
| 447 | else if (S_ISLNK (st.st_mode)) | 
|---|
| 448 | flag = FTW_SL; | 
|---|
| 449 | else | 
|---|
| 450 | flag = FTW_F; | 
|---|
| 451 | } | 
|---|
| 452 |  | 
|---|
| 453 | if (result == 0 | 
|---|
| 454 | && (flag == FTW_NS | 
|---|
| 455 | || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev)) | 
|---|
| 456 | { | 
|---|
| 457 | if (flag == FTW_D) | 
|---|
| 458 | { | 
|---|
| 459 | if ((data->flags & FTW_PHYS) | 
|---|
| 460 | || (!find_object (data, &st) | 
|---|
| 461 | /* Remember the object.  */ | 
|---|
| 462 | && (result = add_object (data, &st)) == 0)) | 
|---|
| 463 | result = ftw_dir (data, &st, dir); | 
|---|
| 464 | } | 
|---|
| 465 | else | 
|---|
| 466 | result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag], | 
|---|
| 467 | &data->ftw); | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SUBTREE) | 
|---|
| 471 | result = 0; | 
|---|
| 472 |  | 
|---|
| 473 | return result; | 
|---|
| 474 | } | 
|---|
| 475 |  | 
|---|
| 476 |  | 
|---|
| 477 | static int | 
|---|
| 478 | __attribute ((noinline)) | 
|---|
| 479 | ftw_dir (struct ftw_data *data, struct STAT *st, struct dir_data *old_dir) | 
|---|
| 480 | { | 
|---|
| 481 | struct dir_data dir; | 
|---|
| 482 | struct dirent64 *d; | 
|---|
| 483 | int previous_base = data->ftw.base; | 
|---|
| 484 | int result; | 
|---|
| 485 | char *startp; | 
|---|
| 486 |  | 
|---|
| 487 | /* Open the stream for this directory.  This might require that | 
|---|
| 488 | another stream has to be closed.  */ | 
|---|
| 489 | result = open_dir_stream (old_dir == NULL ? NULL : &old_dir->streamfd, | 
|---|
| 490 | data, &dir); | 
|---|
| 491 | if (result != 0) | 
|---|
| 492 | { | 
|---|
| 493 | if (errno == EACCES) | 
|---|
| 494 | /* We cannot read the directory.  Signal this with a special flag.  */ | 
|---|
| 495 | result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw); | 
|---|
| 496 |  | 
|---|
| 497 | return result; | 
|---|
| 498 | } | 
|---|
| 499 |  | 
|---|
| 500 | /* First, report the directory (if not depth-first).  */ | 
|---|
| 501 | if (!(data->flags & FTW_DEPTH)) | 
|---|
| 502 | { | 
|---|
| 503 | result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw); | 
|---|
| 504 | if (result != 0) | 
|---|
| 505 | { | 
|---|
| 506 | int save_err; | 
|---|
| 507 | fail: | 
|---|
| 508 | save_err = errno; | 
|---|
| 509 | __closedir (dir.stream); | 
|---|
| 510 | dir.streamfd = -1; | 
|---|
| 511 | __set_errno (save_err); | 
|---|
| 512 |  | 
|---|
| 513 | if (data->actdir-- == 0) | 
|---|
| 514 | data->actdir = data->maxdir - 1; | 
|---|
| 515 | data->dirstreams[data->actdir] = NULL; | 
|---|
| 516 | return result; | 
|---|
| 517 | } | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 | /* If necessary, change to this directory.  */ | 
|---|
| 521 | if (data->flags & FTW_CHDIR) | 
|---|
| 522 | { | 
|---|
| 523 | if (__fchdir (__dirfd (dir.stream)) < 0) | 
|---|
| 524 | { | 
|---|
| 525 | result = -1; | 
|---|
| 526 | goto fail; | 
|---|
| 527 | } | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | /* Next, update the `struct FTW' information.  */ | 
|---|
| 531 | ++data->ftw.level; | 
|---|
| 532 | startp = __rawmemchr (data->dirbuf, '\0'); | 
|---|
| 533 | /* There always must be a directory name.  */ | 
|---|
| 534 | assert (startp != data->dirbuf); | 
|---|
| 535 | if (startp[-1] != '/') | 
|---|
| 536 | *startp++ = '/'; | 
|---|
| 537 | data->ftw.base = startp - data->dirbuf; | 
|---|
| 538 |  | 
|---|
| 539 | while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL) | 
|---|
| 540 | { | 
|---|
| 541 | int d_type = DT_UNKNOWN; | 
|---|
| 542 | #ifdef _DIRENT_HAVE_D_TYPE | 
|---|
| 543 | d_type = d->d_type; | 
|---|
| 544 | #endif | 
|---|
| 545 | result = process_entry (data, &dir, d->d_name, NAMLEN (d), d_type); | 
|---|
| 546 | if (result != 0) | 
|---|
| 547 | break; | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 | if (dir.stream != NULL) | 
|---|
| 551 | { | 
|---|
| 552 | /* The stream is still open.  I.e., we did not need more | 
|---|
| 553 | descriptors.  Simply close the stream now.  */ | 
|---|
| 554 | int save_err = errno; | 
|---|
| 555 |  | 
|---|
| 556 | assert (dir.content == NULL); | 
|---|
| 557 |  | 
|---|
| 558 | __closedir (dir.stream); | 
|---|
| 559 | dir.streamfd = -1; | 
|---|
| 560 | __set_errno (save_err); | 
|---|
| 561 |  | 
|---|
| 562 | if (data->actdir-- == 0) | 
|---|
| 563 | data->actdir = data->maxdir - 1; | 
|---|
| 564 | data->dirstreams[data->actdir] = NULL; | 
|---|
| 565 | } | 
|---|
| 566 | else | 
|---|
| 567 | { | 
|---|
| 568 | int save_err; | 
|---|
| 569 | char *runp = dir.content; | 
|---|
| 570 |  | 
|---|
| 571 | while (result == 0 && *runp != '\0') | 
|---|
| 572 | { | 
|---|
| 573 | char *endp = strchr (runp, '\0'); | 
|---|
| 574 |  | 
|---|
| 575 | // XXX Should store the d_type values as well?! | 
|---|
| 576 | result = process_entry (data, &dir, runp, endp - runp, DT_UNKNOWN); | 
|---|
| 577 |  | 
|---|
| 578 | runp = endp + 1; | 
|---|
| 579 | } | 
|---|
| 580 |  | 
|---|
| 581 | save_err = errno; | 
|---|
| 582 | free (dir.content); | 
|---|
| 583 | __set_errno (save_err); | 
|---|
| 584 | } | 
|---|
| 585 |  | 
|---|
| 586 | if ((data->flags & FTW_ACTIONRETVAL) && result == FTW_SKIP_SIBLINGS) | 
|---|
| 587 | result = 0; | 
|---|
| 588 |  | 
|---|
| 589 | /* Prepare the return, revert the `struct FTW' information.  */ | 
|---|
| 590 | data->dirbuf[data->ftw.base - 1] = '\0'; | 
|---|
| 591 | --data->ftw.level; | 
|---|
| 592 | data->ftw.base = previous_base; | 
|---|
| 593 |  | 
|---|
| 594 | /* Finally, if we process depth-first report the directory.  */ | 
|---|
| 595 | if (result == 0 && (data->flags & FTW_DEPTH)) | 
|---|
| 596 | result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw); | 
|---|
| 597 |  | 
|---|
| 598 | if (old_dir | 
|---|
| 599 | && (data->flags & FTW_CHDIR) | 
|---|
| 600 | && (result == 0 | 
|---|
| 601 | || ((data->flags & FTW_ACTIONRETVAL) | 
|---|
| 602 | && (result != -1 && result != FTW_STOP)))) | 
|---|
| 603 | { | 
|---|
| 604 | /* Change back to the parent directory.  */ | 
|---|
| 605 | int done = 0; | 
|---|
| 606 | if (old_dir->stream != NULL) | 
|---|
| 607 | if (__fchdir (__dirfd (old_dir->stream)) == 0) | 
|---|
| 608 | done = 1; | 
|---|
| 609 |  | 
|---|
| 610 | if (!done) | 
|---|
| 611 | { | 
|---|
| 612 | if (data->ftw.base == 1) | 
|---|
| 613 | { | 
|---|
| 614 | if (__chdir ( "/") < 0) | 
|---|
| 615 | result = -1; | 
|---|
| 616 | } | 
|---|
| 617 | else | 
|---|
| 618 | if (__chdir ( "..") < 0) | 
|---|
| 619 | result = -1; | 
|---|
| 620 | } | 
|---|
| 621 | } | 
|---|
| 622 |  | 
|---|
| 623 | return result; | 
|---|
| 624 | } | 
|---|
| 625 |  | 
|---|
| 626 |  | 
|---|
| 627 | static int | 
|---|
| 628 | __attribute ((noinline)) | 
|---|
| 629 | ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, | 
|---|
| 630 | int flags) | 
|---|
| 631 | { | 
|---|
| 632 | struct ftw_data data; | 
|---|
| 633 | struct STAT st; | 
|---|
| 634 | int result = 0; | 
|---|
| 635 | int save_err; | 
|---|
| 636 | int cwdfd = -1; | 
|---|
| 637 | char *cwd = NULL; | 
|---|
| 638 | char *cp; | 
|---|
| 639 |  | 
|---|
| 640 | /* First make sure the parameters are reasonable.  */ | 
|---|
| 641 | if (dir[0] == '\0') | 
|---|
| 642 | { | 
|---|
| 643 | __set_errno (ENOENT); | 
|---|
| 644 | return -1; | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | data.maxdir = descriptors < 1 ? 1 : descriptors; | 
|---|
| 648 | data.actdir = 0; | 
|---|
| 649 | data.dirstreams = (struct dir_data **) alloca (data.maxdir | 
|---|
| 650 | * sizeof (struct dir_data *)); | 
|---|
| 651 | memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *)); | 
|---|
| 652 |  | 
|---|
| 653 | /* PATH_MAX is always defined when we get here.  */ | 
|---|
| 654 | data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX); | 
|---|
| 655 | data.dirbuf = (char *) malloc (data.dirbufsize); | 
|---|
| 656 | if (data.dirbuf == NULL) | 
|---|
| 657 | return -1; | 
|---|
| 658 | cp = __stpcpy (data.dirbuf, dir); | 
|---|
| 659 | /* Strip trailing slashes.  */ | 
|---|
| 660 | while (cp > data.dirbuf + 1 && cp[-1] == '/') | 
|---|
| 661 | --cp; | 
|---|
| 662 | *cp = '\0'; | 
|---|
| 663 |  | 
|---|
| 664 | data.ftw.level = 0; | 
|---|
| 665 |  | 
|---|
| 666 | /* Find basename.  */ | 
|---|
| 667 | while (cp > data.dirbuf && cp[-1] != '/') | 
|---|
| 668 | --cp; | 
|---|
| 669 | data.ftw.base = cp - data.dirbuf; | 
|---|
| 670 |  | 
|---|
| 671 | data.flags = flags; | 
|---|
| 672 |  | 
|---|
| 673 | /* This assignment might seem to be strange but it is what we want. | 
|---|
| 674 | The trick is that the first three arguments to the `ftw' and | 
|---|
| 675 | `nftw' callback functions are equal.  Therefore we can call in | 
|---|
| 676 | every case the callback using the format of the `nftw' version | 
|---|
| 677 | and get the correct result since the stack layout for a function | 
|---|
| 678 | call in C allows this.  */ | 
|---|
| 679 | data.func = (NFTW_FUNC_T) func; | 
|---|
| 680 |  | 
|---|
| 681 | /* Since we internally use the complete set of FTW_* values we need | 
|---|
| 682 | to reduce the value range before calling a `ftw' callback.  */ | 
|---|
| 683 | data.cvt_arr = is_nftw ? nftw_arr : ftw_arr; | 
|---|
| 684 |  | 
|---|
| 685 | /* No object known so far.  */ | 
|---|
| 686 | data.known_objects = NULL; | 
|---|
| 687 |  | 
|---|
| 688 | /* Now go to the directory containing the initial file/directory.  */ | 
|---|
| 689 | if (flags & FTW_CHDIR) | 
|---|
| 690 | { | 
|---|
| 691 | /* We have to be able to go back to the current working | 
|---|
| 692 | directory.  The best way to do this is to use a file | 
|---|
| 693 | descriptor.  */ | 
|---|
| 694 | cwdfd = __open ( ".", O_RDONLY | O_DIRECTORY); | 
|---|
| 695 | if (cwdfd == -1) | 
|---|
| 696 | { | 
|---|
| 697 | /* Try getting the directory name.  This can be needed if | 
|---|
| 698 | the current directory is executable but not readable.  */ | 
|---|
| 699 | if (errno == EACCES) | 
|---|
| 700 | /* GNU extension ahead.  */ | 
|---|
| 701 | cwd =  __getcwd (NULL, 0); | 
|---|
| 702 |  | 
|---|
| 703 | if (cwd == NULL) | 
|---|
| 704 | goto out_fail; | 
|---|
| 705 | } | 
|---|
| 706 | else if (data.maxdir > 1) | 
|---|
| 707 | /* Account for the file descriptor we use here.  */ | 
|---|
| 708 | --data.maxdir; | 
|---|
| 709 |  | 
|---|
| 710 | if (data.ftw.base > 0) | 
|---|
| 711 | { | 
|---|
| 712 | /* Change to the directory the file is in.  In data.dirbuf | 
|---|
| 713 | we have a writable copy of the file name.  Just NUL | 
|---|
| 714 | terminate it for now and change the directory.  */ | 
|---|
| 715 | if (data.ftw.base == 1) | 
|---|
| 716 | /* I.e., the file is in the root directory.  */ | 
|---|
| 717 | result = __chdir ( "/"); | 
|---|
| 718 | else | 
|---|
| 719 | { | 
|---|
| 720 | char ch = data.dirbuf[data.ftw.base - 1]; | 
|---|
| 721 | data.dirbuf[data.ftw.base - 1] = '\0'; | 
|---|
| 722 | result = __chdir (data.dirbuf); | 
|---|
| 723 | data.dirbuf[data.ftw.base - 1] = ch; | 
|---|
| 724 | } | 
|---|
| 725 | } | 
|---|
| 726 | } | 
|---|
| 727 |  | 
|---|
| 728 | /* Get stat info for start directory.  */ | 
|---|
| 729 | if (result == 0) | 
|---|
| 730 | { | 
|---|
| 731 | const char *name; | 
|---|
| 732 |  | 
|---|
| 733 | if (data.flags & FTW_CHDIR) | 
|---|
| 734 | { | 
|---|
| 735 | name = data.dirbuf + data.ftw.base; | 
|---|
| 736 | if (name[0] == '\0') | 
|---|
| 737 | name = "."; | 
|---|
| 738 | } | 
|---|
| 739 | else | 
|---|
| 740 | name = data.dirbuf; | 
|---|
| 741 |  | 
|---|
| 742 | if (((flags & FTW_PHYS) | 
|---|
| 743 | ? LXSTAT (_STAT_VER, name, &st) | 
|---|
| 744 | : XSTAT (_STAT_VER, name, &st)) < 0) | 
|---|
| 745 | { | 
|---|
| 746 | if (!(flags & FTW_PHYS) | 
|---|
| 747 | && errno == ENOENT | 
|---|
| 748 | && LXSTAT (_STAT_VER, name, &st) == 0 | 
|---|
| 749 | && S_ISLNK (st.st_mode)) | 
|---|
| 750 | result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN], | 
|---|
| 751 | &data.ftw); | 
|---|
| 752 | else | 
|---|
| 753 | /* No need to call the callback since we cannot say anything | 
|---|
| 754 | about the object.  */ | 
|---|
| 755 | result = -1; | 
|---|
| 756 | } | 
|---|
| 757 | else | 
|---|
| 758 | { | 
|---|
| 759 | if (S_ISDIR (st.st_mode)) | 
|---|
| 760 | { | 
|---|
| 761 | /* Remember the device of the initial directory in case | 
|---|
| 762 | FTW_MOUNT is given.  */ | 
|---|
| 763 | data.dev = st.st_dev; | 
|---|
| 764 |  | 
|---|
| 765 | /* We know this directory now.  */ | 
|---|
| 766 | if (!(flags & FTW_PHYS)) | 
|---|
| 767 | result = add_object (&data, &st); | 
|---|
| 768 |  | 
|---|
| 769 | if (result == 0) | 
|---|
| 770 | result = ftw_dir (&data, &st, NULL); | 
|---|
| 771 | } | 
|---|
| 772 | else | 
|---|
| 773 | { | 
|---|
| 774 | int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F; | 
|---|
| 775 |  | 
|---|
| 776 | result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag], | 
|---|
| 777 | &data.ftw); | 
|---|
| 778 | } | 
|---|
| 779 | } | 
|---|
| 780 |  | 
|---|
| 781 | if ((flags & FTW_ACTIONRETVAL) | 
|---|
| 782 | && (result == FTW_SKIP_SUBTREE || result == FTW_SKIP_SIBLINGS)) | 
|---|
| 783 | result = 0; | 
|---|
| 784 | } | 
|---|
| 785 |  | 
|---|
| 786 | /* Return to the start directory (if necessary).  */ | 
|---|
| 787 | if (cwdfd != -1) | 
|---|
| 788 | { | 
|---|
| 789 | int save_err = errno; | 
|---|
| 790 | __fchdir (cwdfd); | 
|---|
| 791 | __close_nocancel_nostatus (cwdfd); | 
|---|
| 792 | __set_errno (save_err); | 
|---|
| 793 | } | 
|---|
| 794 | else if (cwd != NULL) | 
|---|
| 795 | { | 
|---|
| 796 | int save_err = errno; | 
|---|
| 797 | __chdir (cwd); | 
|---|
| 798 | free (cwd); | 
|---|
| 799 | __set_errno (save_err); | 
|---|
| 800 | } | 
|---|
| 801 |  | 
|---|
| 802 | /* Free all memory.  */ | 
|---|
| 803 | out_fail: | 
|---|
| 804 | save_err = errno; | 
|---|
| 805 | __tdestroy (data.known_objects, free); | 
|---|
| 806 | free (data.dirbuf); | 
|---|
| 807 | __set_errno (save_err); | 
|---|
| 808 |  | 
|---|
| 809 | return result; | 
|---|
| 810 | } | 
|---|
| 811 |  | 
|---|
| 812 |  | 
|---|
| 813 |  | 
|---|
| 814 | /* Entry points.  */ | 
|---|
| 815 |  | 
|---|
| 816 | int | 
|---|
| 817 | FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors) | 
|---|
| 818 | { | 
|---|
| 819 | return ftw_startup (path, 0, func, descriptors, 0); | 
|---|
| 820 | } | 
|---|
| 821 |  | 
|---|
| 822 | #ifndef _LIBC | 
|---|
| 823 | int | 
|---|
| 824 | NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) | 
|---|
| 825 | { | 
|---|
| 826 | return ftw_startup (path, 1, func, descriptors, flags); | 
|---|
| 827 | } | 
|---|
| 828 | #else | 
|---|
| 829 |  | 
|---|
| 830 | # include <shlib-compat.h> | 
|---|
| 831 |  | 
|---|
| 832 | int NFTW_NEW_NAME (const char *, NFTW_FUNC_T, int, int); | 
|---|
| 833 |  | 
|---|
| 834 | int | 
|---|
| 835 | NFTW_NEW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) | 
|---|
| 836 | { | 
|---|
| 837 | if (flags | 
|---|
| 838 | & ~(FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH | FTW_ACTIONRETVAL)) | 
|---|
| 839 | { | 
|---|
| 840 | __set_errno (EINVAL); | 
|---|
| 841 | return -1; | 
|---|
| 842 | } | 
|---|
| 843 | return ftw_startup (path, 1, func, descriptors, flags); | 
|---|
| 844 | } | 
|---|
| 845 |  | 
|---|
| 846 | versioned_symbol (libc, NFTW_NEW_NAME, NFTW_NAME, GLIBC_2_3_3); | 
|---|
| 847 |  | 
|---|
| 848 | # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_3_3) | 
|---|
| 849 |  | 
|---|
| 850 | /* Older nftw* version just ignored all unknown flags.  */ | 
|---|
| 851 |  | 
|---|
| 852 | int NFTW_OLD_NAME (const char *, NFTW_FUNC_T, int, int); | 
|---|
| 853 |  | 
|---|
| 854 | int | 
|---|
| 855 | attribute_compat_text_section | 
|---|
| 856 | NFTW_OLD_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags) | 
|---|
| 857 | { | 
|---|
| 858 | flags &= (FTW_PHYS | FTW_MOUNT | FTW_CHDIR | FTW_DEPTH); | 
|---|
| 859 | return ftw_startup (path, 1, func, descriptors, flags); | 
|---|
| 860 | } | 
|---|
| 861 |  | 
|---|
| 862 | compat_symbol (libc, NFTW_OLD_NAME, NFTW_NAME, GLIBC_2_1); | 
|---|
| 863 | # endif | 
|---|
| 864 | #endif | 
|---|
| 865 |  | 
|---|