1
0
Fork 1
mirror of https://github.com/vbatts/tar-split.git synced 2024-12-18 11:36:30 +00:00

tar/asm: finish the buffer FileGetPutter

This commit is contained in:
Vincent Batts 2015-02-27 17:36:24 -05:00
parent 86bf4b98ea
commit 0c9efa4324

View file

@ -1,6 +1,8 @@
package asm
import (
"bytes"
"errors"
"io"
"os"
"path"
@ -13,7 +15,7 @@ type FileGetter interface {
type FilePutter interface {
// Put returns a stream for the provided file path
Put(string, io.Writer) error
Put(string, io.Reader) error
}
type FileGetPutter interface {
@ -40,16 +42,29 @@ type bufferFileGetPutter struct {
}
func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
if _, ok := bfgp.files[name]; !ok {
return nil, errors.New("no such file")
}
b := bytes.NewBuffer(bfgp.files[name])
return &readCloserWrapper{b}, nil
}
type writeCloserWrapper struct {
io.Writer
closer func() error
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) error {
b := bytes.NewBuffer([]byte{})
if _, err := io.Copy(b, r); err != nil {
return err
}
bfgp.files[name] = b.Bytes()
return nil
}
func (w *nopWriteCloser) Close() error { return nil }
type readCloserWrapper struct {
io.Reader
}
func (w *readCloserWrapper) Close() error { return nil }
// NewBufferFileGetPutter is simple in memory FileGetPutter
func NewBufferFileGetPutter() FileGetPutter {
return &bufferFileGetPutter{}
}