Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / modular.py @ 371d907a

History | View | Annotate | Download (46.6 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 binascii
40

    
41
from base import DEFAULT_QUOTA, DEFAULT_VERSIONING, NotAllowedError, QuotaError, BaseBackend
42

    
43
from pithos.lib.hashmap import HashMap
44

    
45
# Default modules and settings.
46
DEFAULT_DB_MODULE = 'pithos.backends.lib.sqlalchemy'
47
DEFAULT_DB_CONNECTION = 'sqlite:///backend.db'
48
DEFAULT_BLOCK_MODULE = 'pithos.backends.lib.hashfiler'
49
DEFAULT_BLOCK_PATH = 'data/'
50
#DEFAULT_QUEUE_MODULE = 'pithos.backends.lib.rabbitmq'
51
#DEFAULT_QUEUE_CONNECTION = 'rabbitmq://guest:guest@localhost:5672/pithos'
52

    
53
QUEUE_MESSAGE_KEY = '#'
54
QUEUE_CLIENT_ID = 2 # Pithos.
55

    
56
( CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED ) = range(3)
57

    
58
inf = float('inf')
59

    
60
ULTIMATE_ANSWER = 42
61

    
62

    
63
logger = logging.getLogger(__name__)
64

    
65

    
66
def backend_method(func=None, autocommit=1):
67
    if func is None:
68
        def fn(func):
69
            return backend_method(func, autocommit)
70
        return fn
71

    
72
    if not autocommit:
73
        return func
74
    def fn(self, *args, **kw):
75
        self.wrapper.execute()
76
        try:
77
            ret = func(self, *args, **kw)
78
            self.wrapper.commit()
79
            return ret
80
        except:
81
            self.wrapper.rollback()
82
            raise
83
    return fn
84

    
85

    
86
class ModularBackend(BaseBackend):
87
    """A modular backend.
88
    
89
    Uses modules for SQL functions and storage.
90
    """
91
    
92
    def __init__(self, db_module=None, db_connection=None,
93
                 block_module=None, block_path=None,
94
                 queue_module=None, queue_connection=None):
95
        db_module = db_module or DEFAULT_DB_MODULE
96
        db_connection = db_connection or DEFAULT_DB_CONNECTION
97
        block_module = block_module or DEFAULT_BLOCK_MODULE
98
        block_path = block_path or DEFAULT_BLOCK_PATH
99
        #queue_module = queue_module or DEFAULT_QUEUE_MODULE
100
        #queue_connection = queue_connection or DEFAULT_QUEUE_CONNECTION
101
        
102
        self.hash_algorithm = 'sha256'
103
        self.block_size = 4 * 1024 * 1024 # 4MB
104
        
105
        self.default_policy = {'quota': DEFAULT_QUOTA, 'versioning': DEFAULT_VERSIONING}
106
        
107
        def load_module(m):
108
            __import__(m)
109
            return sys.modules[m]
110
        
111
        self.db_module = load_module(db_module)
112
        self.wrapper = self.db_module.DBWrapper(db_connection)
113
        params = {'wrapper': self.wrapper}
114
        self.permissions = self.db_module.Permissions(**params)
115
        for x in ['READ', 'WRITE']:
116
            setattr(self, x, getattr(self.db_module, x))
117
        self.node = self.db_module.Node(**params)
118
        for x in ['ROOTNODE', 'SERIAL', 'HASH', 'SIZE', 'TYPE', 'MTIME', 'MUSER', 'UUID', 'CHECKSUM', 'CLUSTER', 'MATCH_PREFIX', 'MATCH_EXACT']:
119
            setattr(self, x, getattr(self.db_module, x))
120
        
121
        self.block_module = load_module(block_module)
122
        params = {'path': block_path,
123
                  'block_size': self.block_size,
124
                  'hash_algorithm': self.hash_algorithm}
125
        self.store = self.block_module.Store(**params)
126

    
127
        if queue_module and queue_connection:
128
            self.queue_module = load_module(queue_module)
129
            params = {'exchange': queue_connection,
130
                      'message_key': QUEUE_MESSAGE_KEY,
131
                      'client_id': QUEUE_CLIENT_ID}
132
            self.queue = self.queue_module.Queue(**params)
133
        else:
134
            class NoQueue:
135
                def send(self, *args):
136
                    pass
137
                
138
                def close(self):
139
                    pass
140
            
141
            self.queue = NoQueue()
142
    
143
    def close(self):
144
        self.wrapper.close()
145
        self.queue.close()
146
    
147
    @backend_method
148
    def list_accounts(self, user, marker=None, limit=10000):
149
        """Return a list of accounts the user can access."""
150
        
151
        logger.debug("list_accounts: %s %s %s", user, marker, limit)
152
        allowed = self._allowed_accounts(user)
153
        start, limit = self._list_limits(allowed, marker, limit)
154
        return allowed[start:start + limit]
155
    
156
    @backend_method
157
    def get_account_meta(self, user, account, domain, until=None):
158
        """Return a dictionary with the account metadata for the domain."""
159
        
160
        logger.debug("get_account_meta: %s %s %s", account, domain, until)
161
        path, node = self._lookup_account(account, user == account)
162
        if user != account:
163
            if until or node is None or account not in self._allowed_accounts(user):
164
                raise NotAllowedError
165
        try:
166
            props = self._get_properties(node, until)
167
            mtime = props[self.MTIME]
168
        except NameError:
169
            props = None
170
            mtime = until
171
        count, bytes, tstamp = self._get_statistics(node, until)
172
        tstamp = max(tstamp, mtime)
173
        if until is None:
174
            modified = tstamp
175
        else:
176
            modified = self._get_statistics(node)[2] # Overall last modification.
177
            modified = max(modified, mtime)
178
        
179
        if user != account:
180
            meta = {'name': account}
181
        else:
182
            meta = {}
183
            if props is not None:
184
                meta.update(dict(self.node.attribute_get(props[self.SERIAL], domain)))
185
            if until is not None:
186
                meta.update({'until_timestamp': tstamp})
187
            meta.update({'name': account, 'count': count, 'bytes': bytes})
188
        meta.update({'modified': modified})
189
        return meta
190
    
191
    @backend_method
192
    def update_account_meta(self, user, account, domain, meta, replace=False):
193
        """Update the metadata associated with the account for the domain."""
194
        
195
        logger.debug("update_account_meta: %s %s %s %s", account, domain, meta, replace)
196
        if user != account:
197
            raise NotAllowedError
198
        path, node = self._lookup_account(account, True)
199
        self._put_metadata(user, node, domain, meta, replace)
200
    
201
    @backend_method
202
    def get_account_groups(self, user, account):
203
        """Return a dictionary with the user groups defined for this account."""
204
        
205
        logger.debug("get_account_groups: %s", account)
206
        if user != account:
207
            if account not in self._allowed_accounts(user):
208
                raise NotAllowedError
209
            return {}
210
        self._lookup_account(account, True)
211
        return self.permissions.group_dict(account)
212
    
213
    @backend_method
214
    def update_account_groups(self, user, account, groups, replace=False):
215
        """Update the groups associated with the account."""
216
        
217
        logger.debug("update_account_groups: %s %s %s", account, groups, replace)
218
        if user != account:
219
            raise NotAllowedError
220
        self._lookup_account(account, True)
221
        self._check_groups(groups)
222
        if replace:
223
            self.permissions.group_destroy(account)
224
        for k, v in groups.iteritems():
225
            if not replace: # If not already deleted.
226
                self.permissions.group_delete(account, k)
227
            if v:
228
                self.permissions.group_addmany(account, k, v)
229
    
230
    @backend_method
231
    def get_account_policy(self, user, account):
232
        """Return a dictionary with the account policy."""
233
        
234
        logger.debug("get_account_policy: %s", account)
235
        if user != account:
236
            if account not in self._allowed_accounts(user):
237
                raise NotAllowedError
238
            return {}
239
        path, node = self._lookup_account(account, True)
240
        return self._get_policy(node)
241
    
242
    @backend_method
243
    def update_account_policy(self, user, account, policy, replace=False):
244
        """Update the policy associated with the account."""
245
        
246
        logger.debug("update_account_policy: %s %s %s", account, policy, replace)
247
        if user != account:
248
            raise NotAllowedError
249
        path, node = self._lookup_account(account, True)
250
        self._check_policy(policy)
251
        self._put_policy(node, policy, replace)
252
    
253
    @backend_method
254
    def put_account(self, user, account, policy={}):
255
        """Create a new account with the given name."""
256
        
257
        logger.debug("put_account: %s %s", account, policy)
258
        if user != account:
259
            raise NotAllowedError
260
        node = self.node.node_lookup(account)
261
        if node is not None:
262
            raise NameError('Account already exists')
263
        if policy:
264
            self._check_policy(policy)
265
        node = self._put_path(user, self.ROOTNODE, account)
266
        self._put_policy(node, policy, True)
267
    
268
    @backend_method
269
    def delete_account(self, user, account):
270
        """Delete the account with the given name."""
271
        
272
        logger.debug("delete_account: %s", account)
273
        if user != account:
274
            raise NotAllowedError
275
        node = self.node.node_lookup(account)
276
        if node is None:
277
            return
278
        if not self.node.node_remove(node):
279
            raise IndexError('Account is not empty')
280
        self.permissions.group_destroy(account)
281
    
282
    @backend_method
283
    def list_containers(self, user, account, marker=None, limit=10000, shared=False, until=None):
284
        """Return a list of containers existing under an account."""
285
        
286
        logger.debug("list_containers: %s %s %s %s %s", account, marker, limit, shared, until)
287
        if user != account:
288
            if until or account not in self._allowed_accounts(user):
289
                raise NotAllowedError
290
            allowed = self._allowed_containers(user, account)
291
            start, limit = self._list_limits(allowed, marker, limit)
292
            return allowed[start:start + limit]
293
        if shared:
294
            allowed = [x.split('/', 2)[1] for x in self.permissions.access_list_shared(account)]
295
            allowed = list(set(allowed))
296
            start, limit = self._list_limits(allowed, marker, limit)
297
            return allowed[start:start + limit]
298
        node = self.node.node_lookup(account)
299
        return [x[0] for x in self._list_object_properties(node, account, '', '/', marker, limit, False, None, [], until)]
300
    
301
    @backend_method
302
    def list_container_meta(self, user, account, container, domain, until=None):
303
        """Return a list with all the container's object meta keys for the domain."""
304
        
305
        logger.debug("list_container_meta: %s %s %s %s", account, container, domain, until)
306
        allowed = []
307
        if user != account:
308
            if until:
309
                raise NotAllowedError
310
            allowed = self.permissions.access_list_paths(user, '/'.join((account, container)))
311
            if not allowed:
312
                raise NotAllowedError
313
        path, node = self._lookup_container(account, container)
314
        before = until if until is not None else inf
315
        allowed = self._get_formatted_paths(allowed)
316
        return self.node.latest_attribute_keys(node, domain, before, CLUSTER_DELETED, allowed)
317
    
318
    @backend_method
319
    def get_container_meta(self, user, account, container, domain, until=None):
320
        """Return a dictionary with the container metadata for the domain."""
321
        
322
        logger.debug("get_container_meta: %s %s %s %s", account, container, domain, until)
323
        if user != account:
324
            if until or container not in self._allowed_containers(user, account):
325
                raise NotAllowedError
326
        path, node = self._lookup_container(account, container)
327
        props = self._get_properties(node, until)
328
        mtime = props[self.MTIME]
329
        count, bytes, tstamp = self._get_statistics(node, until)
330
        tstamp = max(tstamp, mtime)
331
        if until is None:
332
            modified = tstamp
333
        else:
334
            modified = self._get_statistics(node)[2] # Overall last modification.
335
            modified = max(modified, mtime)
336
        
337
        if user != account:
338
            meta = {'name': container}
339
        else:
340
            meta = dict(self.node.attribute_get(props[self.SERIAL], domain))
341
            if until is not None:
342
                meta.update({'until_timestamp': tstamp})
343
            meta.update({'name': container, 'count': count, 'bytes': bytes})
344
        meta.update({'modified': modified})
345
        return meta
346
    
347
    @backend_method
348
    def update_container_meta(self, user, account, container, domain, meta, replace=False):
349
        """Update the metadata associated with the container for the domain."""
350
        
351
        logger.debug("update_container_meta: %s %s %s %s %s", account, container, domain, meta, replace)
352
        if user != account:
353
            raise NotAllowedError
354
        path, node = self._lookup_container(account, container)
355
        self._put_metadata(user, node, domain, meta, replace)
356
    
357
    @backend_method
358
    def get_container_policy(self, user, account, container):
359
        """Return a dictionary with the container policy."""
360
        
361
        logger.debug("get_container_policy: %s %s", account, container)
362
        if user != account:
363
            if container not in self._allowed_containers(user, account):
364
                raise NotAllowedError
365
            return {}
366
        path, node = self._lookup_container(account, container)
367
        return self._get_policy(node)
368
    
369
    @backend_method
370
    def update_container_policy(self, user, account, container, policy, replace=False):
371
        """Update the policy associated with the container."""
372
        
373
        logger.debug("update_container_policy: %s %s %s %s", account, container, policy, replace)
374
        if user != account:
375
            raise NotAllowedError
376
        path, node = self._lookup_container(account, container)
377
        self._check_policy(policy)
378
        self._put_policy(node, policy, replace)
379
    
380
    @backend_method
381
    def put_container(self, user, account, container, policy={}):
382
        """Create a new container with the given name."""
383
        
384
        logger.debug("put_container: %s %s %s", account, container, policy)
385
        if user != account:
386
            raise NotAllowedError
387
        try:
388
            path, node = self._lookup_container(account, container)
389
        except NameError:
390
            pass
391
        else:
392
            raise NameError('Container already exists')
393
        if policy:
394
            self._check_policy(policy)
395
        path = '/'.join((account, container))
396
        node = self._put_path(user, self._lookup_account(account, True)[1], path)
397
        self._put_policy(node, policy, True)
398
    
399
    @backend_method
400
    def delete_container(self, user, account, container, until=None):
401
        """Delete/purge the container with the given name."""
402
        
403
        logger.debug("delete_container: %s %s %s", account, container, until)
404
        if user != account:
405
            raise NotAllowedError
406
        path, node = self._lookup_container(account, container)
407
        
408
        if until is not None:
409
            hashes, size = self.node.node_purge_children(node, until, CLUSTER_HISTORY)
410
            for h in hashes:
411
                self.store.map_delete(h)
412
            self.node.node_purge_children(node, until, CLUSTER_DELETED)
413
            self._report_size_change(user, account, -size, {'action': 'container purge'})
414
            return
415
        
416
        if self._get_statistics(node)[0] > 0:
417
            raise IndexError('Container is not empty')
418
        hashes, size = self.node.node_purge_children(node, inf, CLUSTER_HISTORY)
419
        for h in hashes:
420
            self.store.map_delete(h)
421
        self.node.node_purge_children(node, inf, CLUSTER_DELETED)
422
        self.node.node_remove(node)
423
        self._report_size_change(user, account, -size, {'action': 'container delete'})
424
    
425
    def _list_objects(self, user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, all_props):
426
        allowed = []
427
        if user != account:
428
            if until:
429
                raise NotAllowedError
430
            allowed = self.permissions.access_list_paths(user, '/'.join((account, container)))
431
            if not allowed:
432
                raise NotAllowedError
433
        else:
434
            if shared:
435
                allowed = self.permissions.access_list_shared('/'.join((account, container)))
436
                if not allowed:
437
                    return []
438
        path, node = self._lookup_container(account, container)
439
        allowed = self._get_formatted_paths(allowed)
440
        return self._list_object_properties(node, path, prefix, delimiter, marker, limit, virtual, domain, keys, until, size_range, allowed, all_props)
441
    
442
    @backend_method
443
    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):
