mirror of https://github.com/restic/restic.git
pack: Refactor Finalize()
This commit is contained in:
parent
f889c5e1ed
commit
998887a5d7
39
pack/pack.go
39
pack/pack.go
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue