2013-06-03 11:45:48 +00:00
|
|
|
from configparser import RawConfigParser
|
|
|
|
from itertools import zip_longest
|
2010-10-17 20:04:50 +00:00
|
|
|
import msgpack
|
2010-10-20 19:08:46 +00:00
|
|
|
import os
|
2013-06-26 20:00:23 +00:00
|
|
|
from binascii import hexlify
|
2010-12-21 20:29:09 +00:00
|
|
|
import shutil
|
2010-10-21 19:21:43 +00:00
|
|
|
|
2013-12-15 19:35:29 +00:00
|
|
|
from .helpers import Error, get_cache_dir, decode_dict, st_mtime_ns, unhexlify, UpgradableLock
|
2011-07-30 19:13:48 +00:00
|
|
|
from .hashindex import ChunkIndex
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Cache(object):
|
|
|
|
"""Client Side cache
|
|
|
|
"""
|
|
|
|
|
2013-12-15 19:35:29 +00:00
|
|
|
class RepositoryReplay(Error):
|
|
|
|
"""Cache is newer than repository, refusing to continue"""
|
2013-08-09 21:23:00 +00:00
|
|
|
|
2014-02-16 21:21:18 +00:00
|
|
|
def __init__(self, repository, key, manifest, path=None, sync=True):
|
2013-08-09 21:23:00 +00:00
|
|
|
self.timestamp = None
|
2010-12-21 20:29:09 +00:00
|
|
|
self.txn_active = False
|
2013-06-20 10:44:58 +00:00
|
|
|
self.repository = repository
|
2011-07-30 19:13:48 +00:00
|
|
|
self.key = key
|
2011-09-04 21:02:47 +00:00
|
|
|
self.manifest = manifest
|
2014-02-16 21:21:18 +00:00
|
|
|
self.path = path or os.path.join(get_cache_dir(), hexlify(repository.id).decode('ascii'))
|
2010-12-21 20:29:09 +00:00
|
|
|
if not os.path.exists(self.path):
|
|
|
|
self.create()
|
2010-03-06 17:25:35 +00:00
|
|
|
self.open()
|
2014-02-16 21:21:18 +00:00
|
|
|
if sync and self.manifest.id != self.manifest_id:
|
2013-08-09 21:23:00 +00:00
|
|
|
# If repository is older than the cache something fishy is going on
|
|
|
|
if self.timestamp and self.timestamp > manifest.timestamp:
|
|
|
|
raise self.RepositoryReplay()
|
2010-12-21 20:29:09 +00:00
|
|
|
self.sync()
|
2011-07-30 19:13:48 +00:00
|
|
|
self.commit()
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2013-06-24 20:41:05 +00:00
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
2010-12-21 20:29:09 +00:00
|
|
|
def create(self):
|
2013-08-09 21:23:00 +00:00
|
|
|
"""Create a new empty cache at `path`
|
2010-12-21 20:29:09 +00:00
|
|
|
"""
|
2011-01-04 22:00:39 +00:00
|
|
|
os.makedirs(self.path)
|
2013-06-03 11:45:48 +00:00
|
|
|
with open(os.path.join(self.path, 'README'), 'w') as fd:
|
2013-07-08 21:38:27 +00:00
|
|
|
fd.write('This is an Attic cache')
|
2010-12-21 20:29:09 +00:00
|
|
|
config = RawConfigParser()
|
|
|
|
config.add_section('cache')
|
|
|
|
config.set('cache', 'version', '1')
|
2013-06-20 10:44:58 +00:00
|
|
|
config.set('cache', 'repository', hexlify(self.repository.id).decode('ascii'))
|
2011-08-15 20:32:26 +00:00
|
|
|
config.set('cache', 'manifest', '')
|
2013-06-03 11:45:48 +00:00
|
|
|
with open(os.path.join(self.path, 'config'), 'w') as fd:
|
2010-12-21 20:29:09 +00:00
|
|
|
config.write(fd)
|
2013-06-03 11:45:48 +00:00
|
|
|
ChunkIndex.create(os.path.join(self.path, 'chunks').encode('utf-8'))
|
|
|
|
with open(os.path.join(self.path, 'files'), 'w') as fd:
|
2012-10-17 09:40:23 +00:00
|
|
|
pass # empty file
|
2010-12-21 20:29:09 +00:00
|
|
|
|
2010-03-06 17:25:35 +00:00
|
|
|
def open(self):
|
2010-12-21 20:29:09 +00:00
|
|
|
if not os.path.isdir(self.path):
|
2013-07-08 21:38:27 +00:00
|
|
|
raise Exception('%s Does not look like an Attic cache' % self.path)
|
2013-12-14 19:44:20 +00:00
|
|
|
self.lock = UpgradableLock(os.path.join(self.path, 'config'), exclusive=True)
|
2010-12-21 20:29:09 +00:00
|
|
|
self.rollback()
|
|
|
|
self.config = RawConfigParser()
|
|
|
|
self.config.read(os.path.join(self.path, 'config'))
|
|
|
|
if self.config.getint('cache', 'version') != 1:
|
2013-07-08 21:38:27 +00:00
|
|
|
raise Exception('%s Does not look like an Attic cache')
|
2013-06-20 10:44:58 +00:00
|
|
|
self.id = self.config.get('cache', 'repository')
|
2013-06-26 20:00:23 +00:00
|
|
|
self.manifest_id = unhexlify(self.config.get('cache', 'manifest'))
|
2013-08-09 21:23:00 +00:00
|
|
|
self.timestamp = self.config.get('cache', 'timestamp', fallback=None)
|
2013-06-03 11:45:48 +00:00
|
|
|
self.chunks = ChunkIndex(os.path.join(self.path, 'chunks').encode('utf-8'))
|
2011-07-06 20:11:01 +00:00
|
|
|
self.files = None
|
2011-07-02 18:39:35 +00:00
|
|
|
|
2013-06-24 20:41:05 +00:00
|
|
|
def close(self):
|
2013-12-14 19:44:20 +00:00
|
|
|
self.lock.release()
|
2013-06-24 20:41:05 +00:00
|
|
|
|
2011-07-02 18:39:35 +00:00
|
|
|
def _read_files(self):
|
2011-07-06 20:23:41 +00:00
|
|
|
self.files = {}
|
2012-03-01 21:35:11 +00:00
|
|
|
self._newest_mtime = 0
|
2010-12-21 20:29:09 +00:00
|
|
|
with open(os.path.join(self.path, 'files'), 'rb') as fd:
|
2013-05-17 12:30:39 +00:00
|
|
|
u = msgpack.Unpacker(use_list=True)
|
2010-12-21 20:29:09 +00:00
|
|
|
while True:
|
|
|
|
data = fd.read(64 * 1024)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
u.feed(data)
|
|
|
|
for hash, item in u:
|
2013-05-17 12:30:39 +00:00
|
|
|
item[0] += 1
|
|
|
|
self.files[hash] = item
|
2010-12-21 20:29:09 +00:00
|
|
|
|
|
|
|
def begin_txn(self):
|
|
|
|
# Initialize transaction snapshot
|
|
|
|
txn_dir = os.path.join(self.path, 'txn.tmp')
|
|
|
|
os.mkdir(txn_dir)
|
|
|
|
shutil.copy(os.path.join(self.path, 'config'), txn_dir)
|
|
|
|
shutil.copy(os.path.join(self.path, 'chunks'), txn_dir)
|
|
|
|
shutil.copy(os.path.join(self.path, 'files'), txn_dir)
|
|
|
|
os.rename(os.path.join(self.path, 'txn.tmp'),
|
|
|
|
os.path.join(self.path, 'txn.active'))
|
|
|
|
self.txn_active = True
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
"""Commit transaction
|
|
|
|
"""
|
2011-01-04 22:16:55 +00:00
|
|
|
if not self.txn_active:
|
|
|
|
return
|
2011-07-06 20:11:01 +00:00
|
|
|
if self.files is not None:
|
|
|
|
with open(os.path.join(self.path, 'files'), 'wb') as fd:
|
2013-06-03 11:45:48 +00:00
|
|
|
for item in self.files.items():
|
2012-03-01 21:35:11 +00:00
|
|
|
# Discard cached files with the newest mtime to avoid
|
|
|
|
# issues with filesystem snapshots and mtime precision
|
2013-05-17 12:30:39 +00:00
|
|
|
if item[1][0] < 10 and item[1][3] < self._newest_mtime:
|
2012-03-01 21:35:11 +00:00
|
|
|
msgpack.pack(item, fd)
|
2013-06-03 11:45:48 +00:00
|
|
|
self.config.set('cache', 'manifest', hexlify(self.manifest.id).decode('ascii'))
|
2013-08-09 21:23:00 +00:00
|
|
|
self.config.set('cache', 'timestamp', self.manifest.timestamp)
|
2010-12-21 20:29:09 +00:00
|
|
|
with open(os.path.join(self.path, 'config'), 'w') as fd:
|
|
|
|
self.config.write(fd)
|
|
|
|
self.chunks.flush()
|
|
|
|
os.rename(os.path.join(self.path, 'txn.active'),
|
|
|
|
os.path.join(self.path, 'txn.tmp'))
|
|
|
|
shutil.rmtree(os.path.join(self.path, 'txn.tmp'))
|
|
|
|
self.txn_active = False
|
|
|
|
|
|
|
|
def rollback(self):
|
|
|
|
"""Roll back partial and aborted transactions
|
|
|
|
"""
|
|
|
|
# Roll back active transaction
|
|
|
|
txn_dir = os.path.join(self.path, 'txn.active')
|
|
|
|
if os.path.exists(txn_dir):
|
|
|
|
shutil.copy(os.path.join(txn_dir, 'config'), self.path)
|
|
|
|
shutil.copy(os.path.join(txn_dir, 'chunks'), self.path)
|
|
|
|
shutil.copy(os.path.join(txn_dir, 'files'), self.path)
|
2011-08-14 12:50:56 +00:00
|
|
|
os.rename(txn_dir, os.path.join(self.path, 'txn.tmp'))
|
|
|
|
# Remove partial transaction
|
|
|
|
if os.path.exists(os.path.join(self.path, 'txn.tmp')):
|
|
|
|
shutil.rmtree(os.path.join(self.path, 'txn.tmp'))
|
2010-12-21 20:29:09 +00:00
|
|
|
self.txn_active = False
|
|
|
|
|
|
|
|
def sync(self):
|
2010-03-06 17:25:35 +00:00
|
|
|
"""Initializes cache by fetching and reading all archive indicies
|
|
|
|
"""
|
2012-11-28 20:14:54 +00:00
|
|
|
def add(id, size, csize):
|
2011-08-15 20:32:26 +00:00
|
|
|
try:
|
|
|
|
count, size, csize = self.chunks[id]
|
|
|
|
self.chunks[id] = count + 1, size, csize
|
|
|
|
except KeyError:
|
2012-11-28 20:14:54 +00:00
|
|
|
self.chunks[id] = 1, size, csize
|
2010-12-21 20:29:09 +00:00
|
|
|
self.begin_txn()
|
2013-06-03 11:45:48 +00:00
|
|
|
print('Initializing cache...')
|
2011-06-18 09:25:29 +00:00
|
|
|
self.chunks.clear()
|
2011-07-30 19:13:48 +00:00
|
|
|
unpacker = msgpack.Unpacker()
|
2011-09-04 21:02:47 +00:00
|
|
|
for name, info in self.manifest.archives.items():
|
2013-06-03 11:45:48 +00:00
|
|
|
id = info[b'id']
|
2013-06-20 10:44:58 +00:00
|
|
|
cdata = self.repository.get(id)
|
2011-08-15 20:32:26 +00:00
|
|
|
data = self.key.decrypt(id, cdata)
|
2012-11-28 20:14:54 +00:00
|
|
|
add(id, len(data), len(cdata))
|
2011-07-30 19:13:48 +00:00
|
|
|
archive = msgpack.unpackb(data)
|
2014-01-22 21:10:36 +00:00
|
|
|
if archive[b'version'] != 1:
|
|
|
|
raise Exception('Unknown archive metadata version')
|
2013-06-03 11:45:48 +00:00
|
|
|
decode_dict(archive, (b'name', b'hostname', b'username', b'time')) # fixme: argv
|
|
|
|
print('Analyzing archive:', archive[b'name'])
|
2014-01-22 19:58:48 +00:00
|
|
|
for id_, chunk in zip_longest(archive[b'items'], self.repository.get_many(archive[b'items'])):
|
|
|
|
data = self.key.decrypt(id_, chunk)
|
|
|
|
add(id_, len(data), len(chunk))
|
2012-11-28 20:14:54 +00:00
|
|
|
unpacker.feed(data)
|
2012-10-17 09:40:23 +00:00
|
|
|
for item in unpacker:
|
2014-01-22 19:58:48 +00:00
|
|
|
if b'chunks' in item:
|
|
|
|
for id_, size, csize in item[b'chunks']:
|
|
|
|
add(id_, size, csize)
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2011-08-07 15:10:21 +00:00
|
|
|
def add_chunk(self, id, data, stats):
|
2010-12-21 20:29:09 +00:00
|
|
|
if not self.txn_active:
|
|
|
|
self.begin_txn()
|
2010-03-15 20:23:34 +00:00
|
|
|
if self.seen_chunk(id):
|
2011-08-07 15:10:21 +00:00
|
|
|
return self.chunk_incref(id, stats)
|
2011-07-30 19:13:48 +00:00
|
|
|
size = len(data)
|
2011-08-15 20:32:26 +00:00
|
|
|
data = self.key.encrypt(data)
|
2010-03-15 20:23:34 +00:00
|
|
|
csize = len(data)
|
2013-06-20 10:44:58 +00:00
|
|
|
self.repository.put(id, data, wait=False)
|
2011-07-30 19:13:48 +00:00
|
|
|
self.chunks[id] = (1, size, csize)
|
2011-08-07 15:10:21 +00:00
|
|
|
stats.update(size, csize, True)
|
2011-07-30 19:13:48 +00:00
|
|
|
return id, size, csize
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-03-15 20:23:34 +00:00
|
|
|
def seen_chunk(self, id):
|
2011-07-30 19:13:48 +00:00
|
|
|
return self.chunks.get(id, (0, 0, 0))[0]
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2011-08-07 15:10:21 +00:00
|
|
|
def chunk_incref(self, id, stats):
|
2010-12-21 20:29:09 +00:00
|
|
|
if not self.txn_active:
|
|
|
|
self.begin_txn()
|
2011-07-30 19:13:48 +00:00
|
|
|
count, size, csize = self.chunks[id]
|
|
|
|
self.chunks[id] = (count + 1, size, csize)
|
2011-08-07 15:10:21 +00:00
|
|
|
stats.update(size, csize, False)
|
2011-07-30 19:13:48 +00:00
|
|
|
return id, size, csize
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
def chunk_decref(self, id):
|
2010-12-21 20:29:09 +00:00
|
|
|
if not self.txn_active:
|
|
|
|
self.begin_txn()
|
2011-07-30 19:13:48 +00:00
|
|
|
count, size, csize = self.chunks[id]
|
2010-03-15 20:23:34 +00:00
|
|
|
if count == 1:
|
2010-12-21 20:29:09 +00:00
|
|
|
del self.chunks[id]
|
2013-06-20 10:44:58 +00:00
|
|
|
self.repository.delete(id, wait=False)
|
2010-03-15 20:23:34 +00:00
|
|
|
else:
|
2011-07-30 19:13:48 +00:00
|
|
|
self.chunks[id] = (count - 1, size, csize)
|
2010-10-13 20:07:55 +00:00
|
|
|
|
2010-10-25 20:31:18 +00:00
|
|
|
def file_known_and_unchanged(self, path_hash, st):
|
2011-07-06 20:11:01 +00:00
|
|
|
if self.files is None:
|
2011-07-02 18:39:35 +00:00
|
|
|
self._read_files()
|
2010-12-21 20:29:09 +00:00
|
|
|
entry = self.files.get(path_hash)
|
2013-06-15 18:56:27 +00:00
|
|
|
if (entry and entry[3] == st_mtime_ns(st)
|
2010-10-26 18:50:30 +00:00
|
|
|
and entry[2] == st.st_size and entry[1] == st.st_ino):
|
|
|
|
# reset entry age
|
2013-05-17 12:30:39 +00:00
|
|
|
self.files[path_hash][0] = 0
|
2011-07-30 19:13:48 +00:00
|
|
|
return entry[4]
|
2010-10-25 20:31:18 +00:00
|
|
|
else:
|
2011-07-30 19:13:48 +00:00
|
|
|
return None
|
2010-10-25 20:31:18 +00:00
|
|
|
|
2010-12-21 20:29:09 +00:00
|
|
|
def memorize_file(self, path_hash, st, ids):
|
2010-10-25 20:31:18 +00:00
|
|
|
# Entry: Age, inode, size, mtime, chunk ids
|
2013-06-15 18:56:27 +00:00
|
|
|
mtime_ns = st_mtime_ns(st)
|
|
|
|
self.files[path_hash] = 0, st.st_ino, st.st_size, mtime_ns, ids
|
|
|
|
self._newest_mtime = max(self._newest_mtime, mtime_ns)
|