ftw.c 16 KB

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