ftw.c 20 KB

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