-
Notifications
You must be signed in to change notification settings - Fork 208
/
sys_windows.go
68 lines (60 loc) · 1.64 KB
/
sys_windows.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
// +build windows
package overseer
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
var (
supported = true
uid = syscall.Getuid()
gid = syscall.Getgid()
SIGUSR1 = syscall.SIGTERM
SIGUSR2 = syscall.SIGTERM
SIGTERM = syscall.SIGTERM
)
func move(dst, src string) error {
os.MkdirAll(filepath.Dir(dst), 0755)
if err := os.Rename(src, dst); err == nil {
return nil
}
//HACK: we're shelling out to move because windows
//throws errors when crossing device boundaries.
// https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/move.mspx?mfr=true
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
R := func(s string) string { return replShellMeta.Replace(syscall.EscapeArg(s)) }
cmd := exec.Command("cmd", "/c", `move /y `+R(src)+` `+R(dst))
if b, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%v: %q: %v", cmd.Args, bytes.TrimSpace(b), err)
}
return nil
}
func chmod(f *os.File, perms os.FileMode) error {
if err := f.Chmod(perms); err != nil && !strings.Contains(err.Error(), "not supported") {
return err
}
return nil
}
func chown(f *os.File, uid, gid int) error {
if err := f.Chown(uid, gid); err != nil && !strings.Contains(err.Error(), "not supported") {
return err
}
return nil
}
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
var replShellMeta = strings.NewReplacer(
`(`, `^(`,
`)`, `^)`,
`%`, `^%`,
`!`, `^!`,
`^`, `^^`,
`"`, `^"`,
`<`, `^<`,
`>`, `^>`,
`&`, `^&`,
`|`, `^|`,
)