Welcome to mirror list, hosted at ThFree Co, Russian Federation.

withNamlen.go « godirwalk « karrick « github.com « vendor - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46a4af50045a3e1c9ec33d185ef847beca4f2c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// +build darwin dragonfly freebsd netbsd openbsd

package godirwalk

import (
	"reflect"
	"syscall"
	"unsafe"
)

func nameFromDirent(de *syscall.Dirent) []byte {
	// Because this GOOS' syscall.Dirent provides a Namlen field that says how
	// long the name is, this function does not need to search for the NULL
	// byte.
	ml := int(de.Namlen)

	// Convert syscall.Dirent.Name, which is array of int8, to []byte, by
	// overwriting Cap, Len, and Data slice header fields to values from
	// syscall.Dirent fields. Setting the Cap, Len, and Data field values for
	// the slice header modifies what the slice header points to, and in this
	// case, the name buffer.
	var name []byte
	sh := (*reflect.SliceHeader)(unsafe.Pointer(&name))
	sh.Cap = ml
	sh.Len = ml
	sh.Data = uintptr(unsafe.Pointer(&de.Name[0]))

	return name
}