mirror of
https://github.com/vbatts/tar-split.git
synced 2024-12-18 19:46:29 +00:00
tar/asm: finish the buffer FileGetPutter
This commit is contained in:
parent
86bf4b98ea
commit
0c9efa4324
1 changed files with 21 additions and 6 deletions
|
@ -1,6 +1,8 @@
|
||||||
package asm
|
package asm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -13,7 +15,7 @@ type FileGetter interface {
|
||||||
|
|
||||||
type FilePutter interface {
|
type FilePutter interface {
|
||||||
// Put returns a stream for the provided file path
|
// Put returns a stream for the provided file path
|
||||||
Put(string, io.Writer) error
|
Put(string, io.Reader) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileGetPutter interface {
|
type FileGetPutter interface {
|
||||||
|
@ -40,16 +42,29 @@ type bufferFileGetPutter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
|
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 {
|
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) error {
|
||||||
io.Writer
|
b := bytes.NewBuffer([]byte{})
|
||||||
closer func() error
|
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 {
|
func NewBufferFileGetPutter() FileGetPutter {
|
||||||
return &bufferFileGetPutter{}
|
return &bufferFileGetPutter{}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue