root / snf-pithos-backend / pithos / backends / modular.py @ 29148653
History | View | Annotate | Download (72.3 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 uuid as uuidlib |
36 |
import logging |
37 |
import hashlib |
38 |
import binascii |
39 |
|
40 |
try:
|
41 |
from astakosclient import AstakosClient |
42 |
except ImportError: |
43 |
AstakosClient = None
|
44 |
|
45 |
from base import (DEFAULT_ACCOUNT_QUOTA, DEFAULT_CONTAINER_QUOTA, |
46 |
DEFAULT_CONTAINER_VERSIONING, NotAllowedError, QuotaError, |
47 |
BaseBackend, AccountExists, ContainerExists, AccountNotEmpty, |
48 |
ContainerNotEmpty, ItemNotExists, VersionNotExists) |
49 |
|
50 |
|
51 |
class DisabledAstakosClient(object): |
52 |
def __init__(self, *args, **kwargs): |
53 |
self.args = args
|
54 |
self.kwargs = kwargs
|
55 |
|
56 |
def __getattr__(self, name): |
57 |
m = ("AstakosClient has been disabled, "
|
58 |
"yet an attempt to access it was made")
|
59 |
raise AssertionError(m) |
60 |
|
61 |
|
62 |
# Stripped-down version of the HashMap class found in tools.
|
63 |
|
64 |
class HashMap(list): |
65 |
|
66 |
def __init__(self, blocksize, blockhash): |
67 |
super(HashMap, self).__init__() |
68 |
self.blocksize = blocksize
|
69 |
self.blockhash = blockhash
|
70 |
|
71 |
def _hash_raw(self, v): |
72 |
h = hashlib.new(self.blockhash)
|
73 |
h.update(v) |
74 |
return h.digest()
|
75 |
|
76 |
def hash(self): |
77 |
if len(self) == 0: |
78 |
return self._hash_raw('') |
79 |
if len(self) == 1: |
80 |
return self.__getitem__(0) |
81 |
|
82 |
h = list(self) |
83 |
s = 2
|
84 |
while s < len(h): |
85 |
s = s * 2
|
86 |
h += [('\x00' * len(h[0]))] * (s - len(h)) |
87 |
while len(h) > 1: |
88 |
h = [self._hash_raw(h[x] + h[x + 1]) for x in range(0, len(h), 2)] |
89 |
return h[0] |
90 |
|
91 |
# Default modules and settings.
|
92 |
DEFAULT_DB_MODULE = 'pithos.backends.lib.sqlalchemy'
|
93 |
DEFAULT_DB_CONNECTION = 'sqlite:///backend.db'
|
94 |
DEFAULT_BLOCK_MODULE = 'pithos.backends.lib.hashfiler'
|
95 |
DEFAULT_BLOCK_PATH = 'data/'
|
96 |
DEFAULT_BLOCK_UMASK = 0o022
|
97 |
DEFAULT_BLOCK_SIZE = 4 * 1024 * 1024 # 4MB |
98 |
DEFAULT_HASH_ALGORITHM = 'sha256'
|
99 |
#DEFAULT_QUEUE_MODULE = 'pithos.backends.lib.rabbitmq'
|
100 |
DEFAULT_BLOCK_PARAMS = {'mappool': None, 'blockpool': None} |
101 |
#DEFAULT_QUEUE_HOSTS = '[amqp://guest:guest@localhost:5672]'
|
102 |
#DEFAULT_QUEUE_EXCHANGE = 'pithos'
|
103 |
DEFAULT_PUBLIC_URL_ALPHABET = ('0123456789'
|
104 |
'abcdefghijklmnopqrstuvwxyz'
|
105 |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
106 |
DEFAULT_PUBLIC_URL_SECURITY = 16
|
107 |
|
108 |
QUEUE_MESSAGE_KEY_PREFIX = 'pithos.%s'
|
109 |
QUEUE_CLIENT_ID = 'pithos'
|
110 |
QUEUE_INSTANCE_ID = '1'
|
111 |
|
112 |
(CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED) = range(3) |
113 |
|
114 |
inf = float('inf') |
115 |
|
116 |
ULTIMATE_ANSWER = 42
|
117 |
|
118 |
DEFAULT_SOURCE = 'system'
|
119 |
|
120 |
logger = logging.getLogger(__name__) |
121 |
|
122 |
|
123 |
class ModularBackend(BaseBackend): |
124 |
"""A modular backend.
|
125 |
|
126 |
Uses modules for SQL functions and storage.
|
127 |
"""
|
128 |
|
129 |
def __init__(self, db_module=None, db_connection=None, |
130 |
block_module=None, block_path=None, block_umask=None, |
131 |
block_size=None, hash_algorithm=None, |
132 |
queue_module=None, queue_hosts=None, queue_exchange=None, |
133 |
astakos_url=None, service_token=None, |
134 |
astakosclient_poolsize=None,
|
135 |
free_versioning=True, block_params=None, |
136 |
public_url_security=None,
|
137 |
public_url_alphabet=None,
|
138 |
account_quota_policy=None,
|
139 |
container_quota_policy=None,
|
140 |
container_versioning_policy=None):
|
141 |
db_module = db_module or DEFAULT_DB_MODULE
|
142 |
db_connection = db_connection or DEFAULT_DB_CONNECTION
|
143 |
block_module = block_module or DEFAULT_BLOCK_MODULE
|
144 |
block_path = block_path or DEFAULT_BLOCK_PATH
|
145 |
block_umask = block_umask or DEFAULT_BLOCK_UMASK
|
146 |
block_params = block_params or DEFAULT_BLOCK_PARAMS
|
147 |
block_size = block_size or DEFAULT_BLOCK_SIZE
|
148 |
hash_algorithm = hash_algorithm or DEFAULT_HASH_ALGORITHM
|
149 |
#queue_module = queue_module or DEFAULT_QUEUE_MODULE
|
150 |
account_quota_policy = account_quota_policy or DEFAULT_ACCOUNT_QUOTA
|
151 |
container_quota_policy = container_quota_policy \ |
152 |
or DEFAULT_CONTAINER_QUOTA
|
153 |
container_versioning_policy = container_versioning_policy \ |
154 |
or DEFAULT_CONTAINER_VERSIONING
|
155 |
|
156 |
self.default_account_policy = {'quota': account_quota_policy} |
157 |
self.default_container_policy = {
|
158 |
'quota': container_quota_policy,
|
159 |
'versioning': container_versioning_policy
|
160 |
} |
161 |
#queue_hosts = queue_hosts or DEFAULT_QUEUE_HOSTS
|
162 |
#queue_exchange = queue_exchange or DEFAULT_QUEUE_EXCHANGE
|
163 |
|
164 |
self.public_url_security = (public_url_security or |
165 |
DEFAULT_PUBLIC_URL_SECURITY) |
166 |
self.public_url_alphabet = (public_url_alphabet or |
167 |
DEFAULT_PUBLIC_URL_ALPHABET) |
168 |
|
169 |
self.hash_algorithm = hash_algorithm
|
170 |
self.block_size = block_size
|
171 |
self.free_versioning = free_versioning
|
172 |
|
173 |
def load_module(m): |
174 |
__import__(m)
|
175 |
return sys.modules[m]
|
176 |
|
177 |
self.db_module = load_module(db_module)
|
178 |
self.wrapper = self.db_module.DBWrapper(db_connection) |
179 |
params = {'wrapper': self.wrapper} |
180 |
self.permissions = self.db_module.Permissions(**params) |
181 |
self.config = self.db_module.Config(**params) |
182 |
self.commission_serials = self.db_module.QuotaholderSerial(**params) |
183 |
for x in ['READ', 'WRITE']: |
184 |
setattr(self, x, getattr(self.db_module, x)) |
185 |
self.node = self.db_module.Node(**params) |
186 |
for x in ['ROOTNODE', 'SERIAL', 'HASH', 'SIZE', 'TYPE', 'MTIME', |
187 |
'MUSER', 'UUID', 'CHECKSUM', 'CLUSTER', 'MATCH_PREFIX', |
188 |
'MATCH_EXACT']:
|
189 |
setattr(self, x, getattr(self.db_module, x)) |
190 |
|
191 |
self.block_module = load_module(block_module)
|
192 |
self.block_params = block_params
|
193 |
params = {'path': block_path,
|
194 |
'block_size': self.block_size, |
195 |
'hash_algorithm': self.hash_algorithm, |
196 |
'umask': block_umask}
|
197 |
params.update(self.block_params)
|
198 |
self.store = self.block_module.Store(**params) |
199 |
|
200 |
if queue_module and queue_hosts: |
201 |
self.queue_module = load_module(queue_module)
|
202 |
params = {'hosts': queue_hosts,
|
203 |
'exchange': queue_exchange,
|
204 |
'client_id': QUEUE_CLIENT_ID}
|
205 |
self.queue = self.queue_module.Queue(**params) |
206 |
else:
|
207 |
class NoQueue: |
208 |
def send(self, *args): |
209 |
pass
|
210 |
|
211 |
def close(self): |
212 |
pass
|
213 |
|
214 |
self.queue = NoQueue()
|
215 |
|
216 |
self.astakos_url = astakos_url
|
217 |
self.service_token = service_token
|
218 |
|
219 |
if not astakos_url or not AstakosClient: |
220 |
self.astakosclient = DisabledAstakosClient(
|
221 |
astakos_url, |
222 |
use_pool=True,
|
223 |
pool_size=astakosclient_poolsize) |
224 |
else:
|
225 |
self.astakosclient = AstakosClient(
|
226 |
astakos_url, |
227 |
use_pool=True,
|
228 |
pool_size=astakosclient_poolsize) |
229 |
|
230 |
self.serials = []
|
231 |
self.messages = []
|
232 |
|
233 |
def pre_exec(self, lock_container_path=False): |
234 |
self.lock_container_path = lock_container_path
|
235 |
self.wrapper.execute()
|
236 |
|
237 |
def post_exec(self, success_status=True): |
238 |
if success_status:
|
239 |
# send messages produced
|
240 |
for m in self.messages: |
241 |
self.queue.send(*m)
|
242 |
|
243 |
# register serials
|
244 |
if self.serials: |
245 |
self.commission_serials.insert_many(
|
246 |
self.serials)
|
247 |
|
248 |
# commit to ensure that the serials are registered
|
249 |
# even if resolve commission fails
|
250 |
self.wrapper.commit()
|
251 |
|
252 |
# start new transaction
|
253 |
self.wrapper.execute()
|
254 |
|
255 |
r = self.astakosclient.resolve_commissions(
|
256 |
token=self.service_token,
|
257 |
accept_serials=self.serials,
|
258 |
reject_serials=[]) |
259 |
self.commission_serials.delete_many(
|
260 |
r['accepted'])
|
261 |
|
262 |
self.wrapper.commit()
|
263 |
else:
|
264 |
if self.serials: |
265 |
self.astakosclient.resolve_commissions(
|
266 |
token=self.service_token,
|
267 |
accept_serials=[], |
268 |
reject_serials=self.serials)
|
269 |
self.wrapper.rollback()
|
270 |
|
271 |
def close(self): |
272 |
self.wrapper.close()
|
273 |
self.queue.close()
|
274 |
|
275 |
@property
|
276 |
def using_external_quotaholder(self): |
277 |
return not isinstance(self.astakosclient, DisabledAstakosClient) |
278 |
|
279 |
def list_accounts(self, user, marker=None, limit=10000): |
280 |
"""Return a list of accounts the user can access."""
|
281 |
|
282 |
logger.debug("list_accounts: %s %s %s", user, marker, limit)
|
283 |
allowed = self._allowed_accounts(user)
|
284 |
start, limit = self._list_limits(allowed, marker, limit)
|
285 |
return allowed[start:start + limit]
|
286 |
|
287 |
def get_account_meta( |
288 |
self, user, account, domain, until=None, include_user_defined=True, |
289 |
external_quota=None):
|
290 |
"""Return a dictionary with the account metadata for the domain."""
|
291 |
|
292 |
logger.debug( |
293 |
"get_account_meta: %s %s %s %s", user, account, domain, until)
|
294 |
path, node = self._lookup_account(account, user == account)
|
295 |
if user != account:
|
296 |
if until or (node is None) or (account not |
297 |
in self._allowed_accounts(user)): |
298 |
raise NotAllowedError
|
299 |
try:
|
300 |
props = self._get_properties(node, until)
|
301 |
mtime = props[self.MTIME]
|
302 |
except NameError: |
303 |
props = None
|
304 |
mtime = until |
305 |
count, bytes, tstamp = self._get_statistics(node, until, compute=True) |
306 |
tstamp = max(tstamp, mtime)
|
307 |
if until is None: |
308 |
modified = tstamp |
309 |
else:
|
310 |
modified = self._get_statistics(
|
311 |
node, compute=True)[2] # Overall last modification. |
312 |
modified = max(modified, mtime)
|
313 |
|
314 |
if user != account:
|
315 |
meta = {'name': account}
|
316 |
else:
|
317 |
meta = {} |
318 |
if props is not None and include_user_defined: |
319 |
meta.update( |
320 |
dict(self.node.attribute_get(props[self.SERIAL], domain))) |
321 |
if until is not None: |
322 |
meta.update({'until_timestamp': tstamp})
|
323 |
meta.update({'name': account, 'count': count, 'bytes': bytes}) |
324 |
if self.using_external_quotaholder: |
325 |
external_quota = external_quota or {}
|
326 |
meta['bytes'] = external_quota.get('usage', 0) |
327 |
meta.update({'modified': modified})
|
328 |
return meta
|
329 |
|
330 |
def update_account_meta(self, user, account, domain, meta, replace=False): |
331 |
"""Update the metadata associated with the account for the domain."""
|
332 |
|
333 |
logger.debug("update_account_meta: %s %s %s %s %s", user,
|
334 |
account, domain, meta, replace) |
335 |
if user != account:
|
336 |
raise NotAllowedError
|
337 |
path, node = self._lookup_account(account, True) |
338 |
self._put_metadata(user, node, domain, meta, replace,
|
339 |
update_statistics_ancestors_depth=-1)
|
340 |
|
341 |
def get_account_groups(self, user, account): |
342 |
"""Return a dictionary with the user groups defined for the account."""
|
343 |
|
344 |
logger.debug("get_account_groups: %s %s", user, account)
|
345 |
if user != account:
|
346 |
if account not in self._allowed_accounts(user): |
347 |
raise NotAllowedError
|
348 |
return {}
|
349 |
self._lookup_account(account, True) |
350 |
return self.permissions.group_dict(account) |
351 |
|
352 |
def update_account_groups(self, user, account, groups, replace=False): |
353 |
"""Update the groups associated with the account."""
|
354 |
|
355 |
logger.debug("update_account_groups: %s %s %s %s", user,
|
356 |
account, groups, replace) |
357 |
if user != account:
|
358 |
raise NotAllowedError
|
359 |
self._lookup_account(account, True) |
360 |
self._check_groups(groups)
|
361 |
if replace:
|
362 |
self.permissions.group_destroy(account)
|
363 |
for k, v in groups.iteritems(): |
364 |
if not replace: # If not already deleted. |
365 |
self.permissions.group_delete(account, k)
|
366 |
if v:
|
367 |
self.permissions.group_addmany(account, k, v)
|
368 |
|
369 |
def get_account_policy(self, user, account, external_quota=None): |
370 |
"""Return a dictionary with the account policy."""
|
371 |
|
372 |
logger.debug("get_account_policy: %s %s", user, account)
|
373 |
if user != account:
|
374 |
if account not in self._allowed_accounts(user): |
375 |
raise NotAllowedError
|
376 |
return {}
|
377 |
path, node = self._lookup_account(account, True) |
378 |
policy = self._get_policy(node, is_account_policy=True) |
379 |
if self.using_external_quotaholder: |
380 |
external_quota = external_quota or {}
|
381 |
policy['quota'] = external_quota.get('limit', 0) |
382 |
return policy
|
383 |
|
384 |
def update_account_policy(self, user, account, policy, replace=False): |
385 |
"""Update the policy associated with the account."""
|
386 |
|
387 |
logger.debug("update_account_policy: %s %s %s %s", user,
|
388 |
account, policy, replace) |
389 |
if user != account:
|
390 |
raise NotAllowedError
|
391 |
path, node = self._lookup_account(account, True) |
392 |
self._check_policy(policy, is_account_policy=True) |
393 |
self._put_policy(node, policy, replace, is_account_policy=True) |
394 |
|
395 |
def put_account(self, user, account, policy=None): |
396 |
"""Create a new account with the given name."""
|
397 |
|
398 |
logger.debug("put_account: %s %s %s", user, account, policy)
|
399 |
policy = policy or {}
|
400 |
if user != account:
|
401 |
raise NotAllowedError
|
402 |
node = self.node.node_lookup(account)
|
403 |
if node is not None: |
404 |
raise AccountExists('Account already exists') |
405 |
if policy:
|
406 |
self._check_policy(policy, is_account_policy=True) |
407 |
node = self._put_path(user, self.ROOTNODE, account, |
408 |
update_statistics_ancestors_depth=-1)
|
409 |
self._put_policy(node, policy, True, is_account_policy=True) |
410 |
|
411 |
def delete_account(self, user, account): |
412 |
"""Delete the account with the given name."""
|
413 |
|
414 |
logger.debug("delete_account: %s %s", user, account)
|
415 |
if user != account:
|
416 |
raise NotAllowedError
|
417 |
node = self.node.node_lookup(account)
|
418 |
if node is None: |
419 |
return
|
420 |
if not self.node.node_remove(node, |
421 |
update_statistics_ancestors_depth=-1):
|
422 |
raise AccountNotEmpty('Account is not empty') |
423 |
self.permissions.group_destroy(account)
|
424 |
|
425 |
def list_containers(self, user, account, marker=None, limit=10000, |
426 |
shared=False, until=None, public=False): |
427 |
"""Return a list of containers existing under an account."""
|
428 |
|
429 |
logger.debug("list_containers: %s %s %s %s %s %s %s", user,
|
430 |
account, marker, limit, shared, until, public) |
431 |
if user != account:
|
432 |
if until or account not in self._allowed_accounts(user): |
433 |
raise NotAllowedError
|
434 |
allowed = self._allowed_containers(user, account)
|
435 |
start, limit = self._list_limits(allowed, marker, limit)
|
436 |
return allowed[start:start + limit]
|
437 |
if shared or public: |
438 |
allowed = set()
|
439 |
if shared:
|
440 |
allowed.update([x.split('/', 2)[1] for x in |
441 |
self.permissions.access_list_shared(account)])
|
442 |
if public:
|
443 |
allowed.update([x[0].split('/', 2)[1] for x in |
444 |
self.permissions.public_list(account)])
|
445 |
allowed = sorted(allowed)
|
446 |
start, limit = self._list_limits(allowed, marker, limit)
|
447 |
return allowed[start:start + limit]
|
448 |
node = self.node.node_lookup(account)
|
449 |
containers = [x[0] for x in self._list_object_properties( |
450 |
node, account, '', '/', marker, limit, False, None, [], until)] |
451 |
start, limit = self._list_limits(
|
452 |
[x[0] for x in containers], marker, limit) |
453 |
return containers[start:start + limit]
|
454 |
|
455 |
def list_container_meta(self, user, account, container, domain, |
456 |
until=None):
|
457 |
"""Return a list of the container's object meta keys for a domain."""
|
458 |
|
459 |
logger.debug("list_container_meta: %s %s %s %s %s", user,
|
460 |
account, container, domain, until) |
461 |
allowed = [] |
462 |
if user != account:
|
463 |
if until:
|
464 |
raise NotAllowedError
|
465 |
allowed = self.permissions.access_list_paths(
|
466 |
user, '/'.join((account, container)))
|
467 |
if not allowed: |
468 |
raise NotAllowedError
|
469 |
path, node = self._lookup_container(account, container)
|
470 |
before = until if until is not None else inf |
471 |
allowed = self._get_formatted_paths(allowed)
|
472 |
return self.node.latest_attribute_keys(node, domain, before, |
473 |
CLUSTER_DELETED, allowed) |
474 |
|
475 |
def get_container_meta(self, user, account, container, domain, until=None, |
476 |
include_user_defined=True):
|
477 |
"""Return a dictionary with the container metadata for the domain."""
|
478 |
|
479 |
logger.debug("get_container_meta: %s %s %s %s %s", user,
|
480 |
account, container, domain, until) |
481 |
if user != account:
|
482 |
if until or container not in self._allowed_containers(user, |
483 |
account): |
484 |
raise NotAllowedError
|
485 |
path, node = self._lookup_container(account, container)
|
486 |
props = self._get_properties(node, until)
|
487 |
mtime = props[self.MTIME]
|
488 |
count, bytes, tstamp = self._get_statistics(node, until) |
489 |
tstamp = max(tstamp, mtime)
|
490 |
if until is None: |
491 |
modified = tstamp |
492 |
else:
|
493 |
modified = self._get_statistics(
|
494 |
node)[2] # Overall last modification. |
495 |
modified = max(modified, mtime)
|
496 |
|
497 |
if user != account:
|
498 |
meta = {'name': container}
|
499 |
else:
|
500 |
meta = {} |
501 |
if include_user_defined:
|
502 |
meta.update( |
503 |
dict(self.node.attribute_get(props[self.SERIAL], domain))) |
504 |
if until is not None: |
505 |
meta.update({'until_timestamp': tstamp})
|
506 |
meta.update({'name': container, 'count': count, 'bytes': bytes}) |
507 |
meta.update({'modified': modified})
|
508 |
return meta
|
509 |
|
510 |
def update_container_meta(self, user, account, container, domain, meta, |
511 |
replace=False):
|
512 |
"""Update the metadata associated with the container for the domain."""
|
513 |
|
514 |
logger.debug("update_container_meta: %s %s %s %s %s %s",
|
515 |
user, account, container, domain, meta, replace) |
516 |
if user != account:
|
517 |
raise NotAllowedError
|
518 |
path, node = self._lookup_container(account, container)
|
519 |
src_version_id, dest_version_id = self._put_metadata(
|
520 |
user, node, domain, meta, replace, |
521 |
update_statistics_ancestors_depth=0)
|
522 |
if src_version_id is not None: |
523 |
versioning = self._get_policy(
|
524 |
node, is_account_policy=False)['versioning'] |
525 |
if versioning != 'auto': |
526 |
self.node.version_remove(src_version_id,
|
527 |
update_statistics_ancestors_depth=0)
|
528 |
|
529 |
def get_container_policy(self, user, account, container): |
530 |
"""Return a dictionary with the container policy."""
|
531 |
|
532 |
logger.debug( |
533 |
"get_container_policy: %s %s %s", user, account, container)
|
534 |
if user != account:
|
535 |
if container not in self._allowed_containers(user, account): |
536 |
raise NotAllowedError
|
537 |
return {}
|
538 |
path, node = self._lookup_container(account, container)
|
539 |
return self._get_policy(node, is_account_policy=False) |
540 |
|
541 |
def update_container_policy(self, user, account, container, policy, |
542 |
replace=False):
|
543 |
"""Update the policy associated with the container."""
|
544 |
|
545 |
logger.debug("update_container_policy: %s %s %s %s %s",
|
546 |
user, account, container, policy, replace) |
547 |
if user != account:
|
548 |
raise NotAllowedError
|
549 |
path, node = self._lookup_container(account, container)
|
550 |
self._check_policy(policy, is_account_policy=False) |
551 |
self._put_policy(node, policy, replace, is_account_policy=False) |
552 |
|
553 |
def put_container(self, user, account, container, policy=None): |
554 |
"""Create a new container with the given name."""
|
555 |
|
556 |
logger.debug( |
557 |
"put_container: %s %s %s %s", user, account, container, policy)
|
558 |
policy = policy or {}
|
559 |
if user != account:
|
560 |
raise NotAllowedError
|
561 |
try:
|
562 |
path, node = self._lookup_container(account, container)
|
563 |
except NameError: |
564 |
pass
|
565 |
else:
|
566 |
raise ContainerExists('Container already exists') |
567 |
if policy:
|
568 |
self._check_policy(policy, is_account_policy=False) |
569 |
path = '/'.join((account, container))
|
570 |
node = self._put_path(
|
571 |
user, self._lookup_account(account, True)[1], path, |
572 |
update_statistics_ancestors_depth=-1)
|
573 |
self._put_policy(node, policy, True, is_account_policy=False) |
574 |
|
575 |
def delete_container(self, user, account, container, until=None, prefix='', |
576 |
delimiter=None):
|
577 |
"""Delete/purge the container with the given name."""
|
578 |
|
579 |
logger.debug("delete_container: %s %s %s %s %s %s", user,
|
580 |
account, container, until, prefix, delimiter) |
581 |
if user != account:
|
582 |
raise NotAllowedError
|
583 |
path, node = self._lookup_container(account, container)
|
584 |
|
585 |
if until is not None: |
586 |
hashes, size, serials = self.node.node_purge_children(
|
587 |
node, until, CLUSTER_HISTORY, |
588 |
update_statistics_ancestors_depth=0)
|
589 |
for h in hashes: |
590 |
self.store.map_delete(h)
|
591 |
self.node.node_purge_children(node, until, CLUSTER_DELETED,
|
592 |
update_statistics_ancestors_depth=0)
|
593 |
if not self.free_versioning: |
594 |
self._report_size_change(
|
595 |
user, account, -size, { |
596 |
'action': 'container purge', |
597 |
'path': path,
|
598 |
'versions': ','.join(str(i) for i in serials) |
599 |
} |
600 |
) |
601 |
return
|
602 |
|
603 |
if not delimiter: |
604 |
if self._get_statistics(node)[0] > 0: |
605 |
raise ContainerNotEmpty('Container is not empty') |
606 |
hashes, size, serials = self.node.node_purge_children(
|
607 |
node, inf, CLUSTER_HISTORY, |
608 |
update_statistics_ancestors_depth=0)
|
609 |
for h in hashes: |
610 |
self.store.map_delete(h)
|
611 |
self.node.node_purge_children(node, inf, CLUSTER_DELETED,
|
612 |
update_statistics_ancestors_depth=0)
|
613 |
self.node.node_remove(node, update_statistics_ancestors_depth=0) |
614 |
if not self.free_versioning: |
615 |
self._report_size_change(
|
616 |
user, account, -size, { |
617 |
'action': 'container purge', |
618 |
'path': path,
|
619 |
'versions': ','.join(str(i) for i in serials) |
620 |
} |
621 |
) |
622 |
else:
|
623 |
# remove only contents
|
624 |
src_names = self._list_objects_no_limit(
|
625 |
user, account, container, prefix='', delimiter=None, |
626 |
virtual=False, domain=None, keys=[], shared=False, until=None, |
627 |
size_range=None, all_props=True, public=False) |
628 |
paths = [] |
629 |
for t in src_names: |
630 |
path = '/'.join((account, container, t[0])) |
631 |
node = t[2]
|
632 |
src_version_id, dest_version_id = self._put_version_duplicate(
|
633 |
user, node, size=0, type='', hash=None, checksum='', |
634 |
cluster=CLUSTER_DELETED, |
635 |
update_statistics_ancestors_depth=1)
|
636 |
del_size = self._apply_versioning(
|
637 |
account, container, src_version_id, |
638 |
update_statistics_ancestors_depth=1)
|
639 |
self._report_size_change(
|
640 |
user, account, -del_size, { |
641 |
'action': 'object delete', |
642 |
'path': path,
|
643 |
'versions': ','.join([str(dest_version_id)])}) |
644 |
self._report_object_change(
|
645 |
user, account, path, details={'action': 'object delete'}) |
646 |
paths.append(path) |
647 |
self.permissions.access_clear_bulk(paths)
|
648 |
|
649 |
def _list_objects(self, user, account, container, prefix, delimiter, |
650 |
marker, limit, virtual, domain, keys, shared, until, |
651 |
size_range, all_props, public): |
652 |
if user != account and until: |
653 |
raise NotAllowedError
|
654 |
if shared and public: |
655 |
# get shared first
|
656 |
shared_paths = self._list_object_permissions(
|
657 |
user, account, container, prefix, shared=True, public=False) |
658 |
objects = set()
|
659 |
if shared_paths:
|
660 |
path, node = self._lookup_container(account, container)
|
661 |
shared_paths = self._get_formatted_paths(shared_paths)
|
662 |
objects |= set(self._list_object_properties( |
663 |
node, path, prefix, delimiter, marker, limit, virtual, |
664 |
domain, keys, until, size_range, shared_paths, all_props)) |
665 |
|
666 |
# get public
|
667 |
objects |= set(self._list_public_object_properties( |
668 |
user, account, container, prefix, all_props)) |
669 |
objects = list(objects)
|
670 |
|
671 |
objects.sort(key=lambda x: x[0]) |
672 |
start, limit = self._list_limits(
|
673 |
[x[0] for x in objects], marker, limit) |
674 |
return objects[start:start + limit]
|
675 |
elif public:
|
676 |
objects = self._list_public_object_properties(
|
677 |
user, account, container, prefix, all_props) |
678 |
start, limit = self._list_limits(
|
679 |
[x[0] for x in objects], marker, limit) |
680 |
return objects[start:start + limit]
|
681 |
|
682 |
allowed = self._list_object_permissions(
|
683 |
user, account, container, prefix, shared, public) |
684 |
if shared and not allowed: |
685 |
return []
|
686 |
path, node = self._lookup_container(account, container)
|
687 |
allowed = self._get_formatted_paths(allowed)
|
688 |
objects = self._list_object_properties(
|
689 |
node, path, prefix, delimiter, marker, limit, virtual, domain, |
690 |
keys, until, size_range, allowed, all_props) |
691 |
start, limit = self._list_limits(
|
692 |
[x[0] for x in objects], marker, limit) |
693 |
return objects[start:start + limit]
|
694 |
|
695 |
def _list_public_object_properties(self, user, account, container, prefix, |
696 |
all_props): |
697 |
public = self._list_object_permissions(
|
698 |
user, account, container, prefix, shared=False, public=True) |
699 |
paths, nodes = self._lookup_objects(public)
|
700 |
path = '/'.join((account, container))
|
701 |
cont_prefix = path + '/'
|
702 |
paths = [x[len(cont_prefix):] for x in paths] |
703 |
objects = [(p,) + props for p, props in |
704 |
zip(paths, self.node.version_lookup_bulk( |
705 |
nodes, all_props=all_props))] |
706 |
return objects
|
707 |
|
708 |
def _list_objects_no_limit(self, user, account, container, prefix, |
709 |
delimiter, virtual, domain, keys, shared, until, |
710 |
size_range, all_props, public): |
711 |
objects = [] |
712 |
while True: |
713 |
marker = objects[-1] if objects else None |
714 |
limit = 10000
|
715 |
l = self._list_objects(
|
716 |
user, account, container, prefix, delimiter, marker, limit, |
717 |
virtual, domain, keys, shared, until, size_range, all_props, |
718 |
public) |
719 |
objects.extend(l) |
720 |
if not l or len(l) < limit: |
721 |
break
|
722 |
return objects
|
723 |
|
724 |
def _list_object_permissions(self, user, account, container, prefix, |
725 |
shared, public): |
726 |
allowed = [] |
727 |
path = '/'.join((account, container, prefix)).rstrip('/') |
728 |
if user != account:
|
729 |
allowed = self.permissions.access_list_paths(user, path)
|
730 |
if not allowed: |
731 |
raise NotAllowedError
|
732 |
else:
|
733 |
allowed = set()
|
734 |
if shared:
|
735 |
allowed.update(self.permissions.access_list_shared(path))
|
736 |
if public:
|
737 |
allowed.update( |
738 |
[x[0] for x in self.permissions.public_list(path)]) |
739 |
allowed = sorted(allowed)
|
740 |
if not allowed: |
741 |
return []
|
742 |
return allowed
|
743 |
|
744 |
def list_objects(self, user, account, container, prefix='', delimiter=None, |
745 |
marker=None, limit=10000, virtual=True, domain=None, |
746 |
keys=None, shared=False, until=None, size_range=None, |
747 |
public=False):
|
748 |
"""List (object name, object version_id) under a container."""
|
749 |
|
750 |
logger.debug("list_objects: %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
|
751 |
user, account, container, prefix, delimiter, marker, |
752 |
limit, virtual, domain, keys, shared, until, size_range, |
753 |
public) |
754 |
keys = keys or []
|
755 |
return self._list_objects( |
756 |
user, account, container, prefix, delimiter, marker, limit, |
757 |
virtual, domain, keys, shared, until, size_range, False, public)
|
758 |
|
759 |
def list_object_meta(self, user, account, container, prefix='', |
760 |
delimiter=None, marker=None, limit=10000, |
761 |
virtual=True, domain=None, keys=None, shared=False, |
762 |
until=None, size_range=None, public=False): |
763 |
"""Return a list of metadata dicts of objects under a container."""
|
764 |
|
765 |
logger.debug( |
766 |
"list_object_meta: %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
|
767 |
user, account, container, prefix, delimiter, marker, limit, |
768 |
virtual, domain, keys, shared, until, size_range, public) |
769 |
keys = keys or []
|
770 |
props = self._list_objects(
|
771 |
user, account, container, prefix, delimiter, marker, limit, |
772 |
virtual, domain, keys, shared, until, size_range, True, public)
|
773 |
objects = [] |
774 |
for p in props: |
775 |
if len(p) == 2: |
776 |
objects.append({'subdir': p[0]}) |
777 |
else:
|
778 |
objects.append({ |
779 |
'name': p[0], |
780 |
'bytes': p[self.SIZE + 1], |
781 |
'type': p[self.TYPE + 1], |
782 |
'hash': p[self.HASH + 1], |
783 |
'version': p[self.SERIAL + 1], |
784 |
'version_timestamp': p[self.MTIME + 1], |
785 |
'modified': p[self.MTIME + 1] if until is None else None, |
786 |
'modified_by': p[self.MUSER + 1], |
787 |
'uuid': p[self.UUID + 1], |
788 |
'checksum': p[self.CHECKSUM + 1]}) |
789 |
return objects
|
790 |
|
791 |
def list_object_permissions(self, user, account, container, prefix=''): |
792 |
"""Return a list of paths enforce permissions under a container."""
|
793 |
|
794 |
logger.debug("list_object_permissions: %s %s %s %s", user,
|
795 |
account, container, prefix) |
796 |
return self._list_object_permissions(user, account, container, prefix, |
797 |
True, False) |
798 |
|
799 |
def list_object_public(self, user, account, container, prefix=''): |
800 |
"""Return a mapping of object paths to public ids under a container."""
|
801 |
|
802 |
logger.debug("list_object_public: %s %s %s %s", user,
|
803 |
account, container, prefix) |
804 |
public = {} |
805 |
for path, p in self.permissions.public_list('/'.join((account, |
806 |
container, |
807 |
prefix))): |
808 |
public[path] = p |
809 |
return public
|
810 |
|
811 |
def get_object_meta(self, user, account, container, name, domain, |
812 |
version=None, include_user_defined=True): |
813 |
"""Return a dictionary with the object metadata for the domain."""
|
814 |
|
815 |
logger.debug("get_object_meta: %s %s %s %s %s %s", user,
|
816 |
account, container, name, domain, version) |
817 |
self._can_read(user, account, container, name)
|
818 |
path, node = self._lookup_object(account, container, name)
|
819 |
props = self._get_version(node, version)
|
820 |
if version is None: |
821 |
modified = props[self.MTIME]
|
822 |
else:
|
823 |
try:
|
824 |
modified = self._get_version(
|
825 |
node)[self.MTIME] # Overall last modification. |
826 |
except NameError: # Object may be deleted. |
827 |
del_props = self.node.version_lookup(
|
828 |
node, inf, CLUSTER_DELETED) |
829 |
if del_props is None: |
830 |
raise ItemNotExists('Object does not exist') |
831 |
modified = del_props[self.MTIME]
|
832 |
|
833 |
meta = {} |
834 |
if include_user_defined:
|
835 |
meta.update( |
836 |
dict(self.node.attribute_get(props[self.SERIAL], domain))) |
837 |
meta.update({'name': name,
|
838 |
'bytes': props[self.SIZE], |
839 |
'type': props[self.TYPE], |
840 |
'hash': props[self.HASH], |
841 |
'version': props[self.SERIAL], |
842 |
'version_timestamp': props[self.MTIME], |
843 |
'modified': modified,
|
844 |
'modified_by': props[self.MUSER], |
845 |
'uuid': props[self.UUID], |
846 |
'checksum': props[self.CHECKSUM]}) |
847 |
return meta
|
848 |
|
849 |
def update_object_meta(self, user, account, container, name, domain, meta, |
850 |
replace=False):
|
851 |
"""Update object metadata for a domain and return the new version."""
|
852 |
|
853 |
logger.debug("update_object_meta: %s %s %s %s %s %s %s",
|
854 |
user, account, container, name, domain, meta, replace) |
855 |
self._can_write(user, account, container, name)
|
856 |
path, node = self._lookup_object(account, container, name)
|
857 |
src_version_id, dest_version_id = self._put_metadata(
|
858 |
user, node, domain, meta, replace, |
859 |
update_statistics_ancestors_depth=1)
|
860 |
self._apply_versioning(account, container, src_version_id,
|
861 |
update_statistics_ancestors_depth=1)
|
862 |
return dest_version_id
|
863 |
|
864 |
def get_object_permissions(self, user, account, container, name): |
865 |
"""Return the action allowed on the object, the path
|
866 |
from which the object gets its permissions from,
|
867 |
along with a dictionary containing the permissions."""
|
868 |
|
869 |
logger.debug("get_object_permissions: %s %s %s %s", user,
|
870 |
account, container, name) |
871 |
allowed = 'write'
|
872 |
permissions_path = self._get_permissions_path(account, container, name)
|
873 |
if user != account:
|
874 |
if self.permissions.access_check(permissions_path, self.WRITE, |
875 |
user): |
876 |
allowed = 'write'
|
877 |
elif self.permissions.access_check(permissions_path, self.READ, |
878 |
user): |
879 |
allowed = 'read'
|
880 |
else:
|
881 |
raise NotAllowedError
|
882 |
self._lookup_object(account, container, name)
|
883 |
return (allowed,
|
884 |
permissions_path, |
885 |
self.permissions.access_get(permissions_path))
|
886 |
|
887 |
def update_object_permissions(self, user, account, container, name, |
888 |
permissions): |
889 |
"""Update the permissions associated with the object."""
|
890 |
|
891 |
logger.debug("update_object_permissions: %s %s %s %s %s",
|
892 |
user, account, container, name, permissions) |
893 |
if user != account:
|
894 |
raise NotAllowedError
|
895 |
path = self._lookup_object(account, container, name)[0] |
896 |
self._check_permissions(path, permissions)
|
897 |
self.permissions.access_set(path, permissions)
|
898 |
self._report_sharing_change(user, account, path, {'members': |
899 |
self.permissions.access_members(path)})
|
900 |
|
901 |
def get_object_public(self, user, account, container, name): |
902 |
"""Return the public id of the object if applicable."""
|
903 |
|
904 |
logger.debug( |
905 |
"get_object_public: %s %s %s %s", user, account, container, name)
|
906 |
self._can_read(user, account, container, name)
|
907 |
path = self._lookup_object(account, container, name)[0] |
908 |
p = self.permissions.public_get(path)
|
909 |
return p
|
910 |
|
911 |
def update_object_public(self, user, account, container, name, public): |
912 |
"""Update the public status of the object."""
|
913 |
|
914 |
logger.debug("update_object_public: %s %s %s %s %s", user,
|
915 |
account, container, name, public) |
916 |
self._can_write(user, account, container, name)
|
917 |
path = self._lookup_object(account, container, name)[0] |
918 |
if not public: |
919 |
self.permissions.public_unset(path)
|
920 |
else:
|
921 |
self.permissions.public_set(
|
922 |
path, self.public_url_security, self.public_url_alphabet |
923 |
) |
924 |
|
925 |
def get_object_hashmap(self, user, account, container, name, version=None): |
926 |
"""Return the object's size and a list with partial hashes."""
|
927 |
|
928 |
logger.debug("get_object_hashmap: %s %s %s %s %s", user,
|
929 |
account, container, name, version) |
930 |
self._can_read(user, account, container, name)
|
931 |
path, node = self._lookup_object(account, container, name)
|
932 |
props = self._get_version(node, version)
|
933 |
hashmap = self.store.map_get(binascii.unhexlify(props[self.HASH])) |
934 |
return props[self.SIZE], [binascii.hexlify(x) for x in hashmap] |
935 |
|
936 |
def _update_object_hash(self, user, account, container, name, size, type, |
937 |
hash, checksum, domain, meta, replace_meta,
|
938 |
permissions, src_node=None, src_version_id=None, |
939 |
is_copy=False):
|
940 |
if permissions is not None and user != account: |
941 |
raise NotAllowedError
|
942 |
self._can_write(user, account, container, name)
|
943 |
if permissions is not None: |
944 |
path = '/'.join((account, container, name))
|
945 |
self._check_permissions(path, permissions)
|
946 |
|
947 |
account_path, account_node = self._lookup_account(account, True) |
948 |
container_path, container_node = self._lookup_container(
|
949 |
account, container) |
950 |
|
951 |
path, node = self._put_object_node(
|
952 |
container_path, container_node, name) |
953 |
pre_version_id, dest_version_id = self._put_version_duplicate(
|
954 |
user, node, src_node=src_node, size=size, type=type, hash=hash, |
955 |
checksum=checksum, is_copy=is_copy, |
956 |
update_statistics_ancestors_depth=1)
|
957 |
|
958 |
# Handle meta.
|
959 |
if src_version_id is None: |
960 |
src_version_id = pre_version_id |
961 |
self._put_metadata_duplicate(
|
962 |
src_version_id, dest_version_id, domain, node, meta, replace_meta) |
963 |
|
964 |
del_size = self._apply_versioning(account, container, pre_version_id,
|
965 |
update_statistics_ancestors_depth=1)
|
966 |
size_delta = size - del_size |
967 |
if size_delta > 0: |
968 |
# Check account quota.
|
969 |
if not self.using_external_quotaholder: |
970 |
account_quota = long(self._get_policy( |
971 |
account_node, is_account_policy=True)['quota']) |
972 |
account_usage = self._get_statistics(account_node,
|
973 |
compute=True)[1] |
974 |
if (account_quota > 0 and account_usage > account_quota): |
975 |
raise QuotaError(
|
976 |
'Account quota exceeded: limit: %s, usage: %s' % (
|
977 |
account_quota, account_usage)) |
978 |
|
979 |
# Check container quota.
|
980 |
container_quota = long(self._get_policy( |
981 |
container_node, is_account_policy=False)['quota']) |
982 |
container_usage = self._get_statistics(container_node)[1] |
983 |
if (container_quota > 0 and container_usage > container_quota): |
984 |
# This must be executed in a transaction, so the version is
|
985 |
# never created if it fails.
|
986 |
raise QuotaError(
|
987 |
'Container quota exceeded: limit: %s, usage: %s' % (
|
988 |
container_quota, container_usage |
989 |
) |
990 |
) |
991 |
|
992 |
self._report_size_change(
|
993 |
user, account, size_delta, |
994 |
{'action': 'object update', 'path': path, |
995 |
'versions': ','.join([str(dest_version_id)])}) |
996 |
if permissions is not None: |
997 |
self.permissions.access_set(path, permissions)
|
998 |
self._report_sharing_change(
|
999 |
user, account, path, |
1000 |
{'members': self.permissions.access_members(path)}) |
1001 |
|
1002 |
self._report_object_change(
|
1003 |
user, account, path, |
1004 |
details={'version': dest_version_id, 'action': 'object update'}) |
1005 |
return dest_version_id
|
1006 |
|
1007 |
def update_object_hashmap(self, user, account, container, name, size, type, |
1008 |
hashmap, checksum, domain, meta=None,
|
1009 |
replace_meta=False, permissions=None): |
1010 |
"""Create/update an object's hashmap and return the new version."""
|
1011 |
|
1012 |
logger.debug("update_object_hashmap: %s %s %s %s %s %s %s %s", user,
|
1013 |
account, container, name, size, type, hashmap, checksum)
|
1014 |
meta = meta or {}
|
1015 |
if size == 0: # No such thing as an empty hashmap. |
1016 |
hashmap = [self.put_block('')] |
1017 |
map = HashMap(self.block_size, self.hash_algorithm) |
1018 |
map.extend([binascii.unhexlify(x) for x in hashmap]) |
1019 |
missing = self.store.block_search(map) |
1020 |
if missing:
|
1021 |
ie = IndexError()
|
1022 |
ie.data = [binascii.hexlify(x) for x in missing] |
1023 |
raise ie
|
1024 |
|
1025 |
hash = map.hash()
|
1026 |
hexlified = binascii.hexlify(hash)
|
1027 |
dest_version_id = self._update_object_hash(
|
1028 |
user, account, container, name, size, type, hexlified, checksum,
|
1029 |
domain, meta, replace_meta, permissions) |
1030 |
self.store.map_put(hash, map) |
1031 |
return dest_version_id, hexlified
|
1032 |
|
1033 |
def update_object_checksum(self, user, account, container, name, version, |
1034 |
checksum): |
1035 |
"""Update an object's checksum."""
|
1036 |
|
1037 |
logger.debug("update_object_checksum: %s %s %s %s %s %s",
|
1038 |
user, account, container, name, version, checksum) |
1039 |
# Update objects with greater version and same hashmap
|
1040 |
# and size (fix metadata updates).
|
1041 |
self._can_write(user, account, container, name)
|
1042 |
path, node = self._lookup_object(account, container, name)
|
1043 |
props = self._get_version(node, version)
|
1044 |
versions = self.node.node_get_versions(node)
|
1045 |
for x in versions: |
1046 |
if (x[self.SERIAL] >= int(version) and |
1047 |
x[self.HASH] == props[self.HASH] and |
1048 |
x[self.SIZE] == props[self.SIZE]): |
1049 |
self.node.version_put_property(
|
1050 |
x[self.SERIAL], 'checksum', checksum) |
1051 |
|
1052 |
def _copy_object(self, user, src_account, src_container, src_name, |
1053 |
dest_account, dest_container, dest_name, type,
|
1054 |
dest_domain=None, dest_meta=None, replace_meta=False, |
1055 |
permissions=None, src_version=None, is_move=False, |
1056 |
delimiter=None):
|
1057 |
dest_meta = dest_meta or {}
|
1058 |
dest_version_ids = [] |
1059 |
self._can_read(user, src_account, src_container, src_name)
|
1060 |
path, node = self._lookup_object(src_account, src_container, src_name)
|
1061 |
# TODO: Will do another fetch of the properties in duplicate version...
|
1062 |
props = self._get_version(
|
1063 |
node, src_version) # Check to see if source exists.
|
1064 |
src_version_id = props[self.SERIAL]
|
1065 |
hash = props[self.HASH]
|
1066 |
size = props[self.SIZE]
|
1067 |
is_copy = not is_move and (src_account, src_container, src_name) != ( |
1068 |
dest_account, dest_container, dest_name) # New uuid.
|
1069 |
dest_version_ids.append(self._update_object_hash(
|
1070 |
user, dest_account, dest_container, dest_name, size, type, hash, |
1071 |
None, dest_domain, dest_meta, replace_meta, permissions,
|
1072 |
src_node=node, src_version_id=src_version_id, is_copy=is_copy)) |
1073 |
if is_move and ((src_account, src_container, src_name) != |
1074 |
(dest_account, dest_container, dest_name)): |
1075 |
self._delete_object(user, src_account, src_container, src_name)
|
1076 |
|
1077 |
if delimiter:
|
1078 |
prefix = (src_name + delimiter if not |
1079 |
src_name.endswith(delimiter) else src_name)
|
1080 |
src_names = self._list_objects_no_limit(
|
1081 |
user, src_account, src_container, prefix, delimiter=None,
|
1082 |
virtual=False, domain=None, keys=[], shared=False, until=None, |
1083 |
size_range=None, all_props=True, public=False) |
1084 |
src_names.sort(key=lambda x: x[2]) # order by nodes |
1085 |
paths = [elem[0] for elem in src_names] |
1086 |
nodes = [elem[2] for elem in src_names] |
1087 |
# TODO: Will do another fetch of the properties
|
1088 |
# in duplicate version...
|
1089 |
props = self._get_versions(nodes) # Check to see if source exists. |
1090 |
|
1091 |
for prop, path, node in zip(props, paths, nodes): |
1092 |
src_version_id = prop[self.SERIAL]
|
1093 |
hash = prop[self.HASH]
|
1094 |
vtype = prop[self.TYPE]
|
1095 |
size = prop[self.SIZE]
|
1096 |
dest_prefix = dest_name + delimiter if not dest_name.endswith( |
1097 |
delimiter) else dest_name
|
1098 |
vdest_name = path.replace(prefix, dest_prefix, 1)
|
1099 |
dest_version_ids.append(self._update_object_hash(
|
1100 |
user, dest_account, dest_container, vdest_name, size, |
1101 |
vtype, hash, None, dest_domain, meta={}, |
1102 |
replace_meta=False, permissions=None, src_node=node, |
1103 |
src_version_id=src_version_id, is_copy=is_copy)) |
1104 |
if is_move and ((src_account, src_container, src_name) != |
1105 |
(dest_account, dest_container, dest_name)): |
1106 |
self._delete_object(user, src_account, src_container, path)
|
1107 |
return (dest_version_ids[0] if len(dest_version_ids) == 1 else |
1108 |
dest_version_ids) |
1109 |
|
1110 |
def copy_object(self, user, src_account, src_container, src_name, |
1111 |
dest_account, dest_container, dest_name, type, domain,
|
1112 |
meta=None, replace_meta=False, permissions=None, |
1113 |
src_version=None, delimiter=None): |
1114 |
"""Copy an object's data and metadata."""
|
1115 |
|
1116 |
logger.debug("copy_object: %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
|
1117 |
user, src_account, src_container, src_name, dest_account, |
1118 |
dest_container, dest_name, type, domain, meta,
|
1119 |
replace_meta, permissions, src_version, delimiter) |
1120 |
meta = meta or {}
|
1121 |
dest_version_id = self._copy_object(
|
1122 |
user, src_account, src_container, src_name, dest_account, |
1123 |
dest_container, dest_name, type, domain, meta, replace_meta,
|
1124 |
permissions, src_version, False, delimiter)
|
1125 |
return dest_version_id
|
1126 |
|
1127 |
def move_object(self, user, src_account, src_container, src_name, |
1128 |
dest_account, dest_container, dest_name, type, domain,
|
1129 |
meta=None, replace_meta=False, permissions=None, |
1130 |
delimiter=None):
|
1131 |
"""Move an object's data and metadata."""
|
1132 |
|
1133 |
logger.debug("move_object: %s %s %s %s %s %s %s %s %s %s %s %s %s",
|
1134 |
user, src_account, src_container, src_name, dest_account, |
1135 |
dest_container, dest_name, type, domain, meta,
|
1136 |
replace_meta, permissions, delimiter) |
1137 |
meta = meta or {}
|
1138 |
if user != src_account:
|
1139 |
raise NotAllowedError
|
1140 |
dest_version_id = self._copy_object(
|
1141 |
user, src_account, src_container, src_name, dest_account, |
1142 |
dest_container, dest_name, type, domain, meta, replace_meta,
|
1143 |
permissions, None, True, delimiter) |
1144 |
return dest_version_id
|
1145 |
|
1146 |
def _delete_object(self, user, account, container, name, until=None, |
1147 |
delimiter=None):
|
1148 |
if user != account:
|
1149 |
raise NotAllowedError
|
1150 |
|
1151 |
if until is not None: |
1152 |
path = '/'.join((account, container, name))
|
1153 |
node = self.node.node_lookup(path)
|
1154 |
if node is None: |
1155 |
return
|
1156 |
hashes = [] |
1157 |
size = 0
|
1158 |
serials = [] |
1159 |
h, s, v = self.node.node_purge(node, until, CLUSTER_NORMAL,
|
1160 |
update_statistics_ancestors_depth=1)
|
1161 |
hashes += h |
1162 |
size += s |
1163 |
serials += v |
1164 |
h, s, v = self.node.node_purge(node, until, CLUSTER_HISTORY,
|
1165 |
update_statistics_ancestors_depth=1)
|
1166 |
hashes += h |
1167 |
if not self.free_versioning: |
1168 |
size += s |
1169 |
serials += v |
1170 |
for h in hashes: |
1171 |
self.store.map_delete(h)
|
1172 |
self.node.node_purge(node, until, CLUSTER_DELETED,
|
1173 |
update_statistics_ancestors_depth=1)
|
1174 |
try:
|
1175 |
self._get_version(node)
|
1176 |
except NameError: |
1177 |
self.permissions.access_clear(path)
|
1178 |
self._report_size_change(
|
1179 |
user, account, -size, { |
1180 |
'action': 'object purge', |
1181 |
'path': path,
|
1182 |
'versions': ','.join(str(i) for i in serials) |
1183 |
} |
1184 |
) |
1185 |
return
|
1186 |
|
1187 |
path, node = self._lookup_object(account, container, name)
|
1188 |
src_version_id, dest_version_id = self._put_version_duplicate(
|
1189 |
user, node, size=0, type='', hash=None, checksum='', |
1190 |
cluster=CLUSTER_DELETED, update_statistics_ancestors_depth=1)
|
1191 |
del_size = self._apply_versioning(account, container, src_version_id,
|
1192 |
update_statistics_ancestors_depth=1)
|
1193 |
self._report_size_change(
|
1194 |
user, account, -del_size, |
1195 |
{'action': 'object delete', |
1196 |
'path': path,
|
1197 |
'versions': ','.join([str(dest_version_id)])}) |
1198 |
self._report_object_change(
|
1199 |
user, account, path, details={'action': 'object delete'}) |
1200 |
self.permissions.access_clear(path)
|
1201 |
|
1202 |
if delimiter:
|
1203 |
prefix = name + delimiter if not name.endswith(delimiter) else name |
1204 |
src_names = self._list_objects_no_limit(
|
1205 |
user, account, container, prefix, delimiter=None,
|
1206 |
virtual=False, domain=None, keys=[], shared=False, until=None, |
1207 |
size_range=None, all_props=True, public=False) |
1208 |
paths = [] |
1209 |
for t in src_names: |
1210 |
path = '/'.join((account, container, t[0])) |
1211 |
node = t[2]
|
1212 |
src_version_id, dest_version_id = self._put_version_duplicate(
|
1213 |
user, node, size=0, type='', hash=None, checksum='', |
1214 |
cluster=CLUSTER_DELETED, |
1215 |
update_statistics_ancestors_depth=1)
|
1216 |
del_size = self._apply_versioning(
|
1217 |
account, container, src_version_id, |
1218 |
update_statistics_ancestors_depth=1)
|
1219 |
self._report_size_change(
|
1220 |
user, account, -del_size, |
1221 |
{'action': 'object delete', |
1222 |
'path': path,
|
1223 |
'versions': ','.join([str(dest_version_id)])}) |
1224 |
self._report_object_change(
|
1225 |
user, account, path, details={'action': 'object delete'}) |
1226 |
paths.append(path) |
1227 |
self.permissions.access_clear_bulk(paths)
|
1228 |
|
1229 |
def delete_object(self, user, account, container, name, until=None, |
1230 |
prefix='', delimiter=None): |
1231 |
"""Delete/purge an object."""
|
1232 |
|
1233 |
logger.debug("delete_object: %s %s %s %s %s %s %s", user,
|
1234 |
account, container, name, until, prefix, delimiter) |
1235 |
self._delete_object(user, account, container, name, until, delimiter)
|
1236 |
|
1237 |
def list_versions(self, user, account, container, name): |
1238 |
"""Return a list of all object (version, version_timestamp) tuples."""
|
1239 |
|
1240 |
logger.debug( |
1241 |
"list_versions: %s %s %s %s", user, account, container, name)
|
1242 |
self._can_read(user, account, container, name)
|
1243 |
path, node = self._lookup_object(account, container, name)
|
1244 |
versions = self.node.node_get_versions(node)
|
1245 |
return [[x[self.SERIAL], x[self.MTIME]] for x in versions if |
1246 |
x[self.CLUSTER] != CLUSTER_DELETED]
|
1247 |
|
1248 |
def get_uuid(self, user, uuid): |
1249 |
"""Return the (account, container, name) for the UUID given."""
|
1250 |
|
1251 |
logger.debug("get_uuid: %s %s", user, uuid)
|
1252 |
info = self.node.latest_uuid(uuid, CLUSTER_NORMAL)
|
1253 |
if info is None: |
1254 |
raise NameError |
1255 |
path, serial = info |
1256 |
account, container, name = path.split('/', 2) |
1257 |
self._can_read(user, account, container, name)
|
1258 |
return (account, container, name)
|
1259 |
|
1260 |
def get_public(self, user, public): |
1261 |
"""Return the (account, container, name) for the public id given."""
|
1262 |
|
1263 |
logger.debug("get_public: %s %s", user, public)
|
1264 |
path = self.permissions.public_path(public)
|
1265 |
if path is None: |
1266 |
raise NameError |
1267 |
account, container, name = path.split('/', 2) |
1268 |
self._can_read(user, account, container, name)
|
1269 |
return (account, container, name)
|
1270 |
|
1271 |
def get_block(self, hash): |
1272 |
"""Return a block's data."""
|
1273 |
|
1274 |
logger.debug("get_block: %s", hash) |
1275 |
block = self.store.block_get(binascii.unhexlify(hash)) |
1276 |
if not block: |
1277 |
raise ItemNotExists('Block does not exist') |
1278 |
return block
|
1279 |
|
1280 |
def put_block(self, data): |
1281 |
"""Store a block and return the hash."""
|
1282 |
|
1283 |
logger.debug("put_block: %s", len(data)) |
1284 |
return binascii.hexlify(self.store.block_put(data)) |
1285 |
|
1286 |
def update_block(self, hash, data, offset=0): |
1287 |
"""Update a known block and return the hash."""
|
1288 |
|
1289 |
logger.debug("update_block: %s %s %s", hash, len(data), offset) |
1290 |
if offset == 0 and len(data) == self.block_size: |
1291 |
return self.put_block(data) |
1292 |
h = self.store.block_update(binascii.unhexlify(hash), offset, data) |
1293 |
return binascii.hexlify(h)
|
1294 |
|
1295 |
# Path functions.
|
1296 |
|
1297 |
def _generate_uuid(self): |
1298 |
return str(uuidlib.uuid4()) |
1299 |
|
1300 |
def _put_object_node(self, path, parent, name): |
1301 |
path = '/'.join((path, name))
|
1302 |
node = self.node.node_lookup(path)
|
1303 |
if node is None: |
1304 |
node = self.node.node_create(parent, path)
|
1305 |
return path, node
|
1306 |
|
1307 |
def _put_path(self, user, parent, path, |
1308 |
update_statistics_ancestors_depth=None):
|
1309 |
node = self.node.node_create(parent, path)
|
1310 |
self.node.version_create(node, None, 0, '', None, user, |
1311 |
self._generate_uuid(), '', CLUSTER_NORMAL, |
1312 |
update_statistics_ancestors_depth) |
1313 |
return node
|
1314 |
|
1315 |
def _lookup_account(self, account, create=True): |
1316 |
for_update = True if create else False |
1317 |
node = self.node.node_lookup(account, for_update=for_update)
|
1318 |
if node is None and create: |
1319 |
node = self._put_path(
|
1320 |
account, self.ROOTNODE, account,
|
1321 |
update_statistics_ancestors_depth=-1) # User is account. |
1322 |
return account, node
|
1323 |
|
1324 |
def _lookup_container(self, account, container): |
1325 |
for_update = True if self.lock_container_path else False |
1326 |
path = '/'.join((account, container))
|
1327 |
node = self.node.node_lookup(path, for_update)
|
1328 |
if node is None: |
1329 |
raise ItemNotExists('Container does not exist') |
1330 |
return path, node
|
1331 |
|
1332 |
def _lookup_object(self, account, container, name): |
1333 |
path = '/'.join((account, container, name))
|
1334 |
node = self.node.node_lookup(path)
|
1335 |
if node is None: |
1336 |
raise ItemNotExists('Object does not exist') |
1337 |
return path, node
|
1338 |
|
1339 |
def _lookup_objects(self, paths): |
1340 |
nodes = self.node.node_lookup_bulk(paths)
|
1341 |
return paths, nodes
|
1342 |
|
1343 |
def _get_properties(self, node, until=None): |
1344 |
"""Return properties until the timestamp given."""
|
1345 |
|
1346 |
before = until if until is not None else inf |
1347 |
props = self.node.version_lookup(node, before, CLUSTER_NORMAL)
|
1348 |
if props is None and until is not None: |
1349 |
props = self.node.version_lookup(node, before, CLUSTER_HISTORY)
|
1350 |
if props is None: |
1351 |
raise ItemNotExists('Path does not exist') |
1352 |
return props
|
1353 |
|
1354 |
def _get_statistics(self, node, until=None, compute=False): |
1355 |
"""Return (count, sum of size, timestamp) of everything under node."""
|
1356 |
|
1357 |
if until is not None: |
1358 |
stats = self.node.statistics_latest(node, until, CLUSTER_DELETED)
|
1359 |
elif compute:
|
1360 |
stats = self.node.statistics_latest(node,
|
1361 |
except_cluster=CLUSTER_DELETED) |
1362 |
else:
|
1363 |
stats = self.node.statistics_get(node, CLUSTER_NORMAL)
|
1364 |
if stats is None: |
1365 |
stats = (0, 0, 0) |
1366 |
return stats
|
1367 |
|
1368 |
def _get_version(self, node, version=None): |
1369 |
if version is None: |
1370 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1371 |
if props is None: |
1372 |
raise ItemNotExists('Object does not exist') |
1373 |
else:
|
1374 |
try:
|
1375 |
version = int(version)
|
1376 |
except ValueError: |
1377 |
raise VersionNotExists('Version does not exist') |
1378 |
props = self.node.version_get_properties(version)
|
1379 |
if props is None or props[self.CLUSTER] == CLUSTER_DELETED: |
1380 |
raise VersionNotExists('Version does not exist') |
1381 |
return props
|
1382 |
|
1383 |
def _get_versions(self, nodes): |
1384 |
return self.node.version_lookup_bulk(nodes, inf, CLUSTER_NORMAL) |
1385 |
|
1386 |
def _put_version_duplicate(self, user, node, src_node=None, size=None, |
1387 |
type=None, hash=None, checksum=None, |
1388 |
cluster=CLUSTER_NORMAL, is_copy=False,
|
1389 |
update_statistics_ancestors_depth=None):
|
1390 |
"""Create a new version of the node."""
|
1391 |
|
1392 |
props = self.node.version_lookup(
|
1393 |
node if src_node is None else src_node, inf, CLUSTER_NORMAL) |
1394 |
if props is not None: |
1395 |
src_version_id = props[self.SERIAL]
|
1396 |
src_hash = props[self.HASH]
|
1397 |
src_size = props[self.SIZE]
|
1398 |
src_type = props[self.TYPE]
|
1399 |
src_checksum = props[self.CHECKSUM]
|
1400 |
else:
|
1401 |
src_version_id = None
|
1402 |
src_hash = None
|
1403 |
src_size = 0
|
1404 |
src_type = ''
|
1405 |
src_checksum = ''
|
1406 |
if size is None: # Set metadata. |
1407 |
hash = src_hash # This way hash can be set to None
|
1408 |
# (account or container).
|
1409 |
size = src_size |
1410 |
if type is None: |
1411 |
type = src_type |
1412 |
if checksum is None: |
1413 |
checksum = src_checksum |
1414 |
uuid = self._generate_uuid(
|
1415 |
) if (is_copy or src_version_id is None) else props[self.UUID] |
1416 |
|
1417 |
if src_node is None: |
1418 |
pre_version_id = src_version_id |
1419 |
else:
|
1420 |
pre_version_id = None
|
1421 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1422 |
if props is not None: |
1423 |
pre_version_id = props[self.SERIAL]
|
1424 |
if pre_version_id is not None: |
1425 |
self.node.version_recluster(pre_version_id, CLUSTER_HISTORY,
|
1426 |
update_statistics_ancestors_depth) |
1427 |
|
1428 |
dest_version_id, mtime = self.node.version_create(
|
1429 |
node, hash, size, type, src_version_id, user, uuid, checksum, |
1430 |
cluster, update_statistics_ancestors_depth) |
1431 |
|
1432 |
self.node.attribute_unset_is_latest(node, dest_version_id)
|
1433 |
|
1434 |
return pre_version_id, dest_version_id
|
1435 |
|
1436 |
def _put_metadata_duplicate(self, src_version_id, dest_version_id, domain, |
1437 |
node, meta, replace=False):
|
1438 |
if src_version_id is not None: |
1439 |
self.node.attribute_copy(src_version_id, dest_version_id)
|
1440 |
if not replace: |
1441 |
self.node.attribute_del(dest_version_id, domain, (
|
1442 |
k for k, v in meta.iteritems() if v == '')) |
1443 |
self.node.attribute_set(dest_version_id, domain, node, (
|
1444 |
(k, v) for k, v in meta.iteritems() if v != '')) |
1445 |
else:
|
1446 |
self.node.attribute_del(dest_version_id, domain)
|
1447 |
self.node.attribute_set(dest_version_id, domain, node, ((
|
1448 |
k, v) for k, v in meta.iteritems())) |
1449 |
|
1450 |
def _put_metadata(self, user, node, domain, meta, replace=False, |
1451 |
update_statistics_ancestors_depth=None):
|
1452 |
"""Create a new version and store metadata."""
|
1453 |
|
1454 |
src_version_id, dest_version_id = self._put_version_duplicate(
|
1455 |
user, node, |
1456 |
update_statistics_ancestors_depth= |
1457 |
update_statistics_ancestors_depth) |
1458 |
self._put_metadata_duplicate(
|
1459 |
src_version_id, dest_version_id, domain, node, meta, replace) |
1460 |
return src_version_id, dest_version_id
|
1461 |
|
1462 |
def _list_limits(self, listing, marker, limit): |
1463 |
start = 0
|
1464 |
if marker:
|
1465 |
try:
|
1466 |
start = listing.index(marker) + 1
|
1467 |
except ValueError: |
1468 |
pass
|
1469 |
if not limit or limit > 10000: |
1470 |
limit = 10000
|
1471 |
return start, limit
|
1472 |
|
1473 |
def _list_object_properties(self, parent, path, prefix='', delimiter=None, |
1474 |
marker=None, limit=10000, virtual=True, |
1475 |
domain=None, keys=None, until=None, |
1476 |
size_range=None, allowed=None, |
1477 |
all_props=False):
|
1478 |
keys = keys or []
|
1479 |
allowed = allowed or []
|
1480 |
cont_prefix = path + '/'
|
1481 |
prefix = cont_prefix + prefix |
1482 |
start = cont_prefix + marker if marker else None |
1483 |
before = until if until is not None else inf |
1484 |
filterq = keys if domain else [] |
1485 |
sizeq = size_range |
1486 |
|
1487 |
objects, prefixes = self.node.latest_version_list(
|
1488 |
parent, prefix, delimiter, start, limit, before, CLUSTER_DELETED, |
1489 |
allowed, domain, filterq, sizeq, all_props) |
1490 |
objects.extend([(p, None) for p in prefixes] if virtual else []) |
1491 |
objects.sort(key=lambda x: x[0]) |
1492 |
objects = [(x[0][len(cont_prefix):],) + x[1:] for x in objects] |
1493 |
return objects
|
1494 |
|
1495 |
# Reporting functions.
|
1496 |
|
1497 |
def _report_size_change(self, user, account, size, details=None): |
1498 |
details = details or {}
|
1499 |
|
1500 |
if size == 0: |
1501 |
return
|
1502 |
|
1503 |
account_node = self._lookup_account(account, True)[1] |
1504 |
total = self._get_statistics(account_node, compute=True)[1] |
1505 |
details.update({'user': user, 'total': total}) |
1506 |
logger.debug( |
1507 |
"_report_size_change: %s %s %s %s", user, account, size, details)
|
1508 |
self.messages.append(
|
1509 |
(QUEUE_MESSAGE_KEY_PREFIX % ('resource.diskspace',),
|
1510 |
account, QUEUE_INSTANCE_ID, 'diskspace', float(size), details)) |
1511 |
|
1512 |
if not self.using_external_quotaholder: |
1513 |
return
|
1514 |
|
1515 |
try:
|
1516 |
name = details['path'] if 'path' in details else '' |
1517 |
serial = self.astakosclient.issue_one_commission(
|
1518 |
token=self.service_token,
|
1519 |
holder=account, |
1520 |
source=DEFAULT_SOURCE, |
1521 |
provisions={'pithos.diskspace': size},
|
1522 |
name=name) |
1523 |
except BaseException, e: |
1524 |
raise QuotaError(e)
|
1525 |
else:
|
1526 |
self.serials.append(serial)
|
1527 |
|
1528 |
def _report_object_change(self, user, account, path, details=None): |
1529 |
details = details or {}
|
1530 |
details.update({'user': user})
|
1531 |
logger.debug("_report_object_change: %s %s %s %s", user,
|
1532 |
account, path, details) |
1533 |
self.messages.append((QUEUE_MESSAGE_KEY_PREFIX % ('object',), |
1534 |
account, QUEUE_INSTANCE_ID, 'object', path,
|
1535 |
details)) |
1536 |
|
1537 |
def _report_sharing_change(self, user, account, path, details=None): |
1538 |
logger.debug("_report_permissions_change: %s %s %s %s",
|
1539 |
user, account, path, details) |
1540 |
details = details or {}
|
1541 |
details.update({'user': user})
|
1542 |
self.messages.append((QUEUE_MESSAGE_KEY_PREFIX % ('sharing',), |
1543 |
account, QUEUE_INSTANCE_ID, 'sharing', path,
|
1544 |
details)) |
1545 |
|
1546 |
# Policy functions.
|
1547 |
|
1548 |
def _check_policy(self, policy, is_account_policy=True): |
1549 |
default_policy = self.default_account_policy \
|
1550 |
if is_account_policy else self.default_container_policy |
1551 |
for k in policy.keys(): |
1552 |
if policy[k] == '': |
1553 |
policy[k] = default_policy.get(k) |
1554 |
for k, v in policy.iteritems(): |
1555 |
if k == 'quota': |
1556 |
q = int(v) # May raise ValueError. |
1557 |
if q < 0: |
1558 |
raise ValueError |
1559 |
elif k == 'versioning': |
1560 |
if v not in ['auto', 'none']: |
1561 |
raise ValueError |
1562 |
else:
|
1563 |
raise ValueError |
1564 |
|
1565 |
def _put_policy(self, node, policy, replace, is_account_policy=True): |
1566 |
default_policy = self.default_account_policy \
|
1567 |
if is_account_policy else self.default_container_policy |
1568 |
if replace:
|
1569 |
for k, v in default_policy.iteritems(): |
1570 |
if k not in policy: |
1571 |
policy[k] = v |
1572 |
self.node.policy_set(node, policy)
|
1573 |
|
1574 |
def _get_policy(self, node, is_account_policy=True): |
1575 |
default_policy = self.default_account_policy \
|
1576 |
if is_account_policy else self.default_container_policy |
1577 |
policy = default_policy.copy() |
1578 |
policy.update(self.node.policy_get(node))
|
1579 |
return policy
|
1580 |
|
1581 |
def _apply_versioning(self, account, container, version_id, |
1582 |
update_statistics_ancestors_depth=None):
|
1583 |
"""Delete the provided version if such is the policy.
|
1584 |
Return size of object removed.
|
1585 |
"""
|
1586 |
|
1587 |
if version_id is None: |
1588 |
return 0 |
1589 |
path, node = self._lookup_container(account, container)
|
1590 |
versioning = self._get_policy(
|
1591 |
node, is_account_policy=False)['versioning'] |
1592 |
if versioning != 'auto': |
1593 |
hash, size = self.node.version_remove( |
1594 |
version_id, update_statistics_ancestors_depth) |
1595 |
self.store.map_delete(hash) |
1596 |
return size
|
1597 |
elif self.free_versioning: |
1598 |
return self.node.version_get_properties( |
1599 |
version_id, keys=('size',))[0] |
1600 |
return 0 |
1601 |
|
1602 |
# Access control functions.
|
1603 |
|
1604 |
def _check_groups(self, groups): |
1605 |
# raise ValueError('Bad characters in groups')
|
1606 |
pass
|
1607 |
|
1608 |
def _check_permissions(self, path, permissions): |
1609 |
# raise ValueError('Bad characters in permissions')
|
1610 |
pass
|
1611 |
|
1612 |
def _get_formatted_paths(self, paths): |
1613 |
formatted = [] |
1614 |
for p in paths: |
1615 |
node = self.node.node_lookup(p)
|
1616 |
props = None
|
1617 |
if node is not None: |
1618 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1619 |
if props is not None: |
1620 |
if props[self.TYPE].split(';', 1)[0].strip() in ( |
1621 |
'application/directory', 'application/folder'): |
1622 |
formatted.append((p.rstrip('/') + '/', self.MATCH_PREFIX)) |
1623 |
formatted.append((p, self.MATCH_EXACT))
|
1624 |
return formatted
|
1625 |
|
1626 |
def _get_permissions_path(self, account, container, name): |
1627 |
path = '/'.join((account, container, name))
|
1628 |
permission_paths = self.permissions.access_inherit(path)
|
1629 |
permission_paths.sort() |
1630 |
permission_paths.reverse() |
1631 |
for p in permission_paths: |
1632 |
if p == path:
|
1633 |
return p
|
1634 |
else:
|
1635 |
if p.count('/') < 2: |
1636 |
continue
|
1637 |
node = self.node.node_lookup(p)
|
1638 |
props = None
|
1639 |
if node is not None: |
1640 |
props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
|
1641 |
if props is not None: |
1642 |
if props[self.TYPE].split(';', 1)[0].strip() in ( |
1643 |
'application/directory', 'application/folder'): |
1644 |
return p
|
1645 |
return None |
1646 |
|
1647 |
def _can_read(self, user, account, container, name): |
1648 |
if user == account:
|
1649 |
return True |
1650 |
path = '/'.join((account, container, name))
|
1651 |
if self.permissions.public_get(path) is not None: |
1652 |
return True |
1653 |
path = self._get_permissions_path(account, container, name)
|
1654 |
if not path: |
1655 |
raise NotAllowedError
|
1656 |
if (not self.permissions.access_check(path, self.READ, user) and not |
1657 |
self.permissions.access_check(path, self.WRITE, user)): |
1658 |
raise NotAllowedError
|
1659 |
|
1660 |
def _can_write(self, user, account, container, name): |
1661 |
if user == account:
|
1662 |
return True |
1663 |
path = '/'.join((account, container, name))
|
1664 |
path = self._get_permissions_path(account, container, name)
|
1665 |
if not path: |
1666 |
raise NotAllowedError
|
1667 |
if not self.permissions.access_check(path, self.WRITE, user): |
1668 |
raise NotAllowedError
|
1669 |
|
1670 |
def _allowed_accounts(self, user): |
1671 |
allow = set()
|
1672 |
for path in self.permissions.access_list_paths(user): |
1673 |
allow.add(path.split('/', 1)[0]) |
1674 |
return sorted(allow) |
1675 |
|
1676 |
def _allowed_containers(self, user, account): |
1677 |
allow = set()
|
1678 |
for path in self.permissions.access_list_paths(user, account): |
1679 |
allow.add(path.split('/', 2)[1]) |
1680 |
return sorted(allow) |
1681 |
|
1682 |
# Domain functions
|
1683 |
|
1684 |
def get_domain_objects(self, domain, user=None): |
1685 |
allowed_paths = self.permissions.access_list_paths(
|
1686 |
user, include_owned=user is not None, include_containers=False) |
1687 |
if not allowed_paths: |
1688 |
return []
|
1689 |
obj_list = self.node.domain_object_list(
|
1690 |
domain, allowed_paths, CLUSTER_NORMAL) |
1691 |
return [(path,
|
1692 |
self._build_metadata(props, user_defined_meta),
|
1693 |
self.permissions.access_get(path)) for |
1694 |
path, props, user_defined_meta in obj_list]
|
1695 |
|
1696 |
# util functions
|
1697 |
|
1698 |
def _build_metadata(self, props, user_defined=None, |
1699 |
include_user_defined=True):
|
1700 |
meta = {'bytes': props[self.SIZE], |
1701 |
'type': props[self.TYPE], |
1702 |
'hash': props[self.HASH], |
1703 |
'version': props[self.SERIAL], |
1704 |
'version_timestamp': props[self.MTIME], |
1705 |
'modified_by': props[self.MUSER], |
1706 |
'uuid': props[self.UUID], |
1707 |
'checksum': props[self.CHECKSUM]} |
1708 |
if include_user_defined and user_defined is not None: |
1709 |
meta.update(user_defined) |
1710 |
return meta
|
1711 |
|
1712 |
def _has_read_access(self, user, path): |
1713 |
try:
|
1714 |
account, container, object = path.split('/', 2) |
1715 |
except ValueError: |
1716 |
raise ValueError('Invalid object path') |
1717 |
|
1718 |
assert isinstance(user, basestring), "Invalid user" |
1719 |
|
1720 |
try:
|
1721 |
self._can_read(user, account, container, object) |
1722 |
except NotAllowedError:
|
1723 |
return False |
1724 |
else:
|
1725 |
return True |