-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpackr.go
219 lines (185 loc) · 4.89 KB
/
packr.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
// Copyright 2009 The Ninep 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 ninep
// Create a Rversion message in the specified Fcall.
func PackRversion(fc *Fcall, msize uint32, version string) error {
size := 4 + 2 + len(version) /* msize[4] version[s] */
p, err := packCommon(fc, size, Rversion)
if err != nil {
return err
}
fc.Msize = msize
fc.Version = version
p = pint32(msize, p)
p = pstr(version, p)
return nil
}
// Create a Rauth message in the specified Fcall.
func PackRauth(fc *Fcall, aqid *Qid) error {
size := 13 /* aqid[13] */
p, err := packCommon(fc, size, Rauth)
if err != nil {
return err
}
fc.Qid = *aqid
p = pqid(aqid, p)
return nil
}
// Create a Rerror message in the specified Fcall. If dotu is true,
// the function will create a 9P2000.u message. If false, nerror is
// ignored.
func PackRerror(fc *Fcall, error string, errornum uint32, dotu bool) error {
size := 2 + len(error) /* ename[s] */
if dotu {
size += 4 /* ecode[4] */
}
p, err := packCommon(fc, size, Rerror)
if err != nil {
return err
}
fc.Error = error
p = pstr(error, p)
if dotu {
fc.Errornum = errornum
p = pint32(errornum, p)
}
return nil
}
// Create a Rflush message in the specified Fcall.
func PackRflush(fc *Fcall) error {
_, err := packCommon(fc, 0, Rflush)
return err
}
// Create a Rattach message in the specified Fcall.
func PackRattach(fc *Fcall, aqid *Qid) error {
size := 13 /* aqid[13] */
p, err := packCommon(fc, size, Rattach)
if err != nil {
return err
}
fc.Qid = *aqid
p = pqid(aqid, p)
return nil
}
// Create a Rwalk message in the specified Fcall.
func PackRwalk(fc *Fcall, wqids []Qid) error {
nwqid := len(wqids)
size := 2 + nwqid*13 /* nwqid[2] nwname*wqid[13] */
p, err := packCommon(fc, size, Rwalk)
if err != nil {
return err
}
p = pint16(uint16(nwqid), p)
fc.Wqid = make([]Qid, nwqid)
for i := 0; i < nwqid; i++ {
fc.Wqid[i] = wqids[i]
p = pqid(&wqids[i], p)
}
return nil
}
// Create a Ropen message in the specified Fcall.
func PackRopen(fc *Fcall, qid *Qid, iounit uint32) error {
size := 13 + 4 /* qid[13] iounit[4] */
p, err := packCommon(fc, size, Ropen)
if err != nil {
return err
}
fc.Qid = *qid
fc.Iounit = iounit
p = pqid(qid, p)
p = pint32(iounit, p)
return nil
}
// Create a Rcreate message in the specified Fcall.
func PackRcreate(fc *Fcall, qid *Qid, iounit uint32) error {
size := 13 + 4 /* qid[13] iounit[4] */
p, err := packCommon(fc, size, Rcreate)
if err != nil {
return err
}
fc.Qid = *qid
fc.Iounit = iounit
p = pqid(qid, p)
p = pint32(iounit, p)
return nil
}
// Initializes the specified Fcall value to contain Rread message.
// The user should copy the returned data to the slice pointed by
// fc.Data and call SetRreadCount to update the data size to the
// actual value.
func InitRread(fc *Fcall, count uint32) error {
size := int(4 + count) /* count[4] data[count] */
p, err := packCommon(fc, size, Rread)
if err != nil {
return err
}
fc.Count = count
fc.Data = p[4 : fc.Count+4]
p = pint32(count, p)
return nil
}
// Updates the size of the data returned by Rread. Expects that
// the Fcall value is already initialized by InitRread.
func SetRreadCount(fc *Fcall, count uint32) {
/* we need to update both the packet size as well as the data count */
size := 4 + 1 + 2 + 4 + count /* size[4] id[1] tag[2] count[4] data[count] */
pint32(size, fc.Pkt)
pint32(count, fc.Pkt[7:])
fc.Size = size
fc.Count = count
fc.Pkt = fc.Pkt[0:size]
fc.Data = fc.Data[0:count]
fc.Size = size
}
// Create a Rread message in the specified Fcall.
func PackRread(fc *Fcall, data []byte) error {
count := uint32(len(data))
err := InitRread(fc, count)
if err != nil {
return err
}
copy(fc.Data, data)
return nil
}
// Create a Rwrite message in the specified Fcall.
func PackRwrite(fc *Fcall, count uint32) error {
p, err := packCommon(fc, 4, Rwrite) /* count[4] */
if err != nil {
return err
}
fc.Count = count
p = pint32(count, p)
return nil
}
// Create a Rclunk message in the specified Fcall.
func PackRclunk(fc *Fcall) error {
_, err := packCommon(fc, 0, Rclunk)
return err
}
// Create a Rremove message in the specified Fcall.
func PackRremove(fc *Fcall) error {
_, err := packCommon(fc, 0, Rremove)
return err
}
// Create a Rstat message in the specified Fcall. If dotu is true, the
// function will create a 9P2000.u stat representation that includes
// st.Nuid, st.Ngid, st.Nmuid and st.Ext. Otherwise these values will be
// ignored.
func PackRstat(fc *Fcall, d *Dir, dotu bool) error {
stsz := statsz(d, dotu)
size := 2 + stsz /* stat[n] */
p, err := packCommon(fc, size, Rstat)
if err != nil {
return err
}
p = pint16(uint16(stsz), p)
p = pstat(d, p, dotu)
fc.Dir = *d
return nil
}
// Create a Rwstat message in the specified Fcall.
func PackRwstat(fc *Fcall) error {
_, err := packCommon(fc, 0, Rwstat)
return err
}