444
        """Return a list of object (name, version_id) tuples existing under a container."""
445
        
446
        logger.debug("list_objects: %s %s %s %s %s %s %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range)
447
        return self._list_objects(user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, False)
448
    
449
    @backend_method
450
    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):
451
        """Return a list of object metadata dicts existing under a container."""
452
        
453
        logger.debug("list_object_meta: %s %s %s %s %s %s %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range)
454
        props = self._list_objects(user, account, container, prefix, delimiter, marker, limit, virtual, domain, keys, shared, until, size_range, True)
455
        objects = []
456
        for p in props:
457
            if len(p) == 2:
458
                objects.append({'subdir': p[0]})
459
            else:
460
                objects.append({'name': p[0],
461
                                'bytes': p[self.SIZE + 1],
462
                                'type': p[self.TYPE + 1],
463
                                'hash': p[self.HASH + 1],
464
                                'version': p[self.SERIAL + 1],
465
                                'version_timestamp': p[self.MTIME + 1],
466
                                'modified': p[self.MTIME + 1] if until is None else None,
467
                                'modified_by': p[self.MUSER + 1],
468
                                'uuid': p[self.UUID + 1],
469
                                'checksum': p[self.CHECKSUM + 1]})
470
        return objects
471
    
472
    @backend_method
473
    def get_object_meta(self, user, account, container, name, domain, version=None):
474
        """Return a dictionary with the object metadata for the domain."""
475
        
476
        logger.debug("get_object_meta: %s %s %s %s %s", account, container, name, domain, version)
477
        self._can_read(user, account, container, name)
478
        path, node = self._lookup_object(account, container, name)
479
        props = self._get_version(node, version)
480
        if version is None:
481
            modified = props[self.MTIME]
482
        else:
483
            try:
484
                modified = self._get_version(node)[self.MTIME] # Overall last modification.
485
            except NameError: # Object may be deleted.
486
                del_props = self.node.version_lookup(node, inf, CLUSTER_DELETED)
487
                if del_props is None:
488
                    raise NameError('Object does not exist')
489
                modified = del_props[self.MTIME]
490
        
491
        meta = dict(self.node.attribute_get(props[self.SERIAL], domain))
492
        meta.update({'name': name,
493
                     'bytes': props[self.SIZE],
494
                     'type': props[self.TYPE],
495
                     'hash': props[self.HASH],
496
                     'version': props[self.SERIAL],
497
                     'version_timestamp': props[self.MTIME],
498
                     'modified': modified,
499
                     'modified_by': props[self.MUSER],
500
                     'uuid': props[self.UUID],
501
                     'checksum': props[self.CHECKSUM]})
502
        return meta
503
    
504
    @backend_method
505
    def update_object_meta(self, user, account, container, name, domain, meta, replace=False):
506
        """Update the metadata associated with the object for the domain and return the new version."""
507
        
508
        logger.debug("update_object_meta: %s %s %s %s %s %s", account, container, name, domain, meta, replace)
509
        self._can_write(user, account, container, name)
510
        path, node = self._lookup_object(account, container, name)
511
        src_version_id, dest_version_id = self._put_metadata(user, node, domain, meta, replace)
512
        self._apply_versioning(account, container, src_version_id)
513
        return dest_version_id
514
    
515
    @backend_method
516
    def get_object_permissions(self, user, account, container, name):
517
        """Return the action allowed on the object, the path
518
        from which the object gets its permissions from,
519
        along with a dictionary containing the permissions."""
520
        
521
        logger.debug("get_object_permissions: %s %s %s", account, container, name)
522
        allowed = 'write'
523
        permissions_path = self._get_permissions_path(account, container, name)
524
        if user != account:
525
            if self.permissions.access_check(permissions_path, self.WRITE, user):
526
                allowed = 'write'
527
            elif self.permissions.access_check(permissions_path, self.READ, user):
528
                allowed = 'read'
529
            else:
530
                raise NotAllowedError
531
        self._lookup_object(account, container, name)
532
        return (allowed, permissions_path, self.permissions.access_get(permissions_path))
533
    
534
    @backend_method
535
    def update_object_permissions(self, user, account, container, name, permissions):
536
        """Update the permissions associated with the object."""
537
        
538
        logger.debug("update_object_permissions: %s %s %s %s", account, container, name, permissions)
539
        if user != account:
540
            raise NotAllowedError
541
        path = self._lookup_object(account, container, name)[0]
542
        self._check_permissions(path, permissions)
543
        self.permissions.access_set(path, permissions)
544
    
545
    @backend_method
546
    def get_object_public(self, user, account, container, name):
547
        """Return the public id of the object if applicable."""
548
        
549
        logger.debug("get_object_public: %s %s %s", account, container, name)
550
        self._can_read(user, account, container, name)
551
        path = self._lookup_object(account, container, name)[0]
552
        p = self.permissions.public_get(path)
553
        if p is not None:
554
            p += ULTIMATE_ANSWER
555
        return p
556
    
557
    @backend_method
558
    def update_object_public(self, user, account, container, name, public):
559
        """Update the public status of the object."""
560
        
561
        logger.debug("update_object_public: %s %s %s %s", account, container, name, public)
562
        self._can_write(user, account, container, name)
563
        path = self._lookup_object(account, container, name)[0]
564
        if not public:
565
            self.permissions.public_unset(path)
566
        else:
567
            self.permissions.public_set(path)
568
    
569
    @backend_method
570
    def get_object_hashmap(self, user, account, container, name, version=None):
571
        """Return the object's size and a list with partial hashes."""
572
        
573
        logger.debug("get_object_hashmap: %s %s %s %s", account, container, name, version)
574
        self._can_read(user, account, container, name)
575
        path, node = self._lookup_object(account, container, name)
576
        props = self._get_version(node, version)
577
        hashmap = self.store.map_get(binascii.unhexlify(props[self.HASH]))
578
        return props[self.SIZE], [binascii.hexlify(x) for x in hashmap]
579
    
580
    def _update_object_hash(self, user, account, container, name, size, type, hash, checksum, permissions, src_node=None, is_copy=False):
581
        if permissions is not None and user != account:
582
            raise NotAllowedError
583
        self._can_write(user, account, container, name)
584
        if permissions is not None:
585
            path = '/'.join((account, container, name))
586
            self._check_permissions(path, permissions)
587
        
588
        account_path, account_node = self._lookup_account(account, True)
589
        container_path, container_node = self._lookup_container(account, container)
590
        path, node = self._put_object_node(container_path, container_node, name)
591
        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)
