-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
101 lines (84 loc) · 1.98 KB
/
read.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
// 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 main
import (
"flag"
"fmt"
"io"
"os"
"github.com/adamwalz/keybase-client/go/kbfs/fsrpc"
"github.com/adamwalz/keybase-client/go/kbfs/libkbfs"
"golang.org/x/net/context"
)
type nodeReader struct {
ctx context.Context
kbfsOps libkbfs.KBFSOps
node libkbfs.Node
off int64
verbose bool
}
var _ io.Reader = (*nodeReader)(nil)
func (nr *nodeReader) Read(p []byte) (n int, err error) {
if nr.verbose {
fmt.Fprintf(os.Stderr, "Reading up to %s at offset %d\n", byteCountStr(len(p)), nr.off)
}
n64, err := nr.kbfsOps.Read(nr.ctx, nr.node, p, nr.off)
nr.off += n64
n = int(n64)
if n64 == 0 && err == nil {
if nr.verbose {
fmt.Fprintf(os.Stderr, "EOF encountered\n")
}
err = io.EOF
} else if nr.verbose {
fmt.Fprintf(os.Stderr, "Read %s\n", byteCountStr(n))
}
return
}
func readHelper(ctx context.Context, config libkbfs.Config, args []string) error {
flags := flag.NewFlagSet("kbfs read", flag.ContinueOnError)
verbose := flags.Bool("v", false, "Print extra status output.")
err := flags.Parse(args)
if err != nil {
return err
}
if flags.NArg() != 1 {
return errExactlyOnePath
}
filePathStr := flags.Arg(0)
p, err := fsrpc.NewPath(filePathStr)
if err != nil {
return err
}
if p.PathType != fsrpc.TLFPathType {
return fmt.Errorf("Cannot read %s", p)
}
if *verbose {
fmt.Fprintf(os.Stderr, "Looking up %s\n", p)
}
fileNode, err := p.GetFileNode(ctx, config)
if err != nil {
return err
}
nr := nodeReader{
ctx: ctx,
kbfsOps: config.KBFSOps(),
node: fileNode,
off: 0,
verbose: *verbose,
}
_, err = io.Copy(os.Stdout, &nr)
if err != nil {
return err
}
return nil
}
func read(ctx context.Context, config libkbfs.Config, args []string) (exitStatus int) {
err := readHelper(ctx, config, args)
if err != nil {
printError("read", err)
exitStatus = 1
}
return
}