-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclnt_tag.go
235 lines (194 loc) · 4.16 KB
/
clnt_tag.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 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
type Tag struct {
clnt *Clnt
tag uint16
reqchan chan *Req
respchan chan *Req
donechan chan bool
}
func (clnt *Clnt) TagAlloc(reqchan chan *Req) *Tag {
tag := new(Tag)
tag.clnt = clnt
tag.tag = uint16(clnt.tagpool.getId())
tag.reqchan = reqchan
tag.respchan = make(chan *Req, 16)
tag.donechan = make(chan bool)
go tag.reqproc()
return tag
}
func (clnt *Clnt) TagFree(tag *Tag) {
tag.donechan <- true
clnt.tagpool.putId(uint32(tag.tag))
}
func (tag *Tag) reqAlloc() *Req {
r := new(Req)
r.tag = tag.tag
r.Clnt = tag.clnt
r.Done = tag.respchan
r.Tc = tag.clnt.NewFcall()
return r
}
func (tag *Tag) ReqFree(r *Req) {
tag.clnt.FreeFcall(r.Tc)
}
func (tag *Tag) reqproc() {
for {
select {
case <-tag.donechan:
return
case r := <-tag.respchan:
rc := r.Rc
fid := r.fid
err := r.Rc.Type == Rerror
switch r.Tc.Type {
case Tauth:
if err {
fid.User = nil
}
case Tattach:
if !err {
fid.Qid = rc.Qid
} else {
fid.User = nil
}
case Twalk:
if !err {
fid.walked = true
if len(rc.Wqid) > 0 {
fid.Qid = rc.Wqid[len(rc.Wqid)-1]
}
} else {
fid.User = nil
}
case Topen:
case Tcreate:
if !err {
fid.Iounit = rc.Iounit
fid.Qid = rc.Qid
} else {
fid.Mode = 0
}
case Tclunk:
case Tremove:
tag.clnt.fidpool.putId(fid.Fid)
}
tag.reqchan <- r
}
}
}
func (tag *Tag) Auth(afid *Fid, user User, aname string) error {
req := tag.reqAlloc()
req.fid = afid
err := PackTauth(req.Tc, afid.Fid, user.Name(), aname, uint32(user.Id()), tag.clnt.Dotu)
if err != nil {
return err
}
afid.User = user
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Attach(fid, afid *Fid, user User, aname string) error {
var afno uint32
if afid != nil {
afno = afid.Fid
} else {
afno = NOFID
}
req := tag.reqAlloc()
req.fid = fid
err := PackTattach(req.Tc, fid.Fid, afno, user.Name(), aname, uint32(user.Id()), tag.clnt.Dotu)
if err != nil {
return err
}
fid.User = user
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Walk(fid *Fid, newfid *Fid, wnames []string) error {
req := tag.reqAlloc()
req.fid = newfid
if len(wnames) == 0 {
newfid.Qid = fid.Qid
}
err := PackTwalk(req.Tc, fid.Fid, newfid.Fid, wnames)
if err != nil {
return err
}
newfid.User = fid.User
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Open(fid *Fid, mode uint8) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTopen(req.Tc, fid.Fid, mode)
if err != nil {
return err
}
fid.Mode = mode
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Create(fid *Fid, name string, perm uint32, mode uint8, ext string) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTcreate(req.Tc, fid.Fid, name, perm, mode, ext, tag.clnt.Dotu)
if err != nil {
return err
}
fid.Mode = mode
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Read(fid *Fid, offset uint64, count uint32) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTread(req.Tc, fid.Fid, offset, count)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Write(fid *Fid, data []byte, offset uint64) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTwrite(req.Tc, fid.Fid, offset, uint32(len(data)), data)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Clunk(fid *Fid) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTclunk(req.Tc, fid.Fid)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Remove(fid *Fid) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTremove(req.Tc, fid.Fid)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Stat(fid *Fid) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTstat(req.Tc, fid.Fid)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}
func (tag *Tag) Wstat(fid *Fid, dir *Dir) error {
req := tag.reqAlloc()
req.fid = fid
err := PackTwstat(req.Tc, fid.Fid, dir, tag.clnt.Dotu)
if err != nil {
return err
}
return tag.clnt.Rpcnb(req)
}