fts.c 30 KB

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