2010-10-17 20:04:50 +00:00
|
|
|
import msgpack
|
2010-10-20 19:08:46 +00:00
|
|
|
import os
|
2010-10-21 19:21:43 +00:00
|
|
|
|
2010-10-25 17:51:47 +00:00
|
|
|
from . import NS_ARCHIVE_CHUNKS, NS_CHUNK
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Cache(object):
|
|
|
|
"""Client Side cache
|
|
|
|
"""
|
|
|
|
|
2010-10-31 21:21:59 +00:00
|
|
|
def __init__(self, store, keychain):
|
2010-03-06 17:25:35 +00:00
|
|
|
self.store = store
|
2010-10-31 21:21:59 +00:00
|
|
|
self.keychain = keychain
|
2010-11-09 19:49:21 +00:00
|
|
|
self.path = os.path.join(Cache.cache_dir_path(),
|
2010-10-26 19:25:25 +00:00
|
|
|
'%s.cache' % self.store.id.encode('hex'))
|
2010-03-06 17:25:35 +00:00
|
|
|
self.tid = -1
|
|
|
|
self.open()
|
|
|
|
if self.tid != self.store.tid:
|
2010-10-27 17:30:21 +00:00
|
|
|
self.init()
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-11-09 19:49:21 +00:00
|
|
|
@staticmethod
|
|
|
|
def cache_dir_path():
|
|
|
|
"""Return path to directory used for storing users cache files"""
|
|
|
|
return os.path.join(os.path.expanduser('~'), '.darc', 'cache')
|
|
|
|
|
2010-03-06 17:25:35 +00:00
|
|
|
def open(self):
|
|
|
|
if not os.path.exists(self.path):
|
|
|
|
return
|
2010-10-27 17:30:21 +00:00
|
|
|
with open(self.path, 'rb') as fd:
|
2010-10-31 21:21:59 +00:00
|
|
|
data, hash = self.keychain.decrypt(fd.read())
|
2010-10-27 17:30:21 +00:00
|
|
|
cache = msgpack.unpackb(data)
|
2010-10-25 20:31:18 +00:00
|
|
|
assert cache['version'] == 1
|
2010-10-26 18:50:30 +00:00
|
|
|
self.chunk_counts = cache['chunk_counts']
|
|
|
|
self.file_chunks = cache['file_chunks']
|
2010-10-21 19:30:10 +00:00
|
|
|
self.tid = cache['tid']
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-10-27 17:30:21 +00:00
|
|
|
def init(self):
|
2010-03-06 17:25:35 +00:00
|
|
|
"""Initializes cache by fetching and reading all archive indicies
|
|
|
|
"""
|
2010-10-30 11:44:25 +00:00
|
|
|
print 'Initializing cache...'
|
2010-10-26 18:50:30 +00:00
|
|
|
self.chunk_counts = {}
|
|
|
|
self.file_chunks = {}
|
2010-03-06 17:25:35 +00:00
|
|
|
self.tid = self.store.tid
|
|
|
|
if self.store.tid == 0:
|
|
|
|
return
|
2010-10-25 17:51:47 +00:00
|
|
|
for id in list(self.store.list(NS_ARCHIVE_CHUNKS)):
|
2010-10-31 21:21:59 +00:00
|
|
|
data, hash = self.keychain.decrypt(self.store.get(NS_ARCHIVE_CHUNKS, id))
|
2010-10-24 18:18:18 +00:00
|
|
|
cindex = msgpack.unpackb(data)
|
2010-10-21 19:21:43 +00:00
|
|
|
for id, size in cindex['chunks']:
|
2010-10-20 18:28:29 +00:00
|
|
|
try:
|
2010-10-26 18:50:30 +00:00
|
|
|
count, size = self.chunk_counts[id]
|
|
|
|
self.chunk_counts[id] = count + 1, size
|
2010-10-20 18:28:29 +00:00
|
|
|
except KeyError:
|
2010-10-26 18:50:30 +00:00
|
|
|
self.chunk_counts[id] = 1, size
|
2010-10-17 20:04:50 +00:00
|
|
|
self.save()
|
|
|
|
|
2010-10-27 19:01:57 +00:00
|
|
|
def filter_file_chunks(self):
|
|
|
|
for key, value in self.file_chunks.iteritems():
|
|
|
|
if value[0] < 8:
|
|
|
|
yield key, (value[0] + 1,) + value[1:]
|
|
|
|
|
2010-03-06 17:25:35 +00:00
|
|
|
def save(self):
|
2010-11-23 13:46:53 +00:00
|
|
|
for id, (count, size) in self.chunk_counts.iteritems():
|
|
|
|
if count > 1000000:
|
|
|
|
self.chunk_counts[id] = count - 1000000, size
|
|
|
|
|
2010-10-21 19:30:10 +00:00
|
|
|
cache = {'version': 1,
|
2010-10-21 19:21:43 +00:00
|
|
|
'tid': self.store.tid,
|
2010-10-26 18:50:30 +00:00
|
|
|
'chunk_counts': self.chunk_counts,
|
2010-10-27 19:01:57 +00:00
|
|
|
'file_chunks': dict(self.filter_file_chunks()),
|
2010-10-21 19:21:43 +00:00
|
|
|
}
|
2010-10-31 21:21:59 +00:00
|
|
|
data, hash = self.keychain.encrypt_create(msgpack.packb(cache))
|
2010-03-06 17:25:35 +00:00
|
|
|
cachedir = os.path.dirname(self.path)
|
|
|
|
if not os.path.exists(cachedir):
|
|
|
|
os.makedirs(cachedir)
|
|
|
|
with open(self.path, 'wb') as fd:
|
2010-10-21 19:30:10 +00:00
|
|
|
fd.write(data)
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-10-27 17:30:21 +00:00
|
|
|
def add_chunk(self, id, data):
|
2010-03-15 20:23:34 +00:00
|
|
|
if self.seen_chunk(id):
|
|
|
|
return self.chunk_incref(id)
|
2010-10-31 21:21:59 +00:00
|
|
|
data, hash = self.keychain.encrypt_read(data)
|
2010-03-15 20:23:34 +00:00
|
|
|
csize = len(data)
|
2010-10-25 17:51:47 +00:00
|
|
|
self.store.put(NS_CHUNK, id, data)
|
2010-11-23 13:46:53 +00:00
|
|
|
self.chunk_counts[id] = (1000001, csize)
|
|
|
|
return id
|
2010-03-06 17:25:35 +00:00
|
|
|
|
2010-03-15 20:23:34 +00:00
|
|
|
def seen_chunk(self, id):
|
2010-10-26 18:50:30 +00:00
|
|
|
return self.chunk_counts.get(id, (0, 0))[0]
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
def chunk_incref(self, id):
|
2010-10-26 18:50:30 +00:00
|
|
|
count, size = self.chunk_counts[id]
|
2010-11-23 13:46:53 +00:00
|
|
|
if count < 1000000:
|
|
|
|
self.chunk_counts[id] = (count + 1000001, size)
|
|
|
|
return id
|
2010-03-06 17:25:35 +00:00
|
|
|
|
|
|
|
def chunk_decref(self, id):
|
2010-10-26 18:50:30 +00:00
|
|
|
count, size = self.chunk_counts[id]
|
2010-03-15 20:23:34 +00:00
|
|
|
if count == 1:
|
2010-10-26 18:50:30 +00:00
|
|
|
del self.chunk_counts[id]
|
2010-10-25 17:51:47 +00:00
|
|
|
self.store.delete(NS_CHUNK, id)
|
2010-03-15 20:23:34 +00:00
|
|
|
else:
|
2010-10-26 18:50:30 +00:00
|
|
|
self.chunk_counts[id] = (count - 1, size)
|
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):
|
|
|
|
entry = self.file_chunks.get(path_hash)
|
2010-10-26 18:50:30 +00:00
|
|
|
if (entry and entry[3] == st.st_mtime
|
|
|
|
and entry[2] == st.st_size and entry[1] == st.st_ino):
|
|
|
|
# reset entry age
|
|
|
|
self.file_chunks[path_hash] = (0,) + entry[1:]
|
2010-10-25 20:31:18 +00:00
|
|
|
return entry[4], entry[2]
|
|
|
|
else:
|
|
|
|
return None, 0
|
|
|
|
|
|
|
|
def memorize_file_chunks(self, path_hash, st, ids):
|
|
|
|
# Entry: Age, inode, size, mtime, chunk ids
|
|
|
|
self.file_chunks[path_hash] = 0, st.st_ino, st.st_size, st.st_mtime, ids
|
2010-10-13 20:07:55 +00:00
|
|
|
|