github.com/charlievieth/fastwalk@v1.0.3/internal/dirent/dirent_linux.go (about)

     1  //go:build linux
     2  
     3  package dirent
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  func direntIno(buf []byte) (uint64, bool) {
    12  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Ino), unsafe.Sizeof(syscall.Dirent{}.Ino))
    13  }
    14  
    15  func direntReclen(buf []byte) (uint64, bool) {
    16  	return readInt(buf, unsafe.Offsetof(syscall.Dirent{}.Reclen), unsafe.Sizeof(syscall.Dirent{}.Reclen))
    17  }
    18  
    19  func direntNamlen(buf []byte) (uint64, bool) {
    20  	reclen, ok := direntReclen(buf)
    21  	if !ok {
    22  		return 0, false
    23  	}
    24  	return reclen - uint64(unsafe.Offsetof(syscall.Dirent{}.Name)), true
    25  }
    26  
    27  func direntType(buf []byte) os.FileMode {
    28  	off := unsafe.Offsetof(syscall.Dirent{}.Type)
    29  	if off >= uintptr(len(buf)) {
    30  		return ^os.FileMode(0) // unknown
    31  	}
    32  	typ := buf[off]
    33  	switch typ {
    34  	case syscall.DT_BLK:
    35  		return os.ModeDevice
    36  	case syscall.DT_CHR:
    37  		return os.ModeDevice | os.ModeCharDevice
    38  	case syscall.DT_DIR:
    39  		return os.ModeDir
    40  	case syscall.DT_FIFO:
    41  		return os.ModeNamedPipe
    42  	case syscall.DT_LNK:
    43  		return os.ModeSymlink
    44  	case syscall.DT_REG:
    45  		return 0
    46  	case syscall.DT_SOCK:
    47  		return os.ModeSocket
    48  	}
    49  	return ^os.FileMode(0) // unknown
    50  }