fts_compat.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Copyright (c) 2003-2006 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /* This file implements just enough of the fts family of functions
  20. to make Polipo happy. */
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #ifndef _WIN32
  25. #include <unistd.h>
  26. #include <dirent.h>
  27. #else
  28. #include "dirent_compat.h"
  29. #endif
  30. #include <sys/stat.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. #include "fts_compat.h"
  34. static char *
  35. getcwd_a()
  36. {
  37. char buf[256];
  38. char *ret;
  39. ret = getcwd(buf, 256);
  40. if(ret == NULL)
  41. return NULL;
  42. return strdup(buf);
  43. }
  44. static char *
  45. mkfilename(const char *path, char *filename)
  46. {
  47. int n = strlen(path);
  48. char *buf = malloc(n + 1 + strlen(filename) + 1);
  49. if(buf == NULL)
  50. return NULL;
  51. memcpy(buf, path, n);
  52. if(buf[n - 1] != '/')
  53. buf[n++] = '/';
  54. strcpy(buf + n, filename);
  55. return buf;
  56. }
  57. static int
  58. split(const char *path, int *slash_return, int *dlen, int *blen)
  59. {
  60. int len; int slash;
  61. len = strlen(path);
  62. while(len > 0 && path[len - 1] == '/')
  63. len--;
  64. if(len == 0)
  65. return -1;
  66. slash = len - 1;
  67. while(slash >= 0 && path[slash] != '/')
  68. slash--;
  69. if(slash_return) *slash_return = slash;
  70. if(dlen) *dlen = slash + 1;
  71. if(blen) *blen = len - slash - 1;
  72. return 1;
  73. }
  74. static char *
  75. basename_a(const char *path)
  76. {
  77. int blen, slash;
  78. char *b;
  79. int rc;
  80. rc = split(path, &slash, NULL, &blen);
  81. if(rc < 0 || blen == 0)
  82. return NULL;
  83. b = malloc(blen + 1);
  84. if(b == NULL)
  85. return NULL;
  86. memcpy(b, path + slash + 1, blen);
  87. b[blen] = '\0';
  88. return b;
  89. }
  90. static char *
  91. dirname_a(const char *path)
  92. {
  93. int dlen;
  94. int rc;
  95. char *d;
  96. rc = split(path, NULL, &dlen, NULL);
  97. if(rc < 0)
  98. return NULL;
  99. d = malloc(dlen + 1);
  100. if(d == NULL)
  101. return NULL;
  102. memcpy(d, path, dlen);
  103. d[dlen] = '\0';
  104. return d;
  105. }
  106. #if defined(__svr4__) || defined(SVR4)
  107. static int
  108. dirfd(DIR *dir)
  109. {
  110. return dir->dd_fd;
  111. }
  112. #endif
  113. /*
  114. * Make the directory identified by the argument the current directory.
  115. */
  116. #ifdef WIN32 /*MINGW*/
  117. int
  118. change_to_dir(DIR *dir)
  119. {
  120. errno = ENOSYS;
  121. return -1;
  122. }
  123. #else
  124. int
  125. change_to_dir(DIR *dir)
  126. {
  127. return fchdir(dirfd(dir));
  128. }
  129. #endif
  130. FTS*
  131. fts_open(char * const *path_argv, int options,
  132. int (*compar)(const FTSENT **, const FTSENT **))
  133. {
  134. FTS *fts;
  135. DIR *dir;
  136. char *cwd;
  137. int rc;
  138. if(options != FTS_LOGICAL || compar != NULL || path_argv[1] != NULL) {
  139. errno = ENOSYS;
  140. return NULL;
  141. }
  142. dir = opendir(path_argv[0]);
  143. if(dir == NULL)
  144. return NULL;
  145. fts = calloc(sizeof(FTS), 1);
  146. if(fts == NULL) {
  147. int save = errno;
  148. closedir(dir);
  149. errno = save;
  150. return NULL;
  151. }
  152. cwd = getcwd_a();
  153. if(cwd == NULL) {
  154. int save = errno;
  155. free(fts);
  156. closedir(dir);
  157. errno = save;
  158. return NULL;
  159. }
  160. rc = change_to_dir(dir);
  161. if(rc < 0) {
  162. int save = errno;
  163. free(cwd);
  164. free(fts);
  165. closedir(dir);
  166. errno = save;
  167. return NULL;
  168. }
  169. fts->depth = 0;
  170. fts->dir[0] = dir;
  171. fts->cwd0 = cwd;
  172. fts->cwd = strdup(path_argv[0]);
  173. return fts;
  174. }
  175. int
  176. fts_close(FTS *fts)
  177. {
  178. int save = 0;
  179. int rc;
  180. if(fts->ftsent.fts_path) {
  181. free(fts->ftsent.fts_path);
  182. fts->ftsent.fts_path = NULL;
  183. }
  184. if(fts->dname) {
  185. free(fts->dname);
  186. fts->dname = NULL;
  187. }
  188. rc = chdir(fts->cwd0);
  189. if(rc < 0)
  190. save = errno;
  191. while(fts->depth >= 0) {
  192. closedir(fts->dir[fts->depth]);
  193. fts->depth--;
  194. }
  195. free(fts->cwd0);
  196. if(fts->cwd) free(fts->cwd);
  197. free(fts);
  198. if(rc < 0) {
  199. errno = save;
  200. return -1;
  201. }
  202. return 0;
  203. }
  204. FTSENT *
  205. fts_read(FTS *fts)
  206. {
  207. struct dirent *dirent;
  208. int rc;
  209. char *name;
  210. char buf[1024];
  211. if(fts->ftsent.fts_path) {
  212. free(fts->ftsent.fts_path);
  213. fts->ftsent.fts_path = NULL;
  214. }
  215. if(fts->dname) {
  216. free(fts->dname);
  217. fts->dname = NULL;
  218. }
  219. again:
  220. dirent = readdir(fts->dir[fts->depth]);
  221. if(dirent == NULL) {
  222. char *newcwd = NULL;
  223. closedir(fts->dir[fts->depth]);
  224. fts->dir[fts->depth] = NULL;
  225. fts->depth--;
  226. if(fts->depth >= 0) {
  227. fts->dname = basename_a(fts->cwd);
  228. if(fts->dname == NULL)
  229. goto error;
  230. newcwd = dirname_a(fts->cwd);
  231. if(newcwd == NULL)
  232. goto error;
  233. }
  234. if(fts->cwd) free(fts->cwd);
  235. fts->cwd = NULL;
  236. if(fts->depth < 0)
  237. return NULL;
  238. rc = change_to_dir(fts->dir[fts->depth]);
  239. if(rc < 0) {
  240. free(newcwd);
  241. goto error;
  242. }
  243. fts->cwd = newcwd;
  244. name = fts->dname;
  245. fts->ftsent.fts_info = FTS_DP;
  246. goto done;
  247. }
  248. name = dirent->d_name;
  249. again2:
  250. rc = stat(name, &fts->ftstat);
  251. if(rc < 0) {
  252. fts->ftsent.fts_info = FTS_NS;
  253. goto error2;
  254. }
  255. if(S_ISDIR(fts->ftstat.st_mode)) {
  256. char *newcwd;
  257. DIR *dir;
  258. if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
  259. goto again;
  260. if(fts->depth >= FTS_MAX_DEPTH) {
  261. errno = ENFILE;
  262. goto error;
  263. }
  264. dir = opendir(dirent->d_name);
  265. if(dir == NULL) {
  266. if(errno == EACCES) {
  267. fts->ftsent.fts_info = FTS_DNR;
  268. goto error2;
  269. } else
  270. goto error;
  271. }
  272. newcwd = mkfilename(fts->cwd, dirent->d_name);
  273. rc = change_to_dir(dir);
  274. if(rc < 0) {
  275. free(newcwd);
  276. goto error;
  277. }
  278. free(fts->cwd);
  279. fts->cwd = newcwd;
  280. fts->ftsent.fts_info = FTS_D;
  281. fts->depth++;
  282. fts->dir[fts->depth] = dir;
  283. goto done;
  284. } else if(S_ISREG(fts->ftstat.st_mode)) {
  285. fts->ftsent.fts_info = FTS_F;
  286. goto done;
  287. #ifdef S_ISLNK
  288. } else if(S_ISLNK(fts->ftstat.st_mode)) {
  289. int rc;
  290. rc = readlink(name, buf, 1024);
  291. if(rc < 0)
  292. goto error;
  293. if(rc >= 1023) {
  294. errno = ENAMETOOLONG;
  295. goto error;
  296. }
  297. buf[rc] = '\0';
  298. name = buf;
  299. if(access(buf, F_OK) >= 0)
  300. goto again2;
  301. fts->ftsent.fts_info = FTS_SLNONE;
  302. goto done;
  303. #endif
  304. } else {
  305. fts->ftsent.fts_info = FTS_DEFAULT;
  306. goto done;
  307. }
  308. done:
  309. if(fts->cwd == NULL)
  310. fts->cwd = getcwd_a();
  311. if(fts->cwd == NULL) goto error;
  312. fts->ftsent.fts_path = mkfilename(fts->cwd, name);
  313. if(fts->ftsent.fts_path == NULL) goto error;
  314. fts->ftsent.fts_accpath = name;
  315. fts->ftsent.fts_statp = &fts->ftstat;
  316. return &fts->ftsent;
  317. error:
  318. fts->ftsent.fts_info = FTS_ERR;
  319. error2:
  320. fts->ftsent.fts_errno = errno;
  321. return &fts->ftsent;
  322. }