-
Notifications
You must be signed in to change notification settings - Fork 33
/
push_client_sync.go
504 lines (455 loc) · 12.9 KB
/
push_client_sync.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package sync
import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/no-src/gofs/action"
"github.com/no-src/gofs/api/apiclient"
"github.com/no-src/gofs/auth"
"github.com/no-src/gofs/contract"
"github.com/no-src/gofs/contract/push"
"github.com/no-src/gofs/internal/rate"
"github.com/no-src/gofs/server"
"github.com/no-src/gofs/server/client"
"github.com/no-src/nsgo/fsutil"
"github.com/no-src/nsgo/hashutil"
"github.com/no-src/nsgo/httputil"
"github.com/no-src/nsgo/jsonutil"
)
var (
errPushServerUnsupported = errors.New("the push server is unsupported")
errSendToPushServer = errors.New("send a request to the push server error")
)
type pushClientSync struct {
diskSync
pushAddr string
cookies []*http.Cookie
currentUser *auth.User
client apiclient.Client
httpClient httputil.HttpClient
}
// NewPushClientSync create an instance of the pushClientSync
func NewPushClientSync(opt Option) (Sync, error) {
// the fields of option
dest := opt.Dest
enableHTTP3 := opt.EnableHTTP3
enableTLS := opt.EnableTLS
certFile := opt.TLSCertFile
insecureSkipVerify := opt.TLSInsecureSkipVerify
users := opt.Users
chunkSize := opt.ChunkSize
if chunkSize <= 0 {
return nil, errInvalidChunkSize
}
ds, err := newDiskSync(opt)
if err != nil {
return nil, err
}
httpClient, err := httputil.NewHttpClient(insecureSkipVerify, certFile, enableHTTP3)
if err != nil {
return nil, err
}
var user *auth.User
if len(users) > 0 {
user = users[0]
}
s := &pushClientSync{
diskSync: *ds,
client: apiclient.New(dest.Host(), dest.Port(), enableTLS, certFile, user),
currentUser: user,
httpClient: httpClient,
}
err = s.start()
if err != nil {
return nil, err
}
return s, nil
}
func (pcs *pushClientSync) start() error {
err := pcs.client.Start()
if err != nil {
return err
}
err = pcs.info()
if err == nil {
pcs.client.Stop()
}
return err
}
func (pcs *pushClientSync) info() error {
info, err := pcs.client.GetInfo()
if err != nil {
return err
}
pcs.pushAddr = info.ServerAddr + info.PushAddr
return nil
}
func (pcs *pushClientSync) Create(path string) error {
if !pcs.dest.LocalSyncDisabled() {
if err := pcs.diskSync.Create(path); err != nil {
return err
}
}
return pcs.send(action.CreateAction, path)
}
func (pcs *pushClientSync) Symlink(oldname, newname string) error {
if !pcs.dest.LocalSyncDisabled() {
if err := pcs.diskSync.Symlink(oldname, newname); err != nil {
return err
}
}
return pcs.sendSymlink(oldname, newname)
}
func (pcs *pushClientSync) Write(path string) error {
if !pcs.dest.LocalSyncDisabled() {
if err := pcs.diskSync.Write(path); err != nil {
return err
}
}
isDir, err := pcs.IsDir(path)
if err != nil {
return err
}
if isDir {
return pcs.SyncOnce(path)
}
return pcs.send(action.WriteAction, path)
}
func (pcs *pushClientSync) Remove(path string) error {
if !pcs.dest.LocalSyncDisabled() {
if err := pcs.diskSync.Remove(path); err != nil {
return err
}
}
return pcs.send(action.RemoveAction, path)
}
func (pcs *pushClientSync) Rename(path string) error {
if !pcs.dest.LocalSyncDisabled() {
if err := pcs.diskSync.Remove(path); err != nil {
return err
}
}
return pcs.send(action.RenameAction, path)
}
func (pcs *pushClientSync) Chmod(path string) error {
pcs.logger.Debug("Chmod is unimplemented [%s]", path)
return nil
}
func (pcs *pushClientSync) IsDir(path string) (bool, error) {
return pcs.diskSync.IsDir(path)
}
func (pcs *pushClientSync) SyncOnce(path string) error {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
return filepath.WalkDir(absPath, func(currentPath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if pcs.pi.MatchPath(currentPath, "push client sync", "sync once") {
return nil
}
return pcs.syncWalk(currentPath, d, pcs, fsutil.Readlink)
})
}
func (pcs *pushClientSync) send(act action.Action, path string) (err error) {
isDir := false
if pcs.needCheckDir(act) {
isDir, err = pcs.IsDir(path)
if err != nil {
return err
}
}
var size int64
hash := ""
var hvs hashutil.HashValues
cTime := time.Now()
aTime := time.Now()
mTime := time.Now()
if pcs.needGetFileSizeAndHash(isDir, act) {
size, hash, hvs, err = pcs.hash.GetFileSizeAndHashCheckpoints(path, pcs.chunkSize, pcs.checkpointCount)
if err != nil {
return err
}
} else if pcs.needIgnoreDirWrite(isDir, act) {
return nil
}
if pcs.needGetFileTime(act) {
var timeErr error
cTime, aTime, mTime, timeErr = fsutil.GetFileTime(path)
if timeErr != nil {
return timeErr
}
}
isDirValue := contract.FsNotDir
if isDir {
isDirValue = contract.FsIsDir
}
relPath, err := filepath.Rel(pcs.sourceAbsPath, path)
if err != nil {
return err
}
relPath = filepath.ToSlash(relPath)
pd := push.PushData{
Action: act,
FileInfo: contract.FileInfo{
Path: relPath,
IsDir: isDirValue,
Size: size,
Hash: hash,
HashValues: hvs,
CTime: cTime.Unix(),
ATime: aTime.Unix(),
MTime: mTime.Unix(),
},
ForceChecksum: pcs.forceChecksum,
}
return pcs.sendPushData(pd, act, path)
}
func (pcs *pushClientSync) sendSymlink(oldname, newname string) (err error) {
cTime, aTime, mTime, timeErr := fsutil.GetFileTime(newname)
if timeErr != nil {
return timeErr
}
relPath, err := filepath.Rel(pcs.sourceAbsPath, newname)
if err != nil {
return err
}
relPath = filepath.ToSlash(relPath)
pd := push.PushData{
Action: action.SymlinkAction,
FileInfo: contract.FileInfo{
Path: relPath,
IsDir: contract.FsNotDir,
Size: 0,
CTime: cTime.Unix(),
ATime: aTime.Unix(),
MTime: mTime.Unix(),
LinkTo: oldname,
},
ForceChecksum: pcs.forceChecksum,
}
return pcs.sendPushData(pd, pd.Action, newname)
}
func (pcs *pushClientSync) needCheckDir(act action.Action) bool {
return act != action.RemoveAction && act != action.RenameAction
}
func (pcs *pushClientSync) needGetFileSizeAndHash(isDir bool, act action.Action) bool {
return !isDir && act == action.WriteAction
}
func (pcs *pushClientSync) needIgnoreDirWrite(isDir bool, act action.Action) bool {
return isDir && act == action.WriteAction
}
func (pcs *pushClientSync) needGetFileTime(act action.Action) bool {
return act == action.WriteAction || act == action.CreateAction
}
func (pcs *pushClientSync) sendPushData(pd push.PushData, act action.Action, path string) error {
if act == action.WriteAction {
return pcs.sendFileChunk(path, pd)
}
resp, err := pcs.httpPostWithAuth(pcs.pushAddr, act, push.ParamUpFile, path, pd, nil)
if err != nil {
return err
}
defer resp.Body.Close()
_, _, err = pcs.checkApiResult(resp)
return err
}
func (pcs *pushClientSync) sendFileChunk(path string, pd push.PushData) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
var offset int64
chunk := make([]byte, pcs.chunkSize)
isEnd := false
// if loopCount == 0 means is a request that check file size and hash value only, send it
// if loopCount == 1 means read an empty file maybe, send it
loopCount := -1
checkChunkHash := false
ra := rate.NewReaderAt(f, pcs.maxTranRate, pcs.logger)
for {
loopCount++
n, err := ra.ReadAt(chunk, offset)
if fsutil.IsNonEOF(err) {
return err
}
if fsutil.IsEOF(err) {
isEnd = true
}
chunkSize := n
pd.PushAction = push.WritePushAction
if pcs.needCheckHash(loopCount, chunkSize) {
// if file size is greater than zero in first loop, try to compare file and chunk hash
isEnd = false
n = 0
pd.PushAction = push.CompareFileAndChunkPushAction
checkChunkHash = true
} else if pcs.isReadEmptyFile(loopCount, chunkSize) {
// if read data nothing, send the empty file and cancel file compare request
loopCount++
} else if checkChunkHash {
pd.PushAction = push.CompareChunkPushAction
n = 0
}
if pcs.needSendChunkRequest(loopCount, chunkSize) {
broken, err := pcs.sendChunkRequest(path, &pd, &offset, chunkSize, &checkChunkHash, chunk, n, &isEnd)
if broken {
return err
}
}
if isEnd {
// read to end, send a truncate request finally
return pcs.sendTruncate(path, pd, offset)
}
}
}
func (pcs *pushClientSync) sendChunkRequest(path string, pd *push.PushData, offset *int64, chunkSize int, checkChunkHash *bool, chunk []byte, n int, isEnd *bool) (broken bool, err error) {
defer func() {
// only send HashValues once
if len(pd.FileInfo.HashValues) > 0 {
pd.FileInfo.HashValues = nil
}
}()
pd.Chunk.Offset = *offset
pd.Chunk.Size = int64(chunkSize)
if *checkChunkHash {
pd.Chunk.Hash = pcs.hash.Hash(chunk[:chunkSize])
}
resp, err := pcs.httpPostWithAuth(pcs.pushAddr, action.WriteAction, push.ParamUpFile, path, *pd, chunk[:n])
if err != nil {
return true, err
}
code, hv, err := pcs.checkApiResult(resp)
resp.Body.Close()
if err != nil {
return true, err
} else if code == contract.NotModified {
pcs.logger.Debug("upload a file that not modified, ignore and abort next request => %s", path)
return true, nil
} else if code == contract.ChunkNotModified {
// current chunk is not modified, continue to compare next chunk in the next loop
pcs.logger.Debug("upload a file chunk that not modified, continue to compare next chunk [%d]=> %s", *offset, path)
*checkChunkHash = true
*offset += int64(chunkSize)
// if the checkpoint compare result offset is greater than the next offset, then replace it
if hv != nil && hv.Offset > *offset {
*offset = hv.Offset
}
} else if code == contract.Modified {
// file is modified and the first chunk is modified too, upload the file in the next loop
*offset = 0
*checkChunkHash = false
*isEnd = false
} else if code == contract.ChunkModified {
// write current chunk in the next loop
*checkChunkHash = false
*isEnd = false
} else {
// get success code, continue to write next chunk in the next loop or send a truncate request in the end
*offset += int64(chunkSize)
}
return false, nil
}
func (pcs *pushClientSync) sendTruncate(path string, pd push.PushData, offset int64) error {
pd.PushAction = push.TruncatePushAction
pd.Chunk.Offset = offset
resp, err := pcs.httpPostWithAuth(pcs.pushAddr, action.WriteAction, push.ParamUpFile, path, pd, nil)
if err != nil {
return err
}
defer resp.Body.Close()
_, _, err = pcs.checkApiResult(resp)
return err
}
func (pcs *pushClientSync) needCheckHash(loopCount, dataLen int) bool {
return loopCount == 0 && dataLen > 0
}
func (pcs *pushClientSync) isReadEmptyFile(loopCount, dataLen int) bool {
return loopCount == 0 && dataLen <= 0
}
func (pcs *pushClientSync) needSendChunkRequest(loopCount, dataLen int) bool {
return dataLen > 0 || loopCount <= 1
}
func (pcs *pushClientSync) checkApiResult(resp *http.Response) (code contract.Code, hv *hashutil.HashValue, err error) {
var apiResult server.ApiResult
respData, err := io.ReadAll(resp.Body)
if err != nil {
return code, nil, err
}
err = jsonutil.Unmarshal(respData, &apiResult)
if err != nil {
return code, nil, err
}
if apiResult.Data != nil {
dataBytes, err := jsonutil.Marshal(apiResult.Data)
if err != nil {
return code, nil, err
}
err = jsonutil.Unmarshal(dataBytes, &hv)
if err != nil {
return code, nil, err
}
}
code = apiResult.Code
switch code {
case contract.NotModified, contract.ChunkNotModified, contract.Modified, contract.ChunkModified:
return code, hv, nil
}
if code != contract.Success {
err = fmt.Errorf("%w => %s", errSendToPushServer, apiResult.Message)
}
return code, hv, err
}
func (pcs *pushClientSync) httpPostWithAuth(rawURL string, act action.Action, fieldName string, fileName string, pd push.PushData, chunk []byte) (resp *http.Response, err error) {
pdData, err := jsonutil.Marshal(pd)
if err != nil {
return nil, err
}
data := url.Values{}
data.Set(push.ParamPushData, string(pdData))
sendFile := false
if act == action.WriteAction {
sendFile = true
}
if sendFile {
resp, err = pcs.httpClient.HttpPostFileChunkWithCookie(rawURL, fieldName, fileName, data, chunk, pcs.cookies...)
} else {
resp, err = pcs.httpClient.HttpPostWithCookie(rawURL, data, pcs.cookies...)
}
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusUnauthorized && pcs.currentUser != nil {
// auto login
parseUrl, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
user := pcs.currentUser
cookies, err := client.SignIn(pcs.httpClient, parseUrl.Scheme, parseUrl.Host, user.UserName(), user.Password(), pcs.logger)
if err != nil {
return nil, err
}
if len(cookies) > 0 {
pcs.cookies = cookies
pcs.logger.Debug("try to auto login file server success maybe, retry to get resource => %s", rawURL)
if sendFile {
return pcs.httpClient.HttpPostFileChunkWithCookie(rawURL, fieldName, fileName, data, chunk, pcs.cookies...)
}
return pcs.httpClient.HttpPostWithCookie(rawURL, data, pcs.cookies...)
}
return nil, errFileServerUnauthorized
} else if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%w => %s", errPushServerUnsupported, rawURL)
}
return resp, err
}