-
Notifications
You must be signed in to change notification settings - Fork 137
/
files.go
1420 lines (1348 loc) · 40.3 KB
/
files.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
992
993
994
995
996
997
998
999
1000
package sharing
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/cozy/cozy-stack/client/request"
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/note"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/crypto"
"github.com/cozy/cozy-stack/pkg/metadata"
multierror "github.com/hashicorp/go-multierror"
)
// isTrashed returns true for a file or folder inside the trash
func isTrashed(doc couchdb.JSONDoc) bool {
if doc.Type != consts.Files {
return false
}
if doc.Get("type") == consts.FileType {
return doc.Get("trashed") == true
}
return strings.HasPrefix(doc.Get("path").(string), vfs.TrashDirName+"/")
}
// MakeXorKey generates a key for transforming the file identifiers
func MakeXorKey() []byte {
random := crypto.GenerateRandomBytes(8)
result := make([]byte, 2*len(random))
for i, val := range random {
result[2*i] = val & 0xf
result[2*i+1] = val >> 4
}
return result
}
// XorID transforms the identifier of a file to a new identifier, in a
// reversible way: it makes a XOR on the hexadecimal characters
func XorID(id string, key []byte) string {
l := len(key)
buf := []byte(id)
for i, c := range buf {
switch {
case '0' <= c && c <= '9':
c = (c - '0') ^ key[i%l]
case 'a' <= c && c <= 'f':
c = (c - 'a' + 10) ^ key[i%l]
case 'A' <= c && c <= 'F':
c = (c - 'A' + 10) ^ key[i%l]
default:
continue
}
if c < 10 {
buf[i] = c + '0'
} else {
buf[i] = (c - 10) + 'a'
}
}
return string(buf)
}
// SortFilesToSent sorts the files slice that will be sent in bulk_docs:
// - directories must come before files (if a file is created in a new
// directory, we must create directory before the file)
// - directories are sorted by increasing depth (if a sub-folder is created
// in a new directory, we must create the parent before the child)
// - deleted elements must come at the end, to efficiently cope with moves.
// For example, if we have A->B->C hierarchy and C is moved elsewhere
// and B deleted, we must make the move before deleting B and its children.
func (s *Sharing) SortFilesToSent(files []map[string]interface{}) {
sort.SliceStable(files, func(i, j int) bool {
a, b := files[i], files[j]
if removed, ok := a["_deleted"].(bool); ok && removed {
return false
}
if removed, ok := b["_deleted"].(bool); ok && removed {
return true
}
if a["type"] == consts.FileType {
return false
}
if b["type"] == consts.FileType {
return true
}
q, ok := b["path"].(string)
if !ok {
return false
}
p, ok := a["path"].(string)
if !ok {
return true
}
return strings.Count(p, "/") < strings.Count(q, "/")
})
}
// TransformFileToSent transforms an io.cozy.files document before sending it
// to another cozy instance:
// - its identifier is XORed
// - its dir_id is XORed or removed
// - the referenced_by are XORed or removed
// - the path is removed (directory only)
//
// ruleIndexes is a map of "doctype-docid" -> rule index
func (s *Sharing) TransformFileToSent(doc map[string]interface{}, xorKey []byte, ruleIndex int) {
if doc["type"] == consts.DirType {
delete(doc, "path")
delete(doc, "not_synchronized_on")
}
id := doc["_id"].(string)
doc["_id"] = XorID(id, xorKey)
dir, ok := doc["dir_id"].(string)
if !ok {
return
}
rule := s.Rules[ruleIndex]
var noDirID bool
if rule.Selector == couchdb.SelectorReferencedBy {
noDirID = true
if refs, ok := doc[couchdb.SelectorReferencedBy].([]interface{}); ok {
kept := make([]interface{}, 0)
for _, ref := range refs {
if r, ok := ref.(map[string]interface{}); ok {
v := r["type"].(string) + "/" + r["id"].(string)
for _, val := range rule.Values {
if val == v {
r["id"] = XorID(r["id"].(string), xorKey)
kept = append(kept, r)
break
}
}
}
}
doc[couchdb.SelectorReferencedBy] = kept
}
} else {
for _, v := range rule.Values {
if v == id {
noDirID = true
}
}
delete(doc, couchdb.SelectorReferencedBy)
}
if !noDirID {
for _, v := range rule.Values {
if v == dir {
noDirID = true
break
}
}
}
if noDirID {
delete(doc, "dir_id")
} else {
doc["dir_id"] = XorID(dir, xorKey)
}
}
// EnsureSharedWithMeDir returns the shared-with-me directory, and create it if
// it doesn't exist
func EnsureSharedWithMeDir(inst *instance.Instance) (*vfs.DirDoc, error) {
fs := inst.VFS()
dir, _, err := fs.DirOrFileByID(consts.SharedWithMeDirID)
if err != nil && !errors.Is(err, os.ErrNotExist) {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to find the dir: %s", err)
return nil, err
}
if dir == nil {
name := inst.Translate("Tree Shared with me")
dir, err = vfs.NewDirDocWithPath(name, consts.RootDirID, "/", nil)
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to make the dir: %s", err)
return nil, err
}
dir.DocID = consts.SharedWithMeDirID
dir.CozyMetadata = vfs.NewCozyMetadata(inst.PageURL("/", nil))
err = fs.CreateDir(dir)
if errors.Is(err, os.ErrExist) {
dir, err = fs.DirByPath(dir.Fullpath)
}
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to create the dir: %s", err)
return nil, err
}
return dir, nil
}
if dir.RestorePath != "" {
now := time.Now()
instanceURL := inst.PageURL("/", nil)
if dir.CozyMetadata == nil {
dir.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
dir.CozyMetadata.UpdatedAt = now
}
_, err = vfs.RestoreDir(fs, dir)
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to restore the dir: %s", err)
return nil, err
}
children, err := fs.DirBatch(dir, couchdb.NewSkipCursor(0, 0))
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to find children: %s", err)
return nil, err
}
for _, child := range children {
d, f := child.Refine()
if d != nil {
if d.CozyMetadata == nil {
d.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
d.CozyMetadata.UpdatedAt = now
}
_, err = vfs.TrashDir(fs, d)
} else {
if f.CozyMetadata == nil {
f.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
f.CozyMetadata.UpdatedAt = now
}
_, err = vfs.TrashFile(fs, f)
}
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("EnsureSharedWithMeDir failed to trash children: %s", err)
return nil, err
}
}
}
return dir, nil
}
// CreateDirForSharing creates the directory where files for this sharing will
// be put. This directory will be initially inside the Shared with me folder.
func (s *Sharing) CreateDirForSharing(inst *instance.Instance, rule *Rule, parentID string) (*vfs.DirDoc, error) {
fs := inst.VFS()
var err error
var parent *vfs.DirDoc
if parentID == "" {
parent, err = EnsureSharedWithMeDir(inst)
} else {
parent, err = fs.DirByID(parentID)
}
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("CreateDirForSharing failed to find parent directory: %s", err)
return nil, err
}
dir, err := vfs.NewDirDocWithParent(rule.Title, parent, []string{"from-sharing-" + s.SID})
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("CreateDirForSharing failed to make dir: %s", err)
return nil, err
}
parts := strings.Split(rule.Values[0], "/")
dir.DocID = parts[len(parts)-1]
dir.AddReferencedBy(couchdb.DocReference{
ID: s.SID,
Type: consts.Sharings,
})
dir.CozyMetadata = vfs.NewCozyMetadata(inst.PageURL("/", nil))
basename := dir.DocName
for i := 2; i < 20; i++ {
if err = fs.CreateDir(dir); err == nil {
return dir, nil
}
if couchdb.IsConflictError(err) || errors.Is(err, os.ErrExist) {
doc, err := fs.DirByID(dir.DocID)
if err == nil {
doc.AddReferencedBy(couchdb.DocReference{
ID: s.SID,
Type: consts.Sharings,
})
_ = couchdb.UpdateDoc(inst, doc)
return doc, nil
}
}
dir.DocName = fmt.Sprintf("%s (%d)", basename, i)
dir.Fullpath = path.Join(parent.Fullpath, dir.DocName)
}
inst.Logger().WithNamespace("sharing").
Errorf("Cannot create the sharing directory: %s", err)
return nil, err
}
// AddReferenceForSharingDir adds a reference to the sharing on the sharing directory
func (s *Sharing) AddReferenceForSharingDir(inst *instance.Instance, rule *Rule) error {
fs := inst.VFS()
parts := strings.Split(rule.Values[0], "/")
dir, _, err := fs.DirOrFileByID(parts[len(parts)-1])
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("AddReferenceForSharingDir failed to find dir: %s", err)
return err
}
if dir == nil {
return nil
}
for _, ref := range dir.ReferencedBy {
if ref.Type == consts.Sharings && ref.ID == s.SID {
return nil
}
}
olddoc := dir.Clone().(*vfs.DirDoc)
dir.AddReferencedBy(couchdb.DocReference{
ID: s.SID,
Type: consts.Sharings,
})
if dir.CozyMetadata == nil {
dir.CozyMetadata = vfs.NewCozyMetadata(inst.PageURL("/", nil))
} else {
dir.CozyMetadata.UpdatedAt = time.Now()
}
return fs.UpdateDirDoc(olddoc, dir)
}
// GetSharingDir returns the directory used by this sharing for putting files
// and folders that have no dir_id.
func (s *Sharing) GetSharingDir(inst *instance.Instance) (*vfs.DirDoc, error) {
// When we can, find the sharing dir by its ID
fs := inst.VFS()
rule := s.FirstFilesRule()
if rule != nil {
if rule.Mime != "" {
inst.Logger().WithNamespace("sharing").
Warnf("GetSharingDir called for only one file: %s", s.SID)
return nil, ErrInternalServerError
}
dir, _ := fs.DirByID(rule.Values[0])
if dir != nil {
return dir, nil
}
}
// Else, try to find it by a reference
key := []string{consts.Sharings, s.SID}
end := []string{key[0], key[1], couchdb.MaxString}
req := &couchdb.ViewRequest{
StartKey: key,
EndKey: end,
IncludeDocs: true,
}
var res couchdb.ViewResponse
err := couchdb.ExecView(inst, couchdb.FilesReferencedByView, req, &res)
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("Sharing dir not found: %v (%s)", err, s.SID)
return nil, ErrInternalServerError
}
var parentID string
if len(res.Rows) > 0 {
dir, file, err := fs.DirOrFileByID(res.Rows[0].ID)
if err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("GetSharingDir failed to find dir: %s", err)
return dir, err
}
if dir != nil {
return dir, nil
}
// file is a shortcut
parentID = file.DirID
if err := fs.DestroyFile(file); err != nil {
inst.Logger().WithNamespace("sharing").
Warnf("GetSharingDir failed to delete shortcut: %s", err)
return nil, err
}
s.ShortcutID = ""
_ = couchdb.UpdateDoc(inst, s)
}
if rule == nil {
inst.Logger().WithNamespace("sharing").
Errorf("no first rule for: %#v", s)
return nil, ErrInternalServerError
}
// And, we may have to create it in last resort
return s.CreateDirForSharing(inst, rule, parentID)
}
// RemoveSharingDir removes the reference on the sharing directory, and adds a
// suffix to its name: the suffix will help make the user understand that the
// sharing has been revoked, and it will avoid conflicts if the user accepts a
// new sharing for the same folder. It should be called when a sharing is
// revoked, on the recipient Cozy.
func (s *Sharing) RemoveSharingDir(inst *instance.Instance) error {
dir, err := s.GetSharingDir(inst)
if couchdb.IsNotFoundError(err) {
return nil
} else if err != nil {
return err
}
olddoc := dir.Clone().(*vfs.DirDoc)
dir.RemoveReferencedBy(couchdb.DocReference{
ID: s.SID,
Type: consts.Sharings,
})
if dir.CozyMetadata == nil {
dir.CozyMetadata = vfs.NewCozyMetadata(inst.PageURL("/", nil))
} else {
dir.CozyMetadata.UpdatedAt = time.Now()
}
suffix := inst.Translate("Tree Revoked sharing suffix")
parentPath := filepath.Dir(dir.Fullpath)
basename := fmt.Sprintf("%s (%s)", dir.DocName, suffix)
dir.DocName = basename
for i := 2; i < 100; i++ {
dir.Fullpath = path.Join(parentPath, dir.DocName)
if err = inst.VFS().UpdateDirDoc(olddoc, dir); err == nil {
return nil
}
dir.DocName = fmt.Sprintf("%s (%d)", basename, i)
}
return err
}
// GetNoLongerSharedDir returns the directory used for files and folders that
// are removed from a sharing, but are still used via a reference. It the
// directory does not exist, it is created.
func (s *Sharing) GetNoLongerSharedDir(inst *instance.Instance) (*vfs.DirDoc, error) {
fs := inst.VFS()
dir, _, err := fs.DirOrFileByID(consts.NoLongerSharedDirID)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if dir == nil {
parent, errp := EnsureSharedWithMeDir(inst)
if errp != nil {
return nil, errp
}
if strings.HasPrefix(parent.Fullpath, vfs.TrashDirName) {
parent, errp = fs.DirByID(consts.RootDirID)
if errp != nil {
return nil, errp
}
}
name := inst.Translate("Tree No longer shared")
dir, err = vfs.NewDirDocWithParent(name, parent, nil)
if err != nil {
return nil, err
}
dir.DocID = consts.NoLongerSharedDirID
dir.CozyMetadata = vfs.NewCozyMetadata(inst.PageURL("/", nil))
if err = fs.CreateDir(dir); err != nil {
return nil, err
}
return dir, nil
}
if dir.RestorePath != "" {
now := time.Now()
instanceURL := inst.PageURL("/", nil)
if dir.CozyMetadata == nil {
dir.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
dir.CozyMetadata.UpdatedAt = now
}
dir.CozyMetadata.UpdatedAt = now
_, err = vfs.RestoreDir(fs, dir)
if err != nil {
return nil, err
}
children, err := fs.DirBatch(dir, couchdb.NewSkipCursor(0, 0))
if err != nil {
return nil, err
}
for _, child := range children {
d, f := child.Refine()
if d != nil {
if d.CozyMetadata == nil {
d.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
d.CozyMetadata.UpdatedAt = now
}
_, err = vfs.TrashDir(fs, d)
} else {
if f.CozyMetadata == nil {
f.CozyMetadata = vfs.NewCozyMetadata(instanceURL)
} else {
f.CozyMetadata.UpdatedAt = now
}
_, err = vfs.TrashFile(fs, f)
}
if err != nil {
return nil, err
}
}
}
return dir, nil
}
// GetFolder returns informations about a folder (with XORed IDs)
func (s *Sharing) GetFolder(inst *instance.Instance, m *Member, xoredID string) (map[string]interface{}, error) {
creds := s.FindCredentials(m)
if creds == nil {
return nil, ErrInvalidSharing
}
dirID := XorID(xoredID, creds.XorKey)
ref := &SharedRef{}
err := couchdb.GetDoc(inst, consts.Shared, consts.Files+"/"+dirID, ref)
if err != nil {
return nil, err
}
info, ok := ref.Infos[s.SID]
if !ok || info.Removed {
return nil, ErrFolderNotFound
}
dir, err := inst.VFS().DirByID(dirID)
if err != nil {
return nil, err
}
doc := dirToJSONDoc(dir, inst.PageURL("/", nil)).M
s.TransformFileToSent(doc, creds.XorKey, info.Rule)
return doc, nil
}
// ApplyBulkFiles takes a list of documents for the io.cozy.files doctype and
// will apply changes to the VFS according to those documents.
func (s *Sharing) ApplyBulkFiles(inst *instance.Instance, docs DocsList) error {
type retryOp struct {
target map[string]interface{}
dir *vfs.DirDoc
ref *SharedRef
}
var errm error
var retries []retryOp
fs := inst.VFS()
for _, target := range docs {
id, ok := target["_id"].(string)
if !ok {
errm = multierror.Append(errm, ErrMissingID)
continue
}
ref := &SharedRef{}
err := couchdb.GetDoc(inst, consts.Shared, consts.Files+"/"+id, ref)
if err != nil {
if !couchdb.IsNotFoundError(err) {
inst.Logger().WithNamespace("replicator").
Debugf("Error on finding doc of bulk files: %s", err)
errm = multierror.Append(errm, err)
continue
}
ref = nil
}
var infos SharedInfo
if ref != nil {
infos, ok = ref.Infos[s.SID]
if !ok {
inst.Logger().WithNamespace("replicator").
Infof("Operation aborted for %s on sharing %s", id, s.SID)
errm = multierror.Append(errm, ErrSafety)
continue
}
}
dir, file, err := fs.DirOrFileByID(id)
if err != nil && !errors.Is(err, os.ErrNotExist) {
inst.Logger().WithNamespace("replicator").
Debugf("Error on finding ref of bulk files: %s", err)
errm = multierror.Append(errm, err)
continue
}
if _, ok := target["_deleted"]; ok {
if ref == nil || infos.Removed {
continue
}
if dir == nil && file == nil {
continue
}
if dir != nil {
err = s.TrashDir(inst, dir)
} else {
err = s.TrashFile(inst, file, &s.Rules[infos.Rule])
}
} else if target["type"] != consts.DirType {
// Let the upload worker manages this file
continue
} else if ref != nil && infos.Removed && !infos.Dissociated {
continue
} else if dir == nil {
err = s.CreateDir(inst, target, delayResolution)
if errors.Is(err, os.ErrExist) {
retries = append(retries, retryOp{
target: target,
})
err = nil
}
} else if ref == nil || infos.Dissociated {
// If it is a file: let the upload worker manages this file
// If it is a dir: ignore this (safety rule)
continue
} else {
// XXX we have to clone the dir document as it is modified by the
// UpdateDir function and retrying the operation won't work with
// the modified doc
cloned := dir.Clone().(*vfs.DirDoc)
err = s.UpdateDir(inst, target, dir, ref, delayResolution)
if errors.Is(err, os.ErrExist) {
retries = append(retries, retryOp{
target: target,
dir: cloned,
ref: ref,
})
err = nil
}
}
if err != nil {
inst.Logger().WithNamespace("replicator").
Debugf("Error on apply bulk file: %s (%#v - %#v)", err, target, ref)
errm = multierror.Append(errm, fmt.Errorf("%s - %w", id, err))
}
}
for _, op := range retries {
var err error
if op.dir == nil {
err = s.CreateDir(inst, op.target, resolveResolution)
} else {
err = s.UpdateDir(inst, op.target, op.dir, op.ref, resolveResolution)
}
if err != nil {
inst.Logger().WithNamespace("replicator").
Debugf("Error on apply bulk file: %s (%#v - %#v)", err, op.target, op.ref)
errm = multierror.Append(errm, err)
}
}
return errm
}
func (s *Sharing) GetNotes(inst *instance.Instance) ([]*vfs.FileDoc, error) {
rule := s.FirstFilesRule()
if rule != nil {
if rule.Mime != "" {
if rule.Mime == consts.NoteMimeType {
var notes []*vfs.FileDoc
req := &couchdb.AllDocsRequest{Keys: rule.Values}
if err := couchdb.GetAllDocs(inst, consts.Files, req, ¬es); err != nil {
return nil, fmt.Errorf("failed to fetch notes shared by themselves: %w", err)
}
return notes, nil
} else {
return nil, nil
}
}
sharingDir, err := s.GetSharingDir(inst)
if err != nil {
return nil, fmt.Errorf("failed to get notes sharing dir: %w", err)
}
var notes []*vfs.FileDoc
fs := inst.VFS()
iter := fs.DirIterator(sharingDir, nil)
for {
_, f, err := iter.Next()
if errors.Is(err, vfs.ErrIteratorDone) {
break
}
if err != nil {
return nil, fmt.Errorf("failed to get next shared note: %w", err)
}
if f != nil && f.Mime == consts.NoteMimeType {
notes = append(notes, f)
}
}
return notes, nil
}
return nil, nil
}
func (s *Sharing) FixRevokedNotes(inst *instance.Instance) error {
docs, err := s.GetNotes(inst)
if err != nil {
return fmt.Errorf("failed to get revoked sharing notes: %w", err)
}
var errm error
for _, doc := range docs {
// If the note came from another cozy via a sharing that is now revoked, we
// may need to recreate the trigger.
if err := note.SetupTrigger(inst, doc.ID()); err != nil {
errm = multierror.Append(errm, fmt.Errorf("failed to setup revoked note trigger: %w", err))
}
if err := note.ImportImages(inst, doc); err != nil {
errm = multierror.Append(errm, fmt.Errorf("failed to import revoked note images: %w", err))
}
}
return errm
}
func removeReferencesFromRule(file *vfs.FileDoc, rule *Rule) {
if rule.Selector != couchdb.SelectorReferencedBy {
return
}
refs := file.ReferencedBy[:0]
for _, ref := range file.ReferencedBy {
if !rule.hasReferencedBy(ref) {
refs = append(refs, ref)
}
}
file.ReferencedBy = refs
}
func buildReferencedBy(target, file *vfs.FileDoc, rule *Rule) []couchdb.DocReference {
refs := make([]couchdb.DocReference, 0)
if file != nil {
for _, ref := range file.ReferencedBy {
if !rule.hasReferencedBy(ref) {
refs = append(refs, ref)
}
}
}
for _, ref := range target.ReferencedBy {
if rule.hasReferencedBy(ref) {
refs = append(refs, ref)
}
}
return refs
}
func copySafeFieldsToFile(target, file *vfs.FileDoc) {
file.Tags = target.Tags
file.Metadata = target.Metadata.RemoveCertifiedMetadata()
file.CreatedAt = target.CreatedAt
file.UpdatedAt = target.UpdatedAt
file.Mime = target.Mime
file.Class = target.Class
file.Executable = target.Executable
file.CozyMetadata = target.CozyMetadata
}
func copySafeFieldsToDir(target map[string]interface{}, dir *vfs.DirDoc) {
if tags, ok := target["tags"].([]interface{}); ok {
dir.Tags = make([]string, 0, len(tags))
for _, tag := range tags {
if t, ok := tag.(string); ok {
dir.Tags = append(dir.Tags, t)
}
}
}
if created, ok := target["created_at"].(string); ok {
if at, err := time.Parse(time.RFC3339Nano, created); err == nil {
dir.CreatedAt = at
}
}
if updated, ok := target["updated_at"].(string); ok {
if at, err := time.Parse(time.RFC3339Nano, updated); err == nil {
dir.UpdatedAt = at
}
}
if meta, ok := target["metadata"].(map[string]interface{}); ok {
dir.Metadata = vfs.Metadata(meta).RemoveCertifiedMetadata()
}
if meta, ok := target["cozyMetadata"].(map[string]interface{}); ok {
dir.CozyMetadata = &vfs.FilesCozyMetadata{}
if version, ok := meta["doctypeVersion"].(string); ok {
dir.CozyMetadata.DocTypeVersion = version
}
if version, ok := meta["metadataVersion"].(float64); ok {
dir.CozyMetadata.MetadataVersion = int(version)
}
if created, ok := meta["createdAt"].(string); ok {
if at, err := time.Parse(time.RFC3339Nano, created); err == nil {
dir.CozyMetadata.CreatedAt = at
}
}
if app, ok := meta["createdByApp"].(string); ok {
dir.CozyMetadata.CreatedByApp = app
}
if version, ok := meta["createdByAppVersion"].(string); ok {
dir.CozyMetadata.CreatedByAppVersion = version
}
if instance, ok := meta["createdOn"].(string); ok {
dir.CozyMetadata.CreatedOn = instance
}
if updated, ok := meta["updatedAt"].(string); ok {
if at, err := time.Parse(time.RFC3339Nano, updated); err == nil {
dir.CozyMetadata.UpdatedAt = at
}
}
if updates, ok := meta["updatedByApps"].([]map[string]interface{}); ok {
for _, update := range updates {
if slug, ok := update["slug"].(string); ok {
entry := &metadata.UpdatedByAppEntry{Slug: slug}
if date, ok := update["date"].(string); ok {
if at, err := time.Parse(time.RFC3339Nano, date); err == nil {
entry.Date = at
}
}
if version, ok := update["version"].(string); ok {
entry.Version = version
}
if instance, ok := update["instance"].(string); ok {
entry.Instance = instance
}
dir.CozyMetadata.UpdatedByApps = append(dir.CozyMetadata.UpdatedByApps, entry)
}
}
}
// No upload* for directories
if account, ok := meta["sourceAccount"].(string); ok {
dir.CozyMetadata.SourceAccount = account
}
if id, ok := meta["sourceAccountIdentifier"].(string); ok {
dir.CozyMetadata.SourceIdentifier = id
}
}
}
// resolveConflictSamePath is used when two files/folders are in conflict
// because they have the same path. To resolve the conflict, we take the
// file/folder from the owner instance as the winner and rename the other.
//
// Note: previously, the rule was that the higher id wins, but the rule has
// been changed. The new rule helps to minimize the number of exchanges needed
// between the cozy instance to converge, and, as such, it helps to avoid
// creating more conflicts.
//
// If the winner is the new file/folder from the other cozy, this function
// rename the local file/folder and let the caller retry its operation.
// If the winner is the local file/folder, this function returns the new name
// and let the caller do its operation with the new name (the caller should
// create a dummy revision to let the other cozy know of the renaming).
func (s *Sharing) resolveConflictSamePath(inst *instance.Instance, visitorID, pth string) (string, error) {
inst.Logger().WithNamespace("replicator").
Infof("Resolve conflict for path=%s (docid=%s)", pth, visitorID)
fs := inst.VFS()
d, f, err := fs.DirOrFileByPath(pth)
if err != nil {
return "", err
}
indexer := vfs.NewCouchdbIndexer(inst)
var dirID string
if d != nil {
dirID = d.DirID
} else {
dirID = f.DirID
}
name := conflictName(indexer, dirID, path.Base(pth), f != nil)
if s.Owner {
return name, nil
}
if d != nil {
old := d.Clone().(*vfs.DirDoc)
d.DocName = name
d.Fullpath = path.Join(path.Dir(d.Fullpath), d.DocName)
return "", fs.UpdateDirDoc(old, d)
}
old := f.Clone().(*vfs.FileDoc)
f.DocName = name
f.ResetFullpath()
return "", fs.UpdateFileDoc(old, f)
}
// getDirDocFromInstance fetches informations about a directory from the given
// member of the sharing.
func (s *Sharing) getDirDocFromInstance(inst *instance.Instance, m *Member, creds *Credentials, dirID string) (*vfs.DirDoc, error) {
if creds == nil || creds.AccessToken == nil {
return nil, ErrInvalidSharing
}
u, err := url.Parse(m.Instance)
if err != nil {
return nil, ErrInvalidSharing
}
opts := &request.Options{
Method: http.MethodGet,
Scheme: u.Scheme,
Domain: u.Host,
Path: "/sharings/" + s.SID + "/io.cozy.files/" + dirID,
Headers: request.Headers{
"Accept": "application/json",
"Authorization": "Bearer " + creds.AccessToken.AccessToken,
},
ParseError: ParseRequestError,
}
res, err := request.Req(opts)
if res != nil && res.StatusCode/100 == 4 {
res, err = RefreshToken(inst, err, s, m, creds, opts, nil)
}
if err != nil {
if res != nil && res.StatusCode/100 == 5 {
return nil, ErrInternalServerError
}
return nil, err
}
defer res.Body.Close()
var doc *vfs.DirDoc
if err = json.NewDecoder(res.Body).Decode(&doc); err != nil {
return nil, err
}
return doc, nil
}
// getDirDocFromNetwork fetches informations about a directory from the other
// cozy instances of this sharing.
func (s *Sharing) getDirDocFromNetwork(inst *instance.Instance, dirID string) (*vfs.DirDoc, error) {
if !s.Owner {
return s.getDirDocFromInstance(inst, &s.Members[0], &s.Credentials[0], dirID)
}
for i := range s.Credentials {
doc, err := s.getDirDocFromInstance(inst, &s.Members[i+1], &s.Credentials[i], dirID)
if err == nil {
return doc, nil
}
}
return nil, ErrFolderNotFound
}
// recreateParent is used when a file or folder is added by a cozy, and sent to
// this instance, but its parent directory was trashed and deleted on this
// cozy. To resolve the conflict, this instance will fetch informations from
// the other instance about the parent directory and will recreate it. It can
// be necessary to recurse if there were several levels of directories deleted.
func (s *Sharing) recreateParent(inst *instance.Instance, dirID string) (*vfs.DirDoc, error) {
inst.Logger().WithNamespace("replicator").
Debugf("Recreate parent dirID=%s", dirID)
doc, err := s.getDirDocFromNetwork(inst, dirID)
if err != nil {
return nil, fmt.Errorf("recreateParent: %w", err)
}
fs := inst.VFS()
var parent *vfs.DirDoc
if doc.DirID == "" {
parent, err = s.GetSharingDir(inst)
} else {
parent, err = fs.DirByID(doc.DirID)
if errors.Is(err, os.ErrNotExist) {
parent, err = s.recreateParent(inst, doc.DirID)
}
}
if err != nil {
return nil, err
}
doc.DirID = parent.DocID
doc.Fullpath = path.Join(parent.Fullpath, doc.DocName)
doc.SetRev("")
err = fs.CreateDir(doc)
if err != nil {
// Maybe the directory has been created concurrently, so let's try
// again to fetch it from the database
if errors.Is(err, os.ErrExist) {
return fs.DirByID(dirID)
}
return nil, fmt.Errorf("recreateParent: %w", err)
}
return doc, nil
}
// extractNameAndIndexer takes a target document, extracts the name and creates
// a sharing indexer with _rev and _revisions
func extractNameAndIndexer(inst *instance.Instance, target map[string]interface{}, ref *SharedRef) (string, *sharingIndexer, error) {
name, ok := target["name"].(string)
if !ok {
inst.Logger().WithNamespace("replicator").
Warnf("Missing name for directory %#v", target)
return "", nil, ErrInternalServerError
}
rev, ok := target["_rev"].(string)
if !ok {
inst.Logger().WithNamespace("replicator").
Warnf("Missing _rev for directory %#v", target)
return "", nil, ErrInternalServerError
}
revs := revsMapToStruct(target["_revisions"])
if revs == nil {
inst.Logger().WithNamespace("replicator").
Warnf("Invalid _revisions for directory %#v", target)
return "", nil, ErrInternalServerError
}
indexer := newSharingIndexer(inst, &bulkRevs{