cubefs

Форк
0
/
util.go 
63 строки · 1.0 Кб
1
package metanode
2

3
import (
4
	"fmt"
5
	"os"
6
	"sort"
7
	"strconv"
8
	"strings"
9
)
10

11
type DelExtFile []os.FileInfo
12

13
func (del DelExtFile) Len() int {
14
	return len(del)
15
}
16

17
func (del DelExtFile) Swap(i, j int) {
18
	del[i], del[j] = del[j], del[i]
19
}
20

21
func (del DelExtFile) Less(i, j int) bool {
22
	idx1 := getDelExtFileIdx(del[i].Name())
23
	idx2 := getDelExtFileIdx(del[j].Name())
24

25
	return idx1 < idx2
26
}
27

28
func getDelExtFileIdx(name string) int64 {
29
	arr := strings.Split(name, "_")
30
	size := len(arr)
31
	if size < 2 {
32
		panic(fmt.Errorf("file name is not legal, %s", name))
33
	}
34

35
	idx, err := strconv.ParseInt(arr[size-1], 10, 64)
36
	if err != nil {
37
		panic(fmt.Errorf("file name is not legal, %s", name))
38
	}
39

40
	return idx
41
}
42

43
func sortDelExtFileInfo(files []os.FileInfo) []os.FileInfo {
44
	newFiles := make([]os.FileInfo, 0)
45

46
	for _, info := range files {
47
		if info.IsDir() {
48
			continue
49
		}
50

51
		if strings.HasPrefix(info.Name(), prefixDelExtent) {
52
			newFiles = append(newFiles, info)
53
		}
54
	}
55

56
	if len(newFiles) <= 1 {
57
		return newFiles
58
	}
59

60
	sort.Sort(DelExtFile(newFiles))
61

62
	return newFiles
63
}
64

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.