ftw.c 16 KB

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