From fa7ef28acf6443073c1429c7e6df400eeb8f36af Mon Sep 17 00:00:00 2001 From: jason Date: Fri, 10 Jan 2025 20:54:09 -0500 Subject: Implement _read_directory_iterator in os2. Also, fix minor bug in linux.dirent_name. --- core/sys/linux/wrappers.odin | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'core/sys/linux') diff --git a/core/sys/linux/wrappers.odin b/core/sys/linux/wrappers.odin index 4f6118c80..e367a4db4 100644 --- a/core/sys/linux/wrappers.odin +++ b/core/sys/linux/wrappers.odin @@ -86,22 +86,18 @@ dirent_iterate_buf :: proc "contextless" (buf: []u8, offs: ^int) -> (d: ^Dirent, /// The lifetime of the string is bound to the lifetime of the provided dirent structure dirent_name :: proc "contextless" (dirent: ^Dirent) -> string #no_bounds_check { str := ([^]u8)(&dirent.name) - // Note(flysand): The string size calculated above applies only to the ideal case - // we subtract 1 byte from the string size, because a null terminator is guaranteed - // to be present. But! That said, the dirents are aligned to 8 bytes and the padding - // between the null terminator and the start of the next struct may be not initialized - // which means we also have to scan these garbage bytes. - str_size := int(dirent.reclen) - 1 - cast(int)offset_of(Dirent, name) - // This skips *only* over the garbage, since if we're not garbage we're at nul terminator, - // which skips this loop - for str[str_size] != 0 { - str_size -= 1 - } - for str[str_size-1] == 0 { - str_size -= 1 + // Dirents are aligned to 8 bytes, so there is guaranteed to be a null + // terminator in the last 8 bytes. + str_size := int(dirent.reclen) - cast(int)offset_of(Dirent, name) + + trunc := min(str_size, 8) + str_size -= trunc + for i in 0.. u64 { return u64(id) | (u64(op) << 8) | (u64(res) << 16) -} \ No newline at end of file +} -- cgit v1.2.3