/
commandhandlerscollectionmutations.go
230 lines (184 loc) · 5.68 KB
/
commandhandlerscollectionmutations.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
package stoserver
import (
"errors"
"fmt"
"sort"
"time"
"github.com/function61/eventkit/command"
"github.com/function61/varasto/pkg/stateresolver"
"github.com/function61/varasto/pkg/stoserver/stodb"
"github.com/function61/varasto/pkg/stoserver/stoservertypes"
"github.com/function61/varasto/pkg/stotypes"
"github.com/function61/varasto/pkg/stoutils"
"go.etcd.io/bbolt"
)
func (c *cHandlers) CollectionMoveFilesIntoAnotherCollection(cmd *stoservertypes.CollectionMoveFilesIntoAnotherCollection, ctx *command.Ctx) error {
filesToMove := *cmd.Files
if len(filesToMove) == 0 {
return nil // no-op
}
if err := noDuplicates(filesToMove); err != nil {
return err
}
return c.db.Update(func(tx *bbolt.Tx) error {
collSrc, err := stodb.Read(tx).Collection(cmd.Source)
if err != nil {
return err
}
collDst, err := stodb.Read(tx).Collection(cmd.Destination)
if err != nil {
return err
}
if cmd.Source == cmd.Destination {
return errors.New("Source and destination cannot be same")
}
sourceState, err := stateresolver.ComputeStateAtHead(*collSrc)
if err != nil {
return err
}
deleteFromSource := []string{}
createToDestination := []stotypes.File{}
filesInSource := sourceState.Files()
for _, filePathToMove := range filesToMove {
fileToMove, found := filesInSource[filePathToMove]
if !found {
return fmt.Errorf("File to move not found: %s", filePathToMove)
}
createToDestination = append(createToDestination, fileToMove)
deleteFromSource = append(deleteFromSource, fileToMove.Path)
for _, refSerialized := range fileToMove.BlobRefs {
ref, err := stotypes.BlobRefFromHex(refSerialized)
if err != nil {
return err
}
blob, err := stodb.Read(tx).Blob(*ref)
if err != nil {
return err
}
// if destination collection doesn't have encryption key for this blob,
// copy it over
if stotypes.FindDekEnvelope(blob.EncryptionKeyId, collDst.EncryptionKeys) == nil {
dekEnvelope, err := copyAndReEncryptDekFromAnotherCollection(
blob.EncryptionKeyId,
extractKekPubKeyFingerprints(collSrc),
tx,
c.conf.KeyStore)
if err != nil {
return fmt.Errorf("copyAndReEncryptDekFromAnotherCollection: %v", err)
}
collDst.EncryptionKeys = append(collDst.EncryptionKeys, *dekEnvelope)
}
}
}
srcChangeset := stotypes.NewChangeset(
stoutils.NewCollectionChangesetId(),
collSrc.Head,
ctx.Meta.Timestamp,
nil,
nil,
deleteFromSource)
// duplicate filenames are asserted by appendAndValidateChangeset()
dstChangeset := stotypes.NewChangeset(
stoutils.NewCollectionChangesetId(),
collDst.Head,
ctx.Meta.Timestamp,
createToDestination,
nil,
nil)
if err := appendAndValidateChangeset(srcChangeset, collSrc); err != nil {
return err
}
if err := appendAndValidateChangeset(dstChangeset, collDst); err != nil {
return err
}
if err := stodb.CollectionRepository.Update(collSrc, tx); err != nil {
return err
}
if err := stodb.CollectionRepository.Update(collDst, tx); err != nil {
return err
}
return nil
})
}
func (c *cHandlers) CollectionDeleteFiles(cmd *stoservertypes.CollectionDeleteFiles, ctx *command.Ctx) error {
filesToDelete := *cmd.Files
if len(filesToDelete) == 0 {
return nil // no-op
}
if err := noDuplicates(filesToDelete); err != nil {
return err
}
return c.db.Update(func(tx *bbolt.Tx) error {
coll, err := stodb.Read(tx).Collection(cmd.Source)
if err != nil {
return err
}
stateForValidation, err := stateresolver.ComputeStateAtHead(*coll)
if err != nil {
return err
}
existingFiles := stateForValidation.Files()
for _, fileToDelete := range filesToDelete {
if _, has := existingFiles[fileToDelete]; !has {
return fmt.Errorf("file to delete does not exist: %s", fileToDelete)
}
}
changeset := stotypes.NewChangeset(
stoutils.NewCollectionChangesetId(),
coll.Head,
ctx.Meta.Timestamp,
nil,
nil,
filesToDelete)
if err := appendAndValidateChangeset(changeset, coll); err != nil {
return err
}
return stodb.CollectionRepository.Update(coll, tx)
})
}
func appendAndValidateChangeset(changeset stotypes.CollectionChangeset, coll *stotypes.Collection) error {
currentHeadState, err := stateresolver.ComputeStateAtHead(*coll)
if err != nil {
return err
}
filesAtCurrentHead := currentHeadState.Files()
for _, file := range changeset.FilesCreated {
coll.Created = minDate(coll.Created, file.Created)
coll.Created = minDate(coll.Created, file.Modified)
if _, exists := filesAtCurrentHead[file.Path]; exists {
return fmt.Errorf("cannot create file %s because it already exists at revision %s", file.Path, coll.Head)
}
}
for _, file := range changeset.FilesUpdated {
coll.Created = minDate(coll.Created, file.Created)
coll.Created = minDate(coll.Created, file.Modified)
if _, exists := filesAtCurrentHead[file.Path]; !exists {
return fmt.Errorf("cannot update file %s because it does not exist at revision %s", file.Path, coll.Head)
}
}
for _, file := range changeset.FilesDeleted {
if _, exists := filesAtCurrentHead[file]; !exists {
return fmt.Errorf("cannot delete file %s because it does not exist at revision %s", file, coll.Head)
}
}
coll.Changesets = append(coll.Changesets, changeset)
coll.Head = changeset.ID
coll.BumpGlobalVersion()
return nil
}
func minDate(a, b time.Time) time.Time {
if a.Before(b) {
return a
}
return b
}
func noDuplicates(items []string) error {
// once sorted, we can just assert that current item is not same as previous item
sort.Strings(items)
for i := 1; i < len(items); i++ {
if items[i-1] == items[i] {
return fmt.Errorf("duplicate item in list: %s", items[i])
}
}
return nil
}