592
        
593
        # Check quota.
594
        del_size = self._apply_versioning(account, container, pre_version_id)
595
        size_delta = size - del_size
596
        if size_delta > 0:
597
            account_quota = long(self._get_policy(account_node)['quota'])
598
            container_quota = long(self._get_policy(container_node)['quota'])
599
            if (account_quota > 0 and self._get_statistics(account_node)[1] + size_delta > account_quota) or \
600
               (container_quota > 0 and self._get_statistics(container_node)[1] + size_delta > container_quota):
601
                # This must be executed in a transaction, so the version is never created if it fails.
602
                raise QuotaError
603
        self._report_size_change(user, account, size_delta, {'action': 'object update'})
604
        
605
        if permissions is not None:
606
            self.permissions.access_set(path, permissions)
607
        return pre_version_id, dest_version_id
608
    
609
    @backend_method
610
    def update_object_hashmap(self, user, account, container, name, size, type, hashmap, checksum, domain, meta={}, replace_meta=False, permissions=None):
611
        """Create/update an object with the specified size and partial hashes."""
612
        
613
        logger.debug("update_object_hashmap: %s %s %s %s %s %s %s", account, container, name, size, type, hashmap, checksum)
614
        if size == 0: # No such thing as an empty hashmap.
615
            hashmap = [self.put_block('')]
