ftw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* File tree walker functions.
  2. Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* used by D_EXACT_NAMLEN */
  18. #define strlen __strlen
  19. #define mempcpy __mempcpy
  20. #define stpcpy __stpcpy
  21. #define tsearch __tsearch
  22. #define tdestroy __tdestroy
  23. #define tfind __tfind
  24. #define fchdir __fchdir
  25. #define chdir __chdir
  26. #define dirfd __dirfd
  27. #define getcwd __getcwd
  28. #define opendir __opendir
  29. #define closedir __closedir
  30. #define _GNU_SOURCE
  31. #include <features.h>
  32. #if defined (__UCLIBC_HAS_LFS__) && defined L_ftw64
  33. #ifndef L_ftw
  34. #define L_ftw
  35. #endif
  36. /* If Large file support is enabled, transparently remap
  37. * things to use the 64-bit interfaces */
  38. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64
  39. #undef _FILE_OFFSET_BITS
  40. #define _FILE_OFFSET_BITS 64
  41. #endif
  42. #ifndef __USE_LARGEFILE64
  43. # define __USE_LARGEFILE64 1
  44. #endif
  45. #ifndef __USE_FILE_OFFSET64
  46. # define __USE_FILE_OFFSET64 1
  47. #endif
  48. #define FTW_NAME ftw64
  49. #define NFTW_NAME nftw64
  50. #define INO_T ino64_t
  51. #define STAT stat64
  52. #define LSTAT __lstat64
  53. #define XSTAT __stat64
  54. #define FTW_FUNC_T __ftw64_func_t
  55. #define NFTW_FUNC_T __nftw64_func_t
  56. #define __readdir __readdir64
  57. #else
  58. #undef __stat
  59. #undef __lstat
  60. #define FTW_NAME ftw
  61. #define NFTW_NAME nftw
  62. #define INO_T ino_t
  63. #define STAT stat
  64. #define LSTAT __lstat
  65. #define XSTAT __stat
  66. #define FTW_FUNC_T __ftw_func_t
  67. #define NFTW_FUNC_T __nftw_func_t
  68. #endif
  69. #ifdef L_ftw
  70. #include <alloca.h>
  71. #include <errno.h>
  72. #include <ftw.h>
  73. #include <limits.h>
  74. #include <search.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <unistd.h>
  78. #include <sys/param.h>
  79. #include <sys/stat.h>
  80. #include <assert.h>
  81. #include <dirent.h>
  82. #if 1 /*ndef L_ftw64*/
  83. extern struct dirent *__readdir (DIR *__dirp) __nonnull ((1)) attribute_hidden;
  84. #else
  85. extern struct dirent64 *__readdir64 (DIR *__dirp) __nonnull ((1)) attribute_hidden;
  86. #endif
  87. /* We define PATH_MAX if the system does not provide a definition.
  88. This does not artificially limit any operation. PATH_MAX is simply
  89. used as a guesstimate for the expected maximal path length.
  90. Buffers will be enlarged if necessary. */
  91. #ifndef PATH_MAX
  92. # define PATH_MAX 1024
  93. #endif
  94. struct dir_data
  95. {
  96. DIR *stream;
  97. char *content;
  98. };
  99. struct known_object
  100. {
  101. dev_t dev;
  102. INO_T ino;
  103. };
  104. struct ftw_data
  105. {
  106. /* Array with pointers to open directory streams. */
  107. struct dir_data **dirstreams;
  108. size_t actdir;
  109. size_t maxdir;
  110. /* Buffer containing name of currently processed object. */
  111. char *dirbuf;
  112. size_t dirbufsize;
  113. /* Passed as fourth argument to `nftw' callback. The `base' member
  114. tracks the content of the `dirbuf'. */
  115. struct FTW ftw;
  116. /* Flags passed to `nftw' function. 0 for `ftw'. */
  117. int flags;
  118. /* Conversion array for flag values. It is the identity mapping for
  119. `nftw' calls, otherwise it maps the values to those known by
  120. `ftw'. */
  121. const int *cvt_arr;
  122. /* Callback function. We always use the `nftw' form. */
  123. NFTW_FUNC_T func;
  124. /* Device of starting point. Needed for FTW_MOUNT. */
  125. dev_t dev;
  126. /* Data structure for keeping fingerprints of already processed
  127. object. This is needed when not using FTW_PHYS. */
  128. void *known_objects;
  129. };
  130. /* Internally we use the FTW_* constants used for `nftw'. When invoked
  131. as `ftw', map each flag to the subset of values used by `ftw'. */
  132. static const int nftw_arr[] =
  133. {
  134. FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
  135. };
  136. static const int ftw_arr[] =
  137. {
  138. FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_F, FTW_D, FTW_NS
  139. };
  140. /* Forward declarations of local functions. */
  141. static int ftw_dir (struct ftw_data *data, struct STAT *st) internal_function;
  142. static int
  143. object_compare (const void *p1, const void *p2)
  144. {
  145. /* We don't need a sophisticated and useful comparison. We are only
  146. interested in equality. However, we must be careful not to
  147. accidentally compare `holes' in the structure. */
  148. const struct known_object *kp1 = p1, *kp2 = p2;
  149. int cmp1;
  150. cmp1 = (kp1->ino > kp2->ino) - (kp1->ino < kp2->ino);
  151. if (cmp1 != 0)
  152. return cmp1;
  153. return (kp1->dev > kp2->dev) - (kp1->dev < kp2->dev);
  154. }
  155. static inline int
  156. add_object (struct ftw_data *data, struct STAT *st)
  157. {
  158. struct known_object *newp = malloc (sizeof (struct known_object));
  159. if (newp == NULL)
  160. return -1;
  161. newp->dev = st->st_dev;
  162. newp->ino = st->st_ino;
  163. return tsearch (newp, &data->known_objects, object_compare) ? 0 : -1;
  164. }
  165. static inline int
  166. find_object (struct ftw_data *data, struct STAT *st)
  167. {
  168. struct known_object obj;
  169. obj.dev = st->st_dev;
  170. obj.ino = st->st_ino;
  171. return tfind (&obj, &data->known_objects, object_compare) != NULL;
  172. }
  173. static inline int
  174. __attribute ((always_inline))
  175. open_dir_stream (struct ftw_data *data, struct dir_data *dirp)
  176. {
  177. int result = 0;
  178. if (data->dirstreams[data->actdir] != NULL)
  179. {
  180. /* Oh, oh. We must close this stream. Get all remaining
  181. entries and store them as a list in the `content' member of
  182. the `struct dir_data' variable. */
  183. size_t bufsize = 1024;
  184. char *buf = malloc (bufsize);
  185. if (buf == NULL)
  186. result = -1;
  187. else
  188. {
  189. DIR *st = data->dirstreams[data->actdir]->stream;
  190. struct dirent *d;
  191. size_t actsize = 0;
  192. while ((d = __readdir (st)) != NULL)
  193. {
  194. size_t this_len = _D_EXACT_NAMLEN (d);
  195. if (actsize + this_len + 2 >= bufsize)
  196. {
  197. char *newp;
  198. bufsize += MAX (1024, 2 * this_len);
  199. newp = (char *) realloc (buf, bufsize);
  200. if (newp == NULL)
  201. {
  202. /* No more memory. */
  203. int save_err = errno;
  204. free (buf);
  205. __set_errno (save_err);
  206. result = -1;
  207. break;
  208. }
  209. buf = newp;
  210. }
  211. *((char *) mempcpy (buf + actsize, d->d_name, this_len))
  212. = '\0';
  213. actsize += this_len + 1;
  214. }
  215. /* Terminate the list with an additional NUL byte. */
  216. buf[actsize++] = '\0';
  217. /* Shrink the buffer to what we actually need. */
  218. data->dirstreams[data->actdir]->content = realloc (buf, actsize);
  219. if (data->dirstreams[data->actdir]->content == NULL)
  220. {
  221. int save_err = errno;
  222. free (buf);
  223. __set_errno (save_err);
  224. result = -1;
  225. }
  226. else
  227. {
  228. closedir (st);
  229. data->dirstreams[data->actdir]->stream = NULL;
  230. data->dirstreams[data->actdir] = NULL;
  231. }
  232. }
  233. }
  234. /* Open the new stream. */
  235. if (result == 0)
  236. {
  237. const char *name = ((data->flags & FTW_CHDIR)
  238. ? data->dirbuf + data->ftw.base: data->dirbuf);
  239. assert (data->dirstreams[data->actdir] == NULL);
  240. dirp->stream = opendir (name);
  241. if (dirp->stream == NULL)
  242. result = -1;
  243. else
  244. {
  245. dirp->content = NULL;
  246. data->dirstreams[data->actdir] = dirp;
  247. if (++data->actdir == data->maxdir)
  248. data->actdir = 0;
  249. }
  250. }
  251. return result;
  252. }
  253. static int
  254. internal_function
  255. process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, size_t namlen)
  256. {
  257. struct STAT st;
  258. int result = 0;
  259. int flag = 0;
  260. size_t new_buflen;
  261. if (name[0] == '.' && (name[1] == '\0'
  262. || (name[1] == '.' && name[2] == '\0')))
  263. /* Don't process the "." and ".." entries. */
  264. return 0;
  265. new_buflen = data->ftw.base + namlen + 2;
  266. if (data->dirbufsize < new_buflen)
  267. {
  268. /* Enlarge the buffer. */
  269. char *newp;
  270. data->dirbufsize = 2 * new_buflen;
  271. newp = (char *) realloc (data->dirbuf, data->dirbufsize);
  272. if (newp == NULL)
  273. return -1;
  274. data->dirbuf = newp;
  275. }
  276. *((char *) mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
  277. if ((data->flags & FTW_CHDIR) == 0)
  278. name = data->dirbuf;
  279. if (((data->flags & FTW_PHYS)
  280. ? LSTAT (name, &st)
  281. : XSTAT (name, &st)) < 0)
  282. {
  283. if (errno != EACCES && errno != ENOENT)
  284. result = -1;
  285. else if (!(data->flags & FTW_PHYS)
  286. && LSTAT (name, &st) == 0
  287. && S_ISLNK (st.st_mode))
  288. flag = FTW_SLN;
  289. else
  290. flag = FTW_NS;
  291. }
  292. else
  293. {
  294. if (S_ISDIR (st.st_mode))
  295. flag = FTW_D;
  296. else if (S_ISLNK (st.st_mode))
  297. flag = FTW_SL;
  298. else
  299. flag = FTW_F;
  300. }
  301. if (result == 0
  302. && (flag == FTW_NS
  303. || !(data->flags & FTW_MOUNT) || st.st_dev == data->dev))
  304. {
  305. if (flag == FTW_D)
  306. {
  307. if ((data->flags & FTW_PHYS)
  308. || (!find_object (data, &st)
  309. /* Remember the object. */
  310. && (result = add_object (data, &st)) == 0))
  311. {
  312. result = ftw_dir (data, &st);
  313. if (result == 0 && (data->flags & FTW_CHDIR))
  314. {
  315. /* Change back to the parent directory. */
  316. int done = 0;
  317. if (dir->stream != NULL)
  318. if (fchdir (dirfd (dir->stream)) == 0)
  319. done = 1;
  320. if (!done)
  321. {
  322. if (data->ftw.base == 1)
  323. {
  324. if (chdir ("/") < 0)
  325. result = -1;
  326. }
  327. else
  328. if (chdir ("..") < 0)
  329. result = -1;
  330. }
  331. }
  332. }
  333. }
  334. else
  335. result = (*data->func) (data->dirbuf, &st, data->cvt_arr[flag],
  336. &data->ftw);
  337. }
  338. return result;
  339. }
  340. static int
  341. internal_function
  342. ftw_dir (struct ftw_data *data, struct STAT *st)
  343. {
  344. struct dir_data dir;
  345. struct dirent *d;
  346. int previous_base = data->ftw.base;
  347. int result;
  348. char *startp;
  349. /* Open the stream for this directory. This might require that
  350. another stream has to be closed. */
  351. result = open_dir_stream (data, &dir);
  352. if (result != 0)
  353. {
  354. if (errno == EACCES)
  355. /* We cannot read the directory. Signal this with a special flag. */
  356. result = (*data->func) (data->dirbuf, st, FTW_DNR, &data->ftw);
  357. return result;
  358. }
  359. /* First, report the directory (if not depth-first). */
  360. if (!(data->flags & FTW_DEPTH))
  361. {
  362. result = (*data->func) (data->dirbuf, st, FTW_D, &data->ftw);
  363. if (result != 0)
  364. return result;
  365. }
  366. /* If necessary, change to this directory. */
  367. if (data->flags & FTW_CHDIR)
  368. {
  369. if (fchdir (dirfd (dir.stream)) < 0)
  370. {
  371. int save_err = errno;
  372. closedir (dir.stream);
  373. __set_errno (save_err);
  374. if (data->actdir-- == 0)
  375. data->actdir = data->maxdir - 1;
  376. data->dirstreams[data->actdir] = NULL;
  377. return -1;
  378. }
  379. }
  380. /* Next, update the `struct FTW' information. */
  381. ++data->ftw.level;
  382. startp = __strchr (data->dirbuf, '\0');
  383. /* There always must be a directory name. */
  384. assert (startp != data->dirbuf);
  385. if (startp[-1] != '/')
  386. *startp++ = '/';
  387. data->ftw.base = startp - data->dirbuf;
  388. while (dir.stream != NULL && (d = __readdir (dir.stream)) != NULL)
  389. {
  390. result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
  391. if (result != 0)
  392. break;
  393. }
  394. if (dir.stream != NULL)
  395. {
  396. /* The stream is still open. I.e., we did not need more
  397. descriptors. Simply close the stream now. */
  398. int save_err = errno;
  399. assert (dir.content == NULL);
  400. closedir (dir.stream);
  401. __set_errno (save_err);
  402. if (data->actdir-- == 0)
  403. data->actdir = data->maxdir - 1;
  404. data->dirstreams[data->actdir] = NULL;
  405. }
  406. else
  407. {
  408. int save_err;
  409. char *runp = dir.content;
  410. while (result == 0 && *runp != '\0')
  411. {
  412. char *endp = __strchr (runp, '\0');
  413. result = process_entry (data, &dir, runp, endp - runp);
  414. runp = endp + 1;
  415. }
  416. save_err = errno;
  417. free (dir.content);
  418. __set_errno (save_err);
  419. }
  420. /* Prepare the return, revert the `struct FTW' information. */
  421. data->dirbuf[data->ftw.base - 1] = '\0';
  422. --data->ftw.level;
  423. data->ftw.base = previous_base;
  424. /* Finally, if we process depth-first report the directory. */
  425. if (result == 0 && (data->flags & FTW_DEPTH))
  426. result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw);
  427. return result;
  428. }
  429. static int
  430. internal_function
  431. ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, int flags)
  432. {
  433. struct ftw_data data;
  434. struct STAT st;
  435. int result = 0;
  436. int save_err;
  437. char *cwd = NULL;
  438. char *cp;
  439. /* First make sure the parameters are reasonable. */
  440. if (unlikely(dir==NULL || *dir=='\0')) {
  441. __set_errno (ENOENT);
  442. return -1;
  443. }
  444. if ((__strlen(dir)+1) > NAME_MAX) {
  445. __set_errno(ENAMETOOLONG);
  446. return -1;
  447. }
  448. data.maxdir = descriptors < 1 ? 1 : descriptors;
  449. data.actdir = 0;
  450. data.dirstreams = (struct dir_data **) alloca (data.maxdir
  451. * sizeof (struct dir_data *));
  452. __memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
  453. /* PATH_MAX is always defined when we get here. */
  454. data.dirbufsize = MAX (2 * __strlen (dir), PATH_MAX);
  455. data.dirbuf = (char *) malloc (data.dirbufsize);
  456. if (data.dirbuf == NULL)
  457. return -1;
  458. cp = stpcpy (data.dirbuf, dir);
  459. /* Strip trailing slashes. */
  460. while (cp > data.dirbuf + 1 && cp[-1] == '/')
  461. --cp;
  462. *cp = '\0';
  463. data.ftw.level = 0;
  464. /* Find basename. */
  465. while (cp > data.dirbuf && cp[-1] != '/')
  466. --cp;
  467. data.ftw.base = cp - data.dirbuf;
  468. data.flags = flags;
  469. /* This assignment might seem to be strange but it is what we want.
  470. The trick is that the first three arguments to the `ftw' and
  471. `nftw' callback functions are equal. Therefore we can call in
  472. every case the callback using the format of the `nftw' version
  473. and get the correct result since the stack layout for a function
  474. call in C allows this. */
  475. data.func = (NFTW_FUNC_T) func;
  476. /* Since we internally use the complete set of FTW_* values we need
  477. to reduce the value range before calling a `ftw' callback. */
  478. data.cvt_arr = is_nftw ? nftw_arr : ftw_arr;
  479. /* No object known so far. */
  480. data.known_objects = NULL;
  481. /* Now go to the directory containing the initial file/directory. */
  482. if (flags & FTW_CHDIR)
  483. {
  484. /* GNU extension ahead. */
  485. cwd = getcwd (NULL, 0);
  486. if (cwd == NULL)
  487. result = -1;
  488. else if (data.ftw.base > 0)
  489. {
  490. /* Change to the directory the file is in. In data.dirbuf
  491. we have a writable copy of the file name. Just NUL
  492. terminate it for now and change the directory. */
  493. if (data.ftw.base == 1)
  494. /* I.e., the file is in the root directory. */
  495. result = chdir ("/");
  496. else
  497. {
  498. char ch = data.dirbuf[data.ftw.base - 1];
  499. data.dirbuf[data.ftw.base - 1] = '\0';
  500. result = chdir (data.dirbuf);
  501. data.dirbuf[data.ftw.base - 1] = ch;
  502. }
  503. }
  504. }
  505. /* Get stat info for start directory. */
  506. if (result == 0)
  507. {
  508. const char *name = ((data.flags & FTW_CHDIR)
  509. ? data.dirbuf + data.ftw.base
  510. : data.dirbuf);
  511. if (((flags & FTW_PHYS)
  512. ? LSTAT (name, &st)
  513. : XSTAT (name, &st)) < 0)
  514. {
  515. if (!(flags & FTW_PHYS)
  516. && errno == ENOENT
  517. && LSTAT (name, &st) == 0
  518. && S_ISLNK (st.st_mode))
  519. result = (*data.func) (data.dirbuf, &st, data.cvt_arr[FTW_SLN],
  520. &data.ftw);
  521. else
  522. /* No need to call the callback since we cannot say anything
  523. about the object. */
  524. result = -1;
  525. }
  526. else
  527. {
  528. if (S_ISDIR (st.st_mode))
  529. {
  530. /* Remember the device of the initial directory in case
  531. FTW_MOUNT is given. */
  532. data.dev = st.st_dev;
  533. /* We know this directory now. */
  534. if (!(flags & FTW_PHYS))
  535. result = add_object (&data, &st);
  536. if (result == 0)
  537. result = ftw_dir (&data, &st);
  538. }
  539. else
  540. {
  541. int flag = S_ISLNK (st.st_mode) ? FTW_SL : FTW_F;
  542. result = (*data.func) (data.dirbuf, &st, data.cvt_arr[flag],
  543. &data.ftw);
  544. }
  545. }
  546. }
  547. /* Return to the start directory (if necessary). */
  548. if (cwd != NULL)
  549. {
  550. int save_err = errno;
  551. chdir (cwd);
  552. free (cwd);
  553. __set_errno (save_err);
  554. }
  555. /* Free all memory. */
  556. save_err = errno;
  557. tdestroy (data.known_objects, free);
  558. free (data.dirbuf);
  559. __set_errno (save_err);
  560. return result;
  561. }
  562. /* Entry points. */
  563. int FTW_NAME (const char *path, FTW_FUNC_T func, int descriptors)
  564. {
  565. return ftw_startup (path, 0, func, descriptors, 0);
  566. }
  567. int NFTW_NAME (const char *path, NFTW_FUNC_T func, int descriptors, int flags)
  568. {
  569. return ftw_startup (path, 1, func, descriptors, flags);
  570. }
  571. #endif