ftw.c 16 KB

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