616
        map = HashMap(self.block_size, self.hash_algorithm)
617
        map.extend([binascii.unhexlify(x) for x in hashmap])
618
        missing = self.store.block_search(map)
619
        if missing:
620
            ie = IndexError()
621
            ie.data = [binascii.hexlify(x) for x in missing]
622
            raise ie
623
        
624
        hash = map.hash()
625
        pre_version_id, dest_version_id = self._update_object_hash(user, account, container, name, size, type, binascii.hexlify(hash), checksum, permissions)
626
        self._put_metadata_duplicate(pre_version_id, dest_version_id, domain, meta, replace_meta)
627
        self.store.map_put(hash, map)
628
        return dest_version_id
629
    
630
    @backend_method
631
    def update_object_checksum(self, user, account, container, name, version, checksum):
632
        """Update an object's checksum."""
633
        
634
        logger.debug("update_object_checksum: %s %s %s %s %s", account, container, name, version, checksum)
635
        # Update objects with greater version and same hashmap and size (fix metadata updates).
636
        self._can_write(user, account, container, name)
637
        path, node = self._lookup_object(account, container, name)
638
        props = self._get_version(node, version)
639
        versions = self.node.node_get_versions(node)
640
        for x in versions:
641
            if x[self.SERIAL] >= int(version) and x[self.HASH] == props[self.HASH] and x[self.SIZE] == props[self.SIZE]:
