forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
64 lines (48 loc) · 1.04 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package metanode
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type DelExtFile []os.FileInfo
func (del DelExtFile) Len() int {
return len(del)
}
func (del DelExtFile) Swap(i, j int) {
del[i], del[j] = del[j], del[i]
}
func (del DelExtFile) Less(i, j int) bool {
idx1 := getDelExtFileIdx(del[i].Name())
idx2 := getDelExtFileIdx(del[j].Name())
return idx1 < idx2
}
func getDelExtFileIdx(name string) int64 {
arr := strings.Split(name, "_")
size := len(arr)
if size < 2 {
panic(fmt.Errorf("file name is not legal, %s", name))
}
idx, err := strconv.ParseInt(arr[size-1], 10, 64)
if err != nil {
panic(fmt.Errorf("file name is not legal, %s", name))
}
return idx
}
func sortDelExtFileInfo(files []os.FileInfo) []os.FileInfo {
newFiles := make([]os.FileInfo, 0)
for _, info := range files {
if info.IsDir() {
continue
}
if strings.HasPrefix(info.Name(), prefixDelExtent) {
newFiles = append(newFiles, info)
}
}
if len(newFiles) <= 1 {
return newFiles
}
sort.Sort(DelExtFile(newFiles))
return newFiles
}