-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
215 lines (193 loc) · 4.43 KB
/
file.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package gdk
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// SelfPath gets compiled executable file absolute path
func SelfPath() string {
path, _ := filepath.Abs(os.Args[0])
return path
}
// RealPath get absolute filepath, based on built executable file
func RealPath(fp string) (string, error) {
if path.IsAbs(fp) {
return fp, nil
}
wd, err := os.Getwd()
return path.Join(wd, fp), err
}
// SelfDir get compiled executable file directory
func SelfDir() string {
return filepath.Dir(SelfPath())
}
// Name get filepath base name
func Name(fp string) string {
return path.Base(fp)
}
// Dir get filepath dir name
func Dir(fp string) string {
return path.Dir(fp)
}
// InsureDir mkdir if not exist
func InsureDir(fp string) error {
if IsExist(fp) {
return nil
}
return os.MkdirAll(fp, os.ModePerm)
}
// EnsureDirRW ensure the datadir and make sure it's rw-able
func EnsureDirRW(dataDir string) error {
err := InsureDir(dataDir)
if err != nil {
return err
}
checkFile := fmt.Sprintf("%s/rw.%d", dataDir, time.Now().UnixNano())
fd, err := Create(checkFile)
if err != nil {
if os.IsPermission(err) {
return fmt.Errorf("open %s: rw permission denied", dataDir)
}
return err
}
fd.Close()
Remove(checkFile)
return nil
}
// Create create one file
func Create(name string) (*os.File, error) {
return os.Create(name)
}
// Remove remove one file
func Remove(name string) error {
return os.Remove(name)
}
// Ext returns the file name extension used by path.
// The extension is the suffix beginning at the final dot
// in the final slash-separated element of path;
// it is empty if there is no dot.
func Ext(fp string) string {
return path.Ext(fp)
}
// Rename rename file name
func Rename(src string, target string) error {
return os.Rename(src, target)
}
// IsFile checks whether the path is a file,
// it returns false when it's a directory or does not exist.
func IsFile(fp string) bool {
f, e := os.Stat(fp)
if e != nil {
return false
}
return !f.IsDir()
}
// IsExist checks whether a file or directory exists
// It returns false when the file or directory does not exist.
func IsExist(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}
// Search a file in the give paths.
// this is often used in search config file in /etc ~/
func SearchFile(filename string, paths ...string) (fullPath string, err error) {
for _, path := range paths {
if fullPath = filepath.Join(path, filename); IsExist(fullPath) {
return
}
}
err = fmt.Errorf("%s not found in paths %s", filename, paths)
return
}
// FileMTime get file modified time
func FileMTime(fp string) (int64, error) {
f, e := os.Stat(fp)
if e != nil {
return 0, e
}
return f.ModTime().Unix(), nil
}
// FileSize get file size as how many bytes
func FileSize(fp string) (int64, error) {
f, e := os.Stat(fp)
if e != nil {
return 0, e
}
return f.Size(), nil
}
// DirsUnder list dirs under dirPath
func DirsUnder(dirPath string) ([]string, error) {
if !IsExist(dirPath) {
return []string{}, nil
}
fs, err := ioutil.ReadDir(dirPath)
if err != nil {
return []string{}, err
}
sz := len(fs)
if sz == 0 {
return []string{}, nil
}
ret := make([]string, 0, sz)
for i := 0; i < sz; i++ {
if fs[i].IsDir() {
name := fs[i].Name()
if name != "." && name != ".." {
ret = append(ret, name)
}
}
}
return ret, nil
}
// FilesUnder list files under dirPath
func FilesUnder(dirPath string) ([]string, error) {
if !IsExist(dirPath) {
return []string{}, nil
}
fs, err := ioutil.ReadDir(dirPath)
if err != nil {
return []string{}, err
}
sz := len(fs)
if sz == 0 {
return []string{}, nil
}
ret := make([]string, 0, sz)
for i := 0; i < sz; i++ {
if !fs[i].IsDir() {
ret = append(ret, fs[i].Name())
}
}
return ret, nil
}
// SearchFileWithAffix search file under dirPath and meet the followinng conditions
// match prefix and suffix
// prefix and suffix must been set and not be empty
func SearchFileWithAffix(dirPath, prefix, suffix string) (fullPath string, exist bool) {
if !IsExist(dirPath) {
return "", false
}
fs, err := ioutil.ReadDir(dirPath)
if err != nil {
return "", false
}
sz := len(fs)
if sz == 0 {
return "", false
}
if len(prefix) == 0 || len(suffix) == 0 {
return "", false
}
for i := 0; i < sz; i++ {
if !fs[i].IsDir() {
if strings.HasPrefix(fs[i].Name(), prefix) && strings.HasSuffix(fs[i].Name(), suffix) {
return fs[i].Name(), true
}
}
}
return "", false
}