-
Notifications
You must be signed in to change notification settings - Fork 34
/
funcs.go
80 lines (63 loc) · 1.22 KB
/
funcs.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"syscall"
log "github.com/sirupsen/logrus"
)
func fileGetAbsolutePath(path string) (string, os.FileInfo) {
ret, err := filepath.Abs(path)
if err != nil {
log.Fatalf("invalid file: %v", err)
}
f, err := os.Lstat(ret)
if err != nil {
log.Fatalf("file stats failed: %v", err)
}
return ret, f
}
func checkIfDirectoryExists(path string) bool {
f, err := os.Stat(path)
// check if path exists
if err != nil {
return false
}
return f.IsDir()
}
func checkIfFileExistsAndOwnedByRoot(path string) bool {
f, err := os.Stat(path)
// check if path exists
if err != nil {
return false
}
// check if it is not a file
if !f.Mode().IsRegular() {
return false
}
uidS := fmt.Sprint(f.Sys().(*syscall.Stat_t).Uid)
uid, err := strconv.Atoi(uidS)
if err != nil {
return false
}
if uid != 0 {
return false
}
return true
}
func checkIfFileIsValid(f os.FileInfo, path string) bool {
if f.IsDir() {
return false
}
if f.Mode().IsRegular() {
if f.Mode().Perm()&0022 == 0 {
return true
} else {
log.Infof("ignoring file with wrong modes (not xx22) %s\n", path)
}
} else {
log.Infof("ignoring non regular file %s\n", path)
}
return false
}