1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 00:07:25 +00:00

pack: Refactor Finalize()

This commit is contained in:
Alexander Neumann 2015-04-30 00:36:36 +02:00
parent f889c5e1ed
commit 998887a5d7

View file

@ -130,21 +130,10 @@ func (p *Packer) Finalize() (bytesWritten uint, err error) {
// create writer to encrypt header
wr := crypto.EncryptTo(p.k, p.hw)
// write header
for _, b := range p.blobs {
entry := headerEntry{
Type: b.Type,
Length: b.Length,
}
copy(entry.ID[:], b.ID)
err := binary.Write(wr, binary.LittleEndian, entry)
if err != nil {
wr.Close()
return bytesWritten, err
}
bytesWritten += entrySize
bytesHeader, err := p.writeHeader(wr)
if err != nil {
wr.Close()
return bytesWritten + bytesHeader, err
}
// finalize encrypted header
@ -168,6 +157,26 @@ func (p *Packer) Finalize() (bytesWritten uint, err error) {
return bytesWritten, nil
}
// writeHeader constructs and writes the header to wr.
func (p *Packer) writeHeader(wr io.Writer) (bytesWritten uint, err error) {
for _, b := range p.blobs {
entry := headerEntry{
Type: b.Type,
Length: b.Length,
}
copy(entry.ID[:], b.ID)
err := binary.Write(wr, binary.LittleEndian, entry)
if err != nil {
return bytesWritten, err
}
bytesWritten += entrySize
}
return
}
// ID returns the ID of all data written so far.
func (p *Packer) ID() backend.ID {
p.m.Lock()