root / snf-pithos-backend / pithos / backends / modular.py @ 4d15c94e
History | View | Annotate | Download (54.9 kB)
1 |
# Copyright 2011-2012 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 |
import sys |
35 |
import os |
36 |
import time |
37 |
import uuid as uuidlib |
38 |
import logging |
39 |
import hashlib |
40 |
import binascii |
41 |
|
42 |
from base import DEFAULT_QUOTA, DEFAULT_VERSIONING, NotAllowedError, QuotaError, BaseBackend |
43 |
|
44 |
# Stripped-down version of the HashMap class found in tools.
|
45 |
class HashMap(list): |
46 |
|
47 |
def __init__(self, blocksize, blockhash): |
48 |
super(HashMap, self).__init__() |
49 |
self.blocksize = blocksize
|
50 |
self.blockhash = blockhash
|
51 |
|
52 |
def _hash_raw(self, v): |
53 |
h = hashlib.new(self.blockhash)
|
54 |
h.update(v) |
55 |
return h.digest()
|
56 |
|
57 |
def hash(self): |
58 |
if len(self) == 0: |
59 |
return self._hash_raw('') |
60 |
if len(self) == 1: |
61 |
return self.__getitem__(0) |
62 |
|
63 |
h = list(self) |
64 |
s = 2
|
65 |
while s < len(h): |
66 |
s = s * 2
|
67 |
h += [('\x00' * len(h[0]))] * (s - len(h)) |
68 |
while len(h) > 1: |
69 |
h = [self._hash_raw(h[x] + h[x + 1]) for x in range(0, len(h), 2)] |
70 |
return h[0] |
71 |
|
72 |
# Default modules and settings.
|
73 |
DEFAULT_DB_MODULE = 'pithos.backends.lib.sqlalchemy'
|
74 |
DEFAULT_DB_CONNECTION = 'sqlite:///backend.db'
|
75 |
DEFAULT_BLOCK_MODULE = 'pithos.backends.lib.hashfiler'
|
76 |
DEFAULT_BLOCK_PATH = 'data/'
|
77 |
DEFAULT_BLOCK_UMASK = 0o022
|
78 |
#DEFAULT_QUEUE_MODULE = 'pithos.backends.lib.rabbitmq'
|
79 |
#DEFAULT_QUEUE_CONNECTION = 'rabbitmq://guest:guest@localhost:5672/pithos'
|
80 |
|
81 |
QUEUE_MESSAGE_KEY_PREFIX = 'pithos.%s'
|
82 |
QUEUE_CLIENT_ID = 'pithos'
|
83 |
QUEUE_INSTANCE_ID = '1'
|
84 |
|
85 |
( CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED ) = range(3) |
86 |
|
87 |
inf = float('inf') |
88 |
|
89 |
ULTIMATE_ANSWER = 42
|
90 |
|
91 |
|
92 |
logger = logging.getLogger(__name__) |
93 |
|
94 |
|
95 |
def backend_method(func=None, autocommit=1): |
96 |
if func is None: |
97 |
def fn(func): |
98 |
return backend_method(func, autocommit)
|
99 |
return fn
|
100 |
|
101 |
if not autocommit: |
102 |
return func
|
103 |
def fn(self, *args, **kw): |
104 |
self.wrapper.execute()
|
105 |
try:
|
106 |
self.messages = []
|
107 |
ret = func(self, *args, **kw)
|
108 |
for m in self.messages: |
109 |
self.queue.send(*m)
|
110 |
self.wrapper.commit()
|
111 |
return ret
|
112 |
except:
|
113 |
self.wrapper.rollback()
|
114 |
raise
|
115 |
return fn
|
116 |
|
117 |
|
118 |
class ModularBackend(BaseBackend): |
119 |
"""A modular backend.
|
120 |
|
121 |
Uses modules for SQL functions and storage.
|
122 |
"""
|
123 |
|
124 |
def __init__(self, db_module=None, db_connection=None, |
125 |
block_module=None, block_path=None, block_umask=None, |
126 |
queue_module=None, queue_connection=None): |
127 |
db_module = db_module or DEFAULT_DB_MODULE
|
128 |
db_connection = db_connection or DEFAULT_DB_CONNECTION
|
129 |
block_module = block_module or DEFAULT_BLOCK_MODULE
|
130 |
block_path = block_path or DEFAULT_BLOCK_PATH
|
131 |
block_umask = block_umask or DEFAULT_BLOCK_UMASK
|
132 |
#queue_module = queue_module or DEFAULT_QUEUE_MODULE
|
133 |
#queue_connection = queue_connection or DEFAULT_QUEUE_CONNECTION
|
134 |
|
135 |
self.hash_algorithm = 'sha256' |
136 |
self.block_size = 4 * 1024 * 1024 # 4MB |
137 |
|
138 |
self.default_policy = {'quota': DEFAULT_QUOTA, 'versioning': DEFAULT_VERSIONING} |
139 |
|
140 |
def load_module(m): |
141 |
__import__(m)
|
142 |
return sys.modules[m]
|
143 |
|
144 |
self.db_module = load_module(db_module)
|
145 |
self.wrapper = self.db_module.DBWrapper(db_connection) |
146 |
params = {'wrapper': self.wrapper} |
147 |
self.permissions = self.db_module.Permissions(**params) |
148 |
for x in ['READ', 'WRITE']: |
149 |
setattr(self, x, getattr(self.db_module, x)) |
150 |
self.node = self.db_module.Node(**params) |
151 |
for x in ['ROOTNODE', 'SERIAL', 'HASH', 'SIZE', 'TYPE', 'MTIME', 'MUSER', 'UUID', 'CHECKSUM', 'CLUSTER', 'MATCH_PREFIX', 'MATCH_EXACT']: |
152 |
setattr(self, x, getattr(self.db_module, x)) |
153 |
|
154 |
self.block_module = load_module(block_module)
|
155 |
params = {'path': block_path,
|
156 |
'block_size': self.block_size, |
157 |
'hash_algorithm': self.hash_algorithm, |
158 |
'umask': block_umask}
|
159 |
self.store = self.block_module.Store(**params) |
160 |
|
161 |
if queue_module and queue_connection: |
162 |
self.queue_module = load_module(queue_module)
|
163 |
params = {'exchange': queue_connection,
|
164 |
'client_id': QUEUE_CLIENT_ID}
|
165 |
self.queue = self.queue_module.Queue(**params) |
166 |
else:
|
167 |
class NoQueue: |
168 |
def send(self, *args): |
169 |
pass
|
170 |
|
171 |
def close(self): |
172 |
pass
|
173 |
|
174 |
self.queue = NoQueue()
|
175 |
|
176 |
def close(self): |
177 |
self.wrapper.close()
|
178 |
self.queue.close()
|
179 |
|
180 |
@backend_method
|
181 |
def list_accounts(self, user, marker=None, limit=10000): |
182 |
"""Return a list of accounts the user can access."""
|
183 |
|
184 |
logger.debug("list_accounts: %s %s %s", user, marker, limit)
|
185 |
allowed = self._allowed_accounts(user)
|
186 |
start, limit = self._list_limits(allowed, marker, limit)
|
187 |
return allowed[start:start + limit]
|
188 |
|
189 |
@backend_method
|
190 |
def get_account_meta(self, user, account, domain, until=None, include_user_defined=True): |
191 |
"""Return a dictionary with the account metadata for the domain."""
|
192 |
|
193 |
logger.debug("get_account_meta: %s %s %s %s", user, account, domain, until)
|
194 |
path, node = self._lookup_account(account, user == account)
|
195 |
if user != account:
|
196 |
if until or node is None or account not in self._allowed_accounts(user): |
197 |
raise NotAllowedError
|
198 |
try:
|
199 |
props = self._get_properties(node, until)
|
200 |
mtime = props[self.MTIME]
|
201 |
except NameError: |
202 |
props = None
|
203 |
mtime = until |
204 |
count, bytes, tstamp = self._get_statistics(node, until) |
205 |
tstamp = max(tstamp, mtime)
|
206 |
if until is None: |
207 |
modified = tstamp |
208 |
else:
|
209 |
modified = self._get_statistics(node)[2] # Overall last modification. |
210 |
modified = max(modified, mtime)
|
211 |
|
212 |
if user != account:
|
213 |
meta = {'name': account}
|
214 |
else:
|
215 |
meta = {} |
216 |
if props is not None and include_user_defined: |
217 |
meta.update(dict(self.node.attribute_get(props[self.SERIAL], domain))) |
218 |
if until is not None: |
219 |
meta.update({'until_timestamp': tstamp})
|
220 |
meta.update({'name': account, 'count': count, 'bytes': bytes}) |
221 |
meta.update({'modified': modified})
|
222 |
return meta
|
223 |
|
224 |
@backend_method
|
225 |
def update_account_meta(self, user, account, domain, meta, replace=False): |
226 |
"""Update the metadata associated with the account for the domain."""
|
227 |
|
228 |
logger.debug("update_account_meta: %s %s %s %s %s", user, account, domain, meta, replace)
|
229 |
if user != account:
|
230 |
raise NotAllowedError
|
231 |
path, node = self._lookup_account(account, True) |
232 |
self._put_metadata(user, node, domain, meta, replace)
|
233 |
|
234 |
@backend_method
|
235 |
def get_account_groups(self, user, account): |
236 |
"""Return a dictionary with the user groups defined for this account."""
|
237 |
|
238 |
logger.debug("get_account_groups: %s %s", user, account)
|
239 |
if user != account:
|
240 |
if account not in self._allowed_accounts(user): |
241 |
raise NotAllowedError
|
242 |
return {}
|
243 |
self._lookup_account(account, True) |
244 |
return self.permissions.group_dict(account) |
245 |
|
246 |
@backend_method
|
247 |
def update_account_groups(self, user, account, groups, replace=False): |
248 |
"""Update the groups associated with the account."""
|
249 |
|
250 |
logger.debug("update_account_groups: %s %s %s %s", user, account, groups, replace)
|
251 |
if user != account:
|
252 |
raise NotAllowedError
|
253 |
self._lookup_account(account, True) |
254 |
self._check_groups(groups)
|
255 |
if replace:
|
256 |
self.permissions.group_destroy(account)
|
257 |
for k, v in groups.iteritems(): |
258 |
if not replace: # If not already deleted. |
259 |
self.permissions.group_delete(account, k)
|
260 |
if v:
|
261 |
self.permissions.group_addmany(account, k, v)
|
262 |
|
263 |
@backend_method
|
264 |
def get_account_policy(self, user, account): |
265 |
"""Return a dictionary with the account policy."""
|
266 |
|
267 |
logger.debug("get_account_policy: %s %s", user, account)
|
268 |
if user != account:
|
269 |
if account not in self._allowed_accounts(user): |
270 |
raise NotAllowedError
|
271 |
return {}
|
272 |
path, node = self._lookup_account(account, True) |
273 |
return self._get_policy(node) |
274 |
|
275 |
@backend_method
|
276 |
def update_account_policy(self, user, account, policy, replace=False): |
277 |
"""Update the policy associated with the account."""
|
278 |
|
279 |
logger.debug("update_account_policy: %s %s %s %s", user, account, policy, replace)
|
280 |
if user != account:
|
281 |
raise NotAllowedError
|
282 |
path, node = self._lookup_account(account, True) |
283 |
self._check_policy(policy)
|
284 |
self._put_policy(node, policy, replace)
|
285 |
|
286 |
@backend_method
|
287 |
def put_account(self, user, account, policy={}): |
288 |
"""Create a new account with the given name."""
|
289 |
|
290 |
logger.debug("put_account: %s %s %s", user, account, policy)
|
291 |
if user != account:
|
292 |
raise NotAllowedError
|
293 |
node = self.node.node_lookup(account)
|
294 |
if node is not None: |
295 |
raise NameError('Account already exists') |
296 |
if policy:
|
297 |
self._check_policy(policy)
|
298 |
node = self._put_path(user, self.ROOTNODE, account) |
299 |
self._put_policy(node, policy, True) |
300 |
|
301 |
@backend_method
|
302 |
def delete_account(self, user, account): |
303 |
"""Delete the account with the given name."""
|
304 |
|
305 |
logger.debug("delete_account: %s %s", user, account)
|
306 |
if user != account:
|
307 |
raise NotAllowedError
|
308 |
node = self.node.node_lookup(account)
|
309 |
if node is None: |
310 |
return
|
311 |
if not self.node.node_remove(node): |
312 |
raise IndexError('Account is not empty') |
313 |
self.permissions.group_destroy(account)
|
314 |
|
315 |
@backend_method
|
316 |
def list_containers(self, user, account, marker=None, limit=10000, shared=False, until=None, public=False): |
317 |
"""Return a list of containers existing under an account."""
|
318 |
|
319 |
logger.debug("list_containers: %s %s %s %s %s %s %s", user, account, marker, limit, shared, until, public)
|
320 |
if user != account:
|
321 |
if until or account not in self._allowed_accounts(user): |
322 |
raise NotAllowedError
|
323 |
allowed = self._allowed_containers(user, account)
|
324 |
start, limit = self._list_limits(allowed, marker, limit)
|
325 |
return allowed[start:start + limit]
|
326 |
if shared or public: |
327 |
allowed = [] |
328 |
if shared:
|
329 |
allowed.extend([x.split('/', 2)[1] for x in self.permissions.access_list_shared(account)]) |
330 |
if public:
|
331 |
allowed.extend([x[0].split('/', 2)[1] for x in self.permissions.public_list(account)]) |
332 |
allowed = list(set(allowed)) |
333 |
allowed.sort() |
334 |
start, limit = self._list_limits(allowed, marker, limit)
|
335 |
return allowed[start:start + limit]
|
336 |
node = self.node.node_lookup(account)
|
337 |
return [x[0] for x in self._list_object_properties(node, account, '', '/', marker, limit, False, None, [], until)] |
338 |
|
339 |
@backend_method
|
340 |
def list_container_meta(self, user, account, container, domain, until=None): |
341 |
"""Return a list with all the container's object meta keys for the domain."""
|
342 |
|
343 |
logger.debug("list_container_meta: %s %s %s %s %s", user, account, container, domain, until)
|
344 |
allowed = [] |
345 |
if user != account:
|
346 |
if until:
|
347 |
raise NotAllowedError
|
348 |
allowed = self.permissions.access_list_paths(user, '/'.join((account, container))) |
349 |
if not allowed: |
350 |
raise NotAllowedError
|
351 |
path, node = self._lookup_container(account, container)
|
352 |
before = until if until is not None else inf |
353 |
allowed = self._get_formatted_paths(allowed)
|
354 |
return self.node.latest_attribute_keys(node, domain, before, CLUSTER_DELETED, allowed) |
355 |
|
356 |
@backend_method
|
357 |
def get_container_meta(self, user, account, container, domain, until=None, include_user_defined=True): |
358 |
"""Return a dictionary with the container metadata for the domain."""
|
359 |
|
360 |
logger.debug("get_container_meta: %s %s %s %s %s", user, account, container, domain, until)
|
361 |
if user != account:
|
362 |
if until or container not in self._allowed_containers(user, account): |
363 |
raise NotAllowedError
|
364 |
path, node = self._lookup_container(account, container)
|
365 |
props = self._get_properties(node, until)
|
366 |
mtime = props[self.MTIME]
|
367 |
count, bytes, tstamp = self._get_statistics(node, until) |
368 |
tstamp = max(tstamp, mtime)
|
369 |
if until is None: |
370 |
modified = tstamp |
371 |
else:
|
372 |
modified = self._get_statistics(node)[2] # Overall last modification. |
373 |
modified = max(modified, mtime)
|
374 |
|
375 |
if user != account:
|
376 |
meta = {'name': container}
|
377 |
else:
|
378 |
meta = {} |
379 |
if include_user_defined:
|
380 |
meta.update(dict(self.node.attribute_get(props[self.SERIAL], domain))) |
381 |
if until is not None: |
382 |
meta.update({'until_timestamp': tstamp})
|
383 |
meta.update({'name': container, 'count': count, 'bytes': bytes}) |
384 |
meta.update({'modified': modified})
|
385 |
return meta
|
386 |
|
387 |
@backend_method
|
388 |
def update_container_meta(self, user, account, container, domain, meta, replace=False): |
389 |
"""Update the metadata associated with the container for the domain."""
|
390 |
|
391 |
logger.debug("update_container_meta: %s %s %s %s %s %s", user, account, container, domain, meta, replace)
|
392 |
if user != account:
|
393 |
raise NotAllowedError
|
394 |
path, node = self._lookup_container(account, container)
|
395 |
src_version_id, dest_version_id = self._put_metadata(user, node, domain, meta, replace)
|
396 |
if src_version_id is not None: |
397 |
versioning = self._get_policy(node)['versioning'] |
398 |
if versioning != 'auto': |
399 |
self.node.version_remove(src_version_id)
|
400 |
|
401 |
@backend_method
|
402 |
def get_container_policy(self, user, account, container): |
403 |
"""Return a dictionary with the container policy."""
|
404 |
|
405 |
logger.debug("get_container_policy: %s %s %s", user, account, container)
|
406 |
if user != account:
|
407 |
if container not in self._allowed_containers(user, account): |
408 |
raise NotAllowedError
|
409 |
return {}
|
410 |
path, node = self._lookup_container(account, container)
|
411 |
return self._get_policy(node) |
412 |
|
413 |
@backend_method
|
414 |
def update_container_policy(self, user, account, container, policy, replace=False): |
415 |
"""Update the policy associated with the container."""
|
416 |
|
417 |
logger.debug("update_container_policy: %s %s %s %s %s", user, account, container, policy, replace)
|
418 |
if user != account:
|
419 |
raise NotAllowedError
|
420 |
path, node = self._lookup_container(account, container)
|
421 |
self._check_policy(policy)
|
422 |
self._put_policy(node, policy, replace)
|
423 |
|
424 |
@backend_method
|
425 |
def put_container(self, user, account, container, policy={}): |
426 |
"""Create a new container with the given name."""
|
427 |
|
428 |
logger.debug("put_container: %s %s %s %s", user, account, container, policy)
|
429 |
if user != account:
|
430 |
raise NotAllowedError
|
431 |
try:
|
432 |
path, node = self._lookup_container(account, container)
|
433 |
except NameError: |
434 |
pass
|
435 |
else:
|
436 |
raise NameError('Container already exists') |
437 |
if policy:
|
438 |
self._check_policy(policy)
|
439 |
path = '/'.join((account, container))
|
440 |
node = self._put_path(user, self._lookup_account(account, True)[1], path) |
441 |
self._put_policy(node, policy, True) |
442 |
|
443 |
@backend_method
|
444 |
def delete_container(self, user, account, container, until=None, prefix='', delimiter=None): |
445 |
"""Delete/purge the container with the given name."""
|
446 |
|
447 |
logger.debug("delete_container: %s %s %s %s %s %s", user, account, container, until, prefix, delimiter)
|
448 |
if user != account:
|
449 |
raise NotAllowedError
|
450 |
path, node = self._lookup_container(account, container)
|
451 |
|
452 |
if until is not None: |
453 |
hashes, size = self.node.node_purge_children(node, until, CLUSTER_HISTORY)
|
454 |
for h in hashes: |
455 |
self.store.map_delete(h)
|
456 |
self.node.node_purge_children(node, until, CLUSTER_DELETED)
|
457 |
self._report_size_change(user, account, -size, {'action': 'container purge'}) |
458 |
return
|
459 |
|
460 |
if self._get_statistics(node)[0] > 0: |
461 |
raise IndexError('Container is not empty') |
462 |
hashes, size = self.node.node_purge_children(node, inf, CLUSTER_HISTORY)
|
463 |
for h in hashes: |
464 |
self.store.map_delete(h)
|
465 |
self.node.node_purge_children(node, inf, CLUSTER_DELETED)
|
466 |
self.node.node_remove(node)
|
467 |
self._report_size_change(user, account, -size, {'action': 'container delete'}) |
468 |
|
469 |
def _list_objects(self, user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, all_props, public): |
470 |
if user != account and until: |
471 |
raise NotAllowedError
|
472 |
allowed = self._list_object_permissions(user, account, container, prefix, shared, public)
|
473 |
if (shared or public) and not allowed: |
474 |
return []
|
475 |
path, node = self._lookup_container(account, container)
|
476 |
allowed = self._get_formatted_paths(allowed)
|
477 |
return self._list_object_properties(node, path, prefix, delimiter, marker, limit, virtual, domain, keys, until, size_range, allowed, all_props) |
478 |
|
479 |
def _list_objects_no_limit(self, user, account, container, prefix, delimiter, virtual, domain, keys, shared, until, size_range, all_props, public): |
480 |
objects = [] |
481 |
while True: |
482 |
marker = objects[-1] if objects else None |
483 |
limit = 10000
|
484 |
l = self._list_objects(user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, all_props, public)
|
485 |
objects.extend(l) |
486 |
if not l or len(l) < limit: |
487 |
break
|
488 |
return objects
|
489 |
|
490 |
def _list_object_permissions(self, user, account, container, prefix, shared, public): |
491 |
allowed = [] |
492 |
path = '/'.join((account, container, prefix)).rstrip('/') |
493 |
if user != account:
|
494 |
allowed = self.permissions.access_list_paths(user, path)
|
495 |
if not allowed: |
496 |
raise NotAllowedError
|
497 |
else:
|
498 |
allowed = [] |
499 |
if shared:
|
500 |
allowed.extend(self.permissions.access_list_shared(path))
|
501 |
if public:
|
502 |
allowed.extend([x[0] for x in self.permissions.public_list(path)]) |
503 |
allowed = list(set(allowed)) |
504 |
allowed.sort() |
505 |
if not allowed: |
506 |
return []
|
507 |
return allowed
|
508 |
|
509 |
@backend_method
|
510 |
def list_objects(self, user, account, container, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, domain=None, keys=[], shared=False, until=None, size_range=None, public=False): |
511 |
"""Return a list of object (name, version_id) tuples existing under a container."""
|
512 |
|
513 |
logger.debug("list_objects: %s %s %s %s %s %s %s %s %s %s %s %s %s %s", user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, public)
|
514 |
return self._list_objects(user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, False, public) |
515 |
|
516 |
@backend_method
|
517 |
def list_object_meta(self, user, account, container, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, domain=None, keys=[], shared=False, until=None, size_range=None, public=False): |
518 |
"""Return a list of object metadata dicts existing under a container."""
|
519 |
|
520 |
logger.debug("list_object_meta: %s %s %s %s %s %s %s %s %s %s %s %s %s %s", user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, public)
|
521 |
props = self._list_objects(user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, True, public) |
522 |
objects = [] |
523 |
for p in props: |
524 |
if len(p) == 2: |
525 |
objects.append({'subdir': p[0]}) |
526 |
else:
|
527 |
objects.append({'name': p[0], |
528 |
'bytes': p[self.SIZE + 1], |
529 |
'type': p[self.TYPE + 1], |
530 |
'hash': p[self.HASH + 1], |
531 |
'version': p[self.SERIAL + 1], |
532 |
'version_timestamp': p[self.MTIME + 1], |
533 |
'modified': p[self.MTIME + 1] if until is None else None, |
534 |
'modified_by': p[self.MUSER + 1], |
535 |
'uuid': p[self.UUID + 1], |
536 |
'checksum': p[self.CHECKSUM + 1]}) |
537 |
return objects
|
538 |
|
539 |
@backend_method
|
540 |
def list_object_permissions(self, user, account, container, prefix=''): |
541 |
"""Return a list of paths that enforce permissions under a container."""
|
542 |
|
543 |
logger.debug("list_object_permissions: %s %s %s %s", user, account, container, prefix)
|
544 |
return self._list_object_permissions(user, account, container, prefix, True, False) |
545 |
|
546 |
@backend_method
|
547 |
def list_object_public(self, user, account, container, prefix=''): |
548 |
"""Return a dict mapping paths to public ids for objects that are public under a container."""
|
549 |
|
550 |
logger.debug("list_object_public: %s %s %s %s", user, account, container, prefix)
|
551 |
public = {} |
552 |
for path, p in self.permissions.public_list('/'.join((account, container, prefix))): |
553 |
public[path] = p + ULTIMATE_ANSWER |
554 |
return public
|
555 |
|
556 |
@backend_method
|
557 |
def get_object_meta(self, user, account, container, name, domain, version=None, include_user_defined=True): |
558 |
"""Return a dictionary with the object metadata for the domain."""
|
559 |
|
560 |
logger.debug("get_object_meta: %s %s %s %s %s %s", user, account, container, name, domain, version)
|
561 |
self._can_read(user, account, container, name)
|
562 |
path, node = self._lookup_object(account, container, name)
|
563 |
props = self._get_version(node, version)
|
564 |
if version is None: |
565 |
modified = props[self.MTIME]
|
566 |
else:
|
567 |
try:
|
568 |
modified = self._get_version(node)[self.MTIME] # Overall last modification. |
569 |
except NameError: # Object may be deleted. |
570 |
del_props = self.node.version_lookup(node, inf, CLUSTER_DELETED)
|
571 |
if del_props is None: |
572 |
raise NameError('Object does not exist') |
573 |
modified = del_props[self.MTIME]
|
574 |
|
575 |
meta = {} |
576 |
if include_user_defined:
|
577 |
meta.update(dict(self.node.attribute_get(props[self.SERIAL], domain))) |
578 |
meta.update({'name': name,
|
579 |
'bytes': props[self.SIZE], |
580 |
'type': props[self.TYPE], |
581 |
'hash': props[self.HASH], |
582 |
'version': props[self.SERIAL], |
583 |
'version_timestamp': props[self.MTIME], |
584 |
'modified': modified,
|
585 |
'modified_by': props[self.MUSER], |
586 |
'uuid': props[self.UUID], |
587 |
'checksum': props[self.CHECKSUM]}) |
588 |
return meta
|
589 |
|
590 |
@backend_method
|
591 |
def update_object_meta(self, user, account, container, name, domain, meta, replace=False): |
592 |
"""Update the metadata associated with the object for the domain and return the new version."""
|
593 |
|
594 |
logger.debug("update_object_meta: %s %s %s %s %s %s %s", user, account, container, name, domain, meta, replace)
|
595 |
self._can_write(user, account, container, name)
|
596 |
path, node = self._lookup_object(account, container, name)
|
597 |
src_version_id, dest_version_id = self._put_metadata(user, node, domain, meta, replace)
|
598 |
self._apply_versioning(account, container, src_version_id)
|
599 |
return dest_version_id
|
600 |
|
601 |
@backend_method
|
602 |
def get_object_permissions(self, user, account, container, name): |
603 |
"""Return the action allowed on the object, the path
|
604 |
from which the object gets its permissions from,
|
605 |
along with a dictionary containing the permissions."""
|
606 |
|
607 |
logger.debug("get_object_permissions: %s %s %s %s", user, account, container, name)
|
608 |
allowed = 'write'
|
609 |
permissions_path = self._get_permissions_path(account, container, name)
|
610 |
if user != account:
|
611 |
if self.permissions.access_check(permissions_path, self.WRITE, user): |
612 |
allowed = 'write'
|
613 |
elif self.permissions.access_check(permissions_path, self.READ, user): |
614 |
allowed = 'read'
|
615 |
else:
|
616 |
raise NotAllowedError
|
617 |
self._lookup_object(account, container, name)
|
618 |
return (allowed, permissions_path, self.permissions.access_get(permissions_path)) |
619 |
|
620 |
@backend_method
|
621 |
def update_object_permissions(self, user, account, container, name, permissions): |
622 |
"""Update the permissions associated with the object."""
|
623 |
|
624 |
logger.debug("update_object_permissions: %s %s %s %s %s", user, account, container, name, permissions)
|
625 |
if user != account:
|
626 |
raise NotAllowedError
|
627 |
path = self._lookup_object(account, container, name)[0] |
628 |
self._check_permissions(path, permissions)
|
629 |
self.permissions.access_set(path, permissions)
|
630 |
self._report_sharing_change(user, account, path, {'members':self.permissions.access_members(path)}) |
631 |
|
632 |
@backend_method
|
633 |
def get_object_public(self, user, account, container, name): |
634 |
"""Return the public id of the object if applicable."""
|
635 |
|
636 |
logger.debug("get_object_public: %s %s %s %s", user, account, container, name)
|
637 |
self._can_read(user, account, container, name)
|
638 |
path = self._lookup_object(account, container, name)[0] |
639 |
p = self.permissions.public_get(path)
|
640 |
if p is not None: |
641 |
p += ULTIMATE_ANSWER |
642 |
return p
|
643 |
|
644 |
@backend_method
|
645 |
def update_object_public(self, user, account, container, name, public): |
646 |
"""Update the public status of the object."""
|
647 |
|
648 |
logger.debug("update_object_public: %s %s %s %s %s", user, account, container, name, public)
|
649 |
self._can_write(user, account, container, name)
|
650 |
path = self._lookup_object(account, container, name)[0] |
651 |
if not public: |
652 |
self.permissions.public_unset(path)
|
653 |
else:
|
654 |
self.permissions.public_set(path)
|
655 |
|
656 |
@backend_method
|
657 |
def get_object_hashmap(self, user, account, container, name, version=None): |
658 |
"""Return the object's size and a list with partial hashes."""
|
659 |
|
660 |
logger.debug("get_object_hashmap: %s %s %s %s %s", user, account, container, name, version)
|
661 |
self._can_read(user, account, container, name)
|
662 |
path, node = self._lookup_object(account, container, name)
|
663 |
props = self._get_version(node, version)
|
664 |
hashmap = self.store.map_get(binascii.unhexlify(props[self.HASH])) |
665 |
return props[self.SIZE], [binascii.hexlify(x) for x in hashmap] |
666 |
|
667 |
def _update_object_hash(self, user, account, container, name, size, type, hash, checksum, domain, meta, replace_meta, permissions, src_node=None, src_version_id=None, is_copy=False): |
668 |
if permissions is not None and user != account: |
669 |
raise NotAllowedError
|
670 |
self._can_write(user, account, container, name)
|
671 |
if permissions is not None: |
672 |
path = '/'.join((account, container, name))
|
673 |
self._check_permissions(path, permissions)
|
674 |
|
675 |
account_path, account_node = self._lookup_account(account, True) |
676 |
container_path, container_node = self._lookup_container(account, container)
|
677 |
path, node = self._put_object_node(container_path, container_node, name)
|
678 |
pre_version_id, dest_version_id = self._put_version_duplicate(user, node, src_node=src_node, size=size, type=type, hash=hash, checksum=checksum, is_copy=is_copy) |
679 |
|
680 |
# Handle meta.
|
681 |
if src_version_id is None: |
682 |
src_version_id = pre_version_id |
683 |
self._put_metadata_duplicate(src_version_id, dest_version_id, domain, meta, replace_meta)
|
684 |
|
685 |
# Check quota.
|
686 |
del_size = self._apply_versioning(account, container, pre_version_id)
|
687 |
size_delta = size - del_size |
688 |
if size_delta > 0: |
689 |
account_quota = long(self._get_policy(account_node)['quota']) |
690 |
container_quota = long(self._get_policy(container_node)['quota']) |
691 |
if (account_quota > 0 and self._get_statistics(account_node)[1] + size_delta > account_quota) or \ |
692 |
(container_quota > 0 and self._get_statistics(container_node)[1] + size_delta > container_quota): |
693 |
# This must be executed in a transaction, so the version is never created if it fails.
|
694 |
raise QuotaError
|
695 |
self._report_size_change(user, account, size_delta, {'action': 'object update'}) |
696 |
|
697 |
if permissions is not None: |
698 |
self.permissions.access_set(path, permissions)
|
699 |
self._report_sharing_change(user, account, path, {'members':self.permissions.access_members(path)}) |
700 |
|
701 |
self._report_object_change(user, account, path, details={'version': dest_version_id, 'action': 'object update'}) |
702 |
return dest_version_id
|
703 |
|
704 |
@backend_method
|
705 |
def update_object_hashmap(self, user, account, container, name, size, type, hashmap, checksum, domain, meta={}, replace_meta=False, permissions=None): |
706 |
"""Create/update an object with the specified size and partial hashes."""
|
707 |
|
708 |
logger.debug("update_object_hashmap: %s %s %s %s %s %s %s %s", user, account, container, name, size, type, hashmap, checksum) |
709 |
if size == 0: # No such thing as an empty hashmap. |
710 |
hashmap = [self.put_block('')] |
711 |
map = HashMap(self.block_size, self.hash_algorithm) |
712 |
map.extend([binascii.unhexlify(x) for x in hashmap]) |
713 |
missing = self.store.block_search(map) |
714 |
if missing:
|
715 |
ie = IndexError()
|
716 |
ie.data = [binascii.hexlify(x) for x in missing] |
717 |
raise ie
|
718 |
|
719 |
hash = map.hash()
|
720 |
dest_version_id = self._update_object_hash(user, account, container, name, size, type, binascii.hexlify(hash), checksum, domain, meta, replace_meta, permissions) |
721 |
self.store.map_put(hash, map) |
722 |
return dest_version_id
|
723 |
|
724 |
@backend_method
|
725 |
def update_object_checksum(self, user, account, container, name, version, checksum): |
726 |
"""Update an object's checksum."""
|
727 |
|
728 |
logger.debug("update_object_checksum: %s %s %s %s %s %s", user, account, container, name, version, checksum)
|
729 |
# Update objects with greater version and same hashmap and size (fix metadata updates).
|
730 |
self._can_write(user, account, container, name)
|
731 |
path, node = self._lookup_object(account, container, name)
|
732 |
props = self._get_version(node, version)
|
733 |
versions = self.node.node_get_versions(node)
|
734 |
for x in versions: |
735 |
if x[self.SERIAL] >= int(version) and x[self.HASH] == props[self.HASH] and x[self.SIZE] == props[self.SIZE]: |
736 |
self.node.version_put_property(x[self.SERIAL], 'checksum', checksum) |
737 |
|
738 |
def _copy_object(self, user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, dest_domain=None, dest_meta={}, replace_meta=False, permissions=None, src_version=None, is_move=False, delimiter=None): |
739 |
dest_version_ids = [] |
740 |
self._can_read(user, src_account, src_container, src_name)
|
741 |
path, node = self._lookup_object(src_account, src_container, src_name)
|
742 |
# TODO: Will do another fetch of the properties in duplicate version...
|
743 |
props = self._get_version(node, src_version) # Check to see if source exists. |
744 |
src_version_id = props[self.SERIAL]
|
745 |
hash = props[self.HASH]
|
746 |
size = props[self.SIZE]
|
747 |
is_copy = not is_move and (src_account, src_container, src_name) != (dest_account, dest_container, dest_name) # New uuid. |
748 |
dest_version_ids.append(self._update_object_hash(user, dest_account, dest_container, dest_name, size, type, hash, None, dest_domain, dest_meta, replace_meta, permissions, src_node=node, src_version_id=src_version_id, is_copy=is_copy)) |
749 |
if is_move:
|
750 |
self._delete_object(user, src_account, src_container, src_name)
|
751 |
|
752 |
if delimiter:
|
753 |
prefix = src_name + delimiter if not src_name.endswith(delimiter) else src_name |
754 |
src_names = self._list_objects_no_limit(user, src_account, src_container, prefix, delimiter=None, virtual=True, domain=None, keys=[], shared=False, until=None, size_range=None, all_props=True, public=False) |
755 |
paths = [elem[0] for elem in src_names] |
756 |
nodes = [elem[2] for elem in src_names] |
757 |
# TODO: Will do another fetch of the properties in duplicate version...
|
758 |
props = self._get_versions(nodes) # Check to see if source exists. |
759 |
|
760 |
for prop, path, node in zip(props, paths, nodes): |
761 |
src_version_id = prop[self.SERIAL]
|
762 |
hash = prop[self.HASH]
|
763 |
vtype = prop[self.TYPE]
|
764 |
dest_prefix = dest_name + delimiter if not dest_name.endswith(delimiter) else dest_name |
765 |
vdest_name = path.replace(prefix, dest_prefix, 1)
|
766 |
dest_version_ids.append(self._update_object_hash(user, dest_account, dest_container, vdest_name, size, vtype, hash, None, dest_domain, dest_meta, replace_meta, permissions, src_node=node, src_version_id=src_version_id, is_copy=is_copy)) |
767 |
if is_move:
|
768 |
self._delete_object(user, src_account, src_container, path)
|
769 |
return dest_version_ids[0] if len(dest_version_ids) == 1 else dest_version_ids |
770 |
|
771 |
@backend_method
|
772 |
def copy_object(self, user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta={}, replace_meta=False, permissions=None, src_version=None, delimiter=None): |
773 |
"""Copy an object's data and metadata."""
|
774 |
|
775 |
logger.debug("copy_object: %s %s %s %s %s %s %s %s %s %s %s %s %s %s", user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions, src_version, delimiter) |
776 |
dest_version_id = self._copy_object(user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions, src_version, False, delimiter) |
777 |
return dest_version_id
|
778 |
|
779 |
@backend_method
|
780 |
def move_object(self, user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta={}, replace_meta=False, permissions=None, delimiter=None): |
781 |
"""Move an object's data and metadata."""
|
782 |
|
783 |
logger.debug("move_object: %s %s %s %s %s %s %s %s %s %s %s %s %s", user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions, delimiter) |
784 |
if user != src_account:
|
785 |
raise NotAllowedError
|
786 |
dest_version_id = self._copy_object(user, src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions, None, True, delimiter) |
787 |
return dest_version_id
|
788 |
|
789 |
def _delete_object(self, user, account, container, name, until=None, delimiter=None): |
790 |
if user != account:
|
791 |
raise NotAllowedError
|
792 |
|
793 |
if until is not None: |
794 |
path = '/'.join((account, container, name))
|
795 |
node = self.node.node_lookup(path)
|
796 |
if node is None: |
797 |
return
|
798 |
hashes = [] |
799 |
size = 0
|
800 |
h, s = self.node.node_purge(node, until, CLUSTER_NORMAL)
|
801 |
hashes += h |
802 |
size += s |
803 |
h, s = self.node.node_purge(node, until, CLUSTER_HISTORY)
|
804 |
hashes += h |
805 |
size += s |
806 |
for h in hashes: |
807 |
self.store.map_delete(h)
|
808 |
self.node.node_purge(node, until, CLUSTER_DELETED)
|
809 |
try:
|
810 |
props = self._get_version(node)
|
811 |
except NameError: |
812 |
self.permissions.access_clear(path)
|
813 |
self._report_size_change(user, account, -size, {'action': 'object purge'}) |
814 |
return
|
815 |
|
816 |
path, node = self._lookup_object(account, container, name)
|
817 |
src_version_id, dest_version_id = self._put_version_duplicate(user, node, size=0, type='', hash=None, checksum='', cluster=CLUSTER_DELETED) |
818 |
del_size = self._apply_versioning(account, container, src_version_id)
|
819 |
if del_size:
|
820 |
self._report_size_change(user, account, -del_size, {'action': 'object delete'}) |
821 |
self._report_object_change(user, account, path, details={'action': 'object delete'}) |
822 |
self.permissions.access_clear(path)
|
823 |
|
824 |
if delimiter:
|
825 |
prefix = name + delimiter if not name.endswith(delimiter) else name |
826 |
src_names = self._list_objects_no_limit(user, account, container, prefix, delimiter=None, virtual=True, domain=None, keys=[], shared=False, until=None, size_range=None, all_props=True, public=False) |
827 |
paths = [] |
828 |
for t in src_names: |
829 |
path = '/'.join((account, container, t[0])) |
830 |
node = t[2]
|
831 |
src_version_id, dest_version_id = self._put_version_duplicate(user, node, size=0, type='', hash=None, checksum='', cluster=CLUSTER_DELETED) |
832 |
del_size = self._apply_versioning(account, container, src_version_id)
|
833 |
if del_size:
|
834 |
self._report_size_change(user, account, -del_size, {'action': 'object delete'}) |
835 |
self._report_object_change(user, account, path, details={'action': 'object delete'}) |
836 |
paths.append(path) |
837 |
self.permissions.access_clear_bulk(paths)
|
838 |
|
839 |
@backend_method
|
840 |
def delete_object(self, user, account, container, name, until=None, prefix='', delimiter=None): |
841 |
"""Delete/purge an object."""
|
842 |
|
843 |
logger.debug("delete_object: %s %s %s %s %s %s %s", user, account, container, name, until, prefix, delimiter)
|
844 |
self._delete_object(user, account, container, name, until, delimiter)
|
845 |
|
846 |
@backend_method
|
847 |
def list_versions(self, user, account, container, name): |
848 |
"""Return a list of all (version, version_timestamp) tuples for an object."""
|
849 |
|
850 |
logger.debug("list_versions: %s %s %s %s", user, account, container, name)
|
851 |
self._can_read(user, account, container, name)
|
852 |
path, node = self._lookup_object(account, container, name)
|
853 |
versions = self.node.node_get_versions(node)
|
854 |
return [[x[self.SERIAL], x[self.MTIME]] for x in versions if x[self.CLUSTER] != CLUSTER_DELETED] |
855 |
|
856 |
@backend_method
|
857 |
def get_uuid(self, user, uuid): |
858 |
"""Return the (account, container, name) for the UUID given."""
|
859 |
|
860 |
logger.debug("get_uuid: %s %s", user, uuid)
|
861 |
info = self.node.latest_uuid(uuid)
|
862 |
if info is None: |
863 |
raise NameError |
864 |
path, serial = info |
865 |
account, container, name = path.split('/', 2) |
866 |
self._can_read(user, account, container, name)
|
867 |
return (account, container, name)
|
868 |
|
869 |
@backend_method
|
870 |
def get_public(self, user, public): |
871 |
"""Return the (account, container, name) for the public id given."""
|
872 |
|
873 |
logger.debug("get_public: %s %s", user, public)
|
874 |
if public is None or public < ULTIMATE_ANSWER: |
875 |
raise NameError |
876 |
path = self.permissions.public_path(public - ULTIMATE_ANSWER)
|
877 |
if path is None: |
878 |
raise NameError |
879 |
account, container, name = path.split('/', 2) |
880 |
self._can_read(user, account, container, name)
|
881 |
return (account, container, name)
|
882 |
|
883 |
@backend_method(autocommit=0) |
884 |
def get_block(self, hash): |
885 |
"""Return a block's data."""
|
886 |
|
887 |
logger.debug("get_block: %s", hash) |
888 |
block = self.store.block_get(binascii.unhexlify(hash)) |
889 |
if not block: |
890 |
raise NameError('Block does not exist') |
891 |
return block
|
892 |
|
893 |
@backend_method(autocommit=0) |
894 |
def put_block(self, data): |
895 |
"""Store a block and return the hash."""
|
896 |
|
897 |
logger.debug("put_block: %s", len(data)) |
898 |
return binascii.hexlify(self.store.block_put(data)) |
899 |
|
900 |
@backend_method(autocommit=0) |
901 |
def update_block(self, hash, data, offset=0): |
902 |
"""Update a known block and return the hash."""
|
903 |
|
904 |
logger.debug("update_block: %s %s %s", hash, len(data), offset) |
905 |
if offset == 0 and len(data) == self.block_size: |
906 |
return self.put_block(data) |
907 |
h = self.store.block_update(binascii.unhexlify(hash), offset, data) |
908 |
return binascii.hexlify(h)
|
909 |
|
910 |
# Path functions.
|
911 |
|
912 |
def _generate_uuid(self): |
913 |
return str(uuidlib.uuid4()) |
914 |
|
915 |
def _put_object_node(self, path, parent, name): |
916 |
path = '/'.join((path, name))
|
917 |
node = self.node.node_lookup(path)
|
918 |
if node is None: |
919 |
node = self.node.node_create(parent, path)
|
920 |
return path, node
|
921 |
|
922 |
def _put_path(self, user, parent, path): |
923 |
node = self.node.node_create(parent, path)
|
924 |
self.node.version_create(node, None, 0, '', None, user, self._generate_uuid(), '', CLUSTER_NORMAL) |
925 |
return node
|
926 |
|
927 |
def _lookup_account(self, account, create=True): |
928 |
node = self.node.node_lookup(account)
|
929 |
if node is None and create: |
930 |
node = self._put_path(account, self.ROOTNODE, account) # User is account. |
931 |
return account, node
|
932 |
|
933 |
def _lookup_container(self, account, container): |
934 |
path = '/'.join((account, container))
|
935 |
node = self.node.node_lookup(path)
|
936 |
if node is None: |
937 |
raise NameError('Container does not exist') |
938 |
return path, node
|
939 |
|
940 |
def _lookup_object(self, account, container, name): |
941 |
path = '/'.join((account, container, name))
|
942 |
node = self.node.node_lookup(path)
|
943 |
if node is None: |
944 |
raise NameError('Object does not exist') |
945 |
return path, node
|
946 |
|
947 |
def _get_properties(self, node, until=None): |
948 |
"""Return properties until the timestamp given."""
|
949 |
|
950 |
before = until if until is not None else inf |
951 |
props = self.node.version_lookup(node, before, CLUSTER_NORMAL)
|
952 |
if props is None and until is not None: |
953 |
props = self.node.version_lookup(node, before, CLUSTER_HISTORY)
|
954 |
if props is None: |
955 |
raise NameError('Path does not exist') |
956 |
return props
|
957 |
|
958 |
def _get_statistics(self, node, until=None): |
959 |
"""Return count, sum of size and latest timestamp of everything under node."""
|
960 |
|
961 |
if until is None: |
962 |
stats = self.node.statistics_get(node, CLUSTER_NORMAL)
|
963 |
else:
|
964 |
stats = self.node.statistics_latest(node, until, CLUSTER_DELETED)
|
965 |
if stats is None: |
966 |
stats = (0, 0, 0) |
967 |
return stats
|
968 |
|
969 |
def _get_version(self, node, version=None): |
970 |
if version is None: |
971 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
972 |
if props is None: |
973 |
raise NameError('Object does not exist') |
974 |
else:
|
975 |
try:
|
976 |
version = int(version)
|
977 |
except ValueError: |
978 |
raise IndexError('Version does not exist') |
979 |
props = self.node.version_get_properties(version)
|
980 |
if props is None or props[self.CLUSTER] == CLUSTER_DELETED: |
981 |
raise IndexError('Version does not exist') |
982 |
return props
|
983 |
|
984 |
def _get_versions(self, nodes, version=None): |
985 |
if version is None: |
986 |
props = self.node.version_lookup_bulk(nodes, inf, CLUSTER_NORMAL)
|
987 |
if not props: |
988 |
raise NameError('Object does not exist') |
989 |
else:
|
990 |
try:
|
991 |
version = int(version)
|
992 |
except ValueError: |
993 |
raise IndexError('Version does not exist') |
994 |
props = self.node.version_get_properties(version)
|
995 |
if props is None or props[self.CLUSTER] == CLUSTER_DELETED: |
996 |
raise IndexError('Version does not exist') |
997 |
return props
|
998 |
|
999 |
def _put_version_duplicate(self, user, node, src_node=None, size=None, type=None, hash=None, checksum=None, cluster=CLUSTER_NORMAL, is_copy=False): |
1000 |
"""Create a new version of the node."""
|
1001 |
|
1002 |
props = self.node.version_lookup(node if src_node is None else src_node, inf, CLUSTER_NORMAL) |
1003 |
if props is not None: |
1004 |
src_version_id = props[self.SERIAL]
|
1005 |
src_hash = props[self.HASH]
|
1006 |
src_size = props[self.SIZE]
|
1007 |
src_type = props[self.TYPE]
|
1008 |
src_checksum = props[self.CHECKSUM]
|
1009 |
else:
|
1010 |
src_version_id = None
|
1011 |
src_hash = None
|
1012 |
src_size = 0
|
1013 |
src_type = ''
|
1014 |
src_checksum = ''
|
1015 |
if size is None: # Set metadata. |
1016 |
hash = src_hash # This way hash can be set to None (account or container).
|
1017 |
size = src_size |
1018 |
if type is None: |
1019 |
type = src_type |
1020 |
if checksum is None: |
1021 |
checksum = src_checksum |
1022 |
uuid = self._generate_uuid() if (is_copy or src_version_id is None) else props[self.UUID] |
1023 |
|
1024 |
if src_node is None: |
1025 |
pre_version_id = src_version_id |
1026 |
else:
|
1027 |
pre_version_id = None
|
1028 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1029 |
if props is not None: |
1030 |
pre_version_id = props[self.SERIAL]
|
1031 |
if pre_version_id is not None: |
1032 |
self.node.version_recluster(pre_version_id, CLUSTER_HISTORY)
|
1033 |
|
1034 |
dest_version_id, mtime = self.node.version_create(node, hash, size, type, src_version_id, user, uuid, checksum, cluster) |
1035 |
return pre_version_id, dest_version_id
|
1036 |
|
1037 |
def _put_metadata_duplicate(self, src_version_id, dest_version_id, domain, meta, replace=False): |
1038 |
if src_version_id is not None: |
1039 |
self.node.attribute_copy(src_version_id, dest_version_id)
|
1040 |
if not replace: |
1041 |
self.node.attribute_del(dest_version_id, domain, (k for k, v in meta.iteritems() if v == '')) |
1042 |
self.node.attribute_set(dest_version_id, domain, ((k, v) for k, v in meta.iteritems() if v != '')) |
1043 |
else:
|
1044 |
self.node.attribute_del(dest_version_id, domain)
|
1045 |
self.node.attribute_set(dest_version_id, domain, ((k, v) for k, v in meta.iteritems())) |
1046 |
|
1047 |
def _put_metadata(self, user, node, domain, meta, replace=False): |
1048 |
"""Create a new version and store metadata."""
|
1049 |
|
1050 |
src_version_id, dest_version_id = self._put_version_duplicate(user, node)
|
1051 |
self._put_metadata_duplicate(src_version_id, dest_version_id, domain, meta, replace)
|
1052 |
return src_version_id, dest_version_id
|
1053 |
|
1054 |
def _list_limits(self, listing, marker, limit): |
1055 |
start = 0
|
1056 |
if marker:
|
1057 |
try:
|
1058 |
start = listing.index(marker) + 1
|
1059 |
except ValueError: |
1060 |
pass
|
1061 |
if not limit or limit > 10000: |
1062 |
limit = 10000
|
1063 |
return start, limit
|
1064 |
|
1065 |
def _list_object_properties(self, parent, path, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, domain=None, keys=[], until=None, size_range=None, allowed=[], all_props=False): |
1066 |
cont_prefix = path + '/'
|
1067 |
prefix = cont_prefix + prefix |
1068 |
start = cont_prefix + marker if marker else None |
1069 |
before = until if until is not None else inf |
1070 |
filterq = keys if domain else [] |
1071 |
sizeq = size_range |
1072 |
|
1073 |
objects, prefixes = self.node.latest_version_list(parent, prefix, delimiter, start, limit, before, CLUSTER_DELETED, allowed, domain, filterq, sizeq, all_props)
|
1074 |
objects.extend([(p, None) for p in prefixes] if virtual else []) |
1075 |
objects.sort(key=lambda x: x[0]) |
1076 |
objects = [(x[0][len(cont_prefix):],) + x[1:] for x in objects] |
1077 |
|
1078 |
start, limit = self._list_limits([x[0] for x in objects], marker, limit) |
1079 |
return objects[start:start + limit]
|
1080 |
|
1081 |
# Reporting functions.
|
1082 |
|
1083 |
def _report_size_change(self, user, account, size, details={}): |
1084 |
logger.debug("_report_size_change: %s %s %s %s", user, account, size, details)
|
1085 |
account_node = self._lookup_account(account, True)[1] |
1086 |
total = self._get_statistics(account_node)[1] |
1087 |
details.update({'user': user, 'total': total}) |
1088 |
self.messages.append((QUEUE_MESSAGE_KEY_PREFIX % ('resource.diskspace',), account, QUEUE_INSTANCE_ID, 'diskspace', float(size), details)) |
1089 |
|
1090 |
def _report_object_change(self, user, account, path, details={}): |
1091 |
logger.debug("_report_object_change: %s %s %s %s", user, account, path, details)
|
1092 |
details.update({'user': user})
|
1093 |
self.messages.append((QUEUE_MESSAGE_KEY_PREFIX % ('object',), account, QUEUE_INSTANCE_ID, 'object', path, details)) |
1094 |
|
1095 |
def _report_sharing_change(self, user, account, path, details={}): |
1096 |
logger.debug("_report_permissions_change: %s %s %s %s", user, account, path, details)
|
1097 |
details.update({'user': user})
|
1098 |
self.messages.append((QUEUE_MESSAGE_KEY_PREFIX % ('sharing',), account, QUEUE_INSTANCE_ID, 'sharing', path, details)) |
1099 |
|
1100 |
# Policy functions.
|
1101 |
|
1102 |
def _check_policy(self, policy): |
1103 |
for k in policy.keys(): |
1104 |
if policy[k] == '': |
1105 |
policy[k] = self.default_policy.get(k)
|
1106 |
for k, v in policy.iteritems(): |
1107 |
if k == 'quota': |
1108 |
q = int(v) # May raise ValueError. |
1109 |
if q < 0: |
1110 |
raise ValueError |
1111 |
elif k == 'versioning': |
1112 |
if v not in ['auto', 'none']: |
1113 |
raise ValueError |
1114 |
else:
|
1115 |
raise ValueError |
1116 |
|
1117 |
def _put_policy(self, node, policy, replace): |
1118 |
if replace:
|
1119 |
for k, v in self.default_policy.iteritems(): |
1120 |
if k not in policy: |
1121 |
policy[k] = v |
1122 |
self.node.policy_set(node, policy)
|
1123 |
|
1124 |
def _get_policy(self, node): |
1125 |
policy = self.default_policy.copy()
|
1126 |
policy.update(self.node.policy_get(node))
|
1127 |
return policy
|
1128 |
|
1129 |
def _apply_versioning(self, account, container, version_id): |
1130 |
"""Delete the provided version if such is the policy.
|
1131 |
Return size of object removed.
|
1132 |
"""
|
1133 |
|
1134 |
if version_id is None: |
1135 |
return 0 |
1136 |
path, node = self._lookup_container(account, container)
|
1137 |
versioning = self._get_policy(node)['versioning'] |
1138 |
if versioning != 'auto': |
1139 |
hash, size = self.node.version_remove(version_id) |
1140 |
self.store.map_delete(hash) |
1141 |
return size
|
1142 |
return 0 |
1143 |
|
1144 |
# Access control functions.
|
1145 |
|
1146 |
def _check_groups(self, groups): |
1147 |
# raise ValueError('Bad characters in groups')
|
1148 |
pass
|
1149 |
|
1150 |
def _check_permissions(self, path, permissions): |
1151 |
# raise ValueError('Bad characters in permissions')
|
1152 |
pass
|
1153 |
|
1154 |
def _get_formatted_paths(self, paths): |
1155 |
formatted = [] |
1156 |
for p in paths: |
1157 |
node = self.node.node_lookup(p)
|
1158 |
if node is not None: |
1159 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1160 |
if props is not None: |
1161 |
if props[self.TYPE].split(';', 1)[0].strip() in ('application/directory', 'application/folder'): |
1162 |
formatted.append((p.rstrip('/') + '/', self.MATCH_PREFIX)) |
1163 |
formatted.append((p, self.MATCH_EXACT))
|
1164 |
return formatted
|
1165 |
|
1166 |
def _get_permissions_path(self, account, container, name): |
1167 |
path = '/'.join((account, container, name))
|
1168 |
permission_paths = self.permissions.access_inherit(path)
|
1169 |
permission_paths.sort() |
1170 |
permission_paths.reverse() |
1171 |
for p in permission_paths: |
1172 |
if p == path:
|
1173 |
return p
|
1174 |
else:
|
1175 |
if p.count('/') < 2: |
1176 |
continue
|
1177 |
node = self.node.node_lookup(p)
|
1178 |
if node is not None: |
1179 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1180 |
if props is not None: |
1181 |
if props[self.TYPE].split(';', 1)[0].strip() in ('application/directory', 'application/folder'): |
1182 |
return p
|
1183 |
return None |
1184 |
|
1185 |
def _can_read(self, user, account, container, name): |
1186 |
if user == account:
|
1187 |
return True |
1188 |
path = '/'.join((account, container, name))
|
1189 |
if self.permissions.public_get(path) is not None: |
1190 |
return True |
1191 |
path = self._get_permissions_path(account, container, name)
|
1192 |
if not path: |
1193 |
raise NotAllowedError
|
1194 |
if not self.permissions.access_check(path, self.READ, user) and not self.permissions.access_check(path, self.WRITE, user): |
1195 |
raise NotAllowedError
|
1196 |
|
1197 |
def _can_write(self, user, account, container, name): |
1198 |
if user == account:
|
1199 |
return True |
1200 |
path = '/'.join((account, container, name))
|
1201 |
path = self._get_permissions_path(account, container, name)
|
1202 |
if not path: |
1203 |
raise NotAllowedError
|
1204 |
if not self.permissions.access_check(path, self.WRITE, user): |
1205 |
raise NotAllowedError
|
1206 |
|
1207 |
def _allowed_accounts(self, user): |
1208 |
allow = set()
|
1209 |
for path in self.permissions.access_list_paths(user): |
1210 |
allow.add(path.split('/', 1)[0]) |
1211 |
return sorted(allow) |
1212 |
|
1213 |
def _allowed_containers(self, user, account): |
1214 |
allow = set()
|
1215 |
for path in self.permissions.access_list_paths(user, account): |
1216 |
allow.add(path.split('/', 2)[1]) |
1217 |
return sorted(allow) |