642
                self.node.version_put_property(x[self.SERIAL], 'checksum', checksum)
643
    
644
    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):
645
        self._can_read(user, src_account, src_container, src_name)
646
        path, node = self._lookup_object(src_account, src_container, src_name)
647
        # TODO: Will do another fetch of the properties in duplicate version...
648
        props = self._get_version(node, src_version) # Check to see if source exists.
649
        src_version_id = props[self.SERIAL]
650
        hash = props[self.HASH]
651
        size = props[self.SIZE]
652
        
653
        is_copy = not is_move and (src_account, src_container, src_name) != (dest_account, dest_container, dest_name) # New uuid.
654
        pre_version_id, dest_version_id = self._update_object_hash(user, dest_account, dest_container, dest_name, size, type, hash, None, permissions, src_node=node, is_copy=is_copy)
655
        self._put_metadata_duplicate(src_version_id, dest_version_id, dest_domain, dest_meta, replace_meta)
656
        return dest_version_id
657
    
658
    @backend_method
659
    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):
660
        """Copy an object's data and metadata."""
661
        
662
        logger.debug("copy_object: %s %s %s %s %s %s %s %s %s %s %s %s", src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions, src_version)
663
        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)
664
        return dest_version_id
665
    
666
    @backend_method
667
    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):
