ftw.c 19 KB

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