ftw.c 16 KB

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