github.com/v2fly/tools@v0.100.0/internal/fastwalk/fastwalk_dirent_namlen_linux.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux && !appengine 6 // +build linux,!appengine 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 }