dirent.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <AK/Vector.h>
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <syscall.h>
  18. #include <unistd.h>
  19. extern "C" {
  20. DIR* opendir(const char* name)
  21. {
  22. int fd = open(name, O_RDONLY | O_DIRECTORY);
  23. if (fd == -1)
  24. return nullptr;
  25. DIR* dirp = (DIR*)malloc(sizeof(DIR));
  26. dirp->fd = fd;
  27. dirp->buffer = nullptr;
  28. dirp->buffer_size = 0;
  29. dirp->nextptr = nullptr;
  30. return dirp;
  31. }
  32. int closedir(DIR* dirp)
  33. {
  34. if (!dirp || dirp->fd == -1)
  35. return -EBADF;
  36. free(dirp->buffer);
  37. int rc = close(dirp->fd);
  38. if (rc == 0)
  39. dirp->fd = -1;
  40. free(dirp);
  41. return rc;
  42. }
  43. void rewinddir(DIR* dirp)
  44. {
  45. free(dirp->buffer);
  46. dirp->buffer = nullptr;
  47. dirp->buffer_size = 0;
  48. dirp->nextptr = nullptr;
  49. lseek(dirp->fd, 0, SEEK_SET);
  50. }
  51. struct [[gnu::packed]] sys_dirent {
  52. ino_t ino;
  53. u8 file_type;
  54. u32 namelen;
  55. char name[];
  56. size_t total_size()
  57. {
  58. return sizeof(ino_t) + sizeof(u8) + sizeof(u32) + sizeof(char) * namelen;
  59. }
  60. };
  61. static void create_struct_dirent(sys_dirent* sys_ent, struct dirent* str_ent)
  62. {
  63. str_ent->d_ino = sys_ent->ino;
  64. str_ent->d_type = sys_ent->file_type;
  65. str_ent->d_off = 0;
  66. str_ent->d_reclen = sizeof(struct dirent);
  67. VERIFY(sizeof(str_ent->d_name) > sys_ent->namelen);
  68. // Note: We can't use any normal string function as sys_ent->name is
  69. // not null terminated. All string copy functions will attempt to read
  70. // the non-existent null terminator past the end of the source string.
  71. memcpy(str_ent->d_name, sys_ent->name, sys_ent->namelen);
  72. str_ent->d_name[sys_ent->namelen] = '\0';
  73. }
  74. static int allocate_dirp_buffer(DIR* dirp)
  75. {
  76. if (dirp->buffer) {
  77. return 0;
  78. }
  79. struct stat st;
  80. // preserve errno since this could be a reentrant call
  81. int old_errno = errno;
  82. int rc = fstat(dirp->fd, &st);
  83. if (rc < 0) {
  84. int new_errno = errno;
  85. errno = old_errno;
  86. return new_errno;
  87. }
  88. size_t size_to_allocate = max(st.st_size, static_cast<off_t>(4096));
  89. dirp->buffer = (char*)malloc(size_to_allocate);
  90. if (!dirp->buffer)
  91. return ENOMEM;
  92. for (;;) {
  93. ssize_t nread = syscall(SC_get_dir_entries, dirp->fd, dirp->buffer, size_to_allocate);
  94. if (nread < 0) {
  95. if (nread == -EINVAL) {
  96. size_to_allocate *= 2;
  97. char* new_buffer = (char*)realloc(dirp->buffer, size_to_allocate);
  98. if (new_buffer) {
  99. dirp->buffer = new_buffer;
  100. continue;
  101. } else {
  102. nread = -ENOMEM;
  103. }
  104. }
  105. // uh-oh, the syscall returned an error
  106. free(dirp->buffer);
  107. dirp->buffer = nullptr;
  108. return -nread;
  109. }
  110. dirp->buffer_size = nread;
  111. dirp->nextptr = dirp->buffer;
  112. break;
  113. }
  114. return 0;
  115. }
  116. dirent* readdir(DIR* dirp)
  117. {
  118. if (!dirp)
  119. return nullptr;
  120. if (dirp->fd == -1)
  121. return nullptr;
  122. if (int new_errno = allocate_dirp_buffer(dirp)) {
  123. // readdir is allowed to mutate errno
  124. errno = new_errno;
  125. return nullptr;
  126. }
  127. if (dirp->nextptr >= (dirp->buffer + dirp->buffer_size))
  128. return nullptr;
  129. auto* sys_ent = (sys_dirent*)dirp->nextptr;
  130. create_struct_dirent(sys_ent, &dirp->cur_ent);
  131. dirp->nextptr += sys_ent->total_size();
  132. return &dirp->cur_ent;
  133. }
  134. static bool compare_sys_struct_dirent(sys_dirent* sys_ent, struct dirent* str_ent)
  135. {
  136. size_t namelen = min((size_t)256, sys_ent->namelen);
  137. // These fields are guaranteed by create_struct_dirent to be the same
  138. return sys_ent->ino == str_ent->d_ino
  139. && sys_ent->file_type == str_ent->d_type
  140. && sys_ent->total_size() == str_ent->d_reclen
  141. && strncmp(sys_ent->name, str_ent->d_name, namelen) == 0;
  142. }
  143. int readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result)
  144. {
  145. if (!dirp || dirp->fd == -1) {
  146. *result = nullptr;
  147. return EBADF;
  148. }
  149. if (int new_errno = allocate_dirp_buffer(dirp)) {
  150. *result = nullptr;
  151. return new_errno;
  152. }
  153. // This doesn't care about dirp state; seek until we find the entry.
  154. // Unfortunately, we can't just compare struct dirent to sys_dirent, so
  155. // manually compare the fields. This seems a bit risky, but could work.
  156. auto* buffer = dirp->buffer;
  157. auto* sys_ent = (sys_dirent*)buffer;
  158. bool found = false;
  159. while (!(found || buffer >= dirp->buffer + dirp->buffer_size)) {
  160. found = compare_sys_struct_dirent(sys_ent, entry);
  161. // Make sure if we found one, it's the one after (end of buffer or not)
  162. buffer += sys_ent->total_size();
  163. sys_ent = (sys_dirent*)buffer;
  164. }
  165. // If we found one, but hit end of buffer, then EOD
  166. if (found && buffer >= dirp->buffer + dirp->buffer_size) {
  167. *result = nullptr;
  168. return 0;
  169. }
  170. // If we never found a match for entry in buffer, start from the beginning
  171. else if (!found) {
  172. buffer = dirp->buffer;
  173. sys_ent = (sys_dirent*)buffer;
  174. }
  175. *result = entry;
  176. create_struct_dirent(sys_ent, entry);
  177. return 0;
  178. }
  179. int dirfd(DIR* dirp)
  180. {
  181. VERIFY(dirp);
  182. return dirp->fd;
  183. }
  184. int scandir(const char* dir_name,
  185. struct dirent*** namelist,
  186. int (*select)(const struct dirent*),
  187. int (*compare)(const struct dirent**, const struct dirent**))
  188. {
  189. auto dir = opendir(dir_name);
  190. if (dir == nullptr)
  191. return -1;
  192. ScopeGuard guard = [&] {
  193. closedir(dir);
  194. };
  195. Vector<struct dirent*> tmp_names;
  196. ScopeGuard names_guard = [&] {
  197. tmp_names.remove_all_matching([&](auto& entry) {
  198. free(entry);
  199. return true;
  200. });
  201. };
  202. while (true) {
  203. errno = 0;
  204. auto entry = readdir(dir);
  205. if (!entry)
  206. break;
  207. // Omit entries the caller chooses to ignore.
  208. if (select && !select(entry))
  209. continue;
  210. auto entry_copy = (struct dirent*)malloc(entry->d_reclen);
  211. if (!entry_copy)
  212. break;
  213. memcpy(entry_copy, entry, entry->d_reclen);
  214. tmp_names.append(entry_copy);
  215. }
  216. // Propagate any errors encountered while accumulating back to the user.
  217. if (errno) {
  218. return -1;
  219. }
  220. // Sort the entries if the user provided a comparator.
  221. if (compare) {
  222. qsort(tmp_names.data(), tmp_names.size(), sizeof(struct dirent*), (int (*)(const void*, const void*))compare);
  223. }
  224. const int size = tmp_names.size();
  225. auto names = (struct dirent**)malloc(size * sizeof(struct dirent*));
  226. for (auto i = 0; i < size; i++) {
  227. names[i] = tmp_names[i];
  228. }
  229. // Disable the scope guard which free's names on error.
  230. tmp_names.clear();
  231. *namelist = names;
  232. return size;
  233. }
  234. }