root / pithos / backends / hashfilerback.py @ d3e2551d
History | View | Annotate | Download (18.7 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from pithos.lib.hashfiler import (Filer, ROOTNODE, |
35 |
SERIAL, PARENT, PATH, SIZE, POPULATION, |
36 |
POPSIZE, SOURCE, MTIME, CLUSTER) |
37 |
from base import NotAllowedError |
38 |
from binascii import hexlify, unhexlify |
39 |
from os import makedirs, mkdir |
40 |
from os.path import exists, isdir, split |
41 |
from itertools import chain |
42 |
from collections import defaultdict |
43 |
from heapq import merge |
44 |
|
45 |
debug = 0
|
46 |
|
47 |
READ = 0
|
48 |
WRITE = 1
|
49 |
|
50 |
MAIN_CLUSTER = 0
|
51 |
TRASH_CLUSTER = 1
|
52 |
|
53 |
def backend_method(func=None, name=None, autocommit=1): |
54 |
if func is None: |
55 |
def fn(func): |
56 |
return backend_method(func, name=name, autocommit=autocommit)
|
57 |
return fn
|
58 |
|
59 |
name = func.__name__ if name is None else name |
60 |
thefunc = None
|
61 |
if debug:
|
62 |
def fn(self, *args, **kw): |
63 |
print "%s %s %s" % (name, args, kw) |
64 |
return func(self, *args, **kw) |
65 |
thefunc = fn |
66 |
else:
|
67 |
thefunc = func |
68 |
#XXX: Can't set __doc__ in methods?!
|
69 |
#
|
70 |
#doc = func.__doc__
|
71 |
#if doc is None:
|
72 |
# doc = ''
|
73 |
#func.__doc__ = getattr(BaseBackend, name).__doc__ + '\n***\n' + doc
|
74 |
if not autocommit: |
75 |
return func
|
76 |
|
77 |
def method(self, *args, **kw): |
78 |
with self.hf: |
79 |
return thefunc(self, *args, **kw) |
80 |
|
81 |
return method
|
82 |
|
83 |
|
84 |
class HashFilerBack(object): |
85 |
"""Abstract backend class that serves as a reference for actual implementations.
|
86 |
|
87 |
The purpose of the backend is to provide the necessary functions for handling data
|
88 |
and metadata. It is responsible for the actual storage and retrieval of information.
|
89 |
|
90 |
Note that the account level is always valid as it is checked from another subsystem.
|
91 |
|
92 |
The following variables should be available:
|
93 |
'hash_algorithm': Suggested is 'sha256'
|
94 |
'block_size': Suggested is 4MB
|
95 |
"""
|
96 |
|
97 |
def __init__(self, db): |
98 |
head, tail = split(db) |
99 |
if not exists(head): |
100 |
makedirs(head) |
101 |
if not isdir(head): |
102 |
raise RuntimeError("Cannot open database at '%s'" % (db,)) |
103 |
|
104 |
blocksize = 4*1024*1024 |
105 |
hashtype = 'sha256'
|
106 |
root = head + '/root'
|
107 |
if not exists(root): |
108 |
mkdir(root) |
109 |
db = head + '/db'
|
110 |
params = { 'filepath': db,
|
111 |
'mappath' : root,
|
112 |
'blockpath': root,
|
113 |
'hashtype': hashtype,
|
114 |
'blocksize': blocksize }
|
115 |
|
116 |
hf = Filer(**params) |
117 |
self.hf = hf
|
118 |
self.block_size = blocksize
|
119 |
self.hash_algorithm = hashtype
|
120 |
|
121 |
def quit(self): |
122 |
self.hf.quit()
|
123 |
|
124 |
def get_account(self, account, root=ROOTNODE): |
125 |
#XXX: how are accounts created?
|
126 |
r = self.hf.file_lookup(root, account, create=1) |
127 |
if r is None: |
128 |
raise NameError("account '%s' not found" % (account,)) |
129 |
return r[0] |
130 |
|
131 |
def get_container(self, serial, name): |
132 |
r = self.hf.file_lookup(serial, name, create=0) |
133 |
if r is None: |
134 |
raise NameError("container '%s' not found" % (name,)) |
135 |
return r[0] |
136 |
|
137 |
@staticmethod
|
138 |
def insert_properties(meta, props): |
139 |
(serial, parent, path, size, |
140 |
population, popsize, source, mtime, cluster) = props |
141 |
meta['version'] = serial
|
142 |
meta['name'] = path
|
143 |
meta['bytes'] = size + popsize
|
144 |
meta['count'] = population
|
145 |
meta['created'] = mtime
|
146 |
meta['modified'] = mtime
|
147 |
return meta
|
148 |
|
149 |
def lookup_file(self, account, container, name, |
150 |
create=0, size=0, authorize=0, before=None): |
151 |
# XXX: account/container lookup cache?
|
152 |
serial = self.get_account(account)
|
153 |
serial = self.get_container(serial, container)
|
154 |
if before is None: |
155 |
before = float('inf') |
156 |
props = self.hf.file_lookup(serial, name,
|
157 |
create=create, size=size, before=before) |
158 |
if props is None: |
159 |
raise NameError("File '%s/%s/%s' does not exist" % |
160 |
(account, container, name)) |
161 |
return props
|
162 |
|
163 |
def can_read(self, user, account, container, path): |
164 |
path = "%s:%s:%s" % (account, container, path)
|
165 |
check = self.hf.access_check
|
166 |
return check(READ, path, user) or check(WRITE, path, user) |
167 |
|
168 |
def can_write(self, user, account, container, path): |
169 |
path = "%s:%s:%s" % (account, container, path)
|
170 |
check = self.hf.access_check
|
171 |
return check(WRITE, path, user)
|
172 |
|
173 |
@backend_method
|
174 |
def get_account_meta(self, user, account, until=None): |
175 |
if user != account:
|
176 |
raise NotAllowedError
|
177 |
|
178 |
hf = self.hf
|
179 |
before = float("inf") if until is None else until |
180 |
props = hf.file_lookup(ROOTNODE, account, create=0, before=before)
|
181 |
if props is None: |
182 |
#XXX: why not return error?
|
183 |
meta = {'name': account,
|
184 |
'count': 0, |
185 |
'bytes': 0} |
186 |
if until is not None: |
187 |
meta['until_timestamp'] = 0 |
188 |
return meta
|
189 |
|
190 |
serial = props[SERIAL] |
191 |
meta = dict(hf.file_get_attributes(serial))
|
192 |
meta['name'] = account
|
193 |
self.insert_properties(meta, props)
|
194 |
if until is not None: |
195 |
meta['until_timestamp'] = props[MTIME]
|
196 |
return meta
|
197 |
|
198 |
@backend_method
|
199 |
def update_account_meta(self, user, account, meta, replace=False): |
200 |
if user != account:
|
201 |
raise NotAllowedError
|
202 |
|
203 |
hf = self.hf
|
204 |
serial = self.get_account(account)
|
205 |
if replace:
|
206 |
hf.file_del_attributes(serial) |
207 |
|
208 |
hf.file_set_attributes(serial, meta.iteritems()) |
209 |
|
210 |
@backend_method
|
211 |
def get_account_groups(self, user, account): |
212 |
if user != account:
|
213 |
raise NotAllowedError
|
214 |
|
215 |
groups = defaultdict(list)
|
216 |
grouplist = self.hf.group_list("%s:" % user) |
217 |
for name, member in grouplist: |
218 |
groups[name].append(member) |
219 |
|
220 |
return groups
|
221 |
|
222 |
@backend_method
|
223 |
def update_account_groups(self, user, account, groups, replace=False): |
224 |
if user != account:
|
225 |
raise NotAllowedError
|
226 |
|
227 |
hf = self.hf
|
228 |
group_addmany = hf.group_addmany |
229 |
for name, members in groups: |
230 |
name = "%s:%s" % (user, name)
|
231 |
if not members: |
232 |
members = (name,) |
233 |
group_addmany(name, members) |
234 |
|
235 |
if not replace: |
236 |
return
|
237 |
|
238 |
groupnames = hf.group_list("%s:" % user)
|
239 |
group_destroy = hf.group_destroy |
240 |
for name in groupnames: |
241 |
if name not in groups: |
242 |
group_destroy(name) |
243 |
|
244 |
@backend_method
|
245 |
def delete_account(self, user, account): |
246 |
if user != account:
|
247 |
raise NotAllowedError
|
248 |
|
249 |
hf = self.hf
|
250 |
r = hf.file_lookup(ROOTNODE, account) |
251 |
if r is None or r[SERIAL] is None: |
252 |
raise NameError |
253 |
|
254 |
if r[POPULATION]:
|
255 |
raise IndexError |
256 |
|
257 |
hf.file_remove(r[SERIAL]) |
258 |
|
259 |
@backend_method
|
260 |
def put_container(self, user, account, name, policy=None): |
261 |
if user != account:
|
262 |
raise NotAllowedError
|
263 |
|
264 |
parent = self.get_account(account)
|
265 |
hf = self.hf
|
266 |
r = hf.file_lookup(parent, name) |
267 |
if r is not None: |
268 |
raise NameError("%s:%s already exists" % (account, name)) |
269 |
|
270 |
serial, mtime = hf.file_create(parent, name, 0)
|
271 |
return serial, mtime
|
272 |
|
273 |
@backend_method
|
274 |
def delete_container(self, user, account, name): |
275 |
if user != account:
|
276 |
raise NotAllowedError
|
277 |
|
278 |
serial = self.get_account(account)
|
279 |
serial = self.get_container(serial, name)
|
280 |
r = self.hf.file_remove(serial)
|
281 |
if r is None: |
282 |
raise AssertionError("This should not have happened.") |
283 |
elif r == 0: |
284 |
#XXX: if container has only trashed objects, is it empty or not?
|
285 |
msg = "'%s:%s' not empty. will not delete." % (account, name)
|
286 |
raise IndexError(msg) |
287 |
|
288 |
@backend_method
|
289 |
def get_container_meta(self, user, account, name, until=None): |
290 |
if user != account:
|
291 |
raise NotAllowedError
|
292 |
|
293 |
serial = self.get_account(account)
|
294 |
hf = self.hf
|
295 |
before = float('inf') if until is None else until |
296 |
props = hf.file_lookup(serial, name, before=before) |
297 |
if props is None: |
298 |
raise NameError("%s:%s does not exist" % (account, name)) |
299 |
serial = props[0]
|
300 |
|
301 |
r = hf.file_get_attributes(serial) |
302 |
meta = dict(r)
|
303 |
self.insert_properties(meta, props)
|
304 |
if until:
|
305 |
meta['until_timestamp'] = props[MTIME]
|
306 |
return meta
|
307 |
|
308 |
@backend_method
|
309 |
def update_container_meta(self, user, account, name, meta, replace=False): |
310 |
if user != account:
|
311 |
raise NotAllowedError
|
312 |
|
313 |
serial = self.get_account(account)
|
314 |
serial = self.get_container(serial, name)
|
315 |
hf = self.hf
|
316 |
if replace:
|
317 |
hf.file_del_attributes(serial) |
318 |
|
319 |
hf.file_set_attributes(serial, meta.iteritems()) |
320 |
|
321 |
@backend_method
|
322 |
def get_container_policy(self, user, account, container): |
323 |
return {}
|
324 |
|
325 |
@backend_method
|
326 |
def update_container_policy(self, user, account, |
327 |
container, policy, replace=0):
|
328 |
return
|
329 |
|
330 |
@backend_method
|
331 |
def list_containers(self, user, account, |
332 |
marker=None, limit=10000, until=None): |
333 |
if not marker: |
334 |
marker = ''
|
335 |
if limit is None: |
336 |
limit = -1
|
337 |
serial = self.get_account(account)
|
338 |
r = self.hf.file_list(serial, '', start=marker, limit=limit) |
339 |
objects, prefixes = r |
340 |
return [(o[PATH], o[SERIAL]) for o in objects] |
341 |
|
342 |
@backend_method
|
343 |
def list_objects(self, user, account, container, prefix='', |
344 |
delimiter=None, marker=None, limit=10000, |
345 |
virtual=True, keys=[], until=None): |
346 |
if user != account:
|
347 |
raise NotAllowedError
|
348 |
|
349 |
if not marker: |
350 |
#XXX: '' is a valid string, should be None instead
|
351 |
marker = None
|
352 |
if limit is None: |
353 |
#XXX: limit should be an integer
|
354 |
limit = -1
|
355 |
|
356 |
if not delimiter: |
357 |
delimiter = None
|
358 |
|
359 |
serial = self.get_account(account)
|
360 |
serial = self.get_container(serial, container)
|
361 |
hf = self.hf
|
362 |
filterq = ','.join(keys) if keys else None |
363 |
r = hf.file_list(serial, prefix=prefix, start=marker, |
364 |
versions=0, filterq=filterq,
|
365 |
delimiter=delimiter, limit=limit) |
366 |
objects, prefixes = r |
367 |
# XXX: virtual is not needed since we get them anyway
|
368 |
#if virtual:
|
369 |
# return [(p, None) for p in prefixes]
|
370 |
|
371 |
objects = ((o[PATH], o[MTIME]) for o in objects) |
372 |
prefixes = ((p, None) for p in prefixes) if virtual else () |
373 |
return list(merge(objects, prefixes)) |
374 |
|
375 |
@backend_method
|
376 |
def list_object_meta(self, user, account, container, until=None): |
377 |
serial = self.get_account(account)
|
378 |
serial = self.get_container(serial, container)
|
379 |
return self.hf.file_list_attributes(serial) |
380 |
|
381 |
@backend_method
|
382 |
def get_object_meta(self, user, account, container, name, until=None): |
383 |
props = self.lookup_file(account, container, name,
|
384 |
before=until, authorize=2)
|
385 |
serial = props[0]
|
386 |
meta = dict(self.hf.file_get_attributes(serial)) |
387 |
self.insert_properties(meta, props)
|
388 |
meta['version_timestamp'] = props[MTIME]
|
389 |
meta['modified_by'] = 'fuck you' |
390 |
return meta
|
391 |
|
392 |
@backend_method
|
393 |
def update_object_meta(self, user, account, container, |
394 |
name, meta, replace=False):
|
395 |
props = self.lookup_file(account, container, name, authorize=1) |
396 |
serial = props[0]
|
397 |
hf = self.hf
|
398 |
if replace:
|
399 |
hf.file_del_attributes(serial) |
400 |
hf.file_set_attributes(serial, meta.iteritems()) |
401 |
|
402 |
@backend_method
|
403 |
def get_object_permissions(self, user, account, container, name): |
404 |
path = "%s/%s/%s" % (account, container, name)
|
405 |
perms = defaultdict(list)
|
406 |
for access, ident in self.hf.access_list(path): |
407 |
if access == READ and ident: |
408 |
perms['read'].append(ident)
|
409 |
if access == WRITE and ident: |
410 |
perms['write'].append(ident)
|
411 |
return path, perms
|
412 |
|
413 |
@backend_method
|
414 |
def update_object_permissions(self, user, account, container, name, permissions): |
415 |
if user != account:
|
416 |
return NotAllowedError
|
417 |
|
418 |
hf = self.hf
|
419 |
path = "%s/%s/%s" % (account, container, name)
|
420 |
feature = hf.feature_create() |
421 |
hf.access_grant(READ, path, members=permissions['read'])
|
422 |
hf.access_grant(WRITE, path, members=permissions['write'])
|
423 |
r = hf.xfeature_bestow(path, feature) |
424 |
assert(not r) |
425 |
|
426 |
@backend_method
|
427 |
def get_object_public(self, user, account, container, name): |
428 |
"""Return the public URL of the object if applicable."""
|
429 |
return None |
430 |
|
431 |
@backend_method
|
432 |
def update_object_public(self, user, account, container, name, public): |
433 |
"""Update the public status of the object."""
|
434 |
return
|
435 |
|
436 |
@backend_method
|
437 |
def get_object_hashmap(self, user, account, container, name, version=None): |
438 |
hf = self.hf
|
439 |
if version is None: |
440 |
props = self.lookup_file(account, container, name, authorize=2) |
441 |
else:
|
442 |
props = hf.file_get_properties(version) |
443 |
serial = props[SERIAL] |
444 |
size = props[SIZE] |
445 |
hashes = [hexlify(h) for h in hf.map_retr(serial)] |
446 |
return size, hashes
|
447 |
|
448 |
@backend_method
|
449 |
def update_object_hashmap(self, user, account, container, |
450 |
name, size, hashmap, meta=None,
|
451 |
replace_meta=0, permissions=None): |
452 |
hf = self.hf
|
453 |
props = self.lookup_file(account, container, name,
|
454 |
create=1, size=size, authorize=1) |
455 |
serial = props[SERIAL] |
456 |
if size != props[SIZE]:
|
457 |
hf.file_increment(serial, size) |
458 |
hf.map_stor(serial, (unhexlify(h) for h in hashmap)) |
459 |
if replace_meta:
|
460 |
hf.file_del_attributes(serial) |
461 |
if meta:
|
462 |
hf.file_set_attributes(serial, meta.iteritems()) |
463 |
|
464 |
@backend_method
|
465 |
def copy_object(self, user, account, src_container, src_name, |
466 |
dest_container, dest_name, dest_meta={}, |
467 |
replace_meta=False, permissions=None, src_version=None): |
468 |
hf = self.hf
|
469 |
#FIXME: authorization
|
470 |
if src_version:
|
471 |
props = hf.file_get_properties(src_version) |
472 |
else:
|
473 |
acc = self.get_account(account)
|
474 |
srccont = self.get_container(acc, src_container)
|
475 |
props = hf.file_lookup(srccont, src_name) |
476 |
if props is None: |
477 |
raise NameError |
478 |
srcobj = props[SERIAL] |
479 |
|
480 |
dstcont = self.get_container(acc, dest_container)
|
481 |
serial, mtime = hf.file_copy(srcobj, dstcont, dest_name) |
482 |
if replace_meta:
|
483 |
hf.file_del_attributes(serial) |
484 |
hf.file_set_attributes(serial, dest_meta.iteritems()) |
485 |
|
486 |
@backend_method
|
487 |
def move_object(self, user, account, src_container, src_name, |
488 |
dest_container, dest_name, dest_meta={}, |
489 |
replace_meta=False, permissions=None, src_version=None): |
490 |
hf = self.hf
|
491 |
#FIXME: authorization
|
492 |
if src_version:
|
493 |
props = hf.file_get_properties(src_version) |
494 |
else:
|
495 |
acc = self.get_account(account)
|
496 |
srccont = self.get_container(acc, src_container)
|
497 |
props = hf.file_lookup(srccont, src_name) |
498 |
if props is None: |
499 |
raise NameError |
500 |
srcobj = props[SERIAL] |
501 |
|
502 |
dstcont = self.get_container(acc, dest_container)
|
503 |
serial, mtime = hf.file_copy(srcobj, dstcont, dest_name) |
504 |
if replace_meta:
|
505 |
hf.file_del_attributes(serial) |
506 |
hf.file_set_attributes(serial, dest_meta.iteritems()) |
507 |
hf.file_remove(srcobj) |
508 |
|
509 |
@backend_method
|
510 |
def delete_object(self, user, account, container, name): |
511 |
if user != account:
|
512 |
raise NotAllowedError
|
513 |
|
514 |
props = self.lookup_file(account, container, name, authorize=WRITE)
|
515 |
serial = props[SERIAL] |
516 |
hf = self.hf
|
517 |
#XXX: see delete_container, for empty-trashed ambiguty
|
518 |
# for now, we will just remove the object
|
519 |
hf.file_remove(serial) |
520 |
#hf.file_recluster(serial, TRASH_CLUSTER)
|
521 |
#XXX: Where to unset sharing? on file trash or on trash purge?
|
522 |
# We'll do it on file trash, but should we trash all versions?
|
523 |
hf.xfeature_disinherit("%s/%s/%s" % (account, container, name))
|
524 |
|
525 |
@backend_method
|
526 |
def purge_trash(self, user, acount, container): |
527 |
if user != account:
|
528 |
raise NotAllowedError
|
529 |
|
530 |
serial = self.get_account(account)
|
531 |
serial = self.get_container(serial, container)
|
532 |
self.hf.file_purge_cluster(serial, cluster=TRASH_CLUSTER)
|
533 |
|
534 |
@backend_method(autocommit=0) |
535 |
def get_block(self, blkhash): |
536 |
blocks = self.hf.block_retr((unhexlify(blkhash),))
|
537 |
if not blocks: |
538 |
raise NameError |
539 |
return blocks[0] |
540 |
|
541 |
@backend_method(autocommit=0) |
542 |
def put_block(self, data): |
543 |
hashes, absent = self.hf.block_stor((data,))
|
544 |
#XXX: why hexlify hashes?
|
545 |
return hexlify(hashes[0]) |
546 |
|
547 |
@backend_method(autocommit=0) |
548 |
def update_block(self, blkhash, data, offset=0): |
549 |
h, e = self.hf.block_delta(unhexlify(blkhash), ((offset, data),))
|
550 |
return hexlify(h)
|
551 |
|