ftw.c 16 KB

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