-
Notifications
You must be signed in to change notification settings - Fork 24
/
file.go
223 lines (188 loc) · 4.02 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
216
217
218
219
220
221
222
223
package file
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// SelfPath gets compiled executable file absolute path
func SelfPath() string {
path, _ := filepath.Abs(os.Args[0])
return path
}
// 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 gets compiled executable file directory
func SelfDir() string {
return filepath.Dir(SelfPath())
}
// get filepath base name
func Basename(fp string) string {
return path.Base(fp)
}
// get filepath dir name
func Dir(fp string) string {
return path.Dir(fp)
}
func InsureDir(fp string) error {
if IsExist(fp) {
return nil
}
return os.MkdirAll(fp, os.ModePerm)
}
// mkdir dir if not exist
func EnsureDir(fp string) error {
return os.MkdirAll(fp, os.ModePerm)
}
// ensure the datadir and make sure it's rw-able
func EnsureDirRW(dataDir string) error {
err := EnsureDir(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
}
Close(fd)
Remove(checkFile)
return nil
}
// create one file
func Create(name string) (*os.File, error) {
return os.Create(name)
}
// remove one file
func Remove(name string) error {
return os.Remove(name)
}
// close fd
func Close(fd *os.File) error {
return fd.Close()
}
func Ext(fp string) string {
return path.Ext(fp)
}
// rename file name
func Rename(src string, target string) error {
return os.Rename(src, target)
}
// delete file
func Unlink(fp string) error {
return os.Remove(fp)
}
// 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 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", fullPath)
return
}
// 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
}
// 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
}
// 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
}
// 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
}
func MustOpenLogFile(fp string) *os.File {
if strings.Contains(fp, "/") {
dir := Dir(fp)
err := EnsureDir(dir)
if err != nil {
log.Fatalf("mkdir -p %s occur error %v", dir, err)
}
}
f, err := os.OpenFile(fp, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("open %s occur error %v", fp, err)
}
return f
}