-
Notifications
You must be signed in to change notification settings - Fork 568
/
pfs.go
372 lines (331 loc) · 9.41 KB
/
pfs.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
package pfs
import (
"bytes"
"encoding"
"encoding/hex"
"fmt"
"hash"
"unicode"
"google.golang.org/protobuf/proto"
"github.com/pachyderm/pachyderm/v2/src/auth"
"github.com/pachyderm/pachyderm/v2/src/internal/ancestry"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pachhash"
)
const (
// ChunkSize is the size of file chunks when resumable upload is used
ChunkSize = int64(512 * 1024 * 1024) // 512 MB
// default system repo types
UserRepoType = "user"
MetaRepoType = "meta"
SpecRepoType = "spec"
DefaultProjectName = "default"
// projectNameLimit is the maximum length of a project name, determined
// by the 63-character Kubernetes resource name limit and leaving eight
// characters for pipeline names.
//
// TODO(CORE-1489): raise this to something more sensible, e.g. 63 or 64
// or 256 characters.
projectNameLimit = 63 - 8 - len("-v99")
)
// NewHash returns a hash that PFS uses internally to compute checksums.
func NewHash() hash.Hash {
return pachhash.New()
}
// EncodeHash encodes a hash into a readable format.
func EncodeHash(bytes []byte) string {
return hex.EncodeToString(bytes)
}
// DecodeHash decodes a hash into bytes.
func DecodeHash(hash string) ([]byte, error) {
res, err := hex.DecodeString(hash)
return res, errors.EnsureStack(err)
}
func (p *Project) String() string {
return p.GetName()
}
func (p *Project) Key() string {
return p.GetName()
}
func (r *Repo) String() string {
if r.GetType() == UserRepoType {
if projectName := r.GetProject().String(); projectName != "" {
return projectName + "/" + r.GetName()
}
return r.GetName()
}
if projectName := r.GetProject().String(); projectName != "" {
return projectName + "/" + r.GetName() + "." + r.GetType()
}
return r.GetName() + "." + r.GetType()
}
func (r *Repo) Key() string {
return r.GetProject().Key() + "/" + r.GetName() + "." + r.GetType()
}
func (r *Repo) NewBranch(name string) *Branch {
return &Branch{
Repo: proto.Clone(r).(*Repo),
Name: name,
}
}
func (r *Repo) NewCommit(branch, id string) *Commit {
return &Commit{
Repo: r,
Id: id,
Branch: r.NewBranch(branch),
}
}
func (c *Commit) NewFile(path string) *File {
return &File{
Commit: proto.Clone(c).(*Commit),
Path: path,
}
}
func (c *Commit) String() string {
return c.Repo.String() + "@" + c.Id
}
func (c *Commit) Key() string {
if c == nil {
return ""
}
if c.Repo == nil {
return "<nil repo>@" + c.Id
}
return c.Repo.Key() + "@" + c.Id
}
// TODO(provenance): there's a concern client code will unknowningly call GetRepo() when it shouldn't
func (c *Commit) AccessRepo() *Repo {
if c.GetRepo() != nil {
return c.GetRepo()
}
return c.GetBranch().GetRepo()
}
func (b *Branch) NewCommit(id string) *Commit {
return &Commit{
Branch: proto.Clone(b).(*Branch),
Id: id,
Repo: b.Repo,
}
}
func (b *Branch) String() string {
return b.Repo.String() + "@" + b.Name
}
func (b *Branch) Key() string {
return b.GetRepo().Key() + "@" + b.Name
}
// ValidateName returns an error if the project is nil or its name is an invalid
// project name. DefaultProjectName is always valid; otherwise the ancestry
// package is used to validate the name.
func (p *Project) ValidateName() error {
if p == nil {
return errors.New("nil project")
}
if p.Name == DefaultProjectName {
return nil
}
if len(p.Name) > projectNameLimit {
return errors.Errorf("project name %q is %d characters longer than the %d max", p.Name, len(p.Name)-projectNameLimit, projectNameLimit)
}
if err := ancestry.ValidateName(p.Name); err != nil {
return err
}
first := rune(p.Name[0])
if !unicode.IsLetter(first) && !unicode.IsDigit(first) {
return errors.Errorf("project names must start with an alphanumeric character")
}
return nil
}
// EnsureProject ensures that repo.Project is set. It does nothing if repo is
// nil.
func (r *Repo) EnsureProject() {
if r == nil {
return
}
if r.Project.GetName() == "" {
r.Project = &Project{Name: DefaultProjectName}
}
}
// AuthResource returns the auth resource for a repo. The resource name is the
// project name and repo name separated by a slash. Notably, it does _not_
// include the repo type string.
func (r *Repo) AuthResource() *auth.Resource {
var t auth.ResourceType
if r.GetType() == SpecRepoType {
t = auth.ResourceType_SPEC_REPO
} else {
t = auth.ResourceType_REPO
}
return &auth.Resource{
Type: t,
Name: fmt.Sprintf("%s/%s", r.GetProject().GetName(), r.GetName()),
}
}
// AuthResource returns the auth resource for a project.
func (p *Project) AuthResource() *auth.Resource {
return &auth.Resource{Type: auth.ResourceType_PROJECT, Name: p.GetName()}
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (p *ProjectPicker) UnmarshalText(b []byte) error {
p.Picker = &ProjectPicker_Name{
Name: string(b),
}
if err := (&Project{Name: string(b)}).ValidateName(); err != nil {
return err
}
if err := p.ValidateAll(); err != nil {
return err
}
return nil
}
var _ encoding.TextUnmarshaler = (*ProjectPicker)(nil)
type repoName Repo // repoName is a repo object used only to store the name and type of a repo.
// UnmarshalText implements encoding.TextUnmarshaler.
func (n *repoName) UnmarshalText(b []byte) error {
parts := bytes.SplitN(b, []byte{'.'}, 2)
switch len(parts) {
case 0:
return errors.New("invalid repo name: empty")
case 1:
n.Name = string(b)
n.Type = UserRepoType
case 2:
n.Name = string(parts[0])
n.Type = string(parts[1])
}
return nil
}
var _ encoding.TextUnmarshaler = (*repoName)(nil)
// UnmarshalText implements encoding.TextUnmarshaler.
func (p *RepoPicker) UnmarshalText(b []byte) error {
parts := bytes.SplitN(b, []byte{'/'}, 2)
var project, repo []byte
switch len(parts) {
case 0:
return errors.New("invalid repo picker: empty")
case 1:
project, repo = nil, parts[0]
case 2:
project, repo = parts[0], parts[1]
default:
return errors.New("invalid repo picker: too many slashes")
}
rnp := &RepoPicker_RepoName{
Project: &ProjectPicker{},
}
p.Picker = &RepoPicker_Name{
Name: rnp,
}
if project != nil {
if err := rnp.Project.UnmarshalText(project); err != nil {
return errors.Wrapf(err, "unmarshal project %s", project)
}
}
var repoName repoName
if err := repoName.UnmarshalText(repo); err != nil {
return errors.Wrapf(err, "unmarshal repo name %s", repo)
}
rnp.Name = repoName.Name
rnp.Type = repoName.Type
if err := p.ValidateAll(); err != nil {
return err
}
return nil
}
var _ encoding.TextUnmarshaler = (*RepoPicker)(nil)
func onlyHex(p []byte) bool {
for _, b := range p {
if (b >= 'a' && b <= 'f') || (b >= 'A' && b <= 'F') || (b >= '0' && b <= '9') {
continue
}
return false
}
return true
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (p *CommitPicker) UnmarshalText(b []byte) error {
parts := bytes.SplitN(b, []byte{'@'}, 2)
var repo, id []byte
switch len(parts) {
case 0:
return errors.New("invalid commit picker: empty")
case 1:
return errors.New("invalid commit picker: no @id")
case 2:
repo, id = parts[0], parts[1]
default:
return errors.New("invalid commit picker: too many @s")
}
// TODO(PFS-229): Implement the other parsers.
if bytes.HasSuffix(id, []byte{'^'}) {
// CommitPicker_AncestorOf
return errors.New("ancestor of commit syntax is currently unimplemented (id^); specify the exact id instead")
}
if bytes.Contains(id, []byte{'.'}) {
// CommitPicker_BranchRoot
return errors.New("branch root commit syntax is currently unimplemented (id.N); specify the exact id instead")
}
// To distinguish global IDs and branch names, we look for a valid global ID first. An ID
// is a hex-encoded UUIDv4 without dashes. UUIDv4s always have the 13th (1 indexed) byte
// set to '4'.
if len(id) == 32 && id[12] == '4' && onlyHex(id) {
var rp RepoPicker
if err := rp.UnmarshalText(repo); err != nil {
return errors.Wrapf(err, "unmarshal repo picker %s", repo)
}
p.Picker = &CommitPicker_Id{
Id: &CommitPicker_CommitByGlobalId{
Id: string(bytes.ToLower(id)),
Repo: &rp,
},
}
} else {
// If the ID isn't a valid UUIDv4, the whole expression is treated as a branch name.
var bp BranchPicker
if err := bp.UnmarshalText(b); err != nil {
return errors.Wrapf(err, "unmarshal branch picker %s", b)
}
p.Picker = &CommitPicker_BranchHead{
BranchHead: &bp,
}
}
if err := p.ValidateAll(); err != nil {
return err
}
return nil
}
var _ encoding.TextUnmarshaler = (*CommitPicker)(nil)
// UnmarshalText implements encoding.TextUnmarshaler.
func (p *BranchPicker) UnmarshalText(b []byte) error {
parts := bytes.SplitN(b, []byte{'@'}, 2)
var repo, name []byte
switch len(parts) {
case 0:
return errors.New("invalid branch picker: empty")
case 1:
return errors.New("invalid branch picker: no @id")
case 2:
repo, name = parts[0], parts[1]
default:
return errors.New("invalid branch picker: too many @s")
}
var rp RepoPicker
if err := rp.UnmarshalText(repo); err != nil {
return errors.Wrapf(err, "unmarshal repo picker %s", repo)
}
// If the branch name looks like a commit id, reject it. Pachyderm does not allow branches
// with this name.
if len(name) == 32 && name[12] == '4' && onlyHex(name) {
return errors.New("invalid branch picker: name refers to a commit, not a branch")
}
p.Picker = &BranchPicker_Name{
Name: &BranchPicker_BranchName{
Repo: &rp,
Name: string(name),
},
}
if err := p.ValidateAll(); err != nil {
return err
}
return nil
}
var _ encoding.TextUnmarshaler = (*BranchPicker)(nil)