borg/dedupstore/archiver.py

163 lines
5.3 KiB
Python
Raw Normal View History

2010-02-20 17:23:46 +00:00
import os
import sys
import hashlib
import zlib
2010-02-20 21:28:46 +00:00
import cPickle
2010-02-23 20:34:28 +00:00
from store import Store
2010-02-20 17:23:46 +00:00
CHUNKSIZE = 256 * 1024
class Cache(object):
"""Client Side cache
"""
2010-02-23 20:34:28 +00:00
def __init__(self, path, store):
self.store = store
2010-02-22 20:29:31 +00:00
self.path = path
2010-02-23 20:34:28 +00:00
self.tid = 'unknown'
2010-02-22 20:29:31 +00:00
self.open()
2010-02-23 20:34:28 +00:00
if self.tid != self.store.tid:
print self.tid.encode('hex'), self.store.tid.encode('hex')
2010-02-22 20:29:31 +00:00
self.create()
2010-02-20 21:28:46 +00:00
2010-02-22 20:29:31 +00:00
def open(self):
2010-02-23 20:34:28 +00:00
if self.store.tid == '':
2010-02-21 09:59:26 +00:00
return
2010-02-23 20:34:28 +00:00
filename = os.path.join(self.path, '%s.cache' % self.store.uuid)
2010-02-22 20:29:31 +00:00
if not os.path.exists(filename):
return
print 'Reading cache: ', filename, '...'
data = cPickle.loads(zlib.decompress(open(filename, 'rb').read()))
self.chunkmap = data['chunkmap']
self.tid = data['tid']
self.archives = data['archives']
print 'done'
2010-02-23 20:34:28 +00:00
def update_manifest(self):
print 'old manifest', self.tid.encode('hex')
if self.tid:
self.chunk_decref(self.tid)
manifest = {'archives': self.archives.values()}
hash = self.add_chunk(zlib.compress(cPickle.dumps(manifest)))
print 'new manifest', hash.encode('hex')
self.store.commit(hash)
2010-02-22 20:29:31 +00:00
def create(self):
2010-02-23 20:34:28 +00:00
self.archives = {}
self.chunkmap = {}
self.tid = self.store.tid
if self.store.tid == '':
return
2010-02-22 20:29:31 +00:00
print 'Recreating cache...'
2010-02-23 20:34:28 +00:00
self.chunk_incref(self.store.tid)
manifest = cPickle.loads(zlib.decompress(self.store.get(self.store.tid)))
for hash in manifest['archives']:
self.chunk_incref(hash)
archive = cPickle.loads(zlib.decompress(self.store.get(hash)))
self.archives[archive['name']] = hash
for item in archive['items']:
2010-02-20 21:28:46 +00:00
if item['type'] == 'FILE':
for c in item['chunks']:
self.chunk_incref(c)
2010-02-22 20:29:31 +00:00
print 'done'
2010-02-20 21:28:46 +00:00
def save(self):
2010-02-23 20:34:28 +00:00
assert self.store.state == Store.OPEN
print 'saving cache'
data = {'chunkmap': self.chunkmap, 'tid': self.store.tid, 'archives': self.archives}
filename = os.path.join(self.path, '%s.cache' % self.store.uuid)
2010-02-22 20:29:31 +00:00
print 'Saving cache as:', filename
with open(filename, 'wb') as fd:
fd.write(zlib.compress(cPickle.dumps(data)))
print 'done'
2010-02-20 17:23:46 +00:00
def add_chunk(self, data):
2010-02-23 20:34:28 +00:00
hash = hashlib.sha1(data).digest()
if not self.seen_chunk(hash):
self.store.put(data, hash)
2010-02-20 17:23:46 +00:00
else:
2010-02-23 20:34:28 +00:00
print 'seen chunk', hash.encode('hex')
self.chunk_incref(hash)
return hash
def seen_chunk(self, hash):
return self.chunkmap.get(hash, 0) > 0
2010-02-20 17:23:46 +00:00
2010-02-23 20:34:28 +00:00
def chunk_incref(self, hash):
self.chunkmap.setdefault(hash, 0)
self.chunkmap[hash] += 1
2010-02-20 17:23:46 +00:00
2010-02-23 20:34:28 +00:00
def chunk_decref(self, hash):
count = self.chunkmap.get(hash, 0) - 1
assert count >= 0
self.chunkmap[hash] = count
if not count:
print 'deleting chunk: ', hash.encode('hex')
self.store.delete(hash)
return count
2010-02-20 17:23:46 +00:00
class Archiver(object):
def __init__(self):
2010-02-23 20:34:28 +00:00
self.store = Store('/tmp/store')
self.cache = Cache('/tmp/cache', self.store)
2010-02-20 17:23:46 +00:00
2010-02-20 21:28:46 +00:00
def create_archive(self, archive_name, path):
if archive_name in self.cache.archives:
raise Exception('Archive "%s" already exists' % archive_name)
items = []
2010-02-20 17:23:46 +00:00
for root, dirs, files in os.walk(path):
2010-02-20 21:28:46 +00:00
for d in dirs:
name = os.path.join(root, d)
items.append(self.process_dir(name, self.cache))
2010-02-20 17:23:46 +00:00
for f in files:
2010-02-20 21:28:46 +00:00
name = os.path.join(root, f)
items.append(self.process_file(name, self.cache))
archive = {'name': name, 'items': items}
2010-02-23 20:34:28 +00:00
hash = self.cache.add_chunk(zlib.compress(cPickle.dumps(archive)))
self.cache.archives[archive_name] = hash
self.cache.update_manifest()
self.cache.save()
def delete_archive(self, archive_name):
hash = self.cache.archives.get(archive_name)
if not hash:
raise Exception('Archive "%s" does not exist' % archive_name)
archive = cPickle.loads(zlib.decompress(self.store.get(hash)))
self.cache.chunk_decref(hash)
for item in archive['items']:
if item['type'] == 'FILE':
for c in item['chunks']:
self.cache.chunk_decref(c)
del self.cache.archives[archive_name]
self.cache.update_manifest()
2010-02-20 21:28:46 +00:00
self.cache.save()
def process_dir(self, path, cache):
print 'Directory: %s' % (path)
return {'type': 'DIR', 'path': path}
def process_file(self, path, cache):
fd = open(path, 'rb')
size = 0
chunks = []
while True:
data = fd.read(CHUNKSIZE)
if not data:
break
size += len(data)
chunks.append(cache.add_chunk(zlib.compress(data)))
print 'File: %s (%d chunks)' % (path, len(chunks))
return {'type': 'FILE', 'path': path, 'size': size, 'chunks': chunks}
2010-02-20 17:23:46 +00:00
def main():
archiver = Archiver()
2010-02-23 20:34:28 +00:00
if sys.argv[1] == 'delete':
archiver.delete_archive(sys.argv[2])
else:
archiver.create_archive(sys.argv[1], sys.argv[2])
2010-02-20 17:23:46 +00:00
if __name__ == '__main__':
main()