ftw.c 16 KB

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