forked from acarl005/ls-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls-unix.go
46 lines (40 loc) · 969 Bytes
/
ls-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
37
38
39
40
41
42
43
44
45
46
// +build !windows
package main
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"syscall"
"github.com/willf/pad"
"golang.org/x/sys/unix"
)
func getOwnerAndGroup(fileInfo *os.FileInfo) (string, string) {
stat_t := (*fileInfo).Sys().(*syscall.Stat_t)
uid := fmt.Sprint(stat_t.Uid)
gid := fmt.Sprint(stat_t.Gid)
owner, err := user.LookupId(uid)
var ownerName string
if err == nil {
ownerName = owner.Username
} else {
ownerName = uid
}
group, err := user.LookupGroupId(gid)
var groupName string
if err == nil {
groupName = group.Name
} else {
groupName = gid
}
return ownerName, groupName
}
func deviceNumbers(absPath string) string {
stat := syscall.Stat_t{}
err := syscall.Stat(absPath, &stat)
check(err)
major := strconv.FormatInt(int64(unix.Major(uint64(stat.Rdev))), 10)
minor := strconv.FormatInt(int64(unix.Minor(uint64(stat.Rdev))), 10)
return pad.Left(strings.Join([]string{major, minor}, ","), 7, " ") + " " + Reset
}