2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 write_hdr(uint32_t len, uint8_t type, uint16_t tag)
37 log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
38 len, type, pp_msg_type(type), tag);
41 /* type is one byte, no endiannes issues */
44 evbuffer_add(evb, &len, sizeof(len));
45 evbuffer_add(evb, &type, sizeof(type));
46 evbuffer_add(evb, &tag, sizeof(tag));
50 write_hdr_auto(uint32_t len, uint8_t type)
52 if (++iota_tag == NOTAG)
54 write_hdr(len, type, iota_tag);
58 write_str(uint16_t len, const char *str)
63 evbuffer_add(evb, &len, sizeof(len));
64 evbuffer_add(evb, str, l);
68 write_str_auto(const char *str)
73 write_str(strlen(str), str);
77 write_buf(const void *d, uint32_t len)
80 evbuffer_add(evb, d, len);
87 evbuffer_add(evb, &x, sizeof(x));
91 write_32(uint32_t fid)
94 evbuffer_add(evb, &fid, sizeof(fid));
98 write_16(uint16_t tag)
101 evbuffer_add(evb, &tag, sizeof(tag));
107 evbuffer_add(evb, &x, sizeof(x));
113 tversion(const char *v, uint32_t msize)
120 /* msize[4] version[s] */
121 len = sizeof(msize) + sizeof(sl) + sl;
122 write_hdr(len, Tversion, NOTAG);
128 tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
136 /* fid[4] afid[4] uname[s] aname[s] */
137 len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
139 write_hdr_auto(len, Tattach);
142 write_str(ul, uname);
143 write_str(al, aname);
153 write_hdr_auto(len, Tclunk);
158 tflush(uint16_t oldtag)
163 len = sizeof(oldtag);
164 write_hdr_auto(len, Tflush);
169 twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
174 /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
175 len = sizeof(fid) + sizeof(newfid) + 2;
176 for (i = 0; i < nwname; ++i)
177 len += 2 + strlen(wnames[i]);
179 write_hdr_auto(len, Twalk);
183 for (i = 0; i < nwname; ++i)
184 write_str_auto(wnames[i]);
188 topen(uint32_t fid, uint8_t mode)
193 len = sizeof(fid) + sizeof(mode);
194 write_hdr_auto(len, Topen);
200 tcreate(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
205 /* fid[4] name[s] perm[4] mode[1] */
207 len = sizeof(fid) + sizeof(nl) + nl + sizeof(perm) + sizeof(mode);
208 write_hdr_auto(len, Tcreate);
216 tread(uint32_t fid, uint64_t off, uint32_t count)
220 /* fid[4] off[8] count[4] */
221 len = sizeof(fid) + sizeof(off) + sizeof(count);
222 write_hdr_auto(len, Tread);
229 twrite(uint32_t fid, uint64_t off, const void *data, uint32_t count)
233 /* fid[4] off[8] count[4] data[count] */
234 len = sizeof(fid) + sizeof(off) + sizeof(count) + count;
235 write_hdr_auto(len, Twrite);
238 write_buf(data, count);
245 write_hdr_auto(sizeof(fid), Tstat);
250 twstat(uint32_t fid, const struct np_stat *st)
257 stlen = NPSTATSIZ(0, 0, 0, 0);
258 if (st->name != NULL)
259 stlen += strlen(st->name);
261 stlen += strlen(st->uid);
263 stlen += strlen(st->gid);
264 if (st->muid != NULL)
265 stlen += strlen(st->muid);
267 n = sizeof(stlen) + stlen;
268 len = sizeof(fid) + sizeof(n) + n;
270 write_hdr_auto(len, Twstat);
277 write_8(st->qid.type);
278 write_32(st->qid.vers);
279 write_64(st->qid.path);
284 write_64(st->length);
286 write_str_auto(st->name);
287 write_str_auto(st->uid);
288 write_str_auto(st->gid);
289 write_str_auto(st->muid);
293 tremove(uint32_t fid)
296 write_hdr_auto(sizeof(fid), Tremove);