1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | package fastwalk |
9 | |
10 | import ( |
11 | "bytes" |
12 | "syscall" |
13 | "unsafe" |
14 | ) |
15 | |
16 | func direntNamlen(dirent *syscall.Dirent) uint64 { |
17 | const fixedHdr = uint16(unsafe.Offsetof(syscall.Dirent{}.Name)) |
18 | nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0])) |
19 | const nameBufLen = uint16(len(nameBuf)) |
20 | limit := dirent.Reclen - fixedHdr |
21 | if limit > nameBufLen { |
22 | limit = nameBufLen |
23 | } |
24 | nameLen := bytes.IndexByte(nameBuf[:limit], 0) |
25 | if nameLen < 0 { |
26 | panic("failed to find terminating 0 byte in dirent") |
27 | } |
28 | return uint64(nameLen) |
29 | } |
30 | |