-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclnt_mount.go
93 lines (77 loc) · 2 KB
/
clnt_mount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2009 The Go9p Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package go9p
import (
"net"
)
// Creates an authentication fid for the specified user. Returns the fid, if
// successful, or an Error.
func (clnt *Clnt) Auth(user User, aname string) (*Fid, error) {
fid := clnt.FidAlloc()
tc := clnt.NewFcall()
err := PackTauth(tc, fid.Fid, user.Name(), aname, uint32(user.Id()), clnt.Dotu)
if err != nil {
return nil, err
}
_, err = clnt.Rpc(tc)
if err != nil {
return nil, err
}
fid.User = user
fid.walked = true
return fid, nil
}
// Creates a fid for the specified user that points to the root
// of the file server's file tree. Returns a Fid pointing to the root,
// if successful, or an Error.
func (clnt *Clnt) Attach(afid *Fid, user User, aname string) (*Fid, error) {
var afno uint32
if afid != nil {
afno = afid.Fid
} else {
afno = NOFID
}
fid := clnt.FidAlloc()
tc := clnt.NewFcall()
err := PackTattach(tc, fid.Fid, afno, user.Name(), aname, uint32(user.Id()), clnt.Dotu)
if err != nil {
return nil, err
}
rc, err := clnt.Rpc(tc)
if err != nil {
return nil, err
}
fid.Qid = rc.Qid
fid.User = user
fid.walked = true
return fid, nil
}
// Connects to a file server and attaches to it as the specified user.
func Mount(ntype, addr, aname string, msize uint32, user User) (*Clnt, error) {
c, e := net.Dial(ntype, addr)
if e != nil {
return nil, &Error{e.Error(), EIO}
}
return MountConn(c, aname, msize, user)
}
func MountConn(c net.Conn, aname string, msize uint32, user User) (*Clnt, error) {
clnt, err := Connect(c, msize+IOHDRSZ, true)
if err != nil {
return nil, err
}
fid, err := clnt.Attach(nil, user, aname)
if err != nil {
clnt.Unmount()
return nil, err
}
clnt.Root = fid
return clnt, nil
}
// Closes the connection to the file sever.
func (clnt *Clnt) Unmount() {
clnt.Lock()
clnt.err = &Error{"connection closed", EIO}
clnt.conn.Close()
clnt.Unlock()
}