| 1 | # NFS RPC client -- RFC 1094
|
|---|
| 2 |
|
|---|
| 3 | # XXX This is not yet complete.
|
|---|
| 4 | # XXX Only GETATTR, SETTTR, LOOKUP and READDIR are supported.
|
|---|
| 5 |
|
|---|
| 6 | # (See mountclient.py for some hints on how to write RPC clients in
|
|---|
| 7 | # Python in general)
|
|---|
| 8 |
|
|---|
| 9 | import rpc
|
|---|
| 10 | from rpc import UDPClient, TCPClient
|
|---|
| 11 | from mountclient import FHSIZE, MountPacker, MountUnpacker
|
|---|
| 12 |
|
|---|
| 13 | NFS_PROGRAM = 100003
|
|---|
| 14 | NFS_VERSION = 2
|
|---|
| 15 |
|
|---|
| 16 | # enum stat
|
|---|
| 17 | NFS_OK = 0
|
|---|
| 18 | # (...many error values...)
|
|---|
| 19 |
|
|---|
| 20 | # enum ftype
|
|---|
| 21 | NFNON = 0
|
|---|
| 22 | NFREG = 1
|
|---|
| 23 | NFDIR = 2
|
|---|
| 24 | NFBLK = 3
|
|---|
| 25 | NFCHR = 4
|
|---|
| 26 | NFLNK = 5
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | class NFSPacker(MountPacker):
|
|---|
| 30 |
|
|---|
| 31 | def pack_sattrargs(self, sa):
|
|---|
| 32 | file, attributes = sa
|
|---|
| 33 | self.pack_fhandle(file)
|
|---|
| 34 | self.pack_sattr(attributes)
|
|---|
| 35 |
|
|---|
| 36 | def pack_sattr(self, sa):
|
|---|
| 37 | mode, uid, gid, size, atime, mtime = sa
|
|---|
| 38 | self.pack_uint(mode)
|
|---|
| 39 | self.pack_uint(uid)
|
|---|
| 40 | self.pack_uint(gid)
|
|---|
| 41 | self.pack_uint(size)
|
|---|
| 42 | self.pack_timeval(atime)
|
|---|
| 43 | self.pack_timeval(mtime)
|
|---|
| 44 |
|
|---|
| 45 | def pack_diropargs(self, da):
|
|---|
| 46 | dir, name = da
|
|---|
| 47 | self.pack_fhandle(dir)
|
|---|
| 48 | self.pack_string(name)
|
|---|
| 49 |
|
|---|
| 50 | def pack_readdirargs(self, ra):
|
|---|
| 51 | dir, cookie, count = ra
|
|---|
| 52 | self.pack_fhandle(dir)
|
|---|
| 53 | self.pack_uint(cookie)
|
|---|
| 54 | self.pack_uint(count)
|
|---|
| 55 |
|
|---|
| 56 | def pack_timeval(self, tv):
|
|---|
| 57 | secs, usecs = tv
|
|---|
| 58 | self.pack_uint(secs)
|
|---|
| 59 | self.pack_uint(usecs)
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | class NFSUnpacker(MountUnpacker):
|
|---|
| 63 |
|
|---|
| 64 | def unpack_readdirres(self):
|
|---|
| 65 | status = self.unpack_enum()
|
|---|
| 66 | if status == NFS_OK:
|
|---|
| 67 | entries = self.unpack_list(self.unpack_entry)
|
|---|
| 68 | eof = self.unpack_bool()
|
|---|
| 69 | rest = (entries, eof)
|
|---|
| 70 | else:
|
|---|
| 71 | rest = None
|
|---|
| 72 | return (status, rest)
|
|---|
| 73 |
|
|---|
| 74 | def unpack_entry(self):
|
|---|
| 75 | fileid = self.unpack_uint()
|
|---|
| 76 | name = self.unpack_string()
|
|---|
| 77 | cookie = self.unpack_uint()
|
|---|
| 78 | return (fileid, name, cookie)
|
|---|
| 79 |
|
|---|
| 80 | def unpack_diropres(self):
|
|---|
| 81 | status = self.unpack_enum()
|
|---|
| 82 | if status == NFS_OK:
|
|---|
| 83 | fh = self.unpack_fhandle()
|
|---|
| 84 | fa = self.unpack_fattr()
|
|---|
| 85 | rest = (fh, fa)
|
|---|
| 86 | else:
|
|---|
| 87 | rest = None
|
|---|
| 88 | return (status, rest)
|
|---|
| 89 |
|
|---|
| 90 | def unpack_attrstat(self):
|
|---|
| 91 | status = self.unpack_enum()
|
|---|
| 92 | if status == NFS_OK:
|
|---|
| 93 | attributes = self.unpack_fattr()
|
|---|
| 94 | else:
|
|---|
|
|---|