GPG does zlib compression by default

This commit is contained in:
Jarno Seppänen 2014-05-18 12:34:46 +03:00
parent ec5ba51f98
commit c6a8ce7daa
2 changed files with 13 additions and 11 deletions

View File

@ -34,7 +34,7 @@ def read_index(store, path):
write_index(store, path, root) write_index(store, path, root)
log.info('created %s', path) log.info('created %s', path)
return root return root
data = store.get(path) data = store.get(path, format=gpgstore.FMT_GPG)
buf = StringIO(data) buf = StringIO(data)
if buf.read(len(magic)) != magic: if buf.read(len(magic)) != magic:
raise IOError, 'index parse error: %s' % path raise IOError, 'index parse error: %s' % path
@ -48,7 +48,7 @@ def write_index(store, path, root):
header = '' header = ''
write_atom(buf, header) write_atom(buf, header)
write_dict(buf, root) write_dict(buf, root)
store.put(buf.getvalue(), path=path) store.put(buf.getvalue(), path=path, format=gpgstore.FMT_GPG)
def write_dict(fd, dct): def write_dict(fd, dct):
# breadth-first # breadth-first
@ -225,7 +225,8 @@ class GpgFs(LoggingMixIn, Operations):
now = time.time() now = time.time()
encpath = putx('') encpath = putx('')
parent.children[name] = Entry(mode=mode, encpath=encpath, size=0, parent.children[name] = Entry(mode=mode, encpath=encpath, size=0,
nlink=1, ctime=now, mtime=now) nlink=1, ctime=now, mtime=now,
encformat=gpgstore.FMT_GPG)
parent.mtime = now parent.mtime = now
log.debug('new path %s => %s', path, encpath) log.debug('new path %s => %s', path, encpath)
self.fd += 1 self.fd += 1
@ -282,7 +283,7 @@ class GpgFs(LoggingMixIn, Operations):
self.flush(path, 0) self.flush(path, 0)
ent = self._find(path) ent = self._find(path)
assert ent.mode & stat.S_IFREG assert ent.mode & stat.S_IFREG
data = self.store.get(ent.encpath) data = self.store.get(ent.encpath, format=ent.encformat)
return data[offset:offset + size] return data[offset:offset + size]
def readdir(self, path, fh): def readdir(self, path, fh):
@ -350,7 +351,7 @@ class GpgFs(LoggingMixIn, Operations):
if length == 0: if length == 0:
buf = '' buf = ''
else: else:
buf = self.store.get(ent.encpath) buf = self.store.get(ent.encpath, format=ent.encformat)
buf = buf[:length] buf = buf[:length]
ent.encpath = putx(buf, ent.encpath) ent.encpath = putx(buf, ent.encpath)
ent.size = length ent.size = length
@ -380,7 +381,7 @@ class GpgFs(LoggingMixIn, Operations):
if path != self.write_path: if path != self.write_path:
self.flush(self.write_path, None) self.flush(self.write_path, None)
ent = self._find(path) ent = self._find(path)
buf = self.store.get(ent.encpath) buf = self.store.get(ent.encpath, format=ent.encformat)
self.write_buf = [buf] self.write_buf = [buf]
self.write_len = len(buf) self.write_len = len(buf)
self.write_path = path self.write_path = path

View File

@ -2,26 +2,27 @@
import os import os
import gnupg import gnupg
from binascii import hexlify from binascii import hexlify
import zlib
import errno import errno
import logging import logging
log = logging.getLogger('gpgfs') log = logging.getLogger('gpgfs')
FMT_GPG = 0
class GpgStore(object): class GpgStore(object):
def __init__(self, encroot, keyid): def __init__(self, encroot, keyid):
self.encroot = encroot self.encroot = encroot
self.keyid = keyid self.keyid = keyid
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
def put(self, data, path=None): def put(self, data, path=None, format=FMT_GPG):
assert format == FMT_GPG
if not path: if not path:
path = hexlify(os.urandom(20)) path = hexlify(os.urandom(20))
path = path[:2] + '/' + path[2:] path = path[:2] + '/' + path[2:]
encdir = self.encroot + '/' + path[:2] encdir = self.encroot + '/' + path[:2]
if not os.path.exists(encdir): if not os.path.exists(encdir):
os.mkdir(encdir, 0755) os.mkdir(encdir, 0755)
data = zlib.compress(data, 1)
res = self.gpg.encrypt(data, self.keyid, armor=False) res = self.gpg.encrypt(data, self.keyid, armor=False)
if not res.ok: if not res.ok:
log.error("encryption failed (keyid %s), %s: %s", log.error("encryption failed (keyid %s), %s: %s",
@ -41,7 +42,8 @@ class GpgStore(object):
log.debug('encrypted %s' % path) log.debug('encrypted %s' % path)
return path return path
def get(self, path): def get(self, path, format=FMT_GPG):
assert format == FMT_GPG
try: try:
data = file(self.encroot + '/' + path).read() data = file(self.encroot + '/' + path).read()
except OSError, err: except OSError, err:
@ -53,7 +55,6 @@ class GpgStore(object):
if not res.ok: if not res.ok:
log.error("decryption failed, %s: %s", res.status, path) log.error("decryption failed, %s: %s", res.status, path)
raise OSError(errno.EIO) raise OSError(errno.EIO)
data = zlib.decompress(res.data)
log.debug('decrypted %s' % path) log.debug('decrypted %s' % path)
return data return data