ftw.c 19 KB

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