668
        """Move an object's data and metadata."""
669
        
670
        logger.debug("move_object: %s %s %s %s %s %s %s %s %s %s %s", src_account, src_container, src_name, dest_account, dest_container, dest_name, type, domain, meta, replace_meta, permissions)
671
        if user != src_account:
672
            raise NotAllowedError
673
        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)
674
        if (src_account, src_container, src_name) != (dest_account, dest_container, dest_name):
675
            self._delete_object(user, src_account, src_container, src_name)
676
        return dest_version_id
677
    
678
    def _delete_object(self, user, account, container, name, until=None):
679
        if user != account:
680
            raise NotAllowedError
681
        
682
        if until is not None:
683
            path = '/'.join((account, container, name))
684
            node = self.node.node_lookup(path)
685
            if node is None:
686
                return
687
            hashes = []
688
            size = 0
689
            h, s = self.node.node_purge(node, until, CLUSTER_NORMAL)
690
            hashes += h
691
            size += s
692
            h, s = self.node.node_purge(node, until, CLUSTER_HISTORY)
693
            hashes += h
694
            size += s
695
            for h in hashes:
696
                self.store.map_delete(h)
697
            self.node.node_purge(node, until, CLUSTER_DELETED)
698
            try:
699
                props = self._get_version(node)
700
            except NameError:
701
                self.permissions.access_clear(path)
702
            self._report_size_change(user, account, -size, {'action': 'object purge'})
703
            return
704
        
705
        path, node = self._lookup_object(account, container, name)
706
        src_version_id, dest_version_id = self._put_version_duplicate(user, node, size=0, type='', hash=None, checksum='', cluster=CLUSTER_DELETED)
707
        del_size = self._apply_versioning(account, container, src_version_id)
708
        if del_size:
709
            self._report_size_change(user, account, -del_size, {'action': 'object delete'})
710
        self.permissions.access_clear(path)
711
    
712
    @backend_method
713
    def delete_object(self, user, account, container, name, until=None):
714
        """Delete/purge an object."""
715
        
716
        logger.debug("delete_object: %s %s %s %s", account, container, name, until)
717
        self._delete_object(user, account, container, name, until)
718
    
719
    @backend_method
720
    def list_versions(self, user, account, container, name):
721
        """Return a list of all (version, version_timestamp) tuples for an object."""
722
        
723
        logger.debug("list_versions: %s %s %s", account, container, name)
724
        self._can_read(user, account, container, name)
725
        path, node = self._lookup_object(account, container, name)
726
        versions = self.node.node_get_versions(node)
727
        return [[x[self.SERIAL], x[self.MTIME]] for x in versions if x[self.CLUSTER] != CLUSTER_DELETED]
728
    
729
    @backend_method
730
    def get_uuid(self, user, uuid):
731
        """Return the (account, container, name) for the UUID given."""
732
        
733
        logger.debug("get_uuid: %s", uuid)
734
        info = self.node.latest_uuid(uuid)
735
        if info is None:
736
            raise NameError
737
        path, serial = info
738
        account, container, name = path.split('/', 2)
739
        self._can_read(user, account, container, name)
740
        return (account, container, name)
741
    
742
    @backend_method
743
    def get_public(self, user, public):
744
        """Return the (account, container, name) for the public id given."""
745
        
746
        logger.debug("get_public: %s", public)
747
        if public is None or public < ULTIMATE_ANSWER:
748
            raise NameError
749
        path = self.permissions.public_path(public - ULTIMATE_ANSWER)
750
        if path is None:
751
            raise NameError
752
        account, container, name = path.split('/', 2)
753
        self._can_read(user, account, container, name)
754
        return (account, container, name)
755
    
756
    @backend_method(autocommit=0)
757
    def get_block(self, hash):
758
        """Return a block's data."""
759
        
760
        logger.debug("get_block: %s", hash)
761
        block = self.store.block_get(binascii.unhexlify(hash))
762
        if not block:
763
            raise NameError('Block does not exist')
764
        return block
765
    
766
    @backend_method(autocommit=0)
767
    def put_block(self, data):
768
        """Store a block and return the hash."""
769
        
770
        logger.debug("put_block: %s", len(data))
771
        return binascii.hexlify(self.store.block_put(data))
772
    
773
    @backend_method(autocommit=0)
774
    def update_block(self, hash, data, offset=0):
775
        """Update a known block and return the hash."""
776
        
777
        logger.debug("update_block: %s %s %s", hash, len(data), offset)
778
        if offset == 0 and len(data) == self.block_size:
779
            return self.put_block(data)
780
        h = self.store.block_update(binascii.unhexlify(hash), offset, data)
781
        return binascii.hexlify(h)
782
    
783
    # Path functions.
784
    
785
    def _generate_uuid(self):
786
        return str(uuidlib.uuid4())
787
    
788
    def _put_object_node(self, path, parent, name):
789
        path = '/'.join((path, name))
790
        node = self.node.node_lookup(path)
791
        if node is None:
792
            node = self.node.node_create(parent, path)
793
        return path, node
794
    
795
    def _put_path(self, user, parent, path):
