forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcr_actions.go
991 lines (891 loc) · 28.3 KB
/
cr_actions.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"fmt"
"golang.org/x/net/context"
)
// copyUnmergedEntryAction says that the unmerged entry for the given
// name should be copied directly into the merged version of the
// directory; there should be no conflict. If symPath is non-empty, then
// the unmerged entry becomes a symlink to that path.
//
// Note that under some circumstances (e.g., when the target entry has
// been updated in the merged branch but not in the unmerged branch),
// this action may copy the /merged/ entry instead of the unmerged
// one.
type copyUnmergedEntryAction struct {
fromName string
toName string
symPath string
sizeOnly bool
unique bool
unmergedEntry DirEntry
attr []attrChange
}
func fixupNamesInOps(fromName string, toName string, ops []op,
chains *crChains) (retOps []op) {
retOps = make([]op, 0, len(ops))
for _, uop := range ops {
done := false
switch realOp := uop.(type) {
// The only names that matter are in createOps or
// setAttrOps. rms on the unmerged side wouldn't be
// part of the unmerged entry
case *createOp:
if realOp.NewName == fromName {
realOpCopy := *realOp
realOpCopy.NewName = toName
retOps = append(retOps, &realOpCopy)
done = true
// Fix up the new name if this is a rename.
if realOp.renamed && len(realOp.Refs()) > 0 {
renamed := realOp.Refs()[0]
original, ok := chains.originals[renamed]
if !ok {
original = renamed
}
if ri, ok := chains.renamedOriginals[original]; ok {
ri.newName = toName
chains.renamedOriginals[original] = ri
}
}
}
case *setAttrOp:
if realOp.Name == fromName {
realOpCopy := *realOp
realOpCopy.Name = toName
retOps = append(retOps, &realOpCopy)
done = true
}
}
if !done {
retOps = append(retOps, uop)
}
}
return retOps
}
func (cuea *copyUnmergedEntryAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
if cuea.symPath != "" {
return false, zeroPtr, nil
}
unmergedEntry, ok := unmergedBlock.Children[cuea.fromName]
if !ok {
return false, zeroPtr, NoSuchNameError{cuea.fromName}
}
// If:
// * The entry BlockPointer has an unmerged chain with no (or
// attr-only) ops; AND
// * The entry BlockPointer does have a (non-deleted) merged chain
// copy the merged entry instead by fetching the block for its merged
// most recent parent and using that as the source, just copying over
// the "sizeAttr" fields.
ptr := unmergedEntry.BlockPointer
if chain, ok := unmergedChains.byMostRecent[ptr]; ok {
// If the chain has only setAttr ops, we still want to do the
// swap, but we need to preserve those unmerged attr changes.
for _, op := range chain.ops {
// As soon as we find an op that is NOT a setAttrOp, we
// should abort the swap. Otherwise save the changed
// attributes so we can re-apply them during do().
if sao, ok := op.(*setAttrOp); ok {
cuea.attr = append(cuea.attr, sao.Attr)
} else {
return false, zeroPtr, nil
}
}
ptr = chain.original
}
if _, ok := mergedChains.byOriginal[ptr]; !ok ||
mergedChains.isDeleted(ptr) {
return false, zeroPtr, nil
}
// If this entry was renamed, use the new parent; otherwise,
// return zeroPtr.
parentOrig, newName, ok := mergedChains.renamedParentAndName(ptr)
cuea.unmergedEntry = unmergedEntry
cuea.sizeOnly = true
if !ok {
// What about the unmerged branch?
ri, ok := unmergedChains.renamedOriginals[ptr]
if !ok {
return true, zeroPtr, nil
}
parentOrig = ri.originalOldParent
newName = ri.oldName
}
parentMostRecent, err :=
mergedChains.mostRecentFromOriginalOrSame(parentOrig)
if err != nil {
return false, zeroPtr, err
}
cuea.fromName = newName
return true, parentMostRecent, nil
}
func uniquifyName(block *DirBlock, name string) (string, error) {
if _, ok := block.Children[name]; !ok {
return name, nil
}
base, ext := splitExtension(name)
for i := 1; i <= 100; i++ {
newName := fmt.Sprintf("%s (%d)%s", base, i, ext)
if _, ok := block.Children[newName]; !ok {
return newName, nil
}
}
return "", fmt.Errorf("Couldn't find a unique name for %s", name)
}
func (cuea *copyUnmergedEntryAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
// Find the unmerged entry
unmergedEntry, ok := unmergedBlock.Children[cuea.fromName]
if !ok {
return NoSuchNameError{cuea.fromName}
}
if cuea.symPath != "" {
unmergedEntry.Type = Sym
unmergedEntry.SymPath = cuea.symPath
}
// Make sure this entry is unique.
if cuea.unique {
newName, err := uniquifyName(mergedBlock, cuea.toName)
if err != nil {
return err
}
cuea.toName = newName
}
if cuea.sizeOnly {
if entry, ok := mergedBlock.Children[cuea.toName]; ok {
entry.Size = unmergedEntry.Size
entry.EncodedSize = unmergedEntry.EncodedSize
entry.BlockPointer = unmergedEntry.BlockPointer
mergedBlock.Children[cuea.toName] = entry
return nil
}
// copy any attrs that were explicitly set on the unmerged
// branch.
for _, a := range cuea.attr {
switch a {
case exAttr:
unmergedEntry.Type = cuea.unmergedEntry.Type
case mtimeAttr:
unmergedEntry.Mtime = cuea.unmergedEntry.Mtime
}
}
}
mergedBlock.Children[cuea.toName] = unmergedEntry
return nil
}
func prependOpsToChain(mostRecent BlockPointer, chains *crChains,
newOps ...op) error {
chain := chains.byMostRecent[mostRecent]
// Create the chain if it doesn't exist yet.
if chain == nil && len(newOps) > 0 {
err := chains.makeChainForNewOp(mostRecent, newOps[0])
if err != nil {
return err
}
chain = chains.byMostRecent[mostRecent]
chain.ops = nil // will prepend it below
}
// prepend it
chain.ops = append(newOps, chain.ops...)
return nil
}
func crActionConvertSymlink(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedChain *crChain,
mergedChains *crChains, fromName string, toName string) error {
co, err := newCreateOp(toName, mergedMostRecent, Sym)
if err != nil {
return err
}
// If the chain already exists, just append the operation instead
// of prepending them. If there was something else at that
// location in the unmerged branch, it should be moved out of the
// way first by a `renameOp` (see
// `makeLocalRenameOpForCopyAction()`).
chain, ok := mergedChains.byMostRecent[mergedMostRecent]
if ok {
chain.ops = append(chain.ops, co)
} else {
err := prependOpsToChain(mergedMostRecent, mergedChains, co)
if err != nil {
return err
}
}
return nil
}
// trackSyncPtrChangesInCreate makes sure the correct set of refs and
// unrefs, from the syncOps on the unmerged branch, makes it into the
// createOp for a new file.
func trackSyncPtrChangesInCreate(
mostRecentTargetPtr BlockPointer, unmergedChain *crChain,
unmergedChains *crChains, toName string) {
targetChain, ok := unmergedChains.byMostRecent[mostRecentTargetPtr]
var refs, unrefs []BlockPointer
if ok && targetChain.isFile() {
// The create op also needs to reference the child block ptrs
// created by any sync ops (and not unreferenced by future
// ones).
for _, op := range targetChain.ops {
syncOp, ok := op.(*syncOp)
if !ok {
continue
}
for _, ref := range op.Refs() {
if !unmergedChains.isDeleted(ref) {
refs = append(refs, ref)
}
}
for _, unref := range op.Unrefs() {
unrefs = append(unrefs, unref)
}
// Account for the file ptr too, if it's the most recent.
filePtr := syncOp.File.Ref
_, isMostRecent := unmergedChains.byMostRecent[filePtr]
if isMostRecent && !unmergedChains.isDeleted(filePtr) {
refs = append(refs, filePtr)
}
}
}
if len(refs) > 0 {
for _, uop := range unmergedChain.ops {
cop, ok := uop.(*createOp)
if !ok || cop.NewName != toName {
continue
}
for _, ref := range refs {
cop.AddRefBlock(ref)
}
for _, unref := range unrefs {
cop.AddUnrefBlock(unref)
}
break
}
}
}
func (cuea *copyUnmergedEntryAction) trackSyncPtrChangesInCreate(
mostRecentTargetPtr BlockPointer, unmergedChain *crChain,
unmergedChains *crChains) {
trackSyncPtrChangesInCreate(
mostRecentTargetPtr, unmergedChain, unmergedChains, cuea.toName)
}
func makeLocalRenameOpForCopyAction(
mergedMostRecent BlockPointer, mergedBlock *DirBlock,
mergedChains *crChains, fromName, toName string) error {
newMergedEntry, ok := mergedBlock.Children[toName]
if !ok {
return NoSuchNameError{toName}
}
rop, err := newRenameOp(fromName, mergedMostRecent, toName,
mergedMostRecent, newMergedEntry.BlockPointer,
newMergedEntry.Type)
if err != nil {
return err
}
err = prependOpsToChain(mergedMostRecent, mergedChains, rop)
if err != nil {
return err
}
return nil
}
func (cuea *copyUnmergedEntryAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
unmergedChain, ok := unmergedChains.byMostRecent[unmergedMostRecent]
if !ok {
return fmt.Errorf("Couldn't find unmerged chain for %v",
unmergedMostRecent)
}
if cuea.symPath != "" && !unmergedChain.isFile() {
err := crActionConvertSymlink(unmergedMostRecent, mergedMostRecent,
unmergedChain, mergedChains, cuea.fromName, cuea.toName)
if err != nil {
return err
}
}
// If the name changed, we have to update all the unmerged ops
// with the new name.
// The merged ops don't change, though later we may have to
// manipulate the block pointers in the original ops.
if cuea.fromName != cuea.toName {
unmergedChain.ops =
fixupNamesInOps(cuea.fromName, cuea.toName, unmergedChain.ops,
unmergedChains)
if cuea.unique || cuea.symPath != "" {
// If a directory was renamed locally, either because of a
// direct conflict or because it was turned into a
// symlink, we need to fake a merged rename op from the
// unmerged name to the merged name, before creating the
// symlink, so the local Node objects are updated
// correctly.
makeLocalRenameOpForCopyAction(mergedMostRecent, mergedBlock,
mergedChains, cuea.fromName, cuea.toName)
}
}
// If the target is a file that had child blocks, we need to
// transfer those references over to the createOp.
mergedEntry, ok := mergedBlock.Children[cuea.toName]
if !ok {
return fmt.Errorf("Couldn't find merged entry for %s", cuea.toName)
}
mostRecentTargetPtr := mergedEntry.BlockPointer
cuea.trackSyncPtrChangesInCreate(mostRecentTargetPtr, unmergedChain,
unmergedChains)
return nil
}
func (cuea *copyUnmergedEntryAction) String() string {
return fmt.Sprintf("copyUnmergedEntry: %s -> %s %s",
cuea.fromName, cuea.toName, cuea.symPath)
}
// copyUnmergedAttrAction says that the given attributes in the
// unmerged entry for the given name should be copied directly into
// the merged version of the directory; there should be no conflict.
type copyUnmergedAttrAction struct {
fromName string
toName string
attr []attrChange
moved bool // move this action to the parent at most one time
}
func (cuaa *copyUnmergedAttrAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
return false, zeroPtr, nil
}
func (cuaa *copyUnmergedAttrAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
// Find the unmerged entry
unmergedEntry, ok := unmergedBlock.Children[cuaa.fromName]
if !ok {
return NoSuchNameError{cuaa.fromName}
}
mergedEntry, ok := mergedBlock.Children[cuaa.toName]
if !ok {
return NoSuchNameError{cuaa.toName}
}
for _, attr := range cuaa.attr {
switch attr {
case exAttr:
mergedEntry.Type = unmergedEntry.Type
case mtimeAttr:
mergedEntry.Mtime = unmergedEntry.Mtime
case sizeAttr:
mergedEntry.Size = unmergedEntry.Size
mergedEntry.EncodedSize = unmergedEntry.EncodedSize
mergedEntry.BlockPointer = unmergedEntry.BlockPointer
}
}
mergedBlock.Children[cuaa.toName] = mergedEntry
return nil
}
func (cuaa *copyUnmergedAttrAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
unmergedChain, ok := unmergedChains.byMostRecent[unmergedMostRecent]
if !ok {
return fmt.Errorf("Couldn't find unmerged chain for %v",
unmergedMostRecent)
}
// If the name changed, we have to update all the unmerged ops
// with the new name.
// The merged ops don't change, though later we may have to
// manipulate the block pointers in the original ops.
if cuaa.fromName != cuaa.toName {
unmergedChain.ops =
fixupNamesInOps(cuaa.fromName, cuaa.toName, unmergedChain.ops,
unmergedChains)
}
return nil
}
func (cuaa *copyUnmergedAttrAction) String() string {
return fmt.Sprintf("copyUnmergedAttr: %s -> %s (%s)",
cuaa.fromName, cuaa.toName, cuaa.attr)
}
// rmMergedEntryAction says that the merged entry for the given name
// should be deleted.
type rmMergedEntryAction struct {
name string
}
func (rmea *rmMergedEntryAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
return false, zeroPtr, nil
}
func (rmea *rmMergedEntryAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
delete(mergedBlock.Children, rmea.name)
return nil
}
func (rmea *rmMergedEntryAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
return nil
}
func (rmea *rmMergedEntryAction) String() string {
return fmt.Sprintf("rmMergedEntry: %s", rmea.name)
}
// renameUnmergedAction says that the unmerged copy of a file needs to
// be renamed, and the file blocks should be copied.
type renameUnmergedAction struct {
fromName string
toName string
symPath string
causedByAttr attrChange // was this rename caused by a setAttr?
moved bool // move this action to the parent at most one time
// Set if this conflict is between file writes, and the parent
// chains need to be updated with new create/rename operations.
unmergedParentMostRecent BlockPointer
mergedParentMostRecent BlockPointer
}
func crActionCopyFile(ctx context.Context, copier fileBlockDeepCopier,
fromName string, toName string, toSymPath string,
fromBlock *DirBlock, toBlock *DirBlock) (
BlockPointer, string, error) {
// Find the source entry.
fromEntry, ok := fromBlock.Children[fromName]
if !ok {
return BlockPointer{}, "", NoSuchNameError{fromName}
}
if toSymPath != "" {
fromEntry.Type = Sym
fromEntry.SymPath = toSymPath
}
// We only rename files (or make symlinks to directories).
if fromEntry.Type == Dir {
// Just fill in the last path node, we don't have the full path.
return BlockPointer{}, "", NotFileError{path{path: []pathNode{{
BlockPointer: fromEntry.BlockPointer,
Name: fromName,
}}}}
}
// Make sure the name is unique.
name, err := uniquifyName(toBlock, toName)
if err != nil {
return BlockPointer{}, "", err
}
var ptr BlockPointer
if toSymPath == "" && fromEntry.BlockPointer.IsInitialized() {
// Fetch the top block for copyable files.
var err error
ptr, err = copier(ctx, name, fromEntry.BlockPointer)
if err != nil {
return BlockPointer{}, "", err
}
}
// Set the entry with the new pointer.
oldPointer := fromEntry.BlockPointer
fromEntry.BlockPointer = ptr
toBlock.Children[name] = fromEntry
return oldPointer, name, nil
}
func (rua *renameUnmergedAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
return false, zeroPtr, nil
}
func (rua *renameUnmergedAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
_, name, err := crActionCopyFile(ctx, unmergedCopier, rua.fromName,
rua.toName, rua.symPath, unmergedBlock, mergedBlock)
if err != nil {
return err
}
rua.toName = name
return nil
}
func (rua *renameUnmergedAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
unmergedChain, ok := unmergedChains.byMostRecent[unmergedMostRecent]
if !ok {
return fmt.Errorf("Couldn't find unmerged chain for %v",
unmergedMostRecent)
}
if rua.symPath != "" && !unmergedChain.isFile() {
err := crActionConvertSymlink(unmergedMostRecent, mergedMostRecent,
unmergedChain, mergedChains, rua.fromName, rua.toName)
if err != nil {
return err
}
}
// Rename all operations with the old name to the new name.
unmergedChain.ops =
fixupNamesInOps(rua.fromName, rua.toName, unmergedChain.ops,
unmergedChains)
// The newly renamed entry:
newMergedEntry, ok := mergedBlock.Children[rua.toName]
if !ok {
return NoSuchNameError{rua.toName}
}
if unmergedChain.isFile() {
// Replace the updates on all file operations.
for _, op := range unmergedChain.ops {
switch realOp := op.(type) {
case *syncOp:
var err error
realOp.File, err = makeBlockUpdate(
newMergedEntry.BlockPointer,
newMergedEntry.BlockPointer)
if err != nil {
return err
}
// Nuke the previously referenced blocks, they are no
// longer relevant.
realOp.RefBlocks = nil
case *setAttrOp:
realOp.File = newMergedEntry.BlockPointer
}
}
if !rua.unmergedParentMostRecent.IsInitialized() {
// This is not a file-file conflict.
return nil
}
unmergedChain, ok =
unmergedChains.byMostRecent[rua.unmergedParentMostRecent]
if !ok {
// Couldn't find the parent to update. Sigh.
return fmt.Errorf("Couldn't find parent %v to update "+
"renameUnmergedAction for file %v (%s)",
rua.unmergedParentMostRecent, unmergedMostRecent, rua.toName)
}
unmergedMostRecent = rua.unmergedParentMostRecent
mergedMostRecent = rua.mergedParentMostRecent
}
// Prepend a rename for the unmerged copy to the merged set of
// operations, with another create for the merged file, for local
// playback.
// The entry that got renamed in the unmerged branch:
unmergedEntry, ok := unmergedBlock.Children[rua.fromName]
if !ok {
return NoSuchNameError{rua.fromName}
}
// The entry that gets created in the unmerged branch:
mergedEntry, ok := mergedBlock.Children[rua.fromName]
if !ok {
return NoSuchNameError{rua.fromName}
}
rop, err := newRenameOp(rua.fromName, mergedMostRecent, rua.toName,
mergedMostRecent, newMergedEntry.BlockPointer,
newMergedEntry.Type)
if err != nil {
return err
}
// For local notifications, we need to transform the entry's
// pointer into the new (de-dup'd) pointer. newMergedEntry is
// not yet the final pointer (that happens during syncBlock),
// but a later stage will convert it.
if rua.symPath == "" {
rop.AddUpdate(unmergedEntry.BlockPointer,
newMergedEntry.BlockPointer)
}
co, err := newCreateOp(rua.fromName, mergedMostRecent, mergedEntry.Type)
if err != nil {
return err
}
err = prependOpsToChain(mergedMostRecent, mergedChains, rop, co)
if err != nil {
return err
}
// Before merging the unmerged ops, create a file with the new
// name, unless the create already exists.
found := false
co = nil
for _, op := range unmergedChain.ops {
var ok bool
if co, ok = op.(*createOp); ok && co.NewName == rua.toName {
found = true
if len(co.RefBlocks) > 0 {
co.RefBlocks[0] = newMergedEntry.BlockPointer
}
break
}
}
if !found {
co, err = newCreateOp(rua.toName, unmergedMostRecent, mergedEntry.Type)
if err != nil {
return err
}
if rua.symPath == "" {
co.AddRefBlock(newMergedEntry.BlockPointer)
}
err = prependOpsToChain(unmergedMostRecent, unmergedChains, co)
if err != nil {
return err
}
}
// Since we copied the node, unref the old block but only if
// it's not a symlink and the name changed. If the name is
// the same, it means the old block pointer is still in use
// because we just did a copy of a node still in use in the
// merged branch.
if unmergedEntry.BlockPointer != newMergedEntry.BlockPointer &&
rua.fromName != rua.toName && rua.symPath == "" {
co.AddUnrefBlock(unmergedEntry.BlockPointer)
}
return nil
}
func (rua *renameUnmergedAction) String() string {
return fmt.Sprintf("renameUnmerged: %s -> %s %s", rua.fromName, rua.toName,
rua.symPath)
}
// renameMergedAction says that the merged copy of a file needs to be
// renamed, and the unmerged entry should be added to the merged block
// under the old from name. Merged file blocks do not have to be
// copied, because renaming a merged file can only happen when the
// conflict is with a non-file in the unmerged branch; thus, there can
// be no shared blocks between the two.
//
// Note that symPath below refers to the unmerged entry that is being
// copied into the merged block.
type renameMergedAction struct {
fromName string
toName string
symPath string
}
func (rma *renameMergedAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
return false, zeroPtr, nil
}
func (rma *renameMergedAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
// Find the merged entry
mergedEntry, ok := mergedBlock.Children[rma.fromName]
if !ok {
return NoSuchNameError{rma.fromName}
}
// Make sure this entry is unique.
newName, err := uniquifyName(mergedBlock, rma.toName)
if err != nil {
return err
}
rma.toName = newName
mergedBlock.Children[rma.toName] = mergedEntry
// Add the unmerged entry as the new "fromName".
unmergedEntry, ok := unmergedBlock.Children[rma.fromName]
if !ok {
return NoSuchNameError{rma.fromName}
}
if rma.symPath != "" {
unmergedEntry.Type = Sym
unmergedEntry.SymPath = rma.symPath
}
mergedBlock.Children[rma.fromName] = unmergedEntry
return nil
}
func (rma *renameMergedAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
unmergedChain, ok := unmergedChains.byMostRecent[unmergedMostRecent]
if !ok {
return fmt.Errorf("Couldn't find unmerged chain for %v",
unmergedMostRecent)
}
if rma.symPath != "" && !unmergedChain.isFile() {
err := crActionConvertSymlink(unmergedMostRecent, mergedMostRecent,
unmergedChain, mergedChains, rma.fromName, rma.fromName)
if err != nil {
return err
}
}
// Rename all operations with the old name to the new name.
mergedChain := mergedChains.byMostRecent[mergedMostRecent]
if mergedChain != nil {
mergedChain.ops =
fixupNamesInOps(rma.fromName, rma.toName, mergedChain.ops,
mergedChains)
}
if !unmergedChain.isFile() {
// The entry that gets renamed in the unmerged branch:
mergedEntry, ok := mergedBlock.Children[rma.toName]
if !ok {
return NoSuchNameError{rma.toName}
}
// Prepend a rename for the merged copy to the unmerged set of
// operations.
rop, err := newRenameOp(rma.fromName, unmergedMostRecent, rma.toName,
unmergedMostRecent, mergedEntry.BlockPointer, mergedEntry.Type)
if err != nil {
return err
}
err = prependOpsToChain(unmergedMostRecent, unmergedChains, rop)
if err != nil {
return err
}
// Before playing back the merged ops, create a file with the
// new name, unless the create already exists.
found := false
if mergedChain != nil {
for _, op := range mergedChain.ops {
if co, ok := op.(*createOp); ok && co.NewName == rma.toName {
found = true
break
}
}
}
if !found {
co, err := newCreateOp(rma.toName, mergedMostRecent, mergedEntry.Type)
if err != nil {
return err
}
err = prependOpsToChain(mergedMostRecent, mergedChains, co)
if err != nil {
return err
}
}
}
return nil
}
func (rma *renameMergedAction) String() string {
return fmt.Sprintf("renameMerged: %s -> %s", rma.fromName, rma.toName)
}
// dropUnmergedAction says that the corresponding unmerged
// operation should be dropped.
type dropUnmergedAction struct {
op op
}
func (dua *dropUnmergedAction) swapUnmergedBlock(
unmergedChains *crChains, mergedChains *crChains,
unmergedBlock *DirBlock) (bool, BlockPointer, error) {
return false, zeroPtr, nil
}
func (dua *dropUnmergedAction) do(ctx context.Context,
unmergedCopier fileBlockDeepCopier, mergedCopier fileBlockDeepCopier,
unmergedBlock *DirBlock, mergedBlock *DirBlock) error {
return nil
}
func (dua *dropUnmergedAction) updateOps(unmergedMostRecent BlockPointer,
mergedMostRecent BlockPointer, unmergedBlock *DirBlock,
mergedBlock *DirBlock, unmergedChains *crChains,
mergedChains *crChains) error {
unmergedChain, ok := unmergedChains.byMostRecent[unmergedMostRecent]
if !ok {
return fmt.Errorf("Couldn't find unmerged chain for %v",
unmergedMostRecent)
}
found := false
for i, op := range unmergedChain.ops {
if op == dua.op {
unmergedChain.ops =
append(unmergedChain.ops[:i], unmergedChain.ops[i+1:]...)
found = true
break
}
}
// Return early if this chain didn't contain the op; no need to
// invert on the merged chain in that case.
if !found {
return nil
}
invertedOp, err := invertOpForLocalNotifications(dua.op)
if err != nil {
return err
}
err = prependOpsToChain(mergedMostRecent, mergedChains, invertedOp)
if err != nil {
return err
}
return nil
}
func (dua *dropUnmergedAction) String() string {
return fmt.Sprintf("dropUnmerged: %s", dua.op)
}
type collapseActionInfo struct {
topAction crAction
topActionIndex int
}
type crActionList []crAction
func setTopAction(action crAction, fromName string, index int,
infoMap map[string]collapseActionInfo, indicesToRemove map[int]bool) {
info, ok := infoMap[fromName]
if ok {
indicesToRemove[info.topActionIndex] = true
}
info.topAction = action
info.topActionIndex = index
infoMap[fromName] = info
}
// collapse drops any actions that are made irrelevant by other
// actions in the list. It assumes that file-related actions have
// already been merged into their parent directory action lists.
func (cal crActionList) collapse() crActionList {
// Order of precedence for a given fromName:
// 1) renameUnmergedAction
// 2) copyUnmergedEntryAction
// 3) copyUnmergedAttrAction
infoMap := make(map[string]collapseActionInfo) // fromName -> info
indicesToRemove := make(map[int]bool)
for i, untypedAction := range cal {
switch action := untypedAction.(type) {
// Unmerged actions:
case *renameUnmergedAction:
setTopAction(action, action.fromName, i, infoMap, indicesToRemove)
case *copyUnmergedEntryAction:
untypedTopAction := infoMap[action.fromName].topAction
switch untypedTopAction.(type) {
case *renameUnmergedAction:
indicesToRemove[i] = true
default:
setTopAction(action, action.fromName, i, infoMap,
indicesToRemove)
}
case *copyUnmergedAttrAction:
untypedTopAction := infoMap[action.fromName].topAction
switch topAction := untypedTopAction.(type) {
case *renameUnmergedAction:
indicesToRemove[i] = true
case *copyUnmergedEntryAction:
indicesToRemove[i] = true
case *copyUnmergedAttrAction:
// Add attributes to the current top action, if not
// already there.
for _, a := range action.attr {
found := false
for _, topA := range topAction.attr {
if a == topA {
found = true
break
}
}
if !found {
topAction.attr = append(topAction.attr, a)
}
}
indicesToRemove[i] = true
default:
setTopAction(action, action.fromName, i, infoMap,
indicesToRemove)
}
// Merged actions
case *renameMergedAction:
// Prefix merged actions with a reserved prefix to keep
// them separate from the unmerged actions.
setTopAction(action, ".kbfs_merged_"+action.fromName, i, infoMap,
indicesToRemove)
}
}
if len(indicesToRemove) == 0 {
return cal
}
newList := make(crActionList, 0, len(cal)-len(indicesToRemove))
for i, action := range cal {
if indicesToRemove[i] {
continue
}
newList = append(newList, action)
}
return newList
}