Change conflict (409) replies format to text.
[pithos] / pithos / backends / simple.py
index 44a944b..6d6efa2 100644 (file)
@@ -35,16 +35,34 @@ import os
 import time
 import sqlite3
 import logging
-import types
 import hashlib
-import shutil
-import pickle
+import binascii
 
 from base import NotAllowedError, BaseBackend
-
+from lib.hashfiler import Mapper, Blocker
+from django.utils.encoding import smart_unicode, smart_str
 
 logger = logging.getLogger(__name__)
 
+def backend_method(func=None, autocommit=1):
+    if func is None:
+        def fn(func):
+            return backend_method(func, autocommit)
+        return fn
+
+    if not autocommit:
+        return func
+    def fn(self, *args, **kw):
+        self.con.execute('begin deferred')
+        try:
+            ret = func(self, *args, **kw)
+            self.con.commit()
+            return ret
+        except:
+            self.con.rollback()
+            raise
+    return fn
+
 
 class SimpleBackend(BaseBackend):
     """A simple backend.
@@ -52,19 +70,21 @@ class SimpleBackend(BaseBackend):
     Uses SQLite for storage.
     """
     
-    # TODO: Automatic/manual clean-up after a time interval.
+    # TODO: Create account if not present in all functions.
     
-    def __init__(self, db):
-        self.hash_algorithm = 'sha1'
-        self.block_size = 128 * 1024 # 128KB
+    def __init__(self, db, db_options):
+        self.hash_algorithm = 'sha256'
+        self.block_size = 4 * 1024 * 1024 # 4MB
         
         self.default_policy = {'quota': 0, 'versioning': 'auto'}
         
         basepath = os.path.split(db)[0]
         if basepath and not os.path.exists(basepath):
             os.makedirs(basepath)
+        if not os.path.isdir(basepath):
+            raise RuntimeError("Cannot open database at '%s'" % (db,))
         
-        self.con = sqlite3.connect(db, check_same_thread=False)
+        self.con = sqlite3.connect(basepath + '/db', check_same_thread=False)
         
         sql = '''pragma foreign_keys = on'''
         self.con.execute(sql)