796
        node = self.node.node_create(parent, path)
797
        self.node.version_create(node, None, 0, '', None, user, self._generate_uuid(), '', CLUSTER_NORMAL)
798
        return node
799
    
800
    def _lookup_account(self, account, create=True):
801
        node = self.node.node_lookup(account)
802
        if node is None and create:
803
            node = self._put_path(account, self.ROOTNODE, account) # User is account.
804
        return account, node
805
    
806
    def _lookup_container(self, account, container):
807
        path = '/'.join((account, container))
808
        node = self.node.node_lookup(path)
809
        if node is None:
810
            raise NameError('Container does not exist')
811
        return path, node
812
    
813
    def _lookup_object(self, account, container, name):
814
        path = '/'.join((account, container, name))
815
        node = self.node.node_lookup(path)
816
        if node is None:
817
            raise NameError('Object does not exist')
818
        return path, node
819
    
820
    def _get_properties(self, node, until=None):
821
        """Return properties until the timestamp given."""
822
        
823
        before = until if until is not None else inf
824
        props = self.node.version_lookup(node, before, CLUSTER_NORMAL)
825
        if props is None and until is not None:
826
            props = self.node.version_lookup(node, before, CLUSTER_HISTORY)
827
        if props is None:
828
            raise NameError('Path does not exist')
829
        return props
830
    
831
    def _get_statistics(self, node, until=None):
832
        """Return count, sum of size and latest timestamp of everything under node."""
833
        
834
        if until is None:
835
            stats = self.node.statistics_get(node, CLUSTER_NORMAL)
836
        else:
837
            stats = self.node.statistics_latest(node, until, CLUSTER_DELETED)
838
        if stats is None:
839
            stats = (0, 0, 0)
840
        return stats
841
    
842
    def _get_version(self, node, version=None):
843
        if version is None:
844
            props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
845
            if props is None:
846
                raise NameError('Object does not exist')
847
        else:
848
            try:
849
                version = int(version)
850
            except ValueError:
851
                raise IndexError('Version does not exist')
852
            props = self.node.version_get_properties(version)
853
            if props is None or props[self.CLUSTER] == CLUSTER_DELETED:
854
                raise IndexError('Version does not exist')
855
        return props
856
    
857
    def _put_version_duplicate(self, user, node, src_node=None, size=None, type=None, hash=None, checksum=None, cluster=CLUSTER_NORMAL, is_copy=False):
858
        """Create a new version of the node."""
859
        
860
        props = self.node.version_lookup(node if src_node is None else src_node, inf, CLUSTER_NORMAL)
861
        if props is not None:
862
            src_version_id = props[self.SERIAL]
863
            src_hash = props[self.HASH]
864
            src_size = props[self.SIZE]
865
            src_type = props[self.TYPE]
866
            src_checksum = props[self.CHECKSUM]
867
        else:
868
            src_version_id = None
869
            src_hash = None
870
            src_size = 0
871
            src_type = ''
872
            src_checksum = ''
873
        if size is None: # Set metadata.
874
            hash = src_hash # This way hash can be set to None (account or container).
875
            size = src_size
876
        if type is None:
877
            type = src_type
878
        if checksum is None:
879
            checksum = src_checksum
880
        uuid = self._generate_uuid() if (is_copy or src_version_id is None) else props[self.UUID]
881
        
882
        if src_node is None:
883
            pre_version_id = src_version_id
884
        else:
885
            pre_version_id = None
886
            props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
887
            if props is not None:
888
                pre_version_id = props[self.SERIAL]
889
        if pre_version_id is not None:
890
            self.node.version_recluster(pre_version_id, CLUSTER_HISTORY)
891
        
892
        dest_version_id, mtime = self.node.version_create(node, hash, size, type, src_version_id, user, uuid, checksum, cluster)
893
        return pre_version_id, dest_version_id
894
    
895
    def _put_metadata_duplicate(self, src_version_id, dest_version_id, domain, meta, replace=False):
896
        if src_version_id is not None:
897
            self.node.attribute_copy(src_version_id, dest_version_id)
898
        if not replace:
899
            self.node.attribute_del(dest_version_id, domain, (k for k, v in meta.iteritems() if v == ''))
900
            self.node.attribute_set(dest_version_id, domain, ((k, v) for k, v in meta.iteritems() if v != ''))
901
        else:
902
            self.node.attribute_del(dest_version_id, domain)
903
            self.node.attribute_set(dest_version_id, domain, ((k, v) for k, v in meta.iteritems()))
904
    
905
    def _put_metadata(self, user, node, domain, meta, replace=False):
906
        """Create a new version and store metadata."""
907
        
908
        src_version_id, dest_version_id = self._put_version_duplicate(user, node)
909
        self._put_metadata_duplicate(src_version_id, dest_version_id, domain, meta, replace)
910
        return src_version_id, dest_version_id
911
    
912
    def _list_limits(self, listing, marker, limit):
913
        start = 0
914
        if marker:
915
            try:
916
                start = listing.index(marker) + 1
917
            except ValueError:
918
                pass
919
        if not limit or limit > 10000:
920
            limit = 10000
921
        return start, limit
922
    
923
    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):
924
        cont_prefix = path + '/'
925
        prefix = cont_prefix + prefix
926
        start = cont_prefix + marker if marker else None
