-
Notifications
You must be signed in to change notification settings - Fork 180
/
copy-move.go
597 lines (537 loc) · 19.6 KB
/
copy-move.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package views
import (
"context"
"fmt"
"io"
"math"
"path"
"strings"
"sync"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/pborman/uuid"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
context2 "github.com/pydio/cells/common/utils/context"
"github.com/pydio/cells/common/utils/permissions"
"github.com/pydio/cells/common/views/models"
)
type (
sessionIDKey struct{}
)
const (
// Consider move takes 1s per 100 MB of data to copy
lockExpirationRatioSize = 1024 * 1024 * 100
lockExpirationRatioNumber = 25
)
// WithSessionID returns a context which knows its service assigned color
func WithSessionID(ctx context.Context, session string) context.Context {
return context.WithValue(ctx, sessionIDKey{}, session)
}
// GetSessionID returns the session ID in context
func GetSessionID(ctx context.Context) (string, bool) {
res, ok := ctx.Value(sessionIDKey{}).(string)
return res, ok
}
func extractDSFlat(ctx context.Context, handler Handler, sourceNode, targetNode *tree.Node) (innerFlat, srcFlat, targetFlat bool) {
if router, ok := handler.(*Router); ok {
// We passed a router, call is external, use WrapCallback
router.WrapCallback(func(inputFilter NodeFilter, outputFilter NodeFilter) error {
srcCtx, _, _ := inputFilter(ctx, sourceNode, "from")
tgtCtx, _, _ := inputFilter(ctx, targetNode, "to")
srcDS, _ := GetBranchInfo(srcCtx, "from")
tgtDS, _ := GetBranchInfo(tgtCtx, "to")
if srcDS.Name == tgtDS.Name && srcDS.FlatStorage {
innerFlat = true
}
targetFlat = tgtDS.FlatStorage
srcFlat = srcDS.FlatStorage
return nil
})
} else {
// We passed a Handler, we are already in the routing stack, contexts should be loaded
srcDS, _ := GetBranchInfo(ctx, "from")
tgtDS, _ := GetBranchInfo(ctx, "to")
if srcDS.Name == tgtDS.Name && srcDS.FlatStorage {
innerFlat = true
}
targetFlat = tgtDS.FlatStorage
srcFlat = srcDS.FlatStorage
}
return
}
// is403 checks if error is not nil and has code 403
func is403(e error) bool {
if e == nil {
return false
}
return errors.Parse(e.Error()).Code == 403
}
// CopyMoveNodes performs a recursive copy or move operation of a node to a new location. It can be inter- or intra-datasources.
// It will eventually pass contextual metadata like X-Pydio-Session (to batch event inside the SYNC) or X-Pydio-Move (to
// reconcile creates and deletes when move is done between two differing datasources).
func CopyMoveNodes(ctx context.Context, router Handler, sourceNode *tree.Node, targetNode *tree.Node, move bool, isTask bool, statusChan chan string, progressChan chan float32, tFunc ...i18n.TranslateFunc) (oErr error) {
innerFlat, sourceFlat, targetFlat := extractDSFlat(ctx, router, sourceNode, targetNode)
if innerFlat && move {
log.Logger(ctx).Debug("Move on Flat storage : switching to direct index update")
_, e := router.UpdateNode(ctx, &tree.UpdateNodeRequest{From: sourceNode, To: targetNode})
return e
}
sessionPrefix := common.SyncSessionPrefixCopy
if move {
sessionPrefix = common.SyncSessionPrefixMove
}
session := sessionPrefix + uuid.New()
childrenMoved := 0
var totalSize int64
var locker permissions.SessionLocker
ctx = WithSessionID(ctx, session)
// Make sure all sessions are purged !
defer func() {
if p := recover(); p != nil {
log.Logger(ctx).Error("Error during copy/move", zap.Error(p.(error)))
oErr = p.(error)
go func() {
<-time.After(10 * time.Second)
log.Logger(ctx).Info("Forcing close session " + session + " and unlock")
client.Publish(context.Background(), client.NewPublication(common.TopicIndexEvent, &tree.IndexEvent{
SessionForceClose: session,
}))
if locker != nil {
locker.Unlock(ctx)
}
}()
} else if targetFlat && locker != nil {
locker.Unlock(ctx)
}
}()
publishError := func(dsName, errorPath string) {
client.Publish(context.Background(), client.NewPublication(common.TopicIndexEvent, &tree.IndexEvent{
ErrorDetected: true,
DataSourceName: dsName,
ErrorPath: errorPath,
}))
}
// Setting up logger
logger := log.Logger(ctx)
var taskLogger *zap.Logger
if isTask {
taskLogger = log.TasksLogger(ctx)
} else {
taskLogger = zap.New(zapcore.NewNopCore())
}
// Read root of target to detect if it is on the same datasource as sourceNode
var crossDs bool
var sourceDs, targetDs string
sourceDs = sourceNode.GetStringMeta(common.MetaNamespaceDatasourceName)
if move {
if tDs := targetNode.GetStringMeta(common.MetaNamespaceDatasourceName); tDs != "" {
targetDs = tDs
crossDs = targetDs != sourceDs
} else {
parts := strings.Split(strings.Trim(targetNode.Path, "/"), "/")
if len(parts) > 0 {
if testRoot, e := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: parts[0]}}); e == nil {
targetDs = testRoot.Node.GetStringMeta(common.MetaNamespaceDatasourceName)
crossDs = targetDs != sourceDs
}
}
}
}
var childLockDir, childLockBase string
if parentTarget, e := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: path.Dir(targetNode.Path)}}); e == nil {
childLockDir = parentTarget.GetNode().Uuid
childLockBase = path.Base(targetNode.Path)
}
if !IsUnitTestEnv { // Do not trigger during unit tests as it calls ACL service
if move {
log.Logger(ctx).Debug("Setting Lock on SourceNode with session " + session)
locker = permissions.NewLockSession(sourceNode.Uuid, session, time.Second*30)
} else {
locker = permissions.NewLockSession("", session, time.Second*30)
}
if childLockDir != "" {
log.Logger(ctx).Debug("Setting ChildLock on TargetNode Parent")
locker.AddChildTarget(childLockDir, childLockBase)
}
// Will be unlocked by sync process, or if targetFlat, manually unlock
if err := locker.Lock(ctx); err != nil {
log.Logger(ctx).Warn("Could not init lockSession", zap.Error(err))
}
}
if !sourceNode.IsLeaf() {
if targetFlat || sourceFlat {
log.Logger(ctx).Debug("[Flat Target] Creating target folder before copy")
// Manually create target folder
tgtUuid := uuid.New()
tgt := ctx
if move {
tgtUuid = sourceNode.Uuid
tgt = context2.WithAdditionalMetadata(tgt, map[string]string{common.XPydioMoveUuid: tgtUuid})
}
if _, e := router.CreateNode(tgt, &tree.CreateNodeRequest{Node: &tree.Node{
Uuid: tgtUuid,
Path: targetNode.Path,
Type: tree.NodeType_COLLECTION,
MTime: time.Now().Unix(),
}, IndexationSession: session}); e != nil {
return e
}
}
prefixPathSrc := strings.TrimRight(sourceNode.Path, "/")
prefixPathTarget := strings.TrimRight(targetNode.Path, "/")
targetDsPath := targetNode.GetStringMeta(common.MetaNamespaceDatasourcePath)
// List all children and move them all
streamer, err := router.ListNodes(ctx, &tree.ListNodesRequest{
Node: sourceNode,
Recursive: true,
})
if err != nil {
logger.Error("Copy/move - List Nodes", zap.Error(err))
return err
}
var children []*tree.Node
defer streamer.Close()
var statErrors int
var foldersCount, fileCounts int
for {
child, cE := streamer.Recv()
if cE != nil {
break
}
if child == nil {
continue
}
if child.Node.IsLeaf() {
if _, statErr := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: child.Node, ObjectStats: true}); statErr != nil {
statErrors++
}
totalSize += child.Node.Size
fileCounts++
} else {
foldersCount++
}
children = append(children, child.Node)
}
if statErrors > 0 {
// There are some missing children, this copy/move operation will fail - interrupt now
publishError(sourceDs, sourceNode.Path)
return fmt.Errorf("errors found while copy/move node, stopping")
}
if len(children) > 0 {
cMess := fmt.Sprintf("There are %v children to move", len(children))
logger.Info(cMess)
taskLogger.Info(cMess)
}
//total := len(children)
if !move || targetFlat || sourceFlat {
// For Copy case, first create new folders with fresh UUID - For FlatTarget, create with previous node Uuid
for fI, childNode := range children {
if childNode.IsLeaf() {
continue
}
childPath := childNode.Path
relativePath := strings.TrimPrefix(childPath, prefixPathSrc+"/")
targetPath := prefixPathTarget + "/" + relativePath
statusChan <- copyMoveStatusKey(relativePath, move, tFunc...)
folderNode := childNode.Clone()
folderNode.Path = targetPath
folderNode.Uuid = uuid.New()
createContext := ctx
sess := session
if (targetFlat || sourceFlat) && move {
if sourceFlat && fileCounts == 0 && fI == len(children)-1 {
log.Logger(ctx).Info("Sending session close on last folder")
sess = common.SyncSessionClose_ + session
}
createContext = context2.WithAdditionalMetadata(createContext, map[string]string{
common.XPydioMoveUuid: childNode.Uuid,
})
folderNode.Uuid = childNode.Uuid
}
if targetDsPath != "" {
folderNode.SetMeta(common.MetaNamespaceDatasourcePath, path.Join(targetDsPath, relativePath))
}
log.Logger(ctx).Info("Creating folder", folderNode.ZapPath(), folderNode.ZapUuid())
_, e := router.CreateNode(createContext, &tree.CreateNodeRequest{Node: folderNode, IndexationSession: sess, UpdateIfExists: true})
if e != nil {
logger.Error("-- Create Folder ERROR", zap.Error(e), zap.Any("from", childNode.Path), zap.Any("to", targetPath))
publishError(targetDs, folderNode.Path)
panic(e)
}
logger.Debug("-- Copy Folder Success ", zap.String("to", targetPath), childNode.Zap())
taskLogger.Info("-- Copied Folder To " + targetPath)
}
}
if move {
// For move, update lock expiration based on total size
updateLockerForByteSize(ctx, locker, totalSize, len(children))
}
wg := &sync.WaitGroup{}
queue := make(chan struct{}, 4)
t := time.Now()
var lastNode *tree.Node
var errs []error
progress := ©PgReader{
progressChan: progressChan,
total: totalSize,
}
for idx, childNode := range children {
if idx == len(children)-1 {
lastNode = childNode
continue
}
// copy for inner function
childNode := childNode
wg.Add(1)
queue <- struct{}{}
go func() {
defer func() {
<-queue
defer wg.Done()
}()
e := processCopyMove(ctx, router, session, move, crossDs, sourceDs, targetDs, sourceFlat, targetFlat, false, childNode, prefixPathSrc, prefixPathTarget, targetDsPath, logger, publishError, statusChan, progress, tFunc...)
if is403(e) {
childrenMoved++
taskLogger.Info("-- Ignoring " + childNode.Path + " (" + e.Error() + ")")
} else if e != nil {
errs = append(errs, e)
} else {
childrenMoved++
taskLogger.Info("-- Copy/Move Success for " + childNode.Path)
}
}()
}
wg.Wait()
if len(errs) > 0 {
panic(errs[0])
}
if lastNode != nil {
// Now process very last node
e := processCopyMove(ctx, router, session, move, crossDs, sourceDs, targetDs, sourceFlat, targetFlat, true, lastNode, prefixPathSrc, prefixPathTarget, targetDsPath, logger, publishError, statusChan, progress, tFunc...)
if is403(e) {
childrenMoved++
taskLogger.Info("-- Ignoring " + lastNode.Path + " (" + e.Error() + ")")
} else if e != nil {
panic(e)
} else {
childrenMoved++
taskLogger.Info("-- Copy/Move Success for " + lastNode.Path)
}
}
log.Logger(ctx).Info("Recursive copy operation timing", zap.Duration("duration", time.Since(t)))
}
if childrenMoved > 0 {
log.Logger(ctx).Info(fmt.Sprintf("Successfully copied or moved %v, now moving parent node", childrenMoved))
}
// Now Copy/Move initial node
if sourceNode.IsLeaf() {
updateLockerForByteSize(ctx, locker, sourceNode.Size, 1)
// Prepare Meta for Copy/Delete operations. If Move across DS or Copy, we send directly the close- session
// as this will be a one shot operation on each datasource.
copyMeta := make(map[string]string)
deleteMeta := make(map[string]string)
closeSession := common.SyncSessionClose_ + session
if move {
copyMeta[common.XAmzMetaDirective] = "COPY"
deleteMeta[common.XPydioSessionUuid] = closeSession
if crossDs {
copyMeta[common.XPydioSessionUuid] = closeSession
// Identify copy/delete across 2 datasources
copyMeta[common.XPydioMoveUuid] = sourceNode.Uuid
deleteMeta[common.XPydioMoveUuid] = sourceNode.Uuid
} else {
copyMeta[common.XPydioSessionUuid] = session
}
} else {
copyMeta[common.XAmzMetaDirective] = "REPLACE"
copyMeta[common.XPydioSessionUuid] = closeSession
}
statusChan <- copyMoveStatusKey(sourceNode.Path, move, tFunc...)
_, e := router.CopyObject(ctx, sourceNode, targetNode, &models.CopyRequestData{
Metadata: copyMeta,
Progress: ©PgReader{offset: 0, total: sourceNode.Size, progressChan: progressChan},
})
if e != nil {
publishError(sourceDs, sourceNode.Path)
publishError(targetDs, targetNode.Path)
panic(e)
}
// Remove Source Node
if move {
ctx = context2.WithAdditionalMetadata(ctx, deleteMeta)
_, moveErr := router.DeleteNode(ctx, &tree.DeleteNodeRequest{Node: sourceNode})
if moveErr != nil {
logger.Error("-- Delete Source Error / Reverting Copy", zap.Error(moveErr), sourceNode.Zap())
router.DeleteNode(ctx, &tree.DeleteNodeRequest{Node: targetNode})
publishError(sourceDs, sourceNode.Path)
panic(moveErr)
}
}
} else {
if move && sourceFlat {
// Flat source : remove original folder
log.Logger(ctx).Info("Removing source folder after move")
// Manually create target folder
tgt := context2.WithAdditionalMetadata(ctx, map[string]string{common.XPydioMoveUuid: sourceNode.Uuid})
if _, e := router.DeleteNode(tgt, &tree.DeleteNodeRequest{Node: sourceNode}); e != nil {
return e
}
}
if !move && !targetFlat {
// Now create root target in index, except in Flat mode
session = common.SyncSessionClose_ + session
logger.Debug("-- Copying sourceNode with empty Uuid - Close Session")
targetNode.Type = tree.NodeType_COLLECTION
_, e := router.CreateNode(ctx, &tree.CreateNodeRequest{Node: targetNode, IndexationSession: session, UpdateIfExists: true})
if e != nil {
panic(e)
}
taskLogger.Info("-- Copied sourceNode with empty Uuid - Close Session")
}
}
return
}
func updateLockerForByteSize(ctx context.Context, locker permissions.SessionLocker, totalSize int64, numberOfFiles int) {
if locker == nil {
return
}
countRatio := math.Ceil(float64(numberOfFiles) / float64(lockExpirationRatioNumber))
ratio := math.Ceil(float64(totalSize) / float64(lockExpirationRatioSize))
r := math.Max(countRatio, ratio) + 2
newD := time.Duration(int64(r) * int64(time.Second))
log.Logger(ctx).Debug("Updating lock expiration to ", zap.Duration("duration", newD))
locker.UpdateExpiration(ctx, newD)
}
func copyMoveStatusKey(keyPath string, move bool, tFunc ...i18n.TranslateFunc) string {
var statusPath = keyPath
if path.Base(statusPath) == common.PydioSyncHiddenFile {
statusPath = path.Dir(statusPath)
}
statusPath = path.Base(statusPath)
status := "Copying " + statusPath
tKey := "Jobs.User.CopyingItem"
if move {
status = "Moving " + statusPath
tKey = "Jobs.User.MovingItem"
}
if len(tFunc) > 0 {
status = strings.Replace(tFunc[0](tKey), "%s", statusPath, -1)
}
return status
}
func processCopyMove(ctx context.Context, handler Handler, session string, move, crossDs bool, sourceDs, targetDs string, sourceFlat, targetFlat, closeSession bool, childNode *tree.Node, prefixPathSrc, prefixPathTarget, targetDsPath string, logger *zap.Logger, publishError func(string, string), statusChan chan string, progress io.Reader, tFunc ...i18n.TranslateFunc) error {
childPath := childNode.Path
relativePath := strings.TrimPrefix(childPath, prefixPathSrc+"/")
targetPath := prefixPathTarget + "/" + relativePath
targetNode := &tree.Node{Path: targetPath}
if targetDsPath != "" {
targetNode.SetMeta(common.MetaNamespaceDatasourcePath, path.Join(targetDsPath, relativePath))
}
var justCopied *tree.Node
justCopied = nil
isHidden := path.Base(childPath) == common.PydioSyncHiddenFile
// Copy files - For "Copy" operation or targetFlat, do NOT copy .pydio files
if childNode.IsLeaf() && !(isHidden && (!move || targetFlat)) {
logger.Info("Copy "+childNode.Path+" to "+targetPath, zap.Bool("isHidden", isHidden), zap.Bool("targetFlat", targetFlat))
statusChan <- copyMoveStatusKey(relativePath, move, tFunc...)
meta := make(map[string]string, 1)
if move {
meta[common.XAmzMetaDirective] = "COPY"
} else {
meta[common.XAmzMetaDirective] = "REPLACE"
}
meta[common.XPydioSessionUuid] = session
if crossDs {
/*
if idx == len(children)-1 {
meta[common.XPydioSessionUuid] = "close-" + session
}
*/
if closeSession {
meta[common.XPydioSessionUuid] = common.SyncSessionClose_ + session
}
if move {
meta[common.XPydioMoveUuid] = childNode.Uuid
}
}
_, e := handler.CopyObject(ctx, childNode, targetNode, &models.CopyRequestData{
Metadata: meta,
Progress: progress,
})
if is403(e) {
logger.Warn("-- Copy Ignored", zap.Error(e), zap.Any("from", childNode.Path), zap.Any("to", targetPath))
return e
}
if e != nil {
logger.Error("-- Copy ERROR", zap.Error(e), zap.Any("from", childNode.Path), zap.Any("to", targetPath))
publishError(sourceDs, childNode.Path)
publishError(targetDs, targetPath)
return e
}
justCopied = targetNode
logger.Debug("-- Copy Success: ", zap.String("to", targetPath), childNode.Zap())
}
// Remove original for move case - Skip folders for flat targets, as they are removed recursively
if move && !(!childNode.IsLeaf() && sourceFlat) {
// If we're sending the last Delete here - then we close the session at the same time
if closeSession {
session = common.SyncSessionClose_ + session
}
delCtx := ctx
if crossDs {
delCtx = context2.WithAdditionalMetadata(ctx, map[string]string{common.XPydioMoveUuid: childNode.Uuid})
}
_, moveErr := handler.DeleteNode(delCtx, &tree.DeleteNodeRequest{Node: childNode, IndexationSession: session})
if moveErr != nil {
log.Logger(ctx).Error("-- Delete Error / Reverting Copy", zap.Error(moveErr), childNode.Zap())
if justCopied != nil {
if _, revertErr := handler.DeleteNode(delCtx, &tree.DeleteNodeRequest{Node: justCopied}); revertErr != nil {
log.Logger(ctx).Error("---- Could not Revert", zap.Error(revertErr), justCopied.Zap())
}
}
if is403(moveErr) {
moveErr = fmt.Errorf("some original objects are not allowed to be deleted") // replace by a non-403 to trigger error
} else {
publishError(sourceDs, childNode.Path)
}
return moveErr
}
logger.Debug("-- Delete Success " + childNode.Path)
}
return nil
}
type copyPgReader struct {
offset int64
total int64
progressChan chan float32
}
func (c *copyPgReader) Read(p []byte) (n int, err error) {
c.offset += int64(len(p))
c.progressChan <- float32(c.offset) / float32(c.total)
return len(p), nil
}