9p1lib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. #include "9p1.h"
  11. #define CHAR(x) *p++ = f->x
  12. #define SHORT(x) { uint32_t vvv = f->x; p[0] = vvv; p[1] = vvv>>8; p += 2; }
  13. #define VLONG(q) p[0] = (q); p[1] = (q)>>8; p[2] = (q)>>16; p[3] = (q)>>24; p += 4
  14. #define LONG(x) { uint32_t vvv = f->x; VLONG(vvv); }
  15. #define BYTES(x,n) memmove(p, f->x, n); p += n
  16. #define STRING(x,n) strncpy((char*)p, f->x, n); p += n
  17. int
  18. convS2M9p1(Oldfcall *f, uint8_t *ap)
  19. {
  20. uint8_t *p;
  21. int t;
  22. p = ap;
  23. CHAR(type);
  24. t = f->type;
  25. SHORT(tag);
  26. switch(t)
  27. {
  28. default:
  29. print("convS2M9p1: bad type: %d\n", t);
  30. return 0;
  31. case Tnop9p1:
  32. case Tosession9p1:
  33. break;
  34. case Tsession9p1:
  35. BYTES(chal, sizeof(f->chal));
  36. break;
  37. case Tflush9p1:
  38. SHORT(oldtag);
  39. break;
  40. case Tattach9p1:
  41. SHORT(fid);
  42. STRING(uname, sizeof(f->uname));
  43. STRING(aname, sizeof(f->aname));
  44. BYTES(ticket, sizeof(f->ticket));
  45. BYTES(auth, sizeof(f->auth));
  46. break;
  47. case Toattach9p1:
  48. SHORT(fid);
  49. STRING(uname, sizeof(f->uname));
  50. STRING(aname, sizeof(f->aname));
  51. BYTES(ticket, NAMELEN);
  52. break;
  53. case Tclone9p1:
  54. SHORT(fid);
  55. SHORT(newfid);
  56. break;
  57. case Twalk9p1:
  58. SHORT(fid);
  59. STRING(name, sizeof(f->name));
  60. break;
  61. case Tclwalk9p1:
  62. SHORT(fid);
  63. SHORT(newfid);
  64. STRING(name, sizeof(f->name));
  65. break;
  66. case Topen9p1:
  67. SHORT(fid);
  68. CHAR(mode);
  69. break;
  70. case Tcreate9p1:
  71. SHORT(fid);
  72. STRING(name, sizeof(f->name));
  73. LONG(perm);
  74. CHAR(mode);
  75. break;
  76. case Tread9p1:
  77. SHORT(fid);
  78. LONG(offset); VLONG(0);
  79. SHORT(count);
  80. break;
  81. case Twrite9p1:
  82. SHORT(fid);
  83. LONG(offset); VLONG(0);
  84. SHORT(count);
  85. p++;
  86. if((uint8_t*)p == (uint8_t*)f->data) {
  87. p += f->count;
  88. break;
  89. }
  90. BYTES(data, f->count);
  91. break;
  92. case Tclunk9p1:
  93. case Tremove9p1:
  94. case Tstat9p1:
  95. SHORT(fid);
  96. break;
  97. case Twstat9p1:
  98. SHORT(fid);
  99. BYTES(stat, sizeof(f->stat));
  100. break;
  101. /*
  102. */
  103. case Rnop9p1:
  104. case Rosession9p1:
  105. case Rflush9p1:
  106. break;
  107. case Rsession9p1:
  108. BYTES(chal, sizeof(f->chal));
  109. BYTES(authid, sizeof(f->authid));
  110. BYTES(authdom, sizeof(f->authdom));
  111. break;
  112. case Rerror9p1:
  113. STRING(ename, sizeof(f->ename));
  114. break;
  115. case Rclone9p1:
  116. case Rclunk9p1:
  117. case Rremove9p1:
  118. case Rwstat9p1:
  119. SHORT(fid);
  120. break;
  121. case Rwalk9p1:
  122. case Ropen9p1:
  123. case Rcreate9p1:
  124. case Rclwalk9p1:
  125. SHORT(fid);
  126. LONG(qid.path);
  127. LONG(qid.version);
  128. break;
  129. case Rattach9p1:
  130. SHORT(fid);
  131. LONG(qid.path);
  132. LONG(qid.version);
  133. BYTES(rauth, sizeof(f->rauth));
  134. break;
  135. case Roattach9p1:
  136. SHORT(fid);
  137. LONG(qid.path);
  138. LONG(qid.version);
  139. break;
  140. case Rread9p1:
  141. SHORT(fid);
  142. SHORT(count);
  143. p++;
  144. if((uint8_t*)p == (uint8_t*)f->data) {
  145. p += f->count;
  146. break;
  147. }
  148. BYTES(data, f->count);
  149. break;
  150. case Rwrite9p1:
  151. SHORT(fid);
  152. SHORT(count);
  153. break;
  154. case Rstat9p1:
  155. SHORT(fid);
  156. BYTES(stat, sizeof(f->stat));
  157. break;
  158. }
  159. return p - (uint8_t*)ap;
  160. }
  161. /*
  162. * buggery to give false qid for
  163. * the top 2 levels of the dump fs
  164. */
  165. static uint32_t
  166. fakeqid9p1(Dentry *f)
  167. {
  168. uint32_t q;
  169. int c;
  170. q = f->qid.path;
  171. if(q == (QPROOT|QPDIR)) {
  172. c = f->name[0];
  173. if(c >= '0' && c <= '9') {
  174. q = 3|QPDIR;
  175. c = (c-'0')*10 + (f->name[1]-'0');
  176. if(c >= 1 && c <= 12)
  177. q = 4|QPDIR;
  178. }
  179. }
  180. return q;
  181. }
  182. int
  183. convD2M9p1(Dentry *f, char *ap)
  184. {
  185. uint8_t *p;
  186. uint32_t q;
  187. p = (uint8_t*)ap;
  188. STRING(name, sizeof(f->name));
  189. memset(p, 0, 2*NAMELEN);
  190. uidtostr((char*)p, f->uid);
  191. p += NAMELEN;
  192. uidtostr((char*)p, f->gid);
  193. p += NAMELEN;
  194. q = fakeqid9p1(f);
  195. VLONG(q);
  196. LONG(qid.version);
  197. {
  198. q = f->mode & 0x0fff;
  199. if(f->mode & DDIR)
  200. q |= PDIR;
  201. if(f->mode & DAPND)
  202. q |= PAPND;
  203. if(f->mode & DLOCK)
  204. q |= PLOCK;
  205. VLONG(q);
  206. }
  207. LONG(atime);
  208. LONG(mtime);
  209. LONG(size); VLONG(0);
  210. VLONG(0);
  211. return p - (uint8_t*)ap;
  212. }
  213. #undef CHAR
  214. #undef SHORT
  215. #undef LONG
  216. #undef VLONG
  217. #undef BYTES
  218. #undef STRING
  219. #define CHAR(x) f->x = *p++
  220. #define SHORT(x) f->x = (p[0] | (p[1]<<8)); p += 2
  221. #define VLONG(q) q = (p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24)); p += 4
  222. #define LONG(x) VLONG(f->x)
  223. #define BYTES(x,n) memmove(f->x, p, n); p += n
  224. #define STRING(x,n) memmove(f->x, p, n); p += n
  225. int
  226. convM2S9p1(uint8_t *ap, Oldfcall *f, int n)
  227. {
  228. uint8_t *p;
  229. int t;
  230. p = ap;
  231. CHAR(type);
  232. t = f->type;
  233. SHORT(tag);
  234. switch(t)
  235. {
  236. default:
  237. /*
  238. * only whine if it couldn't be a 9P2000 Tversion9p1.
  239. */
  240. if(t != 19 || ap[4] != 100)
  241. print("convM2S9p1: bad type: %d\n", f->type);
  242. return 0;
  243. case Tnop9p1:
  244. case Tosession9p1:
  245. break;
  246. case Tsession9p1:
  247. BYTES(chal, sizeof(f->chal));
  248. break;
  249. case Tflush9p1:
  250. SHORT(oldtag);
  251. break;
  252. case Tattach9p1:
  253. SHORT(fid);
  254. BYTES(uname, sizeof(f->uname));
  255. BYTES(aname, sizeof(f->aname));
  256. BYTES(ticket, sizeof(f->ticket));
  257. BYTES(auth, sizeof(f->auth));
  258. break;
  259. case Toattach9p1:
  260. SHORT(fid);
  261. BYTES(uname, sizeof(f->uname));
  262. BYTES(aname, sizeof(f->aname));
  263. BYTES(ticket, NAMELEN);
  264. break;
  265. case Tclone9p1:
  266. SHORT(fid);
  267. SHORT(newfid);
  268. break;
  269. case Twalk9p1:
  270. SHORT(fid);
  271. BYTES(name, sizeof(f->name));
  272. break;
  273. case Tclwalk9p1:
  274. SHORT(fid);
  275. SHORT(newfid);
  276. BYTES(name, sizeof(f->name));
  277. break;
  278. case Tremove9p1:
  279. SHORT(fid);
  280. break;
  281. case Topen9p1:
  282. SHORT(fid);
  283. CHAR(mode);
  284. break;
  285. case Tcreate9p1:
  286. SHORT(fid);
  287. BYTES(name, sizeof(f->name));
  288. LONG(perm);
  289. CHAR(mode);
  290. break;
  291. case Tread9p1:
  292. SHORT(fid);
  293. LONG(offset); p += 4;
  294. SHORT(count);
  295. break;
  296. case Twrite9p1:
  297. SHORT(fid);
  298. LONG(offset); p += 4;
  299. SHORT(count);
  300. p++;
  301. f->data = (char*)p; p += f->count;
  302. break;
  303. case Tclunk9p1:
  304. case Tstat9p1:
  305. SHORT(fid);
  306. break;
  307. case Twstat9p1:
  308. SHORT(fid);
  309. BYTES(stat, sizeof(f->stat));
  310. break;
  311. /*
  312. */
  313. case Rnop9p1:
  314. case Rosession9p1:
  315. break;
  316. case Rsession9p1:
  317. BYTES(chal, sizeof(f->chal));
  318. BYTES(authid, sizeof(f->authid));
  319. BYTES(authdom, sizeof(f->authdom));
  320. break;
  321. case Rerror9p1:
  322. BYTES(ename, sizeof(f->ename));
  323. break;
  324. case Rflush9p1:
  325. break;
  326. case Rclone9p1:
  327. case Rclunk9p1:
  328. case Rremove9p1:
  329. case Rwstat9p1:
  330. SHORT(fid);
  331. break;
  332. case Rwalk9p1:
  333. case Rclwalk9p1:
  334. case Ropen9p1:
  335. case Rcreate9p1:
  336. SHORT(fid);
  337. LONG(qid.path);
  338. LONG(qid.version);
  339. break;
  340. case Rattach9p1:
  341. SHORT(fid);
  342. LONG(qid.path);
  343. LONG(qid.version);
  344. BYTES(rauth, sizeof(f->rauth));
  345. break;
  346. case Roattach9p1:
  347. SHORT(fid);
  348. LONG(qid.path);
  349. LONG(qid.version);
  350. break;
  351. case Rread9p1:
  352. SHORT(fid);
  353. SHORT(count);
  354. p++;
  355. f->data = (char*)p; p += f->count;
  356. break;
  357. case Rwrite9p1:
  358. SHORT(fid);
  359. SHORT(count);
  360. break;
  361. case Rstat9p1:
  362. SHORT(fid);
  363. BYTES(stat, sizeof(f->stat));
  364. break;
  365. }
  366. if((uint8_t*)ap+n == p)
  367. return n;
  368. return 0;
  369. }
  370. int
  371. convM2D9p1(char *ap, Dentry *f)
  372. {
  373. uint8_t *p;
  374. char str[28];
  375. p = (uint8_t*)ap;
  376. BYTES(name, sizeof(f->name));
  377. memmove(str, p, NAMELEN);
  378. p += NAMELEN;
  379. f->uid = strtouid(str);
  380. memmove(str, p, NAMELEN);
  381. p += NAMELEN;
  382. f->gid = strtouid(str);
  383. LONG(qid.path);
  384. LONG(qid.version);
  385. {
  386. LONG(atime);
  387. f->mode = (f->atime & 0x0fff) | DALLOC;
  388. if(f->atime & PDIR)
  389. f->mode |= DDIR;
  390. if(f->atime & PAPND)
  391. f->mode |= DAPND;
  392. if(f->atime & PLOCK)
  393. f->mode |= DLOCK;
  394. }
  395. LONG(atime);
  396. LONG(mtime);
  397. LONG(size); p += 4;
  398. p += 4;
  399. return p - (uint8_t*)ap;
  400. }