forked from lionkov/ninep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackt.go
209 lines (169 loc) · 5.71 KB
/
packt.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
// 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
import "bytes"
// Create a Tversion message in the specified Fcall.
func PackTversion(fc *Fcall, msize uint32, version string) error {
b, err := msgEncode(Tversion, NOTAG, Count(msize), version)
if err != nil {
return err
}
// I'm leaving this annoying little line in each function because I think I can
// kill it later.
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tversion, NOTAG
return nil
}
// Create a Tauth message in the specified Fcall.
// TODO: change fid type to FID
func PackTauth(fc *Fcall, fid uint32, uname string, aname string, unamenum uint32, dotu bool) error {
var b *bytes.Buffer
var err error
if !dotu {
b, err = msgEncode(Tauth, NOTAG, FID(fid), uname, aname)
} else {
b, err = msgEncode(Tauth, NOTAG, FID(fid), uname, aname, unamenum)
}
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tauth, NOTAG
fc.Fid, fc.Uname, fc.Aname = fid, uname, aname
return nil
}
// Create a Tflush message in the specified Fcall.
func PackTflush(fc *Fcall, oldtag uint16) error {
b, err := msgEncode(Tflush, NOTAG, Tag(oldtag))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tflush, NOTAG
fc.Oldtag = oldtag
return nil
}
// Create a Tattach message in the specified Fcall. If dotu is true,
// the function will create 9P2000.u including the nuname value, otherwise
// nuname is ignored.
// NOTE: dotu is going away. We hope.
func PackTattach(fc *Fcall, fid uint32, afid uint32, uname string, aname string, unamenum uint32, dotu bool) error {
var b *bytes.Buffer
var err error
if !dotu {
b, err = msgEncode(Tattach, NOTAG, FID(fid), FID(afid), uname, aname)
} else {
b, err = msgEncode(Tattach, NOTAG, FID(fid), FID(afid), uname, aname, unamenum)
}
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tattach, NOTAG
fc.Fid, fc.Afid, fc.Uname, fc.Aname, fc.Unamenum = fid, afid, uname, aname, unamenum
return nil
}
// Create a Twalk message in the specified Fcall.
func PackTwalk(fc *Fcall, fid uint32, newfid uint32, wnames []string) error {
b, err := msgEncode(Twalk, NOTAG, FID(fid), FID(newfid), NumEntries(len(wnames)), wnames)
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Twalk, NOTAG
fc.Fid, fc.Newfid = fid, newfid
return nil
}
// Create a Topen message in the specified Fcall.
func PackTopen(fc *Fcall, fid uint32, mode uint8) error {
b, err := msgEncode(Topen, NOTAG, FID(fid), Mode(mode))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Topen, NOTAG
fc.Fid, fc.Mode = fid, mode
return nil
}
// Create a Tcreate message in the specified Fcall. If dotu is true,
// the function will create a 9P2000.u message that includes ext.
// Otherwise the ext value is ignored.
func PackTcreate(fc *Fcall, fid uint32, name string, perm uint32, mode uint8, ext string, dotu bool) error {
var err error
var b *bytes.Buffer
if ! dotu {
b, err = msgEncode(Tcreate, NOTAG, FID(fid), name, Perm(perm), Mode(mode))
} else {
b, err = msgEncode(Tcreate, NOTAG, FID(fid), name, Perm(perm), Mode(mode), ext)
}
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tcreate, NOTAG
fc.Fid, fc.Name, fc.Perm, fc.Mode = fid, name, perm, mode
return nil
}
// Create a Tread message in the specified Fcall.
func PackTread(fc *Fcall, fid uint32, offset uint64, count uint32) error {
b, err := msgEncode(Tread, NOTAG, FID(fid), Offset(offset), Count(count))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tread, NOTAG
fc.Fid, fc.Offset, fc.Count = fid, offset, count
return nil
}
// Create a Twrite message in the specified Fcall.
func PackTwrite(fc *Fcall, fid uint32, offset uint64, count uint32, data []byte) error {
b, err := msgEncode(Twrite, NOTAG, FID(fid), Offset(offset), Count(count), WriteData(data))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Twrite, NOTAG
fc.Fid, fc.Offset, fc.Count, fc.Data = fid, offset, count, data
return nil
}
// Create a Tclunk message in the specified Fcall.
func PackTclunk(fc *Fcall, fid uint32) error {
b, err := msgEncode(Tclunk, NOTAG, FID(fid))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tclunk, NOTAG
fc.Fid = fid
return nil
}
// Create a Tremove message in the specified Fcall.
func PackTremove(fc *Fcall, fid uint32) error {
b, err := msgEncode(Tremove, NOTAG, FID(fid))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tremove, NOTAG
fc.Fid = fid
return nil
}
// Create a Tstat message in the specified Fcall.
func PackTstat(fc *Fcall, fid uint32) error {
b, err := msgEncode(Tstat, NOTAG, FID(fid))
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Tstat, NOTAG
fc.Fid = fid
return nil
}
// Create a Twstat message in the specified Fcall. If dotu is true
// the function will create 9P2000.u message, otherwise the 9P2000.u
// specific fields from the Stat value will be ignored.
func PackTwstat(fc *Fcall, fid uint32, d *Dir, dotu bool) error {
var b *bytes.Buffer
var err error
if !dotu {
b, err = msgEncode(Twstat, NOTAG, FID(fid), d)
} else {
var du = DirDotu(*d)
b, err = msgEncode(Twstat, NOTAG, FID(fid), &du)
}
if err != nil {
return err
}
fc.Pkt, fc.Size, fc.Type, fc.Tag = b.Bytes(), uint32(b.Len()), Twstat, NOTAG
fc.Fid, fc.Dir = fid, *d
return nil
}