ftw.c 20 KB

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