|
| 1 | +import "stdlib/list.jou" |
| 2 | +import "stdlib/mem.jou" |
| 3 | +import "stdlib/str.jou" |
| 4 | + |
| 5 | +if WINDOWS: |
| 6 | + import "stdlib/assert.jou" |
| 7 | + |
| 8 | + # TODO: This should really use W functions, not A functions. |
| 9 | + # But then we would need a way to convert between byte* and uint16*. |
| 10 | + # I have an idea for that (WTF-8) but I haven't implemented it yet. |
| 11 | + # The rest of the standard library will need changes too. |
| 12 | + class WIN32_FIND_DATAA: |
| 13 | + dwFileAttributes: uint32 |
| 14 | + ftCreationTime: uint32[2] |
| 15 | + ftLastAccessTime: uint32[2] |
| 16 | + ftLastWriteTime: uint32[2] |
| 17 | + nFileSizeHigh: uint32 |
| 18 | + nFileSizeLow: uint32 |
| 19 | + dwReserved0: uint32 |
| 20 | + dwReserved1: uint32 |
| 21 | + cFileName: byte[260] # TODO: this can be quite limiting |
| 22 | + cAlternateFileName: byte[14] |
| 23 | + |
| 24 | + declare FindFirstFileA(FileName: byte*, FindFileData: WIN32_FIND_DATAA*) -> int64 |
| 25 | + declare FindNextFileA(hFindFile: int64, FindFileData: WIN32_FIND_DATAA*) -> int |
| 26 | + declare FindClose(hFindFile: int64) -> int |
| 27 | + const INVALID_HANDLE_VALUE: int64 = -1 |
| 28 | + |
| 29 | + declare GetLastError() -> uint32 |
| 30 | + const ERROR_PATH_NOT_FOUND: uint32 = 3 |
| 31 | + const ERROR_NO_MORE_FILES: uint32 = 18 |
| 32 | + |
| 33 | + declare FormatMessageA( |
| 34 | + dwFlags: uint32, |
| 35 | + lpSource: void*, |
| 36 | + dwMessageId: uint32, |
| 37 | + dwLanguageId: uint32, |
| 38 | + lpBuffer: byte*, |
| 39 | + nSize: uint32, |
| 40 | + Arguments: void*, # actually va_list* |
| 41 | + ) -> uint32 |
| 42 | + const FORMAT_MESSAGE_IGNORE_INSERTS: uint32 = 0x00000200 |
| 43 | + const FORMAT_MESSAGE_FROM_SYSTEM: uint32 = 0x00001000 |
| 44 | + |
| 45 | +else: |
| 46 | + import "stdlib/errno.jou" |
| 47 | + import "stdlib/intnative.jou" |
| 48 | + |
| 49 | + if LINUX: |
| 50 | + # There are two versions of strerror_r(), and the one actually named |
| 51 | + # strerror_r() is the wrong one. |
| 52 | + declare __xpg_strerror_r(errnum: int, buf: byte*, buflen: intnative) -> int |
| 53 | + def strerror_r(errnum: int, buf: byte*, buflen: intnative) -> int: |
| 54 | + return __xpg_strerror_r(errnum, buf, buflen) |
| 55 | + else: |
| 56 | + declare strerror_r(errnum: int, buf: byte*, buflen: intnative) -> int |
| 57 | + |
| 58 | + class DIR: |
| 59 | + pass |
| 60 | + |
| 61 | + if LINUX: |
| 62 | + class dirent: |
| 63 | + d_ino: uint64 |
| 64 | + d_off: int64 |
| 65 | + d_reclen: uint16 |
| 66 | + d_type: byte |
| 67 | + d_name: byte[256] |
| 68 | + elif MACOS: |
| 69 | + # This struct definition was a bit painful to find. One way to find it |
| 70 | + # is to run C preprocessor on '#include <dirent.h>' in GitHub Actions. |
| 71 | + assert not IS_32BIT |
| 72 | + class dirent: |
| 73 | + d_ino: uint64 |
| 74 | + d_seekoff: uint64 |
| 75 | + d_reclen: uint16 |
| 76 | + d_namlen: uint16 |
| 77 | + d_type: byte |
| 78 | + d_name: byte[1024] |
| 79 | + elif NETBSD: |
| 80 | + class dirent: |
| 81 | + d_fileno: uint64 |
| 82 | + d_reclen: uint16 |
| 83 | + d_namlen: uint16 |
| 84 | + d_type: byte |
| 85 | + d_name: byte[512] |
| 86 | + else: |
| 87 | + assert False # unsupported system |
| 88 | + |
| 89 | + if NETBSD: |
| 90 | + # On NetBSD, "opendir" and "readdir" are legacy functions. |
| 91 | + # We can't use them because they generate a linker warning. |
| 92 | + # The dirent.h header magically renames them at compile time to the following names. |
| 93 | + declare __opendir30(name: byte*) -> DIR* |
| 94 | + declare __readdir30(dirp: DIR*) -> dirent* |
| 95 | + def opendir(name: byte*) -> DIR*: |
| 96 | + return __opendir30(name) |
| 97 | + def readdir(dirp: DIR*) -> dirent*: |
| 98 | + return __readdir30(dirp) |
| 99 | + else: |
| 100 | + declare opendir(name: byte*) -> DIR* |
| 101 | + declare readdir(dirp: DIR*) -> dirent* |
| 102 | + |
| 103 | + declare closedir(dirp: DIR*) -> int |
| 104 | + |
| 105 | + |
| 106 | +# Iterating directory contents |
| 107 | +@public |
| 108 | +class DirIter: |
| 109 | + # Inputs given by user |
| 110 | + dir: byte* |
| 111 | + include_dot_and_dotdot: bool |
| 112 | + |
| 113 | + # Output |
| 114 | + path: byte* |
| 115 | + name: byte* |
| 116 | + |
| 117 | + error_code: int # errno or GetLastError |
| 118 | + error_message: byte[512] |
| 119 | + |
| 120 | + # Internal state |
| 121 | + path_list: List[byte] |
| 122 | + if WINDOWS: |
| 123 | + handle: int64 |
| 124 | + else: |
| 125 | + dir_ptr: DIR* |
| 126 | + |
| 127 | + def set_name(self, name: byte*) -> bool: |
| 128 | + if (not self.include_dot_and_dotdot) and (strcmp(name, ".") == 0 or strcmp(name, "..") == 0): |
| 129 | + return False |
| 130 | + |
| 131 | + self.path_list.len = 0 |
| 132 | + self.path_list.extend_from_ptr(self.dir, strlen(self.dir)) |
| 133 | + |
| 134 | + if WINDOWS: |
| 135 | + if not (ends_with(self.dir, "/") or ends_with(self.dir, "\\")): |
| 136 | + self.path_list.append('\\') |
| 137 | + else: |
| 138 | + if not ends_with(self.dir, "/"): |
| 139 | + self.path_list.append('/') |
| 140 | + |
| 141 | + name_start_index = self.path_list.len |
| 142 | + self.path_list.extend_from_ptr(name, strlen(name)) |
| 143 | + self.path_list.append('\0') |
| 144 | + |
| 145 | + self.path = self.path_list.ptr |
| 146 | + self.name = &self.path[name_start_index] |
| 147 | + return True |
| 148 | + |
| 149 | + @public |
| 150 | + def next(self) -> bool: |
| 151 | + if self.dir == NULL: |
| 152 | + return False |
| 153 | + |
| 154 | + if WINDOWS: |
| 155 | + if self.dir[0] == '\0': |
| 156 | + # "\\*" would search the root of the current drive. Let's not do that. |
| 157 | + self.error_code = ERROR_PATH_NOT_FOUND as int |
| 158 | + FormatMessageA( |
| 159 | + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 160 | + NULL, self.error_code as uint32, 0, self.error_message, sizeof(self.error_message) as uint32, NULL, |
| 161 | + ) |
| 162 | + return False |
| 163 | + |
| 164 | + # Jou initializes everything to zero, but INVALID_HANDLE_VALUE is |
| 165 | + # more appropriate for this. Windows never uses zero as a find |
| 166 | + # handle value, so this is fine. |
| 167 | + if self.handle == 0: |
| 168 | + self.handle = INVALID_HANDLE_VALUE |
| 169 | + |
| 170 | + find_data: WIN32_FIND_DATAA |
| 171 | + if self.handle == INVALID_HANDLE_VALUE: |
| 172 | + # First file |
| 173 | + pattern: byte* = NULL |
| 174 | + asprintf(&pattern, "%s\\*", self.dir) |
| 175 | + assert pattern != NULL |
| 176 | + |
| 177 | + self.handle = FindFirstFileA(pattern, &find_data) |
| 178 | + free(pattern) |
| 179 | + found = (self.handle != INVALID_HANDLE_VALUE) |
| 180 | + else: |
| 181 | + # Not first file |
| 182 | + found = (FindNextFileA(self.handle, &find_data) != 0) |
| 183 | + |
| 184 | + while found: |
| 185 | + if self.set_name(find_data.cFileName): |
| 186 | + return True |
| 187 | + found = (FindNextFileA(self.handle, &find_data) != 0) |
| 188 | + |
| 189 | + e = GetLastError() as int |
| 190 | + |
| 191 | + free(self.path_list.ptr) |
| 192 | + if self.handle != INVALID_HANDLE_VALUE: |
| 193 | + FindClose(self.handle) |
| 194 | + *self = DirIter{} |
| 195 | + |
| 196 | + if e != ERROR_NO_MORE_FILES: |
| 197 | + # It failed |
| 198 | + self.error_code = e |
| 199 | + FormatMessageA( |
| 200 | + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 201 | + NULL, self.error_code as uint32, 0, self.error_message, sizeof(self.error_message) as uint32, NULL, |
| 202 | + ) |
| 203 | + |
| 204 | + return False |
| 205 | + |
| 206 | + else: |
| 207 | + if self.dir_ptr == NULL: |
| 208 | + # This is the first time this is called |
| 209 | + self.dir_ptr = opendir(self.dir) |
| 210 | + if self.dir_ptr == NULL: |
| 211 | + # It failed |
| 212 | + *self = DirIter{error_code = get_errno()} |
| 213 | + strerror_r(self.error_code, self.error_message, sizeof(self.error_message)) |
| 214 | + return False |
| 215 | + |
| 216 | + while True: |
| 217 | + set_errno(0) |
| 218 | + entry = readdir(self.dir_ptr) |
| 219 | + if entry == NULL: |
| 220 | + # End of directory, or error reading directory |
| 221 | + e = get_errno() |
| 222 | + free(self.path_list.ptr) |
| 223 | + closedir(self.dir_ptr) |
| 224 | + *self = DirIter{} |
| 225 | + if e != 0: |
| 226 | + # It failed |
| 227 | + self.error_code = e |
| 228 | + strerror_r(e, self.error_message, sizeof(self.error_message)) |
| 229 | + return False |
| 230 | + |
| 231 | + if self.set_name(entry.d_name): |
| 232 | + return True |
0 commit comments