ftw.c 16 KB

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