1 4b33cdd0 2015-11-30 stephen.d package main
3 4b33cdd0 2015-11-30 stephen.d import (
8 4b33cdd0 2015-11-30 stephen.d "strings"
10 c55b00ed 2021-12-31 op "git.omarpolo.com/go-p9p"
11 c55b00ed 2021-12-31 op "git.omarpolo.com/go-p9p/ufs"
15 4b33cdd0 2015-11-30 stephen.d root string
16 4b33cdd0 2015-11-30 stephen.d addr string
19 4b33cdd0 2015-11-30 stephen.d func init() {
20 4b33cdd0 2015-11-30 stephen.d flag.StringVar(&root, "root", "~/", "root of filesystem to serve over 9p")
21 4b33cdd0 2015-11-30 stephen.d flag.StringVar(&addr, "addr", ":5640", "bind addr for 9p server, prefix with unix: for unix socket")
24 4b33cdd0 2015-11-30 stephen.d func main() {
25 4b33cdd0 2015-11-30 stephen.d ctx := context.Background()
26 4b33cdd0 2015-11-30 stephen.d log.SetFlags(0)
27 4b33cdd0 2015-11-30 stephen.d flag.Parse()
29 4b33cdd0 2015-11-30 stephen.d proto := "tcp"
30 4b33cdd0 2015-11-30 stephen.d if strings.HasPrefix(addr, "unix:") {
31 4b33cdd0 2015-11-30 stephen.d proto = "unix"
32 4b33cdd0 2015-11-30 stephen.d addr = addr[5:]
35 4b33cdd0 2015-11-30 stephen.d listener, err := net.Listen(proto, addr)
36 4b33cdd0 2015-11-30 stephen.d if err != nil {
37 4b33cdd0 2015-11-30 stephen.d log.Fatalln("error listening:", err)
39 4b33cdd0 2015-11-30 stephen.d defer listener.Close()
42 4b33cdd0 2015-11-30 stephen.d c, err := listener.Accept()
43 4b33cdd0 2015-11-30 stephen.d if err != nil {
44 4b33cdd0 2015-11-30 stephen.d log.Fatalln("error accepting:", err)
45 4b33cdd0 2015-11-30 stephen.d continue
48 4b33cdd0 2015-11-30 stephen.d go func(conn net.Conn) {
49 4b33cdd0 2015-11-30 stephen.d defer conn.Close()
51 4b33cdd0 2015-11-30 stephen.d ctx := context.WithValue(ctx, "conn", conn)
52 4b33cdd0 2015-11-30 stephen.d log.Println("connected", conn.RemoteAddr())
53 64abb42a 2018-04-10 noreply session, err := ufs.NewSession(ctx, root)
54 4b33cdd0 2015-11-30 stephen.d if err != nil {
55 4b33cdd0 2015-11-30 stephen.d log.Println("error creating session")
59 4b33cdd0 2015-11-30 stephen.d if err := p9p.ServeConn(ctx, conn, p9p.Dispatch(session)); err != nil {
60 4b33cdd0 2015-11-30 stephen.d log.Printf("serving conn: %v", err)