ftw.c 20 KB

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