-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_unix.go
135 lines (111 loc) · 3.05 KB
/
utils_unix.go
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build unix
package sox
import (
"golang.org/x/sys/unix"
"unsafe"
)
type ioVec = unix.Iovec
func errFromUnixErrno(err error) error {
if err == nil {
return nil
}
errno, ok := err.(unix.Errno)
if !ok {
return err
}
switch errno {
case unix.EINTR:
return ErrInterruptedSyscall
case unix.EAGAIN:
return ErrTemporarilyUnavailable
case unix.EINPROGRESS:
return ErrInProgress
case unix.EFAULT:
return ErrFaultParams
case unix.EINVAL:
return ErrInvalidParam
case unix.EMFILE:
return ErrProcessFileLimit
case unix.ENFILE:
return ErrSystemFileLimit
case unix.ENODEV:
return ErrNoDevice
case unix.ENOMEM:
return ErrNoAvailableMemory
case unix.EPERM:
return ErrNoPermission
default:
return errno
}
}
func ioVecFromBytesSlice(iov [][]byte) (addr uintptr, n int) {
vec := make([]unix.Iovec, len(iov))
for i := range len(iov) {
vec[i] = unix.Iovec{Base: unsafe.SliceData(iov[i]), Len: uint64(len(iov[i]))}
}
addr, n = uintptr(unsafe.Pointer(unsafe.SliceData(vec))), len(vec)
return
}
func ioVecAddrLen(vec []ioVec) (addr uintptr, n int) {
addr, n = uintptr(unsafe.Pointer(unsafe.SliceData(vec))), len(vec)
return
}
func ioVecFromPicoBuffers(buffers []PicoBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range len(buffers) {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizePico}
}
return vec
}
func ioVecFromNanoBuffers(buffers []NanoBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range len(buffers) {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeNano}
}
return vec
}
func ioVecFromMicroBuffers(buffers []MicroBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeMicro}
}
return vec
}
func ioVecFromSmallBuffers(buffers []SmallBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeSmall}
}
return vec
}
func ioVecFromMediumBuffers(buffers []MediumBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeMedium}
}
return vec
}
func ioVecFromLargeBuffers(buffers []LargeBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeLarge}
}
return vec
}
func ioVecFromHugeBuffers(buffers []HugeBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeHuge}
}
return vec
}
func ioVecFromGiantBuffers(buffers []GiantBuffer) []ioVec {
vec := make([]ioVec, len(buffers))
for i := range buffers {
vec[i] = ioVec{Base: (*byte)(unsafe.Pointer(&buffers[i])), Len: BufferSizeGiant}
}
return vec
}