summaryrefslogtreecommitdiff
path: root/src/leap/soledad/backends
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/soledad/backends')
-rw-r--r--src/leap/soledad/backends/leap_backend.py25
-rw-r--r--src/leap/soledad/backends/sqlcipher.py26
2 files changed, 43 insertions, 8 deletions
diff --git a/src/leap/soledad/backends/leap_backend.py b/src/leap/soledad/backends/leap_backend.py
index 7e98dd45..ec26dca4 100644
--- a/src/leap/soledad/backends/leap_backend.py
+++ b/src/leap/soledad/backends/leap_backend.py
@@ -8,7 +8,6 @@ from u1db.remote import utils
from u1db.remote.http_target import HTTPSyncTarget
from u1db.remote.http_database import HTTPDatabase
from u1db.errors import BrokenSyncStream
-from leap.soledad.util import GPGWrapper
import uuid
@@ -29,9 +28,10 @@ class LeapDocument(Document):
"""
def __init__(self, doc_id=None, rev=None, json='{}', has_conflicts=False,
- encrypted_json=None, soledad=None):
+ encrypted_json=None, soledad=None, syncable=True):
super(LeapDocument, self).__init__(doc_id, rev, json, has_conflicts)
self._soledad = soledad
+ self._syncable = syncable
if encrypted_json:
self.set_encrypted_json(encrypted_json)
@@ -55,6 +55,18 @@ class LeapDocument(Document):
plaintext = self._soledad.decrypt_symmetric(self.doc_id, ciphertext)
return self.set_json(plaintext)
+ def _get_syncable(self):
+ return self._syncable
+
+ def _set_syncable(self, syncable=True):
+ self._syncable = syncable
+
+ syncable = property(
+ _get_syncable,
+ _set_syncable,
+ doc="Determine if document should be synced with server."
+ )
+
class LeapDatabase(HTTPDatabase):
"""Implement the HTTP remote database API to a Leap server."""
@@ -168,10 +180,11 @@ class LeapSyncTarget(HTTPSyncTarget):
ensure=ensure_callback is not None)
comma = ','
for doc, gen, trans_id in docs_by_generations:
- # encrypt before sending to server.
- size += prepare(id=doc.doc_id, rev=doc.rev,
- content=doc.get_encrypted_json(),
- gen=gen, trans_id=trans_id)
+ if doc.syncable:
+ # encrypt before sending to server.
+ size += prepare(id=doc.doc_id, rev=doc.rev,
+ content=doc.get_encrypted_json(),
+ gen=gen, trans_id=trans_id)
entries.append('\r\n]')
size += len(entries[-1])
self._conn.putheader('content-length', str(size))
diff --git a/src/leap/soledad/backends/sqlcipher.py b/src/leap/soledad/backends/sqlcipher.py
index 08b4df43..6cebcf7d 100644
--- a/src/leap/soledad/backends/sqlcipher.py
+++ b/src/leap/soledad/backends/sqlcipher.py
@@ -25,10 +25,11 @@ from u1db.backends.sqlite_backend import (
SQLitePartialExpandDatabase,
)
from u1db import (
- Document,
errors,
)
+from leap.soledad.backends.leap_backend import LeapDocument
+
def open(path, password, create=True, document_factory=None):
"""Open a database at the given location.
@@ -70,7 +71,7 @@ class SQLCipherDatabase(SQLitePartialExpandDatabase):
SQLCipherDatabase.set_pragma_key(self._db_handle, password)
self._real_replica_uid = None
self._ensure_schema()
- self._factory = document_factory or Document
+ self._factory = document_factory or LeapDocument
def _check_if_db_is_encrypted(self, sqlite_file):
if not os.path.exists(sqlite_file):
@@ -133,5 +134,26 @@ class SQLCipherDatabase(SQLitePartialExpandDatabase):
return Synchronizer(self, LeapSyncTarget(url, creds=creds),
soledad=self._soledad).sync(autocreate=autocreate)
+ def _extra_schema_init(self, c):
+ c.execute(
+ 'ALTER TABLE document '
+ 'ADD COLUMN syncable BOOL NOT NULL DEFAULT TRUE')
+
+ def _put_and_update_indexes(self, old_doc, doc):
+ super(SQLCipherDatabase, self)._put_and_update_indexes(old_doc, doc)
+ c = self._db_handle.cursor()
+ c.execute('UPDATE document SET syncable=? WHERE doc_id=?',
+ (doc.syncable, doc.doc_id))
+
+ def _get_doc(self, doc_id, check_for_conflicts=False):
+ doc = super(SQLCipherDatabase, self)._get_doc(doc_id,
+ check_for_conflicts)
+ if doc:
+ c = self._db_handle.cursor()
+ c.execute('SELECT syncable FROM document WHERE doc_id=?',
+ (doc.doc_id,))
+ doc.syncable = bool(c.fetchone()[0])
+ return doc
+
SQLiteDatabase.register_implementation(SQLCipherDatabase)