pack: Refactor Finalize()

This commit is contained in:
Alexander Neumann 2015-04-30 00:36:36 +02:00
parent f889c5e1ed
commit 998887a5d7
1 changed files with 24 additions and 15 deletions

View File

@ -130,21 +130,10 @@ func (p *Packer) Finalize() (bytesWritten uint, err error) {
// create writer to encrypt header // create writer to encrypt header
wr := crypto.EncryptTo(p.k, p.hw) wr := crypto.EncryptTo(p.k, p.hw)
// write header bytesHeader, err := p.writeHeader(wr)
for _, b := range p.blobs { if err != nil {
entry := headerEntry{ wr.Close()
Type: b.Type, return bytesWritten + bytesHeader, err
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
} }
// finalize encrypted header // finalize encrypted header
@ -168,6 +157,26 @@ func (p *Packer) Finalize() (bytesWritten uint, err error) {
return bytesWritten, nil 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. // ID returns the ID of all data written so far.
func (p *Packer) ID() backend.ID { func (p *Packer) ID() backend.ID {
p.m.Lock() p.m.Lock()