fts.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $
  34. */
  35. #include <util/memory/tempbuf.h>
  36. #include <util/system/compat.h>
  37. #include <util/system/compiler.h>
  38. #include <util/system/defaults.h>
  39. #include <util/system/error.h>
  40. #include <stdlib.h>
  41. #ifndef _win_
  42. #include <inttypes.h>
  43. #include <sys/param.h>
  44. #include <dirent.h>
  45. #include <errno.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #else
  49. #include <direct.h>
  50. #include "dirent_win.h"
  51. #include "lstat_win.h"
  52. #endif
  53. #include <sys/stat.h>
  54. #include <fcntl.h>
  55. #include "fts.h"
  56. #include <limits.h>
  57. #ifndef _win_
  58. static const dird invalidDirD = -1;
  59. dird get_cwdd() {
  60. return open(".", O_RDONLY, 0);
  61. }
  62. dird get_dird(char* path) {
  63. return open(path, O_RDONLY, 0);
  64. }
  65. int valid_dird(dird fd) {
  66. return fd < 0;
  67. }
  68. void close_dird(dird fd) {
  69. (void)close(fd);
  70. }
  71. int chdir_dird(dird fd) {
  72. return fchdir(fd);
  73. }
  74. int cmp_dird(dird fd1, dird fd2) {
  75. return fd1 - fd2;
  76. }
  77. #else // ndef _win_
  78. int stat64UTF(const char* path, struct _stat64* _Stat) {
  79. int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0);
  80. if (len_converted == 0) {
  81. return -1;
  82. }
  83. WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted));
  84. if (buf == nullptr) {
  85. return -1;
  86. }
  87. MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted);
  88. int ret = _wstat64(buf, _Stat);
  89. free(buf);
  90. return ret;
  91. }
  92. int stat64UTF(dird path, struct _stat64* _Stat) {
  93. return _wstat64(path, _Stat);
  94. }
  95. const dird invalidDirD = nullptr;
  96. dird get_cwdd() {
  97. return _wgetcwd(nullptr, 0);
  98. }
  99. int valid_dird(dird fd) {
  100. return fd == nullptr;
  101. }
  102. void close_dird(dird fd) {
  103. free(fd);
  104. }
  105. int chdir_dird(dird fd) {
  106. return _wchdir(fd);
  107. }
  108. int chdir_dird(const char* path) {
  109. int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0);
  110. if (len_converted == 0) {
  111. return -1;
  112. }
  113. WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted));
  114. if (buf == nullptr) {
  115. return -1;
  116. }
  117. MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted);
  118. int ret = _wchdir(buf);
  119. free(buf);
  120. return ret;
  121. }
  122. int cmp_dird(dird fd1, dird fd2) {
  123. return lstrcmpW(fd1, fd2);
  124. }
  125. dird get_dird(char* path) {
  126. int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0);
  127. if (len_converted == 0) {
  128. return nullptr;
  129. }
  130. WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted));
  131. if (buf == nullptr) {
  132. return nullptr;
  133. }
  134. MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted);
  135. WCHAR* ret = _wfullpath(0, buf, 0);
  136. free(buf);
  137. return ret;
  138. }
  139. #endif // ndef _win_
  140. #ifdef _win_
  141. #define S_ISDIR(st_mode) ((st_mode & _S_IFMT) == _S_IFDIR)
  142. #define S_ISREG(st_mode) ((st_mode & _S_IFMT) == _S_IFREG)
  143. #define S_ISLNK(st_mode) ((st_mode & _S_IFMT) == _S_IFLNK)
  144. #define O_RDONLY _O_RDONLY
  145. static int fts_safe_changedir(FTS*, FTSENT*, int, dird);
  146. #endif
  147. #if defined(__svr4__) || defined(__linux__) || defined(__CYGWIN__) || defined(_win_)
  148. #ifdef MAX
  149. #undef MAX
  150. #endif
  151. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  152. #undef ALIGNBYTES
  153. #undef ALIGN
  154. #define ALIGNBYTES (sizeof(long long) - 1)
  155. #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
  156. #if !defined(__linux__) && !defined(__CYGWIN__)
  157. #define dirfd(dirp) ((dirp)->dd_fd)
  158. #endif
  159. #define D_NAMLEN(dirp) (strlen(dirp->d_name))
  160. #else
  161. #define D_NAMLEN(dirp) (dirp->d_namlen)
  162. #endif
  163. static FTSENT* fts_alloc(FTS*, const char*, int);
  164. static FTSENT* fts_build(FTS*, int);
  165. static void fts_lfree(FTSENT*);
  166. static void fts_load(FTS*, FTSENT*);
  167. static size_t fts_maxarglen(char* const*);
  168. static void fts_padjust(FTS*);
  169. static int fts_palloc(FTS*, size_t);
  170. static FTSENT* fts_sort(FTS*, FTSENT*, int);
  171. static u_short fts_stat(FTS*, FTSENT*, int);
  172. static int fts_safe_changedir(FTS*, FTSENT*, int, const char*);
  173. #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
  174. #define CLR(opt) (sp->fts_options &= ~(opt))
  175. #define ISSET(opt) (sp->fts_options & (opt))
  176. #define SET(opt) (sp->fts_options |= (opt))
  177. #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && chdir_dird(fd))
  178. /* fts_build flags */
  179. #define BCHILD 1 /* yfts_children */
  180. #define BNAMES 2 /* yfts_children, names only */
  181. #define BREAD 3 /* yfts_read */
  182. static u_short
  183. yfts_type_from_info(u_short info) {
  184. if (info == FTS_D || info == FTS_DP || info == FTS_DOT) {
  185. return FTS_D;
  186. } else if (info == FTS_F) {
  187. return FTS_F;
  188. } else if (info == FTS_SL || info == FTS_SLNONE) {
  189. return FTS_SL;
  190. }
  191. return FTS_NSOK;
  192. }
  193. static void*
  194. yreallocf(void* ptr, size_t size)
  195. {
  196. void* nptr;
  197. nptr = realloc(ptr, size);
  198. if (!nptr && ptr) {
  199. free(ptr);
  200. }
  201. return (nptr);
  202. }
  203. FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**))
  204. {
  205. FTS* sp;
  206. FTSENT *p, *root;
  207. int nitems;
  208. FTSENT *parent, *tmp;
  209. int len;
  210. errno = 0;
  211. Y_ASSERT(argv);
  212. if (!*argv) {
  213. errno = ENOENT;
  214. return nullptr;
  215. }
  216. /* Options check. */
  217. if (options & ~FTS_OPTIONMASK) {
  218. errno = EINVAL;
  219. return nullptr;
  220. }
  221. /* Allocate/initialize the stream */
  222. if ((sp = (FTS*)malloc(sizeof(FTS))) == nullptr) {
  223. return nullptr;
  224. }
  225. memset(sp, 0, sizeof(FTS));
  226. sp->fts_compar = compar;
  227. sp->fts_options = options;
  228. /* Shush, GCC. */
  229. tmp = nullptr;
  230. /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
  231. if (ISSET(FTS_LOGICAL)) {
  232. SET(FTS_NOCHDIR);
  233. }
  234. /*
  235. * Start out with 1K of path space, and enough, in any case,
  236. * to hold the user's paths.
  237. */
  238. if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) {
  239. goto mem1;
  240. }
  241. /* Allocate/initialize root's parent. */
  242. if ((parent = fts_alloc(sp, "", 0)) == nullptr) {
  243. goto mem2;
  244. }
  245. parent->fts_level = FTS_ROOTPARENTLEVEL;
  246. /* Allocate/initialize root(s). */
  247. for (root = nullptr, nitems = 0; *argv; ++argv, ++nitems) {
  248. /* Don't allow zero-length paths. */
  249. len = strlen(*argv);
  250. //Any subsequent windows call will expect no trailing slashes so we will remove them here
  251. #ifdef _win_
  252. while (len && ((*argv)[len - 1] == '\\' || (*argv)[len - 1] == '/')) {
  253. --len;
  254. }
  255. #endif
  256. if (len == 0) {
  257. errno = ENOENT;
  258. goto mem3;
  259. }
  260. p = fts_alloc(sp, *argv, len);
  261. p->fts_level = FTS_ROOTLEVEL;
  262. p->fts_parent = parent;
  263. p->fts_accpath = p->fts_name;
  264. p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
  265. p->fts_type = yfts_type_from_info(p->fts_info);
  266. /* Command-line "." and ".." are real directories. */
  267. if (p->fts_info == FTS_DOT) {
  268. p->fts_info = FTS_D;
  269. }
  270. /*
  271. * If comparison routine supplied, traverse in sorted
  272. * order; otherwise traverse in the order specified.
  273. */
  274. if (compar) {
  275. p->fts_link = root;
  276. root = p;
  277. } else {
  278. p->fts_link = nullptr;
  279. if (root == nullptr) {
  280. tmp = root = p;
  281. } else {
  282. tmp->fts_link = p;
  283. tmp = p;
  284. }
  285. }
  286. }
  287. if (compar && nitems > 1) {
  288. root = fts_sort(sp, root, nitems);
  289. }
  290. /*
  291. * Allocate a dummy pointer and make yfts_read think that we've just
  292. * finished the node before the root(s); set p->fts_info to FTS_INIT
  293. * so that everything about the "current" node is ignored.
  294. */
  295. if ((sp->fts_cur = fts_alloc(sp, "", 0)) == nullptr) {
  296. goto mem3;
  297. }
  298. sp->fts_cur->fts_level = FTS_ROOTLEVEL;
  299. sp->fts_cur->fts_link = root;
  300. sp->fts_cur->fts_info = FTS_INIT;
  301. /*
  302. * If using chdir(2), grab a file descriptor pointing to dot to ensure
  303. * that we can get back here; this could be avoided for some paths,
  304. * but almost certainly not worth the effort. Slashes, symbolic links,
  305. * and ".." are all fairly nasty problems. Note, if we can't get the
  306. * descriptor we run anyway, just more slowly.
  307. */
  308. if (!ISSET(FTS_NOCHDIR) && valid_dird(sp->fts_rfd = get_cwdd())) {
  309. SET(FTS_NOCHDIR);
  310. }
  311. return (sp);
  312. mem3:
  313. fts_lfree(root);
  314. free(parent);
  315. mem2:
  316. free(sp->fts_path);
  317. mem1:
  318. free(sp);
  319. return nullptr;
  320. }
  321. static void
  322. fts_load(FTS* sp, FTSENT* p)
  323. {
  324. size_t len;
  325. char* cp;
  326. /*
  327. * Load the stream structure for the next traversal. Since we don't
  328. * actually enter the directory until after the preorder visit, set
  329. * the fts_accpath field specially so the chdir gets done to the right
  330. * place and the user can access the first node. From yfts_open it's
  331. * known that the path will fit.
  332. */
  333. len = p->fts_pathlen = p->fts_namelen;
  334. memmove((void*)sp->fts_path, (void*)p->fts_name, len + 1);
  335. if ((cp = strrchr(p->fts_name, LOCSLASH_C)) != nullptr && (cp != p->fts_name || cp[1])) {
  336. len = strlen(++cp);
  337. memmove((void*)p->fts_name, (void*)cp, len + 1);
  338. p->fts_namelen = (u_short)len;
  339. }
  340. p->fts_accpath = p->fts_path = sp->fts_path;
  341. sp->fts_dev = p->fts_dev;
  342. }
  343. int yfts_close(FTS* sp)
  344. {
  345. FTSENT *freep, *p;
  346. int saved_errno;
  347. /*
  348. * This still works if we haven't read anything -- the dummy structure
  349. * points to the root list, so we step through to the end of the root
  350. * list which has a valid parent pointer.
  351. */
  352. if (sp->fts_cur) {
  353. for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
  354. freep = p;
  355. p = p->fts_link ? p->fts_link : p->fts_parent;
  356. free(freep);
  357. }
  358. free(p);
  359. }
  360. /* Free up child linked list, sort array, path buffer. */
  361. if (sp->fts_child) {
  362. fts_lfree(sp->fts_child);
  363. }
  364. if (sp->fts_array) {
  365. free(sp->fts_array);
  366. }
  367. free(sp->fts_path);
  368. /* Return to original directory, save errno if necessary. */
  369. if (!ISSET(FTS_NOCHDIR)) {
  370. saved_errno = chdir_dird(sp->fts_rfd) ? errno : 0;
  371. close_dird(sp->fts_rfd);
  372. /* Set errno and return. */
  373. if (saved_errno != 0) {
  374. /* Free up the stream pointer. */
  375. free(sp);
  376. errno = saved_errno;
  377. return (-1);
  378. }
  379. }
  380. /* Free up the stream pointer. */
  381. free(sp);
  382. return (0);
  383. }
  384. /*
  385. * Special case of "/" at the end of the path so that slashes aren't
  386. * appended which would cause paths to be written as "....//foo".
  387. */
  388. #define NAPPEND(p) \
  389. (p->fts_path[p->fts_pathlen - 1] == LOCSLASH_C \
  390. ? p->fts_pathlen - 1 \
  391. : p->fts_pathlen)
  392. FTSENT*
  393. yfts_read(FTS* sp) {
  394. FTSENT *p, *tmp;
  395. int instr;
  396. char* t;
  397. int saved_errno;
  398. ClearLastSystemError();
  399. /* If finished or unrecoverable error, return NULL. */
  400. if (sp->fts_cur == nullptr || ISSET(FTS_STOP)) {
  401. return nullptr;
  402. }
  403. /* Set current node pointer. */
  404. p = sp->fts_cur;
  405. /* Save and zero out user instructions. */
  406. instr = p->fts_instr;
  407. p->fts_instr = FTS_NOINSTR;
  408. /* Any type of file may be re-visited; re-stat and re-turn. */
  409. if (instr == FTS_AGAIN) {
  410. p->fts_info = fts_stat(sp, p, 0);
  411. p->fts_type = yfts_type_from_info(p->fts_info);
  412. return (p);
  413. }
  414. /*
  415. * Following a symlink -- SLNONE test allows application to see
  416. * SLNONE and recover. If indirecting through a symlink, have
  417. * keep a pointer to current location. If unable to get that
  418. * pointer, follow fails.
  419. */
  420. if (instr == FTS_FOLLOW &&
  421. (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
  422. p->fts_info = fts_stat(sp, p, 1);
  423. p->fts_type = yfts_type_from_info(p->fts_info);
  424. if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
  425. if (valid_dird(p->fts_symfd = get_cwdd())) {
  426. p->fts_errno = errno;
  427. p->fts_info = FTS_ERR;
  428. } else {
  429. p->fts_flags |= FTS_SYMFOLLOW;
  430. }
  431. }
  432. return (p);
  433. }
  434. /* Directory in pre-order. */
  435. if (p->fts_info == FTS_D) {
  436. /* If skipped or crossed mount point, do post-order visit. */
  437. if (instr == FTS_SKIP ||
  438. (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
  439. if (p->fts_flags & FTS_SYMFOLLOW) {
  440. close_dird(p->fts_symfd);
  441. }
  442. if (sp->fts_child) {
  443. fts_lfree(sp->fts_child);
  444. sp->fts_child = nullptr;
  445. }
  446. p->fts_info = FTS_DP;
  447. return (p);
  448. }
  449. /* Rebuild if only read the names and now traversing. */
  450. if (sp->fts_child && ISSET(FTS_NAMEONLY)) {
  451. CLR(FTS_NAMEONLY);
  452. fts_lfree(sp->fts_child);
  453. sp->fts_child = nullptr;
  454. }
  455. /*
  456. * Cd to the subdirectory.
  457. *
  458. * If have already read and now fail to chdir, whack the list
  459. * to make the names come out right, and set the parent errno
  460. * so the application will eventually get an error condition.
  461. * Set the FTS_DONTCHDIR flag so that when we logically change
  462. * directories back to the parent we don't do a chdir.
  463. *
  464. * If haven't read do so. If the read fails, fts_build sets
  465. * FTS_STOP or the fts_info field of the node.
  466. */
  467. if (sp->fts_child) {
  468. if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
  469. p->fts_errno = errno;
  470. p->fts_flags |= FTS_DONTCHDIR;
  471. for (p = sp->fts_child; p; p = p->fts_link) {
  472. p->fts_accpath =
  473. p->fts_parent->fts_accpath;
  474. }
  475. }
  476. } else if ((sp->fts_child = fts_build(sp, BREAD)) == nullptr) {
  477. if (ISSET(FTS_STOP)) {
  478. return nullptr;
  479. }
  480. return (p);
  481. }
  482. p = sp->fts_child;
  483. sp->fts_child = nullptr;
  484. goto name;
  485. }
  486. /* Move to the next node on this level. */
  487. next:
  488. tmp = p;
  489. if ((p = p->fts_link) != nullptr) {
  490. free(tmp);
  491. /*
  492. * If reached the top, return to the original directory (or
  493. * the root of the tree), and load the paths for the next root.
  494. */
  495. if (p->fts_level == FTS_ROOTLEVEL) {
  496. if (FCHDIR(sp, sp->fts_rfd)) {
  497. SET(FTS_STOP);
  498. return nullptr;
  499. }
  500. fts_load(sp, p);
  501. return (sp->fts_cur = p);
  502. }
  503. /*
  504. * User may have called yfts_set on the node. If skipped,
  505. * ignore. If followed, get a file descriptor so we can
  506. * get back if necessary.
  507. */
  508. if (p->fts_instr == FTS_SKIP) {
  509. goto next;
  510. }
  511. if (p->fts_instr == FTS_FOLLOW) {
  512. p->fts_info = fts_stat(sp, p, 1);
  513. p->fts_type = yfts_type_from_info(p->fts_info);
  514. if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
  515. if (valid_dird(p->fts_symfd =
  516. get_cwdd())) {
  517. p->fts_errno = errno;
  518. p->fts_info = FTS_ERR;
  519. } else {
  520. p->fts_flags |= FTS_SYMFOLLOW;
  521. }
  522. }
  523. p->fts_instr = FTS_NOINSTR;
  524. }
  525. name:
  526. t = sp->fts_path + NAPPEND(p->fts_parent);
  527. *t++ = LOCSLASH_C;
  528. memmove(t, p->fts_name, (size_t)p->fts_namelen + 1);
  529. return (sp->fts_cur = p);
  530. }
  531. /* Move up to the parent node. */
  532. p = tmp->fts_parent;
  533. free(tmp);
  534. if (p->fts_level == FTS_ROOTPARENTLEVEL) {
  535. /*
  536. * Done; free everything up and set errno to 0 so the user
  537. * can distinguish between error and EOF.
  538. */
  539. free(p);
  540. errno = 0;
  541. return (sp->fts_cur = nullptr);
  542. }
  543. /* NUL terminate the pathname. */
  544. sp->fts_path[p->fts_pathlen] = '\0';
  545. /*
  546. * Return to the parent directory. If at a root node or came through
  547. * a symlink, go back through the file descriptor. Otherwise, cd up
  548. * one directory.
  549. */
  550. if (p->fts_level == FTS_ROOTLEVEL) {
  551. if (FCHDIR(sp, sp->fts_rfd)) {
  552. SET(FTS_STOP);
  553. return nullptr;
  554. }
  555. } else if (p->fts_flags & FTS_SYMFOLLOW) {
  556. if (FCHDIR(sp, p->fts_symfd)) {
  557. saved_errno = errno;
  558. close_dird(p->fts_symfd);
  559. errno = saved_errno;
  560. SET(FTS_STOP);
  561. return nullptr;
  562. }
  563. close_dird(p->fts_symfd);
  564. } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
  565. fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
  566. SET(FTS_STOP);
  567. return nullptr;
  568. }
  569. p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
  570. return (sp->fts_cur = p);
  571. }
  572. /*
  573. * Fts_set takes the stream as an argument although it's not used in this
  574. * implementation; it would be necessary if anyone wanted to add global
  575. * semantics to fts using yfts_set. An error return is allowed for similar
  576. * reasons.
  577. */
  578. /* ARGSUSED */
  579. int yfts_set(FTS* sp, FTSENT* p, int instr)
  580. {
  581. (void)sp; //Unused
  582. if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
  583. instr != FTS_NOINSTR && instr != FTS_SKIP) {
  584. errno = EINVAL;
  585. return (1);
  586. }
  587. p->fts_instr = (u_short)instr;
  588. return (0);
  589. }
  590. FTSENT*
  591. yfts_children(FTS* sp, int instr)
  592. {
  593. FTSENT* p;
  594. dird fd;
  595. if (instr && instr != FTS_NAMEONLY) {
  596. errno = EINVAL;
  597. return nullptr;
  598. }
  599. /* Set current node pointer. */
  600. p = sp->fts_cur;
  601. /*
  602. * Errno set to 0 so user can distinguish empty directory from
  603. * an error.
  604. */
  605. errno = 0;
  606. /* Fatal errors stop here. */
  607. if (ISSET(FTS_STOP)) {
  608. return nullptr;
  609. }
  610. /* Return logical hierarchy of user's arguments. */
  611. if (p->fts_info == FTS_INIT) {
  612. return (p->fts_link);
  613. }
  614. /*
  615. * If not a directory being visited in pre-order, stop here. Could
  616. * allow FTS_DNR, assuming the user has fixed the problem, but the
  617. * same effect is available with FTS_AGAIN.
  618. */
  619. if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) {
  620. return nullptr;
  621. }
  622. /* Free up any previous child list. */
  623. if (sp->fts_child) {
  624. fts_lfree(sp->fts_child);
  625. }
  626. if (instr == FTS_NAMEONLY) {
  627. SET(FTS_NAMEONLY);
  628. instr = BNAMES;
  629. } else {
  630. instr = BCHILD;
  631. }
  632. /*
  633. * If using chdir on a relative path and called BEFORE yfts_read does
  634. * its chdir to the root of a traversal, we can lose -- we need to
  635. * chdir into the subdirectory, and we don't know where the current
  636. * directory is, so we can't get back so that the upcoming chdir by
  637. * yfts_read will work.
  638. */
  639. if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == LOCSLASH_C ||
  640. ISSET(FTS_NOCHDIR)) {
  641. return (sp->fts_child = fts_build(sp, instr));
  642. }
  643. if (valid_dird(fd = get_cwdd())) {
  644. return nullptr;
  645. }
  646. sp->fts_child = fts_build(sp, instr);
  647. if (chdir_dird(fd)) {
  648. close_dird(fd);
  649. return nullptr;
  650. }
  651. close_dird(fd);
  652. return (sp->fts_child);
  653. }
  654. static inline struct dirent* yreaddir(DIR* dir, struct dirent* de) {
  655. // TODO(yazevnul|IGNIETFERRO-1070): remove these macroses by replacing `readdir_r` with proper
  656. // alternative
  657. Y_PRAGMA_DIAGNOSTIC_PUSH
  658. Y_PRAGMA_NO_DEPRECATED
  659. if (readdir_r(dir, de, &de) == 0) {
  660. Y_PRAGMA_DIAGNOSTIC_POP
  661. return de;
  662. }
  663. return nullptr;
  664. }
  665. /*
  666. * This is the tricky part -- do not casually change *anything* in here. The
  667. * idea is to build the linked list of entries that are used by yfts_children
  668. * and yfts_read. There are lots of special cases.
  669. *
  670. * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is
  671. * set and it's a physical walk (so that symbolic links can't be directories),
  672. * we can do things quickly. First, if it's a 4.4BSD file system, the type
  673. * of the file is in the directory entry. Otherwise, we assume that the number
  674. * of subdirectories in a node is equal to the number of links to the parent.
  675. * The former skips all stat calls. The latter skips stat calls in any leaf
  676. * directories and for any files after the subdirectories in the directory have
  677. * been found, cutting the stat calls by about 2/3.
  678. */
  679. static FTSENT*
  680. fts_build(FTS* sp, int type)
  681. {
  682. struct dirent* dp;
  683. FTSENT *p, *head;
  684. int nitems;
  685. FTSENT *cur, *tail;
  686. #ifdef _win_
  687. dird dirpd;
  688. struct DIR* dirp;
  689. #else
  690. DIR* dirp;
  691. #endif
  692. void* oldaddr;
  693. int cderrno, descend, len, level, maxlen, nlinks, saved_errno,
  694. nostat, doadjust;
  695. char* cp;
  696. /* Set current node pointer. */
  697. cur = sp->fts_cur;
  698. /*
  699. * Open the directory for reading. If this fails, we're done.
  700. * If being called from yfts_read, set the fts_info field.
  701. */
  702. #ifdef FTS_WHITEOUT
  703. if (ISSET(FTS_WHITEOUT))
  704. oflag = DTF_NODUP | DTF_REWIND;
  705. else
  706. oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND;
  707. #else
  708. #define __opendir2(path, flag) opendir(path)
  709. #endif
  710. if ((dirp = __opendir2(cur->fts_accpath, oflag)) == nullptr) {
  711. if (type == BREAD) {
  712. cur->fts_info = FTS_DNR;
  713. cur->fts_errno = errno;
  714. }
  715. return nullptr;
  716. }
  717. #ifdef _win_
  718. dirpd = get_dird(cur->fts_accpath);
  719. #endif
  720. /*
  721. * Nlinks is the number of possible entries of type directory in the
  722. * directory if we're cheating on stat calls, 0 if we're not doing
  723. * any stat calls at all, -1 if we're doing stats on everything.
  724. */
  725. if (type == BNAMES) {
  726. nlinks = 0;
  727. /* Be quiet about nostat, GCC. */
  728. nostat = 0;
  729. } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
  730. nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
  731. nostat = 1;
  732. } else {
  733. nlinks = -1;
  734. nostat = 0;
  735. }
  736. /*
  737. * If we're going to need to stat anything or we want to descend
  738. * and stay in the directory, chdir. If this fails we keep going,
  739. * but set a flag so we don't chdir after the post-order visit.
  740. * We won't be able to stat anything, but we can still return the
  741. * names themselves. Note, that since yfts_read won't be able to
  742. * chdir into the directory, it will have to return different path
  743. * names than before, i.e. "a/b" instead of "b". Since the node
  744. * has already been visited in pre-order, have to wait until the
  745. * post-order visit to return the error. There is a special case
  746. * here, if there was nothing to stat then it's not an error to
  747. * not be able to stat. This is all fairly nasty. If a program
  748. * needed sorted entries or stat information, they had better be
  749. * checking FTS_NS on the returned nodes.
  750. */
  751. cderrno = 0;
  752. if (nlinks || type == BREAD) {
  753. #ifndef _win_
  754. if (fts_safe_changedir(sp, cur, dirfd(dirp), nullptr)) {
  755. #else
  756. if (fts_safe_changedir(sp, cur, -1, dirpd)) {
  757. #endif
  758. if (nlinks && type == BREAD) {
  759. cur->fts_errno = errno;
  760. }
  761. cur->fts_flags |= FTS_DONTCHDIR;
  762. descend = 0;
  763. cderrno = errno;
  764. (void)closedir(dirp);
  765. dirp = nullptr;
  766. #ifdef _win_
  767. close_dird(dirpd);
  768. dirpd = invalidDirD;
  769. #else
  770. Y_UNUSED(invalidDirD);
  771. #endif
  772. } else {
  773. descend = 1;
  774. }
  775. } else {
  776. descend = 0;
  777. }
  778. /*
  779. * Figure out the max file name length that can be stored in the
  780. * current path -- the inner loop allocates more path as necessary.
  781. * We really wouldn't have to do the maxlen calculations here, we
  782. * could do them in yfts_read before returning the path, but it's a
  783. * lot easier here since the length is part of the dirent structure.
  784. *
  785. * If not changing directories set a pointer so that can just append
  786. * each new name into the path.
  787. */
  788. len = NAPPEND(cur);
  789. if (ISSET(FTS_NOCHDIR)) {
  790. cp = sp->fts_path + len;
  791. *cp++ = LOCSLASH_C;
  792. } else {
  793. /* GCC, you're too verbose. */
  794. cp = nullptr;
  795. }
  796. ++len;
  797. maxlen = sp->fts_pathlen - len;
  798. level = cur->fts_level + 1;
  799. /* Read the directory, attaching each entry to the `link' pointer. */
  800. doadjust = 0;
  801. //to ensure enough buffer
  802. TTempBuf dpe;
  803. for (head = tail = nullptr, nitems = 0; dirp && (dp = yreaddir(dirp, (struct dirent*)dpe.Data())) != nullptr;) {
  804. if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) {
  805. continue;
  806. }
  807. if ((p = fts_alloc(sp, dp->d_name, (int)strlen(dp->d_name))) == nullptr) {
  808. goto mem1;
  809. }
  810. if (strlen(dp->d_name) >= (size_t)maxlen) { /* include space for NUL */
  811. oldaddr = sp->fts_path;
  812. if (fts_palloc(sp, strlen(dp->d_name) + len + 1)) {
  813. /*
  814. * No more memory for path or structures. Save
  815. * errno, free up the current structure and the
  816. * structures already allocated.
  817. */
  818. mem1:
  819. saved_errno = errno;
  820. if (p) {
  821. free(p);
  822. }
  823. fts_lfree(head);
  824. (void)closedir(dirp);
  825. #ifdef _win_
  826. close_dird(dirpd);
  827. #endif
  828. cur->fts_info = FTS_ERR;
  829. SET(FTS_STOP);
  830. errno = saved_errno;
  831. return nullptr;
  832. }
  833. /* Did realloc() change the pointer? */
  834. if (oldaddr != sp->fts_path) {
  835. doadjust = 1;
  836. if (ISSET(FTS_NOCHDIR)) {
  837. cp = sp->fts_path + len;
  838. }
  839. }
  840. maxlen = sp->fts_pathlen - len;
  841. }
  842. if (len + strlen(dp->d_name) >= USHRT_MAX) {
  843. /*
  844. * In an FTSENT, fts_pathlen is a u_short so it is
  845. * possible to wraparound here. If we do, free up
  846. * the current structure and the structures already
  847. * allocated, then error out with ENAMETOOLONG.
  848. */
  849. free(p);
  850. fts_lfree(head);
  851. (void)closedir(dirp);
  852. #ifdef _win_
  853. close_dird(dirpd);
  854. #endif
  855. cur->fts_info = FTS_ERR;
  856. SET(FTS_STOP);
  857. errno = ENAMETOOLONG;
  858. return nullptr;
  859. }
  860. p->fts_level = (short)level;
  861. p->fts_parent = sp->fts_cur;
  862. p->fts_pathlen = u_short(len + strlen(dp->d_name));
  863. #ifdef FTS_WHITEOUT
  864. if (dp->d_type == DT_WHT)
  865. p->fts_flags |= FTS_ISW;
  866. #endif
  867. #ifdef _DIRENT_HAVE_D_TYPE
  868. if (dp->d_type == DT_DIR) {
  869. p->fts_type = FTS_D;
  870. } else if (dp->d_type == DT_REG) {
  871. p->fts_type = FTS_F;
  872. } else if (dp->d_type == DT_LNK) {
  873. p->fts_type = FTS_SL;
  874. }
  875. #endif
  876. // coverity[dead_error_line]: false positive
  877. if (cderrno) {
  878. if (nlinks) {
  879. p->fts_info = FTS_NS;
  880. p->fts_errno = cderrno;
  881. } else {
  882. p->fts_info = FTS_NSOK;
  883. }
  884. p->fts_accpath = cur->fts_accpath;
  885. } else if (nlinks == 0
  886. #ifdef DT_DIR
  887. || (nostat &&
  888. dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
  889. #endif
  890. ) {
  891. p->fts_accpath =
  892. ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
  893. p->fts_info = FTS_NSOK;
  894. } else {
  895. /* Build a file name for fts_stat to stat. */
  896. if (ISSET(FTS_NOCHDIR)) {
  897. p->fts_accpath = p->fts_path;
  898. memmove((void*)cp, (void*)p->fts_name, (size_t)p->fts_namelen + 1);
  899. } else {
  900. p->fts_accpath = p->fts_name;
  901. }
  902. /* Stat it. */
  903. p->fts_info = fts_stat(sp, p, 0);
  904. p->fts_type = yfts_type_from_info(p->fts_info);
  905. /* Decrement link count if applicable. */
  906. if (nlinks > 0 && (p->fts_info == FTS_D ||
  907. p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) {
  908. --nlinks;
  909. }
  910. }
  911. /* We walk in directory order so "ls -f" doesn't get upset. */
  912. p->fts_link = nullptr;
  913. if (head == nullptr) {
  914. head = tail = p;
  915. } else {
  916. tail->fts_link = p;
  917. tail = p;
  918. }
  919. ++nitems;
  920. }
  921. if (dirp) {
  922. (void)closedir(dirp);
  923. #ifdef _win_
  924. close_dird(dirpd);
  925. #endif
  926. }
  927. /*
  928. * If realloc() changed the address of the path, adjust the
  929. * addresses for the rest of the tree and the dir list.
  930. */
  931. if (doadjust) {
  932. fts_padjust(sp);
  933. }
  934. /*
  935. * If not changing directories, reset the path back to original
  936. * state.
  937. */
  938. if (ISSET(FTS_NOCHDIR)) {
  939. if (len == sp->fts_pathlen || nitems == 0) {
  940. --cp;
  941. }
  942. *cp = '\0';
  943. }
  944. /*
  945. * If descended after called from yfts_children or after called from
  946. * yfts_read and nothing found, get back. At the root level we use
  947. * the saved fd; if one of yfts_open()'s arguments is a relative path
  948. * to an empty directory, we wind up here with no other way back. If
  949. * can't get back, we're done.
  950. */
  951. if (descend && (type == BCHILD || !nitems) &&
  952. (cur->fts_level == FTS_ROOTLEVEL ? FCHDIR(sp, sp->fts_rfd) : fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
  953. cur->fts_info = FTS_ERR;
  954. SET(FTS_STOP);
  955. fts_lfree(head);
  956. return nullptr;
  957. }
  958. /* If didn't find anything, return NULL. */
  959. if (!nitems) {
  960. if (type == BREAD) {
  961. cur->fts_info = FTS_DP;
  962. }
  963. fts_lfree(head);
  964. return nullptr;
  965. }
  966. /* Sort the entries. */
  967. if (sp->fts_compar && nitems > 1) {
  968. head = fts_sort(sp, head, nitems);
  969. }
  970. return (head);
  971. }
  972. static u_short
  973. fts_stat(FTS* sp, FTSENT* p, int follow)
  974. {
  975. dev_t dev;
  976. ino_t ino;
  977. stat_struct *sbp, sb;
  978. int saved_errno;
  979. /* If user needs stat info, stat buffer already allocated. */
  980. sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
  981. #ifdef FTS_WHITEOUT
  982. /* check for whiteout */
  983. if (p->fts_flags & FTS_ISW) {
  984. if (sbp != &sb) {
  985. memset(sbp, '\0', sizeof(*sbp));
  986. sbp->st_mode = S_IFWHT;
  987. }
  988. return (FTS_W);
  989. }
  990. #endif
  991. /*
  992. * If doing a logical walk, or application requested FTS_FOLLOW, do
  993. * a stat(2). If that fails, check for a non-existent symlink. If
  994. * fail, set the errno from the stat call.
  995. */
  996. if (ISSET(FTS_LOGICAL) || follow) {
  997. if (STAT_FUNC(p->fts_accpath, sbp)) {
  998. saved_errno = errno;
  999. if (!lstat(p->fts_accpath, sbp)) {
  1000. errno = 0;
  1001. return (FTS_SLNONE);
  1002. }
  1003. p->fts_errno = saved_errno;
  1004. memset(sbp, 0, sizeof(stat_struct));
  1005. return (FTS_NS);
  1006. }
  1007. } else if (lstat(p->fts_accpath, sbp)) {
  1008. p->fts_errno = errno;
  1009. memset(sbp, 0, sizeof(stat_struct));
  1010. return (FTS_NS);
  1011. }
  1012. if (S_ISDIR(sbp->st_mode)) {
  1013. /*
  1014. * Set the device/inode. Used to find cycles and check for
  1015. * crossing mount points. Also remember the link count, used
  1016. * in fts_build to limit the number of stat calls. It is
  1017. * understood that these fields are only referenced if fts_info
  1018. * is set to FTS_D.
  1019. */
  1020. dev = p->fts_dev = sbp->st_dev;
  1021. ino = p->fts_ino = sbp->st_ino;
  1022. p->fts_nlink = sbp->st_nlink;
  1023. const char* fts_name_x = p->fts_name;
  1024. if (ISDOT(fts_name_x)) {
  1025. return (FTS_DOT);
  1026. }
  1027. /*
  1028. * Cycle detection is done by brute force when the directory
  1029. * is first encountered. If the tree gets deep enough or the
  1030. * number of symbolic links to directories is high enough,
  1031. * something faster might be worthwhile.
  1032. */
  1033. //There is no way to detect symlink or mount cycles on win32
  1034. #ifndef _win_
  1035. FTSENT* t;
  1036. for (t = p->fts_parent;
  1037. t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) {
  1038. if (ino == t->fts_ino && dev == t->fts_dev) {
  1039. p->fts_cycle = t;
  1040. return (FTS_DC);
  1041. }
  1042. }
  1043. #endif /*_win_*/
  1044. return (FTS_D);
  1045. }
  1046. if (S_ISLNK(sbp->st_mode)) {
  1047. return (FTS_SL);
  1048. }
  1049. if (S_ISREG(sbp->st_mode)) {
  1050. return (FTS_F);
  1051. }
  1052. return (FTS_DEFAULT);
  1053. }
  1054. static FTSENT*
  1055. fts_sort(FTS* sp, FTSENT* head, int nitems)
  1056. {
  1057. FTSENT **ap, *p;
  1058. /*
  1059. * Construct an array of pointers to the structures and call qsort(3).
  1060. * Reassemble the array in the order returned by qsort. If unable to
  1061. * sort for memory reasons, return the directory entries in their
  1062. * current order. Allocate enough space for the current needs plus
  1063. * 40 so don't realloc one entry at a time.
  1064. */
  1065. if (nitems > sp->fts_nitems) {
  1066. struct _ftsent** a;
  1067. sp->fts_nitems = nitems + 40;
  1068. if ((a = (struct _ftsent**)realloc(sp->fts_array,
  1069. sp->fts_nitems * sizeof(FTSENT*))) == nullptr) {
  1070. if (sp->fts_array) {
  1071. free(sp->fts_array);
  1072. }
  1073. sp->fts_array = nullptr;
  1074. sp->fts_nitems = 0;
  1075. return (head);
  1076. }
  1077. sp->fts_array = a;
  1078. }
  1079. for (ap = sp->fts_array, p = head; p; p = p->fts_link) {
  1080. *ap++ = p;
  1081. }
  1082. qsort((void*)sp->fts_array, (size_t)nitems, sizeof(FTSENT*), (int (*)(const void*, const void*))sp->fts_compar);
  1083. for (head = *(ap = sp->fts_array); --nitems; ++ap) {
  1084. ap[0]->fts_link = ap[1];
  1085. }
  1086. ap[0]->fts_link = nullptr;
  1087. return (head);
  1088. }
  1089. static FTSENT*
  1090. fts_alloc(FTS* sp, const char* name, int namelen)
  1091. {
  1092. FTSENT* p;
  1093. size_t len;
  1094. /*
  1095. * The file name is a variable length array and no stat structure is
  1096. * necessary if the user has set the nostat bit. Allocate the FTSENT
  1097. * structure, the file name and the stat structure in one chunk, but
  1098. * be careful that the stat structure is reasonably aligned. Since the
  1099. * fts_name field is declared to be of size 1, the fts_name pointer is
  1100. * namelen + 2 before the first possible address of the stat structure.
  1101. */
  1102. len = sizeof(FTSENT) + namelen;
  1103. if (!ISSET(FTS_NOSTAT)) {
  1104. len += sizeof(stat_struct) + ALIGNBYTES;
  1105. }
  1106. if ((p = (FTSENT*)malloc(len)) == nullptr) {
  1107. return nullptr;
  1108. }
  1109. /* Copy the name and guarantee NUL termination. */
  1110. memmove((void*)p->fts_name, (void*)name, (size_t)namelen);
  1111. p->fts_name[namelen] = '\0';
  1112. if (!ISSET(FTS_NOSTAT)) {
  1113. p->fts_statp = (stat_struct*)ALIGN(p->fts_name + namelen + 2);
  1114. } else {
  1115. p->fts_statp = nullptr;
  1116. }
  1117. p->fts_namelen = (u_short)namelen;
  1118. p->fts_path = sp->fts_path;
  1119. p->fts_errno = 0;
  1120. p->fts_flags = 0;
  1121. p->fts_instr = FTS_NOINSTR;
  1122. p->fts_number = 0;
  1123. p->fts_pointer = nullptr;
  1124. p->fts_type = FTS_NSOK;
  1125. return (p);
  1126. }
  1127. static void
  1128. fts_lfree(FTSENT* head)
  1129. {
  1130. FTSENT* p;
  1131. /* Free a linked list of structures. */
  1132. while ((p = head) != nullptr) {
  1133. head = head->fts_link;
  1134. free(p);
  1135. }
  1136. }
  1137. /*
  1138. * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
  1139. * Most systems will allow creation of paths much longer than MAXPATHLEN, even
  1140. * though the kernel won't resolve them. Add the size (not just what's needed)
  1141. * plus 256 bytes so don't realloc the path 2 bytes at a time.
  1142. */
  1143. static int
  1144. fts_palloc(FTS* sp, size_t more)
  1145. {
  1146. sp->fts_pathlen += more + 256;
  1147. sp->fts_path = (char*)yreallocf(sp->fts_path, (size_t)sp->fts_pathlen);
  1148. return (sp->fts_path == nullptr);
  1149. }
  1150. static void
  1151. ADJUST(FTSENT* p, void* addr)
  1152. {
  1153. if ((p)->fts_accpath >= (p)->fts_path &&
  1154. (p)->fts_accpath < (p)->fts_path + (p)->fts_pathlen) {
  1155. if (p->fts_accpath != p->fts_path) {
  1156. errx(1, "fts ADJUST: accpath %p path %p",
  1157. p->fts_accpath, p->fts_path);
  1158. }
  1159. if (p->fts_level != 0) {
  1160. errx(1, "fts ADJUST: level %d not 0", p->fts_level);
  1161. }
  1162. (p)->fts_accpath =
  1163. (char*)addr + ((p)->fts_accpath - (p)->fts_path);
  1164. }
  1165. (p)->fts_path = (char*)addr;
  1166. }
  1167. /*
  1168. * When the path is realloc'd, have to fix all of the pointers in structures
  1169. * already returned.
  1170. */
  1171. static void
  1172. fts_padjust(FTS* sp)
  1173. {
  1174. FTSENT* p;
  1175. char* addr = sp->fts_path;
  1176. #define ADJUST1(p) \
  1177. { \
  1178. if ((p)->fts_accpath == (p)->fts_path) \
  1179. (p)->fts_accpath = (addr); \
  1180. (p)->fts_path = addr; \
  1181. }
  1182. /* Adjust the current set of children. */
  1183. for (p = sp->fts_child; p; p = p->fts_link) {
  1184. ADJUST(p, addr);
  1185. }
  1186. /* Adjust the rest of the tree. */
  1187. for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
  1188. ADJUST(p, addr);
  1189. p = p->fts_link ? p->fts_link : p->fts_parent;
  1190. }
  1191. }
  1192. static size_t
  1193. fts_maxarglen(char* const* argv)
  1194. {
  1195. size_t len, max;
  1196. for (max = 0; *argv; ++argv) {
  1197. if ((len = strlen(*argv)) > max) {
  1198. max = len;
  1199. }
  1200. }
  1201. return (max + 1);
  1202. }
  1203. /*
  1204. * Change to dir specified by fd or p->fts_accpath without getting
  1205. * tricked by someone changing the world out from underneath us.
  1206. * Assumes p->fts_dev and p->fts_ino are filled in.
  1207. */
  1208. #ifndef _win_
  1209. static int
  1210. fts_safe_changedir(FTS* sp, FTSENT* p, int fd, const char* path)
  1211. {
  1212. int ret, oerrno, newfd;
  1213. stat_struct sb;
  1214. newfd = fd;
  1215. if (ISSET(FTS_NOCHDIR)) {
  1216. return (0);
  1217. }
  1218. if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0) {
  1219. return (-1);
  1220. }
  1221. if (fstat(newfd, &sb)) {
  1222. ret = -1;
  1223. goto bail;
  1224. }
  1225. if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
  1226. errno = ENOENT; /* disinformation */
  1227. ret = -1;
  1228. goto bail;
  1229. }
  1230. ret = fchdir(newfd);
  1231. bail:
  1232. oerrno = errno;
  1233. if (fd < 0) {
  1234. (void)close(newfd);
  1235. }
  1236. errno = oerrno;
  1237. return (ret);
  1238. }
  1239. #else
  1240. static int
  1241. fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, const char* path)
  1242. {
  1243. int ret;
  1244. stat_struct sb;
  1245. if (ISSET(FTS_NOCHDIR))
  1246. return (0);
  1247. if (STAT_FUNC(path, &sb)) {
  1248. ret = -1;
  1249. goto bail;
  1250. }
  1251. if (p->fts_dev != sb.st_dev) {
  1252. errno = ENOENT; /* disinformation */
  1253. ret = -1;
  1254. goto bail;
  1255. }
  1256. ret = chdir_dird(path);
  1257. bail:
  1258. return (ret);
  1259. }
  1260. static int
  1261. fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, dird path) {
  1262. int ret;
  1263. stat_struct sb;
  1264. if (ISSET(FTS_NOCHDIR))
  1265. return (0);
  1266. if (STAT_FUNC(path, &sb)) {
  1267. ret = -1;
  1268. goto bail;
  1269. }
  1270. if (p->fts_dev != sb.st_dev) {
  1271. errno = ENOENT; /* disinformation */
  1272. ret = -1;
  1273. goto bail;
  1274. }
  1275. ret = chdir_dird(path);
  1276. bail:
  1277. return (ret);
  1278. }
  1279. #endif