fts.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <dirent.h>
  33. #include <errno.h>
  34. #include <fts.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #ifdef __UCLIBC_HAS_LFS__
  39. /* this is wrong, either you include this header as first, or not at all */
  40. # include <_lfs_64.h>
  41. #else
  42. # define stat64 stat
  43. # define fstat64 fstat
  44. #endif
  45. /* Largest alignment size needed, minus one.
  46. Usually long double is the worst case. */
  47. #ifndef ALIGNBYTES
  48. #define ALIGNBYTES (__alignof__ (long double) - 1)
  49. #endif
  50. /* Align P to that size. */
  51. #ifndef ALIGN
  52. #define ALIGN(p) (((unsigned long int) (p) + ALIGNBYTES) & ~ALIGNBYTES)
  53. #endif
  54. static FTSENT *fts_alloc (FTS *, const char *, size_t) internal_function;
  55. static FTSENT *fts_build (FTS *, int) internal_function;
  56. static void fts_lfree (FTSENT *) internal_function;
  57. static void fts_load (FTS *, FTSENT *) internal_function;
  58. static size_t fts_maxarglen (char * const *) internal_function;
  59. static void fts_padjust (FTS *, FTSENT *) internal_function;
  60. static int fts_palloc (FTS *, size_t) internal_function;
  61. static FTSENT *fts_sort (FTS *, FTSENT *, int) internal_function;
  62. static u_short fts_stat (FTS *, FTSENT *, int) internal_function;
  63. static int fts_safe_changedir (FTS *, FTSENT *, int, const char *)
  64. internal_function;
  65. #ifndef MAX
  66. #define MAX(a, b) ({ __typeof__ (a) _a = (a); \
  67. __typeof__ (b) _b = (b); \
  68. _a > _b ? _a : _b; })
  69. #endif
  70. #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
  71. #define CLR(opt) (sp->fts_options &= ~(opt))
  72. #define ISSET(opt) (sp->fts_options & (opt))
  73. #define SET(opt) (sp->fts_options |= (opt))
  74. #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd))
  75. /* fts_build flags */
  76. #define BCHILD 1 /* fts_children */
  77. #define BNAMES 2 /* fts_children, names only */
  78. #define BREAD 3 /* fts_read */
  79. FTS *
  80. fts_open( char * const *argv, register int options,
  81. int (*compar) (const FTSENT **, const FTSENT **))
  82. {
  83. register FTS *sp;
  84. register FTSENT *p, *root;
  85. register int nitems;
  86. FTSENT *parent = NULL;
  87. FTSENT *tmp = NULL;
  88. /* Options check. */
  89. if (options & ~FTS_OPTIONMASK) {
  90. __set_errno (EINVAL);
  91. return (NULL);
  92. }
  93. /* Allocate/initialize the stream */
  94. if ((sp = malloc((u_int)sizeof(FTS))) == NULL)
  95. return (NULL);
  96. memset(sp, 0, sizeof(FTS));
  97. sp->fts_compar = (int (*) (const void *, const void *)) compar;
  98. sp->fts_options = options;
  99. /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
  100. if (ISSET(FTS_LOGICAL))
  101. SET(FTS_NOCHDIR);
  102. /*
  103. * Start out with 1K of path space, and enough, in any case,
  104. * to hold the user's paths.
  105. */
  106. #ifndef MAXPATHLEN
  107. #define MAXPATHLEN 1024
  108. #endif
  109. size_t maxarglen = fts_maxarglen(argv);
  110. if (fts_palloc(sp, MAX(maxarglen, MAXPATHLEN)))
  111. goto mem1;
  112. /* Allocate/initialize root's parent. */
  113. if (*argv != NULL) {
  114. if ((parent = fts_alloc(sp, "", 0)) == NULL)
  115. goto mem2;
  116. parent->fts_level = FTS_ROOTPARENTLEVEL;
  117. }
  118. /* Allocate/initialize root(s). */
  119. for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) {
  120. /* Don't allow zero-length paths. */
  121. size_t len = strlen(*argv);
  122. if (len == 0) {
  123. __set_errno (ENOENT);
  124. goto mem3;
  125. }
  126. p = fts_alloc(sp, *argv, len);
  127. p->fts_level = FTS_ROOTLEVEL;
  128. p->fts_parent = parent;
  129. p->fts_accpath = p->fts_name;
  130. p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
  131. /* Command-line "." and ".." are real directories. */
  132. if (p->fts_info == FTS_DOT)
  133. p->fts_info = FTS_D;
  134. /*
  135. * If comparison routine supplied, traverse in sorted
  136. * order; otherwise traverse in the order specified.
  137. */
  138. if (compar) {
  139. p->fts_link = root;
  140. root = p;
  141. } else {
  142. p->fts_link = NULL;
  143. if (root == NULL)
  144. tmp = root = p;
  145. else {
  146. tmp->fts_link = p;
  147. tmp = p;
  148. }
  149. }
  150. }
  151. if (compar && nitems > 1)
  152. root = fts_sort(sp, root, nitems);
  153. /*
  154. * Allocate a dummy pointer and make fts_read think that we've just
  155. * finished the node before the root(s); set p->fts_info to FTS_INIT
  156. * so that everything about the "current" node is ignored.
  157. */
  158. if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL)
  159. goto mem3;
  160. sp->fts_cur->fts_link = root;
  161. sp->fts_cur->fts_info = FTS_INIT;
  162. /*
  163. * If using chdir(2), grab a file descriptor pointing to dot to ensure
  164. * that we can get back here; this could be avoided for some paths,
  165. * but almost certainly not worth the effort. Slashes, symbolic links,
  166. * and ".." are all fairly nasty problems. Note, if we can't get the
  167. * descriptor we run anyway, just more slowly.
  168. */
  169. if (!ISSET(FTS_NOCHDIR)
  170. && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
  171. SET(FTS_NOCHDIR);
  172. return (sp);
  173. mem3: fts_lfree(root);
  174. free(parent);
  175. mem2: free(sp->fts_path);
  176. mem1: free(sp);
  177. return (NULL);
  178. }
  179. static void
  180. internal_function
  181. fts_load(FTS *sp, register FTSENT *p)
  182. {
  183. register int len;
  184. register char *cp;
  185. /*
  186. * Load the stream structure for the next traversal. Since we don't
  187. * actually enter the directory until after the preorder visit, set
  188. * the fts_accpath field specially so the chdir gets done to the right
  189. * place and the user can access the first node. From fts_open it's
  190. * known that the path will fit.
  191. */
  192. len = p->fts_pathlen = p->fts_namelen;
  193. memmove(sp->fts_path, p->fts_name, len + 1);
  194. if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
  195. len = strlen(++cp);
  196. memmove(p->fts_name, cp, len + 1);
  197. p->fts_namelen = len;
  198. }
  199. p->fts_accpath = p->fts_path = sp->fts_path;
  200. sp->fts_dev = p->fts_dev;
  201. }
  202. int
  203. fts_close(FTS *sp)
  204. {
  205. register FTSENT *freep, *p;
  206. int saved_errno;
  207. /*
  208. * This still works if we haven't read anything -- the dummy structure
  209. * points to the root list, so we step through to the end of the root
  210. * list which has a valid parent pointer.
  211. */
  212. if (sp->fts_cur) {
  213. for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
  214. freep = p;
  215. p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
  216. free(freep);
  217. }
  218. free(p);
  219. }
  220. /* Free up child linked list, sort array, path buffer. */
  221. if (sp->fts_child)
  222. fts_lfree(sp->fts_child);
  223. free(sp->fts_array);
  224. free(sp->fts_path);
  225. /* Return to original directory, save errno if necessary. */
  226. if (!ISSET(FTS_NOCHDIR)) {
  227. saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
  228. (void)close(sp->fts_rfd);
  229. /* Set errno and return. */
  230. if (saved_errno != 0) {
  231. /* Free up the stream pointer. */
  232. free(sp);
  233. __set_errno (saved_errno);
  234. return (-1);
  235. }
  236. }
  237. /* Free up the stream pointer. */
  238. free(sp);
  239. return (0);
  240. }
  241. /*
  242. * Special case of "/" at the end of the path so that slashes aren't
  243. * appended which would cause paths to be written as "....//foo".
  244. */
  245. #define NAPPEND(p) \
  246. (p->fts_path[p->fts_pathlen - 1] == '/' \
  247. ? p->fts_pathlen - 1 : p->fts_pathlen)
  248. FTSENT *
  249. fts_read(register FTS *sp)
  250. {
  251. register FTSENT *p, *tmp;
  252. register int instr;
  253. register char *t;
  254. int saved_errno;
  255. /* If finished or unrecoverable error, return NULL. */
  256. if (sp->fts_cur == NULL || ISSET(FTS_STOP))
  257. return (NULL);
  258. /* Set current node pointer. */
  259. p = sp->fts_cur;
  260. /* Save and zero out user instructions. */
  261. instr = p->fts_instr;
  262. p->fts_instr = FTS_NOINSTR;
  263. /* Any type of file may be re-visited; re-stat and re-turn. */
  264. if (instr == FTS_AGAIN) {
  265. p->fts_info = fts_stat(sp, p, 0);
  266. return (p);
  267. }
  268. /*
  269. * Following a symlink -- SLNONE test allows application to see
  270. * SLNONE and recover. If indirecting through a symlink, have
  271. * keep a pointer to current location. If unable to get that
  272. * pointer, follow fails.
  273. */
  274. if (instr == FTS_FOLLOW &&
  275. (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
  276. p->fts_info = fts_stat(sp, p, 1);
  277. if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
  278. if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
  279. p->fts_errno = errno;
  280. p->fts_info = FTS_ERR;
  281. } else
  282. p->fts_flags |= FTS_SYMFOLLOW;
  283. }
  284. return (p);
  285. }
  286. /* Directory in pre-order. */
  287. if (p->fts_info == FTS_D) {
  288. /* If skipped or crossed mount point, do post-order visit. */
  289. if (instr == FTS_SKIP ||
  290. (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
  291. if (p->fts_flags & FTS_SYMFOLLOW)
  292. (void)close(p->fts_symfd);
  293. if (sp->fts_child) {
  294. fts_lfree(sp->fts_child);
  295. sp->fts_child = NULL;
  296. }
  297. p->fts_info = FTS_DP;
  298. return (p);
  299. }
  300. /* Rebuild if only read the names and now traversing. */
  301. if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) {
  302. CLR(FTS_NAMEONLY);
  303. fts_lfree(sp->fts_child);
  304. sp->fts_child = NULL;
  305. }
  306. /*
  307. * Cd to the subdirectory.
  308. *
  309. * If have already read and now fail to chdir, whack the list
  310. * to make the names come out right, and set the parent errno
  311. * so the application will eventually get an error condition.
  312. * Set the FTS_DONTCHDIR flag so that when we logically change
  313. * directories back to the parent we don't do a chdir.
  314. *
  315. * If haven't read do so. If the read fails, fts_build sets
  316. * FTS_STOP or the fts_info field of the node.
  317. */
  318. if (sp->fts_child != NULL) {
  319. if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
  320. p->fts_errno = errno;
  321. p->fts_flags |= FTS_DONTCHDIR;
  322. for (p = sp->fts_child; p != NULL;
  323. p = p->fts_link)
  324. p->fts_accpath =
  325. p->fts_parent->fts_accpath;
  326. }
  327. } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) {
  328. if (ISSET(FTS_STOP))
  329. return (NULL);
  330. return (p);
  331. }
  332. p = sp->fts_child;
  333. sp->fts_child = NULL;
  334. sp->fts_cur = p;
  335. goto name;
  336. }
  337. /* Move to the next node on this level. */
  338. next: tmp = p;
  339. if ((p = p->fts_link) != NULL) {
  340. sp->fts_cur = p;
  341. free(tmp);
  342. /*
  343. * If reached the top, return to the original directory (or
  344. * the root of the tree), and load the paths for the next root.
  345. */
  346. if (p->fts_level == FTS_ROOTLEVEL) {
  347. if (FCHDIR(sp, sp->fts_rfd)) {
  348. SET(FTS_STOP);
  349. return (NULL);
  350. }
  351. fts_load(sp, p);
  352. return p;
  353. }
  354. /*
  355. * User may have called fts_set on the node. If skipped,
  356. * ignore. If followed, get a file descriptor so we can
  357. * get back if necessary.
  358. */
  359. if (p->fts_instr == FTS_SKIP)
  360. goto next;
  361. if (p->fts_instr == FTS_FOLLOW) {
  362. p->fts_info = fts_stat(sp, p, 1);
  363. if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
  364. if ((p->fts_symfd =
  365. open(".", O_RDONLY, 0)) < 0) {
  366. p->fts_errno = errno;
  367. p->fts_info = FTS_ERR;
  368. } else
  369. p->fts_flags |= FTS_SYMFOLLOW;
  370. }
  371. p->fts_instr = FTS_NOINSTR;
  372. }
  373. name: t = sp->fts_path + NAPPEND(p->fts_parent);
  374. *t++ = '/';
  375. memmove(t, p->fts_name, p->fts_namelen + 1);
  376. return p;
  377. }
  378. /* Move up to the parent node. */
  379. p = tmp->fts_parent;
  380. sp->fts_cur = p;
  381. free(tmp);
  382. if (p->fts_level == FTS_ROOTPARENTLEVEL) {
  383. /*
  384. * Done; free everything up and set errno to 0 so the user
  385. * can distinguish between error and EOF.
  386. */
  387. free(p);
  388. __set_errno (0);
  389. return (sp->fts_cur = NULL);
  390. }
  391. /* NUL terminate the pathname. */
  392. sp->fts_path[p->fts_pathlen] = '\0';
  393. /*
  394. * Return to the parent directory. If at a root node or came through
  395. * a symlink, go back through the file descriptor. Otherwise, cd up
  396. * one directory.
  397. */
  398. if (p->fts_level == FTS_ROOTLEVEL) {
  399. if (FCHDIR(sp, sp->fts_rfd)) {
  400. SET(FTS_STOP);
  401. return (NULL);
  402. }
  403. } else if (p->fts_flags & FTS_SYMFOLLOW) {
  404. if (FCHDIR(sp, p->fts_symfd)) {
  405. saved_errno = errno;
  406. (void)close(p->fts_symfd);
  407. __set_errno (saved_errno);
  408. SET(FTS_STOP);
  409. return (NULL);
  410. }
  411. (void)close(p->fts_symfd);
  412. } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
  413. fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
  414. SET(FTS_STOP);
  415. return (NULL);
  416. }
  417. p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
  418. return p;
  419. }
  420. /*
  421. * Fts_set takes the stream as an argument although it's not used in this
  422. * implementation; it would be necessary if anyone wanted to add global
  423. * semantics to fts using fts_set. An error return is allowed for similar
  424. * reasons.
  425. */
  426. /* ARGSUSED */
  427. int
  428. fts_set(FTS *sp, FTSENT *p, int instr)
  429. {
  430. if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
  431. instr != FTS_NOINSTR && instr != FTS_SKIP) {
  432. __set_errno (EINVAL);
  433. return (1);
  434. }
  435. p->fts_instr = instr;
  436. return (0);
  437. }
  438. FTSENT *
  439. fts_children(register FTS *sp, int instr)
  440. {
  441. register FTSENT *p;
  442. int fd;
  443. if (instr != 0 && instr != FTS_NAMEONLY) {
  444. __set_errno (EINVAL);
  445. return (NULL);
  446. }
  447. /* Set current node pointer. */
  448. p = sp->fts_cur;
  449. /*
  450. * Errno set to 0 so user can distinguish empty directory from
  451. * an error.
  452. */
  453. __set_errno (0);
  454. /* Fatal errors stop here. */
  455. if (ISSET(FTS_STOP))
  456. return (NULL);
  457. /* Return logical hierarchy of user's arguments. */
  458. if (p->fts_info == FTS_INIT)
  459. return (p->fts_link);
  460. /*
  461. * If not a directory being visited in pre-order, stop here. Could
  462. * allow FTS_DNR, assuming the user has fixed the problem, but the
  463. * same effect is available with FTS_AGAIN.
  464. */
  465. if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
  466. return (NULL);
  467. /* Free up any previous child list. */
  468. if (sp->fts_child != NULL)
  469. fts_lfree(sp->fts_child);
  470. if (instr == FTS_NAMEONLY) {
  471. SET(FTS_NAMEONLY);
  472. instr = BNAMES;
  473. } else
  474. instr = BCHILD;
  475. /*
  476. * If using chdir on a relative path and called BEFORE fts_read does
  477. * its chdir to the root of a traversal, we can lose -- we need to
  478. * chdir into the subdirectory, and we don't know where the current
  479. * directory is, so we can't get back so that the upcoming chdir by
  480. * fts_read will work.
  481. */
  482. if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
  483. ISSET(FTS_NOCHDIR))
  484. return (sp->fts_child = fts_build(sp, instr));
  485. if ((fd = open(".", O_RDONLY, 0)) < 0)
  486. return (NULL);
  487. sp->fts_child = fts_build(sp, instr);
  488. if (fchdir(fd))
  489. return (NULL);
  490. (void)close(fd);
  491. return (sp->fts_child);
  492. }
  493. /*
  494. * This is the tricky part -- do not casually change *anything* in here. The
  495. * idea is to build the linked list of entries that are used by fts_children
  496. * and fts_read. There are lots of special cases.
  497. *
  498. * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
  499. * set and it's a physical walk (so that symbolic links can't be directories),
  500. * we can do things quickly. First, if it's a 4.4BSD file system, the type
  501. * of the file is in the directory entry. Otherwise, we assume that the number
  502. * of subdirectories in a node is equal to the number of links to the parent.
  503. * The former skips all stat calls. The latter skips stat calls in any leaf
  504. * directories and for any files after the subdirectories in the directory have
  505. * been found, cutting the stat calls by about 2/3.
  506. */
  507. static FTSENT *
  508. internal_function
  509. fts_build(register FTS *sp, int type)
  510. {
  511. register struct dirent *dp;
  512. register FTSENT *p, *head;
  513. register int nitems;
  514. FTSENT *cur, *tail;
  515. DIR *dirp;
  516. void *oldaddr;
  517. int /*cderrno,*/ descend, len, level, nlinks, saved_errno,
  518. nostat, doadjust;
  519. size_t maxlen;
  520. char *cp;
  521. /* Set current node pointer. */
  522. cur = sp->fts_cur;
  523. /*
  524. * Open the directory for reading. If this fails, we're done.
  525. * If being called from fts_read, set the fts_info field.
  526. */
  527. #if defined FTS_WHITEOUT && 0
  528. if (ISSET(FTS_WHITEOUT))
  529. oflag = DTF_NODUP|DTF_REWIND;
  530. else
  531. oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND;
  532. #else
  533. # define opendir2(path, flag) opendir(path)
  534. #endif
  535. if ((dirp = opendir2(cur->fts_accpath, oflag)) == NULL) {
  536. if (type == BREAD) {
  537. cur->fts_info = FTS_DNR;
  538. cur->fts_errno = errno;
  539. }
  540. return (NULL);
  541. }
  542. /*
  543. * Nlinks is the number of possible entries of type directory in the
  544. * directory if we're cheating on stat calls, 0 if we're not doing
  545. * any stat calls at all, -1 if we're doing stats on everything.
  546. */
  547. if (type == BNAMES) {
  548. nlinks = 0;
  549. /* Be quiet about nostat, GCC. */
  550. nostat = 0;
  551. } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
  552. nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
  553. nostat = 1;
  554. } else {
  555. nlinks = -1;
  556. nostat = 0;
  557. }
  558. #ifdef notdef
  559. (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
  560. (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
  561. ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
  562. #endif
  563. /*
  564. * If we're going to need to stat anything or we want to descend
  565. * and stay in the directory, chdir. If this fails we keep going,
  566. * but set a flag so we don't chdir after the post-order visit.
  567. * We won't be able to stat anything, but we can still return the
  568. * names themselves. Note, that since fts_read won't be able to
  569. * chdir into the directory, it will have to return different path
  570. * names than before, i.e. "a/b" instead of "b". Since the node
  571. * has already been visited in pre-order, have to wait until the
  572. * post-order visit to return the error. There is a special case
  573. * here, if there was nothing to stat then it's not an error to
  574. * not be able to stat. This is all fairly nasty. If a program
  575. * needed sorted entries or stat information, they had better be
  576. * checking FTS_NS on the returned nodes.
  577. */
  578. /* cderrno = 0; */
  579. if (nlinks || type == BREAD) {
  580. if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
  581. if (nlinks && type == BREAD)
  582. cur->fts_errno = errno;
  583. cur->fts_flags |= FTS_DONTCHDIR;
  584. descend = 0;
  585. /* cderrno = errno; */
  586. (void)closedir(dirp);
  587. dirp = NULL;
  588. } else
  589. descend = 1;
  590. } else
  591. descend = 0;
  592. /*
  593. * Figure out the max file name length that can be stored in the
  594. * current path -- the inner loop allocates more path as necessary.
  595. * We really wouldn't have to do the maxlen calculations here, we
  596. * could do them in fts_read before returning the path, but it's a
  597. * lot easier here since the length is part of the dirent structure.
  598. *
  599. * If not changing directories set a pointer so that can just append
  600. * each new name into the path.
  601. */
  602. len = NAPPEND(cur);
  603. if (ISSET(FTS_NOCHDIR)) {
  604. cp = sp->fts_path + len;
  605. *cp++ = '/';
  606. } else {
  607. /* GCC, you're too verbose. */
  608. cp = NULL;
  609. }
  610. len++;
  611. maxlen = sp->fts_pathlen - len;
  612. level = cur->fts_level + 1;
  613. /* Read the directory, attaching each entry to the `link' pointer. */
  614. doadjust = 0;
  615. for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
  616. if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name))
  617. continue;
  618. if ((p = fts_alloc(sp, dp->d_name, _D_EXACT_NAMLEN (dp))) == NULL)
  619. goto mem1;
  620. if (_D_EXACT_NAMLEN (dp) >= maxlen) {/* include space for NUL */
  621. oldaddr = sp->fts_path;
  622. if (fts_palloc(sp, _D_EXACT_NAMLEN (dp) + len + 1)) {
  623. /*
  624. * No more memory for path or structures. Save
  625. * errno, free up the current structure and the
  626. * structures already allocated.
  627. */
  628. mem1: saved_errno = errno;
  629. free(p);
  630. fts_lfree(head);
  631. (void)closedir(dirp);
  632. cur->fts_info = FTS_ERR;
  633. SET(FTS_STOP);
  634. __set_errno (saved_errno);
  635. return (NULL);
  636. }
  637. /* Did realloc() change the pointer? */
  638. if (oldaddr != sp->fts_path) {
  639. doadjust = 1;
  640. if (ISSET(FTS_NOCHDIR))
  641. cp = sp->fts_path + len;
  642. }
  643. maxlen = sp->fts_pathlen - len;
  644. }
  645. if (len + _D_EXACT_NAMLEN (dp) >= USHRT_MAX) {
  646. /*
  647. * In an FTSENT, fts_pathlen is a u_short so it is
  648. * possible to wraparound here. If we do, free up
  649. * the current structure and the structures already
  650. * allocated, then error out with ENAMETOOLONG.
  651. */
  652. free(p);
  653. fts_lfree(head);
  654. (void)closedir(dirp);
  655. cur->fts_info = FTS_ERR;
  656. SET(FTS_STOP);
  657. __set_errno (ENAMETOOLONG);
  658. return (NULL);
  659. }
  660. p->fts_level = level;
  661. p->fts_parent = sp->fts_cur;
  662. p->fts_pathlen = len + _D_EXACT_NAMLEN (dp);
  663. #if defined FTS_WHITEOUT && 0
  664. if (dp->d_type == DT_WHT)
  665. p->fts_flags |= FTS_ISW;
  666. #endif
  667. #if 0
  668. /* Unreachable code. cderrno is only ever set to a nonnull
  669. value if dirp is closed at the same time. But then we
  670. cannot enter this loop. */
  671. if (cderrno) {
  672. if (nlinks) {
  673. p->fts_info = FTS_NS;
  674. p->fts_errno = cderrno;
  675. } else
  676. p->fts_info = FTS_NSOK;
  677. p->fts_accpath = cur->fts_accpath;
  678. } else
  679. #endif
  680. if (nlinks == 0
  681. #if defined DT_DIR && defined _DIRENT_HAVE_D_TYPE
  682. || (nostat &&
  683. dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
  684. #endif
  685. ) {
  686. p->fts_accpath =
  687. ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
  688. p->fts_info = FTS_NSOK;
  689. } else {
  690. /* Build a file name for fts_stat to stat. */
  691. if (ISSET(FTS_NOCHDIR)) {
  692. p->fts_accpath = p->fts_path;
  693. memmove(cp, p->fts_name, p->fts_namelen + 1);
  694. } else
  695. p->fts_accpath = p->fts_name;
  696. /* Stat it. */
  697. p->fts_info = fts_stat(sp, p, 0);
  698. /* Decrement link count if applicable. */
  699. if (nlinks > 0 && (p->fts_info == FTS_D ||
  700. p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
  701. --nlinks;
  702. }
  703. /* We walk in directory order so "ls -f" doesn't get upset. */
  704. p->fts_link = NULL;
  705. if (head == NULL)
  706. head = tail = p;
  707. else {
  708. tail->fts_link = p;
  709. tail = p;
  710. }
  711. ++nitems;
  712. }
  713. if (dirp)
  714. (void)closedir(dirp);
  715. /*
  716. * If realloc() changed the address of the path, adjust the
  717. * addresses for the rest of the tree and the dir list.
  718. */
  719. if (doadjust)
  720. fts_padjust(sp, head);
  721. /*
  722. * If not changing directories, reset the path back to original
  723. * state.
  724. */
  725. if (ISSET(FTS_NOCHDIR)) {
  726. if (len == sp->fts_pathlen || nitems == 0)
  727. --cp;
  728. *cp = '\0';
  729. }
  730. /*
  731. * If descended after called from fts_children or after called from
  732. * fts_read and nothing found, get back. At the root level we use
  733. * the saved fd; if one of fts_open()'s arguments is a relative path
  734. * to an empty directory, we wind up here with no other way back. If
  735. * can't get back, we're done.
  736. */
  737. if (descend && (type == BCHILD || !nitems) &&
  738. (cur->fts_level == FTS_ROOTLEVEL ?
  739. FCHDIR(sp, sp->fts_rfd) :
  740. fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
  741. cur->fts_info = FTS_ERR;
  742. SET(FTS_STOP);
  743. fts_lfree(head);
  744. return (NULL);
  745. }
  746. /* If didn't find anything, return NULL. */
  747. if (!nitems) {
  748. if (type == BREAD)
  749. cur->fts_info = FTS_DP;
  750. fts_lfree(head);
  751. return (NULL);
  752. }
  753. /* Sort the entries. */
  754. if (sp->fts_compar && nitems > 1)
  755. head = fts_sort(sp, head, nitems);
  756. return (head);
  757. }
  758. static u_short
  759. internal_function
  760. fts_stat(FTS *sp, register FTSENT *p, int follow)
  761. {
  762. register FTSENT *t;
  763. register dev_t dev;
  764. register ino_t ino;
  765. struct stat *sbp, sb;
  766. int saved_errno;
  767. /* If user needs stat info, stat buffer already allocated. */
  768. sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
  769. #if defined FTS_WHITEOUT && 0
  770. /* check for whiteout */
  771. if (p->fts_flags & FTS_ISW) {
  772. if (sbp != &sb) {
  773. memset(sbp, '\0', sizeof (*sbp));
  774. sbp->st_mode = S_IFWHT;
  775. }
  776. return (FTS_W);
  777. }
  778. #endif
  779. /*
  780. * If doing a logical walk, or application requested FTS_FOLLOW, do
  781. * a stat(2). If that fails, check for a non-existent symlink. If
  782. * fail, set the errno from the stat call.
  783. */
  784. if (ISSET(FTS_LOGICAL) || follow) {
  785. if (stat(p->fts_accpath, sbp)) {
  786. saved_errno = errno;
  787. if (!lstat(p->fts_accpath, sbp)) {
  788. __set_errno (0);
  789. return (FTS_SLNONE);
  790. }
  791. p->fts_errno = saved_errno;
  792. goto err;
  793. }
  794. } else if (lstat(p->fts_accpath, sbp)) {
  795. p->fts_errno = errno;
  796. err: memset(sbp, 0, sizeof(struct stat));
  797. return (FTS_NS);
  798. }
  799. if (S_ISDIR(sbp->st_mode)) {
  800. /*
  801. * Set the device/inode. Used to find cycles and check for
  802. * crossing mount points. Also remember the link count, used
  803. * in fts_build to limit the number of stat calls. It is
  804. * understood that these fields are only referenced if fts_info
  805. * is set to FTS_D.
  806. */
  807. dev = p->fts_dev = sbp->st_dev;
  808. ino = p->fts_ino = sbp->st_ino;
  809. p->fts_nlink = sbp->st_nlink;
  810. if (ISDOT(p->fts_name))
  811. return (FTS_DOT);
  812. /*
  813. * Cycle detection is done by brute force when the directory
  814. * is first encountered. If the tree gets deep enough or the
  815. * number of symbolic links to directories is high enough,
  816. * something faster might be worthwhile.
  817. */
  818. for (t = p->fts_parent;
  819. t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
  820. if (ino == t->fts_ino && dev == t->fts_dev) {
  821. p->fts_cycle = t;
  822. return (FTS_DC);
  823. }
  824. return (FTS_D);
  825. }
  826. if (S_ISLNK(sbp->st_mode))
  827. return (FTS_SL);
  828. if (S_ISREG(sbp->st_mode))
  829. return (FTS_F);
  830. return (FTS_DEFAULT);
  831. }
  832. static FTSENT *
  833. internal_function
  834. fts_sort(FTS *sp, FTSENT *head, register int nitems)
  835. {
  836. register FTSENT **ap, *p;
  837. /*
  838. * Construct an array of pointers to the structures and call qsort(3).
  839. * Reassemble the array in the order returned by qsort. If unable to
  840. * sort for memory reasons, return the directory entries in their
  841. * current order. Allocate enough space for the current needs plus
  842. * 40 so don't realloc one entry at a time.
  843. */
  844. if (nitems > sp->fts_nitems) {
  845. struct _ftsent **a;
  846. sp->fts_nitems = nitems + 40;
  847. if ((a = realloc(sp->fts_array,
  848. (size_t)(sp->fts_nitems * sizeof(FTSENT *)))) == NULL) {
  849. free(sp->fts_array);
  850. sp->fts_array = NULL;
  851. sp->fts_nitems = 0;
  852. return (head);
  853. }
  854. sp->fts_array = a;
  855. }
  856. for (ap = sp->fts_array, p = head; p; p = p->fts_link)
  857. *ap++ = p;
  858. qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar);
  859. for (head = *(ap = sp->fts_array); --nitems; ++ap)
  860. ap[0]->fts_link = ap[1];
  861. ap[0]->fts_link = NULL;
  862. return (head);
  863. }
  864. static FTSENT *
  865. internal_function
  866. fts_alloc(FTS *sp, const char *name, size_t namelen)
  867. {
  868. register FTSENT *p;
  869. size_t len;
  870. /*
  871. * The file name is a variable length array and no stat structure is
  872. * necessary if the user has set the nostat bit. Allocate the FTSENT
  873. * structure, the file name and the stat structure in one chunk, but
  874. * be careful that the stat structure is reasonably aligned. Since the
  875. * fts_name field is declared to be of size 1, the fts_name pointer is
  876. * namelen + 2 before the first possible address of the stat structure.
  877. */
  878. len = sizeof(FTSENT) + namelen;
  879. if (!ISSET(FTS_NOSTAT))
  880. len += sizeof(struct stat) + ALIGNBYTES;
  881. if ((p = malloc(len)) == NULL)
  882. return (NULL);
  883. /* Copy the name and guarantee NUL termination. */
  884. memmove(p->fts_name, name, namelen);
  885. p->fts_name[namelen] = '\0';
  886. if (!ISSET(FTS_NOSTAT))
  887. p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2);
  888. p->fts_namelen = namelen;
  889. p->fts_path = sp->fts_path;
  890. p->fts_errno = 0;
  891. p->fts_flags = 0;
  892. p->fts_instr = FTS_NOINSTR;
  893. p->fts_number = 0;
  894. p->fts_pointer = NULL;
  895. return (p);
  896. }
  897. static void
  898. internal_function
  899. fts_lfree(register FTSENT *head)
  900. {
  901. register FTSENT *p;
  902. /* Free a linked list of structures. */
  903. while ((p = head)) {
  904. head = head->fts_link;
  905. free(p);
  906. }
  907. }
  908. /*
  909. * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
  910. * Most systems will allow creation of paths much longer than MAXPATHLEN, even
  911. * though the kernel won't resolve them. Add the size (not just what's needed)
  912. * plus 256 bytes so don't realloc the path 2 bytes at a time.
  913. */
  914. static int
  915. internal_function
  916. fts_palloc(FTS *sp, size_t more)
  917. {
  918. char *p;
  919. sp->fts_pathlen += more + 256;
  920. /*
  921. * Check for possible wraparound. In an FTS, fts_pathlen is
  922. * a signed int but in an FTSENT it is an unsigned short.
  923. * We limit fts_pathlen to USHRT_MAX to be safe in both cases.
  924. */
  925. if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) {
  926. free(sp->fts_path);
  927. sp->fts_path = NULL;
  928. __set_errno (ENAMETOOLONG);
  929. return (1);
  930. }
  931. p = realloc(sp->fts_path, sp->fts_pathlen);
  932. if (p == NULL) {
  933. free(sp->fts_path);
  934. sp->fts_path = NULL;
  935. return 1;
  936. }
  937. sp->fts_path = p;
  938. return 0;
  939. }
  940. /*
  941. * When the path is realloc'd, have to fix all of the pointers in structures
  942. * already returned.
  943. */
  944. static void
  945. internal_function
  946. fts_padjust(FTS *sp, FTSENT *head)
  947. {
  948. FTSENT *p;
  949. char *addr = sp->fts_path;
  950. #define ADJUST(p) do { \
  951. if ((p)->fts_accpath != (p)->fts_name) { \
  952. (p)->fts_accpath = \
  953. (char *)addr + ((p)->fts_accpath - (p)->fts_path); \
  954. } \
  955. (p)->fts_path = addr; \
  956. } while (0)
  957. /* Adjust the current set of children. */
  958. for (p = sp->fts_child; p; p = p->fts_link)
  959. ADJUST(p);
  960. /* Adjust the rest of the tree, including the current level. */
  961. for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
  962. ADJUST(p);
  963. p = p->fts_link ? p->fts_link : p->fts_parent;
  964. }
  965. }
  966. static size_t
  967. internal_function
  968. fts_maxarglen(char * const *argv)
  969. {
  970. size_t len, max;
  971. for (max = 0; *argv; ++argv)
  972. if ((len = strlen(*argv)) > max)
  973. max = len;
  974. return (max + 1);
  975. }
  976. /*
  977. * Change to dir specified by fd or p->fts_accpath without getting
  978. * tricked by someone changing the world out from underneath us.
  979. * Assumes p->fts_dev and p->fts_ino are filled in.
  980. */
  981. static int
  982. internal_function
  983. fts_safe_changedir(FTS *sp, FTSENT *p, int fd, const char *path)
  984. {
  985. int ret, oerrno, newfd;
  986. struct stat64 sb;
  987. newfd = fd;
  988. if (ISSET(FTS_NOCHDIR))
  989. return (0);
  990. if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
  991. return (-1);
  992. if (fstat64(newfd, &sb)) {
  993. ret = -1;
  994. goto bail;
  995. }
  996. if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
  997. __set_errno (ENOENT); /* disinformation */
  998. ret = -1;
  999. goto bail;
  1000. }
  1001. ret = fchdir(newfd);
  1002. bail:
  1003. oerrno = errno;
  1004. if (fd < 0)
  1005. (void)close(newfd);
  1006. __set_errno (oerrno);
  1007. return (ret);
  1008. }