-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcmd_unix.go
36 lines (30 loc) · 908 Bytes
/
cmd_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
//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package main
import (
"os"
"syscall"
)
// tempFile creates a new temporary hosts-file in an appropriate directory,
// opens the file for writing, and returns the resulting *os.File.
func tempFile(hostsPath string) (*os.File, error) {
fs, err := os.Stat(hostsPath)
if err != nil {
return nil, err
}
f, err := os.CreateTemp("", "hostsfile-temp")
if err != nil {
return nil, err
}
// Set file mode to the same as the hosts-file.
if err = os.Chmod(f.Name(), fs.Mode()); err != nil {
return nil, err
}
// Set ownership to the same as the hosts-file.
uid := fs.Sys().(*syscall.Stat_t).Uid
gid := fs.Sys().(*syscall.Stat_t).Gid
if err = os.Chown(f.Name(), int(uid), int(gid)); err != nil {
return nil, err
}
return f, nil
}