aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/gocmdbridge/server/fileaccess.go
blob: 2913f3e407cf415945ad9deb3a004b39fc49cba1 (plain)
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
//go:build !windows

package main

import (
	"fmt"
	"os/user"
	"strconv"

	"golang.org/x/sys/unix"
)


func isWritable(path string) bool {
	return unix.Access(path, unix.W_OK) == nil
}

func isReadable(path string) bool {
	return unix.Access(path, unix.R_OK) == nil
}

func isExecutable(path string) bool {
	return unix.Access(path, unix.X_OK) == nil
}

func numberOfHardLinks(path string) int {
	var stat unix.Stat_t
	unix.Stat(path, &stat)
	return int(stat.Nlink)
}

func fileId(path string) string {
	var stat unix.Stat_t
	unix.Stat(path, &stat)
	return fmt.Sprintf("%x:%x", stat.Dev, stat.Ino)
}

func freeSpace(path string) uint64 {
	var stat unix.Statfs_t
	unix.Statfs(path, &stat)
	return stat.Bavail * uint64(stat.Bsize)
}

func owner(path string) string {
	uid := strconv.Itoa(unix.Getuid())
	u, _ := user.LookupId(uid)
	return u.Username
}

func ownerId(path string) int {
	return unix.Getuid()
}

func group(path string) string {
	gid := strconv.Itoa(unix.Getgid())
	g, _ := user.LookupGroupId(gid)
	return g.Name
}

func groupId(path string) int {
	return unix.Getgid()
}