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