Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../Demo/rpc
File: nfsclient.py
# NFS RPC client -- RFC 1094
[0] Fix | Delete
[1] Fix | Delete
# XXX This is not yet complete.
[2] Fix | Delete
# XXX Only GETATTR, SETTTR, LOOKUP and READDIR are supported.
[3] Fix | Delete
[4] Fix | Delete
# (See mountclient.py for some hints on how to write RPC clients in
[5] Fix | Delete
# Python in general)
[6] Fix | Delete
[7] Fix | Delete
import rpc
[8] Fix | Delete
from rpc import UDPClient, TCPClient
[9] Fix | Delete
from mountclient import FHSIZE, MountPacker, MountUnpacker
[10] Fix | Delete
[11] Fix | Delete
NFS_PROGRAM = 100003
[12] Fix | Delete
NFS_VERSION = 2
[13] Fix | Delete
[14] Fix | Delete
# enum stat
[15] Fix | Delete
NFS_OK = 0
[16] Fix | Delete
# (...many error values...)
[17] Fix | Delete
[18] Fix | Delete
# enum ftype
[19] Fix | Delete
NFNON = 0
[20] Fix | Delete
NFREG = 1
[21] Fix | Delete
NFDIR = 2
[22] Fix | Delete
NFBLK = 3
[23] Fix | Delete
NFCHR = 4
[24] Fix | Delete
NFLNK = 5
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
class NFSPacker(MountPacker):
[28] Fix | Delete
[29] Fix | Delete
def pack_sattrargs(self, sa):
[30] Fix | Delete
file, attributes = sa
[31] Fix | Delete
self.pack_fhandle(file)
[32] Fix | Delete
self.pack_sattr(attributes)
[33] Fix | Delete
[34] Fix | Delete
def pack_sattr(self, sa):
[35] Fix | Delete
mode, uid, gid, size, atime, mtime = sa
[36] Fix | Delete
self.pack_uint(mode)
[37] Fix | Delete
self.pack_uint(uid)
[38] Fix | Delete
self.pack_uint(gid)
[39] Fix | Delete
self.pack_uint(size)
[40] Fix | Delete
self.pack_timeval(atime)
[41] Fix | Delete
self.pack_timeval(mtime)
[42] Fix | Delete
[43] Fix | Delete
def pack_diropargs(self, da):
[44] Fix | Delete
dir, name = da
[45] Fix | Delete
self.pack_fhandle(dir)
[46] Fix | Delete
self.pack_string(name)
[47] Fix | Delete
[48] Fix | Delete
def pack_readdirargs(self, ra):
[49] Fix | Delete
dir, cookie, count = ra
[50] Fix | Delete
self.pack_fhandle(dir)
[51] Fix | Delete
self.pack_uint(cookie)
[52] Fix | Delete
self.pack_uint(count)
[53] Fix | Delete
[54] Fix | Delete
def pack_timeval(self, tv):
[55] Fix | Delete
secs, usecs = tv
[56] Fix | Delete
self.pack_uint(secs)
[57] Fix | Delete
self.pack_uint(usecs)
[58] Fix | Delete
[59] Fix | Delete
[60] Fix | Delete
class NFSUnpacker(MountUnpacker):
[61] Fix | Delete
[62] Fix | Delete
def unpack_readdirres(self):
[63] Fix | Delete
status = self.unpack_enum()
[64] Fix | Delete
if status == NFS_OK:
[65] Fix | Delete
entries = self.unpack_list(self.unpack_entry)
[66] Fix | Delete
eof = self.unpack_bool()
[67] Fix | Delete
rest = (entries, eof)
[68] Fix | Delete
else:
[69] Fix | Delete
rest = None
[70] Fix | Delete
return (status, rest)
[71] Fix | Delete
[72] Fix | Delete
def unpack_entry(self):
[73] Fix | Delete
fileid = self.unpack_uint()
[74] Fix | Delete
name = self.unpack_string()
[75] Fix | Delete
cookie = self.unpack_uint()
[76] Fix | Delete
return (fileid, name, cookie)
[77] Fix | Delete
[78] Fix | Delete
def unpack_diropres(self):
[79] Fix | Delete
status = self.unpack_enum()
[80] Fix | Delete
if status == NFS_OK:
[81] Fix | Delete
fh = self.unpack_fhandle()
[82] Fix | Delete
fa = self.unpack_fattr()
[83] Fix | Delete
rest = (fh, fa)
[84] Fix | Delete
else:
[85] Fix | Delete
rest = None
[86] Fix | Delete
return (status, rest)
[87] Fix | Delete
[88] Fix | Delete
def unpack_attrstat(self):
[89] Fix | Delete
status = self.unpack_enum()
[90] Fix | Delete
if status == NFS_OK:
[91] Fix | Delete
attributes = self.unpack_fattr()
[92] Fix | Delete
else:
[93] Fix | Delete
attributes = None
[94] Fix | Delete
return status, attributes
[95] Fix | Delete
[96] Fix | Delete
def unpack_fattr(self):
[97] Fix | Delete
type = self.unpack_enum()
[98] Fix | Delete
mode = self.unpack_uint()
[99] Fix | Delete
nlink = self.unpack_uint()
[100] Fix | Delete
uid = self.unpack_uint()
[101] Fix | Delete
gid = self.unpack_uint()
[102] Fix | Delete
size = self.unpack_uint()
[103] Fix | Delete
blocksize = self.unpack_uint()
[104] Fix | Delete
rdev = self.unpack_uint()
[105] Fix | Delete
blocks = self.unpack_uint()
[106] Fix | Delete
fsid = self.unpack_uint()
[107] Fix | Delete
fileid = self.unpack_uint()
[108] Fix | Delete
atime = self.unpack_timeval()
[109] Fix | Delete
mtime = self.unpack_timeval()
[110] Fix | Delete
ctime = self.unpack_timeval()
[111] Fix | Delete
return (type, mode, nlink, uid, gid, size, blocksize, \
[112] Fix | Delete
rdev, blocks, fsid, fileid, atime, mtime, ctime)
[113] Fix | Delete
[114] Fix | Delete
def unpack_timeval(self):
[115] Fix | Delete
secs = self.unpack_uint()
[116] Fix | Delete
usecs = self.unpack_uint()
[117] Fix | Delete
return (secs, usecs)
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
class NFSClient(UDPClient):
[121] Fix | Delete
[122] Fix | Delete
def __init__(self, host):
[123] Fix | Delete
UDPClient.__init__(self, host, NFS_PROGRAM, NFS_VERSION)
[124] Fix | Delete
[125] Fix | Delete
def addpackers(self):
[126] Fix | Delete
self.packer = NFSPacker()
[127] Fix | Delete
self.unpacker = NFSUnpacker('')
[128] Fix | Delete
[129] Fix | Delete
def mkcred(self):
[130] Fix | Delete
if self.cred is None:
[131] Fix | Delete
self.cred = rpc.AUTH_UNIX, rpc.make_auth_unix_default()
[132] Fix | Delete
return self.cred
[133] Fix | Delete
[134] Fix | Delete
def Getattr(self, fh):
[135] Fix | Delete
return self.make_call(1, fh, \
[136] Fix | Delete
self.packer.pack_fhandle, \
[137] Fix | Delete
self.unpacker.unpack_attrstat)
[138] Fix | Delete
[139] Fix | Delete
def Setattr(self, sa):
[140] Fix | Delete
return self.make_call(2, sa, \
[141] Fix | Delete
self.packer.pack_sattrargs, \
[142] Fix | Delete
self.unpacker.unpack_attrstat)
[143] Fix | Delete
[144] Fix | Delete
# Root() is obsolete
[145] Fix | Delete
[146] Fix | Delete
def Lookup(self, da):
[147] Fix | Delete
return self.make_call(4, da, \
[148] Fix | Delete
self.packer.pack_diropargs, \
[149] Fix | Delete
self.unpacker.unpack_diropres)
[150] Fix | Delete
[151] Fix | Delete
# ...
[152] Fix | Delete
[153] Fix | Delete
def Readdir(self, ra):
[154] Fix | Delete
return self.make_call(16, ra, \
[155] Fix | Delete
self.packer.pack_readdirargs, \
[156] Fix | Delete
self.unpacker.unpack_readdirres)
[157] Fix | Delete
[158] Fix | Delete
# Shorthand to get the entire contents of a directory
[159] Fix | Delete
def Listdir(self, dir):
[160] Fix | Delete
list = []
[161] Fix | Delete
ra = (dir, 0, 2000)
[162] Fix | Delete
while 1:
[163] Fix | Delete
(status, rest) = self.Readdir(ra)
[164] Fix | Delete
if status <> NFS_OK:
[165] Fix | Delete
break
[166] Fix | Delete
entries, eof = rest
[167] Fix | Delete
last_cookie = None
[168] Fix | Delete
for fileid, name, cookie in entries:
[169] Fix | Delete
list.append((fileid, name))
[170] Fix | Delete
last_cookie = cookie
[171] Fix | Delete
if eof or last_cookie is None:
[172] Fix | Delete
break
[173] Fix | Delete
ra = (ra[0], last_cookie, ra[2])
[174] Fix | Delete
return list
[175] Fix | Delete
[176] Fix | Delete
[177] Fix | Delete
def test():
[178] Fix | Delete
import sys
[179] Fix | Delete
if sys.argv[1:]: host = sys.argv[1]
[180] Fix | Delete
else: host = ''
[181] Fix | Delete
if sys.argv[2:]: filesys = sys.argv[2]
[182] Fix | Delete
else: filesys = None
[183] Fix | Delete
from mountclient import UDPMountClient, TCPMountClient
[184] Fix | Delete
mcl = TCPMountClient(host)
[185] Fix | Delete
if filesys is None:
[186] Fix | Delete
list = mcl.Export()
[187] Fix | Delete
for item in list:
[188] Fix | Delete
print item
[189] Fix | Delete
return
[190] Fix | Delete
sf = mcl.Mnt(filesys)
[191] Fix | Delete
print sf
[192] Fix | Delete
fh = sf[1]
[193] Fix | Delete
if fh:
[194] Fix | Delete
ncl = NFSClient(host)
[195] Fix | Delete
attrstat = ncl.Getattr(fh)
[196] Fix | Delete
print attrstat
[197] Fix | Delete
list = ncl.Listdir(fh)
[198] Fix | Delete
for item in list: print item
[199] Fix | Delete
mcl.Umnt(filesys)
[200] Fix | Delete
[201] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function