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 21:12:22 +00:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
2010-02-27 22:23:39 +00:00
|
|
|
from chunker import chunker, checksum
|
2010-02-23 20:34:28 +00:00
|
|
|
from store import Store
|
2010-02-20 17:23:46 +00:00
|
|
|
|
2010-02-27 22:23:39 +00:00
|
|
|
|
|
|
|
CHUNKSIZE = 64 * 1024
|
2010-02-24 19:38:27 +00:00
|
|
|
NS_ARCHIVES = 'ARCHIVES'
|
|
|
|
NS_CHUNKS = 'CHUNKS'
|
2010-02-20 17:23:46 +00:00
|
|
|
|
|
|
|
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-24 19:38:27 +00:00
|
|
|
self.tid = -1
|
2010-02-22 20:29:31 +00:00
|
|
|
self.open()
|
2010-02-23 20:34:28 +00:00
|
|
|
if self.tid != self.store.tid:
|
2010-02-24 19:38:27 +00:00
|
|
|
print self.tid, self.store.tid
|
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-24 19:38:27 +00:00
|
|
|
if self.store.tid == -1:
|
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
|
2010-02-24 22:24:19 +00:00
|
|
|
print 'Loading cache: ', filename, '...'
|
2010-02-22 20:29:31 +00:00
|
|
|
data = cPickle.loads(zlib.decompress(open(filename, 'rb').read()))
|
|
|
|
self.chunkmap = data['chunkmap']
|
2010-02-24 22:24:19 +00:00
|
|
|
self.archives = data['archives']
|
2010-02-22 20:29:31 +00:00
|
|
|
self.tid = data['tid']
|
|
|
|
print 'done'
|
|
|
|
|
|
|
|
def create(self):
|
2010-02-23 20:34:28 +00:00
|
|
|
self.chunkmap = {}
|
2010-02-24 22:24:19 +00:00
|
|
|
self.archives = []
|
2010-02-23 20:34:28 +00:00
|
|
|
self.tid = self.store.tid
|
2010-02-24 19:38:27 +00:00
|
|
|
if self.store.tid == 0:
|
2010-02-23 20:34:28 +00:00
|
|
|
return
|
2010-02-22 20:29:31 +00:00
|
|
|
print 'Recreating cache...'
|
2010-02-24 19:38:27 +00:00
|
|
|
for id in self.store.list(NS_ARCHIVES):
|
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, id)))
|
2010-02-24 22:24:19 +00:00
|
|
|
self.archives.append(archive['name'])
|
2010-02-23 20:34:28 +00:00
|
|
|
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'
|
2010-02-24 22:24:19 +00:00
|
|
|
data = {'chunkmap': self.chunkmap, 'tid': self.store.tid, 'archives': self.archives}
|
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
|
|
|
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):
|
2010-02-24 19:38:27 +00:00
|
|
|
self.store.put(NS_CHUNKS, hash, data)
|
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')
|
2010-02-24 19:38:27 +00:00
|
|
|
self.store.delete(NS_CHUNKS, hash)
|
2010-02-23 20:34:28 +00:00
|
|
|
return count
|
2010-02-20 17:23:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Archiver(object):
|
|
|
|
|
2010-02-23 21:12:22 +00:00
|
|
|
def create_archive(self, archive_name, paths):
|
2010-02-24 22:24:19 +00:00
|
|
|
try:
|
|
|
|
self.store.get(NS_ARCHIVES, archive_name)
|
|
|
|
except Store.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception('Archive "%s" already exists' % archive_name)
|
2010-02-20 21:28:46 +00:00
|
|
|
items = []
|
2010-02-23 21:12:22 +00:00
|
|
|
for path in paths:
|
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for d in dirs:
|
|
|
|
name = os.path.join(root, d)
|
|
|
|
items.append(self.process_dir(name, self.cache))
|
|
|
|
for f in files:
|
|
|
|
name = os.path.join(root, f)
|
|
|
|
items.append(self.process_file(name, self.cache))
|
2010-02-24 19:38:27 +00:00
|
|
|
archive = {'name': archive_name, 'items': items}
|
|
|
|
hash = self.store.put(NS_ARCHIVES, archive_name, zlib.compress(cPickle.dumps(archive)))
|
|
|
|
self.store.commit()
|
2010-02-24 22:24:19 +00:00
|
|
|
self.cache.archives.append(archive_name)
|
2010-02-23 20:34:28 +00:00
|
|
|
self.cache.save()
|
|
|
|
|
|
|
|
def delete_archive(self, archive_name):
|
2010-02-24 22:24:19 +00:00
|
|
|
try:
|
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, archive_name)))
|
|
|
|
except Store.DoesNotExist:
|
|
|
|
raise Exception('Archive "%s" does not exist' % archive_name)
|
2010-02-24 19:38:27 +00:00
|
|
|
self.store.delete(NS_ARCHIVES, archive_name)
|
2010-02-23 20:34:28 +00:00
|
|
|
for item in archive['items']:
|
|
|
|
if item['type'] == 'FILE':
|
|
|
|
for c in item['chunks']:
|
|
|
|
self.cache.chunk_decref(c)
|
2010-02-24 19:38:27 +00:00
|
|
|
self.store.commit()
|
2010-02-24 22:24:19 +00:00
|
|
|
self.cache.archives.remove(archive_name)
|
2010-02-20 21:28:46 +00:00
|
|
|
self.cache.save()
|
|
|
|
|
2010-02-24 22:24:19 +00:00
|
|
|
def list_archives(self):
|
|
|
|
print 'Archives:'
|
|
|
|
for archive in sorted(self.cache.archives):
|
|
|
|
print archive
|
|
|
|
|
|
|
|
def list_archive(self, archive_name):
|
|
|
|
try:
|
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, archive_name)))
|
|
|
|
except Store.DoesNotExist:
|
|
|
|
raise Exception('Archive "%s" does not exist' % archive_name)
|
|
|
|
for item in archive['items']:
|
|
|
|
print item['path']
|
|
|
|
|
|
|
|
def verify_archive(self, archive_name):
|
|
|
|
try:
|
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, archive_name)))
|
|
|
|
except Store.DoesNotExist:
|
|
|
|
raise Exception('Archive "%s" does not exist' % archive_name)
|
|
|
|
for item in archive['items']:
|
|
|
|
if item['type'] == 'FILE':
|
|
|
|
print item['path'], '...',
|
|
|
|
for chunk in item['chunks']:
|
|
|
|
data = self.store.get(NS_CHUNKS, chunk)
|
|
|
|
if hashlib.sha1(data).digest() != chunk:
|
|
|
|
print 'ERROR'
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print 'OK'
|
|
|
|
|
2010-02-27 22:23:39 +00:00
|
|
|
def extract_archive(self, archive_name):
|
|
|
|
try:
|
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, archive_name)))
|
|
|
|
except Store.DoesNotExist:
|
|
|
|
raise Exception('Archive "%s" does not exist' % archive_name)
|
|
|
|
for item in archive['items']:
|
|
|
|
assert item['path'][0] not in ('/', '\\', ':')
|
|
|
|
print item['path']
|
|
|
|
if item['type'] == 'DIR':
|
|
|
|
if not os.path.exists(item['path']):
|
|
|
|
os.makedirs(item['path'])
|
|
|
|
if item['type'] == 'FILE':
|
|
|
|
with open(item['path'], 'wb') as fd:
|
|
|
|
for chunk in item['chunks']:
|
|
|
|
fd.write(zlib.decompress(self.store.get(NS_CHUNKS, chunk)))
|
|
|
|
|
2010-02-20 21:28:46 +00:00
|
|
|
def process_dir(self, path, cache):
|
2010-02-24 22:24:19 +00:00
|
|
|
path = path.lstrip('/\\:')
|
2010-02-20 21:28:46 +00:00
|
|
|
print 'Directory: %s' % (path)
|
|
|
|
return {'type': 'DIR', 'path': path}
|
|
|
|
|
|
|
|
def process_file(self, path, cache):
|
2010-02-27 22:23:39 +00:00
|
|
|
with open(path, 'rb') as fd:
|
|
|
|
size = 0
|
|
|
|
chunks = []
|
|
|
|
for chunk in chunker(fd, CHUNKSIZE, {}):
|
|
|
|
size += len(chunk)
|
|
|
|
chunks.append(cache.add_chunk(zlib.compress(chunk)))
|
2010-02-24 22:24:19 +00:00
|
|
|
path = path.lstrip('/\\:')
|
2010-02-20 21:28:46 +00:00
|
|
|
print 'File: %s (%d chunks)' % (path, len(chunks))
|
|
|
|
return {'type': 'FILE', 'path': path, 'size': size, 'chunks': chunks}
|
2010-02-24 22:24:19 +00:00
|
|
|
|
2010-02-23 21:12:22 +00:00
|
|
|
def run(self):
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-C", "--cache", dest="cache",
|
|
|
|
help="cache directory to use", metavar="CACHE")
|
|
|
|
parser.add_option("-s", "--store", dest="store",
|
|
|
|
help="path to dedupe store", metavar="STORE")
|
|
|
|
parser.add_option("-c", "--create", dest="create_archive",
|
|
|
|
help="create ARCHIVE", metavar="ARCHIVE")
|
|
|
|
parser.add_option("-d", "--delete", dest="delete_archive",
|
|
|
|
help="delete ARCHIVE", metavar="ARCHIVE")
|
|
|
|
parser.add_option("-l", "--list-archives", dest="list_archives",
|
2010-02-24 22:24:19 +00:00
|
|
|
action="store_true", default=False,
|
2010-02-23 21:12:22 +00:00
|
|
|
help="list archives")
|
|
|
|
parser.add_option("-V", "--verify", dest="verify_archive",
|
|
|
|
help="verify archive consistency")
|
|
|
|
parser.add_option("-e", "--extract", dest="extract_archive",
|
|
|
|
help="extract ARCHIVE")
|
|
|
|
parser.add_option("-L", "--list-archive", dest="list_archive",
|
|
|
|
help="verify archive consistency", metavar="ARCHIVE")
|
|
|
|
(options, args) = parser.parse_args()
|
2010-02-24 22:24:19 +00:00
|
|
|
if options.store:
|
|
|
|
self.store = Store(options.store)
|
|
|
|
else:
|
|
|
|
parser.error('No store path specified')
|
|
|
|
if options.cache:
|
|
|
|
self.cache = Cache(options.cache, self.store)
|
|
|
|
else:
|
|
|
|
parser.error('No cache path specified')
|
|
|
|
if options.list_archives:
|
|
|
|
self.list_archives()
|
|
|
|
elif options.list_archive:
|
|
|
|
self.list_archive(options.list_archive)
|
|
|
|
elif options.verify_archive:
|
|
|
|
self.verify_archive(options.verify_archive)
|
2010-02-27 22:23:39 +00:00
|
|
|
elif options.extract_archive:
|
|
|
|
self.extract_archive(options.extract_archive)
|
2010-02-24 22:24:19 +00:00
|
|
|
elif options.delete_archive:
|
2010-02-23 21:12:22 +00:00
|
|
|
self.delete_archive(options.delete_archive)
|
|
|
|
else:
|
|
|
|
self.create_archive(options.create_archive, args)
|
2010-02-20 17:23:46 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
archiver = Archiver()
|
2010-02-23 21:12:22 +00:00
|
|
|
archiver.run()
|
2010-02-20 17:23:46 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|