-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathutil.go
219 lines (203 loc) · 5.28 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
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
package main
import (
"bufio"
"database/sql"
"fmt"
"log"
"math"
"os"
"strings"
"time"
"github.com/google/uuid"
)
func split(fileInfo FileInfo, chunkSize int64) (indexList []int64, actualChunkSize int64) {
partNumber := int64(1)
indexList = []int64{0}
if int64(math.Ceil(float64(fileInfo.Size)/float64(chunkSize))) > 10000 {
chunkSize = fileInfo.Size/10000 + 1024 // 对于大于10000分片的大文件,自动调整Chunksize
}
for chunkSize*partNumber < fileInfo.Size { // 如果刚好是"=",则无需再分下一part,所以这里不能用"<="
indexList = append(indexList, chunkSize*partNumber)
partNumber += 1
}
return indexList, chunkSize
}
func withRetries(fn RetryFunc) error {
var err error
for i := 0; i < cfg.MaxRetries; i++ {
err = fn()
if err == nil {
break
}
log.Println("Failed to execute function: ", err, ". Retrying...")
time.Sleep(time.Duration(int64(math.Pow(2, float64(i)))) * time.Second)
}
return err
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func ByteCountSI(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%dBytes", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cBytes", float64(b)/float64(div), "KMGTPE"[exp])
}
func getIgnoreList() []*string {
log.Printf("Checking ignore files list in %s\n", cfg.IgnoreListPath)
ignoreList := []*string{}
_, err := os.Stat(cfg.IgnoreListPath)
if err != nil {
if os.IsNotExist(err) {
log.Printf("No ignore list in path %s\n", cfg.IgnoreListPath)
} else {
log.Println(err)
}
} else {
file, err := os.Open(cfg.IgnoreListPath)
if err != nil {
log.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
prefix := strings.TrimPrefix(scanner.Text(), "/")
ignoreList = append(ignoreList, &prefix)
}
if err := scanner.Err(); err != nil {
log.Println(err)
}
log.Printf("Found ignore files list with prefix Length: %d, in %s", len(ignoreList), cfg.IgnoreListPath)
}
return ignoreList
}
func isIgnored(key string, ignoreList []*string) bool {
for _, prefix := range ignoreList {
if strings.HasPrefix(key, *prefix) {
return true
}
}
return false
}
func getDatabase() (*sql.DB, error) {
var database *sql.DB
var err error
err = withRetries(func() error {
database, err = sql.Open("sqlite3", cfg.DBPath)
if err != nil {
fmt.Println("Failed to connect to sqlite3", err)
return err
}
statement, err := database.Prepare("CREATE TABLE IF NOT EXISTS download (ID TEXT PRIMARY KEY, key TEXT, bucket TEXT, part INT)")
if err != nil {
fmt.Println("Failed to prepare getDatabase statement: ", err)
return err
}
_, err = statement.Exec()
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return database, nil
}
func recordDownloadPart(partInfo PartInfo) {
err := withRetries(func() error {
database, err := getDatabase()
if err != nil {
fmt.Println("Failed to get sqlite3 database", err)
return err
}
defer database.Close()
uuid, err := uuid.NewRandom()
if err != nil {
return err
}
statement, err := database.Prepare("INSERT INTO download (ID, key, bucket, part) VALUES (?, ?, ?, ?)")
if err != nil {
fmt.Println("Failed to prepare recordDownloadPart statement: ", err)
return err
}
_, execErr := statement.Exec(uuid, partInfo.FromKey, partInfo.FromBucket, partInfo.PartNumber)
if execErr != nil {
fmt.Println("Failed to execute recordDownloadPart statement: ", execErr, ". Retrying...")
}
return execErr
})
if err != nil {
fmt.Println("Failed to execute recordDownloadPart statement after retries: ", err)
return
}
}
func getDownloadedParts(fileInfo FileInfo) ([]int, error) {
var partnumberList []int
err := withRetries(func() error {
database, err := getDatabase()
if err != nil {
fmt.Println("Failed to get sqlite3 database", err)
return err
}
defer database.Close()
partnumberList = []int{}
rows, err := database.Query("SELECT part FROM download WHERE key = ? AND bucket = ? ORDER BY part ASC", fileInfo.FromKey, fileInfo.FromBucket)
if err != nil {
fmt.Println("Failed to prepare getDownloadedParts statement: ", err)
return err
}
defer rows.Close()
var part int
for rows.Next() {
err := rows.Scan(&part)
if err != nil {
fmt.Println("Failed to scan row: ", err)
return err
}
partnumberList = append(partnumberList, part)
}
if err = rows.Err(); err != nil {
fmt.Println("Rows iteration error: ", err)
return err
}
return nil
})
if err != nil {
return nil, err
}
return partnumberList, nil
}
func deleteDownloadParts(fileInfo FileInfo) error {
err := withRetries(func() error {
database, err := getDatabase()
if err != nil {
fmt.Println("Failed to get sqlite3 database: ", err)
return err
}
defer database.Close()
statement, err := database.Prepare("DELETE FROM download WHERE key = ? AND bucket = ?")
if err != nil {
fmt.Println("Failed to prepare deleteDownloadParts statement: ", err)
return err
}
_, err = statement.Exec(fileInfo.FromKey, fileInfo.FromBucket)
if err != nil {
fmt.Println("Failed to execute deleteDownloadParts statement: ", err)
return err
}
return nil
})
return err
}