Adding seeker implementation to file

This commit is contained in:
Alex Yatskov 2015-12-20 23:18:58 +09:00
parent c0a6e3de95
commit 046249a524
3 changed files with 20 additions and 10 deletions

View File

@ -23,6 +23,7 @@
package goldsmith
import (
"os"
"runtime"
"sync"
)
@ -66,7 +67,9 @@ func (ctx *context) chain(p Plugin) {
if proc == nil || accept != nil && !accept.Accept(ctx, f) {
ctx.output <- f
} else {
f.rewind()
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
ctx.gs.fault(f, err)
}
if err := proc.Process(ctx, f); err != nil {
ctx.gs.fault(f, err)
}

24
file.go
View File

@ -38,14 +38,6 @@ type file struct {
asset string
}
func (f *file) rewind() {
if f.reader != nil {
if _, err := f.reader.Seek(0, os.SEEK_SET); err != nil {
panic(err)
}
}
}
func (f *file) export(dstPath string) error {
if err := os.MkdirAll(path.Dir(dstPath), 0755); err != nil {
return err
@ -68,7 +60,9 @@ func (f *file) export(dstPath string) error {
return err
}
} else {
f.rewind()
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
return err
}
if _, err := f.WriteTo(fw); err != nil {
return err
}
@ -124,3 +118,15 @@ func (f *file) WriteTo(w io.Writer) (int64, error) {
return f.reader.WriteTo(w)
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
if f.reader == nil && offset == 0 && (whence == os.SEEK_SET || whence == os.SEEK_CUR) {
return 0, nil
}
if err := f.cache(); err != nil {
return 0, err
}
return f.reader.Seek(offset, whence)
}

View File

@ -51,6 +51,7 @@ type File interface {
Read(p []byte) (int, error)
WriteTo(w io.Writer) (int64, error)
Seek(offset int64, whence int) (int64, error)
}
func NewFileFromData(path string, data []byte) File {