commit f41196dcf2b4d9e98d8e2addb0cd403e5405a3f0 from: Stephen J Day date: Fri Oct 30 23:27:39 2015 UTC fs/p9p/new: initial implementation of 9ps To begin testing the server implementation, we defined a 9ps cmd. This simply proxies a client connection to a running ufs server but should let us develop a prototype without implementing all of ufs. Signed-off-by: Stephen J Day commit - 9d3499d9f33f98776fe06479a43da5eafaea2e7f commit + f41196dcf2b4d9e98d8e2addb0cd403e5405a3f0 blob - /dev/null blob + 7818604df1a25843f729bd82b77a66aa08051e81 (mode 644) --- /dev/null +++ cmd/9ps/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "log" + "net" + "strings" + + "github.com/docker/pinata/v1/fs/p9p/new" + "golang.org/x/net/context" +) + +var ( + root string + addr string +) + +func init() { + flag.StringVar(&root, "root", "~/", "root of filesystem to serve over 9p") + flag.StringVar(&addr, "addr", ":5640", "bind addr for 9p server, prefix with unix: for unix socket") +} + +func main() { + ctx := context.Background() + log.SetFlags(0) + flag.Parse() + + proto := "tcp" + if strings.HasPrefix(addr, "unix:") { + proto = "unix" + addr = addr[5:] + } + + listener, err := net.Listen(proto, addr) + if err != nil { + log.Fatalln("error listening:", err) + } + defer listener.Close() + + for { + c, err := listener.Accept() + if err != nil { + log.Fatalln("error accepting:", err) + continue + } + + go func(conn net.Conn) { + ctx := context.WithValue(ctx, "conn", conn) + log.Println("connected", conn.RemoteAddr()) + session, err := newLocalSession(ctx, root) + if err != nil { + log.Println("error creating session") + return + } + + p9pnew.Serve(ctx, conn, session) + }(c) + } +} + +// newLocalSession returns a session to serve the local filesystem, restricted +// to the provided root. +func newLocalSession(ctx context.Context, root string) (p9pnew.Session, error) { + // silly, just connect to ufs for now! replace this with real code later! + log.Println("dialing", ":5640", "for", ctx.Value("conn")) + conn, err := net.Dial("tcp", ":5640") + if err != nil { + return nil, err + } + + session, err := p9pnew.NewSession(ctx, conn) + if err != nil { + return nil, err + } + + return session, nil +}