-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
117 lines (102 loc) · 2 KB
/
info.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package fil
import (
"os"
)
/*
type Path struct {
}
func New(path string) (Path, error) {
fi, err := os.Lstat(path)
if err != nil {
return fi, err
}
return fi, err
}
func (p *Path) IsExists() bool {
xx
}
*/
// Exists checks whether the given file exists or not.
func IsExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
func TypeFileInfo(fi os.FileInfo) string {
m := fi.Mode()
switch {
case m.IsDir():
return "dir"
case m.IsRegular():
return "regular"
case m & os.ModeSymlink != 0:
return "symlink"
case m & os.ModeNamedPipe != 0:
return "namedpipe"
case m & os.ModeSocket != 0:
return "socket"
case m & os.ModeDevice != 0:
return "device"
default:
return ""
}
}
func Type(path string) (string, error) {
fi, err := os.Lstat(path)
if err != nil {
return "", err
}
return TypeFileInfo(fi), nil
}
func IsNotExist(path string) (bool, error) {
ret, err := IsExists(path)
return ! ret, err
}
func IsFile(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return !fi.Mode().IsDir(), err
}
func IsDir(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode().IsDir(), err
}
func IsRegular(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode().IsRegular(), err
}
func isReadable(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode() & 0400 != 0 , err
}
func isWritable(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode() & 0200 != 0 , err
}
func isExecutable(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.Mode() & 0100 != 0 , err
}