dirent.cpp 7.0 KB

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