Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 08e5374acda157721c148c7462f0f1b6992755d3

Remove dead code

Remove leftovers from migration to FSEmulation.
  
2121import errno
2222import fuse
2323import logging
24import operator
2524import os
2625import os.path
2726import shutil
2827import stat
2928import sys
3029import tempfile
31import time
3230
33import dbus
34
3531from sugar.logger import trace
3632import sugar.logger
3733
5959 st_uid=os.getuid(), st_gid=os.getgid(),
6060 st_size=fs_entry.get_size(), st_nlink=n_links,
6161 st_mtime=fs_entry.get_mtime(), st_ctime=fs_entry.get_ctime())
62
63
64class DataStoreObjectStat(fuse.Stat):
65
66 # pylint: disable-msg=R0902,R0903
67 def __init__(self, filesystem, metadata, size, inode):
68 fuse.Stat.__init__(self, st_mode=stat.S_IFREG | 0750, st_ino=inode,
69 st_uid=os.getuid(), st_gid=os.getgid(), st_size=size,
70 st_mtime=self._parse_time(metadata.get('timestamp', '')))
71 self.st_ctime = self.st_mtime
72 self.st_atime = self.st_mtime
73 tags = [tag for tag in metadata.get('tags', '').split()
74 if tag and '/' not in tag]
75 self.st_nlink = len(tags) + 1
76 self.metadata = metadata
77 self._filesystem = filesystem
78 self.object_id = metadata['uid']
79
80 def _parse_time(self, timestamp):
81 if isinstance(timestamp, (int, float)):
82 return timestamp
83
84 try:
85 return int(timestamp, 10)
86 except ValueError:
87 return 0
88
89 def should_truncate(self):
90 return self._filesystem.should_truncate(self.object_id)
91
92 def reset_truncate(self):
93 return self._filesystem.reset_truncate(self.object_id)
94
95
96class Symlink(fuse.Stat):
97 def __init__(self, filesystem, target, inode_nr):
98 self._filesystem = filesystem
99 self.target = target
100 fuse.Stat.__init__(self, st_mode=stat.S_IFLNK | 0777, st_nlink=1,
101 st_uid=os.getuid(), st_gid=os.getgid(), st_ino=inode_nr,
102 st_mtime=time.time())
103 self.st_ctime = self.st_mtime
104 self.st_atime = self.st_mtime
105
106
107class Directory(fuse.Stat):
108 def __init__(self, path, parent_path, filesystem, mode):
109 self._path = path
110 self._parent_path = parent_path
111 self._filesystem = filesystem
112 fuse.Stat.__init__(self, st_mode=stat.S_IFDIR | mode, st_nlink=2,
113 st_uid=os.getuid(), st_gid=os.getgid(),
114 st_mtime=time.time())
115 self.st_ctime = self.st_mtime
116 self.st_atime = self.st_mtime
117 self.st_ino = filesystem.get_inode_number(path)
118
119 def getxattr(self, name_, attribute_):
120 # on Linux ENOATTR=ENODATA (Python errno doesn't contain ENOATTR)
121 raise IOError(errno.ENODATA, os.strerror(errno.ENODATA))
122
123 def listxattr(self, name_):
124 return []
125
126 def lookup(self, name_):
127 raise IOError(errno.ENOENT, os.strerror(errno.ENOENT))
128
129 def mkdir(self, name_):
130 raise IOError(errno.EACCES, os.strerror(errno.EACCES))
131
132 def mknod(self, name_):
133 raise IOError(errno.EACCES, os.strerror(errno.EACCES))
134
135 def readdir(self, offset_):
136 yield fuse.Direntry('.', type=stat.S_IFDIR,
137 ino=self._filesystem.get_inode_number(self._path))
138 yield fuse.Direntry('..', type=stat.S_IFDIR,
139 ino=self._filesystem.get_inode_number(self._parent_path))
140
141 def readlink(self, name):
142 entry = self.lookup(name)
143 if not isinstance(entry, Symlink):
144 raise IOError(errno.EINVAL, os.strerror(errno.EINVAL))
145
146 return entry.target
147
148 def remove(self, name_):
149 raise IOError(errno.EACCES, os.strerror(errno.EACCES))
150
151 def setxattr(self, name_, attribute_, value_, flags_):
152 # On Linux ENOTSUP = EOPNOTSUPP
153 raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
154
155
156class ByTitleDirectory(Directory):
157 def __init__(self, path, parent_path, filesystem):
158 Directory.__init__(self, path, parent_path, filesystem, 0750)
159
160 def readdir(self, offset):
161 for entry in Directory.readdir(self, offset):
162 yield entry
163
164 for entry in self._find_entries():
165 if 'uid' not in entry:
166 # corrupted entry
167 continue
168
169 name = self._filesystem.lookup_title_name(entry['uid'])
170 yield fuse.Direntry(name, type=stat.S_IFDIR,
171 ino=self._filesystem.get_inode_number(entry['uid']))
172
173 @trace()
174 def _find_entries(self):
175 return self._filesystem.find({},
176 {'metadata': ['title', 'uid', 'timestamp']})
177
178 def getxattr(self, name, attribute):
179 object_id = self._filesystem.resolve_title_name(name)
180 metadata = self._filesystem.get_metadata(object_id)
181 if attribute in metadata:
182 return metadata[attribute]
183
184 Directory.getxattr(self, object_id, attribute)
185
186 def listxattr(self, name):
187 object_id = self._filesystem.resolve_title_name(name)
188 metadata = self._filesystem.get_metadata(object_id)
189 return [str(name) for name in metadata.keys()]
190
191 def lookup(self, name):
192 object_id = self._filesystem.resolve_title_name(name)
193 metadata = self._filesystem.get_metadata(object_id)
194 size = self._filesystem.get_data_size(object_id)
195 return DataStoreObjectStat(self._filesystem, metadata, size,
196 self._filesystem.get_inode_number(object_id))
197
198 def mknod(self, name):
199 if self._filesystem.try_resolve_title_name(name):
200 raise IOError(errno.EEXIST, os.strerror(errno.EEXIST))
201
202 object_id = self._filesystem.create_new(name, '')
203
204 def remove(self, name):
205 object_id = self._filesystem.resolve_title_name(name)
206 self._filesystem.remove_entry(object_id)
207
208 def setxattr(self, name, attribute, value, flags):
209 object_id = self._filesystem.resolve_title_name(name)
210 metadata = self._filesystem.get_metadata(object_id)
211 if flags & XATTR_CREATE and attribute in metadata:
212 raise IOError(errno.EEXIST, os.strerror(errno.EEXIST))
213
214 if flags & XATTR_REPLACE and attribute not in metadata:
215 # on Linux ENOATTR=ENODATA (Python errno doesn't contain ENOATTR)
216 raise IOError(errno.ENODATA, os.strerror(errno.ENODATA))
217
218 metadata[attribute] = value
219 self._filesystem.write_metadata(object_id, metadata)
220
221
222class ByIdDirectory(Directory):
223 def __init__(self, path, parent_path, filesystem):
224 Directory.__init__(self, path, parent_path, filesystem, 0550)
225
226 def getxattr(self, object_id, attribute):
227 metadata = self._filesystem.get_metadata(object_id)
228 if attribute in metadata:
229 return metadata[attribute]
230
231 Directory.getxattr(self, object_id, attribute)
232
233 def listxattr(self, object_id):
234 metadata = self._filesystem.get_metadata(object_id)
235 return [str(name) for name in metadata.keys()]
236
237 def lookup(self, object_id):
238 name = self._filesystem.lookup_title_name(object_id)
239 path = '%s/%s' % (self._path, object_id)
240 return Symlink(self._filesystem, '../' + name,
241 self._filesystem.get_inode_number(path))
242
243 def readdir(self, offset):
244 for entry in Directory.readdir(self, offset):
245 yield entry
246
247 for entry in self._filesystem.find({}, {'metadata': ['uid']}):
248 if 'uid' not in entry:
249 # corrupted entry
250 continue
251
252 yield fuse.Direntry(entry['uid'], type=stat.S_IFLNK,
253 ino=self._filesystem.get_inode_number(entry['uid']))
254
255 def remove(self, object_id):
256 self._filesystem.remove_entry(object_id)
257
258
259class ByTagsSubDirectory(ByTitleDirectory):
260 def __init__(self, path, parent_path, filesystem, tags):
261 self._tags = frozenset(tags)
262 ByTitleDirectory.__init__(self, path, parent_path, filesystem)
263
264 def mknod(self, name):
265 if self._filesystem.try_resolve_title_name(name):
266 raise IOError(errno.EEXIST, os.strerror(errno.EEXIST))
267
268 object_id = self._filesystem.create_new(name, '', self._tags)
269
270 @trace()
271 def _find_entries(self):
272 # The current data store doesn't support searching within the tags
273 # property, so we need to do an unspecific full text search and
274 # filter out any extra matches.
275 query = {'query': ' '.join(self._tags)}
276 for entry in self._filesystem.find(query,
277 {'metadata': ['uid', 'tags']}):
278
279 entry_tags = frozenset(entry.get('tags', '').split())
280 if self._tags - entry_tags:
281 continue
282
283 yield entry
284
285
286class ByTagsDirectory(Directory):
287 def __init__(self, path, parent_path, filesystem):
288 Directory.__init__(self, path, parent_path, filesystem, 0550)
289 self._tag_dirs = {}
290
291 def readdir(self, offset):
292 for entry in Directory.readdir(self, offset):
293 yield entry
294
295 for tag in self._get_tags():
296 tag = unicode(tag).encode('utf-8')
297 if '/' in tag:
298 continue
299
300 path = '%s/%s' % (self._path, tag)
301 yield fuse.Direntry(tag, type=stat.S_IFDIR,
302 ino=self._filesystem.get_inode_number(path))
303
304 def lookup(self, tag):
305 if tag not in self._tag_dirs:
306 if not self._check_tag(tag):
307 raise IOError(errno.ENOENT, os.strerror(errno.ENOENT))
308
309 path = '%s/%s' % (self._path, tag)
310 self._tag_dirs[tag] = ByTagsSubDirectory(path, self._path,
311 self._filesystem, [tag])
312
313 return self._tag_dirs[tag]
314
315 @trace()
316 def _check_tag(self, tag):
317 # The current data store doesn't support searching within the tags
318 # property, so we need to do an unspecific full text search and
319 # filter out any extra matches.
320 query = {'query': tag}
321 for entry in self._filesystem.find(query, {'metadata': ['tags']}):
322 if tag in entry.get('tags', '').split():
323 return True
324
325 return False
326
327 @trace()
328 def _get_tags(self):
329 tags = set()
330 # The current data store doesn't support get_uniquevaluesfor('tags').
331 for entry in self._filesystem.find({}, {'metadata': ['tags']}):
332 logging.debug('entry=%r', entry)
333 tags.update(entry.get('tags', '').split())
334
335 tags.discard('')
336 return tags
337
338
339class RootDirectory(ByTitleDirectory):
340 def __init__(self, filesystem):
341 ByTitleDirectory.__init__(self, '/', '/', filesystem)
342 self.by_id_directory = ByIdDirectory('/by-id', '/', filesystem)
343 self.by_tags_directory = ByTagsDirectory('/by-tags', '/', filesystem)
344 self.by_title_directory = self
345
346 def readdir(self, offset_):
347 for name in ['by-id', 'by-tags']:
348 yield fuse.Direntry(name, type=stat.S_IFDIR,
349 ino=self._filesystem.get_inode_number('/' + name))
350
351 for entry in ByTitleDirectory.readdir(self, offset_):
352 yield entry
353
354 def lookup(self, name):
355 if name == 'by-id':
356 return self.by_id_directory
357 elif name == 'by-tags':
358 return self.by_tags_directory
359
360 return ByTitleDirectory.lookup(self, name)
361
362 def remove(self, name):
363 if name in ['by-id', 'by-tags']:
364 raise IOError(errno.EACCES, os.strerror(errno.EACCES))
365
366 return ByTitleDirectory.remove(self, name)
36762
36863
36964class DataStoreFile(object):