forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_extension.go
287 lines (257 loc) · 9.65 KB
/
handle_extension.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
// 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 tlf
import (
"errors"
"fmt"
"regexp"
"strconv"
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/go-codec/codec"
)
const (
// HandleExtensionSep is the string that separates the folder
// participants from an extension suffix in the TLF name.
HandleExtensionSep = " "
// HandleExtensionDateFormat is the date format for the HandleExtension string.
handleExtensionDateFormat = "2006-01-02"
// HandleExtensionDateRegex is the regular expression matching the HandleExtension
// date member in string form.
handleExtensionDateRegex = "2[0-9]{3}-[0-9]{2}-[0-9]{2}"
// HandleExtensionNumberRegex is the regular expression matching the HandleExtension
// number member in string form.
handleExtensionNumberRegex = "[0-9]+"
// HandleExtensionUsernameRegex is the regular expression matching the HandleExtension
// username member in string form.
handleExtensionUsernameRegex = "[a-z0-9_]+"
// HandleExtensionConflictString is the string identifying a conflict extension.
handleExtensionConflictString = "conflicted copy"
// HandleExtensionFinalizedString is the format string identifying a finalized extension.
handleExtensionFinalizedString = "files before %saccount reset"
// HandleExtensionFormat is the formate string for a HandleExtension.
handleExtensionFormat = "(%s %s%s)"
// HandleExtensionStaticTestDate is a static date used for tests (2016-03-14).
HandleExtensionStaticTestDate = 1457913600
)
// HandleExtensionType is the type of extension.
type HandleExtensionType int
const (
// HandleExtensionConflict means the handle conflicted as a result of a social
// assertion resolution.
HandleExtensionConflict HandleExtensionType = iota
// HandleExtensionFinalized means the folder ended up with no more valid writers as
// a result of an account reset.
HandleExtensionFinalized
// HandleExtensionUnknown means the type is unknown.
HandleExtensionUnknown
)
// HandleExtensionFinalizedStringRegex is the regex identifying a finalized extension string.
var handleExtensionFinalizedStringRegex = fmt.Sprintf(
handleExtensionFinalizedString, "(?:"+handleExtensionUsernameRegex+"[\\s]+)*",
)
// HandleExtensionTypeRegex is the regular expression matching the HandleExtension string.
var handleExtensionTypeRegex = handleExtensionConflictString + "|" + handleExtensionFinalizedStringRegex
// HandleExtensionFinalizedRegex is the compiled regular expression matching a finalized
// handle extension.
var handleExtensionFinalizedRegex = regexp.MustCompile(
fmt.Sprintf(handleExtensionFinalizedString, "(?:("+handleExtensionUsernameRegex+")[\\s]+)*"),
)
// String implements the fmt.Stringer interface for HandleExtensionType
func (et HandleExtensionType) String(username libkb.NormalizedUsername) string {
switch et {
case HandleExtensionConflict:
return handleExtensionConflictString
case HandleExtensionFinalized:
if len(username) != 0 {
username += " "
}
return fmt.Sprintf(handleExtensionFinalizedString, username)
}
return "<unknown extension type>"
}
// parseHandleExtensionString parses an extension type and optional username from a string.
func parseHandleExtensionString(s string) (HandleExtensionType, libkb.NormalizedUsername) {
if handleExtensionConflictString == s {
return HandleExtensionConflict, ""
}
m := handleExtensionFinalizedRegex.FindStringSubmatch(s)
if len(m) < 2 {
return HandleExtensionUnknown, ""
}
return HandleExtensionFinalized, libkb.NewNormalizedUsername(m[1])
}
// ErrHandleExtensionInvalidString is returned when a given string is not parsable as a
// valid extension suffix.
var errHandleExtensionInvalidString = errors.New("Invalid TLF handle extension string")
// ErrHandleExtensionInvalidNumber is returned when an invalid number is used in an
// extension definition. Handle extension numbers present in the string must be >1. Numbers
// passed to NewHandleExtension must be >0.
var errHandleExtensionInvalidNumber = errors.New("Invalid TLF handle extension number")
// HandleExtensionRegex is the compiled regular expression matching a valid combination
// of TLF handle extensions in string form.
var handleExtensionRegex = regexp.MustCompile(
fmt.Sprintf("\\"+handleExtensionFormat,
"("+handleExtensionTypeRegex+")",
"("+handleExtensionDateRegex+")",
"(?: #("+handleExtensionNumberRegex+"))?\\"),
)
// HandleExtension is information which identifies a particular extension.
type HandleExtension struct {
Date int64 `codec:"date"`
Number uint16 `codec:"num"`
Type HandleExtensionType `codec:"type"`
Username libkb.NormalizedUsername `codec:"un,omitempty"`
codec.UnknownFieldSetHandler
}
// String implements the fmt.Stringer interface for HandleExtension.
// Ex: "(conflicted copy 2016-05-09 #2)"
func (e HandleExtension) string(isBackedByTeam bool) string {
date := time.Unix(e.Date, 0).UTC().Format(handleExtensionDateFormat)
var num string
minNumberSuffixToShow := uint16(2)
if isBackedByTeam {
// When a TLF is backed by an implicit team, it should always
// use the "#1" suffix, unlike for older TLFs.
minNumberSuffixToShow = 1
}
if e.Number >= minNumberSuffixToShow {
num = " #"
num += strconv.FormatUint(uint64(e.Number), 10)
}
return fmt.Sprintf(handleExtensionFormat, e.Type.String(e.Username), date, num)
}
// String implements the fmt.Stringer interface for HandleExtension.
// Ex: "(conflicted copy 2016-05-09 #2)"
func (e HandleExtension) String() string {
return e.string(false)
}
// NewHandleExtension returns a new HandleExtension struct
// populated with the date from the given time and conflict number.
func NewHandleExtension(extType HandleExtensionType, num uint16, un libkb.NormalizedUsername, now time.Time) (
*HandleExtension, error) {
return newHandleExtension(extType, num, un, now)
}
// NewTestHandleExtensionStaticTime returns a new HandleExtension struct populated with
// a static date for testing.
func NewTestHandleExtensionStaticTime(extType HandleExtensionType, num uint16, un libkb.NormalizedUsername) (
*HandleExtension, error) {
now := time.Unix(HandleExtensionStaticTestDate, 0)
return newHandleExtension(extType, num, un, now)
}
// Helper to instantiate a HandleExtension object.
func newHandleExtension(extType HandleExtensionType, num uint16, un libkb.NormalizedUsername, now time.Time) (
*HandleExtension, error) {
if num == 0 {
return nil, errHandleExtensionInvalidNumber
}
// mask out everything but the date
date := now.UTC().Format(handleExtensionDateFormat)
now, err := time.Parse(handleExtensionDateFormat, date)
if err != nil {
return nil, err
}
return &HandleExtension{
Date: now.UTC().Unix(),
Number: num,
Type: extType,
Username: un,
}, nil
}
// parseHandleExtension parses a HandleExtension array of string fields.
func parseHandleExtension(fields []string) (*HandleExtension, error) {
if len(fields) != 4 {
return nil, errHandleExtensionInvalidString
}
extType, un := parseHandleExtensionString(fields[1])
if extType == HandleExtensionUnknown {
return nil, errHandleExtensionInvalidString
}
date, err := time.Parse(handleExtensionDateFormat, fields[2])
if err != nil {
return nil, err
}
var num uint64 = 1
if len(fields[3]) != 0 {
num, err = strconv.ParseUint(fields[3], 10, 16)
if err != nil {
return nil, err
}
if num < 1 {
return nil, errHandleExtensionInvalidNumber
}
}
return &HandleExtension{
Date: date.UTC().Unix(),
Number: uint16(num),
Type: extType,
Username: un,
}, nil
}
// ParseHandleExtensionSuffix parses a TLF handle extension suffix string.
func ParseHandleExtensionSuffix(s string) ([]HandleExtension, error) {
exts := handleExtensionRegex.FindAllStringSubmatch(s, 2)
if len(exts) < 1 || len(exts) > 2 {
return nil, errHandleExtensionInvalidString
}
extMap := make(map[HandleExtensionType]bool)
var extensions []HandleExtension
for _, e := range exts {
ext, err := parseHandleExtension(e)
if err != nil {
return nil, err
}
if extMap[ext.Type] {
// No duplicate extension types in the same suffix.
return nil, errHandleExtensionInvalidString
}
extMap[ext.Type] = true
extensions = append(extensions, *ext)
}
return extensions, nil
}
// newHandleExtensionSuffix creates a suffix string given a set of extensions.
func newHandleExtensionSuffix(
extensions []HandleExtension, isBackedByTeam bool) string {
var suffix string
for _, extension := range extensions {
suffix += HandleExtensionSep
suffix += extension.string(isBackedByTeam)
}
return suffix
}
// HandleExtensionList allows us to sort extensions by type.
type HandleExtensionList []HandleExtension
func (l HandleExtensionList) Len() int {
return len(l)
}
func (l HandleExtensionList) Less(i, j int) bool {
return l[i].Type < l[j].Type
}
func (l HandleExtensionList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// Splat will deconstruct the list for the caller into individual extension
// pointers (or nil.)
func (l HandleExtensionList) Splat() (ci, fi *HandleExtension) {
for _, extension := range l {
tmp := extension
if extension.Type == HandleExtensionConflict {
ci = &tmp
} else if extension.Type == HandleExtensionFinalized {
fi = &tmp
}
}
return ci, fi
}
// Suffix outputs a suffix string for this extension list.
func (l HandleExtensionList) Suffix() string {
return newHandleExtensionSuffix(l, false)
}
// SuffixForTeamHandle outputs a suffix string for this extension list
// for a handle that's backed by a team (which must be an implicit
// team, since there aren't any suffixes for regulat teams).
func (l HandleExtensionList) SuffixForTeamHandle() string {
return newHandleExtensionSuffix(l, true)
}