fts.c 30 KB

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