You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB

10 years ago
  1. /* -*- coding: utf-8 -*-
  2. * ----------------------------------------------------------------------
  3. * Copyright © 2012, RedJack, LLC.
  4. * All rights reserved.
  5. *
  6. * Please see the COPYING file in this distribution for license
  7. * details.
  8. * ----------------------------------------------------------------------
  9. */
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include "libcork/core/attributes.h"
  18. #include "libcork/core/error.h"
  19. #include "libcork/core/types.h"
  20. #include "libcork/ds/buffer.h"
  21. #include "libcork/helpers/errors.h"
  22. #include "libcork/helpers/posix.h"
  23. #include "libcork/os/files.h"
  24. static int
  25. cork_walk_one_directory(struct cork_dir_walker *w, struct cork_buffer *path,
  26. size_t root_path_size)
  27. {
  28. DIR *dir = NULL;
  29. struct dirent *entry;
  30. size_t dir_path_size;
  31. rip_check_posix(dir = opendir(path->buf));
  32. cork_buffer_append(path, "/", 1);
  33. dir_path_size = path->size;
  34. errno = 0;
  35. while ((entry = readdir(dir)) != NULL) {
  36. struct stat info;
  37. /* Skip the "." and ".." entries */
  38. if (strcmp(entry->d_name, ".") == 0 ||
  39. strcmp(entry->d_name, "..") == 0) {
  40. continue;
  41. }
  42. /* Stat the directory entry */
  43. cork_buffer_append_string(path, entry->d_name);
  44. ei_check_posix(stat(path->buf, &info));
  45. /* If the entry is a subdirectory, recurse into it. */
  46. if (S_ISDIR(info.st_mode)) {
  47. int rc = cork_dir_walker_enter_directory
  48. (w, path->buf, path->buf + root_path_size,
  49. path->buf + dir_path_size);
  50. if (rc != CORK_SKIP_DIRECTORY) {
  51. ei_check(cork_walk_one_directory(w, path, root_path_size));
  52. ei_check(cork_dir_walker_leave_directory
  53. (w, path->buf, path->buf + root_path_size,
  54. path->buf + dir_path_size));
  55. }
  56. } else if (S_ISREG(info.st_mode)) {
  57. ei_check(cork_dir_walker_file
  58. (w, path->buf, path->buf + root_path_size,
  59. path->buf + dir_path_size));
  60. }
  61. /* Remove this entry name from the path buffer. */
  62. cork_buffer_truncate(path, dir_path_size);
  63. /* We have to reset errno to 0 because of the ambiguous way
  64. * readdir uses a return value of NULL. Other functions may
  65. * return normally yet set errno to a non-zero value. dlopen
  66. * on Mac OS X is an ogreish example. Since an error readdir
  67. * is indicated by returning NULL and setting errno to indicate
  68. * the error, then we need to reset it to zero before each call.
  69. * We shall assume, perhaps to our great misery, that functions
  70. * within this loop do proper error checking and act accordingly.
  71. */
  72. errno = 0;
  73. }
  74. /* Check errno immediately after the while loop terminates */
  75. if (CORK_UNLIKELY(errno != 0)) {
  76. cork_system_error_set();
  77. goto error;
  78. }
  79. /* Remove the trailing '/' from the path buffer. */
  80. cork_buffer_truncate(path, dir_path_size - 1);
  81. rii_check_posix(closedir(dir));
  82. return 0;
  83. error:
  84. if (dir != NULL) {
  85. rii_check_posix(closedir(dir));
  86. }
  87. return -1;
  88. }
  89. int
  90. cork_walk_directory(const char *path, struct cork_dir_walker *w)
  91. {
  92. int rc;
  93. char *p;
  94. struct cork_buffer buf = CORK_BUFFER_INIT();
  95. /* Seed the buffer with the directory's path, ensuring that there's no
  96. * trailing '/' */
  97. cork_buffer_append_string(&buf, path);
  98. p = buf.buf;
  99. while (p[buf.size-1] == '/') {
  100. buf.size--;
  101. p[buf.size] = '\0';
  102. }
  103. rc = cork_walk_one_directory(w, &buf, buf.size + 1);
  104. cork_buffer_done(&buf);
  105. return rc;
  106. }