-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnfs4state.py
More file actions
269 lines (234 loc) · 9.54 KB
/
nfs4state.py
File metadata and controls
269 lines (234 loc) · 9.54 KB
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
#!/usr/bin/env python2.2
from nfs4constants import *
from nfs4types import *
import nfs4packer
import rpc
import nfs4lib
import os
import time
import StringIO
from stat import *
import sha
class NFSFileSystem:
"""This implements the NFS4 pseudo-filesystem using NFS4VirtualHandles."""
def __init__(self):
self.dirent = {}
self.filehandles = {}
def bind(self, object):
self.root = object
def get_fh(self, handle):
if self.filehandles.has_key(handle):
return self.filehandles[handle]
else:
return 0
def get_root(self):
return self.root
def register(self, handle, object):
self.filehandles[handle] = object
self.filehandles[object.ref] = object
def unregister(self, handle):
if self.filehandles.has_key(handle):
del self.filehandles[object.ref]
del self.filehandles[handle]
class NFSFileHandle:
def __init__(self, server, ref, file):
self.ref = ref
self.file = file
self.server = server
self.handle = self.get_fhclass() + sha.new(ref).hexdigest()
self.server.register(self.handle, self)
self.change = 0
def __repr__(self):
return "<NFSFileHandle: %s>" % (self.ref)
def get_attributes(self, ncl=nfs4lib.DummyNcl(), attrlist=None):
raise FileHandle, "Implement get_attributes"
def get_directory(self):
raise FileHandle, "Implement get_directory"
def get_type(self):
raise FileHandle, "Implement get_type"
def read(self, offset, count):
raise FileHandle, "Implement read"
class VirtualHandle(NFSFileHandle):
def __init__(self, server, ref, file, type):
NFSFileHandle.__init__(self, server, ref, file)
self.type = type;
self.st_mtime = int(time.time())
self.st_atime = int(time.time())
self.st_ctime = int(time.time())
self.st_size = 0
self.st_ino = 0
self.st_uid = 0
self.st_gid = 0
self.named_attr = {}
if type == NF4DIR:
self.dirent = []
else:
self.file = StringIO.StringIO()
def create(self, name):
if self.type <> NF4DIR:
raise FileHandle, "create called on non-directory (%s)" % self.ref
fh = VirtualHandle(self.server, os.path.join(self.ref, name),
os.path.join(self.ref, name), NF4REG)
self.dirent.append(fh)
return fh
def create_dir(self, name):
if self.type <> NF4DIR:
raise FileHandle, "create_dir called on non-directory (%s)" % self.ref
fh = VirtualHandle(self.server, os.path.join(self.ref, name),
os.path.join(self.ref, name), NF4DIR)
self.dirent.append(fh)
return fh
def get_attributes(self, ncl=nfs4lib.DummyNcl(), attrlist=None):
ret_dict = {};
for attr in attrlist:
if attr == FATTR4_SUPP_ATTR:
ret_dict[attr] = list2attrmask([ FATTR4_SUPP_ATTR, FATTR4_TYPE,
FATTR4_CHANGE, FATTR4_SIZE, FATTR4_FSID,
FATTR4_LEASE_TIME, FATTR4_FILEID,
FATTR4_MAXFILESIZE, FATTR4_MAXREAD,
FATTR4_MAXWRITE, FATTR4_MODE,
FATTR4_NUMLINKS, FATTR4_OWNER,
FATTR4_OWNER_GROUP, FATTR4_RAWDEV ])
elif attr == FATTR4_TYPE:
if type == NF4DIR:
ret_dict[attr] = NF4DIR
else:
ret_dict[attr] = NF4REG
elif attr == FATTR4_CHANGE:
ret_dict[attr] = nfstime4(ncl, self.st_ctime, 0);
elif attr == FATTR4_SIZE:
ret_dict[attr] = self.st_size
elif attr == FATTR4_FSID:
ret_dict[attr] = "woop"
elif attr == FATTR4_LEASE_TIME:
ret_dict[attr] = 1700
elif attr == FATTR4_FILEID:
ret_dict[attr] = self.st_ino
elif attr == FATTR4_MAXFILESIZE:
ret_dict[attr] = 1000000
elif attr == FATTR4_MAXREAD:
ret_dict[attr] = 1000
elif attr == FATTR4_MAXWRITE:
ret_dict[attr] = 1000
elif attr == FATTR4_MODE:
ret_dict[attr] = self.st_mode
elif attr == FATTR4_NUMLINKS:
ret_dict[attr] = self.st_nlink
elif attr == FATTR4_OWNER:
ret_dict[attr] = self.st_uid
elif attr == FATTR4_OWNER_GROUP:
ret_dict[attr] = self.st_gid
elif attr == FATTR4_RAWDEV:
ret_dict[attr] = 0
elif attr == FATTR4_TIME_ACCESS:
ret_dict[attr] = nfstime4(ncl, self.st_atime, 0);
return ret_dict
def get_fhclass(self):
return "virt"
def get_type(self):
return self.type;
def read(self, offset, count):
self.file.seek(offset)
data = self.file.read(count)
return data
def read_dir(self):
if self.type == NF4DIR:
return self.dirent
def write(self, offset, data):
self.file.seek(offset)
count = self.file.write(data)
return len(data)
class HardHandle(NFSFileHandle):
def __init__(self, server, ref, file):
NFSFileHandle.__init__(self, server, ref, file)
def do_lookupp(self):
return self.server.get_fh(os.path.dirname(self.ref))
def get_attributes(self, ncl=nfs4lib.DummyNcl(), attrlist=None):
stat_struct = os.lstat(self.file)
ret_dict = {};
for attr in attrlist:
if attr == FATTR4_TYPE:
if S_ISDIR(stat_struct.st_mode):
ret_dict[attr] = NF4DIR
elif S_ISREG(stat_struct.st_mode):
ret_dict[attr] = NF4REG
elif S_ISLNK(stat_struct.st_mode):
ret_dict[attr] = NF4LNK
elif attr == FATTR4_CHANGE:
ret_dict[attr] = nfstime4(ncl, stat_struct.st_ctime, 0);
elif attr == FATTR4_SIZE:
ret_dict[attr] = stat_struct.st_size
elif attr == FATTR4_FSID:
ret_dict[attr] = "woop"
elif attr == FATTR4_LEASE_TIME:
ret_dict[attr] = 1700
elif attr == FATTR4_FILEID:
ret_dict[attr] = stat_struct.st_ino
elif attr == FATTR4_MAXFILESIZE:
ret_dict[attr] = 1000000
elif attr == FATTR4_MAXREAD:
ret_dict[attr] = 1000
elif attr == FATTR4_MAXWRITE:
ret_dict[attr] = 1000
elif attr == FATTR4_MODE:
ret_dict[attr] = stat_struct.st_mode
elif attr == FATTR4_NUMLINKS:
ret_dict[attr] = stat_struct.st_nlink
elif attr == FATTR4_OWNER:
ret_dict[attr] = stat_struct.st_uid
elif attr == FATTR4_OWNER_GROUP:
ret_dict[attr] = stat_struct.st_gid
elif attr == FATTR4_RAWDEV:
ret_dict[attr] = 0
elif attr == FATTR4_TIME_ACCESS:
ret_dict[attr] = nfstime4(ncl, stat_struct.st_atime, 0);
return ret_dict
def get_fhclass(self):
return "hard"
def get_link(self):
return os.readlink(self.file)
def get_type(self):
stat_struct = os.lstat(self.file)
if S_ISDIR(stat_struct.st_mode):
return NF4DIR
elif S_ISREG(stat_struct.st_mode):
return NF4REG
elif S_ISLNK(stat_struct.st_mode):
return NF4LNK
else:
return NF4REG
def read(self, offset, count):
fh = open(self.file)
fh.seek(offset)
data = fh.read(count)
fh.close()
return data
def read_dir(self):
stat_struct = os.stat(self.file)
if not S_ISDIR(stat_struct.st_mode):
return []
list = []
for i in os.listdir(self.file):
fullref = os.path.join(self.ref, i)
fullfile = os.path.join(self.file, i)
if self.server.get_fh(fullref):
list.append(self.server.get_fh(fullref))
else:
fh = HardHandle(self.server, fullref, fullfile)
list.append(fh)
return list
def write(self, offset, count, data):
fh = open(self.file, 'r+')
fh.seek(offset)
fh.write(data)
fh.close()
return len(data)
class NFSClientHandle:
def __init__(self):
self.confirmed = 0
self.curr_fh = 0
self.fh_stack = []
self.dirlist = {}
self.entry = {}
def confirm(self):
self.confirmed = 1