-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.go
60 lines (47 loc) · 992 Bytes
/
stat.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
package pathutil
import (
"fmt"
"os"
)
// Stat return os.FileInfo
func (path PathImpl) Stat() (os.FileInfo, error) {
file, err := os.Open(path.path)
if err != nil {
return nil, err
}
defer func() {
if err := file.Close(); err != nil {
fmt.Println(err)
}
}()
return file.Stat()
}
// File or dir exists
func (path PathImpl) Exists() bool {
if _, err := path.Stat(); os.IsNotExist(err) {
return false
}
return true
}
// IsDir return true if path is dir
func (path PathImpl) IsDir() bool {
stat, err := path.Stat()
if err != nil {
return false
}
return stat.IsDir()
}
// IsFile return true is path exists and not dir
// (symlinks, devs, regular files)
func (path PathImpl) IsFile() bool {
return path.Exists() && !path.IsDir()
}
// IsRegularFile return true if path is regular file
// (wihtout devs, symlinks, ...)
func (path PathImpl) IsRegularFile() bool {
stat, err := path.Stat()
if err != nil {
return false
}
return stat.Mode().IsRegular()
}