-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
48 lines (36 loc) · 853 Bytes
/
util.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
package main
import (
"os"
)
// Check if the input path is a valid directory for writing
// It is consider writable if one of the condition is true:
// - it does not exist
// - it is a valid folder
// - it is a symlink to a folder
func IsDirWritable(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, err
}
// this is a link then try to follow the link
if stat.Mode() & os.ModeSymlink == os.ModeSymlink {
link, lerr := os.Readlink(path)
if lerr != nil {
return false, nil
}
return IsDirWritable(link)
} else {
return stat.IsDir(), nil
}
return false, nil
}
func IsFile(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
return false, err
}
return !stat.IsDir(), nil
}