Blame


1 4b33cdd0 2015-11-30 stephen.d package main
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import (
4 c55b00ed 2021-12-31 op "context"
5 4b33cdd0 2015-11-30 stephen.d "flag"
6 4b33cdd0 2015-11-30 stephen.d "log"
7 4b33cdd0 2015-11-30 stephen.d "net"
8 4b33cdd0 2015-11-30 stephen.d "strings"
9 4b33cdd0 2015-11-30 stephen.d
10 c55b00ed 2021-12-31 op "git.omarpolo.com/go-p9p"
11 c55b00ed 2021-12-31 op "git.omarpolo.com/go-p9p/ufs"
12 4b33cdd0 2015-11-30 stephen.d )
13 4b33cdd0 2015-11-30 stephen.d
14 4b33cdd0 2015-11-30 stephen.d var (
15 4b33cdd0 2015-11-30 stephen.d root string
16 4b33cdd0 2015-11-30 stephen.d addr string
17 4b33cdd0 2015-11-30 stephen.d )
18 4b33cdd0 2015-11-30 stephen.d
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")
22 4b33cdd0 2015-11-30 stephen.d }
23 4b33cdd0 2015-11-30 stephen.d
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()
28 4b33cdd0 2015-11-30 stephen.d
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:]
33 4b33cdd0 2015-11-30 stephen.d }
34 4b33cdd0 2015-11-30 stephen.d
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)
38 4b33cdd0 2015-11-30 stephen.d }
39 4b33cdd0 2015-11-30 stephen.d defer listener.Close()
40 4b33cdd0 2015-11-30 stephen.d
41 4b33cdd0 2015-11-30 stephen.d for {
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
46 4b33cdd0 2015-11-30 stephen.d }
47 4b33cdd0 2015-11-30 stephen.d
48 4b33cdd0 2015-11-30 stephen.d go func(conn net.Conn) {
49 4b33cdd0 2015-11-30 stephen.d defer conn.Close()
50 4b33cdd0 2015-11-30 stephen.d
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")
56 4b33cdd0 2015-11-30 stephen.d return
57 4b33cdd0 2015-11-30 stephen.d }
58 4b33cdd0 2015-11-30 stephen.d
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)
61 4b33cdd0 2015-11-30 stephen.d }
62 4b33cdd0 2015-11-30 stephen.d }(c)
63 4b33cdd0 2015-11-30 stephen.d }
64 4b33cdd0 2015-11-30 stephen.d }