1 # NFS RPC client -- RFC 1094
3 # XXX This is not yet complete.
4 # XXX Only GETATTR, SETTTR, LOOKUP and READDIR are supported.
6 # (See mountclient.py for some hints on how to write RPC clients in
10 from rpc
import UDPClient
, TCPClient
11 from mountclient
import FHSIZE
, MountPacker
, MountUnpacker
18 # (...many error values...)
29 class NFSPacker(MountPacker
):
31 def pack_sattrargs(self
, sa
):
33 self
.pack_fhandle(file)
34 self
.pack_sattr(attributes
)
36 def pack_sattr(self
, sa
):
37 mode
, uid
, gid
, size
, atime
, mtime
= sa
42 self
.pack_timeval(atime
)
43 self
.pack_timeval(mtime
)
45 def pack_diropargs(self
, da
):
47 self
.pack_fhandle(dir)
48 self
.pack_string(name
)
50 def pack_readdirargs(self
, ra
):
51 dir, cookie
, count
= ra
52 self
.pack_fhandle(dir)
53 self
.pack_uint(cookie
)
56 def pack_timeval(self
, tv
):
62 class NFSUnpacker(MountUnpacker
):
64 def unpack_readdirres(self
):
65 status
= self
.unpack_enum()
67 entries
= self
.unpack_list(self
.unpack_entry
)
68 eof
= self
.unpack_bool()
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
)
80 def unpack_diropres(self
):
81 status
= self
.unpack_enum()
83 fh
= self
.unpack_fhandle()
84 fa
= self
.unpack_fattr()
90 def unpack_attrstat(self
):
91 status
= self
.unpack_enum()
93 attributes
= self
.unpack_fattr()
96 return status
, attributes
98 def unpack_fattr(self
):
99 type = self
.unpack_enum()
100 mode
= self
.unpack_uint()
101 nlink
= self
.unpack_uint()
102 uid
= self
.unpack_uint()
103 gid
= self
.unpack_uint()
104 size
= self
.unpack_uint()
105 blocksize
= self
.unpack_uint()
106 rdev
= self
.unpack_uint()
107 blocks
= self
.unpack_uint()
108 fsid
= self
.unpack_uint()
109 fileid
= self
.unpack_uint()
110 atime
= self
.unpack_timeval()
111 mtime
= self
.unpack_timeval()
112 ctime
= self
.unpack_timeval()
113 return (type, mode
, nlink
, uid
, gid
, size
, blocksize
, \
114 rdev
, blocks
, fsid
, fileid
, atime
, mtime
, ctime
)
116 def unpack_timeval(self
):
117 secs
= self
.unpack_uint()
118 usecs
= self
.unpack_uint()
122 class NFSClient(UDPClient
):
124 def __init__(self
, host
):
125 UDPClient
.__init
__(self
, host
, NFS_PROGRAM
, NFS_VERSION
)
127 def addpackers(self
):
128 self
.packer
= NFSPacker()
129 self
.unpacker
= NFSUnpacker('')
132 if self
.cred
== None:
133 self
.cred
= rpc
.AUTH_UNIX
, rpc
.make_auth_unix_default()
136 def Getattr(self
, fh
):
137 return self
.make_call(1, fh
, \
138 self
.packer
.pack_fhandle
, \
139 self
.unpacker
.unpack_attrstat
)
141 def Setattr(self
, sa
):
142 return self
.make_call(2, sa
, \
143 self
.packer
.pack_sattrargs
, \
144 self
.unpacker
.unpack_attrstat
)
148 def Lookup(self
, da
):
149 return self
.make_call(4, da
, \
150 self
.packer
.pack_diropargs
, \
151 self
.unpacker
.unpack_diropres
)
155 def Readdir(self
, ra
):
156 return self
.make_call(16, ra
, \
157 self
.packer
.pack_readdirargs
, \
158 self
.unpacker
.unpack_readdirres
)
160 # Shorthand to get the entire contents of a directory
161 def Listdir(self
, dir):
165 (status
, rest
) = self
.Readdir(ra
)
170 for fileid
, name
, cookie
in entries
:
171 list.append((fileid
, name
))
173 if eof
or last_cookie
== None:
175 ra
= (ra
[0], last_cookie
, ra
[2])
181 if sys
.argv
[1:]: host
= sys
.argv
[1]
183 if sys
.argv
[2:]: filesys
= sys
.argv
[2]
185 from mountclient
import UDPMountClient
, TCPMountClient
186 mcl
= TCPMountClient(host
)
192 sf
= mcl
.Mnt(filesys
)
196 ncl
= NFSClient(host
)
199 list = ncl
.Listdir(fh
)
200 for item
in list: print item