|
@@ -1,14 +1,13 @@
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
-import hashlib
|
|
|
|
import logging
|
|
import logging
|
|
import os
|
|
import os
|
|
import stat
|
|
import stat
|
|
import sys
|
|
import sys
|
|
-import zlib
|
|
|
|
|
|
|
|
from .cache import NS_ARCHIVES, NS_CHUNKS, NS_CINDEX
|
|
from .cache import NS_ARCHIVES, NS_CHUNKS, NS_CINDEX
|
|
from .chunkifier import chunkify
|
|
from .chunkifier import chunkify
|
|
-from .helpers import uid2user, user2uid, gid2group, group2gid, pack, unpack
|
|
|
|
|
|
+from .crypt import CryptoManager
|
|
|
|
+from .helpers import uid2user, user2uid, gid2group, group2gid
|
|
|
|
|
|
CHUNK_SIZE = 55001
|
|
CHUNK_SIZE = 55001
|
|
|
|
|
|
@@ -16,29 +15,30 @@ CHUNK_SIZE = 55001
|
|
class Archive(object):
|
|
class Archive(object):
|
|
|
|
|
|
def __init__(self, store, name=None):
|
|
def __init__(self, store, name=None):
|
|
|
|
+ self.crypt = CryptoManager(store)
|
|
self.store = store
|
|
self.store = store
|
|
self.items = []
|
|
self.items = []
|
|
self.chunks = []
|
|
self.chunks = []
|
|
self.chunk_idx = {}
|
|
self.chunk_idx = {}
|
|
self.hard_links = {}
|
|
self.hard_links = {}
|
|
if name:
|
|
if name:
|
|
- self.load(hashlib.sha256(name).digest())
|
|
|
|
|
|
+ self.load(self.crypt.id_hash(name))
|
|
|
|
|
|
def load(self, id):
|
|
def load(self, id):
|
|
self.id = id
|
|
self.id = id
|
|
- archive = unpack(self.store.get(NS_ARCHIVES, self.id))
|
|
|
|
|
|
+ archive = self.crypt.unpack_read(self.store.get(NS_ARCHIVES, self.id))
|
|
if archive['version'] != 1:
|
|
if archive['version'] != 1:
|
|
raise Exception('Archive version %r not supported' % archive['version'])
|
|
raise Exception('Archive version %r not supported' % archive['version'])
|
|
self.items = archive['items']
|
|
self.items = archive['items']
|
|
self.name = archive['name']
|
|
self.name = archive['name']
|
|
- cindex = unpack(self.store.get(NS_CINDEX, self.id))
|
|
|
|
|
|
+ cindex = self.crypt.unpack_create(self.store.get(NS_CINDEX, self.id))
|
|
assert cindex['version'] == 1
|
|
assert cindex['version'] == 1
|
|
self.chunks = cindex['chunks']
|
|
self.chunks = cindex['chunks']
|
|
for i, chunk in enumerate(self.chunks):
|
|
for i, chunk in enumerate(self.chunks):
|
|
self.chunk_idx[i] = chunk[0]
|
|
self.chunk_idx[i] = chunk[0]
|
|
|
|
|
|
def save(self, name):
|
|
def save(self, name):
|
|
- self.id = hashlib.sha256(name).digest()
|
|
|
|
|
|
+ self.id = self.crypt.id_hash(name)
|
|
archive = {
|
|
archive = {
|
|
'version': 1,
|
|
'version': 1,
|
|
'name': name,
|
|
'name': name,
|
|
@@ -46,14 +46,15 @@ class Archive(object):
|
|
'ts': datetime.utcnow().isoformat(),
|
|
'ts': datetime.utcnow().isoformat(),
|
|
'items': self.items,
|
|
'items': self.items,
|
|
}
|
|
}
|
|
- _, data = pack(archive)
|
|
|
|
|
|
+ data = self.crypt.pack_read(archive)
|
|
self.store.put(NS_ARCHIVES, self.id, data)
|
|
self.store.put(NS_ARCHIVES, self.id, data)
|
|
cindex = {
|
|
cindex = {
|
|
'version': 1,
|
|
'version': 1,
|
|
'chunks': self.chunks,
|
|
'chunks': self.chunks,
|
|
}
|
|
}
|
|
- _, data = pack(cindex)
|
|
|
|
|
|
+ data = self.crypt.pack_create(cindex)
|
|
self.store.put(NS_CINDEX, self.id, data)
|
|
self.store.put(NS_CINDEX, self.id, data)
|
|
|
|
+ self.crypt.store_key()
|
|
self.store.commit()
|
|
self.store.commit()
|
|
|
|
|
|
def add_chunk(self, id, size):
|
|
def add_chunk(self, id, size):
|
|
@@ -117,7 +118,7 @@ class Archive(object):
|
|
for chunk in item['chunks']:
|
|
for chunk in item['chunks']:
|
|
id = self.chunk_idx[chunk]
|
|
id = self.chunk_idx[chunk]
|
|
try:
|
|
try:
|
|
- fd.write(unpack(self.store.get(NS_CHUNKS, id)))
|
|
|
|
|
|
+ fd.write(self.crypt.unpack_read(self.store.get(NS_CHUNKS, id)))
|
|
except ValueError:
|
|
except ValueError:
|
|
raise Exception('Invalid chunk checksum')
|
|
raise Exception('Invalid chunk checksum')
|
|
self.restore_stat(path, item)
|
|
self.restore_stat(path, item)
|
|
@@ -145,7 +146,7 @@ class Archive(object):
|
|
for chunk in item['chunks']:
|
|
for chunk in item['chunks']:
|
|
id = self.chunk_idx[chunk]
|
|
id = self.chunk_idx[chunk]
|
|
try:
|
|
try:
|
|
- unpack(self.store.get(NS_CHUNKS, id))
|
|
|
|
|
|
+ self.crypt.unpack_read(self.store.get(NS_CHUNKS, id))
|
|
except ValueError:
|
|
except ValueError:
|
|
logging.error('%s ... ERROR', item['path'])
|
|
logging.error('%s ... ERROR', item['path'])
|
|
break
|
|
break
|
|
@@ -242,12 +243,12 @@ class Archive(object):
|
|
})
|
|
})
|
|
|
|
|
|
def process_chunk(self, data, cache):
|
|
def process_chunk(self, data, cache):
|
|
- id = hashlib.sha256(data).digest()
|
|
|
|
|
|
+ id = self.crypt.id_hash(data)
|
|
try:
|
|
try:
|
|
return self.chunk_idx[id]
|
|
return self.chunk_idx[id]
|
|
except KeyError:
|
|
except KeyError:
|
|
idx = len(self.chunks)
|
|
idx = len(self.chunks)
|
|
- size = cache.add_chunk(id, data)
|
|
|
|
|
|
+ size = cache.add_chunk(id, data, self.crypt)
|
|
self.chunks.append((id, size))
|
|
self.chunks.append((id, size))
|
|
self.chunk_idx[id] = idx
|
|
self.chunk_idx[id] = idx
|
|
return idx
|
|
return idx
|