2015-02-24 22:07:00 +00:00
|
|
|
package asm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/vbatts/tar-split/tar/storage"
|
|
|
|
)
|
|
|
|
|
2015-02-25 17:56:40 +00:00
|
|
|
func NewTarStream(fg FileGetter, up storage.Unpacker) io.ReadCloser {
|
2015-02-24 22:07:00 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
entry, err := up.Next()
|
|
|
|
if err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch entry.Type {
|
|
|
|
case storage.SegmentType:
|
|
|
|
if _, err := pw.Write(entry.Payload); err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case storage.FileType:
|
2015-02-25 17:56:40 +00:00
|
|
|
if entry.Size == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fh, err := fg.Get(entry.Name)
|
|
|
|
if err != nil {
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
if _, err := io.Copy(pw, fh); err != nil {
|
2015-02-24 22:07:00 +00:00
|
|
|
pw.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return pr
|
|
|
|
}
|