forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
358 lines (306 loc) · 8.01 KB
/
path.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
// 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 fsrpc
import (
"fmt"
"path/filepath"
"strings"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/tlf"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
const (
topName = "keybase"
publicName = "public"
privateName = "private"
)
// PathType describes the types for different paths
type PathType int
const (
// InvalidPathType denotes an invalid path type
InvalidPathType PathType = iota
// RootPathType is a root path type (like /)
RootPathType
// KeybasePathType is the keybase root (like /keybase)
KeybasePathType
// KeybaseChildPathType is a keybase reserved path (like /keybase/public)
KeybaseChildPathType
// TLFPathType is a top level folder (/keybase/public/gabrielh)
TLFPathType
)
// Path defines a file path in KBFS such as /keybase/public or /keybase/private/gabrielh
type Path struct {
PathType PathType
TLFType tlf.Type
TLFName string
TLFComponents []string
}
func splitHelper(cleanPath string) []string {
parentPathSlash, child := filepath.Split(cleanPath)
parentPath := parentPathSlash[:len(parentPathSlash)-1]
var parentComponents, childComponents []string
if child != "" {
childComponents = []string{child}
}
if parentPath != "" {
parentComponents = splitHelper(parentPath)
}
return append(parentComponents, childComponents...)
}
func split(pathStr string) ([]string, error) {
cleanPath := filepath.Clean(pathStr)
if !filepath.IsAbs(cleanPath) {
return nil, fmt.Errorf("split: %s is not an absolute path", pathStr)
}
return splitHelper(cleanPath), nil
}
func listTypeToTLFType(c string) tlf.Type {
switch c {
case privateName:
return tlf.Private
case publicName:
return tlf.Public
default:
// TODO: support team TLFs.
panic(fmt.Sprintf("Unknown folder list type: %s", c))
}
}
// NewPath constructs a Path from a string
func NewPath(pathStr string) (Path, error) {
components, err := split(pathStr)
if err != nil {
return Path{}, err
}
len := len(components)
if (len >= 1 && components[0] != topName) ||
(len >= 2 && components[1] != publicName && components[1] != privateName) {
return Path{}, InvalidPathErr{pathStr}
}
if len == 0 {
p := Path{
PathType: RootPathType,
}
return p, nil
}
if len == 1 {
p := Path{
PathType: KeybasePathType,
}
return p, nil
}
if len == 2 {
p := Path{
PathType: KeybaseChildPathType,
TLFType: listTypeToTLFType(components[1]),
}
return p, nil
}
p := Path{
PathType: TLFPathType,
TLFType: listTypeToTLFType(components[1]),
TLFName: components[2],
TLFComponents: components[3:],
}
return p, nil
}
func (p Path) String() string {
if p.PathType < RootPathType || p.PathType > TLFPathType {
return ""
}
var components []string
if p.PathType >= KeybasePathType && p.PathType <= TLFPathType {
components = append(components, topName)
}
if p.PathType >= KeybaseChildPathType && p.PathType <= TLFPathType {
switch p.TLFType {
case tlf.Public:
components = append(components, publicName)
case tlf.Private:
components = append(components, privateName)
default:
// TODO: add support for team TLFs.
panic(fmt.Sprintf("Unknown TLF type: %s", p.TLFType))
}
}
if p.PathType == TLFPathType {
components = append(append(components, p.TLFName), p.TLFComponents...)
}
return "/" + strings.Join(components, "/")
}
// DirAndBasename returns directory and base filename
func (p Path) DirAndBasename() (dir Path, basename string, err error) {
switch p.PathType {
case KeybasePathType:
dir = Path{
PathType: RootPathType,
}
basename = topName
return
case KeybaseChildPathType:
dir = Path{
PathType: KeybasePathType,
}
switch p.TLFType {
case tlf.Public:
basename = publicName
case tlf.Private:
basename = privateName
default:
panic(fmt.Sprintf("Unknown TLF type: %s", p.TLFType))
}
return
case TLFPathType:
len := len(p.TLFComponents)
if len == 0 {
dir = Path{
PathType: KeybaseChildPathType,
TLFType: p.TLFType,
}
basename = p.TLFName
} else {
dir = Path{
PathType: TLFPathType,
TLFType: p.TLFType,
TLFName: p.TLFName,
TLFComponents: p.TLFComponents[:len-1],
}
basename = p.TLFComponents[len-1]
}
return
}
err = errors.New("cannot split path")
return
}
// Join will append a path to this path
func (p Path) Join(childName string) (childPath Path, err error) {
switch p.PathType {
case RootPathType:
if childName != topName {
err = CannotJoinPathErr{p, childName}
return
}
childPath = Path{
PathType: KeybasePathType,
}
return
case KeybasePathType:
if childName != publicName && childName != privateName {
err = CannotJoinPathErr{p, childName}
}
childPath = Path{
PathType: KeybaseChildPathType,
TLFType: listTypeToTLFType(childName),
}
return
case KeybaseChildPathType:
childPath = Path{
PathType: TLFPathType,
TLFType: p.TLFType,
TLFName: childName,
}
return
case TLFPathType:
childPath = Path{
PathType: TLFPathType,
TLFType: p.TLFType,
TLFName: p.TLFName,
TLFComponents: append(p.TLFComponents, childName),
}
return
}
err = CannotJoinPathErr{p, childName}
return
}
// ParseTlfHandle is a wrapper around libkbfs.ParseTlfHandle that
// automatically resolves non-canonical names.
func ParseTlfHandle(
ctx context.Context, kbpki libkbfs.KBPKI, mdOps libkbfs.MDOps,
name string, t tlf.Type) (*libkbfs.TlfHandle, error) {
var tlfHandle *libkbfs.TlfHandle
outer:
for {
var parseErr error
tlfHandle, parseErr = libkbfs.ParseTlfHandle(ctx, kbpki, mdOps, name, t)
switch parseErr := errors.Cause(parseErr).(type) {
case nil:
// No error.
break outer
case libkbfs.TlfNameNotCanonical:
// Non-canonical name, so try again.
name = parseErr.NameToTry
default:
// Some other error.
return nil, parseErr
}
}
return tlfHandle, nil
}
// GetNode returns a node
func (p Path) GetNode(ctx context.Context, config libkbfs.Config) (libkbfs.Node, libkbfs.EntryInfo, error) {
if p.PathType != TLFPathType {
entryInfo := libkbfs.EntryInfo{
Type: libkbfs.Dir,
}
return nil, entryInfo, nil
}
tlfHandle, err := ParseTlfHandle(
ctx, config.KBPKI(), config.MDOps(), p.TLFName, p.TLFType)
if err != nil {
return nil, libkbfs.EntryInfo{}, err
}
node, entryInfo, err := config.KBFSOps().GetOrCreateRootNode(ctx, tlfHandle, libkbfs.MasterBranch)
if err != nil {
return nil, libkbfs.EntryInfo{}, err
}
for _, component := range p.TLFComponents {
lookupNode, lookupEntryInfo, lookupErr := config.KBFSOps().Lookup(ctx, node, component)
if lookupErr != nil {
return nil, libkbfs.EntryInfo{}, lookupErr
}
node = lookupNode
entryInfo = lookupEntryInfo
}
return node, entryInfo, nil
}
// GetFileNode returns a file node
func (p Path) GetFileNode(ctx context.Context, config libkbfs.Config) (libkbfs.Node, error) {
n, de, err := p.GetNode(ctx, config)
if err != nil {
return nil, err
}
// TODO: What to do with symlinks?
if de.Type != libkbfs.File && de.Type != libkbfs.Exec {
return nil, fmt.Errorf("openFile: %s is not a file, but a %s", p, de.Type)
}
return n, nil
}
// GetDirNode returns a nil node if this doesn't have type TLFPathType
func (p Path) GetDirNode(ctx context.Context, config libkbfs.Config) (libkbfs.Node, error) {
// TODO: Handle non-TLFPathTypes.
n, de, err := p.GetNode(ctx, config)
if err != nil {
return nil, err
}
// TODO: What to do with symlinks?
if de.Type != libkbfs.Dir {
return nil, fmt.Errorf("openDir: %s is not a dir, but a %s", p, de.Type)
}
return n, nil
}
// InvalidPathErr is error for invalid paths
type InvalidPathErr struct {
pathStr string
}
func (e InvalidPathErr) Error() string {
return fmt.Sprintf("invalid kbfs path %s", e.pathStr)
}
// CannotJoinPathErr is returned on Join error
type CannotJoinPathErr struct {
p Path
name string
}
func (e CannotJoinPathErr) Error() string {
return fmt.Sprintf("cannot join %s to %s", e.p, e.name)
}