-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
380 lines (345 loc) · 8.64 KB
/
fs.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package fs
import (
"bytes"
"context"
"errors"
"fmt"
filepath2 "github.com/hopeio/cherry/utils/io/fs/path"
"github.com/hopeio/cherry/utils/log"
"github.com/hopeio/cherry/utils/scheduler/monitor"
"os"
"path/filepath"
"sort"
"strconv"
)
type Dir string
func (d Dir) Open(name string) (*os.File, error) {
dir := string(d)
if dir == "" {
dir = "."
}
fullName := filepath.Join(dir, filepath.FromSlash(filepath.Clean(string(os.PathSeparator)+name)))
f, err := os.Open(fullName)
if err != nil {
return nil, err
}
return f, nil
}
// path和filepath两个包,filepath文件专用
func FindFile(path string) (string, error) {
files, err := FindFiles(path, 8, 1)
if err != nil {
return "", err
}
return files[0], nil
}
func FindFiles(path string, deep int8, num int) ([]string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
var files []string
filepath1 := filepath.Join(wd, path)
if _, err = os.Stat(filepath1); !os.IsNotExist(err) {
files = append(files, filepath1)
if len(files) == num {
return files, nil
}
}
subDirFiles(wd, path, "", &files, deep, 0, num)
supDirFiles(wd+string(os.PathSeparator), path, &files, deep, 0, num)
if len(files) == 0 {
return nil, errors.New("找不到文件")
}
return files, nil
}
func subDirFiles(dir, path, exclude string, files *[]string, deep, step int8, num int) {
step += 1
if step-1 == deep {
return
}
fileInfos, err := os.ReadDir(dir)
if err != nil {
log.Error(err)
}
for i := range fileInfos {
if fileInfos[i].IsDir() {
if exclude != "" && fileInfos[i].Name() == exclude {
continue
}
filepath1 := filepath.Join(dir, fileInfos[i].Name(), path)
if _, err = os.Stat(filepath1); !os.IsNotExist(err) {
*files = append(*files, filepath1)
if len(*files) == num {
return
}
}
subDirFiles(filepath.Join(dir, fileInfos[i].Name()), path, "", files, deep, step, num)
}
}
}
func supDirFiles(dir, path string, files *[]string, deep, step int8, num int) {
step += 1
if step-1 == deep {
return
}
dir, dirName := filepath.Split(dir[:len(dir)-1])
if _, err := os.Stat(dir); os.IsNotExist(err) {
return
}
filepath1 := filepath.Join(dir, path)
if _, err := os.Stat(filepath1); !os.IsNotExist(err) {
*files = append(*files, filepath1)
if len(*files) == num {
return
}
}
subDirFiles(dir, path, dirName, files, deep, 0, num)
supDirFiles(dir, path, files, deep, step, num)
}
// path和filepath两个包,filepath文件专用
func FindFiles2(path string, deep int8, num int) ([]string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
var file = make(chan string, 1)
//属于回调而不是通知
ctx := monitor.New(context.Background(), func() {
close(file)
})
defer ctx.Cancel()
// 当前目录下先找
filepath1 := filepath.Join(wd, path)
if _, err = os.Stat(filepath1); !os.IsNotExist(err) {
file <- filepath1
}
ctx.Run(func() {
err := subDirFiles2(wd, path, "", file, deep, 0, ctx)
if err != nil {
log.Error(err)
}
})
ctx.Run(func() {
err := supDirFiles2(wd+string(os.PathSeparator), path, file, deep, 0, ctx)
if err != nil {
log.Error(err)
}
})
var files []string
for filepath1 := range file {
if files = append(files, filepath1); len(files) == num {
//close(file) 这里无需做关闭操作,会关的
return files, nil
}
}
return files, nil
}
func subDirFiles2(dir, path, exclude string, file chan string, deep, step int8, ctx *monitor.Monitor) error {
step += 1
if step-1 == deep {
return nil
}
fileInfos, err := os.ReadDir(dir)
if err != nil {
return err
}
for i := range fileInfos {
if fileInfos[i].IsDir() {
if exclude != "" && fileInfos[i].Name() == exclude {
continue
}
subDir := filepath.Join(dir, fileInfos[i].Name())
filepath1 := filepath.Join(subDir, path)
if _, err = os.Stat(filepath1); !os.IsNotExist(err) {
//①如果给出了default语句,那么就会执行default的流程,同时程序的执行会从select语句后的语句中恢复。
//②如果没有default语句,那么select语句将被阻塞,直到至少有一个case可以进行下去。
select {
case <-ctx.Done():
return ctx.Err()
case file <- filepath1:
}
}
ctx.Run(func() {
//TODO: subDirFiles2(filepath.Join(dir, fileInfos[i].Name()), path, "", file, deep, step, ctx) 这种语句有问题,不清楚原因,i会错乱?
// 还是go1.22之前的问题,升级go mod go 标签到1.22
err := subDirFiles2(filepath.Join(dir, fileInfos[i].Name()), path, "", file, deep, step, ctx)
if err != nil {
log.Error(err)
}
})
}
}
return nil
}
func supDirFiles2(dir, path string, file chan string, deep, step int8, ctx *monitor.Monitor) error {
step += 1
if step-1 == deep {
return nil
}
dir, dirName := filepath.Split(dir[:len(dir)-1])
if _, err := os.Stat(dir); os.IsNotExist(err) {
return err
}
filepath1 := filepath.Join(dir, path)
if _, err := os.Stat(filepath1); !os.IsNotExist(err) {
select {
case <-ctx.Done():
return ctx.Err()
case file <- filepath1:
}
}
ctx.Run(func() {
err := subDirFiles2(dir, path, dirName, file, deep, 0, ctx)
if err != nil {
log.Error(err)
}
})
ctx.Run(func() {
err := supDirFiles2(dir, path, file, deep, step, ctx)
if err != nil {
log.Error(err)
}
})
return nil
}
func Mkdir(src string) error {
_, err := os.Stat(src)
if os.IsNotExist(err) {
err = os.Mkdir(src, os.ModePerm)
if err != nil {
return err
}
}
return err
}
func MkdirAll(src string) error {
return os.MkdirAll(src, os.ModePerm)
}
func IsExist(src string) bool {
_, err := os.Stat(src)
return !os.IsNotExist(err)
}
func IsNotExist(src string) bool {
_, err := os.Stat(src)
return os.IsNotExist(err)
}
func IsPermission(src string) bool {
_, err := os.Stat(src)
return os.IsPermission(err)
}
func MustOpen(filePath string) (*os.File, error) {
perm := IsPermission(filePath)
if !perm {
return os.Create(filePath)
}
dir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("os.Getwd err: %v", err)
}
src := dir + PathSeparator + filePath
return Create(src)
}
func Create(filepath string) (*os.File, error) {
return OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
}
func Open(filepath string) (*os.File, error) {
return OpenFile(filepath, os.O_RDWR, 0666)
}
func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {
_, err := os.Stat(filepath)
if os.IsNotExist(err) {
dir := filepath2.CleanDir(filepath)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, err
}
}
return os.OpenFile(filepath, flag, perm)
}
// LastFile 当前目录最后一个创建的文件
func LastFile(dir string) (os.FileInfo, map[string]os.FileInfo, error) {
entries, err := os.ReadDir(dir)
if len(entries) == 0 {
return nil, nil, err
}
sort.Sort(DirEntries(entries))
lastFile, err := entries[0].Info()
if err != nil {
return nil, nil, err
}
m := make(map[string]os.FileInfo)
for _, entity := range entries {
m[entity.Name()], _ = entity.Info()
}
return lastFile, m, nil
}
func Move(src, dst string) error {
dir := filepath2.CleanDir(dst)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
return os.Rename(src, dst)
}
type FileSize int64
// MarshalText
func (f FileSize) MarshalText() ([]byte, error) {
buffer := bytes.NewBufferString("")
if f/FileSize(1024*1024*1024*8) > 0 {
buffer.WriteString(fmt.Sprintf("%.2f", float64(f)/float64(1024*1024*1024*8)))
buffer.WriteString("GB")
} else if f/FileSize(1024*1024*8) > 0 {
buffer.WriteString(fmt.Sprintf("%.2f", float64(f)/float64(1024*1024*8)))
buffer.WriteString("MB")
} else if f/FileSize(1024*8) > 0 {
buffer.WriteString(fmt.Sprintf("%.2f", float64(f)/float64(1024*8)))
buffer.WriteString("KB")
} else {
buffer.WriteString(fmt.Sprintf("%d", f/8))
buffer.WriteString("B")
}
return buffer.Bytes(), nil
}
// UnMarshalText
func (f *FileSize) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
unitLen := 1
unit := text[len(text)-1]
if unit == 'B' {
unit = text[len(text)-2]
}
switch unit {
case 'G', 'g':
unitLen = 2
size, err := strconv.Atoi(string(text[:len(text)-unitLen]))
if err != nil {
return err
}
*f = FileSize(size * 1024 * 1024 * 1024 * 8)
case 'M', 'm':
unitLen = 2
size, err := strconv.Atoi(string(text[:len(text)-unitLen]))
if err != nil {
return err
}
*f = FileSize(size * 1024 * 1024 * 8)
case 'K', 'k':
unitLen = 2
size, err := strconv.Atoi(string(text[:len(text)-unitLen]))
if err != nil {
return err
}
*f = FileSize(size * 1024)
default:
unitLen = 1
size, err := strconv.Atoi(string(text[:len(text)-unitLen]))
if err != nil {
return err
}
*f = FileSize(size * 8)
}
return nil
}