fts.c 30 KB

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