-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_tree_net.py
executable file
·315 lines (270 loc) · 9.62 KB
/
test_tree_net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python2
# pynfs - Python NFS4 tools
#
# Written by Peter Åstrand <[email protected]>
# Copyright (C) 2001 Cendio Systems AB (http://www.cendio.se)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# TODO: Clean up. Support TCP. More error checks.
import sys, os
import nfs4lib
from nfs4constants import *
from nfs4types import *
def clean_dir(directory):
fh = ncl.do_getfh(directory)
entries = ncl.do_readdir(fh)
names = [entry.name for entry in entries]
for name in names:
lookup_dir_ops = ncl.lookup_path(directory)
operations = [ncl.putrootfh_op()] + lookup_dir_ops
operations.append(ncl.remove_op(name))
res = ncl.compound(operations)
if res.status == NFS4ERR_NOTEMPTY:
# Recursive deletion
clean_dir(directory + [name])
# Remove dir itself
lookup_dir_ops = ncl.lookup_path(directory)
operations = [ncl.putrootfh_op()] + lookup_dir_ops
operations.append(ncl.remove_op(name))
res = ncl.compound(operations)
nfs4lib.check_result(res)
elif not res.status == NFS4_OK:
raise "Cannot clean directory %s" % directory
# Verify that all files were removed
entries = ncl.do_readdir(fh)
if entries:
raise "Cannot clean directory %s" % directory
def create_dir(ncl, curdir, newdir):
operations = [ncl.putrootfh_op()] + ncl.lookup_path(curdir)
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, newdir)
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
def create_leading_paths(ncl, prefix):
# Get root fh
fh = ncl.do_getfh([])
for thisdir in prefix:
try:
fh = ncl.do_lookup(fh, thisdir)
except nfs4lib.BadCompoundRes, e:
if e.errcode == NFS4ERR_NOENT:
# Create dir
print "Creating directory", thisdir
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, thisdir)
res = ncl.compound([ncl.putfh_op(fh), createop, ncl.getfh_op()])
nfs4lib.check_result(res)
fh = res.resarray[-1].arm.arm.object
else:
raise
else:
print "Directory", thisdir, "exists"
def main(ncl, unix_prefix):
ncl.init_connection()
prefix = nfs4lib.unixpath2comps(unix_prefix)
putrootfhop = ncl.putrootfh_op()
lookup_treeroot = [putrootfhop] + ncl.lookup_path(prefix)
lookup_dev = lookup_treeroot + ncl.lookup_path(["dev"])
lookup_doc = lookup_treeroot + ncl.lookup_path(["doc"])
lookup_src = lookup_treeroot + ncl.lookup_path(["src"])
lookup_tmp = lookup_treeroot + ncl.lookup_path(["tmp"])
lookup_private = lookup_treeroot + ncl.lookup_path(["private"])
print "Creating path", unix_prefix
create_leading_paths(ncl, prefix)
print "Clearing", unix_prefix
clean_dir(prefix)
print "Creating /dev"
operations = lookup_treeroot[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "dev")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /dev/floppy"
operations = lookup_dev[:]
objtype = createtype4(ncl, type=NF4LNK, linkdata="fd0")
createop = ncl.create(objtype, "floppy")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /dev/fd0"
operations = lookup_dev[:]
devdata = specdata4(ncl, 2, 0)
objtype = createtype4(ncl, type=NF4BLK, devdata=devdata)
createop = ncl.create(objtype, "fd0")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /dev/ttyS0"
operations = lookup_dev[:]
devdata = specdata4(ncl, 4, 64)
objtype = createtype4(ncl, type=NF4CHR, devdata=devdata)
createop = ncl.create(objtype, "ttyS0")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /dev/log"
operations = lookup_dev[:]
objtype = createtype4(ncl, type=NF4SOCK)
createop = ncl.create(objtype, "log")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /dev/initctl"
operations = lookup_dev[:]
objtype = createtype4(ncl, type=NF4FIFO)
createop = ncl.create(objtype, "initctl")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /doc"
operations = lookup_treeroot[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "doc")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating /doc/README"
remote = nfs4lib.NFS4OpenFile(ncl)
remote.open(os.path.join(unix_prefix, "doc/README"), "w")
data ="""\
Welcome to this NFS4 server.
Enjoy.
"""
remote.write(data)
remote.close()
print "Creating directory doc/porting"
operations = lookup_doc[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "porting")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating doc/porting/TODO"
remote = nfs4lib.NFS4OpenFile(ncl)
remote.open(os.path.join(unix_prefix, "doc/porting/TODO"), "w")
data ="""\
Need to work on DNIX support...
Enjoy.
"""
remote.write(data)
remote.close()
print "Creating src"
operations = lookup_treeroot[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "src")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating src/hello.c"
remote = nfs4lib.NFS4OpenFile(ncl)
remote.open(os.path.join(unix_prefix, "src/hello.c"), "w")
data = """\
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\\n");
exit(0);
}
"""
remote.write(data)
remote.close()
print "Creating tmp"
operations = lookup_treeroot[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "tmp")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating tmp/gazonk"
operations = lookup_tmp[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "gazonk")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating tmp/gazonk/foo.c"
remote = nfs4lib.NFS4OpenFile(ncl)
remote.open(os.path.join(unix_prefix, "tmp/gazonk/foo.c"), "w")
data = """\
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\\n");
exit(0);
}
"""
remote.write(data)
remote.close()
print "Creating directory private"
operations = lookup_treeroot[:]
objtype = createtype4(ncl, type=NF4DIR)
createop = ncl.create(objtype, "private")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
print "Creating private/info.txt"
remote = nfs4lib.NFS4OpenFile(ncl)
remote.open(os.path.join(unix_prefix, "private/info.txt"), "w")
remote.write("Personal data.\n")
remote.close()
print "Changing UNIX permissions of private dir to 0000"
operations = lookup_private[:]
stateid = stateid4(ncl, 0, "")
attrmask = nfs4lib.list2attrmask([FATTR4_MODE])
dummy_ncl = nfs4lib.DummyNcl()
dummy_ncl.packer.pack_uint(0000)
attr_vals = dummy_ncl.packer.get_buffer()
obj_attributes = fattr4(ncl, attrmask, attr_vals)
operations.append(ncl.setattr_op(stateid, obj_attributes))
res = ncl.compound(operations)
if res.status == NFS4ERR_ATTRNOTSUPP:
print "UNIX mode attribute not supported"
else:
nfs4lib.check_result(res)
print "Creating symlink src/doc -> ../doc"
operations = lookup_src[:]
objtype = createtype4(ncl, type=NF4LNK, linkdata="../doc")
createop = ncl.create(objtype, "doc")
operations.append(createop)
res = ncl.compound(operations)
nfs4lib.check_result(res)
def usage():
print "Usage: %s [nfs://]host[:[port]]<prefix>" % sys.argv[0]
print "Creates tree contents on server for nfs4st testing"
print "Directories and files will be created"
print "under <server><prefix>/nfs4st/"
print
print "<prefix> defaults to /, e.g. the directory nfs4st is"
print "created directly under the server root"
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
parse_result = nfs4lib.parse_nfs_url(sys.argv[1])
if not parse_result:
usage()
(host, portstring, directory) = parse_result
if portstring:
port = int(portstring)
else:
port = nfs4lib.NFS_PORT
if not directory:
directory = "/"
directory = os.path.join(directory, "nfs4st")
ncl = nfs4lib.create_client(host, port, "auto", uid=0, gid=0)
main(ncl, directory)