927
        before = until if until is not None else inf
928
        filterq = keys if domain else []
929
        sizeq = size_range
930
        
931
        objects, prefixes = self.node.latest_version_list(parent, prefix, delimiter, start, limit, before, CLUSTER_DELETED, allowed, domain, filterq, sizeq, all_props)
932
        objects.extend([(p, None) for p in prefixes] if virtual else [])
933
        objects.sort(key=lambda x: x[0])
934
        objects = [(x[0][len(cont_prefix):],) + x[1:] for x in objects]
935
        
936
        start, limit = self._list_limits([x[0] for x in objects], marker, limit)
937
        return objects[start:start + limit]
938
    
939
    # Reporting functions.
940
    
941
    def _report_size_change(self, user, account, size, details={}):
942
        logger.debug("_report_size_change: %s %s %s %s", user, account, size, details)
943
        account_node = self._lookup_account(account, True)[1]
944
        total = self._get_statistics(account_node)[1]
945
        details.update({'user': user, 'total': total})
946
        self.queue.send(account, 'diskspace', size, details)
947
    
948
    # Policy functions.
949
    
950
    def _check_policy(self, policy):
951
        for k in policy.keys():
952
            if policy[k] == '':
953
                policy[k] = self.default_policy.get(k)
954
        for k, v in policy.iteritems():
955
            if k == 'quota':
956
                q = int(v) # May raise ValueError.
957
                if q < 0:
958
                    raise ValueError
959
            elif k == 'versioning':
960
                if v not in ['auto', 'none']:
961
                    raise ValueError
962
            else:
963
                raise ValueError
964
    
965
    def _put_policy(self, node, policy, replace):
966
        if replace:
967
            for k, v in self.default_policy.iteritems():
968
                if k not in policy:
969
                    policy[k] = v
970
        self.node.policy_set(node, policy)
971
    
972
    def _get_policy(self, node):
973
        policy = self.default_policy.copy()
974
        policy.update(self.node.policy_get(node))
975
        return policy
976
    
977
    def _apply_versioning(self, account, container, version_id):
978
        """Delete the provided version if such is the policy.
979
           Return size of object removed.
980
        """
981
        
982
        if version_id is None:
983
            return 0
984
        path, node = self._lookup_container(account, container)
985
        versioning = self._get_policy(node)['versioning']
986
        if versioning != 'auto':
987
            hash, size = self.node.version_remove(version_id)
988
            self.store.map_delete(hash)
989
            return size
990
        return 0
991
    
992
    # Access control functions.
993
    
994
    def _check_groups(self, groups):
995
        # raise ValueError('Bad characters in groups')
996
        pass
997
    
998
    def _check_permissions(self, path, permissions):
999
        # raise ValueError('Bad characters in permissions')
1000
        pass
1001
    
1002
    def _get_formatted_paths(self, paths):
1003
        formatted = []
1004
        for p in paths:
1005
            node = self.node.node_lookup(p)
1006
            if node is not None:
1007
                props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
1008
            if props is not None:
1009
                if props[self.TYPE] in ('application/directory', 'application/folder'):
1010
                    formatted.append((p.rstrip('/') + '/', self.MATCH_PREFIX))
1011
                formatted.append((p, self.MATCH_EXACT))
1012
        return formatted
1013
    
1014
    def _get_permissions_path(self, account, container, name):
1015
        path = '/'.join((account, container, name))
1016
        permission_paths = self.permissions.access_inherit(path)
1017
        permission_paths.sort()
1018
        permission_paths.reverse()
1019
        for p in permission_paths:
1020
            if p == path:
1021
                return p
1022
            else:
1023
                if p.count('/') < 2:
1024
                    continue
1025
                node = self.node.node_lookup(p)
1026
                if node is not None:
1027
                    props = self.node.version_lookup(node, inf, CLUSTER_NORMAL)
1028
                if props is not None:
1029
                    if props[self.TYPE] in ('application/directory', 'application/folder'):
1030
                        return p
1031
        return None
1032
    
1033
    def _can_read(self, user, account, container, name):
1034
        if user == account:
1035
            return True
1036
        path = '/'.join((account, container, name))
1037
        if self.permissions.public_get(path) is not None:
1038
            return True
1039
        path = self._get_permissions_path(account, container, name)
1040
        if not path:
1041
            raise NotAllowedError
1042
        if not self.permissions.access_check(path, self.READ, user) and not self.permissions.access_check(path, self.WRITE, user):
1043
            raise NotAllowedError
1044
    
1045
    def _can_write(self, user, account, container, name):
1046
        if user == account:
1047
            return True
1048
        path = '/'.join((account, container, name))
1049
        path = self._get_permissions_path(account, container, name)
1050
        if not path:
1051
            raise NotAllowedError
1052
        if not self.permissions.access_check(path, self.WRITE, user):
1053
            raise NotAllowedError
1054
    
1055
    def _allowed_accounts(self, user):
1056
        allow = set()
1057
        for path in self.permissions.access_list_paths(user):
1058
            allow.add(path.split('/', 1)[0])
1059
        return sorted(allow)
1060
    
1061
    def _allowed_containers(self, user, account):
1062
        allow = set()
1063
        for path in self.permissions.access_list_paths(user, account):
1064
            allow.add(path.split('/', 2)[1])
1065
        return sorted(allow)