From c6a8ce7daa7545b6bc19d279e4ff8ab746c06eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarno=20Sepp=C3=A4nen?= Date: Sun, 18 May 2014 12:34:46 +0300 Subject: [PATCH] GPG does zlib compression by default --- gpgfs.py | 13 +++++++------ gpgstore.py | 11 ++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/gpgfs.py b/gpgfs.py index 1f9d490..95e8cd0 100755 --- a/gpgfs.py +++ b/gpgfs.py @@ -34,7 +34,7 @@ def read_index(store, path): write_index(store, path, root) log.info('created %s', path) return root - data = store.get(path) + data = store.get(path, format=gpgstore.FMT_GPG) buf = StringIO(data) if buf.read(len(magic)) != magic: raise IOError, 'index parse error: %s' % path @@ -48,7 +48,7 @@ def write_index(store, path, root): header = '' write_atom(buf, header) 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): # breadth-first @@ -225,7 +225,8 @@ class GpgFs(LoggingMixIn, Operations): now = time.time() encpath = putx('') 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 log.debug('new path %s => %s', path, encpath) self.fd += 1 @@ -282,7 +283,7 @@ class GpgFs(LoggingMixIn, Operations): self.flush(path, 0) ent = self._find(path) 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] def readdir(self, path, fh): @@ -350,7 +351,7 @@ class GpgFs(LoggingMixIn, Operations): if length == 0: buf = '' else: - buf = self.store.get(ent.encpath) + buf = self.store.get(ent.encpath, format=ent.encformat) buf = buf[:length] ent.encpath = putx(buf, ent.encpath) ent.size = length @@ -380,7 +381,7 @@ class GpgFs(LoggingMixIn, Operations): if path != self.write_path: self.flush(self.write_path, None) 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_len = len(buf) self.write_path = path diff --git a/gpgstore.py b/gpgstore.py index cb2304d..55e3e54 100644 --- a/gpgstore.py +++ b/gpgstore.py @@ -2,26 +2,27 @@ import os import gnupg from binascii import hexlify -import zlib import errno import logging log = logging.getLogger('gpgfs') +FMT_GPG = 0 + class GpgStore(object): def __init__(self, encroot, keyid): self.encroot = encroot self.keyid = keyid 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: path = hexlify(os.urandom(20)) path = path[:2] + '/' + path[2:] encdir = self.encroot + '/' + path[:2] if not os.path.exists(encdir): os.mkdir(encdir, 0755) - data = zlib.compress(data, 1) res = self.gpg.encrypt(data, self.keyid, armor=False) if not res.ok: log.error("encryption failed (keyid %s), %s: %s", @@ -41,7 +42,8 @@ class GpgStore(object): log.debug('encrypted %s' % path) return path - def get(self, path): + def get(self, path, format=FMT_GPG): + assert format == FMT_GPG try: data = file(self.encroot + '/' + path).read() except OSError, err: @@ -53,7 +55,6 @@ class GpgStore(object): if not res.ok: log.error("decryption failed, %s: %s", res.status, path) raise OSError(errno.EIO) - data = zlib.decompress(res.data) log.debug('decrypted %s' % path) return data