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{}.Ino), unsafe.Sizeof(syscall.Dirent{}.Ino))
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	reclen, ok := direntReclen(buf)
22	if !ok {
23		return 0, false
24	}
25	return reclen - uint64(unsafe.Offsetof(syscall.Dirent{}.Name)), true
26}
27
28func direntType(buf []byte) FileMode {
29	off := unsafe.Offsetof(syscall.Dirent{}.Type)
30	if off >= uintptr(len(buf)) {
31		return ^FileMode(0) // unknown
32	}
33	typ := buf[off]
34	switch typ {
35	case syscall.DT_BLK:
36		return ModeDevice
37	case syscall.DT_CHR:
38		return ModeDevice | ModeCharDevice
39	case syscall.DT_DIR:
40		return ModeDir
41	case syscall.DT_FIFO:
42		return ModeNamedPipe
43	case syscall.DT_LNK:
44		return ModeSymlink
45	case syscall.DT_REG:
46		return 0
47	case syscall.DT_SOCK:
48		return ModeSocket
49	}
50	return ^FileMode(0) // unknown
51}
52