forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_common.go
285 lines (244 loc) · 7.36 KB
/
md_common.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/kbfs/fsrpc"
"github.com/keybase/kbfs/kbfsmd"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/tlf"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
var mdInputRegexp = regexp.MustCompile("^(.+?)(?::(.*?))?(?:\\^(.*?)(?:-(.*?))?)?$")
func mdSplitInput(input string) (
tlfStr, branchStr, startStr, stopStr string, err error) {
matches := mdInputRegexp.FindStringSubmatch(input)
if matches == nil {
return "", "", "", "", fmt.Errorf("Could not parse %q", input)
}
return matches[1], matches[2], matches[3], matches[4], nil
}
func mdJoinInput(tlfStr, branchStr, startStr, stopStr string) string {
s := tlfStr
if branchStr != "" {
s += ":" + branchStr
}
if startStr != "" || stopStr != "" {
s += "^" + startStr
if stopStr != "" {
s += "-" + stopStr
}
}
return s
}
func mdParseInput(ctx context.Context, config libkbfs.Config,
tlfStr, branchStr, startStr, stopStr string) (
tlfID tlf.ID, branchID kbfsmd.BranchID, start, stop kbfsmd.Revision, err error) {
tlfID, err = getTlfID(ctx, config, tlfStr)
if err != nil {
return tlf.ID{}, kbfsmd.BranchID{}, kbfsmd.RevisionUninitialized,
kbfsmd.RevisionUninitialized, err
}
branchID, err = getBranchID(ctx, config, tlfID, branchStr)
if err != nil {
return tlf.ID{}, kbfsmd.BranchID{}, kbfsmd.RevisionUninitialized,
kbfsmd.RevisionUninitialized, err
}
start, err = getRevision(ctx, config, tlfID, branchID, startStr)
if err != nil {
return tlf.ID{}, kbfsmd.BranchID{}, kbfsmd.RevisionUninitialized,
kbfsmd.RevisionUninitialized, err
}
// TODO: Chunk the range between start and stop.
stop = start
if stopStr != "" {
stop, err = getRevision(ctx, config, tlfID, branchID, stopStr)
if err != nil {
return tlf.ID{}, kbfsmd.BranchID{}, kbfsmd.RevisionUninitialized,
kbfsmd.RevisionUninitialized, err
}
}
return tlfID, branchID, start, stop, nil
}
func parseTLFPath(ctx context.Context, kbpki libkbfs.KBPKI,
mdOps libkbfs.MDOps, tlfStr string) (*libkbfs.TlfHandle, error) {
p, err := fsrpc.NewPath(tlfStr)
if err != nil {
return nil, err
}
if p.PathType != fsrpc.TLFPathType {
return nil, fmt.Errorf("%q is not a TLF path", tlfStr)
}
if len(p.TLFComponents) > 0 {
return nil, fmt.Errorf(
"%q is not the root path of a TLF", tlfStr)
}
return fsrpc.ParseTlfHandle(ctx, kbpki, mdOps, p.TLFName, p.TLFType)
}
func getTlfID(
ctx context.Context, config libkbfs.Config, tlfStr string) (
tlf.ID, error) {
_, err := kbfsmd.ParseID(tlfStr)
if err == nil {
return tlf.ID{}, errors.New("Cannot handle metadata IDs")
}
tlfID, err := tlf.ParseID(tlfStr)
if err == nil {
return tlfID, nil
} else if _, ok := errors.Cause(err).(tlf.InvalidIDError); !ok {
return tlf.ID{}, err
}
handle, err := parseTLFPath(ctx, config.KBPKI(), config.MDOps(), tlfStr)
if err != nil {
return tlf.ID{}, err
}
return config.MDOps().GetIDForHandle(ctx, handle)
}
func getBranchID(ctx context.Context, config libkbfs.Config,
tlfID tlf.ID, branchStr string) (kbfsmd.BranchID, error) {
if branchStr == "master" {
return kbfsmd.NullBranchID, nil
}
if len(branchStr) == 0 || branchStr == "device" {
irmd, err := config.MDOps().GetUnmergedForTLF(
ctx, tlfID, kbfsmd.NullBranchID)
if err != nil {
return kbfsmd.NullBranchID, err
}
if irmd == (libkbfs.ImmutableRootMetadata{}) {
return kbfsmd.NullBranchID, nil
}
return irmd.BID(), nil
}
return kbfsmd.ParseBranchID(branchStr)
}
func getRevision(ctx context.Context, config libkbfs.Config,
tlfID tlf.ID, branchID kbfsmd.BranchID,
revisionStr string) (kbfsmd.Revision, error) {
if len(revisionStr) == 0 || revisionStr == "latest" {
if branchID == kbfsmd.NullBranchID {
irmd, err := config.MDOps().GetForTLF(ctx, tlfID, nil)
if err != nil {
return kbfsmd.RevisionUninitialized, err
}
return irmd.Revision(), nil
}
irmd, err := config.MDOps().GetUnmergedForTLF(
ctx, tlfID, branchID)
if err != nil {
return kbfsmd.RevisionUninitialized, err
}
return irmd.Revision(), nil
}
base := 10
if strings.HasPrefix(revisionStr, "0x") {
base = 16
revisionStr = strings.TrimPrefix(revisionStr, "0x")
}
u, err := strconv.ParseUint(revisionStr, base, 64)
if err != nil {
return kbfsmd.RevisionUninitialized, err
}
return kbfsmd.Revision(u), nil
}
func reverseIRMDList(irmds []libkbfs.ImmutableRootMetadata) []libkbfs.ImmutableRootMetadata {
irmdsReversed := make([]libkbfs.ImmutableRootMetadata, len(irmds))
for i := range irmds {
irmdsReversed[i] = irmds[len(irmds)-1-i]
}
return irmdsReversed
}
func mdGet(ctx context.Context, config libkbfs.Config, tlfID tlf.ID,
branchID kbfsmd.BranchID, start, stop kbfsmd.Revision) (
irmds []libkbfs.ImmutableRootMetadata, err error) {
if start > stop {
panic("start unexpectedly greater than stop")
}
if branchID == kbfsmd.NullBranchID {
irmds, err = config.MDOps().GetRange(ctx, tlfID, start, stop, nil)
} else {
irmds, err = config.MDOps().GetUnmergedRange(
ctx, tlfID, branchID, start, stop)
}
if err != nil {
return nil, err
}
var latestIRMD libkbfs.ImmutableRootMetadata
var uid keybase1.UID
for i, irmd := range irmds {
if !irmd.IsReadable() {
if latestIRMD == (libkbfs.ImmutableRootMetadata{}) {
if branchID == kbfsmd.NullBranchID {
latestIRMD, err = config.MDOps().GetForTLF(ctx, tlfID, nil)
} else {
latestIRMD, err = config.MDOps().GetUnmergedForTLF(ctx, tlfID, branchID)
}
if err != nil {
return nil, err
}
}
if uid == keybase1.UID("") {
session, err := config.KBPKI().GetCurrentSession(ctx)
if err != nil {
return nil, err
}
uid = session.UID
}
irmdCopy, err := libkbfs.MakeCopyWithDecryptedPrivateData(
ctx, config, irmd, latestIRMD, uid)
if err != nil {
return nil, err
}
irmds[i] = irmdCopy
}
}
return irmds, nil
}
func mdGetMergedHeadForWriter(ctx context.Context, config libkbfs.Config,
tlfPath string) (libkbfs.ImmutableRootMetadata, error) {
handle, err := parseTLFPath(ctx, config.KBPKI(), config.MDOps(), tlfPath)
if err != nil {
return libkbfs.ImmutableRootMetadata{}, err
}
session, err := config.KBPKI().GetCurrentSession(ctx)
if err != nil {
return libkbfs.ImmutableRootMetadata{}, err
}
// Make sure we're a writer before doing anything else.
if !handle.IsWriter(session.UID) {
return libkbfs.ImmutableRootMetadata{},
libkbfs.NewWriteAccessError(
handle, session.Name, handle.GetCanonicalPath())
}
fmt.Printf("Looking for unmerged branch...\n")
tlfID, err := config.MDOps().GetIDForHandle(ctx, handle)
if err != nil {
return libkbfs.ImmutableRootMetadata{}, err
}
if tlfID == tlf.NullID {
return libkbfs.ImmutableRootMetadata{}, errors.New("No TLF ID")
}
unmergedIRMD, err := config.MDOps().GetUnmergedForTLF(
ctx, tlfID, kbfsmd.NullBranchID)
if err != nil {
return libkbfs.ImmutableRootMetadata{}, err
}
if unmergedIRMD != (libkbfs.ImmutableRootMetadata{}) {
return libkbfs.ImmutableRootMetadata{}, fmt.Errorf(
"%s has unmerged data; try unstaging it first",
tlfPath)
}
fmt.Printf("Getting latest metadata...\n")
irmd, err := config.MDOps().GetForTLF(ctx, tlfID, nil)
if err != nil {
return libkbfs.ImmutableRootMetadata{}, err
}
if irmd == (libkbfs.ImmutableRootMetadata{}) {
fmt.Printf("No TLF found for %q\n", tlfPath)
return libkbfs.ImmutableRootMetadata{}, nil
}
return irmd, nil
}