-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.go
98 lines (92 loc) · 2.01 KB
/
analyze.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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
func loopMismatchFiles(diffResult string, config *Config, walker func(bucket string, path string, absolutePath string) error) error {
file, err := os.Open(diffResult)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
for scanner.Scan() {
text := scanner.Text()
arr := strings.Split(text, ",")
bucket := arr[0]
path := arr[1]
bucketPath := config.Dirs[bucket]
absolutePath := filepath.Join(bucketPath, path)
err := walker(bucket, path, absolutePath)
if err != nil {
log.Println(err)
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func CountMismatchSize(diffResult string, config *Config) {
var totalSize int64
err := loopMismatchFiles(diffResult, config, func(bucket string, path string, absolutePath string) error {
solid, err := isSolidFile(absolutePath)
if err != nil {
return err
}
if !solid {
return nil
}
fi, _ := os.Stat(path)
size := fi.Size()
totalSize += size
return nil
})
if err != nil {
log.Fatal(err)
}
totalGB := totalSize / gb
fmt.Printf("total size is: %dGB", totalGB)
}
func CountMismatchCount(diffResult string, config *Config) {
totalCount := 0
err := loopMismatchFiles(diffResult, config, func(bucket string, path string, absolutePath string) error {
solid, err := isSolidFile(absolutePath)
if err != nil {
return err
}
if !solid {
return nil
}
// fmt.Println(absolutePath)
totalCount++
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("total count is: %d\n", totalCount)
}
func isSolidFile(path string) (bool, error) {
fi, err := os.Lstat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
// skip simlink files
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
return false, nil
}
if fi.IsDir() {
return false, nil
}
return true, nil
}