-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
126 lines (113 loc) · 2.77 KB
/
files.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
118
119
120
121
122
123
124
125
126
package common
import (
"errors"
"os"
"path/filepath"
"strings"
)
var MOCK_CURRENT_DIR string
func GetCurrentDir() string {
if MOCK_CURRENT_DIR != "" {
return MOCK_CURRENT_DIR
}
if os.Getenv("WORK_DIR") != "" {
return os.Getenv("WORK_DIR")
}
ex, _ := os.Executable()
return filepath.Dir(ex)
}
func GetAbsolutePath(base string, opts ...string) string {
fullPath := base
if strings.HasPrefix(base, "/") == false { // relative filepath are relative to the binary
fullPath = filepath.Join(GetCurrentDir(), base)
}
if len(opts) == 0 {
return fullPath
}
return filepath.Join(append([]string{fullPath}, opts...)...)
}
func IsDirectory(path string) bool {
if path == "" {
return false
}
if path[len(path)-1:] != "/" {
return false
}
return true
}
/*
* Join 2 path together, result has a file
*/
func JoinPath(base, file string) string {
filePath := filepath.Join(base, file)
if strings.HasPrefix(filePath, base) == false {
return base
}
return filePath
}
func EnforceDirectory(path string) string {
if path == "" {
return "/"
} else if path[len(path)-1:] == "/" {
return path
}
return path + "/"
}
func SplitPath(path string) (root string, filename string) {
if path == "" {
path = "/"
}
if IsDirectory(path) == false {
filename = filepath.Base(path)
}
if root = strings.TrimSuffix(path, filename); root == "" {
root = "/"
}
return root, filename
}
func SafeOsMkdir(path string, mode os.FileMode) error {
if err := safePath(path); err != nil {
Log.Debug("common::files safeOsMkdir err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.Mkdir(path, mode)
}
func SafeOsRemove(path string) error {
if err := safePath(path); err != nil {
Log.Debug("common::files safeOsRemove err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.Remove(path)
}
func SafeOsRemoveAll(path string) error {
if err := safePath(path); err != nil {
Log.Debug("common::files safeOsRemoveAll err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.RemoveAll(path)
}
func SafeOsRename(from string, to string) error {
if err := safePath(from); err != nil {
Log.Debug("common::files safeOsRename err[%s] from[%s]", err.Error(), from)
return ErrFilesystemError
} else if err := safePath(to); err != nil {
Log.Debug("common::files safeOsRemove err[%s] to[%s]", err.Error(), to)
return ErrFilesystemError
}
return os.Rename(from, to)
}
func safePath(path string) error {
p, err := filepath.EvalSymlinks(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) == false {
return err
}
parentPath := filepath.Join(path, "../")
return safePath(parentPath)
}
if p != filepath.Clean(path) {
Log.Debug("common::files safePath path[%s] p[%s]", path, p)
return ErrFilesystemError
}
return nil
}