-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutil_unix.go
More file actions
238 lines (223 loc) · 7.72 KB
/
Copy pathutil_unix.go
File metadata and controls
238 lines (223 loc) · 7.72 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//go:build !windows
package main
import (
"bytes"
"io"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"unicode/utf8"
"unsafe"
)
// NOTE: We shouldn't encounter the macOS file limit of 256 anymore now that
// importing "os" automatically raises it for us in an init function.
// https://github.com/golang/go/issues/46279
// https://go-review.googlesource.com/c/go/+/393354/4/src/os/rlimit.go
const (
specialChars = "\\'\"`${[|&;<>()*?!"
extraSpecialChars = " \t\n"
prefixChars = "~"
)
// stop stops the command and all its child processes.
func stop(cmd *exec.Cmd) {
// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
// https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773
pgid := -cmd.Process.Pid
_ = syscall.Kill(pgid, syscall.SIGTERM)
}
// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
// https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773
func setpgid(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
// flushStdin tells the OS to flush the text currently buffered in stdin.
//
// Using raw numbers here instead of importing package constants to avoid
// creating a bunch more files to use conditional compilation to select the
// right package constants for the os and arch.
func flushStdin(r io.Reader) {
f, ok := r.(*os.File)
if !ok {
return
}
switch runtime.GOOS {
case "linux":
// ioctl(fd, TCFLSH, TCIFLUSH)
syscall.Syscall(
linuxSYSIOCTL(),
f.Fd(),
linuxTCFLSH(),
0, /* golang.org/x/sys/unix.TCIFLUSH on linux */
)
case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
// ioctl(fd, TIOCFLUSH, &TCIFLUSH)
flags := int32(1 /* golang.org/x/sys/unix.TCIFLUSH */)
syscall.Syscall(
54, /* syscall.SYS_IOCTL on darwin, dragonfly, freebsd, netbsd, openbsd */
f.Fd(),
0x80047410, /* golang.org/x/sys/unix.TIOCFLUSH on darwin, dragonfly, freebsd, netbsd, openbsd */
uintptr(unsafe.Pointer(&flags)),
)
}
}
// linuxSYSIOCTL returns syscall.SYS_IOCTL based on runtime.GOARCH.
func linuxSYSIOCTL() uintptr {
switch runtime.GOARCH {
case "amd64":
return 16 /* syscall.SYS_IOCTL on linux/amd64 */
case "arm64", "loong64", "riscv64":
return 29 /* syscall.SYS_IOCTL on linux/arm64, linux/loong64, linux/riscv64 */
case "mips", "mipsle":
return 4054 /* syscall.SYS_IOCTL on linux/mips, linux/mipsle */
case "mips64", "mips64le":
return 5015 /* syscall.SYS_IOCTL on linux/mips64, linux/mips64le */
default:
return 54 /* syscall.SYS_IOCTL on other linux architectures */
}
}
// linuxSYSIOCTL returns golang.org/x/sys/unix.TCFLSH based on runtime.GOARCH.
func linuxTCFLSH() uintptr {
switch runtime.GOARCH {
case "mips", "mips64", "mips64le", "mipsle":
return 0x5407 /* golang.org/x/sys/unix.TCFLSH on linux/mips, linux/mips64, linux/mips64le, linux/mipsle */
case "ppc", "ppc64", "ppc64le":
return 0x2000741f /* golang.org/x/sys/unix.TCFLSH on linux/ppc, linux/ppc64, linux/ppc64le */
case "sparc64":
return 0x20005407 /* golang.org/x/sys/unix.TCFLSH on linux/sparc64 */
default:
return 0x540b /* golang.org/x/sys/unix.TCFLSH on other linux architectures */
}
}
// joinArgs joins the arguments of the command into a string which can then be
// passed to `exec.Command("sh", "-c", $STRING)`. Examples:
//
// ["echo", "foo"] => echo foo
//
// ["echo", "hello goodbye"] => echo 'hello goodbye'
func joinArgs(args []string) string {
// https://github.com/kballard/go-shellquote/blob/master/quote.go
//
// Copyright (C) 2014 Kevin Ballard
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var buf bytes.Buffer
for i, arg := range args {
if i != 0 {
buf.WriteByte(' ')
}
quote(arg, &buf)
}
return buf.String()
}
func quote(word string, buf *bytes.Buffer) {
// https://github.com/kballard/go-shellquote/blob/master/quote.go
//
// Copyright (C) 2014 Kevin Ballard
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// We want to try to produce a "nice" output. As such, we will
// backslash-escape most characters, but if we encounter a space, or if we
// encounter an extra-special char (which doesn't work with
// backslash-escaping) we switch over to quoting the whole word. We do this
// with a space because it's typically easier for people to read multi-word
// arguments when quoted with a space rather than with ugly backslashes
// everywhere.
origLen := buf.Len()
if len(word) == 0 {
// oops, no content
buf.WriteString("''")
return
}
cur, prev := word, word
atStart := true
for len(cur) > 0 {
c, l := utf8.DecodeRuneInString(cur)
cur = cur[l:]
if strings.ContainsRune(specialChars, c) || (atStart && strings.ContainsRune(prefixChars, c)) {
// copy the non-special chars up to this point
if len(cur) < len(prev) {
buf.WriteString(prev[0 : len(prev)-len(cur)-l])
}
buf.WriteByte('\\')
buf.WriteRune(c)
prev = cur
} else if strings.ContainsRune(extraSpecialChars, c) {
// start over in quote mode
buf.Truncate(origLen)
goto quote
}
atStart = false
}
if len(prev) > 0 {
buf.WriteString(prev)
}
return
quote:
// quote mode
// Use single-quotes, but if we find a single-quote in the word, we need
// to terminate the string, emit an escaped quote, and start the string up
// again
inQuote := false
for len(word) > 0 {
i := strings.IndexRune(word, '\'')
if i == -1 {
break
}
if i > 0 {
if !inQuote {
buf.WriteByte('\'')
inQuote = true
}
buf.WriteString(word[0:i])
}
word = word[i+1:]
if inQuote {
buf.WriteByte('\'')
inQuote = false
}
buf.WriteString("\\'")
}
if len(word) > 0 {
if !inQuote {
buf.WriteByte('\'')
}
buf.WriteString(word)
buf.WriteByte('\'')
}
}