-
-
Save djdv/682b9d9fd8a58c43d9320dc220a90705 to your computer and use it in GitHub Desktop.
Walk qids thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"github.com/hugelgupf/p9/fsimpl/templatefs" | |
"github.com/hugelgupf/p9/p9" | |
) | |
type ( | |
root struct { | |
compliant bool | |
} | |
dir struct { | |
templatefs.NoopFile | |
compliant bool | |
} | |
) | |
func main() { | |
root, client := setup() | |
fmt.Println("spec compliance `false`:") | |
run(root, client) | |
fmt.Println("\nspec compliance `true`:") | |
root.compliant = true | |
run(root, client) | |
} | |
func setup() (*root, *p9.Client) { | |
var r root | |
l, err := net.Listen("tcp", "127.0.0.1:0") | |
mustNot(err) | |
go func() { | |
srv := p9.NewServer(&r) | |
srv.Serve(l) | |
}() | |
addr := l.Addr() | |
conn, err := net.Dial(addr.Network(), addr.String()) | |
mustNot(err) | |
client, err := p9.NewClient(conn) | |
mustNot(err) | |
return &r, client | |
} | |
func run(r *root, client *p9.Client) { | |
var ( | |
directDir, _ = r.Attach() | |
clientDir, _ = client.Attach("") | |
) | |
if callerQids, _, err := directDir.Walk(nil); err != nil { | |
fmt.Println("direct error:", err) | |
} else { | |
fmt.Println("direct call return: ", callerQids) | |
} | |
if clientQids, _, err := clientDir.Walk(nil); err != nil { | |
fmt.Println("client error:", err) | |
} else { | |
fmt.Println("client response: ", clientQids) | |
} | |
} | |
func (r *root) Attach() (p9.File, error) { | |
return &dir{compliant: r.compliant}, nil | |
} | |
func (d *dir) Walk([]string) ([]p9.QID, p9.File, error) { | |
if d.compliant { | |
return nil, d, nil | |
} | |
qid := p9.QID{ | |
Type: p9.TypeDir, | |
Path: 1, | |
} | |
return []p9.QID{qid}, d, nil | |
} | |
func (d *dir) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) { | |
qid := p9.QID{ | |
Type: p9.TypeDir, | |
Path: 1, | |
} | |
return qid, req, p9.Attr{ | |
Mode: p9.ModeDirectory, | |
}, nil | |
} | |
func mustNot(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment