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.
34 write_hdr(uint32_t len, uint8_t type, uint16_t tag)
38 log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
39 len, type, pp_msg_type(type), tag);
42 /* type is one byte, no endiannes issues */
45 evbuffer_add(evb, &len, sizeof(len));
46 evbuffer_add(evb, &type, sizeof(type));
47 evbuffer_add(evb, &tag, sizeof(tag));
51 write_hdr_auto(uint32_t len, uint8_t type)
53 if (++iota_tag == NOTAG)
55 write_hdr(len, type, iota_tag);
59 write_str(uint16_t len, const char *str)
64 evbuffer_add(evb, &len, sizeof(len));
65 evbuffer_add(evb, str, l);
69 write_str_auto(const char *str)
74 write_str(strlen(str), str);
78 write_buf(const void *d, uint32_t len)
81 evbuffer_add(evb, d, len);
88 evbuffer_add(evb, &x, sizeof(x));
92 write_32(uint32_t fid)
95 evbuffer_add(evb, &fid, sizeof(fid));
99 write_16(uint16_t tag)
102 evbuffer_add(evb, &tag, sizeof(tag));
108 evbuffer_add(evb, &x, sizeof(x));
114 tversion(const char *v, uint32_t msize)
121 /* msize[4] version[s] */
122 len = sizeof(msize) + sizeof(sl) + sl;
123 write_hdr(len, Tversion, NOTAG);
129 tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
137 /* fid[4] afid[4] uname[s] aname[s] */
138 len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
140 write_hdr_auto(len, Tattach);
143 write_str(ul, uname);
144 write_str(al, aname);
154 write_hdr_auto(len, Tclunk);
159 tflush(uint16_t oldtag)
164 len = sizeof(oldtag);
165 write_hdr_auto(len, Tflush);
170 twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
175 /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
176 len = sizeof(fid) + sizeof(newfid) + 2;
177 for (i = 0; i < nwname; ++i)
178 len += 2 + strlen(wnames[i]);
180 write_hdr_auto(len, Twalk);
184 for (i = 0; i < nwname; ++i)
185 write_str_auto(wnames[i]);
189 topen(uint32_t fid, uint8_t mode)
194 len = sizeof(fid) + sizeof(mode);
195 write_hdr_auto(len, Topen);
201 tcreate(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
206 /* fid[4] name[s] perm[4] mode[1] */
208 len = sizeof(fid) + sizeof(nl) + nl + sizeof(perm) + sizeof(mode);
209 write_hdr_auto(len, Tcreate);
217 tread(uint32_t fid, uint64_t off, uint32_t count)
221 /* fid[4] off[8] count[4] */
222 len = sizeof(fid) + sizeof(off) + sizeof(count);
223 write_hdr_auto(len, Tread);
230 twrite(uint32_t fid, uint64_t off, const void *data, uint32_t count)
234 /* fid[4] off[8] count[4] data[count] */
235 len = sizeof(fid) + sizeof(off) + sizeof(count) + count;
236 write_hdr_auto(len, Twrite);
239 write_buf(data, count);
246 write_hdr_auto(sizeof(fid), Tstat);
251 twstat(uint32_t fid, const struct np_stat *st)
258 stlen = NPSTATSIZ(0, 0, 0, 0);
259 if (st->name != NULL)
260 stlen += strlen(st->name);
262 stlen += strlen(st->uid);
264 stlen += strlen(st->gid);
265 if (st->muid != NULL)
266 stlen += strlen(st->muid);
268 len = sizeof(fid) + sizeof(stlen) + stlen;
270 write_hdr_auto(len, Twstat);
276 write_8(st->qid.type);
277 write_32(st->qid.vers);
278 write_64(st->qid.path);
283 write_64(st->length);
285 write_str_auto(st->name);
286 write_str_auto(st->uid);
287 write_str_auto(st->gid);
288 write_str_auto(st->muid);
292 tremove(uint32_t fid)
295 write_hdr_auto(sizeof(fid), Tremove);