1
2
3
4
5
6
7 package unix
8
9 import (
10 "syscall"
11 "unsafe"
12 )
13
14
15
16
17
18
19
20 var (
21 procFstatat,
22 procOpenat,
23 procUnlinkat,
24 procReadlinkat,
25 procMkdirat uintptr
26 )
27
28 func Unlinkat(dirfd int, path string, flags int) error {
29 p, err := syscall.BytePtrFromString(path)
30 if err != nil {
31 return err
32 }
33
34 _, _, errno := syscall6(uintptr(unsafe.Pointer(&procUnlinkat)), 3, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), 0, 0, 0)
35 if errno != 0 {
36 return errno
37 }
38
39 return nil
40 }
41
42 func Openat(dirfd int, path string, flags int, perm uint32) (int, error) {
43 p, err := syscall.BytePtrFromString(path)
44 if err != nil {
45 return 0, err
46 }
47
48 fd, _, errno := syscall6(uintptr(unsafe.Pointer(&procOpenat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), uintptr(perm), 0, 0)
49 if errno != 0 {
50 return 0, errno
51 }
52
53 return int(fd), nil
54 }
55
56 func Fstatat(dirfd int, path string, stat *syscall.Stat_t, flags int) error {
57 p, err := syscall.BytePtrFromString(path)
58 if err != nil {
59 return err
60 }
61
62 _, _, errno := syscall6(uintptr(unsafe.Pointer(&procFstatat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(stat)), uintptr(flags), 0, 0)
63 if errno != 0 {
64 return errno
65 }
66
67 return nil
68 }
69
70 func Readlinkat(dirfd int, path string, buf []byte) (int, error) {
71 p0, err := syscall.BytePtrFromString(path)
72 if err != nil {
73 return 0, err
74 }
75 var p1 unsafe.Pointer
76 if len(buf) > 0 {
77 p1 = unsafe.Pointer(&buf[0])
78 } else {
79 p1 = unsafe.Pointer(&_zero)
80 }
81 n, _, errno := syscall6(uintptr(unsafe.Pointer(&procReadlinkat)), 4,
82 uintptr(dirfd),
83 uintptr(unsafe.Pointer(p0)),
84 uintptr(p1),
85 uintptr(len(buf)),
86 0, 0)
87 if errno != 0 {
88 return 0, errno
89 }
90
91 return int(n), nil
92 }
93
94 func Mkdirat(dirfd int, path string, mode uint32) error {
95 p, err := syscall.BytePtrFromString(path)
96 if err != nil {
97 return err
98 }
99
100 _, _, errno := syscall6(uintptr(unsafe.Pointer(&procMkdirat)), 3,
101 uintptr(dirfd),
102 uintptr(unsafe.Pointer(p)),
103 uintptr(mode),
104 0, 0, 0)
105 if errno != 0 {
106 return errno
107 }
108 return nil
109 }
110
View as plain text