public static void dofid(ninepc.ninep protocol, ushort tag, int fid, int newfid) { int cfid; if (fid != root && newfid != ffid) { protocol.doTclunk(tag, cwd); cfid = cwd; cwd = fid; fid = cfid; } }
public static void Main(string[] args) { ninepc.ninep protocol; string cmd, server; int i; uint offset; Dir[] dirs; string[] lss, lsc; ushort tag = 10; int fid = 6; ffid = 5; root = 9; cwd = 7; protocol = new ninepc.ninep(); //server = "sources.cs.bell-labs.com"; server = "127.0.0.1"; try { //test.connect (server, 564); protocol.connect(server, 9999); protocol.doTversion(65535); protocol.doTauth(tag++); protocol.doTattach(tag++, root); protocol.doTwalk(tag++, root, cwd, new string[0]); dofid(protocol, tag++, root, cwd); for (;;) { Console.Write("{0}% ", server); cmd = Console.ReadLine(); if (cmd.StartsWith("ls")) { protocol.doTwalk(tag++, cwd, ffid, new string[] { "." }); dofid(protocol, tag++, cwd, ffid); //printPacket (test.pktR, "R"); protocol.doTopen(tag++, ffid, 0x00); //printPacket (test.pktR, "R"); protocol.doTread(tag++, ffid, 0, (uint)protocol.mdatasz); //printPacket (test.pktR, "R"); //printPacket (test.pktT, "T"); //printPacket (test.pktR, "R"); dirs = protocol.dols(protocol.readbuf); foreach (Dir d in dirs) { Console.WriteLine("{0} {1} {2} {3} {4}", protocol.modestr(d.mode), d.uid, d.gid, d.length, d.name); } protocol.doTclunk(tag++, ffid); continue; } if (cmd.StartsWith("cd")) { lss = cmd.Split(" ".ToCharArray()); if (lss.Length < 2) { continue; } lsc = lss [1].Split("/".ToCharArray()); protocol.doTwalk(tag++, cwd, fid, lsc); dofid(protocol, tag++, cwd, fid); continue; } if (cmd.StartsWith("cat")) { lss = cmd.Split(" ".ToCharArray()); Array.Copy(lss, 1, lss, 0, lss.Length - 1); for (i = 0; i < (lss.Length - 1); i++) { offset = 0; protocol.doTwalk(tag++, cwd, ffid, new string[] { lss [i] }); dofid(protocol, tag++, cwd, ffid); protocol.doTstat(tag++, ffid); protocol.doTopen(tag++, ffid, 0x00); protocol.doTread(tag++, ffid, offset, (uint)protocol.dir.length); Console.WriteLine(protocol.convstring(protocol.readbuf)); protocol.doTclunk(tag++, ffid); } } if (cmd.StartsWith("rm")) { lss = cmd.Split(" ".ToCharArray()); Array.Copy(lss, 1, lss, 0, lss.Length - 1); for (i = 0; i < (lss.Length - 1); i++) { offset = 0; protocol.doTwalk(tag++, cwd, ffid, new string[] { lss [i] }); dofid(protocol, tag++, cwd, ffid); protocol.doTremove(tag++, ffid); } } if (cmd.StartsWith("touch")) { lss = cmd.Split(" ".ToCharArray()); Array.Copy(lss, 1, lss, 0, lss.Length - 1); for (i = 0; i < (lss.Length - 1); i++) { offset = 0; protocol.doTcreate(tag++, cwd, lss[i], 0x0777, (byte)proto.ORDWR); } } if (cmd.StartsWith("mkdir")) { lss = cmd.Split(" ".ToCharArray()); Array.Copy(lss, 1, lss, 0, lss.Length - 1); for (i = 0; i < (lss.Length - 1); i++) { offset = 0; protocol.doTcreate(tag++, cwd, lss[i], 0x00000777 | (uint)proto.DMDIR, (byte)proto.OREAD); } } if (cmd.StartsWith("wstat")) { lss = cmd.Split(" ".ToCharArray()); //Array.Copy (lss, 1, lss, 0, lss.Length - 1); Array.ForEach(lss, x => Console.WriteLine(x)); protocol.doTwalk(tag++, cwd, ffid, new string[] { lss [1] }); dofid(protocol, tag++, cwd, ffid); Inode node = createnode(lss[2], 0x00000777, lss[3], lss[4], (byte)proto.QTFILE); node.dir.length = 0; Console.WriteLine("{0} {1} {2} {3} {4}", protocol.modestr(node.dir.mode), node.dir.uid, node.dir.gid, node.dir.length, node.dir.name); protocol.doTwstat(tag++, protocol.ffid, node.dir); protocol.doTclunk(tag++, protocol.ffid); } if (cmd.StartsWith("q")) { break; } } protocol.doTclunk(tag++, cwd); protocol.doTclunk(tag++, root); protocol.shutdown(); } catch (Exception ex) { Console.WriteLine("Error main: {0}", ex.ToString()); } }