1 6b84ea70 2015-10-28 stephen.d package main
3 6b84ea70 2015-10-28 stephen.d import (
10 e6bcde66 2015-10-29 stephen.d "net/http"
11 e6bcde66 2015-10-29 stephen.d _ "net/http/pprof"
14 6b84ea70 2015-10-28 stephen.d "strings"
15 6b84ea70 2015-10-28 stephen.d "text/tabwriter"
18 6b84ea70 2015-10-28 stephen.d "github.com/chzyer/readline"
19 6b84ea70 2015-10-28 stephen.d "github.com/docker/pinata/v1/fs/p9p/new"
20 6b84ea70 2015-10-28 stephen.d "golang.org/x/net/context"
23 25c15659 2015-10-30 stephen.d var addr string
25 25c15659 2015-10-30 stephen.d func init() {
26 25c15659 2015-10-30 stephen.d flag.StringVar(&addr, "addr", ":5640", "addr of 9p service")
29 6b84ea70 2015-10-28 stephen.d func main() {
30 e6bcde66 2015-10-29 stephen.d go func() {
31 e6bcde66 2015-10-29 stephen.d log.Println(http.ListenAndServe("localhost:6060", nil))
34 25c15659 2015-10-30 stephen.d ctx := context.Background()
35 6b84ea70 2015-10-28 stephen.d log.SetFlags(0)
36 25c15659 2015-10-30 stephen.d flag.Parse()
38 40d4a02d 2015-11-03 stephen.d proto := "tcp"
39 40d4a02d 2015-11-03 stephen.d if strings.HasPrefix(addr, "unix:") {
40 40d4a02d 2015-11-03 stephen.d proto = "unix"
41 40d4a02d 2015-11-03 stephen.d addr = addr[5:]
44 25c15659 2015-10-30 stephen.d log.Println("dialing", addr)
45 40d4a02d 2015-11-03 stephen.d conn, err := net.Dial(proto, addr)
46 25c15659 2015-10-30 stephen.d if err != nil {
47 25c15659 2015-10-30 stephen.d log.Fatal(err)
50 25c15659 2015-10-30 stephen.d csession, err := p9pnew.NewSession(ctx, conn)
51 e6bcde66 2015-10-29 stephen.d if err != nil {
52 e6bcde66 2015-10-29 stephen.d log.Fatalln(err)
55 6b84ea70 2015-10-28 stephen.d commander := &fsCommander{
56 6b84ea70 2015-10-28 stephen.d ctx: context.Background(),
57 e6bcde66 2015-10-29 stephen.d session: csession,
58 6b84ea70 2015-10-28 stephen.d pwd: "/",
59 6b84ea70 2015-10-28 stephen.d stdout: os.Stdout,
60 6b84ea70 2015-10-28 stephen.d stderr: os.Stderr,
63 6b84ea70 2015-10-28 stephen.d completer := readline.NewPrefixCompleter(
64 6b84ea70 2015-10-28 stephen.d readline.PcItem("ls"),
65 6b84ea70 2015-10-28 stephen.d // readline.PcItem("find"),
66 74ec7ac9 2015-10-30 stephen.d readline.PcItem("stat"),
67 6b84ea70 2015-10-28 stephen.d readline.PcItem("cat"),
68 6b84ea70 2015-10-28 stephen.d readline.PcItem("cd"),
69 6b84ea70 2015-10-28 stephen.d readline.PcItem("pwd"),
72 6b84ea70 2015-10-28 stephen.d rl, err := readline.NewEx(&readline.Config{
73 6b84ea70 2015-10-28 stephen.d HistoryFile: ".history",
74 6b84ea70 2015-10-28 stephen.d AutoComplete: completer,
76 6b84ea70 2015-10-28 stephen.d if err != nil {
77 6b84ea70 2015-10-28 stephen.d log.Fatalln(err)
79 6b84ea70 2015-10-28 stephen.d commander.readline = rl
81 fb37ce2a 2015-10-30 stephen.d msize, version := commander.session.Version()
82 fb37ce2a 2015-10-30 stephen.d if err != nil {
83 fb37ce2a 2015-10-30 stephen.d log.Fatalln(err)
85 fb37ce2a 2015-10-30 stephen.d log.Println("9p version", version, msize)
87 6b84ea70 2015-10-28 stephen.d // attach root
88 6b84ea70 2015-10-28 stephen.d commander.nextfid = 1
89 6b84ea70 2015-10-28 stephen.d if _, err := commander.session.Attach(commander.ctx, commander.nextfid, p9pnew.NOFID, "anyone", "/"); err != nil {
90 6b84ea70 2015-10-28 stephen.d log.Fatalln(err)
92 6b84ea70 2015-10-28 stephen.d commander.rootfid = commander.nextfid
93 6b84ea70 2015-10-28 stephen.d commander.nextfid++
95 6b84ea70 2015-10-28 stephen.d // clone the pwd fid so we can clunk it
96 6b84ea70 2015-10-28 stephen.d if _, err := commander.session.Walk(commander.ctx, commander.rootfid, commander.nextfid); err != nil {
97 6b84ea70 2015-10-28 stephen.d log.Fatalln(err)
99 6b84ea70 2015-10-28 stephen.d commander.pwdfid = commander.nextfid
100 6b84ea70 2015-10-28 stephen.d commander.nextfid++
103 6b84ea70 2015-10-28 stephen.d commander.readline.SetPrompt(fmt.Sprintf("%s 🐳 > ", commander.pwd))
105 6b84ea70 2015-10-28 stephen.d line, err := rl.Readline()
106 6b84ea70 2015-10-28 stephen.d if err != nil {
107 6b84ea70 2015-10-28 stephen.d log.Fatalln("error: ", err)
110 6b84ea70 2015-10-28 stephen.d if line == "" {
111 6b84ea70 2015-10-28 stephen.d continue
114 6b84ea70 2015-10-28 stephen.d args := strings.Fields(line)
116 6b84ea70 2015-10-28 stephen.d name := args[0]
117 e6bcde66 2015-10-29 stephen.d var cmd func(ctx context.Context, args ...string) error
119 6b84ea70 2015-10-28 stephen.d switch name {
120 6b84ea70 2015-10-28 stephen.d case "ls":
121 6b84ea70 2015-10-28 stephen.d cmd = commander.cmdls
122 6b84ea70 2015-10-28 stephen.d case "cd":
123 6b84ea70 2015-10-28 stephen.d cmd = commander.cmdcd
124 6b84ea70 2015-10-28 stephen.d case "pwd":
125 6b84ea70 2015-10-28 stephen.d cmd = commander.cmdpwd
126 6b84ea70 2015-10-28 stephen.d case "cat":
127 6b84ea70 2015-10-28 stephen.d cmd = commander.cmdcat
128 74ec7ac9 2015-10-30 stephen.d case "stat":
129 74ec7ac9 2015-10-30 stephen.d cmd = commander.cmdstat
130 6b84ea70 2015-10-28 stephen.d default:
131 e6bcde66 2015-10-29 stephen.d cmd = func(ctx context.Context, args ...string) error {
132 6b84ea70 2015-10-28 stephen.d return fmt.Errorf("command not implemented")
136 fb37ce2a 2015-10-30 stephen.d ctx, _ = context.WithTimeout(commander.ctx, 5*time.Second)
137 e6bcde66 2015-10-29 stephen.d if err := cmd(ctx, args[1:]...); err != nil {
138 b714e9e0 2015-11-05 stephen.d if err == p9pnew.ErrClosed {
139 b714e9e0 2015-11-05 stephen.d log.Println("connection closed, shutting down")
140 b714e9e0 2015-11-05 stephen.d return
143 6b84ea70 2015-10-28 stephen.d log.Printf("👹 %s: %v", name, err)
148 6b84ea70 2015-10-28 stephen.d type fsCommander struct {
149 6b84ea70 2015-10-28 stephen.d ctx context.Context
150 6b84ea70 2015-10-28 stephen.d session p9pnew.Session
151 6b84ea70 2015-10-28 stephen.d pwd string
152 6b84ea70 2015-10-28 stephen.d pwdfid p9pnew.Fid
153 6b84ea70 2015-10-28 stephen.d rootfid p9pnew.Fid
155 6b84ea70 2015-10-28 stephen.d nextfid p9pnew.Fid
157 6b84ea70 2015-10-28 stephen.d readline *readline.Instance
158 6b84ea70 2015-10-28 stephen.d stdout io.Writer
159 6b84ea70 2015-10-28 stephen.d stderr io.Writer
162 e6bcde66 2015-10-29 stephen.d func (c *fsCommander) cmdls(ctx context.Context, args ...string) error {
163 6b84ea70 2015-10-28 stephen.d ps := []string{c.pwd}
164 6b84ea70 2015-10-28 stephen.d if len(args) > 0 {
165 6b84ea70 2015-10-28 stephen.d ps = args
168 6b84ea70 2015-10-28 stephen.d wr := tabwriter.NewWriter(c.stdout, 0, 8, 8, ' ', 0)
170 6b84ea70 2015-10-28 stephen.d for _, p := range ps {
171 6b84ea70 2015-10-28 stephen.d // create a header if have more than one path.
172 6b84ea70 2015-10-28 stephen.d if len(ps) > 1 {
173 6b84ea70 2015-10-28 stephen.d fmt.Fprintln(wr, p+":")
176 6b84ea70 2015-10-28 stephen.d if !path.IsAbs(p) {
177 6b84ea70 2015-10-28 stephen.d p = path.Join(c.pwd, p)
180 6b84ea70 2015-10-28 stephen.d targetfid := c.nextfid
181 6b84ea70 2015-10-28 stephen.d c.nextfid++
182 6b84ea70 2015-10-28 stephen.d components := strings.Split(strings.Trim(p, "/"), "/")
183 e6bcde66 2015-10-29 stephen.d if _, err := c.session.Walk(ctx, c.rootfid, targetfid, components...); err != nil {
184 6b84ea70 2015-10-28 stephen.d return err
186 e6bcde66 2015-10-29 stephen.d defer c.session.Clunk(ctx, targetfid)
188 74ec7ac9 2015-10-30 stephen.d _, iounit, err := c.session.Open(ctx, targetfid, p9pnew.OREAD)
189 fb37ce2a 2015-10-30 stephen.d if err != nil {
190 6b84ea70 2015-10-28 stephen.d return err
193 a8abc687 2015-10-30 stephen.d if iounit < 1 {
194 a8abc687 2015-10-30 stephen.d msize, _ := c.session.Version()
195 deb98ab4 2015-11-05 stephen.d iounit = uint32(msize - 24) // size of message max minus fcall io header (Rread)
198 a8abc687 2015-10-30 stephen.d p := make([]byte, iounit)
200 e6bcde66 2015-10-29 stephen.d n, err := c.session.Read(ctx, targetfid, p, 0)
201 6b84ea70 2015-10-28 stephen.d if err != nil {
202 6b84ea70 2015-10-28 stephen.d return err
205 6b84ea70 2015-10-28 stephen.d rd := bytes.NewReader(p[:n])
206 deb98ab4 2015-11-05 stephen.d codec := p9pnew.NewCodec() // TODO(stevvooe): Need way to resolve codec based on session.
208 6b84ea70 2015-10-28 stephen.d var d p9pnew.Dir
209 deb98ab4 2015-11-05 stephen.d if err := p9pnew.DecodeDir(codec, rd, &d); err != nil {
210 6b84ea70 2015-10-28 stephen.d if err == io.EOF {
214 6b84ea70 2015-10-28 stephen.d return err
217 6b84ea70 2015-10-28 stephen.d fmt.Fprintf(wr, "%v\t%v\t%v\t%s\n", os.FileMode(d.Mode), d.Length, d.ModTime, d.Name)
220 6b84ea70 2015-10-28 stephen.d if len(ps) > 1 {
221 6b84ea70 2015-10-28 stephen.d fmt.Fprintln(wr, "")
225 6b84ea70 2015-10-28 stephen.d // all output is dumped only after success.
226 6b84ea70 2015-10-28 stephen.d return wr.Flush()
229 e6bcde66 2015-10-29 stephen.d func (c *fsCommander) cmdcd(ctx context.Context, args ...string) error {
230 6b84ea70 2015-10-28 stephen.d var p string
231 6b84ea70 2015-10-28 stephen.d switch len(args) {
232 6b84ea70 2015-10-28 stephen.d case 0:
233 6b84ea70 2015-10-28 stephen.d p = "/"
234 6b84ea70 2015-10-28 stephen.d case 1:
235 6b84ea70 2015-10-28 stephen.d p = args[0]
236 6b84ea70 2015-10-28 stephen.d default:
237 6b84ea70 2015-10-28 stephen.d return fmt.Errorf("cd: invalid args: %v", args)
240 6b84ea70 2015-10-28 stephen.d if !path.IsAbs(p) {
241 6b84ea70 2015-10-28 stephen.d p = path.Join(c.pwd, p)
244 6b84ea70 2015-10-28 stephen.d targetfid := c.nextfid
245 6b84ea70 2015-10-28 stephen.d c.nextfid++
246 6b84ea70 2015-10-28 stephen.d components := strings.Split(strings.TrimSpace(strings.Trim(p, "/")), "/")
247 6b84ea70 2015-10-28 stephen.d if _, err := c.session.Walk(c.ctx, c.rootfid, targetfid, components...); err != nil {
248 6b84ea70 2015-10-28 stephen.d return err
250 6b84ea70 2015-10-28 stephen.d defer c.session.Clunk(c.ctx, c.pwdfid)
252 6b84ea70 2015-10-28 stephen.d log.Println("cd", p, targetfid)
253 6b84ea70 2015-10-28 stephen.d c.pwd = p
254 6b84ea70 2015-10-28 stephen.d c.pwdfid = targetfid
256 6b84ea70 2015-10-28 stephen.d return nil
259 74ec7ac9 2015-10-30 stephen.d func (c *fsCommander) cmdstat(ctx context.Context, args ...string) error {
260 74ec7ac9 2015-10-30 stephen.d ps := []string{c.pwd}
261 74ec7ac9 2015-10-30 stephen.d if len(args) > 0 {
262 74ec7ac9 2015-10-30 stephen.d ps = args
265 74ec7ac9 2015-10-30 stephen.d wr := tabwriter.NewWriter(c.stdout, 0, 8, 8, ' ', 0)
267 74ec7ac9 2015-10-30 stephen.d for _, p := range ps {
268 74ec7ac9 2015-10-30 stephen.d targetfid := c.nextfid
269 74ec7ac9 2015-10-30 stephen.d c.nextfid++
270 74ec7ac9 2015-10-30 stephen.d components := strings.Split(strings.Trim(p, "/"), "/")
271 74ec7ac9 2015-10-30 stephen.d if _, err := c.session.Walk(ctx, c.rootfid, targetfid, components...); err != nil {
272 74ec7ac9 2015-10-30 stephen.d return err
274 74ec7ac9 2015-10-30 stephen.d defer c.session.Clunk(ctx, targetfid)
276 74ec7ac9 2015-10-30 stephen.d d, err := c.session.Stat(ctx, targetfid)
277 74ec7ac9 2015-10-30 stephen.d if err != nil {
278 74ec7ac9 2015-10-30 stephen.d return err
281 74ec7ac9 2015-10-30 stephen.d fmt.Fprintf(wr, "%v\t%v\t%v\t%s\n", os.FileMode(d.Mode), d.Length, d.ModTime, d.Name)
284 74ec7ac9 2015-10-30 stephen.d return wr.Flush()
287 e6bcde66 2015-10-29 stephen.d func (c *fsCommander) cmdpwd(ctx context.Context, args ...string) error {
288 6b84ea70 2015-10-28 stephen.d if len(args) != 0 {
289 6b84ea70 2015-10-28 stephen.d return fmt.Errorf("pwd takes no arguments")
292 6b84ea70 2015-10-28 stephen.d fmt.Println(c.pwd)
293 6b84ea70 2015-10-28 stephen.d return nil
296 e6bcde66 2015-10-29 stephen.d func (c *fsCommander) cmdcat(ctx context.Context, args ...string) error {
297 6b84ea70 2015-10-28 stephen.d var p string
298 6b84ea70 2015-10-28 stephen.d switch len(args) {
299 6b84ea70 2015-10-28 stephen.d case 0:
300 6b84ea70 2015-10-28 stephen.d p = "/"
301 6b84ea70 2015-10-28 stephen.d case 1:
302 6b84ea70 2015-10-28 stephen.d p = args[0]
303 6b84ea70 2015-10-28 stephen.d default:
304 6b84ea70 2015-10-28 stephen.d return fmt.Errorf("cd: invalid args: %v", args)
307 6b84ea70 2015-10-28 stephen.d if !path.IsAbs(p) {
308 6b84ea70 2015-10-28 stephen.d p = path.Join(c.pwd, p)
311 6b84ea70 2015-10-28 stephen.d targetfid := c.nextfid
312 6b84ea70 2015-10-28 stephen.d c.nextfid++
313 6b84ea70 2015-10-28 stephen.d components := strings.Split(strings.TrimSpace(strings.Trim(p, "/")), "/")
314 a8abc687 2015-10-30 stephen.d if _, err := c.session.Walk(ctx, c.rootfid, targetfid, components...); err != nil {
315 6b84ea70 2015-10-28 stephen.d return err
317 a8abc687 2015-10-30 stephen.d defer c.session.Clunk(ctx, c.pwdfid)
319 9d3499d9 2015-10-30 stephen.d _, iounit, err := c.session.Open(ctx, targetfid, p9pnew.OREAD)
320 e6bcde66 2015-10-29 stephen.d if err != nil {
321 6b84ea70 2015-10-28 stephen.d return err
324 9d3499d9 2015-10-30 stephen.d if iounit < 1 {
325 9d3499d9 2015-10-30 stephen.d msize, _ := c.session.Version()
326 deb98ab4 2015-11-05 stephen.d iounit = uint32(msize - 24) // size of message max minus fcall io header (Rread)
329 9d3499d9 2015-10-30 stephen.d b := make([]byte, iounit)
331 a8abc687 2015-10-30 stephen.d n, err := c.session.Read(ctx, targetfid, b, 0)
332 6b84ea70 2015-10-28 stephen.d if err != nil {
333 6b84ea70 2015-10-28 stephen.d return err
336 6b84ea70 2015-10-28 stephen.d if _, err := os.Stdout.Write(b[:n]); err != nil {
337 6b84ea70 2015-10-28 stephen.d return err
340 6b84ea70 2015-10-28 stephen.d os.Stdout.Write([]byte("\n"))
342 6b84ea70 2015-10-28 stephen.d return nil