2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

Remove zlib compression

This commit is contained in:
Alexander Neumann 2015-03-02 09:56:56 +01:00
parent b4cb75693e
commit c056382c9c
2 changed files with 6 additions and 61 deletions

View File

@ -113,10 +113,10 @@ Snapshots
A snapshots represents a directory with all files and sub-directories at a A snapshots represents a directory with all files and sub-directories at a
given point in time. For each backup that is made, a new snapshot is created. A given point in time. For each backup that is made, a new snapshot is created. A
snapshot is a zlib-compressed JSON document that is stored in an encrypted file snapshot is a JSON document that is stored in an encrypted file below the
below the directory `snapshots` in the repository. The filename is the SHA-256 directory `snapshots` in the repository. The filename is the SHA-256 hash of
hash of the (encrypted) contents. This string is unique and used within restic the (encrypted) contents. This string is unique and used within restic to
to uniquely identify a snapshot. uniquely identify a snapshot.
The command `restic cat snapshot` can be used as follows to decrypt and The command `restic cat snapshot` can be used as follows to decrypt and
pretty-print the contents of a snapshot file: pretty-print the contents of a snapshot file:

View File

@ -1,9 +1,6 @@
package restic package restic
import ( import (
"bytes"
"compress/flate"
"compress/zlib"
"crypto/sha256" "crypto/sha256"
"encoding/json" "encoding/json"
"errors" "errors"
@ -109,26 +106,6 @@ func (s Server) LoadJSON(t backend.Type, blob Blob, item interface{}) error {
return s.LoadJSONID(t, blob.Storage, item) return s.LoadJSONID(t, blob.Storage, item)
} }
var (
zEmptyString = []byte("x\x9C\x03\x00\x00\x00\x00\x01")
)
var zReaderPool = sync.Pool{
New: func() interface{} {
rd, err := zlib.NewReader(bytes.NewReader(zEmptyString))
if err != nil {
// shouldn't happen
panic(err)
}
return rd
},
}
type zReader interface {
io.ReadCloser
zlib.Resetter
}
// LoadJSONID calls Load() to get content from the backend and afterwards calls // LoadJSONID calls Load() to get content from the backend and afterwards calls
// json.Unmarshal on the item. // json.Unmarshal on the item.
func (s Server) LoadJSONID(t backend.Type, storageID backend.ID, item interface{}) error { func (s Server) LoadJSONID(t backend.Type, storageID backend.ID, item interface{}) error {
@ -146,21 +123,8 @@ func (s Server) LoadJSONID(t backend.Type, storageID backend.ID, item interface{
return err return err
} }
// unzip
br := decryptRd.(flate.Reader)
unzipRd := zReaderPool.Get().(zReader)
err = unzipRd.Reset(br, nil)
defer func() {
unzipRd.Close()
zReaderPool.Put(unzipRd)
}()
if err != nil {
return err
}
// decode // decode
decoder := json.NewDecoder(unzipRd) decoder := json.NewDecoder(decryptRd)
err = decoder.Decode(item) err = decoder.Decode(item)
if err != nil { if err != nil {
return err return err
@ -275,12 +239,6 @@ func (s Server) SaveFrom(t backend.Type, id backend.ID, length uint, rd io.Reade
}, nil }, nil
} }
var zWriterPool = sync.Pool{
New: func() interface{} {
return zlib.NewWriter(nil)
},
}
// SaveJSON serialises item as JSON and encrypts and saves it in the backend as // SaveJSON serialises item as JSON and encrypts and saves it in the backend as
// type t. // type t.
func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) { func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
@ -290,14 +248,7 @@ func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
} }
encWr := s.key.EncryptTo(backendBlob) encWr := s.key.EncryptTo(backendBlob)
wr := zWriterPool.Get().(*zlib.Writer) hw := backend.NewHashingWriter(encWr, sha256.New())
defer zWriterPool.Put(wr)
wr.Reset(encWr)
if err != nil {
return Blob{}, fmt.Errorf("zlib.NewWriter: %v", err)
}
hw := backend.NewHashingWriter(wr, sha256.New())
enc := json.NewEncoder(hw) enc := json.NewEncoder(hw)
err = enc.Encode(item) err = enc.Encode(item)
@ -305,12 +256,6 @@ func (s Server) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
return Blob{}, fmt.Errorf("json.NewEncoder: %v", err) return Blob{}, fmt.Errorf("json.NewEncoder: %v", err)
} }
// flush zlib writer
err = wr.Close()
if err != nil {
return Blob{}, fmt.Errorf("zlib.Writer.Close(): %v", err)
}
// finish encryption // finish encryption
err = encWr.Close() err = encWr.Close()
if err != nil { if err != nil {