overseer/sys_windows.go

69 lines
1.6 KiB
Go
Raw Normal View History

2016-04-01 05:12:51 +00:00
// +build windows
package overseer
import (
2016-04-01 20:37:28 +00:00
"bytes"
2016-04-01 05:12:51 +00:00
"fmt"
2016-04-01 20:37:28 +00:00
"os"
2016-04-01 05:12:51 +00:00
"os/exec"
2016-04-01 20:37:28 +00:00
"path/filepath"
2016-04-02 09:42:08 +00:00
"strings"
2016-04-01 05:12:51 +00:00
"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 {
2016-04-01 20:37:28 +00:00
os.MkdirAll(filepath.Dir(dst), 0755)
if err := os.Rename(src, dst); err == nil {
return nil
}
2016-04-01 05:12:51 +00:00
//HACK: we're shelling out to move because windows
//throws errors when crossing device boundaryes.
// https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/move.mspx?mfr=true
2016-04-15 08:36:39 +00:00
// 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))
2016-04-01 05:12:51 +00:00
if b, err := cmd.CombinedOutput(); err != nil {
2016-04-01 20:37:28 +00:00
return fmt.Errorf("%v: %q: %v", cmd.Args, bytes.TrimSpace(b), err)
2016-04-01 05:12:51 +00:00
}
return nil
}
2016-04-01 20:37:28 +00:00
func chmod(f *os.File, perms os.FileMode) error {
2016-04-02 09:42:08 +00:00
if err := f.Chmod(perms); err != nil && !strings.Contains(err.Error(), "not supported") {
return err
}
2016-04-01 20:37:28 +00:00
return nil
}
func chown(f *os.File, uid, gid int) error {
2016-04-02 09:42:08 +00:00
if err := f.Chown(uid, gid); err != nil && !strings.Contains(err.Error(), "not supported") {
return err
}
2016-04-01 20:37:28 +00:00
return nil
}
2016-04-15 08:36:39 +00:00
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
var replShellMeta = strings.NewReplacer(
`(`, `^(`,
`)`, `^)`,
`%`, `^%`,
`!`, `^!`,
`^`, `^^`,
`"`, `^"`,
`<`, `^<`,
`>`, `^>`,
`&`, `^&`,
`|`, `^|`,
)