-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-unix.go
61 lines (49 loc) · 1.29 KB
/
main-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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build !windows
// +build !windows
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/check-path
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package main
import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
"github.com/rs/zerolog/log"
)
func main() {
// file, err := os.Stat(filepath.Join("/tmp", "vscode-inno-updater-1603112887.log"))
file, err := os.Stat("/tmp/go1.15.3.linux-amd64.tar.gz")
if err != nil {
if os.IsNotExist(err) {
log.Fatal().Err(err).Msg("file does not exist.")
}
log.Fatal().Err(err).Msg("failed to stat file; unknown error occurred")
}
// fmt.Println(file.Mode())
// fmt.Printf("%#v", file.Sys())
stat, statOK := file.Sys().(*syscall.Stat_t)
if !statOK {
log.Fatal().Msg("failed to access syscall.Stat_t")
}
UID := int(stat.Uid)
GID := int(stat.Gid)
fmt.Println("UID:", UID)
fmt.Println("GID:", GID)
gidNum := strconv.Itoa(GID)
group, err := user.LookupGroupId(gidNum)
if err != nil {
log.Fatal().Err(err).Msg("failed to lookup group for gid")
}
fmt.Println("group name:", group.Name)
uidNum := strconv.Itoa(UID)
user, err := user.LookupId(uidNum)
if err != nil {
log.Fatal().Err(err).Msg("failed to lookup user for uid")
}
fmt.Println("user name:", user.Username)
}