@@ -85,44 +105,56 @@ class SimpleBackend(BaseBackend):
                     foreign key (version_id) references versions(version_id)
                     on delete cascade)'''
         self.con.execute(sql)
-        sql = '''create table if not exists hashmaps (
-                    version_id integer,
-                    pos integer,
-                    block_id text,
-                    primary key (version_id, pos)
-                    foreign key (version_id) references versions(version_id)
-                    on delete cascade)'''
-        self.con.execute(sql)
-        sql = '''create table if not exists blocks (
-                    block_id text, data blob, primary key (block_id))'''
-        self.con.execute(sql)
-        
         sql = '''create table if not exists policy (
                     name text, key text, value text, primary key (name, key))'''
         self.con.execute(sql)
         
+        # Access control tables.
         sql = '''create table if not exists groups (
-                    account text, name text, users text, primary key (account, name))'''
+                    account text, gname text, user text)'''
         self.con.execute(sql)
         sql = '''create table if not exists permissions (
-                    name text, read text, write text, primary key (name))'''
+                    name text, op text, user text)'''
         self.con.execute(sql)
         sql = '''create table if not exists public (
                     name text, primary key (name))'''
         self.con.execute(sql)
+        
         self.con.commit()
+        
+        params = {'blocksize': self.block_size,
+                  'blockpath': basepath + '/blocks',
+                  'hashtype': self.hash_algorithm}
+        self.blocker = Blocker(**params)
+        
+        params = {'mappath': basepath + '/maps',
+                  'namelen': self.blocker.hashlen}
+        self.mapper = Mapper(**params)
+    
+    @backend_method
+    def list_accounts(self, user, marker=None, limit=10000):
+        """Return a list of accounts the user can access."""
+        
+        allowed = self._allowed_accounts(user)
+        start, limit = self._list_limits(allowed, marker, limit)
+        return allowed[start:start + limit]
     
+    @backend_method
     def get_account_meta(self, user, account, until=None):
         """Return a dictionary with the account metadata."""
         
         logger.debug("get_account_meta: %s %s", account, until)
         if user != account:
-            raise NotAllowedError
+            if until or account not in self._allowed_accounts(user):
+                raise NotAllowedError
+        else:
+            self._create_account(user, account)
         try:
             version_id, mtime = self._get_accountinfo(account, until)
         except NameError:
+            # Account does not exist before until.
             version_id = None
-            mtime = 0
+            mtime = until
         count, bytes, tstamp = self._get_pathstats(account, until)
         if mtime > tstamp:
             tstamp = mtime
@@ -140,52 +172,49 @@ class SimpleBackend(BaseBackend):
         row = c.fetchone()
         count = row[0]
         
-        meta = self._get_metadata(account, version_id)
-        meta.update({'name': account, 'count': count, 'bytes': bytes})
-        if modified:
-            meta.update({'modified': modified})
-        if until is not None:
-            meta.update({'until_timestamp': tstamp})
+        if user != account:
+            meta = {'name': account}
+        else:
+            meta = self._get_metadata(account, version_id)
+            meta.update({'name': account, 'count': count, 'bytes': bytes})
+            if until is not None:
+                meta.update({'until_timestamp': tstamp})
+        meta.update({'modified': modified})
         return meta
     
+    @backend_method
     def update_account_meta(self, user, account, meta, replace=False):
         """Update the metadata associated with the account."""
         
         logger.debug("update_account_meta: %s %s %s", account, meta, replace)
         if user != account:
             raise NotAllowedError
-        self._put_metadata(user, account, meta, replace)
+        self._put_metadata(user, account, meta, replace, False)
     
+    @backend_method
     def get_account_groups(self, user, account):
         """Return a dictionary with the user groups defined for this account."""
         
         logger.debug("get_account_groups: %s", account)
         if user != account:
-            raise NotAllowedError
+            if account not in self._allowed_accounts(user):
+                raise NotAllowedError
+            return {}
+        self._create_account(user, account)
         return self._get_groups(account)
     
+    @backend_method
     def update_account_groups(self, user, account, groups, replace=False):
         """Update the groups associated with the account."""
         
         logger.debug("update_account_groups: %s %s %s", account, groups, replace)
         if user != account:
             raise NotAllowedError
-        for k, v in groups.iteritems():
-            if True in [False or ',' in x for x in v]:
-                raise ValueError('Bad characters in groups')
-        if replace:
-            sql = 'delete from groups where account = ?'
-            self.con.execute(sql, (account,))
-        for k, v in groups.iteritems():
-            if len(v) == 0:
-                if not replace:
-                    sql = 'delete from groups where account = ? and name = ?'
-                    self.con.execute(sql, (account, k))
-            else:
-                sql = 'insert or replace into groups (account, name, users) values (?, ?, ?)'
-                self.con.execute(sql, (account, k, ','.join(v)))
-        self.con.commit()
+        self._create_account(user, account)
+        self._check_groups(groups)
+        self._put_groups(account, groups, replace)
     
+    @backend_method
     def put_account(self, user, account):
         """Create a new account with the given name."""
         
@@ -198,9 +227,9 @@ class SimpleBackend(BaseBackend):
             pass
         else:
             raise NameError('Account already exists')
-        version_id = self._put_version(account, user)
-        self.con.commit()
+        self._put_version(account, user)
     
+    @backend_method
     def delete_account(self, user, account):
         """Delete the account with the given name."""
         
@@ -212,24 +241,34 @@ class SimpleBackend(BaseBackend):
             raise IndexError('Account is not empty')
         sql = 'delete from versions where name = ?'
         self.con.execute(sql, (account,))
-        sql = 'delete from groups where name = ?'
-        self.con.execute(sql, (account,))
-        self.con.commit()
+        self._del_groups(account)
     
-    def list_containers(self, user, account, marker=None, limit=10000, until=None):
+    @backend_method
+    def list_containers(self, user, account, marker=None, limit=10000, shared=False, until=None):
         """Return a list of containers existing under an account."""
         
         logger.debug("list_containers: %s %s %s %s", account, marker, limit, until)
         if user != account:
-            raise NotAllowedError
-        return self._list_objects(account, '', '/', marker, limit, False, [], until)
+            if until or account not in self._allowed_accounts(user):
+                raise NotAllowedError
+            allowed = self._allowed_containers(user, account)
+            start, limit = self._list_limits(allowed, marker, limit)
+            return allowed[start:start + limit]
+        else:
+            if shared:
+                allowed = [x.split('/', 2)[1] for x in self._shared_paths(account)]
+                start, limit = self._list_limits(allowed, marker, limit)
+                return allowed[start:start + limit]
+        return [x[0] for x in self._list_objects(account, '', '/', marker, limit, False, [], until)]
     
+    @backend_method
     def get_container_meta(self, user, account, container, until=None):
         """Return a dictionary with the container metadata."""
         
         logger.debug("get_container_meta: %s %s %s", account, container, until)
         if user != account:
-            raise NotAllowedError
+            if until or container not in self._allowed_containers(user, account):
+                raise NotAllowedError
         path, version_id, mtime = self._get_containerinfo(account, container, until)
         count, bytes, tstamp = self._get_pathstats(path, until)
         if mtime > tstamp:
@@ -241,12 +280,16 @@ class SimpleBackend(BaseBackend):
             if mtime > modified:
                 modified = mtime
         
-        meta = self._get_metadata(path, version_id)
-        meta.update({'name': container, 'count': count, 'bytes': bytes, 'modified': modified})
-        if until is not None:
-            meta.update({'until_timestamp': tstamp})
+        if user != account:
+            meta = {'name': container, 'modified': modified}
+        else:
+            meta = self._get_metadata(path, version_id)
+            meta.update({'name': container, 'count': count, 'bytes': bytes, 'modified': modified})
+            if until is not None:
+                meta.update({'until_timestamp': tstamp})
         return meta
     
+    @backend_method
     def update_container_meta(self, user, account, container, meta, replace=False):
         """Update the metadata associated with the container."""
         
@@ -254,17 +297,21 @@ class SimpleBackend(BaseBackend):
         if user != account:
             raise NotAllowedError
         path, version_id, mtime = self._get_containerinfo(account, container)
-        self._put_metadata(user, path, meta, replace)
+        self._put_metadata(user, path, meta, replace, False)
     
+    @backend_method
     def get_container_policy(self, user, account, container):
         """Return a dictionary with the container policy."""
         
         logger.debug("get_container_policy: %s %s", account, container)
         if user != account:
-            raise NotAllowedError
+            if container not in self._allowed_containers(user, account):
+                raise NotAllowedError
+            return {}
         path = self._get_containerinfo(account, container)[0]
         return self._get_policy(path)
     
+    @backend_method
     def update_container_policy(self, user, account, container, policy, replace=False):
         """Update the policy associated with the account."""
         
@@ -280,8 +327,8 @@ class SimpleBackend(BaseBackend):
         for k, v in policy.iteritems():
             sql = 'insert or replace into policy (name, key, value) values (?, ?, ?)'
             self.con.execute(sql, (path, k, v))
-        self.con.commit()
     
+    @backend_method
     def put_container(self, user, account, container, policy=None):
         """Create a new container with the given name."""
         
@@ -296,16 +343,16 @@ class SimpleBackend(BaseBackend):
             raise NameError('Container already exists')
         if policy:
             self._check_policy(policy)
-        path = os.path.join(account, container)
-        version_id = self._put_version(path, user)
+        path = '/'.join((account, container))
+        version_id = self._put_version(path, user)[0]
         for k, v in self.default_policy.iteritems():
             if k not in policy:
                 policy[k] = v
         for k, v in policy.iteritems():
             sql = 'insert or replace into policy (name, key, value) values (?, ?, ?)'
             self.con.execute(sql, (path, k, v))
-        self.con.commit()
     
+    @backend_method
     def delete_container(self, user, account, container, until=None):
         """Delete/purge the container with the given name."""
         
@@ -315,11 +362,12 @@ class SimpleBackend(BaseBackend):
         path, version_id, mtime = self._get_containerinfo(account, container)
         
         if until is not None:
-            sql = '''select version_id from versions where name like ? and tstamp <= ?'''
+            sql = '''select version_id from versions where name like ? and tstamp <= ?
+                        and version_id not in (select version_id from (%s))'''
+            sql = sql % self._sql_until() # Do not delete current versions.
             c = self.con.execute(sql, (path + '/%', until))
             for v in [x[0] for x in c.fetchall()]:
                 self._del_version(v)
-            self.con.commit()
             return
         
         count = self._get_pathstats(path)[0]
@@ -329,30 +377,51 @@ class SimpleBackend(BaseBackend):
         self.con.execute(sql, (path, path + '/%',))
         sql = 'delete from policy where name = ?'
         self.con.execute(sql, (path,))
-        self._copy_version(user, account, account, True, True) # New account version (for timestamp update).
+        self._copy_version(user, account, account, True, False) # New account version (for timestamp update).
     
-    def list_objects(self, user, account, container, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, keys=[], until=None):
+    @backend_method
+    def list_objects(self, user, account, container, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, keys=[], shared=False, until=None):
         """Return a list of objects existing under a container."""
         
-        logger.debug("list_objects: %s %s %s %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit, virtual, keys, until)
+        logger.debug("list_objects: %s %s %s %s %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit, virtual, keys, shared, until)
+        allowed = []
         if user != account:
-            raise NotAllowedError
+            if until:
+                raise NotAllowedError
+            allowed = self._allowed_paths(user, '/'.join((account, container)))
+            if not allowed:
+                raise NotAllowedError
+        else:
+            if shared:
+                allowed = self._shared_paths('/'.join((account, container)))
         path, version_id, mtime = self._get_containerinfo(account, container, until)
-        return self._list_objects(path, prefix, delimiter, marker, limit, virtual, keys, until)
+        return self._list_objects(path, prefix, delimiter, marker, limit, virtual, keys, until, allowed)
     
+    @backend_method
     def list_object_meta(self, user, account, container, until=None):
         """Return a list with all the container's object meta keys."""
         
         logger.debug("list_object_meta: %s %s %s", account, container, until)
+        allowed = []
         if user != account:
-            raise NotAllowedError
+            if until:
+                raise NotAllowedError
+            allowed = self._allowed_paths(user, '/'.join((account, container)))
+            if not allowed:
+                raise NotAllowedError
         path, version_id, mtime = self._get_containerinfo(account, container, until)
         sql = '''select distinct m.key from (%s) o, metadata m
                     where m.version_id = o.version_id and o.name like ?'''
         sql = sql % self._sql_until(until)
-        c = self.con.execute(sql, (path + '/%',))
+        param = (path + '/%',)
+        if allowed:
+            for x in allowed:
+                sql += ' and o.name like ?'
+                param += (x,)
+        c = self.con.execute(sql, param)
         return [x[0] for x in c.fetchall()]
     
+    @backend_method
     def get_object_meta(self, user, account, container, name, version=None):
         """Return a dictionary with the object metadata."""
         
@@ -370,14 +439,16 @@ class SimpleBackend(BaseBackend):
         meta.update({'modified': modified, 'modified_by': muser})
         return meta
     
+    @backend_method
     def update_object_meta(self, user, account, container, name, meta, replace=False):
         """Update the metadata associated with the object."""
         
         logger.debug("update_object_meta: %s %s %s %s %s", account, container, name, meta, replace)
         self._can_write(user, account, container, name)
         path, version_id, muser, mtime, size = self._get_objectinfo(account, container, name)
-        self._put_metadata(user, path, meta, replace)
+        return self._put_metadata(user, path, meta, replace)
     
+    @backend_method
     def get_object_permissions(self, user, account, container, name):
         """Return the path from which this object gets its permissions from,\
         along with a dictionary containing the permissions."""
@@ -387,6 +458,7 @@ class SimpleBackend(BaseBackend):
         path = self._get_objectinfo(account, container, name)[0]
         return self._get_permissions(path)
     
+    @backend_method
     def update_object_permissions(self, user, account, container, name, permissions):
         """Update the permissions associated with the object."""
         
@@ -397,6 +469,7 @@ class SimpleBackend(BaseBackend):
         r, w = self._check_permissions(path, permissions)
         self._put_permissions(path, r, w)
     
+    @backend_method
     def get_object_public(self, user, account, container, name):
         """Return the public URL of the object if applicable."""
         
@@ -407,6 +480,7 @@ class SimpleBackend(BaseBackend):
             return '/public/' + path
         return None
     
+    @backend_method
     def update_object_public(self, user, account, container, name, public):
         """Update the public status of the object."""
         
@@ -415,17 +489,17 @@ class SimpleBackend(BaseBackend):
         path = self._get_objectinfo(account, container, name)[0]
         self._put_public(path, public)
     
+    @backend_method
     def get_object_hashmap(self, user, account, container, name, version=None):
         """Return the object's size and a list with partial hashes."""
         
         logger.debug("get_object_hashmap: %s %s %s %s", account, container, name, version)
         self._can_read(user, account, container, name)
         path, version_id, muser, mtime, size = self._get_objectinfo(account, container, name, version)
-        sql = 'select block_id from hashmaps where version_id = ? order by pos asc'
-        c = self.con.execute(sql, (version_id,))
-        hashmap = [x[0] for x in c.fetchall()]
-        return size, hashmap
+        hashmap = self.mapper.map_retr(version_id)
+        return size, [binascii.hexlify(x) for x in hashmap]
     
+    @backend_method
     def update_object_hashmap(self, user, account, container, name, size, hashmap, meta={}, replace_meta=False, permissions=None):
         """Create/update an object with the specified size and partial hashes."""
         
@@ -433,35 +507,27 @@ class SimpleBackend(BaseBackend):
         if permissions is not None and user != account:
             raise NotAllowedError
         self._can_write(user, account, container, name)
-        missing = []
-        for i in range(len(hashmap)):
-            sql = 'select count(*) from blocks where block_id = ?'
-            c = self.con.execute(sql, (hashmap[i],))
-            if c.fetchone()[0] == 0:
-                missing.append(hashmap[i])
+        missing = self.blocker.block_ping([binascii.unhexlify(x) for x in hashmap])
         if missing:
             ie = IndexError()
             ie.data = missing
             raise ie
         path = self._get_containerinfo(account, container)[0]
-        path = os.path.join(path, name)
+        path = '/'.join((path, name))
         if permissions is not None:
             r, w = self._check_permissions(path, permissions)
         src_version_id, dest_version_id = self._copy_version(user, path, path, not replace_meta, False)
         sql = 'update versions set size = ? where version_id = ?'
         self.con.execute(sql, (size, dest_version_id))
-        # TODO: Check for block_id existence.
-        for i in range(len(hashmap)):
-            sql = 'insert or replace into hashmaps (version_id, pos, block_id) values (?, ?, ?)'
-            self.con.execute(sql, (dest_version_id, i, hashmap[i]))
+        self.mapper.map_stor(dest_version_id, [binascii.unhexlify(x) for x in hashmap])
         for k, v in meta.iteritems():
             sql = 'insert or replace into metadata (version_id, key, value) values (?, ?, ?)'
             self.con.execute(sql, (dest_version_id, k, v))
         if permissions is not None:
-            sql = 'insert or replace into permissions (name, read, write) values (?, ?, ?)'
-            self.con.execute(sql, (path, r, w))
-        self.con.commit()
+            self._put_permissions(path, r, w)
+        return dest_version_id
     
+    @backend_method
     def copy_object(self, user, account, src_container, src_name, dest_container, dest_name, dest_meta={}, replace_meta=False, permissions=None, src_version=None):
         """Copy an object's data and metadata."""
         
@@ -474,9 +540,9 @@ class SimpleBackend(BaseBackend):
         if src_version is None:
             src_path = self._get_objectinfo(account, src_container, src_name)[0]
         else:
-            src_path = os.path.join(account, src_container, src_name)
+            src_path = '/'.join((account, src_container, src_name))
         dest_path = self._get_containerinfo(account, dest_container)[0]
-        dest_path = os.path.join(dest_path, dest_name)
+        dest_path = '/'.join((dest_path, dest_name))
         if permissions is not None:
             r, w = self._check_permissions(dest_path, permissions)
         src_version_id, dest_version_id = self._copy_version(user, src_path, dest_path, not replace_meta, True, src_version)
@@ -484,17 +550,19 @@ class SimpleBackend(BaseBackend):
             sql = 'insert or replace into metadata (version_id, key, value) values (?, ?, ?)'
             self.con.execute(sql, (dest_version_id, k, v))
         if permissions is not None:
-            sql = 'insert or replace into permissions (name, read, write) values (?, ?, ?)'
-            self.con.execute(sql, (dest_path, r, w))
-        self.con.commit()
+            self._put_permissions(dest_path, r, w)
+        return dest_version_id
     
+    @backend_method
     def move_object(self, user, account, src_container, src_name, dest_container, dest_name, dest_meta={}, replace_meta=False, permissions=None):
         """Move an object's data and metadata."""
         
         logger.debug("move_object: %s %s %s %s %s %s %s %s", account, src_container, src_name, dest_container, dest_name, dest_meta, replace_meta, permissions)
-        self.copy_object(user, account, src_container, src_name, dest_container, dest_name, dest_meta, replace_meta, permissions, None)
+        dest_version_id = self.copy_object(user, account, src_container, src_name, dest_container, dest_name, dest_meta, replace_meta, permissions, None)
         self.delete_object(user, account, src_container, src_name)
+        return dest_version_id
     
+    @backend_method
     def delete_object(self, user, account, container, name, until=None):
         """Delete/purge an object."""
         
@@ -503,7 +571,7 @@ class SimpleBackend(BaseBackend):
             raise NotAllowedError
         
         if until is not None:
-            path = os.path.join(account, container, name)
+            path = '/'.join((account, container, name))
             sql = '''select version_id from versions where name = ? and tstamp <= ?'''
             c = self.con.execute(sql, (path, until))
             for v in [x[0] in c.fetchall()]:
@@ -514,59 +582,51 @@ class SimpleBackend(BaseBackend):
                 pass
             else:
                 self._del_sharing(path)
-            self.con.commit()
             return
         
         path = self._get_objectinfo(account, container, name)[0]
         self._put_version(path, user, 0, 1)
         self._del_sharing(path)
     
+    @backend_method
     def list_versions(self, user, account, container, name):
         """Return a list of all (version, version_timestamp) tuples for an object."""
         
         logger.debug("list_versions: %s %s %s", account, container, name)
         self._can_read(user, account, container, name)
         # This will even show deleted versions.
-        path = os.path.join(account, container, name)
+        path = '/'.join((account, container, name))
         sql = '''select distinct version_id, tstamp from versions where name = ? and hide = 0'''
         c = self.con.execute(sql, (path,))
         return [(int(x[0]), int(x[1])) for x in c.fetchall()]
     
+    @backend_method(autocommit=0)
     def get_block(self, hash):
         """Return a block's data."""
         
         logger.debug("get_block: %s", hash)
-        c = self.con.execute('select data from blocks where block_id = ?', (hash,))
-        row = c.fetchone()
-        if row:
-            return str(row[0])
-        else:
+        blocks = self.blocker.block_retr((binascii.unhexlify(hash),))
+        if not blocks:
             raise NameError('Block does not exist')
+        return blocks[0]
     
+    @backend_method(autocommit=0)
     def put_block(self, data):
-        """Create a block and return the hash."""
+        """Store a block and return the hash."""
         
         logger.debug("put_block: %s", len(data))
-        h = hashlib.new(self.hash_algorithm)
-        h.update(data.rstrip('\x00'))
-        hash = h.hexdigest()
-        sql = 'insert or ignore into blocks (block_id, data) values (?, ?)'
-        self.con.execute(sql, (hash, buffer(data)))
-        self.con.commit()
-        return hash
+        hashes, absent = self.blocker.block_stor((data,))
+        return binascii.hexlify(hashes[0])
     
+    @backend_method(autocommit=0)
     def update_block(self, hash, data, offset=0):
         """Update a known block and return the hash."""
         
         logger.debug("update_block: %s %s %s", hash, len(data), offset)
         if offset == 0 and len(data) == self.block_size:
             return self.put_block(data)
-        src_data = self.get_block(hash)
-        bs = self.block_size
-        if offset < 0 or offset > bs or offset + len(data) > bs:
-            raise IndexError('Offset or data outside block limits')
-        dest_data = src_data[:offset] + data + src_data[offset + len(data):]
-        return self.put_block(dest_data)
+        h, e = self.blocker.block_delta(binascii.unhexlify(hash), ((offset, data),))
+        return binascii.hexlify(h)
     
     def _sql_until(self, until=None):
         """Return the sql to get the latest versions until the timestamp given."""
@@ -604,14 +664,13 @@ class SimpleBackend(BaseBackend):
             row = c.fetchone()
             if not row:
                 raise IndexError('Version does not exist')
-        return str(row[0]), str(row[1]), int(row[2]), int(row[3])
+        return smart_str(row[0]), smart_str(row[1]), int(row[2]), int(row[3])
     
     def _put_version(self, path, user, size=0, hide=0):
         tstamp = int(time.time())
         sql = 'insert into versions (name, user, tstamp, size, hide) values (?, ?, ?, ?, ?)'
         id = self.con.execute(sql, (path, user, tstamp, size, hide)).lastrowid
-        self.con.commit()
-        return str(id)
+        return str(id), tstamp
     
     def _copy_version(self, user, src_path, dest_path, copy_meta=True, copy_data=True, src_version=None):
         if src_version is not None:
@@ -625,16 +684,15 @@ class SimpleBackend(BaseBackend):
                 size = 0
         if not copy_data:
             size = 0
-        dest_version_id = self._put_version(dest_path, user, size)
+        dest_version_id = self._put_version(dest_path, user, size)[0]
         if copy_meta and src_version_id is not None:
             sql = 'insert into metadata select %s, key, value from metadata where version_id = ?'
             sql = sql % dest_version_id
             self.con.execute(sql, (src_version_id,))
         if copy_data and src_version_id is not None:
-            sql = 'insert into hashmaps select %s, pos, block_id from hashmaps where version_id = ?'
-            sql = sql % dest_version_id
-            self.con.execute(sql, (src_version_id,))
-        self.con.commit()
+            # TODO: Copy properly.
+            hashmap = self.mapper.map_retr(src_version_id)
+            self.mapper.map_stor(dest_version_id, hashmap)
         return src_version_id, dest_version_id
     
     def _get_versioninfo(self, account, container, name, until=None):
@@ -645,7 +703,7 @@ class SimpleBackend(BaseBackend):
             p = p[:p.index(None)]
         except ValueError:
             pass
-        path = os.path.join(*p)
+        path = '/'.join(p)
         sql = '''select version_id, tstamp, size from (%s) where name = ?'''
         sql = sql % self._sql_until(until)
         c = self.con.execute(sql, (path,))
@@ -669,19 +727,25 @@ class SimpleBackend(BaseBackend):
             raise NameError('Container does not exist')
     
     def _get_objectinfo(self, account, container, name, version=None):
-        path = os.path.join(account, container, name)
+        path = '/'.join((account, container, name))
         version_id, muser, mtime, size = self._get_version(path, version)
         return path, version_id, muser, mtime, size
     
+    def _create_account(self, user, account):
+        try:
+            self._get_accountinfo(account)
+        except NameError:
+            self._put_version(account, user)
+    
     def _get_metadata(self, path, version):
         sql = 'select key, value from metadata where version_id = ?'
         c = self.con.execute(sql, (version,))
         return dict(c.fetchall())
     
-    def _put_metadata(self, user, path, meta, replace=False):
+    def _put_metadata(self, user, path, meta, replace=False, copy_data=True):
         """Create a new version and store metadata."""
         
-        src_version_id, dest_version_id = self._copy_version(user, path, path, not replace, True)
+        src_version_id, dest_version_id = self._copy_version(user, path, path, not replace, copy_data)
         for k, v in meta.iteritems():
             if not replace and v == '':
                 sql = 'delete from metadata where version_id = ? and key = ?'
@@ -689,12 +753,7 @@ class SimpleBackend(BaseBackend):
             else:
                 sql = 'insert or replace into metadata (version_id, key, value) values (?, ?, ?)'
                 self.con.execute(sql, (dest_version_id, k, v))
-        self.con.commit()
-    
-    def _get_groups(self, account):
-        sql = 'select name, users from groups where account = ?'
-        c = self.con.execute(sql, (account,))
-        return dict([(x[0], x[1].split(',')) for x in c.fetchall()])
+        return dest_version_id
     
     def _check_policy(self, policy):
         for k in policy.keys():
@@ -716,86 +775,146 @@ class SimpleBackend(BaseBackend):
         c = self.con.execute(sql, (path,))
         return dict(c.fetchall())
     
-    def _is_allowed(self, user, account, container, name, op='read'):
-        if user == account:
-            return True
-        path = os.path.join(account, container, name)
-        if op == 'read' and self._get_public(path):
-            return True
-        perm_path, perms = self._get_permissions(path)
-        
-        # Expand groups.
-        for x in ('read', 'write'):
-            g_perms = []
-            for y in perms.get(x, []):
-                groups = self._get_groups(account)
-                if y in groups: #it's a group
-                    for g_name in groups[y]:
-                        g_perms.append(g_name)
-                else: #it's a user
-                    g_perms.append(y)
-            perms[x] = g_perms
+    def _list_limits(self, listing, marker, limit):
+        start = 0
+        if marker:
+            try:
+                start = listing.index(marker) + 1
+            except ValueError:
+                pass
+        if not limit or limit > 10000:
+            limit = 10000
+        return start, limit
+    
+    def _list_objects(self, path, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, keys=[], until=None, allowed=[]):
+        cont_prefix = path + '/'
+        if keys and len(keys) > 0:
+            sql = '''select distinct o.name, o.version_id from (%s) o, metadata m where o.name like ? and
+                        m.version_id = o.version_id and m.key in (%s)'''
+            sql = sql % (self._sql_until(until), ', '.join('?' * len(keys)))
+            param = (cont_prefix + prefix + '%',) + tuple(keys)
+            if allowed:
+                sql += ' and (' + ' or '.join(('o.name like ?',) * len(allowed)) + ')'
+                param += tuple([x + '%' for x in allowed])
+            sql += ' order by o.name'
+        else:
+            sql = 'select name, version_id from (%s) where name like ?'
+            sql = sql % self._sql_until(until)
+            param = (cont_prefix + prefix + '%',)
+            if allowed:
+                sql += ' and (' + ' or '.join(('name like ?',) * len(allowed)) + ')'
+                param += tuple([x + '%' for x in allowed])
+            sql += ' order by name'
+        c = self.con.execute(sql, param)
+        objects = [(x[0][len(cont_prefix):], x[1]) for x in c.fetchall()]
+        if delimiter:
+            pseudo_objects = []
+            for x in objects:
+                pseudo_name = x[0]
+                i = pseudo_name.find(delimiter, len(prefix))
+                if not virtual:
+                    # If the delimiter is not found, or the name ends
+                    # with the delimiter's first occurence.
+                    if i == -1 or len(pseudo_name) == i + len(delimiter):
+                        pseudo_objects.append(x)
+                else:
+                    # If the delimiter is found, keep up to (and including) the delimiter.
+                    if i != -1:
+                        pseudo_name = pseudo_name[:i + len(delimiter)]
+                    if pseudo_name not in [y[0] for y in pseudo_objects]:
+                        if pseudo_name == x[0]:
+                            pseudo_objects.append(x)
+                        else:
+                            pseudo_objects.append((pseudo_name, None))
+            objects = pseudo_objects
         
-        if op == 'read' and user in perms.get('read', []):
-            return True
-        if user in perms.get('write', []):
-            return True
-        return False
+        start, limit = self._list_limits([x[0] for x in objects], marker, limit)
+        return objects[start:start + limit]
     
-    def _can_read(self, user, account, container, name):
-        if not self._is_allowed(user, account, container, name, 'read'):
-            raise NotAllowedError
+    def _del_version(self, version):
+        self.mapper.map_remv(version)
+        sql = 'delete from versions where version_id = ?'
+        self.con.execute(sql, (version,))
     
-    def _can_write(self, user, account, container, name):
-        if not self._is_allowed(user, account, container, name, 'write'):
-            raise NotAllowedError
+    # Access control functions.
+    
+    def _check_groups(self, groups):
+        # Example follows.
+        # for k, v in groups.iteritems():
+        #     if True in [False or ',' in x for x in v]:
+        #         raise ValueError('Bad characters in groups')
+        pass
+    
+    def _get_groups(self, account):
+        sql = 'select gname, user from groups where account = ?'
+        c = self.con.execute(sql, (account,))
+        groups = {}
+        for gname, user in c.fetchall():
+            if gname not in groups:
+                groups[gname] = []
+            groups[gname].append(user)
+        return groups
+    
+    def _put_groups(self, account, groups, replace=False):
+        if replace:
+            self._del_groups(account)
+        for k, v in groups.iteritems():
+            sql = 'delete from groups where account = ? and gname = ?'
+            self.con.execute(sql, (account, k))
+            if v:
+                sql = 'insert into groups (account, gname, user) values (?, ?, ?)'
+                self.con.executemany(sql, [(account, k, x) for x in v])
+    
+    def _del_groups(self, account):
+        sql = 'delete from groups where account = ?'
+        self.con.execute(sql, (account,))
     
     def _check_permissions(self, path, permissions):
         # Check for existing permissions.
         sql = '''select name from permissions
                     where name != ? and (name like ? or ? like name || ?)'''
         c = self.con.execute(sql, (path, path + '%', path, '%'))
-        row = c.fetchone()
-        if row:
+        rows = c.fetchall()
+        if rows:
             ae = AttributeError()
-            ae.data = row[0]
+            ae.data = rows
             raise ae
         
         # Format given permissions.
         if len(permissions) == 0:
-            return '', ''
+            return [], []
         r = permissions.get('read', [])
         w = permissions.get('write', [])
-        if True in [False or ',' in x for x in r]:
-            raise ValueError('Bad characters in read permissions')
-        if True in [False or ',' in x for x in w]:
-            raise ValueError('Bad characters in write permissions')
-        return ','.join(r), ','.join(w)
+        # Examples follow.
+        # if True in [False or ',' in x for x in r]:
+        #     raise ValueError('Bad characters in read permissions')
+        # if True in [False or ',' in x for x in w]:
+        #     raise ValueError('Bad characters in write permissions')
+        return r, w
     
     def _get_permissions(self, path):
         # Check for permissions at path or above.
-        sql = 'select name, read, write from permissions where ? like name || ?'
+        sql = 'select name, op, user from permissions where ? like name || ?'
         c = self.con.execute(sql, (path, '%'))
-        row = c.fetchone()
-        if not row:
-            return path, {}
-        
-        name, r, w = row
-        ret = {}
-        if w != '':
-            ret['write'] = w.split(',')
-        if r != '':
-            ret['read'] = r.split(',')
-        return name, ret
+        name = path
+        perms = {} # Return nothing, if nothing is set.
+        for row in c.fetchall():
+            name = row[0]
+            op = row[1]
+            user = row[2]
+            if op not in perms:
+                perms[op] = []
+            perms[op].append(user)
+        return name, perms
     
     def _put_permissions(self, path, r, w):
-        if r == '' and w == '':
-            sql = 'delete from permissions where name = ?'
-            self.con.execute(sql, (path,))
-        else:
-            sql = 'insert or replace into permissions (name, read, write) values (?, ?, ?)'
-            self.con.execute(sql, (path, r, w))
-        self.con.commit()
+        sql = 'delete from permissions where name = ?'
+        self.con.execute(sql, (path,))
+        sql = 'insert into permissions (name, op, user) values (?, ?, ?)'
+        if r:
+            self.con.executemany(sql, [(path, 'read', x) for x in r])
+        if w:
+            self.con.executemany(sql, [(path, 'write', x) for x in w])
     
     def _get_public(self, path):
         sql = 'select name from public where name = ?'
@@ -811,61 +930,72 @@ class SimpleBackend(BaseBackend):
         else:
             sql = 'insert or replace into public (name) values (?)'
         self.con.execute(sql, (path,))
-        self.con.commit()
     
     def _del_sharing(self, path):
         sql = 'delete from permissions where name = ?'
         self.con.execute(sql, (path,))
         sql = 'delete from public where name = ?'
         self.con.execute(sql, (path,))
-        self.con.commit()
     
-    def _list_objects(self, path, prefix='', delimiter=None, marker=None, limit=10000, virtual=True, keys=[], until=None):
-        cont_prefix = path + '/'
-        if keys and len(keys) > 0:
-            sql = '''select distinct o.name, o.version_id from (%s) o, metadata m where o.name like ? and
-                        m.version_id = o.version_id and m.key in (%s) order by o.name'''
-            sql = sql % (self._sql_until(until), ', '.join('?' * len(keys)))
-            param = (cont_prefix + prefix + '%',) + tuple(keys)
-        else:
-            sql = 'select name, version_id from (%s) where name like ? order by name'
-            sql = sql % self._sql_until(until)
-            param = (cont_prefix + prefix + '%',)
-        c = self.con.execute(sql, param)
-        objects = [(x[0][len(cont_prefix):], x[1]) for x in c.fetchall()]
-        if delimiter:
-            pseudo_objects = []
-            for x in objects:
-                pseudo_name = x[0]
-                i = pseudo_name.find(delimiter, len(prefix))
-                if not virtual:
-                    # If the delimiter is not found, or the name ends
-                    # with the delimiter's first occurence.
-                    if i == -1 or len(pseudo_name) == i + len(delimiter):
-                        pseudo_objects.append(x)
+    def _is_allowed(self, user, account, container, name, op='read'):
+        if smart_unicode(user) == smart_unicode(account):
+            return True
+        path = '/'.join((account, container, name))
+        if op == 'read' and self._get_public(path):
+            return True
+        perm_path, perms = self._get_permissions(path)
+        
+        # Expand groups.
+        for x in ('read', 'write'):
+            g_perms = set()
+            for y in perms.get(x, []):
+                if ':' in y:
+                    g_account, g_name = y.split(':', 1)
+                    groups = self._get_groups(g_account)
+                    if g_name in groups.keys():
+                        g_perms.update(groups[g_name])
                 else:
-                    # If the delimiter is found, keep up to (and including) the delimiter.
-                    if i != -1:
-                        pseudo_name = pseudo_name[:i + len(delimiter)]
-                    if pseudo_name not in [y[0] for y in pseudo_objects]:
-                        if pseudo_name == x[0]:
-                            pseudo_objects.append(x)
-                        else:
-                            pseudo_objects.append((pseudo_name, None))
-            objects = pseudo_objects
+                    g_perms.add(y)
+            perms[x] = g_perms
         
-        start = 0
-        if marker:
-            try:
-                start = [x[0] for x in objects].index(marker) + 1
-            except ValueError:
-                pass
-        if not limit or limit > 10000:
-            limit = 10000
-        return objects[start:start + limit]
+        user = smart_unicode(user, strings_only=True)
+        if op == 'read' and ('*' in perms['read'] or user in perms['read']):
+            return True
+        if '*' in perms['write'] or user in perms['write']:
+            return True
+        return False
     
-    def _del_version(self, version):
-        sql = 'delete from hashmaps where version_id = ?'
-        self.con.execute(sql, (version,))
-        sql = 'delete from versions where version_id = ?'
-        self.con.execute(sql, (version,))
+    def _can_read(self, user, account, container, name):
+        if not self._is_allowed(user, account, container, name, 'read'):
+            raise NotAllowedError
+    
+    def _can_write(self, user, account, container, name):
+        if not self._is_allowed(user, account, container, name, 'write'):
+            raise NotAllowedError
+    
+    def _allowed_paths(self, user, prefix=None):
+        sql = '''select distinct name from permissions where (user = ?
+                    or user in (select account || ':' || gname from groups where user = ?))'''
+        param = (user, user)
+        if prefix:
+            sql += ' and name like ?'
+            param += (prefix + '/%',)
+        c = self.con.execute(sql, param)
+        return [x[0] for x in c.fetchall()]
+    
+    def _allowed_accounts(self, user):
+        allow = set()
+        for path in self._allowed_paths(user):
+            allow.add(path.split('/', 1)[0])
+        return sorted(allow)
+    
+    def _allowed_containers(self, user, account):
+        allow = set()
+        for path in self._allowed_paths(user, account):
+            allow.add(path.split('/', 2)[1])
+        return sorted(allow)
+    
+    def _shared_paths(self, prefix):
+        sql = 'select distinct name from permissions where name like ?'
+        c = self.con.execute(sql, (prefix + '/%',))
+        return [x[0] for x in c.fetchall()]