/
cmd_simplefs.go
527 lines (469 loc) · 14.2 KB
/
cmd_simplefs.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/hex"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
var ErrTargetFileExists = errors.New("target file exists")
// NewCmdSimpleFS creates the device command, which is just a holder
// for subcommands.
func NewCmdSimpleFS(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "fs",
Usage: "Perform filesystem operations",
ArgumentHelp: "[arguments...]",
Subcommands: append([]cli.Command{
NewCmdSimpleFSList(cl, g),
NewCmdSimpleFSCopy(cl, g),
NewCmdSimpleFSMove(cl, g),
NewCmdSimpleFSSymlink(cl, g),
NewCmdSimpleFSRead(cl, g),
NewCmdSimpleFSRemove(cl, g),
NewCmdSimpleFSMkdir(cl, g),
NewCmdSimpleFSStat(cl, g),
NewCmdSimpleFSGetStatus(cl, g),
NewCmdSimpleFSKill(cl, g),
NewCmdSimpleFSPs(cl, g),
NewCmdSimpleFSWrite(cl, g),
NewCmdSimpleFSDebug(cl, g),
NewCmdSimpleFSSetDebugLevel(cl, g),
NewCmdSimpleFSHistory(cl, g),
NewCmdSimpleFSQuota(cl, g),
NewCmdSimpleFSRecover(cl, g),
NewCmdSimpleFSReset(cl, g),
NewCmdSimpleFSClearConflicts(cl, g),
NewCmdSimpleFSFinishResolvingConflicts(cl, g),
NewCmdSimpleFSSync(cl, g),
NewCmdSimpleFSUploads(cl, g),
NewCmdSimpleFSCancelUploads(cl, g),
NewCmdSimpleFSArchive(cl, g),
NewCmdSimpleFSSearch(cl, g),
NewCmdSimpleFSResetIndex(cl, g),
NewCmdSimpleFSIndexProgress(cl, g),
}, getBuildSpecificFSCommands(cl, g)...),
}
}
const (
mountDir = "/keybase"
protPrefix = "keybase://"
)
func makeKbfsPath(
path string, rev int64, timeString, relTimeString string) (
keybase1.Path, error) {
p := strings.TrimPrefix(path, mountDir)
if strings.HasPrefix(p, protPrefix) {
var err error
p, err = url.PathUnescape(p)
if err != nil {
return keybase1.Path{}, err
}
p = strings.TrimPrefix(p, protPrefix)
}
if rev == 0 && timeString == "" && relTimeString == "" {
return keybase1.NewPathWithKbfsPath(p), nil
} else if rev != 0 {
if timeString != "" || relTimeString != "" {
return keybase1.Path{}, errors.New(
"can't set both a revision and a time")
}
return keybase1.NewPathWithKbfsArchived(keybase1.KBFSArchivedPath{
Path: p,
ArchivedParam: keybase1.NewKBFSArchivedParamWithRevision(
keybase1.KBFSRevision(rev)),
}), nil
} else if timeString != "" {
if relTimeString != "" {
return keybase1.Path{}, errors.New(
"can't set both an absolute time and a relative time")
}
return keybase1.NewPathWithKbfsArchived(keybase1.KBFSArchivedPath{
Path: p,
ArchivedParam: keybase1.NewKBFSArchivedParamWithTimeString(
timeString),
}), nil
}
return keybase1.NewPathWithKbfsArchived(keybase1.KBFSArchivedPath{
Path: p,
ArchivedParam: keybase1.NewKBFSArchivedParamWithRelTimeString(
relTimeString),
}), nil
}
func makeSimpleFSPathWithArchiveParams(
path string, rev int64, timeString, relTimeString string) (
keybase1.Path, error) {
path = filepath.ToSlash(path)
path = strings.TrimSuffix(path, "/")
// Test for the special mount dir prefix before the absolute test.
// Otherwise the current dir will be prepended, below.
if strings.HasPrefix(path, mountDir) ||
strings.HasPrefix(path, protPrefix) {
return makeKbfsPath(path, rev, timeString, relTimeString)
}
// make absolute
if absPath, err := filepath.Abs(path); err == nil {
path = absPath
}
// eval symlinks
if pathSym, err := filepath.EvalSymlinks(path); err == nil {
path = pathSym
}
path = filepath.ToSlash(filepath.Clean(path))
// Certain users seem to want to use SimpleFS on their
// mounted KBFS. This is for those who want to do so
// from "/keybase/..."
if strings.HasPrefix(path, mountDir) {
return makeKbfsPath(path, rev, timeString, relTimeString)
}
if rev > 0 {
return keybase1.Path{}, fmt.Errorf(
"can't specify a revision for a local path")
} else if timeString != "" {
return keybase1.Path{}, fmt.Errorf(
"can't specify a time string for a local path")
} else if relTimeString != "" {
return keybase1.Path{}, fmt.Errorf(
"can't specify a relative time string for a local path")
}
return keybase1.NewPathWithLocal(path), nil
}
func makeSimpleFSPath(path string) (keybase1.Path, error) {
return makeSimpleFSPathWithArchiveParams(path, 0, "", "")
}
func stringToOpID(arg string) (keybase1.OpID, error) {
var opid keybase1.OpID
bytes, err := hex.DecodeString(arg)
if err != nil {
return keybase1.OpID{}, err
}
if copy(opid[:], bytes) != len(opid) {
return keybase1.OpID{}, errors.New("bad or missing opid")
}
return opid, nil
}
// Check whether the given path is a directory and return its string
func checkPathIsDir(ctx context.Context, cli keybase1.SimpleFSInterface, path keybase1.Path) (bool, string, error) {
var isDir bool
var pathString string
var err error
pathType, _ := path.PathType()
switch pathType {
case keybase1.PathType_KBFS, keybase1.PathType_KBFS_ARCHIVED:
if pathType == keybase1.PathType_KBFS {
pathString = path.Kbfs().Path
} else {
pathString = path.KbfsArchived().Path
}
// See if the dest is a path or file
destEnt, err := cli.SimpleFSStat(ctx, keybase1.SimpleFSStatArg{Path: path})
if err != nil {
return false, "", err
}
if destEnt.DirentType == keybase1.DirentType_DIR {
isDir = true
}
case keybase1.PathType_LOCAL:
pathString = path.Local()
// An error is OK, could be a target filename
// that does not exist yet
fileInfo, err := os.Stat(pathString)
if err == nil {
if fileInfo.IsDir() {
isDir = true
}
}
}
return isDir, pathString, err
}
func joinSimpleFSPaths(destType keybase1.PathType, destPathString, srcPathString string) keybase1.Path {
newDestString := filepath.ToSlash(filepath.Join(destPathString, filepath.Base(srcPathString)))
if destType == keybase1.PathType_KBFS {
return keybase1.NewPathWithKbfsPath(newDestString)
}
return keybase1.NewPathWithLocal(newDestString)
}
func checkElementExists(ctx context.Context, cli keybase1.SimpleFSInterface, dest keybase1.Path) error {
destType, _ := dest.PathType()
var err error
// Check for overwriting
if destType == keybase1.PathType_KBFS {
// See if the dest file exists
_, err2 := cli.SimpleFSStat(ctx, keybase1.SimpleFSStatArg{Path: dest})
if err2 == nil {
err = ErrTargetFileExists
}
} else {
if exists, _ := libkb.FileExists(dest.Local()); exists {
// we should have already tested whether it's a directory
err = ErrTargetFileExists
}
}
return err
}
// Make sure the destination ends with the same filename as the source,
// if any, unless the destination is not a directory
func makeDestPath(
ctx context.Context,
g *libkb.GlobalContext,
cli keybase1.SimpleFSInterface,
src keybase1.Path,
dest keybase1.Path,
isDestPath bool,
destPathString string) (keybase1.Path, error) {
isSrcDir, srcPathString, err := checkPathIsDir(ctx, cli, src)
if err != nil {
return keybase1.Path{}, err
}
g.Log.Debug("makeDestPath: srcPathString: %s isSrcDir: %v", src, isSrcDir)
if isDestPath {
// Source file and dest dir is an append case
appendDest := !isSrcDir
if isSrcDir {
// Here, we have both source and dest as paths, so
// we have to check whether dest exists. If so, append.
err2 := checkElementExists(ctx, cli, dest)
if err2 == ErrTargetFileExists {
appendDest = true
}
g.Log.Debug("makeDestPath: src and dest both dir. append: %v", appendDest)
}
if appendDest {
destType, _ := dest.PathType()
// In this case, we must append the destination filename
dest = joinSimpleFSPaths(destType, destPathString, srcPathString)
g.Log.Debug("makeDestPath: new path with file: %s", dest)
}
}
err = checkElementExists(ctx, cli, dest)
return dest, err
}
func getRelTime(ctx *cli.Context) string {
relTimeString := ctx.String("reltime")
if relTimeString == "" {
relTimeString = ctx.String("relative-time")
}
return relTimeString
}
// Make a list of source paths and one destination path from the given command line args
func parseSrcDestArgs(g *libkb.GlobalContext, ctx *cli.Context, name string) ([]keybase1.Path, keybase1.Path, error) {
nargs := len(ctx.Args())
var srcType, destType keybase1.PathType
var srcPaths []keybase1.Path
var destPath keybase1.Path
if nargs < 2 {
return srcPaths, destPath, errors.New(name + " requires one or more source arguments and a destination argument")
}
for i, src := range ctx.Args() {
rev := int64(0)
timeString := ""
relTimeString := ""
if i != nargs-1 {
// All source paths use the same revision.
rev = int64(ctx.Int("rev"))
timeString = ctx.String("time")
relTimeString = getRelTime(ctx)
}
argPath, err := makeSimpleFSPathWithArchiveParams(
src, rev, timeString, relTimeString)
if err != nil {
return nil, keybase1.Path{}, err
}
tempPathType, err := argPath.PathType()
if err != nil {
return srcPaths, destPath, err
}
// Make sure all source paths are the same type
if i == 0 {
srcType = tempPathType
} else if i == nargs-1 {
destPath = argPath
destType = tempPathType
break
} else if tempPathType != srcType {
return srcPaths, destPath, errors.New(name + " requires all sources to be the same type")
}
srcPaths = append(srcPaths, argPath)
}
if srcType == keybase1.PathType_LOCAL && destType == keybase1.PathType_LOCAL {
return srcPaths, destPath, errors.New(name + " requires KBFS source and/or destination")
}
return srcPaths, destPath, nil
}
func doOverwritePrompt(g *libkb.GlobalContext, dest string) error {
prompt := dest + " exists. Do you want to overwrite?"
if owrite, err := g.UI.GetTerminalUI().PromptYesNo(PromptDescriptorFSOverwrite, prompt, libkb.PromptDefaultNo); err != nil {
return err
} else if !owrite {
return NotConfirmedError{}
}
return nil
}
func newPathWithSameType(
pathString string, oldPath keybase1.Path) (keybase1.Path, error) {
pt, err := oldPath.PathType()
if err != nil {
return keybase1.Path{}, err
}
switch pt {
case keybase1.PathType_LOCAL:
return keybase1.NewPathWithLocal(pathString), nil
case keybase1.PathType_KBFS:
return keybase1.NewPathWithKbfsPath(pathString), nil
case keybase1.PathType_KBFS_ARCHIVED:
return keybase1.NewPathWithKbfsArchived(keybase1.KBFSArchivedPath{
Path: pathString,
ArchivedParam: oldPath.KbfsArchived().ArchivedParam,
}), nil
default:
return keybase1.Path{}, fmt.Errorf("unknown path type: %s", pt)
}
}
func doSimpleFSRemoteGlob(ctx context.Context, g *libkb.GlobalContext, cli keybase1.SimpleFSInterface, path keybase1.Path) ([]keybase1.Path, error) {
var returnPaths []keybase1.Path
pathString := path.String()
directory := filepath.ToSlash(filepath.Dir(pathString))
base := filepath.Base(pathString)
// We know the filename has wildcards at this point.
// kbfs list only works on directories, so build a glob from a list result.
g.Log.Debug("doSimpleFSRemoteGlob %s", pathString)
if strings.ContainsAny(directory, "?*[]") {
return nil, errors.New("wildcards not supported in parent directories")
}
opid, err := cli.SimpleFSMakeOpid(ctx)
if err != nil {
return nil, err
}
defer cli.SimpleFSClose(ctx, opid)
dirPath, err := newPathWithSameType(directory, path)
if err != nil {
return nil, err
}
err = cli.SimpleFSList(ctx, keybase1.SimpleFSListArg{
OpID: opid,
Path: dirPath,
})
if err != nil {
return nil, err
}
err = cli.SimpleFSWait(ctx, opid)
if err != nil {
return nil, err
}
for {
listResult, err2 := cli.SimpleFSReadList(ctx, opid)
if err2 != nil {
// If we have some results, eat this error
if len(returnPaths) == 0 {
err = err2
}
break
}
for _, entry := range listResult.Entries {
match, err := filepath.Match(base, entry.Name)
if err == nil && match {
rp, err := newPathWithSameType(
filepath.ToSlash(filepath.Join(directory, entry.Name)),
path)
if err != nil {
return nil, err
}
returnPaths = append(returnPaths, rp)
}
}
}
return returnPaths, err
}
func doSimpleFSGlob(ctx context.Context, g *libkb.GlobalContext, cli keybase1.SimpleFSInterface, paths []keybase1.Path) ([]keybase1.Path, error) {
var returnPaths []keybase1.Path
for _, path := range paths {
pathType, err := path.PathType()
if err != nil {
return returnPaths, err
}
pathString := path.String()
if !strings.ContainsAny(filepath.Base(pathString), "?*[]") {
returnPaths = append(returnPaths, path)
continue
}
if pathType == keybase1.PathType_KBFS ||
pathType == keybase1.PathType_KBFS_ARCHIVED {
// remote glob
globbed, err := doSimpleFSRemoteGlob(ctx, g, cli, path)
if err != nil {
return nil, err
}
returnPaths = append(returnPaths, globbed...)
} else {
// local glob
matches, err := filepath.Glob(pathString)
if err != nil {
return nil, err
}
for _, match := range matches {
returnPaths = append(returnPaths, keybase1.NewPathWithLocal(match))
}
}
}
return returnPaths, nil
}
type OpCanceler struct {
libkb.Contextified
lock *sync.Mutex
cancelled bool
opids []keybase1.OpID
}
func NewOpCanceler(g *libkb.GlobalContext) *OpCanceler {
return &OpCanceler{
Contextified: libkb.NewContextified(g),
lock: &sync.Mutex{},
}
}
func (j *OpCanceler) AddOp(opid keybase1.OpID) {
j.lock.Lock()
defer j.lock.Unlock()
if j.cancelled {
j.G().Log.Warning("added a SimpleFS opid after cancellation")
}
j.opids = append(j.opids, opid)
}
func (j *OpCanceler) IsCancelled() bool {
j.lock.Lock()
defer j.lock.Unlock()
return j.cancelled
}
func (j *OpCanceler) Cancel() error {
j.lock.Lock()
defer j.lock.Unlock()
j.cancelled = true
cli, err := GetSimpleFSClient(j.G())
if err != nil {
return err
}
var cancelError error
for _, opid := range j.opids {
opidString := hex.EncodeToString(opid[:])
err := cli.SimpleFSCancel(context.TODO(), opid)
if err != nil {
// We retain the first cancel error we see, but we still try to
// cancel all running operations.
if cancelError == nil {
cancelError = err
}
j.G().Log.Error("Error cancelling FS operation %s: %s", opidString, err)
} else {
j.G().Log.Info("Cancelled FS operation %s", opidString)
}
}
return cancelError
}