-
Notifications
You must be signed in to change notification settings - Fork 25
/
ls-windows.go
65 lines (58 loc) · 1.67 KB
/
ls-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
//go:build windows
package main
import (
"os"
"strconv"
"strings"
"syscall"
"unsafe"
"github.com/willf/pad"
"golang.org/x/sys/windows"
)
var (
libadvapi32 = syscall.NewLazyDLL("advapi32.dll")
procGetFileSecurity = libadvapi32.NewProc("GetFileSecurityW")
procGetSecurityDescriptorOwner = libadvapi32.NewProc("GetSecurityDescriptorOwner")
)
func getOwnerAndGroup(fileInfo *os.FileInfo) (string, string) {
path := (*fileInfo).Name()
var needed uint32
procGetFileSecurity.Call(
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
0x00000001, /* OWNER_SECURITY_INFORMATION */
0,
0,
uintptr(unsafe.Pointer(&needed)))
buf := make([]byte, needed)
r1, _, err := procGetFileSecurity.Call(
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
0x00000001, /* OWNER_SECURITY_INFORMATION */
uintptr(unsafe.Pointer(&buf[0])),
uintptr(needed),
uintptr(unsafe.Pointer(&needed)))
if r1 == 0 && err != nil {
return "", ""
}
var ownerDefaulted uint32
var sid *syscall.SID
r1, _, err = procGetSecurityDescriptorOwner.Call(
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&sid)),
uintptr(unsafe.Pointer(&ownerDefaulted)))
if r1 == 0 && err != nil {
return "", ""
}
uid, gid, _, err := sid.LookupAccount("")
if r1 == 0 && err != nil {
return "", ""
}
return uid, gid
}
func deviceNumbers(absPath string) string {
stat := syscall.Stat_t{}
err := syscall.Stat(absPath, &stat)
check(err)
major := strconv.FormatInt(int64(windows.Major(uint64(stat.Rdev))), 10)
minor := strconv.FormatInt(int64(windows.Minor(uint64(stat.Rdev))), 10)
return pad.Left(strings.Join([]string{major, minor}, ","), 7, " ") + " " + Reset
}