fts_subs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* $MirOS: contrib/hosted/fwcf/fts_subs.c,v 1.8 2007/07/02 14:53:03 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <sys/param.h>
  23. #include <sys/stat.h>
  24. #include <err.h>
  25. #include <errno.h>
  26. #if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__APPLE__)
  27. #include "fts_gnu.h"
  28. #else
  29. #include <fts.h>
  30. #endif
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "defs.h"
  34. #define FTSF_INTERNALS
  35. #include "fts_subs.h"
  36. __RCSID("$MirOS: contrib/hosted/fwcf/fts_subs.c,v 1.8 2007/07/02 14:53:03 tg Exp $");
  37. static FTS *handle;
  38. char ftsf_prefix[PATH_MAX];
  39. size_t ftsf_prefixlen;
  40. void
  41. ftsf_start(const char *root)
  42. {
  43. char *paths[2];
  44. if (handle != NULL) {
  45. if (fts_close(handle))
  46. warn("fts_close");
  47. free(handle);
  48. }
  49. if (realpath(root, ftsf_prefix) == NULL)
  50. err(1, "determine realpath for %s", root);
  51. ftsf_prefixlen = strlen(ftsf_prefix) + 1;
  52. paths[0] = ftsf_prefix;
  53. paths[1] = NULL;
  54. handle = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
  55. if (handle == NULL)
  56. err(1, "fts_open on %s for %s", ftsf_prefix, root);
  57. }
  58. int
  59. ftsf_next(ftsf_entry *e)
  60. {
  61. FTSENT *ent;
  62. char *ename;
  63. memset(e, 0, sizeof (ftsf_entry));
  64. if (handle == NULL) {
  65. warn("ftsf_next called before ftsf_open");
  66. return (-1);
  67. }
  68. ftsf_continue:
  69. if ((ent = fts_read(handle)) == NULL) {
  70. if (errno == 0) {
  71. if (fts_close(handle))
  72. warn("fts_close");
  73. handle = NULL;
  74. return (0);
  75. }
  76. warn("fts_read");
  77. if (fts_close(handle))
  78. warn("fts_close");
  79. handle = NULL;
  80. return (-1);
  81. }
  82. ename = ent->fts_path;
  83. #if 0 /* debug */
  84. if (asprintf(&ename, "accpath<%s> path<%s> name<%s>", ent->fts_accpath,
  85. ent->fts_path, ent->fts_name) == -1)
  86. err(1, "asprintf");
  87. #endif
  88. if (strcmp(ename, ftsf_prefix)) {
  89. if (strlcpy(e->pathname, ename + ftsf_prefixlen,
  90. sizeof (e->pathname)) >= sizeof (e->pathname))
  91. warn("truncating file name '%s' to '%s'", ename,
  92. e->pathname);
  93. } else
  94. memcpy(e->pathname, ".", 2);
  95. switch (ent->fts_info) {
  96. case FTS_D:
  97. e->etype = FTSF_DIR;
  98. break;
  99. case FTS_DNR:
  100. warn("directory %s not readable, skipping: %s", ename,
  101. strerror(ent->fts_errno));
  102. /* FALLTHROUGH */
  103. case FTS_DC:
  104. case FTS_DOT:
  105. case FTS_DP:
  106. goto ftsf_continue;
  107. case FTS_DEFAULT:
  108. if (S_ISDIR(ent->fts_statp->st_mode))
  109. e->etype = FTSF_DIR;
  110. else if (S_ISREG(ent->fts_statp->st_mode))
  111. e->etype = FTSF_FILE;
  112. else if (S_ISLNK(ent->fts_statp->st_mode))
  113. e->etype = FTSF_SYMLINK;
  114. else
  115. e->etype = FTSF_OTHER;
  116. break;
  117. case FTS_ERR:
  118. warn("generic error condition %s on %s, skipping",
  119. strerror(ent->fts_errno), ename);
  120. goto ftsf_continue;
  121. case FTS_F:
  122. e->etype = FTSF_FILE;
  123. break;
  124. case FTS_NSOK:
  125. ent->fts_errno = 0;
  126. /* FALLTHROUGH */
  127. case FTS_NS:
  128. warn("skipping due to no stat(2) information on %s: %s",
  129. ename, strerror(ent->fts_errno));
  130. goto ftsf_continue;
  131. case FTS_SL:
  132. case FTS_SLNONE:
  133. e->etype = FTSF_SYMLINK;
  134. break;
  135. default:
  136. warn("unknown fts_info field for %s: %d, skipping",
  137. ename, (int)ent->fts_info);
  138. warn("ent->fts_errno = %d (%s)", ent->fts_errno,
  139. strerror(ent->fts_errno));
  140. goto ftsf_continue;
  141. }
  142. e->statp = ent->fts_statp;
  143. return (1);
  144. }