fts_subs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. static FTS *handle;
  37. char ftsf_prefix[PATH_MAX];
  38. size_t ftsf_prefixlen;
  39. void
  40. ftsf_start(const char *root)
  41. {
  42. char *paths[2];
  43. if (handle != NULL) {
  44. if (fts_close(handle))
  45. warn("fts_close");
  46. free(handle);
  47. }
  48. if (realpath(root, ftsf_prefix) == NULL)
  49. err(1, "determine realpath for %s", root);
  50. ftsf_prefixlen = strlen(ftsf_prefix) + 1;
  51. paths[0] = ftsf_prefix;
  52. paths[1] = NULL;
  53. handle = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
  54. if (handle == NULL)
  55. err(1, "fts_open on %s for %s", ftsf_prefix, root);
  56. }
  57. int
  58. ftsf_next(ftsf_entry *e)
  59. {
  60. FTSENT *ent;
  61. char *ename;
  62. memset(e, 0, sizeof (ftsf_entry));
  63. if (handle == NULL) {
  64. warn("ftsf_next called before ftsf_open");
  65. return (-1);
  66. }
  67. ftsf_continue:
  68. if ((ent = fts_read(handle)) == NULL) {
  69. if (errno == 0) {
  70. if (fts_close(handle))
  71. warn("fts_close");
  72. handle = NULL;
  73. return (0);
  74. }
  75. warn("fts_read");
  76. if (fts_close(handle))
  77. warn("fts_close");
  78. handle = NULL;
  79. return (-1);
  80. }
  81. ename = ent->fts_path;
  82. #if 0 /* debug */
  83. if (asprintf(&ename, "accpath<%s> path<%s> name<%s>", ent->fts_accpath,
  84. ent->fts_path, ent->fts_name) == -1)
  85. err(1, "asprintf");
  86. #endif
  87. if (strcmp(ename, ftsf_prefix)) {
  88. if (strlcpy(e->pathname, ename + ftsf_prefixlen,
  89. sizeof (e->pathname)) >= sizeof (e->pathname))
  90. warn("truncating file name '%s' to '%s'", ename,
  91. e->pathname);
  92. } else
  93. memcpy(e->pathname, ".", 2);
  94. switch (ent->fts_info) {
  95. case FTS_D:
  96. e->etype = FTSF_DIR;
  97. break;
  98. case FTS_DNR:
  99. warn("directory %s not readable, skipping: %s", ename,
  100. strerror(ent->fts_errno));
  101. /* FALLTHROUGH */
  102. case FTS_DC:
  103. case FTS_DOT:
  104. case FTS_DP:
  105. goto ftsf_continue;
  106. case FTS_DEFAULT:
  107. if (S_ISDIR(ent->fts_statp->st_mode))
  108. e->etype = FTSF_DIR;
  109. else if (S_ISREG(ent->fts_statp->st_mode))
  110. e->etype = FTSF_FILE;
  111. else if (S_ISLNK(ent->fts_statp->st_mode))
  112. e->etype = FTSF_SYMLINK;
  113. else
  114. e->etype = FTSF_OTHER;
  115. break;
  116. case FTS_ERR:
  117. warn("generic error condition %s on %s, skipping",
  118. strerror(ent->fts_errno), ename);
  119. goto ftsf_continue;
  120. case FTS_F:
  121. e->etype = FTSF_FILE;
  122. break;
  123. case FTS_NSOK:
  124. ent->fts_errno = 0;
  125. /* FALLTHROUGH */
  126. case FTS_NS:
  127. warn("skipping due to no stat(2) information on %s: %s",
  128. ename, strerror(ent->fts_errno));
  129. goto ftsf_continue;
  130. case FTS_SL:
  131. case FTS_SLNONE:
  132. e->etype = FTSF_SYMLINK;
  133. break;
  134. default:
  135. warn("unknown fts_info field for %s: %d, skipping",
  136. ename, (int)ent->fts_info);
  137. warn("ent->fts_errno = %d (%s)", ent->fts_errno,
  138. strerror(ent->fts_errno));
  139. goto ftsf_continue;
  140. }
  141. e->statp = ent->fts_statp;
  142. return (1);
  143. }